32
32
*/
33
33
34
34
#include "mbed_assert.h"
35
+ #include "mbed_critical.h"
35
36
#include "spi_api.h" // mbed HAL
36
37
#include "spim_regs.h" // bare metal
37
38
#include "spim.h" // Maxim CMSIS driver
@@ -167,19 +168,58 @@ int spi_master_write(spi_t *obj, int value)
167
168
return * req .rx_data ;
168
169
}
169
170
170
- int spi_master_block_write (spi_t * obj , const char * tx_buffer , int tx_length ,
171
- char * rx_buffer , int rx_length , char write_fill ) {
172
- int total = (tx_length > rx_length ) ? tx_length : rx_length ;
171
+ //******************************************************************************
172
+ int spi_master_block_write (spi_t * obj , const char * tx_buffer , int tx_length , char * rx_buffer , int rx_length , char write_fill )
173
+ {
174
+ spim_req_t req ;
175
+
176
+ if (!(tx_length | rx_length ) ||
177
+ (tx_length < 0 ) ||
178
+ (rx_length < 0 )) {
179
+ return 0 ;
180
+ }
181
+
182
+ req .width = SPIM_WIDTH_1 ;
183
+ req .ssel = 0 ;
184
+ req .deass = 1 ;
185
+ req .callback = NULL ;
173
186
174
- for (int i = 0 ; i < total ; i ++ ) {
175
- char out = (i < tx_length ) ? tx_buffer [i ] : write_fill ;
176
- char in = spi_master_write (obj , out );
177
- if (i < rx_length ) {
178
- rx_buffer [i ] = in ;
187
+ core_util_critical_section_enter ();
188
+ if (tx_length == rx_length ) {
189
+ req .tx_data = (uint8_t * )tx_buffer ;
190
+ req .rx_data = (uint8_t * )rx_buffer ;
191
+ req .len = tx_length ;
192
+ SPIM_Trans (obj -> spi , & req );
193
+ } else if (tx_length < rx_length ) {
194
+ req .tx_data = (tx_length > 0 ) ? (uint8_t * )tx_buffer : NULL ;
195
+ req .rx_data = (uint8_t * )rx_buffer ;
196
+ req .len = (tx_length > 0 ) ? tx_length : rx_length ;
197
+ SPIM_Trans (obj -> spi , & req );
198
+
199
+ if (tx_length ) {
200
+ req .tx_data = NULL ;
201
+ req .rx_data = (uint8_t * )(rx_buffer + tx_length );
202
+ req .len = rx_length - tx_length ;
203
+ SPIM_Trans (obj -> spi , & req );
204
+ }
205
+ } else {
206
+ req .tx_data = (uint8_t * )tx_buffer ;
207
+ req .rx_data = (rx_length > 0 ) ? (uint8_t * )rx_buffer : NULL ;
208
+ req .len = (rx_length > 0 ) ? rx_length : tx_length ;
209
+ SPIM_Trans (obj -> spi , & req );
210
+
211
+ if (rx_length ) {
212
+ req .tx_data = (uint8_t * )(tx_buffer + rx_length );
213
+ req .rx_data = NULL ;
214
+ req .len = tx_length - rx_length ;
215
+ SPIM_Trans (obj -> spi , & req );
179
216
}
180
217
}
218
+ core_util_critical_section_exit ();
219
+
220
+ while (SPIM_Busy (obj -> spi ));
181
221
182
- return total ;
222
+ return tx_length > rx_length ? tx_length : rx_length ;
183
223
}
184
224
185
225
//******************************************************************************
@@ -193,4 +233,3 @@ uint8_t spi_get_module(spi_t *obj)
193
233
{
194
234
return obj -> index ;
195
235
}
196
-
0 commit comments