afb520e338e072d42db8c7b24405f03ec98e8998
[i3-mote/i3-mote.git] / Basic-Test-Package / CC2650 / Test_CC2650_I2C_Sensors / main.c
1 /*
2  *  ======== empty_min.c ========
3  */
4 /* XDCtools Header files */
5 #include <xdc/std.h>
6 #include <xdc/runtime/System.h>
7 #include <xdc/runtime/Error.h>
9 /* BIOS Header files */
10 #include <ti/sysbios/BIOS.h>
11 #include <ti/sysbios/knl/Task.h>
12 #include <ti/sysbios/knl/Clock.h>
13 #include <ti/sysbios/knl/Semaphore.h>
14 #include <ti/sysbios/knl/Event.h>
16 /* TI-RTOS Header files */
17 #include <ti/drivers/I2C.h>
18 #include <ti/drivers/PIN.h>
19 // #include <ti/drivers/SPI.h>
20 #include <ti/drivers/UART.h>
21 // #include <ti/drivers/Watchdog.h>
23 /* Board Header files */
24 #include "Board.h"
26 #include "uart_printf.h"
28 #define TASKSTACKSIZE   (1024)
30 Task_Struct task0Struct;
31 Char task0Stack[TASKSTACKSIZE];
33 Task_Struct task1Struct;
34 Char task1Stack[TASKSTACKSIZE];
36 Task_Struct task2Struct;
37 Char task2Stack[TASKSTACKSIZE];
39 #define OPT3001
40 #define HDC1080
41 #define BMP280
42 #define TMP007
44 #undef MPU9250
45 #define LISHH12
48 /* Semaphore */
49 Semaphore_Struct semStruct;
50 Semaphore_Handle semHandle;
52 Event_Handle myEvent;
53 Error_Block eb;
55 /* Pin driver handle */
56 static PIN_Handle ledPinHandle;
57 static PIN_State ledPinState;
59 /*
60  * Application LED pin configuration table:
61  *   - All LEDs board LEDs are off.
62  */
63 PIN_Config ledPinTable[] = {
64     Board_LED0 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
65     Board_LED1 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
66     PIN_TERMINATE
67 };
69 /* Global memory storage for a PIN_Config table */
70 static PIN_Handle buttonPinHandle;
71 static PIN_State buttonPinState;
73 PIN_Config buttonPinTable[] = {
74     Board_BUTTON0  | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_NEGEDGE,
75     //Board_BUTTON1  | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_NEGEDGE,
76     PIN_TERMINATE
77 };
80 void buttonCallbackFxn(PIN_Handle handle, PIN_Id pinId) {
82         PIN_setOutputValue(ledPinHandle, Board_LED0,0);
83         PIN_setOutputValue(ledPinHandle, Board_LED1,0);
84     CPUdelay(8000*200);
85         PIN_setOutputValue(ledPinHandle, Board_LED0,1);
86         PIN_setOutputValue(ledPinHandle, Board_LED1,1);
88         //Event_post(myEvent,Event_Id_02);
89         Semaphore_post(semHandle);
91 }
94 Void uartFxn(UArg arg0, UArg arg1)
95 {
96         char input;
98         while (1) {
99                 // Blocking Read
100                 input=uart_getch();
101                 if(input=='g'){
102                     //Event_post(myEvent,Event_Id_02);
103                     Semaphore_post(semHandle);
104                 }
105                 Task_sleep((UInt)arg0);
106         }
110 Void taskFxn(UArg arg0, UArg arg1)
112         uint8_t         txBuffer[1];
113     uint8_t         rxBuffer[2];
114     I2C_Handle      i2c;
115     I2C_Params      i2cParams;
116     I2C_Transaction i2cTransaction;
118         int flagerr=0;
120     //UInt events;
122     /* Create I2C for usage */
123     I2C_Params_init(&i2cParams);
124     i2cParams.bitRate = I2C_100kHz;
125     i2c = I2C_open(Board_I2C0, &i2cParams);
126     if (i2c == NULL) {
127         System_abort("Error Initializing I2C\r\n");
128     }
130     System_printf("I2C Initialized\r\n");
132     System_printf("Press button:\r\n");
133         Semaphore_pend(semHandle, BIOS_WAIT_FOREVER);
136         /* Start test */
137     PIN_setOutputValue(ledPinHandle, Board_LED0,0);
138     PIN_setOutputValue(ledPinHandle, Board_LED1,0);
140         //while(1)
141     {
143 #ifdef OPT3001
144         /* OPT3001 */
145         txBuffer[0] = 0x7E;
146                 i2cTransaction.slaveAddress = Board_OPT3001_ADDR;
147                 i2cTransaction.writeBuf = txBuffer;
148                 i2cTransaction.writeCount = 1;
149                 i2cTransaction.readBuf = rxBuffer;
150                 i2cTransaction.readCount = 2;
152                 if (I2C_transfer(i2c, &i2cTransaction)) {
153                         uint16_t data = (rxBuffer[0] << 8) | (rxBuffer[1] << 0);
154                         if (data != 0x5449) {
155                                 Event_post(myEvent, Event_Id_00);
156                                 flagerr=1;
157                         }
158                         System_printf("OPT3001 ID 0x5449: %x\r\n",data);
159                 }else{
160                         System_printf("OPT3001 I2c Transaction Failed\r\n");
161                         Event_post(myEvent, Event_Id_00);
162                         flagerr=1;
163                 }
165 #endif
167 #ifdef HDC1080
168                 //txBuffer[0] = 0xFE;
169         txBuffer[0] = 0xFF;
170                 i2cTransaction.slaveAddress = Board_HDC1080_ADDR;
171                 i2cTransaction.writeBuf = txBuffer;
172                 i2cTransaction.writeCount = 1;
173                 i2cTransaction.readBuf = rxBuffer;
174                 i2cTransaction.readCount = 2;
176                 if (I2C_transfer(i2c, &i2cTransaction)) {
177                         uint16_t data = (rxBuffer[0] << 8) | (rxBuffer[1] << 0);
178                         if (data != 0x1050) {
179                                 Event_post(myEvent, Event_Id_00);
180                                 flagerr=1;
181                         }
182                         //System_printf("HDC1080 Manufactured ID 0x5449: %x\n",data);
183                         System_printf("HDC1080 Device ID 0x1050: %x\r\n",data);
184                 }
185                 else{
186                         System_printf("HDC1080 I2c Transaction Failed\r\n");
187                         Event_post(myEvent, Event_Id_00);
188                         flagerr=1;
189                 }
190 #endif
192 #ifdef TMP007
193                 /*
194         txBuffer[0] = 0x1E;
195                 i2cTransaction.slaveAddress = Board_TMP007_ADDR;
196                 i2cTransaction.writeBuf = txBuffer;
197                 i2cTransaction.writeCount = 1;
198                 i2cTransaction.readBuf = rxBuffer;
199                 i2cTransaction.readCount = 2;
201                 if (I2C_transfer(i2c, &i2cTransaction)) {
202                         uint16_t data = (rxBuffer[0] << 8) | (rxBuffer[1] << 0);
203                         if (data != 0x5449) {
204                                 Event_post(myEvent, Event_Id_00);
205                                 flagerr=1;
206                         }
207                         System_printf("TMP007 Manufactured ID 0x5449: %x\r\n",data);
208                 }else{
209                     Event_post(myEvent, Event_Id_00);
210                     flagerr=1;
211                 }
213                 */
215         txBuffer[0] = 0x1F;
216                 i2cTransaction.slaveAddress = Board_TMP007_ADDR;
217                 i2cTransaction.writeBuf = txBuffer;
218                 i2cTransaction.writeCount = 1;
219                 i2cTransaction.readBuf = rxBuffer;
220                 i2cTransaction.readCount = 2;
222                 if (I2C_transfer(i2c, &i2cTransaction)) {
223                         uint16_t data = (rxBuffer[0] << 8) | (rxBuffer[1] << 0);
224                         if (data != 0x0078) {
225                                 Event_post(myEvent, Event_Id_00);
226                                 flagerr=1;
227                         }
228                         System_printf("TMP007 Device ID 0x0078: %x\r\n",data);
229                 }else{
230                         System_printf("TMP007 I2c Transaction Failed\r\n");
231                         Event_post(myEvent, Event_Id_00);
232                         flagerr=1;
233                 }
235 #endif
237 #ifdef BMP280
238             /* BMP280 */
239         txBuffer[0] = 0xD0;
240                 i2cTransaction.slaveAddress = Board_BMP280_ADDR;
241                 i2cTransaction.writeBuf = txBuffer;
242                 i2cTransaction.writeCount = 1;
243                 i2cTransaction.readBuf = rxBuffer;
244                 i2cTransaction.readCount = 1;
246                 if (I2C_transfer(i2c, &i2cTransaction)) {
247                         uint16_t data = (rxBuffer[0]);
248                         if (data != 0x58) {
249                                 Event_post(myEvent, Event_Id_00);
250                                 flagerr=1;
251                         }
252                         System_printf("BMP280 ID 0x58: %x\r\n",data);
253                 }else{
254                         System_printf("BMP280 I2c Transaction Failed\r\n");
255                         Event_post(myEvent, Event_Id_00);
256                         flagerr=1;
257                 }
258 #endif
260 #ifdef MPU9250
261                 txBuffer[0] = 0x75;
262                 i2cTransaction.slaveAddress = Board_MPU9250_ADDR;
263                 i2cTransaction.writeBuf = txBuffer;
264                 i2cTransaction.writeCount = 1;
265                 i2cTransaction.readBuf = rxBuffer;
266                 i2cTransaction.readCount = 1;
268                 if (I2C_transfer(i2c, &i2cTransaction)) {
269                         uint8_t data = rxBuffer[0];
270                         if (data != 0x71) {
271                                 Event_post(myEvent, Event_Id_00);
272                                 flagerr=1;
273                         }
274                         System_printf("MPU9250 WHOIAM 0x71: %x\r\n",data);
275                 }else{
276                         System_printf("MPU9250 I2c Transaction Failed\r\n");
277                         Event_post(myEvent, Event_Id_00);
278                         flagerr=1;
279                 }
280 #endif
282 #ifdef LISHH12
284                 txBuffer[0] = 0x0F;
285                 i2cTransaction.slaveAddress = Board_LISHH12_ADDR;
286                 i2cTransaction.writeBuf = txBuffer;
287                 i2cTransaction.writeCount = 1;
288                 i2cTransaction.readBuf = rxBuffer;
289                 i2cTransaction.readCount = 1;
291                 if (I2C_transfer(i2c, &i2cTransaction)) {
292                         uint8_t data = rxBuffer[0];
293                         if (data != 0x41) {
294                                 Event_post(myEvent, Event_Id_00);
295                                 flagerr=1;
296                         }
297                         System_printf("LISHH12 WHOIAM 0x41: %x\r\n",data);
298                 }else{
299                         System_printf("LISHH12 I2c Transaction Failed\r\n");
300                         Event_post(myEvent, Event_Id_00);
301                         flagerr=1;
302                 }
303 #endif
305                 /* OK! */
306                 if(flagerr==0){
307                         Event_post(myEvent, Event_Id_01);
308                         System_printf("***\r\n");
309                         System_flush();
310                 }else{
311                         System_printf("KO!\r\n");
312                         System_flush();
313                 }
315                 Task_sleep((UInt)arg0);
316     }
318     /* Deinitialized I2C */
319     I2C_close(i2c);
322 /*
323  *  ======== heartBeatFxn ========
324  *  Toggle the Board_LED0. The Task_sleep is determined by arg0 which
325  *  is configured for the heartBeat Task instance.
326  */
327 Void heartBeatFxn(UArg arg0, UArg arg1)
330         UInt events;
332         events=Event_pend(myEvent,Event_Id_NONE,Event_Id_00+Event_Id_01,BIOS_WAIT_FOREVER);
334         if(events & Event_Id_00){
335           while (1)
336           {
337         Task_sleep((UInt)arg0);
338         PIN_setOutputValue(ledPinHandle, Board_LED0,!PIN_getOutputValue(Board_LED0));
339       }
340         }
342         if(events & Event_Id_01){
343       while (1)
344       {
345         Task_sleep((UInt)arg0);
346         PIN_setOutputValue(ledPinHandle, Board_LED1,!PIN_getOutputValue(Board_LED1));
347       }
348         }
353 /*
354  *  ======== main ========
355  */
358 int main(void)
360     Task_Params taskParams;
362     Semaphore_Params semParams;
364     /* Call board init functions */
365     Board_initGeneral();
366     Board_initI2C();
367     // Board_initSPI();
368     Board_initUART();
369     // Board_initWatchdog();
371     /* Default instance configuration params */
372     Error_init(&eb);
373     myEvent = Event_create(NULL,&eb);
374     if (myEvent == NULL) {
375         System_abort("Event create failed");
376     }
380     UART_Params uartParams;
381     UART_Params_init(&uartParams);
382     uartParams.baudRate = 115200;
383     //uartParams.readEcho = UART_ECHO_OFF;
384     UartPrintf_init(UART_open(Board_UART, &uartParams));
386     /* Construct Test Task thread */
387     Task_Params_init(&taskParams);
388     taskParams.arg0 = 100000 / Clock_tickPeriod;
389     taskParams.stackSize = TASKSTACKSIZE;
390     taskParams.stack = &task1Stack;
391     Task_construct(&task1Struct, (Task_FuncPtr)taskFxn, &taskParams, NULL);
394     /* Construct heartBeat Task  thread */
395     Task_Params_init(&taskParams);
396     taskParams.arg0 = 100000 / Clock_tickPeriod;
397     taskParams.stackSize = TASKSTACKSIZE;
398     taskParams.stack = &task0Stack;
399     Task_construct(&task0Struct, (Task_FuncPtr)heartBeatFxn, &taskParams, NULL);
401     /* Construct Uart Task  thread */
402     Task_Params_init(&taskParams);
403     taskParams.arg0 = 100000 / Clock_tickPeriod;
404     taskParams.stackSize = TASKSTACKSIZE;
405     taskParams.stack = &task2Stack;
406     Task_construct(&task2Struct, (Task_FuncPtr)uartFxn, &taskParams, NULL);
409     /* Construct Semaphore and Obtain instance handle */
410     Semaphore_Params_init(&semParams);
411     Semaphore_construct(&semStruct,0, &semParams);
412     semHandle = Semaphore_handle(&semStruct);
414     /* Setup callback for button pins */
415     buttonPinHandle = PIN_open(&buttonPinState, buttonPinTable);
416     if(!buttonPinHandle) {
417         System_abort("Error initializing button pins\n");
418     }
419     if (PIN_registerIntCb(buttonPinHandle, &buttonCallbackFxn) != 0) {
420         System_abort("Error registering button callback function");
421     }
423     /* Open LED pins */
424     ledPinHandle = PIN_open(&ledPinState, ledPinTable);
425     if(!ledPinHandle) {
426         System_abort("Error initializing board LED pins\n");
427     }
429     PIN_setOutputValue(ledPinHandle, Board_LED0,1);
430     PIN_setOutputValue(ledPinHandle, Board_LED1,1);
432     /* Start BIOS */
433     BIOS_start();
435     return (0);