本帖最后由 XinLiYF 于 2018-4-14 18:02 编辑
. b; N: f" k' c: a7 _
& ]4 Z: U; i; P4 ZARM CMSIS Driver 学习 之 USART7 ]% [: e+ K6 i0 t8 o/ o
; d8 X. n$ G; t o 最近把 MDK 升级到了 V5.25 ,发现 Managing Run-Time Environment 中已经有好多好多的库。相比之前已经好了太多太多,从底层驱动,到上层协议栈,常用的有不常用的也有。发现 ARM 对这套系统的更新速度加快了一些,觉得有必要学习一下。从驱动开始学起,先学 USART API 详细介绍见 CMSIS Driver USART API
3 V- U! q. L) _' e
" o& D0 A) u7 K' O& G: B2 gUSART 把收到的数据再发出去程序
* x3 o. b! b$ T4 q- /**0 _+ r8 @6 @) t
- ******************************************************************************' p6 K- A5 \6 k
- * @file main.c; @3 Q& i- A! R; D
- * @author XinLi
" I& R% Z9 v0 O. H1 O - * @version v1.0
, c- [6 M$ o" y( G- ^3 M, H - * @date 20-March-2018
' Z4 R0 V- j9 v" a2 M, E - * @brief Main program body.
( C3 H1 X+ p8 J2 Q. u4 o: N - ******************************************************************************% s4 D( d6 ^. v7 B
- * @attention
( c5 J% J7 g2 P3 s - *- w; D$ s: |3 o0 A3 b7 i$ @
- * <h2><center>Copyright © 2018 XinLi</center></h2>
9 c, c% M! d* J% b3 p( w" W, i - *
$ v: x& O% g! F - * This program is free software: you can redistribute it and/or modify% x8 W/ D4 I( t$ N+ w
- * it under the terms of the GNU General Public License as published by
0 o5 l! u7 x" Z7 ?: O1 {% \3 s - * the Free Software Foundation, either version 3 of the License, or" y0 Z) s) Z& V: h$ l' I( l
- * (at your option) any later version.6 I; B! H1 ^" V0 a4 ]9 M5 c
- *
$ K; E; i+ q. `: a3 r9 m5 N: p - * This program is distributed in the hope that it will be useful,- z1 Q$ x7 f ~4 L) y7 R: o
- * but WITHOUT ANY WARRANTY; without even the implied warranty of9 q7 P3 g# u8 X+ s
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% D+ v5 r4 m1 X9 ?; ~- S - * GNU General Public License for more details.8 T3 s1 N- z; S
- *
, V) _2 r5 Z0 U* d - * You should have received a copy of the GNU General Public License. u9 h+ B% h0 m' N
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
9 n5 b: d: C% |5 ^/ q, U c - *
4 o9 @6 _* b& S - ******************************************************************************
9 z' ]: ~- M3 R) G4 }% I - */: k9 v' Q: l0 s ^/ l+ y
- $ H5 L0 Y6 {6 z* \/ O) E9 J
- /* Header includes -----------------------------------------------------------*/
" A# C( L j ~, S& L - #include "stm32f4xx.h": Z- C& q, |# { p+ m4 _8 [
- #include "Driver_USART.h"; _1 X# q2 r* v8 H, @
- #include <string.h># ?! D/ g4 C5 B9 P0 r3 l* g
- 1 i3 u' d; N7 M2 A* ~% k$ {6 }7 H
- /* Macro definitions ---------------------------------------------------------*/
( T5 W+ N& ` E' r5 G) m9 k - /* Type definitions ----------------------------------------------------------*/5 D" x# q' m. n+ e( D1 ]+ t$ h
- /* Variable declarations -----------------------------------------------------*/
& P. F9 V/ s. W ]0 S5 y - extern ARM_DRIVER_USART Driver_USART1;
0 {; Z5 Y" i% k" a* Z# l$ _, o: P& T - , B! h5 K7 h5 U- I: [, X" m
- /* Variable definitions ------------------------------------------------------*/
1 H7 i; T, m! Q - static uint8_t rxBuffer[1024] = {0};
% d3 S0 ?: ^+ Z) }7 H+ N2 J& B - static uint8_t txBuffer[1024] = {0};, ]' v! c9 I- w
5 G& k1 G+ l9 X1 O& w- /* Function declarations -----------------------------------------------------*/
& x/ }; ]' U% [; f' [( l2 ~2 R8 ] - static void USART1_Callback(uint32_t event);
0 ~6 S& c3 M" N8 o - static void SystemClock_Config(void);8 j# [5 p1 R, q7 u
: g! X. M: x. g# G6 ~" F3 U5 T$ g- /* Function definitions ------------------------------------------------------*/
& I) |6 o1 o I5 F" V
3 N, z! q' d3 [2 c* r- /**
! U- a( z5 J, c1 }) s" { - * @brief Main program.5 [& y$ x% L4 ~; }; }. }
- * @param None.8 n" L1 T+ J1 x
- * @return None./ u* K! t( [, D5 t& H
- */ l0 k7 |3 R- ^2 P& e; ^
- int main(void)# V8 P0 O7 A3 I% a" [
- {- B: h% E: c+ ` |, K
- /* STM32F4xx HAL library initialization:
' J! t! o, V/ S& A - - Configure the Flash prefetch, instruction and Data caches
: c: n4 K8 h8 G, s, ^2 y - - Configure the Systick to generate an interrupt each 1 msec
/ a* x- Y }( c( N/ y" x - - Set NVIC Group Priority to 4
& v1 h7 v$ A: z6 q9 E - - Global MSP (MCU Support Package) initialization
& F" \6 B& D6 \- n$ E. C" a6 V - */- }0 h9 s9 C$ h
- HAL_Init();
3 V" u% E) l: w* a$ d9 k( k -
" O' ]( G/ W/ F/ f - /* Configure the system clock to 168 MHz */
g- o1 d6 Q- z' m+ q! g0 g* g - SystemClock_Config();
* G) K) O( ~9 f) v - ( z. A# V3 x$ U
- Driver_USART1.Initialize(USART1_Callback);" y' k8 O: m1 a8 l" d. [- Q
- Driver_USART1.PowerControl(ARM_POWER_FULL);& x1 G) Z0 K1 X# ~1 B( o
- Driver_USART1.Control(ARM_USART_MODE_ASYNCHRONOUS |5 E# ]6 A2 e0 M% l: ~1 `
- ARM_USART_DATA_BITS_8 |
1 I: f+ {. Z$ `; W! D1 b - ARM_USART_PARITY_NONE | ?4 h" n9 S+ u: l& d, a! l) G
- ARM_USART_STOP_BITS_1 |
8 x- k! H5 I" `4 T0 {7 N) x1 p q - ARM_USART_FLOW_CONTROL_NONE, 115200);
( _- K4 i/ `* f - Driver_USART1.Control(ARM_USART_CONTROL_TX, 1);
2 u; y6 C+ a; D/ V - Driver_USART1.Control(ARM_USART_CONTROL_RX, 1);
$ d! c }4 g% k' }5 ^ -
2 S$ F; y' Q. A5 j6 j - Driver_USART1.Receive(rxBuffer, sizeof(rxBuffer));* U8 E% Z; R+ ]9 x% n" a
- 3 a$ n8 z) u# _. t8 X/ \
- for(;;)
- u; _, |+ w4 ~, J - {; A8 i/ b9 D' ~! J5 Z7 P
- 1 r$ G0 i- q( e) p K: r
- }3 e! _' W9 R0 W1 r8 Z
- }4 U( l0 y1 f7 ^
- " ?1 f6 J& a) I" F; B0 x1 @9 q
- /**
6 U6 J! E4 a, h3 b& n+ X; @& k% H! N - * @brief USART1 callback function.
5 t. W) A% }8 I7 f3 H - * @param event: USART events notification mask.
2 N4 v! |9 J4 b, M2 ]3 {. Q0 P) H0 Y - * @return None.# d' z# E1 [8 W% a
- */
, D9 F+ O% B) q: h/ W - static void USART1_Callback(uint32_t event): m4 Q( L! \, G# K
- {: U8 A& |! ^1 V% ? Z2 Y6 t5 B5 d: |
- if(event & ARM_USART_EVENT_RX_TIMEOUT)
% i! Z- `. a) V- ^" J; ? - {9 ~$ Y, t3 D J& T$ C+ \
- Driver_USART1.Control(ARM_USART_ABORT_RECEIVE, 1);
5 j' `) o5 l3 ^ -
; J- X G6 F7 M - uint32_t length = Driver_USART1.GetRxCount();$ A7 o. A Q+ o' h6 L, c/ |5 k
- 1 \; I& |) T( y0 W+ U# C
- memcpy(txBuffer, rxBuffer, length);
' T. i4 t5 J! v0 `; } -
. x6 n0 c6 X0 j6 u" a% |* E" k% ~ - Driver_USART1.Send(txBuffer, length);
; q. c$ [3 N5 }0 F1 b" i5 Y - Driver_USART1.Receive(rxBuffer, sizeof(rxBuffer));5 }& H& ^2 L, y( E3 O
- }4 w& Y4 x2 g+ ]" s6 S. L8 C1 P
- }
6 ?/ n. q/ v7 p5 K2 J
; g( w @) q. z) a& Q- /**& L) X2 x8 g9 \ q
- * @brief System Clock Configuration
$ n. L. z0 d- \6 g. T9 | - * The system Clock is configured as follow : 4 m! N R% a9 t5 m; K- d
- * System Clock source = PLL (HSE)
" i$ M/ Z- g% R" v6 \. @4 R0 x/ L - * SYSCLK(Hz) = 168000000
8 l b# S; s" H' v# D- Q5 F* e - * HCLK(Hz) = 168000000% M& a3 `1 K2 y( k, ^: h
- * AHB Prescaler = 17 s1 o$ m' Q- ~2 H: D5 E
- * APB1 Prescaler = 4
9 m( e8 N# r& R3 j* U) Y; u - * APB2 Prescaler = 2
) {* N0 }$ ~6 a8 E - * HSE Frequency(Hz) = 80000005 p* g% M( p& t
- * PLL_M = 8. _' U: y4 ]; K' H
- * PLL_N = 3367 ?" P* M& M8 o8 \
- * PLL_P = 25 v I, y n# m4 P, k o
- * PLL_Q = 7
& G% E3 Z2 Q5 N; ^- I) `- M6 F - * VDD(V) = 3.3
: ~. l! n( a4 j - * Main regulator output voltage = Scale1 mode+ h* w, f J8 Z1 W F# D! r
- * Flash Latency(WS) = 5( L! a j1 ? J5 R6 O% j1 _
- * @param None
6 Q, {+ ]* `# T4 g M - * @retval None) h) @% @4 X) ?' u
- */- v4 c+ l7 _) B9 y7 X ^) c+ L
- static void SystemClock_Config(void)% V. f+ i5 A0 l5 v( ~
- {. X( q1 w9 F( \# B
- RCC_ClkInitTypeDef RCC_ClkInitStruct;
6 g# C. o: A+ j9 _7 ~. x - RCC_OscInitTypeDef RCC_OscInitStruct;
3 `% s O5 N* _0 V" L% C - 3 G* D, Z# P" M: d! p9 b
- /* Enable Power Control clock */
/ E- } f4 m# r; J5 A - __HAL_RCC_PWR_CLK_ENABLE();
9 k; v7 A- t8 F) C7 L5 B" B - . Z9 F; Q& C% P' U+ k2 e' |
- /* The voltage scaling allows optimizing the power consumption when the device is 8 Z5 O. H; \3 ~& b. k
- clocked below the maximum system frequency, to update the voltage scaling value
* H& k" Z- E- @2 N+ _1 f4 j( d - regarding system frequency refer to product datasheet. */
/ a( T- c8 ]* Q& H" W - __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
& K, s- g3 c4 ^# _% N8 Q% X - 0 x$ s" u( p) z+ s; J& K& c- ^
- /* Enable HSE Oscillator and activate PLL with HSE as source */0 n% C ~- t1 ~, ]# n6 U/ ?" y
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
, _# l# X) K0 z - RCC_OscInitStruct.HSEState = RCC_HSE_ON;
0 @) y. a0 i& U - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
9 E4 O# a# U0 r8 v0 |5 O - RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
+ s- X* z- c) u0 S) w) L! x8 x - RCC_OscInitStruct.PLL.PLLM = 8;
+ m* A: t& E9 A# n - RCC_OscInitStruct.PLL.PLLN = 336;
: L+ z% z g# W$ S0 o - RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;" t4 w. L. g4 K1 {+ g# Z
- RCC_OscInitStruct.PLL.PLLQ = 7;
2 p" j6 t L7 ?8 i) W7 f2 c2 l - HAL_RCC_OscConfig(&RCC_OscInitStruct);8 N7 Q' _4 [& d. K7 O9 M2 @( m
-
8 n/ {# M3 e1 r! K4 C/ u - /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
" d* l6 H, ]+ ]! u; I) W0 V - clocks dividers */% S/ X) j7 B" ~5 D" j% t
- RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);! d. k$ a5 |$ J% s* B; l
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;& ?( X& i% L0 O. W. M
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
7 ~" S N/ V3 o# h0 h7 d - RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
8 ?% K' z& `% i" D& R6 | - RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; 2 r. u: c- ^' a% @& U- n
- HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
n' g$ D. L* a% `1 y) d i: Y& Z$ k5 O - 6 ^. _4 ?" f) p* d6 m
- /* STM32F405x/407x/415x/417x Revision Z devices: prefetch is supported */. l8 i: |6 U0 V. T
- if (HAL_GetREVID() == 0x1001)/ R7 v/ r& V9 e
- {- {0 p7 U% Y2 T
- /* Enable the Flash prefetch */* h+ u% Z! j, a: z5 L
- __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
+ {. K% e) q( a# b \9 N* l - }# @" @0 j; u. W. L! [
- }0 C8 y: b( R+ u1 i D) L# P3 l
复制代码 , s% {( a( ~2 z) I* d) `0 r9 s& Z2 v1 K
归档链接* n" |3 c! r% ^" K0 t( \
ARM CMSIS Driver 学习 之 SPI+ p; j/ {( K9 h- i) k
* P3 Z- S! r; C) A/ ]' k
|
这个出来有几年了,只不过之前没有现在全,现在可以用到项目里了,底层驱动 ARM 已经实现了跨平台,之后产品换 MCU 也比较方便。
这个趋势很好啊,避免碎片化,抽空看看
是啊,MCU 开发就是缺少这样大一统的框架啊
https://www.stmcu.org.cn/module/forum/thread-615497-1-1.html
{7 ^! o+ J* r, |. c* ~0 s& w* @8 x
// UART1_Comm_Init(115200);
UART2_Comm_Init(9600);/ R& F! [1 v6 z4 }
4 w1 Y9 {: K! p- `2 I4 t: u
Driver_USART2.Send("http://www.cmsoft.cn", sizeof("http://www.cmsoft.cn"));, R( a4 k3 @2 I
while(1);
}
程序运行到Driver_USART2.Send就卡在这一行 无法运行到while那里 同时发送的内容也没发出去
不知道咋回事????
{
if(event & ARM_USART_EVENT_RX_TIMEOUT)
{6 R) g& ]8 f+ }; ^1 u
Driver_USART2.Control(ARM_USART_ABORT_RECEIVE, 1);- Y2 {9 h% Q& d, P# K
uint32_t length = Driver_USART2.GetRxCount();3 L1 Y# f3 `& f4 `0 B* p! G
// memcpy(txBuffer, rxBuffer, length);
// + c ~9 m7 W" i$ f
// Driver_USART1.Send(txBuffer, length);
Driver_USART2.Receive(USART2_RxBfr, sizeof(USART2_RxBfr));' P& |' Z# o" q: r: o# F; Z" O
} % n1 F9 t: l6 r# W
else if(event & ARM_USART_EVENT_SEND_COMPLETE)! Q3 ~3 v+ j. Y/ e4 O' u+ G* X; U* e
{
__NOP();
}* j! b: F" _# j9 T! H
else if(event & ARM_USART_EVENT_RECEIVE_COMPLETE): Z6 T! R) ]+ k2 \
{
__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 |; Q$ D5 M0 R8 U
ARM_USART_PARITY_NONE |) o7 r- U% ~' g2 {/ g& v9 v/ ~
ARM_USART_STOP_BITS_1 |
ARM_USART_FLOW_CONTROL_NONE, Brate
);
/* Enable Receiver and Transmitter lines */
Driver_USART2.Control(ARM_USART_CONTROL_TX, 1);( I) d4 n) ]/ O$ a
Driver_USART2.Control(ARM_USART_CONTROL_RX, 1);
/* Begin to receive */
Driver_USART2.Receive(USART2_RxBfr, sizeof(USART2_RxBfr));
}
这是串口初始化