a112ea09f126986e7589e497ca4a148fab915dcb
[i3-mote/i3-mote.git] / Basic-Test-Package / MSP432 / Test_MSP432_I2C_Sensors / main.c
1 //****************************************************************************
2 //
3 //****************************************************************************
4 #include "i3mote.h"
5 #include "HAL_I2C.h"
7 /* DriverLib Includes */
8 #include <driverlib.h>
10 /* Standard Includes */
11 #include <stdlib.h>
12 #include <stdio.h>
15 /* UART Configuration Parameter. These are the configuration parameters to
16  * make the eUSCI A UART module to operate with a 9600 baud rate.
17  * These values were calculated using the online calculator that TI provides at:
18  * http://software-dl.ti.com/msp430/msp430_public_sw/mcu/msp430/MSP430BaudRateConverter/index.html
19  */
20 #ifdef UART_BAUD_9600
21         const eUSCI_UART_Config uartConfig =
22         {
23                 EUSCI_A_UART_CLOCKSOURCE_SMCLK,          // SMCLK Clock Source
24                 78,                                      // BRDIV = 78
25                 2,                                       // UCxBRF = 2
26                 0,                                       // UCxBRS = 0
27                 EUSCI_A_UART_NO_PARITY,                  // No Parity
28                 EUSCI_A_UART_LSB_FIRST,                  // LSB First
29                 EUSCI_A_UART_ONE_STOP_BIT,               // One stop bit
30                 EUSCI_A_UART_MODE,                       // UART mode
31                 EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION  // Oversampling
32         };
33 #endif
35 #ifdef UART_BAUD_115200
36         const eUSCI_UART_Config uartConfig =
37         {
38                 EUSCI_A_UART_CLOCKSOURCE_SMCLK,          // SMCLK Clock Source
39                 6,                                       // BRDIV = 6
40                 8,                                       // UCxBRF = 8
41                 32,                                      // UCxBRS = 32
42                 EUSCI_A_UART_NO_PARITY,                  // No Parity
43                 EUSCI_A_UART_LSB_FIRST,                  // LSB First
44                 EUSCI_A_UART_ONE_STOP_BIT,               // One stop bit
45                 EUSCI_A_UART_MODE,                       // UART mode
46                 EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION  // Oversampling
47         };
48 #endif
50 #define SYSFREQ 12000000
52 /* External UART */
53 #undef UART
55 /*
56  * Global Variables
57  */
58 #define BUFF_SIZE 4
59 char buffer[BUFF_SIZE];
60 unsigned int i=0;
61 unsigned short val;
64 void exit_err(int val, int err){
65     MAP_GPIO_setOutputHighOnPin(GPIO_PORT_P6, GPIO_PIN2);
66         exit(-1);
67 }
70 void main(void)
71 {
72         /* Halting WDT and disabling master interrupts */
73     MAP_WDT_A_holdTimer();
74     MAP_Interrupt_disableMaster();
76     /* Initializes Clock System: Required Freq 12Mhz */
77     MAP_CS_setDCOCenteredFrequency(CS_DCO_FREQUENCY_12);
78     MAP_CS_initClockSignal(CS_MCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_1 );
79     MAP_CS_initClockSignal(CS_HSMCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_1 );
80     MAP_CS_initClockSignal(CS_SMCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_1 );
81     MAP_CS_initClockSignal(CS_ACLK, CS_REFOCLK_SELECT, CS_CLOCK_DIVIDER_1);
83     /* Set LED as outputs and Initial Value */
84     MAP_GPIO_setAsOutputPin(HID_PORT,LEDR|LEDG);
85     MAP_GPIO_setOutputLowOnPin(HID_PORT,LEDR|LEDG);
87     /* Set ENA Pin as Output and Enable (Active Low) */
88     MAP_GPIO_setAsOutputPin(CTL_PWR_PORT,GPS_PWR_nEN_PIN);
89     MAP_GPIO_setOutputLowOnPin(CTL_PWR_PORT,GPS_PWR_nEN_PIN);
91     /* UART */
92     #ifdef UART
93                 /* Selecting P1.2 and P1.3 in UART mode */
94                 MAP_GPIO_setAsPeripheralModuleFunctionInputPin(UART_PORT,
95                                 UART_RX_PIN | UART_TX_PIN, GPIO_PRIMARY_MODULE_FUNCTION);
96                 /* Configuring UART Module */
97                 MAP_UART_initModule(EUSCI_A0_BASE, &uartConfig);
98                 /* Enable UART module */
99                 MAP_UART_enableModule(EUSCI_A0_BASE);
100                 /* Enabling UART interrupts */
101                 MAP_UART_enableInterrupt(EUSCI_A0_BASE,EUSCI_A_UART_RECEIVE_INTERRUPT);
102                 MAP_Interrupt_enableInterrupt(INT_EUSCIA0);
103                 MAP_Interrupt_enableSleepOnIsrExit();
104     #endif
106     /* Initialize I2C communication */
107     Init_I2C_GPIO();
108     I2C_init();
109         #ifndef UART
110                 printf("I2C Init\n\r");
111     #endif
113         /* INA219 */
114         #ifdef INA219
116                 I2C_setslave(Board_INA219_ADDR);
118                 val=I2C_read16(0x00);
120                 #ifndef UART
121                     printf("INA219 ConfReg (default) 0x399F: %04X\n",val);
122                 #endif
124                 if( (val&0xFFFF) != 0x399F)
125                         exit_err(0x399F,val);
127         #endif
131     /* OPT3001 */
132         #ifdef OPT3001
134                 I2C_setslave(Board_OPT3001_ADDR);
136                 val=I2C_read16(0x7E);
137                 #ifndef UART
138                     printf("OPT3001 Manufactured ID 0x5449: %04X\n",val);
139         #endif
140                 val=I2C_read16(0x7F);
142         #ifndef UART
143                         printf("OPT3001 Device ID 0x3001: %04X\n",val);
144         #endif
146                 if( (val&0xFFFF) != 0x3001)
147                         exit_err(0x3001,val);
149         #endif
152         /* HDC1080 */
153         #ifdef HDC1080
155                 I2C_setslave(Board_HDC1080_ADDR);
157                 val=I2C_read16(0xFE);
158         #ifndef UART
159                     printf("HDC1080 Manufactured ID 0x5449: %04X\n",val);
160             #endif
162                 val=I2C_read16(0xFF);
163                 #ifndef UART
164                     printf("HDC1080 Device ID 0x1050: %04X\n",val);
165                 #endif
167                 if( (val&0xFFFF) != 0x1050)
168                         exit_err(0x1050,val);
170         #endif
173     /* TMP007 */
174         #ifdef TMP007
176                 I2C_setslave(Board_TMP007_ADDR);
178                 val=I2C_read16(0x1E);
179                 #ifndef UART
180                     printf("TMP007 Manufactured ID 0x5449: %04X\n",val);
181         #endif
183                 val=I2C_read16(0x1F);
184                 #ifndef UART
185                     printf("TMP007 Device ID 0x0078: %04X\n",val);
186         #endif
188                 if( (val&0xFFFF) != 0x0078)
189                         exit_err(0x0078,val);
190         #endif
192     /* BMP280 */
193         #ifdef BMP280
195                 I2C_setslave(Board_BMP280_ADDR);
197                 val=I2C_read8(0xD0);
198         #ifndef UART
199                     printf("BMP280 ID 0x58: %02X\n",val);
200         #endif
202                 if( (val&0xFF) != 0x58)
203                         exit_err(0x58,val);
204         #endif
206     /* MPU9250 */
207         #ifdef MPU9250
209                 I2C_setslave(Board_MPU9250_ADDR);
211                 val=I2C_read8(0x75);
212                 #ifndef UART
213                     printf("MPU9250 WHOIAM 0x71: %02X\n",val);
214         #endif
216                 if( (val&0xFF) != 0x71)
217                                 exit_err(0x71,val);
218         #endif
220         /* Done */
221     MAP_GPIO_setOutputHighOnPin(CTL_PWR_PORT,GPS_PWR_nEN_PIN);
223     /*
224      * Configuring SysTick to toggle every 0.25s
225      */
226     MAP_SysTick_enableModule();
227     MAP_SysTick_setPeriod(SYSFREQ/4);
228     MAP_Interrupt_enableSleepOnIsrExit();
229     MAP_SysTick_enableInterrupt();
231     /* Enabling MASTER interrupts */
232     MAP_Interrupt_enableMaster();
234     while(1){
235         MAP_PCM_gotoLPM0();
236     }
241 void SysTick_Handler(void)
243         MAP_GPIO_toggleOutputOnPin(HID_PORT,LEDG);
246 /* EUSCI A0 UART ISR - Nothing to do now */
247 void EUSCIA0_IRQHandler(void)
250         /*
251          * EUSCI A0 UART ISR - Echoes data back to PC host
252          *
253     uint32_t status = MAP_UART_getEnabledInterruptStatus(EUSCI_A0_BASE);
254         MAP_UART_clearInterruptFlag(EUSCI_A0_BASE, status);
256         if(status & EUSCI_A_UART_RECEIVE_INTERRUPT)
257     {
258            MAP_GPIO_toggleOutputOnPin(HID_PORT,LEDR);
259        MAP_UART_transmitData(EUSCI_A0_BASE, MAP_UART_receiveData(EUSCI_A0_BASE));
260     }
261         **/