// ---------- PIN settings --------- #define CONF_WINC_PIN_RESET PIN_PA27 #define CONF_WINC_PIN_CHIP_ENABLE PIN_PA28 #define CONF_WINC_PIN_WAKE PIN_PB08 // ---------- SPI settings --------- #define CONF_WINC_USE_SPI (1) /** SPI pin and instance settings. */ #define CONF_WINC_SPI_MODULE SERCOM2 #define CONF_WINC_SPI_SERCOM_MUX SPI_SIGNAL_MUX_SETTING_D #define CONF_WINC_SPI_PINMUX_PAD0 PINMUX_PA12C_SERCOM2_PAD0 /* out */ #define CONF_WINC_SPI_PINMUX_PAD1 PINMUX_PA13C_SERCOM2_PAD1 /* sck */ #define CONF_WINC_SPI_PINMUX_PAD2 PINMUX_UNUSED /* cs driven from software */ #define CONF_WINC_SPI_PINMUX_PAD3 PINMUX_PA15C_SERCOM2_PAD3 /* in */ #define CONF_WINC_SPI_CS_PIN PIN_PA14 /** SPI interrupt pin. */ #define CONF_WINC_SPI_INT_PIN PIN_PB09A_EIC_EXTINT9 #define CONF_WINC_SPI_INT_MUX MUX_PB09A_EIC_EXTINT9 #define CONF_WINC_SPI_INT_EIC (9)
static void configure_console(void) { struct usart_config usart_conf; usart_get_config_defaults(&usart_conf); usart_conf.mux_setting = EDBG_CDC_SERCOM_MUX_SETTING; usart_conf.pinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0; usart_conf.pinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1; usart_conf.pinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2; usart_conf.pinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3; usart_conf.baudrate = 115200; stdio_serial_init(&cdc_uart_module, EDBG_CDC_MODULE, &usart_conf); usart_enable(&cdc_uart_module); }
int main(void) { tstrWifiInitParam settings; int8_t ret; // Initialize the board. system_init(); // Initialize the UART console. configure_console(); printf(STRING_HEADER); // Initialize the BSP. nm_bsp_init(); // Initialize Wi-Fi parameters structure. memset((uint8_t *)&settings, 0, sizeof(tstrWifiInitParam)); //Initialize Wi-Fi driver with data and status callbacks. ret = m2m_wifi_init(&settings); if (M2M_SUCCESS != ret) { printf("main: m2m_wifi_init call error!(%d)\r\n", ret); while (1); } // Display WINC1500 chip information. printf("Chip ID : \r\t\t\t%x\r\n", (unsigned int)nmi_get_chipid()); printf("RF Revision ID : \r\t\t\t%x\r\n", (unsigned int)nmi_get_rfrevid()); printf("Done.\r\n\r\n"); while (1); return 0; }
// Initialize Wi-Fi parameters structure. memset((uint8_t *)&settings, 0, sizeof(tstrWifiInitParam)); // Initialize Wi-Fi driver with data and status callbacks. settings.pfAppWifiCb = wifi_cb; ret = m2m_wifi_init(&settings); if (M2M_SUCCESS != ret) { printf("main: m2m_wifi_init call error!(%d)\r\n", ret); while (1); }
// Initialize AP mode parameters structure with SSID, channel and OPEN security type. memset(&strM2MAPConfig, 0x00, sizeof(tstrM2MAPConfig)); strcpy((char *)&strM2MAPConfig.au8SSID, MAIN_WLAN_SSID); strM2MAPConfig.u8ListenChannel = MAIN_WLAN_CHANNEL; strM2MAPConfig.u8SecType = MAIN_WLAN_AUTH; strM2MAPConfig.au8DHCPServerIP[0] = 192; strM2MAPConfig.au8DHCPServerIP[1] = 168; strM2MAPConfig.au8DHCPServerIP[2] = 1; strM2MAPConfig.au8DHCPServerIP[3] = 1; #if USE_WEP strcpy((char *)&strM2MAPConfig.au8WepKey, MAIN_WLAN_WEP_KEY); strM2MAPConfig.u8KeySz = strlen(MAIN_WLAN_WEP_KEY); strM2MAPConfig.u8KeyIndx = MAIN_WLAN_WEP_KEY_INDEX; #endif /* Bring up AP mode with parameters structure. */ ret = m2m_wifi_enable_ap(&strM2MAPConfig); if (M2M_SUCCESS != ret) { printf("main: m2m_wifi_enable_ap call error!\r\n"); while (1); } printf("AP mode started. You can connect to %s.\r\n", (char *)MAIN_WLAN_SSID); while (1) { while (m2m_wifi_handle_events(NULL) != M2M_SUCCESS); // Handle pending events from network controller. } // callback static void wifi_cb(uint8_t u8MsgType, void *pvMsg) { switch (u8MsgType) { case M2M_WIFI_RESP_CON_STATE_CHANGED: { tstrM2mWifiStateChanged *pstrWifiState = (tstrM2mWifiStateChanged *)pvMsg; if (pstrWifiState->u8CurrState == M2M_WIFI_CONNECTED) { } else if (pstrWifiState->u8CurrState == M2M_WIFI_DISCONNECTED) { printf("Station disconnected\r\n"); } break; } case M2M_WIFI_REQ_DHCP_CONF: { uint8_t *pu8IPAddress = (uint8_t *)pvMsg; printf("Station connected\r\n"); printf("Station IP is %u.%u.%u.%u\r\n", pu8IPAddress[0], pu8IPAddress[1], pu8IPAddress[2], pu8IPAddress[3]); break; } default: { break; } } } main.h: /** Security mode */ #define USE_WEP (1) /*< Set to (1) to use WEP, and (0) to use OPEN */ /** AP mode Settings */ #define MAIN_WLAN_SSID "WINC1500_AP" /* < SSID */ #if USE_WEP #define MAIN_WLAN_AUTH M2M_WIFI_SEC_WEP /* < Security manner */ #define MAIN_WLAN_WEP_KEY "1234567890" /* < Security Key in WEP Mode */ #define MAIN_WLAN_WEP_KEY_INDEX (1) #else #define MAIN_WLAN_AUTH M2M_WIFI_SEC_OPEN /* < Security manner */ #endif #define MAIN_WLAN_CHANNEL (6) /* < Channel number */
/* Initialize Wi-Fi parameters structure. */ memset((uint8_t *)&settings, 0, sizeof(tstrWifiInitParam)); /* Initialize Wi-Fi driver with data and status callbacks. */ settings.pfAppWifiCb = wifi_cb; ret = m2m_wifi_init(&settings); if (M2M_SUCCESS != ret) { printf("main: m2m_wifi_init call error!(%d)\r\n", ret); while (1); } /* Request scan. */ m2m_wifi_request_scan(M2M_WIFI_CH_ALL);
static void wifi_cb(uint8_t u8MsgType, void *pvMsg) { switch (u8MsgType) { case M2M_WIFI_RESP_SCAN_DONE: { tstrM2mScanDone *pstrInfo = (tstrM2mScanDone *)pvMsg; scan_request_index = 0; if (pstrInfo->u8NumofCh >= 1) { m2m_wifi_req_scan_result(scan_request_index); scan_request_index++; } else { m2m_wifi_request_scan(M2M_WIFI_CH_ALL); } break; } case M2M_WIFI_RESP_SCAN_RESULT: { tstrM2mWifiscanResult *pstrScanResult = (tstrM2mWifiscanResult *)pvMsg; uint16_t demo_ssid_len; uint16_t scan_ssid_len = strlen((const char *)pstrScanResult->au8SSID); /* display founded AP. */ printf("[%d] SSID:%s\r\n", scan_request_index, pstrScanResult->au8SSID); num_founded_ap = m2m_wifi_get_num_ap_found(); if (scan_ssid_len) { /* check same SSID. */ demo_ssid_len = strlen((const char *)MAIN_WLAN_SSID); if( (demo_ssid_len == scan_ssid_len) && (!memcmp(pstrScanResult->au8SSID, (uint8_t *)MAIN_WLAN_SSID, demo_ssid_len))) { /* A scan result matches an entry in the preferred AP List. * Initiate a connection request. */ printf("Found %s \r\n", MAIN_WLAN_SSID); m2m_wifi_connect((char *)MAIN_WLAN_SSID,sizeof(MAIN_WLAN_SSID), MAIN_WLAN_AUTH,(void *)MAIN_WLAN_PSK,M2M_WIFI_CH_ALL); break; } } if (scan_request_index < num_founded_ap) { m2m_wifi_req_scan_result(scan_request_index); scan_request_index++; } else { printf("can not find AP %s\r\n", MAIN_WLAN_SSID); m2m_wifi_request_scan(M2M_WIFI_CH_ALL); } break; } case M2M_WIFI_RESP_CON_STATE_CHANGED: { tstrM2mWifiStateChanged *pstrWifiState = (tstrM2mWifiStateChanged *)pvMsg; if (pstrWifiState->u8CurrState == M2M_WIFI_CONNECTED) { m2m_wifi_request_dhcp_client(); } else if (pstrWifiState->u8CurrState == M2M_WIFI_DISCONNECTED) { printf("Wi-Fi disconnected\r\n"); /* Request scan. */ m2m_wifi_request_scan(M2M_WIFI_CH_ALL); } break; } case M2M_WIFI_REQ_DHCP_CONF: { uint8_t *pu8IPAddress = (uint8_t *)pvMsg; printf("Wi-Fi connected\r\n"); printf("Wi-Fi IP is %u.%u.%u.%u\r\n", pu8IPAddress[0], pu8IPAddress[1], pu8IPAddress[2], pu8IPAddress[3]); break; } default: { break; } } }
static void wifi_cb(uint8_t u8MsgType, void *pvMsg) { switch (u8MsgType) { case M2M_WIFI_RESP_CON_STATE_CHANGED: { tstrM2mWifiStateChanged *pstrWifiState = (tstrM2mWifiStateChanged *)pvMsg; if (pstrWifiState->u8CurrState == M2M_WIFI_CONNECTED) { printf("wifi_cb: M2M_WIFI_RESP_CON_STATE_CHANGED: CONNECTED\r\n"); m2m_wifi_request_dhcp_client(); } else if (pstrWifiState->u8CurrState == M2M_WIFI_DISCONNECTED) { printf("wifi_cb: M2M_WIFI_RESP_CON_STATE_CHANGED: DISCONNECTED\r\n"); wifi_connected = 0; m2m_wifi_connect((char *)MAIN_WLAN_SSID, sizeof(MAIN_WLAN_SSID), MAIN_WLAN_AUTH, (char *)MAIN_WLAN_PSK, M2M_WIFI_CH_ALL); } break; } case M2M_WIFI_REQ_DHCP_CONF: { uint8_t *pu8IPAddress = (uint8_t *)pvMsg; wifi_connected = 1; printf("wifi_cb: M2M_WIFI_REQ_DHCP_CONF: IP is %u.%u.%u.%u\r\n",pu8IPAddress[0], pu8IPAddress[1], pu8IPAddress[2], pu8IPAddress[3]); break; } default: { break; } } }
m2m_wifi_enable_dhcp(0);
static void wifi_cb(uint8_t u8MsgType, void *pvMsg) { printf("wifi_cb: u8MsgType= %d\n",u8MsgType); switch (u8MsgType) { case M2M_WIFI_RESP_CON_STATE_CHANGED: { tstrM2mWifiStateChanged *pstrWifiState = (tstrM2mWifiStateChanged *)pvMsg; if (pstrWifiState->u8CurrState == M2M_WIFI_CONNECTED) { printf("wifi_cb: M2M_WIFI_RESP_CON_STATE_CHANGED: CONNECTED\r\n"); tstrM2MIPConfig ip_client; ip_client.u32StaticIP = _htonl(0xc0a8142d); //corresponds to 192.168.20.45 ip_client.u32DNS = _htonl(0xc0a80266); //corresponds to 192.168.2.102 ip_client.u32SubnetMask =_htonl(0xFFFFFF00); //corresponds to 255.255.255.0 ip_client.u32Gateway = _htonl(0xc0a81401); //corresponds to 192.168.20.1 m2m_wifi_set_static_ip(&ip_client); wifi_connected = 1; } else { if (pstrWifiState->u8CurrState == M2M_WIFI_DISCONNECTED) { printf("wifi_cb: M2M_WIFI_RESP_CON_STATE_CHANGED: DISCONNECTED\r\n"); wifi_connected = 0; m2m_wifi_connect((char *)MAIN_WLAN_SSID, sizeof(MAIN_WLAN_SSID), MAIN_WLAN_AUTH, (char *)MAIN_WLAN_PSK, M2M_WIFI_CH_ALL); } } break; } default: break; } }
Berkeley API | WINC API | Server / Client | TCP / UDP | Description |
---|---|---|---|---|
socket | socket | Both | Both | Creates a new socket |
connect | connect | Client | Tcp | Establishes a TCP connection to a remote server. |
bind | bind | Server | Both | Bindit socket with IP address and port |
listen | listen | Server | Tcp | Creates a TCP server |
send | send | Both | Both | Send data to socket |
sendto | sendto | Both | UDP | Sends packet over UDP |
recv | recv | Both | Both | Receives a packet |
recvfrom | recvfrom | Both | Both | Receives a packet |
close | close | Both | Both | Closes connection and frees up resources. |
gethostbyname | gethostbyname | Both | Both | Gets the IP address by host name |
setsockopt | setsockopt | Both | Both | Sets socket options |
htons / ntohs | htons / ntohs | Both | Both | Changes the byte order in 2-byte integers from the host representation to the network and vice versa. |
htonl / ntohl | htonl / ntohl | Both | Both | Changes the byte order in 4-byte integers from the host representation to the network and vice versa. |
struct sockaddr_in src_addr; addr.sin_family = AF_INET; addr.sin_port = _htons(MAIN_WIFI_M2M_SERVER_PORT); addr.sin_addr.s_addr = _htonl(MAIN_WIFI_M2M_SERVER_IP);
socketInit(); registerSocketCallback(socket_cb, NULL); // Connect to router. m2m_wifi_connect((char *)MAIN_WLAN_SSID, sizeof(MAIN_WLAN_SSID), MAIN_WLAN_AUTH, (char *)MAIN_WLAN_PSK, M2M_WIFI_CH_ALL); printf("m2m_wifi_connect!\r\n");
SOCKET rx_socket = -1; while(1): if (rx_socket < 0) { if ((rx_socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { printf("main: failed to create RX UDP Client socket error!\r\n"); continue; } // Socket bind bind(rx_socket, (struct sockaddr *)&src_addr, sizeof(struct sockaddr_in)); } if (wifi_connected == M2M_WIFI_CONNECTED) { // Open client socket. if (rx_socket < 0) { if ((rx_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) { printf("main: failed to create TCP client socket error!\r\n"); continue; } // Connect server printf("socket_number new connection: %d\r\n", rx_socket); ret=connect(rx_socket, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)); printf("ret value: %d\r\n", ret); connected = FALSE; if (ret < 0) { close(rx_socket); rx_socket = -1; } } }
static void socket_cb(SOCKET sock, uint8_t u8Msg, void *pvMsg) { // Check for socket event on RX socket. if (sock == rx_socket) { if (u8Msg == SOCKET_MSG_BIND) { tstrSocketBindMsg *pstrBind = (tstrSocketBindMsg *)pvMsg; if (pstrBind && pstrBind->status == 0) { // Prepare next buffer reception. sock_bind_state = 1; recv(sock, udp_data_rx, MAIN_WIFI_M2M_BUFFER_SIZE, 0); } else { printf("socket_cb: bind error!\r\n"); } } else if (u8Msg == SOCKET_MSG_RECVFROM) { tstrSocketRecvMsg *pstrRx = (tstrSocketRecvMsg *)pvMsg; if (pstrRx->pu8Buffer && pstrRx->s16BufferSize) { delay = 0; sock_rx_state = pstrRx->s16BufferSize; printf("socket_cb udp recv!\r\n"); printf("rx packet length= %d\n",pstrRx->s16BufferSize); } else { if (pstrRx->s16BufferSize == SOCK_ERR_TIMEOUT) { // Prepare next buffer reception. recv(sock, udp_data_rx, MAIN_WIFI_M2M_BUFFER_SIZE, 0); } } } if (u8Msg == SOCKET_MSG_SENDTO) { recv(sock, udp_data_rx, MAIN_WIFI_M2M_BUFFER_SIZE, 0); sock_tx_state = 1; } } if (sock == tcp_client_socket) { switch (u8Msg) { // Socket connected case SOCKET_MSG_CONNECT: { tstrSocketConnectMsg *pstrConnect = (tstrSocketConnectMsg *)pvMsg; if (pstrConnect && pstrConnect->s8Error >= 0) { printf("socket_cb tcp connect!\r\n"); tcp_ready_to_send=1; } else { close(tcp_client_socket); tcp_client_socket = -1; } } break; // Message send case SOCKET_MSG_SEND: { printf("socket_cb tcp send!\r\n"); recv(tcp_client_socket, tcp_data_rx, sizeof(tcp_data_rx), 0); tcp_tx_ready=1; } break; // Message receive case SOCKET_MSG_RECV: { tstrSocketRecvMsg *pstrRecv = (tstrSocketRecvMsg *)pvMsg; if (pstrRecv && pstrRecv->s16BufferSize > 0) { tcp_rx_ready=pstrRecv->s16BufferSize; } else { close(tcp_client_socket); tcp_client_socket = -1; } } break; default: break; } } }
m2m_wifi_handle_events(NULL);
Source: https://habr.com/ru/post/275359/
All Articles