chore: SDIO conversion to HAL (#3182)

This commit is contained in:
Malte Langermann
2023-08-13 18:56:39 +02:00
committed by GitHub
parent 3342d21195
commit b377a8b5d4
36 changed files with 830 additions and 3013 deletions
@@ -308,7 +308,6 @@ set(SRC
set(FIRMWARE_TARGET_SRC
${FIRMWARE_TARGET_SRC}
# keys_driver.cpp
diskio.cpp
# telemetry_driver.cpp
)
@@ -25,7 +25,6 @@ set(BOOTLOADER_SRC
../../../../../${FATFS_DIR}/ffunicode.c
../../../../../targets/${TARGET_DIR}/${LCD_DRIVER}
../../../../../targets/${TARGET_DIR}/backlight_driver.cpp
../../../../../targets/${TARGET_DIR}/diskio.cpp
../../../../../targets/${TARGET_DIR}/board.cpp
../../../../../targets/${TARGET_DIR}/bootloader/boot_menu.cpp
${CMAKE_CURRENT_BINARY_DIR}/hal_keys.inc
@@ -42,6 +41,7 @@ set(BOOTLOADER_SRC
../stm32_serial_driver.cpp
../stm32_usart_driver.cpp
../stm32_gpio_driver.cpp
../stm32_dma.cpp
init.c
boot.cpp
bin_files.cpp
@@ -77,7 +77,8 @@ if(PCB STREQUAL X10 OR PCB STREQUAL X12S OR PCB STREQUAL NV14)
../../../../../fonts/lvgl/lv_font_roboto_bl_16.c
../../../../../thirdparty/libopenui/src/bitmapbuffer.cpp
../../../../../thirdparty/libopenui/thirdparty/lz4/lz4.c
../../../../../targets/common/arm/stm32/sdio_sd.c
../../../../../targets/common/arm/stm32/sdio_sd.cpp
../../../../../targets/common/arm/stm32/diskio_sdio.cpp
../../../../../targets/common/arm/stm32/rtc_driver.cpp
../../../../../targets/${TARGET_DIR}/sdram_driver.c
../../../../../targets/${TARGET_DIR}/haptic_driver.cpp
@@ -119,6 +120,7 @@ else()
../../../../../gui/common/stdlcd/fonts.cpp
../../../../../gui/common/stdlcd/utf8.cpp
../../../../../targets/${TARGET_DIR}/eeprom_driver.cpp
../../../../../targets/${TARGET_DIR}/diskio.cpp
)
remove_definitions(-DDEBUG)
endif()
@@ -38,6 +38,14 @@ BinFileInfo binFiles[MAX_BIN_FILES];
uint8_t Block_buffer[BLOCK_LEN];
UINT BlockCount;
void sdInit(void)
{
static FATFS fatFS __DMA;
if (f_mount(&fatFS, "", 1) == FR_OK) {
f_chdir("/");
}
}
FRESULT openBinDir(MemoryType mt)
{
@@ -94,9 +94,12 @@ uint32_t eepromWritten = 0;
FlashCheckRes valid;
MemoryType memoryType;
uint32_t unlocked = 0;
uint32_t timer10MsCount;
void interrupt10ms()
{
timer10MsCount++;
tenms |= 1u; // 10 mS has passed
g_tmr10ms++;
@@ -116,6 +119,7 @@ void interrupt10ms()
void init10msTimer()
{
timer10MsCount = 0;
INTERRUPT_xMS_TIMER->ARR = 9999; // 10mS in uS
INTERRUPT_xMS_TIMER->PSC = (PERI1_FREQUENCY * TIMER_MULT_APB1) / 1000000 - 1; // 1uS
INTERRUPT_xMS_TIMER->CCER = 0;
@@ -126,6 +130,11 @@ void init10msTimer()
NVIC_EnableIRQ(INTERRUPT_xMS_IRQn);
}
extern "C" uint32_t HAL_GetTick(void)
{
return timer10MsCount*10;
}
#if !defined(SIMU)
extern "C" void INTERRUPT_xMS_IRQHandler()
{
@@ -188,8 +197,6 @@ int menuFlashFile(uint32_t index, event_t event)
return -1;
}
static uint32_t PowerUpDelay;
void flashWriteBlock()
{
uint32_t blockOffset = 0;
@@ -571,12 +578,16 @@ int bootloaderMain()
lcdRefresh();
#if defined(SDCARD) && defined(PCBTARANIS)
static uint32_t PowerUpDelay = 0;
if (PowerUpDelay < 20) { // 200 mS
PowerUpDelay += 1;
}
else {
sdPoll10ms();
}
#endif
}
if (state != ST_FLASHING && state != ST_USB) {
@@ -0,0 +1,340 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
/* Low level disk I/O module skeleton for FatFs (C)ChaN, 2007 */
/*-----------------------------------------------------------------------*/
/* This is a stub disk I/O module that acts as front end of the existing */
/* disk I/O modules and attach it to FatFs module with common interface. */
/*-----------------------------------------------------------------------*/
#include "stm32_hal.h"
#include "sdio_sd.h"
#include "FatFs/diskio.h"
#include <string.h>
#include "debug.h"
#if FF_MAX_SS != FF_MIN_SS
#error "Variable sector size is not supported"
#endif
#define BLOCK_SIZE FF_MAX_SS
#define SD_TIMEOUT 300 /* 300ms */
// Disk status
extern volatile uint32_t WriteStatus;
extern volatile uint32_t ReadStatus;
/*-----------------------------------------------------------------------*/
/* Inidialize a Drive */
DSTATUS disk_initialize (
BYTE drv /* Physical drive nmuber (0..) */
)
{
DSTATUS stat = 0;
/* Supports only single drive */
if (drv)
{
stat |= STA_NOINIT;
}
/*-------------------------- SD Init ----------------------------- */
SD_Error res = SD_Init();
if (res != SD_OK)
{
TRACE("SD_Init() failed: %d", res);
stat |= STA_NOINIT;
}
TRACE("SD card info:");
TRACE("type: %u", (uint32_t)(SD_GetCardType()));
TRACE("class: %u", (uint32_t)(SD_GetCardClass()));
TRACE("sectors: %u", (uint32_t)(SD_GetSectorCount()));
TRACE("sector size: %u", (uint32_t)(SD_GetSectorSize()));
TRACE("block size: %u", (uint32_t)(SD_GetBlockSize()));
return(stat);
}
DWORD scratch[BLOCK_SIZE / 4] __DMA;
/*-----------------------------------------------------------------------*/
/* Return Disk Status */
DSTATUS disk_status (
BYTE drv /* Physical drive nmuber (0..) */
)
{
DSTATUS stat = 0;
if (SD_Detect() != SD_PRESENT)
stat |= STA_NODISK;
// STA_NOTINIT - Subsystem not initailized
// STA_PROTECTED - Write protected, MMC/SD switch if available
return(stat);
}
uint32_t sdReadRetries = 0;
/*-----------------------------------------------------------------------*/
/* Read Sector(s) */
// Please note:
// this functions assumes that buff is properly aligned
// and in the right RAM segment for DMA
//
DRESULT disk_read_dma(BYTE drv, BYTE * buff, DWORD sector, UINT count)
{
DRESULT res = RES_ERROR;
for (int retry = 0; retry < 3; retry++) {
ReadStatus = 0;
SD_Error Status = SD_ReadBlocks(buff, sector, BLOCK_SIZE, count);
if (Status != SD_OK) {
TRACE("SD ReadBlocks=%d, s:%u c: %u", Status, sector, (uint32_t)count);
++sdReadRetries;
continue;
}
// Wait that the reading process is completed or a timeout occurs
uint32_t timeout = HAL_GetTick();
while((ReadStatus == 0) && ((HAL_GetTick() - timeout) < SD_TIMEOUT));
if (ReadStatus == 0) {
TRACE("SD read timeout, s:%u c:%u", sector, (uint32_t)count);
++sdReadRetries;
continue;
}
ReadStatus = 0;
timeout = HAL_GetTick();
while((HAL_GetTick() - timeout) < SD_TIMEOUT) {
if (SD_GetStatus() == SD_TRANSFER_OK) {
res = RES_OK;
break;
}
}
if (res == RES_OK) {
// exit retry loop
break;
}
TRACE("SD getstatus timeout, s:%u c:%u", sector, (uint32_t)count);
}
return res;
}
DRESULT __disk_read(BYTE drv, BYTE * buff, DWORD sector, UINT count)
{
// If unaligned, do the single block reads with a scratch buffer.
// If aligned and single sector, do a single block read.
// If aligned and multiple sectors, try multi block read.
// If multi block read fails, try single block reads without
// an intermediate buffer (move trough the provided buffer)
// TRACE("disk_read %d %p %10d %d", drv, buff, sector, count);
if (SD_Detect() != SD_PRESENT) {
TRACE("SD_Detect() != SD_PRESENT");
return RES_NOTRDY;
}
if (SD_CheckStatusWithTimeout(SD_TIMEOUT) < 0) {
return RES_ERROR;
}
DRESULT res = RES_OK;
if (count == 0) return res;
if ((DWORD)buff < 0x20000000 || ((DWORD)buff & 3)) {
// buffer is not aligned, use scratch buffer that is aligned
TRACE("disk_read bad alignment (%p)", buff);
while (count--) {
res = disk_read_dma(drv, (BYTE *)scratch, sector++, 1);
if (res != RES_OK) break;
memcpy(buff, scratch, BLOCK_SIZE);
buff += BLOCK_SIZE;
}
return res;
}
res = disk_read_dma(drv, buff, sector, count);
if (res != RES_OK && count > 1) {
// multi-read failed, try reading same sectors, one by one
TRACE("disk_read() multi-block failed, trying single block reads...");
while (count--) {
res = disk_read_dma(drv, buff, sector++, 1);
if (res != RES_OK) break;
buff += BLOCK_SIZE;
}
}
return res;
}
/*-----------------------------------------------------------------------*/
/* Write Sector(s) */
#if _READONLY == 0
DRESULT __disk_write(
BYTE drv, /* Physical drive nmuber (0..) */
const BYTE *buff, /* Data to be written */
DWORD sector, /* Sector address (LBA) */
UINT count /* Number of sectors to write (1..255) */
)
{
DRESULT res = RES_OK;
// TRACE("disk_write %d %p %10d %d", drv, buff, sector, count);
if (SD_Detect() != SD_PRESENT)
return RES_NOTRDY;
if (SD_CheckStatusWithTimeout(SD_TIMEOUT) < 0) {
return RES_ERROR;
}
if ((DWORD)buff < 0x20000000 || ((DWORD)buff & 3)) {
//TRACE("disk_write bad alignment (%p)", buff);
while(count--) {
memcpy(scratch, buff, BLOCK_SIZE);
res = __disk_write(drv, (BYTE *)scratch, sector++, 1);
if (res != RES_OK)
break;
buff += BLOCK_SIZE;
}
return(res);
}
SD_Error Status = SD_WriteBlocks((uint8_t *)buff, sector, BLOCK_SIZE, count);
if (Status != SD_OK) {
TRACE("SD WriteBlocks=%d, s:%u c: %u", Status, sector, (uint32_t)count);
return RES_ERROR;
}
// Wait that the reading process is completed or a timeout occurs
uint32_t timeout = HAL_GetTick();
while((WriteStatus == 0) && ((HAL_GetTick() - timeout) < SD_TIMEOUT));
if (WriteStatus == 0) {
TRACE("SD write timeout, s:%u c:%u", sector, (uint32_t)count);
return RES_ERROR;
}
WriteStatus = 0;
res = RES_ERROR;
timeout = HAL_GetTick();
while((HAL_GetTick() - timeout) < SD_TIMEOUT) {
if (SD_GetStatus() == SD_TRANSFER_OK) {
res = RES_OK;
break;
}
}
if (res != RES_OK) {
TRACE("SD getstatus timeout, s:%u c: %u", sector, (uint32_t)count);
res = RES_ERROR;
}
return res;
}
#endif /* _READONLY */
/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions */
DRESULT disk_ioctl (
BYTE drv, /* Physical drive nmuber (0..) */
BYTE ctrl, /* Control code */
void *buff /* Buffer to send/receive control data */
)
{
DRESULT res;
uint32_t tmp;
if (drv) return RES_PARERR;
res = RES_ERROR;
switch (ctrl) {
case GET_SECTOR_COUNT : /* Get number of sectors on the disk (DWORD) */
tmp = SD_GetSectorCount();
if(tmp == 0) {
res = RES_ERROR;
break;
}
*(DWORD*)buff = tmp;
res = RES_OK;
break;
case GET_SECTOR_SIZE : /* Get R/W sector size (WORD) */
tmp = SD_GetSectorSize();
if(tmp == 0) {
res = RES_ERROR;
break;
}
*(WORD*)buff = tmp;
res = RES_OK;
break;
case GET_BLOCK_SIZE : /* Get erase block size in unit of sector (DWORD) */
tmp = SD_GetBlockSize() / BLOCK_SIZE;
if(tmp == 0) {
res = RES_ERROR;
break;
}
*(DWORD*)buff = tmp;
res = RES_OK;
break;
case CTRL_SYNC:
/* Complete pending write process (needed at _FS_READONLY == 0) */
while (SD_GetStatus() == SD_TRANSFER_BUSY);
res = RES_OK;
break;
default:
res = RES_OK;
break;
}
return res;
}
@@ -15,6 +15,7 @@ if(NOT NATIVE_BUILD)
${STM32CUBE_DIR}/Src/stm32f4xx_ll_usart.c
${STM32CUBE_DIR}/Src/stm32f4xx_ll_rcc.c
${STM32CUBE_DIR}/Src/stm32f4xx_ll_exti.c
${STM32CUBE_DIR}/Src/stm32f4xx_ll_sdmmc.c
${STM32CUBE_DIR}/Src/stm32f4xx_ll_adc.c
${STM32CUBE_DIR}/Src/stm32f4xx_ll_spi.c
# HAL drivers
@@ -25,6 +26,8 @@ if(NOT NATIVE_BUILD)
${STM32CUBE_DIR}/Src/stm32f4xx_hal_rtc.c
${STM32CUBE_DIR}/Src/stm32f4xx_hal_i2c.c
${STM32CUBE_DIR}/Src/stm32f4xx_hal_i2c_ex.c
${STM32CUBE_DIR}/Src/stm32f4xx_hal_sd.c
${STM32CUBE_DIR}/Src/stm32f4xx_hal_dma.c
)
target_compile_options(stm32cube_ll PRIVATE -DUSE_FULL_LL_DRIVER)
@@ -64,7 +67,6 @@ if(CPU_TYPE_FULL STREQUAL STM32F429xI)
${STM32_STDPERIPH_DIR}/src/stm32f4xx_ltdc.c
${STM32_STDPERIPH_DIR}/src/stm32f4xx_dma.c
${STM32_STDPERIPH_DIR}/src/stm32f4xx_dma2d.c
${STM32_STDPERIPH_DIR}/src/stm32f4xx_sdio.c
)
elseif (CPU_TYPE_FULL STREQUAL STM32F413xG)
set(STM32_STDPERIPH_SRC ${STM32_STDPERIPH_SRC}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,420 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "sdio_sd.h"
#include "stm32_dma.h"
#include "stm32_gpio_driver.h"
#include "stm32_hal_ll.h"
#include "stm32_hal.h"
#include "hal.h"
#include "delays_driver.h"
#include "debug.h"
/* Configure PC.08, PC.09, PC.10, PC.11 pins: D0, D1, D2, D3 pins */
#if !defined(SD_SDIO_DATA_GPIO) && !defined(SD_SDIO_DATA_GPIO_PINS)
#define SD_SDIO_DATA_GPIO GPIOC
#define SD_SDIO_DATA_GPIO_PINS \
(LL_GPIO_PIN_8 | LL_GPIO_PIN_9 | LL_GPIO_PIN_10 | LL_GPIO_PIN_11)
#endif
/* Configure PD.02 CMD line */
#if !defined(SD_SDIO_CMD_GPIO) && !defined(SD_SDIO_CMD_GPIO_PIN)
#define SD_SDIO_CMD_GPIO GPIOD
#define SD_SDIO_CMD_GPIO_PIN LL_GPIO_PIN_2
#endif
#if !defined(SD_SDIO_CLK_GPIO) && !defined(SD_SDIO_CLK_GPIO_PIN)
#define SD_SDIO_CLK_GPIO GPIOC
#define SD_SDIO_CLK_GPIO_PIN LL_GPIO_PIN_12
#endif
static SD_HandleTypeDef sdio;
static DMA_HandleTypeDef sdioTxDma;
// Disk status
volatile uint32_t WriteStatus = 0;
volatile uint32_t ReadStatus = 0;
static void SD_LowLevel_Init(void)
{
/* Enable the SDIO APB2 Clock */
__HAL_RCC_SDIO_CLK_ENABLE();
LL_GPIO_InitTypeDef GPIO_InitStructure;
LL_GPIO_StructInit(&GPIO_InitStructure);
stm32_gpio_enable_clock(SD_SDIO_DATA_GPIO);
stm32_gpio_enable_clock(SD_SDIO_CMD_GPIO);
stm32_gpio_enable_clock(SD_SDIO_CLK_GPIO);
GPIO_InitStructure.Pin = SD_SDIO_DATA_GPIO_PINS;
GPIO_InitStructure.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStructure.Mode = LL_GPIO_MODE_ALTERNATE;
GPIO_InitStructure.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStructure.Pull = LL_GPIO_PULL_UP;
GPIO_InitStructure.Alternate = LL_GPIO_AF_12; // SDIO
LL_GPIO_Init(SD_SDIO_DATA_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.Pin = SD_SDIO_CMD_GPIO_PIN;
LL_GPIO_Init(SD_SDIO_CMD_GPIO, &GPIO_InitStructure);
/* Configure PC.12 pin: CLK pin */
GPIO_InitStructure.Pin = SD_SDIO_CLK_GPIO_PIN;
GPIO_InitStructure.Pull = LL_GPIO_PULL_NO;
LL_GPIO_Init(SD_SDIO_CLK_GPIO, &GPIO_InitStructure);
// SDIO Interrupt ENABLE
NVIC_SetPriority(SDIO_IRQn, 0);
NVIC_EnableIRQ(SDIO_IRQn);
// Init SDIO DMA instance
sdioTxDma.Instance = SD_SDIO_DMA_STREAM;
sdioTxDma.Init.Channel = SD_SDIO_DMA_CHANNEL;
sdioTxDma.Init.PeriphInc = DMA_PINC_DISABLE;
sdioTxDma.Init.MemInc = DMA_MINC_ENABLE;
sdioTxDma.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
sdioTxDma.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
sdioTxDma.Init.Mode = DMA_PFCTRL;
sdioTxDma.Init.Priority = DMA_PRIORITY_VERY_HIGH;
sdioTxDma.Init.FIFOMode = DMA_FIFOMODE_ENABLE;
sdioTxDma.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
sdioTxDma.Init.MemBurst = DMA_MBURST_INC4;
sdioTxDma.Init.PeriphBurst = DMA_PBURST_INC4;
stm32_dma_enable_clock(SD_SDIO_DMA);
HAL_DMA_Init(&sdioTxDma);
__HAL_LINKDMA(&sdio, hdmatx, sdioTxDma);
__HAL_LINKDMA(&sdio, hdmarx, sdioTxDma);
// DMA2 STREAMx Interrupt ENABLE
NVIC_SetPriority(SD_SDIO_DMA_IRQn, 0);
NVIC_EnableIRQ(SD_SDIO_DMA_IRQn);
}
SD_Error SD_Init(void)
{
static bool _sdio_init = false;
if(_sdio_init) return SD_OK;
_sdio_init = true;
__IO SD_Error errorstatus = SD_OK;
/* SDIO Peripheral Low Level Init */
SD_LowLevel_Init();
/*!< Configure the SDIO peripheral */
/*!< SDIO_CK = SDIOCLK / (SDIO_TRANSFER_CLK_DIV + 2) */
/*!< on STM32F4xx devices, SDIOCLK is fixed to 48MHz */
sdio.Instance = SDIO;
sdio.Init.ClockEdge = SDIO_CLOCK_EDGE_RISING;
sdio.Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE;
sdio.Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE;
sdio.Init.BusWide = SDIO_BUS_WIDE_1B;
sdio.Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
sdio.Init.ClockDiv = SD_SDIO_TRANSFER_CLK_DIV;
HAL_SD_DeInit(&sdio);
HAL_StatusTypeDef halStatus = HAL_SD_Init(&sdio);
if (halStatus != HAL_OK) {
TRACE("SD_PowerON() status=%d", halStatus);
/*!< CMD Response TimeOut (wait for CMDSENT flag) */
return SD_ERROR;
}
HAL_SD_CardInfoTypeDef cardInfo;
HAL_StatusTypeDef es = HAL_SD_GetCardInfo(&sdio, &cardInfo);
if(es != HAL_OK)
return SD_ERROR;
HAL_SD_ConfigWideBusOperation(&sdio, SDIO_BUS_WIDE_4B);
return errorstatus;
}
/**
* @brief Gets the cuurent sd card data transfer status.
* @param None
* @retval SDTransferState: Data Transfer state.
* This value can be:
* - SD_TRANSFER_OK: No data transfer is acting
* - SD_TRANSFER_BUSY: Data transfer is acting
*/
SDTransferState SD_GetStatus(void)
{
HAL_SD_CardStateTypeDef cardstate = HAL_SD_GetCardState(&sdio);
if (cardstate == HAL_SD_CARD_TRANSFER) {
return SD_TRANSFER_OK;
}
else if (cardstate == HAL_SD_CARD_ERROR) {
return SD_TRANSFER_ERROR;
}
return SD_TRANSFER_BUSY;
}
int SD_CheckStatusWithTimeout(uint32_t timeout)
{
uint32_t timer = HAL_GetTick();
/* block until SDIO IP is ready again or a timeout occur */
while(HAL_GetTick() - timer < timeout) {
auto state = SD_GetStatus();
if (state != SD_TRANSFER_BUSY) {
return state == SD_TRANSFER_OK ? 0 : -1;
}
}
return -1;
}
/**
* @brief Detect if SD card is correctly plugged in the memory slot.
* @param None
* @retval Return if SD is detected or not
*/
uint8_t SD_Detect(void)
{
__IO uint8_t status = SD_PRESENT;
/*!< Check GPIO to detect SD */
if ((LL_GPIO_ReadInputPort(SD_PRESENT_GPIO) & SD_PRESENT_LL_GPIO_PIN) != 0) {
status = SD_NOT_PRESENT;
}
return status;
}
/**
* @brief Returns information about specific card.
* @param cardinfo: pointer to a SD_CardInfo structure that contains all SD card
* information.
* @retval SD_Error: SD Card Error code.
*/
SD_Error SD_GetCardInfo(HAL_SD_CardInfoTypeDef *cardinfo)
{
if(HAL_SD_GetCardInfo(&sdio, cardinfo) != HAL_OK)
return SD_ERROR;
return SD_OK;
}
/**
* @brief Allows to read blocks from a specified address in a card. The Data
* transfer can be managed by DMA mode or Polling mode.
* @note This operation should be followed by two functions to check if the
* DMA Controller and SD Card status.
* - SD_ReadWaitOperation(): this function insure that the DMA
* controller has finished all data transfer.
* - SD_GetStatus(): to check that the SD Card has finished the
* data transfer and it is ready for data.
* @param readbuff: pointer to the buffer that will contain the received data.
* @param ReadAddr: Address from where data are to be read.
* @param BlockSize: the SD card Data block size. The Block size should be 512.
* @param NumberOfBlocks: number of blocks to be read.
* @retval SD_Error: SD Card Error code.
*/
SD_Error SD_ReadBlocks(uint8_t *readbuff, uint32_t ReadAddr, uint16_t BlockSize, uint32_t NumberOfBlocks)
{
HAL_StatusTypeDef res = HAL_SD_ReadBlocks_DMA(&sdio, readbuff, ReadAddr, NumberOfBlocks);
if(res == HAL_OK)
return SD_OK;
return SD_ERROR;
}
// /**
// * @brief This function waits until the SDIO DMA data transfer is finished.
// * This function should be called after SDIO_ReadMultiBlocks() function
// * to insure that all data sent by the card are already transferred by
// * the DMA controller.
// * @param None.
// * @retval SD_Error: SD Card Error code.
// */
// SD_Error SD_WaitReadOperation(uint32_t timeout)
// {
// volatile HAL_SD_CardStateTypeDef state = HAL_SD_GetCardState(&sdio);
// if(state == HAL_SD_CARD_READY || state == HAL_SD_CARD_TRANSFER)
// return SD_OK;
// timeout = 100;
// while((HAL_SD_GetCardState(&sdio) == HAL_SD_CARD_SENDING) && (timeout > 0)) {
// delay_ms(1);
// timeout--;
// }
// state = HAL_SD_GetCardState(&sdio);
// if(timeout > 0 && state == HAL_SD_CARD_TRANSFER)
// return SD_OK;
// return SD_ERROR;
// }
/**
* @brief Allows to write blocks starting from a specified address in a card.
* The Data transfer can be managed by DMA mode only.
* @note This operation should be followed by two functions to check if the
* DMA Controller and SD Card status.
* - SD_ReadWaitOperation(): this function insure that the DMA
* controller has finished all data transfer.
* - SD_GetStatus(): to check that the SD Card has finished the
* data transfer and it is ready for data.
* @param WriteAddr: Address from where data are to be read.
* @param writebuff: pointer to the buffer that contain the data to be transferred.
* @param BlockSize: the SD card Data block size. The Block size should be 512.
* @param NumberOfBlocks: number of blocks to be written.
* @retval SD_Error: SD Card Error code.
*/
SD_Error SD_WriteBlocks(uint8_t *writebuff, uint32_t WriteAddr, uint16_t BlockSize, uint32_t NumberOfBlocks)
{
HAL_StatusTypeDef res = HAL_SD_WriteBlocks_DMA(&sdio, writebuff, WriteAddr, NumberOfBlocks);
if(res == HAL_OK)
return SD_OK;
return SD_ERROR;
}
// /**
// * @brief This function waits until the SDIO DMA data transfer is finished.
// * This function should be called after SDIO_WriteBlock() and
// * SDIO_WriteMultiBlocks() function to insure that all data sent by the
// * card are already transferred by the DMA controller.
// * @param None.
// * @retval SD_Error: SD Card Error code.
// */
// OPTIMIZE("O0") SD_Error SD_WaitWriteOperation(uint32_t timeout)
// {
// HAL_SD_CardStateTypeDef state = HAL_SD_GetCardState(&sdio);
// if(state == HAL_SD_CARD_READY || state == HAL_SD_CARD_TRANSFER)
// return SD_OK;
// timeout = 1000;
// state = HAL_SD_GetCardState(&sdio);
// while((state == HAL_SD_CARD_RECEIVING || state == HAL_SD_CARD_PROGRAMMING) && (timeout > 0)) {
// delay_ms(1);
// timeout--;
// state = HAL_SD_GetCardState(&sdio);
// }
// state = HAL_SD_GetCardState(&sdio);
// if(timeout > 0 && state == HAL_SD_CARD_TRANSFER)
// return SD_OK;
// return SD_ERROR;
// }
uint32_t SD_GetSectorCount()
{
HAL_SD_CardInfoTypeDef cardInfo;
if(SD_GetCardInfo(&cardInfo) != SD_OK)
return 0;
return cardInfo.LogBlockNbr;
}
uint32_t SD_GetSectorSize()
{
HAL_SD_CardInfoTypeDef cardInfo;
if(SD_GetCardInfo(&cardInfo) != SD_OK)
return 0;
return cardInfo.LogBlockSize;
}
uint32_t SD_GetBlockSize()
{
HAL_SD_CardInfoTypeDef cardInfo;
if(SD_GetCardInfo(&cardInfo) != SD_OK)
return 0;
return cardInfo.LogBlockSize;
}
uint32_t SD_GetCardType()
{
HAL_SD_CardInfoTypeDef cardInfo;
if(SD_GetCardInfo(&cardInfo) != SD_OK)
return 0;
return cardInfo.CardType;
}
uint32_t SD_GetCardVersion()
{
HAL_SD_CardInfoTypeDef cardInfo;
if(SD_GetCardInfo(&cardInfo) != SD_OK)
return 0;
return cardInfo.CardVersion;
}
uint32_t SD_GetCardClass()
{
HAL_SD_CardInfoTypeDef cardInfo;
if(SD_GetCardInfo(&cardInfo) != SD_OK)
return 0;
return cardInfo.Class;
}
/**
* @brief Tx Transfer completed callbacks
* @param hsd: SD handle
* @retval None
*/
extern "C" void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsd)
{
UNUSED(hsd);
WriteStatus = 1;
}
/**
* @brief Rx Transfer completed callbacks
* @param hsd: SD handle
* @retval None
*/
extern "C" void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd)
{
UNUSED(hsd);
ReadStatus = 1;
}
extern "C" void SDIO_IRQHandler(void)
{
DEBUG_INTERRUPT(INT_SDIO);
HAL_SD_IRQHandler(&sdio);
}
extern "C" void SD_SDIO_DMA_IRQHANDLER(void)
{
DEBUG_INTERRUPT(INT_SDIO_DMA);
HAL_DMA_IRQHandler(&sdioTxDma);
}
+10 -138
View File
@@ -23,62 +23,11 @@
#ifndef _SDIO_SD_H_
#define _SDIO_SD_H_
#include "board.h"
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
/* Includes ------------------------------------------------------------------*/
typedef enum
{
/**
* @brief SDIO specific error defines
*/
SD_CMD_CRC_FAIL = (1), /*!< Command response received (but CRC check failed) */
SD_DATA_CRC_FAIL = (2), /*!< Data bock sent/received (CRC check Failed) */
SD_CMD_RSP_TIMEOUT = (3), /*!< Command response timeout */
SD_DATA_TIMEOUT = (4), /*!< Data time out */
SD_TX_UNDERRUN = (5), /*!< Transmit FIFO under-run */
SD_RX_OVERRUN = (6), /*!< Receive FIFO over-run */
SD_START_BIT_ERR = (7), /*!< Start bit not detected on all data signals in widE bus mode */
SD_CMD_OUT_OF_RANGE = (8), /*!< CMD's argument was out of range.*/
SD_ADDR_MISALIGNED = (9), /*!< Misaligned address */
SD_BLOCK_LEN_ERR = (10), /*!< Transferred block length is not allowed for the card or the number of transferred bytes does not match the block length */
SD_ERASE_SEQ_ERR = (11), /*!< An error in the sequence of erase command occurs.*/
SD_BAD_ERASE_PARAM = (12), /*!< An Invalid selection for erase groups */
SD_WRITE_PROT_VIOLATION = (13), /*!< Attempt to program a write protect block */
SD_LOCK_UNLOCK_FAILED = (14), /*!< Sequence or password error has been detected in unlock command or if there was an attempt to access a locked card */
SD_COM_CRC_FAILED = (15), /*!< CRC check of the previous command failed */
SD_ILLEGAL_CMD = (16), /*!< Command is not legal for the card state */
SD_CARD_ECC_FAILED = (17), /*!< Card internal ECC was applied but failed to correct the data */
SD_CC_ERROR = (18), /*!< Internal card controller error */
SD_GENERAL_UNKNOWN_ERROR = (19), /*!< General or Unknown error */
SD_STREAM_READ_UNDERRUN = (20), /*!< The card could not sustain data transfer in stream read operation. */
SD_STREAM_WRITE_OVERRUN = (21), /*!< The card could not sustain data programming in stream mode */
SD_CID_CSD_OVERWRITE = (22), /*!< CID/CSD overwrite error */
SD_WP_ERASE_SKIP = (23), /*!< only partial address space was erased */
SD_CARD_ECC_DISABLED = (24), /*!< Command has been executed without using internal ECC */
SD_ERASE_RESET = (25), /*!< Erase sequence was cleared before executing because an out of erase sequence command was received */
SD_AKE_SEQ_ERROR = (26), /*!< Error in sequence of authentication. */
SD_INVALID_VOLTRANGE = (27),
SD_ADDR_OUT_OF_RANGE = (28),
SD_SWITCH_ERROR = (29),
SD_SDIO_DISABLED = (30),
SD_SDIO_FUNCTION_BUSY = (31),
SD_SDIO_FUNCTION_FAILED = (32),
SD_SDIO_UNKNOWN_FUNCTION = (33),
/**
* @brief Standard error defines
*/
SD_INTERNAL_ERROR,
SD_NOT_CONFIGURED,
SD_REQUEST_PENDING,
SD_REQUEST_NOT_APPLICABLE,
SD_INVALID_PARAMETER,
SD_UNSUPPORTED_FEATURE,
SD_UNSUPPORTED_HW,
SD_ERROR,
SD_OK = 0
} SD_Error;
@@ -93,106 +42,29 @@ typedef enum
SD_TRANSFER_ERROR
} SDTransferState;
/**
* @brief Card Specific Data: CSD Register
*/
typedef struct
{
__IO uint8_t CSDStruct; /*!< CSD structure */
__IO uint8_t SysSpecVersion; /*!< System specification version */
__IO uint8_t Reserved1; /*!< Reserved */
__IO uint8_t TAAC; /*!< Data read access-time 1 */
__IO uint8_t NSAC; /*!< Data read access-time 2 in CLK cycles */
__IO uint8_t MaxBusClkFrec; /*!< Max. bus clock frequency */
__IO uint16_t CardComdClasses; /*!< Card command classes */
__IO uint8_t RdBlockLen; /*!< Max. read data block length */
__IO uint8_t PartBlockRead; /*!< Partial blocks for read allowed */
__IO uint8_t WrBlockMisalign; /*!< Write block misalignment */
__IO uint8_t RdBlockMisalign; /*!< Read block misalignment */
__IO uint8_t DSRImpl; /*!< DSR implemented */
__IO uint8_t Reserved2; /*!< Reserved */
__IO uint32_t DeviceSize; /*!< Device Size */
__IO uint8_t MaxRdCurrentVDDMin; /*!< Max. read current @ VDD min */
__IO uint8_t MaxRdCurrentVDDMax; /*!< Max. read current @ VDD max */
__IO uint8_t MaxWrCurrentVDDMin; /*!< Max. write current @ VDD min */
__IO uint8_t MaxWrCurrentVDDMax; /*!< Max. write current @ VDD max */
__IO uint8_t DeviceSizeMul; /*!< Device size multiplier */
__IO uint8_t EraseGrSize; /*!< Erase group size */
__IO uint8_t EraseGrMul; /*!< Erase group size multiplier */
__IO uint8_t WrProtectGrSize; /*!< Write protect group size */
__IO uint8_t WrProtectGrEnable; /*!< Write protect group enable */
__IO uint8_t ManDeflECC; /*!< Manufacturer default ECC */
__IO uint8_t WrSpeedFact; /*!< Write speed factor */
__IO uint8_t MaxWrBlockLen; /*!< Max. write data block length */
__IO uint8_t WriteBlockPaPartial; /*!< Partial blocks for write allowed */
__IO uint8_t Reserved3; /*!< Reserded */
__IO uint8_t ContentProtectAppli; /*!< Content protection application */
__IO uint8_t FileFormatGrouop; /*!< File format group */
__IO uint8_t CopyFlag; /*!< Copy flag (OTP) */
__IO uint8_t PermWrProtect; /*!< Permanent write protection */
__IO uint8_t TempWrProtect; /*!< Temporary write protection */
__IO uint8_t FileFormat; /*!< File Format */
__IO uint8_t ECC; /*!< ECC code */
__IO uint8_t CSD_CRC; /*!< CSD CRC */
__IO uint8_t Reserved4; /*!< always 1*/
} SD_CSD;
/**
* @brief Card Identification Data: CID Register
*/
typedef struct
{
__IO uint8_t ManufacturerID; /*!< ManufacturerID */
__IO uint16_t OEM_AppliID; /*!< OEM/Application ID */
__IO uint32_t ProdName1; /*!< Product Name part1 */
__IO uint8_t ProdName2; /*!< Product Name part2*/
__IO uint8_t ProdRev; /*!< Product Revision */
__IO uint32_t ProdSN; /*!< Product Serial Number */
__IO uint8_t Reserved1; /*!< Reserved1 */
__IO uint16_t ManufactDate; /*!< Manufacturing Date */
__IO uint8_t CID_CRC; /*!< CID CRC */
__IO uint8_t Reserved2; /*!< always 1 */
} SD_CID;
/**
* @brief SD Card information
*/
typedef struct
{
SD_CSD SD_csd;
SD_CID SD_cid;
uint64_t CardCapacity; /*!< Card Capacity */
uint32_t CardBlockSize; /*!< Card Block Size */
uint16_t RCA;
uint8_t CardType;
} SD_CardInfo;
/**
* @brief SD detection on its memory slot
*/
#define SD_PRESENT ((uint8_t)0x01)
#define SD_NOT_PRESENT ((uint8_t)0x00)
// void SD_DeInit(void);
SD_Error SD_Init(void);
SDTransferState SD_GetStatus(void);
int SD_CheckStatusWithTimeout(uint32_t timeout);
uint8_t SD_Detect(void);
SD_Error SD_PowerOFF(void);
SD_Error SD_ReadBlock(uint8_t *readbuff, uint32_t ReadAddr, uint16_t BlockSize);
SD_Error SD_ReadMultiBlocks(uint8_t *readbuff, uint32_t ReadAddr, uint16_t BlockSize, uint32_t NumberOfBlocks);
SD_Error SD_WriteBlock(uint8_t *writebuff, uint32_t WriteAddr, uint16_t BlockSize);
SD_Error SD_WriteMultiBlocks(uint8_t *writebuff, uint32_t WriteAddr, uint16_t BlockSize, uint32_t NumberOfBlocks);
SD_Error SD_ReadBlocks(uint8_t *readbuff, uint32_t ReadAddr, uint16_t BlockSize, uint32_t NumberOfBlocks);
SD_Error SD_WriteBlocks(uint8_t *writebuff, uint32_t WriteAddr, uint16_t BlockSize, uint32_t NumberOfBlocks);
SDTransferState SD_GetTransferState(void);
SD_Error SD_WaitReadOperation(uint32_t timeout);
SD_Error SD_WaitWriteOperation(uint32_t timeout);
#ifdef __cplusplus
}
#endif
extern SD_CardInfo SDCardInfo;
uint32_t SD_GetSectorCount();
uint32_t SD_GetSectorSize();
uint32_t SD_GetBlockSize();
uint32_t SD_GetCardType();
uint32_t SD_GetCardVersion();
uint32_t SD_GetCardClass();
#endif // _SDIO_SD_H_
@@ -70,7 +70,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_SPI_MODULE_ENABLED
// #define HAL_TIM_MODULE_ENABLED
// #define HAL_UART_MODULE_ENABLED
@@ -28,6 +28,13 @@
#include <string.h>
#if FF_MAX_SS != FF_MIN_SS
#error "Variable sector size is not supported"
#endif
#define BLOCK_SIZE FF_MAX_SS
#if !defined(BOOT)
#include "timers_driver.h"
#define WATCHDOG_SUSPEND(x) watchdogSuspend(x)