rt-thread/bsp/gd32450z-eval/drivers/drv_spi_flash.c

86 lines
2.4 KiB
C
Raw Normal View History

2017-08-29 19:30:21 +08:00
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
2017-08-29 19:30:21 +08:00
*
* SPDX-License-Identifier: Apache-2.0
2017-08-29 19:30:21 +08:00
*
* Change Logs:
* Date Author Notes
* 2012-01-01 aozima first implementation.
* 2012-07-27 aozima fixed variable uninitialized.
*/
#include <board.h>
#include "drv_spi.h"
#include "spi_flash.h"
#ifdef RT_USING_SFUD
#include "spi_flash_sfud.h"
#endif
#include <rthw.h>
#include <finsh.h>
#define SPI_PERIPH SPI5
#define SPI_BUS_NAME "spi5"
#define SPI_FLASH_DEVICE_NAME "spi50"
#define SPI_FLASH_CHIP "gd25q16"
static int rt_hw_spi5_init(void)
{
/* register spi bus */
{
2021-03-12 00:03:36 +08:00
rt_err_t result;
2017-08-29 19:30:21 +08:00
rcu_periph_clock_enable(RCU_GPIOG);
rcu_periph_clock_enable(RCU_SPI5);
/* SPI5_CLK(PG13), SPI5_MISO(PG12), SPI5_MOSI(PG14),SPI5_IO2(PG10) and SPI5_IO3(PG11) GPIO pin configuration */
gpio_af_set(GPIOG, GPIO_AF_5, GPIO_PIN_10|GPIO_PIN_11| GPIO_PIN_12|GPIO_PIN_13| GPIO_PIN_14);
gpio_mode_set(GPIOG, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_10|GPIO_PIN_11| GPIO_PIN_12|GPIO_PIN_13| GPIO_PIN_14);
gpio_output_options_set(GPIOG, GPIO_OTYPE_PP, GPIO_OSPEED_200MHZ, GPIO_PIN_10|GPIO_PIN_11| GPIO_PIN_12|GPIO_PIN_13| GPIO_PIN_14);
2021-03-12 00:03:36 +08:00
result = gd32_spi_bus_register(SPI5, SPI_BUS_NAME);
2017-08-29 19:30:21 +08:00
if (result != RT_EOK)
2021-03-12 00:03:36 +08:00
{
return result;
}
2017-08-29 19:30:21 +08:00
}
/* attach cs */
{
static struct rt_spi_device spi_device;
2017-08-30 15:30:38 +08:00
static struct gd32_spi_cs spi_cs;
2021-03-12 00:03:36 +08:00
rt_err_t result;
2017-08-29 19:30:21 +08:00
spi_cs.GPIOx = GPIOG;
spi_cs.GPIO_Pin = GPIO_PIN_9;
2021-03-12 00:03:36 +08:00
2017-08-29 19:30:21 +08:00
/* SPI5_CS(PG9) GPIO pin configuration */
gpio_mode_set(GPIOG, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_9);
gpio_output_options_set(GPIOG, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
2021-03-12 00:03:36 +08:00
2017-08-29 19:30:21 +08:00
gpio_bit_set(GPIOG,GPIO_PIN_9);
result = rt_spi_bus_attach_device(&spi_device, SPI_FLASH_DEVICE_NAME, SPI_BUS_NAME, (void*)&spi_cs);
2021-03-12 00:03:36 +08:00
if (result != RT_EOK)
{
return result;
}
2017-08-29 19:30:21 +08:00
}
2021-03-12 00:03:36 +08:00
return RT_EOK;
2017-08-29 19:30:21 +08:00
}
INIT_DEVICE_EXPORT(rt_hw_spi5_init);
#ifdef RT_USING_SFUD
static int rt_hw_spi_flash_with_sfud_init(void)
{
if (RT_NULL == rt_sfud_flash_probe(SPI_FLASH_CHIP, SPI_FLASH_DEVICE_NAME))
{
return -RT_ERROR;
2017-08-29 19:30:21 +08:00
};
2021-03-12 00:03:36 +08:00
return RT_EOK;
2017-08-29 19:30:21 +08:00
}
INIT_COMPONENT_EXPORT(rt_hw_spi_flash_with_sfud_init)
#endif