Firmware library for the X-NUCLEO-NFC01A1 Dynamic NFC Tag board.

Dependencies:   M24SR

Dependents:   NFC M2M_2016_STM32 MyongjiElec_capstone1 IDW01M1_Cloud_IBM ... more

Fork of X_NUCLEO_NFC01A1 by ST Expansion SW Team

X-NUCLEO-NFC01A1 Dynamic NFC Tag Expansion Board Firmware Package

Introduction

This firmware package includes Components Device Drivers, Board Support Package and example applications for STMicroelectronics X-NUCLEO-NFC01A1 Dynamic NFC Tag Expansion Board based on M24SR.

Firmware Library

Class X_NUCLEO_NFC01A1 is intended to represent the Dynamic NFC Tag Expansion Board with the same name.
It provides an API to access to the M24SR component and to the three onboard LEDs.
It is intentionally implemented as a singleton because only one X_NUCLEO_NFC01A1 at a time might be deployed in a HW component stack.
The library also provides an implementation of the NDEF library API for M24SR, providing an simple way to read/write NDEF formatted messages from/to the M24SR dynamic NFC tag.

Example applications

1. Hello World
2. Asynchronous Hello World

Committer:
giovannivisentini
Date:
Mon May 15 13:47:03 2017 +0000
Revision:
31:85fdf6fd3da5
Parent:
29:7a2dfd06cb29
remove M24SR_IO_PollI2C function

Who changed what in which revision?

UserRevisionLine numberNew contents of line
giovannivisentini 6:96389fb79676 1 /**
giovannivisentini 6:96389fb79676 2 ******************************************************************************
giovannivisentini 6:96389fb79676 3 * @file NdefNfcTagSTM24SR.cpp
giovannivisentini 6:96389fb79676 4 * @author ST Central Labs
giovannivisentini 29:7a2dfd06cb29 5 * @version V2.0.0
giovannivisentini 29:7a2dfd06cb29 6 * @date 28 Apr 2017
giovannivisentini 24:9f98eafa2d39 7 * @brief Wrapper class of the NDefLib library to write/read NDEF messages
giovannivisentini 6:96389fb79676 8 ******************************************************************************
giovannivisentini 9:9f2e2e68d695 9 * @attention
giovannivisentini 6:96389fb79676 10 *
giovannivisentini 9:9f2e2e68d695 11 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
giovannivisentini 6:96389fb79676 12 *
giovannivisentini 6:96389fb79676 13 * Redistribution and use in source and binary forms, with or without modification,
giovannivisentini 6:96389fb79676 14 * are permitted provided that the following conditions are met:
giovannivisentini 6:96389fb79676 15 * 1. Redistributions of source code must retain the above copyright notice,
giovannivisentini 6:96389fb79676 16 * this list of conditions and the following disclaimer.
giovannivisentini 6:96389fb79676 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
giovannivisentini 6:96389fb79676 18 * this list of conditions and the following disclaimer in the documentation
giovannivisentini 6:96389fb79676 19 * and/or other materials provided with the distribution.
giovannivisentini 6:96389fb79676 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
giovannivisentini 6:96389fb79676 21 * may be used to endorse or promote products derived from this software
giovannivisentini 6:96389fb79676 22 * without specific prior written permission.
giovannivisentini 6:96389fb79676 23 *
giovannivisentini 6:96389fb79676 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
giovannivisentini 6:96389fb79676 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
giovannivisentini 6:96389fb79676 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
giovannivisentini 6:96389fb79676 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
giovannivisentini 6:96389fb79676 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
giovannivisentini 6:96389fb79676 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
giovannivisentini 6:96389fb79676 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
giovannivisentini 6:96389fb79676 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
giovannivisentini 6:96389fb79676 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
giovannivisentini 6:96389fb79676 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
giovannivisentini 6:96389fb79676 34 *
giovannivisentini 6:96389fb79676 35 ******************************************************************************
giovannivisentini 6:96389fb79676 36 */
giovannivisentini 19:0b65a5813059 37
giovannivisentini 19:0b65a5813059 38 #include <cmath>
giovannivisentini 6:96389fb79676 39
giovannivisentini 6:96389fb79676 40 #include "NDefNfcTagM24SR.h"
giovannivisentini 12:d1f5eaa85deb 41
giovannivisentini 19:0b65a5813059 42
giovannivisentini 6:96389fb79676 43 /* wait 1sec, driver is configured to let 200ms for command to complete */
giovannivisentini 6:96389fb79676 44 /* which is enough for all commands except GetSession if RF session is already opened */
giovannivisentini 9:9f2e2e68d695 45 /* Smartphone generally releases the session within the second, anyway the user can modify this value */
giovannivisentini 6:96389fb79676 46 #define OPENSESSION_NTRIALS 5
giovannivisentini 6:96389fb79676 47
giovannivisentini 6:96389fb79676 48 #define CC_FILE_LENGTH_BYTE 15
giovannivisentini 19:0b65a5813059 49 ////////////////////////////START OpenSessionCallBack/////////////////////////
giovannivisentini 19:0b65a5813059 50 NDefNfcTagM24SR::OpenSessionCallBack::
giovannivisentini 19:0b65a5813059 51 OpenSessionCallBack(NDefNfcTagM24SR& sender):
giovannivisentini 19:0b65a5813059 52 mSender(sender),mNTrials(OPENSESSION_NTRIALS){}
giovannivisentini 19:0b65a5813059 53
giovannivisentini 29:7a2dfd06cb29 54 void NDefNfcTagM24SR::OpenSessionCallBack::on_session_open(M24SR *nfc,
giovannivisentini 27:3881985097bb 55 M24SR::StatusTypeDef status){
giovannivisentini 27:3881985097bb 56 if(status==M24SR::M24SR_SUCCESS){
giovannivisentini 29:7a2dfd06cb29 57 nfc->select_application();
giovannivisentini 19:0b65a5813059 58 }else{
giovannivisentini 29:7a2dfd06cb29 59 mSender.mCallBack->on_session_open(&mSender,false);
giovannivisentini 19:0b65a5813059 60 }
giovannivisentini 19:0b65a5813059 61 }
giovannivisentini 19:0b65a5813059 62
giovannivisentini 29:7a2dfd06cb29 63 void NDefNfcTagM24SR::OpenSessionCallBack::on_selected_application(M24SR *nfc,
giovannivisentini 27:3881985097bb 64 M24SR::StatusTypeDef status){
giovannivisentini 27:3881985097bb 65 if(status==M24SR::M24SR_SUCCESS){
giovannivisentini 29:7a2dfd06cb29 66 nfc->select_CC_file();
giovannivisentini 19:0b65a5813059 67 }else{
giovannivisentini 19:0b65a5813059 68 if(mNTrials==0)
giovannivisentini 29:7a2dfd06cb29 69 mSender.mCallBack->on_session_open(&mSender,false);
giovannivisentini 19:0b65a5813059 70 else{
giovannivisentini 19:0b65a5813059 71 mNTrials--;
giovannivisentini 29:7a2dfd06cb29 72 nfc->select_application();
giovannivisentini 19:0b65a5813059 73 }//if-else
giovannivisentini 19:0b65a5813059 74 }//if-else
giovannivisentini 19:0b65a5813059 75 }
giovannivisentini 19:0b65a5813059 76
giovannivisentini 29:7a2dfd06cb29 77 void NDefNfcTagM24SR::OpenSessionCallBack::on_selected_CC_file(M24SR *nfc,
giovannivisentini 27:3881985097bb 78 M24SR::StatusTypeDef status){
giovannivisentini 27:3881985097bb 79 if(status==M24SR::M24SR_SUCCESS){
giovannivisentini 29:7a2dfd06cb29 80 nfc->read_binary(0x0000, CC_FILE_LENGTH_BYTE, CCFile);
giovannivisentini 19:0b65a5813059 81 }else{
giovannivisentini 29:7a2dfd06cb29 82 mSender.mCallBack->on_session_open(&mSender,false);
giovannivisentini 19:0b65a5813059 83 }
giovannivisentini 19:0b65a5813059 84 }
giovannivisentini 19:0b65a5813059 85
giovannivisentini 29:7a2dfd06cb29 86 void NDefNfcTagM24SR::OpenSessionCallBack::on_read_byte(M24SR *nfc,
giovannivisentini 27:3881985097bb 87 M24SR::StatusTypeDef status,uint16_t offset,uint8_t *readByte, uint16_t nReadByte){
giovannivisentini 19:0b65a5813059 88 (void)offset;
giovannivisentini 27:3881985097bb 89 if(status!=M24SR::M24SR_SUCCESS || nReadByte!=CC_FILE_LENGTH_BYTE){
giovannivisentini 29:7a2dfd06cb29 90 return mSender.mCallBack->on_session_open(&mSender,false);
giovannivisentini 19:0b65a5813059 91 }//else
giovannivisentini 19:0b65a5813059 92 uint16_t NDefFileId = (uint16_t) ((readByte[0x09] << 8) | readByte[0x0A]);
giovannivisentini 19:0b65a5813059 93 mSender.mMaxReadBytes = (uint16_t) ((readByte[0x03] << 8) | readByte[0x04]);
giovannivisentini 19:0b65a5813059 94 mSender.mMaxWriteBytes = (uint16_t) ((readByte[0x05] << 8) | readByte[0x06]);
giovannivisentini 29:7a2dfd06cb29 95 nfc->select_NDEF_file(NDefFileId);
giovannivisentini 19:0b65a5813059 96 }
giovannivisentini 19:0b65a5813059 97
giovannivisentini 29:7a2dfd06cb29 98 void NDefNfcTagM24SR::OpenSessionCallBack::on_selected_NDEF_file(M24SR *nfc,
giovannivisentini 27:3881985097bb 99 M24SR::StatusTypeDef status){
giovannivisentini 19:0b65a5813059 100 (void)nfc;
giovannivisentini 27:3881985097bb 101
giovannivisentini 27:3881985097bb 102 mSender.mIsSessionOpen = status==M24SR::M24SR_SUCCESS;
giovannivisentini 29:7a2dfd06cb29 103 mSender.mCallBack->on_session_open(&mSender,mSender.mIsSessionOpen);
giovannivisentini 19:0b65a5813059 104 }
giovannivisentini 19:0b65a5813059 105 ////////////////////////////END OpenSessionCallBack/////////////////////////
giovannivisentini 6:96389fb79676 106
giovannivisentini 29:7a2dfd06cb29 107 bool NDefNfcTagM24SR::open_session(bool force) {
giovannivisentini 6:96389fb79676 108
giovannivisentini 29:7a2dfd06cb29 109 if (is_session_open()){
giovannivisentini 29:7a2dfd06cb29 110 mCallBack->on_session_open(this,true);
giovannivisentini 6:96389fb79676 111 return true;
giovannivisentini 6:96389fb79676 112 }
giovannivisentini 6:96389fb79676 113
giovannivisentini 29:7a2dfd06cb29 114 mDevice.set_callback(&mOpenSessionCallback);
giovannivisentini 19:0b65a5813059 115 if(force)
giovannivisentini 29:7a2dfd06cb29 116 return mDevice.force_get_session() == M24SR::M24SR_SUCCESS;
giovannivisentini 19:0b65a5813059 117 else
giovannivisentini 29:7a2dfd06cb29 118 return mDevice.get_session() == M24SR::M24SR_SUCCESS;
giovannivisentini 6:96389fb79676 119
giovannivisentini 6:96389fb79676 120
giovannivisentini 6:96389fb79676 121 }
giovannivisentini 6:96389fb79676 122
giovannivisentini 29:7a2dfd06cb29 123 bool NDefNfcTagM24SR::close_session() {
giovannivisentini 29:7a2dfd06cb29 124 mDevice.set_callback(&mCloseSessionCallback);
giovannivisentini 31:85fdf6fd3da5 125 M24SR::StatusTypeDef status = mDevice.deselect();
giovannivisentini 31:85fdf6fd3da5 126 return status == M24SR::M24SR_SUCCESS;
giovannivisentini 6:96389fb79676 127 }
giovannivisentini 6:96389fb79676 128
giovannivisentini 29:7a2dfd06cb29 129 void NDefNfcTagM24SR::WriteByteCallback::on_updated_binary(M24SR *nfc,
giovannivisentini 27:3881985097bb 130 M24SR::StatusTypeDef status,uint16_t startOffset, uint8_t *writeByte,uint16_t nWriteByte){
giovannivisentini 6:96389fb79676 131
giovannivisentini 27:3881985097bb 132 if(status!=M24SR::M24SR_SUCCESS){ // error -> finish to write
giovannivisentini 19:0b65a5813059 133 mCallback(mCallbackParam,false,mByteToWrite,mNByteToWrite);
giovannivisentini 19:0b65a5813059 134 return;
giovannivisentini 19:0b65a5813059 135 }//else
giovannivisentini 6:96389fb79676 136
giovannivisentini 19:0b65a5813059 137 mByteWrote+=nWriteByte;
giovannivisentini 19:0b65a5813059 138 if(mByteWrote==mNByteToWrite){ //write all -> finish
giovannivisentini 19:0b65a5813059 139 mCallback(mCallbackParam,true,mByteToWrite,mNByteToWrite);
giovannivisentini 19:0b65a5813059 140 }else{ //else write another slice
giovannivisentini 19:0b65a5813059 141 uint16_t tempLenght = std::min(mSender.mMaxWriteBytes,
giovannivisentini 19:0b65a5813059 142 (uint16_t)(mNByteToWrite-mByteWrote));
giovannivisentini 29:7a2dfd06cb29 143 nfc->update_binary(startOffset+nWriteByte,tempLenght,writeByte+nWriteByte);
giovannivisentini 19:0b65a5813059 144 }//if-else
giovannivisentini 19:0b65a5813059 145 }
giovannivisentini 19:0b65a5813059 146
giovannivisentini 19:0b65a5813059 147 bool NDefNfcTagM24SR::writeByte(const uint8_t *buffer, uint16_t length,uint16_t offset,
giovannivisentini 19:0b65a5813059 148 byteOperationCallback_t callback,CallbackStatus_t *callbackStatus){
giovannivisentini 29:7a2dfd06cb29 149 if(!is_session_open())
giovannivisentini 19:0b65a5813059 150 callback(callbackStatus,false,buffer,length);
giovannivisentini 19:0b65a5813059 151 //else
giovannivisentini 29:7a2dfd06cb29 152 mWriteByteCallback.set_task(buffer,length,callback,callbackStatus);
giovannivisentini 29:7a2dfd06cb29 153 mDevice.set_callback(&mWriteByteCallback);
giovannivisentini 6:96389fb79676 154
giovannivisentini 6:96389fb79676 155 if (length > mMaxWriteBytes) {
giovannivisentini 29:7a2dfd06cb29 156 return mDevice.update_binary(offset, mMaxWriteBytes,(uint8_t*) buffer) == M24SR::M24SR_SUCCESS;
giovannivisentini 19:0b65a5813059 157 }else{
giovannivisentini 29:7a2dfd06cb29 158 return mDevice.update_binary(offset,length,(uint8_t*)buffer) == M24SR::M24SR_SUCCESS;
giovannivisentini 19:0b65a5813059 159 }//if-else
giovannivisentini 6:96389fb79676 160
giovannivisentini 6:96389fb79676 161 }
giovannivisentini 6:96389fb79676 162
giovannivisentini 29:7a2dfd06cb29 163 void NDefNfcTagM24SR::ReadByteCallback::on_read_byte(M24SR *nfc,
giovannivisentini 27:3881985097bb 164 M24SR::StatusTypeDef status,uint16_t startOffset, uint8_t *readBffer,uint16_t nReadByte){
giovannivisentini 19:0b65a5813059 165
giovannivisentini 27:3881985097bb 166 if(status!=M24SR::M24SR_SUCCESS){ // error -> finish to write
giovannivisentini 19:0b65a5813059 167 mCallback(mCallbackParam,false,mBuffer,mNByteToRead);
giovannivisentini 19:0b65a5813059 168 return;
giovannivisentini 19:0b65a5813059 169 }//else
giovannivisentini 6:96389fb79676 170
giovannivisentini 19:0b65a5813059 171 mByteRead += nReadByte;
giovannivisentini 19:0b65a5813059 172 if(mByteRead==mNByteToRead){ //read all -> finish
giovannivisentini 19:0b65a5813059 173 mCallback(mCallbackParam,true,mBuffer,mNByteToRead);
giovannivisentini 19:0b65a5813059 174 }else{ //else write another slice
giovannivisentini 19:0b65a5813059 175 uint16_t tempLenght = std::min(mSender.mMaxReadBytes,
giovannivisentini 19:0b65a5813059 176 (uint16_t)(mNByteToRead-mByteRead));
giovannivisentini 29:7a2dfd06cb29 177 nfc->read_binary(startOffset+nReadByte,tempLenght,readBffer+nReadByte);
giovannivisentini 19:0b65a5813059 178 }//if-else
giovannivisentini 19:0b65a5813059 179
giovannivisentini 6:96389fb79676 180 }
giovannivisentini 6:96389fb79676 181
giovannivisentini 19:0b65a5813059 182 bool NDefNfcTagM24SR::readByte(const uint16_t byteOffset, const uint16_t length,
giovannivisentini 19:0b65a5813059 183 uint8_t *buffer, byteOperationCallback_t callback,CallbackStatus_t *callbackStatus){
giovannivisentini 29:7a2dfd06cb29 184 if(!is_session_open()){
giovannivisentini 19:0b65a5813059 185 return callback(callbackStatus,false,buffer,length);
giovannivisentini 19:0b65a5813059 186 }
giovannivisentini 19:0b65a5813059 187 //else
giovannivisentini 29:7a2dfd06cb29 188 mReadByteCallback.set_task(buffer,length,callback,callbackStatus);
giovannivisentini 29:7a2dfd06cb29 189 mDevice.set_callback(&mReadByteCallback);
giovannivisentini 19:0b65a5813059 190
giovannivisentini 19:0b65a5813059 191 if (length > mMaxReadBytes) {
giovannivisentini 29:7a2dfd06cb29 192 return mDevice.read_binary(byteOffset, mMaxReadBytes,buffer)== M24SR::M24SR_SUCCESS;;
giovannivisentini 19:0b65a5813059 193 }else{
giovannivisentini 29:7a2dfd06cb29 194 return mDevice.read_binary(byteOffset,length,buffer)== M24SR::M24SR_SUCCESS;;
giovannivisentini 19:0b65a5813059 195 }//if-else
giovannivisentini 19:0b65a5813059 196
giovannivisentini 19:0b65a5813059 197 }