[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 Semaphore_post(semHandle);
63 PIN_setOutputValue(ledPinHandle, Board_LEDR,1);
65 }
68 Void echoFxn(UArg arg0, UArg arg1)
69 {
71 uint8_t txBufferPointer[4];
72 uint8_t rxBufferPointer[4];
73 SPI_Handle spi;
74 SPI_Params spiParams;
75 SPI_Transaction spiTransaction;
78 SPI_Params_init(&spiParams);
79 // Slave mode
80 spiParams.mode = SPI_MASTER;
81 spiParams.bitRate = 1000000;
82 spiParams.frameFormat = SPI_POL1_PHA1;
84 spi=SPI_open(Board_SPI1,&spiParams);
85 if(!spi){
86 System_printf("SPI did not open");
87 }
88 System_printf("SPI-Master Open\r\n");
90 spiTransaction.rxBuf= rxBufferPointer;
91 spiTransaction.txBuf = txBufferPointer;
92 spiTransaction.count=1;
94 while(1) {
96 Semaphore_pend(semHandle, BIOS_WAIT_FOREVER);
98 txBufferPointer[0]=rxBufferPointer[0];
100 if (SPI_transfer(spi,&spiTransaction)) {
101 //System_printf("RxData: %x TxData: %x\r\n",rxBufferPointer[0],txBufferPointer[0]);
102 PIN_setOutputValue(ledPinHandle, Board_LEDR,0);
103 }
104 }
106 /* Deinitialized I2C */
107 SPI_close(spi);
108 }
111 /*
112 * ======== heartBeatFxn ========
113 * Toggle the Board_LED0. The Task_sleep is determined by arg0 which
114 * is configured for the heartBeat Task instance.
115 */
117 Void heartBeatFxn(UArg arg0, UArg arg1)
118 {
120 while (1) {
122 Task_sleep((UInt)arg0-5000/Clock_tickPeriod);
123 PIN_setOutputValue(ledPinHandle, Board_LEDG,1);
124 Task_sleep((UInt)5000/Clock_tickPeriod);
125 PIN_setOutputValue(ledPinHandle, Board_LEDG,0);
127 //Task_sleep((UInt)arg0);
128 //PIN_setOutputValue(ledPinHandle, Board_LED0,!PIN_getOutputValue(Board_LED0));
129 //PIN_setOutputValue(ledPinHandle, Board_LED1,!PIN_getOutputValue(Board_LED1));
130 }
131 }
133 /*
134 * ======== main ========
135 */
136 int main(void)
137 {
138 Task_Params taskParams;
139 Semaphore_Params semParams;
141 /* Call board init functions */
142 Board_initGeneral();
143 Board_initSPI();
144 // Board_initI2C();
145 // Board_initUART();
146 // Board_initWatchdog();
148 /* UART */
149 /* Init UART for System_printf()*/
150 UART_Params uartParams;
151 UART_Params_init(&uartParams);
152 //uartParams.baudRate = 9600;
153 uartParams.baudRate = 115200;
154 UartPrintf_init(UART_open(Board_UART, &uartParams));
155 System_printf("Uart open\r\n");
157 /* Construct heartBeat Task thread */
158 Task_Params_init(&taskParams);
159 taskParams.arg0 = 2000000 / Clock_tickPeriod;
160 taskParams.stackSize = TASKSTACKSIZE;
161 taskParams.stack = &task0Stack;
162 Task_construct(&task0Struct, (Task_FuncPtr)heartBeatFxn, &taskParams, NULL);
164 /* Construct SPI Echo Task thread */
165 Task_Params_init(&taskParams);
166 taskParams.arg0 = 1000000 / Clock_tickPeriod;
167 taskParams.stackSize = TASKSTACKSIZE;
168 taskParams.stack = &task1Stack;
169 Task_construct(&task1Struct, (Task_FuncPtr)echoFxn, &taskParams, NULL);
171 /* Construct Semaphore */
172 Semaphore_Params_init(&semParams);
173 Semaphore_construct(&semStruct, 1, &semParams);
175 /* Obtain instance handle */
176 semHandle = Semaphore_handle(&semStruct);
178 /* Setup callback for button pins */
179 buttonPinHandle = PIN_open(&buttonPinState, buttonPinTable);
180 if(!buttonPinHandle) {
181 System_abort("Error initializing button pins\n");
182 }
183 if (PIN_registerIntCb(buttonPinHandle, &buttonCallbackFxn) != 0) {
184 System_abort("Error registering button callback function");
185 }
187 /* Open LED pins */
188 ledPinHandle = PIN_open(&ledPinState, ledPinTable);
189 if(!ledPinHandle) {
190 System_abort("Error initializing board LED pins\n");
191 }
193 PIN_setOutputValue(ledPinHandle, Board_LED0, 0);
194 PIN_setOutputValue(ledPinHandle, Board_LED1, 0);
196 /* Start BIOS */
197 BIOS_start();
199 return (0);
200 }