Potmeter FastPWM.h

Dependencies:   mbed

Committer:
vd
Date:
Tue Sep 19 16:52:01 2017 +0000
Revision:
0:16be67f4d9ac
Potmeter FastPWM.h

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vd 0:16be67f4d9ac 1 #if defined(TARGET_LPC11UXX) || defined(TARGET_LPC11XX_11CXX)
vd 0:16be67f4d9ac 2
vd 0:16be67f4d9ac 3 #include "FastPWM.h"
vd 0:16be67f4d9ac 4
vd 0:16be67f4d9ac 5 #define PWM_MR (*(((fastpwm_struct*)fast_obj)->MR))
vd 0:16be67f4d9ac 6 #define PWM_TIMER (((fastpwm_struct*)fast_obj)->timer)
vd 0:16be67f4d9ac 7
vd 0:16be67f4d9ac 8 typedef struct {
vd 0:16be67f4d9ac 9 uint8_t timer;
vd 0:16be67f4d9ac 10 uint8_t mr;
vd 0:16be67f4d9ac 11 } timer_mr;
vd 0:16be67f4d9ac 12
vd 0:16be67f4d9ac 13 #ifdef TARGET_LPC11UXX
vd 0:16be67f4d9ac 14 typedef struct {
vd 0:16be67f4d9ac 15 __IO uint32_t *MR;
vd 0:16be67f4d9ac 16 LPC_CTxxBx_Type *timer;
vd 0:16be67f4d9ac 17 } fastpwm_struct;
vd 0:16be67f4d9ac 18
vd 0:16be67f4d9ac 19 static timer_mr pwm_timer_map[11] = {
vd 0:16be67f4d9ac 20 {0, 0}, {0, 1}, {0, 2},
vd 0:16be67f4d9ac 21 {1, 0}, {1, 1},
vd 0:16be67f4d9ac 22 {2, 0}, {2, 1}, {2, 2},
vd 0:16be67f4d9ac 23 {3, 0}, {3, 1}, {3, 2},
vd 0:16be67f4d9ac 24 };
vd 0:16be67f4d9ac 25
vd 0:16be67f4d9ac 26 static LPC_CTxxBx_Type *Timers[4] = {
vd 0:16be67f4d9ac 27 LPC_CT16B0, LPC_CT16B1,
vd 0:16be67f4d9ac 28 LPC_CT32B0, LPC_CT32B1
vd 0:16be67f4d9ac 29 };
vd 0:16be67f4d9ac 30 #else //LPC11XX
vd 0:16be67f4d9ac 31 typedef struct {
vd 0:16be67f4d9ac 32 __IO uint32_t *MR;
vd 0:16be67f4d9ac 33 LPC_TMR_TypeDef *timer;
vd 0:16be67f4d9ac 34 } fastpwm_struct;
vd 0:16be67f4d9ac 35
vd 0:16be67f4d9ac 36 static timer_mr pwm_timer_map[5] = {
vd 0:16be67f4d9ac 37 {0, 0}, /* CT16B0, MR0 */
vd 0:16be67f4d9ac 38 {0, 1}, /* CT16B0, MR1 */
vd 0:16be67f4d9ac 39
vd 0:16be67f4d9ac 40 {1, 0}, /* CT16B1, MR0 */
vd 0:16be67f4d9ac 41 {1, 1}, /* CT16B1, MR1 */
vd 0:16be67f4d9ac 42
vd 0:16be67f4d9ac 43 {2, 2}, /* CT32B0, MR2 */
vd 0:16be67f4d9ac 44 };
vd 0:16be67f4d9ac 45
vd 0:16be67f4d9ac 46 static LPC_TMR_TypeDef *Timers[3] = {
vd 0:16be67f4d9ac 47 LPC_TMR16B0, LPC_TMR16B1,
vd 0:16be67f4d9ac 48 LPC_TMR32B0
vd 0:16be67f4d9ac 49 };
vd 0:16be67f4d9ac 50 #endif
vd 0:16be67f4d9ac 51
vd 0:16be67f4d9ac 52
vd 0:16be67f4d9ac 53 void FastPWM::initFastPWM( void ) {
vd 0:16be67f4d9ac 54 fast_obj = new fastpwm_struct;
vd 0:16be67f4d9ac 55 timer_mr tid = pwm_timer_map[_pwm.pwm];
vd 0:16be67f4d9ac 56 PWM_TIMER = Timers[tid.timer];
vd 0:16be67f4d9ac 57 (((fastpwm_struct*)fast_obj)->MR) = &PWM_TIMER->MR[tid.mr];
vd 0:16be67f4d9ac 58
vd 0:16be67f4d9ac 59 if (tid.timer < 2)
vd 0:16be67f4d9ac 60 //16-bit timer
vd 0:16be67f4d9ac 61 bits = 16;
vd 0:16be67f4d9ac 62 else
vd 0:16be67f4d9ac 63 //32-bit timer
vd 0:16be67f4d9ac 64 bits = 32;
vd 0:16be67f4d9ac 65 }
vd 0:16be67f4d9ac 66
vd 0:16be67f4d9ac 67 void FastPWM::pulsewidth_ticks( uint32_t ticks ) {
vd 0:16be67f4d9ac 68 if (ticks)
vd 0:16be67f4d9ac 69 PWM_MR = PWM_TIMER->MR3 - ticks; //They inverted PWM on the 11u24
vd 0:16be67f4d9ac 70 else
vd 0:16be67f4d9ac 71 PWM_MR = 0xFFFFFFFF; //If MR3 = ticks 1 clock cycle wide errors appear, this prevents that (unless MR3 = max).
vd 0:16be67f4d9ac 72 }
vd 0:16be67f4d9ac 73
vd 0:16be67f4d9ac 74 void FastPWM::period_ticks( uint32_t ticks ) {
vd 0:16be67f4d9ac 75 PWM_TIMER->TCR = 0x02;
vd 0:16be67f4d9ac 76 PWM_TIMER->MR3 = ticks;
vd 0:16be67f4d9ac 77 PWM_TIMER->TCR = 0x01;
vd 0:16be67f4d9ac 78 }
vd 0:16be67f4d9ac 79
vd 0:16be67f4d9ac 80 uint32_t FastPWM::getPeriod( void ) {
vd 0:16be67f4d9ac 81 return PWM_TIMER->MR3;
vd 0:16be67f4d9ac 82 }
vd 0:16be67f4d9ac 83
vd 0:16be67f4d9ac 84 uint32_t FastPWM::setPrescaler(uint32_t reqScale) {
vd 0:16be67f4d9ac 85 //If 32-bit, disable auto-scaling, return 1
vd 0:16be67f4d9ac 86 if (bits == 32) {
vd 0:16be67f4d9ac 87 dynamicPrescaler = false;
vd 0:16be67f4d9ac 88 return 1;
vd 0:16be67f4d9ac 89 }
vd 0:16be67f4d9ac 90
vd 0:16be67f4d9ac 91 //Else 16-bit timer:
vd 0:16be67f4d9ac 92 if (reqScale == 0)
vd 0:16be67f4d9ac 93 //Return prescaler
vd 0:16be67f4d9ac 94 return PWM_TIMER->PR + 1;
vd 0:16be67f4d9ac 95 if (reqScale > (uint32_t)(1<<16))
vd 0:16be67f4d9ac 96 reqScale = 1<<16;
vd 0:16be67f4d9ac 97 //Else set prescaler, we have to substract one from reqScale since a 0 in PCVAL is prescaler of 1
vd 0:16be67f4d9ac 98 PWM_TIMER->PR = reqScale - 1;
vd 0:16be67f4d9ac 99
vd 0:16be67f4d9ac 100 return reqScale;
vd 0:16be67f4d9ac 101 }
vd 0:16be67f4d9ac 102
vd 0:16be67f4d9ac 103 #endif