87{
89
90 if (dma_buff_rx_.
size_ > 0)
91 {
92 ASSERT(need_write <= dma_buff_rx_.
size_);
93 }
94
95 if (dma_buff_tx_.
size_ > 0)
96 {
97 ASSERT(need_write <= dma_buff_tx_.
size_);
98 }
99
100 if (spi_handle_->State != HAL_SPI_STATE_READY)
101 {
102 return ErrorCode::BUSY;
103 }
104
105 mem_read_ = false;
106
107 if (need_write > dma_enable_min_size_)
108 {
109 rw_op_ = op;
110
111 if (write_data.
size_ > 0)
112 {
113 memcpy(dma_buff_tx_.
addr_, write_data.
addr_, need_write);
114
115#if __DCACHE_PRESENT
116 SCB_CleanDCache_by_Addr(
static_cast<uint32_t *
>(dma_buff_tx_.
addr_),
117 static_cast<int32_t
>(write_data.
size_));
118#endif
119 }
120 if (read_data.
size_ > 0)
121 {
122 read_buff_ = read_data;
123 }
124
125 if (write_data.
size_ > 0 && read_data.
size_ > 0)
126 {
127 HAL_SPI_TransmitReceive_DMA(spi_handle_,
static_cast<uint8_t *
>(dma_buff_tx_.
addr_),
128 static_cast<uint8_t *
>(dma_buff_rx_.
addr_), need_write);
129 }
130 else if (write_data.
size_ > 0)
131 {
132 HAL_SPI_Transmit_DMA(spi_handle_,
static_cast<uint8_t *
>(dma_buff_tx_.
addr_),
133 need_write);
134 }
135 else if (read_data.
size_ > 0)
136 {
137 HAL_SPI_Receive_DMA(spi_handle_,
static_cast<uint8_t *
>(dma_buff_rx_.
addr_),
138 need_write);
139 }
140 else
141 {
142 if (op.type != OperationRW::OperationType::BLOCK)
143 {
144 op.UpdateStatus(false, ErrorCode::OK);
145 }
146 return ErrorCode::OK;
147 }
148
150 if (op.type == OperationRW::OperationType::BLOCK)
151 {
152 return op.data.sem_info.sem->Wait(op.data.sem_info.timeout);
153 }
154 return ErrorCode::OK;
155 }
156 else
157 {
158 if (write_data.
size_ > 0)
159 {
161 }
162
163 ErrorCode ans = ErrorCode::OK;
164 if (read_data.
size_ > 0 && write_data.
size_ > 0)
165 {
166 ans = HAL_SPI_TransmitReceive(
167 spi_handle_,
static_cast<uint8_t *
>(dma_buff_tx_.
addr_),
168 static_cast<uint8_t *
>(dma_buff_rx_.
addr_), need_write, 20) == HAL_OK
169 ? ErrorCode::OK
170 : ErrorCode::BUSY;
171 }
172 else if (read_data.
size_ > 0)
173 {
174 ans = HAL_SPI_Receive(spi_handle_,
static_cast<uint8_t *
>(dma_buff_rx_.
addr_),
175 need_write, 20) == HAL_OK
176 ? ErrorCode::OK
177 : ErrorCode::BUSY;
178 }
179 else if (write_data.
size_ > 0)
180 {
181 ans = HAL_SPI_Transmit(spi_handle_,
static_cast<uint8_t *
>(dma_buff_tx_.
addr_),
182 need_write, 20) == HAL_OK
183 ? ErrorCode::OK
184 : ErrorCode::BUSY;
185 }
186 else
187 {
188 if (op.type != OperationRW::OperationType::BLOCK)
189 {
190 op.UpdateStatus(false, ErrorCode::OK);
191 }
192 return ErrorCode::OK;
193 }
194
196
197 op.UpdateStatus(false, std::forward<ErrorCode>(ans));
198
199 if (op.type == OperationRW::OperationType::BLOCK)
200 {
201 return op.data.sem_info.sem->Wait(op.data.sem_info.timeout);
202 }
203
204 return ans;
205 }
206}
void MarkAsRunning()
标记操作为运行状态。 Marks the operation as running.
constexpr auto max(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最大值