BeRTOS
pwm_hwtest.c
Go to the documentation of this file.
00001 
00060 #include "hw/pwm_map.h" // For PwmDev and channel avaible on thi target
00061 #include "cfg/cfg_pwm.h"
00062 #include <cfg/macros.h>
00063 #include <cfg/debug.h>
00064 
00065 // Define logging setting (for cfg/log.h module).
00066 #define LOG_LEVEL         PWM_LOG_LEVEL
00067 #define LOG_FORMAT        PWM_LOG_FORMAT
00068 #include <cfg/log.h>   // for logging system
00069 
00070 #include <cpu/types.h>
00071 #include <cpu/irq.h>
00072 
00073 #include <drv/pwm.h>
00074 #include CPU_HEADER(pwm)
00075 
00076 #define DELAY_TIME   10000  // This is a number of for cycle before to set a new value of duty
00077 #define PWM_DUTY_INC   200  // Incremental value for duty
00078 
00079 
00080 /*
00081  * Simple struct to store
00082  * the testing value.
00083  */
00084 typedef struct PwmTest
00085 {
00086         int ch;
00087         bool pol;
00088         pwm_freq_t freq;
00089         pwm_duty_t duty;
00090 } PwmTest;
00091 
00092 /*
00093  * Test settings for each channel.
00094  *
00095  * Frequency value is in Hz.
00096  *
00097  * Esample of value for duty cycle"
00098  *
00099  * - 100% => 0xFFFFFFFF
00100  * - 80%  => 0xCCCCCCCC
00101  * - 75%  => 0xBFFFFFFF
00102  * - 50%  => 0x7FFFFFFF
00103  * - 25%  => 0x3FFFFFFF
00104  * - 33%  => 0x55555555
00105  * - 16%  => 0x2AAAAAAA
00106  */
00107 static PwmTest pwm_test_cfg[PWM_CNT] =
00108 {
00109         /* Channel, polarity, frequecy,   duty */
00110         {        0,    false,    100UL,      0 }, /*     100Hz,  0% duty */
00111         {        1,    false,   1000UL, 0x7FFF }, /*      1KHz, 50% duty */
00112         {        2,    false,  12356UL, 0x5555 }, /* 12,356KHz, 33% duty */
00113         {        3,    false, 100000UL, 0xCCCC }  /*    100KHz, 80% duty */
00114 };
00115 
00116 /*
00117  * Setup all needed to test PWM on AT91
00118  *
00119  */
00120 int pwm_testSetup(void)
00121 {
00122     LOG_INFO("Init pwm..");
00123     pwm_init();
00124     LOG_INFO("done.\n");
00125 
00126     return 0;
00127 }
00128 
00129 
00130 /*
00131  * Test suit for genation of pwm waveform.
00132  *
00133  */
00134 void NORETURN pwm_testRun(void)
00135 {
00136         pwm_duty_t duty = 0;
00137         int delay = 0;
00138 
00139         pwm_testSetup();
00140 
00141         LOG_INFO("\n\n===== BeRTOS PWM test =====\n\n");
00142 
00143         for (int i = 0; i < PWM_CNT; i++)
00144         {
00145                 LOG_INFO("PWM test ch[%d]\n", pwm_test_cfg[i].ch);
00146                 LOG_INFO("--> set pol[%d]", pwm_test_cfg[i].pol);
00147                 LOG_INFO("\n(Note: if polarity is false the output waveform start at high level,\n see low level implentation for detail)i\n");
00148                 pwm_setPolarity(pwm_test_cfg[i].ch, pwm_test_cfg[i].pol);
00149                 LOG_INFO("..ok\n");
00150 
00151                 LOG_INFO("--> set freq[%ld]", pwm_test_cfg[i].freq);
00152                 pwm_setFrequency(pwm_test_cfg[i].ch, pwm_test_cfg[i].freq);
00153                 LOG_INFO("..ok\n");
00154 
00155                 LOG_INFO("--> set duty[%d]", pwm_test_cfg[i].duty);
00156                 pwm_setDuty(pwm_test_cfg[i].ch, pwm_test_cfg[i].duty);
00157                 LOG_INFO("..ok\n");
00158 
00159                 LOG_INFO("--> Enable pwm");
00160                 pwm_enable(pwm_test_cfg[i].ch, true);
00161                 LOG_INFO("..ok\n");
00162         }
00163 
00164         LOG_INFO("\n-------------------------- Dinamic PWM test --------------------------\n");
00165         LOG_INFO("We test if driver change correctly the duty cycle durind it working.\n");
00166         LOG_INFO("On your oscilloscope you should see the pwm singal that increase until\n");
00167         LOG_INFO("the duty value is 100%%. After this value we invert a polarity of pwm,\n");
00168         LOG_INFO("and repeat the test. But now you should see that pwm duty decreasing until\n");
00169         LOG_INFO("0%% duty value.\nAfter that, we repeat the test from beginning.\n\n");
00170 
00171         for (;;)
00172         {
00173                 if (delay == DELAY_TIME)
00174                 {
00175                         for (int i = 0; i < PWM_CNT; i++)
00176                         {
00177                                 LOG_INFO("PWM test ch[%d]\n", pwm_test_cfg[i].ch);
00178                                 LOG_INFO("--> set duty[%d]", duty);
00179                                 pwm_setDuty(pwm_test_cfg[i].ch, duty);
00180                                 LOG_INFO("..ok\n");
00181                         }
00182                         LOG_INFO("\n++++++++++++++++++++\n");
00183                         duty += PWM_DUTY_INC;
00184                         delay = 0;
00185                 }
00186 
00187                 //Reset duty cycle overflow
00188                 if (duty >= (pwm_duty_t)0xFFFF)
00189                 {
00190                         duty = 0;
00191                         for (int i = 0; i < PWM_CNT; i++)
00192                         {
00193                                 LOG_INFO("Duty reset, swap polarity:\n");
00194                                 LOG_INFO("--> pol from [%d] to [%d]", pwm_test_cfg[i].pol, !pwm_test_cfg[i].pol);
00195 
00196                                 pwm_test_cfg[i].pol = !pwm_test_cfg[i].pol;
00197                                 pwm_setPolarity(pwm_test_cfg[i].ch, pwm_test_cfg[i].pol);
00198 
00199                                 LOG_INFO("..ok\n");
00200                         }
00201                         LOG_INFO("\n++++++++++++++++++++\n");
00202                 }
00203                 delay++;
00204         }
00205 }
00206 
00207 /*
00208  * End a PWM Test.
00209  * (Unused)
00210  */
00211 int pwm_testTearDown(void)
00212 {
00213     /*    */
00214     return 0;
00215 }
00216 
00217 /*
00218  * Empty main.
00219  *
00220  * Look it as exmple or use it if
00221  * you want test a PWM driver stand alone.
00222  */
00223 #if 0
00224 int main(void)
00225 {
00226     IRQ_ENABLE;
00227     kdbg_init();
00228 
00229     pwm_testRun();
00230 
00231     for(;;)
00232     {
00233     }
00234 
00235 }
00236 #endif
00237 
00238