CMT timer test based on teensy 3 IRremote lib. 50us interrupt or khz carrier

Dependencies:   mbed

Committer:
manitou
Date:
Wed Apr 20 21:09:27 2016 +0000
Revision:
0:0d9b39b610c5
CMT timer test on K64F

Who changed what in which revision?

UserRevisionLine numberNew contents of line
manitou 0:0d9b39b610c5 1 // check pwm and 50us values for IRremote IRremoteInt.h
manitou 0:0d9b39b610c5 2 // CMT timer ch 39, pin pg 196 CMT_IRO PTD7 on 2x4 RF header
manitou 0:0d9b39b610c5 3 // derived from teensy 3 IRremote lib
manitou 0:0d9b39b610c5 4 // https://forum.pjrc.com/threads/26331-IRRemote-issues?p=52316&viewfull=1#post52316
manitou 0:0d9b39b610c5 5
manitou 0:0d9b39b610c5 6 #include "mbed.h"
manitou 0:0d9b39b610c5 7
manitou 0:0d9b39b610c5 8 static uint32_t khz=40, CMT_PPS_VAL, fbus=60000000, cmd2=30, cmd4=19;
manitou 0:0d9b39b610c5 9 DigitalOut led(LED1);
manitou 0:0d9b39b610c5 10
manitou 0:0d9b39b610c5 11 volatile uint32_t ticks;
manitou 0:0d9b39b610c5 12
manitou 0:0d9b39b610c5 13 extern "C" void CMT_IRQHandler() {
manitou 0:0d9b39b610c5 14 uint8_t tmp = CMT_MSC; // reset interrupt
manitou 0:0d9b39b610c5 15 CMT_CMD2 = 30;
manitou 0:0d9b39b610c5 16 led = !led;
manitou 0:0d9b39b610c5 17 ticks++;
manitou 0:0d9b39b610c5 18 }
manitou 0:0d9b39b610c5 19
manitou 0:0d9b39b610c5 20 // ref 39.7.3.1 30+1+19 = 50us period
manitou 0:0d9b39b610c5 21 void init1() {
manitou 0:0d9b39b610c5 22 SIM_SCGC4 |= (1<<SIM_SCGC4_CMT_SHIFT);
manitou 0:0d9b39b610c5 23 CMT_PPS = CMT_PPS_VAL; // 8 mhz
manitou 0:0d9b39b610c5 24 CMT_CGH1 = 1;
manitou 0:0d9b39b610c5 25 CMT_CGL1 = 1;
manitou 0:0d9b39b610c5 26 CMT_CMD1 = 0;
manitou 0:0d9b39b610c5 27 CMT_CMD2 = cmd2; // mark low down counter 8-bit
manitou 0:0d9b39b610c5 28 CMT_CMD3 = 0;
manitou 0:0d9b39b610c5 29 CMT_CMD4 = cmd4; // space low period 8-bit
manitou 0:0d9b39b610c5 30 CMT_OC = 0;
manitou 0:0d9b39b610c5 31 CMT_MSC = 0x03; // enable CMT and interrupt
manitou 0:0d9b39b610c5 32 NVIC_EnableIRQ(CMT_IRQn);
manitou 0:0d9b39b610c5 33 }
manitou 0:0d9b39b610c5 34
manitou 0:0d9b39b610c5 35 void init_pwm(int khz) {
manitou 0:0d9b39b610c5 36 SIM_SCGC4 |= (1<<SIM_SCGC4_CMT_SHIFT);
manitou 0:0d9b39b610c5 37 SIM_SOPT2 |= (1 << SIM_SOPT2_PTD7PAD_SHIFT); // drive strength
manitou 0:0d9b39b610c5 38 CMT_PPS = CMT_PPS_VAL; // 8 mhz
manitou 0:0d9b39b610c5 39 CMT_CGH1 = 2667 / khz; // high time 30% duty 8-bit ticks*.125us
manitou 0:0d9b39b610c5 40 CMT_CGL1 = 5333 / khz; // low time
manitou 0:0d9b39b610c5 41 CMT_CMD1 = 0;
manitou 0:0d9b39b610c5 42 CMT_CMD2 = cmd2; // no effect
manitou 0:0d9b39b610c5 43 CMT_CMD3 = 0;
manitou 0:0d9b39b610c5 44 CMT_CMD4 = 0; // space low
manitou 0:0d9b39b610c5 45 CMT_OC = 0x60; // enable IRO
manitou 0:0d9b39b610c5 46 CMT_MSC = 0x01; // enable
manitou 0:0d9b39b610c5 47
manitou 0:0d9b39b610c5 48 SIM_SCGC5 |= (1 << SIM_SCGC5_PORTD_SHIFT); // power PORTD
manitou 0:0d9b39b610c5 49 PORTD_PCR7 = PORT_PCR_MUX(2)|(1<< PORT_PCR_DSE_SHIFT)|(1 << PORT_PCR_SRE_SHIFT);
manitou 0:0d9b39b610c5 50 }
manitou 0:0d9b39b610c5 51
manitou 0:0d9b39b610c5 52 main() {
manitou 0:0d9b39b610c5 53 // get F_BUS freq
manitou 0:0d9b39b610c5 54 fbus = SystemCoreClock / (((SIM->CLKDIV1 & SIM_CLKDIV1_OUTDIV2_MASK) >> SIM_CLKDIV1_OUTDIV2_SHIFT) + 1);
manitou 0:0d9b39b610c5 55 printf("\nSystemCoreClock %d bus %d %s %s\n",SystemCoreClock,fbus,__TIME__,__DATE__);
manitou 0:0d9b39b610c5 56 if (fbus == 48000000) CMT_PPS_VAL = 5 ;
manitou 0:0d9b39b610c5 57 else if (fbus == 24000000)CMT_PPS_VAL = 2 ;
manitou 0:0d9b39b610c5 58 else if (fbus == 16000000) CMT_PPS_VAL= 1 ;
manitou 0:0d9b39b610c5 59 else if (fbus == 8000000)CMT_PPS_VAL = 0 ;
manitou 0:0d9b39b610c5 60 else if (fbus == 56000000)CMT_PPS_VAL = 6 ;
manitou 0:0d9b39b610c5 61 else if (fbus == 36000000) { CMT_PPS_VAL = 3 ; cmd4=25; khz=(32*khz)/36; }
manitou 0:0d9b39b610c5 62 else if (fbus == 60000000) { CMT_PPS_VAL = 6 ; cmd4=23; khz = (56*khz)/60; }
manitou 0:0d9b39b610c5 63 else if (fbus == 4000000) { CMT_PPS_VAL = 0 ; cmd2=5; khz=2*khz; }
manitou 0:0d9b39b610c5 64 else if (fbus == 2000000) { CMT_PPS_VAL = 0 ; cmd2=5; cmd4=7; khz=4*khz; }
manitou 0:0d9b39b610c5 65
manitou 0:0d9b39b610c5 66
manitou 0:0d9b39b610c5 67 #if 1
manitou 0:0d9b39b610c5 68 init_pwm(khz); // pwm freq in khz
manitou 0:0d9b39b610c5 69 while(true) {
manitou 0:0d9b39b610c5 70 led= !led;
manitou 0:0d9b39b610c5 71 wait(1.0);
manitou 0:0d9b39b610c5 72 }
manitou 0:0d9b39b610c5 73 #else
manitou 0:0d9b39b610c5 74 init1(); // 50us interrupts
manitou 0:0d9b39b610c5 75 while(true) {
manitou 0:0d9b39b610c5 76 printf("ticks %d\n",ticks);
manitou 0:0d9b39b610c5 77 wait(2.);
manitou 0:0d9b39b610c5 78 }
manitou 0:0d9b39b610c5 79 #endif
manitou 0:0d9b39b610c5 80 }
manitou 0:0d9b39b610c5 81