SPI Device API (#304)
* spi device config * fix api_interrupt_call warning * added configs for spi2 bus devices * simplified spi bus api * use new spi device api
This commit is contained in:
parent
6a5e3e83b4
commit
9ed8bebba1
@ -89,7 +89,7 @@ uint8_t u8x8_hw_spi_stm32(u8x8_t* u8x8, uint8_t msg, uint8_t arg_int, void* arg_
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
// TODO: SPI manager
|
// TODO: SPI manager
|
||||||
api_hal_spi_lock(&SPI_D);
|
api_hal_spi_lock_device(&display_spi);
|
||||||
|
|
||||||
// TODO change it to FuriRecord pin
|
// TODO change it to FuriRecord pin
|
||||||
HAL_GPIO_WritePin(DISPLAY_CS_GPIO_Port, DISPLAY_CS_Pin, GPIO_PIN_RESET);
|
HAL_GPIO_WritePin(DISPLAY_CS_GPIO_Port, DISPLAY_CS_Pin, GPIO_PIN_RESET);
|
||||||
@ -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);
|
HAL_GPIO_WritePin(DISPLAY_CS_GPIO_Port, DISPLAY_CS_Pin, GPIO_PIN_SET);
|
||||||
|
|
||||||
// TODO: SPI manager
|
// TODO: SPI manager
|
||||||
api_hal_spi_unlock(&SPI_D);
|
api_hal_spi_unlock_device(&display_spi);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -18,3 +18,4 @@ typedef struct {
|
|||||||
bool api_interrupt_init();
|
bool api_interrupt_init();
|
||||||
void api_interrupt_add(InterruptCallback callback, InterruptType type, void* context);
|
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);
|
@ -291,11 +291,9 @@ static uint8_t SD_ReadData(void);
|
|||||||
* - MSD_OK: Sequence succeed
|
* - MSD_OK: Sequence succeed
|
||||||
*/
|
*/
|
||||||
uint8_t BSP_SD_Init(void) {
|
uint8_t BSP_SD_Init(void) {
|
||||||
// TODO: SPI manager
|
|
||||||
api_hal_spi_lock(&SPI_SD_HANDLE);
|
|
||||||
|
|
||||||
/* Init to maximum slow speed */
|
/* 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 */
|
/* Configure IO functionalities for SD pin */
|
||||||
SD_IO_Init();
|
SD_IO_Init();
|
||||||
@ -304,11 +302,8 @@ uint8_t BSP_SD_Init(void) {
|
|||||||
SdStatus = SD_PRESENT;
|
SdStatus = SD_PRESENT;
|
||||||
uint8_t res = SD_GoIdleState();
|
uint8_t res = SD_GoIdleState();
|
||||||
|
|
||||||
/* Init to maximum fastest speed */
|
|
||||||
SD_SPI_Reconfigure_Fast();
|
|
||||||
|
|
||||||
// TODO: SPI manager
|
// 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 */
|
/* SD initialized and set to SPI mode properly */
|
||||||
return res;
|
return res;
|
||||||
|
@ -87,12 +87,12 @@ Diskio_drvTypeDef USER_Driver = {
|
|||||||
DSTATUS USER_initialize(BYTE pdrv) {
|
DSTATUS USER_initialize(BYTE pdrv) {
|
||||||
/* USER CODE BEGIN INIT */
|
/* USER CODE BEGIN INIT */
|
||||||
// TODO: SPI manager
|
// TODO: SPI manager
|
||||||
api_hal_spi_lock(&SPI_SD_HANDLE);
|
api_hal_spi_lock_device(&sd_fast_spi);
|
||||||
|
|
||||||
DSTATUS status = User_CheckStatus(pdrv);
|
DSTATUS status = User_CheckStatus(pdrv);
|
||||||
|
|
||||||
// TODO: SPI manager
|
// TODO: SPI manager
|
||||||
api_hal_spi_unlock(&SPI_SD_HANDLE);
|
api_hal_spi_unlock_device(&sd_fast_spi);
|
||||||
|
|
||||||
return status;
|
return status;
|
||||||
/* USER CODE END INIT */
|
/* USER CODE END INIT */
|
||||||
@ -122,7 +122,7 @@ DRESULT USER_read(BYTE pdrv, BYTE* buff, DWORD sector, UINT count) {
|
|||||||
DRESULT res = RES_ERROR;
|
DRESULT res = RES_ERROR;
|
||||||
|
|
||||||
// TODO: SPI manager
|
// 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) {
|
if(BSP_SD_ReadBlocks((uint32_t*)buff, (uint32_t)(sector), count, SD_DATATIMEOUT) == MSD_OK) {
|
||||||
/* wait until the read operation is finished */
|
/* wait until the read operation is finished */
|
||||||
@ -132,7 +132,7 @@ DRESULT USER_read(BYTE pdrv, BYTE* buff, DWORD sector, UINT count) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: SPI manager
|
// TODO: SPI manager
|
||||||
api_hal_spi_unlock(&SPI_SD_HANDLE);
|
api_hal_spi_unlock_device(&sd_fast_spi);
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
/* USER CODE END READ */
|
/* USER CODE END READ */
|
||||||
@ -153,7 +153,7 @@ DRESULT USER_write(BYTE pdrv, const BYTE* buff, DWORD sector, UINT count) {
|
|||||||
DRESULT res = RES_ERROR;
|
DRESULT res = RES_ERROR;
|
||||||
|
|
||||||
// TODO: SPI manager
|
// 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) {
|
if(BSP_SD_WriteBlocks((uint32_t*)buff, (uint32_t)(sector), count, SD_DATATIMEOUT) == MSD_OK) {
|
||||||
/* wait until the Write operation is finished */
|
/* wait until the Write operation is finished */
|
||||||
@ -163,7 +163,7 @@ DRESULT USER_write(BYTE pdrv, const BYTE* buff, DWORD sector, UINT count) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: SPI manager
|
// TODO: SPI manager
|
||||||
api_hal_spi_unlock(&SPI_SD_HANDLE);
|
api_hal_spi_unlock_device(&sd_fast_spi);
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
/* USER CODE END WRITE */
|
/* USER CODE END WRITE */
|
||||||
@ -186,7 +186,7 @@ DRESULT USER_ioctl(BYTE pdrv, BYTE cmd, void* buff) {
|
|||||||
if(Stat & STA_NOINIT) return RES_NOTRDY;
|
if(Stat & STA_NOINIT) return RES_NOTRDY;
|
||||||
|
|
||||||
// TODO: SPI manager
|
// TODO: SPI manager
|
||||||
api_hal_spi_lock(&SPI_SD_HANDLE);
|
api_hal_spi_lock_device(&sd_fast_spi);
|
||||||
|
|
||||||
switch(cmd) {
|
switch(cmd) {
|
||||||
/* Make sure that no pending write process */
|
/* Make sure that no pending write process */
|
||||||
@ -220,7 +220,7 @@ DRESULT USER_ioctl(BYTE pdrv, BYTE cmd, void* buff) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: SPI manager
|
// TODO: SPI manager
|
||||||
api_hal_spi_unlock(&SPI_SD_HANDLE);
|
api_hal_spi_unlock_device(&sd_fast_spi);
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
/* USER CODE END IOCTL */
|
/* USER CODE END IOCTL */
|
||||||
|
@ -290,7 +290,7 @@ void SD_SPI_Reconfigure_Fast(void) {
|
|||||||
SPI_SD_HANDLE.Init.CLKPolarity = SPI_POLARITY_LOW;
|
SPI_SD_HANDLE.Init.CLKPolarity = SPI_POLARITY_LOW;
|
||||||
SPI_SD_HANDLE.Init.CLKPhase = SPI_PHASE_1EDGE;
|
SPI_SD_HANDLE.Init.CLKPhase = SPI_PHASE_1EDGE;
|
||||||
SPI_SD_HANDLE.Init.NSS = SPI_NSS_SOFT;
|
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.FirstBit = SPI_FIRSTBIT_MSB;
|
||||||
SPI_SD_HANDLE.Init.TIMode = SPI_TIMODE_DISABLE;
|
SPI_SD_HANDLE.Init.TIMode = SPI_TIMODE_DISABLE;
|
||||||
SPI_SD_HANDLE.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
|
SPI_SD_HANDLE.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
|
||||||
|
@ -26,7 +26,7 @@ bool hal_gpio_read_sd_detect(void) {
|
|||||||
const GpioPin* sd_cs_record = &sd_cs_gpio;
|
const GpioPin* sd_cs_record = &sd_cs_gpio;
|
||||||
|
|
||||||
// TODO: SPI manager
|
// TODO: SPI manager
|
||||||
api_hal_spi_lock(&SPI_SD_HANDLE);
|
api_hal_spi_lock(sd_fast_spi.spi);
|
||||||
|
|
||||||
// configure pin as input
|
// configure pin as input
|
||||||
gpio_init_ex(sd_cs_record, GpioModeInput, GpioPullUp, GpioSpeedVeryHigh);
|
gpio_init_ex(sd_cs_record, GpioModeInput, GpioPullUp, GpioSpeedVeryHigh);
|
||||||
@ -41,7 +41,7 @@ bool hal_gpio_read_sd_detect(void) {
|
|||||||
delay(1);
|
delay(1);
|
||||||
|
|
||||||
// TODO: SPI manager
|
// TODO: SPI manager
|
||||||
api_hal_spi_unlock(&SPI_SD_HANDLE);
|
api_hal_spi_unlock(sd_fast_spi.spi);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
68
firmware/targets/f4/api-hal/api-hal-spi-config.c
Normal file
68
firmware/targets/f4/api-hal/api-hal-spi-config.c
Normal file
@ -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
firmware/targets/f4/api-hal/api-hal-spi-config.h
Normal file
18
firmware/targets/f4/api-hal/api-hal-spi-config.h
Normal file
@ -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
|
@ -1,16 +1,44 @@
|
|||||||
#include "api-hal-spi.h"
|
#include "api-hal-spi.h"
|
||||||
|
#include <cmsis_os.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
osMutexId_t spi_mutex_r;
|
osMutexId_t spi_mutex_r;
|
||||||
osMutexId_t spi_mutex_d;
|
osMutexId_t spi_mutex_d;
|
||||||
|
|
||||||
extern SPI_HandleTypeDef SPI_R;
|
extern SPI_HandleTypeDef SPI_R;
|
||||||
extern SPI_HandleTypeDef SPI_D;
|
extern SPI_HandleTypeDef SPI_D;
|
||||||
|
extern void Enable_SPI(SPI_HandleTypeDef* spi);
|
||||||
|
|
||||||
void api_hal_spi_init() {
|
void api_hal_spi_init() {
|
||||||
spi_mutex_r = osMutexNew(NULL);
|
spi_mutex_r = osMutexNew(NULL);
|
||||||
spi_mutex_d = 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) {
|
void api_hal_spi_lock(SPI_HandleTypeDef* spi) {
|
||||||
if(spi == &SPI_D) {
|
if(spi == &SPI_D) {
|
||||||
osMutexAcquire(spi_mutex_d, osWaitForever);
|
osMutexAcquire(spi_mutex_d, osWaitForever);
|
||||||
@ -30,3 +58,12 @@ void api_hal_spi_unlock(SPI_HandleTypeDef* spi) {
|
|||||||
Error_Handler();
|
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);
|
||||||
|
}
|
@ -1,7 +1,36 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "main.h"
|
#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();
|
void api_hal_spi_init();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lock SPI bus
|
||||||
|
*/
|
||||||
void api_hal_spi_lock(SPI_HandleTypeDef* spi);
|
void api_hal_spi_lock(SPI_HandleTypeDef* spi);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unlock SPI bus
|
||||||
|
*/
|
||||||
void api_hal_spi_unlock(SPI_HandleTypeDef* spi);
|
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
|
Loading…
Reference in New Issue
Block a user