CR95F X-Nucleo NFC03A1 Shield STM

Dependents:   Nucleo_NFC03A1_Demo

Committer:
duchonic
Date:
Mon Sep 03 15:09:39 2018 +0000
Revision:
3:12c871b2412d
Parent:
2:c7006765c441
Child:
4:8fce71523e13
working version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
duchonic 0:57769c77d24e 1 #include <CR95HF.h>
duchonic 0:57769c77d24e 2 #include "main.h"
duchonic 0:57769c77d24e 3
duchonic 0:57769c77d24e 4 DigitalOut InterfacePin(D9);
duchonic 0:57769c77d24e 5
duchonic 1:549e63ac990c 6 DigitalOut led1(D7);
duchonic 1:549e63ac990c 7 DigitalOut led2(D6);
duchonic 1:549e63ac990c 8 DigitalOut led3(D5);
duchonic 1:549e63ac990c 9 DigitalOut led4(D4);
duchonic 1:549e63ac990c 10
duchonic 1:549e63ac990c 11 CR95HF::CR95HF(PinName tx, PinName rx) : nfcDevice(tx,rx)
duchonic 0:57769c77d24e 12 {
duchonic 0:57769c77d24e 13 InterfacePin = false;
duchonic 0:57769c77d24e 14 }
duchonic 0:57769c77d24e 15
duchonic 0:57769c77d24e 16 CR95HF::~CR95HF() {};
duchonic 0:57769c77d24e 17
duchonic 1:549e63ac990c 18 uint8_t CR95HF::Init()
duchonic 0:57769c77d24e 19 {
duchonic 1:549e63ac990c 20 uint8_t commandInfo[] = {0x01, 0x00};
duchonic 3:12c871b2412d 21 uint8_t commandSetProtocoll[] = {0x02, 0x02, 0x01, 0x05};
duchonic 0:57769c77d24e 22
duchonic 0:57769c77d24e 23 printf("CR95HF_Init\r\n");
duchonic 0:57769c77d24e 24
duchonic 1:549e63ac990c 25 nfcDevice.baud(57600);
duchonic 0:57769c77d24e 26
duchonic 1:549e63ac990c 27 wait(1);
duchonic 1:549e63ac990c 28 nfcWrite(commandInfo, 2);
duchonic 1:549e63ac990c 29 wait(1);
duchonic 1:549e63ac990c 30 nfcWrite(commandSetProtocoll, 4);
duchonic 3:12c871b2412d 31 wait(1);
duchonic 3:12c871b2412d 32 nfcDevice.attach(this, &CR95HF::rxCallbackNFC, Serial::RxIrq);
duchonic 0:57769c77d24e 33 return(0);
duchonic 0:57769c77d24e 34 }
duchonic 0:57769c77d24e 35
duchonic 1:549e63ac990c 36 uint8_t CR95HF::ConfigManagerTagHunting()
duchonic 0:57769c77d24e 37 {
duchonic 0:57769c77d24e 38 uint8_t returnValue = false;
duchonic 3:12c871b2412d 39 uint8_t detectCard[] = {0x04, 0x03, 0x26, 0x01, 0x00};
duchonic 0:57769c77d24e 40
duchonic 3:12c871b2412d 41 static uint16_t counter=0;
duchonic 3:12c871b2412d 42
duchonic 3:12c871b2412d 43 rxBufPos = 0;
duchonic 3:12c871b2412d 44 rxMsgComplete = false;
duchonic 3:12c871b2412d 45
duchonic 1:549e63ac990c 46 nfcWrite(detectCard, 5);
duchonic 0:57769c77d24e 47
duchonic 3:12c871b2412d 48 wait(0.2);
duchonic 3:12c871b2412d 49
duchonic 3:12c871b2412d 50 if(blub == 0x80) {
duchonic 3:12c871b2412d 51 led3 = true;
duchonic 3:12c871b2412d 52 led4 = false;
duchonic 3:12c871b2412d 53 printf("tag deteced UID (cnt %u):", counter++);
duchonic 3:12c871b2412d 54 for(int pos=7;pos>=0;pos--)
duchonic 3:12c871b2412d 55 {
duchonic 3:12c871b2412d 56 printf("%02x", rxData[pos]);
duchonic 3:12c871b2412d 57 }
duchonic 3:12c871b2412d 58 printf("\r\n");
duchonic 3:12c871b2412d 59 }
duchonic 3:12c871b2412d 60 else
duchonic 3:12c871b2412d 61 {
duchonic 3:12c871b2412d 62 led4 = true;
duchonic 3:12c871b2412d 63 led3 = false;
duchonic 3:12c871b2412d 64 printf("no tag (%02x)\r\n", blub);
duchonic 3:12c871b2412d 65 }
duchonic 0:57769c77d24e 66 return(returnValue);
duchonic 0:57769c77d24e 67 }
duchonic 0:57769c77d24e 68
duchonic 0:57769c77d24e 69 /** private functions */
duchonic 0:57769c77d24e 70
duchonic 1:549e63ac990c 71 void CR95HF::nfcWrite(uint8_t cmd[], uint8_t len)
duchonic 0:57769c77d24e 72 {
duchonic 1:549e63ac990c 73 for(int pos=0; pos<len;pos++)
duchonic 1:549e63ac990c 74 {
duchonic 1:549e63ac990c 75 nfcDevice.putc(cmd[pos]);
duchonic 1:549e63ac990c 76 }
duchonic 1:549e63ac990c 77 }
duchonic 1:549e63ac990c 78
duchonic 1:549e63ac990c 79 void CR95HF::rxCallbackNFC()
duchonic 1:549e63ac990c 80 {
duchonic 3:12c871b2412d 81 uint8_t character;
duchonic 3:12c871b2412d 82
duchonic 1:549e63ac990c 83 while(nfcDevice.readable()) {
duchonic 3:12c871b2412d 84 character = nfcDevice.getc();
duchonic 3:12c871b2412d 85
duchonic 3:12c871b2412d 86 if(rxBufPos == 0) {
duchonic 3:12c871b2412d 87 blub = character;
duchonic 3:12c871b2412d 88 }
duchonic 3:12c871b2412d 89 else if(rxBufPos == 1) {
duchonic 3:12c871b2412d 90 len = character;
duchonic 3:12c871b2412d 91 }
duchonic 3:12c871b2412d 92 else if(rxBufPos == 2) {
duchonic 3:12c871b2412d 93
duchonic 3:12c871b2412d 94 }
duchonic 3:12c871b2412d 95 else if(rxBufPos == 3) {
duchonic 3:12c871b2412d 96
duchonic 3:12c871b2412d 97 }
duchonic 3:12c871b2412d 98 else {
duchonic 3:12c871b2412d 99 rxData[rxBufPos-4] = character;
duchonic 3:12c871b2412d 100 }
duchonic 3:12c871b2412d 101 rxBufPos++;
duchonic 3:12c871b2412d 102
duchonic 3:12c871b2412d 103 if(rxBufPos >= len) {
duchonic 3:12c871b2412d 104 rxMsgComplete = true;
duchonic 3:12c871b2412d 105 }
duchonic 3:12c871b2412d 106
duchonic 1:549e63ac990c 107 }
duchonic 3:12c871b2412d 108 }