#include "bsp_spi.h" #include "memory.h" #include "stdlib.h" /* 所有的spi instance保存于此,用于callback时判断中断来源*/ static SPIInstance *spi_instance[SPI_DEVICE_CNT] = {NULL}; static uint8_t idx = 0; // 配合中断以及初始化 /** * @brief 当SPI接收完成,将会调用此回调函数,可以进行协议解析或其他必须的数据处理等 * * @param hspi spi handle */ void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi) { for (size_t i = 0; i < idx; i++) { // 如果是当前spi硬件发出的complete,且cs_pin为低电平(说明正在传输),则尝试调用回调函数 if (spi_instance[i]->spi_handle == hspi && HAL_GPIO_ReadPin(spi_instance[i]->GPIO_cs, spi_instance[i]->cs_pin) == GPIO_PIN_RESET) { if (spi_instance[i]->callback) // 回调函数不为空, 则调用回调函数 { // 先拉高片选,结束传输 HAL_GPIO_WritePin(spi_instance[i]->GPIO_cs, spi_instance[i]->cs_pin, GPIO_PIN_SET); spi_instance[i]->callback(spi_instance[i]); } return; } } } /** * @brief 和RxCpltCallback共用解析即可,这里只是形式上封装一下,不用重复写 * * @param hspi spi handle */ void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi) { HAL_SPI_RxCpltCallback(hspi); } SPIInstance *SPIRegister(SPI_Init_Config_s *conf) { SPIInstance *instance = (SPIInstance *)malloc(sizeof(SPIInstance)); memset(instance, 0, sizeof(SPIInstance)); instance->spi_handle = conf->spi_handle; instance->GPIO_cs = conf->GPIO_cs; instance->cs_pin = conf->cs_pin; instance->spi_work_mode = conf->spi_work_mode; instance->callback = conf->callback; instance->id = conf->id; spi_instance[idx++] = instance; return instance; } void SPITransmit(SPIInstance *spi_ins, uint8_t *ptr_data, uint8_t len) { // 拉低片选,开始传输 HAL_GPIO_WritePin(spi_ins->GPIO_cs, spi_ins->cs_pin, GPIO_PIN_RESET); switch (spi_ins->spi_work_mode) { case SPI_DMA_MODE: HAL_SPI_Transmit_DMA(spi_ins->spi_handle, ptr_data, len); break; case SPI_IT_MODE: HAL_SPI_Transmit_IT(spi_ins->spi_handle, ptr_data, len); break; case SPI_BLOCK_MODE: HAL_SPI_Transmit(spi_ins->spi_handle, ptr_data, len, 10); // 阻塞模式不会调用回调函数,传输完成后直接拉高片选结束 HAL_GPIO_WritePin(spi_ins->GPIO_cs, spi_ins->cs_pin, GPIO_PIN_SET); break; default: while (1) ; // error mode! 请查看是否正确设置模式,或出现指针越界导致模式被异常修改的情况 break; } } void SPIRecv(SPIInstance *spi_ins, uint8_t *ptr_data, uint8_t len) { // 用于稍后回调使用 spi_ins->rx_size = len; spi_ins->rx_buffer = ptr_data; // 拉低片选,开始传输 HAL_GPIO_WritePin(spi_ins->GPIO_cs, spi_ins->cs_pin, GPIO_PIN_RESET); switch (spi_ins->spi_work_mode) { case SPI_DMA_MODE: HAL_SPI_Receive_DMA(spi_ins->spi_handle, ptr_data, len); break; case SPI_IT_MODE: HAL_SPI_Receive_IT(spi_ins->spi_handle, ptr_data, len); break; case SPI_BLOCK_MODE: HAL_SPI_Receive(spi_ins->spi_handle, ptr_data, len, 10); // 阻塞模式不会调用回调函数,传输完成后直接拉高片选结束 HAL_GPIO_WritePin(spi_ins->GPIO_cs, spi_ins->cs_pin, GPIO_PIN_SET); break; default: while (1) ; // error mode! 请查看是否正确设置模式,或出现指针越界导致模式被异常修改的情况 break; } } void SPITransRecv(SPIInstance *spi_ins, uint8_t *ptr_data_rx, uint8_t *ptr_data_tx, uint8_t len) { // 用于稍后回调使用 spi_ins->rx_size = len; spi_ins->rx_buffer = ptr_data_rx; // 拉低片选,开始传输 HAL_GPIO_WritePin(spi_ins->GPIO_cs, spi_ins->cs_pin, GPIO_PIN_RESET); switch (spi_ins->spi_work_mode) { case SPI_DMA_MODE: HAL_SPI_TransmitReceive_DMA(spi_ins->spi_handle, ptr_data_tx, ptr_data_rx, len); break; case SPI_IT_MODE: HAL_SPI_TransmitReceive_IT(spi_ins->spi_handle, ptr_data_tx, ptr_data_rx, len); break; case SPI_BLOCK_MODE: HAL_SPI_TransmitReceive(spi_ins->spi_handle, ptr_data_tx, ptr_data_rx, len, 10); // 阻塞模式不会调用回调函数,传输完成后直接拉高片选结束 HAL_GPIO_WritePin(spi_ins->GPIO_cs, spi_ins->cs_pin, GPIO_PIN_SET); break; default: while (1) ; // error mode! 请查看是否正确设置模式,或出现指针越界导致模式被异常修改的情况 break; } } void SPISetMode(SPIInstance *spi_ins, SPI_TXRX_MODE_e spi_mode) { if (spi_mode != SPI_DMA_MODE && spi_mode != SPI_IT_MODE && spi_mode != SPI_BLOCK_MODE) while (1) ; // error mode! 请查看是否正确设置模式,或出现指针越界导致模式被异常修改的情况 if (spi_ins->spi_work_mode != spi_mode) { spi_ins->spi_work_mode = spi_mode; } }