Skip to content

Commit 5d79ba8

Browse files
committed
QSPI write alignment fix for nRF52x
If the data buffer for a write is not 4-byte aligned, use an aligned 16 byte buffer on the stack
1 parent 0e7f112 commit 5d79ba8

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

targets/TARGET_NORDIC/TARGET_NRF5x/qspi_api.c

Lines changed: 30 additions & 6 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -71,6 +71,7 @@ TODO
71

71

72
#define SCK_DELAY 0x05
72
#define SCK_DELAY 0x05
73
#define WORD_MASK 0x03
73
#define WORD_MASK 0x03
74+
#define WORD_COUNT 16
74

75

75
// NRF SFDP defines
76
// NRF SFDP defines
76
#define DWORD_LEN 4
77
#define DWORD_LEN 4
@@ -272,13 +273,36 @@ qspi_status_t qspi_write(qspi_t *obj, const qspi_command_t *command, const void
272
return status;
273
return status;
273
}
274
}
274

275

275-
// write here does not return how much it transfered, we return transfered all
276+
if (is_word_aligned(data)) {
276-
ret_code_t ret = nrf_drv_qspi_write(data, *length, command->address.value);
277+
// write here does not return how much it transfered, we return transfered all
277-
if (ret == NRF_SUCCESS ) {
278+
ret_code_t ret = nrf_drv_qspi_write(data, *length, command->address.value);
278-
return QSPI_STATUS_OK;
279+
if (ret == NRF_SUCCESS ) {
279-
} else {
280+
return QSPI_STATUS_OK;
280-
return QSPI_STATUS_ERROR;
281+
} else {
282+
return QSPI_STATUS_ERROR;
283+
}
284+
}
285+
else {
286+
// if the data buffer is not WORD/4-byte aligned, use an aligned buffer on the stack
287+
uint32_t aligned_buffer[WORD_COUNT];
288+
uint32_t pos = 0;
289+
size_t bytes_to_write = *length;
290+
291+
while(pos < *length) {
292+
// copy into the aligned buffer
293+
size_t diff = bytes_to_write <= sizeof(aligned_buffer) ? bytes_to_write : sizeof(aligned_buffer);
294+
memcpy(aligned_buffer, &((const uint8_t *)data)[pos], diff);
295+
296+
// write one buffer over QSPI
297+
ret_code_t ret = nrf_drv_qspi_write(aligned_buffer, diff, command->address.value+pos);
298+
if (ret != NRF_SUCCESS ) {
299+
return QSPI_STATUS_ERROR;
300+
}
301+
pos += diff;
302+
bytes_to_write -= diff;
303+
}
281
}
304
}
305+
return QSPI_STATUS_OK;
282
}
306
}
283

307

284
qspi_status_t qspi_read(qspi_t *obj, const qspi_command_t *command, void *data, size_t *length)
308
qspi_status_t qspi_read(qspi_t *obj, const qspi_command_t *command, void *data, size_t *length)

0 commit comments

Comments
 (0)