Skip to content

Commit 02a12a8

Browse files
authored
Update spi_api.c
Modified the spi_master_block_write() function yet again. SD related examples still read/erase/write the SD cards as expected and with these alterations the power consumption does not remain high after the SPI transaction has been finished. However i still only tested the SD functionality. Please test other SPI scenarios and different sensors as well to find out if this PR introduces unexpected bugs or not. Changes: - deleted the whole if (xfer.ui32NumBytes) condition as i did not find it logical (by that i mean xfer.ui32NumBytes was also true within the following else if (tx_length != rx_length){} block, so basically when the 2 buffers had different lengths an extra transfer has been done for nothing) - removed the bool Rw = (rx_length >= tx_length) as the comparison >= has no sense anymore after if (tx_length == rx_length) on line 159.
1 parent d56f942 commit 02a12a8

File tree

1 file changed

+29
-44
lines changed
  • targets/TARGET_Ambiq_Micro/TARGET_Apollo3/device

1 file changed

+29
-44
lines changed

targets/TARGET_Ambiq_Micro/TARGET_Apollo3/device/spi_api.c

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -151,57 +151,42 @@ int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, cha
151151
MBED_ASSERT(obj);
152152

153153
int chars_handled = 0;
154+
uint32_t status = AM_HAL_STATUS_SUCCESS;
154155

155-
// perform a duplex xfer for the smaller of the two buffers
156+
// always perform a duplex xfer
156157
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);
167164
}
168165

169166
// handle difference between buffers
170-
else 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 == SPI_FILL_CHAR)) {
181-
// when transmitting (w) or reading with a zero fill just use a simplex transfer
182-
uint8_t fill[xfer.ui32NumBytes];
183-
memset(fill, write_fill, xfer.ui32NumBytes);
184-
xfer.eDirection = AM_HAL_IOM_FULLDUPLEX;
185-
xfer.pui32RxBuffer = (uint32_t *)&fill;
186-
uint32_t status = am_hal_iom_spi_blocking_fullduplex(obj->spi.iom_obj.iom.handle, &xfer);
187-
if (AM_HAL_STATUS_SUCCESS != status) {
188-
return chars_handled;
189-
}
190-
chars_handled += xfer.ui32NumBytes;
191-
} else {
192-
// when reading with a nonzero fill use a duplex transfer
193-
uint8_t fill[xfer.ui32NumBytes];
194-
memset(fill, write_fill, xfer.ui32NumBytes);
195-
xfer.eDirection = AM_HAL_IOM_FULLDUPLEX;
196-
xfer.pui32TxBuffer = (uint32_t *)&fill;
197-
uint32_t status = am_hal_iom_spi_blocking_fullduplex(obj->spi.iom_obj.iom.handle, &xfer);
198-
if (AM_HAL_STATUS_SUCCESS != status) {
199-
return chars_handled;
200-
}
201-
chars_handled += xfer.ui32NumBytes;
202-
}
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;
203187
}
204188

189+
chars_handled += xfer.ui32NumBytes;
205190
return chars_handled;
206191
}
207192

0 commit comments

Comments
 (0)