你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

ARM CMSIS Driver 学习 之 USART

[复制链接]
XinLiYF 发布时间:2018-4-6 19:47
本帖最后由 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 把收到的数据再发出去程序
  1. /**
  2.   ******************************************************************************
  3.   * @file    main.c
  4.   * @author  XinLi
  5.   * @version v1.0
  6.   * @date    20-March-2018
  7.   * @brief   Main program body.
  8.   ******************************************************************************
  9.   * @attention
  10.   *
  11.   * <h2><center>Copyright © 2018 XinLi</center></h2>
  12.   *
  13.   * This program is free software: you can redistribute it and/or modify
  14.   * it under the terms of the GNU General Public License as published by
  15.   * the Free Software Foundation, either version 3 of the License, or
  16.   * (at your option) any later version.
  17.   *
  18.   * This program is distributed in the hope that it will be useful,
  19.   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.   * GNU General Public License for more details.
  22.   *
  23.   * You should have received a copy of the GNU General Public License
  24.   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  25.   *
  26.   ******************************************************************************
  27.   */

  28. /* Header includes -----------------------------------------------------------*/
  29. #include "stm32f4xx.h"
  30. #include "Driver_USART.h"
  31. #include <string.h>

  32. /* Macro definitions ---------------------------------------------------------*/
  33. /* Type definitions ----------------------------------------------------------*/
  34. /* Variable declarations -----------------------------------------------------*/
  35. extern ARM_DRIVER_USART Driver_USART1;

  36. /* Variable definitions ------------------------------------------------------*/
  37. static uint8_t rxBuffer[1024] = {0};
  38. static uint8_t txBuffer[1024] = {0};

  39. /* Function declarations -----------------------------------------------------*/
  40. static void USART1_Callback(uint32_t event);
  41. static void SystemClock_Config(void);

  42. /* Function definitions ------------------------------------------------------*/

  43. /**
  44.   * @brief  Main program.
  45.   * @param  None.
  46.   * @return None.
  47.   */
  48. int main(void)
  49. {
  50.   /* STM32F4xx HAL library initialization:
  51.        - Configure the Flash prefetch, instruction and Data caches
  52.        - Configure the Systick to generate an interrupt each 1 msec
  53.        - Set NVIC Group Priority to 4
  54.        - Global MSP (MCU Support Package) initialization
  55.      */
  56.   HAL_Init();
  57.   
  58.   /* Configure the system clock to 168 MHz */
  59.   SystemClock_Config();
  60.   
  61.   Driver_USART1.Initialize(USART1_Callback);
  62.   Driver_USART1.PowerControl(ARM_POWER_FULL);
  63.   Driver_USART1.Control(ARM_USART_MODE_ASYNCHRONOUS |
  64.                         ARM_USART_DATA_BITS_8 |
  65.                         ARM_USART_PARITY_NONE |
  66.                         ARM_USART_STOP_BITS_1 |
  67.                         ARM_USART_FLOW_CONTROL_NONE, 115200);
  68.   Driver_USART1.Control(ARM_USART_CONTROL_TX, 1);
  69.   Driver_USART1.Control(ARM_USART_CONTROL_RX, 1);
  70.   
  71.   Driver_USART1.Receive(rxBuffer, sizeof(rxBuffer));
  72.   
  73.   for(;;)
  74.   {
  75.    
  76.   }
  77. }

  78. /**
  79.   * @brief  USART1 callback function.
  80.   * @param  event: USART events notification mask.
  81.   * @return None.
  82.   */
  83. static void USART1_Callback(uint32_t event)
  84. {
  85.   if(event & ARM_USART_EVENT_RX_TIMEOUT)
  86.   {
  87.     Driver_USART1.Control(ARM_USART_ABORT_RECEIVE, 1);
  88.    
  89.     uint32_t length = Driver_USART1.GetRxCount();
  90.    
  91.     memcpy(txBuffer, rxBuffer, length);
  92.    
  93.     Driver_USART1.Send(txBuffer, length);
  94.     Driver_USART1.Receive(rxBuffer, sizeof(rxBuffer));
  95.   }
  96. }

  97. /**
  98.   * @brief  System Clock Configuration
  99.   *         The system Clock is configured as follow :
  100.   *            System Clock source            = PLL (HSE)
  101.   *            SYSCLK(Hz)                     = 168000000
  102.   *            HCLK(Hz)                       = 168000000
  103.   *            AHB Prescaler                  = 1
  104.   *            APB1 Prescaler                 = 4
  105.   *            APB2 Prescaler                 = 2
  106.   *            HSE Frequency(Hz)              = 8000000
  107.   *            PLL_M                          = 8
  108.   *            PLL_N                          = 336
  109.   *            PLL_P                          = 2
  110.   *            PLL_Q                          = 7
  111.   *            VDD(V)                         = 3.3
  112.   *            Main regulator output voltage  = Scale1 mode
  113.   *            Flash Latency(WS)              = 5
  114.   * @param  None
  115.   * @retval None
  116.   */
  117. static void SystemClock_Config(void)
  118. {
  119.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  120.   RCC_OscInitTypeDef RCC_OscInitStruct;

  121.   /* Enable Power Control clock */
  122.   __HAL_RCC_PWR_CLK_ENABLE();

  123.   /* The voltage scaling allows optimizing the power consumption when the device is
  124.      clocked below the maximum system frequency, to update the voltage scaling value
  125.      regarding system frequency refer to product datasheet.  */
  126.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  127.   /* Enable HSE Oscillator and activate PLL with HSE as source */
  128.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  129.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  130.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  131.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  132.   RCC_OscInitStruct.PLL.PLLM = 8;
  133.   RCC_OscInitStruct.PLL.PLLN = 336;
  134.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  135.   RCC_OscInitStruct.PLL.PLLQ = 7;
  136.   HAL_RCC_OscConfig(&RCC_OscInitStruct);
  137.   
  138.   /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
  139.      clocks dividers */
  140.   RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  141.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  142.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  143.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;  
  144.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;  
  145.   HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);

  146.   /* STM32F405x/407x/415x/417x Revision Z devices: prefetch is supported  */
  147.   if (HAL_GetREVID() == 0x1001)
  148.   {
  149.     /* Enable the Flash prefetch */
  150.     __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
  151.   }
  152. }
复制代码

归档链接
ARM CMSIS Driver 学习 之 SPI

收藏 1 评论9 发布时间:2018-4-6 19:47

举报

9个回答
黑皮男 回答时间:2018-4-6 20:56:15
CMSIS_Driver 还真没见过之前
XinLiYF 回答时间:2018-4-6 21:18:56
黑皮男 发表于 2018-4-6 20:56
CMSIS_Driver 还真没见过之前

这个出来有几年了,只不过之前没有现在全,现在可以用到项目里了,底层驱动 ARM 已经实现了跨平台,之后产品换 MCU 也比较方便。
黑皮男 回答时间:2018-4-6 21:43:04
XinLiYF 发表于 2018-4-6 21:18
这个出来有几年了,只不过之前没有现在全,现在可以用到项目里了,底层驱动 ARM 已经实现了跨平台,之后 ...

这个趋势很好啊,避免碎片化,抽空看看
XinLiYF 回答时间:2018-4-7 08:17:15
黑皮男 发表于 2018-4-6 21:43
这个趋势很好啊,避免碎片化,抽空看看

是啊,MCU 开发就是缺少这样大一统的框架啊
x5y4z3 回答时间:2018-4-7 13:48:47
这 CMSIS 的 UART 驱动以前还真没见过,谢谢楼主的告知,赶紧来试试学习一下。
zero99 回答时间:2018-4-17 14:47:42
感谢分享,请汇总到4月技术原创
https://www.stmcu.org.cn/module/forum/thread-615497-1-1.html
wlx583193113 回答时间:2020-9-16 16:07:23
int main(void)
{
//        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那里   同时发送的内容也没发出去
不知道咋回事????
wlx583193113 回答时间:2020-9-16 16:08:39
void USART2_Callback(uint32_t event)
{
  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();
        }
}
从电脑端的串口工具发送内容  也没产生串口回调事件   不知道咋回事?????
wlx583193113 回答时间:2020-9-16 16:09:14
void UART2_Comm_Init(uint32_t Brate)
{//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));
}
这是串口初始化

所属标签

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版