ST / X_NUCLEO_IDW01M1

Dependents:   SpwfInterface_NSAPI_Tests HelloWorld_IDW01M1

Fork of X_NUCLEO_IDW01M1 by ST Expansion SW Team

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers wifi_driver.c Source File

wifi_driver.c

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    wifi_driver.c
00004  * @author  Central LAB
00005  * @version V2.0.0
00006  * @date    01-March-2016
00007  * @brief   Enable Wi-Fi functionality using AT cmd set
00008  ******************************************************************************
00009  * @attention
00010  *
00011  * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
00012  *
00013  * Redistribution and use in source and binary forms, with or without modification,
00014  * are permitted provided that the following conditions are met:
00015  *   1. Redistributions of source code must retain the above copyright notice,
00016  *      this list of conditions and the following disclaimer.
00017  *   2. Redistributions in binary form must reproduce the above copyright notice,
00018  *      this list of conditions and the following disclaimer in the documentation
00019  *      and/or other materials provided with the distribution.
00020  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00021  *      may be used to endorse or promote products derived from this software
00022  *      without specific prior written permission.
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00028  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00029  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00030  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00032  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00033  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  ******************************************************************************
00036  */
00037 /* Includes ------------------------------------------------------------------*/
00038 
00039 #include <stddef.h>
00040 #include "wifi_module.h"
00041 #include "ring_buffer.h"
00042 #include "device.h"
00043 #include "wait_api.h"
00044 #include "stdio.h"
00045 #include "string.h"
00046 #include "wifi_driver.h"
00047 
00048 /**
00049 * @brief  Wi-Fi System Tick IRQ handler
00050 *         retrieves byte(s) from the ring buffer
00051 * @param  None
00052 * @retval None
00053 */
00054 void Wifi_ticker(void)
00055 {
00056   Wifi_SysTick_Isr();
00057 }
00058 
00059 /**
00060 * @brief  Wi-Fi Scheduler handler
00061 *         Fetches events from event buffer/makes callbacks to User
00062 * @param  None
00063 * @retval None
00064 */
00065 void Wifi_scheduler(void)
00066 { 
00067   Wifi_TIM_Handler();
00068 }
00069 
00070 /**
00071 * @brief  UART RX IRQ handler
00072 *         stores a byte to the ring buffer
00073 * @param  None
00074 * @retval None
00075 */
00076 void Rx_irq_handler(void)
00077 {
00078 
00079           uint8_t data_byte = (uint8_t)callSpwfSADevice_getChar(spwf_dev);
00080 
00081           status_flag.Uartx_Rx_Processing = WIFI_FALSE;
00082           
00083           Stop_Timer();
00084           __disable_irq();
00085           push_buffer(&big_buff, &data_byte);
00086           __enable_irq();
00087           Start_Timer();
00088           
00089           status_flag.Uartx_Rx_Processing = WIFI_TRUE;
00090           
00091           /*if(is_half_full(&big_buff))
00092           {
00093             status_flag.resume_receive_data = WIFI_TRUE;
00094             if(callSpwfSADevice_read_rts(spwf_dev))
00095             callSpwfSADevice_rts(spwf_dev, GPIO_PIN_SET);
00096             //callSpwfSADevice_attach(0);
00097           } else
00098             {
00099               if(status_flag.AT_Cmd_Processing == WIFI_FALSE)
00100               {
00101                 status_flag.Uartx_Rx_Processing = WIFI_TRUE;
00102               }  
00103             }*/
00104 } 
00105 
00106 
00107 /**
00108 * @brief  wifi_reset
00109 *         Reset WiFi module using PC12 gpio pin
00110 * @param  None
00111 * @retval None
00112 */
00113 void wifi_reset(void)
00114 {  
00115   WiFi_Module_State = Process_Event; 
00116   WiFi_WIND_State.WiFiHWStarted = WIFI_FALSE;
00117   wifi_connected = 0; //reset wifi_connected to get user callback
00118   memset((void*)&WiFi_WIND_State,0x00,sizeof(WiFi_WIND_State)); /*reset the WIND State?*/
00119 
00120   /* ===   RESET PIN - PC12   ===*/
00121   
00122   callSpwfSADevice_reset(spwf_dev, 0); 
00123   wait_ms(200);
00124 
00125   callSpwfSADevice_reset(spwf_dev, 1); 
00126   wait_ms(100);
00127   
00128   while(WiFi_WIND_State.WiFiHWStarted != WIFI_TRUE) 
00129   {
00130         //nothing to do
00131         __NOP();
00132   }
00133 }
00134 
00135 
00136 /**
00137 * @brief  PowerUp_WiFi_Module
00138 *         Power up Wi-Fi module,SET GPIO PA0 pin 
00139 * @param  None
00140 * @retval None
00141 */
00142 void PowerUp_WiFi_Module(void)
00143 {
00144   /* ===   SET PIN - PC12   ===*/
00145   callSpwfSADevice_reset(spwf_dev, 1); 
00146   wait_ms(50);
00147 }
00148 
00149 /**
00150 * @brief  Receive_Data
00151 *         Receive data from UART port
00152 * @param  uint8_t number of bytes to be received
00153 * @retval None
00154 */
00155 void Receive_Data(void)
00156 {
00157   callSpwfSADevice_rts(spwf_dev, 0);
00158 }
00159 
00160 
00161 /**
00162 * @brief  USART_Transmit_AT_Cmd
00163 *         send AT cmd on UART port of wifi module.
00164 * @param  size size of the AT command
00165 * @retval WiFi_Status_t : status of AT cmd
00166 */
00167 WiFi_Status_t USART_Transmit_AT_Cmd(uint16_t size)
00168 {
00169   //Check for Hardware Started
00170   if(status_flag.WiFi_Enabled == WIFI_FALSE) 
00171     return WiFi_NOT_READY;
00172   //Check for Deep-Sleep or Standby Mode, return error if true
00173   if (status_flag.Standby_Enabled == WIFI_TRUE || status_flag.Deep_Sleep_Enabled == WIFI_TRUE)
00174     return WiFi_IN_LOW_POWER_ERROR;
00175 
00176   status_flag.AT_Cmd_Processing = WIFI_TRUE;//Stop Any Rx between the TX call
00177 
00178   if (size == 0)
00179     {
00180         return WiFi_UNHANDLED_IND_ERROR;
00181     }
00182 
00183 #if defined(USART3_INT_MODE)
00184   if(HAL_UART_Transmit_IT(&UartWiFiHandle, (uint8_t *)WiFi_AT_Cmd_Buff, size)!= HAL_OK)
00185   {
00186     Error_Handler();
00187     return WiFi_HAL_UART_ERROR;
00188   }
00189   while (UartReady != SET);
00190   UartReady = RESET; 
00191 
00192 #elif defined(USART3_POLLING_MODE)
00193 
00194   write_size = callSpwfSADevice_write(spwf_dev, (const char *) WiFi_AT_Cmd_Buff, size);
00195 
00196 #else
00197  #error "Please select USART mode in your application (in wifi_module.h file)"
00198 #endif
00199  
00200   status_flag.AT_Cmd_Processing = WIFI_FALSE;//Re-enable Rx for UART
00201   if(status_flag.Uartx_Rx_Processing == WIFI_FALSE)
00202     Receive_Data();//Start receiving Rx from the UART again, if and only if it was stopped in the previous Uartx_Rx_Handler
00203   return WiFi_MODULE_SUCCESS;
00204 }
00205 
00206 /**
00207 * @brief  wifi_socket_server_write
00208 *         Write to a Server socket
00209 * @param  None
00210 * @retval WiFi_Status_t : return status of server socket request
00211 */
00212 int wifi_socket_server_write(uint16_t DataLength,char * pData) 
00213 {
00214     WiFi_Status_t status = WiFi_MODULE_SUCCESS;
00215     /*Can only write if there is a client connected*/
00216     if(!wifi_client_connected)
00217     {
00218     return WiFi_NOT_READY;
00219     }
00220      __disable_irq();
00221     
00222     status_flag.do_not_reset_push_WIFI_event = WIFI_TRUE;
00223     status_flag.prevent_push_WIFI_event = WIFI_TRUE;
00224     __enable_irq();
00225     
00226     while(status_flag.sock_read_ongoing || WIND64_count!= 0)//wait till any pending data is read
00227     {
00228         asm("NOP");
00229     }
00230     
00231     wait_for_command_mode();
00232     
00233     /*to make sure that by default the mode is not switched to command mode from data mode*/
00234     status_flag.switch_by_default_to_command_mode = WIFI_FALSE;
00235     
00236     /*Switch to Data Mode first*/
00237     if(!status_flag.data_mode)
00238     {
00239     WiFi_switch_to_data_mode();//switch by default
00240     while(!status_flag.data_mode)
00241     {
00242       //Wait till data_mode is active
00243         asm("NOP");
00244     }
00245     }  
00246     
00247     write_size = callSpwfSADevice_write(spwf_dev, (const char *) pData, DataLength);
00248     
00249     /*Write the data on the uart*/
00250     /*if(HAL_UART_Transmit(&UartWiFiHandle, (uint8_t *)pData, DataLength,1000)!= HAL_OK)
00251     {
00252     Error_Handler();
00253     return WiFi_HAL_UART_ERROR;
00254     }*/
00255     //HAL_Delay(100);//Wait for tx before switching back to command mode
00256     
00257     /*Switch back to Command Mode*/
00258     if(!status_flag.command_mode)
00259     {
00260     WiFi_switch_to_command_mode();//switch by default
00261     while(!status_flag.command_mode)
00262     {
00263       //Wait till command_mode is active
00264         asm("NOP");
00265     }
00266     }
00267     
00268     status_flag.switch_by_default_to_command_mode = WIFI_TRUE;  /*back to default behaviour*/
00269     
00270     __disable_irq();
00271     status_flag.prevent_push_WIFI_event = WIFI_FALSE;
00272     status_flag.do_not_reset_push_WIFI_event = WIFI_FALSE;
00273     __enable_irq();
00274     
00275     if(status==WiFi_MODULE_SUCCESS)
00276         return write_size;
00277     else
00278         return -1;
00279     //return status;
00280 }