Lecteur NFC sur STM32F746G-DISCO

Dependencies:   TS_DISCO_F746NG mbed LCD_DISCO_F746NG BSP_DISCO_F746NG

/media/uploads/MatthieuBichet/interfa-age_nfc.pdf

Committer:
MatthieuBichet
Date:
Tue Jun 04 14:20:35 2019 +0000
Revision:
1:913f00f0b737
Parent:
0:88344fc82f63
Version 1 : Scan du tag NFC;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MatthieuBichet 0:88344fc82f63 1 #include "mbed.h"
MatthieuBichet 0:88344fc82f63 2 #include "LCD_DISCO_F746NG.h"
MatthieuBichet 0:88344fc82f63 3 #include "TS_DISCO_F746NG.h"
MatthieuBichet 0:88344fc82f63 4 LCD_DISCO_F746NG lcd;
MatthieuBichet 0:88344fc82f63 5 TS_DISCO_F746NG ts;
MatthieuBichet 0:88344fc82f63 6
MatthieuBichet 0:88344fc82f63 7 Serial NFC(PF_7,PF_6,115200);
MatthieuBichet 0:88344fc82f63 8 Serial pc(USBTX, USBRX,115200);
MatthieuBichet 0:88344fc82f63 9 char message[25];
MatthieuBichet 0:88344fc82f63 10 char msg[25];
MatthieuBichet 0:88344fc82f63 11 DigitalOut led1(LED1);
MatthieuBichet 0:88344fc82f63 12 int q=0;
MatthieuBichet 0:88344fc82f63 13 const unsigned char wake[24]={
MatthieuBichet 0:88344fc82f63 14 0x55, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
MatthieuBichet 0:88344fc82f63 15 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x03, 0xfd, 0xd4, 0x14, 0x01, 0x17, 0x00};//wake up NFC module
MatthieuBichet 0:88344fc82f63 16 const unsigned char firmware[9]={
MatthieuBichet 0:88344fc82f63 17 0x00, 0x00, 0xFF, 0x02, 0xFE, 0xD4, 0x02, 0x2A, 0x00};//
MatthieuBichet 0:88344fc82f63 18 const unsigned char tag[11]={
MatthieuBichet 0:88344fc82f63 19 0x00, 0x00, 0xFF, 0x04, 0xFC, 0xD4, 0x4A, 0x01, 0x00, 0xE1, 0x00};//detecting tag command
MatthieuBichet 0:88344fc82f63 20 const unsigned char std_ACK[25] = {
MatthieuBichet 0:88344fc82f63 21 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00, 0x00, 0xFF, 0x0C, \
MatthieuBichet 0:88344fc82f63 22 0xF4, 0xD5, 0x4B, 0x01, 0x01, 0x00, 0x04, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x4b, 0x00};
MatthieuBichet 0:88344fc82f63 23 unsigned char old_id[5];
MatthieuBichet 0:88344fc82f63 24
MatthieuBichet 0:88344fc82f63 25 unsigned char receive_ACK[25];//Command receiving buffer
MatthieuBichet 0:88344fc82f63 26
MatthieuBichet 0:88344fc82f63 27 void NFC_Send_Byte(unsigned char command_data){//send byte to PC
MatthieuBichet 0:88344fc82f63 28 NFC.putc(command_data);
MatthieuBichet 0:88344fc82f63 29 }
MatthieuBichet 0:88344fc82f63 30 void send_tag(void){//send tag[] to device
MatthieuBichet 0:88344fc82f63 31 unsigned char i;
MatthieuBichet 0:88344fc82f63 32 for(i=0;i<11;i++) //send command
MatthieuBichet 0:88344fc82f63 33 NFC_Send_Byte(tag[i]);
MatthieuBichet 0:88344fc82f63 34 }
MatthieuBichet 0:88344fc82f63 35 void wake_card(void){//send wake[] to device
MatthieuBichet 0:88344fc82f63 36 unsigned char i;
MatthieuBichet 0:88344fc82f63 37 for(i=0;i<24;i++) //send command
MatthieuBichet 0:88344fc82f63 38 NFC_Send_Byte(wake[i]);
MatthieuBichet 0:88344fc82f63 39 }
MatthieuBichet 0:88344fc82f63 40
MatthieuBichet 0:88344fc82f63 41
MatthieuBichet 0:88344fc82f63 42
MatthieuBichet 0:88344fc82f63 43 void read_ACK(unsigned char temp){//read ACK into reveive_ACK[]
MatthieuBichet 0:88344fc82f63 44 unsigned char i;
MatthieuBichet 0:88344fc82f63 45 for(i=0;i<temp;i++) {
MatthieuBichet 0:88344fc82f63 46 message[i]= NFC.getc();
MatthieuBichet 0:88344fc82f63 47 }
MatthieuBichet 0:88344fc82f63 48 }
MatthieuBichet 0:88344fc82f63 49 void pc_Send_Byte(unsigned char command_data){//send byte to PC
MatthieuBichet 0:88344fc82f63 50 pc.putc(command_data);
MatthieuBichet 0:88344fc82f63 51 }
MatthieuBichet 0:88344fc82f63 52
MatthieuBichet 0:88344fc82f63 53 void display(unsigned char tem){//send receive_ACK[] to PC
MatthieuBichet 0:88344fc82f63 54 unsigned char i;
MatthieuBichet 0:88344fc82f63 55 for(i=0;i<tem;i++) //send command
MatthieuBichet 0:88344fc82f63 56 pc_Send_Byte(receive_ACK[i]);
MatthieuBichet 0:88344fc82f63 57 pc.printf("\r\n");
MatthieuBichet 0:88344fc82f63 58 }
MatthieuBichet 0:88344fc82f63 59
MatthieuBichet 0:88344fc82f63 60 void firmware_version(void){//send fireware[] to device
MatthieuBichet 0:88344fc82f63 61 unsigned char i;
MatthieuBichet 0:88344fc82f63 62 for(i=0;i<9;i++) //send command
MatthieuBichet 0:88344fc82f63 63 NFC_Send_Byte(firmware[i]);
MatthieuBichet 0:88344fc82f63 64 }
MatthieuBichet 1:913f00f0b737 65
MatthieuBichet 0:88344fc82f63 66
MatthieuBichet 0:88344fc82f63 67 int main()
MatthieuBichet 0:88344fc82f63 68 {
MatthieuBichet 1:913f00f0b737 69
MatthieuBichet 0:88344fc82f63 70 TS_StateTypeDef TS_State;
MatthieuBichet 0:88344fc82f63 71 uint16_t x, y;
MatthieuBichet 0:88344fc82f63 72 uint8_t text[30];
MatthieuBichet 0:88344fc82f63 73 uint8_t status;
MatthieuBichet 0:88344fc82f63 74 uint8_t idx;
MatthieuBichet 0:88344fc82f63 75 uint8_t cleared = 0;
MatthieuBichet 0:88344fc82f63 76 uint8_t prev_nb_touches = 0;
MatthieuBichet 0:88344fc82f63 77
MatthieuBichet 0:88344fc82f63 78 status = ts.Init(lcd.GetXSize(), lcd.GetYSize());
MatthieuBichet 0:88344fc82f63 79 if (status != TS_OK) {
MatthieuBichet 0:88344fc82f63 80 lcd.Clear(LCD_COLOR_RED);
MatthieuBichet 0:88344fc82f63 81 lcd.SetBackColor(LCD_COLOR_RED);
MatthieuBichet 0:88344fc82f63 82 lcd.SetTextColor(LCD_COLOR_WHITE);
MatthieuBichet 0:88344fc82f63 83 lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT FAIL", CENTER_MODE);
MatthieuBichet 0:88344fc82f63 84 } else {
MatthieuBichet 0:88344fc82f63 85 lcd.Clear(LCD_COLOR_GREEN);
MatthieuBichet 0:88344fc82f63 86 lcd.SetBackColor(LCD_COLOR_GREEN);
MatthieuBichet 0:88344fc82f63 87 lcd.SetTextColor(LCD_COLOR_WHITE);
MatthieuBichet 0:88344fc82f63 88 lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT OK", CENTER_MODE);
MatthieuBichet 0:88344fc82f63 89 }
MatthieuBichet 0:88344fc82f63 90
MatthieuBichet 0:88344fc82f63 91 wait(1);
MatthieuBichet 0:88344fc82f63 92 wake_card();
MatthieuBichet 0:88344fc82f63 93 pc.printf("\r\n");
MatthieuBichet 0:88344fc82f63 94 send_tag();
MatthieuBichet 0:88344fc82f63 95 lcd.Clear(LCD_COLOR_WHITE);
MatthieuBichet 0:88344fc82f63 96 lcd.SetBackColor(LCD_COLOR_WHITE);
MatthieuBichet 0:88344fc82f63 97 lcd.SetTextColor(LCD_COLOR_RED);
MatthieuBichet 0:88344fc82f63 98 lcd.FillRect(100, 100, 100, 75);
MatthieuBichet 0:88344fc82f63 99 lcd.DisplayStringAt(0, LINE(3), (uint8_t *) " SCAN", LEFT_MODE);
MatthieuBichet 0:88344fc82f63 100 while(1)
MatthieuBichet 1:913f00f0b737 101 {
MatthieuBichet 0:88344fc82f63 102
MatthieuBichet 0:88344fc82f63 103 ts.GetState(&TS_State);
MatthieuBichet 0:88344fc82f63 104 if (TS_State.touchDetected)
MatthieuBichet 0:88344fc82f63 105 {
MatthieuBichet 0:88344fc82f63 106 x = TS_State.touchX[0];
MatthieuBichet 0:88344fc82f63 107 y = TS_State.touchY[0];
MatthieuBichet 0:88344fc82f63 108 if((x >115) && (x <190) && (y>125) && (y<170))
MatthieuBichet 0:88344fc82f63 109 {
MatthieuBichet 0:88344fc82f63 110 lcd.SetTextColor(LCD_COLOR_GREEN);
MatthieuBichet 0:88344fc82f63 111 lcd.FillRect(100, 100, 100, 75);
MatthieuBichet 0:88344fc82f63 112 send_tag();
MatthieuBichet 0:88344fc82f63 113 wait(0.200);
MatthieuBichet 1:913f00f0b737 114 read_ACK(18);
MatthieuBichet 0:88344fc82f63 115 }
MatthieuBichet 0:88344fc82f63 116 else
MatthieuBichet 0:88344fc82f63 117 {
MatthieuBichet 0:88344fc82f63 118 lcd.SetTextColor(LCD_COLOR_RED);
MatthieuBichet 0:88344fc82f63 119 lcd.FillRect(100, 100, 100, 75);
MatthieuBichet 0:88344fc82f63 120 }
MatthieuBichet 0:88344fc82f63 121 }
MatthieuBichet 0:88344fc82f63 122 else{
MatthieuBichet 0:88344fc82f63 123 lcd.SetTextColor(LCD_COLOR_RED);
MatthieuBichet 0:88344fc82f63 124 lcd.FillRect(100, 100, 100, 75);
MatthieuBichet 0:88344fc82f63 125 }
MatthieuBichet 0:88344fc82f63 126
MatthieuBichet 0:88344fc82f63 127
MatthieuBichet 0:88344fc82f63 128
MatthieuBichet 0:88344fc82f63 129 q=0;
MatthieuBichet 0:88344fc82f63 130 sprintf(msg, "ID %d %02X%02X%02X%02X%02X%02X",q,(int)message[13],(int)message[14],(int)message[15],(int)message[16],(int)message[17],(int)message[18]);
MatthieuBichet 0:88344fc82f63 131 lcd.DisplayStringAt(0,LINE(9), (uint8_t *)msg, CENTER_MODE);
MatthieuBichet 0:88344fc82f63 132
MatthieuBichet 0:88344fc82f63 133 }
MatthieuBichet 0:88344fc82f63 134 }
MatthieuBichet 0:88344fc82f63 135