D D / es_wifi

Dependents:   DISCO_L475VG_IOT01-Telegram-BOT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers es_wifi_io.c Source File

es_wifi_io.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    es_wifi_io.c
00004   * @author  MCD Application Team
00005   * @version V1.1.0
00006   * @date    04-September-2017
00007   * @brief   This file implments the IO operations to deal with the es-wifi
00008   *          module. It mainly Inits and Deinits the SPI interface. Send and
00009   *          receive data over it.
00010   ******************************************************************************
00011   * @attention
00012   *
00013   * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics International N.V. 
00014   * All rights reserved.</center></h2>
00015   *
00016   * Redistribution and use in source and binary forms, with or without 
00017   * modification, are permitted, provided that the following conditions are met:
00018   *
00019   * 1. Redistribution of source code must retain the above copyright notice, 
00020   *    this list of conditions and the following disclaimer.
00021   * 2. Redistributions in binary form must reproduce the above copyright notice,
00022   *    this list of conditions and the following disclaimer in the documentation
00023   *    and/or other materials provided with the distribution.
00024   * 3. Neither the name of STMicroelectronics nor the names of other 
00025   *    contributors to this software may be used to endorse or promote products 
00026   *    derived from this software without specific written permission.
00027   * 4. This software, including modifications and/or derivative works of this 
00028   *    software, must execute solely and exclusively on microcontroller or
00029   *    microprocessor devices manufactured by or for STMicroelectronics.
00030   * 5. Redistribution and use of this software other than as permitted under 
00031   *    this license is void and will automatically terminate your rights under 
00032   *    this license. 
00033   *
00034   * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS" 
00035   * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT 
00036   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
00037   * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
00038   * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT 
00039   * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00040   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00041   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
00042   * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
00043   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
00044   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00045   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00046   *
00047   ******************************************************************************
00048   */
00049 
00050 /* Includes ------------------------------------------------------------------*/
00051 #include "es_wifi_io.h"
00052 #include <string.h>
00053 
00054 /* Private define ------------------------------------------------------------*/
00055 #define MIN(a, b)  ((a) < (b) ? (a) : (b))
00056 /* Private typedef -----------------------------------------------------------*/
00057 /* Private macro -------------------------------------------------------------*/
00058 /* Private variables ---------------------------------------------------------*/
00059 SPI_HandleTypeDef hspi;
00060 
00061 /* Private function prototypes -----------------------------------------------*/
00062 
00063 /* Private functions ---------------------------------------------------------*/
00064 /*******************************************************************************
00065                        COM Driver Interface (SPI)
00066 *******************************************************************************/
00067 /**
00068   * @brief  Initialize SPI MSP
00069   * @param  hspi: SPI handle
00070   * @retval None
00071   */
00072 void SPI_WIFI_MspInit(SPI_HandleTypeDef* hspi)
00073 {
00074   
00075   GPIO_InitTypeDef GPIO_Init;
00076   
00077   __HAL_RCC_SPI3_CLK_ENABLE();
00078 
00079     __HAL_RCC_GPIOB_CLK_ENABLE();
00080   __HAL_RCC_GPIOC_CLK_ENABLE();
00081   __HAL_RCC_GPIOE_CLK_ENABLE();
00082     
00083   /* configure Wake up pin */
00084   HAL_GPIO_WritePin(GPIOB,GPIO_PIN_13, GPIO_PIN_RESET ); 
00085   GPIO_Init.Pin       = GPIO_PIN_13;
00086   GPIO_Init.Mode      = GPIO_MODE_OUTPUT_PP;
00087   GPIO_Init.Pull      = GPIO_NOPULL;
00088   GPIO_Init.Speed     = GPIO_SPEED_FREQ_LOW;
00089   HAL_GPIO_Init(GPIOB, &GPIO_Init );
00090 
00091   /* configure Data ready pin */
00092   GPIO_Init.Pin       = GPIO_PIN_1;
00093   GPIO_Init.Mode      = GPIO_MODE_IT_RISING;
00094   GPIO_Init.Pull      = GPIO_NOPULL;
00095   GPIO_Init.Speed     = GPIO_SPEED_FREQ_LOW;
00096   HAL_GPIO_Init(GPIOE, &GPIO_Init );
00097 
00098   /* configure Reset pin */
00099   GPIO_Init.Pin       = GPIO_PIN_8;
00100   GPIO_Init.Mode      = GPIO_MODE_OUTPUT_PP;
00101   GPIO_Init.Pull      = GPIO_NOPULL;
00102   GPIO_Init.Speed     = GPIO_SPEED_FREQ_LOW;
00103   GPIO_Init.Alternate = 0;
00104   HAL_GPIO_Init(GPIOE, &GPIO_Init );
00105   
00106   /* configure SPI NSS pin pin */
00107   HAL_GPIO_WritePin( GPIOE , GPIO_PIN_0, GPIO_PIN_SET ); 
00108   GPIO_Init.Pin       =  GPIO_PIN_0;
00109   GPIO_Init.Mode      = GPIO_MODE_OUTPUT_PP;
00110   GPIO_Init.Pull      = GPIO_NOPULL;
00111   GPIO_Init.Speed     = GPIO_SPEED_FREQ_MEDIUM;
00112   HAL_GPIO_Init( GPIOE, &GPIO_Init );
00113   
00114   /* configure SPI CLK pin */
00115   GPIO_Init.Pin       =  GPIO_PIN_10;
00116   GPIO_Init.Mode      = GPIO_MODE_AF_PP;
00117   GPIO_Init.Pull      = GPIO_NOPULL;
00118   GPIO_Init.Speed     = GPIO_SPEED_FREQ_MEDIUM;
00119   GPIO_Init.Alternate = GPIO_AF6_SPI3;
00120   HAL_GPIO_Init(GPIOC, &GPIO_Init );
00121   
00122   /* configure SPI MOSI pin */
00123   GPIO_Init.Pin       = GPIO_PIN_12;
00124   GPIO_Init.Mode      = GPIO_MODE_AF_PP;
00125   GPIO_Init.Pull      = GPIO_NOPULL;
00126   GPIO_Init.Speed     = GPIO_SPEED_FREQ_MEDIUM;
00127   GPIO_Init.Alternate = GPIO_AF6_SPI3;
00128   HAL_GPIO_Init( GPIOC, &GPIO_Init );
00129   
00130   /* configure SPI MISO pin */
00131   GPIO_Init.Pin       = GPIO_PIN_11;
00132   GPIO_Init.Mode      = GPIO_MODE_AF_PP;
00133   GPIO_Init.Pull      = GPIO_PULLUP;
00134   GPIO_Init.Speed     = GPIO_SPEED_FREQ_MEDIUM;
00135   GPIO_Init.Alternate = GPIO_AF6_SPI3;
00136   HAL_GPIO_Init(GPIOC, &GPIO_Init );
00137 }
00138 
00139 /**
00140   * @brief  Initialize the SPI3
00141   * @param  None
00142   * @retval None
00143   */
00144 int8_t SPI_WIFI_Init(void)
00145 {
00146   uint32_t tickstart = HAL_GetTick();
00147   uint8_t Prompt[6];
00148   uint8_t count = 0;
00149   HAL_StatusTypeDef  Status;
00150   
00151   hspi.Instance               = SPI3;
00152   SPI_WIFI_MspInit(&hspi);
00153   
00154   hspi.Init.Mode              = SPI_MODE_MASTER;
00155   hspi.Init.Direction         = SPI_DIRECTION_2LINES;
00156   hspi.Init.DataSize          = SPI_DATASIZE_16BIT;
00157   hspi.Init.CLKPolarity       = SPI_POLARITY_LOW;
00158   hspi.Init.CLKPhase          = SPI_PHASE_1EDGE;
00159   hspi.Init.NSS               = SPI_NSS_SOFT;
00160   hspi.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8; /* 80/8= 10MHz (Inventek WIFI module supportes up to 20MHz)*/
00161   hspi.Init.FirstBit          = SPI_FIRSTBIT_MSB;
00162   hspi.Init.TIMode            = SPI_TIMODE_DISABLE;
00163   hspi.Init.CRCCalculation    = SPI_CRCCALCULATION_DISABLE;
00164   hspi.Init.CRCPolynomial     = 0;
00165   
00166   if(HAL_SPI_Init( &hspi ) != HAL_OK)
00167   {
00168     return -1;
00169   }
00170   
00171   WIFI_RESET_MODULE();
00172   
00173   WIFI_ENABLE_NSS(); 
00174   
00175   while (WIFI_IS_CMDDATA_READY())
00176   {
00177     Status = HAL_SPI_Receive(&hspi , &Prompt[count], 1, 0xFFFF);  
00178     count += 2;
00179     if(((HAL_GetTick() - tickstart ) > 0xFFFF) || (Status != HAL_OK))
00180     {
00181       WIFI_DISABLE_NSS(); 
00182       return -1;
00183     }    
00184   }
00185   
00186   if((Prompt[0] != 0x15) ||(Prompt[1] != 0x15) ||(Prompt[2] != '\r')||
00187        (Prompt[3] != '\n') ||(Prompt[4] != '>') ||(Prompt[5] != ' '))
00188   {
00189     WIFI_DISABLE_NSS(); 
00190     return -1;
00191   }    
00192    
00193   WIFI_DISABLE_NSS(); 
00194   return 0;
00195 }
00196 
00197 /**
00198   * @brief  DeInitialize the SPI
00199   * @param  None
00200   * @retval None
00201   */
00202 int8_t SPI_WIFI_DeInit(void)
00203 {
00204   HAL_SPI_DeInit( &hspi );
00205   return 0;
00206 }
00207 
00208 /**
00209   * @brief  Receive wifi Data from SPI
00210   * @param  pdata : pointer to data
00211   * @param  len : Data length
00212   * @param  timeout : send timeout in mS
00213   * @retval Length of received data (payload)
00214   */
00215 int16_t SPI_WIFI_ReceiveData(uint8_t *pData, uint16_t len, uint32_t timeout)
00216 {
00217   uint32_t tickstart = HAL_GetTick();
00218   int16_t length = 0;
00219   uint8_t tmp[2];
00220 
00221   HAL_SPIEx_FlushRxFifo(&hspi);
00222   
00223  
00224   WIFI_DISABLE_NSS(); 
00225   
00226   while (!WIFI_IS_CMDDATA_READY())
00227   {
00228     if((HAL_GetTick() - tickstart ) > timeout)
00229     {
00230       return -1;
00231     }
00232   }
00233   
00234   WIFI_ENABLE_NSS(); 
00235   
00236   while (WIFI_IS_CMDDATA_READY())
00237   {
00238     if((length < len) || (!len))
00239     {
00240       HAL_SPI_Receive(&hspi, tmp, 1, timeout) ;
00241       /* let some time to hardware to change CMDDATA signal */
00242       if(tmp[1] == 0x15)
00243       {
00244        SPI_WIFI_Delay(1);
00245       }  
00246       /*This the last data */
00247       if(!WIFI_IS_CMDDATA_READY())
00248       {
00249         if(tmp[1] == 0x15)
00250         {
00251           if ((tmp[0] != 0x15))
00252           {
00253             pData[0] = tmp[0];
00254             length++;
00255           }
00256           break;
00257         }     
00258       }
00259       
00260       pData[0] = tmp[0];
00261       pData[1] = tmp[1];
00262       length += 2;
00263       pData  += 2;
00264       
00265       if((HAL_GetTick() - tickstart ) > timeout)
00266       {
00267         WIFI_DISABLE_NSS(); 
00268         return -1;
00269       }
00270     }
00271     else
00272     {
00273       break;
00274     }
00275   }
00276   
00277   WIFI_DISABLE_NSS(); 
00278   return length;
00279 }
00280 /**
00281   * @brief  Send wifi Data thru SPI
00282   * @param  pdata : pointer to data
00283   * @param  len : Data length
00284   * @param  timeout : send timeout in mS
00285   * @retval Length of sent data
00286   */
00287 int16_t SPI_WIFI_SendData( uint8_t *pdata,  uint16_t len, uint32_t timeout)
00288 {
00289   uint32_t tickstart = HAL_GetTick();
00290   uint8_t Padding[2];
00291   
00292   while (!WIFI_IS_CMDDATA_READY())
00293   {
00294     if((HAL_GetTick() - tickstart ) > timeout)
00295     {
00296       WIFI_DISABLE_NSS();       
00297       return -1;
00298     }
00299   }
00300   
00301   WIFI_ENABLE_NSS(); 
00302   
00303   if (len > 1)
00304   {
00305     if( HAL_SPI_Transmit(&hspi, (uint8_t *)pdata , len/2, timeout) != HAL_OK)
00306     {
00307       WIFI_DISABLE_NSS(); 
00308       return -1;
00309     }
00310   }
00311   
00312   if ( len & 1)
00313   {
00314     Padding[0] = pdata[len-1];
00315     Padding[1] = '\n';
00316     
00317     if( HAL_SPI_Transmit(&hspi, Padding, 1, timeout) != HAL_OK)
00318     {
00319       WIFI_DISABLE_NSS();       
00320       return -1;
00321     }
00322   }
00323   
00324   return len;
00325 }
00326 
00327 /**
00328   * @brief  Delay
00329   * @param  Delay in ms
00330   * @retval None
00331   */
00332 void SPI_WIFI_Delay(uint32_t Delay)
00333 {
00334   HAL_Delay(Delay);
00335 }
00336 /**
00337   * @}
00338   */ 
00339 
00340 /**
00341   * @}
00342   */ 
00343 
00344 /**
00345   * @}
00346   */
00347 
00348 /**
00349   * @}
00350   */ 
00351 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/