iowfehu;gdbjwHJAOPIHO?L
Fork of X_NUCLEO_IDW01M1 by
Spwf/wifi_driver.c@6:e7a3fca2df10, 2016-05-09 (annotated)
- Committer:
- mridup
- Date:
- Mon May 09 10:38:22 2016 +0000
- Revision:
- 6:e7a3fca2df10
- Parent:
- 1:bd9db471d47d
- Child:
- 8:0f302a13e21b
Socket Server support.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mridup | 0:dc55f40eb04f | 1 | /** |
mridup | 0:dc55f40eb04f | 2 | ****************************************************************************** |
mridup | 0:dc55f40eb04f | 3 | * @file wifi_driver.c |
mridup | 0:dc55f40eb04f | 4 | * @author Central LAB |
mridup | 0:dc55f40eb04f | 5 | * @version V2.0.0 |
mridup | 0:dc55f40eb04f | 6 | * @date 01-March-2016 |
mridup | 0:dc55f40eb04f | 7 | * @brief Enable Wi-Fi functionality using AT cmd set |
mridup | 0:dc55f40eb04f | 8 | ****************************************************************************** |
mridup | 0:dc55f40eb04f | 9 | * @attention |
mridup | 0:dc55f40eb04f | 10 | * |
mridup | 0:dc55f40eb04f | 11 | * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> |
mridup | 0:dc55f40eb04f | 12 | * |
mridup | 0:dc55f40eb04f | 13 | * Redistribution and use in source and binary forms, with or without modification, |
mridup | 0:dc55f40eb04f | 14 | * are permitted provided that the following conditions are met: |
mridup | 0:dc55f40eb04f | 15 | * 1. Redistributions of source code must retain the above copyright notice, |
mridup | 0:dc55f40eb04f | 16 | * this list of conditions and the following disclaimer. |
mridup | 0:dc55f40eb04f | 17 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mridup | 0:dc55f40eb04f | 18 | * this list of conditions and the following disclaimer in the documentation |
mridup | 0:dc55f40eb04f | 19 | * and/or other materials provided with the distribution. |
mridup | 0:dc55f40eb04f | 20 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
mridup | 0:dc55f40eb04f | 21 | * may be used to endorse or promote products derived from this software |
mridup | 0:dc55f40eb04f | 22 | * without specific prior written permission. |
mridup | 0:dc55f40eb04f | 23 | * |
mridup | 0:dc55f40eb04f | 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mridup | 0:dc55f40eb04f | 25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mridup | 0:dc55f40eb04f | 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
mridup | 0:dc55f40eb04f | 27 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
mridup | 0:dc55f40eb04f | 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mridup | 0:dc55f40eb04f | 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
mridup | 0:dc55f40eb04f | 30 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mridup | 0:dc55f40eb04f | 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
mridup | 0:dc55f40eb04f | 32 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
mridup | 0:dc55f40eb04f | 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mridup | 0:dc55f40eb04f | 34 | * |
mridup | 0:dc55f40eb04f | 35 | ****************************************************************************** |
mridup | 0:dc55f40eb04f | 36 | */ |
mridup | 0:dc55f40eb04f | 37 | /* Includes ------------------------------------------------------------------*/ |
mridup | 0:dc55f40eb04f | 38 | |
mridup | 0:dc55f40eb04f | 39 | #include <stddef.h> |
mridup | 0:dc55f40eb04f | 40 | #include "wifi_module.h" |
mridup | 0:dc55f40eb04f | 41 | #include "ring_buffer.h" |
mridup | 0:dc55f40eb04f | 42 | #include "device.h" |
mridup | 0:dc55f40eb04f | 43 | #include "wait_api.h" |
mridup | 0:dc55f40eb04f | 44 | #include "stdio.h" |
mridup | 0:dc55f40eb04f | 45 | #include "string.h" |
mridup | 0:dc55f40eb04f | 46 | #include "wifi_driver.h" |
mridup | 0:dc55f40eb04f | 47 | |
mridup | 0:dc55f40eb04f | 48 | void Wifi_ticker(void) |
mridup | 0:dc55f40eb04f | 49 | { |
mridup | 0:dc55f40eb04f | 50 | Wifi_SysTick_Isr(); |
mridup | 0:dc55f40eb04f | 51 | } |
mridup | 0:dc55f40eb04f | 52 | |
mridup | 0:dc55f40eb04f | 53 | void Wifi_scheduler(void) |
mridup | 0:dc55f40eb04f | 54 | { |
mridup | 0:dc55f40eb04f | 55 | Wifi_TIM_Handler(); |
mridup | 0:dc55f40eb04f | 56 | } |
mridup | 0:dc55f40eb04f | 57 | |
mridup | 0:dc55f40eb04f | 58 | void Rx_irq_handler(void) |
mridup | 0:dc55f40eb04f | 59 | { |
mridup | 0:dc55f40eb04f | 60 | |
mridup | 0:dc55f40eb04f | 61 | uint8_t data_byte = (uint8_t)callSpwfSADevice_getChar(spwf_dev); |
mridup | 0:dc55f40eb04f | 62 | |
mridup | 0:dc55f40eb04f | 63 | status_flag.Uartx_Rx_Processing = WIFI_FALSE; |
mridup | 0:dc55f40eb04f | 64 | |
mridup | 0:dc55f40eb04f | 65 | Stop_Timer(); |
mridup | 0:dc55f40eb04f | 66 | __disable_irq(); |
mridup | 0:dc55f40eb04f | 67 | push_buffer(&big_buff, &data_byte); |
mridup | 0:dc55f40eb04f | 68 | __enable_irq(); |
mridup | 0:dc55f40eb04f | 69 | Start_Timer(); |
mridup | 0:dc55f40eb04f | 70 | |
mridup | 0:dc55f40eb04f | 71 | status_flag.Uartx_Rx_Processing = WIFI_TRUE; |
mridup | 0:dc55f40eb04f | 72 | |
mridup | 0:dc55f40eb04f | 73 | /*if(is_half_full(&big_buff)) |
mridup | 0:dc55f40eb04f | 74 | { |
mridup | 0:dc55f40eb04f | 75 | status_flag.resume_receive_data = WIFI_TRUE; |
mridup | 0:dc55f40eb04f | 76 | if(callSpwfSADevice_read_rts(spwf_dev)) |
mridup | 0:dc55f40eb04f | 77 | callSpwfSADevice_rts(spwf_dev, GPIO_PIN_SET); |
mridup | 0:dc55f40eb04f | 78 | //callSpwfSADevice_attach(0); |
mridup | 0:dc55f40eb04f | 79 | } else |
mridup | 0:dc55f40eb04f | 80 | { |
mridup | 0:dc55f40eb04f | 81 | if(status_flag.AT_Cmd_Processing == WIFI_FALSE) |
mridup | 0:dc55f40eb04f | 82 | { |
mridup | 0:dc55f40eb04f | 83 | status_flag.Uartx_Rx_Processing = WIFI_TRUE; |
mridup | 0:dc55f40eb04f | 84 | } |
mridup | 0:dc55f40eb04f | 85 | }*/ |
mridup | 0:dc55f40eb04f | 86 | } |
mridup | 0:dc55f40eb04f | 87 | |
mridup | 0:dc55f40eb04f | 88 | |
mridup | 0:dc55f40eb04f | 89 | /** |
mridup | 0:dc55f40eb04f | 90 | * @brief wifi_reset |
mridup | 0:dc55f40eb04f | 91 | * Reset WiFi module using PC12 gpio pin |
mridup | 0:dc55f40eb04f | 92 | * @param None |
mridup | 0:dc55f40eb04f | 93 | * @retval None |
mridup | 0:dc55f40eb04f | 94 | */ |
mridup | 0:dc55f40eb04f | 95 | void wifi_reset(void) |
mridup | 0:dc55f40eb04f | 96 | { |
mridup | 0:dc55f40eb04f | 97 | WiFi_Module_State = Process_Event; |
mridup | 0:dc55f40eb04f | 98 | WiFi_WIND_State.WiFiHWStarted = WIFI_FALSE; |
mridup | 0:dc55f40eb04f | 99 | wifi_connected = 0; //reset wifi_connected to get user callback |
mridup | 0:dc55f40eb04f | 100 | memset((void*)&WiFi_WIND_State,0x00,sizeof(WiFi_WIND_State)); /*reset the WIND State?*/ |
mridup | 0:dc55f40eb04f | 101 | |
mridup | 0:dc55f40eb04f | 102 | /* === RESET PIN - PC12 ===*/ |
mridup | 0:dc55f40eb04f | 103 | |
mridup | 0:dc55f40eb04f | 104 | callSpwfSADevice_reset(spwf_dev, 0); |
mridup | 1:bd9db471d47d | 105 | wait_ms(200); |
mridup | 0:dc55f40eb04f | 106 | |
mridup | 0:dc55f40eb04f | 107 | callSpwfSADevice_reset(spwf_dev, 1); |
mridup | 0:dc55f40eb04f | 108 | wait_ms(100); |
mridup | 0:dc55f40eb04f | 109 | |
mridup | 0:dc55f40eb04f | 110 | while(WiFi_WIND_State.WiFiHWStarted != WIFI_TRUE) |
mridup | 0:dc55f40eb04f | 111 | { |
mridup | 0:dc55f40eb04f | 112 | //nothing to do |
mridup | 0:dc55f40eb04f | 113 | __NOP(); |
mridup | 0:dc55f40eb04f | 114 | } |
mridup | 0:dc55f40eb04f | 115 | } |
mridup | 0:dc55f40eb04f | 116 | |
mridup | 0:dc55f40eb04f | 117 | |
mridup | 0:dc55f40eb04f | 118 | /** |
mridup | 0:dc55f40eb04f | 119 | * @brief PowerUp_WiFi_Module |
mridup | 0:dc55f40eb04f | 120 | * Power up Wi-Fi module,SET GPIO PA0 pin |
mridup | 0:dc55f40eb04f | 121 | * @param None |
mridup | 0:dc55f40eb04f | 122 | * @retval None |
mridup | 0:dc55f40eb04f | 123 | */ |
mridup | 0:dc55f40eb04f | 124 | void PowerUp_WiFi_Module(void) |
mridup | 0:dc55f40eb04f | 125 | { |
mridup | 0:dc55f40eb04f | 126 | /* === SET PIN - PC12 ===*/ |
mridup | 0:dc55f40eb04f | 127 | callSpwfSADevice_reset(spwf_dev, 1); |
mridup | 0:dc55f40eb04f | 128 | wait_ms(50); |
mridup | 0:dc55f40eb04f | 129 | } |
mridup | 0:dc55f40eb04f | 130 | |
mridup | 0:dc55f40eb04f | 131 | /** |
mridup | 0:dc55f40eb04f | 132 | * @brief Receive_Data |
mridup | 0:dc55f40eb04f | 133 | * Receive data from UART port |
mridup | 0:dc55f40eb04f | 134 | * @param uint8_t number of bytes to be received |
mridup | 0:dc55f40eb04f | 135 | * @retval None |
mridup | 0:dc55f40eb04f | 136 | */ |
mridup | 0:dc55f40eb04f | 137 | void Receive_Data(void) |
mridup | 0:dc55f40eb04f | 138 | { |
mridup | 0:dc55f40eb04f | 139 | callSpwfSADevice_rts(spwf_dev, 0); |
mridup | 0:dc55f40eb04f | 140 | } |
mridup | 0:dc55f40eb04f | 141 | |
mridup | 0:dc55f40eb04f | 142 | |
mridup | 0:dc55f40eb04f | 143 | /** |
mridup | 0:dc55f40eb04f | 144 | * @brief USART_Transmit_AT_Cmd |
mridup | 0:dc55f40eb04f | 145 | * send AT cmd on UART port of wifi module. |
mridup | 0:dc55f40eb04f | 146 | * @param size size of the AT command |
mridup | 0:dc55f40eb04f | 147 | * @retval WiFi_Status_t : status of AT cmd |
mridup | 0:dc55f40eb04f | 148 | */ |
mridup | 0:dc55f40eb04f | 149 | WiFi_Status_t USART_Transmit_AT_Cmd(uint16_t size) |
mridup | 0:dc55f40eb04f | 150 | { |
mridup | 0:dc55f40eb04f | 151 | //Check for Hardware Started |
mridup | 0:dc55f40eb04f | 152 | if(status_flag.WiFi_Enabled == WIFI_FALSE) |
mridup | 0:dc55f40eb04f | 153 | return WiFi_NOT_READY; |
mridup | 0:dc55f40eb04f | 154 | //Check for Deep-Sleep or Standby Mode, return error if true |
mridup | 0:dc55f40eb04f | 155 | if (status_flag.Standby_Enabled == WIFI_TRUE || status_flag.Deep_Sleep_Enabled == WIFI_TRUE) |
mridup | 0:dc55f40eb04f | 156 | return WiFi_IN_LOW_POWER_ERROR; |
mridup | 0:dc55f40eb04f | 157 | |
mridup | 0:dc55f40eb04f | 158 | status_flag.AT_Cmd_Processing = WIFI_TRUE;//Stop Any Rx between the TX call |
mridup | 0:dc55f40eb04f | 159 | |
mridup | 0:dc55f40eb04f | 160 | if (size == 0) |
mridup | 0:dc55f40eb04f | 161 | { |
mridup | 0:dc55f40eb04f | 162 | return WiFi_UNHANDLED_IND_ERROR; |
mridup | 0:dc55f40eb04f | 163 | } |
mridup | 0:dc55f40eb04f | 164 | |
mridup | 0:dc55f40eb04f | 165 | #if defined(USART3_INT_MODE) |
mridup | 0:dc55f40eb04f | 166 | if(HAL_UART_Transmit_IT(&UartWiFiHandle, (uint8_t *)WiFi_AT_Cmd_Buff, size)!= HAL_OK) |
mridup | 0:dc55f40eb04f | 167 | { |
mridup | 0:dc55f40eb04f | 168 | Error_Handler(); |
mridup | 0:dc55f40eb04f | 169 | return WiFi_HAL_UART_ERROR; |
mridup | 0:dc55f40eb04f | 170 | } |
mridup | 0:dc55f40eb04f | 171 | while (UartReady != SET); |
mridup | 0:dc55f40eb04f | 172 | UartReady = RESET; |
mridup | 0:dc55f40eb04f | 173 | |
mridup | 0:dc55f40eb04f | 174 | #elif defined(USART3_POLLING_MODE) |
mridup | 0:dc55f40eb04f | 175 | |
mridup | 0:dc55f40eb04f | 176 | callSpwfSADevice_write(spwf_dev, (const char *) WiFi_AT_Cmd_Buff, size); |
mridup | 0:dc55f40eb04f | 177 | |
mridup | 0:dc55f40eb04f | 178 | #else |
mridup | 0:dc55f40eb04f | 179 | #error "Please select USART mode in your application (in wifi_module.h file)" |
mridup | 0:dc55f40eb04f | 180 | #endif |
mridup | 0:dc55f40eb04f | 181 | |
mridup | 0:dc55f40eb04f | 182 | status_flag.AT_Cmd_Processing = WIFI_FALSE;//Re-enable Rx for UART |
mridup | 0:dc55f40eb04f | 183 | if(status_flag.Uartx_Rx_Processing == WIFI_FALSE) |
mridup | 0:dc55f40eb04f | 184 | Receive_Data();//Start receiving Rx from the UART again, if and only if it was stopped in the previous Uartx_Rx_Handler |
mridup | 0:dc55f40eb04f | 185 | return WiFi_MODULE_SUCCESS; |
mridup | 0:dc55f40eb04f | 186 | } |
mridup | 0:dc55f40eb04f | 187 | |
mridup | 6:e7a3fca2df10 | 188 | /** |
mridup | 6:e7a3fca2df10 | 189 | * @brief wifi_socket_server_write |
mridup | 6:e7a3fca2df10 | 190 | * Write to a Server socket |
mridup | 6:e7a3fca2df10 | 191 | * @param None |
mridup | 6:e7a3fca2df10 | 192 | * @retval WiFi_Status_t : return status of server socket request |
mridup | 6:e7a3fca2df10 | 193 | */ |
mridup | 6:e7a3fca2df10 | 194 | WiFi_Status_t wifi_socket_server_write(uint16_t DataLength,char * pData) |
mridup | 6:e7a3fca2df10 | 195 | { |
mridup | 6:e7a3fca2df10 | 196 | WiFi_Status_t status = WiFi_MODULE_SUCCESS; |
mridup | 6:e7a3fca2df10 | 197 | /*Can only write if there is a client connected*/ |
mridup | 6:e7a3fca2df10 | 198 | if(!wifi_client_connected) |
mridup | 6:e7a3fca2df10 | 199 | { |
mridup | 6:e7a3fca2df10 | 200 | return WiFi_NOT_READY; |
mridup | 6:e7a3fca2df10 | 201 | } |
mridup | 6:e7a3fca2df10 | 202 | __disable_irq(); |
mridup | 6:e7a3fca2df10 | 203 | |
mridup | 6:e7a3fca2df10 | 204 | status_flag.do_not_reset_push_WIFI_event = WIFI_TRUE; |
mridup | 6:e7a3fca2df10 | 205 | status_flag.prevent_push_WIFI_event = WIFI_TRUE; |
mridup | 6:e7a3fca2df10 | 206 | __enable_irq(); |
mridup | 6:e7a3fca2df10 | 207 | |
mridup | 6:e7a3fca2df10 | 208 | while(status_flag.sock_read_ongoing || WIND64_count!= 0)//wait till any pending data is read |
mridup | 6:e7a3fca2df10 | 209 | { |
mridup | 6:e7a3fca2df10 | 210 | asm("NOP"); |
mridup | 6:e7a3fca2df10 | 211 | } |
mridup | 6:e7a3fca2df10 | 212 | |
mridup | 6:e7a3fca2df10 | 213 | wait_for_command_mode(); |
mridup | 6:e7a3fca2df10 | 214 | |
mridup | 6:e7a3fca2df10 | 215 | /*to make sure that by default the mode is not switched to command mode from data mode*/ |
mridup | 6:e7a3fca2df10 | 216 | status_flag.switch_by_default_to_command_mode = WIFI_FALSE; |
mridup | 6:e7a3fca2df10 | 217 | |
mridup | 6:e7a3fca2df10 | 218 | /*Switch to Data Mode first*/ |
mridup | 6:e7a3fca2df10 | 219 | if(!status_flag.data_mode) |
mridup | 6:e7a3fca2df10 | 220 | { |
mridup | 6:e7a3fca2df10 | 221 | WiFi_switch_to_data_mode();//switch by default |
mridup | 6:e7a3fca2df10 | 222 | while(!status_flag.data_mode) |
mridup | 6:e7a3fca2df10 | 223 | { |
mridup | 6:e7a3fca2df10 | 224 | //Wait till data_mode is active |
mridup | 6:e7a3fca2df10 | 225 | asm("NOP"); |
mridup | 6:e7a3fca2df10 | 226 | } |
mridup | 6:e7a3fca2df10 | 227 | } |
mridup | 6:e7a3fca2df10 | 228 | |
mridup | 6:e7a3fca2df10 | 229 | callSpwfSADevice_write(spwf_dev, (const char *) pData, DataLength); |
mridup | 6:e7a3fca2df10 | 230 | |
mridup | 6:e7a3fca2df10 | 231 | /*Write the data on the uart*/ |
mridup | 6:e7a3fca2df10 | 232 | /*if(HAL_UART_Transmit(&UartWiFiHandle, (uint8_t *)pData, DataLength,1000)!= HAL_OK) |
mridup | 6:e7a3fca2df10 | 233 | { |
mridup | 6:e7a3fca2df10 | 234 | Error_Handler(); |
mridup | 6:e7a3fca2df10 | 235 | return WiFi_HAL_UART_ERROR; |
mridup | 6:e7a3fca2df10 | 236 | }*/ |
mridup | 6:e7a3fca2df10 | 237 | //HAL_Delay(100);//Wait for tx before switching back to command mode |
mridup | 6:e7a3fca2df10 | 238 | |
mridup | 6:e7a3fca2df10 | 239 | /*Switch back to Command Mode*/ |
mridup | 6:e7a3fca2df10 | 240 | if(!status_flag.command_mode) |
mridup | 6:e7a3fca2df10 | 241 | { |
mridup | 6:e7a3fca2df10 | 242 | WiFi_switch_to_command_mode();//switch by default |
mridup | 6:e7a3fca2df10 | 243 | while(!status_flag.command_mode) |
mridup | 6:e7a3fca2df10 | 244 | { |
mridup | 6:e7a3fca2df10 | 245 | //Wait till command_mode is active |
mridup | 6:e7a3fca2df10 | 246 | asm("NOP"); |
mridup | 6:e7a3fca2df10 | 247 | } |
mridup | 6:e7a3fca2df10 | 248 | } |
mridup | 6:e7a3fca2df10 | 249 | |
mridup | 6:e7a3fca2df10 | 250 | status_flag.switch_by_default_to_command_mode = WIFI_TRUE; /*back to default behaviour*/ |
mridup | 6:e7a3fca2df10 | 251 | |
mridup | 6:e7a3fca2df10 | 252 | __disable_irq(); |
mridup | 6:e7a3fca2df10 | 253 | status_flag.prevent_push_WIFI_event = WIFI_FALSE; |
mridup | 6:e7a3fca2df10 | 254 | status_flag.do_not_reset_push_WIFI_event = WIFI_FALSE; |
mridup | 6:e7a3fca2df10 | 255 | __enable_irq(); |
mridup | 6:e7a3fca2df10 | 256 | |
mridup | 6:e7a3fca2df10 | 257 | return status; |
mridup | 6:e7a3fca2df10 | 258 | } |