SHARE
TWEET

CG2271 Code

a guest Feb 21st, 2020 99 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "MKL25Z4.h"
  2. #include "RTE_Components.h"
  3. #include CMSIS_device_header
  4. #include "cmsis_os2.h"
  5. #define PTB0_Pin 0
  6. #define PTB1_Pin 1
  7. #define PTD0_Pin 0
  8. #define PTB2_Pin 2
  9. #define PTB3_Pin 3
  10.  
  11. #define RED_LED 18 // PortB Pin 18
  12. #define MASK(x) (1 << (x))
  13.  
  14. #define FREQ 50
  15. #define BAUD_RATE 9600
  16. #define UART_TX_PORTE22 22
  17. #define UART_RX_PORTE23 23
  18. #define UART_INT_PRIO 128
  19. #define Q_SIZE (32)
  20.  
  21. volatile uint8_t data = 0x01;
  22.  
  23. volatile uint8_t backLED = 0;
  24.  
  25. volatile int check = 0;
  26. typedef struct{
  27. unsigned char Data[Q_SIZE];
  28. unsigned int Head; // points to oldest data element
  29. unsigned int Tail; // points to next free space
  30. unsigned int Size; // quantity of elements in queue
  31. } Q_T;
  32.  
  33. Q_T TxQ, RxQ;
  34.  
  35. void Q_Init(Q_T * q) {
  36. unsigned int i;
  37. for (i=0; i<Q_SIZE; i++)
  38. q->Data[i] = 0; // to simplify our lives when debugging
  39. q->Head = 0;
  40. q->Tail = 0;
  41. q->Size = 0;
  42. }
  43.  
  44. int Q_Empty(Q_T * q) {
  45. return q->Size == 0;
  46. }
  47. int Q_Full(Q_T * q) {
  48. return q->Size == Q_SIZE;
  49. }
  50.  
  51. int Q_Enqueue(Q_T * q, unsigned char d) {
  52.     // What if queue is full?
  53.     if (!Q_Full(q)) {
  54.         q->Data[q->Tail++] = d;
  55.         q->Tail %= Q_SIZE;
  56.         q->Size++;
  57.         return 1; // success
  58.     } else
  59.         return 0; // failure
  60. }
  61. unsigned char Q_Dequeue(Q_T * q) {
  62.     // Must check to see if queue is empty before dequeueing
  63.     unsigned char t=0;
  64.     if (!Q_Empty(q)) {
  65.         t = q->Data[q->Head];
  66.         q->Data[q->Head++] = 0; // to simplify debugging
  67.         q->Head %= Q_SIZE;
  68.         q->Size--;
  69.     }
  70.     return t;
  71. }
  72.  
  73. int mod_value(int frequency){
  74.     return DEFAULT_SYSTEM_CLOCK / (frequency * 128)  - 1;
  75. }
  76.  
  77. static void delay(volatile uint32_t nof) {
  78.     while(nof!=0) {
  79.         __asm("NOP");
  80.         nof--;
  81.     }
  82. }  
  83.  
  84.  
  85. /* initPWM() */
  86. void initPWM(void){
  87.     SIM_SCGC5 |= SIM_SCGC5_PORTB_MASK | SIM_SCGC5_PORTD_MASK;
  88.    
  89.     PORTD->PCR[PTD0_Pin] &= ~PORT_PCR_MUX_MASK;
  90.     PORTD->PCR[PTD0_Pin] |= PORT_PCR_MUX(4);
  91.    
  92.     PORTB->PCR[PTB0_Pin] &= ~PORT_PCR_MUX_MASK;
  93.     PORTB->PCR[PTB0_Pin] |= PORT_PCR_MUX(3);
  94.    
  95.     PORTB->PCR[PTB1_Pin] &= ~PORT_PCR_MUX_MASK;
  96.     PORTB->PCR[PTB1_Pin] |= PORT_PCR_MUX(3);
  97.    
  98.     PORTB->PCR[PTB2_Pin] &= ~PORT_PCR_MUX_MASK;
  99.     PORTB->PCR[PTB2_Pin] |= PORT_PCR_MUX(3);
  100.    
  101.     PORTB->PCR[PTB3_Pin] &= ~PORT_PCR_MUX_MASK;
  102.     PORTB->PCR[PTB3_Pin] |= PORT_PCR_MUX(3);
  103.    
  104.     SIM->SCGC6 |= SIM_SCGC6_TPM1_MASK | SIM_SCGC6_TPM0_MASK;
  105.     SIM->SCGC6 |= SIM_SCGC6_TPM2_MASK;
  106.    
  107.     SIM->SOPT2 &= ~SIM_SOPT2_TPMSRC_MASK;
  108.     SIM->SOPT2 |= SIM_SOPT2_TPMSRC(1);
  109.        
  110.     TPM1->MOD = mod_value(FREQ);
  111.     TPM2->MOD = mod_value(FREQ);
  112.     TPM1_C0V = (mod_value(FREQ) + 1 ) / 2;
  113.     TPM1_C1V = (mod_value(FREQ) + 1 ) / 2;
  114.     TPM2_C0V = (mod_value(FREQ) + 1 ) / 2;
  115.     TPM2_C1V = (mod_value(FREQ) + 1 ) / 2;
  116.    
  117.    
  118.     TPM1->SC &= ~((TPM_SC_CMOD_MASK) | (TPM_SC_PS_MASK));
  119.     TPM1->SC |= (TPM_SC_CMOD(1) | TPM_SC_PS(7));
  120.     TPM1->SC &= ~(TPM_SC_CPWMS_MASK);
  121.    
  122.     TPM1_C0SC &= ~((TPM_CnSC_ELSB_MASK) | (TPM_CnSC_ELSA_MASK) | (TPM_CnSC_MSA_MASK) | (TPM_CnSC_MSB_MASK));
  123.     TPM1_C0SC |= (TPM_CnSC_ELSB(1) | TPM_CnSC_MSB(1));
  124.    
  125.     TPM1_C1SC &= ~((TPM_CnSC_ELSB_MASK) | (TPM_CnSC_ELSA_MASK) | (TPM_CnSC_MSA_MASK) | (TPM_CnSC_MSB_MASK));
  126.     TPM1_C1SC |= (TPM_CnSC_ELSB(1) | TPM_CnSC_MSB(1));
  127.    
  128.     TPM2->SC &= ~((TPM_SC_CMOD_MASK) | (TPM_SC_PS_MASK));
  129.     TPM2->SC |= (TPM_SC_CMOD(1) | TPM_SC_PS(7));
  130.     TPM2->SC &= ~(TPM_SC_CPWMS_MASK);
  131.    
  132.     TPM2_C0SC &= ~((TPM_CnSC_ELSB_MASK) | (TPM_CnSC_ELSA_MASK) | (TPM_CnSC_MSA_MASK) | (TPM_CnSC_MSB_MASK));
  133.     TPM2_C0SC |= (TPM_CnSC_ELSB(1) | TPM_CnSC_MSB(1));
  134.    
  135.     TPM2_C1SC &= ~((TPM_CnSC_ELSB_MASK) | (TPM_CnSC_ELSA_MASK) | (TPM_CnSC_MSA_MASK) | (TPM_CnSC_MSB_MASK));
  136.     TPM2_C1SC |= (TPM_CnSC_ELSB(1) | TPM_CnSC_MSB(1));
  137.    
  138.    
  139.     TPM0->MOD = mod_value(FREQ);
  140.     TPM0_C0V = (mod_value(FREQ) + 1 ) / 2;
  141.    
  142.     TPM0->SC &= ~((TPM_SC_CMOD_MASK) | (TPM_SC_PS_MASK));
  143.     TPM0->SC |= (TPM_SC_CMOD(1) | TPM_SC_PS(7));
  144.     TPM0->SC &= ~(TPM_SC_CPWMS_MASK);
  145.    
  146.     TPM0_C0SC &= ~((TPM_CnSC_ELSB_MASK) | (TPM_CnSC_ELSA_MASK) | (TPM_CnSC_MSA_MASK) | (TPM_CnSC_MSB_MASK));
  147.     TPM0_C0SC |= (TPM_CnSC_ELSB(1) | TPM_CnSC_MSB(1));
  148. }
  149.  
  150. void UART2_IRQHandler(void) {
  151.     check = 1;
  152.     if (UART2->S1 & UART_S1_RDRF_MASK) {
  153.         // received a character
  154.         if (!Q_Full(&RxQ)) {
  155.         Q_Enqueue(&RxQ, UART2->D);
  156.         } else {
  157.         // error -queue full.
  158.         }
  159.     }
  160. }
  161. void initLED() {
  162.     // Enable Clock to PORTB and PORTD
  163.     SIM->SCGC5 |= ((SIM_SCGC5_PORTB_MASK) | (SIM_SCGC5_PORTD_MASK));
  164.     // Configure MUX settings to make all 3 pins GPIO
  165.     PORTB->PCR[RED_LED] &= ~PORT_PCR_MUX_MASK;
  166.     PORTB->PCR[RED_LED] |= PORT_PCR_MUX(1);
  167.    
  168.     PTB->PDDR |= MASK(RED_LED);
  169.     //PTB->PDOR |= MASK(RED_LED);
  170. }
  171.  
  172. void initUART2(uint32_t baud_rate) {
  173.     uint32_t divisor, bus_clock;
  174.    
  175.     SIM->SCGC4 |= SIM_SCGC4_UART2_MASK;
  176.     SIM->SCGC5 |= SIM_SCGC5_PORTE_MASK;
  177.    
  178.     PORTE->PCR[UART_TX_PORTE22] &= ~PORT_PCR_MUX_MASK;
  179.     PORTE->PCR[UART_TX_PORTE22] |= PORT_PCR_MUX(4);
  180.    
  181.     PORTE->PCR[UART_RX_PORTE23] &= ~PORT_PCR_MUX_MASK;
  182.     PORTE->PCR[UART_RX_PORTE23] |= PORT_PCR_MUX(4);
  183.    
  184.    
  185.  
  186.    
  187.    
  188.     UART2->C2 &= ~((UART_C2_TE_MASK) | (UART_C2_RE_MASK));
  189.    
  190.    
  191.     bus_clock = (DEFAULT_SYSTEM_CLOCK)/2;
  192.     divisor = bus_clock / (baud_rate*16);
  193.     UART2->BDH = UART_BDH_SBR(divisor >> 8);
  194.     UART2->BDL = UART_BDL_SBR(divisor);
  195.    
  196.     NVIC_SetPriority(UART2_IRQn, 128);
  197.     NVIC_ClearPendingIRQ(UART2_IRQn);
  198.     NVIC_EnableIRQ(UART2_IRQn);
  199.    
  200.     UART2->C1 = 0;
  201.     UART2->S2 = 0;
  202.     UART2->C3 = 0;
  203.    
  204.     //UART2->C2 |= ((UART_C2_TE_MASK) | (UART_C2_RE_MASK));
  205.     UART2->C2 |= ((UART_C2_TE_MASK) | (UART_C2_RE_MASK) | UART_C2_RIE_MASK);
  206.     Q_Init(&RxQ);
  207. }
  208.  
  209.  
  210. void LEDControl(void *arg){
  211.     for(;;) {
  212.         PTB->PDOR = !MASK(RED_LED);
  213.         osDelay(500);
  214.         PTB->PDOR = MASK(RED_LED);
  215.         osDelay(500);
  216.     }
  217. }
  218.     /*
  219.     while (!Q_Empty(&RxQ)) {
  220.         data = Q_Dequeue(&RxQ);
  221.         if(data == 0x03) {
  222.             PTB->PDOR = !MASK(RED_LED);
  223.         }
  224.         if(data == 0x02) {
  225.             PTB->PDOR = MASK(RED_LED);
  226.         }
  227.     }
  228.     osDelay(1);
  229.     }
  230.     */
  231.  
  232.  
  233. void BuzzerControl(void *arg){
  234.     for(;;){
  235.         TPM0->MOD = mod_value(440);
  236.         TPM0_C0V = (mod_value(440) + 1 ) / 2;
  237.         osDelay(500);
  238.         TPM0->MOD = mod_value(440);
  239.         TPM0_C0V = 0;
  240.         osDelay(500);
  241.     }
  242. }
  243.  
  244. void forwardFreq(int frequency) {
  245.     TPM1_C0V = (mod_value(frequency) + 1 ) / 2;
  246.     TPM1_C1V = 0;
  247.     TPM2_C0V = (mod_value(frequency) + 1 ) / 2;
  248.     TPM2_C1V = 0;
  249. }
  250.    
  251. void backwardFreq(int frequency) {
  252.     TPM1_C1V = (mod_value(frequency) + 1 ) / 2;
  253.     TPM1_C0V = 0;
  254.     TPM2_C1V = (mod_value(frequency) + 1 ) / 2;
  255.     TPM2_C0V = 0;
  256. }
  257.  
  258. void stop() {
  259.     TPM1_C1V = 0;
  260.     TPM1_C0V = 0;
  261.     TPM2_C1V = 0;
  262.     TPM2_C0V = 0;
  263.     }
  264.  
  265. void trial (void *args) {
  266. for (;;) {
  267. }
  268. }
  269.  
  270.  
  271. void motorControl (void *args) {
  272.     for(;;) {
  273.     while (!Q_Empty(&RxQ)) {
  274.         data = Q_Dequeue(&RxQ);
  275.         if (data == 0x02) {
  276.             forwardFreq(FREQ);
  277.         } else if (data == 0x03) {
  278.             backwardFreq(FREQ);
  279.         } else if (data == 0x09) {
  280.             stop();
  281.         }
  282.     }
  283.     osDelay(1);
  284.     }
  285. }
  286.    
  287.  
  288. int main(){
  289.   SystemCoreClockUpdate();
  290.     initPWM();
  291.     initLED();
  292.     initUART2(BAUD_RATE);  
  293.     osKernelInitialize();
  294.     const osThreadAttr_t BuzzerControlAttr = {
  295.         .priority = osPriorityNormal
  296.     };
  297.     osThreadNew(BuzzerControl, NULL, &BuzzerControlAttr);
  298.     const osThreadAttr_t motorControlAttr = {
  299.         .priority = osPriorityLow
  300.     };
  301.     osThreadNew(motorControl, NULL, &motorControlAttr);
  302.     // need to work out LED
  303.     //const osThreadAttr_t LEDControlAttr = {
  304.     //  .priority = osPriorityHigh
  305.     //};
  306.     //osThreadNew(LEDControl, NULL, &LEDControlAttr);
  307.  
  308.     osKernelStart();
  309.    
  310.     for(;;) {}
  311. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top