#include "common/common.h" /* TEST 18M: Tx ok, Rx over counted ??? (120000/100000) ??? - could not shorten delay because of this platform limit. 9M: Tx, Rx have transmit problem. 4.5M: If it has enough delay, it works ok. but under below conditions, Tx ok, Rx over-counted a little. (1002xx/100000) - Actual Speed 930 kbps */ #include "stm32f10x.h" #include "stm32f10x_spi.h" // WizFi250 SPI CONTROL CODE #define SPI_NULL (uint8_t) 0xF0 #define SPI_ESC (uint8_t) 0xF1 #define SPI_F0 (uint8_t) 0x00 #define SPI_F1 (uint8_t) 0x01 #define SPI_SYNC (uint8_t) 0x02 #define SPI_XON (uint8_t) 0x03 #define SPI_XOFF (uint8_t) 0x04 #define SPI_ERR (uint8_t) 0x05 #define SPI_RXBUFFER_MAX 1024 uint8 g_spi_rx_buffer[SPI_RXBUFFER_MAX]; uint32 g_spi_rx_length = 0; uint8_t g_opt_print_mode = 3; uint16_t g_opt_spi_prescaler = SPI_BaudRatePrescaler_256; uint8 wizspix_byte(wizpf_spi spi, uint8 byte, uint8 . , uint16 delay1, uint16 delay2, uint8 print) { uint8 spi_data; if ( issue_cs!=0 ) wizspi_cs(WIZ_SPI2, VAL_LOW); if ( delay1>0 ) Delay_us(delay1); spi_data = wizspi_byte(spi, byte); if ( delay2>0 ) Delay_us(delay2); if ( issue_cs!=0 ) wizspi_cs(WIZ_SPI2, VAL_HIGH); if ( print==1 ) { printf("{0x%02x}", spi_data); } else if ( print==2 ) { if (isprint(spi_data) || spi_data==0x0d || spi_data==0x0a ) putc(spi_data, WIZ_USART1); else printf("{0x%02x}", spi_data); } else if ( print==3 ) { if ( spi_data!=SPI_NULL ) { if (isprint(spi_data) || spi_data==0x0d || spi_data==0x0a ) putc(spi_data, WIZ_USART1); else printf("{0x%02x}", spi_data); } } return spi_data; } void process_esc_code(uint8* spi_recv, uint8* valid_data) { static uint8 esc_mode = 0; static uint8 esc2_mode = 0; uint8 spi2_recv = 0; (*valid_data) = 0; if ( (*spi_recv)==SPI_NULL ) { return; } if ( (*spi_recv)==SPI_ESC ) { esc_mode = 1; return; } if ( esc_mode==1 ) { esc_mode = 0; switch((*spi_recv)) { case SPI_F0: // Pass 'F0' to upper (*valid_data) = 1; (*spi_recv) = SPI_NULL; break; case SPI_F1: // Pass 'F1' to upper (*valid_data) = 1; (*spi_recv) = SPI_ESC; break; case SPI_SYNC: // Handle Sync Signal (*spi_recv) = wizspix_byte(WIZ_SPI2, (uint8)SPI_ESC, 1, 0, 0, g_opt_print_mode); if ( (*spi_recv)!=SPI_NULL ) printf("DBG>>> Need SPI_NULL\r\n"); (*spi_recv) = wizspix_byte(WIZ_SPI2, (uint8)SPI_SYNC, 1, 0, 0, g_opt_print_mode); if ( (*spi_recv)!=SPI_NULL ) printf("DBG>>> Need SPI_NULL\r\n"); break; case SPI_XON: // Handle Xon Signal break; case SPI_XOFF: // Handle Xoff Signal { uint8 found_xon = 0; while(1) { Delay_ms(10); printf("."); spi2_recv = wizspix_byte(WIZ_SPI2, (uint8)SPI_NULL, 1, 0, 0, g_opt_print_mode); if ( spi2_recv==SPI_NULL ) { continue; } if ( spi2_recv==SPI_ESC ) { esc2_mode = 1; return; } if ( esc2_mode==1 ) { esc2_mode = 0; switch(spi2_recv) { case SPI_XON: // Handle Xon Signal found_xon = 1; break; default: break; } } if ( found_xon==1 ) break; } } break; case SPI_ERR: // Handle Error Signal break; case SPI_ESC: // Lost ESC Data esc_mode = 1; //Just Ignore previous ESC break; default: break; } } else { (*valid_data) = 1; } } void spi_senddata(uint8* send_data, uint32 send_length, uint8* recv_data, uint32* recv_length) { uint32 i = 0; uint8 spi_recv_byte; uint8 spi_valid_data; (*recv_length) = 0; if ( send_length>SPI_RXBUFFER_MAX ) { printf("DBG>>> Error : send-data-size if too big\r\n"); return; } for (i=0; i>> SPI Send : %s\r\n", send_command); // Recv Response if ( str_find1==0 ) is_found1 = 1; if ( str_find2==0 ) is_found2 = 1; for (i=0; i0 ) { if ( debug_print==1 ) printf("\r\nDBG>>> SPI Recv \r\n%s\r\n", g_spi_rx_buffer); } // Error : SPI Buffer overflow if ( nResult==1 ) { printf("DBG>>> Error : g_spi_rx_buffer overflow\r\n"); return nResult; } // Error : Timeout or Not Found Success String if ( !(is_found1 && is_found2) ) { printf("DBG>>> Error : Timeout or Not Found Success String\r\n"); nResult = 2; return nResult; } return nResult; } int32 main(void) { uint32 uart_menu; uint32 uart_recv; uint8 spi_recv_byte; uint8 spi_valid_data; uint32 i; uint8 step_result = 0; uint32_t send_size = 1024; char szPrescaler[5] = {0,}; if(platform_init(NULL) != RET_OK) goto SPI_DEMO_ERROR; /////////////////////////////////////////////////////////////////// printf("\r\nDBG>>> Initializing SPI.....\r\n"); wizpf_gpio_init(GPIOB, GPIO_Pin_7, GMOD_IN_PULLDOWN); if ( wizspi_init(WIZ_SPI2)!=RET_OK ) { printf("DBG>>> Error : wizspi_init\r\n"); goto SPI_DEMO_ERROR; } strcpy(szPrescaler, "256"); /////////////////////////////////////////////////////////////////// printf("DBG>>> Checking SPI NULL.....\r\n"); step_result = 0; for (i=0; i<100; i++) { spi_recv_byte = wizspix_byte(WIZ_SPI2, SPI_NULL, 1, 0, 0, 1); if ( spi_recv_byte==SPI_NULL ) { step_result = 1; break; } } printf("\r\n"); if ( step_result==0 ) { printf("DBG>>> Error : Checking SPI NULL\r\n"); //goto SPI_DEMO_ERROR; } wizpf_gpio_init(GPIOB, GPIO_Pin_1, GMOD_OUT_PUSHPULL); GPIO_SetBits(GPIOB, GPIO_Pin_1); /////////////////////////////////////////////////////////////////// // Main Loop while(1) { printf("\r\n\r\n///////////////////////////////////////////////////////////\r\n"); printf("// 1 : Bypass mode\r\n"); printf("// 2 : for a while\r\n"); printf("// 4 : Send Test mode 1\r\n"); printf("// 5 : Send Test mode 2 (AT+SSEND)\r\n"); printf("// 6 : Recv Test mode 1\r\n"); printf("// 7 : AT Commands Example 1 (Read Information) \r\n"); printf("// 8 : AT Commands Example 2 (TCP Server & Data mode) \r\n"); printf("// 9 : AT Commands Example 3 (TCP Client & Command mode) \r\n"); printf("// (Esc) : Back to the menu \r\n"); printf("// (option) :

print-mode(%d)(2: all print 3:ignore SPI_NULL(0xF0)) \r\n", g_opt_print_mode); printf("// : SPI-Prescaler(%s) \r\n", szPrescaler); printf("// : Toggle send size(%d) \r\n", send_size); printf("///////////////////////////////////////////////////////////\r\n"); while(1) { uart_menu = getc_nonblk(WIZ_USART1); if ( uart_menu!=RET_NOK ) break; } if ( uart_menu == '0' ) { int i=0; printf("TEST Mode 0\r\n"); /* GPIO_ResetBits(GPIOB, GPIO_Pin_1); Delay_ms(100); GPIO_SetBits(GPIOB, GPIO_Pin_1); Delay_ms(100); GPIO_ResetBits(GPIOB, GPIO_Pin_1); Delay_ms(100); GPIO_SetBits(GPIOB, GPIO_Pin_1); Delay_ms(100); GPIO_ResetBits(GPIOB, GPIO_Pin_1); Delay_ms(100); GPIO_SetBits(GPIOB, GPIO_Pin_1); */ while(1) { uart_recv = getc_nonblk(WIZ_USART1); if ( uart_recv!=RET_NOK ) { if ( ((uint8)uart_recv)==0x1b ) break; spi_recv_byte = wizspix_byte(WIZ_SPI2, (uint8)uart_recv, 1, 0, 0, 3); } } } if ( uart_menu=='p' ) { if ( g_opt_print_mode==2 ) g_opt_print_mode = 3; else g_opt_print_mode = 2; } if ( uart_menu=='t' ) { if ( send_size==32 ) send_size = 128; else if ( send_size==128 ) send_size = 256; else if ( send_size==256 ) send_size = 1024; else if ( send_size==1024 ) send_size = 10240; else if ( send_size==10240 ) send_size = 102400; else if ( send_size==102400) send_size = 1024000; else if ( send_size==1024000) send_size = 32; } if ( uart_menu=='s' ) { if ( g_opt_spi_prescaler==SPI_BaudRatePrescaler_256 ) { strcpy(szPrescaler, "128"); g_opt_spi_prescaler = SPI_BaudRatePrescaler_128; } else if ( g_opt_spi_prescaler==SPI_BaudRatePrescaler_128 ) { strcpy(szPrescaler, "64"); g_opt_spi_prescaler = SPI_BaudRatePrescaler_64; } else if ( g_opt_spi_prescaler==SPI_BaudRatePrescaler_64 ) { strcpy(szPrescaler, "32"); g_opt_spi_prescaler = SPI_BaudRatePrescaler_32; } else if ( g_opt_spi_prescaler==SPI_BaudRatePrescaler_32 ) { strcpy(szPrescaler, "16"); g_opt_spi_prescaler = SPI_BaudRatePrescaler_16; } else if ( g_opt_spi_prescaler==SPI_BaudRatePrescaler_16 ) { strcpy(szPrescaler, "8"); g_opt_spi_prescaler = SPI_BaudRatePrescaler_8; } else if ( g_opt_spi_prescaler==SPI_BaudRatePrescaler_8 ) { strcpy(szPrescaler, "4"); g_opt_spi_prescaler = SPI_BaudRatePrescaler_4; } else if ( g_opt_spi_prescaler==SPI_BaudRatePrescaler_4 ) { strcpy(szPrescaler, "2"); g_opt_spi_prescaler = SPI_BaudRatePrescaler_2; } else if ( g_opt_spi_prescaler==SPI_BaudRatePrescaler_2 ) { strcpy(szPrescaler, "256"); g_opt_spi_prescaler = SPI_BaudRatePrescaler_256; } if ( wizspi_init(WIZ_SPI2)!=RET_OK ) { printf("DBG>>> Error : wizspi_init\r\n"); goto SPI_DEMO_ERROR; } printf("DBG>>> wizspi_init OK (%s)\r\n", szPrescaler); } if ( uart_menu=='1' ) { printf("\r\nDBG>>> Starting Bypass mode.....\r\n"); while(1) { if(wizpf_gpio_get(GPIOB, GPIO_Pin_7, VAL_FALSE) == VAL_HIGH) { spi_recv_byte = wizspix_byte(WIZ_SPI2, (uint8)SPI_NULL, 1, 0, 0, g_opt_print_mode); process_esc_code(&spi_recv_byte, &spi_valid_data); } uart_recv = getc_nonblk(WIZ_USART1); if ( uart_recv!=RET_NOK ) { if ( ((uint8)uart_recv)==0x1b ) break; spi_recv_byte = wizspix_byte(WIZ_SPI2, (uint8)uart_recv, 1, 0, 0, g_opt_print_mode); process_esc_code(&spi_recv_byte, &spi_valid_data); } } } if ( uart_menu=='2' ) { for (i=0; i<64; i++) { spi_recv_byte = wizspix_byte(WIZ_SPI2, (uint8)SPI_NULL, 1, 0, 0, 2); process_esc_code(&spi_recv_byte, &spi_valid_data); } printf("\r\n"); } if ( uart_menu=='4' ) { printf("\r\nDBG>>> Sending data....."); uint8_t buff_data = 'A'; for (i=0; i>> Sending data....."); uint8_t buff_data = 'A'; uint32 total_send = 0; while(1) { char szSendSPI[50] = "AT+SSEND=0,,,1024\r"; for (i=0; i=send_size ) break; } } if ( uart_menu=='6' ) { uint32 len_total_recv = 0; uint32 len_invalid_recv = 0; uint32 len_buff_recv = 0; printf("\r\nDBG>>> Receiving data....."); while(1) { if(wizpf_gpio_get(GPIOB, GPIO_Pin_7, VAL_FALSE) == VAL_HIGH) { spi_recv_byte = wizspix_byte(WIZ_SPI2, (uint8)SPI_NULL, 1, 0, 0, 3); if ( spi_recv_byte!=SPI_NULL ) { len_total_recv++; len_buff_recv++; if ( len_buff_recv==256 ) { printf("#"); len_buff_recv = 0; } if ( spi_recv_byte=='E' ) break; } else { len_invalid_recv++; } } uart_recv = getc_nonblk(WIZ_USART1); if ( ((uint8)uart_recv)==0x1b ) break; } printf("\r\nDBG>>> Received Data : %d(%d)\r\n", len_total_recv, len_invalid_recv); } if ( uart_menu=='7' ) { if ( send_and_check_command("AT" , 1, 500, 4, "[OK]", "", 1) !=0 ) { printf("DBG>>> Error : AT Command\r\n"); continue; } if ( send_and_check_command("AT+MINFO" , 1, 500, 6, "[OK]", "", 1) !=0 ) { printf("DBG>>> Error : AT Command\r\n"); continue; } if ( send_and_check_command("AT+WSCAN" , 1, 500, 10, "[OK]", "", 1) !=0 ) { printf("DBG>>> Error : AT Command\r\n"); continue; } if ( send_and_check_command("AT+MMAC=?" , 1, 500, 6, "[OK]", "", 1) !=0 ) { printf("DBG>>> Error : AT Command\r\n"); continue; } if ( send_and_check_command("AT+WSTAT" , 1, 500, 6, "[OK]", "", 1) !=0 ) { printf("DBG>>> Error : AT Command\r\n"); continue; } if ( send_and_check_command("AT+WNET=?" , 1, 500, 6, "[OK]", "", 1) !=0 ) { printf("DBG>>> Error : AT Command\r\n"); continue; } } if ( uart_menu=='8' ) { if ( send_and_check_command("AT" , 1, 500, 4, "[OK]", "", 1) !=0 ) { printf("DBG>>> Error : AT Command\r\n"); continue; } if ( send_and_check_command("AT+WLEAVE" , 1, 500, 6, "[OK]", "", 1) !=0 ) { printf("DBG>>> Error : AT Command\r\n"); continue; } if ( send_and_check_command("AT+WNET=1" , 1, 500, 6, "[OK]", "", 1) !=0 ) { printf("DBG>>> Error : AT Command\r\n"); continue; } if ( send_and_check_command("AT+WSET=0,Wiznet_Kaizen" , 1, 500, 6, "[OK]", "", 1) !=0 ) { printf("DBG>>> Error : AT Command\r\n"); continue; } if ( send_and_check_command("AT+WSEC=0,,123456789" , 1, 500, 6, "[OK]", "", 1) !=0 ) { printf("DBG>>> Error : AT Command\r\n"); continue; } if ( send_and_check_command("AT+WJOIN" , 1, 500, 40, "[OK]", "[Link-Up Event]", 1)!=0 ) { printf("DBG>>> Error : AT Command\r\n"); continue; } // This is for TCP Server & Command mode if ( send_and_check_command("AT+SCON=O,TSN,,,7000,1" , 1, 500, 6, "[OK]", "", 1) !=0 ) { printf("DBG>>> Error : AT Command\r\n"); continue; } while(1) { // clear spi rx buffer memset(g_spi_rx_buffer, 0, sizeof(g_spi_rx_buffer)); g_spi_rx_length = 0; uart_recv = 0; while(1) { static uint8 dot_print = 0; if ( (dot_print++)>100 ) { printf("."); dot_print = 0; } Delay_ms(10); uart_recv = getc_nonblk(WIZ_USART1); if ( ((uint8)uart_recv)==0x1b ) break; if ( wizpf_gpio_get(GPIOB, GPIO_Pin_7, VAL_FALSE)==VAL_HIGH ) { spi_recv_byte = wizspix_byte(WIZ_SPI2, (uint8)SPI_NULL, 1, 0, 0, 0); process_esc_code(&spi_recv_byte, &spi_valid_data); if ( spi_valid_data==1 ) { g_spi_rx_buffer[g_spi_rx_length] = spi_recv_byte; g_spi_rx_length++; } } else { break; } } if ( ((uint8)uart_recv)==0x1b ) break; uart_recv = getc_nonblk(WIZ_USART1); if ( ((uint8)uart_recv)==0x1b ) break; if ( g_spi_rx_length>0 ) { if ( strstr((char*)g_spi_rx_buffer, "[CONNECT")!=0 ) { printf("\r\nDBG>>> WizFi250 Event : Connect\r\n"); continue; } if ( strstr((char*)g_spi_rx_buffer, "[DISCONNECT")!=0 ) { printf("\r\nDBG>>> WizFi250 Event : Disconnect\r\n"); break; } if ( strstr((char*)g_spi_rx_buffer, "[Link-Down Event]")!=0 ) { printf("\r\nDBG>>> WizFi250 Event : Link Down\r\n"); break; } if ( strstr((char*)g_spi_rx_buffer, "[Reset Event]")!=0 ) { printf("\r\nDBG>>> WizFi250 Event : Reset\r\n"); break; } printf("\r\nRecv : %s \r\n", g_spi_rx_buffer); // when it receive a data, it returns "Hello!!!". if ( send_and_check_command("Hello!!!", 0, 0, 1, "", "", 0)!=0 ) { printf("WizFi250 Send Error \r\n"); break; } } } } if ( uart_menu=='9' ) { if ( send_and_check_command("AT" , 1, 500, 4, "[OK]", "", 1) !=0 ) { printf("DBG>>> Error : AT Command\r\n"); continue; } if ( send_and_check_command("AT+WLEAVE" , 1, 500, 6, "[OK]", "", 1) !=0 ) { printf("DBG>>> Error : AT Command\r\n"); continue; } if ( send_and_check_command("AT+WNET=1" , 1, 500, 6, "[OK]", "", 1) !=0 ) { printf("DBG>>> Error : AT Command\r\n"); continue; } if ( send_and_check_command("AT+WSET=0,Wiznet_Kaizen" , 1, 500, 6, "[OK]", "", 1) !=0 ) { printf("DBG>>> Error : AT Command\r\n"); continue; } if ( send_and_check_command("AT+WSEC=0,,123456789" , 1, 500, 6, "[OK]", "", 1) !=0 ) { printf("DBG>>> Error : AT Command\r\n"); continue; } if ( send_and_check_command("AT+WJOIN" , 1, 500, 40, "[OK]", "[Link-Up Event]", 1)!=0 ) { printf("DBG>>> Error : AT Command\r\n"); continue; } // This is for TCP Client & Data mode if ( send_and_check_command("AT+SCON=O,TCN,192.168.15.6,6000,,0" , 1, 500, 20, "[OK]", "[CONNECT", 1) !=0 ) { printf("DBG>>> Error : AT Command\r\n"); continue; } while(1) { // clear spi rx buffer memset(g_spi_rx_buffer, 0, sizeof(g_spi_rx_buffer)); g_spi_rx_length = 0; uart_recv = 0; while(1) { static uint8 dot_print = 0; if ( (dot_print++)>100 ) { printf("."); dot_print = 0; } Delay_ms(10); uart_recv = getc_nonblk(WIZ_USART1); if ( ((uint8)uart_recv)==0x1b ) break; if ( wizpf_gpio_get(GPIOB, GPIO_Pin_7, VAL_FALSE)==VAL_HIGH ) { spi_recv_byte = wizspix_byte(WIZ_SPI2, (uint8)SPI_NULL, 1, 0, 0, 0); process_esc_code(&spi_recv_byte, &spi_valid_data); if ( spi_valid_data==1 ) { g_spi_rx_buffer[g_spi_rx_length] = spi_recv_byte; g_spi_rx_length++; } } else { break; } } uart_recv = getc_nonblk(WIZ_USART1); if ( ((uint8)uart_recv)==0x1b ) break; if ( g_spi_rx_length>0 ) { if ( strstr((char*)g_spi_rx_buffer, "[CONNECT")!=0 ) { printf("\r\nDBG>>> WizFi250 Event : Connect \r\n"); break; } if ( strstr((char*)g_spi_rx_buffer, "[DISCONNECT")!=0 ) { printf("\r\nDBG>>> WizFi250 Event : Disconnect \r\n"); break; } if ( strstr((char*)g_spi_rx_buffer, "[Link-Down Event]")!=0 ) { printf("\r\nDBG>>> WizFi250 Event : Link Down \r\n"); break; } if ( strstr((char*)g_spi_rx_buffer, "[Reset Event]")!=0 ) { printf("\r\nDBG>>> WizFi250 Event : Reset \r\n"); break; } printf("\r\nDBG>>> Recv : %s \r\n", g_spi_rx_buffer); // when it receive a data, it returns "Hello!!!". if ( send_and_check_command("AT+SSEND=0,,,8\rHello!!!", 0, 500, 4, "[OK]", "", 0)!=0 ) { printf("DBG>>> Error : WizFi250 Send Error\r\n"); break; } } } } } SPI_DEMO_ERROR: printf("DBG>>> WizFi250 SPI Error\r\n"); return 0; }