From f0d90404d995f4a023be047e7d431c81ff56ed5a Mon Sep 17 00:00:00 2001 From: Giovanni Di Sirio Date: Mon, 9 May 2016 10:23:42 +0000 Subject: Flash driver works. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@9454 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/ex/Micron/n25q128.c | 62 ++++++++++++++-- os/ex/Micron/n25q128.h | 7 +- os/hal/lib/peripherals/flash/hal_flash.h | 4 +- ...3xx-SPI-N25Q128 (OpenOCD, Flash and Run).launch | 2 +- testhal/STM32/STM32F3xx/SPI-N25Q128/main.c | 86 ++++++++++++++++++++++ testhal/STM32/STM32F3xx/SPI-N25Q128/mcuconf.h | 4 +- 6 files changed, 149 insertions(+), 16 deletions(-) diff --git a/os/ex/Micron/n25q128.c b/os/ex/Micron/n25q128.c index 57ff6df5e..b6a00170a 100644 --- a/os/ex/Micron/n25q128.c +++ b/os/ex/Micron/n25q128.c @@ -110,10 +110,10 @@ static flash_error_t flash_poll_status(N25Q128Driver *devp) { #endif /* Read status command.*/ spiSelect(spip); - flash_send_cmd(devp, N25Q128_CMD_READ_STATUS_REGISTER); + flash_send_cmd(devp, N25Q128_CMD_READ_FLAG_STATUS_REGISTER); spiReceive(spip, 1, &sts); spiUnselect(spip); - } while ((sts & N25Q128_STS_BUSY) != 0U); + } while ((sts & N25Q128_STS_BUSY) == 0U); /* Checking for errors.*/ if ((sts & N25Q128_STS_ALL_ERRORS) != 0U) { @@ -202,7 +202,6 @@ static flash_error_t erase_sectors(void *instance, spiUnselect(spip); (void) spiPolledExchange(spip, 0xFF); /* One frame delay.*/ - /* Sub-sector erase command.*/ spiSelect(spip); flash_send_cmd_addr(devp, N25Q128_CMD_SUBSECTOR_ERASE, addr); @@ -362,7 +361,7 @@ static flash_error_t read(void *instance, flash_address_t addr, * * @init */ -void n15q128ObjectInit(N25Q128Driver *devp) { +void n25q128ObjectInit(N25Q128Driver *devp) { osalDbgCheck(devp != NULL); @@ -379,15 +378,28 @@ void n15q128ObjectInit(N25Q128Driver *devp) { * * @api */ -void n15q128Start(N25Q128Driver *devp, const N25Q128Config *config) { +void n25q128Start(N25Q128Driver *devp, const N25Q128Config *config) { osalDbgCheck((devp != NULL) && (config != NULL)); osalDbgAssert(devp->state != FLASH_UNINIT, "invalid state"); if (devp->state == FLASH_STOP) { -#if N25Q128_SHARED_SPI == FALSE + SPIDriver *spip = config->spip; + + devp->config = config; spiStart(devp->config->spip, devp->config->spicfg); -#endif + + /* Reset Enable command.*/ + spiSelect(spip); + flash_send_cmd(devp, N25Q128_CMD_RESET_ENABLE); + spiUnselect(spip); + + (void) spiPolledExchange(spip, 0xFF); /* One frame delay.*/ + + /* Reset Memory command.*/ + spiSelect(spip); + flash_send_cmd(devp, N25Q128_CMD_RESET_MEMORY); + spiUnselect(spip); devp->state = FLASH_READY; } } @@ -399,7 +411,7 @@ void n15q128Start(N25Q128Driver *devp, const N25Q128Config *config) { * * @api */ -void n15q128Stop(N25Q128Driver *devp) { +void n25q128Stop(N25Q128Driver *devp) { SPIDriver *spip = devp->config->spip; osalDbgCheck(devp != NULL); @@ -411,6 +423,7 @@ void n15q128Stop(N25Q128Driver *devp) { #endif spiStop(spip); + devp->config = NULL; devp->state = FLASH_STOP; #if N25Q128_SHARED_SPI == TRUE @@ -419,4 +432,37 @@ void n15q128Stop(N25Q128Driver *devp) { } } +/** + * @brief Reads the device identifier. + * + * @param[in] devp pointer to the @p N25Q128Driver object + * @param[in] rp pointer to the read buffer + * @param[in] n number of bytes to read (1..17) + * + * @api + */ +void n25q128ReadId(N25Q128Driver *devp, uint8_t *rp, size_t n) { + SPIDriver *spip = devp->config->spip; + + osalDbgCheck((devp != NULL) && (rp != NULL) && (n > 0U) && (n <= 17U)); + osalDbgAssert(devp->state == FLASH_READY, "invalid state"); + +#if N25Q128_SHARED_SPI == TRUE + spiAcquireBus(spip); + spiStart(spip, devp->config->spicfg); +#endif + devp->state = FLASH_ACTIVE; + + /* Read Id command.*/ + spiSelect(spip); + flash_send_cmd(devp, N25Q128_CMD_READ_ID); + spiReceive(spip, n, rp); + spiUnselect(spip); + + devp->state = FLASH_READY; +#if N25Q128_SHARED_SPI == TRUE + spiReleaseBus(spip); +#endif +} + /** @} */ diff --git a/os/ex/Micron/n25q128.h b/os/ex/Micron/n25q128.h index b86cfefc7..bcd1ff97b 100644 --- a/os/ex/Micron/n25q128.h +++ b/os/ex/Micron/n25q128.h @@ -189,9 +189,10 @@ typedef struct { #ifdef __cplusplus extern "C" { #endif - void n15q128ObjectInit(N25Q128Driver *devp); - void n15q128Start(N25Q128Driver *devp, const N25Q128Config *config); - void n15q128Stop(N25Q128Driver *devp); + void n25q128ObjectInit(N25Q128Driver *devp); + void n25q128Start(N25Q128Driver *devp, const N25Q128Config *config); + void n25q128Stop(N25Q128Driver *devp); + void n25q128ReadId(N25Q128Driver *devp, uint8_t *rp, size_t n); #ifdef __cplusplus } #endif diff --git a/os/hal/lib/peripherals/flash/hal_flash.h b/os/hal/lib/peripherals/flash/hal_flash.h index 3544b328f..722326607 100644 --- a/os/hal/lib/peripherals/flash/hal_flash.h +++ b/os/hal/lib/peripherals/flash/hal_flash.h @@ -255,7 +255,7 @@ typedef struct { * * @api */ -#define flashProgram(ip) \ +#define flashProgram(ip, addr, pp, n) \ (ip)->vmt_baseflash->program(ip, addr, pp, n) /** @@ -269,7 +269,7 @@ typedef struct { * * @api */ -#define flashRead(ip) \ +#define flashRead(ip, addr, rp, n) \ (ip)->vmt_baseflash->read(ip, addr, rp, n) /** @} */ diff --git a/testhal/STM32/STM32F3xx/SPI-N25Q128/debug/STM32F3xx-SPI-N25Q128 (OpenOCD, Flash and Run).launch b/testhal/STM32/STM32F3xx/SPI-N25Q128/debug/STM32F3xx-SPI-N25Q128 (OpenOCD, Flash and Run).launch index 3d2ffb322..f8a5dd2d5 100644 --- a/testhal/STM32/STM32F3xx/SPI-N25Q128/debug/STM32F3xx-SPI-N25Q128 (OpenOCD, Flash and Run).launch +++ b/testhal/STM32/STM32F3xx/SPI-N25Q128/debug/STM32F3xx-SPI-N25Q128 (OpenOCD, Flash and Run).launch @@ -33,7 +33,7 @@ - + diff --git a/testhal/STM32/STM32F3xx/SPI-N25Q128/main.c b/testhal/STM32/STM32F3xx/SPI-N25Q128/main.c index 7df94b53f..c8ca4aaa4 100644 --- a/testhal/STM32/STM32F3xx/SPI-N25Q128/main.c +++ b/testhal/STM32/STM32F3xx/SPI-N25Q128/main.c @@ -19,6 +19,46 @@ #include "n25q128.h" +/* + * Maximum speed SPI configuration (18MHz, CPHA=0, CPOL=0, MSb first). + */ +static const SPIConfig hs_spicfg = { + NULL, + GPIOB, + 12, + 0, + SPI_CR2_DS_2 | SPI_CR2_DS_1 | SPI_CR2_DS_0 +}; + +/* + * Flash driver configuration. + */ +static const N25Q128Config flashcfg = { + &SPID2, + &hs_spicfg +}; + +/* + * Flash driver object. + */ +static N25Q128Driver flash; + +/* + * Generic buffer. + */ +uint8_t buffer[2048]; + +const uint8_t pattern[128] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 +}; + /* * LED blinker thread, times are in milliseconds. */ @@ -37,6 +77,7 @@ static THD_FUNCTION(Thread1, arg) { * Application entry point. */ int main(void) { + flash_error_t err; /* * System initializations. @@ -53,6 +94,51 @@ int main(void) { */ chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO + 1, Thread1, NULL); + + /* + * SPI2 I/O pins setup. + */ + palSetPad(GPIOB, 12); + palSetPadMode(GPIOB, 12, PAL_MODE_OUTPUT_PUSHPULL | + PAL_STM32_OSPEED_HIGHEST); /* New CS. */ + palSetPadMode(GPIOB, 13, PAL_MODE_ALTERNATE(5) | + PAL_STM32_OSPEED_HIGHEST); /* New SCK. */ + palSetPadMode(GPIOB, 14, PAL_MODE_ALTERNATE(5) | + PAL_STM32_OSPEED_HIGHEST); /* New MISO. */ + palSetPadMode(GPIOB, 15, PAL_MODE_ALTERNATE(5) | + PAL_STM32_OSPEED_HIGHEST); /* New MOSI. */ + + /* + * Initializing and starting flash driver. + */ + n25q128ObjectInit(&flash); + n25q128Start(&flash, &flashcfg); + + /* + * Reading flash identifier. + */ + n25q128ReadId(&flash, buffer, 17); + + /* + * Writing then reading a pattern on a single page with final erase and + * verify. + */ + err = flashProgram(&flash, 0, pattern, 128); + if (err != FLASH_NO_ERROR) + chSysHalt("program error"); + err = flashRead(&flash, 0, buffer, 128); + if (err != FLASH_NO_ERROR) + chSysHalt("read error"); + err = flashEraseSectors(&flash, 0, 1); + if (err != FLASH_NO_ERROR) + chSysHalt("erase error"); + err = flashVerifyErase(&flash, 0, 1); + if (err != FLASH_NO_ERROR) + chSysHalt("verify erase error"); + err = flashRead(&flash, 0, buffer, 128); + if (err != FLASH_NO_ERROR) + chSysHalt("read error"); + while (true) { chThdSleepMilliseconds(500); } diff --git a/testhal/STM32/STM32F3xx/SPI-N25Q128/mcuconf.h b/testhal/STM32/STM32F3xx/SPI-N25Q128/mcuconf.h index 09de76c82..8d7a8d2ef 100644 --- a/testhal/STM32/STM32F3xx/SPI-N25Q128/mcuconf.h +++ b/testhal/STM32/STM32F3xx/SPI-N25Q128/mcuconf.h @@ -206,8 +206,8 @@ /* * SPI driver system settings. */ -#define STM32_SPI_USE_SPI1 TRUE -#define STM32_SPI_USE_SPI2 FALSE +#define STM32_SPI_USE_SPI1 FALSE +#define STM32_SPI_USE_SPI2 TRUE #define STM32_SPI_USE_SPI3 FALSE #define STM32_SPI_SPI1_DMA_PRIORITY 1 #define STM32_SPI_SPI2_DMA_PRIORITY 1 -- cgit v1.2.3