Skip to content

Commit

Permalink
Added CRC check to sd card read and write
Browse files Browse the repository at this point in the history
  • Loading branch information
avtoku committed Oct 24, 2024
1 parent 8e21b42 commit f07a02f
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 10 deletions.
27 changes: 24 additions & 3 deletions common/drivers/Sd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,12 @@ uint32_t Sd::init(SD_HandleTypeDef * hsd, SD_TypeDef * hsd_instance)
return initializationStatus_;
}


#define SD_CRC_BYTES sizeof(uint32_t)

bool Sd::read(uint8_t * dest, size_t len)
{
uint16_t Nblocks = (len + SD_BLKSIZE - 1) / SD_BLKSIZE;
uint16_t Nblocks = (len + SD_CRC_BYTES + SD_BLKSIZE - 1) / SD_BLKSIZE;
// if(Nblocks > sd_info.BlockNbr) Nblocks = sd_info.BlockNbr;
if (Nblocks > SD_MAXBLKS) { return 0; } // too large and don't want to be here forever, throw an error

Expand All @@ -102,19 +105,37 @@ bool Sd::read(uint8_t * dest, size_t len)

while (!rxComplete_ && (timeout > time64.Us())) {} // wait for DMA to complete

if (rxComplete_) { memcpy(dest, sd_rx_buf, len); }
if (rxComplete_) {
// CRC must be set-up for byte sized data, i.e., hcrc.InputDataFormat = CRC_INPUTDATA_FORMAT_BYTES
// I'm not bothering to code for the case of data in any other format
if (hcrc.InputDataFormat != CRC_INPUTDATA_FORMAT_BYTES) { return false; }
uint32_t crc = HAL_CRC_Calculate(&hcrc,(uint32_t *) sd_rx_buf, len); // len Assumes hcrc.InputDataFormat = CRC_INPUTDATA_FORMAT_BYTES
uint8_t *crc2 = (uint8_t*)(&crc);
uint8_t *crc1 = (uint8_t*)(&(sd_rx_buf[len]));
if((crc1[0] != crc2[0])||(crc1[1] != crc2[1])||(crc1[2] != crc2[2])||(crc1[3] != crc2[3])) { return false; }
memcpy(dest, sd_rx_buf, len);
}

return rxComplete_;
}
bool Sd::write(uint8_t * src, size_t len)
{
uint16_t Nblocks = (len + SD_BLKSIZE - 1) / SD_BLKSIZE;
uint16_t Nblocks = (len + SD_CRC_BYTES + SD_BLKSIZE - 1) / SD_BLKSIZE;
// if(Nblocks > sd_info.BlockNbr) Nblocks = sd_info.BlockNbr;
if (Nblocks > SD_MAXBLKS) return 0; // too large and don't want to be here forever, throw an error
HAL_StatusTypeDef hal_status;

txComplete_ = false;
memcpy(sd_tx_buf, src, len);
// CRC must be set-up for byte sized data hcrc.InputDataFormat = CRC_INPUTDATA_FORMAT_BYTES
// I'm not bothering to code for the case of data in any other format
if (hcrc.InputDataFormat != CRC_INPUTDATA_FORMAT_BYTES) { return false; }
uint32_t crc = HAL_CRC_Calculate(&hcrc,(uint32_t *) sd_tx_buf, len);
uint8_t *crc2 = (uint8_t*)(&crc);
sd_tx_buf[len] = crc2[0];
sd_tx_buf[len+1] = crc2[1];
sd_tx_buf[len+2] = crc2[2];
sd_tx_buf[len+3] = crc2[3];

hal_status = HAL_SD_WriteBlocks_DMA(hsd_, sd_tx_buf, 0, Nblocks);
if (hal_status != HAL_OK) return false;
Expand Down
8 changes: 4 additions & 4 deletions pixracer_pro/specific/Varmint_Init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,17 @@ void Varmint::init_board(void)
// MX_USART1_UART_Init(); // Serial 5&6 connector, not used
MX_USART2_UART_Init(); // Telem/Serial 1
// MX_USART3_UART_Init(); // Serial 2 connector, not used
// MX_USART6_UART_Init();
// MX_USART6_UART_Init(); // RC UART, initialized elsewhere

// MX_ADC3_Init(); // initialized elsewhere
// MX_ADC1_Init(); // initialized elsewhere

MX_FDCAN1_Init(); // not used yet
MX_FDCAN2_Init(); // not used yet

// MX_CRC_Init(); // not used
// MX_RNG_Init(); // not used
// MX_RTC_Init(); // not used
MX_CRC_Init(); // Used for SD Card data checksum
MX_RNG_Init(); // not used
MX_RTC_Init(); // not used

#if _USBD_USE_HS // USB HS (480MB/s
MX_USB_OTG_HS_PCD_Init();
Expand Down
4 changes: 2 additions & 2 deletions varmint_10X/specific/Varmint_Init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ void Varmint::init_board(void)
// MX_USART3_UART_Init(); // S.Bus, initialized elsewhere
// MX_ADC1_Init(); // initialized elsewhere
// MX_ADC3_Init(); // initialized elsewhere
// MX_USB_DEVICE_Init();
// MX_USB_DEVICE_Init(); // initialized elsewhere
MX_FDCAN1_Init(); // not used
// MX_SDMMC1_SD_Init(); // initialized elsewhere
MX_RTC_Init(); // not used
MX_CRC_Init(); // not used
MX_CRC_Init(); // Used for SD Card data checksum
MX_RNG_Init(); // not used

#if _USBD_USE_HS // USB HS (480MB/s
Expand Down
30 changes: 29 additions & 1 deletion varmint_10X/specific/sandbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,34 @@ void verbose_equals(void)

void sandbox(void)
{
time64.dMs(5000);

// time64.dMs(5000);
// SD Card read/write (and CRC & RNG)
// #define RNGLEN (512)
// static uint32_t random_numbers[512];
// for(int i=0;i<RNGLEN;i++) HAL_RNG_GenerateRandomNumber(&hrng, &(random_numbers[i]));
//
// uint16_t len = RNGLEN*sizeof(uint32_t)-3; // subtract 1 to make it odd.
// uint8_t *test1 = (uint8_t *)random_numbers;
//
// // Write the test1 data to SD
// bool ok1 = varmint.sd_.write(test1,len);
// if(ok1) misc_printf("OK1 \n"); else misc_printf("NOT OK1 \n");
//
// // result array
// static uint8_t test2[RNGLEN*sizeof(uint32_t)];
//
// // Read the data back
//
// bool ok2 = varmint.sd_.read(test2,len);
// if(ok2) misc_printf("OK2 \n"); else misc_printf("NOT OK2 \n");
//
// for(int i=1; i<len; i++)
// {
// if (test1[i]!=test2[i]) misc_printf("[%u] %02X -> %02X\n ",i,test1[i],test2[i]);
// }
//
// time64.dMs(5000);
// Test pwm outputs
//
// float rates[PWM_CHANNELS] = {3e5,3e5,3e5,3e5,6e5,6e5,6e5,6e5,490,490};
Expand All @@ -82,6 +108,8 @@ void sandbox(void)
// time64.dUs(450); ~ 2khs update rate
// }

time64.dMs(5000);

verbose = true;
uint32_t n = 0;
while (1) {
Expand Down

0 comments on commit f07a02f

Please sign in to comment.