static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message) { RT_ASSERT(device != RT_NULL); RT_ASSERT(device->bus != RT_NULL); RT_ASSERT(device->bus->parent.user_data != RT_NULL); struct wm_sw_spi_cs *cs = device->parent.user_data; struct tls_spi_transfer tls_transfer; tls_transfer.tx_buf = message->send_buf; tls_transfer.rx_buf = message->recv_buf; tls_transfer.len = message->length; rt_int32_t length = 0; rt_int32_t remain_length = message->length; length = spi_fill_txfifo(&tls_transfer, remain_length); spi_set_sclk_length(length * 8, 0); if (message->cs_take) { tls_gpio_write((enum tls_io_name)cs->pin, 0); } spi_sclk_start(); while (remain_length > 0) { while (spi_i2s_get_busy_status() == 1) ; length = spi_get_rxfifo(&tls_transfer, remain_length); remain_length -= length; if (remain_length == 0) { while (spi_i2s_get_busy_status() == 1) ; if (message->cs_release) { tls_gpio_write((enum tls_io_name)cs->pin, 1); } } while (spi_i2s_get_busy_status() == 1) ; length = spi_fill_txfifo(&tls_transfer, remain_length); if (length) { spi_set_sclk_length(length * 8, 0); spi_sclk_start(); } } while (spi_i2s_get_busy_status() == 1) ; if (message->cs_release) { tls_gpio_write((enum tls_io_name)cs->pin, 1); } return message->length - remain_length; }
u8 TestSPITransferData(void) { u32 i = 0; u8 cmd[40]; u8 buf[40]; u8 *TXBuf; int time; // int ret; TXBuf = tls_mem_alloc(TEST_SPI_SPEED_SIZE); if(NULL == TXBuf) return 0; memset(TXBuf,0xaa,TEST_SPI_SPEED_SIZE); memset(cmd,0,32); memset(buf,0,32); cmd[0] = 0x03; while(1) { #if CS_CTRL_SOFT tls_gpio_write(18, 0); #endif tls_spi_write_then_read(cmd, 1, buf, 2); #if CS_CTRL_SOFT tls_gpio_write(18, 1); #endif if(buf[0] & 0x01) break; OSTimeDly(1); printf("\ncan not tx data\n"); } cmd[0] =0x00; *TXBuf = 0x90; //命令字 time = OSTimeGet(); printf("\ntime1 = %d\n",time); for(i = 0;i < 1000;i ++) { #if CS_CTRL_SOFT tls_gpio_write(18, 0); #endif tls_spi_write(TXBuf,TEST_SPI_SPEED_SIZE); #if CS_CTRL_SOFT tls_gpio_write(18, 1); #endif } time = OSTimeGet(); printf("\ntime2 = %d\n",time); printf("\ntx cnt =%d\n",i); tls_mem_free(TXBuf); return 0; }
rt_err_t wm_spi_bus_attach_device(const char *bus_name, const char *device_name, rt_uint32_t pin) { rt_err_t ret; rt_int16_t gpio_pin; struct rt_spi_device *spi_device; struct wm_sw_spi_cs *cs_pin; gpio_pin = wm_get_pin(pin); if (gpio_pin == WM_PIN_DEFAULT) { return -RT_ERROR; } spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device)); RT_ASSERT(spi_device != RT_NULL); cs_pin = (struct wm_sw_spi_cs *)rt_malloc(sizeof(struct wm_sw_spi_cs)); RT_ASSERT(cs_pin != RT_NULL); cs_pin->pin = gpio_pin; tls_gpio_cfg((enum tls_io_name)gpio_pin, WM_GPIO_DIR_OUTPUT, WM_GPIO_ATTR_PULLHIGH); tls_gpio_write((enum tls_io_name)gpio_pin, 1); ret = rt_spi_bus_attach_device(spi_device, device_name, bus_name, (void *)cs_pin); return ret; }
void TestSPIReceiveData(void) { u8 cmd[40]; u8 buf[40]; u16 temp = 0; u16 i; #if 1 memset(cmd,0,32); memset(buf,0,32); cmd[0] = 0x06; #if CS_CTRL_SOFT tls_gpio_write(18, 0); #endif tls_spi_write_then_read(cmd, 1, buf, 2); #if CS_CTRL_SOFT tls_gpio_write(18, 1); #endif printf("\nrx buf[%x][%x]\n",buf[0],buf[1]); #endif if(buf[0] & 0x01) //数据或命令已经准备好 { temp = 0; cmd[0] = 0x02; #if CS_CTRL_SOFT tls_gpio_write(18, 0); #endif tls_spi_write_then_read(cmd,1,buf,2); #if CS_CTRL_SOFT tls_gpio_write(18, 1); #endif temp |= buf[0]; temp |= buf[1] << 8; printf("\ntemp=%d\n",temp); if(temp > 0) { //这里最好判断一下数据长度是否4的整数倍,留4个byte通过0x10命令接收 cmd[0] = 0; #if !CS_CTRL_SOFT tls_spi_write_then_read(cmd,1,buf,(temp-1)/4*4); cmd[0] = 0x10; tls_spi_write_then_read(cmd,1,buf + (temp-1)/4*4,temp-(temp-1)/4*4); #else //下面这段程序主要测试把写命令和读数据分成两步来做,如果不用 //软件控制cs,写完之后cs会被拉高,读之前再拉低,则这样读出的数据 //开始会有一个无效byte. tls_gpio_write(18, 0); tls_spi_write(cmd,1); for(i = 0;i < temp/4;i ++) { if((i+1)*4 == temp) break; tls_spi_read(buf + i*4,4); } tls_gpio_write(18, 1); tls_gpio_write(18, 0); cmd[0] = 0x10; tls_spi_write(cmd,1); tls_spi_read(buf + i*4,temp - i * 4); tls_gpio_write(18, 1); #endif for(i = 0;i < temp; i++) { printf("[%d]=[%x]\r\n",i,buf[i]); } } } }
u8 TestSPITransferCMD(void) { u8 buf[40]; u8 cmd[100]; u16 ret; memset(buf,0,32); memset(cmd,0,100); cmd[0] = 0x03; //命令字一个byte之后,不能跟0,否则命令下面识别不对 #if CS_CTRL_SOFT tls_gpio_write(18, 0); #endif tls_spi_write_then_read(cmd, 1, buf, 2); #if CS_CTRL_SOFT tls_gpio_write(18, 1); #endif printf("\ntx cmd[%x][%x]\n",buf[0],buf[1]); //OSTimeDly(10); #if 1 if(buf[0]&0x02) { cmd[0] = 0x91; cmd[1] = 0xaa; cmd[2] = 0x01; //TYPE 命令端口传输 jj cmd[3] = 0x00; // cmd[4] = 0x04; //数据长度 4byte cmd[5] = 0x00; //序号0 cmd[6] = 0x00; //FLG cmd[7] = 0x00; //DA cmd[8] = 0X05; //CHK /*获取版本号*/ cmd[9] = 0X01; //精简指令获取版本号 cmd[10] = 0X07; cmd[11] = 0X00; cmd[12] = 0X00; cmd[13] = 0X08; //CHK cmd[14] = 0; cmd[15] = 0; cmd[16] = 0; //while(1) { // temp = *((u32 *)cmd); // printf("\ntemp = %x\n",temp); #if CS_CTRL_SOFT tls_gpio_write(18, 0); #endif ret = tls_spi_write(cmd,20); //写精简指令 #if CS_CTRL_SOFT tls_gpio_write(18, 1); #endif printf("\nret =%d\n",ret); // OSTimeDly(10); } } #endif return 0; }
static void BlinkTimerProc(void) { // #define HIO_LINK 13 #define HIO_LINK 11 static u32 div_blink = 0; static u32 mode_blink = 0; u32 blink[][2] = { /************************************************ |<--------blink------->| |<--light-->| |-----------| |-----------| | | | | ---| |----------| |--- blink | light *************************************************/ {22, 2}, /* mode = 0 */ {10, 5}, /* mode = 1 */ {2, 1}, /* mode = 2 */ }; u32 mode; mode = net_up_status?1:0; if(tls_fwup_get_status()) { mode = 2; } if (mode != mode_blink) { mode_blink = mode; div_blink = 0; } tls_gpio_cfg(HIO_LINK, TLS_GPIO_DIR_OUTPUT, TLS_GPIO_ATTR_FLOATING); if(blink[mode_blink][0] == 0) { if (blink[mode_blink][1] == 0) { tls_gpio_write(HIO_LINK, 0); } else { tls_gpio_write(HIO_LINK, 1); } } else { ++div_blink; if (div_blink <= blink[mode_blink][1]) { tls_gpio_write(HIO_LINK, 1); } else if (div_blink <= blink[mode_blink][0]) { tls_gpio_write(HIO_LINK, 0); } else { div_blink = 0; } } }