ST Expansion SW Team / BSP

Dependents:   mbed-os-nfc04a1 Wiagro-Lanza34-XDot

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers x_nucleo_nfc04a1.cpp Source File

x_nucleo_nfc04a1.cpp

00001 /**
00002   ******************************************************************************
00003   * @file    x_nucleo_nfc04a1.c
00004   * @author  MMY Application Team
00005   * @version $Revision: 3351 $
00006   * @date    $Date: 2017-01-25 17:28:08 +0100 (Wed, 25 Jan 2017) $
00007   * @brief   This file provides nfc04a1 specific functions
00008   ******************************************************************************
00009   * @attention
00010   *
00011   * <h2><center>&copy; COPYRIGHT 2017 STMicroelectronics</center></h2>
00012   *
00013   * Licensed under ST MYLIBERTY SOFTWARE LICENSE AGREEMENT (the "License");
00014   * You may not use this file except in compliance with the License.
00015   * You may obtain a copy of the License at:
00016   *
00017   *        http://www.st.com/myliberty  
00018   *
00019   * Unless required by applicable law or agreed to in writing, software 
00020   * distributed under the License is distributed on an "AS IS" BASIS, 
00021   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
00022   * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY,
00023   * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
00024   * See the License for the specific language governing permissions and
00025   * limitations under the License.
00026   *
00027   ******************************************************************************
00028   */ 
00029 
00030 /* Includes ------------------------------------------------------------------*/
00031 #include "x_nucleo_nfc04a1.h"
00032 //#include "x_nucleo_nfc04a1_nfctag.h"
00033 //#include "mbed.h"
00034 //#include "PinNames.h"
00035 #include "time.h"
00036 
00037 /*
00038 #if define
00039 define
00040 #endif
00041 */
00042 
00043 
00044 /*Stream *_serial = NULL;
00045 #if defined(ARDUINO_SAM_DUE)
00046 TwoWire *_wire = &Wire1;
00047 #else
00048 TwoWire *_wire = &Wire;
00049 #endif
00050 * @addtogroup BSP
00051  * @{
00052  */
00053 
00054 /** @defgroup X_NUCLEO_NFC04A1
00055  * @{
00056  */
00057 /* Private typedef -----------------------------------------------------------*/
00058 /* Private defines -----------------------------------------------------------*/
00059 /* Private macros ------------------------------------------------------------*/
00060 /* Private variables ---------------------------------------------------------*/ 
00061 /* Global variables ----------------------------------------------------------*/
00062 /** @defgroup X_NUCLEO_NFC04A1_Global_Variables
00063  * @{
00064  */
00065 //uint8_t NFC04A1_Led[3] = {  1 , 2 , 3 };
00066 
00067 /**
00068  * @}
00069  */
00070 
00071 /* Private function prototypes -----------------------------------------------*/
00072 
00073 NFCTAG_StatusTypeDef ST25DV_IO_Init( I2C* mi2cChannel, DigitalOut *mLPD );
00074 NFCTAG_StatusTypeDef ST25DV_IO_MemWrite( const uint8_t * const pData, const uint8_t DevAddr, const uint16_t TarAddr, const uint16_t Size, I2C* mi2cChannel );
00075 NFCTAG_StatusTypeDef ST25DV_IO_MemRead( uint8_t * const pData, const uint8_t DevAddr, const uint16_t TarAddr, const uint16_t Size, I2C* mi2cChannel );
00076 NFCTAG_StatusTypeDef ST25DV_IO_Read( uint8_t * const pData, const uint8_t DevAddr, const uint16_t Size, I2C* mi2cChannel );
00077 NFCTAG_StatusTypeDef ST25DV_IO_IsDeviceReady( const uint8_t DevAddr, const uint32_t Trials, I2C* mi2cChannel);
00078 NFCTAG_StatusTypeDef NFCTAG_ConvertStatus(uint8_t ret);
00079 
00080 
00081 /* Functions Definition ------------------------------------------------------*/
00082 /** @defgroup X_NUCLEO_NFC04A1_Public_Functions
00083  * @{
00084  */
00085 
00086 
00087 
00088 /**
00089   * @brief  Toggles the selected LED
00090   * @param  led : Specifies the Led to be toggled
00091   * @retval None
00092   */
00093 void NFC04A1_LED_Toggle( DigitalOut* led)
00094 {
00095     *led = !(*led);
00096 }
00097 
00098 
00099 
00100 /**
00101   * @brief  This function get the GPIO value through GPIO
00102   * @param  None
00103   * @retval HAL GPIO pin status
00104   */
00105 DigitalOut NFC04A1_GPO_ReadPin( DigitalOut *mMISO )
00106 {
00107   return *mMISO;
00108 }
00109 
00110 /**
00111   * @brief  This function initialize the GPIO to manage the NFCTAG LPD pin
00112   * @param  None
00113   * @retval None
00114   */
00115 
00116 void NFC04A1_LPD_Init( DigitalOut *mLPD )
00117 {
00118     *mLPD = 0;
00119 }
00120 
00121 /**
00122   * @brief  This function get the GPIO value through GPIO
00123   * @param  None
00124   * @retval HAL GPIO pin status
00125   */
00126 uint8_t NFC04A1_LPD_ReadPin( DigitalOut *mLPD )
00127 {
00128   return mLPD -> read();
00129 }
00130 
00131 /**
00132   * @brief  This function get the GPIO value through GPIO
00133   * @param  None
00134   * @retval HAL GPIO pin status
00135   */
00136 void NFC04A1_LPD_WritePin( uint8_t LpdPinState, DigitalOut *mLPD )
00137 {
00138   mLPD -> write( LpdPinState );
00139 }
00140 
00141 /**
00142   * @brief  This function select the i2cChannel1 speed to communicate with NFCTAG
00143   * @param  i2cChannelspeedchoice Number from 0 to 5 to select i2cChannel speed
00144   * @param mi2cChannel : I2C channel
00145   * @retval HAL GPIO pin status
00146   */
00147 void NFC04A1_Selecti2cSpeed( uint8_t i2cspeedchoice, I2C* mi2cChannel)
00148 {
00149   
00150   switch( i2cspeedchoice )
00151   {
00152     case 0:
00153       
00154       mi2cChannel -> frequency(10000);
00155       break;
00156     
00157     case 1:
00158       
00159       mi2cChannel -> frequency(100000);
00160       break;
00161     
00162     case 2:
00163       
00164     mi2cChannel -> frequency(200000);
00165       break;
00166     
00167     case 3:
00168       
00169     mi2cChannel -> frequency(400000);
00170       break;
00171     
00172     case 4:
00173 
00174     mi2cChannel -> frequency(800000);
00175       break;
00176     
00177     case 5:
00178       
00179     mi2cChannel -> frequency(1000000);
00180       break;
00181     
00182     default:
00183       
00184     mi2cChannel -> frequency(1000000);
00185       break;
00186   }    
00187 
00188 }
00189 
00190 /**
00191  * @}
00192  */
00193 
00194 /** @defgroup X_NUCLEO_NFC04A1_Private_Functions
00195  * @{
00196  */
00197 /******************************** LINK EEPROM COMPONENT *****************************/
00198 
00199 /**
00200   * @brief  Initializes peripherals used by the i2cChannel NFCTAG driver
00201   * @param mi2cChannel : I2C channel
00202   * @param mLPD
00203   * @retval NFCTAG enum status
00204   */
00205 NFCTAG_StatusTypeDef ST25DV_IO_Init( I2C* mi2cChannel, DigitalOut *mLPD )
00206 {
00207 
00208   
00209   NFC04A1_LPD_Init( mLPD );
00210 
00211   NFC04A1_Selecti2cSpeed(3, mi2cChannel);
00212 
00213   return NFCTAG_OK;
00214 }
00215 
00216 /**
00217   * @brief  Write data, at specific address, through i2c to the ST25DV
00218   * @param  pData: pointer to the data to write
00219   * @param  DevAddr : Target device address
00220   * @param  TarAddr : i2c data memory address to write
00221   * @param  Size : Size in bytes of the value to be written
00222   * @param mi2cChannel : I2C channel
00223   * @retval NFCTAG enum status
00224   */
00225 NFCTAG_StatusTypeDef ST25DV_IO_MemWrite( const uint8_t * const pData, const uint8_t DevAddr, const uint16_t TarAddr, const uint16_t Size, I2C* mi2cChannel )
00226 {
00227 
00228   uint8_t ret = 4;
00229   
00230   uint8_t Addr = DevAddr;
00231 
00232 
00233   uint8_t buffer[2];
00234   buffer[0] = (uint8_t) (TarAddr>>8);
00235   buffer[1] = (uint8_t) (TarAddr&0xFF);
00236 
00237 
00238    char * pDataChar = (char*) pData;
00239 
00240 
00241   ret = mi2cChannel -> write(Addr, (const char*)buffer, 2 , true);
00242 
00243   // Address is not OK
00244     if(ret != 0)
00245         return NFCTAG_ConvertStatus(ret);
00246 
00247     ret = mi2cChannel -> write(Addr, pDataChar, Size, false);
00248 
00249   return NFCTAG_ConvertStatus(ret);
00250 }
00251 
00252 
00253 /*
00254   * @brief  Reads data at a specific address from the NFCTAG.
00255   * @param  pData: pointer to store read data
00256   * @param  DevAddr : Target device address
00257   * @param  TarAddr : i2c data memory address to read
00258   * @param  Size : Size in bytes of the value to be read
00259   * @param mi2cChannel : I2C channel
00260   * @retval NFCTAG enum status
00261   */
00262 
00263 NFCTAG_StatusTypeDef ST25DV_IO_MemRead( uint8_t * const pData, const uint8_t DevAddr, const uint16_t TarAddr, const uint16_t Size, I2C* mi2cChannel )
00264 {
00265 
00266     uint8_t ret = 4;
00267     uint8_t Addr = DevAddr;
00268 
00269     uint8_t buffer[2];
00270     buffer[0] = (uint8_t) (TarAddr>>8);
00271     buffer[1] = (uint8_t) (TarAddr&0xFF);
00272 
00273     ret = mi2cChannel -> write(Addr, (const char*)buffer , 2 , false);
00274 
00275     // Address is not OK
00276     if(ret != 0)
00277         return NFCTAG_ConvertStatus(ret);
00278 
00279     char * pDataChar = (char*) pData;
00280 
00281     ret = mi2cChannel -> read(DevAddr, pDataChar, Size, false );
00282 
00283     return NFCTAG_ConvertStatus(ret);
00284 }
00285 
00286 NFCTAG_StatusTypeDef NFCTAG_ConvertStatus(uint8_t ret) {
00287     if (ret == 0) {
00288         return NFCTAG_OK;
00289     } else if ((ret == 2) || (ret == 3)) {
00290         return NFCTAG_NACK;
00291     } else {
00292         return NFCTAG_ERROR;
00293     }
00294 }
00295 
00296 
00297 /**
00298   * @brief  Reads data at current address from the NFCTAG.
00299   * @param  pData: pointer to store read data
00300   * @param  DevAddr : Target device address
00301   * @param  Size : Size in bytes of the value to be read
00302   * @retval NFCTAG enum status
00303   */
00304 NFCTAG_StatusTypeDef ST25DV_IO_Read( uint8_t * const pData, const uint8_t DevAddr, const uint16_t Size, I2C* mi2cChannel )
00305 {
00306     //this has to change( send  addr then read)
00307     int i = 0;
00308     uint8_t ret = 4;
00309 
00310     char * pDataChar = (char*) pData;
00311     uint8_t ReadAddr = DevAddr | 1u;
00312     ret = mi2cChannel -> read(ReadAddr, pDataChar, 1, false );
00313 
00314 
00315 // Tell slave we need to read 1byte from the current register
00316     while(mi2cChannel -> read( 0 ) != 0) {
00317         pData[i++] = mi2cChannel -> read( 0 );
00318 
00319     }
00320 
00321 
00322   return NFCTAG_ConvertStatus( ret);
00323 }
00324 
00325 
00326 /**
00327 * @brief  Checks if target device is ready for communication
00328 * @note   This function is used with Memory devices
00329 * @param  DevAddr : Target device address
00330 * @param  mi2cChannel : I2C channel
00331 * @retval NFCTAG enum status
00332 */
00333 NFCTAG_StatusTypeDef ST25DV_IO_IsDeviceReady( const uint8_t DevAddr, const uint32_t Trials, I2C* mi2cChannel)
00334 { 
00335   int ret = 4;
00336   uint32_t count = 0;
00337 
00338   uint8_t Addr = DevAddr;
00339 
00340 
00341   while ((count++ < Trials && ret) ) {
00342       ret = mi2cChannel -> write(Addr, NULL, 0 , false);
00343   }
00344   return NFCTAG_ConvertStatus(ret);
00345 }
00346 
00347 
00348 
00349 
00350 /**
00351  * @}
00352  */
00353 
00354 /**
00355  * @}
00356  */
00357 
00358 /**
00359  * @}
00360  */
00361 
00362 /******************* (C) COPYRIGHT 2017 STMicroelectronics *****END OF FILE****/