@@ -142,7 +142,7 @@ void spi_frequency(spi_t *obj, int hz)
142
142
int spi_master_write (spi_t * obj , int value )
143
143
{
144
144
uint32_t rxval = 0 ;
145
- spi_master_block_write (obj , (const char * )& value , 1 , (char * )& rxval , 1 , 0x00 );
145
+ spi_master_block_write (obj , (const char * )& value , 1 , (char * )& rxval , 1 , SPI_FILL_CHAR );
146
146
return rxval ;
147
147
}
148
148
@@ -151,53 +151,42 @@ int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, cha
151
151
MBED_ASSERT (obj );
152
152
153
153
int chars_handled = 0 ;
154
+ uint32_t status = AM_HAL_STATUS_SUCCESS ;
154
155
155
- // perform a duplex xfer for the smaller of the two buffers
156
+ // always perform a duplex xfer
156
157
xfer .eDirection = AM_HAL_IOM_FULLDUPLEX ;
157
- xfer .ui32NumBytes = (tx_length > rx_length ) ? rx_length : tx_length ;
158
- xfer .pui32RxBuffer = (uint32_t * )rx_buffer ;
159
- xfer .pui32TxBuffer = (uint32_t * )tx_buffer ;
160
-
161
- if (xfer .ui32NumBytes ) {
162
- uint32_t status = am_hal_iom_spi_blocking_fullduplex (obj -> spi .iom_obj .iom .handle , & xfer );
163
- if (AM_HAL_STATUS_SUCCESS != status ) {
164
- return 0 ;
165
- }
166
- chars_handled += xfer .ui32NumBytes ;
158
+
159
+ if (tx_length == rx_length ) {
160
+ xfer .pui32RxBuffer = (uint32_t * )rx_buffer ;
161
+ xfer .pui32TxBuffer = (uint32_t * )tx_buffer ;
162
+ xfer .ui32NumBytes = tx_length ;
163
+ status = am_hal_iom_spi_blocking_fullduplex (obj -> spi .iom_obj .iom .handle , & xfer );
167
164
}
168
165
169
166
// handle difference between buffers
170
- if (tx_length != rx_length ) {
171
- bool Rw = (rx_length >= tx_length );
172
-
173
- // set up common config
174
- xfer .eDirection = (Rw ) ? AM_HAL_IOM_RX : AM_HAL_IOM_TX ;
175
- xfer .ui32NumBytes = (Rw ) ? (rx_length - tx_length ) : (tx_length - rx_length );
176
- xfer .pui32RxBuffer = (Rw ) ? (uint32_t * )(rx_buffer + chars_handled ) : NULL ;
177
- xfer .pui32TxBuffer = (Rw ) ? NULL : (uint32_t * )(tx_buffer + chars_handled );
178
-
179
- uint32_t status = AM_HAL_STATUS_SUCCESS ;
180
- if (!Rw || (write_fill == 0x00 )) {
181
- // when transmitting (w) or reading with a zero fill just use a simplex transfer
182
- status = am_hal_iom_blocking_transfer (obj -> spi .iom_obj .iom .handle , & xfer );
183
- if (AM_HAL_STATUS_SUCCESS != status ) {
184
- return chars_handled ;
185
- }
186
- chars_handled += xfer .ui32NumBytes ;
187
- } else {
188
- // when reading with a nonzero fill use a duplex transfer
189
- uint8_t fill [xfer .ui32NumBytes ];
190
- memset (fill , write_fill , xfer .ui32NumBytes );
191
- xfer .eDirection = AM_HAL_IOM_FULLDUPLEX ;
192
- xfer .pui32TxBuffer = (uint32_t * )& fill ;
193
- uint32_t status = am_hal_iom_spi_blocking_fullduplex (obj -> spi .iom_obj .iom .handle , & xfer );
194
- if (AM_HAL_STATUS_SUCCESS != status ) {
195
- return chars_handled ;
196
- }
197
- chars_handled += xfer .ui32NumBytes ;
198
- }
167
+ else if (tx_length < rx_length ) {
168
+ xfer .pui32RxBuffer = (uint32_t * )rx_buffer ;
169
+ xfer .ui32NumBytes = rx_length - tx_length ;
170
+ uint8_t fill [xfer .ui32NumBytes ];
171
+ memset (fill , write_fill , xfer .ui32NumBytes );
172
+ xfer .pui32TxBuffer = (uint32_t * )& fill ;
173
+ status = am_hal_iom_spi_blocking_fullduplex (obj -> spi .iom_obj .iom .handle , & xfer );
174
+ }
175
+
176
+ else {
177
+ xfer .pui32TxBuffer = (uint32_t * )tx_buffer ;
178
+ xfer .ui32NumBytes = tx_length - rx_length ;
179
+ uint8_t fill [xfer .ui32NumBytes ];
180
+ memset (fill , write_fill , xfer .ui32NumBytes );
181
+ xfer .pui32RxBuffer = (uint32_t * )& fill ;
182
+ status = am_hal_iom_spi_blocking_fullduplex (obj -> spi .iom_obj .iom .handle , & xfer );
183
+ }
184
+
185
+ if (AM_HAL_STATUS_SUCCESS != status ) {
186
+ return 0 ;
199
187
}
200
188
189
+ chars_handled += xfer .ui32NumBytes ;
201
190
return chars_handled ;
202
191
}
203
192
0 commit comments