Skip to content

Commit 87e6ae9

Browse files
authored
Merge pull request #959 from rickgaiser/mx4sio-unaligned-buffers
mx4sio: read and write buffers that are not word aligned
2 parents 3aaaa88 + 3e59cc2 commit 87e6ae9

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

‎iop/sio/mx4sio_bd/src/imports.lst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ stdio_IMPORTS_end
3030
#endif
3131

3232
sysclib_IMPORTS_start
33+
I_memcpy
3334
I_strcmp
3435
stdio_IMPORTS_end
3536

‎iop/sio/mx4sio_bd/src/spi_sdcard_driver.c‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <sysclib.h>
12
#include <bdm.h>
23
#include <stdint.h>
34
#include <thevent.h>
@@ -577,6 +578,11 @@ static int spisd_write_multi_end()
577578
* BDM interface:
578579
* - BDM -> "spisd" library
579580
*/
581+
/* DMA and reverse_buffer work on words: a buffer that is not word aligned
582+
* (FatFs passes the caller's, at any offset) goes a sector at a time through
583+
* this one */
584+
static uint32_t bounce[SECTOR_SIZE / 4];
585+
580586
int spisd_read(struct block_device *bd, uint64_t sector, void *buffer, uint16_t count)
581587
{
582588
uint16_t sectors_left = count;
@@ -588,6 +594,16 @@ int spisd_read(struct block_device *bd, uint64_t sector, void *buffer, uint16_t
588594
if (count == 0)
589595
return 0;
590596

597+
if ((uint32_t)buffer & 3) {
598+
uint16_t i;
599+
for (i = 0; i < count; i++) {
600+
if (spisd_read(bd, sector + i, bounce, 1) != 1)
601+
break;
602+
memcpy((uint8_t *)buffer + i * SECTOR_SIZE, bounce, SECTOR_SIZE);
603+
}
604+
return i;
605+
}
606+
591607
M_DEBUG("%s: sector %i, count: %i \n", __FUNCTION__, (u32)sector, count);
592608

593609
mx_sio2_lock(INTR_RX);
@@ -642,6 +658,16 @@ int spisd_write(struct block_device *bd, uint64_t sector, const void *buffer, ui
642658
if (count == 0)
643659
return 0;
644660

661+
if ((uint32_t)buffer & 3) {
662+
uint16_t i;
663+
for (i = 0; i < count; i++) {
664+
memcpy(bounce, (const uint8_t *)buffer + i * SECTOR_SIZE, SECTOR_SIZE);
665+
if (spisd_write(bd, sector + i, bounce, 1) != 1)
666+
break;
667+
}
668+
return i;
669+
}
670+
645671
M_DEBUG("%s: sector %i, count: %i \n", __FUNCTION__, (u32)sector, count);
646672

647673
/* recast */

0 commit comments

Comments
 (0)