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

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