|
@@ -9,27 +9,13 @@
|
|
|
|
|
|
#define SPI_FLASH SPI3
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
#define INCLUDE_SPI_TEST 0
|
|
|
|
|
|
-//SemaphoreHandle_t spi_mutex;
|
|
|
-
|
|
|
-static uint8_t spi_tx_rx(uint8_t byte) {
|
|
|
- return common_spi_tx_rx(SPI_FLASH, byte);
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
#define CMD_WREN 0x06 // +
|
|
|
#define CMD_WRDI 0x04 // +
|
|
|
#define CMD_WRSR 0x01 // +
|
|
|
#define CMD_RDSR 0x05 // +
|
|
|
-#if defined (FLASH_TYPE_AT25SF161)
|
|
|
-#define CMD_READ 0x03 //0x0B // +
|
|
|
-#elif defined (FLASH_TYPE_MX25L1606E)
|
|
|
#define CMD_READ 0x03 // +
|
|
|
-#endif
|
|
|
#define CMD_SE 0x20 // + Block Erse 4 Kbytes
|
|
|
#define CMD_BE 0x52 // Block Erase 32 Kbytes
|
|
|
#define CMD_CE 0x60 // + Chip Erase
|
|
@@ -40,35 +26,57 @@ static uint8_t spi_tx_rx(uint8_t byte) {
|
|
|
#define SR_WEL (1 << 1)
|
|
|
#define SR_SRWD (1 << 7)
|
|
|
|
|
|
+
|
|
|
static spi_flash_desc_t spi_flash_desc;
|
|
|
|
|
|
-static inline void wait_write_enable(void) {
|
|
|
+
|
|
|
+//
|
|
|
+static uint8_t spi_tx_rx(uint8_t byte)
|
|
|
+{
|
|
|
+ return common_spi_tx_rx(SPI_FLASH, byte);
|
|
|
+}
|
|
|
+
|
|
|
+//
|
|
|
+static inline void wait_write_enable(void)
|
|
|
+{
|
|
|
uint8_t status;
|
|
|
+
|
|
|
SPI_FLASH_CS_L();
|
|
|
spi_tx_rx(CMD_RDSR);
|
|
|
- do {
|
|
|
+
|
|
|
+ do {
|
|
|
status = spi_tx_rx(0);
|
|
|
} while (!(status & SR_WEL));
|
|
|
+
|
|
|
SPI_FLASH_CS_H();
|
|
|
}
|
|
|
|
|
|
-static inline void wait_write_end(void) {
|
|
|
+//
|
|
|
+static inline void wait_write_end(void)
|
|
|
+{
|
|
|
uint8_t status;
|
|
|
+
|
|
|
SPI_FLASH_CS_L();
|
|
|
spi_tx_rx(CMD_RDSR);
|
|
|
- do {
|
|
|
+
|
|
|
+ do {
|
|
|
status = spi_tx_rx(0);
|
|
|
} while (status & SR_WIP);
|
|
|
- SPI_FLASH_CS_H();
|
|
|
+
|
|
|
+ SPI_FLASH_CS_H();
|
|
|
}
|
|
|
|
|
|
-static inline void send_addr(int addr) {
|
|
|
+//
|
|
|
+static inline void send_addr(int addr)
|
|
|
+{
|
|
|
spi_tx_rx((addr >> 16) & 0xFF);
|
|
|
spi_tx_rx((addr >> 8) & 0xFF);
|
|
|
spi_tx_rx(addr & 0xFF);
|
|
|
}
|
|
|
|
|
|
-static int spi_flash_read_sfdp(int addr, void *buf, size_t len) {
|
|
|
+//
|
|
|
+static int spi_flash_read_sfdp(int addr, void *buf, size_t len)
|
|
|
+{
|
|
|
uint8_t *ptr = (uint8_t*)buf;
|
|
|
//xSemaphoreTake(spi_mutex, portMAX_DELAY);
|
|
|
SPI_FLASH_CS_L();
|
|
@@ -82,7 +90,9 @@ static int spi_flash_read_sfdp(int addr, void *buf, size_t len) {
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-ssize_t spi_flash_read(int addr, void *buf, size_t len, uint32_t timeout) {
|
|
|
+//
|
|
|
+ssize_t spi_flash_read(int addr, void *buf, size_t len, uint32_t timeout)
|
|
|
+{
|
|
|
uint8_t *ptr = (uint8_t*)buf;
|
|
|
(void)timeout;
|
|
|
//xSemaphoreTake(spi_mutex, portMAX_DELAY);
|
|
@@ -98,7 +108,8 @@ ssize_t spi_flash_read(int addr, void *buf, size_t len, uint32_t timeout) {
|
|
|
|
|
|
#define TIMEOUT 10000
|
|
|
|
|
|
-uint16_t spi_flash_pp(int addr, const void *buf, size_t len, uint32_t timeout) {
|
|
|
+uint16_t spi_flash_pp(int addr, const void *buf, size_t len, uint32_t timeout)
|
|
|
+{
|
|
|
uint8_t *ptr = (uint8_t*)buf;
|
|
|
(void)timeout;
|
|
|
ssize_t ret = 0;
|
|
@@ -125,7 +136,9 @@ uint16_t spi_flash_pp(int addr, const void *buf, size_t len, uint32_t timeout) {
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
-ssize_t spi_flash_write(int addr, const void *buf, size_t len, uint32_t timeout) {
|
|
|
+//
|
|
|
+ssize_t spi_flash_write(int addr, const void *buf, size_t len, uint32_t timeout)
|
|
|
+{
|
|
|
(void)timeout;
|
|
|
int ret = 0, offset = 0;
|
|
|
do {
|
|
@@ -136,7 +149,9 @@ ssize_t spi_flash_write(int addr, const void *buf, size_t len, uint32_t timeout)
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-int spi_flash_chip_erase(uint32_t timeout) {
|
|
|
+//
|
|
|
+int spi_flash_chip_erase(uint32_t timeout)
|
|
|
+{
|
|
|
(void)timeout;
|
|
|
//xSemaphoreTake(spi_mutex, portMAX_DELAY);
|
|
|
SPI_FLASH_CS_L();
|
|
@@ -153,7 +168,9 @@ int spi_flash_chip_erase(uint32_t timeout) {
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-int spi_flash_erase_sector(int addr, uint32_t timeout) {
|
|
|
+//
|
|
|
+int spi_flash_erase_sector(int addr, uint32_t timeout)
|
|
|
+{
|
|
|
(void)timeout;
|
|
|
//xSemaphoreTake(spi_mutex, portMAX_DELAY);
|
|
|
SPI_FLASH_CS_L();
|
|
@@ -172,23 +189,27 @@ int spi_flash_erase_sector(int addr, uint32_t timeout) {
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
+//
|
|
|
unsigned int spi_flash_get_sector_size(void) {
|
|
|
return spi_flash_desc.sector_size;
|
|
|
}
|
|
|
|
|
|
+//
|
|
|
unsigned int spi_flash_get_sector_count(void) {
|
|
|
return spi_flash_desc.sector_count;
|
|
|
}
|
|
|
|
|
|
+//
|
|
|
unsigned int spi_flash_get_total_size(void) {
|
|
|
return spi_flash_desc.sector_count * spi_flash_desc.sector_size;
|
|
|
}
|
|
|
|
|
|
+//
|
|
|
bool spi_flash_is_init(void) {
|
|
|
return spi_flash_desc.present;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+//
|
|
|
bool spi_flash_init(void)
|
|
|
{
|
|
|
bool ret = false;
|
|
@@ -197,6 +218,7 @@ bool spi_flash_init(void)
|
|
|
SPI_FLASH_CS_H();
|
|
|
|
|
|
spi_flash_get_id(tmp);
|
|
|
+
|
|
|
if (!(tmp[0] == 0x1F && tmp[1] == 0x86 && tmp[2] == 0x01))
|
|
|
{
|
|
|
ret = false;
|
|
@@ -222,26 +244,6 @@ bool spi_flash_init(void)
|
|
|
return false;
|
|
|
|
|
|
#if 0
|
|
|
-#if defined (FLASH_TYPE_AT25SF161)
|
|
|
-
|
|
|
- uint8_t tmp[4] = {0};
|
|
|
-
|
|
|
- SPI_FLASH_CS_H();
|
|
|
-
|
|
|
- spi_flash_get_id(tmp);
|
|
|
- if (!(tmp[0] == 0x1F && tmp[1] == 0x86 && tmp[2] == 0x01))
|
|
|
- return 0;
|
|
|
-
|
|
|
- spi_flash_desc.sector_size = SPI_FLASH_SECTOR_SIZE;
|
|
|
- spi_flash_desc.sector_erase_op = 32;
|
|
|
- spi_flash_desc.sector_count = 512;
|
|
|
- spi_flash_desc.present = true;
|
|
|
-
|
|
|
-
|
|
|
- return 1;
|
|
|
-
|
|
|
-#elif defined (FLASH_TYPE_MX25L1606E)
|
|
|
-
|
|
|
uint32_t i, ptable, bitsize = 0;
|
|
|
uint8_t tmp[4];
|
|
|
|
|
@@ -288,12 +290,8 @@ bool spi_flash_init(void)
|
|
|
return 0;
|
|
|
|
|
|
spi_flash_desc.present = true;
|
|
|
-
|
|
|
- return 1;
|
|
|
-
|
|
|
-#endif
|
|
|
- return 0;
|
|
|
#endif
|
|
|
+ return 1;
|
|
|
}
|
|
|
|
|
|
|
|
@@ -320,15 +318,14 @@ void init_spi_mutex(void)
|
|
|
|
|
|
void spi_flash_get_id(uint8_t *buf)
|
|
|
{
|
|
|
- //xSemaphoreTake(spi_mutex, portMAX_DELAY);
|
|
|
SPI_FLASH_CS_L();
|
|
|
spi_tx_rx(0x9F);
|
|
|
+
|
|
|
*buf++ = spi_tx_rx(0);
|
|
|
*buf++ = spi_tx_rx(0);
|
|
|
*buf++ = spi_tx_rx(0);
|
|
|
|
|
|
SPI_FLASH_CS_H();
|
|
|
- //xSemaphoreGive(spi_mutex);
|
|
|
}
|
|
|
|
|
|
// -------------------------------------------------------------------------- //
|