fsfw-hal/stm32h7/spi/spiCore.cpp

177 lines
4.6 KiB
C++
Raw Normal View History

2021-06-03 14:38:34 +02:00
#include "spiCore.h"
2021-06-04 16:34:38 +02:00
#include <cstdio>
2021-06-03 14:38:34 +02:00
2021-06-04 16:34:38 +02:00
SPI_HandleTypeDef* spiHandle = nullptr;
2021-06-05 13:29:43 +02:00
DMA_HandleTypeDef* hdmaTx = nullptr;
DMA_HandleTypeDef* hdmaRx = nullptr;
2021-06-03 14:38:34 +02:00
2021-06-05 13:29:43 +02:00
msp_func_t mspInitFunc = nullptr;
void* mspInitArgs = nullptr;
2021-06-03 21:42:52 +02:00
2021-06-05 13:29:43 +02:00
msp_func_t mspDeinitFunc = nullptr;
void* mspDeinitArgs = nullptr;
2021-06-03 21:42:52 +02:00
2021-06-05 13:29:43 +02:00
spi_transfer_cb_t rxTxCb = nullptr;
void* rxTxArgs = nullptr;
spi_transfer_cb_t txCb = nullptr;
void* txArgs = nullptr;
spi_transfer_cb_t rxCb = nullptr;
void* rxArgs = nullptr;
spi_transfer_cb_t errorCb = nullptr;
void* errorArgs = nullptr;
void spi::setDmaHandles(DMA_HandleTypeDef* txHandle, DMA_HandleTypeDef* rxHandle) {
hdmaTx = txHandle;
hdmaRx = rxHandle;
2021-06-03 14:38:34 +02:00
}
2021-06-05 13:29:43 +02:00
void spi::getDmaHandles(DMA_HandleTypeDef** txHandle, DMA_HandleTypeDef** rxHandle) {
*txHandle = hdmaTx;
*rxHandle = hdmaRx;
2021-06-03 14:38:34 +02:00
}
2021-06-05 13:29:43 +02:00
void spi::setSpiHandle(SPI_HandleTypeDef *spiHandle_) {
2021-06-03 14:38:34 +02:00
if(spiHandle_ == NULL) {
return;
}
spiHandle = spiHandle_;
}
2021-06-05 13:29:43 +02:00
void spi::assignTransferRxTxCompleteCallback(spi_transfer_cb_t callback, void *userArgs) {
rxTxCb = callback;
rxTxArgs = userArgs;
}
void spi::assignTransferRxCompleteCallback(spi_transfer_cb_t callback, void *userArgs) {
rxCb = callback;
rxArgs = userArgs;
}
void spi::assignTransferTxCompleteCallback(spi_transfer_cb_t callback, void *userArgs) {
txCb = callback;
txArgs = userArgs;
}
void spi::assignTransferErrorCallback(spi_transfer_cb_t callback, void *userArgs) {
errorCb = callback;
errorArgs = userArgs;
}
SPI_HandleTypeDef* spi::getSpiHandle() {
2021-06-03 14:38:34 +02:00
return spiHandle;
}
2021-06-05 13:29:43 +02:00
void spi::setSpiMspFunctions(msp_func_t init_func, void* init_args, msp_func_t deinit_func,
2021-06-03 21:42:52 +02:00
void* deinit_args) {
2021-06-05 13:29:43 +02:00
mspInitFunc = init_func;
mspInitArgs = init_args;
mspDeinitFunc = deinit_func;
mspDeinitArgs = deinit_args;
2021-06-03 21:42:52 +02:00
}
2021-06-05 13:29:43 +02:00
void spi::getMspInitFunction(msp_func_t* init_func, void **args) {
2021-06-03 21:42:52 +02:00
if(init_func != NULL && args != NULL) {
2021-06-05 13:29:43 +02:00
*init_func = mspInitFunc;
*args = mspInitArgs;
2021-06-03 21:42:52 +02:00
}
}
2021-06-05 13:29:43 +02:00
void spi::getMspDeinitFunction(msp_func_t* deinit_func, void **args) {
2021-06-03 21:42:52 +02:00
if(deinit_func != NULL && args != NULL) {
2021-06-05 13:29:43 +02:00
*deinit_func = mspDeinitFunc;
*args = mspDeinitArgs;
2021-06-03 21:42:52 +02:00
}
}
/**
* @brief SPI MSP Initialization
* This function configures the hardware resources used in this example:
* - Peripheral's clock enable
* - Peripheral's GPIO Configuration
* - DMA configuration for transmission request by peripheral
* - NVIC configuration for DMA interrupt request enable
* @param hspi: SPI handle pointer
* @retval None
*/
2021-06-04 16:34:38 +02:00
extern "C" void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi) {
2021-06-05 13:29:43 +02:00
if(mspInitFunc != NULL) {
mspInitFunc(mspInitArgs);
2021-06-03 21:42:52 +02:00
}
else {
printf("HAL_SPI_MspInit: Please call set_msp_functions to assign SPI MSP functions\n");
}
}
/**
* @brief SPI MSP De-Initialization
* This function frees the hardware resources used in this example:
* - Disable the Peripheral's clock
* - Revert GPIO, DMA and NVIC configuration to their default state
* @param hspi: SPI handle pointer
* @retval None
*/
2021-06-04 16:34:38 +02:00
extern "C" void HAL_SPI_MspDeInit(SPI_HandleTypeDef *hspi) {
2021-06-05 13:29:43 +02:00
if(mspDeinitFunc != NULL) {
mspDeinitFunc(mspDeinitArgs);
2021-06-03 21:42:52 +02:00
}
else {
printf("HAL_SPI_MspDeInit: Please call set_msp_functions to assign SPI MSP functions\n");
}
}
2021-06-05 13:29:43 +02:00
/**
* @brief TxRx Transfer completed callback.
* @param hspi: SPI handle
*/
extern "C" void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi) {
if(rxTxCb != NULL) {
rxTxCb(hspi, rxTxArgs);
}
else {
printf("HAL_SPI_TxRxCpltCallback: No user callback specified\n");
}
}
/**
* @brief TxRx Transfer completed callback.
* @param hspi: SPI handle
*/
extern "C" void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi) {
if(txCb != NULL) {
txCb(hspi, txArgs);
}
else {
printf("HAL_SPI_TxCpltCallback: No user callback specified\n");
}
}
/**
* @brief TxRx Transfer completed callback.
* @param hspi: SPI handle
*/
extern "C" void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi) {
if(rxCb != nullptr) {
rxCb(hspi, rxArgs);
}
else {
printf("HAL_SPI_RxCpltCallback: No user callback specified\n");
}
}
/**
* @brief SPI error callbacks.
* @param hspi: SPI handle
* @note This example shows a simple way to report transfer error, and you can
* add your own implementation.
* @retval None
*/
extern "C" void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi) {
if(errorCb != nullptr) {
errorCb(hspi, rxArgs);
}
else {
printf("HAL_SPI_ErrorCallback: No user callback specified\n");
}
}