FastPWM clone
Device/FastPWM_LPC_SCT.cpp@35:d6c2b73d71f5, 2019-10-30 (annotated)
- Committer:
- blaze
- Date:
- Wed Oct 30 03:00:00 2019 +0000
- Revision:
- 35:d6c2b73d71f5
- Parent:
- 30:87e38b846651
Replace new/delete with malloc/free for void *. Deleting void pointer is undefined behavior in C++.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sissors | 29:3e4d3b900850 | 1 | //For targets which use the SCT |
Sissors | 29:3e4d3b900850 | 2 | #if defined(TARGET_LPC81X) || defined(TARGET_LPC82X) |
Sissors | 29:3e4d3b900850 | 3 | |
Sissors | 29:3e4d3b900850 | 4 | #ifdef TARGET_LPC82X |
Sissors | 29:3e4d3b900850 | 5 | #define CTRL_U CTRL |
Sissors | 29:3e4d3b900850 | 6 | #endif |
Sissors | 29:3e4d3b900850 | 7 | |
Sissors | 29:3e4d3b900850 | 8 | #include "FastPWM.h" |
Sissors | 29:3e4d3b900850 | 9 | |
Sissors | 29:3e4d3b900850 | 10 | void FastPWM::initFastPWM( void ) { |
Sissors | 29:3e4d3b900850 | 11 | //Mbed uses the timer as a single unified 32-bit timer, who are we to argue with this, and it is easier |
Sissors | 29:3e4d3b900850 | 12 | bits = 32; |
Sissors | 29:3e4d3b900850 | 13 | |
Sissors | 30:87e38b846651 | 14 | #ifdef TARGET_LPC82X |
Sissors | 30:87e38b846651 | 15 | //The mbed lib uses the PWM peripheral slightly different, which is irritating. This sets it bck to the LPC81X |
Sissors | 30:87e38b846651 | 16 | _pwm.pwm->EVENT[_pwm.pwm_ch + 1].CTRL = (1 << 12) | (_pwm.pwm_ch + 1); // Event_n on Match_n |
Sissors | 30:87e38b846651 | 17 | _pwm.pwm->EVENT[_pwm.pwm_ch + 1].STATE = 0xFFFFFFFF; // All states |
Sissors | 30:87e38b846651 | 18 | _pwm.pwm->OUT[_pwm.pwm_ch].SET = (1 << 0); // All PWM channels are SET on Event_0 |
Sissors | 30:87e38b846651 | 19 | _pwm.pwm->OUT[_pwm.pwm_ch].CLR = (1 << (_pwm.pwm_ch + 1)); // PWM ch is CLRed on Event_(ch+1) |
Sissors | 30:87e38b846651 | 20 | #endif |
Sissors | 30:87e38b846651 | 21 | |
Sissors | 29:3e4d3b900850 | 22 | //With 32-bit we fix prescaler to 1 |
Sissors | 29:3e4d3b900850 | 23 | _pwm.pwm->CTRL_U |= (1 << 2) | (1 << 3); |
Sissors | 29:3e4d3b900850 | 24 | _pwm.pwm->CTRL_U &= ~(0x7F << 5); |
Sissors | 29:3e4d3b900850 | 25 | _pwm.pwm->CTRL_U &= ~(1 << 2); |
Sissors | 29:3e4d3b900850 | 26 | |
Sissors | 29:3e4d3b900850 | 27 | } |
Sissors | 29:3e4d3b900850 | 28 | |
Sissors | 29:3e4d3b900850 | 29 | void FastPWM::pulsewidth_ticks( uint32_t ticks ) { |
Sissors | 29:3e4d3b900850 | 30 | #ifdef TARGET_LPC81X |
Sissors | 29:3e4d3b900850 | 31 | _pwm.pwm->MATCHREL[_pwm.pwm_ch + 1].U = ticks; |
Sissors | 29:3e4d3b900850 | 32 | #else |
Sissors | 29:3e4d3b900850 | 33 | _pwm.pwm->MATCHREL[_pwm.pwm_ch + 1] = ticks; |
Sissors | 29:3e4d3b900850 | 34 | #endif |
Sissors | 29:3e4d3b900850 | 35 | } |
Sissors | 29:3e4d3b900850 | 36 | |
Sissors | 29:3e4d3b900850 | 37 | void FastPWM::period_ticks( uint32_t ticks ) { |
Sissors | 29:3e4d3b900850 | 38 | #ifdef TARGET_LPC81X |
Sissors | 29:3e4d3b900850 | 39 | _pwm.pwm->MATCHREL[0].U = ticks; |
Sissors | 29:3e4d3b900850 | 40 | #else |
Sissors | 29:3e4d3b900850 | 41 | _pwm.pwm->MATCHREL[0] = ticks; |
Sissors | 29:3e4d3b900850 | 42 | #endif |
Sissors | 29:3e4d3b900850 | 43 | } |
Sissors | 29:3e4d3b900850 | 44 | |
Sissors | 29:3e4d3b900850 | 45 | uint32_t FastPWM::getPeriod( void ) { |
Sissors | 29:3e4d3b900850 | 46 | #ifdef TARGET_LPC81X |
Sissors | 29:3e4d3b900850 | 47 | return _pwm.pwm->MATCHREL[0].U; |
Sissors | 29:3e4d3b900850 | 48 | #else |
Sissors | 29:3e4d3b900850 | 49 | return _pwm.pwm->MATCHREL[0]; |
Sissors | 29:3e4d3b900850 | 50 | #endif |
Sissors | 29:3e4d3b900850 | 51 | } |
Sissors | 29:3e4d3b900850 | 52 | |
Sissors | 29:3e4d3b900850 | 53 | //Maybe implemented later, but needing to change the prescaler for a 32-bit |
Sissors | 29:3e4d3b900850 | 54 | //timer used in PWM mode is kinda unlikely. |
Sissors | 29:3e4d3b900850 | 55 | //If you really need to do it, rejoice, you can make it run so slow a period is over 40,000 year |
Sissors | 29:3e4d3b900850 | 56 | uint32_t FastPWM::setPrescaler(uint32_t reqScale) { |
Sissors | 29:3e4d3b900850 | 57 | //Disable dynamic prescaling |
Sissors | 29:3e4d3b900850 | 58 | dynamicPrescaler = false; |
Sissors | 29:3e4d3b900850 | 59 | |
Sissors | 29:3e4d3b900850 | 60 | return 1; |
Sissors | 29:3e4d3b900850 | 61 | } |
Sissors | 29:3e4d3b900850 | 62 | |
Sissors | 29:3e4d3b900850 | 63 | #endif |