[i3-mote/i3-mote.git] / Basic-Test-Package / CC2650 / Test_CC2650_3wSPI_Master_MSP432_SlaveIRQ / 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 Semaphore_Struct semStruct;
34 Semaphore_Handle semHandle;
37 /*
38 * Application LED pin configuration table:
39 * - All LEDs board LEDs are off.
40 */
41 static PIN_Handle ledPinHandle;
42 static PIN_State ledPinState;
44 PIN_Config ledPinTable[] = {
45 Board_LED0 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
46 Board_LED1 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
47 PIN_TERMINATE
48 };
51 /* Global memory storage for a PIN_Config table */
52 static PIN_Handle buttonPinHandle;
53 static PIN_State buttonPinState;
55 PIN_Config buttonPinTable[] = {
56 Board_BUTTON1 | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_NEGEDGE,
57 PIN_TERMINATE
58 };
60 void buttonCallbackFxn(PIN_Handle handle, PIN_Id pinId) {
62 /* Wait IRQ from MSP432 line*/
63 PIN_setOutputValue(ledPinHandle, Board_LED1,1);
64 Semaphore_post(semHandle);
65 CPUdelay(8000*100);
66 PIN_setOutputValue(ledPinHandle, Board_LED1,0);
68 }
72 Void echoFxn(UArg arg0, UArg arg1)
73 {
74 uint8_t i=0;
75 uint8_t txBufferPointer[4];
76 uint8_t rxBufferPointer[4];
77 SPI_Handle spi;
78 SPI_Params spiParams;
79 SPI_Transaction spiTransaction;
82 SPI_Params_init(&spiParams);
83 // Slave mode
84 spiParams.mode = SPI_MASTER;
85 spiParams.bitRate = 1000000;
86 spiParams.frameFormat = SPI_POL1_PHA1;
88 spi=SPI_open(Board_SPI1,&spiParams);
89 if(!spi){
90 System_printf("SPI did not open");
91 }
92 System_printf("SPI Init Done\r\n");
94 spiTransaction.rxBuf= rxBufferPointer;
95 spiTransaction.txBuf = txBufferPointer;
96 spiTransaction.count=1;
98 while(1) {
100 Semaphore_pend(semHandle, BIOS_WAIT_FOREVER);
102 txBufferPointer[0]=i++;
103 rxBufferPointer[0]=0;
105 if (SPI_transfer(spi,&spiTransaction)) {
106 System_printf("TxData: %x RxData: %x\r\n",txBufferPointer[0],rxBufferPointer[0]);
107 }
108 }
110 /* Deinitialized I2C */
111 SPI_close(spi);
112 }
115 /*
116 * ======== heartBeatFxn ========
117 * Toggle the Board_LED0. The Task_sleep is determined by arg0 which
118 * is configured for the heartBeat Task instance.
119 */
120 Void heartBeatFxn(UArg arg0, UArg arg1)
121 {
122 while (1) {
123 Task_sleep((UInt)arg0);
124 //PIN_setOutputValue(ledPinHandle, Board_LED0,!PIN_getOutputValue(Board_LED0));
125 //PIN_setOutputValue(ledPinHandle, Board_LED1,!PIN_getOutputValue(Board_LED1));
126 }
127 }
129 /*
130 * ======== main ========
131 */
132 int main(void)
133 {
134 Task_Params taskParams;
135 Semaphore_Params semParams;
137 /* Call board init functions */
138 Board_initGeneral();
139 Board_initSPI();
140 // Board_initI2C();
141 // Board_initUART();
142 // Board_initWatchdog();
144 /* UART */
145 /* Init UART for System_printf()*/
146 UART_Params uartParams;
147 UART_Params_init(&uartParams);
148 //uartParams.baudRate = 9600;
149 uartParams.baudRate = 115200;
150 UartPrintf_init(UART_open(Board_UART, &uartParams));
151 System_printf("Uart open\r\n");
153 /* Construct heartBeat Task thread */
154 Task_Params_init(&taskParams);
155 taskParams.arg0 = 500000 / Clock_tickPeriod;
156 taskParams.stackSize = TASKSTACKSIZE;
157 taskParams.stack = &task0Stack;
158 Task_construct(&task0Struct, (Task_FuncPtr)heartBeatFxn, &taskParams, NULL);
160 /* Construct SPI Echo Task thread */
161 Task_Params_init(&taskParams);
162 taskParams.arg0 = 1000000 / Clock_tickPeriod;
163 taskParams.stackSize = TASKSTACKSIZE;
164 taskParams.stack = &task1Stack;
165 Task_construct(&task1Struct, (Task_FuncPtr)echoFxn, &taskParams, NULL);
167 /* Construct Semaphore */
168 Semaphore_Params_init(&semParams);
169 Semaphore_construct(&semStruct, 1, &semParams);
171 /* Obtain instance handle */
172 semHandle = Semaphore_handle(&semStruct);
174 /* Setup callback for button pins */
175 buttonPinHandle = PIN_open(&buttonPinState, buttonPinTable);
176 if(!buttonPinHandle) {
177 System_abort("Error initializing button pins\n");
178 }
179 if (PIN_registerIntCb(buttonPinHandle, &buttonCallbackFxn) != 0) {
180 System_abort("Error registering button callback function");
181 }
183 /* Open LED pins */
184 ledPinHandle = PIN_open(&ledPinState, ledPinTable);
185 if(!ledPinHandle) {
186 System_abort("Error initializing board LED pins\n");
187 }
189 PIN_setOutputValue(ledPinHandle, Board_LED0, 0);
190 PIN_setOutputValue(ledPinHandle, Board_LED1, 0);
192 /* Start BIOS */
193 BIOS_start();
195 return (0);
196 }