Added Test Wake-Up
[i3-mote/i3-mote.git] / Basic-Test-Package / MSP432 / Test_MSP432_ClockSystem / main.c
1 /*******************************************************************************
2  * MSP432 Clock System - HFXT Startup
3  *
4  * Description:
5  *
6  * HFXTAL:
7  * Starts the 48MHz crystal attached to HFXTIN/HFXTOUT
8  * Sources MCLK from the crystal
9  *
10  * LFXTL:
11  * Starts the 32khz crystal attached to LFXTIN/LFXTOUT
12  * Sources AMCLK from crystal
13  *
14  * Internal DCO:
15  * Starts the DCO
16  * Sources SMCLK from DCO
17  *
18  * Blinks LEDs using SysTick (which is sourced from MCLK).
19  *
20  *
21  *
22  *              MSP432P401
23  *             ------------------
24  *         /|\|                  |
25  *          | |                  |
26  *          --|RST         P8.5  |---> LED
27  *            |                  |
28  *            |      PJ.3 HFXTIN |<--------
29  *            |                  |   < 48Mhz xTal >
30  *            |     PJ.2 HFXTOUT |<--------
31  *            |                  |
32  *            |       PJ.0 LFXIN |<--------
33  *            |                  |   < 32khz xTal >
34  *            |      PJ.1 LFXOUT |<--------
35  *            |                  |
36  *            |             P4.2 |--> ACLK   - 32.768 KHz
37  *            |             P4.3 |--> MCLK   - 12.000 MHz
38  *            |             P4.4 |--> HSMCLK -  6.000 MHz
39  *            |             P7.0 |--> SMCLK
40  *            |                  |
41  *
42  * Author: Timothy Logan
43  * Rev: B.Martinez
44  ******************************************************************************/
45 #include <MSP432_I3M_driverlib.h>
47 /* DriverLib Includes */
48 #include "driverlib.h"
50 /* Standard Includes */
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <stdint.h>
54 #include <stdbool.h>
56 #define DEBUG_UART
58 #ifdef UART_BAUD_9600
59         const eUSCI_UART_Config uartConfig =
60         {
61                 EUSCI_A_UART_CLOCKSOURCE_SMCLK,          // SMCLK Clock Source
62                 78,                                      // BRDIV = 78
63                 2,                                       // UCxBRF = 2
64                 0,                                       // UCxBRS = 0
65                 EUSCI_A_UART_NO_PARITY,                  // No Parity
66                 EUSCI_A_UART_LSB_FIRST,                  // LSB First
67                 EUSCI_A_UART_ONE_STOP_BIT,               // One stop bit
68                 EUSCI_A_UART_MODE,                       // UART mode
69                 EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION  // Oversampling
70         };
71 #endif
73 #ifdef UART_BAUD_115200
74         const eUSCI_UART_Config uartConfig =
75         {
76                 EUSCI_A_UART_CLOCKSOURCE_SMCLK,          // SMCLK Clock Source
77                 6,                                       // BRDIV = 78
78                 8,                                       // UCxBRF = 2
79                 0,                                       // UCxBRS = 0
80                 EUSCI_A_UART_NO_PARITY,                  // No Parity
81                 EUSCI_A_UART_LSB_FIRST,                  // LSB First
82                 EUSCI_A_UART_ONE_STOP_BIT,               // One stop bit
83                 EUSCI_A_UART_MODE,                       // UART mode
84                 EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION  // Oversampling
85         };
86 #endif
90 /*
91  *  Timer_A0 (Fast)
92  *  UpMode Configuration Parameter
93  */
94 const Timer_A_UpModeConfig upConfigA0 =
95 {
96         TIMER_A_CLOCKSOURCE_SMCLK,             // SMCLK Clock Source
97         TIMER_A_CLOCKSOURCE_DIVIDER_1,         // SMCLK/1 = 3MHz
98         6000-1,                                // 6000000/12000 > 1ms tick period
99         TIMER_A_TAIE_INTERRUPT_DISABLE,        // Disable Timer interrupt
100         TIMER_A_CCIE_CCR0_INTERRUPT_ENABLE ,   // Enable CCR0 interrupt
101         TIMER_A_DO_CLEAR                       // Clear value
102 };
105 /*
106  *  Timer_A (Slow)
107  *  UpMode Configuration Parameter
108  */
109 const Timer_A_UpModeConfig upConfigA1 =
111         TIMER_A_CLOCKSOURCE_ACLK,              // ACLK Clock Source
112         TIMER_A_CLOCKSOURCE_DIVIDER_1,         // ACLK/1 = 3MHz
113         32768-1,
114         //32-1,                                // 32768/32=1024 tick period
115         TIMER_A_TAIE_INTERRUPT_DISABLE,        // Disable Timer interrupt
116         TIMER_A_CCIE_CCR0_INTERRUPT_ENABLE ,   // Enable CCR0 interrupt
117         TIMER_A_DO_CLEAR                       // Clear value
118 };
121 uint16_t count;
122 uint16_t count_tmp;
124 #define MAXLEN 32
125 char tmp[MAXLEN];
127 int main(void)
130         int i=0;
132     /* Halting the Watchdog */
133     MAP_WDT_A_holdTimer();
134     
135     /* Configuring pins for peripheral/crystal usage*/
136     /* LFXTAL */
137     MAP_GPIO_setAsPeripheralModuleFunctionOutputPin(LFXTAL_PORT,
138                 LFXTAL_PINS, GPIO_PRIMARY_MODULE_FUNCTION);
140     /* HFXTL */
141     MAP_GPIO_setAsPeripheralModuleFunctionOutputPin(HFXTAL_PORT,
142                 HFXTAL_PINS, GPIO_PRIMARY_MODULE_FUNCTION);
144     /* Clocks Output */
145     GPIO_setAsPeripheralModuleFunctionOutputPin(ACLK_OUT_PORT,
146                 ACLK_OUT_PIN, GPIO_PRIMARY_MODULE_FUNCTION);
147     GPIO_setAsPeripheralModuleFunctionOutputPin(MCLK_OUT_PORT,
148                 MCLK_OUT_PIN, GPIO_PRIMARY_MODULE_FUNCTION);
149     GPIO_setAsPeripheralModuleFunctionOutputPin(HSMCLK_OUT_PORT,
150             HSMCLK_OUT_PIN, GPIO_PRIMARY_MODULE_FUNCTION);
151     GPIO_setAsPeripheralModuleFunctionOutputPin(SMCLK_OUT_PORT,
152                 SMCLK_OUT_PIN, GPIO_PRIMARY_MODULE_FUNCTION);
154     /* LEDs */
155     MAP_GPIO_setAsOutputPin(HID_PORT,LEDG|LEDR);
156     MAP_GPIO_setOutputHighOnPin(HID_PORT,LEDG|LEDR);
158     /* Setting the external clock frequency. This API is optional */
159     CS_setExternalClockSourceFrequency(32768,48000000);
161     /* Starting HFXT in non-bypass mode without a timeout. Before we start
162      * we have to change VCORE to 1 to support the 48MHz frequency */
163     MAP_PCM_setCoreVoltageLevel(PCM_VCORE1);
164     MAP_FlashCtl_setWaitState(FLASH_BANK0, 2);
165     MAP_FlashCtl_setWaitState(FLASH_BANK1, 2);
166     CS_startHFXT(false);
169     /* Initializes other Clocks with DCO */
170     MAP_CS_setDCOCenteredFrequency(CS_DCO_FREQUENCY_48);
172     /* Initializing MCLK DCO/4 = 12 */
173     MAP_CS_initClockSignal(CS_MCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_4);
175     /* Initializing SMCLK to HFXT (effectively 48/8=6 MHz) */
176     MAP_CS_initClockSignal(CS_SMCLK,CS_HFXTCLK_SELECT,CS_CLOCK_DIVIDER_8);
178     /* Initializing ACLK to LFXT */
179     CS_startLFXT(false);
180     MAP_CS_initClockSignal(CS_ACLK, CS_LFXTCLK_SELECT, CS_CLOCK_DIVIDER_1);
182     /* HSMCLK DCO/16 = 3 */
183     MAP_CS_initClockSignal(CS_HSMCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_16);
187 #ifdef DEBUG_UART
189     /* Selecting P1.2 and P1.3 in UART mode */
190     MAP_GPIO_setAsPeripheralModuleFunctionInputPin(UART_PORT,UART_RX_PIN | UART_TX_PIN, GPIO_PRIMARY_MODULE_FUNCTION);
191     /* Configuring UART Module */
192     MAP_UART_initModule(EUSCI_A0_BASE, &uartConfig);
193     /* Enable UART module */
194     MAP_UART_enableModule(EUSCI_A0_BASE);
196     /* Enabling UART interrupts
197      * MAP_UART_enableInterrupt(EUSCI_A0_BASE, EUSCI_A_UART_RECEIVE_INTERRUPT);
198      * MAP_Interrupt_enableInterrupt(INT_EUSCIA0);
199      */
200 #endif
202 #ifdef DEBUG_UART
204     /* Set Clock to DCO */
205     MAP_CS_initClockSignal(CS_SMCLK,CS_DCOCLK_SELECT,CS_CLOCK_DIVIDER_4);
207     /* Launch test */
208     while(MAP_UART_receiveData(EUSCI_A0_BASE)!='g');
209     MAP_GPIO_setOutputLowOnPin(HID_PORT,LEDG);
210     MAP_GPIO_setOutputHighOnPin(HID_PORT,LEDR);
213     /* Set SMCLK to test HFXTAL from Timer */
214     MAP_CS_initClockSignal(CS_SMCLK,CS_HFXTCLK_SELECT,CS_CLOCK_DIVIDER_8);
216 #endif
218     /*
219      * Configuring SysTick to trigger at 12000000
220      * MCLK is 12MHz so this will make it toggle every 1s
221      *
222                 MAP_SysTick_enableModule();
223                 MAP_SysTick_setPeriod(12000000);
224                 MAP_SysTick_enableInterrupt();
225         */
227     /* Configuring Timer_A0 for Up Mode, with SMCLK (HFXTAL) */
228     MAP_Timer_A_configureUpMode(TIMER_A0_BASE, &upConfigA0);
229     /* Enabling interrupts and starting the timer */
230     MAP_Interrupt_enableInterrupt(INT_TA0_0);
231     MAP_Timer_A_startCounter(TIMER_A0_BASE, TIMER_A_UP_MODE);
233     /* Configuring Timer_A1 for Up Mode with ACLK (LFXTAL) */
234     MAP_Timer_A_configureUpMode(TIMER_A1_BASE, &upConfigA1);
235     /* Enabling interrupts and starting the timer */
236     MAP_Interrupt_enableInterrupt(INT_TA1_0);
237     MAP_Timer_A_startCounter(TIMER_A1_BASE, TIMER_A_UP_MODE);
239     MAP_Interrupt_enableSleepOnIsrExit();
240     MAP_Interrupt_enableMaster();
241     MAP_GPIO_setOutputLowOnPin(HID_PORT,LEDG);
243         MAP_PCM_gotoLPM0();
245         /* Restore DCLK for UART */
246         MAP_CS_initClockSignal(CS_SMCLK,CS_DCOCLK_SELECT,CS_CLOCK_DIVIDER_4);
247         sprintf(tmp,"Timer-A Ticks: %d\r\n",count_tmp);
248         while(tmp[i]!='\0'){
249                 MAP_UART_transmitData(EUSCI_A0_BASE,tmp[i]);
250                 i++;
251         }
253         /* Do Nothing */
254     while(1){
255          MAP_PCM_gotoLPM0();
256          MAP_GPIO_toggleOutputOnPin(HID_PORT,LEDR);
257     }
262 void SysTick_Handler(void)
264     //MAP_UART_transmitData(EUSCI_A0_BASE, (count_tmp>>8)&0xFF);
265     //MAP_UART_transmitData(EUSCI_A0_BASE, (count_tmp)&0xFF);
268 //******************************************************************************
269 // TIMER
270 //******************************************************************************
271 void TA0_0_IRQHandler(void)
273     MAP_Timer_A_clearCaptureCompareInterrupt(TIMER_A0_BASE,TIMER_A_CAPTURECOMPARE_REGISTER_0);
274     MAP_GPIO_toggleOutputOnPin(HID_PORT, LEDR);
275     count++;
278 void TA1_0_IRQHandler(void)
280     MAP_Timer_A_clearCaptureCompareInterrupt(TIMER_A1_BASE,TIMER_A_CAPTURECOMPARE_REGISTER_0);
281     MAP_GPIO_toggleOutputOnPin(HID_PORT,LEDG);
283         count_tmp=count;
284         count=0;
286         /* Disable Ints */
287     MAP_Interrupt_disableInterrupt(INT_TA1_0);
288     MAP_Interrupt_disableInterrupt(INT_TA0_0);
289     /* Return Control to Main Program */
290     MAP_Interrupt_disableSleepOnIsrExit();