Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.94 KB | None | 0 0
  1. /*
  2. * MSP432 UART - Poll
  3. *
  4. * Copyright (c) Dr. Xinrong Li @ UNT
  5. * All rights reserved.
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <stdint.h> //Exact-width integer types
  11. #include <ti\devices\msp432p4xx\driverlib\driverlib.h> //Driver library
  12.  
  13.  
  14. #define DCO_FREQ 48e6 //unit: Hz; DCO nominal frequencies: 1.5, 3, 6, 12, 24, 48 MHz.
  15. #define TIMER0_FREQ 1 //unit: Hz
  16.  
  17.  
  18. #define NUM_DISP_TEXT_LINE 4
  19.  
  20.  
  21. //function prototypes
  22. void initDevice(void);
  23. void initGPIO(void);
  24. void initUART(void);
  25.  
  26. void uart0_transmitStr(const char *str);
  27.  
  28. // global variables
  29. uint32_t clockMCLK;
  30.  
  31. uint8_t BlueTooth = 0;
  32. uint8_t Temp;
  33.  
  34. const char *terminalDisplayText[NUM_DISP_TEXT_LINE] =
  35. {
  36. "\r\n",
  37. "UART Demo\r\n",
  38. "R: red, G: green, B:blue, H: Help\r\n",
  39. "> "
  40. };
  41.  
  42.  
  43. void main(void)
  44. {
  45. uint32_t i;
  46. uint8_t data;
  47.  
  48. initDevice();
  49. initGPIO();
  50. initUART();
  51.  
  52. // Enable interrupts
  53. Interrupt_enableMaster();
  54.  
  55. Timer32_startTimer(TIMER32_0_BASE, false);
  56.  
  57. //Initial display on terminal.
  58. for(i=0; i<NUM_DISP_TEXT_LINE; i++)
  59. {
  60. uart0_transmitStr(terminalDisplayText[i]);
  61. }
  62.  
  63. while(1)
  64. {
  65. if(UART_getInterruptStatus(EUSCI_A0_BASE, EUSCI_A_UART_RECEIVE_INTERRUPT_FLAG))
  66. {
  67. data = UART_receiveData(EUSCI_A0_BASE);
  68.  
  69.  
  70. UART_clearInterruptFlag(EUSCI_A0_BASE, EUSCI_A_UART_RECEIVE_INTERRUPT_FLAG);
  71.  
  72. switch(data)
  73. {
  74. case 'U':
  75. case 'u':
  76. uart0_transmitStr("Sample.\r\n> ");
  77. printf("BlueTooth Data = %d %x \n",BlueTooth,BlueTooth);
  78. if(BlueTooth==127)
  79. {
  80. uart0_transmitStr("High.\r\n> ");
  81. }
  82. if(BlueTooth==120)
  83. {
  84. uart0_transmitStr("Low.\r\n> ");
  85. }
  86. BlueTooth=0;
  87. break;
  88. case 'H':
  89. case 'h':
  90. for(i=0; i<NUM_DISP_TEXT_LINE; i++)
  91. {
  92. uart0_transmitStr(terminalDisplayText[i]);
  93. }
  94. break;
  95. }
  96. } //end of if
  97. } //end of while
  98. }
  99.  
  100.  
  101. void initDevice(void)
  102. {
  103. WDT_A_holdTimer(); //stop Watchdog timer
  104.  
  105. //Change VCORE to 1 to support a frequency higher than 24MHz.
  106. //See data sheet for Flash wait-state requirement for a given frequency.
  107. PCM_setPowerState(PCM_AM_LDO_VCORE1);
  108. FlashCtl_setWaitState(FLASH_BANK0, 1);
  109. FlashCtl_setWaitState(FLASH_BANK1, 1);
  110.  
  111. //Enable FPU for DCO Frequency calculation.
  112. FPU_enableModule();
  113. FPU_enableLazyStacking(); // Required to use FPU within ISR.
  114.  
  115. //Only use DCO nominal frequencies: 1.5, 3, 6, 12, 24, 48MHz.
  116. CS_setDCOFrequency(DCO_FREQ);
  117.  
  118. //Divider: 1, 2, 4, 8, 16, 32, 64, or 128.
  119. //SMCLK used by UART and ADC14.
  120.  
  121. CS_initClockSignal(CS_SMCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_16);
  122.  
  123. }
  124.  
  125.  
  126. void initGPIO(void)
  127. {
  128. // - P3.2: IN ( UART: UCA2RXD ).
  129. // - P3.3: OUT ( UART: UCA2TXD ).
  130. // P3->SEL0 |= ( BIT2 | BIT3 );
  131. GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P3, GPIO_PIN2, GPIO_PRIMARY_MODULE_FUNCTION);
  132. // P3->SEL1 &= ~( BIT2 | BIT3 );
  133. GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P3, GPIO_PIN3, GPIO_PRIMARY_MODULE_FUNCTION);
  134. }
  135.  
  136.  
  137.  
  138.  
  139. void initUART(void)
  140. {
  141. EUSCI_A2->CTLW0 |= EUSCI_A_CTLW0_SWRST;
  142. //Configuration for 3MHz SMCLK, 9600 baud rate.
  143. //Calculated using the online calculator that TI provides at:
  144. //http://software-dl.ti.com/msp430/msp430_public_sw/mcu/msp430/MSP430BaudRateConverter/index.html
  145. const eUSCI_UART_Config config =
  146. {
  147. EUSCI_A_UART_CLOCKSOURCE_SMCLK, //SMCLK Clock Source
  148. 19, //BRDIV = 19
  149. 8, //UCxBRF = 8
  150. 85, //UCxBRS = 0
  151. EUSCI_A_UART_NO_PARITY, //No Parity
  152. EUSCI_A_UART_LSB_FIRST, //MSB First
  153. EUSCI_A_UART_ONE_STOP_BIT, //One stop bit
  154. EUSCI_A_UART_MODE, //UART mode
  155. EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION //Oversampling
  156. };
  157.  
  158. const eUSCI_UART_Config config2 =
  159. {
  160. EUSCI_A_UART_CLOCKSOURCE_SMCLK, //SMCLK Clock Source
  161. 19, //BRDIV = 19
  162. 9, //UCxBRF = 8
  163. 85, //UCxBRS = 0
  164. EUSCI_A_UART_NO_PARITY, //No Parity
  165. EUSCI_A_UART_LSB_FIRST, //MSB First
  166. EUSCI_A_UART_ONE_STOP_BIT, //One stop bit
  167. EUSCI_A_UART_MODE, //UART mode
  168. EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION //Oversampling
  169. };
  170.  
  171. // Configure GPIO pins for UART. RX: P1.2, TX:P1.3.
  172. GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P1, GPIO_PIN2|GPIO_PIN3, GPIO_PRIMARY_MODULE_FUNCTION);
  173.  
  174. UART_initModule(EUSCI_A0_BASE, &config); //takes the config and plugs it into the UART initiation function
  175. UART_enableModule(EUSCI_A0_BASE);
  176.  
  177.  
  178. UART_initModule(EUSCI_A2_BASE, &config2);
  179. UART_enableModule(EUSCI_A2_BASE); // Initialize eUSCI
  180.  
  181. //EUSCI_A2->IFG &= ~EUSCI_A_IFG_RXIFG; //EUSCI_A_IFG_RXIFG=1 Clear eUSCI RX interrupt flag
  182. UART_clearInterruptFlag(EUSCI_A2_BASE,~EUSCI_A_IFG_RXIFG);
  183.  
  184. //EUSCI_A2->IE |= EUSCI_A_IE_RXIE; // Enable USCI_A2 RX interrupt
  185. UART_enableInterrupt(EUSCI_A2_BASE,EUSCI_A_IE_RXIE);
  186.  
  187. //see website fore more explanation on NVIC: https://www.keil.com/pack/doc/CMSIS/Core/html/group__NVIC__gr.html#ga5bb7f43ad92937c039dee3d36c3c2798
  188. NVIC_EnableIRQ ( EUSCIA2_IRQn ); //for cortex-M based device
  189. NVIC_SetPriority ( EUSCIA2_IRQn, 16 );//for cortex-M based device
  190.  
  191. NVIC_EnableIRQ ( EUSCIA0_IRQn ); //for cortex-M based device
  192. NVIC_SetPriority ( EUSCIA0_IRQn, 17 );//for cortex-M based device
  193. }
  194.  
  195.  
  196. // Transmit a string through UART0.
  197. void uart0_transmitStr(const char *str)
  198. {
  199. uint32_t len, i=0;
  200.  
  201. len = strlen(str);
  202.  
  203. while(i < len)
  204. {
  205. UART_transmitData(EUSCI_A0_BASE, str[i++]);
  206.  
  207. while(!UART_getInterruptStatus(EUSCI_A0_BASE, EUSCI_A_UART_TRANSMIT_COMPLETE_INTERRUPT_FLAG));
  208. UART_clearInterruptFlag(EUSCI_A0_BASE, EUSCI_A_UART_TRANSMIT_COMPLETE_INTERRUPT_FLAG);
  209. }
  210. }
  211.  
  212. void EUSCIA2_IRQHandler (void)
  213. {
  214. uint8_t Mask = 0x7f;
  215. //Bluetooth transmits a packet that can be set to 9 bits that is read LSB first.
  216. //check to see if setting UART to LSB manipulates the Data to become MSB or manipulation is required
  217. //Check to see the default HC-06 packet size setting packets vary from 5-9 bits and register is 8 bits
  218. // UART_clearInterruptFlag(EUSCI_A2_BASE,~EUSCI_A_IFG_RXIFG);
  219.  
  220. if (EUSCI_A2->IFG & EUSCI_A_IFG_RXIFG)
  221. {
  222.  
  223. Temp = UART_receiveData(EUSCI_A2_BASE);
  224. BlueTooth = (Temp & Mask);
  225. //BlueTooth = EUSCI_A2->RXBUF;
  226. UART_clearInterruptFlag(EUSCI_A2_BASE,~EUSCI_A_IFG_RXIFG);
  227.  
  228. }
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement