34
Dependencies: BSP libNDEF libxDot-mbed5 ST25DV
code/src/x_nucleo_nfc04.cpp@1:77a5e662bf40, 2020-10-03 (annotated)
- Committer:
- umickelsen
- Date:
- Sat Oct 03 22:39:19 2020 +0000
- Revision:
- 1:77a5e662bf40
Version 3.4
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
umickelsen | 1:77a5e662bf40 | 1 | #include "x_nucleo_nfc04.h" |
umickelsen | 1:77a5e662bf40 | 2 | |
umickelsen | 1:77a5e662bf40 | 3 | extern sCCFileInfo CCFileStruct; |
umickelsen | 1:77a5e662bf40 | 4 | |
umickelsen | 1:77a5e662bf40 | 5 | // This is a constructor |
umickelsen | 1:77a5e662bf40 | 6 | |
umickelsen | 1:77a5e662bf40 | 7 | X_Nucleo_NFC04::X_Nucleo_NFC04(I2C* i2cChannel, DigitalOut* led_1, DigitalOut* led_2, DigitalOut* led_3, DigitalOut* LPD, DigitalIn* MISO){ |
umickelsen | 1:77a5e662bf40 | 8 | mi2cChannel = i2cChannel; |
umickelsen | 1:77a5e662bf40 | 9 | mled_1 = led_1; |
umickelsen | 1:77a5e662bf40 | 10 | mled_2 = led_2; |
umickelsen | 1:77a5e662bf40 | 11 | mled_3 = led_3; |
umickelsen | 1:77a5e662bf40 | 12 | mLPD = LPD; |
umickelsen | 1:77a5e662bf40 | 13 | mMISO = MISO; |
umickelsen | 1:77a5e662bf40 | 14 | } |
umickelsen | 1:77a5e662bf40 | 15 | |
umickelsen | 1:77a5e662bf40 | 16 | /** |
umickelsen | 1:77a5e662bf40 | 17 | * @brief This function initialize the device |
umickelsen | 1:77a5e662bf40 | 18 | * @param mi2cChannel : I2C Channel |
umickelsen | 1:77a5e662bf40 | 19 | * @param mLPD : A digital PIN |
umickelsen | 1:77a5e662bf40 | 20 | * @retval status(int) : Return if the write operation is succesful |
umickelsen | 1:77a5e662bf40 | 21 | */ |
umickelsen | 1:77a5e662bf40 | 22 | |
umickelsen | 1:77a5e662bf40 | 23 | int X_Nucleo_NFC04::begin(I2C* mi2cChannel, DigitalOut *mLPD) { |
umickelsen | 1:77a5e662bf40 | 24 | int ret = 0; |
umickelsen | 1:77a5e662bf40 | 25 | |
umickelsen | 1:77a5e662bf40 | 26 | // Light some leds |
umickelsen | 1:77a5e662bf40 | 27 | ledOn(mled_1); |
umickelsen | 1:77a5e662bf40 | 28 | wait_ms( 300 ); |
umickelsen | 1:77a5e662bf40 | 29 | ledOn(mled_2); |
umickelsen | 1:77a5e662bf40 | 30 | |
umickelsen | 1:77a5e662bf40 | 31 | /* NFCTAG Init */ |
umickelsen | 1:77a5e662bf40 | 32 | ret = BSP_NFCTAG_Init(mi2cChannel, mLPD); |
umickelsen | 1:77a5e662bf40 | 33 | if(ret != NDEF_OK) |
umickelsen | 1:77a5e662bf40 | 34 | return ret; |
umickelsen | 1:77a5e662bf40 | 35 | |
umickelsen | 1:77a5e662bf40 | 36 | /* Reset MBEN Dynamic and initializes the CCFile struct */ |
umickelsen | 1:77a5e662bf40 | 37 | BSP_NFCTAG_GetExtended_Drv()->ResetMBEN_Dyn( mi2cChannel ); |
umickelsen | 1:77a5e662bf40 | 38 | if( NfcType5_NDEFDetection( mi2cChannel) != NDEF_OK ) |
umickelsen | 1:77a5e662bf40 | 39 | { |
umickelsen | 1:77a5e662bf40 | 40 | CCFileStruct.MagicNumber = NFCT5_MAGICNUMBER_E1_CCFILE; |
umickelsen | 1:77a5e662bf40 | 41 | CCFileStruct.Version = NFCT5_VERSION_V1_0; |
umickelsen | 1:77a5e662bf40 | 42 | CCFileStruct.MemorySize = ( ST25DV_MAX_SIZE / 8 ) & 0xFF; |
umickelsen | 1:77a5e662bf40 | 43 | CCFileStruct.TT5Tag = 0x05; |
umickelsen | 1:77a5e662bf40 | 44 | /* Init of the Type Tag 5 component (M24LR) */ |
umickelsen | 1:77a5e662bf40 | 45 | ret = NfcType5_TT5Init( mi2cChannel ); |
umickelsen | 1:77a5e662bf40 | 46 | if (ret != NDEF_OK) |
umickelsen | 1:77a5e662bf40 | 47 | return ret; |
umickelsen | 1:77a5e662bf40 | 48 | } |
umickelsen | 1:77a5e662bf40 | 49 | |
umickelsen | 1:77a5e662bf40 | 50 | ledOff( mled_1 ); |
umickelsen | 1:77a5e662bf40 | 51 | wait_ms( 300 ); |
umickelsen | 1:77a5e662bf40 | 52 | ledOff( mled_2 ); |
umickelsen | 1:77a5e662bf40 | 53 | wait_ms( 300 ); |
umickelsen | 1:77a5e662bf40 | 54 | ledOff( mled_3 ); |
umickelsen | 1:77a5e662bf40 | 55 | wait_ms( 300 ); |
umickelsen | 1:77a5e662bf40 | 56 | return NDEF_OK; |
umickelsen | 1:77a5e662bf40 | 57 | } |
umickelsen | 1:77a5e662bf40 | 58 | |
umickelsen | 1:77a5e662bf40 | 59 | |
umickelsen | 1:77a5e662bf40 | 60 | /** |
umickelsen | 1:77a5e662bf40 | 61 | * @brief This function light on selected Led |
umickelsen | 1:77a5e662bf40 | 62 | * @param led : Led to be lit on |
umickelsen | 1:77a5e662bf40 | 63 | * @retval None |
umickelsen | 1:77a5e662bf40 | 64 | */ |
umickelsen | 1:77a5e662bf40 | 65 | void X_Nucleo_NFC04::ledOn(DigitalOut* led) { |
umickelsen | 1:77a5e662bf40 | 66 | led -> write(1); |
umickelsen | 1:77a5e662bf40 | 67 | } |
umickelsen | 1:77a5e662bf40 | 68 | |
umickelsen | 1:77a5e662bf40 | 69 | /** |
umickelsen | 1:77a5e662bf40 | 70 | * @brief This function light off selected Led |
umickelsen | 1:77a5e662bf40 | 71 | * @param led : Led to be lit off |
umickelsen | 1:77a5e662bf40 | 72 | * @retval None |
umickelsen | 1:77a5e662bf40 | 73 | */ |
umickelsen | 1:77a5e662bf40 | 74 | |
umickelsen | 1:77a5e662bf40 | 75 | void X_Nucleo_NFC04::ledOff(DigitalOut* led) { |
umickelsen | 1:77a5e662bf40 | 76 | *led = 0; |
umickelsen | 1:77a5e662bf40 | 77 | } |
umickelsen | 1:77a5e662bf40 | 78 | |
umickelsen | 1:77a5e662bf40 | 79 | |
umickelsen | 1:77a5e662bf40 | 80 | /** |
umickelsen | 1:77a5e662bf40 | 81 | * @brief This function write an URI into the NFC Tag |
umickelsen | 1:77a5e662bf40 | 82 | * @param protocol : Protocol of the uri to write |
umickelsen | 1:77a5e662bf40 | 83 | * @param uri : String containing the uri to write in the NFC Tag |
umickelsen | 1:77a5e662bf40 | 84 | * @param info : Additional info |
umickelsen | 1:77a5e662bf40 | 85 | * @param mi2cChannel : Object of the I2C channel |
umickelsen | 1:77a5e662bf40 | 86 | * @retval status(int) : Return if the write operation is succesful |
umickelsen | 1:77a5e662bf40 | 87 | */ |
umickelsen | 1:77a5e662bf40 | 88 | int X_Nucleo_NFC04::writeURI(string protocol, string uri, string info, I2C* mi2cChannel) { |
umickelsen | 1:77a5e662bf40 | 89 | sURI_Info _URI; |
umickelsen | 1:77a5e662bf40 | 90 | strcpy( _URI.protocol,protocol.c_str() ); |
umickelsen | 1:77a5e662bf40 | 91 | strcpy( _URI.URI_Message,uri.c_str() ); |
umickelsen | 1:77a5e662bf40 | 92 | strcpy( _URI.Information,info.c_str() ); |
umickelsen | 1:77a5e662bf40 | 93 | |
umickelsen | 1:77a5e662bf40 | 94 | |
umickelsen | 1:77a5e662bf40 | 95 | return NDEF_WriteURI( &_URI, mi2cChannel ); |
umickelsen | 1:77a5e662bf40 | 96 | } |
umickelsen | 1:77a5e662bf40 | 97 | |
umickelsen | 1:77a5e662bf40 | 98 | /** |
umickelsen | 1:77a5e662bf40 | 99 | * @brief This function read an URI from the NFC Tag |
umickelsen | 1:77a5e662bf40 | 100 | * @param mi2cChannel : Object of the I2C channel |
umickelsen | 1:77a5e662bf40 | 101 | * @retval string : Return the string that has been read from the NFC Tag |
umickelsen | 1:77a5e662bf40 | 102 | */ |
umickelsen | 1:77a5e662bf40 | 103 | |
umickelsen | 1:77a5e662bf40 | 104 | string X_Nucleo_NFC04::readURI( I2C* mi2cChannel) { |
umickelsen | 1:77a5e662bf40 | 105 | sURI_Info uri = {"","",""}; |
umickelsen | 1:77a5e662bf40 | 106 | sRecordInfo_t recordInfo; |
umickelsen | 1:77a5e662bf40 | 107 | // increase buffer size for bigger messages |
umickelsen | 1:77a5e662bf40 | 108 | if(NDEF_ReadNDEF(NDEF_Buffer, mi2cChannel)) |
umickelsen | 1:77a5e662bf40 | 109 | return (string)NULL; |
umickelsen | 1:77a5e662bf40 | 110 | |
umickelsen | 1:77a5e662bf40 | 111 | if(NDEF_IdentifyBuffer(&recordInfo, NDEF_Buffer)) |
umickelsen | 1:77a5e662bf40 | 112 | return (string)NULL; |
umickelsen | 1:77a5e662bf40 | 113 | if(NDEF_ReadURI(&recordInfo,&uri)) |
umickelsen | 1:77a5e662bf40 | 114 | return (string)NULL; |
umickelsen | 1:77a5e662bf40 | 115 | return string(uri.protocol) + string(uri.URI_Message); |
umickelsen | 1:77a5e662bf40 | 116 | } |
umickelsen | 1:77a5e662bf40 | 117 | |
umickelsen | 1:77a5e662bf40 | 118 | |
umickelsen | 1:77a5e662bf40 | 119 | /** |
umickelsen | 1:77a5e662bf40 | 120 | * @brief This function write WiFi Credentials into the NFC Tag |
umickelsen | 1:77a5e662bf40 | 121 | * @param NetworkSSID : SSID of the WiFi Network |
umickelsen | 1:77a5e662bf40 | 122 | * @param AuthenticationType : |
umickelsen | 1:77a5e662bf40 | 123 | * @param EncryptionType : |
umickelsen | 1:77a5e662bf40 | 124 | * @param NetworkKey : Password of the WiFi |
umickelsen | 1:77a5e662bf40 | 125 | * @param mi2cChannel : Object of the I2C channel |
umickelsen | 1:77a5e662bf40 | 126 | * @retval status(int) : Return if the write operation is succesful |
umickelsen | 1:77a5e662bf40 | 127 | */ |
umickelsen | 1:77a5e662bf40 | 128 | uint16_t X_Nucleo_NFC04::writeWiFi(string NetworkSSID, Ndef_Wifi_Authentication_t AuthenticationType, Ndef_Wifi_Encryption_t EncryptionType, string NetworkKey, I2C* mi2cChannel ){ |
umickelsen | 1:77a5e662bf40 | 129 | |
umickelsen | 1:77a5e662bf40 | 130 | sWifiTokenInfo _wifi; |
umickelsen | 1:77a5e662bf40 | 131 | |
umickelsen | 1:77a5e662bf40 | 132 | strcpy( _wifi.NetworkSSID,NetworkSSID.c_str() ); |
umickelsen | 1:77a5e662bf40 | 133 | _wifi.AuthenticationType = AuthenticationType; |
umickelsen | 1:77a5e662bf40 | 134 | _wifi.EncryptionType = EncryptionType; |
umickelsen | 1:77a5e662bf40 | 135 | strcpy( _wifi.NetworkKey,NetworkKey.c_str() ); |
umickelsen | 1:77a5e662bf40 | 136 | |
umickelsen | 1:77a5e662bf40 | 137 | |
umickelsen | 1:77a5e662bf40 | 138 | |
umickelsen | 1:77a5e662bf40 | 139 | return NDEF_WriteWifiToken(&_wifi, mi2cChannel ); |
umickelsen | 1:77a5e662bf40 | 140 | } |
umickelsen | 1:77a5e662bf40 | 141 | |
umickelsen | 1:77a5e662bf40 | 142 |