.
EEPROM4UID.cpp@0:c8ec63cf1007, 2022-01-23 (annotated)
- Committer:
- anyela
- Date:
- Sun Jan 23 05:06:09 2022 +0000
- Revision:
- 0:c8ec63cf1007
V
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
anyela | 0:c8ec63cf1007 | 1 | #include "EEPROM4UID.h" |
anyela | 0:c8ec63cf1007 | 2 | |
anyela | 0:c8ec63cf1007 | 3 | EEPROM4UID::EEPROM4UID(PinName SDA,PinName SCL,uint16_t epraddr):eeprom(SDA,SCL){ |
anyela | 0:c8ec63cf1007 | 4 | eeprom.frequency(400000); |
anyela | 0:c8ec63cf1007 | 5 | EPR_ADDR = epraddr; |
anyela | 0:c8ec63cf1007 | 6 | } |
anyela | 0:c8ec63cf1007 | 7 | |
anyela | 0:c8ec63cf1007 | 8 | // Funciòn para escrbir en la EEPROM |
anyela | 0:c8ec63cf1007 | 9 | int8_t EEPROM4UID::writeStr_EEPROM(uint16_t mem_addr, uint8_t* str){ |
anyela | 0:c8ec63cf1007 | 10 | eeprom.start(); |
anyela | 0:c8ec63cf1007 | 11 | int ack=eeprom.write(EPR_ADDR<<1); |
anyela | 0:c8ec63cf1007 | 12 | //printf("%d\n",ack); |
anyela | 0:c8ec63cf1007 | 13 | if(ack==1){ |
anyela | 0:c8ec63cf1007 | 14 | eeprom.write(mem_addr>>8);//MSB de la posicion inicial a escribir |
anyela | 0:c8ec63cf1007 | 15 | eeprom.write(mem_addr);//LSB de la posicion inicial a escribir |
anyela | 0:c8ec63cf1007 | 16 | int i=0; |
anyela | 0:c8ec63cf1007 | 17 | while(str[i]!='\n'){ |
anyela | 0:c8ec63cf1007 | 18 | eeprom.write(str[i]); |
anyela | 0:c8ec63cf1007 | 19 | i++; |
anyela | 0:c8ec63cf1007 | 20 | } |
anyela | 0:c8ec63cf1007 | 21 | eeprom.stop(); |
anyela | 0:c8ec63cf1007 | 22 | printf("escritura completa\n"); |
anyela | 0:c8ec63cf1007 | 23 | return 1; |
anyela | 0:c8ec63cf1007 | 24 | } |
anyela | 0:c8ec63cf1007 | 25 | |
anyela | 0:c8ec63cf1007 | 26 | else if(ack==0){ |
anyela | 0:c8ec63cf1007 | 27 | printf("NACK"); |
anyela | 0:c8ec63cf1007 | 28 | return -1; |
anyela | 0:c8ec63cf1007 | 29 | } |
anyela | 0:c8ec63cf1007 | 30 | else if(ack==2){ |
anyela | 0:c8ec63cf1007 | 31 | printf("TIMEOUT"); |
anyela | 0:c8ec63cf1007 | 32 | return -2; |
anyela | 0:c8ec63cf1007 | 33 | } |
anyela | 0:c8ec63cf1007 | 34 | return 0; |
anyela | 0:c8ec63cf1007 | 35 | } |
anyela | 0:c8ec63cf1007 | 36 | |
anyela | 0:c8ec63cf1007 | 37 | // Funcciòn para leer en la EPPROM |
anyela | 0:c8ec63cf1007 | 38 | int8_t EEPROM4UID::readByte_EEPROM(uint16_t mem_addr, uint8_t* byte){ |
anyela | 0:c8ec63cf1007 | 39 | //secuencia de lectura de 1 byte desde una direccion especifica de la eeprom |
anyela | 0:c8ec63cf1007 | 40 | eeprom.start(); |
anyela | 0:c8ec63cf1007 | 41 | int ack=eeprom.write(EPR_ADDR<<1); |
anyela | 0:c8ec63cf1007 | 42 | if(ack==1){ |
anyela | 0:c8ec63cf1007 | 43 | eeprom.write(mem_addr>>8);//MSB de la posicion a leer |
anyela | 0:c8ec63cf1007 | 44 | eeprom.write(mem_addr); //LSB de la posicion a leer |
anyela | 0:c8ec63cf1007 | 45 | eeprom.start(); |
anyela | 0:c8ec63cf1007 | 46 | eeprom.write(EPR_ADDR<<1|0x01); |
anyela | 0:c8ec63cf1007 | 47 | *byte=eeprom.read(0); |
anyela | 0:c8ec63cf1007 | 48 | eeprom.stop(); |
anyela | 0:c8ec63cf1007 | 49 | return 1; |
anyela | 0:c8ec63cf1007 | 50 | } |
anyela | 0:c8ec63cf1007 | 51 | else if(ack==0){ |
anyela | 0:c8ec63cf1007 | 52 | printf("NACK"); |
anyela | 0:c8ec63cf1007 | 53 | return -1; |
anyela | 0:c8ec63cf1007 | 54 | } |
anyela | 0:c8ec63cf1007 | 55 | else if(ack==2){ |
anyela | 0:c8ec63cf1007 | 56 | printf("TIMEOUT"); |
anyela | 0:c8ec63cf1007 | 57 | return -2; |
anyela | 0:c8ec63cf1007 | 58 | } |
anyela | 0:c8ec63cf1007 | 59 | return 0; |
anyela | 0:c8ec63cf1007 | 60 | } |
anyela | 0:c8ec63cf1007 | 61 | |
anyela | 0:c8ec63cf1007 | 62 | // esta función actualiza el UID |
anyela | 0:c8ec63cf1007 | 63 | void EEPROM4UID::updt_uidn(NUID &num){ // se declara dentro de la calse EEPROM4UID |
anyela | 0:c8ec63cf1007 | 64 | uint8_t initUIDnum[]={num.uidn_hl[1],num.uidn_hl[0],'\n'}; |
anyela | 0:c8ec63cf1007 | 65 | writeStr_EEPROM(0x0000,initUIDnum); // se escribe desde la posición cero |
anyela | 0:c8ec63cf1007 | 66 | } |
anyela | 0:c8ec63cf1007 | 67 | uint16_t EEPROM4UID::wrt_newUID(UID &newUID){ |
anyela | 0:c8ec63cf1007 | 68 | uint8_t newUID_f[]={newUID.bytes[3],newUID.bytes[2],newUID.bytes[1],newUID.bytes[0],'\n'}; |
anyela | 0:c8ec63cf1007 | 69 | writeStr_EEPROM(4*n.uidn+2,newUID_f); |
anyela | 0:c8ec63cf1007 | 70 | n.uidn++; |
anyela | 0:c8ec63cf1007 | 71 | this->updt_uidn(n); // actualizar el # de UIDS que hay en la memoria |
anyela | 0:c8ec63cf1007 | 72 | return n.uidn; // # de UIDS |
anyela | 0:c8ec63cf1007 | 73 | |
anyela | 0:c8ec63cf1007 | 74 | |
anyela | 0:c8ec63cf1007 | 75 | } |
anyela | 0:c8ec63cf1007 | 76 | |
anyela | 0:c8ec63cf1007 | 77 | |
anyela | 0:c8ec63cf1007 | 78 | uint16_t EEPROM4UID::read_uidn(){ |
anyela | 0:c8ec63cf1007 | 79 | uint8_t NUIDH, NUIDL; |
anyela | 0:c8ec63cf1007 | 80 | readByte_EEPROM(0x0000,&NUIDH);//leer cantidad de uid parte alta |
anyela | 0:c8ec63cf1007 | 81 | readByte_EEPROM(0x0001,&NUIDL);// " " parte baja |
anyela | 0:c8ec63cf1007 | 82 | n.uidn= NUIDH<<8 | NUIDL; |
anyela | 0:c8ec63cf1007 | 83 | return n.uidn; |
anyela | 0:c8ec63cf1007 | 84 | } |
anyela | 0:c8ec63cf1007 | 85 | |
anyela | 0:c8ec63cf1007 | 86 | // Función que vuelca la bse de datos de la eeprom a al ram de la mbed |
anyela | 0:c8ec63cf1007 | 87 | void EEPROM4UID::read_database(UID* dtbse){ |
anyela | 0:c8ec63cf1007 | 88 | for(int i=0; i<n.uidn; i++){ |
anyela | 0:c8ec63cf1007 | 89 | for(int j=0; j<4; j++){ // este for es porque se lee byte a byte |
anyela | 0:c8ec63cf1007 | 90 | readByte_EEPROM(i*4+j+2,&(dtbse[i].bytes[j])); |
anyela | 0:c8ec63cf1007 | 91 | // Las primeras dos posiciones de la memoria están ocupadas por el # de UIDs |
anyela | 0:c8ec63cf1007 | 92 | } |
anyela | 0:c8ec63cf1007 | 93 | } |
anyela | 0:c8ec63cf1007 | 94 | } |
anyela | 0:c8ec63cf1007 | 95 | |
anyela | 0:c8ec63cf1007 | 96 | |
anyela | 0:c8ec63cf1007 | 97 | /* |
anyela | 0:c8ec63cf1007 | 98 | //comparar desde el key |
anyela | 0:c8ec63cf1007 | 99 | // ESTE ES EL ALGORITMO DE BUSQUEDA |
anyela | 0:c8ec63cf1007 | 100 | void EEPROM4UID::busqueda(UID* dtse,uint16_t N,UID &IDbus){ |
anyela | 0:c8ec63cf1007 | 101 | |
anyela | 0:c8ec63cf1007 | 102 | // if(updt_uidn(n)> 0){ // updt es quien actualiza numero de UIDs |
anyela | 0:c8ec63cf1007 | 103 | for(int i=0; i<N; i++){ |
anyela | 0:c8ec63cf1007 | 104 | if (dtse[i].key == IDbus.key){ |
anyela | 0:c8ec63cf1007 | 105 | printf("El UID a buscar se encuentra en la base de datos \n") ; |
anyela | 0:c8ec63cf1007 | 106 | } |
anyela | 0:c8ec63cf1007 | 107 | printf("El UID a buscar NO se encuentra en la base de datos \n") ; |
anyela | 0:c8ec63cf1007 | 108 | } |
anyela | 0:c8ec63cf1007 | 109 | } |
anyela | 0:c8ec63cf1007 | 110 | */ |
anyela | 0:c8ec63cf1007 | 111 | |
anyela | 0:c8ec63cf1007 | 112 |