1,开发环境2 C2 @% I, _. p) V! g
. y j8 e' H8 v
1,固件库:STM32F10x_StdPeriph_Lib_V3.5.0
' h' T! W* ^; @, t8 e% b3 w& X% f5 Z
U. w+ ?1 J- _ u; D- u2,编译器:ARMCC V5.06
( }9 t9 d+ y4 M: f, `5 m+ s3 q q/ B, Y) d
3,IDE:Keil uVision5
8 z5 A9 V* k5 c/ C* D4 n5 a' Y8 o$ ?$ j# I! t
4,操作系统:Windows 10 专业版
4 z9 e" I+ q% z5 H* p. e7 c' O' m# J6 g
- R& c7 N5 s P, T, T2,程序源码2 V" v {0 \( I0 I6 k5 c
" g7 a; W. w% \! h' d+ r
RingBuffer.h 文件
5 l) v' F( ? r- /**
, Q1 b# G7 z4 P - ******************************************************************************8 I; P4 p* |3 v6 ~1 g; c, \
- * @file RingBuffer.h4 j( F* X, ^* Z& L1 V7 C' R+ v: W
- * @author XinLi/ c. x' T0 u8 `7 |) t
- * @version v1.1$ j; c2 k* z6 P; e0 K$ w
- * @date 15-January-2018
& L) d! V, ~9 x* e( G - * @brief Header file for RingBuffer.c module.
2 L9 K7 L6 a+ O - ******************************************************************************$ C' P; \$ u- D7 t# _- {9 u2 B! P
- * @attention: }8 a- e1 Y) j0 @* A
- *" ?# A V* S- {
- * <h2><center>Copyright © 2018 XinLi</center></h2>. `+ g* n9 D: w+ f. I7 }
- *
; U3 J+ s: M: }: D; ~% l - * This program is free software: you can redistribute it and/or modify+ S4 }; x+ v% v
- * it under the terms of the GNU General Public License as published by
" }, s, s$ N$ | - * the Free Software Foundation, either version 3 of the License, or; e) A+ k' _, r( [# J
- * (at your option) any later version.
8 p& I% \- s7 O/ S( t0 g - *+ l3 S, b ?0 v# T
- * This program is distributed in the hope that it will be useful,2 q& i9 l2 u1 c |
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
: Z3 E, ^. _1 d1 z; m5 R - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the& N1 n8 t3 J5 Q! r" c" p" d) D k1 }
- * GNU General Public License for more details.
W+ X z6 v% |7 w - *0 n3 w5 @& m: ]- z
- * You should have received a copy of the GNU General Public License _1 Q3 j0 ^( w# o6 m% I- z
- * along with this program. If not, see <<a href="https://www.gnu.org/licenses/>." target="_blank">https://www.gnu.org/licenses/>.</a>9 O$ \' f/ C8 n% E' r
- *0 ^' R0 @9 y' \% a4 a2 H; T
- ******************************************************************************) p8 A* O! y3 q% ~0 @, _
- */
1 D a/ C+ g# n$ t* W( k! ~
( Q$ i7 I; C+ d* i. \) @) b- #ifndef __RINGBUFFER_H
& C9 H4 R+ b; X. t" s - #define __RINGBUFFER_H
2 }# M6 [$ o% K0 ?6 H1 v& I - ( G7 j7 L! V" O$ J& v/ w) z& t$ Z
- #ifdef __cplusplus
4 t$ I5 \% |/ G! F - extern "C" {; q7 \ U; P6 s0 `: t1 [
- #endif
. h. T$ g3 f J5 A. I }2 C( e
! i! @0 h, U2 i& a# D" n- /* Header includes -----------------------------------------------------------*/
I9 H1 A% B$ X3 X+ L# t - #include <stdint.h>
0 ]' m/ G, `7 V# i5 a - #include <stdbool.h>6 L7 ^4 c. ~7 P- _# L; N: F: F
- #include <stdlib.h>0 J9 Z Z! K; T" K
- * d; `7 I8 y. \0 K& K
- /* Macro definitions ---------------------------------------------------------*/
" I( g! ?3 r, |! q - #define RING_BUFFER_MALLOC(size) malloc(size)
' Z& }6 k9 J `; i. {1 l - #define RING_BUFFER_FREE(block) free(block)
8 O- d* t. Q% U- Q+ b8 G
! ]$ G8 B4 T" w8 o0 }$ M- /* Type definitions ----------------------------------------------------------*/8 B% g0 N' H6 Q, q
- typedef struct. k# z& F2 h/ a& @# u8 {( m% R& `7 e
- {
) M! q& X+ S5 Y- s, N/ q, j - uint8_t *buffer;
) t% j s: O5 E1 q7 k - uint32_t size;" v& N! Q7 s$ H
- uint32_t in;
; ^- N) D! D# w) l Y v - uint32_t out;4 U5 J: f3 F' m& g" H& g
- }RingBuffer;
) ~' `5 o6 ^! p+ N, T: }
( I/ Z7 f4 ^5 ]- u% R; T- /* Variable declarations -----------------------------------------------------*/5 m; g4 P& @" s8 L. }
- /* Variable definitions ------------------------------------------------------*/7 ^1 `, N# B6 `7 Q# d! P1 e1 ~
- /* Function declarations -----------------------------------------------------*/5 I& P( M% ]: J
- RingBuffer *RingBuffer_Malloc(uint32_t size);: B7 g1 w5 y N% l& w
- void RingBuffer_Free(RingBuffer *fifo);: M8 b/ f8 Q: b, y' B2 q: [
. M7 i) _+ _- ?- uint32_t RingBuffer_In(RingBuffer *fifo, void *in, uint32_t len);% I# M$ e! P- X4 U8 \
- uint32_t RingBuffer_Out(RingBuffer *fifo, void *out, uint32_t len);
4 p4 J; R t6 [; }4 H! j - & [2 ~' {; z# p1 C+ K6 I* e
- /* Function definitions ------------------------------------------------------*/! ?' _5 o* @9 M4 J# D( r
\, S. Y$ v% o. b0 G0 k- /**( d% S$ X) y4 d0 W; R. s
- * @brief Removes the entire FIFO contents.; V6 b$ z0 k9 A; q6 A1 I
- * @param [in] fifo: The fifo to be emptied." R, ]( }/ _. l- k% j' q' C
- * @return None.) b5 ^$ \) @6 x N4 I
- *// H3 t6 M! h; `
- static inline void RingBuffer_Reset(RingBuffer *fifo)9 T1 y1 @. ] {$ Z; v- x
- {
$ k$ K# V y; A' X - fifo->in = fifo->out = 0;6 V5 i1 a1 n2 F$ j4 {" s
- }
( t/ T9 a% W) K& n% I4 m/ [ - 1 \ y4 s b- b2 K. L
- /*** C8 n$ c' F0 o. ^7 Z: s, }+ T
- * @brief Returns the size of the FIFO in bytes.
+ W/ O, B. x- Q! R/ y! ^ - * @param [in] fifo: The fifo to be used.: {; P' r7 K1 q: b( w
- * @return The size of the FIFO.: t& {. ^7 c, x9 t+ H
- */+ \7 _" H% I/ e0 w& X
- static inline uint32_t RingBuffer_Size(RingBuffer *fifo)2 `. G8 r: P- \
- {/ _5 B4 o: ]( @5 N
- return fifo->size;; r! N; p% E4 g: t H& | ?
- }
3 q9 w3 l) L; `6 f" x l - 2 w+ x# }, P/ f* o3 ?
- /**/ M3 j9 V9 T# J; r
- * @brief Returns the number of used bytes in the FIFO., L& L5 w& f# K c. ]
- * @param [in] fifo: The fifo to be used.6 v+ t$ U' m+ D4 i- m ]9 d' ?
- * @return The number of used bytes.+ Y( l: P5 Z0 {* P) j/ I( o
- */
3 t" j0 G, `$ j6 L4 B3 i$ H - static inline uint32_t RingBuffer_Len(RingBuffer *fifo)
2 u3 O+ J5 q8 S0 c - {
: {- W' i1 R* Q( ~- O) ]7 l - return fifo->in - fifo->out;! q2 y% l9 N$ a2 ^
- }! |& d% v. e: X! L% m: }; A8 D6 u
* t- t( Q! Y1 x$ W) `% X9 u- /**
9 Y6 _! _0 T7 e0 n; O3 |' ? - * @brief Returns the number of bytes available in the FIFO.) ]8 ?9 B9 S6 |/ D/ j
- * @param [in] fifo: The fifo to be used.) O# u# n9 O6 `+ A B! ` D
- * @return The number of bytes available.% y, u$ p% O2 p0 r# }" \, }( P8 B
- */$ z' Z; N1 C% g5 H F
- static inline uint32_t RingBuffer_Avail(RingBuffer *fifo)
z( ?% h# o2 P+ a+ | - {; D1 P! x1 O% j
- return RingBuffer_Size(fifo) - RingBuffer_Len(fifo);
( D1 F4 x* A7 V - }4 r. t& |! i4 D8 h" h5 q
3 P, E0 c# W" l% n- /**. C# G g U0 a# |4 y4 H
- * @brief Is the FIFO empty?
3 |; @4 w0 W& V. s0 @. m X. m4 B - * @param [in] fifo: The fifo to be used.& Z4 ^' t* j, b* M& C Q0 H& F
- * @retval true: Yes.
# ]) ~. P) n: M9 i$ U8 U - * @retval false: No.
2 s/ e5 F$ V" u8 B - */9 u" Z) y) @* U8 p4 o/ d
- static inline bool RingBuffer_IsEmpty(RingBuffer *fifo)
: h: i6 v) {6 E. | - {9 c6 ?. W' Q, u( ^0 H# |
- return RingBuffer_Len(fifo) == 0;
8 v0 \% M1 f* ] - }# y3 L7 H* a4 Q% ?4 H: t2 r
- ; y5 n0 N8 t) p$ {( r8 x! E0 e5 V
- /**
' `0 x- ?( @' Y5 W, `3 a - * @brief Is the FIFO full? u; r! A% e) u n
- * @param [in] fifo: The fifo to be used.
# w# u5 |/ w- ]5 s: B. V8 M( u - * @retval true: Yes.
2 I% ]2 b. g; S" m0 q3 O2 L1 d x - * @retval false: No." d. _8 [# n. Y" h0 c% X0 L; ?6 ?8 E
- */; \3 N4 @4 w. F3 N: D7 w0 `: N# g
- static inline bool RingBuffer_IsFull(RingBuffer *fifo)
- s# S# o, y; D - {
. W/ E) j) G/ p/ X9 U# ? - return RingBuffer_Avail(fifo) == 0;; V2 j" E" i* r# M. }. d
- }
7 M! } t: i2 V5 f! U; ]
X2 c3 `: a K F$ }+ `2 b' m. E- #ifdef __cplusplus
5 N D. O2 |+ R X, e0 h- t - }
! Q: D' f) Z/ Y# R4 U - #endif
8 R& |1 E) t) B4 ~( l$ i& P# ^ - ) ^8 h' x f3 y
- #endif /* __RINGBUFFER_H */
6 r8 Z8 S, R1 H4 Y, |* h
复制代码 . u( y! G( R I* W
& C- q2 @& M. W6 I6 L
RingBuffer.c 文件
" S- c# D( s! a0 ]- <div>/**' F4 l& F0 f6 H1 U
- ******************************************************************************
# ^) x- p! u/ O+ C7 s5 x - * @file RingBuffer.c3 T3 V( S$ S i/ }+ h# E# S
- * @author XinLi
: F; g6 J3 s, N - * @version v1.1
# L) T0 S0 e4 n# a - * @date 15-January-2018
; j8 X* v- p6 s% G - * @brief Ring buffer module source file.
% d, g1 L( U# o/ v. p, }$ n/ H# W& e - ****************************************************************************** Z* w. D" R; s5 e8 d
- * @attention8 o% k( T9 {. r+ D" [
- *
6 ?/ Z- o( j5 h T3 L" n8 f8 U - * <h2><center>Copyright © 2018 XinLi</center></h2>6 G! i8 H9 }" ]( q( x' ^; S6 m
- *# ^1 X- j! U- n8 d
- * This program is free software: you can redistribute it and/or modify
4 F" Q9 \* N0 Y/ c- ~ - * it under the terms of the GNU General Public License as published by
* s4 E, B& E7 O& c% q1 X - * the Free Software Foundation, either version 3 of the License, or
2 f* q# k9 y+ p7 J - * (at your option) any later version.
# @9 d) f4 c% D7 f4 _ - *3 e, m7 r7 [9 s" C0 F/ q" O
- * This program is distributed in the hope that it will be useful,0 _3 Y' e. d) l, Q( j6 @' l
- * but WITHOUT ANY WARRANTY; without even the implied warranty of7 P( t) R2 n: U+ ]4 D
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; |- l4 L% f" P - * GNU General Public License for more details.2 _7 J( K8 f5 {8 w' y
- *
m1 s% j9 S( B. { - * You should have received a copy of the GNU General Public License2 ~+ ^# l9 _+ A/ C$ g3 P$ z9 F
- * along with this program. If not, see <<a href="https://www.gnu.org/licenses/>." target="_blank">https://www.gnu.org/licenses/>.</a># T0 f% ]' m+ C% y" A+ x1 `# k
- *
+ ]6 V6 {0 Y, n, M) L" H# g8 d - ******************************************************************************- g: s9 L% R& N; m) p
- */
( G" H: L* ^8 U/ S$ Y - ' g5 X! Y; u C& d" r$ X
- /* Header includes -----------------------------------------------------------*/) t/ O6 v9 Y) _
- #include "RingBuffer.h"
8 J0 Y4 |$ m" [, \0 ` - #include <string.h>
& L- A- Q! B+ C' H6 ^
% s7 @0 g3 G0 p+ b" L- /* Macro definitions ---------------------------------------------------------*/6 n' o1 e$ n# E: ~! t
- #define min(a, b) (((a) < (b)) ? (a) : (b))
4 C r1 l& v1 j - 5 `: a6 x; ^* x" E
- /* Type definitions ----------------------------------------------------------*/
! I3 h3 x$ y* S" t' l - /* Variable declarations -----------------------------------------------------*/# B$ y! {) o) _7 }6 c
- /* Variable definitions ------------------------------------------------------*/- v0 [/ i% ? B% X6 [8 _
- /* Function declarations -----------------------------------------------------*/7 \; U3 I6 ?2 |) |% m2 Y/ T/ V
- static bool is_power_of_2(uint32_t x);
& h7 h( m1 ]) L O4 b& l - static uint32_t roundup_pow_of_two(uint32_t x);, ^" e- I- n2 S( n2 {5 `
- 6 h/ {: U, C% h8 g5 h7 Z4 s
- /* Function definitions ------------------------------------------------------*/" F0 k- U; L3 ~% L1 ~1 a3 U
8 ]0 j+ O( |9 g6 U2 w0 z- /** o7 h% {2 r0 ^; P; K
- * @brief Allocates a new FIFO and its internal buffer.6 y( D/ y- z: G: |# x6 {# P
- * @param [in] size: The size of the internal buffer to be allocated.
3 j# W6 s) E2 O" S9 v6 D. P - * @note The size will be rounded-up to a power of 2.
7 J5 _5 y. g# O0 v* o - * @return RingBuffer pointer.
! [4 E( z; L+ ~2 m, n) R6 H+ X - */. B5 W$ Q+ ?5 K
- RingBuffer *RingBuffer_Malloc(uint32_t size)1 H& N) `- A0 D4 m1 P; C) c4 O
- {
# C" W7 Z& [3 M. e9 S - RingBuffer *fifo = RING_BUFFER_MALLOC(sizeof(RingBuffer));1 l" [& E0 [% T4 }4 V: M) S! W
: s7 U$ g' G) X0 q; \: T, g9 A- if(fifo != NULL)* D$ A+ |2 B+ [/ P
- {4 D+ b! i( o: U, B6 }$ T0 A
- if(is_power_of_2(size) != true). G3 W; d/ a/ N# @; t# z
- {% j# C, r. R `8 V2 o; Y. {
- if(size > 0x80000000UL)
( q: D- T' ~8 |/ S$ r# V1 O - {
* P. A: R. ?4 l+ W8 M) @ - RING_BUFFER_FREE(fifo);3 i. q0 i9 v1 s* K9 S* I3 [+ p' L
- return NULL;$ @+ o( O2 o7 x9 M8 V
- }
3 z; ^$ [0 a! k - # g. V3 _2 k/ n9 ?, r5 w
- size = roundup_pow_of_two(size);( P3 ]# z+ m+ N
- }4 ]. d# t; o% K* r* j/ T
- # Z: u' O6 m1 J
- fifo->buffer = RING_BUFFER_MALLOC(size);6 p# a) t; M/ O
+ E. p, I0 F! e3 t2 P- if(fifo->buffer == NULL)( K; z& a' W3 m/ J5 ]0 W9 Y3 p
- {
4 j0 e8 g5 m/ `' x; f: l- @ - RING_BUFFER_FREE(fifo);( V, R: P9 v6 D
- return NULL;% z( v' V0 [" H- z
- }
9 L7 A/ u2 J3 ]$ r L, M - ! o o' r$ D3 v1 s/ h
- fifo->size = size;7 t6 i0 X" z( W; v: `
- fifo->in = fifo->out = 0;
* s6 S Q; c3 ?! J( \) `8 l3 | - }
9 b7 A* T( K: M6 q - ; B! C, Z8 H7 z( _; K9 l( X/ p
- return fifo;
/ w! _; ~4 X% _$ ^4 s* P$ p - }: }9 r ^' a! e# I/ G/ p+ g
- 8 @# ]% U- i- n- W& x0 }3 F) D
- /**# w' l6 r3 z* z7 c; k
- * @brief Frees the FIFO. j- `) s. d6 F8 ?: q$ j# G- l
- * @param [in] fifo: The fifo to be freed.
9 g. n7 `! H& w, Q- f# W - * @return None.# ] n" N2 Z1 c$ r i
- */1 M+ }, M6 O `; a6 g I
- void RingBuffer_Free(RingBuffer *fifo)
/ c6 i) z1 P2 j p+ p# N/ Q; ` - {
2 [1 e. h/ j4 @% E - RING_BUFFER_FREE(fifo->buffer);
& b7 g G# E9 m$ X- v8 e% Z( Q - RING_BUFFER_FREE(fifo);
. m0 V8 \: j+ i. r. q8 d5 b- W' O - }7 x9 B# y/ b: k. S+ O
1 W1 t) e$ t/ Y. W) E- /**
9 _- O- j: H% j0 ^4 W' g) V6 J) _ - * @brief Puts some data into the FIFO.
( s. k G$ ^9 h2 C- C - * @param [in] fifo: The fifo to be used.
: r$ b1 t' J# n0 Y- l4 ^! J4 h5 B1 U - * @param [in] in: The data to be added.
9 U5 e5 o' h2 w, U - * @param [in] len: The length of the data to be added.
. F: t1 K7 l M) n - * @return The number of bytes copied.
8 q$ q1 d) O) @$ ?/ v) x - * @note This function copies at most @len bytes from the @in into
4 W$ s3 g3 U! w% e - * the FIFO depending on the free space, and returns the number& M) N) x$ d# h' Y
- * of bytes copied.
7 Y. b; P+ F% d - */
) j8 I+ z0 Q+ K% M6 l. F8 n0 g - uint32_t RingBuffer_In(RingBuffer *fifo, void *in, uint32_t len)
& z- G, w; R' S" Z" y/ Y/ T - {
; Q. n# t' I" p- X - len = min(len, RingBuffer_Avail(fifo));
! `- R$ D$ e( G( e/ d2 p - ! c2 H! Q4 K& N. n9 ?
- /* First put the data starting from fifo->in to buffer end. */
3 f4 K: A; }, A" O# J$ z. y - uint32_t l = min(len, fifo->size - (fifo->in & (fifo->size - 1)));
2 k6 t/ ?/ F- e7 O& v+ Z4 L - memcpy(fifo->buffer + (fifo->in & (fifo->size - 1)), in, l);
: p! \# d& N, m; C1 ^% y - ; t" }. H2 U$ b* z
- /* Then put the rest (if any) at the beginning of the buffer. */+ |" p$ X3 ]6 J0 w1 s4 x5 x' |
- memcpy(fifo->buffer, (uint8_t *)in + l, len - l);
5 |3 B& V: q; J: P) ~; Y - ( B/ b' X: T7 `
- fifo->in += len;
. c! d" J5 L2 k% H - V# C1 j' K# E4 w/ [
- return len; h0 o) V& z% B: I6 ?$ F, o9 P' I2 l* v6 h
- }
; `) p; C s7 f7 ~: O - 4 W6 w) H7 B: P4 A' t6 ^; q9 w5 b# N
- /**
! h* J5 Z3 p2 \5 J. \ - * @brief Gets some data from the FIFO.
% c) D; e8 T, L+ C' n3 U - * @param [in] fifo: The fifo to be used.
! }, K, }" f4 n' N - * @param [in] out: Where the data must be copied.2 Z9 c& q- h1 Y5 i
- * @param [in] len: The size of the destination buffer.
0 c9 a) B0 a# I. S. B1 n - * @return The number of copied bytes.& O% W' G# q6 h8 a
- * @note This function copies at most @len bytes from the FIFO into
3 j! P7 U! h6 e - * the @out and returns the number of copied bytes.
1 n, o) w, L; f, O - */
9 t; M( G, V+ H, L - uint32_t RingBuffer_Out(RingBuffer *fifo, void *out, uint32_t len)/ \; U' m% M4 K8 P3 `
- {
% Z+ C2 h1 d; C) }7 U* h" K+ P8 p - len = min(len, RingBuffer_Len(fifo));$ X) N2 D' o( C9 R6 H
7 W9 O3 y9 m/ D, E) w7 F) c( h- /* First get the data from fifo->out until the end of the buffer. */
/ w, H( w6 P5 H& d - uint32_t l = min(len, fifo->size - (fifo->out & (fifo->size - 1)));
6 ^% i* S, b+ }. F6 n U - memcpy(out, fifo->buffer + (fifo->out & (fifo->size - 1)), l);
4 p" q( e0 W% L4 A* @3 S
( D* Q/ J( A$ o8 m7 \- /* Then get the rest (if any) from the beginning of the buffer. */0 H: h& M9 o5 G/ g$ x5 g
- memcpy((uint8_t *)out + l, fifo->buffer, len - l);+ e+ U# J l$ b+ i2 p! t
; q3 S# H# d1 t- fifo->out += len;
0 t2 t [( k, I* f$ n3 G - - `. F7 Y; @) m* U+ E4 I# {
- return len;
! w8 [/ {( S7 x% s# Z" G - }
0 @2 L' L, q5 [: }
& Z4 w. P& S. y. N4 ~- /**
" y# U! o* p# i# e* Q - * @brief Determine whether some value is a power of two. f, ]4 D% p- {8 u0 h: }8 }
- * @param [in] x: The number to be confirmed.
: q S# H. O5 g2 d0 N - * @retval true: Yes.- k" w1 \1 A! s- y X; C9 X
- * @retval false: No.$ i& G; O' x9 O5 |: s6 h% m1 L
- * @note Where zero is not considered a power of two.
) F: S+ {; e/ t3 y* | - */
4 C* V, P3 e. X* k7 l - static bool is_power_of_2(uint32_t x)+ T, d4 M, a+ l* U$ i
- {6 M0 S- ]* S& G
- return (x != 0) && ((x & (x - 1)) == 0);$ E0 f2 `& v0 E2 @! |, f& i
- }
; C R% K. H; U. ]7 _. T - 2 I8 r' V6 C3 O
- /**3 i& i0 |8 T9 Z$ n$ C
- * @brief Round the given value up to nearest power of two. t- Z& o2 V$ s: M5 b$ [0 @/ W: Z
- * @param [in] x: The number to be converted.; J) @) B- K; `# B. D0 b
- * @return The power of two.
+ I/ e9 l, w C8 Q - */# @" z, u' g: F7 O8 O
- static uint32_t roundup_pow_of_two(uint32_t x)
Q9 ^8 @& g0 b0 n/ T' T/ Y+ C* a" X - {
5 y* H+ {1 g( \$ O0 c" z7 l - uint32_t b = 0;1 ~4 j9 s( q9 M( H8 V$ _
0 \5 J' _, g! h1 K ~' F; i- for(int i = 0; i < 32; i++)4 G$ x0 W3 Y6 l
- {" [! q( `& _/ A6 z/ L# z( d8 x( C
- b = 1UL << i;" d; L3 l" X) F$ q. e+ [8 X4 Q
- 8 P) R. t2 n/ p6 B, o
- if(x <= b)3 Q, |) r! |2 F2 X9 w+ L3 d) C
- {
. l2 q1 `; _7 A5 C - break;0 T3 Y; G8 @( Z9 c/ R* u) [- _
- }$ }5 O, D% p' [5 |, M8 p2 y
- }
) O" n5 K9 |; P7 R8 i; j
7 @; z" G! ]' m1 _5 t: p- return b;1 B4 |' s( U' E1 d4 x
- }</div><div></div>
复制代码 2 A/ T4 T* e, g- Z9 i7 d% _9 ^2 O1 `
1 ^( H$ n( h% _6 f; G( c) BCAN.h 文件, @1 ^. g+ H" S
- /**
. {' H5 H. z1 \/ y* ^, U2 Y5 L - ******************************************************************************1 _" b% x, `' ~9 `% g/ {( W0 }
- * @file CAN.h
7 c) h2 Q' u. m0 [1 T9 @, w - * @author XinLi6 N$ o3 r/ ?2 @6 {* t! C% K% R
- * @version v1.0
% d. r8 [+ I. m! e) M! E: D - * @date 24-June-2018: M* J- h) _& ]8 F* ~7 u& c' d
- * @brief Header file for CAN.c module.
# Q1 n' ~+ t7 `1 E7 t' Q" `/ w - ******************************************************************************
: S* U5 l+ B8 o i+ {0 r- ] - * @attention
* j1 J/ T' d+ O0 V - *7 l6 B4 K0 F+ G' d
- * <h2><center>Copyright © 2018 XinLi</center></h2>
) c3 e% S$ M0 s6 v( n$ ] - *
6 |5 P9 n% U( c* n- J! e. [1 G6 P - * This program is free software: you can redistribute it and/or modify
8 }8 X7 Z6 [ u' ~& B- T* L7 h - * it under the terms of the GNU General Public License as published by( b) s8 g# D6 `" ^. C0 [- \3 J% k
- * the Free Software Foundation, either version 3 of the License, or0 x6 l' w7 F2 j" A
- * (at your option) any later version.6 d8 p' @* p/ x q6 W
- *
" a7 j( ]" v; i2 `8 e8 t B. E - * This program is distributed in the hope that it will be useful,: T& j! _# V: Z' i+ B1 J
- * but WITHOUT ANY WARRANTY; without even the implied warranty of. T$ i2 O; f/ Y+ x) I! C
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
" L6 ]! U! U7 W! W - * GNU General Public License for more details.
. J; k. C# ^( V - *
7 W- @( u" K5 t/ ] - * You should have received a copy of the GNU General Public License
?- g+ v& }8 P# J! c. ?) ~3 d - * along with this program. If not, see <<a href="https://www.gnu.org/licenses/>." target="_blank">https://www.gnu.org/licenses/>.</a>
) l+ [8 K9 V) O: {/ X6 b* _5 z - *4 D3 E. B" D# t( i: c
- ******************************************************************************
3 b; q. G+ S. y7 O - */
. v3 J+ I/ @, L4 J$ Z
. {- [- H$ d: _7 c9 E5 l- #ifndef __CAN_H7 Y! Q ~8 W Q" @4 P4 R0 B
- #define __CAN_H. l* K: y5 Q' j7 h& D f3 a
/ v. m% y& i9 Z8 x- #ifdef __cplusplus
" K) a2 H$ K+ R# U( Q& E% l, X - extern "C" {
2 ~/ {. P6 V5 r+ ?- }0 U - #endif
" d$ T( y& G. o {1 y5 H3 N2 k- N - . F0 w- X6 i' B+ \
- /* Header includes -----------------------------------------------------------*/
( l! z2 V0 y: d! d - #include "stm32f10x.h"' y4 l4 ~$ A: I
- #include <stdbool.h>6 U( x/ u1 B; A# w; s
* _5 f2 u: x! Q* L: j, |3 b& g- /* Macro definitions ---------------------------------------------------------*/7 }5 e! J3 N0 \% b; ]
& R6 z7 p3 Z5 u7 a9 M8 k- }- /******************************* CAN1 Configure *******************************/
" w ]2 D' E2 g7 i* Y1 w - #define CAN1_TX_BUFFER_SIZE (16)* a& v/ V$ O5 K- y
- #define CAN1_RX_BUFFER_SIZE (16)* T) _) @/ R7 _# E
% E; }3 J( n8 C& I2 S* y- #define CAN1_TX_GPIO_CLOCK RCC_APB2Periph_GPIOB2 a, P4 U0 ^5 ^
- #define CAN1_RX_GPIO_CLOCK RCC_APB2Periph_GPIOB
, w/ o; w+ x- ^& f4 u! n1 I - 2 d9 e3 Q/ B# f' G$ w9 I& ]2 E+ O& z
- #define CAN1_TX_GPIO_PORT GPIOB; x& N; }" t+ @) ^" V$ V( Y; b; r9 A
- #define CAN1_RX_GPIO_PORT GPIOB- N0 |' p0 S) \+ q- V
- 3 |. L# y6 H% {. Z: {0 ^, B6 |, W
- #define CAN1_TX_GPIO_PIN GPIO_Pin_95 t7 ^6 W3 y6 i$ [# A
- #define CAN1_RX_GPIO_PIN GPIO_Pin_8! ^: m7 h H4 A6 p+ x0 A
5 X5 |" N$ m+ V: g% T4 z- #define CAN1_IRQ_PREEMPT_PRIORITY (0)
8 v9 b- O# P$ |; e' y - #define CAN1_IRQ_SUB_PRIORITY (0)5 {# |: h5 K+ L, O1 e. G
! v. Z% O( ^5 }# m- #define CAN1_PORT_REMAP() GPIO_PinRemapConfig(GPIO_Remap1_CAN1 , ENABLE)# V* o L* g9 T0 Q( N! E% P
- /******************************************************************************/* g' C& `4 }2 T1 [
9 E1 ~1 @/ N: B: J+ {, T- #ifdef STM32F10X_CL
0 r8 S4 x# B& W O+ U, H; B/ p - /******************************* CAN2 Configure *******************************/
* N7 X& h2 J. K( @4 {. | - #define CAN2_TX_BUFFER_SIZE (16)
8 s' K2 f: ?1 w4 n0 M0 ?; [ - #define CAN2_RX_BUFFER_SIZE (16)' Z5 q9 N9 a! k$ B
- 0 t: c- L/ u% ? J2 [( H
- #define CAN2_TX_GPIO_CLOCK RCC_APB2Periph_GPIOB
0 Z8 Y/ [5 P+ B1 m( R - #define CAN2_RX_GPIO_CLOCK RCC_APB2Periph_GPIOB
C: Q; [, O {- ?2 V' x
# u' k4 D; V/ a1 I. ^- b- #define CAN2_TX_GPIO_PORT GPIOB
( B) Y f- |. O& H) } - #define CAN2_RX_GPIO_PORT GPIOB5 j5 q+ U' Y. X8 T m/ W3 J
3 g; h' W; V, Z: g& [/ w- #define CAN2_TX_GPIO_PIN GPIO_Pin_13
3 I7 A e- l) L' b - #define CAN2_RX_GPIO_PIN GPIO_Pin_12/ C" q- ?- @/ m* i& m9 D
- * u! U# G% P* J8 z& D
- #define CAN2_IRQ_PREEMPT_PRIORITY (0)
% e- t. A6 T, I - #define CAN2_IRQ_SUB_PRIORITY (0)9 {+ t4 ~9 @: q+ ^ C- d
$ ^ m: e3 g" L9 q- #define CAN2_PORT_REMAP() GPIO_PinRemapConfig(GPIO_Remap_CAN2 , DISABLE)
, W% a0 I; z2 } - /******************************************************************************/" P9 R' I. p& B+ B+ r K
- #endif /* STM32F10X_CL */
1 Z, O/ Y8 E+ _# ? _5 Q5 ?
+ \2 W. E; e* ~8 p1 F2 H- /* Type definitions ----------------------------------------------------------*/$ \. D( W+ ^& I
- typedef enum
% z& c8 e; j* [& _ E - {7 T$ D! a9 `7 d- b3 ~5 B
- CAN_WorkModeNormal = CAN_Mode_Normal,
6 Y+ L# m$ V: k - CAN_WorkModeLoopBack = CAN_Mode_LoopBack
7 _( \. r7 P6 u, m5 S - }CAN_WorkMode;3 j1 s* R i, U% R3 G7 [& f
8 s$ R$ u! r$ u2 E% Q+ [: E% C) ~9 q- typedef enum
, k7 J9 `# K5 z. h- `( j - {
5 X; J y, |$ g5 S - CAN_BaudRate1000K = 6,
7 G. g7 ]% m/ x2 f, N - CAN_BaudRate500K = 12,
8 x5 T) J1 c, `6 p+ r - CAN_BaudRate250K = 24,
4 J: V: Z! d. v. J - CAN_BaudRate125K = 48,
0 I8 [/ \! {$ z) p# \0 D0 ] - CAN_BaudRate100K = 60,, _' o6 f# g& ]7 J- J
- CAN_BaudRate50K = 120,: f u0 [4 u+ P% e& Y- z. t
- CAN_BaudRate20K = 300,6 y. e7 h1 H$ h/ L! N3 ]( x: [. \
- CAN_BaudRate10K = 600
/ ~- [; z! s/ f- A( o& J - }CAN_BaudRate;/ s4 K2 Z3 d/ N) S; N
- ( y, i0 H: j7 `
- /* Variable declarations -----------------------------------------------------*/9 y2 ?! |8 F: O. C6 }, ]* r; Y
- /* Variable definitions ------------------------------------------------------*/
) k4 T4 @1 \% f& }5 R1 L& n - /* Function declarations -----------------------------------------------------*/
: [5 h6 k. J% P - void CAN_Configure(CAN_TypeDef *CANx, CAN_WorkMode WorkMode, CAN_BaudRate BaudRate, uint32_t StdId, uint32_t ExtId);7 y, c; v! U& A% c0 h- Q9 i; G: ~ _
- void CAN_Unconfigure(CAN_TypeDef *CANx);8 E& X1 u; K& i- a# z
- % A$ V* C3 Z9 G% N
- void CAN_SetTransmitFinishCallback(CAN_TypeDef *CANx, void (*Callback)(void));, J/ g$ F A- N R X" A
- void CAN_SetReceiveFinishCallback(CAN_TypeDef *CANx, void (*Callback)(void));: R# f; e/ Z7 Z @ N4 @
! g( a% ~1 D: N9 h7 a6 c- uint32_t CAN_SetTransmitMessage(CAN_TypeDef *CANx, CanTxMsg *Message, uint32_t Number);) B! r7 B/ N* [3 Z; Y4 ?
- uint32_t CAN_GetReceiveMessage(CAN_TypeDef *CANx, CanRxMsg *Message, uint32_t Number);/ _5 B, a3 Q3 a6 Z$ z) Q
# V: D/ s- _/ f' Z4 y3 B1 U# J- uint32_t CAN_GetUsedTransmitBufferSize(CAN_TypeDef *CANx);
" X! s# D0 i$ O' W - uint32_t CAN_GetUsedReceiveBufferSize(CAN_TypeDef *CANx);( G% O/ \& [/ @0 [+ k) K
- uint32_t CAN_GetUnusedTransmitBufferSize(CAN_TypeDef *CANx);
- ]' U1 b# e! Z1 C3 T - uint32_t CAN_GetUnusedReceiveBufferSize(CAN_TypeDef *CANx);7 C# K, ~! j" X/ \# h1 U5 g) H) m
5 i: g( u9 a1 d- bool CAN_IsTransmitBufferEmpty(CAN_TypeDef *CANx);8 t5 V0 G- E8 H# i
- bool CAN_IsReceiveBufferEmpty(CAN_TypeDef *CANx);, x- p% @& t" i, i( C
- bool CAN_IsTransmitBufferFull(CAN_TypeDef *CANx);
( d7 b S6 A6 u7 D - bool CAN_IsReceiveBufferFull(CAN_TypeDef *CANx);" p* B8 J" b" }9 o
' V7 e1 @; J! Y: D0 M# F- void CAN_ClearTransmitBuffer(CAN_TypeDef *CANx);
T& `7 B. n1 E& _ P - void CAN_ClearReceiveBuffer(CAN_TypeDef *CANx);
' \8 J+ Z/ s. P$ F0 U
3 _1 r- Z3 W. y7 M- bool CAN_IsTransmitMessage(CAN_TypeDef *CANx);
9 z/ r/ I r0 W) o
6 o i* o- l) x% o0 T" Y" [- \- /* Function definitions ------------------------------------------------------*/
1 V4 o- J: I) R4 B' [( j
+ {( i. u. ]! d9 F- #ifdef __cplusplus
$ h/ p& x p+ G r/ D* w$ j! r - }
- {2 p* C8 g- W2 m7 q$ D - #endif) {" c- ^* }& j& ^
5 Z0 B) y! N5 m/ g, e* `- #endif /* __CAN_H */
m8 y$ L. L2 M/ R3 f
复制代码 , A3 ~3 G) c+ k+ v1 c3 w
- p) g# {6 _! R# H: u+ K: k
9 r# u- \0 r' \+ W: c6 ?* @
CAN.c 文件
& E, j3 z7 B) c& a! H5 m2 f
9 p- ^. v2 U' l' e8 m# E- /**
4 E- w( l j9 V+ a3 _" u3 }) D9 t - ******************************************************************************
" {$ D- l' z5 e& w - * @file CAN.c
4 e# ~1 V. T j% w' @ - * @author XinLi
2 r' i9 a) x( F' N - * @version v1.0
# N. u* B; m* L% t5 x - * @date 24-June-2018; U5 w0 n5 B) @. @- Z2 v
- * @brief CAN module driver.
7 [ B( s, D; v) S$ z8 K - ******************************************************************************
7 c' n- N3 z, q' K4 H - * @attention. W: T% _: m2 J% c) s/ |: W2 c- ?
- *0 T/ P. ^0 h5 @) @; i
- * <h2><center>Copyright © 2018 XinLi</center></h2>0 Q5 w7 ]3 P7 q& I: q) G' E2 ]0 b
- *! }2 a; P" ]4 O9 C, r! [
- * This program is free software: you can redistribute it and/or modify
$ }4 D( Q0 Y3 W2 ?/ d2 R) ~9 c - * it under the terms of the GNU General Public License as published by9 p- D1 S) A' e
- * the Free Software Foundation, either version 3 of the License, or' q; ~/ \5 m! D( `0 V3 _/ K
- * (at your option) any later version.5 o5 \! G7 K+ R, P
- *; d5 s! K0 V4 T) Q9 q, Z5 A
- * This program is distributed in the hope that it will be useful,9 V4 P M6 I# V+ r1 E* B8 H) s
- * but WITHOUT ANY WARRANTY; without even the implied warranty of0 P2 |3 u4 ]0 ^
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
/ `. b( f! p/ N, h* {( g9 Y( I - * GNU General Public License for more details.
) `+ D/ [8 e$ q, S! T7 F - *+ o( M: G! k/ H! H
- * You should have received a copy of the GNU General Public License
7 M! z! p+ o. u - * along with this program. If not, see <<a href="https://www.gnu.org/licenses/>." target="_blank">https://www.gnu.org/licenses/>.</a>( u5 j( s2 v# L4 m& V
- *# [6 R7 K" _( |& E* ]. y) y
- ******************************************************************************
/ \) \ y8 V1 @ w# j3 w' s - */
j" Z+ W1 I9 g6 G& Q' W: p
6 g2 c: j/ N s' }0 u' i- /* Header includes -----------------------------------------------------------*/
! Z$ D3 c4 ?# h7 Q - #include "CAN.h"
9 B* Q( [8 a9 Q0 P1 y% S - #include "RingBuffer.h"
+ `. c/ z* T0 h0 `! }# I - 5 b3 x9 q8 w2 q; }) {# ?3 p* X. R
- /* Macro definitions ---------------------------------------------------------*/
, l" Y; h9 E, @1 \ _: ^ - /* Type definitions ----------------------------------------------------------*/) D; h2 S/ `; B& p. ^! b0 V" f
- /* Variable declarations -----------------------------------------------------*/: u4 N/ M5 a! S2 P& P7 w/ j) y
- static volatile bool can1InitFlag = false;
) r6 ~2 ^2 V( Z8 ? - static volatile bool can1TransmitFlag = false;
. Z( @2 | g6 R; a3 o2 i
2 D* G' D( _; q) W7 }; R8 l- static volatile void (*can1TransmitFinishCallback)(void) = 0;. j9 }" g- o0 Z+ v$ B/ K2 u
- static volatile void (*can1ReceiveFinishCallback)(void) = 0;
) G/ i* `7 M; q( ]: G8 P - % O9 E! X% H5 b* h! {. J. c& b
- static RingBuffer *can1TxBuffer = 0;7 [: J4 M/ F. G
- static RingBuffer *can1RxBuffer = 0;/ I. |- U8 T1 ^6 E" V
- * c1 l$ a/ i' E* w t! w# ]
- #ifdef STM32F10X_CL
+ O/ V* `& R- Q8 V - static volatile bool can2InitFlag = false;
$ B, v: E# g, `- G0 c# a Y - static volatile bool can2TransmitFlag = false;1 P9 G6 _* |/ z% R
- & G; a; m: j8 ^) ~( U& s
- static volatile void (*can2TransmitFinishCallback)(void) = 0;
4 P$ T" }! F/ x; ^# m! w9 @( [ - static volatile void (*can2ReceiveFinishCallback)(void) = 0;
9 A7 U( t. d3 ] Z - 9 N5 b4 E- A) x
- static RingBuffer *can2TxBuffer = 0;
- e$ R) k" M T$ x' P - static RingBuffer *can2RxBuffer = 0;
! X" L6 H' C- [, p, \ - #endif /* STM32F10X_CL */
- x6 Z( K# r- [. K* q
/ }4 f$ W* E9 z- /* Variable definitions ------------------------------------------------------*/2 Y9 b! M3 R) Z* e$ e( c
- /* Function declarations -----------------------------------------------------*/
4 ?( ~# g# x* e" h) R+ u - /* Function definitions ------------------------------------------------------*/: N) h `/ v! X1 w
- : r5 Y! ?- n9 j# Q+ }' Y
- /**
; I4 D' i" l) N8 m& c - * @brief CAN configure.8 v& N6 |2 e7 z( L8 p5 L
- * @param [in] CANx: Where x can be 1 or 2 to select the CAN peripheral.
2 J0 L& ]5 I0 A' I% N7 t7 ? - * @param [in] WorkMode: Work mode." q2 ?8 E2 s9 I
- * @param [in] BaudRate: Communication baud rate.& m4 A$ |/ g) m7 \- \
- * @param [in] StdId: Filter standard frame ID.
: c/ d+ p* s) |8 h/ G - * @param [in] ExtId: Filter extended frame ID., X- G$ H5 h; ~
- * @return None.
, X# l4 d- V( Z. \% C2 P& Q# e - */, ^2 I9 g/ T: a2 d3 {* o
- void CAN_Configure(CAN_TypeDef *CANx, CAN_WorkMode WorkMode, CAN_BaudRate BaudRate, uint32_t StdId, uint32_t ExtId)
1 ^& s3 `5 S# V, P2 ]! I' V* b! z - {
" H: ?: U& b9 }) N5 _. a A/ V - GPIO_InitTypeDef GPIO_InitStructure = {0};6 @( }* p# @$ g! O
- CAN_InitTypeDef CAN_InitStructure = {0};! l( N0 C. ?! q( V
- CAN_FilterInitTypeDef CAN_FilterInitStructure = {0};
- s* j' A+ b T# ^# k7 U - NVIC_InitTypeDef NVIC_InitStructure = {0};
- [" d6 @ R. s$ x! g - 5 u# `8 {! R! ~
- if(CANx == CAN1)
# r. W) p$ S: q$ k - {& _- u- _" I4 q( R
- if(can1InitFlag == false)( |! j" C& b9 ?0 h4 d1 }
- {% l/ W: w' j/ ~0 c+ Y
- can1InitFlag = true;$ N0 o" ~4 d# R b* Z! g
- 3 z, Z4 u$ s- y4 S! @9 B
- can1TransmitFlag = false;
- z" k C3 ~" |: k
' y2 P( G: N1 h( x2 e9 O* Q: E- can1TransmitFinishCallback = 0;" }5 X. s; t# |
- can1ReceiveFinishCallback = 0;
3 W8 y7 c! q' Q - 3 E9 z# W E- m" A
- can1TxBuffer = RingBuffer_Malloc(sizeof(CanTxMsg) * CAN1_TX_BUFFER_SIZE);
2 {) `' H" @1 B! ~: \# [ - can1RxBuffer = RingBuffer_Malloc(sizeof(CanRxMsg) * CAN1_RX_BUFFER_SIZE);
3 ^# p1 B. A) U9 T3 v
2 x. a3 L4 R% a4 L) f) _- #ifdef STM32F10X_CL
5 `" W& `. n; [$ R% a, C5 \ - if(can2InitFlag == false)" ~- R2 G+ D' B c8 x) J
- #endif /* STM32F10X_CL */
# j* N+ `, M) V) w p3 L - {
9 k; l/ k4 L* @, o) p - RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);# \$ N$ h7 \0 s
- }$ `3 s* `: o; w Q
7 q/ _7 P/ L- B. z3 g3 w2 L- X- RCC_APB2PeriphClockCmd(CAN1_TX_GPIO_CLOCK | CAN1_RX_GPIO_CLOCK | RCC_APB2Periph_AFIO, ENABLE);1 G! {! s& @( ?2 T" X W
d. |" N& }+ X8 [: P7 k- GPIO_InitStructure.GPIO_Pin = CAN1_TX_GPIO_PIN;- N. _( _, P. w0 X0 ]; P9 `
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;" T$ w9 p4 g$ ^+ @% W" A6 M
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;- B, z9 |9 v4 @( m4 `. |
- GPIO_Init(CAN1_TX_GPIO_PORT, &GPIO_InitStructure);( a5 v: C( M3 c# A
% @ V: u7 A! [* m- GPIO_InitStructure.GPIO_Pin = CAN1_RX_GPIO_PIN;8 Q! ?4 ~6 U/ `
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
5 w" \, ~) ^8 R2 E8 K5 u& {: T - GPIO_Init(CAN1_RX_GPIO_PORT, &GPIO_InitStructure);1 r1 j8 q7 D, K7 C; O, b2 G
) p) m0 j, O5 H- CAN1_PORT_REMAP();4 }5 o$ U( W8 N4 U4 {5 Y
- 8 x. ]+ T" I# m
- CAN_InitStructure.CAN_Prescaler = BaudRate;
4 [. a( M4 }" Q - CAN_InitStructure.CAN_Mode = WorkMode;+ l+ z0 V9 m( R) [" s1 L, C
- CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
0 f" @) Z. r" a' ^ - CAN_InitStructure.CAN_BS1 = CAN_BS1_3tq;: U* H2 w3 q; g! }% n/ X
- CAN_InitStructure.CAN_BS2 = CAN_BS2_2tq;
' s* ?- g4 N8 ^9 V - CAN_InitStructure.CAN_TTCM = DISABLE;
; l0 R# [4 F3 ^5 y5 {* f - CAN_InitStructure.CAN_ABOM = ENABLE;) S$ `$ M0 T+ o
- CAN_InitStructure.CAN_AWUM = DISABLE;
& V. }. k3 N7 o5 @2 \/ ]0 S - CAN_InitStructure.CAN_NART = ENABLE;
) s) |" p0 {2 B* j6 K) z - CAN_InitStructure.CAN_RFLM = DISABLE;
9 ~" _4 r+ p! y: M - CAN_InitStructure.CAN_TXFP = ENABLE;
( i- n* w9 k% D6 D - # b6 I6 A3 k/ Q! ]0 r3 \
- CAN_DeInit(CAN1);. W" A+ Q3 C/ T; W
- CAN_Init(CAN1, &CAN_InitStructure);
+ o8 Y B1 l% A% K
0 M4 f9 e& v Y6 d a; W- CAN_FilterInitStructure.CAN_FilterIdHigh = (uint16_t)((((StdId<<18)|ExtId)<<3)>>16);
$ i* z2 c7 E/ G1 n( l3 G3 j5 [ - CAN_FilterInitStructure.CAN_FilterIdLow = (uint16_t)(((StdId<<18)|ExtId)<<3);. G7 h( M) f8 J, V+ y" I" y
- CAN_FilterInitStructure.CAN_FilterMaskIdHigh = (~((uint16_t)((((StdId<<18)|ExtId)<<3)>>16)))&0xFFFF;
% R2 f( a! K4 l- G. v% O - CAN_FilterInitStructure.CAN_FilterMaskIdLow = (~((uint16_t)(((StdId<<18)|ExtId)<<3)))&0xFFF8;$ v& B5 A& d: h% ^, k9 _+ F
- CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0;9 C1 E* i, N+ Y9 p- V: E9 ?$ T
- CAN_FilterInitStructure.CAN_FilterNumber = 0;0 {! W% A3 Y$ J' J/ W( H
- CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;( W1 f5 e+ N# ]. P
- CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
0 m8 X- h( N5 V- o, u$ M$ T1 H* p - CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
6 P- S- N5 ? q& o, i1 A% `5 X - CAN_FilterInit(&CAN_FilterInitStructure);
1 y7 p C6 ] i% m - 6 G7 o* x4 p! }: j- k
- CAN_ITConfig(CAN1, CAN_IT_TME | CAN_IT_FMP0 |CAN_IT_FF0 | CAN_IT_FOV0 | CAN_IT_FMP1 | CAN_IT_FF1 | CAN_IT_FOV1 |
" C9 `1 ` j; d, G9 m6 w7 h - CAN_IT_WKU | CAN_IT_SLK |CAN_IT_EWG | CAN_IT_EPV | CAN_IT_BOF | CAN_IT_LEC | CAN_IT_ERR, DISABLE);
& g9 I& n8 A, ]5 H! N2 k; d - CAN_ITConfig(CAN1, CAN_IT_TME | CAN_IT_FMP0, ENABLE);
- D6 \# S) w! f( C" A. C4 F
& A2 C! V/ y% [6 m( ^5 a- #ifdef STM32F10X_CL v; B7 g* j7 I; r
- NVIC_InitStructure.NVIC_IRQChannel = CAN1_TX_IRQn;
# t' p* J$ @# U, @/ h P$ i1 m - #else
1 g* s0 p4 k+ U s% k) q) n/ `+ k0 k7 | - NVIC_InitStructure.NVIC_IRQChannel = USB_HP_CAN1_TX_IRQn;- _+ r. j* S, e! k' W6 n
- #endif /* STM32F10X_CL */1 n+ o+ _7 k. q3 z1 s
- M/ w: E# t) p; X0 U- ]3 l
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = CAN1_IRQ_PREEMPT_PRIORITY;
6 _8 g& c6 B" e$ E7 Q, z X - NVIC_InitStructure.NVIC_IRQChannelSubPriority = CAN1_IRQ_SUB_PRIORITY;
' h4 _3 M+ ^. w - NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;+ p# a W C7 H+ k
- NVIC_Init(&NVIC_InitStructure);. A3 d, ~4 Q' o) ?( A
- ) S4 f# T, y9 d8 B8 D5 T: r
- #ifdef STM32F10X_CL
6 t: t# S% Q! {1 E I+ K' p" _( Y - NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;( @5 S4 a. ]( U* v) y% e* ~3 R, T
- #else
$ H ^6 D$ z- j- X- C - NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
; u8 |- _* o+ P' Z: o - #endif /* STM32F10X_CL */
/ M2 ?) S3 [' ^/ w - # |0 a: N' } o" T5 C" g( ?" R% }
- NVIC_Init(&NVIC_InitStructure);& _) H( b5 _" J6 H
- }
3 R5 h( ?- L! T) G - }
( @6 ?0 e# y: D# _. U$ S1 i
, x0 \/ i: ]& S- #ifdef STM32F10X_CL$ b/ k$ Q, x4 c6 N- q
- if(CANx == CAN2)
, S0 p6 c" y1 g: n. J - {
) N$ r6 g- ~9 R) y - if(can2InitFlag == false)
) H# _3 ?- ]4 _8 z7 V - {
1 Y( i0 R# \1 ?/ h - can2InitFlag = true;
" u _7 `9 P3 T+ @: D, B1 P - # g& k7 A( k( u7 \
- can2TransmitFlag = false;
, J, q" l3 J) n8 @: \; m4 u$ {8 P& Z
- t- A; m' q) D4 a0 m) S- }: {: M- can2TransmitFinishCallback = 0;
4 Z: {* @) x ^4 [* h - can2ReceiveFinishCallback = 0; U# e3 p; s% I4 W5 T. Y' ?" a. M! {
3 {2 S: [: g4 i6 M. s/ @, c- can2TxBuffer = RingBuffer_Malloc(sizeof(CanTxMsg) * CAN2_TX_BUFFER_SIZE);% X: q0 I* m- A- A6 m$ |+ N
- can2RxBuffer = RingBuffer_Malloc(sizeof(CanRxMsg) * CAN2_RX_BUFFER_SIZE);
0 `6 S2 Q# y3 d" o, v4 Z6 d - ; c- g/ p+ o, P6 x4 G/ m# x
- if(can1InitFlag == false)
' ]' l( F6 f) O. `6 x: ? - {
# ^1 C$ P& j/ N: Z - RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);; J5 L: R' S6 w9 D. q
- }
?( p9 s+ }/ O8 }' d6 l" Q
6 h9 v8 @1 Y# W, N* E; \- RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2, ENABLE); w* T7 F, c% `* H' O- Q) @! l1 i
- RCC_APB2PeriphClockCmd(CAN2_TX_GPIO_CLOCK | CAN2_RX_GPIO_CLOCK | RCC_APB2Periph_AFIO, ENABLE);% c2 s; P- c3 X5 l( |) t
- " m$ J+ B/ I3 U( x9 F! ^ R
- GPIO_InitStructure.GPIO_Pin = CAN2_TX_GPIO_PIN;
9 `, Z4 F4 i* u6 _9 e ` - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;: b5 f% y- m/ ^1 l) m
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
5 H: ~# ^. ~- }5 u - GPIO_Init(CAN2_TX_GPIO_PORT, &GPIO_InitStructure);
1 }8 Z8 L6 _! O5 ^8 |+ c - / l4 p/ w2 t* X5 g% [9 `: C% `
- GPIO_InitStructure.GPIO_Pin = CAN2_RX_GPIO_PIN;
p. w H& c6 ~4 l @) F - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
6 O9 B! t4 Z7 t3 C5 P - GPIO_Init(CAN2_RX_GPIO_PORT, &GPIO_InitStructure);: w" a$ X) O3 O1 m7 Q
/ O- D ?' @/ e: `- CAN2_PORT_REMAP();7 h8 X( i6 N6 ?5 F' h1 `3 \7 u
- 6 m5 l. A- T" m3 H( x5 D) g# Z0 }2 ?
- CAN_InitStructure.CAN_Prescaler = BaudRate;5 _+ n/ j' Z) {/ a" p
- CAN_InitStructure.CAN_Mode = WorkMode;/ a2 |: p+ Y$ y9 v7 L9 }
- CAN_InitStructure.CAN_SJW = CAN_SJW_1tq; ^' T2 v( Q, d3 _. l$ i& S
- CAN_InitStructure.CAN_BS1 = CAN_BS1_3tq;
/ Y0 [/ B, u& H t. M+ D - CAN_InitStructure.CAN_BS2 = CAN_BS2_2tq;
/ H. j. t, Y5 _6 { - CAN_InitStructure.CAN_TTCM = DISABLE;) R6 p" @' R& O7 B: x
- CAN_InitStructure.CAN_ABOM = ENABLE;# l$ n/ C t' N, N
- CAN_InitStructure.CAN_AWUM = DISABLE;
0 d* \+ H" P- E - CAN_InitStructure.CAN_NART = ENABLE;; o/ d: n8 ]+ L, H, N* z
- CAN_InitStructure.CAN_RFLM = DISABLE;/ ?5 ^/ | b1 J# B7 B
- CAN_InitStructure.CAN_TXFP = ENABLE;
+ h" C+ s5 g: M% G/ _& X6 V
* s& \3 h. H7 S K% i9 ~- CAN_DeInit(CAN2);* e A$ Y" y4 A) Y5 Y7 e& O
- CAN_Init(CAN2, &CAN_InitStructure);3 g0 K) |$ h) @9 ?5 }2 ]$ L
- ( ^! H/ Y6 N! s9 e+ e2 f1 A
- CAN_FilterInitStructure.CAN_FilterIdHigh = (uint16_t)((((StdId<<18)|ExtId)<<3)>>16);
) r# k3 ~1 D# I - CAN_FilterInitStructure.CAN_FilterIdLow = (uint16_t)(((StdId<<18)|ExtId)<<3);
; L1 b D/ i- M4 I9 r/ W - CAN_FilterInitStructure.CAN_FilterMaskIdHigh = (~((uint16_t)((((StdId<<18)|ExtId)<<3)>>16)))&0xFFFF;5 A& C6 d* B% ~
- CAN_FilterInitStructure.CAN_FilterMaskIdLow = (~((uint16_t)(((StdId<<18)|ExtId)<<3)))&0xFFF8;
2 T0 F( N/ `' w j7 } - CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0;6 B8 q) X j! Y7 o* t4 A2 f- W
- CAN_FilterInitStructure.CAN_FilterNumber = 14;
6 z: Q) Q0 _) q6 c1 h" d - CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;7 e& G' f7 I0 O8 G8 U2 t$ Q- k2 g2 j
- CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
" J: A- S' Z& F- d$ K - CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;2 C1 ]% d* x/ Z3 @
- CAN_FilterInit(&CAN_FilterInitStructure);
+ i2 c( v' K, y! E - 9 N$ l* D. Y" o5 ]( }8 _
- CAN_ITConfig(CAN2, CAN_IT_TME | CAN_IT_FMP0 |CAN_IT_FF0 | CAN_IT_FOV0 | CAN_IT_FMP1 | CAN_IT_FF1 | CAN_IT_FOV1 |
4 i: ]- X7 ]4 M: I2 R, E* |/ S3 Q - CAN_IT_WKU | CAN_IT_SLK |CAN_IT_EWG | CAN_IT_EPV | CAN_IT_BOF | CAN_IT_LEC | CAN_IT_ERR, DISABLE);
, K' V( F$ `, B8 N$ `( u9 x/ t - CAN_ITConfig(CAN2, CAN_IT_TME | CAN_IT_FMP0, ENABLE);
# ^9 V/ @) w% T) q6 l( j
' @ V1 i# |6 Q+ e- NVIC_InitStructure.NVIC_IRQChannel = CAN2_TX_IRQn;
, M& O6 F- w$ a% e K - NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = CAN2_IRQ_PREEMPT_PRIORITY;+ y) ?2 W9 v, k" T# o% M" E
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = CAN2_IRQ_SUB_PRIORITY;
F* u4 e6 U& D - NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;2 m5 t/ H4 u7 c) _0 [+ J6 ~( i
- NVIC_Init(&NVIC_InitStructure);% R) |) l" _# v) q' f6 r5 a
- * t. j) Y0 ?1 O, f2 o" u4 E9 e6 _
- NVIC_InitStructure.NVIC_IRQChannel = CAN2_RX0_IRQn;. X3 k: c9 ^! j
- NVIC_Init(&NVIC_InitStructure);& x- y/ u% K& p, g3 i" j1 e' ~+ o
- }
' K) A+ Y# E; v9 q - }
" w3 s& B$ @3 I, u+ P - #endif /* STM32F10X_CL */& G) k: C, y' I4 b5 [) V* X
- }
* M1 k: H4 K9 A" m) q
9 B9 F0 x7 L5 w2 E& `- /**
0 w' ]! k7 ^2 z5 M/ c$ f- [ - * @brief CAN unconfigure.$ P: s1 {# l, c2 S
- * @param [in] CANx: Where x can be 1 or 2 to select the CAN peripheral.5 o, p. M/ l- m3 J3 U9 U1 Q3 w& S
- * @return None.' L; L8 S0 B' }+ Y4 `
- */, o0 t% X+ j) l5 p! G
- void CAN_Unconfigure(CAN_TypeDef *CANx)
: z7 Z7 s9 x9 O - { E* h, u6 w& b
- NVIC_InitTypeDef NVIC_InitStructure = {0};) ^9 M- j7 O: s8 Q
- 8 w3 k2 @$ `8 Y' W9 |
- if(CANx == CAN1)
+ M. n5 |( D# _2 o7 ^9 ? - {
! s# U2 @. r2 R% M6 D9 a6 J - if(can1InitFlag == true)& H/ x5 t8 _2 ]# h7 o" r* }# n4 g' j
- {" y( y+ G* I/ |4 ~1 d: i
- can1InitFlag = false;$ L' b0 q3 A- t% L
- 9 B( W/ b+ C6 M+ h! s. b
- #ifdef STM32F10X_CL+ b& g2 j+ x- W" d2 G/ {( B
- NVIC_InitStructure.NVIC_IRQChannel = CAN1_TX_IRQn;
3 }+ T$ Z" f, {* h) j. i; }+ ` - #else! F* t6 }& v& ]# X
- NVIC_InitStructure.NVIC_IRQChannel = USB_HP_CAN1_TX_IRQn;
) F) ~- Q1 K# b8 a - #endif /* STM32F10X_CL */
3 x0 C+ ?! `+ u6 X' \; G
6 [' k/ S9 {! b1 A% F+ n( Z- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = CAN1_IRQ_PREEMPT_PRIORITY;
8 M# w" f+ p7 w# M# q - NVIC_InitStructure.NVIC_IRQChannelSubPriority = CAN1_IRQ_SUB_PRIORITY;
( N7 H$ c: H/ r7 {. b$ q - NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
6 U; S2 v t, M - NVIC_Init(&NVIC_InitStructure);
6 y* e4 t' g1 B. H' B! [3 U1 { - # X3 q. b" N0 U/ [: k
- #ifdef STM32F10X_CL
- R+ S1 N0 [4 N% s2 W( A3 Z+ \ - NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
3 Y; h' C$ m$ L - #else3 @& L) A: s# R6 y8 d
- NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
% B- P. F$ H! | - #endif /* STM32F10X_CL */
: ], F t/ y& e. l# \. c3 S W! ?
5 x i7 b" X( Y6 X0 j7 Y' H# y- NVIC_Init(&NVIC_InitStructure);
8 j. a/ t; v. @7 B( R/ d' c8 V; h
% [) G! i- u9 R- CAN_DeInit(CAN1);3 y5 n- s1 C# _1 e/ n
( Y6 r" n: ? o! L& W- #ifdef STM32F10X_CL
" T! ]. \" o" M. `# G5 _, q$ O- g - if(can2InitFlag == false)
0 k ?0 _; D. ^+ N# y - #endif /* STM32F10X_CL */7 ~0 V( X9 F: O. \2 }
- {% x# T, }2 R g5 B5 W- z7 {
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, DISABLE);, P' M. N* N# D( K" N
- }
5 a. a9 G1 e9 C5 Y, n, a3 Y8 q5 s - + v7 {7 A% X* ^& j }, j/ X/ x
- can1TransmitFlag = false;, {8 J. x8 @3 l# M. w
- $ M6 B+ w/ z' O; a
- can1TransmitFinishCallback = 0;2 s) }6 z7 |) R6 i: j8 p
- can1ReceiveFinishCallback = 0;
1 ~. R, |! e$ S5 V% y, P* k* y% ^
+ W0 e4 F7 J* r4 |- RingBuffer_Free(can1TxBuffer);
$ B: v4 L. w$ d - RingBuffer_Free(can1RxBuffer);
, u& b8 o, m3 |' D3 r6 i/ L9 K' N3 h - }
7 U0 x! |, w! J - }* s2 E/ [" Z; f! B& \# A# C
- " @; ?" [# N F5 d
- #ifdef STM32F10X_CL1 v r; t: j2 o
- if(CANx == CAN2)
) k+ X' k, ]1 a! J% O - {
" ~3 W/ q( k9 V3 \8 ? - if(can2InitFlag == true), l7 Y" z7 E+ |6 c, E% g5 H
- {4 U! Z1 i0 K. C `0 b4 E
- can2InitFlag = false;
' T3 U* C: H9 j& k
" M7 Y: O& i) o7 V2 Q0 v* ^: @- NVIC_InitStructure.NVIC_IRQChannel = CAN2_TX_IRQn;
; ~ e3 f; l# ~7 @4 \# S* ] - NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = CAN2_IRQ_PREEMPT_PRIORITY;9 g9 R% o5 p* r/ i, W5 Q6 A
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = CAN2_IRQ_SUB_PRIORITY;! F# h7 W& X! B7 w
- NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
; g. L D# \: v% ] - NVIC_Init(&NVIC_InitStructure);
4 q) J) y L$ `( C. j5 i - b+ y! j3 }/ T, a: H' K, Z* I
- NVIC_InitStructure.NVIC_IRQChannel = CAN2_RX0_IRQn;* Q* M! B) S' C0 z( Y! V8 d
- NVIC_Init(&NVIC_InitStructure);6 p* |" x$ ? u- O) c- g
- 9 e. H1 P$ b9 u* j/ l% m H
- CAN_DeInit(CAN2);
% y* x# u( s8 T$ t6 g
3 J; I1 ~) a! ?/ ~- if(can1InitFlag == false)
- b h; w8 g" d0 A9 h - {" H1 V6 J0 i3 @' p- D
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, DISABLE);
& }: [9 y) `# d9 e4 ~$ |3 | - }# y) G3 |; i+ b) q7 d6 }" ~
; m, E7 E+ N) p4 B. d- `# i- RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2, DISABLE);' ~0 T, W; L. d! `' A+ d
1 L: [+ P0 V& m- can2TransmitFlag = false;
# Y# y: g$ r, r% { - - Z/ F6 R+ w6 O# g3 y' _8 Y
- can2TransmitFinishCallback = 0;; ~' m6 H/ M; c- n: W/ K
- can2ReceiveFinishCallback = 0;
3 O. m' z9 H# S( D
: w* ?9 P) \/ [& Y) g- RingBuffer_Free(can2TxBuffer);8 m4 `. }5 g8 u, Z! y: ^' q
- RingBuffer_Free(can2RxBuffer);
; a1 _$ l4 T% }/ a3 r - }
% [$ u4 r( ?7 I' C( [ - }
( g" H) F+ T2 n# b - #endif /* STM32F10X_CL */) j c& E2 ~% b" M( L$ e5 n
- }
$ _* ]' V, o+ |8 A
6 Q: M( V- G! n- T& |8 }* Q; w7 [) s- /**7 w9 x+ \9 \. k2 E, l
- * @brief CAN set transmit finish callback.( o8 b8 J# S: j: a, C3 L# X
- * @param [in] CANx: Where x can be 1 or 2 to select the CAN peripheral.4 @( q& y: r- R% H/ N
- * @param [in] Callback: Callback.
: b& f7 ]5 {: m - * @return None.
3 e/ z/ |: G# B) R% j - */
+ V( m7 u# a$ ?7 { - void CAN_SetTransmitFinishCallback(CAN_TypeDef *CANx, void (*Callback)(void))
$ |+ e8 }1 |7 P! ^2 p/ J - {; u9 G; ^9 N2 a5 q; E1 v/ h
- if(CANx == CAN1)9 H0 B9 l) \3 r% k5 f
- {& q) Q/ |+ n8 t8 y9 H N1 f
- if(can1InitFlag == true), l# D, z$ U- X2 R5 r
- {# d# a" z9 `# p& l; v) c
- can1TransmitFinishCallback = (volatile void (*)(void))Callback;
1 f9 Y+ l" G7 s8 y1 |) a - }0 k& O' s6 d! e3 R' f4 F0 K
- }, b. Y- P! c$ \0 E1 y
0 u& r$ p9 @' f u8 A0 t" S6 u# V/ |- #ifdef STM32F10X_CL' b& W' Q# o' Q$ H, U z5 y6 f5 k
- if(CANx == CAN2)
: y+ y4 S: t( F. h+ C - {
% Y# h/ h0 Z$ \ - if(can2InitFlag == true)% S1 N# D0 q5 \- A
- {
* C6 y; `# k# ?* Y& X - can2TransmitFinishCallback = (volatile void (*)(void))Callback;
# v8 ?8 b5 u/ f5 S! D0 `$ i p) [6 U - }" ~( q: a$ W# c w. `: Q
- }
: o/ _" I" c& Y - #endif /* STM32F10X_CL */5 X& Q1 o% m$ s# D
- }% H2 p- J6 `7 ^; u" A
- I+ q7 N4 p& S( j N8 i) H0 g- /**+ w% l( J1 X1 g6 y) @' F1 t
- * @brief CAN set receive finish callback.
/ d0 U5 B7 V* B5 d' z! p# l1 D; x - * @param [in] CANx: Where x can be 1 or 2 to select the CAN peripheral.. J$ q. O0 {: w$ Z$ \+ h5 {0 d" q/ H
- * @param [in] Callback: Callback.
4 w! [4 n* `$ u6 ~ - * @return None.
2 x1 K4 L8 X% G; q! B# U; i - */
* G4 K& d: U4 Q/ C# V7 a& V - void CAN_SetReceiveFinishCallback(CAN_TypeDef *CANx, void (*Callback)(void))
/ S5 W. `! d- ]* V) T: p7 C' x+ O( ` - {1 H! D+ [& X5 b% e6 }7 k
- if(CANx == CAN1)
1 _$ ?$ k3 r3 d% t% C& \ - {$ V: |+ v% K: U) h8 Y; w: i
- if(can1InitFlag == true)) Z5 X4 B! K* ?, G) O
- {
. Z# R! h1 ]1 _. f - can1ReceiveFinishCallback = (volatile void (*)(void))Callback;
; ^. k" j! @4 B1 V9 i) H2 S9 m - }
7 w- W+ a5 U) R m$ X e - }
& _# ]/ E( `8 E7 s - ; X6 x; X/ y9 E1 g# z1 y
- #ifdef STM32F10X_CL# u* j# @5 ?) v& D1 v( E, h
- if(CANx == CAN2)% ^8 R" V7 i- F: }4 X, o4 }
- {
) K0 ?" P y3 x: |. h, ] - if(can2InitFlag == true)# \$ P' I6 g; u5 |8 h" \
- {9 J( P( J6 b/ f, B+ \& r' T
- can2ReceiveFinishCallback = (volatile void (*)(void))Callback;9 Y( s5 |8 J! \! W8 z S6 v' o
- }
+ m0 J, o$ c+ s - }
5 `3 q c h8 f - #endif /* STM32F10X_CL */( E) C/ ~7 b5 r& B
- }
' l' G! j/ \) x, ?
) r" `% ] H& b- /**0 Y* F3 {4 P$ o$ G# Q, N! M% k" {
- * @brief CAN set transmit message.
0 ~) l9 V+ X$ b6 H @) e. k - * @param [in] CANx: Where x can be 1 or 2 to select the CAN peripheral.
/ _9 \' F7 o, w) b! U& J! m - * @param [in] Message: The address of the message to be transmit.* k& s* y( d7 r# S
- * @param [in] Number: The number of the message to be transmit.# t1 Z7 F# k, |
- * @return The number of message transmit.
; Z5 v. U' [$ V) T* Q - */
- _$ @/ E3 B. ` - uint32_t CAN_SetTransmitMessage(CAN_TypeDef *CANx, CanTxMsg *Message, uint32_t Number)
8 ]: Q( ~ E0 q) r - {7 }. v7 T8 t' c3 v& |
- if(CANx == CAN1)1 B- z7 ]/ x1 [/ l
- {9 o1 w* `# z" q" n3 w% l
- if(can1InitFlag == true)
/ z3 k% W8 L& F# D - {- b! x' t3 p$ q* H; l
- uint32_t available = RingBuffer_Avail(can1TxBuffer) / sizeof(CanTxMsg);+ F0 M! ?3 l5 o& n- Y; u, w% s
- 6 {& p4 I9 @- z* @
- if(available > Number)
+ I( j9 |/ p: D! j/ Q; x - {
U! T+ e, k* d1 b - Number = RingBuffer_In(can1TxBuffer, Message, sizeof(CanTxMsg) * Number) / sizeof(CanTxMsg);
5 ^. f0 ]. r4 w# u# @7 F3 i' d - }( a2 x* a. t- f7 U( J
- else
5 B2 h" I" f# ~# x" Z8 F. w, } - {
5 ~1 X2 ]- @% i F - Number = RingBuffer_In(can1TxBuffer, Message, sizeof(CanTxMsg) * available) / sizeof(CanTxMsg);; \5 q- d C5 |/ \7 a9 X
- }
v* Q, a% l: L
* ~, G/ d: S. k% `9 L% P# v3 f' P5 U- if(Number > 0)/ [2 h( `( _( a- n2 A- t, a }
- {8 ^ n& T& C; r. U
- if(can1TransmitFlag == false)4 E8 |6 L* _2 Y* D* K
- {% B6 Q. h8 y3 ~& v5 Q6 C$ Q9 K' a
- can1TransmitFlag = true; R$ I/ E; v1 d2 C- x9 f" u
, U- j; K) D, _9 S' j- K+ Z$ _- CanTxMsg canTxMsg = {0};7 _: Z+ G8 C/ L$ c
- RingBuffer_Out(can1TxBuffer, &canTxMsg, sizeof(canTxMsg));
0 \) x1 J0 | v8 j - CAN_Transmit(CAN1, &canTxMsg);
. q) o% n6 o. ?: D7 Y4 W0 Z) m - }
2 h* V5 n, x5 N+ {1 k - }3 O2 ?2 Z, g5 X
- 8 Y5 A# y" X* z3 m3 G+ ~' r
- return Number;" R2 U+ _. ?0 ^6 o# {
- }
7 n; D/ N; M1 O* p2 ^- G) W - }. e, O3 \( Y8 c! h; C
( T- s+ u) i0 H' j5 H- #ifdef STM32F10X_CL) X7 ?" I& `5 D5 y% X
- if(CANx == CAN2)* }# T" i0 w8 @' d3 B# T6 {( A
- {
, e% D9 F. I# Z- {4 w - if(can2InitFlag == true)( C9 b$ a) b4 R- x) o: L( C; g# P4 k
- {
5 l# @3 i, d5 ?( k4 h4 y3 B - uint32_t available = RingBuffer_Avail(can2TxBuffer) / sizeof(CanTxMsg);
& a3 q) x$ P! s: L
# G3 o3 w- z# T3 Z- if(available > Number)
+ |1 D0 W5 |# i5 g2 Z- W - {8 l7 r6 C$ k- M, x
- Number = RingBuffer_In(can2TxBuffer, Message, sizeof(CanTxMsg) * Number) / sizeof(CanTxMsg);
. c" ^" P9 O$ N" F N - }
' R3 `2 t- J+ Z" m! ^ - else
' u8 f3 q M# G6 |6 b7 w- i! n+ C - {: w; u% E3 Z) g- c8 e% r, ^: I, D, `
- Number = RingBuffer_In(can2TxBuffer, Message, sizeof(CanTxMsg) * available) / sizeof(CanTxMsg);
/ ?1 Q% r8 J5 Q' w; F4 \ - }% c2 E# O" m+ \4 g
- ! _. e9 i( @! ^# l- K3 a$ j
- if(Number > 0)% z( ?7 _2 e X: k
- {
6 F1 W+ o" c1 X - if(can2TransmitFlag == false)7 L2 w+ e! Z x1 I# k6 L" b) b6 b' V
- {
* L' r- J+ a! T" J3 F5 A - can2TransmitFlag = true;- m7 I' [: J; o- V$ C
- 4 b9 X" D* Y! b) b* g* d
- CanTxMsg canTxMsg = {0};( N' n( O. _% |" S$ ?: C2 j2 y
- RingBuffer_Out(can2TxBuffer, &canTxMsg, sizeof(canTxMsg));* e3 D2 b/ b# ~' `% Q) v4 g4 \: U
- CAN_Transmit(CAN2, &canTxMsg);+ ^' w# n" Z# k, L
- }
4 m# k) `5 t# Y% _6 l( _8 B - }5 b3 f6 t( c' w4 X
& u$ t$ O1 ?( K7 f2 Y- return Number;- U! m# \8 m9 p: P. V" k
- }
1 ]' T6 j6 b" W9 e) ^. n; ?+ ? - }. {' g3 \1 v& Q
- #endif /* STM32F10X_CL */$ \0 N: { x' n+ u% S6 R5 h2 Q
" g! K: |' N; p `- return 0;
5 r# T+ t- q D - }
/ V7 ?* J( Z& B \ - 5 D$ h4 D n) |/ z) ], B# |% |; f
- /**( O8 f2 n3 e# g- v6 |$ U
- * @brief CAN get receive message.2 ^9 y5 ~5 ^+ I
- * @param [in] CANx: Where x can be 1 or 2 to select the CAN peripheral.
3 r* @# {0 [/ U9 M" u - * @param [in] Message: To store the address of the receive message.
. \* [2 W5 e4 R+ v T2 c - * @param [in] Number: To read the number of the received message.% q6 P# i' U1 t- B! J. }
- * @return The number of message obtained.
( y7 c$ y5 n4 ?* b% @* x2 q& ` - */* l- j% Q [( V# M8 ~5 R
- uint32_t CAN_GetReceiveMessage(CAN_TypeDef *CANx, CanRxMsg *Message, uint32_t Number)
8 ~. @' A$ ~: J" E( j - {
: W- {* M- K. e9 L, z% r - if(CANx == CAN1)
0 U$ P# A% O7 P. i$ O/ ? - {
& `" Q" a7 L* R2 s8 A - if(can1InitFlag == true)
- ?( K' l7 T- ~! W' I k0 }4 r - {( B2 `- [' a6 M3 b" Y/ o' C
- return RingBuffer_Out(can1RxBuffer, Message, sizeof(CanRxMsg) * Number) / sizeof(CanRxMsg);
6 m! X) b |/ Y" W* K& | - }
$ C. U' U& S% |/ _ A# Y - }! u# A" W- o0 i
- * k* k I/ Y" x6 c. X: V
- #ifdef STM32F10X_CL
; u2 S$ Q' ~/ m8 [' F$ | - if(CANx == CAN2)
: u# s- d& Q z/ ] - {7 _" g; l* \2 Q; i; V& q
- if(can2InitFlag == true)5 Z2 A8 e( h4 V& @
- {
$ p* k D+ q% k9 P3 ^5 U( ^ - return RingBuffer_Out(can2RxBuffer, Message, sizeof(CanRxMsg) * Number) / sizeof(CanRxMsg);+ Y7 h- L; U5 ], x& e
- }
6 @: y% W. V4 ^8 d5 e# a$ P$ s - }
) q5 A2 v7 r) }2 s7 J - #endif /* STM32F10X_CL */9 K; M) `0 h, _, u4 L& r
S3 ~& R9 i7 ?$ o3 T+ \2 H- return 0;
3 h/ d/ }5 d9 l$ o/ F+ O - }( ~2 T" x8 N. v1 |: I3 g% f
0 G! U. h5 }! O. j3 W* e& h! D- /**
9 f) }; u7 I6 `# M8 m- g - * @brief Get the size of the CAN transmit buffer used. S' u2 L: F/ F: S& b/ M& X
- * @param [in] CANx: Where x can be 1 or 2 to select the CAN peripheral.
+ ^, U' f! u, L. W/ J0 j - * @return Used the size of the transmit buffer.
# p6 B+ Z U" v. M* t6 P9 u - */+ U {! I W% `" }6 A
- uint32_t CAN_GetUsedTransmitBufferSize(CAN_TypeDef *CANx)
, i2 o! ~+ D! h B( N8 {7 Y - {: e- A7 K- R' {! ^. o! ~
- if(CANx == CAN1)
# s3 {% A3 N& K8 _* @, i% @5 ? - {
2 b. i+ O6 O9 } O - if(can1InitFlag == true)% O1 J5 [! N+ u
- {
^7 l& `4 D, D$ j v# I - return RingBuffer_Len(can1TxBuffer) / sizeof(CanTxMsg);( T3 d4 j8 i9 f+ X9 ]
- }
) j& D8 s& \# c) z. M - }
( y5 z# } U4 y, ^, E9 I; }& z
. _5 f' f& Y" c' d) V: d( p9 D- #ifdef STM32F10X_CL
) l6 @! n* u; |! Y9 V - if(CANx == CAN2)" ~* r9 D/ B; C: D6 u
- {
+ U0 r8 T2 C6 P- t" \# f R - if(can2InitFlag == true)9 Q4 Z: d' o; r
- {2 i6 m7 t$ }: y, y/ K2 f
- return RingBuffer_Len(can2TxBuffer) / sizeof(CanTxMsg);9 U; T9 Y1 H+ z/ l) B
- }/ t: z7 Z" Y+ S7 k
- }! G% n8 t0 x% R
- #endif /* STM32F10X_CL */
# Z u" U' z$ E1 L3 _$ T" O q
' j- l; }8 ^+ L E. {- return 0;; W1 ]% E6 `6 i0 C9 j: ?
- }# ?0 \: M" x7 p# \, F; I y
/ s8 v, n: a; Y( |) F) a' `; |- /**2 S8 U9 P" P- l
- * @brief Get the size of the CAN receive buffer used.0 ?; @; n- A- Y5 @! |
- * @param [in] CANx: Where x can be 1 or 2 to select the CAN peripheral.8 z( X, W4 r( q. q
- * @return Used the size of the receive buffer.: e E9 {7 p) r* @
- */
. V8 w* M" ]! s9 e5 n - uint32_t CAN_GetUsedReceiveBufferSize(CAN_TypeDef *CANx)+ C w6 r, H' _2 G1 _
- {/ D/ J2 o- d2 Z0 K: |- |- i
- if(CANx == CAN1)" V% ?% {7 W" @* z4 i$ A
- {
1 z2 N0 A! ?) A; u& U# u9 u; J - if(can1InitFlag == true)! x; v' [" V( p. p. D& ?0 P) t
- {/ ], M2 _% _' x; D7 O
- return RingBuffer_Len(can1RxBuffer) / sizeof(CanRxMsg);) M0 ?8 J& H' m4 s& V* T( M
- }
! W0 t, y0 ^) ^7 u% E - }1 } w0 p) M0 p. }. H. d
* Q6 z& l3 Q: }5 Q- #ifdef STM32F10X_CL
% P: n' e9 d2 J$ f6 e8 Z9 h. Y% W* A - if(CANx == CAN2)
8 N9 W f( c0 F+ q) P0 Z4 ]% d$ y - {
# C* H" r- p: i- w - if(can2InitFlag == true). c. {/ u* W, f, t5 {6 v
- {
; b! ?4 ]" l% d - return RingBuffer_Len(can2RxBuffer) / sizeof(CanRxMsg);! O0 { M9 }4 @
- }
) I. S/ ^6 k+ S3 q - }+ q" k+ N- W+ l* g& `+ @
- #endif /* STM32F10X_CL */1 x& X) O, L( @9 e3 e& ?8 U, n
0 _. Z& t/ {6 z3 m, @- return 0;+ V. u( @: ]1 E" |( W+ e7 t
- }: u2 O1 M, ~& Q$ U0 ^
& _8 I% Y8 x2 I+ F ]% U1 H- /**
) p- W' `% Q! c% R- D/ b/ r( T - * @brief Get the size of the CAN transmit buffer unused.' a/ M5 A% f* @- v$ i
- * @param [in] CANx: Where x can be 1 or 2 to select the CAN peripheral.+ C) ^4 ~, z8 l
- * @return Unused the size of the transmit buffer.
/ C; V% b9 x) c2 K2 o - */' t: Y3 q S: M: l5 M* P+ f. C( A
- uint32_t CAN_GetUnusedTransmitBufferSize(CAN_TypeDef *CANx): q C% }6 S& f1 O
- {
. g5 ]9 c/ j, Y( f% ?: I1 r - if(CANx == CAN1)! B4 y) `5 Q# g4 Y$ J
- {' z. j3 ]( D' Z3 K! ~
- if(can1InitFlag == true)7 p! } H* \4 e* M0 X, q
- {
9 {1 `8 y7 A$ h% s# g2 j3 o9 ?* J& X - return RingBuffer_Avail(can1TxBuffer) / sizeof(CanTxMsg);/ W" |2 D* r- C4 f6 S/ U0 z
- }3 z$ t: E+ `1 r& `3 D% ?2 n: m$ o
- }
2 D( k6 ~6 J' N! W- }% n0 u - S7 s5 O4 C( c K. J
- #ifdef STM32F10X_CL
' ?0 ]) A6 j8 N" T: v - if(CANx == CAN2)
& o8 ?5 |' G' X! H - {* Z; M2 q/ `3 B
- if(can2InitFlag == true)
! C7 F$ e" q1 C5 i6 F - {' C" A2 w6 o6 I4 s6 \0 @
- return RingBuffer_Avail(can2TxBuffer) / sizeof(CanTxMsg);! v/ `- C% ~8 J5 }& z4 T) I
- }
& ?0 W# C+ B1 B0 j - }
8 C5 m4 |8 @( l/ Q4 I5 I) c - #endif /* STM32F10X_CL */
9 M; |! l+ p. b1 C
. ^3 z- T* @. y4 j% Q- return 0;0 O5 e! ?9 R' P. r u( ?$ o# R
- }/ `4 A3 K2 q/ \, ^/ q
0 q/ N0 g W& R5 V) O# A: l0 z- /**# k8 h, E8 u( \: C/ ^' \
- * @brief Get the size of the CAN receive buffer unused.1 s: ~: C3 G, A' w2 ]: ^
- * @param [in] CANx: Where x can be 1 or 2 to select the CAN peripheral.+ }, c- \8 B- I. [
- * @return Unused the size of the receive buffer.
+ u# U2 M: n; a. E* X9 G; V - */
g: ?8 e# T; R, `5 L: } - uint32_t CAN_GetUnusedReceiveBufferSize(CAN_TypeDef *CANx)
$ `, q' b. @. Q1 i/ b8 x - {" ~/ y+ g. l: z" y$ ^2 W
- if(CANx == CAN1)& {# o, T6 y: U. B! a& Q2 N
- {8 \: Q8 {+ S( q
- if(can1InitFlag == true)% Z0 u B5 u5 E' Z; s: j
- {
P) Q7 R1 R z- T D, X - return RingBuffer_Avail(can1RxBuffer) / sizeof(CanRxMsg);
% ]6 R* H) q; r3 x# a - }8 k' D3 C3 j3 M
- }8 w+ g# f7 o+ a& x% r
- n4 F4 B2 u& Y- #ifdef STM32F10X_CL! n: `6 u7 u$ C+ ~9 b
- if(CANx == CAN2)
, W, U" u3 v1 Y6 a" b! B - {) g4 v' R+ o' B. A" X4 X
- if(can2InitFlag == true)
6 Z% b' W/ J A - { w8 E0 L1 u5 K- C4 j" ]; o% A
- return RingBuffer_Avail(can2RxBuffer) / sizeof(CanRxMsg);& J5 @" }* G- m1 B/ x/ b! F. r. l
- }
6 R/ b1 ?3 v' p) I+ C: A - }
8 d$ w! ]* g) U4 h - #endif /* STM32F10X_CL */1 \; W7 u! Z1 }1 `( P% S* G
- ( ?1 k7 V+ {4 j
- return 0;
( p; J2 o' b: I0 x7 T0 s; m& Y- X - }+ X7 e% p- p! L# h
' j) r* G; T5 v! ]6 M. u7 ?- /**
M- b) H5 C2 d8 j& T$ k5 ^ - * @brief Is the CAN transmit buffer empty?, X5 S& J8 @% q+ g( `
- * @param [in] CANx: Where x can be 1 or 2 to select the CAN peripheral.
' c/ W6 `3 k) a9 Q7 x - * @retval true: The transmit buffer is empty.
9 a/ U' ~" q/ v6 ]4 I1 m - * @retval false: The transmit buffer is not empty.
V9 _+ ~9 \' \0 p( Z - */
# i3 m) ?6 }7 W. U2 P7 V$ R - bool CAN_IsTransmitBufferEmpty(CAN_TypeDef *CANx)1 D# J9 Q2 S: P
- { H( d- {! {; N2 p% A+ p5 o/ l
- if(CANx == CAN1)
3 R, [3 c# v8 T$ ~- k7 K2 B6 @ - { B& \* f L2 I: P8 f! `1 ^
- if(can1InitFlag == true). S o. r& y1 A# X. M {
- {" N) b, g0 d1 r# `$ H
- return !(RingBuffer_Len(can1TxBuffer) / sizeof(CanTxMsg));
. R5 R5 o+ \) k, h' W - }
7 G/ o2 ]" c* d# g( a3 g2 N3 m - }
2 h1 T6 j: ~+ ? - . f ^( z, q8 H. X8 x
- #ifdef STM32F10X_CL
# k- m7 _# ?; ^# a! \7 t" r - if(CANx == CAN2) i( S1 _ ]+ a0 A
- {
' U. l! q! s# ]( r: Y, ~% G) N9 ^ - if(can2InitFlag == true)
$ W- `* j: e2 m+ \7 T1 ] - {
4 C M g) C* W4 |3 u3 D: } - return !(RingBuffer_Len(can2TxBuffer) / sizeof(CanTxMsg));
k5 |3 Z# @5 ?0 z' i* g - }0 r. f- r5 F- W8 F2 K$ O) Z
- }1 T% n1 r" X( \4 r6 V
- #endif /* STM32F10X_CL */
g9 P% [( K+ v" D$ }/ l
/ Q) w* r+ s0 I: A- return false;8 p1 x) ]( U" ~
- }' w4 A. J/ O- g( d! m
- 6 |$ w' ~) ^+ V' n" r, r8 h: b; V
- /**
& o$ H/ F1 [; h( q, v - * @brief Is the CAN receive buffer empty?+ }9 ]* s w3 \
- * @param [in] CANx: Where x can be 1 or 2 to select the CAN peripheral.
7 {6 b& P! f, x) U: b - * @retval true: The receive buffer is empty.$ H+ H) J% ]+ i
- * @retval false: The receive buffer is not empty.( e, _; Y8 t5 e0 c! }- F; I
- */
" |/ f: `- F$ q; I7 s$ I$ K - bool CAN_IsReceiveBufferEmpty(CAN_TypeDef *CANx)
- D8 \! X9 |2 o0 o* P - {( g" E/ r) y' g$ A) @
- if(CANx == CAN1)
3 {3 U2 M1 N' V) R' b3 ]# w - {
+ J5 `3 }, E: v8 @! b- c - if(can1InitFlag == true)' i- R: r- p" N* A! F1 i
- {9 n! F' ]$ d% A
- return !(RingBuffer_Len(can1RxBuffer) / sizeof(CanRxMsg));" ?: z' L4 ~% K, }) @% o
- }' M& Z1 d# y+ J
- }
0 r) m1 ?* u/ m A) G
2 b/ T7 |# x9 u7 \5 j' n3 f- #ifdef STM32F10X_CL* b) b2 o$ {! h( j5 G6 {
- if(CANx == CAN2) E# R0 C+ s/ ]/ U3 k \8 ^9 H
- {/ D: R2 x1 v: s: E& ^
- if(can2InitFlag == true)9 l3 v5 N* J/ t# F- `' I
- {2 Z' j% p! k+ U2 x' N2 K
- return !(RingBuffer_Len(can2RxBuffer) / sizeof(CanRxMsg));
& [% K1 F+ I, |. h9 g9 m! F. a* p# R - }2 `) l! z5 \9 m* J% Z
- }- T7 D0 y' U$ m! p0 V- ?
- #endif /* STM32F10X_CL */! a y0 {' x0 F9 V1 I
& ~& f* R7 D. f: F! }- return false;4 E: E/ _4 N8 W2 ]+ g% x4 R8 x
- }
8 J* G7 k) R7 F' j+ V$ } - # M, h& Q x* ~: s" y
- /**
* P' A8 C& b. }7 u$ h0 M7 ^5 [ - * @brief Is the CAN transmit buffer full?5 k/ q1 \% @( R6 p9 L
- * @param [in] CANx: Where x can be 1 or 2 to select the CAN peripheral.* L' M, H2 U- T0 i/ ?9 ^
- * @retval true: The transmit buffer is full.
! v8 z! R8 N$ m l - * @retval false: The transmit buffer is not full.
4 A( \% ^6 ~/ r9 @* z - */4 H) x5 {6 ~- P
- bool CAN_IsTransmitBufferFull(CAN_TypeDef *CANx), y1 g% ~/ G( i# a6 M- g" w- a
- {
1 ]0 Q! R5 V0 ]5 K( s - if(CANx == CAN1)
. B6 p% [# K6 l) B - {
/ O. I) I7 ~$ p0 E - if(can1InitFlag == true)" p- v/ s" m/ e7 ?- V- Y n/ s+ R1 b
- {
, l# v% u7 b% e# m0 ]) c - return !(RingBuffer_Avail(can1TxBuffer) / sizeof(CanTxMsg));2 o4 K: h* Q' ^
- }
( q5 `3 O9 {" b2 U - }
V8 H& @- i! w - 7 y6 _2 B' m; L! k
- #ifdef STM32F10X_CL8 ]3 o, T0 D' W% m6 h4 u; l* F2 u
- if(CANx == CAN2)
0 N+ k% G4 Y, U5 _& H8 {$ }" q, B' A - {& Y) L3 E" @1 U: r4 |1 b
- if(can2InitFlag == true)6 L2 k* K6 r& i5 G b
- {
8 _- \* x0 S$ a* } - return !(RingBuffer_Avail(can2TxBuffer) / sizeof(CanTxMsg));
% ~6 K8 l! s; `3 o% f+ b. h - }; c- ~6 T Q8 p2 f1 g
- }
4 U1 j% H& l( B - #endif /* STM32F10X_CL */1 m+ f1 }1 m# A2 B4 [; l
! f2 m6 x; D% q# `" d- return false;
; W) Q8 p/ V; w7 x% X - }6 C: J! S) Z2 P# M, L- m+ l
% g0 G) Y/ [* A. o5 ~* @/ X- /**
. h% L3 n. c7 V% J" Q) u( W - * @brief Is the CAN receive buffer full?- ^1 [" Q2 {8 B# q0 R2 h. {( c) v
- * @param [in] CANx: Where x can be 1 or 2 to select the CAN peripheral.& T- [7 M' D+ s, Y* Y2 j
- * @retval true: The receive buffer is full.
' K/ `# s* g1 j' Z - * @retval false: The receive buffer is not full.$ ]" G' V7 D M; h! `
- */5 n" d5 j* r# x
- bool CAN_IsReceiveBufferFull(CAN_TypeDef *CANx) c' y' H6 ]/ _7 }; }
- {
) ~' V$ U) r( V% y8 S9 J - if(CANx == CAN1)2 H, t3 z2 }2 P# h( R1 D
- {& T7 r9 a& n9 G
- if(can1InitFlag == true)6 ?& B" O0 k3 C; ?8 P. c' {
- {9 G- [4 @" K2 b$ }2 e+ @# z
- return !(RingBuffer_Avail(can1RxBuffer) / sizeof(CanRxMsg));* n/ K7 I! C/ `
- }
$ ?& Q5 l0 A* w - }
: z& Z# m) }3 z3 ~* p" y
* i% j6 |& \1 r- #ifdef STM32F10X_CL
9 ^2 x. Y- z7 G, M ^, ^ - if(CANx == CAN2); q6 R7 \6 W- j( T; m
- {
0 |; j$ L2 w0 N' j - if(can2InitFlag == true)
1 D# B% H: @0 C) }) T5 t; s3 J0 b1 [3 r - {
+ }5 i& H0 j4 R# ?, m) } - return !(RingBuffer_Avail(can2RxBuffer) / sizeof(CanRxMsg));0 B6 T( d8 \. o, }! a( A
- }
+ h7 ?+ w/ k, q, Q3 J9 B - }
1 V/ K/ i) |- a - #endif /* STM32F10X_CL */' m7 V7 A' N8 e% l+ c6 l
- 5 v. C3 }- N% i# n
- return false;1 C6 J# ?3 ?1 M
- }
- v. N7 i. C: Z5 `7 R; D
, A, r( e! _( H: T% V- ?- /**# u: {! ]7 T1 }& L4 j/ a
- * @brief Clear the CAN transmit buffer.
$ [) x9 i6 [+ S" G$ K+ j3 [: M - * @param [in] CANx: Where x can be 1 or 2 to select the CAN peripheral.
: \0 Z+ g6 K, a1 E' }1 ]8 S - * @return None.+ ], a6 ^( u r7 \: R9 P
- */
, E. g1 a3 d- V: L, @ G8 ?/ E - void CAN_ClearTransmitBuffer(CAN_TypeDef *CANx)1 }* g6 y& V; p& G
- {
9 t* l6 n2 T( k - if(CANx == CAN1)/ c: m ^- b8 z& s2 L2 I
- {% z8 @' B$ \9 z2 \
- if(can1InitFlag == true)1 k1 Y! e7 a3 E7 h
- {
6 p5 x7 n' \ N1 q% a - RingBuffer_Reset(can1TxBuffer);
2 J/ \8 g+ l# q# Q" y - }0 a# C* X$ t; P$ \9 f4 J1 a
- }( w) o* F( M1 i& \6 D) J& l4 }3 f7 ]
- ' a* T0 V- z* a1 R. t4 c* `+ k' Z
- #ifdef STM32F10X_CL
# \2 v4 `+ N. N) h) `) x - if(CANx == CAN2)5 K6 r, Z) a+ _3 p" j
- {6 ]2 J- C0 e3 V) n0 {& f/ N# v1 Z1 n8 d
- if(can2InitFlag == true)
2 U! ]$ K4 E; d- K: g/ P4 F - {6 Z. g0 e$ J8 ], n
- RingBuffer_Reset(can2TxBuffer);
. g: u, p) c$ P5 e' f - }; R1 `" w' _/ s& d1 M
- }
?6 _( X3 x' V - #endif /* STM32F10X_CL */0 Z, v3 I [: h8 o, ^. y) X; @
- }3 c, o3 o4 ^7 e) N
- - ]) U5 ` b5 s, K1 M" @
- /**
" ^! y* `% m$ c$ W) r: _ - * @brief Clear the CAN receive buffer.' Z" h/ i4 l6 Z$ p. x% w
- * @param [in] CANx: Where x can be 1 or 2 to select the CAN peripheral.
$ G; J; E0 x8 Q9 X8 V* ` - * @return None.
% R9 T1 |6 o. @; ? - */" W3 [" ]8 K' \5 O
- void CAN_ClearReceiveBuffer(CAN_TypeDef *CANx)0 y4 A9 F2 D/ D' L! C0 E5 B/ X
- {$ K% H' L. { | T
- if(CANx == CAN1)
* H! u3 X& Z) }2 l- d" e( @$ l2 o - {" u, o l) E! W% v) B6 k2 n
- if(can1InitFlag == true)$ j( V; R1 }+ r& W; ]
- {3 [0 B/ a+ H2 j# ~
- RingBuffer_Reset(can1RxBuffer);
% e: Z+ \. Y- b3 J - }
# {* {5 F7 u& y5 `6 e# f G - }
0 Q5 N, k: ^, o0 d% |
2 F7 x2 i$ E4 Q) [1 v- #ifdef STM32F10X_CL! Y- H" _: p3 x6 Q) o
- if(CANx == CAN2)# `/ Z, K- |( w$ a+ u9 Z$ j5 b
- {
: \3 k3 }/ b ^) P8 }" | - if(can2InitFlag == true)
2 F% Q) N$ `! E& V3 \ - {1 V( z+ r6 @/ N' f5 A3 H
- RingBuffer_Reset(can2RxBuffer);
( I; o! y7 f/ m) e. ~# } - }
. ~2 W7 N4 |2 Y! g - }, W9 H4 C. {* |/ {4 Q
- #endif /* STM32F10X_CL */. f$ t$ _% ^- C! ^# @: u4 H
- }1 e, q1 _/ M5 u
- 1 ~' r! U9 @8 g( d6 ]$ Q4 q
- /**0 Z7 A) A2 P. g) J& }. p
- * @brief Is the CAN transmit a message?
$ k* z8 G# r% K3 ]% e - * @param [in] CANx: Where x can be 1 or 2 to select the CAN peripheral.' H) h0 j3 m; z' k
- * @retval true: Is transmit a message.& k5 g% i8 j/ l: U! r0 J& ~
- * @retval false: Not transmit a message.' R, A6 Q) m( O1 M* X
- */3 s6 C) Q. m8 o# \+ `
- bool CAN_IsTransmitMessage(CAN_TypeDef *CANx)
* f5 r& T. g8 }( ]( k s4 G8 p - {
9 w, T2 @7 g# c4 n1 W( d) ]- q - if(CANx == CAN1)( w6 _* h& Y) p& m1 p
- {, z s4 P, N3 G9 j
- if(can1InitFlag == true)+ ]6 i2 W, i6 x7 @) ^6 U
- {% @ G1 ]2 W, N( Z7 u# `
- return can1TransmitFlag;
% \6 t3 F# `" H$ H- L* V) M - }- @) r& u. Q% ?8 y9 g
- }
6 _; g4 q8 p- U1 D* G9 R! ] - + D% e5 l6 H8 E4 j! E6 F6 q6 ]$ j$ n0 H
- #ifdef STM32F10X_CL
. C+ P8 H7 ], m, y E - if(CANx == CAN2): C" k% ]* K; @7 K
- {) ^3 Z2 E# e+ M5 @! u
- if(can2InitFlag == true)
; D2 }' `) A2 x) P( X - {' Z: Y. ?! x! s* g- F+ b% ^
- return can2TransmitFlag;
0 S" r# ], b2 y - }+ C7 s5 K. g7 B3 q8 {- D
- }9 p# x2 W" I A" p
- #endif /* STM32F10X_CL */
; t6 t* d" ^: U4 Q& O# ~9 u; t; J - 1 h4 t& M9 n! T5 U( M( u8 J
- return false;
( H$ v) r) O" C, P: q' r) L% Q9 D - }; @( W" ?+ a/ }( K8 ^
- & j/ g9 \! L$ u& o! _
- /**
$ G3 V0 u* u, ^1 e - * @brief This function handles CAN1 TX Handler.# m: ~4 w/ v: o. `$ [
- * @param None.. e ?9 S/ e* y c0 e# M
- * @return None.
8 }# p6 m) h* e - */$ @, j) O2 t9 l0 C/ |% V8 E6 m
- #ifdef STM32F10X_CL
0 A, }& y$ W' ?" |7 }/ ~ - void CAN1_TX_IRQHandler(void)6 ~ Z! D( ^3 [$ C+ d
- #else% x+ K# \8 \$ i. T( k$ T, l {/ E
- void USB_HP_CAN1_TX_IRQHandler(void)5 v u9 ~7 @$ m) w
- #endif /* STM32F10X_CL */
- i% j# `* b# D# I4 u) ^1 @6 | - {
* J0 O) y( y( z) B& `: b5 y& [, | - if(CAN_GetITStatus(CAN1, CAN_IT_TME) != RESET)# Q! R6 P; T! M# |/ a* [
- {
4 f$ k; L) E5 p5 ^ - CAN_ClearITPendingBit(CAN1, CAN_IT_TME);
- o, E3 P9 [$ ~. f4 T6 _ - , i3 g& @4 x+ O2 M4 ], M/ \
- CanTxMsg canTxMsg = {0};
8 N+ b9 p3 u: s' E - uint8_t number = RingBuffer_Out(can1TxBuffer, &canTxMsg, sizeof(canTxMsg)) / sizeof(canTxMsg);
( }) ]0 u. J: V! y" M
0 v' I- T* E% z- if(number > 0)) M3 N" ^4 i, S
- {4 L4 I3 q/ X0 c
- CAN_Transmit(CAN1, &canTxMsg);7 @# Z7 N; u2 ]7 m' Y- s
- }
$ P1 {8 @5 h3 N$ v$ H( c - else. |9 G, [4 P* ^
- {
6 z! U& e% _- [ - can1TransmitFlag = false;
: M9 b9 X {$ H6 {5 h( w' X
2 u3 o$ }* x" x+ P3 g- if(can1TransmitFinishCallback != 0)- c2 S$ F% V8 D/ R1 w
- {
1 F3 [( E* I+ L - can1TransmitFinishCallback();: F) ^$ Y9 V2 w: O9 [1 J
- }) G# L4 G0 \* I9 ]* U. }3 E
- } `) Y/ X% [( I) V
- }5 T$ H7 x* ^$ W, ]' y' _8 G; p
- }
% q7 r- W8 g# }2 Y, k3 F9 a
2 i0 V" e* E: N6 C) }- /**1 |& c- [) |/ p& x- g- P" S
- * @brief This function handles CAN1 RX0 Handler.
9 d% ~, O1 Y+ J3 U/ Z/ p - * @param None. I6 n+ s% d; o7 X! K. i
- * @return None.
! z8 O g: J5 A: [ - */
& d% H) t5 n# V - #ifdef STM32F10X_CL# H8 \. K) f5 O9 P# H, Q
- void CAN1_RX0_IRQHandler(void)
, x3 D" K# ^3 P- _# N( ? x - #else
& {: q; X9 E8 n/ x5 Y9 m7 R - void USB_LP_CAN1_RX0_IRQHandler(void)
0 z, v) _' k: M7 g - #endif /* STM32F10X_CL */* _5 t: K8 w: E
- {
, x/ Z8 D. [8 ^8 [6 X0 Z - if(CAN_GetITStatus(CAN1, CAN_IT_FMP0) != RESET)4 @. [: s4 X* B9 a$ C6 r+ ?# _7 U
- {& k& }* g& S" d) H# @7 s
- CAN_ClearITPendingBit(CAN1, CAN_IT_FMP0);' v. t/ f! W' \( U1 b
- 6 `: I; _' I! S8 g! B8 V
- CanRxMsg canRxMsg = {0};
2 j, l0 ]' Q. g% N - CAN_Receive(CAN1, CAN_FIFO0, &canRxMsg);' I% B7 H, `' [5 A+ d4 I
: p5 F( }' f8 d- if(RingBuffer_Avail(can1RxBuffer) / sizeof(CanRxMsg) > 0)9 ]$ L, @" N9 T% E# L3 L
- {
' H. y2 B3 H% p# y4 R3 `& j - RingBuffer_In(can1RxBuffer, &canRxMsg, sizeof(canRxMsg));/ k( C9 F; q4 c
- }3 |7 Y; L/ c, X4 y
' Z [1 f( f8 Y- d( B6 I" [4 u- if(can1ReceiveFinishCallback != 0)
% {/ U6 L, D6 w) u - {
3 I, e4 | t6 k6 b; S - can1ReceiveFinishCallback();9 Z& I9 w0 r: A1 E6 \
- }- F) |* f$ J8 D7 |+ d
- }
9 V1 U4 n0 U- ~5 U+ w/ f" J - }
" i4 w' G: {. A% i - 9 Q& D2 [ Q1 Z3 ^$ ]0 h
- #ifdef STM32F10X_CL! a3 o; z/ }7 \+ f9 y
- /**) `4 a2 \9 ]) V$ P! h, v: n0 a3 u2 \
- * @brief This function handles CAN2 TX Handler.4 s, s2 ?' z) w
- * @param None.9 e6 ~) j. Y0 l6 d+ r
- * @return None.: A y4 E& ^1 O$ N& v
- */5 O. R: r( f; n# z
- void CAN2_TX_IRQHandler(void)
. n5 m* m% D# w/ F# N! Z - {2 A* \% g: [- A4 p6 N7 l
- if(CAN_GetITStatus(CAN2, CAN_IT_TME) != RESET)
7 p. W. ?3 ?2 G# e# M2 b - {
- T# U" w3 v% i( @* _. h& v - CAN_ClearITPendingBit(CAN2, CAN_IT_TME);
- C* h! P# o9 }) U# n) t- ~
( N2 r5 a6 E) N1 ~- CanTxMsg canTxMsg = {0};
/ Q5 U, {$ w0 @: X# k& _. U - uint8_t number = RingBuffer_Out(can2TxBuffer, &canTxMsg, sizeof(canTxMsg)) / sizeof(canTxMsg);; W& [# z0 B0 P" [$ `) }1 |
( F+ L* X7 H+ s' G# M- if(number > 0)9 |0 T/ D2 ~) K5 E0 _! B
- {
0 X- g; @3 O$ t% k - CAN_Transmit(CAN2, &canTxMsg);
9 ?" Q9 |0 q' b, Q - }$ t! s9 |; e a; E8 I/ p# _
- else6 F/ K% K5 }/ f9 S) X; p
- {
. x8 U9 j& a) w; v6 ` - can2TransmitFlag = false;1 s/ n& k# I) z' \; K
- 5 l. z h$ K: c9 v
- if(can2TransmitFinishCallback != 0)$ }' o) e$ y, q! e6 {
- {3 k) ~& f# S% H) }5 I# ?
- can2TransmitFinishCallback();+ d- d; v8 |# u. s; M; R1 w+ ~
- }
2 z- G J0 g: Z D - }
" I9 c) g( B. V: Z# }8 @ - }& [+ g6 h. S& o+ [1 g* F/ u$ U6 X
- }
$ E p2 @$ j- z, p; [/ }: O# i7 g: L6 E - ( C4 _7 m+ Z; K) O5 P
- /**
0 u+ D" g1 W9 ]) A - * @brief This function handles CAN2 RX0 Handler., J# g: f8 t* R; |# P. W1 q* u
- * @param None.
+ E: I5 z/ W/ F - * @return None.
0 v$ e9 t h+ S - */: i6 k5 _5 O1 a7 Z
- void CAN2_RX0_IRQHandler(void)
+ G5 ]* y% E+ M. |# n6 e$ g+ i - {8 v2 z$ i) Q. u1 A% B
- if(CAN_GetITStatus(CAN2, CAN_IT_FMP0) != RESET); T# s n+ V, O0 Y. W! m
- {
* S) O5 G' [7 q4 u - CAN_ClearITPendingBit(CAN2, CAN_IT_FMP0);, e. d1 M( Z: ?( e. |
- $ H. g6 y6 B D- P9 @
- CanRxMsg canRxMsg = {0};
. M" C# \2 v! p& z - CAN_Receive(CAN2, CAN_FIFO0, &canRxMsg);
: h, o- |; u, U
, x5 p# X' q2 n$ F5 Z) _$ n3 R- if(RingBuffer_Avail(can2RxBuffer) / sizeof(CanRxMsg) > 0)& P- {; W7 \8 C$ H, Q
- {
8 R" f% m6 k/ v# p9 k, A5 M - RingBuffer_In(can2RxBuffer, &canRxMsg, sizeof(canRxMsg));, [% Z4 U! m/ D' b! X+ K
- }
# O+ {1 @6 W) s8 _
2 y3 b) f" z' C* ?- if(can2ReceiveFinishCallback != 0)7 {) |3 `+ C8 b# }( Y; X! ~! V8 q2 l
- {
5 v& T9 ]: X+ z' x" d - can2ReceiveFinishCallback();1 E& d ~* D! M* v4 `, a2 J- w
- }; A. J, v$ Y& L+ a; o5 `
- }
/ s4 y3 t+ K$ D H+ l - }
% l: W, C$ Z6 X - #endif /* STM32F10X_CL */1 B; o; I6 X& c
复制代码
% y! R- s% J, w8 ^+ F9 t4 D1 j9 O! w3 |
main.h 文件
4 E7 p5 @6 f& n: k+ \- /**
7 b! U; R7 O$ ?# v- w( Y - ******************************************************************************, ^8 O4 Q' G0 b& C9 B
- * @file main.h
) I1 n+ V4 B' T# l - * @author XinLi( {4 M( t) w- O. O" ]
- * @version v1.0( B) y" I9 s) H+ W* s
- * @date 24-June-2018
# }. M' o4 v: ^6 ^ - * @brief Header file for main.c module.
( D! _0 \& ~ i+ `/ }3 H2 e1 `$ P - ******************************************************************************
: d* z- N! n7 l( f9 l* ^ - * @attention
/ Z3 {& c$ @0 f - *
: S2 r7 m9 b+ i+ a @6 r - * <h2><center>Copyright © 2018 XinLi</center></h2>
: W! K4 I1 n( c7 i1 ~ - *1 }/ m3 V. _) R/ W( X$ {
- * This program is free software: you can redistribute it and/or modify
, }! D$ A" B ` - * it under the terms of the GNU General Public License as published by! Q2 h# B R+ s4 f9 p
- * the Free Software Foundation, either version 3 of the License, or
) r/ Q- j# ]! K; Y# d+ ?) [ - * (at your option) any later version.
- P, R/ c9 n! |6 z& Q0 i - *: g) ]7 `* g( R/ G
- * This program is distributed in the hope that it will be useful,
4 L. l8 L6 ?3 h# a - * but WITHOUT ANY WARRANTY; without even the implied warranty of
& ^; ^6 k/ f$ E - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
. H% c0 }* E+ M1 ] w - * GNU General Public License for more details.) X/ r$ R, y6 ]% p2 L' G
- *
6 {; e& X; R0 v- H& p: `) M - * You should have received a copy of the GNU General Public License
& _# s5 t9 [8 D$ ~1 H" m - * along with this program. If not, see <<a href="https://www.gnu.org/licenses/>." target="_blank">https://www.gnu.org/licenses/>.</a>8 q( t* _1 H. M" c# Z
- *
& a1 E/ C1 n- z& b - ******************************************************************************, P' W% m& T6 y6 D
- */* k: d7 @% H: w) |" e# V0 x8 `6 c8 G6 C
- 1 M9 |6 F% E' D( u6 R% s- i
- #ifndef __MAIN_H
& t. j( F7 k4 L( z+ R; j8 ]4 @ - #define __MAIN_H N+ s3 s5 v+ o# u t& V) b/ Q6 n$ i/ r
- ( w# I5 c4 n, i5 {/ b! d
- #ifdef __cplusplus
' i9 l7 G1 y2 [ p - extern "C" {; }) R5 w! i4 }+ Z6 g
- #endif. i1 J3 T0 p' [# D
4 k0 ?1 H9 J- x* C- /* Header includes -----------------------------------------------------------*/3 E' ], B5 Q I1 o
- #include "stm32f10x.h"( e e# |6 n3 J" q( R" X4 i: l. V
- $ ] b# k1 L6 a& F( L& W
- /* Macro definitions ---------------------------------------------------------*/
. P' ^ G9 |" s: Z* o$ t - /* Type definitions ----------------------------------------------------------*/- [. ?9 r- @5 d" D1 X& E
- /* Variable declarations -----------------------------------------------------*// k" t& z" q) q8 e- K+ W* R p/ l
- /* Variable definitions ------------------------------------------------------*/
& z) }2 }, d! b7 m9 [& V2 l! G - /* Function declarations -----------------------------------------------------*/# a0 z1 n4 b9 E
- /* Function definitions ------------------------------------------------------*/
/ Y6 M- m/ n3 W& _* [- \- \
! J( u2 w3 d+ D& n( j- #ifdef __cplusplus( |5 O6 x: [) ^" c; C9 m
- }! J7 y E r+ J% F8 p8 U ~
- #endif
}- @) |: X! E7 C# m& h+ B, H - / `. t! m5 H8 n% }1 x7 j- Y0 m0 t! ^
- #endif /* __MAIN_H */
复制代码 # o# J+ j( e% {
9 O) B8 S' V) Qmain.c 文件
5 @! D- w! S2 z- /**
( f( J5 q1 X- H! a: E7 m, ~ - ******************************************************************************, {4 I3 ]0 l! u' o% _
- * @file main.c% m& `$ }7 X7 X! L
- * @author XinLi
' v) |& T' H; o7 H# u: y% \+ W - * @version v1.0- @4 ` Y) i) h- g0 c1 [
- * @date 24-June-2018
# G0 T. O' g+ \& f; a8 M0 u: G - * @brief Main program body./ Q( v/ H7 P! d \7 a, Z6 h
- ******************************************************************************
+ z/ H% }7 z2 O" E8 l8 o6 y - * @attention
; E4 f3 N, W) ]7 a% |8 ~2 {5 P - *
7 r% D( f9 ^$ C- q. M5 ^6 d - * <h2><center>Copyright © 2018 XinLi</center></h2>
; j& }6 Z( q# O3 ?/ M1 R) { - *9 W4 I2 t9 r- R" q0 x3 `! ?6 r
- * This program is free software: you can redistribute it and/or modify1 q4 n1 \) M6 w% N* ~
- * it under the terms of the GNU General Public License as published by. X* Q3 `* r# V9 M6 D1 M8 x
- * the Free Software Foundation, either version 3 of the License, or% A; Z: D: p+ E+ m% c
- * (at your option) any later version.
7 S9 S% k+ I* r) T- N - *
" n( B0 ]" o* Q2 u3 A$ a - * This program is distributed in the hope that it will be useful,
5 x$ v* b% A3 d8 u - * but WITHOUT ANY WARRANTY; without even the implied warranty of# f4 k0 Q) a8 T$ p$ s! O4 U
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
: F9 P; ^, l" \( S - * GNU General Public License for more details.
& b; f$ a: T5 K. I - *0 y: D+ d4 y9 ^6 r
- * You should have received a copy of the GNU General Public License5 d: |. T( q# Z6 ]7 Y. O$ h( @
- * along with this program. If not, see <<a href="https://www.gnu.org/licenses/>." target="_blank">https://www.gnu.org/licenses/>.</a>
. A: M) p! `# r - *5 s) ]1 {# S: s5 l: w* X
- ******************************************************************************
, f7 q9 \6 H) V) ]2 n( ~ - */
6 ~! p/ b& C0 e7 T u$ l
8 V0 E6 Y7 p9 p6 L% N/ ]- /* Header includes -----------------------------------------------------------*/" T+ N- ~: L' f# w# ~6 z! d
- #include "main.h"* X, {* B# o! C
- #include "CAN.h"3 p& }; a9 S z8 i% E9 ^, R
- ; q6 w# J2 Z) ? x; T/ o
- #ifdef _RTE_! z: q/ G* ^ p# m; h
- #include "RTE_Components.h"
; m- c2 V( B- J( n3 w/ T - #endif
4 L' }' o3 u- I2 _6 l - ) F$ n! g# D- v9 T0 p
- #ifdef RTE_CMSIS_RTOS2
0 S$ [' o6 w8 P/ C: p1 Q* o' ? - #include "cmsis_os2.h"5 u. I' r) ]3 _" ~1 S- r- O+ t
- #endif
4 O4 Q1 }% K {, j8 K - 1 t1 a7 g( `1 K7 B( Y
- /* Macro definitions ---------------------------------------------------------*/
7 P. i1 z7 G3 |6 J - /* Type definitions ----------------------------------------------------------*/. o$ S8 d5 }$ x( w
- /* Variable declarations -----------------------------------------------------*/
: I w3 S% C7 m; I4 G5 x - /* Variable definitions ------------------------------------------------------*/
8 m1 z7 X. k4 a* d# ~ X" j4 W - static CanTxMsg canTxMsg = {0};5 i, l( j/ C1 Q* W
- static CanRxMsg canRxMsg = {0};2 v$ t6 i2 X3 }
- / L9 N, J3 e$ b* N+ @( X# [
- /* Function declarations -----------------------------------------------------*/
( U. T& N$ ^% a+ {/ W( z/ Q - static void SystemClock_Config(void);
3 s( |4 M# Q2 x+ n6 g - ! W- @: r) l+ Z1 a# R: X
- /* Function definitions ------------------------------------------------------*/- E1 ?9 P: h3 i; N. r
- 5 O% D. q& I$ O
- /**
% n3 o0 H9 B5 @) S2 n3 p+ _+ J3 G - * @brief Main program.
* N+ Z% |+ g: {- K8 h) H8 c% G - * @param None." w z' j0 u6 F( P$ G: f
- * @return None.
: B9 E6 e5 u' b# [1 z - */
# T7 Y& z* u9 v4 B' ^% H - int main(void)
6 C, s+ O, T r - {
7 p$ Z& { k2 I' _4 X3 t9 u+ w - /* Configure the system clock to 72 MHz */0 O+ m0 Z$ ]& H" F5 m/ S3 D/ t S: Q
- SystemClock_Config();
% F$ w( P5 T8 R7 z5 R" z2 x - SystemCoreClockUpdate();8 L* d4 @4 P: S7 C- F
- ?. a# P5 V; n$ f9 t- /* Add your application code here */
b% v! S) n( h5 d5 [ - CAN_Configure(CAN1, CAN_WorkModeLoopBack, CAN_BaudRate250K, 0xAA55, 0x55AA);; h) i1 i9 X+ d' C0 J0 Y
1 p5 u* g# y; [0 J8 |- #ifdef RTE_CMSIS_RTOS2
9 v4 x# @' }' Y9 Y* f - /* Initialize CMSIS-RTOS2 */
/ T' h( f% v4 G0 I; p - osKernelInitialize();, U1 ] t W" T( b9 L: ~
- * i9 m! l! [2 F: S! ~3 J
- /* Create thread functions that start executing,
4 @# c1 Z+ Q W* x - Example: osThreadNew(app_main, NULL, NULL); */
; m5 N' L0 O/ f6 i) `( S6 C1 V - ' q- N5 r& { u& f2 e# @. A
- /* Start thread execution */
: G3 r4 D) e3 ]3 w2 p9 W! u - osKernelStart();8 j6 r" T f1 G, t7 N" E
- #endif
s& K9 H s$ T* V4 ^ - 7 A- x5 w! y4 e
- /* Infinite loop */1 _ q# R' w$ e+ }6 g+ |
- while(1)6 @1 F5 l ^: A. C! U$ D0 z p
- {* e& t) g9 M6 W+ S) G
- canTxMsg.StdId = 0xAA55;7 p J! h4 ?% X2 |
- canTxMsg.ExtId = 0x55AA;
2 p+ n0 G( \5 U) L+ W0 [ - canTxMsg.IDE = CAN_ID_STD;
& N0 j1 d- f( j9 e5 b" u5 C$ f" q - canTxMsg.RTR = CAN_RTR_DATA;2 m$ j4 O) |6 [' e9 D
- canTxMsg.DLC = 8;! q+ u& m2 \3 t+ S
- + z6 `7 u; e& ?' O+ T5 |. z1 k
- canTxMsg.Data[0]++;6 Z1 I, ]0 s3 _& b
- canTxMsg.Data[1]++;) t, y S7 M, b% s0 J1 q0 g
- canTxMsg.Data[2]++;( n* ^8 w7 S8 c- V X
- canTxMsg.Data[3]++;
9 b1 e$ E2 T" w0 I - canTxMsg.Data[4]++;! X1 G- d# Q, m# Q% R7 L# w+ p
- canTxMsg.Data[5]++;3 T5 T# T* t5 C; L {
- canTxMsg.Data[6]++;
+ v" a' S; ]8 R6 r% L( h - canTxMsg.Data[7]++;
2 H- n4 c2 S4 Y c: f6 D - / d' v- _! N# I9 Q3 w/ @ i! z+ j
- CAN_SetTransmitMessage(CAN1, &canTxMsg, 1);
/ Z. R7 C; L# b3 ~2 J3 R - : V1 {* }( L7 f2 m( E2 v
- while(CAN_IsReceiveBufferEmpty(CAN1) == true);) W' a6 X$ A! I, u
1 z" f$ S: Z7 h8 w7 t0 k- M- CAN_GetReceiveMessage(CAN1, &canRxMsg, 1);) f+ ?$ L2 S% S0 E3 j
- }: l5 I" |. G$ A! u6 A' b9 f
- }
1 m8 P7 X( t: h- Y. ] - 9 H# V8 T, G9 H7 r" u# s$ l; |
- /**; p' K* P6 x/ s
- * @brief System Clock Configuration.: q: b5 e' _$ n5 x: @2 `, ?- P
- * The system Clock is configured as follow : - j9 D" _7 J1 [
- * System Clock source = PLL (HSE)
) r. p! S, b! Z9 t3 \# Q - * SYSCLK(Hz) = 72000000
" y( O- D# e" s/ G# W0 f- ] - * HCLK(Hz) = 72000000- m; ?$ a( r1 e- j# y5 \0 c
- * AHB Prescaler = 1
7 R. s. G4 @8 j/ L7 C - * APB1 Prescaler = 28 r* G5 o4 u. s' q
- * APB2 Prescaler = 1 r" _ E0 y- G+ y
- * HSE Frequency(Hz) = 8000000
+ O7 }! `1 T. A( A - * HSE PREDIV1 = 11 z7 Z E" R) h/ D+ A& J8 }
- * PLLMUL = 9
1 v% f8 ?: X$ v7 j0 h7 T3 q - * Flash Latency(WS) = 21 Z+ T8 _/ A% z; e
- * @param None.
" |/ E7 M/ n4 k# Y - * @return None.
7 _& e! [* u# e - */: ]$ D4 p" k4 o8 n9 [: P
- static void SystemClock_Config(void)
' E C' D9 {, N1 L - {8 Y; w. M( _6 W* U! a
- /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration */
6 g2 A6 Q0 Y# p - /* RCC system reset */) G9 q2 m3 Q2 _$ G" Q" T
- RCC_DeInit();4 q8 ~! w- D; K
0 B; ~2 w: Q+ ^- /* Enable HSE */8 I$ ]5 i: }* ^! K- R- J4 W
- RCC_HSEConfig(RCC_HSE_ON);# U" u1 Z, _# I3 q7 O$ @. \
- 3 E8 w* Q# o# a3 u) Y
- /* Wait till HSE is ready */
. K: M2 \# _4 E( k& {% a - ErrorStatus HSEStartUpStatus = RCC_WaitForHSEStartUp();
0 c( S& k, J0 w5 y7 ~4 `4 k
! U" W0 O/ R: ]9 y: `- if(HSEStartUpStatus == SUCCESS)
7 f" X9 R: a* J) A8 [ E - {
( U, B4 u1 p s6 a; M+ x3 j - /* Enable Prefetch Buffer */& N* q ]0 L4 u. V. |2 }/ s R
- FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
' u* G6 E% i3 ^) x2 s: x
1 U4 `* z/ q% }, [* M- /* Flash 2 wait state */5 y3 S, p* k$ W1 c" v8 _
- FLASH_SetLatency(FLASH_Latency_2);5 z2 v/ ?( t# e! I
- ( F7 r; v) B: ?: C5 n8 x3 o; O
- /* HCLK = SYSCLK */
9 s! L# l# U( P; i - RCC_HCLKConfig(RCC_SYSCLK_Div1);
& F0 ]2 D8 }2 U2 I) X3 E2 i - ) D9 `; S& p0 |$ w& T
- /* PCLK2 = HCLK */( U- _2 V. O: u5 V( e
- RCC_PCLK2Config(RCC_HCLK_Div1); 0 k9 W! ]' g( K. ^
- \) P$ M/ `8 d( }! C
- /* PCLK1 = HCLK / 2 */4 }- d# |+ G* {) _- V0 P4 ^: X6 K( ^
- RCC_PCLK1Config(RCC_HCLK_Div2);- { V3 j2 ~! H8 F ~4 i& p4 `
+ u. w* s; ^: u' a* |- /* Configure PLLs */& O2 J w* e5 b$ P+ ~& q
- #ifdef STM32F10X_CL
' I" `. y& L; k9 l0 `4 } - /* PLL2 configuration: PLL2CLK = (HSE(8MHz) / 2) * 10 = 40MHz */
' E; k4 \) S0 X$ X* F ]9 ]$ H - RCC_PREDIV2Config(RCC_PREDIV2_Div2);- Q, n0 O5 J0 a" z
- RCC_PLL2Config(RCC_PLL2Mul_10);
5 t. H3 r) O0 k# ^/ ~, i& f - 6 H6 D7 V1 F. V$ A8 m
- /* Enable PLL2 */
/ b) E, f8 y; Q c: g/ t3 e! o - RCC_PLL2Cmd(ENABLE);
S, ~9 i0 w* g2 c% O) L1 ^8 W1 G( O - , r5 _9 m/ o1 j, i; a
- /* Wait till PLL2 is ready */
' X+ U+ j& ?6 j5 O; } - while(RCC_GetFlagStatus(RCC_FLAG_PLL2RDY) == RESET);% h$ A) `! j: h& h9 C/ G2 f) m' P
1 ]) j& @6 t _. }# j- /* PLL configuration: PLLCLK = (PLL2(40MHz) / 5) * 9 = 72MHz */
0 E! V4 x4 v9 D' k - RCC_PREDIV1Config(RCC_PREDIV1_Source_PLL2, RCC_PREDIV1_Div5);
$ f" f, N T4 }1 z - RCC_PLLConfig(RCC_PLLSource_PREDIV1, RCC_PLLMul_9);
9 ~ i7 o: w1 e. r5 {6 t, I6 p2 z - #else! L( h) s' T! y0 p- \
- /* PLLCLK = HSE(8MHz) * 9 = 72MHz */! v2 K. {- p o# n+ J- P
- RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
. h u" |' t+ ~! l) H - #endif) ^. e, G8 y! _# v0 u" b) y+ e: X
- 8 P* @3 g' a: h2 n: |/ z1 B
- /* Enable PLL */# g8 Z( J" K" Z
- RCC_PLLCmd(ENABLE);
( p: y7 g, H3 F; V
4 I; I W6 j; q# e* |. N- /* Wait till PLL is ready */
% T' h6 C- J( N! Y$ T/ z1 x6 \ - while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
& @7 T: e6 j2 C1 @7 c3 u
8 X4 d% d) b' i" D: R! L- /* Select PLL as system clock source */6 U0 l _& w8 r& r- f2 N' Q" p
- RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
9 {5 p% b* J1 _5 u9 \
, q9 l% r, E4 `, F( d- /* Wait till PLL is used as system clock source */0 H. S# M# r7 \6 O" G& r8 z, ]
- while(RCC_GetSYSCLKSource() != 0x08);
7 E& M2 e* {0 t- o - }
( x3 p1 P/ O4 P( E - else4 K. _" v k9 }
- {
. b5 Y. }7 o$ E - /* Disable HSE */
% W% p( B7 n0 [6 t/ R. k - RCC_HSEConfig(RCC_HSE_OFF);
* y7 e; \9 m/ D+ b, [
( `. N7 N# p L+ s. N- /* Enable HSI */' Q1 ^5 |2 e6 J. l
- RCC_HSICmd(ENABLE);
: m' [% b3 K; N8 ?, d - 5 M1 p! ^2 M! _. s! T5 Y( j: ^. G; ^
- /* Enable Prefetch Buffer */
, I1 M' `! g% ^* v$ m/ d' {3 ~ C - FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
5 }) ]5 _/ Z/ C7 g7 {- @2 c, @ - 7 y9 \% L0 Z' Q) g
- /* Flash 1 wait state */) J+ t! s- a2 k0 I
- FLASH_SetLatency(FLASH_Latency_1);9 E9 R$ }1 F- Q( q7 m8 f; `
- ; I) Y6 o7 ^1 j
- /* HCLK = SYSCLK */5 c0 Z+ h9 j; d: g( g9 w+ [6 u
- RCC_HCLKConfig(RCC_SYSCLK_Div1);
2 c7 @; k _1 P" B. W$ } - 9 o- q" V! X& K- i4 l! B4 y
- /* PCLK2 = HCLK */2 ^( s0 G% k, {
- RCC_PCLK2Config(RCC_HCLK_Div1); 2 i) A: V: i& J" Q, |+ A
- ! a E% u9 j' @$ r" B
- /* PCLK1 = HCLK */
/ u L A: a4 }/ D8 s; f* _2 f - RCC_PCLK1Config(RCC_HCLK_Div1);
3 u7 o9 ~+ C* X* D6 v! h
; j" q4 G& |# J; z2 g9 _- @; X1 _7 C- /* Configure PLLs */: p& k6 v# G9 {
- /* PLLCLK = HSI(8MHz) / 2 * 9 = 36MHz */
8 H) [! @* t- v0 |% m8 m - RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_PLLMul_9);
3 U0 J; B$ N2 J - / Y: a y3 r6 x6 w# R& \
- /* Enable PLL */
2 c" z0 | U" l8 R# U - RCC_PLLCmd(ENABLE);8 x7 G$ u; e8 r& w: g$ d% c! G/ h
- 2 p+ a2 l7 i6 i1 h: H
- /* Wait till PLL is ready */" K) }+ s" c6 O/ A
- while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
/ b* z- ]- K m; t# Y
1 K- S9 D# g0 H( |8 n' V) c- /* Select PLL as system clock source */; ~/ u" k1 t# Z+ r# [
- RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);. K T. ^8 E! A) m: i
- 0 [/ l/ j, o/ _
- /* Wait till PLL is used as system clock source */
5 f: O2 z$ d7 r6 ^% p - while(RCC_GetSYSCLKSource() != 0x08);9 e, a9 w. M; L6 `1 s" |6 y
- }0 @' q2 t% X$ o+ R, M
- }
& R. w& v, ^7 L$ E- | - # T, R3 @1 h+ L1 L/ o8 m
- #ifdef USE_FULL_ASSERT
3 |. H/ c% D* y+ J6 P) I - /**/ F: v) A; A5 x% g7 k: Z" E
- * @brief Reports the name of the source file and the source line number' ~1 U$ Q- M1 N0 {5 q' n
- * where the assert_param error has occurred.
( i% n, U m+ l+ S, _* Q/ g- v - * @param file: pointer to the source file name./ s/ ]6 s8 t$ B/ q, W
- * @param line: assert_param error line source number.
9 J1 {% o9 p9 w7 x+ l4 t1 i - * @return None.( `1 J, H! A+ r& ^% e( w
- */ N4 [) P4 a- c3 S
- void assert_failed(uint8_t *file, uint32_t line)2 D5 E7 L1 ?3 V4 p% k/ u5 `1 C
- {
5 ~ ?' O: [( n' Q - /* User can add his own implementation to report the file name and line number,; w* o% Z9 C) G& I) b$ Q; f; v2 V
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */" R1 W0 Z% l$ u9 b9 Z6 s$ Y6 F) v
- + b, y. h9 @4 O9 s
- /* Infinite loop */
5 W3 j; P) J7 j; n, `' m - while(1)
/ U# |! H1 ]/ I4 R- b. R1 C* { - {. E: K \& ]* Z" [5 X, x
- }
+ G9 z7 g4 u6 J0 [+ R6 z. u. l - }
1 ^) B7 f2 h% z" D - #endif
复制代码
) i, S0 R7 g/ }, ~4 x0 ^3,注意& A: M8 H; n8 ]) J% G
. I/ Q# Q5 s# f+ h0 B
CAN 消息发送缓冲区和接收缓冲区的大小,可以根据应用的需求进行修改,缓冲区使用的是堆内存,需要根据缓冲区大小和应用程序中堆内存使用情况进行配置。' v! c0 l1 J' S5 n$ V( ]# S
; I' S1 R, ^7 Q% b! H0 Z! O. x
! ?/ d3 B1 ~/ Y( a, ]
/ {& y! @5 Z, ?7 v# K7 T5 y |