Skip to content

Commit

Permalink
refactor(spi-flash,lfs): change static functions to non-static
Browse files Browse the repository at this point in the history
  • Loading branch information
hashemmm96 committed Apr 10, 2024
1 parent 4019849 commit 31d8b5a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 32 deletions.
42 changes: 20 additions & 22 deletions libs/stm32-common/src/lfs-wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,20 @@ static StackType_t
SemaphoreHandle_t mutex = NULL;
StaticSemaphore_t mutex_buf;

static void write_file_task(void *unused);
void write_file_task(void *unused);

// Functions required by littlefs
static int spi_flash_read(const struct lfs_config *config, lfs_block_t block,
lfs_off_t offset, void *buffer, lfs_size_t size);
static int spi_flash_prog(const struct lfs_config *config, lfs_block_t block,
lfs_off_t offset, const void *buffer,
lfs_size_t size);
static int spi_flash_erase(const struct lfs_config *config, lfs_block_t block);
static int spi_flash_sync(const struct lfs_config *config);
int spi_flash_read(const struct lfs_config *config, lfs_block_t block,
lfs_off_t offset, void *buffer, lfs_size_t size);
int spi_flash_prog(const struct lfs_config *config, lfs_block_t block,
lfs_off_t offset, const void *buffer, lfs_size_t size);
int spi_flash_erase(const struct lfs_config *config, lfs_block_t block);
int spi_flash_sync(const struct lfs_config *config);

// Helpers
static void format_and_mount(void);
static void lfs_lock(void);
static void lfs_unlock(void);
void format_and_mount(void);
void lfs_lock(void);
void lfs_unlock(void);

static const struct lfs_config lfs_cfg = {
// Operations
Expand Down Expand Up @@ -141,7 +140,7 @@ int init_lfs_task(uint32_t write_priority) {
return APP_OK;
}

static void write_file_task(void *unused) {
void write_file_task(void *unused) {
(void)unused;

for (;;) {
Expand Down Expand Up @@ -252,27 +251,26 @@ int write_file_async(const file_t *file) {
return APP_OK;
}

static int spi_flash_read(const struct lfs_config *config, lfs_block_t block,
lfs_off_t offset, void *buffer, lfs_size_t size) {
int spi_flash_read(const struct lfs_config *config, lfs_block_t block,
lfs_off_t offset, void *buffer, lfs_size_t size) {
return read(block * config->block_size + offset, buffer, size);
}

static int spi_flash_prog(const struct lfs_config *config, lfs_block_t block,
lfs_off_t offset, const void *buffer,
lfs_size_t size) {
int spi_flash_prog(const struct lfs_config *config, lfs_block_t block,
lfs_off_t offset, const void *buffer, lfs_size_t size) {
return program(block * config->block_size + offset, (uint8_t *)buffer, size);
}

static int spi_flash_erase(const struct lfs_config *config, lfs_block_t block) {
int spi_flash_erase(const struct lfs_config *config, lfs_block_t block) {
return erase(block * config->block_size);
}

static int spi_flash_sync(const struct lfs_config *config) {
int spi_flash_sync(const struct lfs_config *config) {
(void)config;
return APP_OK;
}

static void format_and_mount(void) {
void format_and_mount(void) {
int err = lfs_format(&lfs, &lfs_cfg);
if (err < 0) {
printf("Fatal flash error, couldn't format flash: %d\r\n", err);
Expand All @@ -285,13 +283,13 @@ static void format_and_mount(void) {
}
}

static void lfs_lock(void) {
void lfs_lock(void) {
if (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING) {
xSemaphoreTake(mutex, portMAX_DELAY);
}
}

static void lfs_unlock(void) {
void lfs_unlock(void) {
if (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING) {
xSemaphoreGive(mutex);
}
Expand Down
20 changes: 10 additions & 10 deletions libs/stm32-common/src/spi-flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
#define MAX_BLOCK_TIME_MS 100

// Helpers
static int program_page(uint32_t page_address, uint8_t *bytes, size_t size);
static int wait_until_ready(void);
static int set_write_enable_flag(void);
static void start_spi_cmd(void);
static void end_spi_cmd(void);
int program_page(uint32_t page_address, uint8_t *bytes, size_t size);
int wait_until_ready(void);
int set_write_enable_flag(void);
void start_spi_cmd(void);
void end_spi_cmd(void);

int spi_flash_workaround_init(void) {
// There is a timing issue on cold reset where the flash cannot be read
Expand Down Expand Up @@ -178,7 +178,7 @@ int read(uint32_t address, uint8_t *data, size_t size) {
return APP_OK;
}

static int program_page(uint32_t page_address, uint8_t *bytes, size_t size) {
int program_page(uint32_t page_address, uint8_t *bytes, size_t size) {
// Command consists of command number followed by page address in big
// endian format. If an entire page should be programmed the last address byte
// should be set to 0.
Expand Down Expand Up @@ -225,7 +225,7 @@ static int program_page(uint32_t page_address, uint8_t *bytes, size_t size) {
return wait_until_ready();
}

static int wait_until_ready(void) {
int wait_until_ready(void) {
common_peripherals_t *peripherals = get_common_peripherals();
uint8_t cmd = READ_SR1_COMMAND;

Expand All @@ -251,7 +251,7 @@ static int wait_until_ready(void) {
return APP_OK;
}

static int set_write_enable_flag(void) {
int set_write_enable_flag(void) {
common_peripherals_t *peripherals = get_common_peripherals();
uint8_t cmd = WRITE_ENABLE_COMMAND;

Expand Down Expand Up @@ -297,12 +297,12 @@ static int set_write_enable_flag(void) {
return APP_OK;
}

static void start_spi_cmd(void) {
void start_spi_cmd(void) {
taskENTER_CRITICAL();
HAL_GPIO_WritePin(SPI_FLASH_GPIO_PORT, SPI_FLASH_NSS_PIN, GPIO_PIN_RESET);
}

static void end_spi_cmd(void) {
void end_spi_cmd(void) {
HAL_GPIO_WritePin(SPI_FLASH_GPIO_PORT, SPI_FLASH_NSS_PIN, GPIO_PIN_SET);
taskEXIT_CRITICAL();
}
Expand Down

0 comments on commit 31d8b5a

Please sign in to comment.