trac#29: Cleanup timer on demo exit
[bertos.git] / bertos / drv / pwm_test.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2005 Develer S.r.l. (http://www.develer.com/)
30  * -->
31  *
32  *
33  * \brief Test for PWM driver (implementation)
34  *
35  * This is a simple test for PWM driver. This module
36  * is target independent, so you can test all target that
37  * BeRTOS support.
38  * To use this test you should include a pwm_map.h header where
39  * are defined the PWM channels for your target. Then you should add
40  * or remove a test setting in pwm_test_cfg array, and edit a value for
41  * your specific test.
42  * Afther this, all is ready and you can test PWM driver.
43  *
44  * The test check first if all PWM channel starts, and then try
45  * to change a PWM duty cicle for all channel.
46  * The change of duty cycle is operate when a PWM channel is enable,
47  * in this way you can see if a pwm signal is clean and work properly.
48  * The duty value is change incrementaly, and when it arrive to 100% or 0%,
49  * we reset the duty value and restart the test.
50  * Further the duty test, we check also a PWM polarity, infact when we
51  * reach a reset duty value, we invert a polary of PWM wavform.
52  * So you can see if the hardware manage correctly this situation.
53  *
54  * Note: To be simple and target independently we not use a timer module,
55  * and so the delay is do with a for cycle.
56  *
57  * \version $Id$
58  *
59  * \author Daniele Basile <asterix@develer.com>
60  */
61
62 #include "hw/pwm_map.h" // For PwmDev and channel avaible on thi target
63 #include "cfg/cfg_pwm.h"
64 #include <cfg/macros.h>
65 #include <cfg/debug.h>
66
67 // Define logging setting (for cfg/log.h module).
68 #define LOG_LEVEL         PWM_LOG_LEVEL
69 #define LOG_VERBOSITY     PWM_LOG_VERBOSITY
70 #include <cfg/log.h>   // for logging system
71
72 #include <cpu/types.h>
73 #include <cpu/irq.h>
74
75 #include <drv/pwm.h>
76 #include CPU_HEADER(pwm)
77
78 #define DELAY_TIME   10000  // This is a number of for cycle before to set a new value of duty
79 #define PWM_DUTY_INC   200  // Incremental value for duty
80
81
82 /**
83  * Simple struct to store
84  * the testing value.
85  */
86 typedef struct PwmTest
87 {
88         int ch;
89         bool pol;
90         pwm_freq_t freq;
91         pwm_duty_t duty;
92 } PwmTest;
93
94 /*
95  * Test settings for each channel.
96  *
97  * Frequency value is in Hz.
98  *
99  * Esample of value for duty cycle"
100  *
101  * - 100% => 0xFFFFFFFF
102  * - 80%  => 0xCCCCCCCC
103  * - 75%  => 0xBFFFFFFF
104  * - 50%  => 0x7FFFFFFF
105  * - 25%  => 0x3FFFFFFF
106  * - 33%  => 0x55555555
107  * - 16%  => 0x2AAAAAAA
108  */
109 static PwmTest pwm_test_cfg[PWM_CNT] =
110 {
111         /* Channel, polarity, frequecy,   duty */
112         {        0,    false,    100UL,      0 }, /*     100Hz,  0% duty */
113         {        1,    false,   1000UL, 0x7FFF }, /*      1KHz, 50% duty */
114         {        2,    false,  12356UL, 0x5555 }, /* 12,356KHz, 33% duty */
115         {        3,    false, 100000UL, 0xCCCC }  /*    100KHz, 80% duty */
116 };
117
118 /**
119  * Setup all needed to test PWM on AT91
120  *
121  */
122 int pwm_testSetUp(void)
123 {
124         LOG_INFO("Init pwm..");
125         pwm_init();
126         LOG_INFO("done.\n");
127
128         return 0;
129 }
130
131
132 /**
133  * Test suit for genation of pwm waveform.
134  *
135  */
136 int pwm_testRun(void)
137 {
138         pwm_duty_t duty = 0;
139         int delay = 0;
140
141         pwm_testSetUp();
142
143         LOG_INFO("\n\n===== BeRTOS PWM test =====\n\n");
144
145         for (int i = 0; i < PWM_CNT; i++)
146         {
147                 LOG_INFO("PWM test ch[%d]\n", pwm_test_cfg[i].ch);
148                 LOG_INFO("--> set pol[%d]", pwm_test_cfg[i].pol);
149                 LOG_INFO("\n(Note: if polarity is false the output waveform start at high level,\n see low level implentation for detail)i\n");
150                 pwm_setPolarity(pwm_test_cfg[i].ch, pwm_test_cfg[i].pol);
151                 LOG_INFO("..ok\n");
152
153                 LOG_INFO("--> set freq[%ld]", pwm_test_cfg[i].freq);
154                 pwm_setFrequency(pwm_test_cfg[i].ch, pwm_test_cfg[i].freq);
155                 LOG_INFO("..ok\n");
156
157                 LOG_INFO("--> set duty[%d]", pwm_test_cfg[i].duty);
158                 pwm_setDuty(pwm_test_cfg[i].ch, pwm_test_cfg[i].duty);
159                 LOG_INFO("..ok\n");
160
161                 LOG_INFO("--> Enable pwm");
162                 pwm_enable(pwm_test_cfg[i].ch, true);
163                 LOG_INFO("..ok\n");
164         }
165
166         LOG_INFO("\n-------------------------- Dinamic PWM test --------------------------\n");
167         LOG_INFO("We test if driver change correctly the duty cycle durind it working.\n");
168         LOG_INFO("On your oscilloscope you should see the pwm singal that increase until\n");
169         LOG_INFO("the duty value is 100%%. After this value we invert a polarity of pwm,\n");
170         LOG_INFO("and repeat the test. But now you should see that pwm duty decreasing until\n");
171         LOG_INFO("0%% duty value.\nAfter that, we repeat the test from beginning.\n\n");
172
173         for (;;)
174         {
175                 if (delay == DELAY_TIME)
176                 {
177                         for (int i = 0; i < PWM_CNT; i++)
178                         {
179                                 LOG_INFO("PWM test ch[%d]\n", pwm_test_cfg[i].ch);
180                                 LOG_INFO("--> set duty[%d]", duty);
181                                 pwm_setDuty(pwm_test_cfg[i].ch, duty);
182                                 LOG_INFO("..ok\n");
183                         }
184                         LOG_INFO("\n++++++++++++++++++++\n");
185                         duty += PWM_DUTY_INC;
186                         delay = 0;
187                 }
188
189                 //Reset duty cycle overflow
190                 if (duty >= (pwm_duty_t)0xFFFF)
191                 {
192                         duty = 0;
193                         for (int i = 0; i < PWM_CNT; i++)
194                         {
195                                 LOG_INFO("Duty reset, swap polarity:\n");
196                                 LOG_INFO("--> pol from [%d] to [%d]", pwm_test_cfg[i].pol, !pwm_test_cfg[i].pol);
197
198                                 pwm_test_cfg[i].pol = !pwm_test_cfg[i].pol;
199                                 pwm_setPolarity(pwm_test_cfg[i].ch, pwm_test_cfg[i].pol);
200
201                                 LOG_INFO("..ok\n");
202                         }
203                         LOG_INFO("\n++++++++++++++++++++\n");
204                 }
205                 delay++;
206         }
207
208                 return 0;
209 }
210
211 /**
212  * End a PWM Test.
213  * (Unused)
214  */
215 int pwm_testTearDown(void)
216 {
217         /*    */
218         return 0;
219 }
220
221 /*
222  * Empty main.
223  *
224  * Look it as exmple or use it if
225  * you want test a PWM driver stand alone.
226  */
227 #if 0
228 int main(void)
229 {
230         IRQ_ENABLE;
231         kdbg_init();
232
233         pwm_testRun();
234
235         for(;;)
236         {
237         }
238
239 }
240 #endif
241
242