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 lib_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 help to manage M24SR in a NFC forum context.
EnricoG 0:ffc2448b65ef 8 ******************************************************************************
EnricoG 0:ffc2448b65ef 9 * @attention
EnricoG 0:ffc2448b65ef 10 *
EnricoG 0:ffc2448b65ef 11 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
EnricoG 0:ffc2448b65ef 12 *
EnricoG 0:ffc2448b65ef 13 * Redistribution and use in source and binary forms, with or without modification,
EnricoG 0:ffc2448b65ef 14 * are permitted provided that the following conditions are met:
EnricoG 0:ffc2448b65ef 15 * 1. Redistributions of source code must retain the above copyright notice,
EnricoG 0:ffc2448b65ef 16 * this list of conditions and the following disclaimer.
EnricoG 0:ffc2448b65ef 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
EnricoG 0:ffc2448b65ef 18 * this list of conditions and the following disclaimer in the documentation
EnricoG 0:ffc2448b65ef 19 * and/or other materials provided with the distribution.
EnricoG 0:ffc2448b65ef 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
EnricoG 0:ffc2448b65ef 21 * may be used to endorse or promote products derived from this software
EnricoG 0:ffc2448b65ef 22 * without specific prior written permission.
EnricoG 0:ffc2448b65ef 23 *
EnricoG 0:ffc2448b65ef 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
EnricoG 0:ffc2448b65ef 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
EnricoG 0:ffc2448b65ef 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
EnricoG 0:ffc2448b65ef 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
EnricoG 0:ffc2448b65ef 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
EnricoG 0:ffc2448b65ef 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
EnricoG 0:ffc2448b65ef 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
EnricoG 0:ffc2448b65ef 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
EnricoG 0:ffc2448b65ef 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
EnricoG 0:ffc2448b65ef 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
EnricoG 0:ffc2448b65ef 34 *
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 "lib_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 /** @addtogroup lib_M24SR
EnricoG 0:ffc2448b65ef 48 * @{
EnricoG 0:ffc2448b65ef 49 * @brief This is the library to interface with the M24SR dynamic tag.
EnricoG 0:ffc2448b65ef 50 * This layer simplify the use of the M24SR driver by sequencing
EnricoG 0:ffc2448b65ef 51 * some commands.
EnricoG 0:ffc2448b65ef 52 */
EnricoG 0:ffc2448b65ef 53
EnricoG 0:ffc2448b65ef 54 uint8_t I2CPassword[16]={0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
EnricoG 0:ffc2448b65ef 55 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
EnricoG 0:ffc2448b65ef 56
EnricoG 0:ffc2448b65ef 57 static uint32_t NDEFSessionOpenID=NDEF_SESSION_CLOSED;
EnricoG 0:ffc2448b65ef 58
EnricoG 0:ffc2448b65ef 59 /* Init NDEF_FileID with bad value in case Init failed */
EnricoG 0:ffc2448b65ef 60 static uint16_t NDEF_FileID = 0xDEAD;
EnricoG 0:ffc2448b65ef 61
EnricoG 0:ffc2448b65ef 62 #ifdef __MBED__
EnricoG 0:ffc2448b65ef 63 extern void wait_ms(int ms);
EnricoG 0:ffc2448b65ef 64 #endif
EnricoG 0:ffc2448b65ef 65
EnricoG 0:ffc2448b65ef 66 /** @defgroup libM24SR_Private_Functions
EnricoG 0:ffc2448b65ef 67 * @{
EnricoG 0:ffc2448b65ef 68 */
EnricoG 0:ffc2448b65ef 69
EnricoG 0:ffc2448b65ef 70 /**
EnricoG 0:ffc2448b65ef 71 * @}
EnricoG 0:ffc2448b65ef 72 */
EnricoG 0:ffc2448b65ef 73
EnricoG 0:ffc2448b65ef 74
EnricoG 0:ffc2448b65ef 75 /** @defgroup libM24SR_Public_Functions
EnricoG 0:ffc2448b65ef 76 * @{
EnricoG 0:ffc2448b65ef 77 */
EnricoG 0:ffc2448b65ef 78
EnricoG 0:ffc2448b65ef 79 /**
EnricoG 0:ffc2448b65ef 80 * @brief This fonction initialize the M24SR
EnricoG 0:ffc2448b65ef 81 * @param CCBuffer : pointer on the buffer to store CC file
EnricoG 0:ffc2448b65ef 82 * @param size : number of byte of data to read
EnricoG 0:ffc2448b65ef 83 * @retval SUCCESS : Initalization done
EnricoG 0:ffc2448b65ef 84 * @retval ERROR : Not able to Initialize.
EnricoG 0:ffc2448b65ef 85 */
EnricoG 0:ffc2448b65ef 86 uint16_t M24SR_Initialization ( uint8_t* CCBuffer, uint8_t size )
EnricoG 0:ffc2448b65ef 87 {
EnricoG 0:ffc2448b65ef 88 uint16_t status = ERROR;
EnricoG 0:ffc2448b65ef 89 uint16_t trials = 5; /* wait 1sec, driver is configured to let 200ms for command to complete */
EnricoG 0:ffc2448b65ef 90 /* which is enough for all commands except GetSession if RF session is already opened */
EnricoG 0:ffc2448b65ef 91 /* Smartphone generaly release the session within the second, but customer can modify this value */
EnricoG 0:ffc2448b65ef 92
EnricoG 0:ffc2448b65ef 93 /* Perform HW initialization */
EnricoG 0:ffc2448b65ef 94 M24SR_Init();
EnricoG 0:ffc2448b65ef 95
EnricoG 0:ffc2448b65ef 96 /* Read CC file */
EnricoG 0:ffc2448b65ef 97 while( status != M24SR_ACTION_COMPLETED && trials)
EnricoG 0:ffc2448b65ef 98 {
EnricoG 0:ffc2448b65ef 99 //printf("Calling M24SR_GetSession(), attempts remaining %d\n", trials);
EnricoG 0:ffc2448b65ef 100 status = M24SR_GetSession();
EnricoG 0:ffc2448b65ef 101 //printf("M24SR_GetSession() returned: %d\n", status);
EnricoG 0:ffc2448b65ef 102 trials--;
EnricoG 0:ffc2448b65ef 103 }
EnricoG 0:ffc2448b65ef 104 if (status != M24SR_ACTION_COMPLETED)
EnricoG 0:ffc2448b65ef 105 {
EnricoG 0:ffc2448b65ef 106 //printf("M24SR_GetSession() failed\n");
EnricoG 0:ffc2448b65ef 107 return ERROR;
EnricoG 0:ffc2448b65ef 108 }
EnricoG 0:ffc2448b65ef 109 /*===================================*/
EnricoG 0:ffc2448b65ef 110 /* Select the NFC type 4 application */
EnricoG 0:ffc2448b65ef 111 /*===================================*/
EnricoG 0:ffc2448b65ef 112 errorchk( M24SR_SelectApplication() );
EnricoG 0:ffc2448b65ef 113
EnricoG 0:ffc2448b65ef 114 /*==================*/
EnricoG 0:ffc2448b65ef 115 /* select a CC file */
EnricoG 0:ffc2448b65ef 116 /*==================*/
EnricoG 0:ffc2448b65ef 117 errorchk (M24SR_SelectCCfile() );
EnricoG 0:ffc2448b65ef 118
EnricoG 0:ffc2448b65ef 119 /* read the first 15 bytes of the CC file */
EnricoG 0:ffc2448b65ef 120 if( M24SR_ReadData ( 0x0000 , 0x0F , CCBuffer )== M24SR_ACTION_COMPLETED)
EnricoG 0:ffc2448b65ef 121 {
EnricoG 0:ffc2448b65ef 122 NDEF_FileID = (uint16_t) ((CCBuffer[0x09]<<8) | CCBuffer[0x0A]);
EnricoG 0:ffc2448b65ef 123 errorchk( M24SR_Deselect () );
EnricoG 0:ffc2448b65ef 124 return SUCCESS;
EnricoG 0:ffc2448b65ef 125 }
EnricoG 0:ffc2448b65ef 126 else
EnricoG 0:ffc2448b65ef 127 errorchk( M24SR_Deselect () );
EnricoG 0:ffc2448b65ef 128
EnricoG 0:ffc2448b65ef 129 Error:
EnricoG 0:ffc2448b65ef 130 printf("Error in M24SR_Initialization\n");
EnricoG 0:ffc2448b65ef 131 return ERROR;
EnricoG 0:ffc2448b65ef 132
EnricoG 0:ffc2448b65ef 133 }
EnricoG 0:ffc2448b65ef 134
EnricoG 0:ffc2448b65ef 135 /**
EnricoG 0:ffc2448b65ef 136 * @brief This fonction retrieve the NDEF file ID of NDEF file present in M24SR
EnricoG 0:ffc2448b65ef 137 * @param NDEF_fileID : To store NDEF ID
EnricoG 0:ffc2448b65ef 138 * @retval SUCCESS : File ID read
EnricoG 0:ffc2448b65ef 139 * @retval ERROR : Not able to read file ID.
EnricoG 0:ffc2448b65ef 140 */
EnricoG 0:ffc2448b65ef 141 uint16_t M24SR_GetNDEFFileId ( uint16_t *NDEF_fileID )
EnricoG 0:ffc2448b65ef 142 {
EnricoG 0:ffc2448b65ef 143 if( NDEF_FileID != 0xDEAD)
EnricoG 0:ffc2448b65ef 144 {
EnricoG 0:ffc2448b65ef 145 *NDEF_fileID = NDEF_FileID;
EnricoG 0:ffc2448b65ef 146 return SUCCESS;
EnricoG 0:ffc2448b65ef 147 }
EnricoG 0:ffc2448b65ef 148 else
EnricoG 0:ffc2448b65ef 149 {
EnricoG 0:ffc2448b65ef 150 return ERROR;
EnricoG 0:ffc2448b65ef 151 }
EnricoG 0:ffc2448b65ef 152 }
EnricoG 0:ffc2448b65ef 153
EnricoG 0:ffc2448b65ef 154
EnricoG 0:ffc2448b65ef 155 /**
EnricoG 0:ffc2448b65ef 156 * @brief This fonction configure the M24SR to access NDEF message by I2C
EnricoG 0:ffc2448b65ef 157 * @param NDEF_fileID : NDEF identification to select NDEF in M24SR
EnricoG 0:ffc2448b65ef 158 * @param Priority: 2 options: check if M24SR available to open session (no RF session on going)
EnricoG 0:ffc2448b65ef 159 * Kill RF session and open I2C sesssion.
EnricoG 0:ffc2448b65ef 160 * @retval SUCCESS : Session is opened
EnricoG 0:ffc2448b65ef 161 * @retval ERROR : Not able to open session.
EnricoG 0:ffc2448b65ef 162 */
EnricoG 0:ffc2448b65ef 163 uint16_t M24SR_OpenNDEFSession ( uint16_t NDEF_fileID, uint16_t Priority )
EnricoG 0:ffc2448b65ef 164 {
EnricoG 0:ffc2448b65ef 165 uint16_t status = ERROR;
EnricoG 0:ffc2448b65ef 166 uint16_t trials = 5; /* wait 1sec, driver is configured to let 200ms for command to complete */
EnricoG 0:ffc2448b65ef 167 /* which is enough for all commands except GetSession if RF session is already opened */
EnricoG 0:ffc2448b65ef 168 /* Smartphone generaly release the session within the second, but customer can modify this value */
EnricoG 0:ffc2448b65ef 169
EnricoG 0:ffc2448b65ef 170 if(NDEFSessionOpenID == NDEF_SESSION_CLOSED)
EnricoG 0:ffc2448b65ef 171 {
EnricoG 0:ffc2448b65ef 172 if( Priority == TAKE_SESSION)
EnricoG 0:ffc2448b65ef 173 {
EnricoG 0:ffc2448b65ef 174 status = M24SR_KillSession();
EnricoG 0:ffc2448b65ef 175 }
EnricoG 0:ffc2448b65ef 176 else
EnricoG 0:ffc2448b65ef 177 {
EnricoG 0:ffc2448b65ef 178 while( status != M24SR_ACTION_COMPLETED && trials)
EnricoG 0:ffc2448b65ef 179 {
EnricoG 0:ffc2448b65ef 180 status = M24SR_GetSession();
EnricoG 0:ffc2448b65ef 181 trials--;
EnricoG 0:ffc2448b65ef 182 }
EnricoG 0:ffc2448b65ef 183 }
EnricoG 0:ffc2448b65ef 184 if (status != M24SR_ACTION_COMPLETED)
EnricoG 0:ffc2448b65ef 185 {
EnricoG 0:ffc2448b65ef 186 /* seems session already open on RF side */
EnricoG 0:ffc2448b65ef 187 /* But in case of I2C issue try to init again */
EnricoG 0:ffc2448b65ef 188 M24SR_Init();
EnricoG 0:ffc2448b65ef 189 return ERROR;
EnricoG 0:ffc2448b65ef 190 }
EnricoG 0:ffc2448b65ef 191
EnricoG 0:ffc2448b65ef 192 /*===================================*/
EnricoG 0:ffc2448b65ef 193 /* Select the NFC type 4 application */
EnricoG 0:ffc2448b65ef 194 /*===================================*/
EnricoG 0:ffc2448b65ef 195 errorchk( M24SR_SelectApplication() );
EnricoG 0:ffc2448b65ef 196
EnricoG 0:ffc2448b65ef 197 /*====================*/
EnricoG 0:ffc2448b65ef 198 /* select NDEF file */
EnricoG 0:ffc2448b65ef 199 /*====================*/
EnricoG 0:ffc2448b65ef 200 errorchk( M24SR_SelectNDEFfile(NDEF_fileID) );
EnricoG 0:ffc2448b65ef 201
EnricoG 0:ffc2448b65ef 202 NDEFSessionOpenID = (uint32_t)(NDEF_fileID);
EnricoG 0:ffc2448b65ef 203
EnricoG 0:ffc2448b65ef 204 return SUCCESS;
EnricoG 0:ffc2448b65ef 205 }
EnricoG 0:ffc2448b65ef 206 else if(NDEFSessionOpenID == NDEF_fileID)
EnricoG 0:ffc2448b65ef 207 {
EnricoG 0:ffc2448b65ef 208 /* Session already Open not an issue caller can perform access in NDEF file */
EnricoG 0:ffc2448b65ef 209 return SUCCESS;
EnricoG 0:ffc2448b65ef 210 }
EnricoG 0:ffc2448b65ef 211
EnricoG 0:ffc2448b65ef 212 Error:
EnricoG 0:ffc2448b65ef 213 return ERROR;
EnricoG 0:ffc2448b65ef 214 }
EnricoG 0:ffc2448b65ef 215
EnricoG 0:ffc2448b65ef 216 /**
EnricoG 0:ffc2448b65ef 217 * @brief This fonction close the NDEF Session.
EnricoG 0:ffc2448b65ef 218 * @param NDEF_fileID : NDEF identification to select NDEF in M24SR
EnricoG 0:ffc2448b65ef 219 * @retval SUCCESS : Session is closed
EnricoG 0:ffc2448b65ef 220 * @retval ERROR : Not able to close session.
EnricoG 0:ffc2448b65ef 221 */
EnricoG 0:ffc2448b65ef 222 uint16_t M24SR_CloseNDEFSession ( uint16_t NDEF_fileID )
EnricoG 0:ffc2448b65ef 223 {
EnricoG 0:ffc2448b65ef 224 uint16_t status = ERROR;
EnricoG 0:ffc2448b65ef 225
EnricoG 0:ffc2448b65ef 226 if(NDEFSessionOpenID == (uint32_t)(NDEF_fileID))
EnricoG 0:ffc2448b65ef 227 {
EnricoG 0:ffc2448b65ef 228 errorchk( M24SR_Deselect () );
EnricoG 0:ffc2448b65ef 229 NDEFSessionOpenID = NDEF_SESSION_CLOSED;
EnricoG 0:ffc2448b65ef 230
EnricoG 0:ffc2448b65ef 231 return SUCCESS;
EnricoG 0:ffc2448b65ef 232 }
EnricoG 0:ffc2448b65ef 233 else if(NDEFSessionOpenID == NDEF_SESSION_CLOSED)
EnricoG 0:ffc2448b65ef 234 {
EnricoG 0:ffc2448b65ef 235 /* Not an error as session is already closed */
EnricoG 0:ffc2448b65ef 236 return SUCCESS;
EnricoG 0:ffc2448b65ef 237 }
EnricoG 0:ffc2448b65ef 238
EnricoG 0:ffc2448b65ef 239 Error:
EnricoG 0:ffc2448b65ef 240 return ERROR;
EnricoG 0:ffc2448b65ef 241 }
EnricoG 0:ffc2448b65ef 242
EnricoG 0:ffc2448b65ef 243 /**
EnricoG 0:ffc2448b65ef 244 * @brief This fonction read the data stored in M24SR at defined offset
EnricoG 0:ffc2448b65ef 245 * @param Offset : Offset in the NDEF file in M24SR
EnricoG 0:ffc2448b65ef 246 * @param DataSize : Number of byte to read
EnricoG 0:ffc2448b65ef 247 * @param pData : pointer on buffer to store read data
EnricoG 0:ffc2448b65ef 248 * @retval Status (SW1&SW2) : Status of the operation.
EnricoG 0:ffc2448b65ef 249 */
EnricoG 0:ffc2448b65ef 250 uint16_t M24SR_ReadData ( uint16_t Offset , uint16_t DataSize , uint8_t* pData)
EnricoG 0:ffc2448b65ef 251 {
EnricoG 0:ffc2448b65ef 252 uint16_t status;
EnricoG 0:ffc2448b65ef 253
EnricoG 0:ffc2448b65ef 254 if( DataSize > M24SR_READ_MAX_NBBYTE)
EnricoG 0:ffc2448b65ef 255 {
EnricoG 0:ffc2448b65ef 256 do
EnricoG 0:ffc2448b65ef 257 {
EnricoG 0:ffc2448b65ef 258 status = M24SR_ReadBinary ( Offset, M24SR_READ_MAX_NBBYTE , pData);
EnricoG 0:ffc2448b65ef 259 Offset += M24SR_READ_MAX_NBBYTE;
EnricoG 0:ffc2448b65ef 260 pData += M24SR_READ_MAX_NBBYTE;
EnricoG 0:ffc2448b65ef 261 DataSize -= M24SR_READ_MAX_NBBYTE;
EnricoG 0:ffc2448b65ef 262 }while( DataSize > M24SR_READ_MAX_NBBYTE && status == M24SR_ACTION_COMPLETED);
EnricoG 0:ffc2448b65ef 263 if( status == M24SR_ACTION_COMPLETED && DataSize)
EnricoG 0:ffc2448b65ef 264 status = M24SR_ReadBinary ( Offset, (uint8_t)(DataSize) , pData);
EnricoG 0:ffc2448b65ef 265 }
EnricoG 0:ffc2448b65ef 266 else
EnricoG 0:ffc2448b65ef 267 status = M24SR_ReadBinary ( Offset, (uint8_t)(DataSize) , pData);
EnricoG 0:ffc2448b65ef 268
EnricoG 0:ffc2448b65ef 269 return status;
EnricoG 0:ffc2448b65ef 270 }
EnricoG 0:ffc2448b65ef 271
EnricoG 0:ffc2448b65ef 272 /**
EnricoG 0:ffc2448b65ef 273 * @brief This fonction read the data stored in M24SR at defined offset without NDEF concideration
EnricoG 0:ffc2448b65ef 274 * @param Offset : Offset in the NDEF file in M24SR
EnricoG 0:ffc2448b65ef 275 * @param DataSize : Number of byte to read
EnricoG 0:ffc2448b65ef 276 * @param pData : pointer on buffer to store read data
EnricoG 0:ffc2448b65ef 277 * @retval Status (SW1&SW2) : Status of the operation.
EnricoG 0:ffc2448b65ef 278 */
EnricoG 0:ffc2448b65ef 279 uint16_t M24SR_ForceReadData ( uint16_t Offset , uint16_t DataSize , uint8_t* pData)
EnricoG 0:ffc2448b65ef 280 {
EnricoG 0:ffc2448b65ef 281 uint16_t status;
EnricoG 0:ffc2448b65ef 282
EnricoG 0:ffc2448b65ef 283 if( DataSize > M24SR_READ_MAX_NBBYTE)
EnricoG 0:ffc2448b65ef 284 {
EnricoG 0:ffc2448b65ef 285 do
EnricoG 0:ffc2448b65ef 286 {
EnricoG 0:ffc2448b65ef 287 status = M24SR_STReadBinary ( Offset, M24SR_READ_MAX_NBBYTE , pData);
EnricoG 0:ffc2448b65ef 288 Offset += M24SR_READ_MAX_NBBYTE;
EnricoG 0:ffc2448b65ef 289 pData += M24SR_READ_MAX_NBBYTE;
EnricoG 0:ffc2448b65ef 290 DataSize -= M24SR_READ_MAX_NBBYTE;
EnricoG 0:ffc2448b65ef 291 }while( DataSize > M24SR_READ_MAX_NBBYTE && status == M24SR_ACTION_COMPLETED);
EnricoG 0:ffc2448b65ef 292 if( status == M24SR_ACTION_COMPLETED && DataSize)
EnricoG 0:ffc2448b65ef 293 status = M24SR_STReadBinary ( Offset, (uint8_t)(DataSize) , pData);
EnricoG 0:ffc2448b65ef 294 }
EnricoG 0:ffc2448b65ef 295 else
EnricoG 0:ffc2448b65ef 296 status = M24SR_STReadBinary ( Offset, (uint8_t)(DataSize) , pData);
EnricoG 0:ffc2448b65ef 297
EnricoG 0:ffc2448b65ef 298 return status;
EnricoG 0:ffc2448b65ef 299 }
EnricoG 0:ffc2448b65ef 300
EnricoG 0:ffc2448b65ef 301 /**
EnricoG 0:ffc2448b65ef 302 * @brief This fonction write data in M24SR at defined offset
EnricoG 0:ffc2448b65ef 303 * @param Offset : Offset in the NDEF file in M24SR
EnricoG 0:ffc2448b65ef 304 * @param DataSize : Number of byte to read
EnricoG 0:ffc2448b65ef 305 * @param pData : pointer on buffer to copy in M24SR
EnricoG 0:ffc2448b65ef 306 * @retval Status (SW1&SW2) : Status of the operation.
EnricoG 0:ffc2448b65ef 307 */
EnricoG 0:ffc2448b65ef 308 uint16_t M24SR_WriteData ( uint16_t Offset , uint16_t DataSize , uint8_t* pData)
EnricoG 0:ffc2448b65ef 309 {
EnricoG 0:ffc2448b65ef 310 uint16_t status;
EnricoG 0:ffc2448b65ef 311
EnricoG 0:ffc2448b65ef 312 if( DataSize > M24SR_WRITE_MAX_NBBYTE)
EnricoG 0:ffc2448b65ef 313 {
EnricoG 0:ffc2448b65ef 314 do
EnricoG 0:ffc2448b65ef 315 {
EnricoG 0:ffc2448b65ef 316 status = M24SR_UpdateBinary ( Offset, M24SR_WRITE_MAX_NBBYTE , pData);
EnricoG 0:ffc2448b65ef 317 Offset += M24SR_WRITE_MAX_NBBYTE;
EnricoG 0:ffc2448b65ef 318 pData += M24SR_WRITE_MAX_NBBYTE;
EnricoG 0:ffc2448b65ef 319 DataSize -= M24SR_WRITE_MAX_NBBYTE;
EnricoG 0:ffc2448b65ef 320 }while( DataSize > M24SR_WRITE_MAX_NBBYTE && status == M24SR_ACTION_COMPLETED);
EnricoG 0:ffc2448b65ef 321 if( status == M24SR_ACTION_COMPLETED && DataSize)
EnricoG 0:ffc2448b65ef 322 status = M24SR_UpdateBinary ( Offset, (uint8_t)(DataSize) , pData);
EnricoG 0:ffc2448b65ef 323 }
EnricoG 0:ffc2448b65ef 324 else
EnricoG 0:ffc2448b65ef 325 status = M24SR_UpdateBinary ( Offset, (uint8_t)(DataSize) , pData);
EnricoG 0:ffc2448b65ef 326
EnricoG 0:ffc2448b65ef 327 return status;
EnricoG 0:ffc2448b65ef 328 }
EnricoG 0:ffc2448b65ef 329
EnricoG 0:ffc2448b65ef 330 /**
EnricoG 0:ffc2448b65ef 331 * @brief This fonction activate the need of a password for next read access
EnricoG 0:ffc2448b65ef 332 * @param pCurrentWritePassword : Write password is needed to have the right to enable Read Password
EnricoG 0:ffc2448b65ef 333 * @param pNewPassword : The password that will be requiered for next read access
EnricoG 0:ffc2448b65ef 334 * @retval SUCCESS : Read password is activated
EnricoG 0:ffc2448b65ef 335 * @retval ERROR : operation does not complete
EnricoG 0:ffc2448b65ef 336 */
EnricoG 0:ffc2448b65ef 337 uint16_t M24SR_EnableReadPassword( uint8_t* pCurrentWritePassword, uint8_t* pNewPassword)
EnricoG 0:ffc2448b65ef 338 {
EnricoG 0:ffc2448b65ef 339 uint16_t status = SUCCESS;
EnricoG 0:ffc2448b65ef 340
EnricoG 0:ffc2448b65ef 341 if(M24SR_Verify( WRITE_PWD ,0x10 ,pCurrentWritePassword ) == M24SR_PWD_CORRECT)
EnricoG 0:ffc2448b65ef 342 {
EnricoG 0:ffc2448b65ef 343 /* Set new password */
EnricoG 0:ffc2448b65ef 344 M24SR_ChangeReferenceData ( READ_PWD, pNewPassword );
EnricoG 0:ffc2448b65ef 345 M24SR_EnableVerificationRequirement( READ_PWD );
EnricoG 0:ffc2448b65ef 346 status = SUCCESS;
EnricoG 0:ffc2448b65ef 347 }
EnricoG 0:ffc2448b65ef 348 else
EnricoG 0:ffc2448b65ef 349 {
EnricoG 0:ffc2448b65ef 350 /* M24SR already lock but password not known */
EnricoG 0:ffc2448b65ef 351 status = ERROR;
EnricoG 0:ffc2448b65ef 352 }
EnricoG 0:ffc2448b65ef 353
EnricoG 0:ffc2448b65ef 354 return status;
EnricoG 0:ffc2448b65ef 355 }
EnricoG 0:ffc2448b65ef 356
EnricoG 0:ffc2448b65ef 357 /**
EnricoG 0:ffc2448b65ef 358 * @brief This fonction desactivate the need of a password for next read access
EnricoG 0:ffc2448b65ef 359 * @param pCurrentWritePassword : Write password is needed to have the right to disable Read Password
EnricoG 0:ffc2448b65ef 360 * @retval SUCCESS : Read password is desactivated
EnricoG 0:ffc2448b65ef 361 * @retval ERROR : operation does not complete
EnricoG 0:ffc2448b65ef 362 */
EnricoG 0:ffc2448b65ef 363 uint16_t M24SR_DisableReadPassword( uint8_t* pCurrentWritePassword)
EnricoG 0:ffc2448b65ef 364 {
EnricoG 0:ffc2448b65ef 365 uint16_t status = SUCCESS;
EnricoG 0:ffc2448b65ef 366
EnricoG 0:ffc2448b65ef 367 if(M24SR_Verify( WRITE_PWD ,0x10 ,pCurrentWritePassword ) == M24SR_PWD_CORRECT)
EnricoG 0:ffc2448b65ef 368 {
EnricoG 0:ffc2448b65ef 369 /* Set new password */
EnricoG 0:ffc2448b65ef 370 M24SR_DisableVerificationRequirement( READ_PWD );
EnricoG 0:ffc2448b65ef 371 status = SUCCESS;
EnricoG 0:ffc2448b65ef 372 }
EnricoG 0:ffc2448b65ef 373 else
EnricoG 0:ffc2448b65ef 374 {
EnricoG 0:ffc2448b65ef 375 /* M24SR already lock but password not known */
EnricoG 0:ffc2448b65ef 376 status = ERROR;
EnricoG 0:ffc2448b65ef 377 }
EnricoG 0:ffc2448b65ef 378
EnricoG 0:ffc2448b65ef 379 return status;
EnricoG 0:ffc2448b65ef 380 }
EnricoG 0:ffc2448b65ef 381
EnricoG 0:ffc2448b65ef 382 /**
EnricoG 0:ffc2448b65ef 383 * @brief This fonction activate the need of a password for next write access
EnricoG 0:ffc2448b65ef 384 * @param pCurrentWritePassword : Write password must be prensented to have the right to modify write Password
EnricoG 0:ffc2448b65ef 385 * @param pNewPassword : The password that will be requiered for next write access
EnricoG 0:ffc2448b65ef 386 * @retval SUCCESS : Write password is activated
EnricoG 0:ffc2448b65ef 387 * @retval ERROR : operation does not complete
EnricoG 0:ffc2448b65ef 388 */
EnricoG 0:ffc2448b65ef 389 uint16_t M24SR_EnableWritePassword( uint8_t* pCurrentWritePassword, uint8_t* pNewPassword)
EnricoG 0:ffc2448b65ef 390 {
EnricoG 0:ffc2448b65ef 391 uint16_t status;
EnricoG 0:ffc2448b65ef 392
EnricoG 0:ffc2448b65ef 393 /* check we have the good password */
EnricoG 0:ffc2448b65ef 394 if (M24SR_Verify( WRITE_PWD ,0x10 ,pCurrentWritePassword )== M24SR_PWD_CORRECT)
EnricoG 0:ffc2448b65ef 395 {
EnricoG 0:ffc2448b65ef 396 /* Set new password */
EnricoG 0:ffc2448b65ef 397 M24SR_ChangeReferenceData ( WRITE_PWD, pNewPassword );
EnricoG 0:ffc2448b65ef 398 M24SR_EnableVerificationRequirement( WRITE_PWD );
EnricoG 0:ffc2448b65ef 399 status = SUCCESS;
EnricoG 0:ffc2448b65ef 400 }
EnricoG 0:ffc2448b65ef 401 else /* we don't have the good password */
EnricoG 0:ffc2448b65ef 402 {
EnricoG 0:ffc2448b65ef 403 status = ERROR;
EnricoG 0:ffc2448b65ef 404 }
EnricoG 0:ffc2448b65ef 405
EnricoG 0:ffc2448b65ef 406 return status;
EnricoG 0:ffc2448b65ef 407 }
EnricoG 0:ffc2448b65ef 408
EnricoG 0:ffc2448b65ef 409 /**
EnricoG 0:ffc2448b65ef 410 * @brief This fonction desactivate the need of a password for next write access
EnricoG 0:ffc2448b65ef 411 * @param pCurrentWritePassword : Write password must be prensented to have the right to disable it
EnricoG 0:ffc2448b65ef 412 * @retval SUCCESS : Write password is desactivated
EnricoG 0:ffc2448b65ef 413 * @retval ERROR : operation does not complete
EnricoG 0:ffc2448b65ef 414 */
EnricoG 0:ffc2448b65ef 415 uint16_t M24SR_DisableWritePassword( uint8_t* pCurrentWritePassword)
EnricoG 0:ffc2448b65ef 416 {
EnricoG 0:ffc2448b65ef 417 uint16_t status = SUCCESS;
EnricoG 0:ffc2448b65ef 418
EnricoG 0:ffc2448b65ef 419 if(M24SR_Verify( WRITE_PWD ,0x10 ,pCurrentWritePassword ) == M24SR_PWD_CORRECT)
EnricoG 0:ffc2448b65ef 420 {
EnricoG 0:ffc2448b65ef 421 M24SR_DisableVerificationRequirement( WRITE_PWD );
EnricoG 0:ffc2448b65ef 422 status = SUCCESS;
EnricoG 0:ffc2448b65ef 423 }
EnricoG 0:ffc2448b65ef 424 else
EnricoG 0:ffc2448b65ef 425 {
EnricoG 0:ffc2448b65ef 426 /* M24SR already lock but password not known */
EnricoG 0:ffc2448b65ef 427 status = ERROR;
EnricoG 0:ffc2448b65ef 428 }
EnricoG 0:ffc2448b65ef 429
EnricoG 0:ffc2448b65ef 430 return status;
EnricoG 0:ffc2448b65ef 431 }
EnricoG 0:ffc2448b65ef 432
EnricoG 0:ffc2448b65ef 433 /**
EnricoG 0:ffc2448b65ef 434 * @brief This fonction desactivate the need of read and write password for next access
EnricoG 0:ffc2448b65ef 435 * @param pSuperUserPassword : I2C super user password to overwrite read and write password
EnricoG 0:ffc2448b65ef 436 * @retval SUCCESS : M24SR access is now free (no password needed)
EnricoG 0:ffc2448b65ef 437 * @retval ERROR : operation does not complete
EnricoG 0:ffc2448b65ef 438 */
EnricoG 0:ffc2448b65ef 439 uint16_t M24SR_DisableAllPassword( uint8_t* pSuperUserPassword)
EnricoG 0:ffc2448b65ef 440 {
EnricoG 0:ffc2448b65ef 441 uint16_t status = SUCCESS;
EnricoG 0:ffc2448b65ef 442
EnricoG 0:ffc2448b65ef 443 if(M24SR_Verify( I2C_PWD ,0x10 ,pSuperUserPassword ) == M24SR_PWD_CORRECT)
EnricoG 0:ffc2448b65ef 444 {
EnricoG 0:ffc2448b65ef 445 M24SR_DisablePermanentState( READ_PWD );
EnricoG 0:ffc2448b65ef 446 M24SR_DisablePermanentState( WRITE_PWD );
EnricoG 0:ffc2448b65ef 447
EnricoG 0:ffc2448b65ef 448 M24SR_DisableVerificationRequirement( READ_PWD );
EnricoG 0:ffc2448b65ef 449 M24SR_DisableVerificationRequirement( WRITE_PWD );
EnricoG 0:ffc2448b65ef 450
EnricoG 0:ffc2448b65ef 451 /* reset password */
EnricoG 0:ffc2448b65ef 452 M24SR_ChangeReferenceData ( READ_PWD, pSuperUserPassword );
EnricoG 0:ffc2448b65ef 453 M24SR_ChangeReferenceData ( WRITE_PWD, pSuperUserPassword );
EnricoG 0:ffc2448b65ef 454 status = SUCCESS;
EnricoG 0:ffc2448b65ef 455 }
EnricoG 0:ffc2448b65ef 456 else
EnricoG 0:ffc2448b65ef 457 {
EnricoG 0:ffc2448b65ef 458 /* M24SR already lock but password not known */
EnricoG 0:ffc2448b65ef 459 status = ERROR;
EnricoG 0:ffc2448b65ef 460 }
EnricoG 0:ffc2448b65ef 461
EnricoG 0:ffc2448b65ef 462 return status;
EnricoG 0:ffc2448b65ef 463 }
EnricoG 0:ffc2448b65ef 464
EnricoG 0:ffc2448b65ef 465 /**
EnricoG 0:ffc2448b65ef 466 * @brief This fonction enable read only mode
EnricoG 0:ffc2448b65ef 467 * @param pCurrentWritePassword : Write password is needed to have right to enable read only mode
EnricoG 0:ffc2448b65ef 468 * @retval SUCCESS : M24SR access is now forbidden in write mode
EnricoG 0:ffc2448b65ef 469 * @retval ERROR : operation does not complete
EnricoG 0:ffc2448b65ef 470 */
EnricoG 0:ffc2448b65ef 471 uint16_t M24SR_EnableReadOnly( uint8_t* pCurrentWritePassword)
EnricoG 0:ffc2448b65ef 472 {
EnricoG 0:ffc2448b65ef 473 uint16_t status = SUCCESS;
EnricoG 0:ffc2448b65ef 474
EnricoG 0:ffc2448b65ef 475 if(M24SR_Verify( WRITE_PWD ,0x10 ,pCurrentWritePassword ) == M24SR_PWD_CORRECT)
EnricoG 0:ffc2448b65ef 476 {
EnricoG 0:ffc2448b65ef 477 M24SR_EnablePermanentState( WRITE_PWD ); /* lock write to have read only */
EnricoG 0:ffc2448b65ef 478 status = SUCCESS;
EnricoG 0:ffc2448b65ef 479 }
EnricoG 0:ffc2448b65ef 480 else
EnricoG 0:ffc2448b65ef 481 {
EnricoG 0:ffc2448b65ef 482 /* M24SR already lock but password not known */
EnricoG 0:ffc2448b65ef 483 status = ERROR;
EnricoG 0:ffc2448b65ef 484 }
EnricoG 0:ffc2448b65ef 485
EnricoG 0:ffc2448b65ef 486 return status;
EnricoG 0:ffc2448b65ef 487 }
EnricoG 0:ffc2448b65ef 488
EnricoG 0:ffc2448b65ef 489 /**
EnricoG 0:ffc2448b65ef 490 * @brief This fonction disable read only mode
EnricoG 0:ffc2448b65ef 491 * @param pCurrentWritePassword : Write password is needed to have right to disable read only mode
EnricoG 0:ffc2448b65ef 492 * @retval SUCCESS : M24SR write access is now allowed
EnricoG 0:ffc2448b65ef 493 * @retval ERROR : operation does not complete
EnricoG 0:ffc2448b65ef 494 */
EnricoG 0:ffc2448b65ef 495 uint16_t M24SR_DisableReadOnly( uint8_t* pCurrentWritePassword)
EnricoG 0:ffc2448b65ef 496 {
EnricoG 0:ffc2448b65ef 497 uint16_t status = SUCCESS;
EnricoG 0:ffc2448b65ef 498
EnricoG 0:ffc2448b65ef 499 if(M24SR_Verify( I2C_PWD ,0x10 ,I2CPassword ) == M24SR_PWD_CORRECT)
EnricoG 0:ffc2448b65ef 500 {
EnricoG 0:ffc2448b65ef 501 M24SR_DisablePermanentState( WRITE_PWD ); /* disable write protection to disable read only mode */
EnricoG 0:ffc2448b65ef 502 M24SR_DisableVerificationRequirement( WRITE_PWD );
EnricoG 0:ffc2448b65ef 503 status = SUCCESS;
EnricoG 0:ffc2448b65ef 504 }
EnricoG 0:ffc2448b65ef 505 else
EnricoG 0:ffc2448b65ef 506 {
EnricoG 0:ffc2448b65ef 507 /* we don't have the good I2C password nothing to do anymore */
EnricoG 0:ffc2448b65ef 508 status = ERROR;
EnricoG 0:ffc2448b65ef 509 }
EnricoG 0:ffc2448b65ef 510
EnricoG 0:ffc2448b65ef 511 return status;
EnricoG 0:ffc2448b65ef 512 }
EnricoG 0:ffc2448b65ef 513
EnricoG 0:ffc2448b65ef 514 /**
EnricoG 0:ffc2448b65ef 515 * @brief This fonction enable write only mode
EnricoG 0:ffc2448b65ef 516 * @param pCurrentWritePassword : Write password is needed to have right to enable write only mode
EnricoG 0:ffc2448b65ef 517 * @retval SUCCESS : M24SR access is now forbidden in read mode
EnricoG 0:ffc2448b65ef 518 * @retval ERROR : operation does not complete
EnricoG 0:ffc2448b65ef 519 */
EnricoG 0:ffc2448b65ef 520 uint16_t M24SR_EnableWriteOnly( uint8_t* pCurrentWritePassword)
EnricoG 0:ffc2448b65ef 521 {
EnricoG 0:ffc2448b65ef 522 uint16_t status = SUCCESS;
EnricoG 0:ffc2448b65ef 523
EnricoG 0:ffc2448b65ef 524 if(M24SR_Verify( WRITE_PWD ,0x10 ,pCurrentWritePassword ) == M24SR_PWD_CORRECT)
EnricoG 0:ffc2448b65ef 525 {
EnricoG 0:ffc2448b65ef 526 M24SR_EnablePermanentState( READ_PWD ); /* disable read access and keep write */
EnricoG 0:ffc2448b65ef 527 status = SUCCESS;
EnricoG 0:ffc2448b65ef 528 }
EnricoG 0:ffc2448b65ef 529 else
EnricoG 0:ffc2448b65ef 530 {
EnricoG 0:ffc2448b65ef 531 /* M24SR already lock but password not known */
EnricoG 0:ffc2448b65ef 532 status = ERROR;
EnricoG 0:ffc2448b65ef 533 }
EnricoG 0:ffc2448b65ef 534
EnricoG 0:ffc2448b65ef 535 return status;
EnricoG 0:ffc2448b65ef 536 }
EnricoG 0:ffc2448b65ef 537
EnricoG 0:ffc2448b65ef 538 /**
EnricoG 0:ffc2448b65ef 539 * @brief This fonction disable write only mode
EnricoG 0:ffc2448b65ef 540 * @param pCurrentWritePassword : Write password is needed to have right to disable write only mode
EnricoG 0:ffc2448b65ef 541 * @retval SUCCESS : M24SR read access is now allowed
EnricoG 0:ffc2448b65ef 542 * @retval ERROR : operation does not complete
EnricoG 0:ffc2448b65ef 543 */
EnricoG 0:ffc2448b65ef 544 uint16_t M24SR_DisableWriteOnly( uint8_t* pCurrentWritePassword)
EnricoG 0:ffc2448b65ef 545 {
EnricoG 0:ffc2448b65ef 546 uint16_t status = SUCCESS;
EnricoG 0:ffc2448b65ef 547
EnricoG 0:ffc2448b65ef 548 if(M24SR_Verify( I2C_PWD ,0x10 ,I2CPassword ) == M24SR_PWD_CORRECT)
EnricoG 0:ffc2448b65ef 549 {
EnricoG 0:ffc2448b65ef 550 M24SR_DisablePermanentState( READ_PWD ); /* disable write only -> enable write acces */
EnricoG 0:ffc2448b65ef 551 M24SR_DisableVerificationRequirement( READ_PWD );
EnricoG 0:ffc2448b65ef 552 status = SUCCESS;
EnricoG 0:ffc2448b65ef 553 }
EnricoG 0:ffc2448b65ef 554 else
EnricoG 0:ffc2448b65ef 555 {
EnricoG 0:ffc2448b65ef 556 /* M24SR already lock but password not known */
EnricoG 0:ffc2448b65ef 557 status = ERROR;
EnricoG 0:ffc2448b65ef 558 }
EnricoG 0:ffc2448b65ef 559
EnricoG 0:ffc2448b65ef 560 return status;
EnricoG 0:ffc2448b65ef 561 }
EnricoG 0:ffc2448b65ef 562
EnricoG 0:ffc2448b65ef 563 /**
EnricoG 0:ffc2448b65ef 564 * @brief This function configure GPO purpose for RF session
EnricoG 0:ffc2448b65ef 565 * @param GPO_config: GPO configuration to set
EnricoG 0:ffc2448b65ef 566 * @param mode: select RF or I2C, GPO config to update
EnricoG 0:ffc2448b65ef 567 * @retval Status : Status of the operation.
EnricoG 0:ffc2448b65ef 568 */
EnricoG 0:ffc2448b65ef 569 uint16_t M24SR_ManageGPO( uc8 GPO_config, uc8 mode)
EnricoG 0:ffc2448b65ef 570 {
EnricoG 0:ffc2448b65ef 571 uint16_t status;
EnricoG 0:ffc2448b65ef 572
EnricoG 0:ffc2448b65ef 573 if( mode == RF_GPO)
EnricoG 0:ffc2448b65ef 574 {
EnricoG 0:ffc2448b65ef 575 status = M24SR_ManageRFGPO ( GPO_config );
EnricoG 0:ffc2448b65ef 576 }
EnricoG 0:ffc2448b65ef 577 else
EnricoG 0:ffc2448b65ef 578 {
EnricoG 0:ffc2448b65ef 579 status = M24SR_ManageI2CGPO ( GPO_config );
EnricoG 0:ffc2448b65ef 580 }
EnricoG 0:ffc2448b65ef 581 return status;
EnricoG 0:ffc2448b65ef 582 }
EnricoG 0:ffc2448b65ef 583
EnricoG 0:ffc2448b65ef 584 /**
EnricoG 0:ffc2448b65ef 585 * @}
EnricoG 0:ffc2448b65ef 586 */
EnricoG 0:ffc2448b65ef 587
EnricoG 0:ffc2448b65ef 588 /**
EnricoG 0:ffc2448b65ef 589 * @}
EnricoG 0:ffc2448b65ef 590 */
EnricoG 0:ffc2448b65ef 591
EnricoG 0:ffc2448b65ef 592 /**
EnricoG 0:ffc2448b65ef 593 * @}
EnricoG 0:ffc2448b65ef 594 */
EnricoG 0:ffc2448b65ef 595
EnricoG 0:ffc2448b65ef 596 /******************* (C) COPYRIGHT 2013 STMicroelectronics *****END OF FILE****/
EnricoG 0:ffc2448b65ef 597
EnricoG 0:ffc2448b65ef 598