diff --git a/bsp/stm32f10x-HAL/applications/main.c b/bsp/stm32f10x-HAL/applications/main.c index ae855a5b1..0f5e6000a 100644 --- a/bsp/stm32f10x-HAL/applications/main.c +++ b/bsp/stm32f10x-HAL/applications/main.c @@ -15,6 +15,6 @@ int main(void) { - /* user app entry */ - return 0; + /* user app entry */ + return 0; } diff --git a/bsp/stm32f10x-HAL/drivers/board.c b/bsp/stm32f10x-HAL/drivers/board.c index 3ff64ed3d..d52041198 100644 --- a/bsp/stm32f10x-HAL/drivers/board.c +++ b/bsp/stm32f10x-HAL/drivers/board.c @@ -24,12 +24,9 @@ /*@{*/ - void HAL_MspInit(void) { - HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4); - /* System interrupt init*/ __HAL_RCC_AFIO_CLK_ENABLE(); /* MemoryManagement_IRQn interrupt configuration */ @@ -46,16 +43,14 @@ void HAL_MspInit(void) HAL_NVIC_SetPriority(PendSV_IRQn, 15, 0); /* SysTick_IRQn interrupt configuration */ HAL_NVIC_SetPriority(SysTick_IRQn, 15, 0); - /**DISABLE: JTAG-DP Disabled and SW-DP Disabled**/ __HAL_AFIO_REMAP_SWJ_NOJTAG(); } + void SystemClock_Config(void) { - RCC_OscInitTypeDef RCC_OscInitStruct; RCC_ClkInitTypeDef RCC_ClkInitStruct; - /**Initializes the CPU, AHB and APB busses clocks */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; @@ -73,22 +68,17 @@ void SystemClock_Config(void) RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; - RT_ASSERT(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) == HAL_OK); - /**Configure the Systick interrupt time */ HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / RT_TICK_PER_SECOND); - /**Configure the Systick */ HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK); - /* SysTick_IRQn interrupt configuration */ HAL_NVIC_SetPriority(SysTick_IRQn, 15, 0); } - /** * This is the timer interrupt service routine. * @@ -97,10 +87,8 @@ void SysTick_Handler(void) { /* enter interrupt */ rt_interrupt_enter(); - HAL_IncTick(); rt_tick_increase(); - /* leave interrupt */ rt_interrupt_leave(); } @@ -115,15 +103,10 @@ void rt_hw_board_init(void) #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 - } - -/*@}*/ diff --git a/bsp/stm32f10x-HAL/drivers/board.h b/bsp/stm32f10x-HAL/drivers/board.h index 617b27586..731dc67a4 100644 --- a/bsp/stm32f10x-HAL/drivers/board.h +++ b/bsp/stm32f10x-HAL/drivers/board.h @@ -197,7 +197,6 @@ #define STM32_SRAM_SIZE 64 //#define STM32F107xC #endif - /* whether use board external SRAM memory */ // Use external SRAM memory on the board // Enable External SRAM memory @@ -209,10 +208,8 @@ // Default: 0x68080000 #define STM32_EXT_SRAM_END 0x68080000 /* the end address of external SRAM */ // - // Internal SRAM memory size[Kbytes] <8-64> // Default: 64 - #define STM32_SRAM_END (0x20000000 + STM32_SRAM_SIZE * 1024) #ifdef __CC_ARM extern int Image$$RW_IRAM1$$ZI$$Limit; diff --git a/bsp/stm32f10x-HAL/drivers/drv_gpio.c b/bsp/stm32f10x-HAL/drivers/drv_gpio.c index a61f5ab2a..6a0db7401 100644 --- a/bsp/stm32f10x-HAL/drivers/drv_gpio.c +++ b/bsp/stm32f10x-HAL/drivers/drv_gpio.c @@ -18,7 +18,6 @@ #include #include #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} @@ -35,49 +34,49 @@ static void GPIOB_CLK_ENABLE(void) #endif } #if (STM32F10X_PIN_NUMBERS >36) - static void GPIOC_CLK_ENABLE(void) - { - #ifdef __HAL_RCC_GPIOC_CLK_ENABLE - __HAL_RCC_GPIOC_CLK_ENABLE(); - #endif - } - #if (STM32F10X_PIN_NUMBERS >48) +static void GPIOC_CLK_ENABLE(void) +{ +#ifdef __HAL_RCC_GPIOC_CLK_ENABLE + __HAL_RCC_GPIOC_CLK_ENABLE(); +#endif +} +#if (STM32F10X_PIN_NUMBERS >48) - static void GPIOD_CLK_ENABLE(void) - { - #ifdef __HAL_RCC_GPIOD_CLK_ENABLE - __HAL_RCC_GPIOD_CLK_ENABLE(); - #endif - } - #if (STM32F10X_PIN_NUMBERS >64) - static void GPIOE_CLK_ENABLE(void) - { - #ifdef __HAL_RCC_GPIOE_CLK_ENABLE - __HAL_RCC_GPIOE_CLK_ENABLE(); - #endif - } - #if (STM32F10X_PIN_NUMBERS >100) - static void GPIOF_CLK_ENABLE(void) - { - #ifdef __HAL_RCC_GPIOF_CLK_ENABLE - __HAL_RCC_GPIOF_CLK_ENABLE(); - #endif - } - static void GPIOG_CLK_ENABLE(void) - { - #ifdef __HAL_RCC_GPIOG_CLK_ENABLE - __HAL_RCC_GPIOG_CLK_ENABLE(); - #endif - } - static void GPIOH_CLK_ENABLE(void) - { - #ifdef __HAL_RCC_GPIOH_CLK_ENABLE - __HAL_RCC_GPIOH_CLK_ENABLE(); - #endif - } - #endif - #endif - #endif +static void GPIOD_CLK_ENABLE(void) +{ +#ifdef __HAL_RCC_GPIOD_CLK_ENABLE + __HAL_RCC_GPIOD_CLK_ENABLE(); +#endif +} +#if (STM32F10X_PIN_NUMBERS >64) +static void GPIOE_CLK_ENABLE(void) +{ +#ifdef __HAL_RCC_GPIOE_CLK_ENABLE + __HAL_RCC_GPIOE_CLK_ENABLE(); +#endif +} +#if (STM32F10X_PIN_NUMBERS >100) +static void GPIOF_CLK_ENABLE(void) +{ +#ifdef __HAL_RCC_GPIOF_CLK_ENABLE + __HAL_RCC_GPIOF_CLK_ENABLE(); +#endif +} +static void GPIOG_CLK_ENABLE(void) +{ +#ifdef __HAL_RCC_GPIOG_CLK_ENABLE + __HAL_RCC_GPIOG_CLK_ENABLE(); +#endif +} +static void GPIOH_CLK_ENABLE(void) +{ +#ifdef __HAL_RCC_GPIOH_CLK_ENABLE + __HAL_RCC_GPIOH_CLK_ENABLE(); +#endif +} +#endif +#endif +#endif #endif /* STM32 GPIO driver */ struct pin_index @@ -549,7 +548,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]; @@ -560,20 +558,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); } @@ -581,17 +576,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; } @@ -599,22 +590,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_HIGH; - if (mode == PIN_MODE_OUTPUT) { /* output setting */ @@ -645,9 +632,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; @@ -660,6 +647,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); @@ -675,7 +663,6 @@ rt_err_t stm32_pin_attach_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) { @@ -686,7 +673,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 && @@ -706,15 +692,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) { @@ -725,7 +710,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) { @@ -737,9 +721,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) { @@ -748,7 +732,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) { @@ -806,9 +789,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, @@ -822,7 +805,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; } @@ -840,36 +822,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(); @@ -880,6 +868,7 @@ void EXTI9_5_IRQHandler(void) HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_9); rt_interrupt_leave(); } + void EXTI15_10_IRQHandler(void) { rt_interrupt_enter(); @@ -891,5 +880,4 @@ void EXTI15_10_IRQHandler(void) HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_15); rt_interrupt_leave(); } - #endif diff --git a/bsp/stm32f10x-HAL/drivers/drv_gpio.h b/bsp/stm32f10x-HAL/drivers/drv_gpio.h index 7b4b38f2b..06d5bbc5e 100644 --- a/bsp/stm32f10x-HAL/drivers/drv_gpio.h +++ b/bsp/stm32f10x-HAL/drivers/drv_gpio.h @@ -14,18 +14,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 diff --git a/bsp/stm32f10x-HAL/drivers/drv_sdcard.c b/bsp/stm32f10x-HAL/drivers/drv_sdcard.c index df833b106..e3486d195 100644 --- a/bsp/stm32f10x-HAL/drivers/drv_sdcard.c +++ b/bsp/stm32f10x-HAL/drivers/drv_sdcard.c @@ -21,18 +21,15 @@ * Date Author Notes * 2018-01-13 Liu2guang the first version. */ -#include -#include +#include +#include #include "board.h" #include "drv_sdcard.h" - #ifndef SDIO_CLK_DIV -#define SDIO_CLK_DIV 2 + #define SDIO_CLK_DIV 2 #endif - #define SDIO_TIMEOUT ((uint32_t)0x100000) - -static SD_HandleTypeDef hsdcard; +static SD_HandleTypeDef hsdcard; static DMA_HandleTypeDef hdma; static struct rt_semaphore sd_lock; @@ -43,11 +40,11 @@ void SDIO_IRQHandler(void) rt_interrupt_leave(); } -#if defined(USING_SD_RX_DMA) || defined(USING_SD_TX_DMA) +#if defined(USING_SD_RX_DMA) || defined(USING_SD_TX_DMA) void DMA2_Channel4_5_IRQHandler(void) { rt_interrupt_enter(); - HAL_DMA_IRQHandler(&hdma); + HAL_DMA_IRQHandler(&hdma); rt_interrupt_leave(); } #endif @@ -57,47 +54,42 @@ rt_err_t stm32_read_blocks(uint32_t *data, uint32_t addr, uint32_t num) uint32_t timeout = 0; HAL_SD_StateTypeDef state_return; HAL_SD_CardStateTypeDef sd_card_state_return; - -#if defined(USING_SD_RX_DMA) && defined(USING_SD_TX_DMA) +#if defined(USING_SD_RX_DMA) && defined(USING_SD_TX_DMA) hdma.Init.Direction = DMA_PERIPH_TO_MEMORY; - hdma.Init.PeriphInc = DMA_PINC_DISABLE; - hdma.Init.MemInc = DMA_MINC_ENABLE; + hdma.Init.PeriphInc = DMA_PINC_DISABLE; + hdma.Init.MemInc = DMA_MINC_ENABLE; HAL_DMA_DeInit(&hdma); - HAL_DMA_Init(&hdma); + HAL_DMA_Init(&hdma); #endif - #if defined(USING_SD_RX_DMA) - if(HAL_SD_ReadBlocks_DMA(&hsdcard, (uint8_t *)data, addr, num) != HAL_OK) + if (HAL_SD_ReadBlocks_DMA(&hsdcard, (uint8_t *)data, addr, num) != HAL_OK) #else - if(HAL_SD_ReadBlocks(&hsdcard, (uint8_t *)data, addr, num, SDIO_TIMEOUT) != HAL_OK) + if (HAL_SD_ReadBlocks(&hsdcard, (uint8_t *)data, addr, num, SDIO_TIMEOUT) != HAL_OK) #endif { return RT_EIO; } - do { state_return = HAL_SD_GetState(&hsdcard); timeout++; - }while((HAL_SD_STATE_BUSY == state_return) && (SDIO_TIMEOUT > timeout)); - - if(HAL_SD_STATE_READY != state_return) - { - return RT_ERROR; } - + while ((HAL_SD_STATE_BUSY == state_return) && (SDIO_TIMEOUT > timeout)); + if (HAL_SD_STATE_READY != state_return) + { + return RT_ERROR; + } do { sd_card_state_return = HAL_SD_GetCardState(&hsdcard); timeout++; - }while((HAL_SD_CARD_TRANSFER != sd_card_state_return) && (SDIO_TIMEOUT > timeout)); - - if(SDIO_TIMEOUT <= timeout) - { - return RT_ETIMEOUT; } - - return RT_EOK; + while ((HAL_SD_CARD_TRANSFER != sd_card_state_return) && (SDIO_TIMEOUT > timeout)); + if (SDIO_TIMEOUT <= timeout) + { + return RT_ETIMEOUT; + } + return RT_EOK; } rt_err_t stm32_write_blocks(uint32_t *data, uint32_t addr, uint32_t num) @@ -105,220 +97,185 @@ rt_err_t stm32_write_blocks(uint32_t *data, uint32_t addr, uint32_t num) uint32_t timeout = 0; HAL_SD_StateTypeDef state_return; HAL_SD_CardStateTypeDef sd_card_state_return; - -#if defined(USING_SD_RX_DMA) && defined(USING_SD_TX_DMA) +#if defined(USING_SD_RX_DMA) && defined(USING_SD_TX_DMA) hdma.Init.Direction = DMA_MEMORY_TO_PERIPH; - hdma.Init.PeriphInc = DMA_MINC_ENABLE; - hdma.Init.MemInc = DMA_PINC_DISABLE; + hdma.Init.PeriphInc = DMA_MINC_ENABLE; + hdma.Init.MemInc = DMA_PINC_DISABLE; HAL_DMA_DeInit(&hdma); - HAL_DMA_Init(&hdma); + HAL_DMA_Init(&hdma); #endif - #if defined(USING_SD_TX_DMA) - if(HAL_SD_WriteBlocks_DMA(&hsdcard, (uint8_t *)data, addr, num) != HAL_OK) + if (HAL_SD_WriteBlocks_DMA(&hsdcard, (uint8_t *)data, addr, num) != HAL_OK) #else - if(HAL_SD_WriteBlocks(&hsdcard, (uint8_t *)data, addr, num, SDIO_TIMEOUT) != HAL_OK) + if (HAL_SD_WriteBlocks(&hsdcard, (uint8_t *)data, addr, num, SDIO_TIMEOUT) != HAL_OK) #endif { return RT_ERROR; } - do { state_return = HAL_SD_GetState(&hsdcard); timeout++; - }while((HAL_SD_STATE_BUSY == state_return) && (SDIO_TIMEOUT > timeout)); - - if(HAL_SD_STATE_READY != state_return) - { - return RT_ERROR; } - + while ((HAL_SD_STATE_BUSY == state_return) && (SDIO_TIMEOUT > timeout)); + if (HAL_SD_STATE_READY != state_return) + { + return RT_ERROR; + } do { sd_card_state_return = HAL_SD_GetCardState(&hsdcard); timeout++; - }while((HAL_SD_CARD_TRANSFER != sd_card_state_return) && (SDIO_TIMEOUT > timeout)); - - if(SDIO_TIMEOUT <= timeout) - { - return RT_ETIMEOUT; } - - return RT_EOK; + while ((HAL_SD_CARD_TRANSFER != sd_card_state_return) && (SDIO_TIMEOUT > timeout)); + if (SDIO_TIMEOUT <= timeout) + { + return RT_ETIMEOUT; + } + return RT_EOK; } static rt_err_t stm32_sdcard_init(rt_device_t dev) { GPIO_InitTypeDef GPIO_InitStruct; - - if(rt_sem_init(&sd_lock, "sdlock", 1, RT_IPC_FLAG_FIFO) != RT_EOK) + if (rt_sem_init(&sd_lock, "sdlock", 1, RT_IPC_FLAG_FIFO) != RT_EOK) { return RT_ERROR; } - __HAL_RCC_GPIOD_CLK_ENABLE(); __HAL_RCC_GPIOC_CLK_ENABLE(); - - GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | + GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; - HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); - + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; + HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_2; HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); - -#if defined(USING_SD_RX_DMA) || defined(USING_SD_TX_DMA) +#if defined(USING_SD_RX_DMA) || defined(USING_SD_TX_DMA) __HAL_RCC_DMA2_CLK_ENABLE(); - - hdma.Instance = DMA2_Channel4; - hdma.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD; - hdma.Init.MemDataAlignment = DMA_MDATAALIGN_WORD; - hdma.Init.Mode = DMA_NORMAL; - hdma.Init.Priority = DMA_PRIORITY_HIGH; - + hdma.Instance = DMA2_Channel4; + hdma.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD; + hdma.Init.MemDataAlignment = DMA_MDATAALIGN_WORD; + hdma.Init.Mode = DMA_NORMAL; + hdma.Init.Priority = DMA_PRIORITY_HIGH; #if defined(USING_SD_RX_DMA) - hdma.Init.Direction = DMA_PERIPH_TO_MEMORY; - hdma.Init.PeriphInc = DMA_PINC_DISABLE; - hdma.Init.MemInc = DMA_MINC_ENABLE; - __HAL_LINKDMA(&hsdcard, hdmarx, hdma); + hdma.Init.Direction = DMA_PERIPH_TO_MEMORY; + hdma.Init.PeriphInc = DMA_PINC_DISABLE; + hdma.Init.MemInc = DMA_MINC_ENABLE; + __HAL_LINKDMA(&hsdcard, hdmarx, hdma); #endif - #if defined(USING_SD_TX_DMA) hdma.Init.Direction = DMA_MEMORY_TO_PERIPH; hdma.Init.PeriphInc = DMA_MINC_ENABLE; - hdma.Init.MemInc = DMA_PINC_DISABLE; - __HAL_LINKDMA(&hsdcard, hdmatx, hdma); + hdma.Init.MemInc = DMA_PINC_DISABLE; + __HAL_LINKDMA(&hsdcard, hdmatx, hdma); #endif - HAL_DMA_DeInit(&hdma); - if(HAL_DMA_Init(&hdma) != HAL_OK) + if (HAL_DMA_Init(&hdma) != HAL_OK) { rt_kprintf("HAL_DMA_Init error\n"); return RT_EIO; } #endif - HAL_NVIC_SetPriority(DMA2_Channel4_5_IRQn, 3, 0); - HAL_NVIC_EnableIRQ(DMA2_Channel4_5_IRQn); - - __HAL_RCC_SDIO_CLK_ENABLE(); - - hsdcard.Instance = SDIO; - hsdcard.Init.ClockEdge = SDIO_CLOCK_EDGE_RISING; + HAL_NVIC_EnableIRQ(DMA2_Channel4_5_IRQn); + __HAL_RCC_SDIO_CLK_ENABLE(); + hsdcard.Instance = SDIO; + hsdcard.Init.ClockEdge = SDIO_CLOCK_EDGE_RISING; hsdcard.Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE; hsdcard.Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE; hsdcard.Init.BusWide = SDIO_BUS_WIDE_1B; hsdcard.Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_ENABLE; - hsdcard.Init.ClockDiv = SDIO_CLK_DIV; - + hsdcard.Init.ClockDiv = SDIO_CLK_DIV; HAL_SD_DeInit(&hsdcard); - if(HAL_SD_Init(&hsdcard) != HAL_OK) + if (HAL_SD_Init(&hsdcard) != HAL_OK) { rt_kprintf("HAL_SD_Init error\n"); - return RT_EIO; + return RT_EIO; } - HAL_NVIC_SetPriority(SDIO_IRQn, 1, 0); HAL_NVIC_EnableIRQ(SDIO_IRQn); - - if(HAL_SD_ConfigWideBusOperation(&hsdcard, SDIO_BUS_WIDE_4B) != HAL_OK) + if (HAL_SD_ConfigWideBusOperation(&hsdcard, SDIO_BUS_WIDE_4B) != HAL_OK) { rt_kprintf("HAL_SD_ConfigWideBusOperation error\n"); return RT_EIO; } - - return RT_EOK; + return RT_EOK; } static rt_err_t stm32_sdcard_open(rt_device_t dev, rt_uint16_t oflag) { - return RT_EOK; + return RT_EOK; } -static rt_err_t stm32_sdcard_close(rt_device_t dev) +static rt_err_t stm32_sdcard_close(rt_device_t dev) { - return RT_EOK; -} + return RT_EOK; +} -static rt_size_t stm32_sdcard_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size) -{ - int ret = RT_EOK; - - rt_sem_take(&sd_lock, RT_WAITING_FOREVER); - ret = stm32_read_blocks((uint32_t *)buffer, pos, size); - rt_sem_release(&sd_lock); - - if(ret != RT_EOK) - { - return 0; - } - - return size; -} - -static rt_size_t stm32_sdcard_write(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size) +static rt_size_t stm32_sdcard_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size) { int ret = RT_EOK; - rt_sem_take(&sd_lock, RT_WAITING_FOREVER); - ret = stm32_write_blocks((uint32_t *)buffer, pos, size); + ret = stm32_read_blocks((uint32_t *)buffer, pos, size); rt_sem_release(&sd_lock); - - if(ret != RT_EOK) + if (ret != RT_EOK) { - return 0; + return 0; } - - return size; -} + return size; +} + +static rt_size_t stm32_sdcard_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size) +{ + int ret = RT_EOK; + rt_sem_take(&sd_lock, RT_WAITING_FOREVER); + ret = stm32_write_blocks((uint32_t *)buffer, pos, size); + rt_sem_release(&sd_lock); + if (ret != RT_EOK) + { + return 0; + } + return size; +} static rt_err_t stm32_sdcard_control(rt_device_t dev, int cmd, void *args) { RT_ASSERT(dev != RT_NULL); - - // RT_DEVICE_CTRL_BLK_GETGEOME - if(cmd == RT_DEVICE_CTRL_BLK_GETGEOME) + // RT_DEVICE_CTRL_BLK_GETGEOME + if (cmd == RT_DEVICE_CTRL_BLK_GETGEOME) { HAL_SD_CardInfoTypeDef sdcard_info; struct rt_device_blk_geometry *geometry; - HAL_SD_GetCardInfo(&hsdcard, &sdcard_info); - geometry = (struct rt_device_blk_geometry *)args; - geometry->bytes_per_sector = sdcard_info.BlockSize; geometry->block_size = sdcard_info.BlockSize; geometry->sector_count = sdcard_info.BlockNbr; } - return RT_EOK; -} +} -static struct rt_device device; +static struct rt_device device; int rt_hw_sdcard_init(void) { - rt_err_t ret = RT_EOK; - + rt_err_t ret = RT_EOK; device.type = RT_Device_Class_Block; device.init = stm32_sdcard_init; - device.open = stm32_sdcard_open; + device.open = stm32_sdcard_open; device.read = stm32_sdcard_read; device.write = stm32_sdcard_write; device.control = stm32_sdcard_control; device.close = stm32_sdcard_close; - - ret = rt_device_register(&device, "sd0", - RT_DEVICE_FLAG_REMOVABLE | - RT_DEVICE_FLAG_RDWR | - RT_DEVICE_FLAG_STANDALONE); - if(ret != RT_EOK) + ret = rt_device_register(&device, "sd0", + RT_DEVICE_FLAG_REMOVABLE | + RT_DEVICE_FLAG_RDWR | + RT_DEVICE_FLAG_STANDALONE); + if (ret != RT_EOK) { - return ret; + return ret; } - - return RT_EOK; + return RT_EOK; } -INIT_DEVICE_EXPORT(rt_hw_sdcard_init); +INIT_DEVICE_EXPORT(rt_hw_sdcard_init); diff --git a/bsp/stm32f10x-HAL/drivers/drv_sdcard.h b/bsp/stm32f10x-HAL/drivers/drv_sdcard.h index d483020b6..beaf1faf2 100644 --- a/bsp/stm32f10x-HAL/drivers/drv_sdcard.h +++ b/bsp/stm32f10x-HAL/drivers/drv_sdcard.h @@ -22,9 +22,9 @@ * 2018-01-13 Liu2guang the first version. */ -#ifndef __DRV_SDCARD_H_ -#define __DRV_SDCARD_H_ +#ifndef __DRV_SDCARD_H_ +#define __DRV_SDCARD_H_ -int rt_hw_sdcard_init(void); +int rt_hw_sdcard_init(void); #endif diff --git a/bsp/stm32f10x-HAL/drivers/drv_spi.c b/bsp/stm32f10x-HAL/drivers/drv_spi.c index 15fe2ff25..14b24365f 100644 --- a/bsp/stm32f10x-HAL/drivers/drv_spi.c +++ b/bsp/stm32f10x-HAL/drivers/drv_spi.c @@ -16,30 +16,25 @@ #include #include #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; @@ -145,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 \ @@ -195,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; @@ -203,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); @@ -238,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) { @@ -378,34 +373,3 @@ void HAL_SPI_MspDeInit(SPI_HandleTypeDef *spiHandle) } } #endif /*RT_USING_SPI*/ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/bsp/stm32f10x-HAL/drivers/drv_spi.h b/bsp/stm32f10x-HAL/drivers/drv_spi.h index 0775522e2..ce88c2be3 100644 --- a/bsp/stm32f10x-HAL/drivers/drv_spi.h +++ b/bsp/stm32f10x-HAL/drivers/drv_spi.h @@ -18,21 +18,7 @@ #include #include #include - - - -extern int stm32_spi_register_bus(SPI_TypeDef * SPIx,const char * name); -extern rt_err_t stm32_spi_bus_attach_device(rt_uint32_t pin,const char * bus_name,const char * device_name); +extern int stm32_spi_register_bus(SPI_TypeDef *SPIx, const char *name); +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 - - - - - - - - diff --git a/bsp/stm32f10x-HAL/drivers/drv_usart.c b/bsp/stm32f10x-HAL/drivers/drv_usart.c index 53603edcb..80bf5578d 100644 --- a/bsp/stm32f10x-HAL/drivers/drv_usart.c +++ b/bsp/stm32f10x-HAL/drivers/drv_usart.c @@ -23,7 +23,6 @@ #include #include - /* STM32 uart driver */ struct stm32_uart { @@ -34,17 +33,13 @@ struct stm32_uart static rt_err_t stm32_configure(struct rt_serial_device *serial, struct serial_configure *cfg) { struct stm32_uart *uart; - RT_ASSERT(serial != RT_NULL); RT_ASSERT(cfg != RT_NULL); - uart = (struct stm32_uart *)serial->parent.user_data; - uart->huart.Init.BaudRate = cfg->baud_rate; uart->huart.Init.HwFlowCtl = UART_HWCONTROL_NONE; uart->huart.Init.Mode = UART_MODE_TX_RX; uart->huart.Init.OverSampling = UART_OVERSAMPLING_16; - switch (cfg->data_bits) { case DATA_BITS_8: @@ -84,23 +79,18 @@ static rt_err_t stm32_configure(struct rt_serial_device *serial, struct serial_c uart->huart.Init.Parity = UART_PARITY_NONE; break; } - if (HAL_UART_Init(&uart->huart) != HAL_OK) { return RT_ERROR; } - return RT_EOK; } static rt_err_t stm32_control(struct rt_serial_device *serial, int cmd, void *arg) { struct stm32_uart *uart; -// rt_uint32_t ctrl_arg = (rt_uint32_t)(arg); - RT_ASSERT(serial != RT_NULL); uart = (struct stm32_uart *)serial->parent.user_data; - switch (cmd) { /* disable interrupt */ @@ -124,7 +114,6 @@ static rt_err_t stm32_control(struct rt_serial_device *serial, int cmd, void *ar static int stm32_putc(struct rt_serial_device *serial, char c) { struct stm32_uart *uart; - RT_ASSERT(serial != RT_NULL); uart = (struct stm32_uart *)serial->parent.user_data; while (__HAL_UART_GET_FLAG(&uart->huart, UART_FLAG_TXE) == RESET); @@ -146,7 +135,6 @@ static int stm32_getc(struct rt_serial_device *serial) return ch; } - /** * Uart common interrupt process. This need add to uart ISR. * @@ -155,9 +143,7 @@ static int stm32_getc(struct rt_serial_device *serial) static void uart_isr(struct rt_serial_device *serial) { struct stm32_uart *uart = (struct stm32_uart *) serial->parent.user_data; - RT_ASSERT(uart != RT_NULL); - if ((__HAL_UART_GET_FLAG(&uart->huart, UART_FLAG_RXNE) != RESET) && (__HAL_UART_GET_IT_SOURCE(&uart->huart, UART_IT_RXNE) != RESET)) { rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND); @@ -186,9 +172,7 @@ void USART1_IRQHandler(void) { /* enter interrupt */ rt_interrupt_enter(); - uart_isr(&serial1); - /* leave interrupt */ rt_interrupt_leave(); } @@ -250,7 +234,7 @@ int rt_hw_usart_init(void) MX_USART_UART_Init(&uart->huart); /* register UART1 device */ rt_hw_serial_register(&serial1, "uart1", - RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX , + RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, uart); #endif /* RT_USING_UART1 */ @@ -262,7 +246,7 @@ int rt_hw_usart_init(void) MX_USART_UART_Init(&uart->huart); /* register UART1 device */ rt_hw_serial_register(&serial2, "uart2", - RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX , + RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, uart); #endif /* RT_USING_UART1 */ @@ -274,14 +258,13 @@ int rt_hw_usart_init(void) MX_USART_UART_Init(&uart->huart); /* register UART1 device */ rt_hw_serial_register(&serial3, "uart3", - RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX , + RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, uart); #endif /* RT_USING_UART1 */ return 0; } INIT_BOARD_EXPORT(rt_hw_usart_init); - static void MX_USART_UART_Init(UART_HandleTypeDef *uartHandle) { uartHandle->Init.BaudRate = 115200; @@ -296,15 +279,12 @@ static void MX_USART_UART_Init(UART_HandleTypeDef *uartHandle) } /* USART2 init function */ - void HAL_UART_MspInit(UART_HandleTypeDef *uartHandle) { - GPIO_InitTypeDef GPIO_InitStruct; if (uartHandle->Instance == USART1) { /* USER CODE BEGIN USART1_MspInit 0 */ - /* USER CODE END USART1_MspInit 0 */ /* USART1 clock enable */ __HAL_RCC_USART1_CLK_ENABLE(); @@ -317,23 +297,19 @@ void HAL_UART_MspInit(UART_HandleTypeDef *uartHandle) GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - GPIO_InitStruct.Pin = GPIO_PIN_10; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - /* USART1 interrupt Init */ HAL_NVIC_SetPriority(USART1_IRQn, 5, 0); HAL_NVIC_EnableIRQ(USART1_IRQn); /* USER CODE BEGIN USART1_MspInit 1 */ - /* USER CODE END USART1_MspInit 1 */ } else if (uartHandle->Instance == USART2) { /* USER CODE BEGIN USART2_MspInit 0 */ - /* USER CODE END USART2_MspInit 0 */ /* USART2 clock enable */ __HAL_RCC_USART2_CLK_ENABLE(); @@ -346,12 +322,10 @@ void HAL_UART_MspInit(UART_HandleTypeDef *uartHandle) GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - GPIO_InitStruct.Pin = GPIO_PIN_3; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - /* USART2 interrupt Init */ HAL_NVIC_SetPriority(USART2_IRQn, 5, 0); HAL_NVIC_EnableIRQ(USART2_IRQn); @@ -362,7 +336,6 @@ void HAL_UART_MspInit(UART_HandleTypeDef *uartHandle) else if (uartHandle->Instance == USART3) { /* USER CODE BEGIN USART3_MspInit 0 */ - /* USER CODE END USART3_MspInit 0 */ /* USART3 clock enable */ __HAL_RCC_USART3_CLK_ENABLE(); @@ -375,86 +348,66 @@ void HAL_UART_MspInit(UART_HandleTypeDef *uartHandle) GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - GPIO_InitStruct.Pin = GPIO_PIN_11; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - /* USART3 interrupt Init */ HAL_NVIC_SetPriority(USART3_IRQn, 5, 0); HAL_NVIC_EnableIRQ(USART3_IRQn); /* USER CODE BEGIN USART3_MspInit 1 */ - /* USER CODE END USART3_MspInit 1 */ } } void HAL_UART_MspDeInit(UART_HandleTypeDef *uartHandle) { - if (uartHandle->Instance == USART1) { /* USER CODE BEGIN USART1_MspDeInit 0 */ - /* USER CODE END USART1_MspDeInit 0 */ /* Peripheral clock disable */ __HAL_RCC_USART1_CLK_DISABLE(); - /**USART1 GPIO Configuration PA9 ------> USART1_TX PA10 ------> USART1_RX */ HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9 | GPIO_PIN_10); - /* USART1 interrupt Deinit */ HAL_NVIC_DisableIRQ(USART1_IRQn); /* USER CODE BEGIN USART1_MspDeInit 1 */ - /* USER CODE END USART1_MspDeInit 1 */ } else if (uartHandle->Instance == USART2) { /* USER CODE BEGIN USART2_MspDeInit 0 */ - /* USER CODE END USART2_MspDeInit 0 */ /* Peripheral clock disable */ __HAL_RCC_USART2_CLK_DISABLE(); - /**USART2 GPIO Configuration PA2 ------> USART2_TX PA3 ------> USART2_RX */ HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2 | GPIO_PIN_3); - /* USART2 interrupt Deinit */ HAL_NVIC_DisableIRQ(USART2_IRQn); /* USER CODE BEGIN USART2_MspDeInit 1 */ - /* USER CODE END USART2_MspDeInit 1 */ } else if (uartHandle->Instance == USART3) { /* USER CODE BEGIN USART3_MspDeInit 0 */ - /* USER CODE END USART3_MspDeInit 0 */ /* Peripheral clock disable */ __HAL_RCC_USART3_CLK_DISABLE(); - /**USART3 GPIO Configuration PB10 ------> USART3_TX PB11 ------> USART3_RX */ HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10 | GPIO_PIN_11); - /* USART3 interrupt Deinit */ HAL_NVIC_DisableIRQ(USART3_IRQn); /* USER CODE BEGIN USART3_MspDeInit 1 */ - /* USER CODE END USART3_MspDeInit 1 */ } } - - - - diff --git a/bsp/stm32f10x-HAL/drivers/drv_usart.h b/bsp/stm32f10x-HAL/drivers/drv_usart.h index af7109003..78b46739e 100644 --- a/bsp/stm32f10x-HAL/drivers/drv_usart.h +++ b/bsp/stm32f10x-HAL/drivers/drv_usart.h @@ -11,12 +11,9 @@ * Date Author Notes * 2009-01-05 Bernard the first version */ - #ifndef __USART_H__ #define __USART_H__ - #include #include int rt_hw_usart_init(void); - #endif diff --git a/bsp/stm32f10x-HAL/drivers/drv_usb.c b/bsp/stm32f10x-HAL/drivers/drv_usb.c index bade24f08..111fc1b30 100644 --- a/bsp/stm32f10x-HAL/drivers/drv_usb.c +++ b/bsp/stm32f10x-HAL/drivers/drv_usb.c @@ -16,9 +16,7 @@ #include #include #include "board.h" - -#define USB_DISCONNECT_PIN 30 //PA9 - +#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 USB_LP_CAN1_RX0_IRQHandler(void) { rt_interrupt_enter(); - HAL_PCD_IRQHandler(&_stm_pcd); - rt_interrupt_leave(); } @@ -51,10 +47,9 @@ void HAL_PCD_ResetCallback(PCD_HandleTypeDef *pcd) void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd) { - rt_usbd_ep0_setup_handler(&_stm_udc, (struct urequest*)hpcd->Setup); + rt_usbd_ep0_setup_handler(&_stm_udc, (struct urequest *)hpcd->Setup); } - void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) { if (epnum == 0) @@ -63,7 +58,7 @@ void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) } else { - rt_usbd_ep_in_handler(&_stm_udc, 0x80|epnum,hpcd->IN_ep[epnum].xfer_count); + rt_usbd_ep_in_handler(&_stm_udc, 0x80 | epnum, hpcd->IN_ep[epnum].xfer_count); } } @@ -90,46 +85,43 @@ void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) } else { - rt_usbd_ep0_out_handler(&_stm_udc,hpcd->OUT_ep[0].xfer_count); + rt_usbd_ep0_out_handler(&_stm_udc, hpcd->OUT_ep[0].xfer_count); } } - void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state) { - if(state == 1) + if (state == 1) { - rt_pin_write(USB_DISCONNECT_PIN,PIN_HIGH); + rt_pin_write(USB_DISCONNECT_PIN, PIN_HIGH); } else { - rt_pin_write(USB_DISCONNECT_PIN,PIN_LOW); - } + rt_pin_write(USB_DISCONNECT_PIN, PIN_LOW); + } } -void HAL_PCD_MspInit(PCD_HandleTypeDef* pcdHandle) +void HAL_PCD_MspInit(PCD_HandleTypeDef *pcdHandle) { - if(pcdHandle->Instance==USB) + if (pcdHandle->Instance == USB) { __HAL_RCC_GPIOA_CLK_ENABLE(); - rt_pin_mode(USB_DISCONNECT_PIN,PIN_MODE_OUTPUT); - rt_pin_write(USB_DISCONNECT_PIN,PIN_LOW); + rt_pin_mode(USB_DISCONNECT_PIN, PIN_MODE_OUTPUT); + rt_pin_write(USB_DISCONNECT_PIN, PIN_LOW); /* Peripheral clock enable */ __HAL_RCC_USB_CLK_ENABLE(); - /* Peripheral interrupt init */ HAL_NVIC_SetPriority(USB_LP_CAN1_RX0_IRQn, 5, 0); HAL_NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn); } } -void HAL_PCD_MspDeInit(PCD_HandleTypeDef* pcdHandle) +void HAL_PCD_MspDeInit(PCD_HandleTypeDef *pcdHandle) { - if(pcdHandle->Instance==USB) + if (pcdHandle->Instance == USB) { /* Peripheral clock disable */ __HAL_RCC_USB_CLK_DISABLE(); - /* Peripheral interrupt Deinit*/ HAL_NVIC_DisableIRQ(USB_LP_CAN1_RX0_IRQn); } @@ -143,7 +135,7 @@ static rt_err_t _ep_set_stall(rt_uint8_t address) static rt_err_t _ep_clear_stall(rt_uint8_t address) { - HAL_PCD_EP_ClrStall(&_stm_pcd, address); + HAL_PCD_EP_ClrStall(&_stm_pcd, address); return RT_EOK; } @@ -164,7 +156,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; } @@ -196,7 +187,7 @@ static rt_size_t _ep_write(rt_uint8_t address, void *buffer, rt_size_t size) } static rt_err_t _ep0_send_status(void) -{ +{ HAL_PCD_EP_Transmit(&_stm_pcd, 0x00, NULL, 0); return RT_EOK; } @@ -214,10 +205,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 = (PCD_HandleTypeDef *)device->user_data; pcd->Instance = USB; pcd->Init.dev_endpoints = 8; pcd->Init.speed = PCD_SPEED_FULL; @@ -225,19 +214,16 @@ static rt_err_t _init(rt_device_t device) pcd->Init.low_power_enable = DISABLE; pcd->Init.lpm_enable = DISABLE; pcd->Init.battery_charging_enable = DISABLE; - /* Initialize LL Driver */ HAL_PCD_Init(pcd); - - HAL_PCDEx_PMAConfig(pcd , 0x00 , PCD_SNG_BUF, 0x18); - HAL_PCDEx_PMAConfig(pcd , 0x80 , PCD_SNG_BUF, 0x58); - HAL_PCDEx_PMAConfig(pcd , 0x81 , PCD_SNG_BUF, 0x98); - HAL_PCDEx_PMAConfig(pcd , 0x01 , PCD_SNG_BUF, 0x118); - HAL_PCDEx_PMAConfig(pcd , 0x82 , PCD_SNG_BUF, 0xD8); - HAL_PCDEx_PMAConfig(pcd , 0x02 , PCD_SNG_BUF, 0x158); - HAL_PCDEx_PMAConfig(pcd , 0x83 , PCD_SNG_BUF, 0x198); + HAL_PCDEx_PMAConfig(pcd, 0x00, PCD_SNG_BUF, 0x18); + HAL_PCDEx_PMAConfig(pcd, 0x80, PCD_SNG_BUF, 0x58); + HAL_PCDEx_PMAConfig(pcd, 0x81, PCD_SNG_BUF, 0x98); + HAL_PCDEx_PMAConfig(pcd, 0x01, PCD_SNG_BUF, 0x118); + HAL_PCDEx_PMAConfig(pcd, 0x82, PCD_SNG_BUF, 0xD8); + HAL_PCDEx_PMAConfig(pcd, 0x02, PCD_SNG_BUF, 0x158); + HAL_PCDEx_PMAConfig(pcd, 0x83, PCD_SNG_BUF, 0x198); HAL_PCD_Start(pcd); - return RT_EOK; } @@ -257,11 +243,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; @@ -269,10 +253,9 @@ 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 0; } + INIT_DEVICE_EXPORT(stm_usbd_register); - diff --git a/bsp/stm32f10x-HAL/drivers/drv_usb.h b/bsp/stm32f10x-HAL/drivers/drv_usb.h index 040016e5a..37932fae2 100644 --- a/bsp/stm32f10x-HAL/drivers/drv_usb.h +++ b/bsp/stm32f10x-HAL/drivers/drv_usb.h @@ -14,7 +14,5 @@ #ifndef __STM32_USB_H__ #define __STM32_USB_H__ #include - int stm_usbd_register(void); - #endif diff --git a/bsp/stm32f10x-HAL/drivers/stm32f1xx_hal_conf.h b/bsp/stm32f10x-HAL/drivers/stm32f1xx_hal_conf.h index 4475e0904..e50abfd1f 100644 --- a/bsp/stm32f10x-HAL/drivers/stm32f1xx_hal_conf.h +++ b/bsp/stm32f10x-HAL/drivers/stm32f1xx_hal_conf.h @@ -35,17 +35,17 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ****************************************************************************** - */ + */ /* Define to prevent recursive inclusion -------------------------------------*/ #ifndef __STM32F1xx_HAL_CONF_H #define __STM32F1xx_HAL_CONF_H #ifdef __cplusplus - extern "C" { +extern "C" { #endif #include "rtthread.h" -#ifndef NULL +#ifndef NULL #define NULL RT_NULL #endif /* Exported types ------------------------------------------------------------*/ @@ -53,7 +53,7 @@ /* ########################## Module Selection ############################## */ /** - * @brief This is the list of modules to be used in the HAL driver + * @brief This is the list of modules to be used in the HAL driver */ #define HAL_MODULE_ENABLED // #define HAL_ADC_MODULE_ENABLED @@ -75,23 +75,23 @@ // #define HAL_NOR_MODULE_ENABLED // #define HAL_PCCARD_MODULE_ENABLED #ifdef RT_USING_USB_DEVICE - #define HAL_PCD_MODULE_ENABLED +#define HAL_PCD_MODULE_ENABLED #endif #define HAL_PWR_MODULE_ENABLED #define HAL_RCC_MODULE_ENABLED // #define HAL_RTC_MODULE_ENABLED -#ifdef RT_USING_SDCARD - #define HAL_SD_MODULE_ENABLED +#ifdef RT_USING_SDCARD +#define HAL_SD_MODULE_ENABLED #endif // #define HAL_SMARTCARD_MODULE_ENABLED #ifdef RT_USING_SPI - #define HAL_SPI_MODULE_ENABLED +#define HAL_SPI_MODULE_ENABLED #endif // #define HAL_SRAM_MODULE_ENABLED #define HAL_TIM_MODULE_ENABLED #ifdef RT_USING_SERIAL - #define HAL_UART_MODULE_ENABLED - #define HAL_USART_MODULE_ENABLED +#define HAL_UART_MODULE_ENABLED +#define HAL_USART_MODULE_ENABLED #endif // #define HAL_WWDG_MODULE_ENABLED @@ -101,49 +101,49 @@ /** * @brief Adjust the value of External High Speed oscillator (HSE) used in your application. * This value is used by the RCC HAL module to compute the system frequency - * (when HSE is used as system clock source, directly or through the PLL). + * (when HSE is used as system clock source, directly or through the PLL). */ #define HSE_VALUE ((unsigned int)RT_HSE_VALUE) -#if !defined (HSE_VALUE) +#if !defined (HSE_VALUE) #if defined(USE_STM3210C_EVAL) - #define HSE_VALUE 25000000U /*!< Value of the External oscillator in Hz */ +#define HSE_VALUE 25000000U /*!< Value of the External oscillator in Hz */ #else - #define HSE_VALUE 8000000U /*!< Value of the External oscillator in Hz */ +#define HSE_VALUE 8000000U /*!< Value of the External oscillator in Hz */ #endif #endif /* HSE_VALUE */ #if !defined (HSE_STARTUP_TIMEOUT) - #define HSE_STARTUP_TIMEOUT 100U /*!< Time out for HSE start up, in ms */ +#define HSE_STARTUP_TIMEOUT 100U /*!< Time out for HSE start up, in ms */ #endif /* HSE_STARTUP_TIMEOUT */ /** * @brief Internal High Speed oscillator (HSI) value. * This value is used by the RCC HAL module to compute the system frequency - * (when HSI is used as system clock source, directly or through the PLL). + * (when HSI is used as system clock source, directly or through the PLL). */ #if !defined (HSI_VALUE) - #define HSI_VALUE 8000000U /*!< Value of the Internal oscillator in Hz */ +#define HSI_VALUE 8000000U /*!< Value of the Internal oscillator in Hz */ #endif /* HSI_VALUE */ /** * @brief Internal Low Speed oscillator (LSI) value. */ -#if !defined (LSI_VALUE) - #define LSI_VALUE 40000U /*!< LSI Typical Value in Hz */ +#if !defined (LSI_VALUE) +#define LSI_VALUE 40000U /*!< LSI Typical Value in Hz */ #endif /* LSI_VALUE */ /*!< Value of the Internal Low Speed oscillator in Hz - The real value may vary depending on the variations - in voltage and temperature. */ +The real value may vary depending on the variations +in voltage and temperature. */ /** * @brief External Low Speed oscillator (LSE) value. * This value is used by the UART, RTC HAL module to compute the system frequency */ #if !defined (LSE_VALUE) - #define LSE_VALUE 32768U /*!< Value of the External Low Speed oscillator in Hz */ +#define LSE_VALUE 32768U /*!< Value of the External Low Speed oscillator in Hz */ #endif /* LSE_VALUE */ #if !defined (LSE_STARTUP_TIMEOUT) - #define LSE_STARTUP_TIMEOUT 5000U /*!< Time out for LSE start up, in ms */ +#define LSE_STARTUP_TIMEOUT 5000U /*!< Time out for LSE start up, in ms */ #endif /* LSE_STARTUP_TIMEOUT */ /* Tip: To avoid modifying this file each time you need to use different HSE, @@ -152,7 +152,7 @@ /* ########################### System Configuration ######################### */ /** * @brief This is the HAL system configuration section - */ + */ #define VDD_VALUE 3300U /*!< Value of VDD in mv */ #define TICK_INT_PRIORITY 0x0FU /*!< tick interrupt priority */ #define USE_RTOS 0U @@ -160,7 +160,7 @@ /* ########################## Assert Selection ############################## */ /** - * @brief Uncomment the line below to expanse the "assert_param" macro in the + * @brief Uncomment the line below to expanse the "assert_param" macro in the * HAL drivers code */ /* #define USE_FULL_ASSERT 1U */ @@ -177,7 +177,7 @@ #define MAC_ADDR4 0U #define MAC_ADDR5 0U -/* Definition of the Ethernet driver buffers size and count */ +/* Definition of the Ethernet driver buffers size and count */ #define ETH_RX_BUF_SIZE ETH_MAX_PACKET_SIZE /* buffer size for receive */ #define ETH_TX_BUF_SIZE ETH_MAX_PACKET_SIZE /* buffer size for transmit */ #define ETH_RXBUFNB 8U /* 8 Rx buffers of size ETH_RX_BUF_SIZE */ @@ -185,9 +185,9 @@ /* Section 2: PHY configuration section */ -/* DP83848 PHY Address*/ +/* DP83848 PHY Address*/ #define DP83848_PHY_ADDRESS 0x01U -/* PHY Reset delay these values are based on a 1 ms Systick interrupt*/ +/* PHY Reset delay these values are based on a 1 ms Systick interrupt*/ #define PHY_RESET_DELAY 0x000000FFU /* PHY Configuration delay */ #define PHY_CONFIG_DELAY 0x00000FFFU @@ -199,7 +199,7 @@ #define PHY_BCR ((uint16_t)0x0000) /*!< Transceiver Basic Control Register */ #define PHY_BSR ((uint16_t)0x0001) /*!< Transceiver Basic Status Register */ - + #define PHY_RESET ((uint16_t)0x8000) /*!< PHY Reset */ #define PHY_LOOPBACK ((uint16_t)0x4000) /*!< Select loop-back mode */ #define PHY_FULLDUPLEX_100M ((uint16_t)0x2100) /*!< Set the full-duplex mode at 100 Mb/s */ @@ -214,13 +214,13 @@ #define PHY_AUTONEGO_COMPLETE ((uint16_t)0x0020) /*!< Auto-Negotiation process completed */ #define PHY_LINKED_STATUS ((uint16_t)0x0004) /*!< Valid link established */ #define PHY_JABBER_DETECTION ((uint16_t)0x0002) /*!< Jabber condition detected */ - + /* Section 4: Extended PHY Registers */ #define PHY_SR ((uint16_t)0x0010) /*!< PHY status register Offset */ #define PHY_MICR ((uint16_t)0x0011) /*!< MII Interrupt Control Register */ #define PHY_MISR ((uint16_t)0x0012) /*!< MII Interrupt Status and Misc. Control Register */ - + #define PHY_LINK_STATUS ((uint16_t)0x0001) /*!< PHY Link mask */ #define PHY_SPEED_STATUS ((uint16_t)0x0002) /*!< PHY Speed mask */ #define PHY_DUPLEX_STATUS ((uint16_t)0x0004) /*!< PHY Duplex mask */ @@ -242,131 +242,131 @@ /* Includes ------------------------------------------------------------------*/ /** - * @brief Include module's header file + * @brief Include module's header file */ #ifdef HAL_RCC_MODULE_ENABLED - #include "stm32f1xx_hal_rcc.h" +#include "stm32f1xx_hal_rcc.h" #endif /* HAL_RCC_MODULE_ENABLED */ #ifdef HAL_GPIO_MODULE_ENABLED - #include "stm32f1xx_hal_gpio.h" +#include "stm32f1xx_hal_gpio.h" #endif /* HAL_GPIO_MODULE_ENABLED */ - + #ifdef HAL_DMA_MODULE_ENABLED - #include "stm32f1xx_hal_dma.h" +#include "stm32f1xx_hal_dma.h" #endif /* HAL_DMA_MODULE_ENABLED */ - + #ifdef HAL_ETH_MODULE_ENABLED - #include "stm32f1xx_hal_eth.h" -#endif /* HAL_ETH_MODULE_ENABLED */ - +#include "stm32f1xx_hal_eth.h" +#endif /* HAL_ETH_MODULE_ENABLED */ + #ifdef HAL_CAN_MODULE_ENABLED - #include "stm32f1xx_hal_can.h" +#include "stm32f1xx_hal_can.h" #endif /* HAL_CAN_MODULE_ENABLED */ #ifdef HAL_CEC_MODULE_ENABLED - #include "stm32f1xx_hal_cec.h" +#include "stm32f1xx_hal_cec.h" #endif /* HAL_CEC_MODULE_ENABLED */ #ifdef HAL_CORTEX_MODULE_ENABLED - #include "stm32f1xx_hal_cortex.h" +#include "stm32f1xx_hal_cortex.h" #endif /* HAL_CORTEX_MODULE_ENABLED */ #ifdef HAL_ADC_MODULE_ENABLED - #include "stm32f1xx_hal_adc.h" +#include "stm32f1xx_hal_adc.h" #endif /* HAL_ADC_MODULE_ENABLED */ #ifdef HAL_CRC_MODULE_ENABLED - #include "stm32f1xx_hal_crc.h" +#include "stm32f1xx_hal_crc.h" #endif /* HAL_CRC_MODULE_ENABLED */ #ifdef HAL_DAC_MODULE_ENABLED - #include "stm32f1xx_hal_dac.h" +#include "stm32f1xx_hal_dac.h" #endif /* HAL_DAC_MODULE_ENABLED */ #ifdef HAL_FLASH_MODULE_ENABLED - #include "stm32f1xx_hal_flash.h" +#include "stm32f1xx_hal_flash.h" #endif /* HAL_FLASH_MODULE_ENABLED */ #ifdef HAL_SRAM_MODULE_ENABLED - #include "stm32f1xx_hal_sram.h" +#include "stm32f1xx_hal_sram.h" #endif /* HAL_SRAM_MODULE_ENABLED */ #ifdef HAL_NOR_MODULE_ENABLED - #include "stm32f1xx_hal_nor.h" +#include "stm32f1xx_hal_nor.h" #endif /* HAL_NOR_MODULE_ENABLED */ #ifdef HAL_I2C_MODULE_ENABLED - #include "stm32f1xx_hal_i2c.h" +#include "stm32f1xx_hal_i2c.h" #endif /* HAL_I2C_MODULE_ENABLED */ #ifdef HAL_I2S_MODULE_ENABLED - #include "stm32f1xx_hal_i2s.h" +#include "stm32f1xx_hal_i2s.h" #endif /* HAL_I2S_MODULE_ENABLED */ #ifdef HAL_IWDG_MODULE_ENABLED - #include "stm32f1xx_hal_iwdg.h" +#include "stm32f1xx_hal_iwdg.h" #endif /* HAL_IWDG_MODULE_ENABLED */ #ifdef HAL_PWR_MODULE_ENABLED - #include "stm32f1xx_hal_pwr.h" +#include "stm32f1xx_hal_pwr.h" #endif /* HAL_PWR_MODULE_ENABLED */ #ifdef HAL_RTC_MODULE_ENABLED - #include "stm32f1xx_hal_rtc.h" +#include "stm32f1xx_hal_rtc.h" #endif /* HAL_RTC_MODULE_ENABLED */ #ifdef HAL_PCCARD_MODULE_ENABLED - #include "stm32f1xx_hal_pccard.h" -#endif /* HAL_PCCARD_MODULE_ENABLED */ +#include "stm32f1xx_hal_pccard.h" +#endif /* HAL_PCCARD_MODULE_ENABLED */ #ifdef HAL_SD_MODULE_ENABLED - #include "stm32f1xx_hal_sd.h" -#endif /* HAL_SD_MODULE_ENABLED */ +#include "stm32f1xx_hal_sd.h" +#endif /* HAL_SD_MODULE_ENABLED */ #ifdef HAL_NAND_MODULE_ENABLED - #include "stm32f1xx_hal_nand.h" -#endif /* HAL_NAND_MODULE_ENABLED */ +#include "stm32f1xx_hal_nand.h" +#endif /* HAL_NAND_MODULE_ENABLED */ #ifdef HAL_SPI_MODULE_ENABLED - #include "stm32f1xx_hal_spi.h" +#include "stm32f1xx_hal_spi.h" #endif /* HAL_SPI_MODULE_ENABLED */ #ifdef HAL_TIM_MODULE_ENABLED - #include "stm32f1xx_hal_tim.h" +#include "stm32f1xx_hal_tim.h" #endif /* HAL_TIM_MODULE_ENABLED */ #ifdef HAL_UART_MODULE_ENABLED - #include "stm32f1xx_hal_uart.h" +#include "stm32f1xx_hal_uart.h" #endif /* HAL_UART_MODULE_ENABLED */ #ifdef HAL_USART_MODULE_ENABLED - #include "stm32f1xx_hal_usart.h" +#include "stm32f1xx_hal_usart.h" #endif /* HAL_USART_MODULE_ENABLED */ #ifdef HAL_IRDA_MODULE_ENABLED - #include "stm32f1xx_hal_irda.h" +#include "stm32f1xx_hal_irda.h" #endif /* HAL_IRDA_MODULE_ENABLED */ #ifdef HAL_SMARTCARD_MODULE_ENABLED - #include "stm32f1xx_hal_smartcard.h" +#include "stm32f1xx_hal_smartcard.h" #endif /* HAL_SMARTCARD_MODULE_ENABLED */ #ifdef HAL_WWDG_MODULE_ENABLED - #include "stm32f1xx_hal_wwdg.h" +#include "stm32f1xx_hal_wwdg.h" #endif /* HAL_WWDG_MODULE_ENABLED */ #ifdef HAL_PCD_MODULE_ENABLED - #include "stm32f1xx_hal_pcd.h" +#include "stm32f1xx_hal_pcd.h" #endif /* HAL_PCD_MODULE_ENABLED */ #ifdef HAL_HCD_MODULE_ENABLED - #include "stm32f1xx_hal_hcd.h" +#include "stm32f1xx_hal_hcd.h" #endif /* HAL_HCD_MODULE_ENABLED */ #ifdef HAL_MMC_MODULE_ENABLED - #include "stm32f1xx_hal_mmc.h" +#include "stm32f1xx_hal_mmc.h" #endif /* HAL_MMC_MODULE_ENABLED */ /* Exported macro ------------------------------------------------------------*/ @@ -375,15 +375,15 @@ * @brief The assert_param macro is used for function's parameters check. * @param expr: If expr is false, it calls assert_failed function * which reports the name of the source file and the source - * line number of the call that failed. + * line number of the call that failed. * If expr is true, it returns no value. * @retval None */ - #define assert_param(expr) ((expr) ? (void)0U : assert_failed((uint8_t *)__FILE__, __LINE__)) +#define assert_param(expr) ((expr) ? (void)0U : assert_failed((uint8_t *)__FILE__, __LINE__)) /* Exported functions ------------------------------------------------------- */ - void assert_failed(uint8_t* file, uint32_t line); +void assert_failed(uint8_t *file, uint32_t line); #else - #define assert_param(expr) ((void)0U) +#define assert_param(expr) ((void)0U) #endif /* USE_FULL_ASSERT */ diff --git a/bsp/stm32f10x-HAL/drivers/stm32f1xx_it.h b/bsp/stm32f10x-HAL/drivers/stm32f1xx_it.h index 24c3f7fe5..ac3168779 100644 --- a/bsp/stm32f10x-HAL/drivers/stm32f1xx_it.h +++ b/bsp/stm32f10x-HAL/drivers/stm32f1xx_it.h @@ -36,8 +36,8 @@ #define __STM32F1xx_IT_H #ifdef __cplusplus - extern "C" { -#endif +extern "C" { +#endif /* Includes ------------------------------------------------------------------*/ #include "stm32f1xx_hal.h"