SPI Demo

```language #include <stdio.h> #include <string.h> #include <stdlib.h> #include <stdint.h> #include "UART.h" #include "osa.h" #include "SPI_interface.h" #undef printf #define printf(fmt, args...) do { fatal_printf("[sdk]"fmt, ##args); } while(0) static OSATimerRef _timer_ref = NULL; static OSTaskRef _task_ref = NULL; static OSFlagRef _flag_ref = NULL; static int count = 0; static void _timer_callback(UINT32 param); static void _task(void *ptr); #define _TASK_STACK_SIZE 1280 static UINT32 _task_stack[_TASK_STACK_SIZE/sizeof(UINT32)]; #define TASK_TIMER_CHANGE_FLAG_BIT 0x01 #define TASK_TIMER_CHANGE2_FLAG_BIT 0x02 // Device bootup hook before Phase1Inits. // If you have some work to be init, you may implete it here. // ex: you may start your task here. or do some initialize here. extern void Phase1Inits_enter(void); // Device bootup hook after Phase1Inits. // If you have some work to be init, you may implete it here. // ex: you may start your task here. or do some initialize here. extern void Phase1Inits_exit(void); // Device bootup hook before Phase2Inits. // If you have some work to be init, you may implete it here. // ex: you may start your task here. or do some initialize here. extern void Phase2Inits_enter(void); // Device bootup hook after Phase2Inits. // If you have some work to be init, you may implete it here. // ex: you may start your task here. or do some initialize here. extern void Phase2Inits_exit(void); void Phase1Inits_enter(void) { } void Phase1Inits_exit(void) { } void Phase2Inits_enter(void) { } void Phase2Inits_exit(void) { int ret; printf("debug> enter main\n"); fatal_printf("+SPI SSP0 Initialize\r\n"); SPI_Initialize(SSP_SPI_MODE0, SPI_CLK_26MHZ); ret = OSAFlagCreate(&_flag_ref); ASSERT(ret == OS_SUCCESS); ret = OSATimerCreate(&_timer_ref); ASSERT(ret == OS_SUCCESS); ret = OSATaskCreate(&_task_ref, _task_stack, _TASK_STACK_SIZE, 200, "filetest-task", _task, NULL); ASSERT(ret == OS_SUCCESS); OSATimerStart(_timer_ref, 20 * 200, 5 * 200, _timer_callback, 0); // 10 seconds timer } static void _timer_callback(UINT32 param) { int temp; count++; temp = count %2; if (temp) { OSAFlagSet(_flag_ref, TASK_TIMER_CHANGE_FLAG_BIT, OSA_FLAG_OR); } else { OSAFlagSet(_flag_ref, TASK_TIMER_CHANGE2_FLAG_BIT, OSA_FLAG_OR); } } static void _task(void *ptr) { OSA_STATUS status; UINT32 flag_value; UINT32 flag_mask = TASK_TIMER_CHANGE_FLAG_BIT | TASK_TIMER_CHANGE2_FLAG_BIT; int fd = 0; char outBuf[] = "1234567890"; char temp_buf[30] = {0}; UINT32 ret_val = 0; char temp_buf2[30] = {0}; while(1) { status = OSAFlagWait(_flag_ref, flag_mask, OSA_FLAG_OR_CLEAR, &flag_value, OSA_SUSPEND); ASSERT(status == OS_SUCCESS); printf("debug> count = %d\n",count); if (flag_value & TASK_TIMER_CHANGE2_FLAG_BIT) { temp_buf[0] = 0x01; temp_buf[1] = 0x01; temp_buf[2] = 0x01; temp_buf[3] = 0x01; temp_buf[4] = 0x01; temp_buf[5] = 0x01; temp_buf[6] = 0x01; temp_buf[7] = 0x01; temp_buf[8] = 0x01; temp_buf[9] = 0x01; ret_val = SPI_WriteData(temp_buf, 3); printf("SPI_WriteData ret_val = %d, temp_buf %02X\n",ret_val, temp_buf[0]); } else if (flag_value & TASK_TIMER_CHANGE_FLAG_BIT) { #if 0 //ret_val = SPI_ReadData(temp_buf, 10); ret_val = SPI_DataTransfer(temp_buf, outBuf, 10); printf("SPI_ReadData ret_val = %d, %02X, %02X, %02X\n",ret_val, temp_buf[0], temp_buf[1], temp_buf[2]); #else temp_buf2[0] = 0xFE; temp_buf2[1] = 0xFE; temp_buf2[2] = 0xFE; temp_buf2[3] = 0xFE; temp_buf2[4] = 0xFE; temp_buf2[5] = 0xFE; temp_buf2[6] = 0xFE; temp_buf2[7] = 0xFE; temp_buf2[8] = 0xFE; temp_buf2[9] = 0xFE; ret_val = SPI_WriteData(temp_buf2, 10); printf("SPI_WriteData ret_val = %d, temp_buf2 %02X\n",ret_val, temp_buf2[0]); #endif } } } ```