Functions for accessing M24SR device.

Dependents:   Nucleo_NFC_Example I2C_NFC_Master Print_Entire_Nucleo_NFC01A1_Memory

Fork of lib_M24SR by Enrico Gregoratto

Warning: Deprecated!

Supported drivers and applications can be found at this link.

Committer:
EnricoG
Date:
Mon Dec 15 19:41:52 2014 +0000
Revision:
0:ffc2448b65ef
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
EnricoG 0:ffc2448b65ef 1 /**
EnricoG 0:ffc2448b65ef 2 ******************************************************************************
EnricoG 0:ffc2448b65ef 3 * @file drv_M24SR.c
EnricoG 0:ffc2448b65ef 4 * @author MMY Application Team
EnricoG 0:ffc2448b65ef 5 * @version V1.1.0
EnricoG 0:ffc2448b65ef 6 * @date 20-October-2014
EnricoG 0:ffc2448b65ef 7 * @brief This file provides a set of functions to interface with the M24SR
EnricoG 0:ffc2448b65ef 8 * device.
EnricoG 0:ffc2448b65ef 9 ******************************************************************************
EnricoG 0:ffc2448b65ef 10 * @attention
EnricoG 0:ffc2448b65ef 11 *
EnricoG 0:ffc2448b65ef 12 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
EnricoG 0:ffc2448b65ef 13 *
EnricoG 0:ffc2448b65ef 14 * Redistribution and use in source and binary forms, with or without modification,
EnricoG 0:ffc2448b65ef 15 * are permitted provided that the following conditions are met:
EnricoG 0:ffc2448b65ef 16 * 1. Redistributions of source code must retain the above copyright notice,
EnricoG 0:ffc2448b65ef 17 * this list of conditions and the following disclaimer.
EnricoG 0:ffc2448b65ef 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
EnricoG 0:ffc2448b65ef 19 * this list of conditions and the following disclaimer in the documentation
EnricoG 0:ffc2448b65ef 20 * and/or other materials provided with the distribution.
EnricoG 0:ffc2448b65ef 21 * 3. Neither the name of STMicroelectronics nor the names of its contributors
EnricoG 0:ffc2448b65ef 22 * may be used to endorse or promote products derived from this software
EnricoG 0:ffc2448b65ef 23 * without specific prior written permission.
EnricoG 0:ffc2448b65ef 24 *
EnricoG 0:ffc2448b65ef 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
EnricoG 0:ffc2448b65ef 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
EnricoG 0:ffc2448b65ef 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
EnricoG 0:ffc2448b65ef 28 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
EnricoG 0:ffc2448b65ef 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
EnricoG 0:ffc2448b65ef 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
EnricoG 0:ffc2448b65ef 31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
EnricoG 0:ffc2448b65ef 32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
EnricoG 0:ffc2448b65ef 33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
EnricoG 0:ffc2448b65ef 34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
EnricoG 0:ffc2448b65ef 35 *
EnricoG 0:ffc2448b65ef 36 ******************************************************************************
EnricoG 0:ffc2448b65ef 37 */
EnricoG 0:ffc2448b65ef 38
EnricoG 0:ffc2448b65ef 39 /* Includes ------------------------------------------------------------------*/
EnricoG 0:ffc2448b65ef 40 #include "m24sr.h"
EnricoG 0:ffc2448b65ef 41
EnricoG 0:ffc2448b65ef 42 /** @addtogroup M24SR_Driver
EnricoG 0:ffc2448b65ef 43 * @{
EnricoG 0:ffc2448b65ef 44 * @brief <b>This folder contains the driver layer of M24SR family (M24SR64, M24SR16, M24SR04, M24SR02)</b>
EnricoG 0:ffc2448b65ef 45 */
EnricoG 0:ffc2448b65ef 46
EnricoG 0:ffc2448b65ef 47
EnricoG 0:ffc2448b65ef 48 /** @addtogroup drv_M24SR
EnricoG 0:ffc2448b65ef 49 * @{
EnricoG 0:ffc2448b65ef 50 * @brief This file contains the driver which implements all the M24SR commands.
EnricoG 0:ffc2448b65ef 51 */
EnricoG 0:ffc2448b65ef 52
EnricoG 0:ffc2448b65ef 53 static C_APDU Command;
EnricoG 0:ffc2448b65ef 54 //static R_APDU Response;
EnricoG 0:ffc2448b65ef 55 static uint8_t DataBuffer[0xFF];
EnricoG 0:ffc2448b65ef 56 uint8_t uM24SRbuffer [0xFF];
EnricoG 0:ffc2448b65ef 57 static uint8_t uDIDbyte =0x00;
EnricoG 0:ffc2448b65ef 58
EnricoG 0:ffc2448b65ef 59
EnricoG 0:ffc2448b65ef 60 static uint16_t M24SR_UpdateCrc ( uint8_t ch, uint16_t *lpwCrc);
EnricoG 0:ffc2448b65ef 61 static uint16_t M24SR_ComputeCrc ( uc8 *Data, uint8_t Length);
EnricoG 0:ffc2448b65ef 62 static uint16_t M24SR_IsCorrectCRC16Residue ( uc8 *DataIn,uc8 Length);
EnricoG 0:ffc2448b65ef 63 static void M24SR_InitStructure ( void );
EnricoG 0:ffc2448b65ef 64 static void M24SR_BuildIBlockCommand ( uc16 CommandStructure, C_APDU Command, uint16_t *NbByte , uint8_t *pCommand);
EnricoG 0:ffc2448b65ef 65 static int8_t IsSBlock (uc8 *pBuffer);
EnricoG 0:ffc2448b65ef 66 #if 0
EnricoG 0:ffc2448b65ef 67 static int8_t IsRBlock (uc8 *pBuffer);
EnricoG 0:ffc2448b65ef 68 static int8_t IsIBlock (uc8 *pBuffer);
EnricoG 0:ffc2448b65ef 69 #endif
EnricoG 0:ffc2448b65ef 70 static uint16_t M24SR_FWTExtension ( uint8_t FWTbyte );
EnricoG 0:ffc2448b65ef 71
EnricoG 0:ffc2448b65ef 72 /** @defgroup drvM24SR_Private_Functions
EnricoG 0:ffc2448b65ef 73 * @{
EnricoG 0:ffc2448b65ef 74 */
EnricoG 0:ffc2448b65ef 75
EnricoG 0:ffc2448b65ef 76 /**
EnricoG 0:ffc2448b65ef 77 * @brief This function updates the CRC
EnricoG 0:ffc2448b65ef 78 * @param None
EnricoG 0:ffc2448b65ef 79 * @retval None
EnricoG 0:ffc2448b65ef 80 */
EnricoG 0:ffc2448b65ef 81 static uint16_t M24SR_UpdateCrc (uint8_t ch, uint16_t *lpwCrc)
EnricoG 0:ffc2448b65ef 82 {
EnricoG 0:ffc2448b65ef 83 ch = (ch^(uint8_t)((*lpwCrc) & 0x00FF));
EnricoG 0:ffc2448b65ef 84 ch = (ch^(ch<<4));
EnricoG 0:ffc2448b65ef 85 *lpwCrc = (*lpwCrc >> 8)^((uint16_t)ch << 8)^((uint16_t)ch<<3)^((uint16_t)ch>>4);
EnricoG 0:ffc2448b65ef 86
EnricoG 0:ffc2448b65ef 87 return(*lpwCrc);
EnricoG 0:ffc2448b65ef 88 }
EnricoG 0:ffc2448b65ef 89
EnricoG 0:ffc2448b65ef 90 /**
EnricoG 0:ffc2448b65ef 91 * @brief This function returns the CRC 16
EnricoG 0:ffc2448b65ef 92 * @param Data : pointer on the data used to compute the CRC16
EnricoG 0:ffc2448b65ef 93 * @param Length : number of byte of the data
EnricoG 0:ffc2448b65ef 94 * @retval CRC16
EnricoG 0:ffc2448b65ef 95 */
EnricoG 0:ffc2448b65ef 96 static uint16_t M24SR_ComputeCrc(uc8 *Data, uint8_t Length)
EnricoG 0:ffc2448b65ef 97 {
EnricoG 0:ffc2448b65ef 98 uint8_t chBlock;
EnricoG 0:ffc2448b65ef 99 uint16_t wCrc;
EnricoG 0:ffc2448b65ef 100
EnricoG 0:ffc2448b65ef 101 wCrc = 0x6363; // ITU-V.41
EnricoG 0:ffc2448b65ef 102
EnricoG 0:ffc2448b65ef 103 do {
EnricoG 0:ffc2448b65ef 104 chBlock = *Data++;
EnricoG 0:ffc2448b65ef 105 M24SR_UpdateCrc(chBlock, &wCrc);
EnricoG 0:ffc2448b65ef 106 } while (--Length);
EnricoG 0:ffc2448b65ef 107
EnricoG 0:ffc2448b65ef 108 return wCrc ;
EnricoG 0:ffc2448b65ef 109 }
EnricoG 0:ffc2448b65ef 110
EnricoG 0:ffc2448b65ef 111
EnricoG 0:ffc2448b65ef 112 /**
EnricoG 0:ffc2448b65ef 113 * @brief This function computes the CRC16 residue as defined by CRC ISO/IEC 13239
EnricoG 0:ffc2448b65ef 114 * @param DataIn : input to data
EnricoG 0:ffc2448b65ef 115 * @param Length : Number of bits of DataIn
EnricoG 0:ffc2448b65ef 116 * @retval Status (SW1&SW2) : CRC16 residue is correct
EnricoG 0:ffc2448b65ef 117 * @retval M24SR_ERROR_CRC : CRC16 residue is false
EnricoG 0:ffc2448b65ef 118 */
EnricoG 0:ffc2448b65ef 119 static uint16_t M24SR_IsCorrectCRC16Residue (uc8 *DataIn,uc8 Length)
EnricoG 0:ffc2448b65ef 120 {
EnricoG 0:ffc2448b65ef 121 uint16_t ResCRC=0;
EnricoG 0:ffc2448b65ef 122
EnricoG 0:ffc2448b65ef 123 /* check the CRC16 Residue */
EnricoG 0:ffc2448b65ef 124 if (Length !=0)
EnricoG 0:ffc2448b65ef 125 ResCRC= M24SR_ComputeCrc (DataIn, Length);
EnricoG 0:ffc2448b65ef 126
EnricoG 0:ffc2448b65ef 127 if ( ResCRC == 0x0000)
EnricoG 0:ffc2448b65ef 128 {
EnricoG 0:ffc2448b65ef 129 /* Good CRC, but error status from M24SR */
EnricoG 0:ffc2448b65ef 130 return( ((DataIn[Length-UB_STATUS_OFFSET]<<8) & 0xFF00) | (DataIn[Length-LB_STATUS_OFFSET] & 0x00FF) );
EnricoG 0:ffc2448b65ef 131 }
EnricoG 0:ffc2448b65ef 132 else
EnricoG 0:ffc2448b65ef 133 {
EnricoG 0:ffc2448b65ef 134 ResCRC=0;
EnricoG 0:ffc2448b65ef 135 ResCRC= M24SR_ComputeCrc (DataIn, 5);
EnricoG 0:ffc2448b65ef 136 if ( ResCRC != 0x0000)
EnricoG 0:ffc2448b65ef 137 {
EnricoG 0:ffc2448b65ef 138 /* Bad CRC */
EnricoG 0:ffc2448b65ef 139 return M24SR_ERROR_CRC;
EnricoG 0:ffc2448b65ef 140 }
EnricoG 0:ffc2448b65ef 141 else
EnricoG 0:ffc2448b65ef 142 {
EnricoG 0:ffc2448b65ef 143 /* Good CRC, but error status from M24SR */
EnricoG 0:ffc2448b65ef 144 return( ((DataIn[1]<<8) & 0xFF00) | (DataIn[2] & 0x00FF) );
EnricoG 0:ffc2448b65ef 145 }
EnricoG 0:ffc2448b65ef 146 }
EnricoG 0:ffc2448b65ef 147 }
EnricoG 0:ffc2448b65ef 148
EnricoG 0:ffc2448b65ef 149 /**
EnricoG 0:ffc2448b65ef 150 * @brief Initialize the command and response structure
EnricoG 0:ffc2448b65ef 151 * @param None
EnricoG 0:ffc2448b65ef 152 * @retval None
EnricoG 0:ffc2448b65ef 153 */
EnricoG 0:ffc2448b65ef 154 static void M24SR_InitStructure ( void )
EnricoG 0:ffc2448b65ef 155 {
EnricoG 0:ffc2448b65ef 156 /* build the command */
EnricoG 0:ffc2448b65ef 157 Command.Header.CLA = 0x00;
EnricoG 0:ffc2448b65ef 158 Command.Header.INS = 0x00;
EnricoG 0:ffc2448b65ef 159 /* copy the offset */
EnricoG 0:ffc2448b65ef 160 Command.Header.P1 = 0x00 ;
EnricoG 0:ffc2448b65ef 161 Command.Header.P2 = 0x00 ;
EnricoG 0:ffc2448b65ef 162 /* copy the number of byte of the data field */
EnricoG 0:ffc2448b65ef 163 Command.Body.LC = 0x00 ;
EnricoG 0:ffc2448b65ef 164 /* copy the number of byte to read */
EnricoG 0:ffc2448b65ef 165 Command.Body.LE = 0x00 ;
EnricoG 0:ffc2448b65ef 166 Command.Body.pData = DataBuffer;
EnricoG 0:ffc2448b65ef 167
EnricoG 0:ffc2448b65ef 168 // /* initializes the response structure*/
EnricoG 0:ffc2448b65ef 169 // Response.pData = DataBuffer;
EnricoG 0:ffc2448b65ef 170 // Response.SW1 = 0x00;
EnricoG 0:ffc2448b65ef 171 // Response.SW2 = 0x00;
EnricoG 0:ffc2448b65ef 172 }
EnricoG 0:ffc2448b65ef 173
EnricoG 0:ffc2448b65ef 174 /**
EnricoG 0:ffc2448b65ef 175 * @brief This functions creates an I block command according to the structures CommandStructure and Command.
EnricoG 0:ffc2448b65ef 176 * @param Command : structue which contains the field of the different parameter
EnricoG 0:ffc2448b65ef 177 * @param CommandStructure : structure that contain the structure of the command (if the different field are presnet or not
EnricoG 0:ffc2448b65ef 178 * @param NbByte : number of byte of the command
EnricoG 0:ffc2448b65ef 179 * @param pCommand : pointer of the command created
EnricoG 0:ffc2448b65ef 180 */
EnricoG 0:ffc2448b65ef 181 static void M24SR_BuildIBlockCommand ( uc16 CommandStructure, C_APDU Command, uint16_t *NbByte , uint8_t *pCommand)
EnricoG 0:ffc2448b65ef 182 {
EnricoG 0:ffc2448b65ef 183 uint16_t uCRC16;
EnricoG 0:ffc2448b65ef 184 static uint8_t BlockNumber = 0x01;
EnricoG 0:ffc2448b65ef 185
EnricoG 0:ffc2448b65ef 186 (*NbByte) = 0;
EnricoG 0:ffc2448b65ef 187
EnricoG 0:ffc2448b65ef 188 /* add the PCD byte */
EnricoG 0:ffc2448b65ef 189 if ((CommandStructure & M24SR_PCB_NEEDED) !=0)
EnricoG 0:ffc2448b65ef 190 {
EnricoG 0:ffc2448b65ef 191 /* toggle the block number */
EnricoG 0:ffc2448b65ef 192 BlockNumber = TOGGLE ( BlockNumber );
EnricoG 0:ffc2448b65ef 193 /* Add the I block byte */
EnricoG 0:ffc2448b65ef 194 pCommand[(*NbByte)++] = 0x02 | BlockNumber;
EnricoG 0:ffc2448b65ef 195 }
EnricoG 0:ffc2448b65ef 196
EnricoG 0:ffc2448b65ef 197 /* add the DID byte */
EnricoG 0:ffc2448b65ef 198 if ((BlockNumber & M24SR_DID_NEEDED) !=0)
EnricoG 0:ffc2448b65ef 199 {
EnricoG 0:ffc2448b65ef 200 /* Add the I block byte */
EnricoG 0:ffc2448b65ef 201 pCommand[(*NbByte)++] = uDIDbyte;
EnricoG 0:ffc2448b65ef 202 }
EnricoG 0:ffc2448b65ef 203
EnricoG 0:ffc2448b65ef 204 /* add the Class byte */
EnricoG 0:ffc2448b65ef 205 if ((CommandStructure & M24SR_CLA_NEEDED) !=0)
EnricoG 0:ffc2448b65ef 206 {
EnricoG 0:ffc2448b65ef 207 pCommand[(*NbByte)++] = Command.Header.CLA ;
EnricoG 0:ffc2448b65ef 208 }
EnricoG 0:ffc2448b65ef 209 /* add the instruction byte byte */
EnricoG 0:ffc2448b65ef 210 if ( (CommandStructure & M24SR_INS_NEEDED) !=0)
EnricoG 0:ffc2448b65ef 211 {
EnricoG 0:ffc2448b65ef 212 pCommand[(*NbByte)++] = Command.Header.INS ;
EnricoG 0:ffc2448b65ef 213 }
EnricoG 0:ffc2448b65ef 214 /* add the Selection Mode byte */
EnricoG 0:ffc2448b65ef 215 if ((CommandStructure & M24SR_P1_NEEDED) !=0)
EnricoG 0:ffc2448b65ef 216 {
EnricoG 0:ffc2448b65ef 217 pCommand[(*NbByte)++] = Command.Header.P1 ;
EnricoG 0:ffc2448b65ef 218 }
EnricoG 0:ffc2448b65ef 219 /* add the Selection Mode byte */
EnricoG 0:ffc2448b65ef 220 if ((CommandStructure & M24SR_P2_NEEDED) !=0)
EnricoG 0:ffc2448b65ef 221 {
EnricoG 0:ffc2448b65ef 222 pCommand[(*NbByte)++] = Command.Header.P2 ;
EnricoG 0:ffc2448b65ef 223 }
EnricoG 0:ffc2448b65ef 224 /* add Data field lengthbyte */
EnricoG 0:ffc2448b65ef 225 if ((CommandStructure & M24SR_LC_NEEDED) !=0)
EnricoG 0:ffc2448b65ef 226 {
EnricoG 0:ffc2448b65ef 227 pCommand[(*NbByte)++] = Command.Body.LC ;
EnricoG 0:ffc2448b65ef 228 }
EnricoG 0:ffc2448b65ef 229 /* add Data field */
EnricoG 0:ffc2448b65ef 230 if ((CommandStructure & M24SR_DATA_NEEDED) !=0)
EnricoG 0:ffc2448b65ef 231 {
EnricoG 0:ffc2448b65ef 232 memcpy(&(pCommand[(*NbByte)]) ,Command.Body.pData,Command.Body.LC ) ;
EnricoG 0:ffc2448b65ef 233 (*NbByte) += Command.Body.LC ;
EnricoG 0:ffc2448b65ef 234 }
EnricoG 0:ffc2448b65ef 235 /* add Le field */
EnricoG 0:ffc2448b65ef 236 if ((CommandStructure & M24SR_LE_NEEDED) !=0)
EnricoG 0:ffc2448b65ef 237 {
EnricoG 0:ffc2448b65ef 238 pCommand[(*NbByte)++] = Command.Body.LE ;
EnricoG 0:ffc2448b65ef 239 }
EnricoG 0:ffc2448b65ef 240 /* add CRC field */
EnricoG 0:ffc2448b65ef 241 if ((CommandStructure & M24SR_CRC_NEEDED) !=0)
EnricoG 0:ffc2448b65ef 242 {
EnricoG 0:ffc2448b65ef 243 uCRC16 = M24SR_ComputeCrc (pCommand,(uint8_t) (*NbByte));
EnricoG 0:ffc2448b65ef 244 /* append the CRC16 */
EnricoG 0:ffc2448b65ef 245 pCommand [(*NbByte)++] = GETLSB (uCRC16 ) ;
EnricoG 0:ffc2448b65ef 246 pCommand [(*NbByte)++] = GETMSB (uCRC16 ) ;
EnricoG 0:ffc2448b65ef 247 }
EnricoG 0:ffc2448b65ef 248
EnricoG 0:ffc2448b65ef 249 }
EnricoG 0:ffc2448b65ef 250
EnricoG 0:ffc2448b65ef 251 #if 0
EnricoG 0:ffc2448b65ef 252 /**
EnricoG 0:ffc2448b65ef 253 * @brief This function return M24SR_STATUS_SUCCESS if the pBuffer is an I-block
EnricoG 0:ffc2448b65ef 254 * @param pBuffer : pointer of the data
EnricoG 0:ffc2448b65ef 255 * @retval M24SR_STATUS_SUCCESS : the data is a I-Block
EnricoG 0:ffc2448b65ef 256 * @retval M24SR_ERROR_DEFAULT : the data is not a I-Block
EnricoG 0:ffc2448b65ef 257 */
EnricoG 0:ffc2448b65ef 258 static int8_t IsIBlock (uc8 *pBuffer)
EnricoG 0:ffc2448b65ef 259 {
EnricoG 0:ffc2448b65ef 260
EnricoG 0:ffc2448b65ef 261 if ((pBuffer[M24SR_OFFSET_PCB] & M24SR_MASK_BLOCK) == M24SR_MASK_IBLOCK)
EnricoG 0:ffc2448b65ef 262 {
EnricoG 0:ffc2448b65ef 263 return M24SR_STATUS_SUCCESS;
EnricoG 0:ffc2448b65ef 264 }
EnricoG 0:ffc2448b65ef 265 else
EnricoG 0:ffc2448b65ef 266 {
EnricoG 0:ffc2448b65ef 267 return M24SR_ERROR_DEFAULT;
EnricoG 0:ffc2448b65ef 268 }
EnricoG 0:ffc2448b65ef 269
EnricoG 0:ffc2448b65ef 270 }
EnricoG 0:ffc2448b65ef 271
EnricoG 0:ffc2448b65ef 272 /**
EnricoG 0:ffc2448b65ef 273 * @brief This function return M24SR_STATUS_SUCCESS if the pBuffer is an R-block
EnricoG 0:ffc2448b65ef 274 * @param pBuffer : pointer of the data
EnricoG 0:ffc2448b65ef 275 * @retval M24SR_STATUS_SUCCESS : the data is a R-Block
EnricoG 0:ffc2448b65ef 276 * @retval M24SR_ERROR_DEFAULT : the data is not a R-Block
EnricoG 0:ffc2448b65ef 277 */
EnricoG 0:ffc2448b65ef 278 static int8_t IsRBlock (uc8 *pBuffer)
EnricoG 0:ffc2448b65ef 279 {
EnricoG 0:ffc2448b65ef 280
EnricoG 0:ffc2448b65ef 281 if ((pBuffer[M24SR_OFFSET_PCB] & M24SR_MASK_BLOCK) == M24SR_MASK_RBLOCK)
EnricoG 0:ffc2448b65ef 282 {
EnricoG 0:ffc2448b65ef 283 return M24SR_STATUS_SUCCESS;
EnricoG 0:ffc2448b65ef 284 }
EnricoG 0:ffc2448b65ef 285 else
EnricoG 0:ffc2448b65ef 286 {
EnricoG 0:ffc2448b65ef 287 return M24SR_ERROR_DEFAULT;
EnricoG 0:ffc2448b65ef 288 }
EnricoG 0:ffc2448b65ef 289
EnricoG 0:ffc2448b65ef 290 }
EnricoG 0:ffc2448b65ef 291 #endif
EnricoG 0:ffc2448b65ef 292
EnricoG 0:ffc2448b65ef 293 /**
EnricoG 0:ffc2448b65ef 294 * @brief This function return M24SR_STATUS_SUCCESS if the pBuffer is an s-block
EnricoG 0:ffc2448b65ef 295 * @param pBuffer : pointer of the data
EnricoG 0:ffc2448b65ef 296 * @retval M24SR_STATUS_SUCCESS : the data is a S-Block
EnricoG 0:ffc2448b65ef 297 * @retval M24SR_ERROR_DEFAULT : the data is not a S-Block
EnricoG 0:ffc2448b65ef 298 */
EnricoG 0:ffc2448b65ef 299 static int8_t IsSBlock (uc8 *pBuffer)
EnricoG 0:ffc2448b65ef 300 {
EnricoG 0:ffc2448b65ef 301
EnricoG 0:ffc2448b65ef 302 if ((pBuffer[M24SR_OFFSET_PCB] & M24SR_MASK_BLOCK) == M24SR_MASK_SBLOCK)
EnricoG 0:ffc2448b65ef 303 {
EnricoG 0:ffc2448b65ef 304 return M24SR_STATUS_SUCCESS;
EnricoG 0:ffc2448b65ef 305 }
EnricoG 0:ffc2448b65ef 306 else
EnricoG 0:ffc2448b65ef 307 {
EnricoG 0:ffc2448b65ef 308 return M24SR_ERROR_DEFAULT;
EnricoG 0:ffc2448b65ef 309 }
EnricoG 0:ffc2448b65ef 310
EnricoG 0:ffc2448b65ef 311 }
EnricoG 0:ffc2448b65ef 312
EnricoG 0:ffc2448b65ef 313 /**
EnricoG 0:ffc2448b65ef 314 * @brief This function sends the FWT extension command (S-Block format)
EnricoG 0:ffc2448b65ef 315 * @param FWTbyte : FWT value
EnricoG 0:ffc2448b65ef 316 * @retval Status (SW1&SW2) : Status of the operation to complete.
EnricoG 0:ffc2448b65ef 317 * @retval M24SR_ERROR_I2CTIMEOUT : The I2C timeout occured.
EnricoG 0:ffc2448b65ef 318 */
EnricoG 0:ffc2448b65ef 319 static uint16_t M24SR_FWTExtension ( uint8_t FWTbyte )
EnricoG 0:ffc2448b65ef 320 {
EnricoG 0:ffc2448b65ef 321 uint8_t pBuffer[M24SR_STATUSRESPONSE_NBBYTE];
EnricoG 0:ffc2448b65ef 322 uint16_t status ;
EnricoG 0:ffc2448b65ef 323 uint16_t NthByte = 0,
EnricoG 0:ffc2448b65ef 324 uCRC16;
EnricoG 0:ffc2448b65ef 325
EnricoG 0:ffc2448b65ef 326 /* create the response */
EnricoG 0:ffc2448b65ef 327 pBuffer[NthByte++] = 0xF2 ;
EnricoG 0:ffc2448b65ef 328 pBuffer[NthByte++] = FWTbyte ;
EnricoG 0:ffc2448b65ef 329 /* compute the CRC */
EnricoG 0:ffc2448b65ef 330 uCRC16 = M24SR_ComputeCrc (pBuffer,0x02);
EnricoG 0:ffc2448b65ef 331 /* append the CRC16 */
EnricoG 0:ffc2448b65ef 332 pBuffer [NthByte++] = GETLSB (uCRC16 ) ;
EnricoG 0:ffc2448b65ef 333 pBuffer [NthByte++]= GETMSB (uCRC16 ) ;
EnricoG 0:ffc2448b65ef 334
EnricoG 0:ffc2448b65ef 335 /* send the request */
EnricoG 0:ffc2448b65ef 336 errchk( M24SR_SendI2Ccommand ( NthByte , pBuffer ));
EnricoG 0:ffc2448b65ef 337 errchk( M24SR_IsAnswerReady ( ));
EnricoG 0:ffc2448b65ef 338 /* read the response */
EnricoG 0:ffc2448b65ef 339 errchk( M24SR_ReceiveI2Cresponse ( M24SR_STATUSRESPONSE_NBBYTE , pBuffer ));
EnricoG 0:ffc2448b65ef 340
EnricoG 0:ffc2448b65ef 341 status = M24SR_IsCorrectCRC16Residue (pBuffer, M24SR_STATUSRESPONSE_NBBYTE);
EnricoG 0:ffc2448b65ef 342 return status;
EnricoG 0:ffc2448b65ef 343
EnricoG 0:ffc2448b65ef 344 Error :
EnricoG 0:ffc2448b65ef 345 return M24SR_ERROR_I2CTIMEOUT;
EnricoG 0:ffc2448b65ef 346 }
EnricoG 0:ffc2448b65ef 347
EnricoG 0:ffc2448b65ef 348 /**
EnricoG 0:ffc2448b65ef 349 * @}
EnricoG 0:ffc2448b65ef 350 */
EnricoG 0:ffc2448b65ef 351
EnricoG 0:ffc2448b65ef 352
EnricoG 0:ffc2448b65ef 353 /** @defgroup drvM24SR_Public_Functions
EnricoG 0:ffc2448b65ef 354 * @{
EnricoG 0:ffc2448b65ef 355 */
EnricoG 0:ffc2448b65ef 356
EnricoG 0:ffc2448b65ef 357 /**
EnricoG 0:ffc2448b65ef 358 * @brief This function initialize the M24SR device
EnricoG 0:ffc2448b65ef 359 * @retval None
EnricoG 0:ffc2448b65ef 360 */
EnricoG 0:ffc2448b65ef 361 void M24SR_Init( void )
EnricoG 0:ffc2448b65ef 362 {
EnricoG 0:ffc2448b65ef 363 M24SR_I2CInit();
EnricoG 0:ffc2448b65ef 364 // M24SR_GPOInit(); // Empty function
EnricoG 0:ffc2448b65ef 365
EnricoG 0:ffc2448b65ef 366 M24SR_InitStructure();
EnricoG 0:ffc2448b65ef 367
EnricoG 0:ffc2448b65ef 368 #if defined (I2C_GPO_SYNCHRO_ALLOWED) || defined (I2C_GPO_INTERRUPT_ALLOWED)
EnricoG 0:ffc2448b65ef 369 if( M24SR_KillSession() == M24SR_ACTION_COMPLETED)
EnricoG 0:ffc2448b65ef 370 {
EnricoG 0:ffc2448b65ef 371 M24SR_ManageI2CGPO(I2C_ANSWER_READY);
EnricoG 0:ffc2448b65ef 372 M24SR_Deselect ();
EnricoG 0:ffc2448b65ef 373 }
EnricoG 0:ffc2448b65ef 374 #endif /* I2C_GPO_SYNCHRO_ALLOWED */
EnricoG 0:ffc2448b65ef 375 }
EnricoG 0:ffc2448b65ef 376
EnricoG 0:ffc2448b65ef 377 /**
EnricoG 0:ffc2448b65ef 378 * @brief This function sends the GetSession command to the M24SR device
EnricoG 0:ffc2448b65ef 379 * @retval M24SR_ACTION_COMPLETED : the function is succesful.
EnricoG 0:ffc2448b65ef 380 * @retval Status (SW1&SW2) : if operation does not complete.
EnricoG 0:ffc2448b65ef 381 */
EnricoG 0:ffc2448b65ef 382 uint16_t M24SR_GetSession ( void )
EnricoG 0:ffc2448b65ef 383 {
EnricoG 0:ffc2448b65ef 384 uint8_t Buffer = M24SR_OPENSESSION;
EnricoG 0:ffc2448b65ef 385 int16_t status;
EnricoG 0:ffc2448b65ef 386
EnricoG 0:ffc2448b65ef 387 errchk(M24SR_SendI2Ccommand ( 0x01 , &Buffer ));
EnricoG 0:ffc2448b65ef 388
EnricoG 0:ffc2448b65ef 389 /* Insure no access will be done just after open session */
EnricoG 0:ffc2448b65ef 390 /* The only way here is to poll I2C to know when M24SR is ready */
EnricoG 0:ffc2448b65ef 391 /* GPO can not be use with GetSession command */
EnricoG 0:ffc2448b65ef 392 errchk(M24SR_PollI2C ( ));
EnricoG 0:ffc2448b65ef 393
EnricoG 0:ffc2448b65ef 394 return M24SR_ACTION_COMPLETED;
EnricoG 0:ffc2448b65ef 395 Error :
EnricoG 0:ffc2448b65ef 396 return M24SR_ERROR_I2CTIMEOUT;
EnricoG 0:ffc2448b65ef 397 }
EnricoG 0:ffc2448b65ef 398
EnricoG 0:ffc2448b65ef 399
EnricoG 0:ffc2448b65ef 400
EnricoG 0:ffc2448b65ef 401 /**
EnricoG 0:ffc2448b65ef 402 * @brief This function sends the KillSession command to the M24SR device
EnricoG 0:ffc2448b65ef 403 * @param None
EnricoG 0:ffc2448b65ef 404 * @retval M24SR_ACTION_COMPLETED : the function is succesful.
EnricoG 0:ffc2448b65ef 405 * @retval M24SR_ERROR_I2CTIMEOUT : The I2C timeout occured.
EnricoG 0:ffc2448b65ef 406 */
EnricoG 0:ffc2448b65ef 407 uint16_t M24SR_KillSession ( void )
EnricoG 0:ffc2448b65ef 408 {
EnricoG 0:ffc2448b65ef 409 uint8_t pBuffer[] = {M24SR_KILLSESSION};
EnricoG 0:ffc2448b65ef 410 int8_t status;
EnricoG 0:ffc2448b65ef 411
EnricoG 0:ffc2448b65ef 412 errchk(M24SR_SendI2Ccommand ( 0x01 , pBuffer ));
EnricoG 0:ffc2448b65ef 413
EnricoG 0:ffc2448b65ef 414 /* Insure no access will be done just after open session */
EnricoG 0:ffc2448b65ef 415 /* The only way here is to poll I2C to know when M24SR is ready */
EnricoG 0:ffc2448b65ef 416 /* GPO can not be use with KillSession command */
EnricoG 0:ffc2448b65ef 417 errchk(M24SR_PollI2C ( ));
EnricoG 0:ffc2448b65ef 418
EnricoG 0:ffc2448b65ef 419 return M24SR_ACTION_COMPLETED;
EnricoG 0:ffc2448b65ef 420 Error :
EnricoG 0:ffc2448b65ef 421 return M24SR_ERROR_I2CTIMEOUT;
EnricoG 0:ffc2448b65ef 422 }
EnricoG 0:ffc2448b65ef 423
EnricoG 0:ffc2448b65ef 424
EnricoG 0:ffc2448b65ef 425 /**
EnricoG 0:ffc2448b65ef 426 * @brief This function sends the Deselect command (S-Block format)
EnricoG 0:ffc2448b65ef 427 * @retval M24SR_ACTION_COMPLETED : the function is succesful.
EnricoG 0:ffc2448b65ef 428 * @retval M24SR_ERROR_I2CTIMEOUT : The I2C timeout occured.
EnricoG 0:ffc2448b65ef 429 */
EnricoG 0:ffc2448b65ef 430 uint16_t M24SR_Deselect ( void )
EnricoG 0:ffc2448b65ef 431 {
EnricoG 0:ffc2448b65ef 432 uint8_t pBuffer[] = {0xC2,0xE0,0xB4} ;
EnricoG 0:ffc2448b65ef 433 int8_t status ;
EnricoG 0:ffc2448b65ef 434
EnricoG 0:ffc2448b65ef 435 /* send the request */
EnricoG 0:ffc2448b65ef 436 errchk( M24SR_SendI2Ccommand ( M24SR_DESELECTREQUEST_NBBYTE , pBuffer ));
EnricoG 0:ffc2448b65ef 437
EnricoG 0:ffc2448b65ef 438 errchk( M24SR_IsAnswerReady ( ));
EnricoG 0:ffc2448b65ef 439 /* flush the M24SR buffer */
EnricoG 0:ffc2448b65ef 440 errchk( M24SR_ReceiveI2Cresponse ( M24SR_DESELECTREQUEST_NBBYTE , pBuffer ));
EnricoG 0:ffc2448b65ef 441
EnricoG 0:ffc2448b65ef 442 return M24SR_ACTION_COMPLETED;
EnricoG 0:ffc2448b65ef 443
EnricoG 0:ffc2448b65ef 444 Error :
EnricoG 0:ffc2448b65ef 445 return M24SR_ERROR_I2CTIMEOUT;
EnricoG 0:ffc2448b65ef 446 }
EnricoG 0:ffc2448b65ef 447
EnricoG 0:ffc2448b65ef 448
EnricoG 0:ffc2448b65ef 449
EnricoG 0:ffc2448b65ef 450
EnricoG 0:ffc2448b65ef 451 /**
EnricoG 0:ffc2448b65ef 452 * @brief This function sends the SelectApplication command
EnricoG 0:ffc2448b65ef 453 * @retval M24SR_ACTION_COMPLETED : the function is succesful.
EnricoG 0:ffc2448b65ef 454 * @retval M24SR_ERROR_I2CTIMEOUT : The I2C timeout occured.
EnricoG 0:ffc2448b65ef 455 */
EnricoG 0:ffc2448b65ef 456 uint16_t M24SR_SelectApplication ( void )
EnricoG 0:ffc2448b65ef 457 {
EnricoG 0:ffc2448b65ef 458 uint8_t *pBuffer = uM24SRbuffer ,
EnricoG 0:ffc2448b65ef 459 NbByteToRead = M24SR_STATUSRESPONSE_NBBYTE;
EnricoG 0:ffc2448b65ef 460 uint8_t uLc = 0x07,
EnricoG 0:ffc2448b65ef 461 pData[] = {0xD2,0x76,0x00,0x00,0x85,0x01,0x01},
EnricoG 0:ffc2448b65ef 462 uLe = 0x00;
EnricoG 0:ffc2448b65ef 463 uint16_t status ;
EnricoG 0:ffc2448b65ef 464 uint16_t uP1P2 =0x0400,
EnricoG 0:ffc2448b65ef 465 NbByte;
EnricoG 0:ffc2448b65ef 466
EnricoG 0:ffc2448b65ef 467 /* build the command */
EnricoG 0:ffc2448b65ef 468 Command.Header.CLA = C_APDU_CLA_DEFAULT;
EnricoG 0:ffc2448b65ef 469 Command.Header.INS = C_APDU_SELECT_FILE;
EnricoG 0:ffc2448b65ef 470 /* copy the offset */
EnricoG 0:ffc2448b65ef 471 Command.Header.P1 = GETMSB (uP1P2 ) ;
EnricoG 0:ffc2448b65ef 472 Command.Header.P2 = GETLSB (uP1P2 ) ;
EnricoG 0:ffc2448b65ef 473 /* copy the number of byte of the data field */
EnricoG 0:ffc2448b65ef 474 Command.Body.LC = uLc ;
EnricoG 0:ffc2448b65ef 475 /* copy the data */
EnricoG 0:ffc2448b65ef 476 memcpy(Command.Body.pData, pData, uLc);
EnricoG 0:ffc2448b65ef 477 /* copy the number of byte to read */
EnricoG 0:ffc2448b65ef 478 Command.Body.LE = uLe ;
EnricoG 0:ffc2448b65ef 479 /* build the I￿C command */
EnricoG 0:ffc2448b65ef 480 M24SR_BuildIBlockCommand ( M24SR_CMDSTRUCT_SELECTAPPLICATION, Command, &NbByte , pBuffer);
EnricoG 0:ffc2448b65ef 481
EnricoG 0:ffc2448b65ef 482 /* send the request */
EnricoG 0:ffc2448b65ef 483 errchk( M24SR_SendI2Ccommand ( NbByte , pBuffer ));
EnricoG 0:ffc2448b65ef 484 errchk( M24SR_IsAnswerReady ( ));
EnricoG 0:ffc2448b65ef 485 /* read the response */
EnricoG 0:ffc2448b65ef 486 errchk( M24SR_ReceiveI2Cresponse ( NbByteToRead , pBuffer ));
EnricoG 0:ffc2448b65ef 487
EnricoG 0:ffc2448b65ef 488 status = M24SR_IsCorrectCRC16Residue (pBuffer,NbByteToRead);
EnricoG 0:ffc2448b65ef 489 return status;
EnricoG 0:ffc2448b65ef 490
EnricoG 0:ffc2448b65ef 491 Error :
EnricoG 0:ffc2448b65ef 492 return M24SR_ERROR_I2CTIMEOUT;
EnricoG 0:ffc2448b65ef 493 }
EnricoG 0:ffc2448b65ef 494
EnricoG 0:ffc2448b65ef 495 /**
EnricoG 0:ffc2448b65ef 496 * @brief This function sends the SelectCCFile command
EnricoG 0:ffc2448b65ef 497 * @retval M24SR_ACTION_COMPLETED : the function is succesful.
EnricoG 0:ffc2448b65ef 498 * @retval M24SR_ERROR_I2CTIMEOUT : The I2C timeout occured.
EnricoG 0:ffc2448b65ef 499 * @retval Status (SW1&SW2) : if operation does not complete for another reason.
EnricoG 0:ffc2448b65ef 500 */
EnricoG 0:ffc2448b65ef 501 uint16_t M24SR_SelectCCfile ( void )
EnricoG 0:ffc2448b65ef 502 {
EnricoG 0:ffc2448b65ef 503 uint8_t *pBuffer = uM24SRbuffer ,
EnricoG 0:ffc2448b65ef 504 NbByteToRead = M24SR_STATUSRESPONSE_NBBYTE;
EnricoG 0:ffc2448b65ef 505 uint8_t uLc = 0x02;
EnricoG 0:ffc2448b65ef 506 uint16_t status ;
EnricoG 0:ffc2448b65ef 507 uint16_t uP1P2 =0x000C,
EnricoG 0:ffc2448b65ef 508 uNbFileId =CC_FILE_ID,
EnricoG 0:ffc2448b65ef 509 NbByte;
EnricoG 0:ffc2448b65ef 510
EnricoG 0:ffc2448b65ef 511 /* build the command */
EnricoG 0:ffc2448b65ef 512 Command.Header.CLA = C_APDU_CLA_DEFAULT;
EnricoG 0:ffc2448b65ef 513 Command.Header.INS = C_APDU_SELECT_FILE;
EnricoG 0:ffc2448b65ef 514 /* copy the offset */
EnricoG 0:ffc2448b65ef 515 Command.Header.P1 = GETMSB (uP1P2 ) ;
EnricoG 0:ffc2448b65ef 516 Command.Header.P2 = GETLSB (uP1P2 ) ;
EnricoG 0:ffc2448b65ef 517 /* copy the number of byte of the data field */
EnricoG 0:ffc2448b65ef 518 Command.Body.LC = uLc ;
EnricoG 0:ffc2448b65ef 519 /* copy the File Id */
EnricoG 0:ffc2448b65ef 520 Command.Body.pData[0] = GETMSB (uNbFileId ) ;
EnricoG 0:ffc2448b65ef 521 Command.Body.pData[1] = GETLSB (uNbFileId ) ;
EnricoG 0:ffc2448b65ef 522 /* build the I￿C command */
EnricoG 0:ffc2448b65ef 523 M24SR_BuildIBlockCommand ( M24SR_CMDSTRUCT_SELECTCCFILE, Command, &NbByte , pBuffer);
EnricoG 0:ffc2448b65ef 524
EnricoG 0:ffc2448b65ef 525 /* send the request */
EnricoG 0:ffc2448b65ef 526 errchk( M24SR_SendI2Ccommand ( NbByte , pBuffer ));
EnricoG 0:ffc2448b65ef 527 errchk( M24SR_IsAnswerReady ( ));
EnricoG 0:ffc2448b65ef 528 /* read the response */
EnricoG 0:ffc2448b65ef 529 errchk( M24SR_ReceiveI2Cresponse ( NbByteToRead , pBuffer ));
EnricoG 0:ffc2448b65ef 530
EnricoG 0:ffc2448b65ef 531 status = M24SR_IsCorrectCRC16Residue (pBuffer,NbByteToRead);
EnricoG 0:ffc2448b65ef 532 return status;
EnricoG 0:ffc2448b65ef 533
EnricoG 0:ffc2448b65ef 534 Error :
EnricoG 0:ffc2448b65ef 535 return M24SR_ERROR_I2CTIMEOUT;
EnricoG 0:ffc2448b65ef 536 }
EnricoG 0:ffc2448b65ef 537
EnricoG 0:ffc2448b65ef 538
EnricoG 0:ffc2448b65ef 539 /**
EnricoG 0:ffc2448b65ef 540 * @brief This function sends the SelectSystemFile command
EnricoG 0:ffc2448b65ef 541 * @retval Status (SW1&SW2) : Status of the operation to complete.
EnricoG 0:ffc2448b65ef 542 * @retval M24SR_ERROR_I2CTIMEOUT : The I2C timeout occured.
EnricoG 0:ffc2448b65ef 543 */
EnricoG 0:ffc2448b65ef 544 uint16_t M24SR_SelectSystemfile ( void )
EnricoG 0:ffc2448b65ef 545 {
EnricoG 0:ffc2448b65ef 546 uint8_t *pBuffer = uM24SRbuffer ,
EnricoG 0:ffc2448b65ef 547 NbByteToRead = M24SR_STATUSRESPONSE_NBBYTE;
EnricoG 0:ffc2448b65ef 548 uint8_t uLc = 0x02;
EnricoG 0:ffc2448b65ef 549 uint16_t status ;
EnricoG 0:ffc2448b65ef 550 uint16_t uP1P2 =0x000C,
EnricoG 0:ffc2448b65ef 551 uNbFileId =SYSTEM_FILE_ID,
EnricoG 0:ffc2448b65ef 552 NbByte;
EnricoG 0:ffc2448b65ef 553
EnricoG 0:ffc2448b65ef 554 /* build the command */
EnricoG 0:ffc2448b65ef 555 Command.Header.CLA = C_APDU_CLA_DEFAULT;
EnricoG 0:ffc2448b65ef 556 Command.Header.INS = C_APDU_SELECT_FILE;
EnricoG 0:ffc2448b65ef 557 /* copy the offset */
EnricoG 0:ffc2448b65ef 558 Command.Header.P1 = GETMSB (uP1P2 ) ;
EnricoG 0:ffc2448b65ef 559 Command.Header.P2 = GETLSB (uP1P2 ) ;
EnricoG 0:ffc2448b65ef 560 /* copy the number of byte of the data field */
EnricoG 0:ffc2448b65ef 561 Command.Body.LC = uLc ;
EnricoG 0:ffc2448b65ef 562 /* copy the File Id */
EnricoG 0:ffc2448b65ef 563 Command.Body.pData[0] = GETMSB (uNbFileId ) ;
EnricoG 0:ffc2448b65ef 564 Command.Body.pData[1] = GETLSB (uNbFileId ) ;
EnricoG 0:ffc2448b65ef 565 /* build the I￿C command */
EnricoG 0:ffc2448b65ef 566 M24SR_BuildIBlockCommand ( M24SR_CMDSTRUCT_SELECTCCFILE, Command, &NbByte , pBuffer);
EnricoG 0:ffc2448b65ef 567
EnricoG 0:ffc2448b65ef 568 /* send the request */
EnricoG 0:ffc2448b65ef 569 errchk( M24SR_SendI2Ccommand ( NbByte , pBuffer ));
EnricoG 0:ffc2448b65ef 570 errchk( M24SR_IsAnswerReady ( ));
EnricoG 0:ffc2448b65ef 571 /* read the response */
EnricoG 0:ffc2448b65ef 572 errchk( M24SR_ReceiveI2Cresponse ( NbByteToRead , pBuffer ));
EnricoG 0:ffc2448b65ef 573
EnricoG 0:ffc2448b65ef 574 status = M24SR_IsCorrectCRC16Residue (pBuffer,NbByteToRead);
EnricoG 0:ffc2448b65ef 575 return status;
EnricoG 0:ffc2448b65ef 576
EnricoG 0:ffc2448b65ef 577 Error :
EnricoG 0:ffc2448b65ef 578 return M24SR_ERROR_I2CTIMEOUT;
EnricoG 0:ffc2448b65ef 579 }
EnricoG 0:ffc2448b65ef 580
EnricoG 0:ffc2448b65ef 581 /**
EnricoG 0:ffc2448b65ef 582 * @brief This function sends the SelectNDEFfile command
EnricoG 0:ffc2448b65ef 583 * @retval Status (SW1&SW2) : Status of the operation to complete.
EnricoG 0:ffc2448b65ef 584 * @retval M24SR_ERROR_I2CTIMEOUT : The I2C timeout occured.
EnricoG 0:ffc2448b65ef 585 */
EnricoG 0:ffc2448b65ef 586 uint16_t M24SR_SelectNDEFfile ( uc16 NDEFfileId )
EnricoG 0:ffc2448b65ef 587 {
EnricoG 0:ffc2448b65ef 588 uint8_t *pBuffer = uM24SRbuffer ,
EnricoG 0:ffc2448b65ef 589 NbByteToRead = M24SR_STATUSRESPONSE_NBBYTE;
EnricoG 0:ffc2448b65ef 590 uint8_t uLc = 0x02;
EnricoG 0:ffc2448b65ef 591 uint16_t status ;
EnricoG 0:ffc2448b65ef 592 uint16_t uP1P2 =0x000C,
EnricoG 0:ffc2448b65ef 593 NbByte;
EnricoG 0:ffc2448b65ef 594
EnricoG 0:ffc2448b65ef 595 /* build the command */
EnricoG 0:ffc2448b65ef 596 Command.Header.CLA = C_APDU_CLA_DEFAULT;
EnricoG 0:ffc2448b65ef 597 Command.Header.INS = C_APDU_SELECT_FILE;
EnricoG 0:ffc2448b65ef 598 /* copy the offset */
EnricoG 0:ffc2448b65ef 599 Command.Header.P1 = GETMSB (uP1P2 ) ;
EnricoG 0:ffc2448b65ef 600 Command.Header.P2 = GETLSB (uP1P2 ) ;
EnricoG 0:ffc2448b65ef 601 /* copy the number of byte of the data field */
EnricoG 0:ffc2448b65ef 602 Command.Body.LC = uLc ;
EnricoG 0:ffc2448b65ef 603 /* copy the offset */
EnricoG 0:ffc2448b65ef 604 Command.Body.pData[0] = GETMSB (NDEFfileId ) ;
EnricoG 0:ffc2448b65ef 605 Command.Body.pData[1] = GETLSB (NDEFfileId ) ;
EnricoG 0:ffc2448b65ef 606 /* build the I￿C command */
EnricoG 0:ffc2448b65ef 607 M24SR_BuildIBlockCommand ( M24SR_CMDSTRUCT_SELECTNDEFFILE, Command, &NbByte , pBuffer);
EnricoG 0:ffc2448b65ef 608
EnricoG 0:ffc2448b65ef 609 /* send the request */
EnricoG 0:ffc2448b65ef 610 errchk( M24SR_SendI2Ccommand ( NbByte , pBuffer ));
EnricoG 0:ffc2448b65ef 611 errchk( M24SR_IsAnswerReady ( ));
EnricoG 0:ffc2448b65ef 612 /* read the response */
EnricoG 0:ffc2448b65ef 613 errchk( M24SR_ReceiveI2Cresponse ( NbByteToRead , pBuffer ));
EnricoG 0:ffc2448b65ef 614
EnricoG 0:ffc2448b65ef 615 status = M24SR_IsCorrectCRC16Residue (pBuffer,NbByteToRead);
EnricoG 0:ffc2448b65ef 616 return status;
EnricoG 0:ffc2448b65ef 617
EnricoG 0:ffc2448b65ef 618 Error :
EnricoG 0:ffc2448b65ef 619 return M24SR_ERROR_I2CTIMEOUT;
EnricoG 0:ffc2448b65ef 620
EnricoG 0:ffc2448b65ef 621 }
EnricoG 0:ffc2448b65ef 622
EnricoG 0:ffc2448b65ef 623 /**
EnricoG 0:ffc2448b65ef 624 * @brief This function sends a read binary command
EnricoG 0:ffc2448b65ef 625 * @param Offset : first byte to read
EnricoG 0:ffc2448b65ef 626 * @param NbByteToRead : number of byte to read
EnricoG 0:ffc2448b65ef 627 * @param pBufferRead : pointer of the buffer read from the M24SR device
EnricoG 0:ffc2448b65ef 628 * @retval Status (SW1&SW2) : Status of the operation to complete.
EnricoG 0:ffc2448b65ef 629 * @retval M24SR_ERROR_I2CTIMEOUT : The I2C timeout occured.
EnricoG 0:ffc2448b65ef 630 */
EnricoG 0:ffc2448b65ef 631 uint16_t M24SR_ReadBinary ( uc16 Offset ,uc8 NbByteToRead , uint8_t *pBufferRead )
EnricoG 0:ffc2448b65ef 632 {
EnricoG 0:ffc2448b65ef 633 uint8_t *pBuffer = uM24SRbuffer ;
EnricoG 0:ffc2448b65ef 634 uint16_t status ;
EnricoG 0:ffc2448b65ef 635 uint16_t NbByte;
EnricoG 0:ffc2448b65ef 636
EnricoG 0:ffc2448b65ef 637 /* build the command */
EnricoG 0:ffc2448b65ef 638 Command.Header.CLA = C_APDU_CLA_DEFAULT;
EnricoG 0:ffc2448b65ef 639 Command.Header.INS = C_APDU_READ_BINARY;
EnricoG 0:ffc2448b65ef 640 /* copy the offset */
EnricoG 0:ffc2448b65ef 641 Command.Header.P1 = GETMSB (Offset ) ;
EnricoG 0:ffc2448b65ef 642 Command.Header.P2 = GETLSB (Offset ) ;
EnricoG 0:ffc2448b65ef 643 /* copy the number of byte to read */
EnricoG 0:ffc2448b65ef 644 Command.Body.LE = NbByteToRead ;
EnricoG 0:ffc2448b65ef 645
EnricoG 0:ffc2448b65ef 646 M24SR_BuildIBlockCommand ( M24SR_CMDSTRUCT_READBINARY, Command, &NbByte , pBuffer);
EnricoG 0:ffc2448b65ef 647
EnricoG 0:ffc2448b65ef 648 errchk( M24SR_SendI2Ccommand ( NbByte , pBuffer ));
EnricoG 0:ffc2448b65ef 649 errchk( M24SR_IsAnswerReady ( ));
EnricoG 0:ffc2448b65ef 650 errchk( M24SR_ReceiveI2Cresponse ( NbByteToRead + M24SR_STATUSRESPONSE_NBBYTE , pBuffer ));
EnricoG 0:ffc2448b65ef 651
EnricoG 0:ffc2448b65ef 652 status = M24SR_IsCorrectCRC16Residue (pBuffer,NbByteToRead+ M24SR_STATUSRESPONSE_NBBYTE);
EnricoG 0:ffc2448b65ef 653 /* retrieve the data without SW1 & SW2 as provided as return value of the function */
EnricoG 0:ffc2448b65ef 654 memcpy(pBufferRead ,&pBuffer[1],NbByteToRead);
EnricoG 0:ffc2448b65ef 655 return status;
EnricoG 0:ffc2448b65ef 656
EnricoG 0:ffc2448b65ef 657 Error :
EnricoG 0:ffc2448b65ef 658 return M24SR_ERROR_I2CTIMEOUT;
EnricoG 0:ffc2448b65ef 659
EnricoG 0:ffc2448b65ef 660 }
EnricoG 0:ffc2448b65ef 661
EnricoG 0:ffc2448b65ef 662
EnricoG 0:ffc2448b65ef 663
EnricoG 0:ffc2448b65ef 664 /**
EnricoG 0:ffc2448b65ef 665 * @brief This function sends a ST read binary command (no error if access is not inside NDEF file)
EnricoG 0:ffc2448b65ef 666 * @param Offset : first byte to read
EnricoG 0:ffc2448b65ef 667 * @param NbByteToRead : number of byte to read
EnricoG 0:ffc2448b65ef 668 * @param pBufferRead : pointer of the buffer read from the M24SR device
EnricoG 0:ffc2448b65ef 669 * @retval Status (SW1&SW2) : Status of the operation to complete.
EnricoG 0:ffc2448b65ef 670 * @retval M24SR_ERROR_I2CTIMEOUT : The I2C timeout occured.
EnricoG 0:ffc2448b65ef 671 */
EnricoG 0:ffc2448b65ef 672 uint16_t M24SR_STReadBinary ( uc16 Offset, uc8 NbByteToRead, uint8_t *pBufferRead )
EnricoG 0:ffc2448b65ef 673 {
EnricoG 0:ffc2448b65ef 674 uint8_t *pBuffer = uM24SRbuffer ;
EnricoG 0:ffc2448b65ef 675 uint16_t status ;
EnricoG 0:ffc2448b65ef 676 uint16_t NbByte;
EnricoG 0:ffc2448b65ef 677
EnricoG 0:ffc2448b65ef 678 /* build the command */
EnricoG 0:ffc2448b65ef 679 Command.Header.CLA = C_APDU_CLA_ST;
EnricoG 0:ffc2448b65ef 680 Command.Header.INS = C_APDU_READ_BINARY;
EnricoG 0:ffc2448b65ef 681 /* copy the offset */
EnricoG 0:ffc2448b65ef 682 Command.Header.P1 = GETMSB (Offset ) ;
EnricoG 0:ffc2448b65ef 683 Command.Header.P2 = GETLSB (Offset ) ;
EnricoG 0:ffc2448b65ef 684 /* copy the number of byte to read */
EnricoG 0:ffc2448b65ef 685 Command.Body.LE = NbByteToRead ;
EnricoG 0:ffc2448b65ef 686
EnricoG 0:ffc2448b65ef 687 M24SR_BuildIBlockCommand ( M24SR_CMDSTRUCT_READBINARY, Command, &NbByte , pBuffer);
EnricoG 0:ffc2448b65ef 688
EnricoG 0:ffc2448b65ef 689 errchk( M24SR_SendI2Ccommand ( NbByte , pBuffer ));
EnricoG 0:ffc2448b65ef 690 errchk( M24SR_IsAnswerReady ( ));
EnricoG 0:ffc2448b65ef 691 errchk( M24SR_ReceiveI2Cresponse ( NbByteToRead + M24SR_STATUSRESPONSE_NBBYTE , pBuffer ));
EnricoG 0:ffc2448b65ef 692
EnricoG 0:ffc2448b65ef 693 status = M24SR_IsCorrectCRC16Residue (pBuffer,NbByteToRead+ M24SR_STATUSRESPONSE_NBBYTE);
EnricoG 0:ffc2448b65ef 694 /* retrieve the data without SW1 & SW2 as provided as return value of the function */
EnricoG 0:ffc2448b65ef 695 memcpy(pBufferRead ,&pBuffer[1],NbByteToRead);
EnricoG 0:ffc2448b65ef 696 return status;
EnricoG 0:ffc2448b65ef 697
EnricoG 0:ffc2448b65ef 698 Error :
EnricoG 0:ffc2448b65ef 699 return M24SR_ERROR_I2CTIMEOUT;
EnricoG 0:ffc2448b65ef 700
EnricoG 0:ffc2448b65ef 701 }
EnricoG 0:ffc2448b65ef 702
EnricoG 0:ffc2448b65ef 703 /**
EnricoG 0:ffc2448b65ef 704 * @brief This function sends a Update binary command
EnricoG 0:ffc2448b65ef 705 * @param Offset : first byte to read
EnricoG 0:ffc2448b65ef 706 * @param NbByteToWrite : number of byte to write
EnricoG 0:ffc2448b65ef 707 * @param pBufferRead : pointer of the buffer read from the M24SR device
EnricoG 0:ffc2448b65ef 708 * @retval Status (SW1&SW2) : Status of the operation to complete.
EnricoG 0:ffc2448b65ef 709 * @retval M24SR_ERROR_I2CTIMEOUT : The I2C timeout occured.
EnricoG 0:ffc2448b65ef 710 */
EnricoG 0:ffc2448b65ef 711 uint16_t M24SR_UpdateBinary ( uc16 Offset ,uc8 NbByteToWrite,uc8 *pDataToWrite )
EnricoG 0:ffc2448b65ef 712 {
EnricoG 0:ffc2448b65ef 713 uint8_t *pBuffer = uM24SRbuffer ;
EnricoG 0:ffc2448b65ef 714 uint16_t status ;
EnricoG 0:ffc2448b65ef 715 uint16_t NbByte;
EnricoG 0:ffc2448b65ef 716
EnricoG 0:ffc2448b65ef 717 /* build the command */
EnricoG 0:ffc2448b65ef 718 Command.Header.CLA = C_APDU_CLA_DEFAULT;
EnricoG 0:ffc2448b65ef 719 Command.Header.INS = C_APDU_UPDATE_BINARY;
EnricoG 0:ffc2448b65ef 720 /* copy the offset */
EnricoG 0:ffc2448b65ef 721 Command.Header.P1 = GETMSB (Offset ) ;
EnricoG 0:ffc2448b65ef 722 Command.Header.P2 = GETLSB (Offset ) ;
EnricoG 0:ffc2448b65ef 723 /* copy the number of byte of the data field */
EnricoG 0:ffc2448b65ef 724 Command.Body.LC = NbByteToWrite ;
EnricoG 0:ffc2448b65ef 725 /* copy the File Id */
EnricoG 0:ffc2448b65ef 726 memcpy(Command.Body.pData ,pDataToWrite, NbByteToWrite );
EnricoG 0:ffc2448b65ef 727
EnricoG 0:ffc2448b65ef 728 M24SR_BuildIBlockCommand ( M24SR_CMDSTRUCT_UPDATEBINARY, Command, &NbByte , pBuffer);
EnricoG 0:ffc2448b65ef 729
EnricoG 0:ffc2448b65ef 730 errchk( M24SR_SendI2Ccommand ( NbByte , pBuffer ));
EnricoG 0:ffc2448b65ef 731 errchk( M24SR_IsAnswerReady ( ));
EnricoG 0:ffc2448b65ef 732 errchk( M24SR_ReceiveI2Cresponse ( M24SR_STATUSRESPONSE_NBBYTE , pBuffer ));
EnricoG 0:ffc2448b65ef 733 /* if the response is a Watiting frame extenstion request */
EnricoG 0:ffc2448b65ef 734 if (IsSBlock (pBuffer) == M24SR_STATUS_SUCCESS)
EnricoG 0:ffc2448b65ef 735 {
EnricoG 0:ffc2448b65ef 736 /*check the CRC */
EnricoG 0:ffc2448b65ef 737 if (M24SR_IsCorrectCRC16Residue (pBuffer , M24SR_WATINGTIMEEXTRESPONSE_NBBYTE) != M24SR_ERROR_CRC)
EnricoG 0:ffc2448b65ef 738 {
EnricoG 0:ffc2448b65ef 739 /* send the FrameExension response*/
EnricoG 0:ffc2448b65ef 740 status = M24SR_FWTExtension ( pBuffer [M24SR_OFFSET_PCB+1] );
EnricoG 0:ffc2448b65ef 741 }
EnricoG 0:ffc2448b65ef 742 }
EnricoG 0:ffc2448b65ef 743 else
EnricoG 0:ffc2448b65ef 744 {
EnricoG 0:ffc2448b65ef 745 status = M24SR_IsCorrectCRC16Residue (pBuffer, M24SR_STATUSRESPONSE_NBBYTE);
EnricoG 0:ffc2448b65ef 746 }
EnricoG 0:ffc2448b65ef 747
EnricoG 0:ffc2448b65ef 748 return status;
EnricoG 0:ffc2448b65ef 749
EnricoG 0:ffc2448b65ef 750
EnricoG 0:ffc2448b65ef 751 Error :
EnricoG 0:ffc2448b65ef 752 return M24SR_ERROR_I2CTIMEOUT;
EnricoG 0:ffc2448b65ef 753 }
EnricoG 0:ffc2448b65ef 754
EnricoG 0:ffc2448b65ef 755
EnricoG 0:ffc2448b65ef 756 /**
EnricoG 0:ffc2448b65ef 757 * @brief This function sends the Verify command
EnricoG 0:ffc2448b65ef 758 * @param uPwdId : PasswordId ( 0x0001 : Read NDEF pwd or 0x0002 : Write NDEF pwd or 0x0003 : I2C pwd)
EnricoG 0:ffc2448b65ef 759 * @param NbPwdByte : Number of byte ( 0x00 or 0x10)
EnricoG 0:ffc2448b65ef 760 * @param pPwd : pointer on the passwaord
EnricoG 0:ffc2448b65ef 761 * @retval Status (SW1&SW2) : Status of the operation to complete.
EnricoG 0:ffc2448b65ef 762 * @retval M24SR_ERROR_I2CTIMEOUT : The I2C timeout occured.
EnricoG 0:ffc2448b65ef 763 */
EnricoG 0:ffc2448b65ef 764 uint16_t M24SR_Verify ( uc16 uPwdId,uc8 NbPwdByte ,uc8 *pPwd )
EnricoG 0:ffc2448b65ef 765 {
EnricoG 0:ffc2448b65ef 766 uint8_t *pBuffer = uM24SRbuffer ;
EnricoG 0:ffc2448b65ef 767 uint16_t status = 0x0000 ;
EnricoG 0:ffc2448b65ef 768 uint16_t NbByte;
EnricoG 0:ffc2448b65ef 769
EnricoG 0:ffc2448b65ef 770 /*check the parameters */
EnricoG 0:ffc2448b65ef 771 if (uPwdId > 0x0003)
EnricoG 0:ffc2448b65ef 772 {
EnricoG 0:ffc2448b65ef 773 return M24SR_ERROR_PARAMETER;
EnricoG 0:ffc2448b65ef 774 }
EnricoG 0:ffc2448b65ef 775 if ( (NbPwdByte != 0x00) && (NbPwdByte != 0x10))
EnricoG 0:ffc2448b65ef 776 {
EnricoG 0:ffc2448b65ef 777 return M24SR_ERROR_PARAMETER;
EnricoG 0:ffc2448b65ef 778 }
EnricoG 0:ffc2448b65ef 779
EnricoG 0:ffc2448b65ef 780 /* build the command */
EnricoG 0:ffc2448b65ef 781 Command.Header.CLA = C_APDU_CLA_DEFAULT;
EnricoG 0:ffc2448b65ef 782 Command.Header.INS = C_APDU_VERIFY;
EnricoG 0:ffc2448b65ef 783 /* copy the Password Id */
EnricoG 0:ffc2448b65ef 784 Command.Header.P1 = GETMSB (uPwdId ) ;
EnricoG 0:ffc2448b65ef 785 Command.Header.P2 = GETLSB (uPwdId ) ;
EnricoG 0:ffc2448b65ef 786 /* copy the number of byte of the data field */
EnricoG 0:ffc2448b65ef 787 Command.Body.LC = NbPwdByte ;
EnricoG 0:ffc2448b65ef 788
EnricoG 0:ffc2448b65ef 789 if (NbPwdByte == 0x10)
EnricoG 0:ffc2448b65ef 790 {
EnricoG 0:ffc2448b65ef 791 /* copy the password */
EnricoG 0:ffc2448b65ef 792 memcpy(Command.Body.pData, pPwd, NbPwdByte);
EnricoG 0:ffc2448b65ef 793 /* build the I￿C command */
EnricoG 0:ffc2448b65ef 794 M24SR_BuildIBlockCommand ( M24SR_CMDSTRUCT_VERIFYBINARYWITHPWD, Command, &NbByte , pBuffer);
EnricoG 0:ffc2448b65ef 795 }
EnricoG 0:ffc2448b65ef 796 else
EnricoG 0:ffc2448b65ef 797 {
EnricoG 0:ffc2448b65ef 798 /* build the I￿C command */
EnricoG 0:ffc2448b65ef 799 M24SR_BuildIBlockCommand ( M24SR_CMDSTRUCT_VERIFYBINARYWOPWD, Command, &NbByte , pBuffer);
EnricoG 0:ffc2448b65ef 800 }
EnricoG 0:ffc2448b65ef 801
EnricoG 0:ffc2448b65ef 802 /* send the request */
EnricoG 0:ffc2448b65ef 803 errchk( M24SR_SendI2Ccommand ( NbByte , pBuffer ));
EnricoG 0:ffc2448b65ef 804 /* wait for answer ready */
EnricoG 0:ffc2448b65ef 805 errchk( M24SR_IsAnswerReady ( ));
EnricoG 0:ffc2448b65ef 806 /* read the response */
EnricoG 0:ffc2448b65ef 807 errchk( M24SR_ReceiveI2Cresponse ( M24SR_STATUSRESPONSE_NBBYTE , pBuffer ));
EnricoG 0:ffc2448b65ef 808
EnricoG 0:ffc2448b65ef 809 status = M24SR_IsCorrectCRC16Residue (pBuffer, M24SR_STATUSRESPONSE_NBBYTE);
EnricoG 0:ffc2448b65ef 810 return status;
EnricoG 0:ffc2448b65ef 811
EnricoG 0:ffc2448b65ef 812 Error :
EnricoG 0:ffc2448b65ef 813 return M24SR_ERROR_I2CTIMEOUT;
EnricoG 0:ffc2448b65ef 814 }
EnricoG 0:ffc2448b65ef 815
EnricoG 0:ffc2448b65ef 816
EnricoG 0:ffc2448b65ef 817 /**
EnricoG 0:ffc2448b65ef 818 * @brief This function sends the ChangeReferenceData command
EnricoG 0:ffc2448b65ef 819 * @param uPwdId : PasswordId ( 0x0001 : Read NDEF pwd or 0x0002 : Write NDEF pwd or 0x0003 : I2C pwd)
EnricoG 0:ffc2448b65ef 820 * @param pPwd : pointer on the passwaord
EnricoG 0:ffc2448b65ef 821 * @retval Status (SW1&SW2) : Status of the operation to complete.
EnricoG 0:ffc2448b65ef 822 * @retval M24SR_ERROR_I2CTIMEOUT : The I2C timeout occured.
EnricoG 0:ffc2448b65ef 823 */
EnricoG 0:ffc2448b65ef 824 uint16_t M24SR_ChangeReferenceData ( uc16 uPwdId,uc8 *pPwd )
EnricoG 0:ffc2448b65ef 825 {
EnricoG 0:ffc2448b65ef 826 uint8_t *pBuffer = uM24SRbuffer;
EnricoG 0:ffc2448b65ef 827 uint16_t status ;
EnricoG 0:ffc2448b65ef 828 uint16_t NbByte;
EnricoG 0:ffc2448b65ef 829
EnricoG 0:ffc2448b65ef 830 /*check the parameters */
EnricoG 0:ffc2448b65ef 831 if (uPwdId > 0x0003)
EnricoG 0:ffc2448b65ef 832 {
EnricoG 0:ffc2448b65ef 833 return M24SR_ERROR_PARAMETER;
EnricoG 0:ffc2448b65ef 834 }
EnricoG 0:ffc2448b65ef 835
EnricoG 0:ffc2448b65ef 836 /* build the command */
EnricoG 0:ffc2448b65ef 837 Command.Header.CLA = C_APDU_CLA_DEFAULT;
EnricoG 0:ffc2448b65ef 838 Command.Header.INS = C_APDU_CHANGE;
EnricoG 0:ffc2448b65ef 839 /* copy the Password Id */
EnricoG 0:ffc2448b65ef 840 Command.Header.P1 = GETMSB (uPwdId ) ;
EnricoG 0:ffc2448b65ef 841 Command.Header.P2 = GETLSB (uPwdId ) ;
EnricoG 0:ffc2448b65ef 842 /* copy the number of byte of the data field */
EnricoG 0:ffc2448b65ef 843 Command.Body.LC = M24SR_PASSWORD_NBBYTE ;
EnricoG 0:ffc2448b65ef 844 /* copy the password */
EnricoG 0:ffc2448b65ef 845 memcpy(Command.Body.pData, pPwd, M24SR_PASSWORD_NBBYTE);
EnricoG 0:ffc2448b65ef 846 /* build the I￿C command */
EnricoG 0:ffc2448b65ef 847 M24SR_BuildIBlockCommand ( M24SR_CMDSTRUCT_CHANGEREFDATA, Command, &NbByte , pBuffer);
EnricoG 0:ffc2448b65ef 848
EnricoG 0:ffc2448b65ef 849
EnricoG 0:ffc2448b65ef 850 /* send the request */
EnricoG 0:ffc2448b65ef 851 errchk( M24SR_SendI2Ccommand ( NbByte , pBuffer ));
EnricoG 0:ffc2448b65ef 852 errchk( M24SR_IsAnswerReady ( ));
EnricoG 0:ffc2448b65ef 853 /* read the response */
EnricoG 0:ffc2448b65ef 854 errchk( M24SR_ReceiveI2Cresponse ( M24SR_STATUSRESPONSE_NBBYTE , pBuffer ));
EnricoG 0:ffc2448b65ef 855
EnricoG 0:ffc2448b65ef 856 status = M24SR_IsCorrectCRC16Residue (pBuffer, M24SR_STATUSRESPONSE_NBBYTE);
EnricoG 0:ffc2448b65ef 857 return status;
EnricoG 0:ffc2448b65ef 858
EnricoG 0:ffc2448b65ef 859 Error :
EnricoG 0:ffc2448b65ef 860 return M24SR_ERROR_I2CTIMEOUT;
EnricoG 0:ffc2448b65ef 861 }
EnricoG 0:ffc2448b65ef 862
EnricoG 0:ffc2448b65ef 863
EnricoG 0:ffc2448b65ef 864 /**
EnricoG 0:ffc2448b65ef 865 * @brief This function sends the EnableVerificationRequirement command
EnricoG 0:ffc2448b65ef 866 * @param uReadOrWrite : enable the read or write protection ( 0x0001 : Read or 0x0002 : Write )
EnricoG 0:ffc2448b65ef 867 * @retval Status (SW1&SW2) : Status of the operation to complete.
EnricoG 0:ffc2448b65ef 868 * @retval M24SR_ERROR_I2CTIMEOUT : The I2C timeout occured.
EnricoG 0:ffc2448b65ef 869 */
EnricoG 0:ffc2448b65ef 870 uint16_t M24SR_EnableVerificationRequirement ( uc16 uReadOrWrite )
EnricoG 0:ffc2448b65ef 871 {
EnricoG 0:ffc2448b65ef 872 uint8_t *pBuffer = uM24SRbuffer;
EnricoG 0:ffc2448b65ef 873 uint16_t status ;
EnricoG 0:ffc2448b65ef 874 uint16_t NbByte;
EnricoG 0:ffc2448b65ef 875
EnricoG 0:ffc2448b65ef 876 /*check the parameters */
EnricoG 0:ffc2448b65ef 877 if ( (uReadOrWrite != 0x0001) && (uReadOrWrite != 0x0002))
EnricoG 0:ffc2448b65ef 878 {
EnricoG 0:ffc2448b65ef 879 return M24SR_ERROR_PARAMETER;
EnricoG 0:ffc2448b65ef 880 }
EnricoG 0:ffc2448b65ef 881
EnricoG 0:ffc2448b65ef 882 /* build the command */
EnricoG 0:ffc2448b65ef 883 Command.Header.CLA = C_APDU_CLA_DEFAULT;
EnricoG 0:ffc2448b65ef 884 Command.Header.INS = C_APDU_ENABLE;
EnricoG 0:ffc2448b65ef 885 /* copy the Password Id */
EnricoG 0:ffc2448b65ef 886 Command.Header.P1 = GETMSB (uReadOrWrite ) ;
EnricoG 0:ffc2448b65ef 887 Command.Header.P2 = GETLSB (uReadOrWrite ) ;
EnricoG 0:ffc2448b65ef 888 /* build the I￿C command */
EnricoG 0:ffc2448b65ef 889 M24SR_BuildIBlockCommand ( M24SR_CMDSTRUCT_ENABLEVERIFREQ, Command, &NbByte , pBuffer);
EnricoG 0:ffc2448b65ef 890
EnricoG 0:ffc2448b65ef 891 /* send the request */
EnricoG 0:ffc2448b65ef 892 errchk( M24SR_SendI2Ccommand ( NbByte , pBuffer ));
EnricoG 0:ffc2448b65ef 893 /* The right access to be updated in EEPROM need at least 6ms */
EnricoG 0:ffc2448b65ef 894 errchk( M24SR_IsAnswerReady ( ));
EnricoG 0:ffc2448b65ef 895 /* read the response */
EnricoG 0:ffc2448b65ef 896 errchk( M24SR_ReceiveI2Cresponse ( M24SR_STATUSRESPONSE_NBBYTE , pBuffer ));
EnricoG 0:ffc2448b65ef 897
EnricoG 0:ffc2448b65ef 898 status = M24SR_IsCorrectCRC16Residue (pBuffer, M24SR_STATUSRESPONSE_NBBYTE);
EnricoG 0:ffc2448b65ef 899 return status;
EnricoG 0:ffc2448b65ef 900
EnricoG 0:ffc2448b65ef 901 Error :
EnricoG 0:ffc2448b65ef 902 return M24SR_ERROR_I2CTIMEOUT;
EnricoG 0:ffc2448b65ef 903 }
EnricoG 0:ffc2448b65ef 904
EnricoG 0:ffc2448b65ef 905 /**
EnricoG 0:ffc2448b65ef 906 * @brief This function sends the DisableVerificationRequirement command
EnricoG 0:ffc2448b65ef 907 * @param uReadOrWrite : enable the read or write protection ( 0x0001 : Read or 0x0002 : Write )
EnricoG 0:ffc2448b65ef 908 * @retval Status (SW1&SW2) : Status of the operation to complete.
EnricoG 0:ffc2448b65ef 909 * @retval M24SR_ERROR_I2CTIMEOUT : The I2C timeout occured.
EnricoG 0:ffc2448b65ef 910 */
EnricoG 0:ffc2448b65ef 911 uint16_t M24SR_DisableVerificationRequirement ( uc16 uReadOrWrite )
EnricoG 0:ffc2448b65ef 912 {
EnricoG 0:ffc2448b65ef 913 uint8_t *pBuffer = uM24SRbuffer;
EnricoG 0:ffc2448b65ef 914 uint16_t status ;
EnricoG 0:ffc2448b65ef 915 uint16_t NbByte;
EnricoG 0:ffc2448b65ef 916
EnricoG 0:ffc2448b65ef 917 /*check the parameters */
EnricoG 0:ffc2448b65ef 918 if ( (uReadOrWrite != 0x0001) && (uReadOrWrite != 0x0002))
EnricoG 0:ffc2448b65ef 919 {
EnricoG 0:ffc2448b65ef 920 return M24SR_ERROR_PARAMETER;
EnricoG 0:ffc2448b65ef 921 }
EnricoG 0:ffc2448b65ef 922
EnricoG 0:ffc2448b65ef 923 /* build the command */
EnricoG 0:ffc2448b65ef 924 Command.Header.CLA = C_APDU_CLA_DEFAULT;
EnricoG 0:ffc2448b65ef 925 Command.Header.INS = C_APDU_DISABLE;
EnricoG 0:ffc2448b65ef 926 /* copy the Password Id */
EnricoG 0:ffc2448b65ef 927 Command.Header.P1 = GETMSB (uReadOrWrite ) ;
EnricoG 0:ffc2448b65ef 928 Command.Header.P2 = GETLSB (uReadOrWrite ) ;
EnricoG 0:ffc2448b65ef 929 /* build the I￿C command */
EnricoG 0:ffc2448b65ef 930 M24SR_BuildIBlockCommand ( M24SR_CMDSTRUCT_DISABLEVERIFREQ, Command, &NbByte , pBuffer);
EnricoG 0:ffc2448b65ef 931
EnricoG 0:ffc2448b65ef 932 /* send the request */
EnricoG 0:ffc2448b65ef 933 errchk( M24SR_SendI2Ccommand ( NbByte , pBuffer ));
EnricoG 0:ffc2448b65ef 934 /* The right access to be updated in EEPROM need at least 6ms */
EnricoG 0:ffc2448b65ef 935 errchk( M24SR_IsAnswerReady ( ));
EnricoG 0:ffc2448b65ef 936 /* read the response */
EnricoG 0:ffc2448b65ef 937 errchk( M24SR_ReceiveI2Cresponse ( M24SR_STATUSRESPONSE_NBBYTE , pBuffer ));
EnricoG 0:ffc2448b65ef 938
EnricoG 0:ffc2448b65ef 939 status = M24SR_IsCorrectCRC16Residue (pBuffer, M24SR_STATUSRESPONSE_NBBYTE);
EnricoG 0:ffc2448b65ef 940 return status;
EnricoG 0:ffc2448b65ef 941
EnricoG 0:ffc2448b65ef 942 Error :
EnricoG 0:ffc2448b65ef 943 return M24SR_ERROR_I2CTIMEOUT;
EnricoG 0:ffc2448b65ef 944 }
EnricoG 0:ffc2448b65ef 945
EnricoG 0:ffc2448b65ef 946 /**
EnricoG 0:ffc2448b65ef 947 * @brief This function sends the EnablePermananentState command
EnricoG 0:ffc2448b65ef 948 * @param uReadOrWrite : enable the read or write protection ( 0x0001 : Read or 0x0002 : Write )
EnricoG 0:ffc2448b65ef 949 * @retval Status (SW1&SW2) : Status of the operation to complete.
EnricoG 0:ffc2448b65ef 950 * @retval M24SR_ERROR_I2CTIMEOUT : The I2C timeout occured.
EnricoG 0:ffc2448b65ef 951 */
EnricoG 0:ffc2448b65ef 952 uint16_t M24SR_EnablePermanentState ( uc16 uReadOrWrite )
EnricoG 0:ffc2448b65ef 953 {
EnricoG 0:ffc2448b65ef 954 uint8_t *pBuffer = uM24SRbuffer;
EnricoG 0:ffc2448b65ef 955 uint16_t status ;
EnricoG 0:ffc2448b65ef 956 uint16_t NbByte;
EnricoG 0:ffc2448b65ef 957
EnricoG 0:ffc2448b65ef 958 /*check the parameters */
EnricoG 0:ffc2448b65ef 959 if ( (uReadOrWrite != 0x0001) && (uReadOrWrite != 0x0002))
EnricoG 0:ffc2448b65ef 960 {
EnricoG 0:ffc2448b65ef 961 return M24SR_ERROR_PARAMETER;
EnricoG 0:ffc2448b65ef 962 }
EnricoG 0:ffc2448b65ef 963
EnricoG 0:ffc2448b65ef 964 /* build the command */
EnricoG 0:ffc2448b65ef 965 Command.Header.CLA = C_APDU_CLA_ST;
EnricoG 0:ffc2448b65ef 966 Command.Header.INS = C_APDU_ENABLE;
EnricoG 0:ffc2448b65ef 967 /* copy the Password Id */
EnricoG 0:ffc2448b65ef 968 Command.Header.P1 = GETMSB (uReadOrWrite ) ;
EnricoG 0:ffc2448b65ef 969 Command.Header.P2 = GETLSB (uReadOrWrite ) ;
EnricoG 0:ffc2448b65ef 970 /* build the I￿C command */
EnricoG 0:ffc2448b65ef 971 M24SR_BuildIBlockCommand ( M24SR_CMDSTRUCT_ENABLEVERIFREQ, Command, &NbByte , pBuffer);
EnricoG 0:ffc2448b65ef 972
EnricoG 0:ffc2448b65ef 973 /* send the request */
EnricoG 0:ffc2448b65ef 974 errchk( M24SR_SendI2Ccommand ( NbByte , pBuffer ));
EnricoG 0:ffc2448b65ef 975 errchk( M24SR_IsAnswerReady ( ));
EnricoG 0:ffc2448b65ef 976 /* read the response */
EnricoG 0:ffc2448b65ef 977 errchk( M24SR_ReceiveI2Cresponse ( M24SR_STATUSRESPONSE_NBBYTE , pBuffer ));
EnricoG 0:ffc2448b65ef 978
EnricoG 0:ffc2448b65ef 979 status = M24SR_IsCorrectCRC16Residue (pBuffer, M24SR_STATUSRESPONSE_NBBYTE);
EnricoG 0:ffc2448b65ef 980 return status;
EnricoG 0:ffc2448b65ef 981
EnricoG 0:ffc2448b65ef 982 Error :
EnricoG 0:ffc2448b65ef 983 return M24SR_ERROR_I2CTIMEOUT;
EnricoG 0:ffc2448b65ef 984 }
EnricoG 0:ffc2448b65ef 985
EnricoG 0:ffc2448b65ef 986 /**
EnricoG 0:ffc2448b65ef 987 * @brief This function sends the DisablePermanentState command
EnricoG 0:ffc2448b65ef 988 * @param uReadOrWrite : enable the read or write protection ( 0x0001 : Read or 0x0002 : Write )
EnricoG 0:ffc2448b65ef 989 * @retval Status (SW1&SW2) : Status of the operation to complete.
EnricoG 0:ffc2448b65ef 990 * @retval M24SR_ERROR_I2CTIMEOUT : The I2C timeout occured.
EnricoG 0:ffc2448b65ef 991 */
EnricoG 0:ffc2448b65ef 992 uint16_t M24SR_DisablePermanentState ( uc16 uReadOrWrite )
EnricoG 0:ffc2448b65ef 993 {
EnricoG 0:ffc2448b65ef 994 uint8_t *pBuffer = uM24SRbuffer;
EnricoG 0:ffc2448b65ef 995 uint16_t status ;
EnricoG 0:ffc2448b65ef 996 uint16_t NbByte;
EnricoG 0:ffc2448b65ef 997
EnricoG 0:ffc2448b65ef 998 /*check the parameters */
EnricoG 0:ffc2448b65ef 999 if ( (uReadOrWrite != 0x0001) && (uReadOrWrite != 0x0002))
EnricoG 0:ffc2448b65ef 1000 {
EnricoG 0:ffc2448b65ef 1001 return M24SR_ERROR_PARAMETER;
EnricoG 0:ffc2448b65ef 1002 }
EnricoG 0:ffc2448b65ef 1003
EnricoG 0:ffc2448b65ef 1004 /* build the command */
EnricoG 0:ffc2448b65ef 1005 Command.Header.CLA = C_APDU_CLA_ST;
EnricoG 0:ffc2448b65ef 1006 Command.Header.INS = C_APDU_DISABLE;
EnricoG 0:ffc2448b65ef 1007 /* copy the Password Id */
EnricoG 0:ffc2448b65ef 1008 Command.Header.P1 = GETMSB (uReadOrWrite ) ;
EnricoG 0:ffc2448b65ef 1009 Command.Header.P2 = GETLSB (uReadOrWrite ) ;
EnricoG 0:ffc2448b65ef 1010 /* build the I￿C command */
EnricoG 0:ffc2448b65ef 1011 M24SR_BuildIBlockCommand ( M24SR_CMDSTRUCT_DISABLEVERIFREQ, Command, &NbByte , pBuffer);
EnricoG 0:ffc2448b65ef 1012
EnricoG 0:ffc2448b65ef 1013 /* send the request */
EnricoG 0:ffc2448b65ef 1014 errchk( M24SR_SendI2Ccommand ( NbByte , pBuffer ));
EnricoG 0:ffc2448b65ef 1015 errchk( M24SR_IsAnswerReady ( ));
EnricoG 0:ffc2448b65ef 1016 /* read the response */
EnricoG 0:ffc2448b65ef 1017 errchk( M24SR_ReceiveI2Cresponse ( M24SR_STATUSRESPONSE_NBBYTE , pBuffer ));
EnricoG 0:ffc2448b65ef 1018
EnricoG 0:ffc2448b65ef 1019 status = M24SR_IsCorrectCRC16Residue (pBuffer, M24SR_STATUSRESPONSE_NBBYTE);
EnricoG 0:ffc2448b65ef 1020 return status;
EnricoG 0:ffc2448b65ef 1021
EnricoG 0:ffc2448b65ef 1022 Error :
EnricoG 0:ffc2448b65ef 1023 return M24SR_ERROR_I2CTIMEOUT;
EnricoG 0:ffc2448b65ef 1024 }
EnricoG 0:ffc2448b65ef 1025
EnricoG 0:ffc2448b65ef 1026 /**
EnricoG 0:ffc2448b65ef 1027 * @brief This function generates a interrupt on GPO pin
EnricoG 0:ffc2448b65ef 1028 * @param None
EnricoG 0:ffc2448b65ef 1029 * @retval Status (SW1&SW2) : Status of the operation to complete.
EnricoG 0:ffc2448b65ef 1030 * @retval M24SR_ERROR_I2CTIMEOUT : The I2C timeout occured.
EnricoG 0:ffc2448b65ef 1031 */
EnricoG 0:ffc2448b65ef 1032 uint16_t M24SR_SendInterrupt ( void )
EnricoG 0:ffc2448b65ef 1033 {
EnricoG 0:ffc2448b65ef 1034 uint8_t *pBuffer = uM24SRbuffer;
EnricoG 0:ffc2448b65ef 1035 uint16_t uP1P2 =0x001E;
EnricoG 0:ffc2448b65ef 1036 uint16_t status ;
EnricoG 0:ffc2448b65ef 1037 uint16_t NbByte;
EnricoG 0:ffc2448b65ef 1038
EnricoG 0:ffc2448b65ef 1039 status = M24SR_ManageI2CGPO( INTERRUPT);
EnricoG 0:ffc2448b65ef 1040
EnricoG 0:ffc2448b65ef 1041 /* build the command */
EnricoG 0:ffc2448b65ef 1042 Command.Header.CLA = C_APDU_CLA_ST;
EnricoG 0:ffc2448b65ef 1043 Command.Header.INS = C_APDU_INTERRUPT;
EnricoG 0:ffc2448b65ef 1044 /* copy the Password Id */
EnricoG 0:ffc2448b65ef 1045 Command.Header.P1 = GETMSB (uP1P2 ) ;
EnricoG 0:ffc2448b65ef 1046 Command.Header.P2 = GETLSB (uP1P2 ) ;
EnricoG 0:ffc2448b65ef 1047 Command.Body.LC = 0x00 ;
EnricoG 0:ffc2448b65ef 1048 /* build the I￿C command */
EnricoG 0:ffc2448b65ef 1049 M24SR_BuildIBlockCommand ( M24SR_CMDSTRUCT_SENDINTERRUPT, Command, &NbByte , pBuffer);
EnricoG 0:ffc2448b65ef 1050
EnricoG 0:ffc2448b65ef 1051 /* send the request */
EnricoG 0:ffc2448b65ef 1052 errchk( M24SR_SendI2Ccommand ( NbByte , pBuffer ));
EnricoG 0:ffc2448b65ef 1053 errchk( M24SR_IsAnswerReady ( ));
EnricoG 0:ffc2448b65ef 1054 /* read the response */
EnricoG 0:ffc2448b65ef 1055 errchk( M24SR_ReceiveI2Cresponse ( M24SR_STATUSRESPONSE_NBBYTE , pBuffer ));
EnricoG 0:ffc2448b65ef 1056
EnricoG 0:ffc2448b65ef 1057 status = M24SR_IsCorrectCRC16Residue (pBuffer, M24SR_STATUSRESPONSE_NBBYTE);
EnricoG 0:ffc2448b65ef 1058 return status;
EnricoG 0:ffc2448b65ef 1059
EnricoG 0:ffc2448b65ef 1060 Error :
EnricoG 0:ffc2448b65ef 1061 return M24SR_ERROR_I2CTIMEOUT;
EnricoG 0:ffc2448b65ef 1062 }
EnricoG 0:ffc2448b65ef 1063
EnricoG 0:ffc2448b65ef 1064 /**
EnricoG 0:ffc2448b65ef 1065 * @brief This function force GPO pin to low state or high Z
EnricoG 0:ffc2448b65ef 1066 * @param uSetOrReset : select if GPO must be low (reset) or HiZ
EnricoG 0:ffc2448b65ef 1067 * @retval Status (SW1&SW2) : Status of the operation to complete.
EnricoG 0:ffc2448b65ef 1068 * @retval M24SR_ERROR_I2CTIMEOUT : The I2C timeout occured.
EnricoG 0:ffc2448b65ef 1069 */
EnricoG 0:ffc2448b65ef 1070 uint16_t M24SR_StateControl ( uc8 uSetOrReset )
EnricoG 0:ffc2448b65ef 1071 {
EnricoG 0:ffc2448b65ef 1072 uint8_t *pBuffer = uM24SRbuffer;
EnricoG 0:ffc2448b65ef 1073 uint16_t uP1P2 =0x001F;
EnricoG 0:ffc2448b65ef 1074 uint16_t status ;
EnricoG 0:ffc2448b65ef 1075 uint16_t NbByte;
EnricoG 0:ffc2448b65ef 1076
EnricoG 0:ffc2448b65ef 1077 /*check the parameters */
EnricoG 0:ffc2448b65ef 1078 if ( (uSetOrReset != 0x01) && (uSetOrReset != 0x00))
EnricoG 0:ffc2448b65ef 1079 {
EnricoG 0:ffc2448b65ef 1080 return M24SR_ERROR_PARAMETER;
EnricoG 0:ffc2448b65ef 1081 }
EnricoG 0:ffc2448b65ef 1082
EnricoG 0:ffc2448b65ef 1083 status = M24SR_ManageI2CGPO( STATE_CONTROL);
EnricoG 0:ffc2448b65ef 1084
EnricoG 0:ffc2448b65ef 1085 /* build the command */
EnricoG 0:ffc2448b65ef 1086 Command.Header.CLA = C_APDU_CLA_ST;
EnricoG 0:ffc2448b65ef 1087 Command.Header.INS = C_APDU_INTERRUPT;
EnricoG 0:ffc2448b65ef 1088 /* copy the Password Id */
EnricoG 0:ffc2448b65ef 1089 Command.Header.P1 = GETMSB (uP1P2 ) ;
EnricoG 0:ffc2448b65ef 1090 Command.Header.P2 = GETLSB (uP1P2 ) ;
EnricoG 0:ffc2448b65ef 1091 Command.Body.LC = 0x01 ;
EnricoG 0:ffc2448b65ef 1092 /* copy the data */
EnricoG 0:ffc2448b65ef 1093 memcpy(Command.Body.pData , &uSetOrReset, 0x01 );
EnricoG 0:ffc2448b65ef 1094 //Command.Body.LE = 0x00 ;
EnricoG 0:ffc2448b65ef 1095 /* build the I￿C command */
EnricoG 0:ffc2448b65ef 1096 M24SR_BuildIBlockCommand ( M24SR_CMDSTRUCT_GPOSTATE, Command, &NbByte , pBuffer);
EnricoG 0:ffc2448b65ef 1097
EnricoG 0:ffc2448b65ef 1098 /* send the request */
EnricoG 0:ffc2448b65ef 1099 errchk( M24SR_SendI2Ccommand ( NbByte , pBuffer ));
EnricoG 0:ffc2448b65ef 1100 errchk( M24SR_IsAnswerReady ( ));
EnricoG 0:ffc2448b65ef 1101 /* read the response */
EnricoG 0:ffc2448b65ef 1102 errchk( M24SR_ReceiveI2Cresponse ( M24SR_STATUSRESPONSE_NBBYTE , pBuffer ));
EnricoG 0:ffc2448b65ef 1103
EnricoG 0:ffc2448b65ef 1104 status = M24SR_IsCorrectCRC16Residue (pBuffer, M24SR_STATUSRESPONSE_NBBYTE);
EnricoG 0:ffc2448b65ef 1105 return status;
EnricoG 0:ffc2448b65ef 1106
EnricoG 0:ffc2448b65ef 1107 Error :
EnricoG 0:ffc2448b65ef 1108 return M24SR_ERROR_I2CTIMEOUT;
EnricoG 0:ffc2448b65ef 1109 }
EnricoG 0:ffc2448b65ef 1110
EnricoG 0:ffc2448b65ef 1111 /**
EnricoG 0:ffc2448b65ef 1112 * @brief This function configure GPO purpose for I2C session
EnricoG 0:ffc2448b65ef 1113 * @param GPO_I2Cconfig: GPO configuration to set
EnricoG 0:ffc2448b65ef 1114 * @retval Status (SW1&SW2) : Status of the operation to complete.
EnricoG 0:ffc2448b65ef 1115 */
EnricoG 0:ffc2448b65ef 1116 uint16_t M24SR_ManageI2CGPO( uc8 GPO_I2Cconfig)
EnricoG 0:ffc2448b65ef 1117 {
EnricoG 0:ffc2448b65ef 1118 uint16_t status;
EnricoG 0:ffc2448b65ef 1119 uint8_t GPO_config;
EnricoG 0:ffc2448b65ef 1120 uint8_t DefaultPassword[16]={0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
EnricoG 0:ffc2448b65ef 1121 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
EnricoG 0:ffc2448b65ef 1122
EnricoG 0:ffc2448b65ef 1123 if( GPO_I2Cconfig > STATE_CONTROL)
EnricoG 0:ffc2448b65ef 1124 {
EnricoG 0:ffc2448b65ef 1125 return M24SR_ERROR_PARAMETER;
EnricoG 0:ffc2448b65ef 1126 }
EnricoG 0:ffc2448b65ef 1127
EnricoG 0:ffc2448b65ef 1128 /* we must not be in interrupt mode for I2C synchro as we will change GPO purpose */
EnricoG 0:ffc2448b65ef 1129 M24SR_SetI2CSynchroMode(M24SR_WAITINGTIME_POLLING);
EnricoG 0:ffc2448b65ef 1130
EnricoG 0:ffc2448b65ef 1131 M24SR_SelectApplication();
EnricoG 0:ffc2448b65ef 1132 M24SR_SelectSystemfile();
EnricoG 0:ffc2448b65ef 1133 M24SR_ReadBinary ( 0x0004 , 0x01 , &GPO_config );
EnricoG 0:ffc2448b65ef 1134
EnricoG 0:ffc2448b65ef 1135 /* Update only GPO purpose for I2C */
EnricoG 0:ffc2448b65ef 1136 GPO_config = (GPO_config & 0xF0) | GPO_I2Cconfig;
EnricoG 0:ffc2448b65ef 1137 M24SR_SelectSystemfile();
EnricoG 0:ffc2448b65ef 1138 M24SR_Verify( I2C_PWD ,0x10 ,DefaultPassword );
EnricoG 0:ffc2448b65ef 1139 status = M24SR_UpdateBinary ( 0x0004 ,0x01, &(GPO_config) );
EnricoG 0:ffc2448b65ef 1140
EnricoG 0:ffc2448b65ef 1141 /* if we have set interrupt mode for I2C synchro we can enable interrupt mode */
EnricoG 0:ffc2448b65ef 1142 if ( GPO_I2Cconfig == I2C_ANSWER_READY && status == M24SR_ACTION_COMPLETED)
EnricoG 0:ffc2448b65ef 1143 #ifdef I2C_GPO_SYNCHRO_ALLOWED
EnricoG 0:ffc2448b65ef 1144 M24SR_SetI2CSynchroMode(M24SR_WAITINGTIME_GPO);
EnricoG 0:ffc2448b65ef 1145 #else
EnricoG 0:ffc2448b65ef 1146 M24SR_SetI2CSynchroMode(M24SR_INTERRUPT_GPO);
EnricoG 0:ffc2448b65ef 1147 #endif
EnricoG 0:ffc2448b65ef 1148
EnricoG 0:ffc2448b65ef 1149 return status;
EnricoG 0:ffc2448b65ef 1150 }
EnricoG 0:ffc2448b65ef 1151
EnricoG 0:ffc2448b65ef 1152 /**
EnricoG 0:ffc2448b65ef 1153 * @brief This function configure GPO purpose for RF session
EnricoG 0:ffc2448b65ef 1154 * @param GPO_RFconfig: GPO configuration to set
EnricoG 0:ffc2448b65ef 1155 * @retval Status (SW1&SW2) : Status of the operation to complete.
EnricoG 0:ffc2448b65ef 1156 */
EnricoG 0:ffc2448b65ef 1157 uint16_t M24SR_ManageRFGPO( uc8 GPO_RFconfig)
EnricoG 0:ffc2448b65ef 1158 {
EnricoG 0:ffc2448b65ef 1159 uint16_t status;
EnricoG 0:ffc2448b65ef 1160 uint8_t GPO_config;
EnricoG 0:ffc2448b65ef 1161 uint8_t DefaultPassword[16]={0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
EnricoG 0:ffc2448b65ef 1162 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
EnricoG 0:ffc2448b65ef 1163
EnricoG 0:ffc2448b65ef 1164 if( GPO_RFconfig > STATE_CONTROL)
EnricoG 0:ffc2448b65ef 1165 {
EnricoG 0:ffc2448b65ef 1166 return M24SR_ERROR_PARAMETER;
EnricoG 0:ffc2448b65ef 1167 }
EnricoG 0:ffc2448b65ef 1168
EnricoG 0:ffc2448b65ef 1169 M24SR_SelectApplication();
EnricoG 0:ffc2448b65ef 1170 M24SR_SelectSystemfile();
EnricoG 0:ffc2448b65ef 1171 M24SR_ReadBinary ( 0x0004 , 0x01 , &GPO_config );
EnricoG 0:ffc2448b65ef 1172
EnricoG 0:ffc2448b65ef 1173 /* Update only GPO purpose for I2C */
EnricoG 0:ffc2448b65ef 1174 GPO_config = (GPO_config & 0x0F) | (GPO_RFconfig<<4);
EnricoG 0:ffc2448b65ef 1175 M24SR_SelectSystemfile();
EnricoG 0:ffc2448b65ef 1176 M24SR_Verify( I2C_PWD ,0x10 ,DefaultPassword );
EnricoG 0:ffc2448b65ef 1177 status = M24SR_UpdateBinary ( 0x0004 ,0x01, &(GPO_config) );
EnricoG 0:ffc2448b65ef 1178
EnricoG 0:ffc2448b65ef 1179 return status;
EnricoG 0:ffc2448b65ef 1180 }
EnricoG 0:ffc2448b65ef 1181
EnricoG 0:ffc2448b65ef 1182
EnricoG 0:ffc2448b65ef 1183 /**
EnricoG 0:ffc2448b65ef 1184 * @brief This function enable or disable RF communication
EnricoG 0:ffc2448b65ef 1185 * @param OnOffChoice: GPO configuration to set
EnricoG 0:ffc2448b65ef 1186 * @retval Status (SW1&SW2) : Status of the operation to complete.
EnricoG 0:ffc2448b65ef 1187 */
EnricoG 0:ffc2448b65ef 1188 void M24SR_RFConfig( uc8 OnOffChoice)
EnricoG 0:ffc2448b65ef 1189 {
EnricoG 0:ffc2448b65ef 1190 M24SR_RFConfig_Hard(OnOffChoice);
EnricoG 0:ffc2448b65ef 1191 }
EnricoG 0:ffc2448b65ef 1192
EnricoG 0:ffc2448b65ef 1193 /**
EnricoG 0:ffc2448b65ef 1194 * @}
EnricoG 0:ffc2448b65ef 1195 */
EnricoG 0:ffc2448b65ef 1196
EnricoG 0:ffc2448b65ef 1197 /**
EnricoG 0:ffc2448b65ef 1198 * @}
EnricoG 0:ffc2448b65ef 1199 */
EnricoG 0:ffc2448b65ef 1200
EnricoG 0:ffc2448b65ef 1201 /**
EnricoG 0:ffc2448b65ef 1202 * @}
EnricoG 0:ffc2448b65ef 1203 */
EnricoG 0:ffc2448b65ef 1204
EnricoG 0:ffc2448b65ef 1205 /******************* (C) COPYRIGHT 2013 STMicroelectronics *****END OF FILE****/