From 206f2d5e6b2c2b6dd9ab1c3dcace9d8fd2d17b04 Mon Sep 17 00:00:00 2001 From: Peter Weingartner Date: Sun, 22 Dec 2024 21:21:47 -0500 Subject: [PATCH] Fixed SDC sector count logic for SDC read/write --- src/dev/sdc_f256.c | 5 +++-- src/utilities.c | 10 ++++++++++ src/utilities.h | 9 +++++++++ src/version.h | 2 +- 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/dev/sdc_f256.c b/src/dev/sdc_f256.c index 48e2f7c..c6f7546 100644 --- a/src/dev/sdc_f256.c +++ b/src/dev/sdc_f256.c @@ -23,6 +23,7 @@ #include "interrupt.h" #include "F256/sdc_spi.h" #include "sdc_f256.h" +#include "utilities.h" /* MMC/SD command (SPI mode) */ #define CMD0 (0) /* GO_IDLE_STATE */ @@ -379,7 +380,7 @@ static short sdc_read(p_dev_block dev, long lba, uint8_t * buffer, short size) { p_sd_card_info card = (p_sd_card_info)dev->data; p_sdc_spi sd = card->reg; uint8_t cmd; - short count = size % 512 + 1; + short count = ceil_div_short(size, 512); if (card->status & SDC_STAT_NOINIT) { return ERR_NOT_READY; @@ -420,7 +421,7 @@ static short sdc_write(p_dev_block dev, long lba, const uint8_t * buffer, short p_sd_card_info card = (p_sd_card_info)dev->data; p_sdc_spi sd = card->reg; uint8_t cmd; - short count = size % 512 + 1; + short count = ceil_div_short(size, 512); if (card->status & SDC_STAT_NOINIT) { return ERR_NOT_READY; diff --git a/src/utilities.c b/src/utilities.c index 24a1174..f1b7fb7 100644 --- a/src/utilities.c +++ b/src/utilities.c @@ -7,6 +7,16 @@ #include #include "utilities.h" +/** + * Return the ceiling of a/b using only short operations + * + * @param a the numerator + * @param b the denominator + * @return the smallest short c such that c >= a / b + */ +short ceil_div_short(short a, short b) { + return (a + (b - 1)) / b; +} /** * Re-entrant version of strtok_r, because VBCC does not provide it diff --git a/src/utilities.h b/src/utilities.h index c4f2e06..46a2815 100644 --- a/src/utilities.h +++ b/src/utilities.h @@ -13,6 +13,15 @@ /** Return the maximum value of x or y */ #define max(x, y) ((x < y) ? y : x) +/** + * Return the ceiling of a/b using only short operations + * + * @param a the numerator + * @param b the denominator + * @return the smallest short c such that c >= a / b + */ +extern short ceil_div_short(short a, short b); + /** * Re-entrant version of strtok_r, because VBCC does not provide it * diff --git a/src/version.h b/src/version.h index 4bc6355..48291ed 100644 --- a/src/version.h +++ b/src/version.h @@ -7,6 +7,6 @@ #define VER_MAJOR 1 #define VER_MINOR 1 -#define VER_BUILD 9 +#define VER_BUILD 10 #endif