feat add third_party_driver/sdio for hc32f4a0 board

This commit is contained in:
Liu_Weichao 2022-10-18 17:27:30 +08:00
parent 700a07bc84
commit 9802c89fd6
23 changed files with 2586 additions and 42 deletions

View File

@ -35,7 +35,7 @@ menu "aiit-arm32-board feature"
menu "config board peripheral"
config MOUNT_SDCARD
bool "mount cd card"
bool "mount sd card"
default n
select BSP_USING_SDIO
endmenu

View File

@ -37,7 +37,7 @@ menu "aiit-riscv64-board feature"
menu "config board peripheral"
config MOUNT_SDCARD
bool "mount cd card"
bool "mount sd card"
default n
select BSP_USING_SDIO
config MOUNT_USB

View File

@ -75,7 +75,7 @@ CONFIG_KERNEL_SEMAPHORE=y
CONFIG_KERNEL_MUTEX=y
CONFIG_KERNEL_EVENT=y
CONFIG_KERNEL_MESSAGEQUEUE=y
CONFIG_KERNEL_SOFTTIMER=y
CONFIG_KERNEL_SOFTTIMER=n
CONFIG_SCHED_POLICY_RR_REMAINSLICE=y
# CONFIG_SCHED_POLICY_RR is not set
# CONFIG_SCHED_POLICY_FIFO is not set
@ -134,7 +134,7 @@ CONFIG_SHELL_HELP_LIST_VAR=y
# Kernel data structure Manage
#
CONFIG_KERNEL_QUEUEMANAGE=y
CONFIG_KERNEL_WORKQUEUE=y
CONFIG_KERNEL_WORKQUEUE=n
CONFIG_WORKQUEUE_KTASK_STACKSIZE=2048
CONFIG_WORKQUEUE_KTASK_PRIORITY=23
CONFIG_QUEUE_MAX=16

View File

@ -28,6 +28,12 @@ menu "hc32f4a0 feature"
endmenu
endmenu
config MOUNT_SDCARD
bool
default n
config MOUNT_USB
bool
default n
endmenu
menu "Hardware feature"

View File

@ -34,6 +34,10 @@ Modification:
#include <hc32_ll.h>
#include <connect_usart.h>
#ifdef BSP_USING_SDIO
#include <connect_sdio.h>
#endif
extern void entry(void);
extern int HwUsartInit();
@ -46,15 +50,10 @@ void SystemClockConfig(void)
stc_clock_xtal_init_t stcXtalInit;
stc_clock_pll_init_t stcPLLHInit;
/* PCLK0, HCLK Max 240MHz */
/* PCLK1, PCLK4 Max 120MHz */
/* PCLK2, PCLK3 Max 60MHz */
/* EX BUS Max 120MHz */
CLK_SetClockDiv(CLK_BUS_CLK_ALL, \
(CLK_PCLK0_DIV1 | CLK_PCLK1_DIV2 | CLK_PCLK2_DIV4 | \
CLK_PCLK3_DIV4 | CLK_PCLK4_DIV2 | CLK_EXCLK_DIV2 | \
CLK_SetClockDiv(CLK_BUS_CLK_ALL,
(CLK_PCLK0_DIV1 | CLK_PCLK1_DIV2 | CLK_PCLK2_DIV4 |
CLK_PCLK3_DIV4 | CLK_PCLK4_DIV2 | CLK_EXCLK_DIV2 |
CLK_HCLK_DIV1));
(void)CLK_XtalStructInit(&stcXtalInit);
/* Config Xtal and enable Xtal */
stcXtalInit.u8Mode = CLK_XTAL_MD_OSC;
@ -64,26 +63,23 @@ void SystemClockConfig(void)
(void)CLK_XtalInit(&stcXtalInit);
(void)CLK_PLLStructInit(&stcPLLHInit);
/* VCO = (8/1)*120 = 960MHz*/
/* VCO = (8/1)*100 = 800MHz*/
stcPLLHInit.u8PLLState = CLK_PLL_ON;
stcPLLHInit.PLLCFGR = 0UL;
stcPLLHInit.PLLCFGR_f.PLLM = 1UL - 1UL;
stcPLLHInit.PLLCFGR_f.PLLN = 120UL - 1UL;
stcPLLHInit.PLLCFGR_f.PLLN = 100UL - 1UL;
stcPLLHInit.PLLCFGR_f.PLLP = 4UL - 1UL;
stcPLLHInit.PLLCFGR_f.PLLQ = 4UL - 1UL;
stcPLLHInit.PLLCFGR_f.PLLR = 4UL - 1UL;
stcPLLHInit.PLLCFGR_f.PLLSRC = CLK_PLL_SRC_XTAL;
(void)CLK_PLLInit(&stcPLLHInit);
/* Highspeed SRAM set to 0 Read/Write wait cycle */
SRAM_SetWaitCycle(SRAM_SRAMH, SRAM_WAIT_CYCLE0, SRAM_WAIT_CYCLE0);
/* SRAM1_2_3_4_backup set to 1 Read/Write wait cycle */
SRAM_SetWaitCycle((SRAM_SRAM123 | SRAM_SRAM4 | SRAM_SRAMB), SRAM_WAIT_CYCLE1, SRAM_WAIT_CYCLE1);
/* Highspeed SRAM set to 1 Read/Write wait cycle */
SRAM_SetWaitCycle(SRAM_SRAMH, SRAM_WAIT_CYCLE1, SRAM_WAIT_CYCLE1);
/* SRAM1_2_3_4_backup set to 2 Read/Write wait cycle */
SRAM_SetWaitCycle((SRAM_SRAM123 | SRAM_SRAM4 | SRAM_SRAMB), SRAM_WAIT_CYCLE2, SRAM_WAIT_CYCLE2);
/* 0-wait @ 40MHz */
(void)EFM_SetWaitCycle(EFM_WAIT_CYCLE5);
EFM_SetWaitCycle(EFM_WAIT_CYCLE5);
/* 4 cycles for 200 ~ 250MHz */
GPIO_SetReadWaitCycle(GPIO_RD_WAIT4);
CLK_SetSysClockSrc(CLK_SYSCLK_SRC_PLL);
@ -98,7 +94,7 @@ void PeripheralClockConfig(void)
CLK_SetCANClockSrc(CLK_CAN2, CLK_CANCLK_SYSCLK_DIV6);
#endif
#if defined(RT_USING_ADC)
#if defined(BSP_USING_ADC)
CLK_SetPeriClockSrc(CLK_PERIPHCLK_PCLK);
#endif
}
@ -117,11 +113,19 @@ void SysTickConfiguration(void)
void SysTick_Handler(void)
{
x_base lock = 0;
lock = DISABLE_INTERRUPT();
TickAndTaskTimesliceUpdate();
ENABLE_INTERRUPT(lock);
}
struct InitSequenceDesc _board_init[] =
{
#ifdef BSP_USING_SDIO
{ "sdio", HwSdioInit },
#endif
{ " NONE ", NONE },
};

View File

@ -8,8 +8,16 @@ menuconfig BSP_USING_UART
menuconfig BSP_USING_SPI
bool "Using SPI device"
default y
default n
select RESOURCES_SPI
if BSP_USING_SPI
source "$BSP_DIR/third_party_driver/spi/Kconfig"
endif
menuconfig BSP_USING_SDIO
bool "Using SD CARD device"
default n
select RESOURCES_SDIO
if BSP_USING_SDIO
source "$BSP_DIR/third_party_driver/sdio/Kconfig"
endif

View File

@ -8,4 +8,8 @@ ifeq ($(CONFIG_BSP_USING_SPI),y)
SRC_DIR += spi
endif
ifeq ($(CONFIG_BSP_USING_SDIO),y)
SRC_DIR += sdio
endif
include $(KERNEL_ROOT)/compiler.mk

View File

@ -4,4 +4,8 @@ ifeq ($(CONFIG_BSP_USING_UART),y)
SRC_FILES += hc32_ll_usart.c
endif
ifeq ($(CONFIG_BSP_USING_SDIO),y)
SRC_FILES += hc32_ll_sdioc.c
endif
include $(KERNEL_ROOT)/compiler.mk

View File

@ -0,0 +1,42 @@
/*
* Copyright (c) 2020 AIIT XUOS Lab
* XiUOS is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/**
* @file connect_sdio.h
* @brief define hc32f4a0-board sdio function and struct
* @version 2.0
* @author AIIT XUOS Lab
* @date 2022-03-15
*/
#ifndef CONNECT_SDIO_H
#define CONNECT_SDIO_H
#include <device.h>
#include <hc32_ll_gpio.h>
#include <hc32_ll_fcg.h>
#include <hc32_ll_sdioc.h>
#include <hardware_sdio.h>
#ifdef __cplusplus
extern "C" {
#endif
#define SD_CARD_STACK_SIZE 2048
int HwSdioInit(void);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -49,9 +49,9 @@ struct UsartHwCfg
#define FCG_TMR0_CLK FCG_Fcg2PeriphClockCmd
#define FCG_DMA_CLK FCG_Fcg0PeriphClockCmd
#define KERNEL_CONSOLE_BUS_NAME SERIAL_BUS_NAME_3
#define KERNEL_CONSOLE_DRV_NAME SERIAL_DRV_NAME_3
#define KERNEL_CONSOLE_DEVICE_NAME SERIAL_3_DEVICE_NAME_0
#define KERNEL_CONSOLE_BUS_NAME SERIAL_BUS_NAME_6
#define KERNEL_CONSOLE_DRV_NAME SERIAL_DRV_NAME_6
#define KERNEL_CONSOLE_DEVICE_NAME SERIAL_6_DEVICE_NAME_0
int HwUsartInit(void);

View File

@ -0,0 +1,298 @@
/**
*******************************************************************************
* @file sdioc/sdioc_sd/source/sd.h
* @brief This file contains all the functions prototypes of the Secure
* Digital(SD) driver library.
@verbatim
Change Logs:
Date Author Notes
2022-03-31 CDT First version
@endverbatim
*******************************************************************************
* Copyright (C) 2022, Xiaohua Semiconductor Co., Ltd. All rights reserved.
*
* This software component is licensed by XHSC under BSD 3-Clause license
* (the "License"); You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
*******************************************************************************
*/
/**
* @file hardware_sdio.h
* @brief define hc32f4a0-board sdio function and struct
* @version 2.0
* @author AIIT XUOS Lab
* @date 2022-10-18
*/
#ifndef __HARDWARE_SDIO_H__
#define __HARDWARE_SDIO_H__
/* C binding of definitions if building with C++ compiler */
#ifdef __cplusplus
extern "C"
{
#endif
/*******************************************************************************
* Include files
******************************************************************************/
#include "hc32_ll_sdioc.h"
#include "hc32_ll_dma.h"
#include "hc32_ll_utility.h"
/**
* @addtogroup HC32F4A0_DDL_Examples
* @{
*/
/**
* @addtogroup SDIOC_SD_Card
* @{
*/
/*******************************************************************************
* Global type definitions ('typedef')
******************************************************************************/
/**
* @defgroup SD_Global_Types SD Global Types
* @{
*/
/**
* @brief SD Card State enumeration structure definition
*/
typedef enum {
SD_CARD_STAT_IDLE = 0x00U, /*!< Card state is idle */
SD_CARD_STAT_RDY = 0x01U, /*!< Card state is ready */
SD_CARD_STAT_IDENTIFY = 0x02U, /*!< Card is in identification state */
SD_CARD_STAT_STANDBY = 0x03U, /*!< Card is in standby state */
SD_CARD_STAT_TRANS = 0x04U, /*!< Card is in transfer state */
SD_CARD_STAT_TX_DATA = 0x05U, /*!< Card is sending an operation */
SD_CARD_STAT_RX_DATA = 0x06U, /*!< Card is receiving operation information */
SD_CARD_STAT_PGM = 0x07U, /*!< Card is in programming state */
SD_CARD_STAT_DISCONNECT = 0x08U /*!< Card is disconnected */
} en_sd_card_state_t;
/**
* @brief SD Card Information Structure definition
*/
typedef struct {
uint32_t u32CardType; /*!< Specifies the card Type */
uint32_t u32CardVersion; /*!< Specifies the card version */
uint32_t u32Class; /*!< Specifies the class of the card class */
uint32_t u32RelativeCardAddr; /*!< Specifies the Relative Card Address */
uint32_t u32BlockNum; /*!< Specifies the Card Capacity in blocks */
uint32_t u32BlockSize; /*!< Specifies one block size in bytes */
uint32_t u32LogBlockNum; /*!< Specifies the Card logical Capacity in blocks */
uint32_t u32LogBlockSize; /*!< Specifies logical block size in bytes */
} stc_sd_card_info_t;
/**
* @brief SD Card Specific Data(CSD Register) Structure definition
*/
typedef struct {
uint8_t u8CSDStruct; /*!< CSD structure */
uint8_t u8Reserved1; /*!< Reserved */
uint8_t u8TAAC; /*!< Data read access time 1 */
uint8_t u8NSAC; /*!< Data read access time 2 in CLK cycles */
uint8_t u8MaxBusClkFreq; /*!< Max. bus clock frequency */
uint16_t u16CardCmdClass; /*!< Card command classes */
uint8_t u8ReadBlockLen; /*!< Max. read data block length */
uint8_t u8BlockReadPartial; /*!< Partial blocks for read allowed */
uint8_t u8WriteBlockMisalign; /*!< Write block misalignment */
uint8_t u8ReadBlockMisalign; /*!< Read block misalignment */
uint8_t u8DSRImplement; /*!< DSR implemented */
uint8_t u8Reserved2; /*!< Reserved */
uint32_t u32DeviceSize; /*!< Device Size */
uint8_t u8MaxReadCurrVDDMin; /*!< Max. read current @ VDD min */
uint8_t u8MaxReadCurrVDDMax; /*!< Max. read current @ VDD max */
uint8_t u8MaxWriteCurrVDDMin; /*!< Max. write current @ VDD min */
uint8_t u8MaxWriteCurrVDDMax; /*!< Max. write current @ VDD max */
uint8_t u8DeviceSizeMul; /*!< Device size multiplier */
uint8_t u8EraseSingleBlockEn; /*!< Erase single block enable */
uint8_t u8EraseSectorSize; /*!< Erase sector size */
uint8_t u8WriteProtectGroupSize; /*!< Write protect group size */
uint8_t u8WriteProtectGroupEn; /*!< Write protect group enable */
uint8_t u8Reserved3; /*!< Reserved */
uint8_t u8WriteSpeedFactor; /*!< Write speed factor */
uint8_t u8MaxWriteBlockLen; /*!< Max. write data block length */
uint8_t u8WriteBlockPartial; /*!< Partial blocks for write allowed */
uint8_t u8Reserved4; /*!< Reserved */
uint8_t u8FileFormatGroup; /*!< File format group */
uint8_t u8CopyFlag; /*!< Copy flag (OTP) */
uint8_t u8PermWriteProtect; /*!< Permanent write protection */
uint8_t u8TempWriteProtect; /*!< Temporary write protection */
uint8_t u8FileFormat; /*!< File format */
uint8_t u8Reserved5; /*!< Reserved */
uint8_t u8CRCChecksum; /*!< CSD CRC7 checksum */
uint8_t u8Reserved6; /*!< Always 1 */
} stc_sd_card_csd_t;
/**
* @brief SD Card Identification Data(CID Register) Structure definition
*/
typedef struct {
uint8_t u8ManufacturerID; /*!< Manufacturer ID */
uint16_t u16OemAppID; /*!< OEM/Application ID */
uint32_t u32ProductName1; /*!< Product Name part1 */
uint8_t u8ProductName2; /*!< Product Name part2 */
uint8_t u8ProductRevision; /*!< Product Revision */
uint32_t u32ProductSN; /*!< Product Serial Number */
uint8_t u8Reserved1; /*!< Reserved1 */
uint16_t u16ManufactDate; /*!< Manufacturing Date */
uint8_t u8CRCChecksum; /*!< CRC7 checksum */
uint8_t u8Reserved2; /*!< Always 1 */
} stc_sd_card_cid_t;
/**
* @brief SD Card Status Structure definition (returned by ACMD13)
*/
typedef struct {
uint8_t u8DataBusWidth; /*!< Shows the currently defined data bus width */
uint8_t u8SecuredMode; /*!< Card is in secured mode of operation */
uint16_t u16CardType; /*!< Carries information about card type */
uint32_t u32ProtectedAreaSize; /*!< Carries information about the capacity of protected area */
uint8_t u8SpeedClass; /*!< Carries information about the speed class of the card */
uint8_t u8PerformanceMove; /*!< Carries information about the card's performance move */
uint8_t u8AllocUnitSize; /*!< Carries information about the card's allocation unit size */
uint16_t u16EraseSize; /*!< Determines the number of AUs to be erased in one operation */
uint8_t u8EraseTimeout; /*!< Determines the timeout for any number of AU erase */
uint8_t u8EraseOffset; /*!< Carries information about the erase offset */
} stc_sd_card_status_t;
/**
* @brief SD handle Structure definition
*/
typedef struct {
CM_SDIOC_TypeDef *SDIOCx; /*!< Pointer to SDIOC registers base address */
stc_sdioc_init_t stcSdiocInit; /*!< SDIOC Initialize structure @ref stc_sdioc_init_t */
CM_DMA_TypeDef *DMAx; /*!< Pointer to DMA registers base address */
uint8_t u8DmaTxCh; /*!< Specifies the DMA channel used to send */
uint8_t u8DmaRxCh; /*!< Specifies the DMA channel used to receive */
uint8_t *pu8Buffer; /*!< Pointer to SD Tx/Rx transfer Buffer */
uint32_t u32Len; /*!< SD Tx/Rx Transfer length */
uint32_t u32Context; /*!< SD transfer context */
uint32_t u32ErrorCode; /*!< SD Card Error codes */
stc_sd_card_info_t stcSdCardInfo; /*!< SD Card information */
uint32_t u32CSD[4]; /*!< SD card specific data table */
uint32_t u32CID[4]; /*!< SD card identification number table */
uint32_t u32SCR[2]; /*!< SD Configuration Register table */
} stc_sd_handle_t;
/**
* @}
*/
/*******************************************************************************
* Global pre-processor symbols/macros ('#define')
******************************************************************************/
/**
* @defgroup SD_Global_Macros SD Global Macros
* @{
*/
/**
* @defgroup SD_Transfer_Context SD Transfer Context
* @{
*/
#define SD_CONTEXT_NONE (0x00UL) /*!< None */
#define SD_CONTEXT_RD_SINGLE_BLOCK (0x01UL) /*!< Read single block operation */
#define SD_CONTEXT_RD_MULTI_BLOCK (0x02UL) /*!< Read multiple blocks operation */
#define SD_CONTEXT_WR_SINGLE_BLOCK (0x10UL) /*!< Write single block operation */
#define SD_CONTEXT_WR_MULTI_BLOCK (0x20UL) /*!< Write multiple blocks operation */
#define SD_CONTEXT_INT (0x40UL) /*!< Process in Interrupt mode */
#define SD_CONTEXT_DMA (0x80UL) /*!< Process in DMA mode */
/**
* @}
*/
/**
* @defgroup SD_Support_Memory_Card SD Support Memory Card
* @{
*/
#define SD_CARD_SDSC (0x00UL)
#define SD_CARD_SDHC_SDXC (0x01UL)
#define SD_CARD_SECURED (0x03UL)
/**
* @}
*/
/**
* @defgroup SD_Support_Version SD Support Version
* @{
*/
#define SD_CARD_V1_X (0x00UL) /*!< SD Card version: 1.x */
#define SD_CARD_V2_X (0x01UL) /*!< SD Card version: 2.x */
/**
* @}
*/
/**
* @}
*/
/*******************************************************************************
* Global variable definitions ('extern')
******************************************************************************/
/*******************************************************************************
Global function prototypes (definition in C source)
******************************************************************************/
/**
* @addtogroup SD_Global_Functions
* @{
*/
int32_t SD_DeInit(stc_sd_handle_t *handle);
int32_t SD_Init(stc_sd_handle_t *handle);
int32_t SD_GetCardState(stc_sd_handle_t *handle, en_sd_card_state_t *peCardState);
int32_t SD_GetCardCID(const stc_sd_handle_t *handle, stc_sd_card_cid_t *pstcCardCID);
int32_t SD_GetCardCSD(stc_sd_handle_t *handle, stc_sd_card_csd_t *pstcCardCSD);
int32_t SD_GetCardStatus(stc_sd_handle_t *handle, stc_sd_card_status_t *pstcCardStatus);
int32_t SD_GetCardInfo(stc_sd_handle_t *handle, stc_sd_card_info_t *pstcCardInfo);
int32_t SD_GetErrorCode(const stc_sd_handle_t *handle, uint32_t *pu32ErrorCode);
/* Callback in non blocking modes */
void SD_IRQHandler(stc_sd_handle_t *handle);
void SD_TxCompleteCallback(stc_sd_handle_t *handle);
void SD_RxCompleteCallback(stc_sd_handle_t *handle);
void SD_ErrorCallback(stc_sd_handle_t *handle);
/* Polling Mode */
int32_t SD_Erase(stc_sd_handle_t *handle, uint32_t u32BlockStartAddr, uint32_t u32BlockEndAddr);
int32_t SD_ReadBlocks(stc_sd_handle_t *handle, uint32_t u32BlockAddr, uint16_t u16BlockCount,
uint8_t *pu8Data, uint32_t u32Timeout);
int32_t SD_WriteBlocks(stc_sd_handle_t *handle, uint32_t u32BlockAddr, uint16_t u16BlockCount,
uint8_t *pu8Data, uint32_t u32Timeout);
/* Interrupt Mode */
int32_t SD_ReadBlocks_INT(stc_sd_handle_t *handle, uint32_t u32BlockAddr, uint16_t u16BlockCount, uint8_t *pu8Data);
int32_t SD_WriteBlocks_INT(stc_sd_handle_t *handle, uint32_t u32BlockAddr, uint16_t u16BlockCount, uint8_t *pu8Data);
/* DMA Mode */
int32_t SD_ReadBlocks_DMA(stc_sd_handle_t *handle, uint32_t u32BlockAddr, uint16_t u16BlockCount, uint8_t *pu8Data);
int32_t SD_WriteBlocks_DMA(stc_sd_handle_t *handle, uint32_t u32BlockAddr, uint16_t u16BlockCount, uint8_t *pu8Data);
/* Abort */
int32_t SD_Abort(stc_sd_handle_t *handle);
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif /* __SD_H__ */
/*******************************************************************************
* EOF (not truncated)
******************************************************************************/

View File

@ -0,0 +1,24 @@
if BSP_USING_SDIO
config SDIO_BUS_NAME
string "sdio bus name"
default "sdio"
config SDIO_DRIVER_NAME
string "sdio driver name"
default "sdio_drv"
config SDIO_DEVICE_NAME
string "sdio device name"
default "sdio_dev"
config MOUNT_SDCARD_FS
bool "mount sd card file system"
default y
select MOUNT_SDCARD
if MOUNT_SDCARD_FS
config MOUNT_SDCARD_FS_TYPE
int "choose file system type : FATFS(0) LWEXT4(3)"
default 0
endif
endif

View File

@ -0,0 +1,3 @@
SRC_FILES := connect_sdio.c hardware_sdio.c
include $(KERNEL_ROOT)/compiler.mk

View File

@ -0,0 +1,349 @@
/**
*******************************************************************************
* @file sdioc/sdioc_sd/source/main.c
* @brief Main program of SDIOC SD card for the Device Driver Library.
@verbatim
Change Logs:
Date Author Notes
2022-03-31 CDT First version
@endverbatim
*******************************************************************************
* Copyright (C) 2022, Xiaohua Semiconductor Co., Ltd. All rights reserved.
*
* This software component is licensed by XHSC under BSD 3-Clause license
* (the "License"); You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
*******************************************************************************
*/
/**
* @file connect_sdio.c
* @brief support sdio function using bus driver framework on hc32f4a0 board
* @version 2.0
* @author AIIT XUOS Lab
* @date 2022-10-18
*/
/*************************************************
File name: connect_sdio.c
Description: support hc32f4a0-board sd card configure and sdio bus register function
Others:
History:
1. Date: 2022-01-24
Author: AIIT XUOS Lab
Modification:
1. support hc32f4a0-board sdio configure, write and read
2. support hc32f4a0-board sdio bus device and driver register
*************************************************/
#include <connect_sdio.h>
/* SD transfer mode */
#define SD_TRANS_MD_POLLING (0U)
#define SD_TRANS_MD_INT (1U)
#define SD_TRANS_MD_DMA (2U)
/* Populate the following macro with an value, reference "SD transfer mode" */
#define SD_TRANS_MD (SD_TRANS_MD_POLLING)
/* SDIOC configuration define */
#define SDIOC_SD_UINT (CM_SDIOC1)
#define SDIOC_SD_CLK (FCG1_PERIPH_SDIOC1)
/* CD = PB13 */
#define SDIOC_CD_PORT (GPIO_PORT_B)
#define SDIOC_CD_PIN (GPIO_PIN_13)
/* CK = PC12 */
#define SDIOC_CK_PORT (GPIO_PORT_C)
#define SDIOC_CK_PIN (GPIO_PIN_12)
/* CMD = PD02 */
#define SDIOC_CMD_PORT (GPIO_PORT_D)
#define SDIOC_CMD_PIN (GPIO_PIN_02)
/* D0 = PB07 */
#define SDIOC_D0_PORT (GPIO_PORT_B)
#define SDIOC_D0_PIN (GPIO_PIN_07)
/* D1 = PA08 */
#define SDIOC_D1_PORT (GPIO_PORT_A)
#define SDIOC_D1_PIN (GPIO_PIN_08)
/* D2 = PC10 */
#define SDIOC_D2_PORT (GPIO_PORT_C)
#define SDIOC_D2_PIN (GPIO_PIN_10)
/* D3 = PB05 */
#define SDIOC_D3_PORT (GPIO_PORT_B)
#define SDIOC_D3_PIN (GPIO_PIN_05)
static stc_sd_handle_t gSdHandle;
static int sd_lock = -1;
static void SdCardConfig(void)
{
/* Enable SDIOC clock */
FCG_Fcg1PeriphClockCmd(SDIOC_SD_CLK, ENABLE);
/* SDIOC pins configuration */
stc_gpio_init_t stcGpioInit;
(void)GPIO_StructInit(&stcGpioInit);
stcGpioInit.u16PinState = PIN_STAT_RST;
stcGpioInit.u16PinDir = PIN_DIR_IN;
(void)GPIO_Init(SDIOC_CD_PORT, SDIOC_CD_PIN, &stcGpioInit);
GPIO_SetFunc(SDIOC_CK_PORT, SDIOC_CK_PIN, GPIO_FUNC_9);
GPIO_SetFunc(SDIOC_CMD_PORT, SDIOC_CMD_PIN, GPIO_FUNC_9);
GPIO_SetFunc(SDIOC_D0_PORT, SDIOC_D0_PIN, GPIO_FUNC_9);
GPIO_SetFunc(SDIOC_D1_PORT, SDIOC_D1_PIN, GPIO_FUNC_9);
GPIO_SetFunc(SDIOC_D2_PORT, SDIOC_D2_PIN, GPIO_FUNC_9);
GPIO_SetFunc(SDIOC_D3_PORT, SDIOC_D3_PIN, GPIO_FUNC_9);
/* Configure structure initialization */
gSdHandle.SDIOCx = SDIOC_SD_UINT;
gSdHandle.stcSdiocInit.u32Mode = SDIOC_MD_SD;
gSdHandle.stcSdiocInit.u8CardDetect = SDIOC_CARD_DETECT_CD_PIN_LVL;
gSdHandle.stcSdiocInit.u8SpeedMode = SDIOC_SPEED_MD_HIGH;
gSdHandle.stcSdiocInit.u8BusWidth = SDIOC_BUS_WIDTH_4BIT;
gSdHandle.stcSdiocInit.u16ClockDiv = SDIOC_CLK_DIV2;
gSdHandle.DMAx = NULL;
}
static uint32 SdioConfigure(void *drv, struct BusConfigureInfo *configure_info)
{
NULL_PARAM_CHECK(drv);
NULL_PARAM_CHECK(configure_info);
if (configure_info->configure_cmd == OPER_BLK_GETGEOME) {
NULL_PARAM_CHECK(configure_info->private_data);
struct DeviceBlockArrange *args = (struct DeviceBlockArrange *)configure_info->private_data;
args->size_perbank = gSdHandle.stcSdCardInfo.u32BlockSize;
args->block_size = gSdHandle.stcSdCardInfo.u32BlockSize;
args->bank_num = gSdHandle.stcSdCardInfo.u32BlockNum;
}
return EOK;
}
static uint32 SdioOpen(void *dev)
{
NULL_PARAM_CHECK(dev);
if(sd_lock >= 0) {
KSemaphoreDelete(sd_lock);
}
sd_lock = KSemaphoreCreate(1);
if (sd_lock < 0){
return ERROR;
}
return EOK;
}
static uint32 SdioClose(void *dev)
{
NULL_PARAM_CHECK(dev);
KSemaphoreDelete(sd_lock);
return EOK;
}
static uint32 SdioRead(void *dev, struct BusBlockReadParam *read_param)
{
uint8 ret = EOK;
uint32 sector = read_param->pos;
uint32 block_num = read_param->size;
uint8 *read_buffer = (uint8 *)read_param->buffer;
KSemaphoreObtain(sd_lock, WAITING_FOREVER);
if (LL_OK != SD_ReadBlocks(&gSdHandle, sector, block_num, read_buffer, 2000UL)) {
KPrintf("Read data blocks failed.\r\n");
return 0;
}
KSemaphoreAbandon(sd_lock);
return read_param->size;
}
static uint32 SdioWrite(void *dev, struct BusBlockWriteParam *write_param)
{
uint8 ret = EOK;
uint32 sector = write_param->pos;
uint32 block_num = write_param->size;
uint8 *write_buffer = (uint8 *)write_param->buffer;
KSemaphoreObtain(sd_lock, WAITING_FOREVER);
if (LL_OK != SD_WriteBlocks(&gSdHandle, sector, block_num, write_buffer, 2000U)) {
KPrintf("Write data blocks failed.\r\n");
return 0;
}
KSemaphoreAbandon(sd_lock);
return write_param->size;
}
static int SdioControl(struct HardwareDev *dev, struct HalDevBlockParam *block_param)
{
NULL_PARAM_CHECK(dev);
if (OPER_BLK_GETGEOME == block_param->cmd) {
block_param->dev_block.size_perbank = gSdHandle.stcSdCardInfo.u32BlockSize;
block_param->dev_block.block_size = gSdHandle.stcSdCardInfo.u32BlockSize;
block_param->dev_block.bank_num = gSdHandle.stcSdCardInfo.u32BlockNum;
}
return EOK;
}
static struct SdioDevDone dev_done =
{
SdioOpen,
SdioClose,
SdioWrite,
SdioRead,
};
#if defined(FS_VFS) && defined(MOUNT_SDCARD_FS)
#include <iot-vfs.h>
/**
* @description: Mount SD card
* @return 0
*/
static int MountSDCardFs(enum FilesystemType fs_type)
{
if (MountFilesystem(SDIO_BUS_NAME, SDIO_DEVICE_NAME, SDIO_DRIVER_NAME, fs_type, "/") == 0)
KPrintf("Sd card mount to '/'");
else
KPrintf("Sd card mount to '/' failed!");
return 0;
}
#endif
static void SdCardAttach(void)
{
static stc_sd_handle_t *sd_handle = &gSdHandle;
KPrintf("\r\nCard inserted.\r\n");
/* Reset and init SDIOC */
if (LL_OK != SDIOC_SWReset(sd_handle->SDIOCx, SDIOC_SW_RST_ALL)) {
KPrintf("Reset SDIOC failed!\r\n");
} else if (LL_OK != SD_Init(sd_handle)) {
KPrintf("SD card initialize failed!\r\n");
} else {
KPrintf("SD card init done!\r\n");
}
#ifdef MOUNT_SDCARD_FS
/*mount file system*/
MountSDCardFs(MOUNT_SDCARD_FS_TYPE);
#endif
}
static void SdCardDetach(void)
{
/*unmount file system*/
KPrintf("\r\nCard detect extracted.\r\n");
#ifdef MOUNT_SDCARD_FS
UnmountFileSystem("/");
#endif
}
static uint8 SdCardReadCd(void)
{
en_pin_state_t sd_cd_state = GPIO_ReadInputPins(SDIOC_CD_PORT, SDIOC_CD_PIN);
if (PIN_RESET == sd_cd_state) {
return 0;
} else {
return 1;
}
}
static void SdCardTask(void* parameter)
{
static int sd_card_status = 0;
while (1) {
if (!SdCardReadCd()) {
if (!sd_card_status) {
SdCardAttach();
sd_card_status = 1;
}
} else {
if (sd_card_status) {
SdCardDetach();
sd_card_status = 0;
}
}
}
}
#ifdef MOUNT_SDCARD
int MountSDCard()
{
int sd_card_task = 0;
sd_card_task = KTaskCreate("sd_card", SdCardTask, NONE,
SD_CARD_STACK_SIZE, 8);
if(sd_card_task < 0) {
KPrintf("sd_card_task create failed ...%s %d.\n", __FUNCTION__,__LINE__);
return ERROR;
}
StartupKTask(sd_card_task);
return EOK;
}
#endif
int HwSdioInit(void)
{
x_err_t ret = EOK;
static struct SdioBus sdio_bus;
static struct SdioDriver sdio_drv;
static struct SdioHardwareDevice sdio_dev;
memset(&sdio_bus, 0, sizeof(struct SdioBus));
memset(&sdio_drv, 0, sizeof(struct SdioDriver));
memset(&sdio_dev, 0, sizeof(struct SdioHardwareDevice));
SdCardConfig();
ret = SdioBusInit(&sdio_bus, SDIO_BUS_NAME);
if (ret != EOK) {
KPrintf("Sdio bus init error %d\n", ret);
return ERROR;
}
ret = SdioDriverInit(&sdio_drv, SDIO_DRIVER_NAME);
if (ret != EOK) {
KPrintf("Sdio driver init error %d\n", ret);
return ERROR;
}
ret = SdioDriverAttachToBus(SDIO_DRIVER_NAME, SDIO_BUS_NAME);
if (ret != EOK) {
KPrintf("Sdio driver attach error %d\n", ret);
return ERROR;
}
sdio_dev.dev_done = &dev_done;
sdio_dev.haldev.dev_block_control = SdioControl;
ret = SdioDeviceRegister(&sdio_dev, SDIO_DEVICE_NAME);
if (ret != EOK) {
KPrintf("Sdio device register error %d\n", ret);
return ERROR;
}
ret = SdioDeviceAttachToBus(SDIO_DEVICE_NAME, SDIO_BUS_NAME);
if (ret != EOK) {
KPrintf("Sdio device register error %d\n", ret);
return ERROR;
}
return ret;
}

File diff suppressed because it is too large Load Diff

View File

@ -104,12 +104,22 @@ struct SerialHardwareDevice serial_device_3;
void Uart3RxIrqHandler(void)
{
x_base lock = 0;
lock = DISABLE_INTERRUPT();
SerialSetIsr(&serial_device_3, SERIAL_EVENT_RX_IND);
ENABLE_INTERRUPT(lock);
}
void Uart3RxErrIrqHandler(void)
{
x_base lock = 0;
lock = DISABLE_INTERRUPT();
UartRxErrIsr(&serial_bus_3, &serial_driver_3, &serial_device_3);
ENABLE_INTERRUPT(lock);
}
#endif
@ -120,12 +130,22 @@ struct SerialHardwareDevice serial_device_6;
void Uart6RxIrqHandler(void)
{
x_base lock = 0;
lock = DISABLE_INTERRUPT();
SerialSetIsr(&serial_device_6, SERIAL_EVENT_RX_IND);
ENABLE_INTERRUPT(lock);
}
void Uart6RxErrIrqHandler(void)
{
x_base lock = 0;
lock = DISABLE_INTERRUPT();
UartRxErrIsr(&serial_bus_6, &serial_driver_6, &serial_device_6);
ENABLE_INTERRUPT(lock);
}
#endif

View File

@ -42,7 +42,7 @@ menu "imxrt1176-sbc feature"
menu "config board peripheral"
config MOUNT_SDCARD
bool "mount cd card"
bool "mount sd card"
default n
endmenu

View File

@ -46,7 +46,7 @@ menu "kd233 feature"
menu "config board peripheral"
config MOUNT_SDCARD
bool "mount cd card"
bool "mount sd card"
default n
select BSP_USING_SPI
select BSP_USING_SPI1

View File

@ -42,7 +42,7 @@ menu "ok1052-c feature"
menu "config board peripheral"
config MOUNT_SDCARD
bool "mount cd card"
bool "mount sd card"
default n
endmenu

View File

@ -12,7 +12,7 @@ if BSP_USING_SDIO
default "sdio_dev"
config MOUNT_SDCARD_FS
bool "mount cd card file system"
bool "mount sd card file system"
default n
select MOUNT_SDCARD

View File

@ -12,7 +12,7 @@ if BSP_USING_SDIO
default "sdio_dev"
config MOUNT_SDCARD_FS
bool "mount cd card file system"
bool "mount sd card file system"
default n
select MOUNT_SDCARD

View File

@ -37,7 +37,7 @@ menu "xidatong-riscv64 feature"
menu "config board peripheral"
config MOUNT_SDCARD
bool "mount cd card"
bool "mount sd card"
default n
config MOUNT_USB
bool "mount usb"

View File

@ -12,7 +12,7 @@ if BSP_USING_SDIO
default "sdio_dev"
config MOUNT_SDCARD_FS
bool "mount cd card file system"
bool "mount sd card file system"
default n
select MOUNT_SDCARD