Skip to content

Commit

Permalink
SPI Device API (#304)
Browse files Browse the repository at this point in the history
* spi device config
* fix api_interrupt_call warning
* added configs for spi2 bus devices
* simplified spi bus api
* use new spi device api
  • Loading branch information
DrZlo13 authored Jan 12, 2021
1 parent 6a5e3e8 commit 9ed8beb
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 24 deletions.
4 changes: 2 additions & 2 deletions applications/gui/u8g2_periphery.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ uint8_t u8x8_hw_spi_stm32(u8x8_t* u8x8, uint8_t msg, uint8_t arg_int, void* arg_
#endif

// TODO: SPI manager
api_hal_spi_lock(&SPI_D);
api_hal_spi_lock_device(&display_spi);

// TODO change it to FuriRecord pin
HAL_GPIO_WritePin(DISPLAY_CS_GPIO_Port, DISPLAY_CS_Pin, GPIO_PIN_RESET);
Expand All @@ -106,7 +106,7 @@ uint8_t u8x8_hw_spi_stm32(u8x8_t* u8x8, uint8_t msg, uint8_t arg_int, void* arg_
HAL_GPIO_WritePin(DISPLAY_CS_GPIO_Port, DISPLAY_CS_Pin, GPIO_PIN_SET);

// TODO: SPI manager
api_hal_spi_unlock(&SPI_D);
api_hal_spi_unlock_device(&display_spi);

break;

Expand Down
3 changes: 2 additions & 1 deletion core/api-hal/api-interrupt-mgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ typedef struct {

bool api_interrupt_init();
void api_interrupt_add(InterruptCallback callback, InterruptType type, void* context);
void api_interrupt_remove(InterruptCallback callback);
void api_interrupt_remove(InterruptCallback callback);
void api_interrupt_call(InterruptType type, void* hw);
11 changes: 3 additions & 8 deletions firmware/targets/f4/Src/fatfs/stm32_adafruit_sd.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,9 @@ static uint8_t SD_ReadData(void);
* - MSD_OK: Sequence succeed
*/
uint8_t BSP_SD_Init(void) {
// TODO: SPI manager
api_hal_spi_lock(&SPI_SD_HANDLE);

/* Init to maximum slow speed */
SD_SPI_Reconfigure_Slow();
// TODO: SPI manager
api_hal_spi_lock_device(&sd_slow_spi);

/* Configure IO functionalities for SD pin */
SD_IO_Init();
Expand All @@ -304,11 +302,8 @@ uint8_t BSP_SD_Init(void) {
SdStatus = SD_PRESENT;
uint8_t res = SD_GoIdleState();

/* Init to maximum fastest speed */
SD_SPI_Reconfigure_Fast();

// TODO: SPI manager
api_hal_spi_unlock(&SPI_SD_HANDLE);
api_hal_spi_unlock_device(&sd_slow_spi);

/* SD initialized and set to SPI mode properly */
return res;
Expand Down
16 changes: 8 additions & 8 deletions firmware/targets/f4/Src/fatfs/user_diskio.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ Diskio_drvTypeDef USER_Driver = {
DSTATUS USER_initialize(BYTE pdrv) {
/* USER CODE BEGIN INIT */
// TODO: SPI manager
api_hal_spi_lock(&SPI_SD_HANDLE);
api_hal_spi_lock_device(&sd_fast_spi);

DSTATUS status = User_CheckStatus(pdrv);

// TODO: SPI manager
api_hal_spi_unlock(&SPI_SD_HANDLE);
api_hal_spi_unlock_device(&sd_fast_spi);

return status;
/* USER CODE END INIT */
Expand Down Expand Up @@ -122,7 +122,7 @@ DRESULT USER_read(BYTE pdrv, BYTE* buff, DWORD sector, UINT count) {
DRESULT res = RES_ERROR;

// TODO: SPI manager
api_hal_spi_lock(&SPI_SD_HANDLE);
api_hal_spi_lock_device(&sd_fast_spi);

if(BSP_SD_ReadBlocks((uint32_t*)buff, (uint32_t)(sector), count, SD_DATATIMEOUT) == MSD_OK) {
/* wait until the read operation is finished */
Expand All @@ -132,7 +132,7 @@ DRESULT USER_read(BYTE pdrv, BYTE* buff, DWORD sector, UINT count) {
}

// TODO: SPI manager
api_hal_spi_unlock(&SPI_SD_HANDLE);
api_hal_spi_unlock_device(&sd_fast_spi);

return res;
/* USER CODE END READ */
Expand All @@ -153,7 +153,7 @@ DRESULT USER_write(BYTE pdrv, const BYTE* buff, DWORD sector, UINT count) {
DRESULT res = RES_ERROR;

// TODO: SPI manager
api_hal_spi_lock(&SPI_SD_HANDLE);
api_hal_spi_lock_device(&sd_fast_spi);

if(BSP_SD_WriteBlocks((uint32_t*)buff, (uint32_t)(sector), count, SD_DATATIMEOUT) == MSD_OK) {
/* wait until the Write operation is finished */
Expand All @@ -163,7 +163,7 @@ DRESULT USER_write(BYTE pdrv, const BYTE* buff, DWORD sector, UINT count) {
}

// TODO: SPI manager
api_hal_spi_unlock(&SPI_SD_HANDLE);
api_hal_spi_unlock_device(&sd_fast_spi);

return res;
/* USER CODE END WRITE */
Expand All @@ -186,7 +186,7 @@ DRESULT USER_ioctl(BYTE pdrv, BYTE cmd, void* buff) {
if(Stat & STA_NOINIT) return RES_NOTRDY;

// TODO: SPI manager
api_hal_spi_lock(&SPI_SD_HANDLE);
api_hal_spi_lock_device(&sd_fast_spi);

switch(cmd) {
/* Make sure that no pending write process */
Expand Down Expand Up @@ -220,7 +220,7 @@ DRESULT USER_ioctl(BYTE pdrv, BYTE cmd, void* buff) {
}

// TODO: SPI manager
api_hal_spi_unlock(&SPI_SD_HANDLE);
api_hal_spi_unlock_device(&sd_fast_spi);

return res;
/* USER CODE END IOCTL */
Expand Down
2 changes: 1 addition & 1 deletion firmware/targets/f4/Src/spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ void SD_SPI_Reconfigure_Fast(void) {
SPI_SD_HANDLE.Init.CLKPolarity = SPI_POLARITY_LOW;
SPI_SD_HANDLE.Init.CLKPhase = SPI_PHASE_1EDGE;
SPI_SD_HANDLE.Init.NSS = SPI_NSS_SOFT;
SPI_SD_HANDLE.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64;
SPI_SD_HANDLE.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
SPI_SD_HANDLE.Init.FirstBit = SPI_FIRSTBIT_MSB;
SPI_SD_HANDLE.Init.TIMode = SPI_TIMODE_DISABLE;
SPI_SD_HANDLE.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
Expand Down
4 changes: 2 additions & 2 deletions firmware/targets/f4/api-hal/api-hal-gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ bool hal_gpio_read_sd_detect(void) {
const GpioPin* sd_cs_record = &sd_cs_gpio;

// TODO: SPI manager
api_hal_spi_lock(&SPI_SD_HANDLE);
api_hal_spi_lock(sd_fast_spi.spi);

// configure pin as input
gpio_init_ex(sd_cs_record, GpioModeInput, GpioPullUp, GpioSpeedVeryHigh);
Expand All @@ -41,7 +41,7 @@ bool hal_gpio_read_sd_detect(void) {
delay(1);

// TODO: SPI manager
api_hal_spi_unlock(&SPI_SD_HANDLE);
api_hal_spi_unlock(sd_fast_spi.spi);

return result;
}
Expand Down
68 changes: 68 additions & 0 deletions firmware/targets/f4/api-hal/api-hal-spi-config.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include "main.h"
#include "api-hal-spi-config.h"

extern SPI_HandleTypeDef SPI_R;
extern SPI_HandleTypeDef SPI_D;

/**
* SD Card in fast mode (after init)
*/
const SPIDevice sd_fast_spi = {
.spi = &SPI_D,
.config = {
.Mode = SPI_MODE_MASTER,
.Direction = SPI_DIRECTION_2LINES,
.DataSize = SPI_DATASIZE_8BIT,
.CLKPolarity = SPI_POLARITY_LOW,
.CLKPhase = SPI_PHASE_1EDGE,
.NSS = SPI_NSS_SOFT,
.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2,
.FirstBit = SPI_FIRSTBIT_MSB,
.TIMode = SPI_TIMODE_DISABLE,
.CRCCalculation = SPI_CRCCALCULATION_DISABLE,
.CRCPolynomial = 7,
.CRCLength = SPI_CRC_LENGTH_DATASIZE,
.NSSPMode = SPI_NSS_PULSE_ENABLE,
}};

/**
* SD Card in slow mode (before init)
*/
const SPIDevice sd_slow_spi = {
.spi = &SPI_D,
.config = {
.Mode = SPI_MODE_MASTER,
.Direction = SPI_DIRECTION_2LINES,
.DataSize = SPI_DATASIZE_8BIT,
.CLKPolarity = SPI_POLARITY_LOW,
.CLKPhase = SPI_PHASE_1EDGE,
.NSS = SPI_NSS_SOFT,
.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256,
.FirstBit = SPI_FIRSTBIT_MSB,
.TIMode = SPI_TIMODE_DISABLE,
.CRCCalculation = SPI_CRCCALCULATION_DISABLE,
.CRCPolynomial = 7,
.CRCLength = SPI_CRC_LENGTH_DATASIZE,
.NSSPMode = SPI_NSS_PULSE_ENABLE,
}};

/**
* Display
*/
const SPIDevice display_spi = {
.spi = &SPI_D,
.config = {
.Mode = SPI_MODE_MASTER,
.Direction = SPI_DIRECTION_2LINES,
.DataSize = SPI_DATASIZE_8BIT,
.CLKPolarity = SPI_POLARITY_LOW,
.CLKPhase = SPI_PHASE_1EDGE,
.NSS = SPI_NSS_SOFT,
.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16,
.FirstBit = SPI_FIRSTBIT_MSB,
.TIMode = SPI_TIMODE_DISABLE,
.CRCCalculation = SPI_CRCCALCULATION_DISABLE,
.CRCPolynomial = 7,
.CRCLength = SPI_CRC_LENGTH_DATASIZE,
.NSSPMode = SPI_NSS_PULSE_ENABLE,
}};
18 changes: 18 additions & 0 deletions firmware/targets/f4/api-hal/api-hal-spi-config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#ifdef __cplusplus
extern "C" {
#endif

typedef struct {
SPI_HandleTypeDef* spi;
const SPI_InitTypeDef config;
} SPIDevice;

extern const SPIDevice sd_fast_spi;
extern const SPIDevice sd_slow_spi;
extern const SPIDevice display_spi;

#ifdef __cplusplus
}
#endif
37 changes: 37 additions & 0 deletions firmware/targets/f4/api-hal/api-hal-spi.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,44 @@
#include "api-hal-spi.h"
#include <cmsis_os.h>
#include <stdbool.h>
#include <string.h>

osMutexId_t spi_mutex_r;
osMutexId_t spi_mutex_d;

extern SPI_HandleTypeDef SPI_R;
extern SPI_HandleTypeDef SPI_D;
extern void Enable_SPI(SPI_HandleTypeDef* spi);

void api_hal_spi_init() {
spi_mutex_r = osMutexNew(NULL);
spi_mutex_d = osMutexNew(NULL);
}

void api_hal_spi_apply_config(const SPIDevice* device) {
osKernelLock();

memcpy(&device->spi->Init, &device->config, sizeof(SPI_InitTypeDef));

if(HAL_SPI_Init(device->spi) != HAL_OK) {
Error_Handler();
}

Enable_SPI(device->spi);

osKernelUnlock();
}

bool api_hal_spi_config_are_actual(const SPIDevice* device) {
return (memcmp(&device->config, &device->spi->Init, sizeof(SPI_InitTypeDef)) == 0);
}

void api_hal_spi_config_device(const SPIDevice* device) {
if(!api_hal_spi_config_are_actual(device)) {
api_hal_spi_apply_config(device);
}
}

void api_hal_spi_lock(SPI_HandleTypeDef* spi) {
if(spi == &SPI_D) {
osMutexAcquire(spi_mutex_d, osWaitForever);
Expand All @@ -29,4 +57,13 @@ void api_hal_spi_unlock(SPI_HandleTypeDef* spi) {
} else {
Error_Handler();
}
}

void api_hal_spi_lock_device(const SPIDevice* device) {
api_hal_spi_lock(device->spi);
api_hal_spi_config_device(device);
}

void api_hal_spi_unlock_device(const SPIDevice* device) {
api_hal_spi_unlock(device->spi);
}
33 changes: 31 additions & 2 deletions firmware/targets/f4/api-hal/api-hal-spi.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,36 @@
#pragma once
#include "main.h"
#include <cmsis_os.h>
#include "api-hal-spi-config.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
* Init SPI API
*/
void api_hal_spi_init();

/**
* Lock SPI bus
*/
void api_hal_spi_lock(SPI_HandleTypeDef* spi);
void api_hal_spi_unlock(SPI_HandleTypeDef* spi);

/**
* Unlock SPI bus
*/
void api_hal_spi_unlock(SPI_HandleTypeDef* spi);

/**
* Lock SPI device bus and apply config if needed
*/
void api_hal_spi_lock_device(const SPIDevice* device);

/**
* Unlock SPI device bus
*/
void api_hal_spi_unlock_device(const SPIDevice* device);

#ifdef __cplusplus
}
#endif

0 comments on commit 9ed8beb

Please sign in to comment.