Fixed SDMMC DMA transfer, enabled load from SD card.

This commit is contained in:
imi415 2022-01-13 19:39:36 +08:00
parent 54eeaf989b
commit 9d79f9942b
Signed by: imi415
GPG Key ID: 17F01E106F9F5E0A
7 changed files with 96 additions and 111 deletions

View File

@ -64,7 +64,6 @@ set(TARGET_C_SOURCES
"Middlewares/Third_Party/FatFs/src/diskio.c"
"Middlewares/Third_Party/FatFs/src/ff.c"
"Middlewares/Third_Party/FatFs/src/ff_gen_drv.c"
"Middlewares/Third_Party/FatFs/src/option/ccsbcs.c"
"Middlewares/Third_Party/FatFs/src/option/syscall.c"
"Middlewares/Third_Party/FreeRTOS/Source/croutine.c"
"Middlewares/Third_Party/FreeRTOS/Source/event_groups.c"
@ -93,7 +92,6 @@ set(TARGET_C_DEFINES
set(TARGET_C_DEFINES_RAM
"VECT_TAB_SRAM"
"USER_VECT_TAB_ADDRESS"
"DATA_IN_D2_SRAM"
)
# Copy them from Makefile

View File

@ -68,7 +68,7 @@
#define configTICK_RATE_HZ ((TickType_t)1000)
#define configMAX_PRIORITIES ( 56 )
#define configMINIMAL_STACK_SIZE ((uint16_t)128)
#define configTOTAL_HEAP_SIZE ((size_t)81920)
#define configTOTAL_HEAP_SIZE ((size_t)49152)
#define configMAX_TASK_NAME_LEN ( 16 )
#define configUSE_TRACE_FACILITY 1
#define configUSE_16_BIT_TICKS 0

View File

@ -6,8 +6,8 @@
extern UART_HandleTypeDef huart1;
/* Place heap inside DTCMRAM. */
#define HEAP_BASE 0x20000000
#define HEAP_SIZE 0x00020000
#define HEAP_BASE 0x30000000
#define HEAP_SIZE 0x00048000
size_t s_heap_sz = 0;

View File

@ -55,7 +55,7 @@ UART_HandleTypeDef huart1;
osThreadId_t mrbTaskHandle;
const osThreadAttr_t mrbTask_attributes = {
.name = "mrbTask",
.stack_size = 1024 * 4,
.stack_size = 4096 * 4,
.priority = (osPriority_t) osPriorityNormal,
};
/* USER CODE BEGIN PV */
@ -64,7 +64,6 @@ const osThreadAttr_t mrbTask_attributes = {
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MPU_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_SDMMC1_SD_Init(void);
@ -89,15 +88,6 @@ int main(void)
/* USER CODE END 1 */
/* MPU Configuration--------------------------------------------------------*/
MPU_Config();
/* Enable I-Cache---------------------------------------------------------*/
SCB_EnableICache();
/* Enable D-Cache---------------------------------------------------------*/
SCB_EnableDCache();
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
@ -121,10 +111,6 @@ int main(void)
MX_FATFS_Init();
/* USER CODE BEGIN 2 */
if(BSP_SD_Init() != MSD_OK) {
printf("SD initialization failed.\r\n");
}
/* USER CODE END 2 */
/* Init scheduler */
@ -348,7 +334,7 @@ static void report_card_info(void) {
break;
}
char *card_speed = "N";
char *card_speed = "normal";
switch(cinfo.CardSpeed) {
case CARD_HIGH_SPEED:
@ -361,9 +347,11 @@ static void report_card_info(void) {
break;
}
printf("This is an %s card, speed grade %s\r\n", card_type, card_speed);
printf("Has %ld %ldB blocks.\r\n", cinfo.BlockNbr, cinfo.BlockSize);
printf("This is an %s card, speed %s, size %ldMiB\r\n",
card_type,
card_speed,
cinfo.BlockNbr / 1048576 * cinfo.BlockSize
);
}
@ -372,11 +360,45 @@ static void report_heap(void) {
printf("\r\nHeap usage: %d bytes.\r\n", mi.uordblks);
}
void FFSTask(void *argument) {
//
for(;;) {
// Detect card CD interrupt and mount FatFS.
static char *load_boot_script(void) {
if(f_mount(&SDFatFS, SDPath, 1) != FR_OK) {
printf("FS mount failed.\r\n");
return NULL;
}
report_card_info();
char *ret = NULL;
FIL boot_file;
if(f_open(&boot_file, "0:/boot.rb", FA_READ) != FR_OK) {
printf("boot.rb not found.\r\n");
goto out_umount;
}
UINT boot_file_size = f_size(&boot_file);
ret = malloc(boot_file_size + 1);
if(ret == NULL) {
printf("Failed to allocate memory for script.\r\n");
goto out_close;
}
UINT read_size;
if(f_read(&boot_file, ret, boot_file_size, &read_size) != FR_OK) {
printf("Failed to read script.\r\n");
free(ret);
ret = NULL;
} else {
ret[read_size] = '\0';
}
out_close:
f_close(&boot_file);
out_umount:
f_mount(NULL, SDPath, 0);
return ret;
}
/* USER CODE END 4 */
@ -391,7 +413,37 @@ void FFSTask(void *argument) {
void RunMRBTask(void *argument)
{
/* USER CODE BEGIN 5 */
report_card_info();
report_heap();
mrb_state *mrb = mrb_open();
if(mrb == NULL) {
printf("Failed to open mruby core.\r\n");
}
mrbc_context *cxt = mrbc_context_new(mrb);
if(cxt == NULL) {
printf("Failed to create compiler.\r\n");
goto cleanup_mrb;
}
char *boot_script = load_boot_script();
if(boot_script != NULL) {
printf("Loaded boot.rb from SD card.\r\n");
struct mrb_parser_state *parser_state = mrb_parse_string(mrb, boot_script, cxt);
if(parser_state == NULL) {
printf("Failed to parse main script.\r\n");
} else {
free(boot_script);
mrb_load_exec(mrb, parser_state, cxt);
}
}
printf("Boot script exited, cleaning up.\r\n");
cleanup_mrb:
mrb_close(mrb);
report_heap();
/* Infinite loop */
for(;;)
@ -401,73 +453,6 @@ void RunMRBTask(void *argument)
/* USER CODE END 5 */
}
/* MPU Configuration */
void MPU_Config(void)
{
MPU_Region_InitTypeDef MPU_InitStruct = {0};
/* Disables the MPU */
HAL_MPU_Disable();
/** Initializes and configures the Region and the memory to be protected
*/
MPU_InitStruct.Enable = MPU_REGION_ENABLE;
MPU_InitStruct.Number = MPU_REGION_NUMBER0;
MPU_InitStruct.BaseAddress = 0x0;
MPU_InitStruct.Size = MPU_REGION_SIZE_4GB;
MPU_InitStruct.SubRegionDisable = 0x0;
MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL0;
MPU_InitStruct.AccessPermission = MPU_REGION_FULL_ACCESS;
MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_DISABLE;
MPU_InitStruct.IsShareable = MPU_ACCESS_SHAREABLE;
MPU_InitStruct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE;
MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;
HAL_MPU_ConfigRegion(&MPU_InitStruct);
/** Initializes and configures the Region and the memory to be protected
*/
MPU_InitStruct.Number = MPU_REGION_NUMBER1;
MPU_InitStruct.BaseAddress = 0x20000000;
MPU_InitStruct.Size = MPU_REGION_SIZE_128KB;
MPU_InitStruct.IsCacheable = MPU_ACCESS_CACHEABLE;
MPU_InitStruct.IsBufferable = MPU_ACCESS_BUFFERABLE;
HAL_MPU_ConfigRegion(&MPU_InitStruct);
/** Initializes and configures the Region and the memory to be protected
*/
MPU_InitStruct.Number = MPU_REGION_NUMBER2;
MPU_InitStruct.BaseAddress = 0x24000000;
MPU_InitStruct.Size = MPU_REGION_SIZE_512KB;
MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_ENABLE;
HAL_MPU_ConfigRegion(&MPU_InitStruct);
/** Initializes and configures the Region and the memory to be protected
*/
MPU_InitStruct.Number = MPU_REGION_NUMBER3;
MPU_InitStruct.BaseAddress = 0x30000000;
MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_DISABLE;
HAL_MPU_ConfigRegion(&MPU_InitStruct);
/** Initializes and configures the Region and the memory to be protected
*/
MPU_InitStruct.Number = MPU_REGION_NUMBER4;
MPU_InitStruct.BaseAddress = 0x90000000;
MPU_InitStruct.Size = MPU_REGION_SIZE_8MB;
MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_ENABLE;
HAL_MPU_ConfigRegion(&MPU_InitStruct);
/** Initializes and configures the Region and the memory to be protected
*/
MPU_InitStruct.Number = MPU_REGION_NUMBER5;
MPU_InitStruct.BaseAddress = 0x08000000;
MPU_InitStruct.Size = MPU_REGION_SIZE_128KB;
HAL_MPU_ConfigRegion(&MPU_InitStruct);
/* Enables the MPU */
HAL_MPU_Enable(MPU_HFNMI_PRIVDEF_NONE);
}
/**
* @brief Period elapsed callback in non blocking mode
* @note This function is called when TIM7 interrupt took place, inside

View File

@ -110,7 +110,7 @@
/ 950 - Traditional Chinese (DBCS)
*/
#define _USE_LFN 2 /* 0 to 3 */
#define _USE_LFN 0 /* 0 to 3 */
#define _MAX_LFN 255 /* Maximum LFN length to handle (12 to 255) */
/* The _USE_LFN switches the support of long file name (LFN).
/
@ -132,7 +132,7 @@
/ To use Unicode string for the path name, enable LFN and set _LFN_UNICODE = 1.
/ This option also affects behavior of string I/O functions. */
#define _STRF_ENCODE 3
#define _STRF_ENCODE 0
/* When _LFN_UNICODE == 1, this option selects the character encoding ON THE FILE to
/ be read/written via string I/O functions, f_gets(), f_putc(), f_puts and f_printf().
/
@ -210,7 +210,7 @@
/ Instead of private sector buffer eliminated from the file object, common sector
/ buffer in the file system object (FATFS) is used for the file data transfer. */
#define _FS_EXFAT 1
#define _FS_EXFAT 0
/* This option switches support of exFAT file system. (0:Disable or 1:Enable)
/ When enable exFAT, also LFN needs to be enabled. (_USE_LFN >= 1)
/ Note that enabling exFAT discards C89 compatibility. */

View File

@ -147,7 +147,7 @@ SECTIONS
. = ALIGN(4);
_edata = .; /* define a global symbol at data end */
} >RAM_D2 AT> RAM_D1
} >RAM_D1 AT> RAM_D1
/* Uninitialized data section */
@ -164,7 +164,7 @@ SECTIONS
. = ALIGN(4);
_ebss = .; /* define a global symbol at bss end */
__bss_end__ = _ebss;
} >RAM_D2
} >RAM_D1
/* User_heap_stack section, used to check that there is enough RAM left */
._user_heap_stack :
@ -175,7 +175,7 @@ SECTIONS
. = . + _Min_Heap_Size;
. = . + _Min_Stack_Size;
. = ALIGN(8);
} >RAM_D2
} >RAM_D1

View File

@ -10,8 +10,8 @@ CORTEX_M7.BaseAddress-Cortex_Memory_Protection_Unit_Region2_Settings=0x24000000
CORTEX_M7.BaseAddress-Cortex_Memory_Protection_Unit_Region3_Settings=0x30000000
CORTEX_M7.BaseAddress-Cortex_Memory_Protection_Unit_Region4_Settings=0x90000000
CORTEX_M7.BaseAddress-Cortex_Memory_Protection_Unit_Region5_Settings=0x08000000
CORTEX_M7.CPU_DCache=Enabled
CORTEX_M7.CPU_ICache=Enabled
CORTEX_M7.CPU_DCache=Disabled
CORTEX_M7.CPU_ICache=Disabled
CORTEX_M7.DisableExec-Cortex_Memory_Protection_Unit_Region0_Settings=MPU_INSTRUCTION_ACCESS_DISABLE
CORTEX_M7.DisableExec-Cortex_Memory_Protection_Unit_Region1_Settings=MPU_INSTRUCTION_ACCESS_DISABLE
CORTEX_M7.DisableExec-Cortex_Memory_Protection_Unit_Region3_Settings=MPU_INSTRUCTION_ACCESS_DISABLE
@ -40,7 +40,7 @@ CORTEX_M7.IsShareable-Cortex_Memory_Protection_Unit_Region2_Settings=MPU_ACCESS_
CORTEX_M7.IsShareable-Cortex_Memory_Protection_Unit_Region3_Settings=MPU_ACCESS_SHAREABLE
CORTEX_M7.IsShareable-Cortex_Memory_Protection_Unit_Region4_Settings=MPU_ACCESS_SHAREABLE
CORTEX_M7.IsShareable-Cortex_Memory_Protection_Unit_Region5_Settings=MPU_ACCESS_SHAREABLE
CORTEX_M7.MPU_Control=MPU_HFNMI_PRIVDEF_NONE
CORTEX_M7.MPU_Control=__NULL
CORTEX_M7.Size-Cortex_Memory_Protection_Unit_Region0_Settings=MPU_REGION_SIZE_4GB
CORTEX_M7.Size-Cortex_Memory_Protection_Unit_Region1_Settings=MPU_REGION_SIZE_128KB
CORTEX_M7.Size-Cortex_Memory_Protection_Unit_Region2_Settings=MPU_REGION_SIZE_512KB
@ -48,9 +48,11 @@ CORTEX_M7.Size-Cortex_Memory_Protection_Unit_Region3_Settings=MPU_REGION_SIZE_51
CORTEX_M7.Size-Cortex_Memory_Protection_Unit_Region4_Settings=MPU_REGION_SIZE_8MB
CORTEX_M7.Size-Cortex_Memory_Protection_Unit_Region5_Settings=MPU_REGION_SIZE_128KB
FATFS.BSP.number=1
FATFS.IPParameters=_USE_LFN,_FS_EXFAT,_USE_MUTEX
FATFS._FS_EXFAT=1
FATFS._USE_LFN=2
FATFS.IPParameters=_USE_LFN,_FS_EXFAT,_USE_MUTEX,_LFN_UNICODE,_STRF_ENCODE
FATFS._FS_EXFAT=0
FATFS._LFN_UNICODE=0
FATFS._STRF_ENCODE=0
FATFS._USE_LFN=0
FATFS._USE_MUTEX=1
FATFS0.BSP.STBoard=false
FATFS0.BSP.api=Unknown
@ -66,9 +68,9 @@ FATFS0.BSP.semaphore=
FATFS0.BSP.solution=PC13
FREERTOS.FootprintOK=true
FREERTOS.IPParameters=Tasks01,configENABLE_FPU,configTOTAL_HEAP_SIZE,configUSE_NEWLIB_REENTRANT,FootprintOK
FREERTOS.Tasks01=mrbTask,24,1024,RunMRBTask,Default,NULL,Dynamic,NULL,NULL
FREERTOS.Tasks01=mrbTask,24,4096,RunMRBTask,Default,NULL,Dynamic,NULL,NULL
FREERTOS.configENABLE_FPU=1
FREERTOS.configTOTAL_HEAP_SIZE=81920
FREERTOS.configTOTAL_HEAP_SIZE=49152
FREERTOS.configUSE_NEWLIB_REENTRANT=1
File.Version=6
GPIO.groupedBy=Group By Peripherals
@ -199,7 +201,7 @@ ProjectManager.StackSize=0x400
ProjectManager.TargetToolchain=Makefile
ProjectManager.ToolChainLocation=
ProjectManager.UnderRoot=false
ProjectManager.functionlistsort=1-MX_GPIO_Init-GPIO-false-HAL-true,2-SystemClock_Config-RCC-false-HAL-false,3-MX_USART1_UART_Init-USART1-false-HAL-true,4-MX_SDMMC1_SD_Init-SDMMC1-false-HAL-true,5-MX_FATFS_Init-FATFS-false-HAL-false,0-MX_CORTEX_M7_Init-CORTEX_M7-false-HAL-true
ProjectManager.functionlistsort=1-MX_GPIO_Init-GPIO-false-HAL-true,2-SystemClock_Config-RCC-false-HAL-false,3-MX_USART1_UART_Init-USART1-false-HAL-true,4-MX_SDMMC1_SD_Init-SDMMC1-false-HAL-true,5-MX_MDMA_Init-MDMA-false-HAL-true,5-MX_FATFS_Init-FATFS-false-HAL-false,0-MX_CORTEX_M7_Init-CORTEX_M7-false-HAL-true
RCC.ADCFreq_Value=16125000
RCC.AHB12Freq_Value=50000000
RCC.AHB4Freq_Value=50000000