stm32f10x单片机进阶--spi使用
使用SPI与外部flash(MX25L6406EM21)IC通信
- 连接方式
如上图所示,MCU通过SPI2与外部flash芯片进行相连接。
- MCUspi2初始化
定义SPI2中所使用的SPI端口号
spi2中通信功能初始化
读数据与写数据
- 指令说明
查看flash芯片的datasheet,mx25芯片支持按照页来写,按照扇区来擦除。page_program为 0x02 ,sector_erase为0x20。并且在向每个页中写数据前,必须先保证此页所在的扇区已经进行擦除操作。并且每一页最大写256byte ,一个扇区的大小为4Kbyte
- 写数据,按照页来写数据(写一页数据)
依据芯片写指令 0x06
void spi_flash_write_page(uint8_t* pBuffer,uint32_t write_address,uint16_t num_byte_to_write) //向页中写入数据,其中write_address必须为256的整数据部,即一页的开始位置, { uint16_t i=0; // debug_out("SpiPage:0x%08x Len:%d\r\n", write_address, num_byte_to_write); spi_flash_wait_busy(); //忙等待 spi_flash_write_enable(); spi_flash_wait_busy(); spi2_cs_low(); spi2_read_write_byte(MX25_PAGE_PROGRAM); spi2_read_write_byte((uint8_t) ( ((write_address) >> 16 )&0xFF) ); spi2_read_write_byte((uint8_t) ( ((write_address) >> 8 )&0xFF) ); spi2_read_write_byte((uint8_t) ( (write_address)&0xff ) ); for(i=0;i<num_byte_to_write;i++) { spi2_read_write_byte(pBuffer[i]); } spi2_cs_high(); spi_flash_wait_busy(); }
- 擦除扇区
void spi_flash_erase_sector(uint32_t addr) //addr地起为4K的倍数,清除一个扇区 { spi_flash_wait_busy(); spi_flash_write_enable(); spi_flash_wait_busy(); //½øÐвÁ³ý²Ù×÷ spi2_cs_low(); spi2_read_write_byte(MX25_SECTOR_ERASE); spi2_read_write_byte((uint8_t) ( ((addr) >> )&0xFF) ); spi2_read_write_byte((uint8_t) ( ((addr) >> )&0xFF) ); spi2_read_write_byte((uint8_t) ( (addr)&0xff ) ); spi2_cs_high(); spi_flash_wait_busy(); }
- 在指定地址处开始写入指定长度的数据
从指定地址,写入指定长度数据。也是从一个扇区的开始处进行写,计算出一共需要写入多少页,同时如果为一个扇区的开始处,记得需要进行扇区擦除操作。
void spi_flash_write(uint8_t* pBuffer,uint32_t address,uint16_t buffer_len) { uint16_t pageCount = ; uint16_t pageIndex = ; uint16_t length = ; uint16_t i=; pageCount = buffer_len / SPI_FLASH_PAGE_SIZE; if( ( buffer_len % SPI_FLASH_PAGE_SIZE ) != ) { pageCount += ; } while( pageIndex < pageCount ) { length = buffer_len - ( pageIndex * SPI_FLASH_PAGE_SIZE ) ; length = length > SPI_FLASH_PAGE_SIZE ? SPI_FLASH_PAGE_SIZE : length; if( ( ( address + pageIndex * SPI_FLASH_PAGE_SIZE ) % SPI_FLASH_SECTOR_SIZE ) == ) { spi_flash_erase_sector( address + pageIndex * SPI_FLASH_PAGE_SIZE ); } spi_flash_write_page(pBuffer + pageIndex * SPI_FLASH_PAGE_SIZE, address + pageIndex * SPI_FLASH_PAGE_SIZE, length ); pageIndex ++; } }
- 读数据
读数据相对于写数据来说,显得比较简单的多,从指定地址开始读,读取指定的长度
void spi_flash_read(uint8_t* pBuffer,uint32_t read_address,uint16_t num_byte_to_read) { uint16_t i=0; spi_flash_wait_busy(); spi2_cs_low(); spi2_read_write_byte(MX25_READ_DATA); spi2_read_write_byte((uint8_t) ( ((read_address) >> 16 )&0xFF) ); spi2_read_write_byte((uint8_t) ( ((read_address) >> 8 )&0xFF) ); spi2_read_write_byte((uint8_t) ( (read_address)&0xff ) ); for(i=0;i<num_byte_to_read;i++) { pBuffer[i]=spi2_read_write_byte(0xFF); } spi2_cs_high(); }
至此整个驱动编写完成。
相关推荐
yaneng 2020-06-10
老和山下的小学童 2020-02-22
lsfreeing 2020-02-13
quguang 2020-02-12
82921934 2019-12-15
supjia 2019-12-09
evanbai 2015-04-30
baskMMU 2019-11-04
internetofthings 2018-04-09
pbyanglove 2012-02-05
qifei 2012-06-13
caidigou 2018-03-21
lynjay 2019-09-05
lightindark 2018-03-21
JoeQiang 2016-01-28
qwerrr 2012-04-16
sinco嵌入式游记 2010-09-15
BMRBMR 2010-07-26
wangcaipang 2007-06-21