libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
stm32_spi.cpp
1#include "stm32_spi.hpp"
2
3#ifdef HAL_SPI_MODULE_ENABLED
4
5using namespace LibXR;
6
7STM32SPI *STM32SPI::map[STM32_SPI_NUMBER] = {nullptr};
8
9stm32_spi_id_t STM32_SPI_GetID(SPI_TypeDef *addr)
10{
11 if (addr == nullptr)
12 { // NOLINT
13 return stm32_spi_id_t::STM32_SPI_ID_ERROR;
14 }
15#ifdef SPI1
16 else if (addr == SPI1)
17 { // NOLINT
18 return stm32_spi_id_t::STM32_SPI1;
19 }
20#endif
21#ifdef SPI2
22 else if (addr == SPI2)
23 { // NOLINT
24 return stm32_spi_id_t::STM32_SPI2;
25 }
26#endif
27#ifdef SPI3
28 else if (addr == SPI3)
29 { // NOLINT
30 return stm32_spi_id_t::STM32_SPI3;
31 }
32#endif
33#ifdef SPI4
34 else if (addr == SPI4)
35 { // NOLINT
36 return stm32_spi_id_t::STM32_SPI4;
37 }
38#endif
39#ifdef SPI5
40 else if (addr == SPI5)
41 { // NOLINT
42 return stm32_spi_id_t::STM32_SPI5;
43 }
44#endif
45#ifdef SPI6
46 else if (addr == SPI6)
47 { // NOLINT
48 return stm32_spi_id_t::STM32_SPI6;
49 }
50#endif
51#ifdef SPI7
52 else if (addr == SPI7)
53 { // NOLINT
54 return stm32_spi_id_t::STM32_SPI7;
55 }
56#endif
57#ifdef SPI8
58 else if (addr == SPI8)
59 { // NOLINT
60 return stm32_spi_id_t::STM32_SPI8;
61 }
62#endif
63 else
64 {
65 return stm32_spi_id_t::STM32_SPI_ID_ERROR;
66 }
67}
68
69extern "C" void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)
70{
71 STM32SPI *spi = STM32SPI::map[STM32_SPI_GetID(hspi->Instance)];
72
73 if (spi->read_buff_.size_ > 0)
74 {
75#if __DCACHE_PRESENT
76 SCB_InvalidateDCache_by_Addr(spi->dma_buff_rx_.addr_, spi->read_buff_.size_);
77#endif
78 if (!spi->mem_read_)
79 {
80 memcpy(spi->read_buff_.addr_, spi->dma_buff_rx_.addr_, spi->read_buff_.size_);
81 }
82 else
83 {
84 uint8_t *rx_dma_buff = reinterpret_cast<uint8_t *>(spi->dma_buff_rx_.addr_);
85 memcpy(spi->read_buff_.addr_, rx_dma_buff + 1, spi->read_buff_.size_);
86 }
87 }
88
89 spi->rw_op_.UpdateStatus(true, ErrorCode::OK);
90}
91
92extern "C" void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi)
93{
94 STM32SPI *spi = STM32SPI::map[STM32_SPI_GetID(hspi->Instance)];
95 spi->rw_op_.UpdateStatus(false, ErrorCode::FAILED);
96}
97
98#endif
void UpdateStatus(bool in_isr, Status &&...status)
Updates operation status based on type.
Definition libxr_rw.hpp:173
size_t size_
数据大小(字节)。 The size of the data (in bytes).
void * addr_
数据存储地址。 The storage address of the data.
LibXR 命名空间
Definition ch32_gpio.hpp:9