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:
DrZlo13
2021-01-13 03:33:36 +10:00
committed by GitHub
parent 6a5e3e83b4
commit 9ed8bebba1
10 changed files with 172 additions and 24 deletions

View File

@@ -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);
@@ -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;
}

View 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,
}};

View 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

View File

@@ -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);
@@ -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);
}

View File

@@ -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