a86aee15b2d634d5b8d6432da872e7fdecd0522d
[i3-mote/i3-mote.git] / Basic-Test-Package / MSP432 / Test_MSP432_3wSPI_SlaveIRQ_CC2650_Master / main.c
1 /******************************************************************************
2  * MSP432 SPI - 3-wire Slave Echo
3  *
4  * This example shows how SPI slave echoes to SPI master using 3-wire mode.
5  *
6  * Incrementing data is sent by the master starting at 0x01. Received data is
7  * expected to be same as the previous transmission.  eUSCI RX ISR is used to
8  * handle communication with the CPU, normally in LPM0.
9  *
10  * Transactions are started by the slave (MSP432) through IRQ line.
11  *
12  * ACLK = ~32.768kHz, MCLK = SMCLK = DCO 12MHz
13  *
14  *                MSP432P401
15  *              -----------------
16  *             |                 |
17  *             |             P2.3|<- Data In (UCA1SIMO)
18  *             |                 |
19  *             |             P2.2|-> Data Out (UCA1SOMI)
20  *             |                 |
21  *             |             P2.1|<- Serial Clock Out (UCA1CLK)
22  *             |                 |
23  *             |             P2.0|<- CS
24  *             |                 |
25  *
26  * Use with Test_CC2650_3wSPI_Master_MSP432_SlaveIRQ
27  *
28  * I3Mote: B.Martinez
29 *******************************************************************************/
31 #include "i3mote.h"
33 /* DriverLib Includes */
34 #include "driverlib.h"
36 /* Standard Includes */
37 #include <stdint.h>
38 #include <stdbool.h>
39 #include <stdio.h>
41 //#define DEBUG_UART
42 #define UART_BAUD_115200
44 /* Statics */
45 static volatile uint8_t RXData = 0;
46 static volatile uint8_t RXDataCnt = 0;
48 static volatile uint8_t TXData = 0;
50 const eUSCI_SPI_SlaveConfig spiSlaveConfig =
51 {
52         EUSCI_B_SPI_MSB_FIRST,                          // MSB First
53         EUSCI_B_SPI_PHASE_DATA_CHANGED_ONFIRST_CAPTURED_ON_NEXT,  // Phase
54         EUSCI_B_SPI_CLOCKPOLARITY_INACTIVITY_HIGH,     // Normal Polarity
55         EUSCI_B_SPI_3PIN                               // 3wire mode
56 };
58 /* UART Configuration Parameter. These are the configuration parameters to
59  * make the eUSCI A UART module to operate with a 9600 baud rate.
60  * These values were calculated using the online calculator that TI provides at:
61  * http://software-dl.ti.com/msp430/msp430_public_sw/mcu/msp430/MSP430BaudRateConverter/index.html
62  */
63 #ifdef UART_BAUD_9600
64         const eUSCI_UART_Config uartConfig =
65         {
66                 EUSCI_A_UART_CLOCKSOURCE_SMCLK,          // SMCLK Clock Source
67                 78,                                      // BRDIV = 78
68                 2,                                       // UCxBRF = 2
69                 0,                                       // UCxBRS = 0
70                 EUSCI_A_UART_NO_PARITY,                  // No Parity
71                 EUSCI_A_UART_LSB_FIRST,                  // LSB First
72                 EUSCI_A_UART_ONE_STOP_BIT,               // One stop bit
73                 EUSCI_A_UART_MODE,                       // UART mode
74                 EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION  // Oversampling
75         };
76 #endif
78 #ifdef UART_BAUD_115200
79         const eUSCI_UART_Config uartConfig =
80         {
81                 EUSCI_A_UART_CLOCKSOURCE_SMCLK,          // SMCLK Clock Source
82                 6,                                      // BRDIV = 78
83                 8,                                       // UCxBRF = 2
84                 0,                                       // UCxBRS = 0
85                 EUSCI_A_UART_NO_PARITY,                  // No Parity
86                 EUSCI_A_UART_LSB_FIRST,                  // LSB First
87                 EUSCI_A_UART_ONE_STOP_BIT,               // One stop bit
88                 EUSCI_A_UART_MODE,                       // UART mode
89                 EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION  // Oversampling
90         };
91 #endif
94 int main(void)
95 {
96     volatile uint32_t ii;
98     /* Halting WDT  */
99     WDT_A_holdTimer();
101     /* Set LED Pins as Output */
102     MAP_GPIO_setAsOutputPin(HID_PORT,LEDG|LEDR);
103     MAP_GPIO_setOutputLowOnPin(HID_PORT,LEDG|LEDR);
105     /* Setting DCO to 12MHz */
106     CS_setDCOCenteredFrequency(CS_DCO_FREQUENCY_12);
108         /******************************************************************************/
109         /******************************************************************************/
111     /* Configure INT Pin */
112     MAP_GPIO_setOutputHighOnPin(CC2650_IRQ_PORT, CC2650_IRQ_PIN);
113     MAP_GPIO_setAsOutputPin(CC2650_IRQ_PORT, CC2650_IRQ_PIN);
115         /* CS Configuring P2.0 as Input */
116         MAP_GPIO_setAsInputPin(CC2650_SPI_PORT, CC2650_SPI_CSN_PIN);
118     /* Selecting P2.1, P2.2 and P2.3 in SPI mode */
119     GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P2,
120                 CC2650_SPI_CLK_PIN | CC2650_SPI_MOSI_PIN | CC2650_SPI_MISO_PIN, GPIO_PRIMARY_MODULE_FUNCTION);
122     /* Configuring SPI in 3wire master mode */
123     //SPI_initMaster(EUSCI_A1_BASE, &spiMasterConfig);
124     SPI_initSlave(EUSCI_A1_BASE, &spiSlaveConfig);
126     /* Enable SPI module */
127     SPI_enableModule(EUSCI_A1_BASE);
129     /* Enabling interrupts */
130     SPI_enableInterrupt(EUSCI_A1_BASE, EUSCI_A_SPI_RECEIVE_INTERRUPT);
131     Interrupt_enableSleepOnIsrExit();
132     Interrupt_enableInterrupt(INT_EUSCIA1);
133     MAP_Interrupt_enableMaster();
135         /******************************************************************************/
136         /******************************************************************************/
138     MAP_SysTick_enableModule();
139     MAP_SysTick_setPeriod(12000000);
140     MAP_Interrupt_enableSleepOnIsrExit();
141     MAP_SysTick_enableInterrupt();
143         /******************************************************************************/
144         /******************************************************************************/
146 #ifdef DEBUG_UART
148     /* Selecting P1.2 and P1.3 in UART mode */
149     MAP_GPIO_setAsPeripheralModuleFunctionInputPin(UART_PORT,
150                 UART_RX_PIN | UART_TX_PIN, GPIO_PRIMARY_MODULE_FUNCTION);
152     /* Configuring UART Module */
153     MAP_UART_initModule(EUSCI_A0_BASE, &uartConfig);
155     /* Enable UART module */
156     MAP_UART_enableModule(EUSCI_A0_BASE);
158     /* Enabling UART interrupts */
159     /*
160      * MAP_UART_enableInterrupt(EUSCI_A0_BASE, EUSCI_A_UART_RECEIVE_INTERRUPT);
161      * MAP_Interrupt_enableInterrupt(INT_EUSCIA0);
162      */
163 #endif
165         /******************************************************************************/
166         /******************************************************************************/
169     PCM_gotoLPM0();
170     __no_operation();
173 //******************************************************************************
174 //
175 //This is the EUSCI_A1 interrupt vector service routine.
176 //
177 //******************************************************************************
178 void EUSCIA1_IRQHandler(void)
181         int i;
183     uint32_t status = SPI_getEnabledInterruptStatus(EUSCI_A1_BASE);
185     SPI_clearInterruptFlag(EUSCI_A1_BASE, status);
187     if(status & EUSCI_A_SPI_RECEIVE_INTERRUPT)
188     {
190         MAP_GPIO_setOutputHighOnPin(GPIO_PORT_P6,GPIO_PIN3);
192         RXData = SPI_receiveData(EUSCI_A1_BASE);
193         RXDataCnt++;
195         printf("Cnt: %d Rx: %02X\n",RXDataCnt,RXData);
197         /* Extrenal UART Debug */
198                 #ifdef DEBUG_UART
199             MAP_UART_transmitData(EUSCI_A0_BASE, TXData);
200             MAP_UART_transmitData(EUSCI_A0_BASE, RXData);
201                 #endif
203         /* Send the next data packet */
205         /* USCI_A1 TX buffer ready? */
206         while (!(SPI_getInterruptStatus(EUSCI_A1_BASE, EUSCI_A_SPI_TRANSMIT_INTERRUPT)));
208                 SPI_transmitData(EUSCI_A1_BASE, RXData);
210         for(i=0;i<20000;i++);
211                 MAP_GPIO_toggleOutputOnPin(GPIO_PORT_P6,GPIO_PIN3);
213     }
217 void SysTick_Handler(void)
219     int i;
220         /* Generate Periodic Interrupt */
221         MAP_GPIO_setOutputLowOnPin(CC2650_IRQ_PORT, CC2650_IRQ_PIN);
222     for(i=0;i<10000;i++);
223     MAP_GPIO_setOutputHighOnPin(CC2650_IRQ_PORT, CC2650_IRQ_PIN);
225     //MAP_GPIO_toggleOutputOnPin(GPIO_PORT_P6, GPIO_PIN2);
229 //******************************************************************************
230 /* EUSCI A0 UART ISR - Echoes data back to PC host */
231 //******************************************************************************
232 /*
233  *
234  * void EUSCIA0_IRQHandler(void)
235  * {
236  *     uint32_t status = MAP_UART_getEnabledInterruptStatus(EUSCI_A0_BASE);
237  *
238  *     MAP_UART_clearInterruptFlag(EUSCI_A0_BASE, status);
239  *
240  *         if(status & EUSCI_A_UART_RECEIVE_INTERRUPT)
241  *     {
242  *        MAP_UART_transmitData(EUSCI_A0_BASE, MAP_UART_receiveData(EUSCI_A0_BASE));
243  *     }
244  *
245  * }
246  *
247  */