Added Test Wake-Up
[i3-mote/i3-mote.git] / Basic-Test-Package / CC2650 / Test_CC2650_3wSPI_DAC8730 / 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"
25 #include "uart_printf.h"
27 #define TASKSTACKSIZE   512
29 Task_Struct task0Struct;
30 Char task0Stack[TASKSTACKSIZE];
32 Task_Struct task1Struct;
33 Char task1Stack[TASKSTACKSIZE];
35 Task_Struct task2Struct;
36 Char task2Stack[TASKSTACKSIZE];
40 Event_Handle myEvent;
41 Error_Block eb;
43 Semaphore_Struct semStruct;
44 Semaphore_Handle semHandle;
47 /*
48  * Application LED pin configuration table:
49  *   - All LEDs board LEDs are off.
50  */
51 static PIN_Handle ledPinHandle;
52 static PIN_State ledPinState;
54 PIN_Config ledPinTable[] = {
55     Board_LED0 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
56     Board_LED1 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
57         PIN_TERMINATE
58 };
60 /**
61 static PIN_Handle csnPinHandle;
62 static PIN_State csnPinState;
64 PIN_Config csnPinTable[] = {
65         Board_DAC8730_CSN| PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
66         PIN_TERMINATE
67 };
68 **/
70 /* Global memory storage for a PIN_Config table */
71 static PIN_Handle buttonPinHandle;
72 static PIN_State buttonPinState;
74 PIN_Config buttonPinTable[] = {
75     Board_BUTTON0  | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_NEGEDGE,
76     //Board_BUTTON1  | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_NEGEDGE,
77     PIN_TERMINATE
78 };
80 void buttonCallbackFxn(PIN_Handle handle, PIN_Id pinId) {
82         CPUdelay(8000*200);
84         Semaphore_post(semHandle);
85         //Event_post(myEvent,Event_Id_02);
87 }
89 Void uartFxn(UArg arg0, UArg arg1)
90 {
91         char input;
93         while (1) {
94                 // Blocking Read
95                 input=uart_getch();
96                 if(input=='g'){
97                     //Event_post(myEvent,Event_Id_02);
98                     Semaphore_post(semHandle);
99                 }
100             Task_sleep((UInt)arg0);
101         }
107 #define REGSBW 0x1E
108 #define REGSBR 0x0F
110 #define EEPSBW 0x5A
111 #define EEPSBR 0x4B
113 #define CMRST 0xC3
115 #define WENA  0x00
116 #define VCFG1 0x01
117 #define VCFG2 0x01
119 #define STA1 0x07
120 #define STA2 0x08
121 #define GPVR 0x0C
123 #define CFG3 0xA6
126 Void spiFxn(UArg arg0, UArg arg1)
128         uint8_t         txBufferPointer[8];
129         uint8_t         rxBufferPointer[8];
131         int i;
132         uint8_t k=0;
133         int flagerr=0;
135         SPI_Handle      spi;
136         SPI_Params      spiParams;
137     SPI_Transaction spiTransaction;
139     UInt events;
141     SPI_Params_init(&spiParams);
142     // Slave mode
143     spiParams.mode = SPI_MASTER;
144     spiParams.bitRate = 10000;
145     spiParams.transferMode = SPI_MODE_BLOCKING;
146     spiParams.frameFormat = SPI_POL0_PHA0;
148     spi=SPI_open(Board_SPI0,&spiParams);
149     if(!spi){
150       System_printf("SPI did not open");
151     }
152     System_printf("SPI Init Done\r\n");
154         //Task_sleep((UInt)arg0);
156     System_printf("Press button:\r\n");
157     PIN_setOutputValue(ledPinHandle, Board_LED0,1);
158     PIN_setOutputValue(ledPinHandle, Board_LED1,1);
160     Semaphore_pend(semHandle, BIOS_WAIT_FOREVER);
162         /* Start test */
163     PIN_setOutputValue(ledPinHandle, Board_LED0,0);
164     PIN_setOutputValue(ledPinHandle, Board_LED1,0);
166         /* Send SPI Reset Command */
168         spiTransaction.rxBuf = rxBufferPointer;
169         spiTransaction.txBuf = txBufferPointer;
170         spiTransaction.count=3;
172         txBufferPointer[0]=CMRST;
173         txBufferPointer[1]=0x55;
174         txBufferPointer[2]=0xFF;
176         rxBufferPointer[0]=0;
177         rxBufferPointer[1]=0;
178         rxBufferPointer[2]=0;
180         if (SPI_transfer(spi,&spiTransaction)) {
181                 if (rxBufferPointer[2] != 0xF0)
182                 {
183                         Event_post(myEvent, Event_Id_00);
184                         flagerr=1;
185                 }
186                 System_printf("RST: %x %x %x\r\n",rxBufferPointer[0],rxBufferPointer[1],rxBufferPointer[2]);
187                 System_flush();
188         }else{
189                 Event_post(myEvent, Event_Id_00);
190                 flagerr=1;
191                 //Task_exit();
192         }
193         for(i=1000;i>0;i--);
196         /* Read 4 Bytes from EEPROM: Chip ID*/
197         txBufferPointer[0]=0x63;
198         txBufferPointer[1]=0xA0;
199         txBufferPointer[2]=0x04;
200         txBufferPointer[3]=0x03;
201         txBufferPointer[4]=0x02;
202         txBufferPointer[5]=0x01;
203         txBufferPointer[6]=0xFF;
205         spiTransaction.rxBuf = rxBufferPointer;
206         spiTransaction.txBuf = txBufferPointer;
207         spiTransaction.count = 7;
209         if (SPI_transfer(spi,&spiTransaction)) {
211                 if (rxBufferPointer[2] != 0xF0)
212                 {
213                         Event_post(myEvent, Event_Id_00);
214                         flagerr=1;
215                 }
217                 System_printf("IDC: ");
218                 for(i=0;i<7;i++){
219                         System_printf("%x ",rxBufferPointer[i]);
220                 }
221                 System_printf("\r\n");
222                 System_flush();
223         }else{
224                 Event_post(myEvent, Event_Id_00);
225                 flagerr=1;
226                 //Task_exit();
227         }
229         /* OK! */
230         if(flagerr==0){
231                 Event_post(myEvent, Event_Id_01);
232                 System_printf("***\r\n");
233                 System_flush();
234         }else{
235                 System_printf("KO!\r\n");
236                 System_flush();
237         }
239     System_printf("Press button to continue:\r\n");
240     Semaphore_pend(semHandle, BIOS_WAIT_FOREVER);
242         while(1){
244                 Task_sleep((UInt)arg0);
246                 // Enable Write GPVR Register
247                 txBufferPointer[0]=REGSBW;
248         txBufferPointer[1]=WENA;
249         txBufferPointer[2]=GPVR;
250         txBufferPointer[3]=0xFF;
252         spiTransaction.rxBuf = rxBufferPointer;
253         spiTransaction.txBuf = txBufferPointer;
254         spiTransaction.count = 4;
256         System_printf("TX: %x\r\n",k);
258                 if (SPI_transfer(spi,&spiTransaction)) {
259                         System_printf("WENA: %x %x %x %x\r\n",rxBufferPointer[0],rxBufferPointer[1],rxBufferPointer[2],rxBufferPointer[3]);
260                         System_flush();
261                 }
263                 // Write GPVR Register
264         txBufferPointer[0]=REGSBW;
265         txBufferPointer[1]=GPVR;
266         txBufferPointer[2]=k++;
267         txBufferPointer[3]=0xFF;
269         spiTransaction.rxBuf = rxBufferPointer;
270         spiTransaction.txBuf = txBufferPointer;
271         spiTransaction.count = 4;
273         if (SPI_transfer(spi,&spiTransaction)) {
274                         System_printf("REGSBW: %x %x %x %x\r\n",rxBufferPointer[0],rxBufferPointer[1],rxBufferPointer[2],rxBufferPointer[3]);
275                         System_flush();
276                 }
278                 // Read GPVR Register
279                 txBufferPointer[0]=REGSBR;
280         txBufferPointer[1]=GPVR;
281         txBufferPointer[2]=0x01;
282         txBufferPointer[3]=0xFF;
284         spiTransaction.rxBuf = rxBufferPointer;
285         spiTransaction.txBuf = txBufferPointer;
286         spiTransaction.count = 4;
288                 if (SPI_transfer(spi,&spiTransaction)) {
289                         System_printf("REGSBR: %x %x %x %x\r\n",rxBufferPointer[0],rxBufferPointer[1],rxBufferPointer[2],rxBufferPointer[3]);
290                         System_flush();
291                 }
293                 /* READ 1 BYTE from EEPROM
294                 txBufferPointer[0]=EEPSBR;
295                 txBufferPointer[1]=0xA0;
296                 txBufferPointer[2]=0x01;
297                 txBufferPointer[3]=0xFF;
299                 spiTransaction.rxBuf = rxBufferPointer;
300                 spiTransaction.txBuf = txBufferPointer;
301                 spiTransaction.count = 4;
303                 if (SPI_transfer(spi,&spiTransaction)) {
304                         System_printf("ID0: %x %x %x %x\r\n",rxBufferPointer[0],rxBufferPointer[1],rxBufferPointer[2],rxBufferPointer[3]);
305                         System_flush();
306                 }
307                 */
309     }
311     /* Deinitialized I2C */
312     SPI_close(spi);
316 /*
317  *  ======== heartBeatFxn ========
318  *  Toggle the Board_LED0. The Task_sleep is determined by arg0 which
319  *  is configured for the heartBeat Task instance.
320  */
321 Void heartBeatFxn(UArg arg0, UArg arg1)
324         UInt events;
326         events=Event_pend(myEvent,Event_Id_NONE,Event_Id_00+Event_Id_01,BIOS_WAIT_FOREVER);
328         if(events & Event_Id_00){
329           while (1)
330           {
331         Task_sleep((UInt)arg0);
332         PIN_setOutputValue(ledPinHandle, Board_LED1,!PIN_getOutputValue(Board_LED1));
333       }
334         }
336         if(events & Event_Id_01){
337       while (1)
338       {
339         Task_sleep((UInt)arg0);
340         PIN_setOutputValue(ledPinHandle, Board_LED0,!PIN_getOutputValue(Board_LED0));
341       }
342         }
345 /*
346  *  ======== main ========
347  */
348 int main(void)
350     Task_Params taskParams;
351     Semaphore_Params semParams;
353     /* Call board init functions */
354     Board_initGeneral();
355     Board_initSPI();
356     // Board_initI2C();
357     Board_initUART();
358     // Board_initWatchdog();
361     /* Default instance configuration params */
362     Error_init(&eb);
363     myEvent = Event_create(NULL,&eb);
364     if (myEvent == NULL) {
365         System_abort("Event create failed");
366     }
369     /* BMH  Init UART for System_printf() */
370     UART_Params uartParams;
371     UART_Params_init(&uartParams);
372     uartParams.baudRate = 115200;
373     UartPrintf_init(UART_open(Board_UART, &uartParams));
374     System_printf("Uart open\r\n");
377     /* Construct SPI Echo Task thread */
378     Task_Params_init(&taskParams);
379     taskParams.arg0 = 1000000 / Clock_tickPeriod;
380     taskParams.stackSize = TASKSTACKSIZE;
381     taskParams.stack = &task1Stack;
382     Task_construct(&task1Struct, (Task_FuncPtr)spiFxn, &taskParams, NULL);
385     /* Construct heartBeat Task thread */
386     Task_Params_init(&taskParams);
387     taskParams.arg0 = 500000 / Clock_tickPeriod;
388     taskParams.stackSize = TASKSTACKSIZE;
389     taskParams.stack = &task0Stack;
390     Task_construct(&task0Struct, (Task_FuncPtr)heartBeatFxn, &taskParams, NULL);
393     /* Construct Uart Task  thread */
394     Task_Params_init(&taskParams);
395     taskParams.arg0 = 100000 / Clock_tickPeriod;
396     taskParams.stackSize = TASKSTACKSIZE;
397     taskParams.stack = &task2Stack;
398     Task_construct(&task2Struct, (Task_FuncPtr)uartFxn, &taskParams, NULL);
401     /* Construct Semaphore Obtain instance handle */
402     Semaphore_Params_init(&semParams);
403     Semaphore_construct(&semStruct,0, &semParams);
404     semHandle = Semaphore_handle(&semStruct);
407     /* Setup callback for button pins */
408     buttonPinHandle = PIN_open(&buttonPinState, buttonPinTable);
409     if(!buttonPinHandle) {
410         System_abort("Error initializing button pins\n");
411     }
412     if (PIN_registerIntCb(buttonPinHandle, &buttonCallbackFxn) != 0) {
413         System_abort("Error registering button callback function");
414     }
417     /* Open LED pins */
418     ledPinHandle = PIN_open(&ledPinState, ledPinTable);
419     if(!ledPinHandle) {
420         System_abort("Error initializing board LED pins\n");
421     }
423     PIN_setOutputValue(ledPinHandle, Board_LED0, 1);
424     PIN_setOutputValue(ledPinHandle, Board_LED1, 1);
426     /* Start BIOS */
427     BIOS_start();
429     return (0);