Skip to content

Commit

Permalink
New SDIO driver
Browse files Browse the repository at this point in the history
This patch adds a new SDIO driver as well as a dedicated BBIO interface
to use raw SDIO commands.
  • Loading branch information
Baldanos committed Mar 10, 2023
1 parent 4013b8e commit 28deb4f
Show file tree
Hide file tree
Showing 16 changed files with 927 additions and 215 deletions.
116 changes: 16 additions & 100 deletions src/drv/stm32cube/bsp_mmc.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
HydraBus/HydraNFC - Copyright (C) 2014-2015 Benjamin VERNOUX
HydraBus/HydraNFC - Copyright (C) 2014-2023 Benjamin VERNOUX
HydraBus/HydraNFC - Copyright (C) 2014-2023 Nicolas OBERLI
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -13,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
#include "bsp_mmc.h"
#include "bsp_mmc_conf.h"
#include "bsp_sdio.h"

/*
Warning in order to use this driver all GPIOs peripherals shall be enabled.
Expand All @@ -25,104 +26,25 @@ static MMC_HandleTypeDef mmc_handle[NB_MMC];
static mode_config_proto_t* mmc_mode_conf[NB_MMC];
static volatile uint16_t dummy_read;

/**
* @brief Init low level hardware: GPIO, CLOCK, NVIC...
* @param dev_num: MMC dev num
* @retval None
*/
/*
This function replaces HAL_MMC_MspInit() in order to manage multiple devices.
HAL_MMC_MspInit() shall be empty/not defined
*/
static void mmc_gpio_hw_init(bsp_dev_mmc_t dev_num)
{
GPIO_InitTypeDef GPIO_InitStructure;
(void)dev_num;

/* Enable the MMC peripheral */
__SDIO_CLK_ENABLE();

GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
GPIO_InitStructure.Pull = GPIO_NOPULL;
GPIO_InitStructure.Speed = BSP_MMC_GPIO_SPEED;
GPIO_InitStructure.Alternate = BSP_MMC_AF;


GPIO_InitStructure.Pin = BSP_MMC_CK_PIN;
HAL_GPIO_Init(BSP_MMC_CK_PORT, &GPIO_InitStructure);

GPIO_InitStructure.Mode = GPIO_MODE_AF_OD;
GPIO_InitStructure.Pull = GPIO_PULLUP;

GPIO_InitStructure.Pin = BSP_MMC_CMD_PIN;
HAL_GPIO_Init(BSP_MMC_CMD_PORT, &GPIO_InitStructure);

GPIO_InitStructure.Pin = BSP_MMC_D0_PIN;
HAL_GPIO_Init(BSP_MMC_D0_PORT, &GPIO_InitStructure);
}

/**
* @brief DeInit low level hardware: GPIO, CLOCK, NVIC...
* @param dev_num: MMC dev num
* @retval None
*/
/*
This function replaces HAL_MMC_MspDeInit() in order to manage multiple devices.
HAL_MMC_MspDeInit() shall be empty/not defined
*/
static void mmc_gpio_hw_deinit(bsp_dev_mmc_t dev_num)
{
(void)dev_num;
__SDIO_FORCE_RESET();
__SDIO_RELEASE_RESET();
/* Disable peripherals GPIO */
HAL_GPIO_DeInit(BSP_MMC_CK_PORT, BSP_MMC_CK_PIN);
HAL_GPIO_DeInit(BSP_MMC_CMD_PORT, BSP_MMC_CMD_PIN);
HAL_GPIO_DeInit(BSP_MMC_D0_PORT, BSP_MMC_D0_PIN);
}

/**
* @brief MMCx error treatment function.
* @param dev_num: MMC dev num
* @retval None
*/
static void mmc_error(bsp_dev_mmc_t dev_num)__attribute__((unused));
static void mmc_error(bsp_dev_mmc_t dev_num)
{
(void)dev_num;
if(bsp_mmc_deinit(dev_num) == BSP_OK) {
/* Re-Initialize the MMC comunication bus */
bsp_mmc_init(dev_num, mmc_mode_conf[dev_num]);
}
}

/**
* @brief Init MMC device.
* @param dev_num: MMC dev num.
* @param mode_conf: Mode config proto.
* @retval status: status of the init.
*/
bsp_status_t bsp_mmc_init(bsp_dev_mmc_t dev_num, mode_config_proto_t* mode_conf)
bsp_status_t bsp_mmc_init(bsp_dev_sdio_t dev_num, mode_config_proto_t* mode_conf)
{
MMC_HandleTypeDef* hmmc;
bsp_status_t status;

mmc_mode_conf[dev_num] = mode_conf;
hmmc = &mmc_handle[dev_num];

mmc_gpio_hw_init(dev_num);

__HAL_MMC_RESET_HANDLE_STATE(hmmc);
status = (bsp_status_t) bsp_sdio_init(dev_num, mode_conf);

hmmc->Instance = BSP_MMC;
hmmc->Init.ClockEdge = SDIO_CLOCK_EDGE_RISING;
hmmc->Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE;
hmmc->Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE;
hmmc->Init.BusWide = SDIO_BUS_WIDE_1B;
hmmc->Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
hmmc->Init.ClockDiv = SDIO_INIT_CLK_DIV;
bsp_sdio_get_instance(dev_num, (void *)&(hmmc->Instance));

status = (bsp_status_t) HAL_MMC_Init(hmmc);
__HAL_MMC_RESET_HANDLE_STATE(hmmc);

if(status != BSP_OK) {
return status;
Expand All @@ -138,23 +60,17 @@ bsp_status_t bsp_mmc_init(bsp_dev_mmc_t dev_num, mode_config_proto_t* mode_conf)
* @param dev_num: MMC dev num.
* @retval status: status of the deinit.
*/
bsp_status_t bsp_mmc_deinit(bsp_dev_mmc_t dev_num)
bsp_status_t bsp_mmc_deinit(bsp_dev_sdio_t dev_num)
{
MMC_HandleTypeDef* hmmc;
bsp_status_t status;

hmmc = &mmc_handle[dev_num];

/* De-initialize the MMC comunication bus */
status = (bsp_status_t) HAL_MMC_DeInit(hmmc);

/* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
mmc_gpio_hw_deinit(dev_num);
status = (bsp_status_t) bsp_sdio_deinit(dev_num);

return status;
}

uint32_t *bsp_mmc_get_cid(bsp_dev_mmc_t dev_num)
uint32_t *bsp_mmc_get_cid(bsp_dev_sdio_t dev_num)
{
MMC_HandleTypeDef* hmmc;

Expand All @@ -163,7 +79,7 @@ uint32_t *bsp_mmc_get_cid(bsp_dev_mmc_t dev_num)
return hmmc->CID;
}

uint32_t *bsp_mmc_get_csd(bsp_dev_mmc_t dev_num)
uint32_t *bsp_mmc_get_csd(bsp_dev_sdio_t dev_num)
{
MMC_HandleTypeDef* hmmc;

Expand All @@ -172,7 +88,7 @@ uint32_t *bsp_mmc_get_csd(bsp_dev_mmc_t dev_num)
return hmmc->CSD;
}

bsp_status_t bsp_mmc_change_bus_width(bsp_dev_mmc_t dev_num, uint8_t bus_size)
bsp_status_t bsp_mmc_change_bus_width(bsp_dev_sdio_t dev_num, uint8_t bus_size)
{
MMC_HandleTypeDef* hmmc;
bsp_status_t status;
Expand All @@ -197,7 +113,7 @@ bsp_status_t bsp_mmc_change_bus_width(bsp_dev_mmc_t dev_num, uint8_t bus_size)
return status;
}

bsp_status_t bsp_mmc_read_extcsd(bsp_dev_mmc_t dev_num, uint8_t * extcsd)
bsp_status_t bsp_mmc_read_extcsd(bsp_dev_sdio_t dev_num, uint8_t * extcsd)
{
MMC_HandleTypeDef* hmmc;
hmmc = &mmc_handle[dev_num];
Expand Down Expand Up @@ -256,7 +172,7 @@ bsp_status_t bsp_mmc_read_extcsd(bsp_dev_mmc_t dev_num, uint8_t * extcsd)
return BSP_OK;
}

bsp_status_t bsp_mmc_get_info(bsp_dev_mmc_t dev_num, bsp_mmc_info_t * mmc_info)
bsp_status_t bsp_mmc_get_info(bsp_dev_sdio_t dev_num, bsp_mmc_info_t * mmc_info)
{
MMC_HandleTypeDef* hmmc;

Expand All @@ -274,7 +190,7 @@ bsp_status_t bsp_mmc_get_info(bsp_dev_mmc_t dev_num, bsp_mmc_info_t * mmc_info)
* @param nb_data: Number of data to send.
* @retval status of the transfer.
*/
bsp_status_t bsp_mmc_write_block(bsp_dev_mmc_t dev_num, uint8_t* tx_data, uint32_t block_number)
bsp_status_t bsp_mmc_write_block(bsp_dev_sdio_t dev_num, uint8_t* tx_data, uint32_t block_number)
{
MMC_HandleTypeDef* hmmc;
bsp_status_t status;
Expand All @@ -293,7 +209,7 @@ bsp_status_t bsp_mmc_write_block(bsp_dev_mmc_t dev_num, uint8_t* tx_data, uint32
* @param nb_data: Number of data to receive.
* @retval status of the transfer.
*/
bsp_status_t bsp_mmc_read_block(bsp_dev_mmc_t dev_num, uint8_t* rx_data, uint32_t block_number)
bsp_status_t bsp_mmc_read_block(bsp_dev_sdio_t dev_num, uint8_t* rx_data, uint32_t block_number)
{
MMC_HandleTypeDef* hmmc;
bsp_status_t status;
Expand Down
30 changes: 12 additions & 18 deletions src/drv/stm32cube/bsp_mmc.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
HydraBus/HydraNFC - Copyright (C) 2014-2015 Benjamin VERNOUX
HydraBus/HydraNFC - Copyright (C) 2014-2023 Benjamin VERNOUX
HydraBus/HydraNFC - Copyright (C) 2014-2023 Nicolas OBERLI
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -16,15 +17,9 @@ limitations under the License.
#define _BSP_MMC_H_

#include "bsp.h"
#include "bsp_sdio.h"
#include "mode_config.h"

#define BSP_MMC_BLOCK_LEN 512

typedef enum {
BSP_DEV_MMC1 = 0,
BSP_DEV_MMC_END = 1
} bsp_dev_mmc_t;

typedef struct bsp_mmc_info_t {
uint32_t CardType; /*!< Specifies the card Type */
uint32_t Class; /*!< Specifies the class of the card class */
Expand All @@ -35,16 +30,15 @@ typedef struct bsp_mmc_info_t {
uint32_t LogBlockSize; /*!< Specifies logical block size in bytes */
} bsp_mmc_info_t;

bsp_status_t bsp_mmc_init(bsp_dev_sdio_t dev_num, mode_config_proto_t* mode_conf);
bsp_status_t bsp_mmc_deinit(bsp_dev_sdio_t dev_num);

bsp_status_t bsp_mmc_init(bsp_dev_mmc_t dev_num, mode_config_proto_t* mode_conf);
bsp_status_t bsp_mmc_deinit(bsp_dev_mmc_t dev_num);

bsp_status_t bsp_mmc_write_block(bsp_dev_mmc_t dev_num, uint8_t* tx_data, uint32_t block_number);
bsp_status_t bsp_mmc_read_block(bsp_dev_mmc_t dev_num, uint8_t* rx_data, uint32_t block_number);
uint32_t *bsp_mmc_get_cid(bsp_dev_mmc_t dev_num);
uint32_t *bsp_mmc_get_csd(bsp_dev_mmc_t dev_num);
bsp_status_t bsp_mmc_get_info(bsp_dev_mmc_t dev_num, bsp_mmc_info_t * mmc_info);
bsp_status_t bsp_mmc_change_bus_width(bsp_dev_mmc_t dev_num, uint8_t bus_size);
bsp_status_t bsp_mmc_read_extcsd(bsp_dev_mmc_t dev_num, uint8_t * extcsd);
bsp_status_t bsp_mmc_write_block(bsp_dev_sdio_t dev_num, uint8_t* tx_data, uint32_t block_number);
bsp_status_t bsp_mmc_read_block(bsp_dev_sdio_t dev_num, uint8_t* rx_data, uint32_t block_number);
uint32_t *bsp_mmc_get_cid(bsp_dev_sdio_t dev_num);
uint32_t *bsp_mmc_get_csd(bsp_dev_sdio_t dev_num);
bsp_status_t bsp_mmc_get_info(bsp_dev_sdio_t dev_num, bsp_mmc_info_t * mmc_info);
bsp_status_t bsp_mmc_change_bus_width(bsp_dev_sdio_t dev_num, uint8_t bus_size);
bsp_status_t bsp_mmc_read_extcsd(bsp_dev_sdio_t dev_num, uint8_t * extcsd);

#endif /* _BSP_MMC_H_ */
33 changes: 0 additions & 33 deletions src/drv/stm32cube/bsp_mmc_conf.h

This file was deleted.

Loading

0 comments on commit 28deb4f

Please sign in to comment.