|
使用Platformio平台的libopencm3开发框架来开发STM32G0,以下为串口中断的使用。 & v# q2 \& \+ ]& J3 ?; C
1 新建项目建立uart项目 ; I: o" K; q, |, }6 v
在PIO的Home页面新建项目,项目名称uart,选择开发板为上一次建立的自定义开发板型号 MonkeyPi_STM32_G070RB,开发框架选择libopencm3; - 1upload_protocol = cmsis-dap
: \* s. g$ b: x, H3 {% f) f+ A - 2debug_tool = cmsis-dap
复制代码
4 r( f5 a9 g; P9 L0 x$ `: T' T9 Z( d9 J5 s" x4 X+ b
2 编写程序- 1//system clock$ }! b( T% N# s; ?7 B
- 2rcc_clock_setup(&rcc_clock_config[RCC_CLOCK_CONFIG_HSI_PLL_64MHZ]);
; I' c6 q6 k; ^6 |' N$ @- z - 3
6 I7 h+ A( ]* {' v - 4//uart pin
4 C& O* B/ o. _ - 5rcc_periph_clock_enable(RCC_USART1);! n& j P0 p/ @+ K/ n$ B
- 6rcc_periph_clock_enable(RCC_GPIOB);
复制代码
# Q& p* ?$ N& W- t$ `: f u- J( p! n, J) G+ u) P. z. |
先设置系统时钟为内部PLL生成64MHz,然后设置串口外设和串口引脚外设的时钟; 引脚复用功能设置
% e; a+ V* k* K8 z9 l' m0 j
- 1gpio_mode_setup(GPIOB,GPIO_MODE_AF,GPIO_PUPD_NONE,GPIO6|GPIO7);
& t( v: ?& N2 Q - 2gpio_set_af(GPIOB,GPIO_AF0,GPIO6|GPIO7);
复制代码
) h1 x4 n4 S3 X! B; W5 G. H; @ a, z3 l
根据芯片datasheet文档,使用USART1其引脚为PA9\PA10,复用功能AF1为串口功能; ' Q9 v! o4 i% V
串口设置
4 A1 t% `/ y; _/ x6 {
- 1usart_set_baudrate(USART1,115200);* [% a) u! ~' h8 K% B% C
- 2usart_set_databits(USART1,8);1 a. J) m8 h) Z! K. F3 V' U2 M
- 3usart_set_stopbits(USART1,USART_STOPBITS_1);
2 e0 h9 a a- o$ j+ T) C) K - 4usart_set_parity(USART1,USART_PARITY_NONE);
% P3 `7 z% j. E( \3 j" a% S8 e - 5usart_set_flow_control(USART1,USART_FLOWCONTROL_NONE);
5 Z% o+ q( I' j$ @9 T - 6usart_set_mode(USART1,USART_MODE_TX_RX);
# g$ g9 `* J8 V T$ n - 7
1 ^' q: U! U! Y; s! ] - 8//uart isr
# R, }1 z: D, t$ x0 f+ I. V - 9nvic_enable_irq(NVIC_USART1_IRQ);$ A: u; l# c" u3 G" }
- 10
( \& J5 \, A* @. D1 a5 F3 Y. z - 11usart_enable(USART1);, U' G6 Y4 t) s% U1 l, X
- 12
: C2 r8 C7 {4 y y+ M6 R+ O7 a0 x; a - 13usart_enable_rx_interrupt(USART1);
复制代码
" s- t b3 D& w$ U1 N7 a3 t0 I& z/ F& |1 Q, u7 I3 v
先设置串口波特率、数据位数、停止位、校验、流控等设置,再开启串口中断,使能串口和其接收中断;
/ ~- I, X" l1 C2 V. w1 u* i
串口发送 : ?' |" z* g" l$ G1 x
- 1char buff[32] = "hello, makerinchina.cn\n";* v$ N& n) |3 ^1 N, Y7 [0 L ~
- 2for(int i=0; i<strlen(buff); i++){6 ~" p2 e0 M# I6 x% v$ j
- 3 usart_send_blocking(USART1, buff[i]);
0 K8 ^- d, L% I k; W# i - 4}
复制代码 - p8 J# @5 j* d( P* U: t1 `( g# N
! v9 n" ]$ H. _3 j& t* y- s发送直接使用 usart_send_blocking 接口发送一个字节数据; 9 ]- z- d: ?% k4 a5 P7 D
串口接收 9 \ w4 w! v5 b& i0 M; S0 F8 f
- 1/**
# O9 R$ {0 i8 x b8 Z2 Z) h( r - 2 * @brief uart1 isr function
; G/ U/ Z5 j' E# y8 }! S) n - 3 *
7 `: L; f" l* | j/ m - 4 */
* T0 Q0 m0 c+ | - 5void usart1_isr(void)
2 {. v W% v: U! F - 6{) V6 s9 K4 p, J5 N5 P, K
- 7 //receive interrupt0 _: z+ O0 p# G3 Y
- 8 if (((USART_CR1(USART1) & USART_CR1_RXNEIE) != 0) &&
1 `' ?8 L' g/ X$ h+ } - 9 ((USART_ISR(USART1) & USART_ISR_RXNE) != 0)) {; o+ q8 Z; a7 z( M$ \
- 10
1 w: V! x1 S5 u6 v* c - 11 if(recv_index < BUFF_SIZE){6 ~& {$ g1 P* n5 n
- 12 recv_buff[recv_index++] = usart_recv(USART1);" O# a% _7 d w, V
- 13 }else{' [9 ~5 X8 w' d8 V5 D" [* p5 n; k
- 14 recv_index = 0;
0 Z q; B( A2 H1 o - 15 }
3 U- G3 h% M6 l' Y5 m - 16 }
& q5 \! \1 s0 c - 17}
复制代码
/ @/ E2 h' x) m2 T6 c4 q9 I3 n9 R& g8 F* q1 ?
串口接收使用中断方式,接收到数据后将其存放的buff中,然后主程序中取出打印显示出来: - 1#include <libopencm3/stm32/usart.h>" ^1 W! n) G8 E8 G6 p, S. n* T; Q, A
- 2#include <libopencm3/stm32/rcc.h>
& K% J" E3 N% X5 N) q& f - 3#include <libopencm3/stm32/gpio.h>
5 d) S- W3 q" z& R - 4#include <libopencm3/cm3/nvic.h>( _3 n) L/ l X; h- B i+ L* s$ p
- 5
5 q( c9 [' n" ^5 V - 6#include <string.h>: b/ \; ` i& R# [) ^; V. s2 W
- 7
% N, c% V9 \. _, o+ r - 8volatile uint8_t recv_index = 0;1 K/ w& F. F/ U$ `
- 9volatile uint8_t send_index = 0;
( h% W7 g" A1 H/ W' ] - 103 ?, `, T! r5 ?# P( E& Y- {' f
- 11#define BUFF_SIZE 649 e6 B! V) q4 N0 D% B
- 12uint8_t recv_buff[BUFF_SIZE] = {0};% {- y/ u! e8 J6 h! ?
- 13
& ]4 x2 _8 X) h8 I5 m* |% ^ - 14int main(void)/ z2 ^& @, k' c2 l" a4 X; A
- 15{
, p5 f3 Z; [6 ? - 16 ...
+ j1 _3 c- K, O y - 175 K+ g) a& D, v3 n& h3 @
- 18 while (1)) S+ }# o6 P& i7 J0 ~9 h
- 19 {
, X' Z1 \! Y" |+ U. n6 ~) \) [' l ~ - 20+ I# ?5 ~' c3 A, ~
- 21 if(recv_index != send_index){ # f# N; k0 }' s; T4 W
- 22
9 u! g* g; N ?7 M" B. i - 23 if(send_index < BUFF_SIZE){& ^8 J- k4 d7 H w
- 24 usart_send_blocking(USART1, recv_buff[send_index++]);
# ?0 v$ ~5 P- X' S - 25 }else{3 o) H6 L6 |- P2 y( D; ?" t
- 26 send_index = 0;7 {0 S0 {! Y$ N( W: p& M9 E
- 27 }4 ~: r- ^, W( d2 ^
- 28 }
4 X2 A6 ^: N+ G; P0 }' F - 295 i; \9 e0 u' L) c* b# ?
- 30 }
6 x. x$ s3 v0 J$ s! g - 31}
复制代码
8 r; V% }/ J( K2 H' _8 b3 烧写测试点击 platformio:Upload按钮或快捷键 Ctrl+Alt+U即可完成编译、烧写过程,打开串口工具,可以看到发送的数据和接收数据一样;
5 f) P9 A' f& K/ `' a8 h 4 printf使用如果要使用printf功能,需要定义如下函数,将串口重定向: - 1/*
+ }1 i; J4 x, o( g - 2 * Called by libc stdio fwrite functions% b* E+ \5 W" h" _4 Y# E: {
- 3 */6 n0 K! ?# \ A3 F' r4 V5 N
- 4int
, W+ @/ s( D; G5 } - 5_write(int fd, char *ptr, int len)/ V& R0 g( b/ ?( n C, _6 s
- 6{* l4 } w$ B y+ C+ A
- 7 int i = 0;
" s1 V7 j3 X3 ^ {1 J% d6 [7 i - 86 m3 {& p2 {' c
- 9 /*
0 N3 g! W' v- Z- R* ~! q: I# e - 10 * Write "len" of char from "ptr" to file id "fd"
* o8 G3 T- I ?9 K2 m! m7 W9 a - 11 * Return number of char written.
% `' ^6 G* ?- W1 |$ J - 12 *
" S* ?: l6 ^# \ n9 e3 Q w - 13 * Only work for STDOUT, STDIN, and STDERR& U3 N: V* L# c2 S7 q3 U
- 14 */
4 m9 `7 ?! \% E3 Z - 15 if (fd > 2) {4 p p- ]4 ~+ Q3 o5 {
- 16 return -1;
- n9 x5 \1 {3 R8 s* a4 J, \ - 17 }8 M* e3 n1 v0 y1 \- W& ~
- 18 while (*ptr && (i < len)) {4 C5 n I4 k, e
- 19 usart_send_blocking(USART1, *ptr);' U( m D" b6 U, y+ `9 d. t5 Q D
- 20 if (*ptr == '\n') {, r! g( j5 c6 Q- i( [" N
- 21 usart_send_blocking(USART1, '\r');
( V$ j0 f/ S- y3 t- Q$ R - 22 }
! e# K4 Y4 h: _, F - 23 i++;
/ }% z' q6 C# J - 24 ptr++;
e* D4 A& |/ h# ` - 25 }
! R! \$ r8 e/ U c$ S4 m, _ - 26 return i;; |1 L. w/ o% d* D
- 27}
复制代码
/ v$ Z( s4 a5 T- i6 Q现在就可以直接使用printf函数进行打印显示到串口设备;
; J! p* A0 G% X$ T, q$ q
转载自:MakerInChina.cn
9 H0 M* ~) D+ m7 z/ z |