8fc2001fda30ae1e208579d0c71c8f25a1e0e8b4
[i3-mote/i3-mote.git] / Basic-Test-Package / MSP432 / Test_MSP432_DebugUART_EchoPC / main.c
1 /******************************************************************************
2  * MSP432 UART - PC Echo with 12MHz BRCLK
3  *
4  * Description: This demo echoes back characters received via a PC serial port.
5  * SMCLK/DCO is used as a clock source and the device is put in LPM0
6  * The auto-clock enable feature is used by the eUSCI and SMCLK is turned off
7  * when the UART is idle and turned on when a receive edge is detected.
8  * Note that level shifter hardware is needed to shift between RS232 and MSP
9  * voltage levels.
10  *
11  *               MSP432P401
12  *             -----------------
13  *            |                 |
14  *            |                 |
15  *            |                 |
16  *       RST -|     P1.3/UCA0TXD|----> PC (echo)
17  *            |                 |
18  *            |                 |
19  *            |     P1.2/UCA0RXD|<---- PC
20  *            |                 |
21  *
22  * Author: Timothy Logan
23  *
24  * I3Mote Version: B.Martinez
25  *
26  *
27  *
28 *******************************************************************************/
29 #include "i3mote.h"
31 /* DriverLib Includes */
32 #include "driverlib.h"
35 /* Standard Includes */
36 #include <stdint.h>
37 #include <stdbool.h>
39 #define SYSFREQ 12000000
41 /* UART Configuration Parameter. These are the configuration parameters to
42  * make the eUSCI A UART module to operate with a 9600 baud rate.
43  * These values were calculated using the online calculator that TI provides at:
44  * http://software-dl.ti.com/msp430/msp430_public_sw/mcu/msp430/MSP430BaudRateConverter/index.html
45  */
47 #ifdef UART_BAUD_9600
48         const eUSCI_UART_Config uartConfig =
49         {
50                 EUSCI_A_UART_CLOCKSOURCE_SMCLK,          // SMCLK Clock Source
51                 78,                                      // BRDIV = 78
52                 2,                                       // UCxBRF = 2
53                 0,                                       // UCxBRS = 0
54                 EUSCI_A_UART_NO_PARITY,                  // No Parity
55                 EUSCI_A_UART_LSB_FIRST,                  // LSB First
56                 EUSCI_A_UART_ONE_STOP_BIT,               // One stop bit
57                 EUSCI_A_UART_MODE,                       // UART mode
58                 EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION  // Oversampling
59         };
60 #endif
62 #ifdef UART_BAUD_115200
63         const eUSCI_UART_Config uartConfig =
64         {
65                 EUSCI_A_UART_CLOCKSOURCE_SMCLK,          // SMCLK Clock Source
66                 6,                                       // BRDIV = 6
67                 8,                                       // UCxBRF = 8
68                 32,                                      // UCxBRS = 32
69                 EUSCI_A_UART_NO_PARITY,                  // No Parity
70                 EUSCI_A_UART_LSB_FIRST,                  // LSB First
71                 EUSCI_A_UART_ONE_STOP_BIT,               // One stop bit
72                 EUSCI_A_UART_MODE,                       // UART mode
73                 EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION  // Oversampling
74         };
75 #endif
79 int main(void)
80 {
82         /* Halting WDT  */
83     MAP_WDT_A_holdTimer();
85     /* Selecting P1.2 and P1.3 in UART mode */
86     MAP_GPIO_setAsPeripheralModuleFunctionInputPin(UART_PORT,
87                 UART_RX_PIN | UART_TX_PIN, GPIO_PRIMARY_MODULE_FUNCTION);
89         /* Set LEDS as output */
90     MAP_GPIO_setAsOutputPin(HID_PORT,LEDG|LEDR);
91     MAP_GPIO_setOutputLowOnPin(HID_PORT,LEDG|LEDR);
93     /* Setting DCO to 12MHz */
94     #if SYSFREQ==12000000
95        CS_setDCOCenteredFrequency(CS_DCO_FREQUENCY_12);
96     #else
97        #error "Undefinde CLock Frequency"
98     #endif
100     /* Configuring UART Module */
101     MAP_UART_initModule(EUSCI_A0_BASE, &uartConfig);
103     /* Enable UART module */
104     MAP_UART_enableModule(EUSCI_A0_BASE);
106     /* Enabling UART interrupts */
107     MAP_UART_enableInterrupt(EUSCI_A0_BASE, EUSCI_A_UART_RECEIVE_INTERRUPT);
108     MAP_Interrupt_enableInterrupt(INT_EUSCIA0);
109     MAP_Interrupt_enableSleepOnIsrExit();
110     MAP_Interrupt_enableMaster();   
112     /* Enable SysTick (Toggle 0.5s) */
113     MAP_SysTick_enableModule();
114     MAP_SysTick_setPeriod(SYSFREQ/2);
115     MAP_SysTick_enableInterrupt();
117     /* Enabling MASTER interrupts */
118     MAP_Interrupt_enableMaster();
120     while(1)
121     {
122         MAP_PCM_gotoLPM0();
123     }
126 /* EUSCI A0 UART ISR - Echoes data back to PC host */
127 void EUSCIA0_IRQHandler(void)
129     uint32_t status = MAP_UART_getEnabledInterruptStatus(EUSCI_A0_BASE);
131     MAP_UART_clearInterruptFlag(EUSCI_A0_BASE, status);
133         if(status & EUSCI_A_UART_RECEIVE_INTERRUPT)
134     {
135        MAP_UART_transmitData(EUSCI_A0_BASE, MAP_UART_receiveData(EUSCI_A0_BASE));
136     }
140 void SysTick_Handler(void)
142         MAP_GPIO_toggleOutputOnPin(HID_PORT,LEDG);