summaryrefslogtreecommitdiffstats
blob: d5f37318669da1511ab4b6e290bc77cd0eec9dc0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*******************************************************************************
 * MSP432 Clock System - HFXT Startup
 *
 * Description:
 *
 * HFXTAL:
 * Starts the 48MHz crystal attached to HFXTIN/HFXTOUT
 * Sources MCLK from the crystal
 *
 * LFXTL:
 * Starts the 32khz crystal attached to LFXTIN/LFXTOUT
 * Sources AMCLK from crystal
 *
 * Internal DCO:
 * Starts the DCO
 * Sources SMCLK from DCO
 *
 * Blinks LEDs using SysTick (which is sourced from MCLK).
 *
 *
 *
 *              MSP432P401
 *             ------------------
 *         /|\|                  |
 *          | |                  |
 *          --|RST         P8.5  |---> LED
 *            |                  |
 *            |      PJ.3 HFXTIN |<--------
 *            |                  |   < 48Mhz xTal >
 *            |     PJ.2 HFXTOUT |<--------
 *            |                  |
 *            |       PJ.0 LFXIN |<--------
 *            |                  |   < 32khz xTal >
 *            |      PJ.1 LFXOUT |<--------
 *            |                  |
 *            |             P4.2 |--> ACLK   - 32.768 KHz
 *            |             P4.3 |--> MCLK   - 12.000 MHz
 *            |             P4.4 |--> HSMCLK -  6.000 MHz
 *            |             P7.0 |--> SMCLK
 *            |                  |
 *
 * Author: Timothy Logan
 * Rev: B.Martinez
 ******************************************************************************/
#include <MSP432_I3M_driverlib.h>

/* DriverLib Includes */
#include "driverlib.h"

/* Standard Includes */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>

#define DEBUG_UART

#ifdef UART_BAUD_9600
	const eUSCI_UART_Config uartConfig =
	{
		EUSCI_A_UART_CLOCKSOURCE_SMCLK,          // SMCLK Clock Source
		78,                                      // BRDIV = 78
		2,                                       // UCxBRF = 2
		0,                                       // UCxBRS = 0
		EUSCI_A_UART_NO_PARITY,                  // No Parity
		EUSCI_A_UART_LSB_FIRST,                  // LSB First
		EUSCI_A_UART_ONE_STOP_BIT,               // One stop bit
		EUSCI_A_UART_MODE,                       // UART mode
		EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION  // Oversampling
	};
#endif

#ifdef UART_BAUD_115200
	const eUSCI_UART_Config uartConfig =
	{
		EUSCI_A_UART_CLOCKSOURCE_SMCLK,          // SMCLK Clock Source
		6,                                       // BRDIV = 78
		8,                                       // UCxBRF = 2
		0,                                       // UCxBRS = 0
		EUSCI_A_UART_NO_PARITY,                  // No Parity
		EUSCI_A_UART_LSB_FIRST,                  // LSB First
		EUSCI_A_UART_ONE_STOP_BIT,               // One stop bit
		EUSCI_A_UART_MODE,                       // UART mode
		EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION  // Oversampling
	};
#endif



/*
 *  Timer_A0 (Fast)
 *  UpMode Configuration Parameter
 */
const Timer_A_UpModeConfig upConfigA0 =
{
	TIMER_A_CLOCKSOURCE_SMCLK,             // SMCLK Clock Source
	TIMER_A_CLOCKSOURCE_DIVIDER_1,         // SMCLK/1 = 3MHz
	6000-1,                                // 6000000/12000 > 1ms tick period
	TIMER_A_TAIE_INTERRUPT_DISABLE,        // Disable Timer interrupt
	TIMER_A_CCIE_CCR0_INTERRUPT_ENABLE ,   // Enable CCR0 interrupt
	TIMER_A_DO_CLEAR                       // Clear value
};


/*
 *  Timer_A (Slow)
 *  UpMode Configuration Parameter
 */
const Timer_A_UpModeConfig upConfigA1 =
{
	TIMER_A_CLOCKSOURCE_ACLK,              // ACLK Clock Source
	TIMER_A_CLOCKSOURCE_DIVIDER_1,         // ACLK/1 = 3MHz
	32768-1,
	//32-1,                                // 32768/32=1024 tick period
	TIMER_A_TAIE_INTERRUPT_DISABLE,        // Disable Timer interrupt
	TIMER_A_CCIE_CCR0_INTERRUPT_ENABLE ,   // Enable CCR0 interrupt
	TIMER_A_DO_CLEAR                       // Clear value
};


uint16_t count;
uint16_t count_tmp;

#define MAXLEN 32
char tmp[MAXLEN];

int main(void)
{

	int i=0;

    /* Halting the Watchdog */
    MAP_WDT_A_holdTimer();
    
    /* Configuring pins for peripheral/crystal usage*/
    /* LFXTAL */
    MAP_GPIO_setAsPeripheralModuleFunctionOutputPin(LFXTAL_PORT,
    		LFXTAL_PINS, GPIO_PRIMARY_MODULE_FUNCTION);

    /* HFXTL */
    MAP_GPIO_setAsPeripheralModuleFunctionOutputPin(HFXTAL_PORT,
    		HFXTAL_PINS, GPIO_PRIMARY_MODULE_FUNCTION);

    /* Clocks Output */
    GPIO_setAsPeripheralModuleFunctionOutputPin(ACLK_OUT_PORT,
    		ACLK_OUT_PIN, GPIO_PRIMARY_MODULE_FUNCTION);
    GPIO_setAsPeripheralModuleFunctionOutputPin(MCLK_OUT_PORT,
        	MCLK_OUT_PIN, GPIO_PRIMARY_MODULE_FUNCTION);
    GPIO_setAsPeripheralModuleFunctionOutputPin(HSMCLK_OUT_PORT,
            HSMCLK_OUT_PIN, GPIO_PRIMARY_MODULE_FUNCTION);
    GPIO_setAsPeripheralModuleFunctionOutputPin(SMCLK_OUT_PORT,
    		SMCLK_OUT_PIN, GPIO_PRIMARY_MODULE_FUNCTION);

    /* LEDs */
    MAP_GPIO_setAsOutputPin(HID_PORT,LEDG|LEDR);
    MAP_GPIO_setOutputHighOnPin(HID_PORT,LEDG|LEDR);

    /* Setting the external clock frequency. This API is optional */
    CS_setExternalClockSourceFrequency(32768,12000000);

    /* Starting HFXT in non-bypass mode without a timeout. Before we start
     * we have to change VCORE to 1 to support the 48MHz frequency */
    MAP_PCM_setCoreVoltageLevel(PCM_VCORE1);
    MAP_FlashCtl_setWaitState(FLASH_BANK0, 2);
    MAP_FlashCtl_setWaitState(FLASH_BANK1, 2);
    CS_startHFXT(false);


    /* Initializes other Clocks with DCO */
    MAP_CS_setDCOCenteredFrequency(CS_DCO_FREQUENCY_48);

    /* Initializing MCLK DCO/4 = 12 */
    MAP_CS_initClockSignal(CS_MCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_4);

    /* Initializing SMCLK to HFXT (effectively 48/8=6 MHz) */
    MAP_CS_initClockSignal(CS_SMCLK,CS_HFXTCLK_SELECT,CS_CLOCK_DIVIDER_8);

    /* Initializing ACLK to LFXT */
    CS_startLFXT(false);
    MAP_CS_initClockSignal(CS_ACLK, CS_LFXTCLK_SELECT, CS_CLOCK_DIVIDER_1);

    /* HSMCLK DCO/16 = 3 */
    MAP_CS_initClockSignal(CS_HSMCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_16);



#ifdef DEBUG_UART

    /* Selecting P1.2 and P1.3 in UART mode */
    MAP_GPIO_setAsPeripheralModuleFunctionInputPin(UART_PORT,UART_RX_PIN | UART_TX_PIN, GPIO_PRIMARY_MODULE_FUNCTION);
    /* Configuring UART Module */
    MAP_UART_initModule(EUSCI_A0_BASE, &uartConfig);
    /* Enable UART module */
    MAP_UART_enableModule(EUSCI_A0_BASE);

    /* Enabling UART interrupts
     * MAP_UART_enableInterrupt(EUSCI_A0_BASE, EUSCI_A_UART_RECEIVE_INTERRUPT);
     * MAP_Interrupt_enableInterrupt(INT_EUSCIA0);
     */
#endif

#ifdef DEBUG_UART

    /* Set Clock to DCO */
    MAP_CS_initClockSignal(CS_SMCLK,CS_DCOCLK_SELECT,CS_CLOCK_DIVIDER_4);

    /* Launch test */
    while(MAP_UART_receiveData(EUSCI_A0_BASE)!='g');
    MAP_GPIO_setOutputLowOnPin(HID_PORT,LEDG);
    MAP_GPIO_setOutputHighOnPin(HID_PORT,LEDR);


    /* Set SMCLK to test HFXTAL from Timer */
    MAP_CS_initClockSignal(CS_SMCLK,CS_HFXTCLK_SELECT,CS_CLOCK_DIVIDER_8);

#endif

    /*
     * Configuring SysTick to trigger at 12000000
     * MCLK is 12MHz so this will make it toggle every 1s
     *
		MAP_SysTick_enableModule();
		MAP_SysTick_setPeriod(12000000);
		MAP_SysTick_enableInterrupt();
	*/

    /* Configuring Timer_A0 for Up Mode, with SMCLK (HFXTAL) */
    MAP_Timer_A_configureUpMode(TIMER_A0_BASE, &upConfigA0);
    /* Enabling interrupts and starting the timer */
    MAP_Interrupt_enableInterrupt(INT_TA0_0);
    MAP_Timer_A_startCounter(TIMER_A0_BASE, TIMER_A_UP_MODE);

    /* Configuring Timer_A1 for Up Mode with ACLK (LFXTAL) */
    MAP_Timer_A_configureUpMode(TIMER_A1_BASE, &upConfigA1);
    /* Enabling interrupts and starting the timer */
    MAP_Interrupt_enableInterrupt(INT_TA1_0);
    MAP_Timer_A_startCounter(TIMER_A1_BASE, TIMER_A_UP_MODE);

    MAP_Interrupt_enableSleepOnIsrExit();
    MAP_Interrupt_enableMaster();
    MAP_GPIO_setOutputLowOnPin(HID_PORT,LEDG);

	MAP_PCM_gotoLPM0();

	/* Restore DCLK for UART */
	MAP_CS_initClockSignal(CS_SMCLK,CS_DCOCLK_SELECT,CS_CLOCK_DIVIDER_4);
	sprintf(tmp,"Timer-A Ticks: %d\r\n",count_tmp);
	while(tmp[i]!='\0'){
		MAP_UART_transmitData(EUSCI_A0_BASE,tmp[i]);
		i++;
	}

	/* Do Nothing */
    while(1){
    	 MAP_PCM_gotoLPM0();
    	 MAP_GPIO_toggleOutputOnPin(HID_PORT,LEDR);
    }

}


void SysTick_Handler(void)
{
    //MAP_UART_transmitData(EUSCI_A0_BASE, (count_tmp>>8)&0xFF);
    //MAP_UART_transmitData(EUSCI_A0_BASE, (count_tmp)&0xFF);
}

//******************************************************************************
// TIMER
//******************************************************************************
void TA0_0_IRQHandler(void)
{
    MAP_Timer_A_clearCaptureCompareInterrupt(TIMER_A0_BASE,TIMER_A_CAPTURECOMPARE_REGISTER_0);
    MAP_GPIO_toggleOutputOnPin(HID_PORT, LEDR);
    count++;
}

void TA1_0_IRQHandler(void)
{
    MAP_Timer_A_clearCaptureCompareInterrupt(TIMER_A1_BASE,TIMER_A_CAPTURECOMPARE_REGISTER_0);
    MAP_GPIO_toggleOutputOnPin(HID_PORT,LEDG);

	count_tmp=count;
	count=0;

	/* Disable Ints */
    MAP_Interrupt_disableInterrupt(INT_TA1_0);
    MAP_Interrupt_disableInterrupt(INT_TA0_0);
    /* Return Control to Main Program */
    MAP_Interrupt_disableSleepOnIsrExit();

}