本帖最后由 XinLiYF 于 2018-4-14 18:02 编辑
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 <http://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();
- }
- }
复制代码
归档链接
ARM CMSIS Driver 学习 之 SPI
|
这个出来有几年了,只不过之前没有现在全,现在可以用到项目里了,底层驱动 ARM 已经实现了跨平台,之后产品换 MCU 也比较方便。
这个趋势很好啊,避免碎片化,抽空看看
是啊,MCU 开发就是缺少这样大一统的框架啊
https://www.stmcu.org.cn/module/forum/thread-615497-1-1.html
{
// UART1_Comm_Init(115200);
UART2_Comm_Init(9600);
Driver_USART2.Send("http://www.cmsoft.cn", sizeof("http://www.cmsoft.cn"));
while(1);
}
程序运行到Driver_USART2.Send就卡在这一行 无法运行到while那里 同时发送的内容也没发出去
不知道咋回事????
{
if(event & ARM_USART_EVENT_RX_TIMEOUT)
{
Driver_USART2.Control(ARM_USART_ABORT_RECEIVE, 1);
uint32_t length = Driver_USART2.GetRxCount();
// memcpy(txBuffer, rxBuffer, length);
//
// Driver_USART1.Send(txBuffer, length);
Driver_USART2.Receive(USART2_RxBfr, sizeof(USART2_RxBfr));
}
else if(event & ARM_USART_EVENT_SEND_COMPLETE)
{
__NOP();
}
else if(event & ARM_USART_EVENT_RECEIVE_COMPLETE)
{
__NOP();
}
}
从电脑端的串口工具发送内容 也没产生串口回调事件 不知道咋回事?????
{//usb
Driver_USART2.Initialize(USART2_Callback);
Driver_USART2.PowerControl(ARM_POWER_FULL);
Driver_USART2.Control(ARM_USART_MODE_ASYNCHRONOUS |
ARM_USART_DATA_BITS_8 |
ARM_USART_PARITY_NONE |
ARM_USART_STOP_BITS_1 |
ARM_USART_FLOW_CONTROL_NONE, Brate
);
/* Enable Receiver and Transmitter lines */
Driver_USART2.Control(ARM_USART_CONTROL_TX, 1);
Driver_USART2.Control(ARM_USART_CONTROL_RX, 1);
/* Begin to receive */
Driver_USART2.Receive(USART2_RxBfr, sizeof(USART2_RxBfr));
}
这是串口初始化