本帖最后由 XinLiArmfly 于 2018-4-6 21:22 编辑
ARM CMSIS Driver 学习 之 USART
最近把 MDK 升级到了 V5.25 ,发现 Managing Run-Time Environment 中已经有好多好多的库。相比之前已经好了太多太多,从底层驱动,到上层协议栈,常用的有不常用的也有。发现 ARM 对这套系统的更新速度加快了一些,觉得有必要学习一下。从驱动开始学起,先学 USART API 详细介绍见 CMSIS Driver USART API
USART 把收到的数据再发出去程序
- /**
- ******************************************************************************
- * @file main.c
- * @author XinLi
- * @version v1.0
- * @date 20-March-2018
- * @brief Main program body.
- ******************************************************************************
- * @attention
- *
- * <h2><center>Copyright © 2018 XinLi</center></h2>
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
- *
- ******************************************************************************
- */
- /* Header includes -----------------------------------------------------------*/
- #include "stm32f4xx.h"
- #include "Driver_USART.h"
- #include <string.h>
- /* Macro definitions ---------------------------------------------------------*/
- /* Type definitions ----------------------------------------------------------*/
- /* Variable declarations -----------------------------------------------------*/
- extern ARM_DRIVER_USART Driver_USART1;
- /* Variable definitions ------------------------------------------------------*/
- static uint8_t rxBuffer[1024] = {0};
- static uint8_t txBuffer[1024] = {0};
- /* Function declarations -----------------------------------------------------*/
- static void USART1_Callback(uint32_t event);
- static void SystemClock_Config(void);
- /* Function definitions ------------------------------------------------------*/
- /**
- * @brief Main program.
- * @param None.
- * @return None.
- */
- int main(void)
- {
- /* STM32F4xx HAL library initialization:
- - Configure the Flash prefetch, instruction and Data caches
- - Configure the Systick to generate an interrupt each 1 msec
- - Set NVIC Group Priority to 4
- - Global MSP (MCU Support Package) initialization
- */
- HAL_Init();
-
- /* Configure the system clock to 168 MHz */
- SystemClock_Config();
-
- Driver_USART1.Initialize(USART1_Callback);
- Driver_USART1.PowerControl(ARM_POWER_FULL);
- Driver_USART1.Control(ARM_USART_MODE_ASYNCHRONOUS |
- ARM_USART_DATA_BITS_8 |
- ARM_USART_PARITY_NONE |
- ARM_USART_STOP_BITS_1 |
- ARM_USART_FLOW_CONTROL_NONE, 115200);
- Driver_USART1.Control(ARM_USART_CONTROL_TX, 1);
- Driver_USART1.Control(ARM_USART_CONTROL_RX, 1);
-
- Driver_USART1.Receive(rxBuffer, sizeof(rxBuffer));
-
- for(;;)
- {
-
- }
- }
- /**
- * @brief USART1 callback function.
- * @param event: USART events notification mask.
- * @return None.
- */
- static void USART1_Callback(uint32_t event)
- {
- if(event & ARM_USART_EVENT_RX_TIMEOUT)
- {
- Driver_USART1.Control(ARM_USART_ABORT_RECEIVE, 1);
-
- uint32_t length = Driver_USART1.GetRxCount();
-
- memcpy(txBuffer, rxBuffer, length);
-
- Driver_USART1.Send(txBuffer, length);
- Driver_USART1.Receive(rxBuffer, sizeof(rxBuffer));
- }
- }
- /**
- * @brief System Clock Configuration
- * The system Clock is configured as follow :
- * System Clock source = PLL (HSE)
- * SYSCLK(Hz) = 168000000
- * HCLK(Hz) = 168000000
- * AHB Prescaler = 1
- * APB1 Prescaler = 4
- * APB2 Prescaler = 2
- * HSE Frequency(Hz) = 8000000
- * PLL_M = 8
- * PLL_N = 336
- * PLL_P = 2
- * PLL_Q = 7
- * VDD(V) = 3.3
- * Main regulator output voltage = Scale1 mode
- * Flash Latency(WS) = 5
- * @param None
- * @retval None
- */
- static void SystemClock_Config(void)
- {
- RCC_ClkInitTypeDef RCC_ClkInitStruct;
- RCC_OscInitTypeDef RCC_OscInitStruct;
- /* Enable Power Control clock */
- __HAL_RCC_PWR_CLK_ENABLE();
- /* The voltage scaling allows optimizing the power consumption when the device is
- clocked below the maximum system frequency, to update the voltage scaling value
- regarding system frequency refer to product datasheet. */
- __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
- /* Enable HSE Oscillator and activate PLL with HSE as source */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
- RCC_OscInitStruct.HSEState = RCC_HSE_ON;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- RCC_OscInitStruct.PLL.PLLM = 8;
- RCC_OscInitStruct.PLL.PLLN = 336;
- RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
- RCC_OscInitStruct.PLL.PLLQ = 7;
- HAL_RCC_OscConfig(&RCC_OscInitStruct);
-
- /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
- clocks dividers */
- RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
- HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
- /* STM32F405x/407x/415x/417x Revision Z devices: prefetch is supported */
- if (HAL_GetREVID() == 0x1001)
- {
- /* Enable the Flash prefetch */
- __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
- }
- }
复制代码
|