剔除多余空行格式化文件

This commit is contained in:
zyh 2018-04-17 17:26:59 +08:00
parent 74d045093b
commit a716028850
15 changed files with 272 additions and 472 deletions

View File

@ -24,12 +24,9 @@
/*@{*/ /*@{*/
void HAL_MspInit(void) void HAL_MspInit(void)
{ {
HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4); HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
/* System interrupt init*/ /* System interrupt init*/
__HAL_RCC_AFIO_CLK_ENABLE(); __HAL_RCC_AFIO_CLK_ENABLE();
/* MemoryManagement_IRQn interrupt configuration */ /* MemoryManagement_IRQn interrupt configuration */
@ -46,16 +43,14 @@ void HAL_MspInit(void)
HAL_NVIC_SetPriority(PendSV_IRQn, 15, 0); HAL_NVIC_SetPriority(PendSV_IRQn, 15, 0);
/* SysTick_IRQn interrupt configuration */ /* SysTick_IRQn interrupt configuration */
HAL_NVIC_SetPriority(SysTick_IRQn, 15, 0); HAL_NVIC_SetPriority(SysTick_IRQn, 15, 0);
/**DISABLE: JTAG-DP Disabled and SW-DP Disabled**/ /**DISABLE: JTAG-DP Disabled and SW-DP Disabled**/
__HAL_AFIO_REMAP_SWJ_NOJTAG(); __HAL_AFIO_REMAP_SWJ_NOJTAG();
} }
void SystemClock_Config(void) void SystemClock_Config(void)
{ {
RCC_OscInitTypeDef RCC_OscInitStruct; RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct; RCC_ClkInitTypeDef RCC_ClkInitStruct;
/**Initializes the CPU, AHB and APB busses clocks /**Initializes the CPU, AHB and APB busses clocks
*/ */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
@ -73,22 +68,17 @@ void SystemClock_Config(void)
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
RT_ASSERT(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) == HAL_OK); RT_ASSERT(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) == HAL_OK);
/**Configure the Systick interrupt time /**Configure the Systick interrupt time
*/ */
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / RT_TICK_PER_SECOND); HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / RT_TICK_PER_SECOND);
/**Configure the Systick /**Configure the Systick
*/ */
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK); HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
/* SysTick_IRQn interrupt configuration */ /* SysTick_IRQn interrupt configuration */
HAL_NVIC_SetPriority(SysTick_IRQn, 15, 0); HAL_NVIC_SetPriority(SysTick_IRQn, 15, 0);
} }
/** /**
* This is the timer interrupt service routine. * This is the timer interrupt service routine.
* *
@ -97,10 +87,8 @@ void SysTick_Handler(void)
{ {
/* enter interrupt */ /* enter interrupt */
rt_interrupt_enter(); rt_interrupt_enter();
HAL_IncTick(); HAL_IncTick();
rt_tick_increase(); rt_tick_increase();
/* leave interrupt */ /* leave interrupt */
rt_interrupt_leave(); rt_interrupt_leave();
} }
@ -115,15 +103,10 @@ void rt_hw_board_init(void)
#ifdef RT_USING_HEAP #ifdef RT_USING_HEAP
rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END); rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
#endif #endif
#ifdef RT_USING_COMPONENTS_INIT #ifdef RT_USING_COMPONENTS_INIT
rt_components_board_init(); rt_components_board_init();
#endif #endif
#ifdef RT_USING_CONSOLE #ifdef RT_USING_CONSOLE
rt_console_set_device(RT_CONSOLE_DEVICE_NAME); rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
#endif #endif
} }
/*@}*/

View File

@ -197,7 +197,6 @@
#define STM32_SRAM_SIZE 64 #define STM32_SRAM_SIZE 64
//#define STM32F107xC //#define STM32F107xC
#endif #endif
/* whether use board external SRAM memory */ /* whether use board external SRAM memory */
// <e>Use external SRAM memory on the board // <e>Use external SRAM memory on the board
// <i>Enable External SRAM memory // <i>Enable External SRAM memory
@ -209,10 +208,8 @@
// <i>Default: 0x68080000 // <i>Default: 0x68080000
#define STM32_EXT_SRAM_END 0x68080000 /* the end address of external SRAM */ #define STM32_EXT_SRAM_END 0x68080000 /* the end address of external SRAM */
// </e> // </e>
// <o> Internal SRAM memory size[Kbytes] <8-64> // <o> Internal SRAM memory size[Kbytes] <8-64>
// <i>Default: 64 // <i>Default: 64
#define STM32_SRAM_END (0x20000000 + STM32_SRAM_SIZE * 1024) #define STM32_SRAM_END (0x20000000 + STM32_SRAM_SIZE * 1024)
#ifdef __CC_ARM #ifdef __CC_ARM
extern int Image$$RW_IRAM1$$ZI$$Limit; extern int Image$$RW_IRAM1$$ZI$$Limit;

View File

@ -18,7 +18,6 @@
#include <board.h> #include <board.h>
#include <rtthread.h> #include <rtthread.h>
#ifdef RT_USING_PIN #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(index, gpio, gpio_index) {index, GPIO##gpio##_CLK_ENABLE, GPIO##gpio, GPIO_PIN_##gpio_index}
#define __STM32_PIN_DEFAULT {-1, 0, 0, 0} #define __STM32_PIN_DEFAULT {-1, 0, 0, 0}
@ -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 *get_pin(uint8_t pin)
{ {
const struct pin_index *index; const struct pin_index *index;
if (pin < ITEM_NUM(pins)) if (pin < ITEM_NUM(pins))
{ {
index = &pins[pin]; index = &pins[pin];
@ -560,20 +558,17 @@ const struct pin_index *get_pin(uint8_t pin)
{ {
index = RT_NULL; index = RT_NULL;
} }
return index; return index;
}; };
void stm32_pin_write(rt_device_t dev, rt_base_t pin, rt_base_t value) void stm32_pin_write(rt_device_t dev, rt_base_t pin, rt_base_t value)
{ {
const struct pin_index *index; const struct pin_index *index;
index = get_pin(pin); index = get_pin(pin);
if (index == RT_NULL) if (index == RT_NULL)
{ {
return; return;
} }
HAL_GPIO_WritePin(index->gpio, index->pin, (GPIO_PinState)value); 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; int value;
const struct pin_index *index; const struct pin_index *index;
value = PIN_LOW; value = PIN_LOW;
index = get_pin(pin); index = get_pin(pin);
if (index == RT_NULL) if (index == RT_NULL)
{ {
return value; return value;
} }
value = HAL_GPIO_ReadPin(index->gpio, index->pin); value = HAL_GPIO_ReadPin(index->gpio, index->pin);
return value; 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; const struct pin_index *index;
GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitTypeDef GPIO_InitStruct;
index = get_pin(pin); index = get_pin(pin);
if (index == RT_NULL) if (index == RT_NULL)
{ {
return; return;
} }
/* GPIO Periph clock enable */ /* GPIO Periph clock enable */
index->rcc(); index->rcc();
/* Configure GPIO_InitStructure */ /* Configure GPIO_InitStructure */
GPIO_InitStruct.Pin = index->pin; GPIO_InitStruct.Pin = index->pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
if (mode == PIN_MODE_OUTPUT) if (mode == PIN_MODE_OUTPUT)
{ {
/* output setting */ /* 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.Mode = GPIO_MODE_OUTPUT_OD;
GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Pull = GPIO_NOPULL;
} }
HAL_GPIO_Init(index->gpio, &GPIO_InitStruct); HAL_GPIO_Init(index->gpio, &GPIO_InitStruct);
} }
rt_inline rt_int32_t bit2bitno(rt_uint32_t bit) rt_inline rt_int32_t bit2bitno(rt_uint32_t bit)
{ {
int i; int i;
@ -660,6 +647,7 @@ rt_inline rt_int32_t bit2bitno(rt_uint32_t bit)
} }
return -1; return -1;
} }
rt_inline const struct pin_irq_map *get_pin_irq_map(uint32_t pinbit) rt_inline const struct pin_irq_map *get_pin_irq_map(uint32_t pinbit)
{ {
rt_int32_t mapindex = bit2bitno(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; const struct pin_index *index;
rt_base_t level; rt_base_t level;
rt_int32_t irqindex = -1; rt_int32_t irqindex = -1;
index = get_pin(pin); index = get_pin(pin);
if (index == RT_NULL) 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; return RT_ENOSYS;
} }
level = rt_hw_interrupt_disable(); level = rt_hw_interrupt_disable();
if (pin_irq_hdr_tab[irqindex].pin == pin && if (pin_irq_hdr_tab[irqindex].pin == pin &&
pin_irq_hdr_tab[irqindex].hdr == hdr && 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].mode = mode;
pin_irq_hdr_tab[irqindex].args = args; pin_irq_hdr_tab[irqindex].args = args;
rt_hw_interrupt_enable(level); rt_hw_interrupt_enable(level);
return RT_EOK; return RT_EOK;
} }
rt_err_t stm32_pin_dettach_irq(struct rt_device *device, rt_int32_t pin) rt_err_t stm32_pin_dettach_irq(struct rt_device *device, rt_int32_t pin)
{ {
const struct pin_index *index; const struct pin_index *index;
rt_base_t level; rt_base_t level;
rt_int32_t irqindex = -1; rt_int32_t irqindex = -1;
index = get_pin(pin); index = get_pin(pin);
if (index == RT_NULL) 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; return RT_ENOSYS;
} }
level = rt_hw_interrupt_disable(); level = rt_hw_interrupt_disable();
if (pin_irq_hdr_tab[irqindex].pin == -1) 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].mode = 0;
pin_irq_hdr_tab[irqindex].args = RT_NULL; pin_irq_hdr_tab[irqindex].args = RT_NULL;
rt_hw_interrupt_enable(level); rt_hw_interrupt_enable(level);
return RT_EOK; return RT_EOK;
} }
rt_err_t stm32_pin_irq_enable(struct rt_device *device, rt_base_t pin, rt_err_t stm32_pin_irq_enable(struct rt_device *device, rt_base_t pin,
rt_uint32_t enabled) 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_base_t level;
rt_int32_t irqindex = -1; rt_int32_t irqindex = -1;
GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitTypeDef GPIO_InitStruct;
index = get_pin(pin); index = get_pin(pin);
if (index == RT_NULL) 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_ENOSYS;
} }
return RT_EOK; return RT_EOK;
} }
const static struct rt_pin_ops _stm32_pin_ops = const static struct rt_pin_ops _stm32_pin_ops =
{ {
stm32_pin_mode, stm32_pin_mode,
@ -822,7 +805,6 @@ const static struct rt_pin_ops _stm32_pin_ops =
int rt_hw_pin_init(void) int rt_hw_pin_init(void)
{ {
int result; int result;
result = rt_device_pin_register("pin", &_stm32_pin_ops, RT_NULL); result = rt_device_pin_register("pin", &_stm32_pin_ops, RT_NULL);
return result; return result;
} }
@ -840,36 +822,42 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{ {
pin_irq_hdr(bit2bitno(GPIO_Pin)); pin_irq_hdr(bit2bitno(GPIO_Pin));
} }
void EXTI0_IRQHandler(void) void EXTI0_IRQHandler(void)
{ {
rt_interrupt_enter(); rt_interrupt_enter();
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0); HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0);
rt_interrupt_leave(); rt_interrupt_leave();
} }
void EXTI1_IRQHandler(void) void EXTI1_IRQHandler(void)
{ {
rt_interrupt_enter(); rt_interrupt_enter();
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_1); HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_1);
rt_interrupt_leave(); rt_interrupt_leave();
} }
void EXTI2_IRQHandler(void) void EXTI2_IRQHandler(void)
{ {
rt_interrupt_enter(); rt_interrupt_enter();
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_2); HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_2);
rt_interrupt_leave(); rt_interrupt_leave();
} }
void EXTI3_IRQHandler(void) void EXTI3_IRQHandler(void)
{ {
rt_interrupt_enter(); rt_interrupt_enter();
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_3); HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_3);
rt_interrupt_leave(); rt_interrupt_leave();
} }
void EXTI4_IRQHandler(void) void EXTI4_IRQHandler(void)
{ {
rt_interrupt_enter(); rt_interrupt_enter();
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_4); HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_4);
rt_interrupt_leave(); rt_interrupt_leave();
} }
void EXTI9_5_IRQHandler(void) void EXTI9_5_IRQHandler(void)
{ {
rt_interrupt_enter(); rt_interrupt_enter();
@ -880,6 +868,7 @@ void EXTI9_5_IRQHandler(void)
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_9); HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_9);
rt_interrupt_leave(); rt_interrupt_leave();
} }
void EXTI15_10_IRQHandler(void) void EXTI15_10_IRQHandler(void)
{ {
rt_interrupt_enter(); rt_interrupt_enter();
@ -891,5 +880,4 @@ void EXTI15_10_IRQHandler(void)
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_15); HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_15);
rt_interrupt_leave(); rt_interrupt_leave();
} }
#endif #endif

View File

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

View File

@ -25,13 +25,10 @@
#include <rtdevice.h> #include <rtdevice.h>
#include "board.h" #include "board.h"
#include "drv_sdcard.h" #include "drv_sdcard.h"
#ifndef SDIO_CLK_DIV #ifndef SDIO_CLK_DIV
#define SDIO_CLK_DIV 2 #define SDIO_CLK_DIV 2
#endif #endif
#define SDIO_TIMEOUT ((uint32_t)0x100000) #define SDIO_TIMEOUT ((uint32_t)0x100000)
static SD_HandleTypeDef hsdcard; static SD_HandleTypeDef hsdcard;
static DMA_HandleTypeDef hdma; static DMA_HandleTypeDef hdma;
static struct rt_semaphore sd_lock; static struct rt_semaphore sd_lock;
@ -57,7 +54,6 @@ rt_err_t stm32_read_blocks(uint32_t *data, uint32_t addr, uint32_t num)
uint32_t timeout = 0; uint32_t timeout = 0;
HAL_SD_StateTypeDef state_return; HAL_SD_StateTypeDef state_return;
HAL_SD_CardStateTypeDef sd_card_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.Direction = DMA_PERIPH_TO_MEMORY;
hdma.Init.PeriphInc = DMA_PINC_DISABLE; hdma.Init.PeriphInc = DMA_PINC_DISABLE;
@ -65,7 +61,6 @@ rt_err_t stm32_read_blocks(uint32_t *data, uint32_t addr, uint32_t num)
HAL_DMA_DeInit(&hdma); HAL_DMA_DeInit(&hdma);
HAL_DMA_Init(&hdma); HAL_DMA_Init(&hdma);
#endif #endif
#if defined(USING_SD_RX_DMA) #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 #else
@ -74,29 +69,26 @@ rt_err_t stm32_read_blocks(uint32_t *data, uint32_t addr, uint32_t num)
{ {
return RT_EIO; return RT_EIO;
} }
do do
{ {
state_return = HAL_SD_GetState(&hsdcard); state_return = HAL_SD_GetState(&hsdcard);
timeout++; timeout++;
}while((HAL_SD_STATE_BUSY == state_return) && (SDIO_TIMEOUT > timeout)); }
while ((HAL_SD_STATE_BUSY == state_return) && (SDIO_TIMEOUT > timeout));
if (HAL_SD_STATE_READY != state_return) if (HAL_SD_STATE_READY != state_return)
{ {
return RT_ERROR; return RT_ERROR;
} }
do do
{ {
sd_card_state_return = HAL_SD_GetCardState(&hsdcard); sd_card_state_return = HAL_SD_GetCardState(&hsdcard);
timeout++; timeout++;
}while((HAL_SD_CARD_TRANSFER != sd_card_state_return) && (SDIO_TIMEOUT > timeout)); }
while ((HAL_SD_CARD_TRANSFER != sd_card_state_return) && (SDIO_TIMEOUT > timeout));
if (SDIO_TIMEOUT <= timeout) if (SDIO_TIMEOUT <= timeout)
{ {
return RT_ETIMEOUT; return RT_ETIMEOUT;
} }
return RT_EOK; return RT_EOK;
} }
@ -105,7 +97,6 @@ rt_err_t stm32_write_blocks(uint32_t *data, uint32_t addr, uint32_t num)
uint32_t timeout = 0; uint32_t timeout = 0;
HAL_SD_StateTypeDef state_return; HAL_SD_StateTypeDef state_return;
HAL_SD_CardStateTypeDef sd_card_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.Direction = DMA_MEMORY_TO_PERIPH;
hdma.Init.PeriphInc = DMA_MINC_ENABLE; hdma.Init.PeriphInc = DMA_MINC_ENABLE;
@ -113,7 +104,6 @@ rt_err_t stm32_write_blocks(uint32_t *data, uint32_t addr, uint32_t num)
HAL_DMA_DeInit(&hdma); HAL_DMA_DeInit(&hdma);
HAL_DMA_Init(&hdma); HAL_DMA_Init(&hdma);
#endif #endif
#if defined(USING_SD_TX_DMA) #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 #else
@ -122,76 +112,64 @@ rt_err_t stm32_write_blocks(uint32_t *data, uint32_t addr, uint32_t num)
{ {
return RT_ERROR; return RT_ERROR;
} }
do do
{ {
state_return = HAL_SD_GetState(&hsdcard); state_return = HAL_SD_GetState(&hsdcard);
timeout++; timeout++;
}while((HAL_SD_STATE_BUSY == state_return) && (SDIO_TIMEOUT > timeout)); }
while ((HAL_SD_STATE_BUSY == state_return) && (SDIO_TIMEOUT > timeout));
if (HAL_SD_STATE_READY != state_return) if (HAL_SD_STATE_READY != state_return)
{ {
return RT_ERROR; return RT_ERROR;
} }
do do
{ {
sd_card_state_return = HAL_SD_GetCardState(&hsdcard); sd_card_state_return = HAL_SD_GetCardState(&hsdcard);
timeout++; timeout++;
}while((HAL_SD_CARD_TRANSFER != sd_card_state_return) && (SDIO_TIMEOUT > timeout)); }
while ((HAL_SD_CARD_TRANSFER != sd_card_state_return) && (SDIO_TIMEOUT > timeout));
if (SDIO_TIMEOUT <= timeout) if (SDIO_TIMEOUT <= timeout)
{ {
return RT_ETIMEOUT; return RT_ETIMEOUT;
} }
return RT_EOK; return RT_EOK;
} }
static rt_err_t stm32_sdcard_init(rt_device_t dev) static rt_err_t stm32_sdcard_init(rt_device_t dev)
{ {
GPIO_InitTypeDef GPIO_InitStruct; 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; return RT_ERROR;
} }
__HAL_RCC_GPIOD_CLK_ENABLE(); __HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOC_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_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_2; GPIO_InitStruct.Pin = GPIO_PIN_2;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); 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(); __HAL_RCC_DMA2_CLK_ENABLE();
hdma.Instance = DMA2_Channel4; hdma.Instance = DMA2_Channel4;
hdma.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD; hdma.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
hdma.Init.MemDataAlignment = DMA_MDATAALIGN_WORD; hdma.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
hdma.Init.Mode = DMA_NORMAL; hdma.Init.Mode = DMA_NORMAL;
hdma.Init.Priority = DMA_PRIORITY_HIGH; hdma.Init.Priority = DMA_PRIORITY_HIGH;
#if defined(USING_SD_RX_DMA) #if defined(USING_SD_RX_DMA)
hdma.Init.Direction = DMA_PERIPH_TO_MEMORY; hdma.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma.Init.PeriphInc = DMA_PINC_DISABLE; hdma.Init.PeriphInc = DMA_PINC_DISABLE;
hdma.Init.MemInc = DMA_MINC_ENABLE; hdma.Init.MemInc = DMA_MINC_ENABLE;
__HAL_LINKDMA(&hsdcard, hdmarx, hdma); __HAL_LINKDMA(&hsdcard, hdmarx, hdma);
#endif #endif
#if defined(USING_SD_TX_DMA) #if defined(USING_SD_TX_DMA)
hdma.Init.Direction = DMA_MEMORY_TO_PERIPH; hdma.Init.Direction = DMA_MEMORY_TO_PERIPH;
hdma.Init.PeriphInc = DMA_MINC_ENABLE; hdma.Init.PeriphInc = DMA_MINC_ENABLE;
hdma.Init.MemInc = DMA_PINC_DISABLE; hdma.Init.MemInc = DMA_PINC_DISABLE;
__HAL_LINKDMA(&hsdcard, hdmatx, hdma); __HAL_LINKDMA(&hsdcard, hdmatx, hdma);
#endif #endif
HAL_DMA_DeInit(&hdma); HAL_DMA_DeInit(&hdma);
if (HAL_DMA_Init(&hdma) != HAL_OK) if (HAL_DMA_Init(&hdma) != HAL_OK)
{ {
@ -199,12 +177,9 @@ static rt_err_t stm32_sdcard_init(rt_device_t dev)
return RT_EIO; return RT_EIO;
} }
#endif #endif
HAL_NVIC_SetPriority(DMA2_Channel4_5_IRQn, 3, 0); HAL_NVIC_SetPriority(DMA2_Channel4_5_IRQn, 3, 0);
HAL_NVIC_EnableIRQ(DMA2_Channel4_5_IRQn); HAL_NVIC_EnableIRQ(DMA2_Channel4_5_IRQn);
__HAL_RCC_SDIO_CLK_ENABLE(); __HAL_RCC_SDIO_CLK_ENABLE();
hsdcard.Instance = SDIO; hsdcard.Instance = SDIO;
hsdcard.Init.ClockEdge = SDIO_CLOCK_EDGE_RISING; hsdcard.Init.ClockEdge = SDIO_CLOCK_EDGE_RISING;
hsdcard.Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE; hsdcard.Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE;
@ -212,23 +187,19 @@ static rt_err_t stm32_sdcard_init(rt_device_t dev)
hsdcard.Init.BusWide = SDIO_BUS_WIDE_1B; hsdcard.Init.BusWide = SDIO_BUS_WIDE_1B;
hsdcard.Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_ENABLE; hsdcard.Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_ENABLE;
hsdcard.Init.ClockDiv = SDIO_CLK_DIV; hsdcard.Init.ClockDiv = SDIO_CLK_DIV;
HAL_SD_DeInit(&hsdcard); 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"); rt_kprintf("HAL_SD_Init error\n");
return RT_EIO; return RT_EIO;
} }
HAL_NVIC_SetPriority(SDIO_IRQn, 1, 0); HAL_NVIC_SetPriority(SDIO_IRQn, 1, 0);
HAL_NVIC_EnableIRQ(SDIO_IRQn); 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"); rt_kprintf("HAL_SD_ConfigWideBusOperation error\n");
return RT_EIO; return RT_EIO;
} }
return RT_EOK; return RT_EOK;
} }
@ -245,54 +216,43 @@ static rt_err_t stm32_sdcard_close(rt_device_t dev)
static rt_size_t stm32_sdcard_read(rt_device_t dev, rt_off_t pos, 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; int ret = RT_EOK;
rt_sem_take(&sd_lock, RT_WAITING_FOREVER); rt_sem_take(&sd_lock, RT_WAITING_FOREVER);
ret = stm32_read_blocks((uint32_t *)buffer, pos, size); ret = stm32_read_blocks((uint32_t *)buffer, pos, size);
rt_sem_release(&sd_lock); 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) 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; int ret = RT_EOK;
rt_sem_take(&sd_lock, RT_WAITING_FOREVER); rt_sem_take(&sd_lock, RT_WAITING_FOREVER);
ret = stm32_write_blocks((uint32_t *)buffer, pos, size); ret = stm32_write_blocks((uint32_t *)buffer, pos, size);
rt_sem_release(&sd_lock); rt_sem_release(&sd_lock);
if (ret != RT_EOK) if (ret != RT_EOK)
{ {
return 0; return 0;
} }
return size; return size;
} }
static rt_err_t stm32_sdcard_control(rt_device_t dev, int cmd, void *args) static rt_err_t stm32_sdcard_control(rt_device_t dev, int cmd, void *args)
{ {
RT_ASSERT(dev != RT_NULL); RT_ASSERT(dev != RT_NULL);
// RT_DEVICE_CTRL_BLK_GETGEOME // RT_DEVICE_CTRL_BLK_GETGEOME
if (cmd == RT_DEVICE_CTRL_BLK_GETGEOME) if (cmd == RT_DEVICE_CTRL_BLK_GETGEOME)
{ {
HAL_SD_CardInfoTypeDef sdcard_info; HAL_SD_CardInfoTypeDef sdcard_info;
struct rt_device_blk_geometry *geometry; struct rt_device_blk_geometry *geometry;
HAL_SD_GetCardInfo(&hsdcard, &sdcard_info); HAL_SD_GetCardInfo(&hsdcard, &sdcard_info);
geometry = (struct rt_device_blk_geometry *)args; geometry = (struct rt_device_blk_geometry *)args;
geometry->bytes_per_sector = sdcard_info.BlockSize; geometry->bytes_per_sector = sdcard_info.BlockSize;
geometry->block_size = sdcard_info.BlockSize; geometry->block_size = sdcard_info.BlockSize;
geometry->sector_count = sdcard_info.BlockNbr; geometry->sector_count = sdcard_info.BlockNbr;
} }
return RT_EOK; return RT_EOK;
} }
@ -301,7 +261,6 @@ static struct rt_device device;
int rt_hw_sdcard_init(void) 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.type = RT_Device_Class_Block;
device.init = stm32_sdcard_init; device.init = stm32_sdcard_init;
device.open = stm32_sdcard_open; device.open = stm32_sdcard_open;
@ -309,7 +268,6 @@ int rt_hw_sdcard_init(void)
device.write = stm32_sdcard_write; device.write = stm32_sdcard_write;
device.control = stm32_sdcard_control; device.control = stm32_sdcard_control;
device.close = stm32_sdcard_close; device.close = stm32_sdcard_close;
ret = rt_device_register(&device, "sd0", ret = rt_device_register(&device, "sd0",
RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_REMOVABLE |
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_RDWR |
@ -318,7 +276,6 @@ int rt_hw_sdcard_init(void)
{ {
return ret; return ret;
} }
return RT_EOK; return RT_EOK;
} }
INIT_DEVICE_EXPORT(rt_hw_sdcard_init); INIT_DEVICE_EXPORT(rt_hw_sdcard_init);

View File

@ -16,30 +16,25 @@
#include <board.h> #include <board.h>
#include <drv_spi.h> #include <drv_spi.h>
#ifdef RT_USING_SPI #ifdef RT_USING_SPI
#define SPIRXEVENT 0x01 #define SPIRXEVENT 0x01
#define SPITXEVENT 0x02 #define SPITXEVENT 0x02
#define SPITIMEOUT 2 #define SPITIMEOUT 2
#define SPICRCEN 0 #define SPICRCEN 0
struct stm32_hw_spi_cs struct stm32_hw_spi_cs
{ {
rt_uint32_t pin; rt_uint32_t pin;
}; };
struct stm32_spi struct stm32_spi
{ {
SPI_TypeDef *Instance; SPI_TypeDef *Instance;
struct rt_spi_configuration *cfg; struct rt_spi_configuration *cfg;
}; };
static rt_err_t stm32_spi_init(SPI_TypeDef *spix, struct rt_spi_configuration *cfg) static rt_err_t stm32_spi_init(SPI_TypeDef *spix, struct rt_spi_configuration *cfg)
{ {
SPI_HandleTypeDef hspi; SPI_HandleTypeDef hspi;
hspi.Instance = spix; hspi.Instance = spix;
if (cfg->mode & RT_SPI_SLAVE) if (cfg->mode & RT_SPI_SLAVE)
{ {
hspi.Init.Mode = SPI_MODE_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); __HAL_SPI_ENABLE(&hspi);
return RT_EOK; return RT_EOK;
} }
#define SPISTEP(datalen) (((datalen) == 8) ? 1 : 2) #define SPISTEP(datalen) (((datalen) == 8) ? 1 : 2)
#define SPISEND_1(reg, ptr, datalen) \ #define SPISEND_1(reg, ptr, datalen) \
do \ 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); SPIRECV_1(hspi->Instance->DR, rcvb, hspi->cfg->data_width);
return RT_EOK; return RT_EOK;
} }
static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message) static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
{ {
rt_err_t res; 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); RT_ASSERT(device->bus->parent.user_data != RT_NULL);
struct stm32_spi *hspi = (struct stm32_spi *)device->bus->parent.user_data; struct stm32_spi *hspi = (struct stm32_spi *)device->bus->parent.user_data;
struct stm32_hw_spi_cs *cs = device->parent.user_data; struct stm32_hw_spi_cs *cs = device->parent.user_data;
if (message->cs_take) if (message->cs_take)
{ {
rt_pin_write(cs->pin, 0); 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; return message->length - length;
} }
rt_err_t spi_configure(struct rt_spi_device *device, rt_err_t spi_configure(struct rt_spi_device *device,
struct rt_spi_configuration *configuration) struct rt_spi_configuration *configuration)
{ {
@ -378,34 +373,3 @@ void HAL_SPI_MspDeInit(SPI_HandleTypeDef *spiHandle)
} }
} }
#endif /*RT_USING_SPI*/ #endif /*RT_USING_SPI*/

View File

@ -18,21 +18,7 @@
#include <rtthread.h> #include <rtthread.h>
#include <rthw.h> #include <rthw.h>
#include <rtdevice.h> #include <rtdevice.h>
extern int stm32_spi_register_bus(SPI_TypeDef *SPIx, const char *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 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); extern int stm32_hw_spi_init(void);
#endif #endif

View File

@ -23,7 +23,6 @@
#include <rtdevice.h> #include <rtdevice.h>
#include <drv_usart.h> #include <drv_usart.h>
/* STM32 uart driver */ /* STM32 uart driver */
struct stm32_uart 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) static rt_err_t stm32_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
{ {
struct stm32_uart *uart; struct stm32_uart *uart;
RT_ASSERT(serial != RT_NULL); RT_ASSERT(serial != RT_NULL);
RT_ASSERT(cfg != RT_NULL); RT_ASSERT(cfg != RT_NULL);
uart = (struct stm32_uart *)serial->parent.user_data; uart = (struct stm32_uart *)serial->parent.user_data;
uart->huart.Init.BaudRate = cfg->baud_rate; uart->huart.Init.BaudRate = cfg->baud_rate;
uart->huart.Init.HwFlowCtl = UART_HWCONTROL_NONE; uart->huart.Init.HwFlowCtl = UART_HWCONTROL_NONE;
uart->huart.Init.Mode = UART_MODE_TX_RX; uart->huart.Init.Mode = UART_MODE_TX_RX;
uart->huart.Init.OverSampling = UART_OVERSAMPLING_16; uart->huart.Init.OverSampling = UART_OVERSAMPLING_16;
switch (cfg->data_bits) switch (cfg->data_bits)
{ {
case DATA_BITS_8: 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; uart->huart.Init.Parity = UART_PARITY_NONE;
break; break;
} }
if (HAL_UART_Init(&uart->huart) != HAL_OK) if (HAL_UART_Init(&uart->huart) != HAL_OK)
{ {
return RT_ERROR; return RT_ERROR;
} }
return RT_EOK; return RT_EOK;
} }
static rt_err_t stm32_control(struct rt_serial_device *serial, int cmd, void *arg) static rt_err_t stm32_control(struct rt_serial_device *serial, int cmd, void *arg)
{ {
struct stm32_uart *uart; struct stm32_uart *uart;
// rt_uint32_t ctrl_arg = (rt_uint32_t)(arg);
RT_ASSERT(serial != RT_NULL); RT_ASSERT(serial != RT_NULL);
uart = (struct stm32_uart *)serial->parent.user_data; uart = (struct stm32_uart *)serial->parent.user_data;
switch (cmd) switch (cmd)
{ {
/* disable interrupt */ /* 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) static int stm32_putc(struct rt_serial_device *serial, char c)
{ {
struct stm32_uart *uart; struct stm32_uart *uart;
RT_ASSERT(serial != RT_NULL); RT_ASSERT(serial != RT_NULL);
uart = (struct stm32_uart *)serial->parent.user_data; uart = (struct stm32_uart *)serial->parent.user_data;
while (__HAL_UART_GET_FLAG(&uart->huart, UART_FLAG_TXE) == RESET); 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; return ch;
} }
/** /**
* Uart common interrupt process. This need add to uart ISR. * 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) static void uart_isr(struct rt_serial_device *serial)
{ {
struct stm32_uart *uart = (struct stm32_uart *) serial->parent.user_data; struct stm32_uart *uart = (struct stm32_uart *) serial->parent.user_data;
RT_ASSERT(uart != RT_NULL); 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)) 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); rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
@ -186,9 +172,7 @@ void USART1_IRQHandler(void)
{ {
/* enter interrupt */ /* enter interrupt */
rt_interrupt_enter(); rt_interrupt_enter();
uart_isr(&serial1); uart_isr(&serial1);
/* leave interrupt */ /* leave interrupt */
rt_interrupt_leave(); rt_interrupt_leave();
} }
@ -281,7 +265,6 @@ int rt_hw_usart_init(void)
} }
INIT_BOARD_EXPORT(rt_hw_usart_init); INIT_BOARD_EXPORT(rt_hw_usart_init);
static void MX_USART_UART_Init(UART_HandleTypeDef *uartHandle) static void MX_USART_UART_Init(UART_HandleTypeDef *uartHandle)
{ {
uartHandle->Init.BaudRate = 115200; uartHandle->Init.BaudRate = 115200;
@ -296,15 +279,12 @@ static void MX_USART_UART_Init(UART_HandleTypeDef *uartHandle)
} }
/* USART2 init function */ /* USART2 init function */
void HAL_UART_MspInit(UART_HandleTypeDef *uartHandle) void HAL_UART_MspInit(UART_HandleTypeDef *uartHandle)
{ {
GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitTypeDef GPIO_InitStruct;
if (uartHandle->Instance == USART1) if (uartHandle->Instance == USART1)
{ {
/* USER CODE BEGIN USART1_MspInit 0 */ /* USER CODE BEGIN USART1_MspInit 0 */
/* USER CODE END USART1_MspInit 0 */ /* USER CODE END USART1_MspInit 0 */
/* USART1 clock enable */ /* USART1 clock enable */
__HAL_RCC_USART1_CLK_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.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_10; GPIO_InitStruct.Pin = GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USART1 interrupt Init */ /* USART1 interrupt Init */
HAL_NVIC_SetPriority(USART1_IRQn, 5, 0); HAL_NVIC_SetPriority(USART1_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(USART1_IRQn); HAL_NVIC_EnableIRQ(USART1_IRQn);
/* USER CODE BEGIN USART1_MspInit 1 */ /* USER CODE BEGIN USART1_MspInit 1 */
/* USER CODE END USART1_MspInit 1 */ /* USER CODE END USART1_MspInit 1 */
} }
else if (uartHandle->Instance == USART2) else if (uartHandle->Instance == USART2)
{ {
/* USER CODE BEGIN USART2_MspInit 0 */ /* USER CODE BEGIN USART2_MspInit 0 */
/* USER CODE END USART2_MspInit 0 */ /* USER CODE END USART2_MspInit 0 */
/* USART2 clock enable */ /* USART2 clock enable */
__HAL_RCC_USART2_CLK_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.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_3; GPIO_InitStruct.Pin = GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USART2 interrupt Init */ /* USART2 interrupt Init */
HAL_NVIC_SetPriority(USART2_IRQn, 5, 0); HAL_NVIC_SetPriority(USART2_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(USART2_IRQn); HAL_NVIC_EnableIRQ(USART2_IRQn);
@ -362,7 +336,6 @@ void HAL_UART_MspInit(UART_HandleTypeDef *uartHandle)
else if (uartHandle->Instance == USART3) else if (uartHandle->Instance == USART3)
{ {
/* USER CODE BEGIN USART3_MspInit 0 */ /* USER CODE BEGIN USART3_MspInit 0 */
/* USER CODE END USART3_MspInit 0 */ /* USER CODE END USART3_MspInit 0 */
/* USART3 clock enable */ /* USART3 clock enable */
__HAL_RCC_USART3_CLK_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.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_11; GPIO_InitStruct.Pin = GPIO_PIN_11;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* USART3 interrupt Init */ /* USART3 interrupt Init */
HAL_NVIC_SetPriority(USART3_IRQn, 5, 0); HAL_NVIC_SetPriority(USART3_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(USART3_IRQn); HAL_NVIC_EnableIRQ(USART3_IRQn);
/* USER CODE BEGIN USART3_MspInit 1 */ /* USER CODE BEGIN USART3_MspInit 1 */
/* USER CODE END USART3_MspInit 1 */ /* USER CODE END USART3_MspInit 1 */
} }
} }
void HAL_UART_MspDeInit(UART_HandleTypeDef *uartHandle) void HAL_UART_MspDeInit(UART_HandleTypeDef *uartHandle)
{ {
if (uartHandle->Instance == USART1) if (uartHandle->Instance == USART1)
{ {
/* USER CODE BEGIN USART1_MspDeInit 0 */ /* USER CODE BEGIN USART1_MspDeInit 0 */
/* USER CODE END USART1_MspDeInit 0 */ /* USER CODE END USART1_MspDeInit 0 */
/* Peripheral clock disable */ /* Peripheral clock disable */
__HAL_RCC_USART1_CLK_DISABLE(); __HAL_RCC_USART1_CLK_DISABLE();
/**USART1 GPIO Configuration /**USART1 GPIO Configuration
PA9 ------> USART1_TX PA9 ------> USART1_TX
PA10 ------> USART1_RX PA10 ------> USART1_RX
*/ */
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9 | GPIO_PIN_10); HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9 | GPIO_PIN_10);
/* USART1 interrupt Deinit */ /* USART1 interrupt Deinit */
HAL_NVIC_DisableIRQ(USART1_IRQn); HAL_NVIC_DisableIRQ(USART1_IRQn);
/* USER CODE BEGIN USART1_MspDeInit 1 */ /* USER CODE BEGIN USART1_MspDeInit 1 */
/* USER CODE END USART1_MspDeInit 1 */ /* USER CODE END USART1_MspDeInit 1 */
} }
else if (uartHandle->Instance == USART2) else if (uartHandle->Instance == USART2)
{ {
/* USER CODE BEGIN USART2_MspDeInit 0 */ /* USER CODE BEGIN USART2_MspDeInit 0 */
/* USER CODE END USART2_MspDeInit 0 */ /* USER CODE END USART2_MspDeInit 0 */
/* Peripheral clock disable */ /* Peripheral clock disable */
__HAL_RCC_USART2_CLK_DISABLE(); __HAL_RCC_USART2_CLK_DISABLE();
/**USART2 GPIO Configuration /**USART2 GPIO Configuration
PA2 ------> USART2_TX PA2 ------> USART2_TX
PA3 ------> USART2_RX PA3 ------> USART2_RX
*/ */
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2 | GPIO_PIN_3); HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2 | GPIO_PIN_3);
/* USART2 interrupt Deinit */ /* USART2 interrupt Deinit */
HAL_NVIC_DisableIRQ(USART2_IRQn); HAL_NVIC_DisableIRQ(USART2_IRQn);
/* USER CODE BEGIN USART2_MspDeInit 1 */ /* USER CODE BEGIN USART2_MspDeInit 1 */
/* USER CODE END USART2_MspDeInit 1 */ /* USER CODE END USART2_MspDeInit 1 */
} }
else if (uartHandle->Instance == USART3) else if (uartHandle->Instance == USART3)
{ {
/* USER CODE BEGIN USART3_MspDeInit 0 */ /* USER CODE BEGIN USART3_MspDeInit 0 */
/* USER CODE END USART3_MspDeInit 0 */ /* USER CODE END USART3_MspDeInit 0 */
/* Peripheral clock disable */ /* Peripheral clock disable */
__HAL_RCC_USART3_CLK_DISABLE(); __HAL_RCC_USART3_CLK_DISABLE();
/**USART3 GPIO Configuration /**USART3 GPIO Configuration
PB10 ------> USART3_TX PB10 ------> USART3_TX
PB11 ------> USART3_RX PB11 ------> USART3_RX
*/ */
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10 | GPIO_PIN_11); HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10 | GPIO_PIN_11);
/* USART3 interrupt Deinit */ /* USART3 interrupt Deinit */
HAL_NVIC_DisableIRQ(USART3_IRQn); HAL_NVIC_DisableIRQ(USART3_IRQn);
/* USER CODE BEGIN USART3_MspDeInit 1 */ /* USER CODE BEGIN USART3_MspDeInit 1 */
/* USER CODE END USART3_MspDeInit 1 */ /* USER CODE END USART3_MspDeInit 1 */
} }
} }

View File

@ -11,12 +11,9 @@
* Date Author Notes * Date Author Notes
* 2009-01-05 Bernard the first version * 2009-01-05 Bernard the first version
*/ */
#ifndef __USART_H__ #ifndef __USART_H__
#define __USART_H__ #define __USART_H__
#include <rthw.h> #include <rthw.h>
#include <rtthread.h> #include <rtthread.h>
int rt_hw_usart_init(void); int rt_hw_usart_init(void);
#endif #endif

View File

@ -16,9 +16,7 @@
#include <rtthread.h> #include <rtthread.h>
#include <rtdevice.h> #include <rtdevice.h>
#include "board.h" #include "board.h"
#define USB_DISCONNECT_PIN 30 //PA9 #define USB_DISCONNECT_PIN 30 //PA9
static PCD_HandleTypeDef _stm_pcd; static PCD_HandleTypeDef _stm_pcd;
static struct udcd _stm_udc; static struct udcd _stm_udc;
static struct ep_id _ep_pool[] = static struct ep_id _ep_pool[] =
@ -35,9 +33,7 @@ static struct ep_id _ep_pool[] =
void USB_LP_CAN1_RX0_IRQHandler(void) void USB_LP_CAN1_RX0_IRQHandler(void)
{ {
rt_interrupt_enter(); rt_interrupt_enter();
HAL_PCD_IRQHandler(&_stm_pcd); HAL_PCD_IRQHandler(&_stm_pcd);
rt_interrupt_leave(); rt_interrupt_leave();
} }
@ -54,7 +50,6 @@ 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) void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
{ {
if (epnum == 0) if (epnum == 0)
@ -94,7 +89,6 @@ void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
} }
} }
void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state) void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state)
{ {
if (state == 1) if (state == 1)
@ -116,7 +110,6 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef* pcdHandle)
rt_pin_write(USB_DISCONNECT_PIN, PIN_LOW); rt_pin_write(USB_DISCONNECT_PIN, PIN_LOW);
/* Peripheral clock enable */ /* Peripheral clock enable */
__HAL_RCC_USB_CLK_ENABLE(); __HAL_RCC_USB_CLK_ENABLE();
/* Peripheral interrupt init */ /* Peripheral interrupt init */
HAL_NVIC_SetPriority(USB_LP_CAN1_RX0_IRQn, 5, 0); HAL_NVIC_SetPriority(USB_LP_CAN1_RX0_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn); HAL_NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn);
@ -129,7 +122,6 @@ void HAL_PCD_MspDeInit(PCD_HandleTypeDef* pcdHandle)
{ {
/* Peripheral clock disable */ /* Peripheral clock disable */
__HAL_RCC_USB_CLK_DISABLE(); __HAL_RCC_USB_CLK_DISABLE();
/* Peripheral interrupt Deinit*/ /* Peripheral interrupt Deinit*/
HAL_NVIC_DisableIRQ(USB_LP_CAN1_RX0_IRQn); HAL_NVIC_DisableIRQ(USB_LP_CAN1_RX0_IRQn);
} }
@ -164,7 +156,6 @@ static rt_err_t _ep_enable(uep_t ep)
RT_ASSERT(ep->ep_desc != RT_NULL); RT_ASSERT(ep->ep_desc != RT_NULL);
HAL_PCD_EP_Open(&_stm_pcd, ep->ep_desc->bEndpointAddress, HAL_PCD_EP_Open(&_stm_pcd, ep->ep_desc->bEndpointAddress,
ep->ep_desc->wMaxPacketSize, ep->ep_desc->bmAttributes); ep->ep_desc->wMaxPacketSize, ep->ep_desc->bmAttributes);
return RT_EOK; return RT_EOK;
} }
@ -214,10 +205,8 @@ static rt_err_t _wakeup(void)
static rt_err_t _init(rt_device_t device) static rt_err_t _init(rt_device_t device)
{ {
PCD_HandleTypeDef *pcd; PCD_HandleTypeDef *pcd;
/* Set LL Driver parameters */ /* Set LL Driver parameters */
pcd = (PCD_HandleTypeDef *)device->user_data; pcd = (PCD_HandleTypeDef *)device->user_data;
pcd->Instance = USB; pcd->Instance = USB;
pcd->Init.dev_endpoints = 8; pcd->Init.dev_endpoints = 8;
pcd->Init.speed = PCD_SPEED_FULL; pcd->Init.speed = PCD_SPEED_FULL;
@ -225,10 +214,8 @@ static rt_err_t _init(rt_device_t device)
pcd->Init.low_power_enable = DISABLE; pcd->Init.low_power_enable = DISABLE;
pcd->Init.lpm_enable = DISABLE; pcd->Init.lpm_enable = DISABLE;
pcd->Init.battery_charging_enable = DISABLE; pcd->Init.battery_charging_enable = DISABLE;
/* Initialize LL Driver */ /* Initialize LL Driver */
HAL_PCD_Init(pcd); HAL_PCD_Init(pcd);
HAL_PCDEx_PMAConfig(pcd, 0x00, PCD_SNG_BUF, 0x18); HAL_PCDEx_PMAConfig(pcd, 0x00, PCD_SNG_BUF, 0x18);
HAL_PCDEx_PMAConfig(pcd, 0x80, PCD_SNG_BUF, 0x58); HAL_PCDEx_PMAConfig(pcd, 0x80, PCD_SNG_BUF, 0x58);
HAL_PCDEx_PMAConfig(pcd, 0x81, PCD_SNG_BUF, 0x98); HAL_PCDEx_PMAConfig(pcd, 0x81, PCD_SNG_BUF, 0x98);
@ -237,7 +224,6 @@ static rt_err_t _init(rt_device_t device)
HAL_PCDEx_PMAConfig(pcd, 0x02, PCD_SNG_BUF, 0x158); HAL_PCDEx_PMAConfig(pcd, 0x02, PCD_SNG_BUF, 0x158);
HAL_PCDEx_PMAConfig(pcd, 0x83, PCD_SNG_BUF, 0x198); HAL_PCDEx_PMAConfig(pcd, 0x83, PCD_SNG_BUF, 0x198);
HAL_PCD_Start(pcd); HAL_PCD_Start(pcd);
return RT_EOK; return RT_EOK;
} }
@ -257,11 +243,9 @@ const static struct udcd_ops _udc_ops =
_wakeup, _wakeup,
}; };
int stm_usbd_register(void) int stm_usbd_register(void)
{ {
rt_memset((void *)&_stm_udc, 0, sizeof(struct udcd)); rt_memset((void *)&_stm_udc, 0, sizeof(struct udcd));
_stm_udc.parent.type = RT_Device_Class_USBDevice; _stm_udc.parent.type = RT_Device_Class_USBDevice;
_stm_udc.parent.init = _init; _stm_udc.parent.init = _init;
_stm_udc.parent.user_data = &_stm_pcd; _stm_udc.parent.user_data = &_stm_pcd;
@ -269,10 +253,9 @@ int stm_usbd_register(void)
/* Register endpoint infomation */ /* Register endpoint infomation */
_stm_udc.ep_pool = _ep_pool; _stm_udc.ep_pool = _ep_pool;
_stm_udc.ep0.id = &_ep_pool[0]; _stm_udc.ep0.id = &_ep_pool[0];
rt_device_register((rt_device_t)&_stm_udc, "usbd", 0); rt_device_register((rt_device_t)&_stm_udc, "usbd", 0);
rt_usb_device_init(); rt_usb_device_init();
return 0; return 0;
} }
INIT_DEVICE_EXPORT(stm_usbd_register);
INIT_DEVICE_EXPORT(stm_usbd_register);

View File

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