
Lecteur NFC sur STM32F746G-DISCO
Dependencies: TS_DISCO_F746NG mbed LCD_DISCO_F746NG BSP_DISCO_F746NG
/media/uploads/MatthieuBichet/interfa-age_nfc.pdf
main.cpp@0:88344fc82f63, 2019-06-04 (annotated)
- Committer:
- MatthieuBichet
- Date:
- Tue Jun 04 13:57:22 2019 +0000
- Revision:
- 0:88344fc82f63
- Child:
- 1:913f00f0b737
Version 1;
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:88344fc82f63 | 65 | void callback() |
MatthieuBichet | 0:88344fc82f63 | 66 | { |
MatthieuBichet | 0:88344fc82f63 | 67 | |
MatthieuBichet | 0:88344fc82f63 | 68 | |
MatthieuBichet | 0:88344fc82f63 | 69 | } |
MatthieuBichet | 0:88344fc82f63 | 70 | |
MatthieuBichet | 0:88344fc82f63 | 71 | int main() |
MatthieuBichet | 0:88344fc82f63 | 72 | { |
MatthieuBichet | 0:88344fc82f63 | 73 | //NFC.attach(&callback); |
MatthieuBichet | 0:88344fc82f63 | 74 | TS_StateTypeDef TS_State; |
MatthieuBichet | 0:88344fc82f63 | 75 | uint16_t x, y; |
MatthieuBichet | 0:88344fc82f63 | 76 | uint8_t text[30]; |
MatthieuBichet | 0:88344fc82f63 | 77 | uint8_t status; |
MatthieuBichet | 0:88344fc82f63 | 78 | uint8_t idx; |
MatthieuBichet | 0:88344fc82f63 | 79 | uint8_t cleared = 0; |
MatthieuBichet | 0:88344fc82f63 | 80 | uint8_t prev_nb_touches = 0; |
MatthieuBichet | 0:88344fc82f63 | 81 | |
MatthieuBichet | 0:88344fc82f63 | 82 | status = ts.Init(lcd.GetXSize(), lcd.GetYSize()); |
MatthieuBichet | 0:88344fc82f63 | 83 | if (status != TS_OK) { |
MatthieuBichet | 0:88344fc82f63 | 84 | lcd.Clear(LCD_COLOR_RED); |
MatthieuBichet | 0:88344fc82f63 | 85 | lcd.SetBackColor(LCD_COLOR_RED); |
MatthieuBichet | 0:88344fc82f63 | 86 | lcd.SetTextColor(LCD_COLOR_WHITE); |
MatthieuBichet | 0:88344fc82f63 | 87 | lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT FAIL", CENTER_MODE); |
MatthieuBichet | 0:88344fc82f63 | 88 | } else { |
MatthieuBichet | 0:88344fc82f63 | 89 | lcd.Clear(LCD_COLOR_GREEN); |
MatthieuBichet | 0:88344fc82f63 | 90 | lcd.SetBackColor(LCD_COLOR_GREEN); |
MatthieuBichet | 0:88344fc82f63 | 91 | lcd.SetTextColor(LCD_COLOR_WHITE); |
MatthieuBichet | 0:88344fc82f63 | 92 | lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT OK", CENTER_MODE); |
MatthieuBichet | 0:88344fc82f63 | 93 | } |
MatthieuBichet | 0:88344fc82f63 | 94 | |
MatthieuBichet | 0:88344fc82f63 | 95 | wait(1); |
MatthieuBichet | 0:88344fc82f63 | 96 | wake_card(); |
MatthieuBichet | 0:88344fc82f63 | 97 | pc.printf("\r\n"); |
MatthieuBichet | 0:88344fc82f63 | 98 | send_tag(); |
MatthieuBichet | 0:88344fc82f63 | 99 | lcd.Clear(LCD_COLOR_WHITE); |
MatthieuBichet | 0:88344fc82f63 | 100 | lcd.SetBackColor(LCD_COLOR_WHITE); |
MatthieuBichet | 0:88344fc82f63 | 101 | lcd.SetTextColor(LCD_COLOR_RED); |
MatthieuBichet | 0:88344fc82f63 | 102 | lcd.FillRect(100, 100, 100, 75); |
MatthieuBichet | 0:88344fc82f63 | 103 | lcd.DisplayStringAt(0, LINE(3), (uint8_t *) " SCAN", LEFT_MODE); |
MatthieuBichet | 0:88344fc82f63 | 104 | while(1) |
MatthieuBichet | 0:88344fc82f63 | 105 | {//lcd.Clear(LCD_COLOR_WHITE); |
MatthieuBichet | 0:88344fc82f63 | 106 | |
MatthieuBichet | 0:88344fc82f63 | 107 | //lcd.Clear(LCD_COLOR_WHITE); |
MatthieuBichet | 0:88344fc82f63 | 108 | |
MatthieuBichet | 0:88344fc82f63 | 109 | ts.GetState(&TS_State); |
MatthieuBichet | 0:88344fc82f63 | 110 | if (TS_State.touchDetected) |
MatthieuBichet | 0:88344fc82f63 | 111 | { |
MatthieuBichet | 0:88344fc82f63 | 112 | x = TS_State.touchX[0]; |
MatthieuBichet | 0:88344fc82f63 | 113 | y = TS_State.touchY[0]; |
MatthieuBichet | 0:88344fc82f63 | 114 | if((x >115) && (x <190) && (y>125) && (y<170)) |
MatthieuBichet | 0:88344fc82f63 | 115 | { |
MatthieuBichet | 0:88344fc82f63 | 116 | lcd.SetTextColor(LCD_COLOR_GREEN); |
MatthieuBichet | 0:88344fc82f63 | 117 | lcd.FillRect(100, 100, 100, 75); |
MatthieuBichet | 0:88344fc82f63 | 118 | send_tag(); |
MatthieuBichet | 0:88344fc82f63 | 119 | wait(0.200); |
MatthieuBichet | 0:88344fc82f63 | 120 | read_ACK(19); |
MatthieuBichet | 0:88344fc82f63 | 121 | } |
MatthieuBichet | 0:88344fc82f63 | 122 | else |
MatthieuBichet | 0:88344fc82f63 | 123 | { |
MatthieuBichet | 0:88344fc82f63 | 124 | lcd.SetTextColor(LCD_COLOR_RED); |
MatthieuBichet | 0:88344fc82f63 | 125 | lcd.FillRect(100, 100, 100, 75); |
MatthieuBichet | 0:88344fc82f63 | 126 | } |
MatthieuBichet | 0:88344fc82f63 | 127 | } |
MatthieuBichet | 0:88344fc82f63 | 128 | else{ |
MatthieuBichet | 0:88344fc82f63 | 129 | lcd.SetTextColor(LCD_COLOR_RED); |
MatthieuBichet | 0:88344fc82f63 | 130 | lcd.FillRect(100, 100, 100, 75); |
MatthieuBichet | 0:88344fc82f63 | 131 | } |
MatthieuBichet | 0:88344fc82f63 | 132 | //; |
MatthieuBichet | 0:88344fc82f63 | 133 | //firmware_version(); |
MatthieuBichet | 0:88344fc82f63 | 134 | //read_ACK(11); |
MatthieuBichet | 0:88344fc82f63 | 135 | //pc.printf("\r\n"); |
MatthieuBichet | 0:88344fc82f63 | 136 | |
MatthieuBichet | 0:88344fc82f63 | 137 | |
MatthieuBichet | 0:88344fc82f63 | 138 | |
MatthieuBichet | 0:88344fc82f63 | 139 | //wait(1); |
MatthieuBichet | 0:88344fc82f63 | 140 | q=0; |
MatthieuBichet | 0:88344fc82f63 | 141 | //while(NFC.readable()) |
MatthieuBichet | 0:88344fc82f63 | 142 | //{ |
MatthieuBichet | 0:88344fc82f63 | 143 | // message[q] = NFC.getc(); |
MatthieuBichet | 0:88344fc82f63 | 144 | //q++; |
MatthieuBichet | 0:88344fc82f63 | 145 | //pc.printf("%c",message[q]); |
MatthieuBichet | 0:88344fc82f63 | 146 | //} |
MatthieuBichet | 0:88344fc82f63 | 147 | 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 | 148 | lcd.DisplayStringAt(0,LINE(9), (uint8_t *)msg, CENTER_MODE); |
MatthieuBichet | 0:88344fc82f63 | 149 | |
MatthieuBichet | 0:88344fc82f63 | 150 | //lcd.SetTextColor(LCD_COLOR_RED); |
MatthieuBichet | 0:88344fc82f63 | 151 | //lcd.SetBackColor(LCD_COLOR_WHITE); |
MatthieuBichet | 0:88344fc82f63 | 152 | //lcd.FillRect(10,25,25,25); |
MatthieuBichet | 0:88344fc82f63 | 153 | //wait(1); |
MatthieuBichet | 0:88344fc82f63 | 154 | } |
MatthieuBichet | 0:88344fc82f63 | 155 | } |
MatthieuBichet | 0:88344fc82f63 | 156 |