changed for STM32F4

Fork of FastPWM by Erik -

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FastPWM_LPC11XX.cpp Source File

FastPWM_LPC11XX.cpp

00001 #if defined(TARGET_LPC11UXX) || defined(TARGET_LPC11XX_11CXX)
00002 
00003 #include "FastPWM.h"
00004 
00005 #define PWM_MR              (*(((fastpwm_struct*)fast_obj)->MR))   
00006 #define PWM_TIMER           (((fastpwm_struct*)fast_obj)->timer)      
00007 
00008 typedef struct {
00009     uint8_t timer;
00010     uint8_t mr;
00011 } timer_mr;
00012  
00013 #ifdef TARGET_LPC11UXX
00014 typedef struct  {
00015     __IO uint32_t *MR;
00016     LPC_CTxxBx_Type *timer;
00017 } fastpwm_struct;
00018 
00019 static timer_mr pwm_timer_map[11] = {
00020     {0, 0}, {0, 1}, {0, 2},
00021     {1, 0}, {1, 1},
00022     {2, 0}, {2, 1}, {2, 2},
00023     {3, 0}, {3, 1}, {3, 2},
00024 };
00025 
00026 static LPC_CTxxBx_Type *Timers[4] = {
00027     LPC_CT16B0, LPC_CT16B1,
00028     LPC_CT32B0, LPC_CT32B1
00029 };
00030 #else           //LPC11XX
00031 typedef struct  {
00032     __IO uint32_t *MR;
00033     LPC_TMR_TypeDef *timer;
00034 } fastpwm_struct;
00035 
00036 static timer_mr pwm_timer_map[5] = {
00037     {0, 0}, /* CT16B0, MR0 */
00038     {0, 1}, /* CT16B0, MR1 */
00039  
00040     {1, 0}, /* CT16B1, MR0 */
00041     {1, 1}, /* CT16B1, MR1 */
00042  
00043     {2, 2}, /* CT32B0, MR2 */
00044 };
00045 
00046 static LPC_TMR_TypeDef *Timers[3] = {
00047     LPC_TMR16B0, LPC_TMR16B1,
00048     LPC_TMR32B0
00049 };
00050 #endif
00051 
00052 
00053 void FastPWM::initFastPWM( void ) {
00054     fast_obj = new fastpwm_struct;
00055     timer_mr tid = pwm_timer_map[_pwm.pwm];
00056     PWM_TIMER = Timers[tid.timer];
00057     (((fastpwm_struct*)fast_obj)->MR) = &PWM_TIMER->MR[tid.mr];
00058     
00059     if (tid.timer < 2)
00060         //16-bit timer
00061         bits = 16;
00062     else
00063         //32-bit timer
00064         bits = 32;  
00065 }
00066 
00067 void FastPWM::pulsewidth_ticks( uint32_t ticks ) {
00068     if (ticks)
00069         PWM_MR = PWM_TIMER->MR3 - ticks;  //They inverted PWM on the 11u24
00070     else
00071         PWM_MR = 0xFFFFFFFF;           //If MR3 = ticks 1 clock cycle wide errors appear, this prevents that (unless MR3 = max).
00072 }
00073 
00074 void FastPWM::period_ticks( uint32_t ticks ) {  
00075     PWM_TIMER->TCR = 0x02;
00076     PWM_TIMER->MR3 = ticks;
00077     PWM_TIMER->TCR = 0x01;   
00078 }
00079 
00080 uint32_t FastPWM::getPeriod( void ) {
00081     return PWM_TIMER->MR3;
00082 }
00083 
00084 uint32_t FastPWM::setPrescaler(uint32_t reqScale) {
00085     //If 32-bit, disable auto-scaling, return 1
00086     if (bits == 32) {
00087         dynamicPrescaler = false;
00088         return 1;
00089     }
00090     
00091     //Else 16-bit timer:
00092     if (reqScale == 0)
00093         //Return prescaler
00094         return PWM_TIMER->PR + 1;
00095     if (reqScale > (uint32_t)(1<<16))
00096         reqScale = 1<<16;
00097     //Else set prescaler, we have to substract one from reqScale since a 0 in PCVAL is prescaler of 1
00098     PWM_TIMER->PR = reqScale - 1;
00099 
00100     return reqScale;
00101 }
00102 
00103 #endif