Skip to content

Commit b5d713d

Browse files
authored
Merge pull request #9866 from j3hill/QSPI_Align
QSPI write alignment fix for nRF52x
2 parents f8d254f + 5d79ba8 commit b5d713d

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 numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ TODO
7171

7272
#define SCK_DELAY 0x05
7373
#define WORD_MASK 0x03
74+
#define WORD_COUNT 16
7475

7576
// NRF SFDP defines
7677
#define DWORD_LEN 4
@@ -272,13 +273,36 @@ qspi_status_t qspi_write(qspi_t *obj, const qspi_command_t *command, const void
272273
return status;
273274
}
274275

275-
// write here does not return how much it transfered, we return transfered all
276-
ret_code_t ret = nrf_drv_qspi_write(data, *length, command->address.value);
277-
if (ret == NRF_SUCCESS ) {
278-
return QSPI_STATUS_OK;
279-
} else {
280-
return QSPI_STATUS_ERROR;
276+
if (is_word_aligned(data)) {
277+
// write here does not return how much it transfered, we return transfered all
278+
ret_code_t ret = nrf_drv_qspi_write(data, *length, command->address.value);
279+
if (ret == NRF_SUCCESS ) {
280+
return QSPI_STATUS_OK;
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+
}
281304
}
305+
return QSPI_STATUS_OK;
282306
}
283307

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

0 commit comments

Comments
 (0)