[Bsp][stm32f4xx-HAL]剔除多余空行格式化

This commit is contained in:
zyh 2018-04-18 09:47:49 +08:00
parent a716028850
commit 1d48f9ff4b
22 changed files with 548 additions and 784 deletions

View File

@ -12,14 +12,11 @@
* 2009-09-22 Bernard add board.h to this bsp
* 2017-12-29 ZYH Correctly generate the 48M clock
*/
#include <rtthread.h>
#include "board.h"
/**
* @addtogroup STM32
*/
/*@{*/
#ifdef RT_USING_HSI
#error Can not using HSI on this bsp
@ -28,6 +25,7 @@
|| defined(RCC_PERIPHCLK_SPDIFRX) || defined(RCC_PERIPHCLK_FMPI2C1) || defined(RCC_PERIPHCLK_LPTIM1)
#warning Please give priority to the correctness of the clock tree when the peripherals are abnormal
#endif
static void SystemClock_Config(void)
{
rt_uint32_t hse_clk, sys_clk;
@ -44,9 +42,7 @@ static void SystemClock_Config(void)
/**Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
@ -111,14 +107,12 @@ static void SystemClock_Config(void)
while (1)
{}
}
/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
#if (RT_HSE_HCLK <= 42000000UL)
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
@ -169,6 +163,7 @@ static void SystemClock_Config(void)
}
#endif
}
/**
* This is the timer interrupt service routine.
*
@ -179,9 +174,7 @@ void SysTick_Handler(void)
rt_interrupt_enter();
/* tick for HAL Library */
HAL_IncTick();
rt_tick_increase();
/* leave interrupt */
rt_interrupt_leave();
}
@ -191,10 +184,8 @@ HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
{
/*Configure the SysTick to have interrupt in 1ms time basis*/
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / RT_TICK_PER_SECOND);
/*Configure the SysTick IRQ priority */
HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority, 0);
/* Return function status */
return HAL_OK;
}
@ -213,11 +204,10 @@ void HAL_ResumeTick(void)
{
/* we should not resume tick */
}
void HAL_MspInit(void)
{
HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
/* System interrupt init*/
/* MemoryManagement_IRQn interrupt configuration */
HAL_NVIC_SetPriority(MemoryManagement_IRQn, 0, 0);
@ -240,26 +230,16 @@ void HAL_MspInit(void)
*/
void rt_hw_board_init()
{
/* Configure the system clock @ 84 Mhz */
SystemClock_Config();
HAL_Init();
#ifdef RT_USING_HEAP
rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
#endif
#ifdef RT_USING_COMPONENTS_INIT
rt_components_board_init();
#endif
#ifdef RT_USING_CONSOLE
rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
#endif
}
/*@}*/

View File

@ -11,12 +11,9 @@
* Date Author Notes
* 2009-09-22 Bernard add board.h to this bsp
*/
// <<< Use Configuration Wizard in Context Menu >>>
#ifndef __BOARD_H__
#define __BOARD_H__
#include <stm32f4xx.h>
#include <stm32f4xx_hal.h>
#include <rtthread.h>
@ -171,7 +168,6 @@
defined(SOC_STM32F479NG)
#define STM32F4xx_PIN_NUMBERS 216
#endif
#if \
defined(SOC_STM32F405RG)||\
defined(SOC_STM32F405VG)||\
@ -372,7 +368,6 @@
#define STM32_SRAM_SIZE 320
#endif
#define STM32_SRAM_END (0x20000000 + STM32_SRAM_SIZE * 1024)
#ifdef __CC_ARM
@ -385,8 +380,8 @@ extern int Image$$RW_IRAM1$$ZI$$Limit;
extern int __bss_end;
#define HEAP_BEGIN ((void *)&__bss_end)
#endif
#define HEAP_END STM32_SRAM_END
#endif
#define HEAP_END STM32_SRAM_END
extern void rt_hw_board_init(void);
// <<< Use Configuration Wizard in Context Menu >>>
#endif

View File

@ -11,13 +11,10 @@
* Date Author Notes
* 2017-10-20 ZYH the first version
*/
#include <rthw.h>
#include <rtdevice.h>
#include <board.h>
#ifdef RT_USING_PIN
#define __STM32_PIN(index, gpio, gpio_index) {index, GPIO##gpio##_CLK_ENABLE, GPIO##gpio, GPIO_PIN_##gpio_index}
#define __STM32_PIN_DEFAULT {-1, 0, 0, 0}
@ -27,20 +24,22 @@ static void GPIOA_CLK_ENABLE(void)
__HAL_RCC_GPIOA_CLK_ENABLE();
#endif
}
static void GPIOB_CLK_ENABLE(void)
{
#ifdef __HAL_RCC_GPIOB_CLK_ENABLE
__HAL_RCC_GPIOB_CLK_ENABLE();
#endif
}
static void GPIOC_CLK_ENABLE(void)
{
#ifdef __HAL_RCC_GPIOC_CLK_ENABLE
__HAL_RCC_GPIOC_CLK_ENABLE();
#endif
}
#if (STM32F4xx_PIN_NUMBERS > 48)
#if (STM32F4xx_PIN_NUMBERS > 48)
static void GPIOD_CLK_ENABLE(void)
{
#ifdef __HAL_RCC_GPIOD_CLK_ENABLE
@ -106,7 +105,6 @@ struct pin_index
GPIO_TypeDef *gpio;
uint32_t pin;
};
static const struct pin_index pins[] =
{
#if (STM32F4xx_PIN_NUMBERS == 36)
@ -1495,6 +1493,7 @@ struct pin_irq_map
rt_uint16_t pinbit;
IRQn_Type irqno;
};
static const struct pin_irq_map pin_irq_map[] =
{
{GPIO_PIN_0, EXTI0_IRQn},
@ -1514,6 +1513,7 @@ static const struct pin_irq_map pin_irq_map[] =
{GPIO_PIN_14, EXTI15_10_IRQn},
{GPIO_PIN_15, EXTI15_10_IRQn},
};
struct rt_pin_irq_hdr pin_irq_hdr_tab[] =
{
{-1, 0, RT_NULL, RT_NULL},
@ -1538,7 +1538,6 @@ struct rt_pin_irq_hdr pin_irq_hdr_tab[] =
const struct pin_index *get_pin(uint8_t pin)
{
const struct pin_index *index;
if (pin < ITEM_NUM(pins))
{
index = &pins[pin];
@ -1549,20 +1548,17 @@ const struct pin_index *get_pin(uint8_t pin)
{
index = RT_NULL;
}
return index;
};
void stm32_pin_write(rt_device_t dev, rt_base_t pin, rt_base_t value)
{
const struct pin_index *index;
index = get_pin(pin);
if (index == RT_NULL)
{
return;
}
HAL_GPIO_WritePin(index->gpio, index->pin, (GPIO_PinState)value);
}
@ -1570,17 +1566,13 @@ int stm32_pin_read(rt_device_t dev, rt_base_t pin)
{
int value;
const struct pin_index *index;
value = PIN_LOW;
index = get_pin(pin);
if (index == RT_NULL)
{
return value;
}
value = HAL_GPIO_ReadPin(index->gpio, index->pin);
return value;
}
@ -1588,22 +1580,18 @@ void stm32_pin_mode(rt_device_t dev, rt_base_t pin, rt_base_t mode)
{
const struct pin_index *index;
GPIO_InitTypeDef GPIO_InitStruct;
index = get_pin(pin);
if (index == RT_NULL)
{
return;
}
/* GPIO Periph clock enable */
index->rcc();
/* Configure GPIO_InitStructure */
GPIO_InitStruct.Pin = index->pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
if (mode == PIN_MODE_OUTPUT)
{
/* output setting */
@ -1634,9 +1622,9 @@ void stm32_pin_mode(rt_device_t dev, rt_base_t pin, rt_base_t mode)
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
GPIO_InitStruct.Pull = GPIO_NOPULL;
}
HAL_GPIO_Init(index->gpio, &GPIO_InitStruct);
}
rt_inline rt_int32_t bit2bitno(rt_uint32_t bit)
{
int i;
@ -1649,6 +1637,7 @@ rt_inline rt_int32_t bit2bitno(rt_uint32_t bit)
}
return -1;
}
rt_inline const struct pin_irq_map *get_pin_irq_map(uint32_t pinbit)
{
rt_int32_t mapindex = bit2bitno(pinbit);
@ -1658,13 +1647,13 @@ rt_inline const struct pin_irq_map *get_pin_irq_map(uint32_t pinbit)
}
return &pin_irq_map[mapindex];
};
rt_err_t stm32_pin_attach_irq(struct rt_device *device, rt_int32_t pin,
rt_uint32_t mode, void (*hdr)(void *args), void *args)
{
const struct pin_index *index;
rt_base_t level;
rt_int32_t irqindex = -1;
index = get_pin(pin);
if (index == RT_NULL)
{
@ -1675,7 +1664,6 @@ rt_err_t stm32_pin_attach_irq(struct rt_device *device, rt_int32_t pin,
{
return RT_ENOSYS;
}
level = rt_hw_interrupt_disable();
if (pin_irq_hdr_tab[irqindex].pin == pin &&
pin_irq_hdr_tab[irqindex].hdr == hdr &&
@ -1695,15 +1683,14 @@ rt_err_t stm32_pin_attach_irq(struct rt_device *device, rt_int32_t pin,
pin_irq_hdr_tab[irqindex].mode = mode;
pin_irq_hdr_tab[irqindex].args = args;
rt_hw_interrupt_enable(level);
return RT_EOK;
}
rt_err_t stm32_pin_dettach_irq(struct rt_device *device, rt_int32_t pin)
{
const struct pin_index *index;
rt_base_t level;
rt_int32_t irqindex = -1;
index = get_pin(pin);
if (index == RT_NULL)
{
@ -1714,7 +1701,6 @@ rt_err_t stm32_pin_dettach_irq(struct rt_device *device, rt_int32_t pin)
{
return RT_ENOSYS;
}
level = rt_hw_interrupt_disable();
if (pin_irq_hdr_tab[irqindex].pin == -1)
{
@ -1726,9 +1712,9 @@ rt_err_t stm32_pin_dettach_irq(struct rt_device *device, rt_int32_t pin)
pin_irq_hdr_tab[irqindex].mode = 0;
pin_irq_hdr_tab[irqindex].args = RT_NULL;
rt_hw_interrupt_enable(level);
return RT_EOK;
}
rt_err_t stm32_pin_irq_enable(struct rt_device *device, rt_base_t pin,
rt_uint32_t enabled)
{
@ -1737,7 +1723,6 @@ rt_err_t stm32_pin_irq_enable(struct rt_device *device, rt_base_t pin,
rt_base_t level;
rt_int32_t irqindex = -1;
GPIO_InitTypeDef GPIO_InitStruct;
index = get_pin(pin);
if (index == RT_NULL)
{
@ -1793,9 +1778,9 @@ rt_err_t stm32_pin_irq_enable(struct rt_device *device, rt_base_t pin,
{
return RT_ENOSYS;
}
return RT_EOK;
}
const static struct rt_pin_ops _stm32_pin_ops =
{
stm32_pin_mode,
@ -1809,7 +1794,6 @@ const static struct rt_pin_ops _stm32_pin_ops =
int rt_hw_pin_init(void)
{
int result;
result = rt_device_pin_register("pin", &_stm32_pin_ops, RT_NULL);
return result;
}
@ -1827,36 +1811,42 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
pin_irq_hdr(bit2bitno(GPIO_Pin));
}
void EXTI0_IRQHandler(void)
{
rt_interrupt_enter();
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0);
rt_interrupt_leave();
}
void EXTI1_IRQHandler(void)
{
rt_interrupt_enter();
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_1);
rt_interrupt_leave();
}
void EXTI2_IRQHandler(void)
{
rt_interrupt_enter();
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_2);
rt_interrupt_leave();
}
void EXTI3_IRQHandler(void)
{
rt_interrupt_enter();
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_3);
rt_interrupt_leave();
}
void EXTI4_IRQHandler(void)
{
rt_interrupt_enter();
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_4);
rt_interrupt_leave();
}
void EXTI9_5_IRQHandler(void)
{
rt_interrupt_enter();
@ -1867,6 +1857,7 @@ void EXTI9_5_IRQHandler(void)
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_9);
rt_interrupt_leave();
}
void EXTI15_10_IRQHandler(void)
{
rt_interrupt_enter();
@ -1878,5 +1869,4 @@ void EXTI15_10_IRQHandler(void)
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_15);
rt_interrupt_leave();
}
#endif

View File

@ -13,18 +13,12 @@
*/
#ifndef GPIO_H__
#define GPIO_H__
struct stm32_hw_pin_userdata
{
int pin;
uint32_t mode;
};
#define PIN_USERDATA_END {-1,0}
extern struct stm32_hw_pin_userdata stm32_pins[];
int rt_hw_pin_init(void);
#endif

View File

@ -22,17 +22,13 @@
* 2017-06-05 tanek first implementation.
* 2018-03-08 ZYH Porting for stm32f4xx
*/
#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#include "drv_i2c.h"
#include <board.h>
/*user can change this*/
#define I2C_BUS_NAME "i2c2"
/*user should change this to adapt specific board*/
#define I2C_SCL_PIN GPIO_PIN_6
#define I2C_SCL_PORT GPIOB
@ -41,26 +37,21 @@
#define I2C_SDA_PORT GPIOB
#define I2C_SDA_PORT_CLK_ENABLE __HAL_RCC_GPIOB_CLK_ENABLE
static void drv_i2c_gpio_init()
{
GPIO_InitTypeDef GPIO_Initure;
I2C_SCL_PORT_CLK_ENABLE();
I2C_SDA_PORT_CLK_ENABLE();
GPIO_Initure.Pin = I2C_SCL_PIN;
GPIO_Initure.Mode = GPIO_MODE_OUTPUT_OD;
GPIO_Initure.Pull = GPIO_PULLUP;
GPIO_Initure.Speed = GPIO_SPEED_HIGH;
HAL_GPIO_Init(I2C_SCL_PORT, &GPIO_Initure);
GPIO_Initure.Pin = I2C_SDA_PIN;
GPIO_Initure.Mode = GPIO_MODE_OUTPUT_OD;
GPIO_Initure.Pull = GPIO_PULLUP;
GPIO_Initure.Speed = GPIO_SPEED_HIGH;
HAL_GPIO_Init(I2C_SDA_PORT, &GPIO_Initure);
HAL_GPIO_WritePin(I2C_SCL_PORT, I2C_SCL_PIN, GPIO_PIN_SET);
HAL_GPIO_WritePin(I2C_SDA_PORT, I2C_SDA_PIN, GPIO_PIN_SET);
}
@ -85,7 +76,6 @@ static rt_int32_t drv_get_scl(void *data)
return HAL_GPIO_ReadPin(I2C_SCL_PORT, I2C_SCL_PIN) ? 1 : 0;
}
static void drv_udelay(rt_uint32_t us)
{
int i = (HAL_RCC_GetHCLKFreq() / 4000000 * us);
@ -110,15 +100,10 @@ static const struct rt_i2c_bit_ops drv_bit_ops =
int drv_i2c_init(void)
{
static struct rt_i2c_bus_device i2c2_bus;
drv_i2c_gpio_init();
rt_memset((void *)&i2c2_bus, 0, sizeof(struct rt_i2c_bus_device));
i2c2_bus.priv = (void *)&drv_bit_ops;
rt_i2c_bit_add_bus(&i2c2_bus, I2C_BUS_NAME);
return RT_EOK;
}
INIT_DEVICE_EXPORT(drv_i2c_init);

View File

@ -21,10 +21,7 @@
* Date Author Notes
* 2017-06-05 tanek first implementation.
*/
#ifndef __DRV_I2C__
#define __DRV_I2C__
int hw_i2c_init(void);
#endif

View File

@ -30,6 +30,7 @@ static rt_err_t drv_init(rt_watchdog_t *wdt)
}
return RT_EOK;
}
static rt_err_t drv_control(rt_watchdog_t *wdt, int cmd, void *arg)
{
switch (cmd)
@ -49,28 +50,21 @@ static rt_err_t drv_control(rt_watchdog_t *wdt, int cmd, void *arg)
}
return RT_EOK;
}
static struct rt_watchdog_ops _ops =
{
drv_init,
drv_control
};
static rt_watchdog_t _iwg =
{
.ops = &_ops
};
int rt_iwg_init(void)
{
return rt_hw_watchdog_register(&_iwg, "iwg", RT_DEVICE_FLAG_DEACTIVATE, RT_NULL);
}
INIT_BOARD_EXPORT(rt_iwg_init);
#endif

View File

@ -14,15 +14,4 @@
#ifndef __DRV_IWG_H__
#define __DRV_IWG_H__
extern int rt_iwg_init(void);
#endif

View File

@ -27,13 +27,11 @@
#include <string.h>
#include <time.h>
RTC_HandleTypeDef hrtc;
/* RTC init function */
void MX_RTC_Init(void)
{
RTC_TimeTypeDef sTime;
RTC_DateTypeDef sDate;
/**Initialize RTC Only
*/
hrtc.Instance = RTC;
@ -54,51 +52,41 @@ void MX_RTC_Init(void)
sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
sTime.StoreOperation = RTC_STOREOPERATION_RESET;
RT_ASSERT(HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) == HAL_OK);
sDate.WeekDay = RTC_WEEKDAY_THURSDAY;
sDate.Month = RTC_MONTH_OCTOBER;
sDate.Date = 26;
sDate.Year = 17;
RT_ASSERT(HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) == HAL_OK);
}
HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR0, 0x32F2);
}
void HAL_RTC_MspInit(RTC_HandleTypeDef *rtcHandle)
{
if (rtcHandle->Instance == RTC)
{
/* USER CODE BEGIN RTC_MspInit 0 */
/* USER CODE END RTC_MspInit 0 */
/* RTC clock enable */
__HAL_RCC_RTC_ENABLE();
/* USER CODE BEGIN RTC_MspInit 1 */
/* USER CODE END RTC_MspInit 1 */
}
}
void HAL_RTC_MspDeInit(RTC_HandleTypeDef *rtcHandle)
{
if (rtcHandle->Instance == RTC)
{
/* USER CODE BEGIN RTC_MspDeInit 0 */
/* USER CODE END RTC_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_RTC_DISABLE();
/* USER CODE BEGIN RTC_MspDeInit 1 */
/* USER CODE END RTC_MspDeInit 1 */
}
}
static rt_err_t stm32_rtc_control(struct rt_device *dev,
int cmd,
void *args)
@ -141,18 +129,22 @@ static rt_err_t stm32_rtc_control(struct rt_device *dev,
}
return RT_EOK;
}
static rt_err_t stm32_rtc_init(struct rt_device *dev)
{
return RT_EOK;
}
static rt_err_t stm32_rtc_open(struct rt_device *dev, rt_uint16_t oflag)
{
return RT_EOK;
}
static rt_err_t stm32_rtc_close(struct rt_device *dev)
{
return RT_EOK;
}
static rt_size_t stm32_rtc_read(struct rt_device *dev,
rt_off_t pos,
void *buffer,
@ -170,16 +162,14 @@ static rt_size_t stm32_rtc_write(struct rt_device *dev,
stm32_rtc_control(dev, RT_DEVICE_CTRL_RTC_SET_TIME, (void *)buffer);
return size;
}
struct rt_device rtc_device;
struct rt_device rtc_device;
int rt_hw_rtc_init(void)
{
MX_RTC_Init();
rtc_device.type = RT_Device_Class_RTC;
rtc_device.rx_indicate = RT_NULL;
rtc_device.tx_complete = RT_NULL;
rtc_device.init = stm32_rtc_init;
rtc_device.open = stm32_rtc_open;
rtc_device.close = stm32_rtc_close;
@ -187,20 +177,7 @@ int rt_hw_rtc_init(void)
rtc_device.write = stm32_rtc_write;
rtc_device.control = stm32_rtc_control;
rtc_device.user_data = RT_NULL;
/* register a character device */
return rt_device_register(&rtc_device, "rtc", RT_DEVICE_FLAG_DEACTIVATE);
}
INIT_BOARD_EXPORT(rt_hw_rtc_init);

View File

@ -11,37 +11,30 @@
* Date Author Notes
* 2017-11-08 ZYH the first version
*/
#include "board.h"
#include <rtthread.h>
#include <rtdevice.h>
#include <rthw.h>
#ifdef RT_USING_SPI
#define SPIRXEVENT 0x01
#define SPITXEVENT 0x02
#define SPITIMEOUT 2
#define SPICRCEN 0
struct stm32_hw_spi_cs
{
rt_uint32_t pin;
};
struct stm32_spi
{
SPI_TypeDef *Instance;
struct rt_spi_configuration *cfg;
};
static rt_err_t stm32_spi_init(SPI_TypeDef *spix, struct rt_spi_configuration *cfg)
{
SPI_HandleTypeDef hspi;
hspi.Instance = spix;
if (cfg->mode & RT_SPI_SLAVE)
{
hspi.Init.Mode = SPI_MODE_SLAVE;
@ -147,6 +140,7 @@ static rt_err_t stm32_spi_init(SPI_TypeDef *spix, struct rt_spi_configuration *c
__HAL_SPI_ENABLE(&hspi);
return RT_EOK;
}
#define SPISTEP(datalen) (((datalen) == 8) ? 1 : 2)
#define SPISEND_1(reg, ptr, datalen) \
do \
@ -197,6 +191,7 @@ static rt_err_t spitxrx1b(struct stm32_spi *hspi, void *rcvb, const void *sndb)
SPIRECV_1(hspi->Instance->DR, rcvb, hspi->cfg->data_width);
return RT_EOK;
}
static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
{
rt_err_t res;
@ -205,7 +200,6 @@ static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *
RT_ASSERT(device->bus->parent.user_data != RT_NULL);
struct stm32_spi *hspi = (struct stm32_spi *)device->bus->parent.user_data;
struct stm32_hw_spi_cs *cs = device->parent.user_data;
if (message->cs_take)
{
rt_pin_write(cs->pin, 0);
@ -240,7 +234,6 @@ static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *
return message->length - length;
}
rt_err_t spi_configure(struct rt_spi_device *device,
struct rt_spi_configuration *configuration)
{
@ -248,7 +241,6 @@ rt_err_t spi_configure(struct rt_spi_device *device,
hspi->cfg = configuration;
return stm32_spi_init(hspi->Instance, configuration);
}
const struct rt_spi_ops stm_spi_ops =
{
.configure = spi_configure,
@ -265,15 +257,18 @@ int stm32_spi_register_bus(SPI_TypeDef * SPIx,const char * name)
{
spi_bus = &_spi_bus1;
spi = &_spi1;
}else if(SPIx == SPI2)
}
else if (SPIx == SPI2)
{
spi_bus = &_spi_bus2;
spi = &_spi2;
}
else if(SPIx == SPI3){
else if (SPIx == SPI3)
{
spi_bus = &_spi_bus3;
spi = &_spi3;
}else
}
else
{
return -1;
}
@ -281,6 +276,7 @@ int stm32_spi_register_bus(SPI_TypeDef * SPIx,const char * name)
spi_bus->parent.user_data = spi;
return rt_spi_bus_register(spi_bus, name, &stm_spi_ops);
}
//cannot be used before completion init
rt_err_t stm32_spi_bus_attach_device(rt_uint32_t pin, const char *bus_name, const char *device_name)
{
@ -312,7 +308,6 @@ INIT_BOARD_EXPORT(stm32_hw_spi_init);
void HAL_SPI_MspInit(SPI_HandleTypeDef *spiHandle)
{
GPIO_InitTypeDef GPIO_InitStruct;
if (spiHandle->Instance == SPI1)
{
@ -369,12 +364,10 @@ void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle)
void HAL_SPI_MspDeInit(SPI_HandleTypeDef *spiHandle)
{
if (spiHandle->Instance == SPI1)
{
/* Peripheral clock disable */
__HAL_RCC_SPI1_CLK_DISABLE();
/**SPI1 GPIO Configuration
PA5 ------> SPI1_SCK
PA6 ------> SPI1_MISO
@ -386,7 +379,6 @@ void HAL_SPI_MspDeInit(SPI_HandleTypeDef* spiHandle)
{
/* Peripheral clock disable */
__HAL_RCC_SPI2_CLK_DISABLE();
/**SPI2 GPIO Configuration
PB13 ------> SPI2_SCK
PB14 ------> SPI2_MISO
@ -398,7 +390,6 @@ void HAL_SPI_MspDeInit(SPI_HandleTypeDef* spiHandle)
{
/* Peripheral clock disable */
__HAL_RCC_SPI3_CLK_DISABLE();
/**SPI3 GPIO Configuration
PC10 ------> SPI3_SCK
PC11 ------> SPI3_MISO
@ -408,34 +399,3 @@ void HAL_SPI_MspDeInit(SPI_HandleTypeDef* spiHandle)
}
}
#endif /*RT_USING_SPI*/

View File

@ -11,27 +11,12 @@
* Date Author Notes
* 2017-10-20 ZYH the first version
*/
#ifndef __STM32_SPI_H_
#define __STM32_SPI_H_
#include <rtthread.h>
#include <rthw.h>
#include <rtdevice.h>
//cannot be used before completion init
extern rt_err_t stm32_spi_bus_attach_device(rt_uint32_t pin, const char *bus_name, const char *device_name);
extern int stm32_hw_spi_init(void);
#endif

View File

@ -21,13 +21,4 @@ int rt_w25qxx_init(void)
return w25qxx_init("flash0", "w25qxx");
}
INIT_DEVICE_EXPORT(rt_w25qxx_init);
#endif

View File

@ -15,14 +15,3 @@
#define __STM32_SPI_FLASH_H_
extern int rt_w25qxx_init(void);
#endif

View File

@ -24,13 +24,11 @@
* 2016-01-15 ArdaFu the first version for stm32f4xx with STM32 HAL
* 2016-01-15 zyh the first version for stm32f401rc with STM32 HAL
*/
#include "drv_usart.h"
#include "board.h"
#include <rtdevice.h>
#include <rthw.h>
#include <rtthread.h>
/* STM32 uart driver */
struct drv_uart
{
@ -42,17 +40,13 @@ static rt_err_t drv_configure(struct rt_serial_device *serial,
struct serial_configure *cfg)
{
struct drv_uart *uart;
RT_ASSERT(serial != RT_NULL);
RT_ASSERT(cfg != RT_NULL);
uart = (struct drv_uart *)serial->parent.user_data;
uart->UartHandle.Init.BaudRate = cfg->baud_rate;
uart->UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
uart->UartHandle.Init.Mode = UART_MODE_TX_RX;
uart->UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
switch (cfg->data_bits)
{
case DATA_BITS_8:
@ -92,12 +86,10 @@ struct serial_configure *cfg)
uart->UartHandle.Init.Parity = UART_PARITY_NONE;
break;
}
if (HAL_UART_Init(&uart->UartHandle) != HAL_OK)
{
return RT_ERROR;
}
return RT_EOK;
}
@ -105,10 +97,8 @@ static rt_err_t drv_control(struct rt_serial_device *serial,
int cmd, void *arg)
{
struct drv_uart *uart;
RT_ASSERT(serial != RT_NULL);
uart = (struct drv_uart *)serial->parent.user_data;
switch (cmd)
{
case RT_DEVICE_CTRL_CLR_INT:
@ -124,17 +114,14 @@ int cmd, void *arg)
__HAL_UART_ENABLE_IT(&uart->UartHandle, UART_IT_RXNE);
break;
}
return RT_EOK;
}
static int drv_putc(struct rt_serial_device *serial, char c)
{
struct drv_uart *uart;
RT_ASSERT(serial != RT_NULL);
uart = (struct drv_uart *)serial->parent.user_data;
while ((__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_TXE) == RESET));
uart->UartHandle.Instance->DR = c;
return 1;
@ -144,10 +131,8 @@ static int drv_getc(struct rt_serial_device *serial)
{
int ch;
struct drv_uart *uart;
RT_ASSERT(serial != RT_NULL);
uart = (struct drv_uart *)serial->parent.user_data;
ch = -1;
if (__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_RXNE) != RESET)
ch = uart->UartHandle.Instance->DR & 0xff;
@ -161,19 +146,17 @@ static const struct rt_uart_ops drv_uart_ops =
drv_putc,
drv_getc,
};
#if defined(RT_USING_UART1)
/* UART1 device driver structure */
static struct drv_uart uart1;
struct rt_serial_device serial1;
void USART1_IRQHandler(void)
{
struct drv_uart *uart;
uart = &uart1;
/* enter interrupt */
rt_interrupt_enter();
/* UART in mode Receiver -------------------------------------------------*/
if ((__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_RXNE) != RESET) &&
(__HAL_UART_GET_IT_SOURCE(&uart->UartHandle, UART_IT_RXNE) != RESET))
@ -185,22 +168,18 @@ void USART1_IRQHandler(void)
/* leave interrupt */
rt_interrupt_leave();
}
#endif /* RT_USING_UART1 */
#if defined(RT_USING_UART2)
/* UART2 device driver structure */
static struct drv_uart uart2;
struct rt_serial_device serial2;
void USART2_IRQHandler(void)
{
struct drv_uart *uart;
uart = &uart2;
/* enter interrupt */
rt_interrupt_enter();
/* UART in mode Receiver -------------------------------------------------*/
if ((__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_RXNE) != RESET) &&
(__HAL_UART_GET_IT_SOURCE(&uart->UartHandle, UART_IT_RXNE) != RESET))
@ -212,23 +191,18 @@ void USART2_IRQHandler(void)
/* leave interrupt */
rt_interrupt_leave();
}
#endif /* RT_USING_UART2 */
#if defined(RT_USING_UART6)
/* UART2 device driver structure */
static struct drv_uart uart6;
struct rt_serial_device serial6;
void USART6_IRQHandler(void)
{
struct drv_uart *uart;
uart = &uart6;
/* enter interrupt */
rt_interrupt_enter();
/* UART in mode Receiver -------------------------------------------------*/
if ((__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_RXNE) != RESET) &&
(__HAL_UART_GET_IT_SOURCE(&uart->UartHandle, UART_IT_RXNE) != RESET))
@ -240,7 +214,6 @@ void USART6_IRQHandler(void)
/* leave interrupt */
rt_interrupt_leave();
}
#endif /* RT_USING_UART3 */
/**
@ -254,7 +227,6 @@ void USART6_IRQHandler(void)
*/
void HAL_UART_MspInit(UART_HandleTypeDef *uartHandle)
{
GPIO_InitTypeDef GPIO_InitStruct;
if (uartHandle->Instance == USART1)
{
@ -308,12 +280,10 @@ void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
void HAL_UART_MspDeInit(UART_HandleTypeDef *uartHandle)
{
if (uartHandle->Instance == USART1)
{
/* Peripheral clock disable */
__HAL_RCC_USART1_CLK_DISABLE();
/**USART1 GPIO Configuration
PA9 ------> USART1_TX
PA10 ------> USART1_RX
@ -324,7 +294,6 @@ void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
{
/* Peripheral clock disable */
__HAL_RCC_USART2_CLK_DISABLE();
/**USART2 GPIO Configuration
PA2 ------> USART2_TX
PA3 ------> USART2_RX
@ -335,7 +304,6 @@ void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
{
/* Peripheral clock disable */
__HAL_RCC_USART6_CLK_DISABLE();
/**USART6 GPIO Configuration
PC6 ------> USART6_TX
PC7 ------> USART6_RX
@ -354,7 +322,6 @@ int hw_usart_init(void)
uart->irq = USART1_IRQn;
serial1.ops = &drv_uart_ops;
serial1.config = config;
/* register UART1 device */
rt_hw_serial_register(&serial1, "uart1",
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
@ -366,7 +333,6 @@ int hw_usart_init(void)
uart->irq = USART2_IRQn;
serial2.ops = &drv_uart_ops;
serial2.config = config;
/* register UART2 device */
rt_hw_serial_register(&serial2, "uart2",
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
@ -378,13 +344,11 @@ int hw_usart_init(void)
uart->irq = USART6_IRQn;
serial6.ops = &drv_uart_ops;
serial6.config = config;
/* register UART2 device */
rt_hw_serial_register(&serial6, "uart6",
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
uart);
#endif /* RT_USING_UART2 */
return 0;
}
INIT_BOARD_EXPORT(hw_usart_init);

View File

@ -13,9 +13,7 @@
* 2015-08-01 xiaonong the first version for stm32f7xx
* 2016-01-15 ArdaFu the first version for stm32f4xx with STM32 HAL
*/
#ifndef __USART_H__
#define __USART_H__
extern int hw_usart_init(void);
#endif

View File

@ -15,9 +15,7 @@
#include <rtthread.h>
#include <rtdevice.h>
#include "board.h"
//#define USB_DISCONNECT_PIN 30 //PA9
static PCD_HandleTypeDef _stm_pcd;
static struct udcd _stm_udc;
static struct ep_id _ep_pool[] =
@ -35,9 +33,7 @@ static struct ep_id _ep_pool[] =
void OTG_FS_IRQHandler(void)
{
rt_interrupt_enter();
HAL_PCD_IRQHandler(&_stm_pcd);
/* leave interrupt */
rt_interrupt_leave();
@ -56,7 +52,6 @@ void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
rt_usbd_ep0_setup_handler(&_stm_udc, (struct urequest *)hpcd->Setup);
}
void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
{
if (epnum == 0)
@ -96,7 +91,6 @@ void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
}
}
void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state)
{
if (state == 1)
@ -114,17 +108,14 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef* pcdHandle)
{
/* USER CODE BEGIN USB_MspInit 0 */
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_11 | GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* Peripheral clock enable */
__HAL_RCC_USB_OTG_FS_CLK_ENABLE();
/* Peripheral interrupt init */
HAL_NVIC_SetPriority(OTG_FS_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(OTG_FS_IRQn);
@ -137,13 +128,11 @@ void HAL_PCD_MspDeInit(PCD_HandleTypeDef* pcdHandle)
{
/* Peripheral clock disable */
__HAL_RCC_USB_OTG_FS_CLK_DISABLE();
/**USB_OTG_FS GPIO Configuration
PA11 ------> USB_OTG_FS_DM
PA12 ------> USB_OTG_FS_DP
*/
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11 | GPIO_PIN_12);
/* Peripheral interrupt Deinit*/
HAL_NVIC_DisableIRQ(OTG_FS_IRQn);
}
@ -178,7 +167,6 @@ static rt_err_t _ep_enable(uep_t ep)
RT_ASSERT(ep->ep_desc != RT_NULL);
HAL_PCD_EP_Open(&_stm_pcd, ep->ep_desc->bEndpointAddress,
ep->ep_desc->wMaxPacketSize, ep->ep_desc->bmAttributes);
return RT_EOK;
}
@ -193,9 +181,7 @@ static rt_err_t _ep_disable(uep_t ep)
static rt_size_t _ep_read(rt_uint8_t address, void *buffer)
{
rt_size_t size = 0;
RT_ASSERT(buffer != RT_NULL);
return size;
}
@ -230,10 +216,8 @@ static rt_err_t _wakeup(void)
static rt_err_t _init(rt_device_t device)
{
PCD_HandleTypeDef *pcd;
/* Set LL Driver parameters */
pcd = (PCD_HandleTypeDef *)device->user_data;
pcd->Instance = USB_OTG_FS;
pcd->Init.dev_endpoints = 4;
pcd->Init.speed = PCD_SPEED_FULL;
@ -245,17 +229,14 @@ static rt_err_t _init(rt_device_t device)
pcd->Init.lpm_enable = DISABLE;
pcd->Init.vbus_sensing_enable = DISABLE;
pcd->Init.use_dedicated_ep1 = DISABLE;
/* Initialize LL Driver */
HAL_PCD_Init(pcd);
HAL_PCDEx_SetRxFiFo(pcd, 0x80);
HAL_PCDEx_SetTxFiFo(pcd, 0, 0x40);
HAL_PCDEx_SetTxFiFo(pcd, 1, 0x40);
HAL_PCDEx_SetTxFiFo(pcd, 2, 0x40);
HAL_PCDEx_SetTxFiFo(pcd, 3, 0x40);
HAL_PCD_Start(pcd);
return RT_EOK;
}
@ -275,11 +256,9 @@ const static struct udcd_ops _udc_ops =
_wakeup,
};
int stm_usbd_register(void)
{
rt_memset((void *)&_stm_udc, 0, sizeof(struct udcd));
_stm_udc.parent.type = RT_Device_Class_USBDevice;
_stm_udc.parent.init = _init;
_stm_udc.parent.user_data = &_stm_pcd;
@ -287,10 +266,8 @@ int stm_usbd_register(void)
/* Register endpoint infomation */
_stm_udc.ep_pool = _ep_pool;
_stm_udc.ep0.id = &_ep_pool[0];
rt_device_register((rt_device_t)&_stm_udc, "usbd", 0);
rt_usb_device_init();
return RT_EOK;
}
INIT_DEVICE_EXPORT(stm_usbd_register);

View File

@ -14,7 +14,5 @@
#ifndef __STM32_USBD_H__
#define __STM32_USBD_H__
#include <rtthread.h>
int stm_usbd_register(void);
#endif

View File

@ -42,10 +42,8 @@ void HAL_HCD_MspInit(HCD_HandleTypeDef *hcdHandle)
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* Peripheral clock enable */
__HAL_RCC_USB_OTG_FS_CLK_ENABLE();
/* Peripheral interrupt init */
HAL_NVIC_SetPriority(OTG_FS_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(OTG_FS_IRQn);
@ -58,7 +56,6 @@ void HAL_HCD_MspDeInit(HCD_HandleTypeDef *hcdHandle)
{
/* Peripheral clock disable */
__HAL_RCC_USB_OTG_FS_CLK_DISABLE();
/**USB_OTG_FS GPIO Configuration
PA9 ------> USB_OTG_FS_VBUS
PA10 ------> USB_OTG_FS_ID
@ -69,21 +66,23 @@ void HAL_HCD_MspDeInit(HCD_HandleTypeDef *hcdHandle)
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9);
#endif
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_12 | GPIO_PIN_11);
/* Peripheral interrupt Deinit*/
HAL_NVIC_DisableIRQ(OTG_FS_IRQn);
}
}
void OTG_FS_IRQHandler(void)
{
rt_interrupt_enter();
HAL_HCD_IRQHandler(&_stm_hhcd_fs);
rt_interrupt_leave();
}
void HAL_HCD_SOF_Callback(HCD_HandleTypeDef *hhcd)//提供定时器
{
//rt_kprintf("sof callback\n");
}
static __IO rt_bool_t connect_status = RT_FALSE;
void HAL_HCD_Connect_Callback(HCD_HandleTypeDef *hhcd)
{
@ -95,6 +94,7 @@ void HAL_HCD_Connect_Callback(HCD_HandleTypeDef *hhcd)
rt_usbh_root_hub_connect_handler(hcd, OTG_FS_PORT, RT_FALSE);
}
}
void HAL_HCD_Disconnect_Callback(HCD_HandleTypeDef *hhcd)
{
uhcd_t hcd = (uhcd_t)hhcd->pData;
@ -105,11 +105,13 @@ void HAL_HCD_Disconnect_Callback(HCD_HandleTypeDef *hhcd)
rt_usbh_root_hub_disconnect_handler(hcd, OTG_FS_PORT);
}
}
void HAL_HCD_HC_NotifyURBChange_Callback(HCD_HandleTypeDef *hhcd, uint8_t chnum, HCD_URBStateTypeDef urb_state)
{
//rt_kprintf("NotifyURBChange_Callback\n");
rt_completion_done(&urb_completion);
}
static rt_err_t drv_reset_port(rt_uint8_t port)
{
RT_DEBUG_LOG(RT_DEBUG_USB, ("reset port\n"));
@ -186,6 +188,7 @@ static int drv_pipe_xfer(upipe_t pipe, rt_uint8_t token, void *buffer, int nbyte
return -1;
}
}
static rt_uint16_t pipe_index = 0;
static rt_uint8_t drv_get_free_pipe_index()
{
@ -200,14 +203,15 @@ static rt_uint8_t drv_get_free_pipe_index()
}
return 0xff;
}
static void drv_free_pipe_index(rt_uint8_t index)
{
pipe_index &= ~(0x01 << index);
}
static rt_err_t drv_open_pipe(upipe_t pipe)
{
pipe->pipe_index = drv_get_free_pipe_index();
HAL_HCD_HC_Init(&_stm_hhcd_fs,
pipe->pipe_index,
pipe->ep.bEndpointAddress,
@ -226,6 +230,7 @@ static rt_err_t drv_open_pipe(upipe_t pipe)
}
return RT_EOK;
}
static rt_err_t drv_close_pipe(upipe_t pipe)
{
HAL_HCD_HC_Halt(&_stm_hhcd_fs, pipe->pipe_index);
@ -254,24 +259,22 @@ static rt_err_t _init(rt_device_t device)
HAL_HCD_Start(hhcd);
#ifdef USBH_USING_CONTROLLABLE_POWER
rt_pin_mode(USBH_POWER_PIN, PIN_MODE_OUTPUT);
rt_pin_write(USBH_POWER_PIN, PIN_LOW);
#endif
return RT_EOK;
}
int stm_usbh_register(void)
{
uhcd_t uhcd = (uhcd_t)rt_malloc(sizeof(struct uhcd));
RT_ASSERT(uhcd != RT_NULL);
rt_memset((void *)uhcd, 0, sizeof(struct uhcd));
uhcd->parent.type = RT_Device_Class_USBHost;
uhcd->parent.init = _init;
uhcd->parent.user_data = &_stm_hhcd_fs;
uhcd->ops = &_uhcd_ops;
uhcd->num_ports = 1;
_stm_hhcd_fs.pData = uhcd;
rt_device_register((rt_device_t)uhcd, "usbh", 0);
rt_usb_host_init();
return RT_EOK;

View File

@ -14,7 +14,5 @@
#ifndef __STM32_USBH_H__
#define __STM32_USBH_H__
#include <rtthread.h>
int stm_usbh_register(void);
#endif