Skip to content

Commit

Permalink
add my own SD card driver!
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitmel committed Sep 24, 2023
1 parent 92e6c4e commit a8a78d3
Show file tree
Hide file tree
Showing 20 changed files with 1,942 additions and 830 deletions.
45 changes: 0 additions & 45 deletions src/stm32_hal_patches.h

This file was deleted.

4 changes: 1 addition & 3 deletions src/stm32f4xx_hal_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
/* #define HAL_RNG_MODULE_ENABLED */
/* #define HAL_RTC_MODULE_ENABLED */
/* #define HAL_SAI_MODULE_ENABLED */
#define HAL_SD_MODULE_ENABLED
/* #define HAL_SD_MODULE_ENABLED */
/* #define HAL_MMC_MODULE_ENABLED */
/* #define HAL_SPI_MODULE_ENABLED */
#define HAL_TIM_MODULE_ENABLED
Expand Down Expand Up @@ -271,8 +271,6 @@
* @brief Include module's header file
*/

#include "stm32_hal_patches.h"

#ifdef HAL_RCC_MODULE_ENABLED
#include "stm32f4xx_hal_rcc.h"
#endif /* HAL_RCC_MODULE_ENABLED */
Expand Down
5 changes: 0 additions & 5 deletions src/stmes/demos/image_viewer.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "stmes/demos.h"
#include "stmes/fatfs.h"
#include "stmes/sdio.h"
#include "stmes/utils.h"
#include "stmes/video/framebuf.h"
#include "stmes/video/vga.h"
Expand All @@ -13,10 +12,6 @@ void image_viewer_demo(void) {
static FATFS SDFatFS;
static FIL SDFile;

while (BSP_SD_Init() != HAL_OK) {
HAL_Delay(500);
}

check_fs_error(f_mount(&SDFatFS, "", 1));
check_fs_error(f_open(&SDFile, "penguins_palette.bin", FA_READ));

Expand Down
88 changes: 62 additions & 26 deletions src/stmes/demos/sd_card_benchmark.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "stmes/demos.h"
#include "stmes/drivers/sdmmc.h"
#include "stmes/fatfs.h"
#include "stmes/kernel/task.h"
#include "stmes/sdio.h"
#include "stmes/utils.h"
#include "stmes/video/console.h"
#include "stmes/video/vga.h"
Expand All @@ -25,51 +25,81 @@ static struct Notification progress_task_notify;

static FATFS SDFatFS;
static FIL SDFile;
#if !_FS_READONLY
static FIL SDFile2;
#endif

static void test_task_fn(__UNUSED void* user_data) {
while (BSP_SD_Init() != HAL_OK) {
printf(".");
task_sleep(1000);
}

console_clear_screen();
static char buf[SDMMC_BLOCK_SIZE * 32];

check_fs_error(f_mount(&SDFatFS, "", 1));
FRESULT fres = f_mount(&SDFatFS, "", 1);
if (fres == FR_NO_FILESYSTEM) {
#if !_FS_READONLY && _USE_MKFS
fres = f_mkfs("", FM_ANY, 0, buf, sizeof(buf));
#endif
}
check_fs_error(fres);

const struct SdmmcCard* card = sdmmc_get_card();
printf(
"CID: %08" PRIX32 " %08" PRIX32 " %08" PRIX32 " %08" PRIX32 "\n",
card->cid.words[3],
card->cid.words[2],
card->cid.words[1],
card->cid.words[0]
);
const struct SdCID* cid = &card->cid.sd;
printf(" Manufacturer ID : 0x%02X\n", cid->manufacturer_id);
printf(" OEM/Application ID : %.2s\n", cid->oem_application_id);
const char* name = cid->product_name;
printf(" Product Name : %c%c%c%c%c\n", name[4], name[3], name[2], name[1], name[0]);
printf(
" Product Revision : %d.%d\n",
(cid->product_revision >> 4) & 0xF,
cid->product_revision & 0xF
);
printf(" Serial Number : 0x%08" PRIX32 "\n", cid->serial_number);
printf(
" Manufacturing Date : %04d/%02d\n", cid->manufacturing_year + 2000, cid->manufacturing_month
);

task_sleep(3000);

while (true) {
check_fs_error(f_open(&SDFile, "bebop_palette.bin", FA_READ));
#if !_FS_READONLY
check_fs_error(f_open(&SDFile2, "copy.bin", FA_WRITE | FA_CREATE_ALWAYS));
#endif

printf("loading %lu\n", f_size(&SDFile));
task_yield();

usize total_bytes = 0;
static char buf[BLOCKSIZE * 8];
Systime start_time = systime_now();

while (true) {
task_notify(&progress_task_notify);
// yield();
task_yield();
#if 1
usize bytes_read = 0;
if (f_read(&SDFile, buf, sizeof(buf), &bytes_read) != FR_OK) {
break;
}
if (bytes_read == 0) {
check_fs_error(f_read(&SDFile, buf, sizeof(buf), &bytes_read));
if (bytes_read == 0) break;
total_bytes += bytes_read;
#if !_FS_READONLY
check_fs_error(f_write(&SDFile2, buf, bytes_read, &bytes_read));
#endif
#else
// disk_read(0, (BYTE*)buf, total_bytes / SDMMC_BLOCK_SIZE, sizeof(buf) / SDMMC_BLOCK_SIZE);
sdmmc_read(
(u8*)buf, total_bytes / SDMMC_BLOCK_SIZE, sizeof(buf) / SDMMC_BLOCK_SIZE, NO_DEADLINE
);
total_bytes += sizeof(buf);
if (total_bytes >= 1024 * 1024) {
break;
}
total_bytes += bytes_read;
#endif
}

// for (u32 i = 0, sectors = sizeof(buf) / BLOCKSIZE; true; i += sectors) {
// task_notify(&progress_task_notify);
// // yield();
// disk_read(0, (BYTE*)buf, i, sectors);
// u32 bytes_read = BLOCKSIZE * sectors;
// if (total_bytes >= 1024 * 1024) {
// break;
// }
// total_bytes += bytes_read;
// }

task_notify(&progress_task_notify);
task_yield();

Expand All @@ -84,7 +114,13 @@ static void test_task_fn(__UNUSED void* user_data) {

task_sleep(2000);
f_close(&SDFile);
#if !_FS_READONLY
f_close(&SDFile2);
break;
#endif
}

check_fs_error(f_mount(0, "", 0));
}

static void progress_task_fn(__UNUSED void* user_data) {
Expand Down
6 changes: 0 additions & 6 deletions src/stmes/demos/terminal.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "stmes/demos.h"
#include "stmes/fatfs.h"
#include "stmes/kernel/task.h"
#include "stmes/sdio.h"
#include "stmes/utils.h"
#include "stmes/video/console.h"
#include "stmes/video/vga.h"
Expand All @@ -19,11 +18,6 @@ static void terminal_task_fn(__UNUSED void* user_data) {
static FATFS SDFatFS;
static DIR SDDir;

while (BSP_SD_Init() != HAL_OK) {
printf(".");
task_sleep(1000);
}

check_fs_error(f_mount(&SDFatFS, "", 1));
check_fs_error(f_opendir(&SDDir, "/"));

Expand Down
11 changes: 4 additions & 7 deletions src/stmes/demos/video_player.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#include "stmes/demos.h"
#include "stmes/drivers/sdmmc.h"
#include "stmes/fatfs.h"
#include "stmes/gpio.h"
#include "stmes/kernel/crash.h"
#include "stmes/sdio.h"
#include "stmes/kernel/task.h"
#include "stmes/utils.h"
#include "stmes/video/framebuf.h"
#include "stmes/video/vga.h"
#include "stmes/video/vga_color.h"
#include <ff.h>
#include <stdlib.h>
#include <stm32f4xx_hal.h>
#include <stm32f4xx_ll_gpio.h>

Expand All @@ -22,7 +23,7 @@ static void button_task_fn(void* user_data) {
}

struct BufferedReader {
u8 buffer[BLOCKSIZE * 8];
u8 buffer[SDMMC_BLOCK_SIZE * 16];
FSIZE_t offset_start;
FSIZE_t offset_end;
};
Expand All @@ -31,7 +32,7 @@ static u8* buffered_read(struct BufferedReader* self, FIL* file, FSIZE_t pos, FS
const FSIZE_t capacity = SIZEOF(self->buffer);
ASSERT(len <= capacity);
if (unlikely(!(self->offset_start <= pos && pos + len <= self->offset_end))) {
self->offset_start = pos & ~(BLOCKSIZE - 1); // align to 512-byte boundaries
self->offset_start = align_to(pos, SDMMC_BLOCK_SIZE);
self->offset_end = self->offset_start;
if (f_tell(file) != self->offset_start) {
check_fs_error(f_lseek(file, self->offset_start));
Expand Down Expand Up @@ -59,10 +60,6 @@ void video_player_demo(void) {
static FATFS SDFatFS;
static FIL SDFile;

while (BSP_SD_Init() != HAL_OK) {
HAL_Delay(500);
}

check_fs_error(f_mount(&SDFatFS, "", 1));
check_fs_error(f_open(&SDFile, "bebop_palette.bin", FA_READ));

Expand Down
16 changes: 8 additions & 8 deletions src/stmes/drivers/dma.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,27 @@ struct DmaConfig {
memory_burst : 2;
};

#define DMA_FLAG_FEIF BIT(0)
#define DMA_FLAG_DMEIF BIT(2)
#define DMA_FLAG_TEIF BIT(3)
#define DMA_FLAG_HTIF BIT(4)
#define DMA_FLAG_TCIF BIT(5)
#define DMA_FLAG_FEIF BIT(0) // FIFO error
#define DMA_FLAG_DMEIF BIT(2) // Direct mode error
#define DMA_FLAG_TEIF BIT(3) // Transfer error
#define DMA_FLAG_HTIF BIT(4) // Transfer half-complete
#define DMA_FLAG_TCIF BIT(5) // Transfer complete
#define DMA_ALL_INTERRUPT_FLAGS \
(DMA_FLAG_FEIF | DMA_FLAG_DMEIF | DMA_FLAG_TEIF | DMA_FLAG_HTIF | DMA_FLAG_TCIF)

__STATIC_FORCEINLINE DMA_TypeDef* dma_get_base_registers(DMA_Stream_TypeDef* dma) {
__STATIC_FORCEINLINE DMA_TypeDef* dma_get_base_registers(const DMA_Stream_TypeDef* dma) {
// RM0383 section 2.3 "Memory map"
// <https://github.com/STMicroelectronics/STM32CubeF4/blob/v1.27.1/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma.c#L1201>
return (DMA_TypeDef*)((usize)dma & ~0x3FF);
}

__STATIC_FORCEINLINE usize dma_get_stream_index(DMA_Stream_TypeDef* dma) {
__STATIC_FORCEINLINE usize dma_get_stream_index(const DMA_Stream_TypeDef* dma) {
// RM0383 section 9.5.11 "DMA register map"
// <https://github.com/STMicroelectronics/STM32CubeF4/blob/v1.27.1/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma.c#L1187>
return (((usize)dma & 0xFF) - sizeof(DMA_TypeDef)) / sizeof(DMA_Stream_TypeDef);
}

__STATIC_FORCEINLINE u32 dma_get_interrupt_flags(DMA_Stream_TypeDef* dma) {
__STATIC_FORCEINLINE u32 dma_get_interrupt_flags(const DMA_Stream_TypeDef* dma) {
DMA_TypeDef* base = dma_get_base_registers(dma);
usize idx = dma_get_stream_index(dma);
static const u8 flags_offset[] = { 0, 6, 16, 22 };
Expand Down
Loading

0 comments on commit a8a78d3

Please sign in to comment.