summaryrefslogtreecommitdiff
path: root/controller/fw/src/spi_flash.c
diff options
context:
space:
mode:
Diffstat (limited to 'controller/fw/src/spi_flash.c')
-rw-r--r--controller/fw/src/spi_flash.c88
1 files changed, 49 insertions, 39 deletions
diff --git a/controller/fw/src/spi_flash.c b/controller/fw/src/spi_flash.c
index 98c773f..305be8f 100644
--- a/controller/fw/src/spi_flash.c
+++ b/controller/fw/src/spi_flash.c
@@ -22,7 +22,6 @@
*/
#include "spi_flash.h"
-#include <libopencm3/stm32/spi.h>
enum {
WRITE_ENABLE = 0x06,
@@ -49,6 +48,10 @@ enum {
};
+static uint8_t spi_xfer(volatile SPI_TypeDef *spi, uint8_t b);
+static uint8_t spi_read(struct spi_flash_if *spif);
+static void spi_write(struct spi_flash_if *spif, uint8_t b);
+
static void spif_write_page(struct spi_flash_if *spif, size_t addr, size_t len, const char* data);
static uint8_t spif_read_status(struct spi_flash_if *spif);
static void spif_enable_write(struct spi_flash_if *spif);
@@ -58,32 +61,38 @@ static void spif_wait_for_write(struct spi_flash_if *spif);
#define mid_byte(x) ((x>>8)&0xff)
#define high_byte(x) ((x>>16)&0xff)
+uint8_t spi_xfer(volatile SPI_TypeDef *spi, uint8_t b) {
+ while (!(spi->SR & SPI_SR_TXE))
+ ;
+ (void) spi->DR; /* perform dummy read to clear RXNE flag */
+ spi->DR = b;
+ while (!(spi->SR & SPI_SR_RXNE))
+ ;
+ return spi->DR;
+}
-void spif_init(struct spi_flash_if *spif, size_t page_size, uint32_t spi_base, void (*cs)(bool val)) {
+uint8_t spi_read(struct spi_flash_if *spif) {
+ return spi_xfer(spif->spi, 0);
+}
+
+void spi_write(struct spi_flash_if *spif, uint8_t b) {
+ (void)spi_xfer(spif->spi, b);
+}
- spif->spi_base = spi_base;
+void spif_init(struct spi_flash_if *spif, size_t page_size, SPI_TypeDef *spi, void (*cs)(bool val)) {
+
+ spif->spi = spi;
spif->page_size = page_size;
spif->cs = cs;
spif->cs(1);
- spi_reset(spif->spi_base);
- spi_init_master(spif->spi_base,
- SPI_CR1_BAUDRATE_FPCLK_DIV_2,
- SPI_CR1_CPOL_CLK_TO_1_WHEN_IDLE,
- SPI_CR1_CPHA_CLK_TRANSITION_2,
- SPI_CR1_DFF_8BIT,
- SPI_CR1_MSBFIRST);
-
- spi_enable_software_slave_management(spif->spi_base);
- spi_set_nss_high(spif->spi_base);
-
- spi_enable(spif->spi_base);
+ spi->CR1 = (0<<SPI_CR1_BR_Pos) | SPI_CR1_CPOL | SPI_CR1_CPHA | SPI_CR1_SSM | SPI_CR1_SSI | SPI_CR1_SPE | SPI_CR1_MSTR;
spif->cs(0);
- spi_send(spif->spi_base, READ_IDENTIFICATION);
- spif->id.mfg_id = spi_xfer(spif->spi_base, 0);
- spif->id.type = spi_xfer(spif->spi_base, 0);
- spif->id.size = 1<<spi_xfer(spif->spi_base, 0);
+ spi_write(spif, READ_IDENTIFICATION);
+ spif->id.mfg_id = spi_read(spif);
+ spif->id.type = spi_read(spif);
+ spif->id.size = 1<<spi_read(spif);
spif->cs(1);
}
@@ -91,13 +100,13 @@ void spif_read(struct spi_flash_if *spif, size_t addr, size_t len, char* data) {
spif_enable_write(spif);
spif->cs(0);
- spi_xfer(spif->spi_base, READ_DATA);
- spi_xfer(spif->spi_base, high_byte(addr));
- spi_xfer(spif->spi_base, mid_byte(addr));
- spi_xfer(spif->spi_base, low_byte(addr));
+ spi_write(spif, READ_DATA);
+ spi_write(spif, high_byte(addr));
+ spi_write(spif, mid_byte(addr));
+ spi_write(spif, low_byte(addr));
for (size_t i = 0; i < len; i++)
- data[i] = spi_xfer(spif->spi_base, 0);
+ data[i] = spi_read(spif);
spif->cs(1);
}
@@ -117,8 +126,8 @@ void spif_write(struct spi_flash_if *spif, size_t addr, size_t len, const char*
static uint8_t spif_read_status(struct spi_flash_if *spif) {
spif->cs(0);
- spi_xfer(spif->spi_base, READ_STATUS);
- uint8_t status = spi_xfer(spif->spi_base, 0);
+ spi_write(spif, READ_STATUS);
+ uint8_t status = spi_read(spif);
spif->cs(1);
return status;
@@ -128,10 +137,10 @@ void spif_clear_sector(struct spi_flash_if *spif, size_t addr) {
spif_enable_write(spif);
spif->cs(0);
- spi_xfer(spif->spi_base, SECTOR_ERASE);
- spi_xfer(spif->spi_base, high_byte(addr));
- spi_xfer(spif->spi_base, mid_byte(addr));
- spi_xfer(spif->spi_base, low_byte(addr));
+ spi_write(spif, SECTOR_ERASE);
+ spi_write(spif, high_byte(addr));
+ spi_write(spif, mid_byte(addr));
+ spi_write(spif, low_byte(addr));
spif->cs(1);
spif_wait_for_write(spif);
@@ -141,7 +150,7 @@ void spif_clear_mem(struct spi_flash_if *spif) {
spif_enable_write(spif);
spif->cs(0);
- spi_xfer(spif->spi_base, BULK_ERASE);
+ spi_write(spif, BULK_ERASE);
spif->cs(1);
spif_wait_for_write(spif);
@@ -151,13 +160,13 @@ static void spif_write_page(struct spi_flash_if *spif, size_t addr, size_t len,
spif_enable_write(spif);
spif->cs(0);
- spi_xfer(spif->spi_base, PAGE_PROGRAM);
- spi_xfer(spif->spi_base, high_byte(addr));
- spi_xfer(spif->spi_base, mid_byte(addr));
- spi_xfer(spif->spi_base, low_byte(addr));
+ spi_write(spif, PAGE_PROGRAM);
+ spi_write(spif, high_byte(addr));
+ spi_write(spif, mid_byte(addr));
+ spi_write(spif, low_byte(addr));
for (size_t i = 0; i < len; i++) {
- spi_xfer(spif->spi_base, data[i]);
+ spi_write(spif, data[i]);
}
spif->cs(1);
@@ -166,7 +175,7 @@ static void spif_write_page(struct spi_flash_if *spif, size_t addr, size_t len,
static void spif_enable_write(struct spi_flash_if *spif) {
spif->cs(0);
- spi_xfer(spif->spi_base, WRITE_ENABLE);
+ spi_write(spif, WRITE_ENABLE);
spif->cs(1);
}
@@ -178,12 +187,13 @@ static void spif_wait_for_write(struct spi_flash_if *spif) {
void spif_deep_power_down(struct spi_flash_if *spif) {
spif->cs(0);
- spi_xfer(spif->spi_base, DEEP_POWER_DOWN);
+ spi_write(spif, DEEP_POWER_DOWN);
spif->cs(1);
}
void spif_wakeup(struct spi_flash_if *spif) {
spif->cs(0);
- spi_xfer(spif->spi_base, DEEP_POWER_DOWN_RELEASE);
+ spi_write(spif, DEEP_POWER_DOWN_RELEASE);
spif->cs(1);
}
+