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