Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDMMC SD Card Drivers #45

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open

Conversation

lekangwang
Copy link
Contributor

@lekangwang lekangwang commented Apr 12, 2024

Description

What was completed, changed, or updated?

  • Added FATFS third party libraries
  • Enabled and configure SDMMC to 4-bit data mode
  • Update CMakeList.txt to compile FATFS files
  • Added low level drivers that calls HAL functions to interact with SD card via SDMMC board peripheral
  • Created 2 thread-safe high level functions to read/write to SD card, (file names are limited to 8.3 format)

Why was this done (if applicable)?

  • For logging purposes (i.e. crash report)

Testing

What manual tests were used to validate the code?

Tested on 16 and 32 GB card

  • Can write the same message 100 times to a file
    Screenshot 2024-04-11 at 10 01 12 PM

  • Reading the first line, data matches as expected
    Screenshot 2024-04-11 at 10 00 06 PM

  • Multithreaded write to same file is handled correctly
    Screenshot 2024-05-30 at 11 53 18 PM


What unit tests were used to validate the code?


Documentation

Milestone number and name: M2

Link to Asana task: https://app.asana.com/0/1203458353737758/1204645027312218/f

Link to Confluence documentation: https://uwarg-docs.atlassian.net/wiki/spaces/ZP/pages/2251620424/SDMMC+SD+Card


Reminders

  • Add reviewers to the PR

  • Mention the PR in the appropriate discord channel

Copy link
Member

@antholuo antholuo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes mostly look good, especially if tested working.

I'm curious to see how it plays with freertos task scheduling and if that might cause problems with data stalling or weird interrupt issues. Might be worth trying to schedule two threads and having one thread write to the SD card while the other one does busy work?

@@ -82,7 +82,7 @@ void MX_I2C2_Init(void)

/* USER CODE END I2C2_Init 1 */
hi2c2.Instance = I2C2;
hi2c2.Init.Timing = 0x40505681;
hi2c2.Init.Timing = 0x20505885;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did we need to change this timing? Or was it just a i2c configuration changed and this was part of the auto-generated files?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was using PLL clock for the SDMMC peripheral, and I changed the PLL freq to be slow enough for the SD card. I changed this so I'm not using PLL anymore and instead Clock48 with a clock divider factor of 8 for SDMMC peripheral.

|GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does our speed need to be very high? This tends to cause ringing and can cause bad data.

Would advise to bring it back down to FREQ_MEDIUM or FREQ_LOW.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to FREQ_MEDIUM

HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

/* SDMMC1 interrupt Init */
HAL_NVIC_SetPriority(SDMMC1_IRQn, 5, 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't something that we need to consider yet, but we should probably start thinking about our task / interrupt priorities....

@@ -46,7 +44,7 @@ void MX_LPUART1_UART_Init(void)

/* USER CODE END LPUART1_Init 1 */
hlpuart1.Instance = LPUART1;
hlpuart1.Init.BaudRate = 209700;
hlpuart1.Init.BaudRate = 230400;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a similar issue as the i2c, the baudrate is changed wiredly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did this so I could print stuff on my terminal on my laptop, will change this back later

@lekangwang lekangwang force-pushed the feature/driver/sdmmc-sd-card branch from 390ec93 to 323f6f8 Compare May 31, 2024 03:46
@lekangwang lekangwang force-pushed the feature/driver/sdmmc-sd-card branch from 323f6f8 to 19ff31d Compare May 31, 2024 03:54
Copy link
Contributor

@HardyYu HardyYu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow! Having DMA and thread guard implemented is really cool. Very impressive! This pr is kinda large and couldn't finish reviewing it today. I will leave my approvement here since this code was tested to be working. Good work!

*/
void HAL_Delay(uint32_t delay)
{
osDelay(delay);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HAL_Delay is not used anywhere in this driver. Overriding an existed hal function might be a little confusing. I think people should be using osDelay directly.

Copy link
Contributor Author

@lekangwang lekangwang Jun 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its used in ZeroPilot-SW-3.5/Boardfiles/nucleol552zeq/Drivers/STM32L5xx_HAL_Driver/Src/stm32l5xx_hal_sd.c for function HAL_SD_InitCard which is called by HAL_SD_Init which is called by BSP_SD_Init which is called by SD_initialize

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants