一、介绍
RT-Thread使用SPI读写eeprom例程,芯片使用的是M95020,在AT32F407和STM32f107上已经测试使用过。
二、软件架构
软件架构说明 依赖 RT-Thread-V4.1.0 的SPI设备驱动框架。
三、修改
1、根据自己的硬件设计,修改SPI通道,并确定SPI正常。
2、修改EEPROM_CS_PIN和EEPROM_WP_PIN。
3、修改EEPROM_TYPE
#ifndef EEPROM_TYPE
#define EEPROM_TYPE EEPROM_M95020
#endif
#define BUS_NAME "spi1"
#define CS_GPIO_X GPIOA
#define CS_GPIO_PIN GPIO_PINS_4
#define SPI_DEVICE_NAME "m950x0"
#define EEPROM_WP_PIN GET_PIN(C, 4)
当前使用的电路如下:
四、测试接口
1、检查测试(向最后一个字节写入数据,再读出来进行判断)
eeprom_check
2、擦除(将指定的数据块擦除成指定的数据)
eeprom_erase [addr] [date] [len]
3、写入(写入随意数据)
eeprom_write [start addr] 11 22 33 44
4、读取
eeprom_read [addr] [count]
五、测试结果
eeprom检查
msh >eeprom_check
[D/eeprom] eeprom check ok
读取20个字节msh >eeprom_read 0 20
[I/eeprom] read buf[0]=0xff
[I/eeprom] read buf[1]=0xff
[I/eeprom] read buf[2]=0xff
[I/eeprom] read buf[3]=0xff
[I/eeprom] read buf[4]=0xff
[I/eeprom] read buf[5]=0xff
[I/eeprom] read buf[6]=0xff
[I/eeprom] read buf[7]=0xff
[I/eeprom] read buf[8]=0xff
[I/eeprom] read buf[9]=0xff
[I/eeprom] read buf[10]=0xff
[I/eeprom] read buf[11]=0xff
[I/eeprom] read buf[12]=0xff
[I/eeprom] read buf[13]=0xff
[I/eeprom] read buf[14]=0xff
[I/eeprom] read buf[15]=0xff
[I/eeprom] read buf[16]=0xff
[I/eeprom] read buf[17]=0xff
[I/eeprom] read buf[18]=0xff
[I/eeprom] read buf[19]=0xff
从地址0开始写入1~8msh >eeprom_write 0 1 2 3 4 5 6 7 8
[D/eeprom] buf[0]=1 (0x1)
[D/eeprom] buf[1]=2 (0x2)
[D/eeprom] buf[2]=3 (0x3)
[D/eeprom] buf[3]=4 (0x4)
[D/eeprom] buf[4]=5 (0x5)
[D/eeprom] buf[5]=6 (0x6)
[D/eeprom] buf[6]=7 (0x7)
[D/eeprom] buf[7]=8 (0x8)
从地址10开始,写入1~8
msh >eeprom_write 10 1 2 3 4 5 6 7 8
[D/eeprom] buf[0]=1 (0x1)
[D/eeprom] buf[1]=2 (0x2)
[D/eeprom] buf[2]=3 (0x3)
[D/eeprom] buf[3]=4 (0x4)
[D/eeprom] buf[4]=5 (0x5)
[D/eeprom] buf[5]=6 (0x6)
[D/eeprom] buf[6]=7 (0x7)
[D/eeprom] buf[7]=8 (0x8)
读取后对比msh >eeprom_read 0 20
[I/eeprom] read buf[0]=0x1
[I/eeprom] read buf[1]=0x2
[I/eeprom] read buf[2]=0x3
[I/eeprom] read buf[3]=0x4
[I/eeprom] read buf[4]=0x5
[I/eeprom] read buf[5]=0x6
[I/eeprom] read buf[6]=0x7
[I/eeprom] read buf[7]=0x8
[I/eeprom] read buf[8]=0xff
[I/eeprom] read buf[9]=0xff
[I/eeprom] read buf[10]=0x1
[I/eeprom] read buf[11]=0x2
[I/eeprom] read buf[12]=0x3
[I/eeprom] read buf[13]=0x4
[I/eeprom] read buf[14]=0x5
[I/eeprom] read buf[15]=0x6
[I/eeprom] read buf[16]=0x7
[I/eeprom] read buf[17]=0x8
[I/eeprom] read buf[18]=0xff
[I/eeprom] read buf[19]=0xff