Microchip® Advanced Software Framework

common/components/wifi/winc1500/power_profiling_demo/main.c File Reference

WINC1500 Power Profiling APP.

Copyright (c) 2017-2018 Microchip Technology Inc. and its subsidiaries.

#include "asf.h"
#include "driver/include/m2m_wifi.h"
#include "socket/include/socket.h"
#include "driver/include/m2m_periph.h"
#include "driver/source/nmasic.h"
#include "main.h"
#include "string.h"

Macros

#define DOZE_MODE   3
 
#define STRING_EOL   "\r\n"
 
#define STRING_HEADER
 

Functions

static void configure_console (void)
 Configure UART console. More...
 
static int8_t connect_to_server (char *ip_addr, int tcp_mode)
 Creates and connects to an secure socket to be used for SSL. More...
 
static void disable_pullups (void)
 Disables pull-ups for the pins in WINC which are used for communication with host MCU. More...
 
static void execute_state_machine (int server_mode, low_power_test_param *tp)
 State machine to connect and exchange data with a TCP, SSL or a UDP server. More...
 
static void handle_tcp_socket_cb (SOCKET sock, uint8_t u8Msg, void *pvMsg)
 Handles Callback event for TCP client socket. More...
 
static void handle_udp_client_socket_cb (SOCKET sock, uint8_t u8Msg, void *pvMsg)
 Handles Callback event for UDP client socket. More...
 
static void handle_udp_serv_socket_cb (SOCKET sock, uint8_t u8Msg, void *pvMsg)
 Handles Callback event for UDP server socket. More...
 
int main (void)
 Main application function. More...
 
static void reset_start_time (void)
 Resets the timer. More...
 
static void run_profiling_app (int enable_ssl, low_power_test_param *tp)
 Execute profiling App. More...
 
static void run_provisioning (low_power_test_param *tp)
 Switch the WINC to provisioning mode. More...
 
static void run_sample_test_app (int enable_ssl, low_power_test_param *tp)
 Execute Sample Test App - connect and transmit on button press. More...
 
static void run_udp_client (uint32_t target_ip, uint32_t delay_btwn_packets, uint32_t packet_size)
 Run UDP client on WINC. More...
 
static void run_udp_server (void)
 Run UDP server on WINC. More...
 
static void set_state (app_states state)
 Set the application status. More...
 
static void socket_cb (SOCKET sock, uint8_t u8Msg, void *pvMsg)
 Callback function for all socket events. More...
 
static void start_low_power_mode_test (low_power_test_param *tp)
 Function to set the desired low power mode to WINC and execute UDP server in case of Auto deep sleep. More...
 
static void start_receive_test (void)
 Execute Reception test by receiving UDP frames in UDP server mode. More...
 
static void start_transmission_test (low_power_test_param *tp)
 Execute Transmission test by transmitting UDP frames continously. More...
 
void SysTick_Handler (void)
 SysTick handler used to measure precise delay. More...
 
static void test_init (void)
 Connect to a pre-configured AP and initialize the server address. More...
 
static void wifi_cb (uint8_t u8MsgType, void *pvMsg)
 Callback to get the Wi-Fi status update. More...
 
static void winc_init (void)
 Initialize WINC. More...
 

Variables

struct sockaddr_in addr
 
static app_states app_state = IDLE_STATE
 
static struct usart_module cdc_uart_module
 UART module for debug. More...
 
bool curr_button_state = false
 
static int dtim = DEFAULT_DTIM_FLAG
 
static uint8 fake_data [TX_PACKET_SIZE]
 
static uint32_t gateway_ip = 0
 
static uint8_t gau8SocketTestBuffer [TEST_WINC_RECEIVE_BUFFER] = {0}
 Test buffer. More...
 
static tstrM2MAPConfig gstrM2MAPConfig
 
static int listen_interval = 0
 
static int menu_number = 0
 
volatile uint32_t ms_ticks = 0
 SysTick counter to avoid busy wait delay. More...
 
static uint32_t packetCnt = 0
 UDP packet count. More...
 
bool prev_button_state = false
 
static int ps_mode = 0
 
static uint32 send_cnt = 0
 
static int ssl_enable = 0
 
static uint32_t start_time = 0
 
static SOCKET tcp_client_socket = -1
 Client and Server socket handler. More...
 
bool tcp_connected = false
 
static int transmit_pwr_mode
 
static uint32 u32TotalBytesSent = 0
 
static SOCKET udp_client_socket = -1
 
bool udp_keep_alive_sent = false
 
static SOCKET udp_server_socket = -1
 
bool upload_completed = false
 
static uint8_t wifi_connected
 

#define DOZE_MODE   3
#define STRING_EOL   "\r\n"
#define STRING_HEADER
Value:
"-- WINC1500 Power Profiling App --"STRING_EOL \
"-- Compiled: "__DATE__ " "__TIME__ " --"STRING_EOL
#define BOARD_NAME
Definition: app_init.h:140
#define STRING_EOL
Definition: common/components/wifi/winc1500/power_profiling_demo/main.c:112

Referenced by main().

static void configure_console ( void  )
static
static int8_t connect_to_server ( char *  ip_addr,
int  tcp_mode 
)
static

Creates and connects to an secure socket to be used for SSL.

Parameters
[in]None.
Returns
SOCK_ERR_NO_ERROR if success, -1 if socket create error, SOCK_ERR_INVALID if socket connect error.

References _htons, AF_INET, close(), connect(), nmi_inet_addr(), printf, in_addr::s_addr, sockaddr_in::sin_addr, sockaddr_in::sin_family, sockaddr_in::sin_port, SOCK_ERR_INVALID, SOCK_ERR_NO_ERROR, SOCK_STREAM, socket(), SOCKET_FLAGS_SSL, tcp_client_socket, TEST_SSL_SERVER_PORT, and TEST_TCP_SERVER_PORT.

Referenced by execute_state_machine(), and run_sample_test_app().

static void disable_pullups ( void  )
static

Disables pull-ups for the pins in WINC which are used for communication with host MCU.

Parameters
[in]None
Returns
None.

References m2m_periph_pullup_ctrl(), M2M_PERIPH_PULLUP_DIS_HOST_WAKEUP, M2M_PERIPH_PULLUP_DIS_SD_CMD_SPI_SCK, and M2M_PERIPH_PULLUP_DIS_SD_DAT0_SPI_TXD.

Referenced by winc_init().

static void execute_state_machine ( int  server_mode,
low_power_test_param tp 
)
static
static void handle_tcp_socket_cb ( SOCKET  sock,
uint8_t  u8Msg,
void *  pvMsg 
)
static

Handles Callback event for TCP client socket.

Parameters
[in]sockSocket for which event callback is called
[in]u8MsgEvent to be handled
[in]*pvMsgData related to the event
Returns
None.

References close(), fake_data, port_pin_set_output_level(), printf, recv(), tstrSocketRecvMsg::s16BufferSize, tstrSocketConnectMsg::s8Error, set_state(), SOCK_ERR_NO_ERROR, SOCKET_CONNECTION_STATE, SOCKET_MSG_CONNECT, SOCKET_MSG_RECV, SOCKET_MSG_SEND, tcp_client_socket, tcp_connected, TEST_SSL_UPLOAD_SIZE, TX_PACKET_SIZE, u32TotalBytesSent, and upload_completed.

Referenced by socket_cb().

static void handle_udp_client_socket_cb ( SOCKET  sock,
uint8_t  u8Msg,
void *  pvMsg 
)
static

Handles Callback event for UDP client socket.

Parameters
[in]sockSocket for which event callback is called
[in]u8MsgEvent to be handled
[in]*pvMsgData related to the event
Returns
None.

References SOCKET_MSG_SENDTO, and udp_keep_alive_sent.

Referenced by socket_cb().

static void handle_udp_serv_socket_cb ( SOCKET  sock,
uint8_t  u8Msg,
void *  pvMsg 
)
static

Handles Callback event for UDP server socket.

Parameters
[in]sockSocket for which event callback is called
[in]u8MsgEvent to be handled
[in]*pvMsgData related to the event
Returns
None.

References gau8SocketTestBuffer, packetCnt, printf, tstrSocketRecvMsg::pu8Buffer, recvfrom(), tstrSocketRecvMsg::s16BufferSize, SOCK_ERR_TIMEOUT, SOCKET_MSG_BIND, SOCKET_MSG_RECVFROM, tstrSocketBindMsg::status, and TEST_WINC_RECEIVE_BUFFER.

Referenced by socket_cb().

static void reset_start_time ( void  )
static

Resets the timer.

Parameters
[in]None
Returns
None.

References ms_ticks, and start_time.

Referenced by run_sample_test_app().

static void run_profiling_app ( int  enable_ssl,
low_power_test_param tp 
)
static

Execute profiling App.

Parameters
[in]enable_sslTo connect to a secure server
[in]tpTest parameter
Returns
None.

References curr_button_state, execute_state_machine(), m2m_wifi_handle_events(), NULL, port_pin_get_input_level(), and prev_button_state.

Referenced by main().

static void run_provisioning ( low_power_test_param tp)
static

Switch the WINC to provisioning mode.

Parameters
[in]tptest parameter
Returns
None.

References tstrM2MAPConfig::au8SSID, m2m_wifi_handle_events(), m2m_wifi_start_provision_mode(), NULL, and printf.

Referenced by main().

static void run_udp_client ( uint32_t  target_ip,
uint32_t  delay_btwn_packets,
uint32_t  packet_size 
)
static

Run UDP client on WINC.

Parameters
[in]target_ipIP address of the server
[in]delay_btwn_packetsdelay between sending 2 packets
[in]packet_sizesize of each packet
Returns
None.

References addr, AF_INET, gau8SocketTestBuffer, M2M_SUCCESS, M2M_WIFI_CONNECTED, m2m_wifi_handle_events(), ms_ticks, NULL, printf, ret, in_addr::s_addr, sendto(), set_state(), sockaddr_in::sin_addr, SOCK_DGRAM, socket(), SOCKET_CONNECTION_STATE, start_time, udp_client_socket, udp_keep_alive_sent, and wifi_connected.

Referenced by execute_state_machine(), run_sample_test_app(), and start_transmission_test().

static void run_udp_server ( void  )
static

Run UDP server on WINC.

Parameters
[in]None
Returns
None.

References addr, AF_INET, bind(), M2M_WIFI_CONNECTED, m2m_wifi_handle_events(), NULL, printf, SOCK_DGRAM, socket(), udp_server_socket, and wifi_connected.

Referenced by start_low_power_mode_test(), and start_receive_test().

static void set_state ( app_states  state)
static

Set the application status.

Parameters
[in]stateState value to be set
Returns
None.

References app_state, and state.

Referenced by execute_state_machine(), handle_tcp_socket_cb(), run_udp_client(), and wifi_cb().

static void socket_cb ( SOCKET  sock,
uint8_t  u8Msg,
void *  pvMsg 
)
static

Callback function for all socket events.

Parameters
[in]socksocket handler.
[in]u8MsgType of Socket notification
[in]pvMsgA structure contains notification informations.
Returns
None.

References handle_tcp_socket_cb(), handle_udp_client_socket_cb(), handle_udp_serv_socket_cb(), tcp_client_socket, udp_client_socket, and udp_server_socket.

Referenced by winc_init().

static void start_low_power_mode_test ( low_power_test_param tp)
static
static void start_receive_test ( void  )
static

Execute Reception test by receiving UDP frames in UDP server mode.

Parameters
[in]None
Returns
None.

References m2m_wifi_enable_sntp(), run_udp_server(), and test_init().

Referenced by main().

static void start_transmission_test ( low_power_test_param tp)
static

Execute Transmission test by transmitting UDP frames continously.

Parameters
[in]tptest parameter
Returns
None.

References gateway_ip, m2m_wifi_enable_sntp(), run_udp_client(), test_init(), TX_DELAY_MS, and TX_PACKET_SIZE.

Referenced by main().

void SysTick_Handler ( void  )

SysTick handler used to measure precise delay.

Parameters
[in]None
Returns
None.

References ms_ticks.

static void wifi_cb ( uint8_t  u8MsgType,
void *  pvMsg 
)
static

Callback to get the Wi-Fi status update.

Parameters
[in]u8MsgTypeType of Wi-Fi notification.
[in]pvMsgA pointer to a buffer containing the notification parameters.
Returns
None.

AP_CONNECTION_TOGGLE

AP_CONNECTION_TOGGLE

References close(), gateway_ip, M2M_SUCCESS, M2M_WIFI_CONNECTED, M2M_WIFI_DISCONNECTED, M2M_WIFI_REQ_DHCP_CONF, M2M_WIFI_RESP_CON_STATE_CHANGED, M2M_WIFI_RESP_PROVISION_INFO, port_pin_toggle_output_level(), printf, set_state(), tcp_client_socket, tcp_connected, tstrM2MIPConfig::u32Gateway, tstrM2mWifiStateChanged::u8CurrState, tstrM2mWifiStateChanged::u8ErrCode, tstrM2MProvisionInfo::u8Status, udp_client_socket, udp_server_socket, wifi_connected, and WIFI_CONNECTION_STATE.

Referenced by winc_init().

static void winc_init ( void  )
static

struct sockaddr_in addr
app_states app_state = IDLE_STATE
static
struct usart_module cdc_uart_module
static

UART module for debug.

bool curr_button_state = false
int dtim = DEFAULT_DTIM_FLAG
static

Referenced by main().

uint32_t gateway_ip = 0
static
uint8_t gau8SocketTestBuffer[TEST_WINC_RECEIVE_BUFFER] = {0}
static

Test buffer.

Referenced by handle_udp_serv_socket_cb(), and run_udp_client().

tstrM2MAPConfig gstrM2MAPConfig
static
Initial value:
= {
"WINC1500_POWERSAVE_AP",
0,
"1234567890",
{192, 168, 1, 1}
}
Definition: winc3400/wifi_drv/driver/include/m2m_types.h:951
Definition: winc3400/wifi_drv/driver/include/m2m_types.h:916
#define WEP_40_KEY_STRING_SIZE
Definition: winc3400/wifi_drv/driver/include/m2m_types.h:351
Definition: winc3400/wifi_drv/driver/include/m2m_types.h:937
int listen_interval = 0
static

Referenced by main().

int menu_number = 0
static

Referenced by main().

volatile uint32_t ms_ticks = 0

SysTick counter to avoid busy wait delay.

TLS read functionality.

SysTick counter for non busy wait delay.

uint32_t packetCnt = 0
static

UDP packet count.

Referenced by handle_udp_serv_socket_cb().

bool prev_button_state = false
int ps_mode = 0
static

Referenced by main().

uint32 send_cnt = 0
static

Referenced by run_sample_test_app().

int ssl_enable = 0
static

Referenced by main().

uint32_t start_time = 0
static

Referenced by reset_start_time(), and run_udp_client().

SOCKET tcp_client_socket = -1
static
int transmit_pwr_mode
static

Referenced by main().

uint32 u32TotalBytesSent = 0
static

Referenced by handle_tcp_socket_cb().

SOCKET udp_client_socket = -1
static

Referenced by run_udp_client(), socket_cb(), and wifi_cb().

SOCKET udp_server_socket = -1
static

Referenced by run_udp_server(), socket_cb(), and wifi_cb().

bool upload_completed = false