fsfw-hal/stm32h7/spi/spiCore.cpp

177 lines
4.6 KiB
C++

#include "spiCore.h"
#include <cstdio>
SPI_HandleTypeDef* spiHandle = nullptr;
DMA_HandleTypeDef* hdmaTx = nullptr;
DMA_HandleTypeDef* hdmaRx = nullptr;
msp_func_t mspInitFunc = nullptr;
void* mspInitArgs = nullptr;
msp_func_t mspDeinitFunc = nullptr;
void* mspDeinitArgs = nullptr;
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;
}
void spi::getDmaHandles(DMA_HandleTypeDef** txHandle, DMA_HandleTypeDef** rxHandle) {
*txHandle = hdmaTx;
*rxHandle = hdmaRx;
}
void spi::setSpiHandle(SPI_HandleTypeDef *spiHandle_) {
if(spiHandle_ == NULL) {
return;
}
spiHandle = spiHandle_;
}
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() {
return spiHandle;
}
void spi::setSpiMspFunctions(msp_func_t init_func, void* init_args, msp_func_t deinit_func,
void* deinit_args) {
mspInitFunc = init_func;
mspInitArgs = init_args;
mspDeinitFunc = deinit_func;
mspDeinitArgs = deinit_args;
}
void spi::getMspInitFunction(msp_func_t* init_func, void **args) {
if(init_func != NULL && args != NULL) {
*init_func = mspInitFunc;
*args = mspInitArgs;
}
}
void spi::getMspDeinitFunction(msp_func_t* deinit_func, void **args) {
if(deinit_func != NULL && args != NULL) {
*deinit_func = mspDeinitFunc;
*args = mspDeinitArgs;
}
}
/**
* @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
*/
extern "C" void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi) {
if(mspInitFunc != NULL) {
mspInitFunc(mspInitArgs);
}
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
*/
extern "C" void HAL_SPI_MspDeInit(SPI_HandleTypeDef *hspi) {
if(mspDeinitFunc != NULL) {
mspDeinitFunc(mspDeinitArgs);
}
else {
printf("HAL_SPI_MspDeInit: Please call set_msp_functions to assign SPI MSP functions\n");
}
}
/**
* @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");
}
}