test
Dependencies: mbed-STM32F103C8T6 SHT21_ncleee
eeprom.cpp@1:0fe432e5dfc4, 2020-04-15 (annotated)
- Committer:
- bbw
- Date:
- Wed Apr 15 11:36:03 2020 +0000
- Revision:
- 1:0fe432e5dfc4
Test_bob_0415
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bbw | 1:0fe432e5dfc4 | 1 | #include "eeprom.h" |
bbw | 1:0fe432e5dfc4 | 2 | #include "main.h" |
bbw | 1:0fe432e5dfc4 | 3 | |
bbw | 1:0fe432e5dfc4 | 4 | void readID(int addy){ |
bbw | 1:0fe432e5dfc4 | 5 | char ucdata_write[1]; |
bbw | 1:0fe432e5dfc4 | 6 | char ucdata_read[1]; |
bbw | 1:0fe432e5dfc4 | 7 | ucdata_write[0] = addy; //MCP24AA02_DID, Address here for Device ID |
bbw | 1:0fe432e5dfc4 | 8 | while (i2c.write((MCP24AA02_ADDR|WRITE), ucdata_write, 1, 0)){;}//Wait for ACK if EEPROM is in 'write' cycle |
bbw | 1:0fe432e5dfc4 | 9 | i2c.read((MCP24AA02_ADDR|READ),ucdata_read,1,0); |
bbw | 1:0fe432e5dfc4 | 10 | if(ucdata_read[0]==0x29){ |
bbw | 1:0fe432e5dfc4 | 11 | debug_uart.printf("Code=%#x (Microchip Technology ltd.(c))\r\n",ucdata_read[0]); |
bbw | 1:0fe432e5dfc4 | 12 | } |
bbw | 1:0fe432e5dfc4 | 13 | if(ucdata_read[0]==0x41){ |
bbw | 1:0fe432e5dfc4 | 14 | debug_uart.printf("Code=%#x (2K EEPROM Using i2c)\r\n",ucdata_read[0]); |
bbw | 1:0fe432e5dfc4 | 15 | }else{ |
bbw | 1:0fe432e5dfc4 | 16 | debug_uart.printf("Unknown eeprom device %x\r\n", ucdata_read[0]); |
bbw | 1:0fe432e5dfc4 | 17 | } |
bbw | 1:0fe432e5dfc4 | 18 | } |
bbw | 1:0fe432e5dfc4 | 19 | |
bbw | 1:0fe432e5dfc4 | 20 | void readEE(int addy){ |
bbw | 1:0fe432e5dfc4 | 21 | char ucdata_write[1]; |
bbw | 1:0fe432e5dfc4 | 22 | char ucdata_read[1]; |
bbw | 1:0fe432e5dfc4 | 23 | ucdata_write[0] = addy; //Address here to read |
bbw | 1:0fe432e5dfc4 | 24 | while (i2c.write((MCP24AA02_ADDR|WRITE), ucdata_write, 1, 0)){} //Wait for ACK if EEPROM is in 'write' cycle |
bbw | 1:0fe432e5dfc4 | 25 | i2c.read((MCP24AA02_ADDR|READ),ucdata_read,1,0); //Note 'OR' Address with Read bit |
bbw | 1:0fe432e5dfc4 | 26 | debug_uart.printf("%c",ucdata_read[0]); |
bbw | 1:0fe432e5dfc4 | 27 | } |
bbw | 1:0fe432e5dfc4 | 28 | |
bbw | 1:0fe432e5dfc4 | 29 | void writeEE(int addy,int data){ |
bbw | 1:0fe432e5dfc4 | 30 | char ucdata_write[2]; |
bbw | 1:0fe432e5dfc4 | 31 | ucdata_write[0] = addy; |
bbw | 1:0fe432e5dfc4 | 32 | ucdata_write[1] = data; |
bbw | 1:0fe432e5dfc4 | 33 | while (i2c.write((MCP24AA02_ADDR|WRITE), ucdata_write, 1, 0)){} //Wait for ACK if EEPROM is in 'write' cycle |
bbw | 1:0fe432e5dfc4 | 34 | i2c.write((MCP24AA02_ADDR|WRITE),ucdata_write,2,0); //Note 'OR' Adress with Write bit |
bbw | 1:0fe432e5dfc4 | 35 | debug_uart.printf("%04d %c\t",addy,data); |
bbw | 1:0fe432e5dfc4 | 36 | } |
bbw | 1:0fe432e5dfc4 | 37 | |
bbw | 1:0fe432e5dfc4 | 38 | void eraseEE(void){ |
bbw | 1:0fe432e5dfc4 | 39 | for (int i=0;i<0xFA;i++){ //0xFA to 0xFF are read only with Manufacture/Hardware ID and a Unique Serial Number |
bbw | 1:0fe432e5dfc4 | 40 | writeEE(i,0xFF); |
bbw | 1:0fe432e5dfc4 | 41 | } |
bbw | 1:0fe432e5dfc4 | 42 | } |