Added Test Wake-Up
[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         CPUdelay(8000*200);
84         //Event_post(myEvent,Event_Id_02);
85         Semaphore_post(semHandle);
87 }
90 Void uartFxn(UArg arg0, UArg arg1)
91 {
92         char input;
94         while (1) {
95                 // Blocking Read
96                 input=uart_getch();
97                 if(input=='g'){
98                     //Event_post(myEvent,Event_Id_02);
99                     Semaphore_post(semHandle);
100                 }
101                 Task_sleep((UInt)arg0);
102         }
106 Void taskFxn(UArg arg0, UArg arg1)
108         uint8_t         txBuffer[1];
109     uint8_t         rxBuffer[2];
110     I2C_Handle      i2c;
111     I2C_Params      i2cParams;
112     I2C_Transaction i2cTransaction;
114         int flagerr=0;
116     //UInt events;
118     /* Create I2C for usage */
119     I2C_Params_init(&i2cParams);
120     i2cParams.bitRate = I2C_100kHz;
121     i2c = I2C_open(Board_I2C0, &i2cParams);
122     if (i2c == NULL) {
123         System_abort("Error Initializing I2C\r\n");
124     }
125     System_printf("I2C Initialized\r\n");
127     /* Wait Semaphore */
128     System_printf("Press Button:\r\n");
130     PIN_setOutputValue(ledPinHandle, Board_LED0,1);
131     PIN_setOutputValue(ledPinHandle, Board_LED1,1);
133     Semaphore_pend(semHandle, BIOS_WAIT_FOREVER);
135         /* Start test */
136     PIN_setOutputValue(ledPinHandle, Board_LED0,0);
137     PIN_setOutputValue(ledPinHandle, Board_LED1,0);
139         //while(1)
140     {
142 #ifdef OPT3001
143         /* OPT3001 */
144         txBuffer[0] = 0x7E;
145                 i2cTransaction.slaveAddress = Board_OPT3001_ADDR;
146                 i2cTransaction.writeBuf = txBuffer;
147                 i2cTransaction.writeCount = 1;
148                 i2cTransaction.readBuf = rxBuffer;
149                 i2cTransaction.readCount = 2;
151                 if (I2C_transfer(i2c, &i2cTransaction)) {
152                         uint16_t data = (rxBuffer[0] << 8) | (rxBuffer[1] << 0);
153                         if (data != 0x5449) {
154                                 Event_post(myEvent, Event_Id_00);
155                                 flagerr=1;
156                         }
157                         System_printf("OPT3001 Device ID [0x5449]: %x\r\n",data);
158                 }else{
159                         System_printf("OPT3001 I2c Transaction Failed\r\n");
160                         Event_post(myEvent, Event_Id_00);
161                         flagerr=1;
162                 }
164 #endif
166 #ifdef HDC1080
167                 //txBuffer[0] = 0xFE;
168         txBuffer[0] = 0xFF;
169                 i2cTransaction.slaveAddress = Board_HDC1080_ADDR;
170                 i2cTransaction.writeBuf = txBuffer;
171                 i2cTransaction.writeCount = 1;
172                 i2cTransaction.readBuf = rxBuffer;
173                 i2cTransaction.readCount = 2;
175                 if (I2C_transfer(i2c, &i2cTransaction)) {
176                         uint16_t data = (rxBuffer[0] << 8) | (rxBuffer[1] << 0);
177                         if (data != 0x1050) {
178                                 Event_post(myEvent, Event_Id_00);
179                                 flagerr=1;
180                         }
181                         //System_printf("HDC1080 Manufactured ID 0x5449: %x\n",data);
182                         System_printf("HDC1080 Device ID [0x1050]: %x\r\n",data);
183                 }
184                 else{
185                         System_printf("HDC1080 I2c Transaction Failed\r\n");
186                         Event_post(myEvent, Event_Id_00);
187                         flagerr=1;
188                 }
189 #endif
191 #ifdef TMP007
192                 /*
193         txBuffer[0] = 0x1E;
194                 i2cTransaction.slaveAddress = Board_TMP007_ADDR;
195                 i2cTransaction.writeBuf = txBuffer;
196                 i2cTransaction.writeCount = 1;
197                 i2cTransaction.readBuf = rxBuffer;
198                 i2cTransaction.readCount = 2;
200                 if (I2C_transfer(i2c, &i2cTransaction)) {
201                         uint16_t data = (rxBuffer[0] << 8) | (rxBuffer[1] << 0);
202                         if (data != 0x5449) {
203                                 Event_post(myEvent, Event_Id_00);
204                                 flagerr=1;
205                         }
206                         System_printf("TMP007 Manufactured ID 0x5449: %x\r\n",data);
207                 }else{
208                     Event_post(myEvent, Event_Id_00);
209                     flagerr=1;
210                 }
212                 */
214         txBuffer[0] = 0x1F;
215                 i2cTransaction.slaveAddress = Board_TMP007_ADDR;
216                 i2cTransaction.writeBuf = txBuffer;
217                 i2cTransaction.writeCount = 1;
218                 i2cTransaction.readBuf = rxBuffer;
219                 i2cTransaction.readCount = 2;
221                 if (I2C_transfer(i2c, &i2cTransaction)) {
222                         uint16_t data = (rxBuffer[0] << 8) | (rxBuffer[1] << 0);
223                         if (data != 0x0078) {
224                                 Event_post(myEvent, Event_Id_00);
225                                 flagerr=1;
226                         }
227                         System_printf("TMP007 Device ID [0x0078]: %x\r\n",data);
228                 }else{
229                         System_printf("TMP007 I2c Transaction Failed\r\n");
230                         Event_post(myEvent, Event_Id_00);
231                         flagerr=1;
232                 }
234 #endif
236 #ifdef BMP280
237             /* BMP280 */
238         txBuffer[0] = 0xD0;
239                 i2cTransaction.slaveAddress = Board_BMP280_ADDR;
240                 i2cTransaction.writeBuf = txBuffer;
241                 i2cTransaction.writeCount = 1;
242                 i2cTransaction.readBuf = rxBuffer;
243                 i2cTransaction.readCount = 1;
245                 if (I2C_transfer(i2c, &i2cTransaction)) {
246                         uint16_t data = (rxBuffer[0]);
247                         if (data != 0x58) {
248                                 Event_post(myEvent, Event_Id_00);
249                                 flagerr=1;
250                         }
251                         System_printf("BMP280 Device ID [0x58]: %x\r\n",data);
252                 }else{
253                         System_printf("BMP280 I2c Transaction Failed\r\n");
254                         Event_post(myEvent, Event_Id_00);
255                         flagerr=1;
256                 }
257 #endif
259 #ifdef MPU9250
260                 txBuffer[0] = 0x75;
261                 i2cTransaction.slaveAddress = Board_MPU9250_ADDR;
262                 i2cTransaction.writeBuf = txBuffer;
263                 i2cTransaction.writeCount = 1;
264                 i2cTransaction.readBuf = rxBuffer;
265                 i2cTransaction.readCount = 1;
267                 if (I2C_transfer(i2c, &i2cTransaction)) {
268                         uint8_t data = rxBuffer[0];
269                         if (data != 0x71) {
270                                 Event_post(myEvent, Event_Id_00);
271                                 flagerr=1;
272                         }
273                         System_printf("MPU9250 WHOIAM [0x71]: %x\r\n",data);
274                 }else{
275                         System_printf("MPU9250 I2c Transaction Failed\r\n");
276                         Event_post(myEvent, Event_Id_00);
277                         flagerr=1;
278                 }
279 #endif
281 #ifdef LISHH12
283                 txBuffer[0] = 0x0F;
284                 i2cTransaction.slaveAddress = Board_LISHH12_ADDR;
285                 i2cTransaction.writeBuf = txBuffer;
286                 i2cTransaction.writeCount = 1;
287                 i2cTransaction.readBuf = rxBuffer;
288                 i2cTransaction.readCount = 1;
290                 if (I2C_transfer(i2c, &i2cTransaction)) {
291                         uint8_t data = rxBuffer[0];
292                         if (data != 0x41) {
293                                 Event_post(myEvent, Event_Id_00);
294                                 flagerr=1;
295                         }
296                         System_printf("LISHH12 WHOIAM [0x41]: %x\r\n",data);
297                 }else{
298                         System_printf("LISHH12 I2c Transaction Failed\r\n");
299                         Event_post(myEvent, Event_Id_00);
300                         flagerr=1;
301                 }
302 #endif
304                 /* OK! */
305                 if(flagerr==0){
306                         Event_post(myEvent, Event_Id_01);
307                         System_printf("***\r\n");
308                         System_flush();
309                 }else{
310                         System_printf("KO!\r\n");
311                         System_flush();
312                 }
314                 Task_sleep((UInt)arg0);
315     }
317     /* Deinitialized I2C */
318     I2C_close(i2c);
321 /*
322  *  ======== heartBeatFxn ========
323  *  Toggle the Board_LED0. The Task_sleep is determined by arg0 which
324  *  is configured for the heartBeat Task instance.
325  */
326 Void heartBeatFxn(UArg arg0, UArg arg1)
329         UInt events;
331         events=Event_pend(myEvent,Event_Id_NONE,Event_Id_00+Event_Id_01,BIOS_WAIT_FOREVER);
333         if(events & Event_Id_00){
334           while (1)
335           {
336         Task_sleep((UInt)arg0);
337         PIN_setOutputValue(ledPinHandle, Board_LED0,!PIN_getOutputValue(Board_LED0));
338       }
339         }
341         if(events & Event_Id_01){
342       while (1)
343       {
344         Task_sleep((UInt)arg0);
345         PIN_setOutputValue(ledPinHandle, Board_LED1,!PIN_getOutputValue(Board_LED1));
346       }
347         }
352 /*
353  *  ======== main ========
354  */
357 int main(void)
359     Task_Params taskParams;
361     Semaphore_Params semParams;
363     /* Call board init functions */
364     Board_initGeneral();
365     Board_initI2C();
366     // Board_initSPI();
367     Board_initUART();
368     // Board_initWatchdog();
370     /* Default instance configuration params */
371     Error_init(&eb);
372     myEvent = Event_create(NULL,&eb);
373     if (myEvent == NULL) {
374         System_abort("Event create failed");
375     }
379     UART_Params uartParams;
380     UART_Params_init(&uartParams);
381     uartParams.baudRate = 115200;
382     //uartParams.readEcho = UART_ECHO_OFF;
383     UartPrintf_init(UART_open(Board_UART, &uartParams));
385     /* Construct Test Task thread */
386     Task_Params_init(&taskParams);
387     taskParams.arg0 = 100000 / Clock_tickPeriod;
388     taskParams.stackSize = TASKSTACKSIZE;
389     taskParams.stack = &task1Stack;
390     Task_construct(&task1Struct, (Task_FuncPtr)taskFxn, &taskParams, NULL);
393     /* Construct heartBeat Task  thread */
394     Task_Params_init(&taskParams);
395     taskParams.arg0 = 100000 / Clock_tickPeriod;
396     taskParams.stackSize = TASKSTACKSIZE;
397     taskParams.stack = &task0Stack;
398     Task_construct(&task0Struct, (Task_FuncPtr)heartBeatFxn, &taskParams, NULL);
400     /* Construct Uart Task  thread */
401     Task_Params_init(&taskParams);
402     taskParams.arg0 = 100000 / Clock_tickPeriod;
403     taskParams.stackSize = TASKSTACKSIZE;
404     taskParams.stack = &task2Stack;
405     Task_construct(&task2Struct, (Task_FuncPtr)uartFxn, &taskParams, NULL);
408     /* Construct Semaphore and Obtain instance handle */
409     Semaphore_Params_init(&semParams);
410     Semaphore_construct(&semStruct,0, &semParams);
411     semHandle = Semaphore_handle(&semStruct);
413     /* Setup callback for button pins */
414     buttonPinHandle = PIN_open(&buttonPinState, buttonPinTable);
415     if(!buttonPinHandle) {
416         System_abort("Error initializing button pins\n");
417     }
418     if (PIN_registerIntCb(buttonPinHandle, &buttonCallbackFxn) != 0) {
419         System_abort("Error registering button callback function");
420     }
422     /* Open LED pins */
423     ledPinHandle = PIN_open(&ledPinState, ledPinTable);
424     if(!ledPinHandle) {
425         System_abort("Error initializing board LED pins\n");
426     }
428     PIN_setOutputValue(ledPinHandle, Board_LED0,1);
429     PIN_setOutputValue(ledPinHandle, Board_LED1,1);
431     /* Start BIOS */
432     BIOS_start();
434     return (0);