Skip to content

Commit 74f9603

Browse files
authored
Merge pull request ARMmbed#13861 from idea--list/ambiq-apollo3-dev-squash
Ambiq apollo3 fix of an SPI related SD bug
2 parents e725df7 + 02a12a8 commit 74f9603

File tree

1 file changed

+30
-41
lines changed
  • targets/TARGET_Ambiq_Micro/TARGET_Apollo3/device

1 file changed

+30
-41
lines changed

targets/TARGET_Ambiq_Micro/TARGET_Apollo3/device/spi_api.c

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ void spi_frequency(spi_t *obj, int hz)
142142
int spi_master_write(spi_t *obj, int value)
143143
{
144144
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);
146146
return rxval;
147147
}
148148

@@ -151,53 +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-
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;
199187
}
200188

189+
chars_handled += xfer.ui32NumBytes;
201190
return chars_handled;
202191
}
203192

0 commit comments

Comments
 (0)