6240674ce10987fa312370b66aa39f541ecf9459
[i3-mote/i3-mote.git] / Basic-Test-Package / MSP432 / Test_MSP432_3wSPI_M25P40 / main.c
1 /******************************************************************************
2  * MSP432 SPI - 3-wire Master Interface to on-board M24P40
3  *
4  * Reads M25P40 Chip ID:
5  *
6  * ACLK = ~32.768kHz, MCLK = SMCLK = DCO 12MHz
7  *
8  *
9  *                MSP432P401
10  *              -----------------
11  *             |                 |
12  *             |             P1.4|-> FLASH_SPI_CSN (GPIO)
13  *             |                 |
14  *             |             P1.5|-> FLASH_SPI_CLK (UCB0CLK)
15  *             |                 |
16  *             |             P1.6|-> FLASH_SPI_MOSI_PIN (UCB0SIMO)
17  *             |                 |
18  *             |             P1.7|<- FLASH_SPI_MISO_PIN (UCB0SOMI)
19  *             |                 |
20  *
21  * Author: B.Martinez
22 *******************************************************************************/
23 #include "i3mote.h"
26 /* DriverLib Includes */
27 #include "driverlib.h"
29 /* Standard Includes */
30 #include <stdint.h>
31 #include <stdlib.h>
32 #include <stdbool.h>
33 #include <stdio.h>
35 /* Statics */
36 static volatile uint8_t RXData = 0;
37 static volatile uint8_t RXDataCnt = 0;
38 static uint8_t TXData = 0;
40 #define SYSFREQ 12000000
42 /* SPI Master Configuration Parameter */
43 const eUSCI_SPI_MasterConfig spiMasterConfig =
44 {
45         EUSCI_A_SPI_CLOCKSOURCE_SMCLK,            // SMCLK Clock Source
46         SYSFREQ,                                  // SMCLK = DCO = 12MHZ
47         100000,                                   // SPICLK = 100kbps
48         EUSCI_A_SPI_MSB_FIRST,                    // MSB First
49         EUSCI_A_SPI_PHASE_DATA_CHANGED_ONFIRST_CAPTURED_ON_NEXT,  // Phase (Default)
50         EUSCI_A_SPI_CLOCKPOLARITY_INACTIVITY_HIGH, // High polarity
51         EUSCI_A_SPI_3PIN
52 };
55 int main(void)
56 {
57     volatile uint32_t ii;
59     /* Halting WDT  */
60     WDT_A_holdTimer();
62     /* Initializes Clock System */
63     MAP_CS_setDCOCenteredFrequency(CS_DCO_FREQUENCY_12);
64     MAP_CS_initClockSignal(CS_MCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_1 );
65     MAP_CS_initClockSignal(CS_HSMCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_1 );
66     MAP_CS_initClockSignal(CS_SMCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_1 );
67     MAP_CS_initClockSignal(CS_ACLK, CS_REFOCLK_SELECT, CS_CLOCK_DIVIDER_1);
70     /* LEDS as output */
71     MAP_GPIO_setAsOutputPin(HID_PORT,LEDR|LEDG);
72     MAP_GPIO_setOutputLowOnPin(HID_PORT,LEDR|LEDG);
75     /* CS Configuring P3.0 as output */
76     MAP_GPIO_setAsOutputPin(FLASH_SPI_PORT, FLASH_SPI_CSN_PIN);
77     MAP_GPIO_setOutputHighOnPin(FLASH_SPI_PORT, FLASH_SPI_CSN_PIN);
79     /* SPI */
80     GPIO_setAsPeripheralModuleFunctionInputPin(FLASH_SPI_PORT,
81                 FLASH_SPI_CLK_PIN | FLASH_SPI_MISO_PIN | FLASH_SPI_MOSI_PIN, GPIO_PRIMARY_MODULE_FUNCTION);
84     /* Configuring SPI in 3wire master mode */
85     SPI_initMaster(EUSCI_A2_BASE, &spiMasterConfig);
87     /* Enable SPI module */
88     SPI_enableModule(EUSCI_A2_BASE);
90     /* Enabling interrupts */
91     SPI_enableInterrupt(EUSCI_A2_BASE, EUSCI_A_SPI_RECEIVE_INTERRUPT);
92     Interrupt_enableInterrupt(INT_EUSCIA2);
93     Interrupt_enableSleepOnIsrExit();
95     /* Polling to see if the TX buffer is ready */
96     while (!(SPI_getInterruptStatus(EUSCI_A2_BASE,EUSCI_A_SPI_TRANSMIT_INTERRUPT)));
98     // CS
99     MAP_GPIO_setOutputLowOnPin(FLASH_SPI_PORT,FLASH_SPI_CSN_PIN);
101     /* Transmitting first command to slave */
102     TXData=0x9E;
103     SPI_transmitData(EUSCI_A2_BASE, TXData);
105     //P6OUT |= BIT0;
106         //MAP_GPIO_setOutputHighOnPin(GPIO_PORT_P6, LEDG);
107         /* Enabling MASTER interrupts */
108         // MAP_Interrupt_enableMaster();
110     while(1)
111     {
112         MAP_PCM_gotoLPM0();
113     }
117 //******************************************************************************
118 //
119 //This is the EUSCI_A2 interrupt vector service routine.
120 //
121 //******************************************************************************
122 void EUSCIA2_IRQHandler(void)
124     uint32_t status = SPI_getEnabledInterruptStatus(EUSCI_A2_BASE);
125     uint32_t jj;
127     SPI_clearInterruptFlag(EUSCI_A2_BASE, status);
129     if(status & EUSCI_A_SPI_RECEIVE_INTERRUPT)
130     {
132         /* USCI_B0 TX buffer ready? */
133         while (!(SPI_getInterruptStatus(EUSCI_A2_BASE, EUSCI_A_SPI_TRANSMIT_INTERRUPT)));
135         RXData = SPI_receiveData(EUSCI_A2_BASE);
136         printf("%i %02X\n",RXDataCnt,RXData);
138         switch(RXDataCnt){
139                    case 1:
140                      if(RXData!=0x20){
141                          MAP_GPIO_setOutputHighOnPin(HID_PORT,LEDR);
142                          exit(-1);
143                      }
144            break;
145                    case 2:
146                      if(RXData!=0x71){
147                          MAP_GPIO_setOutputHighOnPin(HID_PORT,LEDR);
148                          exit(-1);
149                      }
150            break;
151            case 3:
152                      if(RXData!=0x15){
153                          MAP_GPIO_setOutputHighOnPin(HID_PORT,LEDR);
154                          exit(-1);
155                      }
156            break;
157                    case 4:
158                      if(RXData!=0x10){
159                          MAP_GPIO_setOutputHighOnPin(HID_PORT,LEDR);
160                          exit(-1);
161                      }
162            break;
163         }
165         RXDataCnt++;
167         if(RXDataCnt==5){
169             MAP_GPIO_setOutputHighOnPin(FLASH_SPI_PORT, FLASH_SPI_CSN_PIN);
171             /* Enable SysTick and Blink Forever */
172             MAP_SysTick_enableModule();
173                 MAP_SysTick_setPeriod(SYSFREQ/4);
174                 MAP_Interrupt_enableSleepOnIsrExit();
175                 MAP_SysTick_enableInterrupt();
177         }
178         else{
179             /* Send the next data packet */
180             SPI_transmitData(EUSCI_A2_BASE, ++TXData);
181         }
183         /* Delay between transmissions for slave to process information */
184         for(jj=50;jj<50;jj++);
185     }
188 void SysTick_Handler(void)
190         MAP_GPIO_toggleOutputOnPin(HID_PORT,LEDG);