pwm_hwtest.c

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