AT-AT Robot Ops / DISCO_L475VG_IOT01A_wifi-client-baseclient

Fork of DISCO_L475VG_IOT01A_wifi by ST

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.8.0
00006   * @date    21-April-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   __HAL_RCC_GPIOB_CLK_ENABLE();
00079   __HAL_RCC_GPIOC_CLK_ENABLE();
00080   __HAL_RCC_GPIOE_CLK_ENABLE();
00081   
00082   /* configure Wake up pin */
00083   HAL_GPIO_WritePin(GPIOB, GPIO_PIN_13, GPIO_PIN_RESET ); 
00084   GPIO_Init.Pin       = GPIO_PIN_13;
00085   GPIO_Init.Mode      = GPIO_MODE_OUTPUT_PP;
00086   GPIO_Init.Pull      = GPIO_NOPULL;
00087   GPIO_Init.Speed     = GPIO_SPEED_FREQ_LOW;
00088   HAL_GPIO_Init(GPIOB, &GPIO_Init );
00089 
00090   /* configure Data ready pin */
00091   GPIO_Init.Pin       = GPIO_PIN_1;
00092   GPIO_Init.Mode      = GPIO_MODE_IT_RISING;
00093   GPIO_Init.Pull      = GPIO_NOPULL;
00094   GPIO_Init.Speed     = GPIO_SPEED_FREQ_LOW;
00095   HAL_GPIO_Init(GPIOE, &GPIO_Init );
00096 
00097   /* configure Reset pin */
00098   GPIO_Init.Pin       = GPIO_PIN_8;
00099   GPIO_Init.Mode      = GPIO_MODE_OUTPUT_PP;
00100   GPIO_Init.Pull      = GPIO_NOPULL;
00101   GPIO_Init.Speed     = GPIO_SPEED_FREQ_LOW;
00102   GPIO_Init.Alternate = 0;
00103   HAL_GPIO_Init(GPIOE, &GPIO_Init );
00104   
00105   /* configure SPI NSS pin pin */
00106   HAL_GPIO_WritePin( GPIOE, GPIO_PIN_0, GPIO_PIN_SET ); 
00107   GPIO_Init.Pin       = GPIO_PIN_0;
00108   GPIO_Init.Mode      = GPIO_MODE_OUTPUT_PP;
00109   GPIO_Init.Pull      = GPIO_NOPULL;
00110   GPIO_Init.Speed     = GPIO_SPEED_FREQ_MEDIUM;
00111   HAL_GPIO_Init( GPIOE, &GPIO_Init );
00112   
00113   /* configure SPI CLK pin */
00114   GPIO_Init.Pin       = GPIO_PIN_10;
00115   GPIO_Init.Mode      = GPIO_MODE_AF_PP;
00116   GPIO_Init.Pull      = GPIO_NOPULL;
00117   GPIO_Init.Speed     = GPIO_SPEED_FREQ_MEDIUM;
00118   GPIO_Init.Alternate = GPIO_AF6_SPI3;
00119   HAL_GPIO_Init( GPIOC, &GPIO_Init );
00120   
00121   /* configure SPI MOSI pin */
00122   GPIO_Init.Pin       = GPIO_PIN_12;
00123   GPIO_Init.Mode      = GPIO_MODE_AF_PP;
00124   GPIO_Init.Pull      = GPIO_NOPULL;
00125   GPIO_Init.Speed     = GPIO_SPEED_FREQ_MEDIUM;
00126   GPIO_Init.Alternate = GPIO_AF6_SPI3;
00127   HAL_GPIO_Init( GPIOC, &GPIO_Init );
00128   
00129   /* configure SPI MISO pin */
00130   GPIO_Init.Pin       = GPIO_PIN_11;
00131   GPIO_Init.Mode      = GPIO_MODE_AF_PP;
00132   GPIO_Init.Pull      = GPIO_PULLUP;
00133   GPIO_Init.Speed     = GPIO_SPEED_FREQ_MEDIUM;
00134   GPIO_Init.Alternate = GPIO_AF6_SPI3;
00135   HAL_GPIO_Init( GPIOC, &GPIO_Init );
00136 }
00137 
00138 /**
00139   * @brief  Initialize the SPI3
00140   * @param  None
00141   * @retval None
00142   */
00143 int8_t SPI_WIFI_Init(void)
00144 {
00145   uint32_t tickstart = HAL_GetTick();
00146   uint8_t Prompt[6];
00147   uint8_t count = 0;
00148   HAL_StatusTypeDef  Status;
00149   
00150   hspi.Instance               = SPI3;
00151   SPI_WIFI_MspInit(&hspi);
00152   
00153   hspi.Init.Mode              = SPI_MODE_MASTER;
00154   hspi.Init.Direction         = SPI_DIRECTION_2LINES;
00155   hspi.Init.DataSize          = SPI_DATASIZE_16BIT;
00156   hspi.Init.CLKPolarity       = SPI_POLARITY_LOW;
00157   hspi.Init.CLKPhase          = SPI_PHASE_1EDGE;
00158   hspi.Init.NSS               = SPI_NSS_SOFT;
00159   hspi.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;  /* 80/8= 10MHz (Inventek WIFI module supportes up to 20MHz)*/
00160   hspi.Init.FirstBit          = SPI_FIRSTBIT_MSB;
00161   hspi.Init.TIMode            = SPI_TIMODE_DISABLE;
00162   hspi.Init.CRCCalculation    = SPI_CRCCALCULATION_DISABLE;
00163   hspi.Init.CRCPolynomial     = 0;
00164   
00165   if(HAL_SPI_Init( &hspi ) != HAL_OK)
00166   {
00167     return -1;
00168   }
00169   
00170   WIFI_RESET_MODULE();
00171   
00172   WIFI_ENABLE_NSS(); 
00173   
00174   while (WIFI_IS_CMDDATA_READY())
00175   {
00176     Status = HAL_SPI_Receive(&hspi , &Prompt[count], 1, 0xFFFF);  
00177     count += 2;
00178     if(((HAL_GetTick() - tickstart ) > 0xFFFF) || (Status != HAL_OK))
00179     {
00180       WIFI_DISABLE_NSS(); 
00181       return -1;
00182     }    
00183   }
00184   
00185   if((Prompt[0] != 0x15) ||(Prompt[1] != 0x15) ||(Prompt[2] != '\r')||
00186        (Prompt[3] != '\n') ||(Prompt[4] != '>') ||(Prompt[5] != ' '))
00187   {
00188     WIFI_DISABLE_NSS(); 
00189     return -1;
00190   }    
00191    
00192   WIFI_DISABLE_NSS(); 
00193   return 0;
00194 }
00195 
00196 /**
00197   * @brief  DeInitialize the SPI
00198   * @param  None
00199   * @retval None
00200   */
00201 int8_t SPI_WIFI_DeInit(void)
00202 {
00203   HAL_SPI_DeInit( &hspi );
00204   return 0;
00205 }
00206 
00207 /**
00208   * @brief  Receive wifi Data from SPI
00209   * @param  pdata : pointer to data
00210   * @param  len : Data length
00211   * @param  timeout : send timeout in mS
00212   * @retval Length of received data (payload)
00213   */
00214 int16_t SPI_WIFI_ReceiveData(uint8_t *pData, uint16_t len, uint32_t timeout)
00215 {
00216   uint32_t tickstart = HAL_GetTick();
00217   int16_t length = 0;
00218   uint8_t tmp[2];
00219   
00220   HAL_SPIEx_FlushRxFifo(&hspi);
00221   
00222   WIFI_DISABLE_NSS(); 
00223   
00224   while (!WIFI_IS_CMDDATA_READY())
00225   {
00226     if((HAL_GetTick() - tickstart ) > timeout)
00227     {
00228       return -1;
00229     }
00230   }
00231   
00232   WIFI_ENABLE_NSS(); 
00233   
00234   while (WIFI_IS_CMDDATA_READY())
00235   {
00236     if((length < len) || (!len))
00237     {
00238       HAL_SPI_Receive(&hspi, tmp, 1, timeout) ;    
00239       /* let some time to hardware to change CMDDATA signal */
00240       if(tmp[1] == 0x15)
00241       {
00242        SPI_WIFI_Delay(1);
00243       }
00244       /*This the last data */
00245       if(!WIFI_IS_CMDDATA_READY())
00246       {
00247         if(tmp[1] == 0x15)
00248         {
00249           pData[0] = tmp[0];
00250           length++;
00251           break;
00252         }     
00253       }
00254       
00255       pData[0] = tmp[0];
00256       pData[1] = tmp[1];
00257       length += 2;
00258       pData  += 2;
00259       
00260       if((HAL_GetTick() - tickstart ) > timeout)
00261       {
00262         WIFI_DISABLE_NSS(); 
00263         return -1;
00264       }
00265     }
00266     else
00267     {
00268       break;
00269     }
00270   }
00271   
00272   WIFI_DISABLE_NSS(); 
00273   return length;
00274 }
00275 /**
00276   * @brief  Send wifi Data thru SPI
00277   * @param  pdata : pointer to data
00278   * @param  len : Data length
00279   * @param  timeout : send timeout in mS
00280   * @retval Length of sent data
00281   */
00282 int16_t SPI_WIFI_SendData( uint8_t *pdata,  uint16_t len, uint32_t timeout)
00283 {
00284   uint32_t tickstart = HAL_GetTick();
00285   uint8_t Padding[2];
00286   
00287   while (!WIFI_IS_CMDDATA_READY())
00288   {
00289     if((HAL_GetTick() - tickstart ) > timeout)
00290     {
00291       WIFI_DISABLE_NSS();       
00292       return -1;
00293     }
00294   }
00295   
00296   WIFI_ENABLE_NSS(); 
00297   if (len > 1)
00298   {
00299    if( HAL_SPI_Transmit(&hspi, (uint8_t *)pdata , len/2, timeout) != HAL_OK)
00300    {
00301      WIFI_DISABLE_NSS(); 
00302      return -1;
00303    }
00304   }
00305   
00306   if ( len & 1)
00307   {
00308     Padding[0] = pdata[len-1];
00309     Padding[1] = '\n';
00310     
00311     if( HAL_SPI_Transmit(&hspi, Padding, 1, timeout) != HAL_OK)
00312     {
00313       WIFI_DISABLE_NSS();       
00314       return -1;
00315     }
00316   }
00317   
00318   return len;
00319 }
00320 
00321 /**
00322   * @brief  Delay
00323   * @param  Delay in ms
00324   * @retval None
00325   */
00326 void SPI_WIFI_Delay(uint32_t Delay)
00327 {
00328   HAL_Delay(Delay);
00329 }
00330 
00331 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/