本帖最后由 XinLiYF 于 2018-4-14 18:02 编辑 # e3 P8 r8 g) k; M! q
7 U% V' e/ U! {% jARM CMSIS Driver 学习 之 USART
( s- r! ?, ^8 {0 T L & P& f. S" d$ J& `1 C
最近把 MDK 升级到了 V5.25 ,发现 Managing Run-Time Environment 中已经有好多好多的库。相比之前已经好了太多太多,从底层驱动,到上层协议栈,常用的有不常用的也有。发现 ARM 对这套系统的更新速度加快了一些,觉得有必要学习一下。从驱动开始学起,先学 USART API 详细介绍见 CMSIS Driver USART API, S* y' B& q+ c5 f; T
1 D) `1 K) K% v1 D N9 [8 u
USART 把收到的数据再发出去程序, b2 X) n+ }8 Y
- /**" j2 y H' K0 X* Y2 z
- ******************************************************************************
4 b& Z2 r. K5 @. g, V [. V! ]: N - * @file main.c
: e ]& t" c4 n; l4 E" l/ i - * @author XinLi
/ O: b& d( g$ |2 p% L4 Z' O - * @version v1.0
9 B5 x% V- n$ {0 N8 @ - * @date 20-March-2018
7 R* @* f! d+ k4 U( t/ Q X3 U6 E - * @brief Main program body." V+ n3 E$ y: L" {1 r. {
- ******************************************************************************0 `6 j# t& e9 l! H E* M
- * @attention
+ T. |! f& }. y# |& m - *! z7 t( X/ P4 U
- * <h2><center>Copyright © 2018 XinLi</center></h2>
( s G$ P! Y8 e% g3 e5 B# y$ @ - *
! A- e- }3 e- R' k - * This program is free software: you can redistribute it and/or modify) \+ a; o( D h
- * it under the terms of the GNU General Public License as published by
0 E- F( E( I8 x) S - * the Free Software Foundation, either version 3 of the License, or N+ @% I9 T! i, d& G
- * (at your option) any later version.
! o, l& J1 E- n) }0 Y1 c }/ @ - *7 a4 [$ Y6 ~" t
- * This program is distributed in the hope that it will be useful,3 n8 G* [# J% Z" ^& a8 \! {
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
) z4 x; x# d/ I1 L7 B# [4 \ - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0 w6 Z: T0 ?& `5 c! k5 X+ \4 { - * GNU General Public License for more details.! B- t6 [5 j) \5 Z
- *0 d) v& Y5 _5 o \
- * You should have received a copy of the GNU General Public License
! r: y# L8 m3 [& v/ G/ _ c& w - * along with this program. If not, see <http://www.gnu.org/licenses/>.
4 d- |% Y2 w+ j/ \; R8 e - *
$ Q, G3 G+ m- z$ z - ******************************************************************************
# W# [9 p/ b9 s% y$ [ - */3 ~9 W) \; @' n3 w! j0 M6 I
- : B" u$ c/ C3 Y$ w
- /* Header includes -----------------------------------------------------------*/
' u2 ]+ Q: D* r$ Z# i9 J' {' K& q! G - #include "stm32f4xx.h": ]' z" L- `: z+ h; p- L4 }! V
- #include "Driver_USART.h"3 y8 h" ^7 U5 m, s; l
- #include <string.h>
; B( t5 U: v/ I9 V& }
9 K7 R% j6 V8 f- /* Macro definitions ---------------------------------------------------------*/: u- x' {8 ]8 I- v/ a
- /* Type definitions ----------------------------------------------------------*/: g% m. l- X6 l% a Y& Z4 s
- /* Variable declarations -----------------------------------------------------*/
2 Y6 y' A [; g; G. x8 X - extern ARM_DRIVER_USART Driver_USART1;
( u4 X6 N7 j; e# Q+ r/ f - + j2 _9 P, ]9 t, W; I" |9 ]2 {
- /* Variable definitions ------------------------------------------------------*/
5 j1 ~8 d3 ?9 V* v1 G1 i - static uint8_t rxBuffer[1024] = {0};
1 m4 D: R- l) g1 o, c - static uint8_t txBuffer[1024] = {0};
- f& Q5 G" u5 g, F% N' M9 Q: ~% S
7 j" g- i9 ~4 h) b' C- /* Function declarations -----------------------------------------------------*/
& {) V# N5 K# F' M2 q - static void USART1_Callback(uint32_t event);: W5 E' G3 j! O" M. A, b
- static void SystemClock_Config(void);* p! V& e e( O9 `1 N
) m$ H, F% F) R S6 x! S$ A7 x$ N- /* Function definitions ------------------------------------------------------*/. m/ K8 s4 e1 F2 j2 [* t
- 7 J- u' P( H' N2 D5 A7 s
- /**
- r! c) g- U# J6 ~0 l7 M/ H8 e - * @brief Main program.* u: i5 y* c$ R3 x
- * @param None.
) [0 }1 h/ }- m3 T" c7 ~8 Z) b - * @return None.
, C9 u1 O: Y9 o! I+ _9 c0 C: A - */( N- r/ y4 M. b; B. [' B4 v
- int main(void)9 s& F/ o( m) V6 T
- {( y; ?# {9 H2 X" f
- /* STM32F4xx HAL library initialization:2 ?% r# L; T! D6 n; \8 J6 B. I
- - Configure the Flash prefetch, instruction and Data caches
! E+ ^0 I( A) Y/ e _% P9 W2 N - - Configure the Systick to generate an interrupt each 1 msec
. A9 |/ Y* H. Q0 J, f- x3 y - - Set NVIC Group Priority to 4$ h0 l* a: t6 E- u4 O! F6 _
- - Global MSP (MCU Support Package) initialization
' Q: t: e* f7 V2 \ - */
, m# `+ ^8 B. D) Q6 m+ F, m - HAL_Init();8 @, e3 I+ V& D- {- q9 J
-
5 h \; m. O: Z. ? - /* Configure the system clock to 168 MHz */
6 v o8 f' ]# n0 q - SystemClock_Config();! y5 s" d: i- A; q; J
- 5 l. a# \1 V% D" ^, w! ?6 i
- Driver_USART1.Initialize(USART1_Callback);& q" ~9 X5 M- M3 u
- Driver_USART1.PowerControl(ARM_POWER_FULL);" V5 ^5 v. L1 _3 Y( c* D" o* T
- Driver_USART1.Control(ARM_USART_MODE_ASYNCHRONOUS | s7 r: ^. r% b1 L6 d7 S
- ARM_USART_DATA_BITS_8 |
: ?2 C& _' A* ^# p8 s4 V - ARM_USART_PARITY_NONE |
, S) ^1 h0 ^+ B# ]* u - ARM_USART_STOP_BITS_1 |5 l0 O- j) a. h* y1 J
- ARM_USART_FLOW_CONTROL_NONE, 115200);
4 G5 }+ m6 G9 g- B e/ ^4 K - Driver_USART1.Control(ARM_USART_CONTROL_TX, 1);
& ~5 q& [/ [+ z" u - Driver_USART1.Control(ARM_USART_CONTROL_RX, 1);6 @! P3 k I* X9 r3 f! A0 W
-
1 w2 C$ u7 d+ h [ - Driver_USART1.Receive(rxBuffer, sizeof(rxBuffer));* ^# Y: \# n. A% u) c
-
+ C. f9 Q) Y" G' W1 O - for(;;)1 [, J- {$ U* r$ N) l1 n3 R
- {4 d' D$ T' Y8 x7 p7 S$ k8 p
-
- r; y) q' E0 P/ r% T* J* }7 o - }
+ }. X, u6 k: g% j - }
* n5 A" x3 ]: X8 b3 f - 2 ~0 z6 w' P) p3 s& Q1 o. i* c8 T
- /**: J, q8 G% B6 a0 ] s
- * @brief USART1 callback function.
2 q9 G0 R1 `+ [! a) _4 Q - * @param event: USART events notification mask.5 |8 I- b+ S' V: \9 O* i3 ^
- * @return None.
% @+ v, F2 L& Z9 M; ?! z9 V - */' s; W+ J( Q+ H+ `1 M! ?4 g
- static void USART1_Callback(uint32_t event)
/ ]- H, W; h& ]9 `6 W6 T5 r - {+ G% W& D1 A1 M( U& L
- if(event & ARM_USART_EVENT_RX_TIMEOUT)0 l6 F6 }" w0 w) t- j; h" ~
- {
- H. h( h0 q( d. \. ?4 d. |1 j& l - Driver_USART1.Control(ARM_USART_ABORT_RECEIVE, 1);
, q B3 m$ q! N( O: ~: ^* k" v$ C - ' m" T3 \& G/ c3 _& {
- uint32_t length = Driver_USART1.GetRxCount();
. s' R" j& P) `& ]/ N: {5 z0 S -
+ y5 M, K9 s9 v - memcpy(txBuffer, rxBuffer, length);# z8 Y1 T! t" s4 Y2 x5 c6 P
- . q7 F% P7 [- k# i
- Driver_USART1.Send(txBuffer, length);: k4 N& i Q/ P" S
- Driver_USART1.Receive(rxBuffer, sizeof(rxBuffer));
7 |, q% P; \! E4 S2 ^' ]5 w+ v$ y - }
+ B/ M) P9 n0 V m; ?6 v. O - }, {- g% R! m5 K
- 3 w1 R% b2 `) `; Q+ L
- /**: ?( k% |# z. K6 J' w% X
- * @brief System Clock Configuration
1 w9 f$ e. {' q' @& @ - * The system Clock is configured as follow : . F3 P# T9 q' B! C8 @: e, `
- * System Clock source = PLL (HSE)
5 H2 F V6 |8 Y2 b$ k - * SYSCLK(Hz) = 168000000% V$ _7 l+ r3 a, }7 V* ?7 F& [ w
- * HCLK(Hz) = 1680000008 r( O( H7 @, ~ H: O2 N
- * AHB Prescaler = 1
& K- k0 A$ n( {+ N. d8 p+ K( n8 k - * APB1 Prescaler = 44 d9 I8 M3 u6 ^
- * APB2 Prescaler = 2
; w1 p+ `+ z9 u3 @% L0 c+ b - * HSE Frequency(Hz) = 8000000" Y' ]9 O7 Q) X9 T1 g
- * PLL_M = 8
- i) i) k- \8 s3 Q" l - * PLL_N = 336
p4 I3 _3 z# u# u, Y - * PLL_P = 2
; b! w) ]' t6 _; L9 D: q9 l - * PLL_Q = 7
& i/ s, ~: X ?& @, I! t, ]+ |% ] - * VDD(V) = 3.3
& R/ @* U. ?+ E+ m* Y3 L. A - * Main regulator output voltage = Scale1 mode
' K5 m6 U" T5 L% U( c2 o - * Flash Latency(WS) = 5; o; N/ h( K- O- `
- * @param None
! T9 H6 d" [4 P6 f! X+ J2 v - * @retval None
; _$ d0 d- R! A) I - */
! E! k5 ~+ _! K4 \3 S8 K - static void SystemClock_Config(void)$ k* O8 J' O, m9 r8 C P6 a; m
- {2 x# ^ v. _" _" r2 A; R
- RCC_ClkInitTypeDef RCC_ClkInitStruct;% {% ]: c& l- z) |
- RCC_OscInitTypeDef RCC_OscInitStruct;! K: x# A; \3 w `
% q' F; {" o; H* i4 `- /* Enable Power Control clock */
$ P- G+ g$ x$ u2 a$ [/ @ - __HAL_RCC_PWR_CLK_ENABLE();, l$ }( b u' q
3 s0 V2 l* P& a9 k( t+ N" ]6 i- /* The voltage scaling allows optimizing the power consumption when the device is 9 {. k6 ]2 j6 P/ Y. w3 R
- clocked below the maximum system frequency, to update the voltage scaling value
7 Y+ n- k. o5 o3 {3 N; G - regarding system frequency refer to product datasheet. */
% W5 G. P, c) w } - __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/ N# s! ]' V1 A5 Z8 w7 z
u; S. g& h5 n; }" d- /* Enable HSE Oscillator and activate PLL with HSE as source */
# u) _( z, k& @$ P" s - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;& `5 Q/ S) e* i1 U5 g1 \
- RCC_OscInitStruct.HSEState = RCC_HSE_ON;# i2 f# T/ D9 E. \' _+ ~7 s& t( i
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;/ F' m9 I4 z' }
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
( f' u* |% B4 C - RCC_OscInitStruct.PLL.PLLM = 8;& J6 Q" z+ L4 T1 v7 D4 q
- RCC_OscInitStruct.PLL.PLLN = 336;( q9 K V7 z- q5 p- h; T* d
- RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;8 k6 q. j/ U6 ?9 e+ B2 x
- RCC_OscInitStruct.PLL.PLLQ = 7;
) T9 Q7 Y4 I. T! Q1 y. r - HAL_RCC_OscConfig(&RCC_OscInitStruct);
3 q w1 L) o( O% W$ t/ A3 n, | -
9 v) k, b8 v: e) F) Y- r. U& B - /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 8 F. x3 ?* F% T \7 |& u- a
- clocks dividers */* Y( ]) Z( [( X8 D0 n
- RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);/ J5 g5 R, Z3 s% G& l- i
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;* }- \0 L' P0 T6 H
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;; V& |" F; n) X3 c! \. @3 R: ?
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
5 h: h/ O F; R# b - RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; 1 _- `- l2 B6 q$ D3 D1 ^
- HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);' Y# Z& `7 \6 A/ O8 d( s0 O. s5 B0 p
4 J0 V M* u1 c6 I8 `- /* STM32F405x/407x/415x/417x Revision Z devices: prefetch is supported */" _( R3 V1 ] g- g! O0 I% ?" j
- if (HAL_GetREVID() == 0x1001)* e( B# r5 R- q* o/ ?! f" l7 K$ n0 o
- {
3 m: N A0 D+ w6 _6 E; Y - /* Enable the Flash prefetch */$ `4 R; ?8 {0 I3 _5 Y6 Y/ f
- __HAL_FLASH_PREFETCH_BUFFER_ENABLE();. S& x( v, u0 l
- }
) l N- K: A/ g4 h - }
& F3 D5 `( J- J: y: B2 k: C
复制代码 ; ]0 t- E/ G( d, I
归档链接
( k2 y& {! `" @ARM CMSIS Driver 学习 之 SPI
6 [( i" V9 N. |1 ?/ G
7 Y2 F$ o4 R3 S* F r: ^) j( ?* x |
这个出来有几年了,只不过之前没有现在全,现在可以用到项目里了,底层驱动 ARM 已经实现了跨平台,之后产品换 MCU 也比较方便。
这个趋势很好啊,避免碎片化,抽空看看
是啊,MCU 开发就是缺少这样大一统的框架啊
https://www.stmcu.org.cn/module/forum/thread-615497-1-1.html
{
// UART1_Comm_Init(115200);
UART2_Comm_Init(9600);! B1 B* t5 G# H) m0 e
3 e( X8 Q% f: g+ _
Driver_USART2.Send("http://www.cmsoft.cn", sizeof("http://www.cmsoft.cn"));7 }5 E3 A9 ?$ d8 v4 z) o
while(1);
}
程序运行到Driver_USART2.Send就卡在这一行 无法运行到while那里 同时发送的内容也没发出去
不知道咋回事????
{( C, G0 x @6 D. M: o
if(event & ARM_USART_EVENT_RX_TIMEOUT)
{
Driver_USART2.Control(ARM_USART_ABORT_RECEIVE, 1);
uint32_t length = Driver_USART2.GetRxCount();
/ k5 i; z9 c2 ^% [
// memcpy(txBuffer, rxBuffer, length);: p U- v) X: e# ^6 F5 G
// , E3 I) Z# W) F7 _
// Driver_USART1.Send(txBuffer, length);
Driver_USART2.Receive(USART2_RxBfr, sizeof(USART2_RxBfr));
} & X! e7 t0 F( |9 r- k1 b
else if(event & ARM_USART_EVENT_SEND_COMPLETE)
{. \7 @$ ~) i. d
__NOP();. r( [# q" b5 _( |3 H0 }; J
}$ v( L" t: N' @/ r
else if(event & ARM_USART_EVENT_RECEIVE_COMPLETE): C% D% d$ ^* |* [
{1 Y) S8 w w& @7 L+ F, S* `1 ^
__NOP();
}
}+ r% f, y2 ?: }
从电脑端的串口工具发送内容 也没产生串口回调事件 不知道咋回事?????
{//usb
Driver_USART2.Initialize(USART2_Callback);6 A3 b* W7 |( [, J
Driver_USART2.PowerControl(ARM_POWER_FULL);2 Z! Q6 r2 z% |0 t) Q
Driver_USART2.Control(ARM_USART_MODE_ASYNCHRONOUS |
ARM_USART_DATA_BITS_8 |$ Q$ [, U) _" B6 g( Z
ARM_USART_PARITY_NONE |
ARM_USART_STOP_BITS_1 |. @# d; w+ O( C
ARM_USART_FLOW_CONTROL_NONE, Brate
);+ a% o4 o( Z p/ K9 D( |8 r, h
/* Enable Receiver and Transmitter lines */
Driver_USART2.Control(ARM_USART_CONTROL_TX, 1);$ ?" f8 g) b8 _) y* l
Driver_USART2.Control(ARM_USART_CONTROL_RX, 1);
/* Begin to receive */% ~: G, ~1 q6 L/ r' t- e! B+ K
Driver_USART2.Receive(USART2_RxBfr, sizeof(USART2_RxBfr));
}) R8 Y' l! L& c8 a. K4 d* B2 Q
这是串口初始化