a3594192c94fe90cce12243337ff6e61bc394eff
[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>
8 /* BIOS Header files */
9 #include <ti/sysbios/BIOS.h>
10 #include <ti/sysbios/knl/Task.h>
11 #include <ti/sysbios/knl/Clock.h>
12 #include <ti/sysbios/knl/Semaphore.h>
14 /* TI-RTOS Header files */
15 //#include <ti/drivers/I2C.h>
16 #include <ti/drivers/PIN.h>
17 #include <ti/drivers/SPI.h>
18 #include <ti/drivers/UART.h>
19 // #include <ti/drivers/Watchdog.h>
21 /* Board Header files */
22 #include "Board.h"
23 #include "uart_printf.h"
25 #define TASKSTACKSIZE   512
27 Task_Struct task0Struct;
28 Char task0Stack[TASKSTACKSIZE];
30 Task_Struct task1Struct;
31 Char task1Stack[TASKSTACKSIZE];
33 Task_Struct task2Struct;
34 Char task2Stack[TASKSTACKSIZE];
37 Semaphore_Struct semStruct;
38 Semaphore_Handle semHandle;
41 /*
42  * Application LED pin configuration table:
43  *   - All LEDs board LEDs are off.
44  */
45 static PIN_Handle ledPinHandle;
46 static PIN_State ledPinState;
48 PIN_Config ledPinTable[] = {
49     Board_LED0 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
50     Board_LED1 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
51         PIN_TERMINATE
52 };
54 /**
55 static PIN_Handle csnPinHandle;
56 static PIN_State csnPinState;
58 PIN_Config csnPinTable[] = {
59         Board_DAC8730_CSN| PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
60         PIN_TERMINATE
61 };
62 **/
64 /* Global memory storage for a PIN_Config table */
65 static PIN_Handle buttonPinHandle;
66 static PIN_State buttonPinState;
68 PIN_Config buttonPinTable[] = {
69     Board_BUTTON0  | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_NEGEDGE,
70     //Board_BUTTON1  | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_NEGEDGE,
71     PIN_TERMINATE
72 };
74 void buttonCallbackFxn(PIN_Handle handle, PIN_Id pinId) {
76         PIN_setOutputValue(ledPinHandle, Board_LED1,1);
77     Semaphore_post(semHandle);
78     CPUdelay(8000*50);
79         PIN_setOutputValue(ledPinHandle, Board_LED1,0);
80 }
83 #define REGSBW 0x1E
84 #define REGSBR 0x0F
85 #define EEPSBR 0x4B
87 #define CMRST 0xC3
89 #define WENA 0x00
90 #define GPVR 0x0C
93 Void spiFxn(UArg arg0, UArg arg1)
94 {
95         uint8_t         txBufferPointer[8];
96         uint8_t         rxBufferPointer[8];
98         int i;
99         uint8_t k=0;
101         SPI_Handle      spi;
102         SPI_Params      spiParams;
103     SPI_Transaction spiTransaction;
106     SPI_Params_init(&spiParams);
107     // Slave mode
108     spiParams.mode = SPI_MASTER;
109     spiParams.bitRate = 10000;
110     spiParams.transferMode = SPI_MODE_BLOCKING;
111     spiParams.frameFormat = SPI_POL0_PHA0;
113     spi=SPI_open(Board_SPI0,&spiParams);
114     if(!spi){
115       System_printf("SPI did not open");
116     }
117     System_printf("SPI Init Done\r\n");
119         //Task_sleep((UInt)arg0);
121     System_printf("Press button:\r\n");
122     Semaphore_pend(semHandle, BIOS_WAIT_FOREVER);
124         /* Send SPI Reset Command */
126         spiTransaction.rxBuf = rxBufferPointer;
127         spiTransaction.txBuf = txBufferPointer;
128         spiTransaction.count=3;
130         txBufferPointer[0]=CMRST;
131         txBufferPointer[1]=0x55;
132         txBufferPointer[2]=0xFF;
134         rxBufferPointer[0]=0;
135         rxBufferPointer[1]=0;
136         rxBufferPointer[2]=0;
138         if (SPI_transfer(spi,&spiTransaction)) {
139                 System_printf("Rst: %x %x %x\r\n",rxBufferPointer[0],rxBufferPointer[1],rxBufferPointer[2]);
140                 System_flush();
141         }
142         for(i=1000;i>0;i--);
145         /* Read 4 Bytes from EEPROM: Chip ID*/
146         txBufferPointer[0]=0x63;
147         txBufferPointer[1]=0xA0;
148         txBufferPointer[2]=0x04;
149         txBufferPointer[3]=0x03;
150         txBufferPointer[4]=0x02;
151         txBufferPointer[5]=0x01;
152         txBufferPointer[6]=0xFF;
154         spiTransaction.rxBuf = rxBufferPointer;
155         spiTransaction.txBuf = txBufferPointer;
156         spiTransaction.count = 7;
158         if (SPI_transfer(spi,&spiTransaction)) {
159                 System_printf("IDC: ");
160                 for(i=0;i<7;i++){
161                         System_printf("%x ",rxBufferPointer[i]);
162                 }
163                 System_printf("\r\n");
164                 System_flush();
165         }
168         while(1){
170                 Task_sleep((UInt)arg0);
172                 // Enable Write GPVR Register
173                 txBufferPointer[0]=REGSBW;
174         txBufferPointer[1]=WENA;
175         txBufferPointer[2]=GPVR;
176         txBufferPointer[3]=0xFF;
178         spiTransaction.rxBuf = rxBufferPointer;
179         spiTransaction.txBuf = txBufferPointer;
180         spiTransaction.count = 4;
182         System_printf("TX: %x\r\n",k);
184                 if (SPI_transfer(spi,&spiTransaction)) {
185                         System_printf("WENA: %x %x %x %x\r\n",rxBufferPointer[0],rxBufferPointer[1],rxBufferPointer[2],rxBufferPointer[3]);
186                         System_flush();
187                 }
189                 // Write GPVR Register
190         txBufferPointer[0]=REGSBW;
191         txBufferPointer[1]=GPVR;
192         txBufferPointer[2]=k++;
193         txBufferPointer[3]=0xFF;
195         spiTransaction.rxBuf = rxBufferPointer;
196         spiTransaction.txBuf = txBufferPointer;
197         spiTransaction.count = 4;
199         if (SPI_transfer(spi,&spiTransaction)) {
200                         System_printf("REGSBW: %x %x %x %x\r\n",rxBufferPointer[0],rxBufferPointer[1],rxBufferPointer[2],rxBufferPointer[3]);
201                         System_flush();
202                 }
204                 // Read GPVR Register
205                 txBufferPointer[0]=REGSBR;
206         txBufferPointer[1]=GPVR;
207         txBufferPointer[2]=0x01;
208         txBufferPointer[3]=0xFF;
210         spiTransaction.rxBuf = rxBufferPointer;
211         spiTransaction.txBuf = txBufferPointer;
212         spiTransaction.count = 4;
214                 if (SPI_transfer(spi,&spiTransaction)) {
215                         System_printf("REGSBR: %x %x %x %x\r\n",rxBufferPointer[0],rxBufferPointer[1],rxBufferPointer[2],rxBufferPointer[3]);
216                         System_flush();
217                 }
220                 /* READ 1 BYTE from EEPROM
221                 txBufferPointer[0]=EEPSBR;
222                 txBufferPointer[1]=0xA0;
223                 txBufferPointer[2]=0x01;
224                 txBufferPointer[3]=0xFF;
226                 spiTransaction.rxBuf = rxBufferPointer;
227                 spiTransaction.txBuf = txBufferPointer;
228                 spiTransaction.count = 4;
230                 if (SPI_transfer(spi,&spiTransaction)) {
231                         System_printf("ID0: %x %x %x %x\r\n",rxBufferPointer[0],rxBufferPointer[1],rxBufferPointer[2],rxBufferPointer[3]);
232                         System_flush();
233                 }
234                 */
236     }
238     /* Deinitialized I2C */
239     SPI_close(spi);
243 /*
244  *  ======== heartBeatFxn ========
245  *  Toggle the Board_LED0. The Task_sleep is determined by arg0 which
246  *  is configured for the heartBeat Task instance.
247  */
248 Void heartBeatFxn(UArg arg0, UArg arg1)
250     while (1) {
251         Task_sleep((UInt)arg0);
252         PIN_setOutputValue(ledPinHandle, Board_LED0,!PIN_getOutputValue(Board_LED0));
253     }
256 /*
257  *  ======== main ========
258  */
259 int main(void)
261     Task_Params taskParams;
262     Semaphore_Params semParams;
264     /* Call board init functions */
265     Board_initGeneral();
266     Board_initSPI();
267     // Board_initI2C();
268     Board_initUART();
269     // Board_initWatchdog();
271     /* BMH  Init UART for System_printf() */
272     UART_Params uartParams;
273     UART_Params_init(&uartParams);
274     uartParams.baudRate = 115200;
275     UartPrintf_init(UART_open(Board_UART, &uartParams));
276     System_printf("Uart open\r\n");
279     /* Construct heartBeat Task thread */
280     Task_Params_init(&taskParams);
281     taskParams.arg0 = 500000 / Clock_tickPeriod;
282     taskParams.stackSize = TASKSTACKSIZE;
283     taskParams.stack = &task0Stack;
284     Task_construct(&task0Struct, (Task_FuncPtr)heartBeatFxn, &taskParams, NULL);
286     /* Construct SPI Echo Task thread */
287     Task_Params_init(&taskParams);
288     taskParams.arg0 = 1000000 / Clock_tickPeriod;
289     taskParams.stackSize = TASKSTACKSIZE;
290     taskParams.stack = &task1Stack;
291     Task_construct(&task1Struct, (Task_FuncPtr)spiFxn, &taskParams, NULL);
293     /* Construct Semaphore */
294     Semaphore_Params_init(&semParams);
295     Semaphore_construct(&semStruct,0, &semParams);
297     /* Obtain instance handle */
298     semHandle = Semaphore_handle(&semStruct);
300     /* Setup callback for button pins */
301     buttonPinHandle = PIN_open(&buttonPinState, buttonPinTable);
302     if(!buttonPinHandle) {
303         System_abort("Error initializing button pins\n");
304     }
305     if (PIN_registerIntCb(buttonPinHandle, &buttonCallbackFxn) != 0) {
306         System_abort("Error registering button callback function");
307     }
310     /* Open LED pins */
311     ledPinHandle = PIN_open(&ledPinState, ledPinTable);
312     if(!ledPinHandle) {
313         System_abort("Error initializing board LED pins\n");
314     }
316     PIN_setOutputValue(ledPinHandle, Board_LED0, 0);
317     PIN_setOutputValue(ledPinHandle, Board_LED1, 0);
319     /* Start BIOS */
320     BIOS_start();
322     return (0);