|
本帖最后由 XIVN1987 于 2024-1-8 17:51 编辑
源码:https://gitee.com/synwit-co-ltd/micropython
User Guide 中有每个外设的使用示例代码。。
读写 SPI FLash 示例代码:- import machine, time
- from machine import Pin, SPI
- from array import array
- spi = machine.SPI(1, 1000000, mosi='PC4', miso='PC5', sck='PC3')
- spi_ss = machine.Pin('PC6', Pin.OUT)
- spi_ss.high()
- GD25Q21_CMD_READ_CHIPID = 0x90
- GD25Q21_CMD_READ_DATA = 0x03
- GD25Q21_CMD_WRITE_PAGE = 0x02
- GD25Q21_CMD_ERASE_SECTOR = 0x20
- GD25Q21_CMD_WRITE_ENABLE = 0x06
- GD25Q21_CMD_WRITE_DISABLE = 0x04
- def flash_identify():
- spi_ss.low()
- spi.write(array('B', [GD25Q21_CMD_READ_CHIPID, 0x00, 0x00, 0x00]))
- memview = spi.read(2)
- spi_ss.high()
- return array('B', memview)
- def flash_erase(addr):
- spi_ss.low()
- spi.write(array('B', [GD25Q21_CMD_WRITE_ENABLE]))
- spi_ss.high()
- spi_ss.low()
- spi.write(array('B', [GD25Q21_CMD_ERASE_SECTOR, (addr >> 16) & 0xFF, (addr >> 8) & 0xFF, (addr >> 0) & 0xFF]))
- spi_ss.high()
- def flash_write(addr, buf):
- spi_ss.low()
- spi.write(array('B', [GD25Q21_CMD_WRITE_ENABLE]))
- spi_ss.high()
- spi_ss.low()
- spi.write(array('B', [GD25Q21_CMD_WRITE_PAGE, (addr >> 16) & 0xFF, (addr >> 8) & 0xFF, (addr >> 0) & 0xFF]))
- spi.write(buf)
- spi_ss.high()
- def flash_read(addr, cnt):
- spi_ss.low()
- spi.write(array('B', [GD25Q21_CMD_READ_DATA, (addr >> 16) & 0xFF, (addr >> 8) & 0xFF, (addr >> 0) & 0xFF]))
- memview = spi.read(cnt)
- spi_ss.high()
- return array('B', memview)
- flash_identify()
- flash_erase(0x1000)
- time.sleep(2)
- flash_read(0x1000, 32)
- flash_write(0x1000, array('B', [i for i in range(32)]))
- time.sleep(1)
- flash_read(0x1000, 32)
复制代码
|
|