你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【经验分享】STM32F1(CAN)

[复制链接]
STMCU小助手 发布时间:2021-11-26 17:00
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
  1. /**
    6 @/ x: c" q3 A
  2.   ******************************************************************************
    2 Q* ^: E2 f+ o: d( x
  3.   * @file    RingBuffer.h4 ]  V9 j8 H$ m+ g
  4.   * @author  XinLi
    ; ~# k# R3 M9 W! T5 C6 m
  5.   * @version v1.18 d" s! H+ ^' i! B9 k: D$ G
  6.   * @date    15-January-2018
    + r' ?# p$ E+ m2 q1 W+ q8 D; \
  7.   * @brief   Header file for RingBuffer.c module.9 p/ [9 f* Q& D9 X1 g+ T, l
  8.   ******************************************************************************
    3 L( `4 Y$ }' s8 B$ e& Z0 Q
  9.   * @attention
    * @$ ^0 p6 s9 a& z  `" w/ A3 F
  10.   *! w3 D9 z& R- p4 c, N+ n# ]
  11.   * <h2><center>Copyright © 2018 XinLi</center></h2>
    - q" J+ F. c' T( L' T
  12.   *
    % ?% [! Y* k7 m6 ?) @, v! V- g
  13.   * This program is free software: you can redistribute it and/or modify
    : ?2 N; l! p* P& D1 X
  14.   * it under the terms of the GNU General Public License as published by
    1 N1 l7 K- X. ~$ y; ?% a+ q
  15.   * the Free Software Foundation, either version 3 of the License, or
    , B/ u3 T; \! u. h- g# F% w  U
  16.   * (at your option) any later version.- M0 Q# L0 ]+ W
  17.   *
    ! h1 m8 [* b4 z: V% D2 d& ^
  18.   * This program is distributed in the hope that it will be useful,
    + |# ]; ]  J5 k
  19.   * but WITHOUT ANY WARRANTY; without even the implied warranty of7 {. E! a* ]: }' d
  20.   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the5 p1 g5 H8 W, A( f% u
  21.   * GNU General Public License for more details.
    7 B+ z; U0 J+ Q$ p% |" \% j
  22.   *7 K0 u4 Z4 S8 k4 y8 ]: _
  23.   * You should have received a copy of the GNU General Public License. L0 p5 w- x& y' h
  24.   * 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
  25.   *
    9 {7 @& V! w' M2 [5 Z$ g
  26.   ******************************************************************************
    7 W! \" w0 l( i
  27.   */
    % k5 O9 W) t4 q+ z5 J9 J
  28. : n5 ]7 ^' F' a8 l2 v
  29. #ifndef __RINGBUFFER_H
    - T6 x$ \2 w/ V) S9 Z# \4 W, R
  30. #define __RINGBUFFER_H# j' G! g# i, ~! }5 |! E

  31. 9 d; @' \  m7 a" }
  32. #ifdef __cplusplus
    * Y2 i6 F" H: a2 M
  33. extern "C" {* W5 u6 R+ ~' a( a3 E' g0 n
  34. #endif
    8 O4 l( |5 o0 o$ Q

  35. " M# u3 w/ G9 a* H
  36. /* Header includes -----------------------------------------------------------*/: k8 N7 a( p  k# B2 ]
  37. #include <stdint.h>
    / ^; F/ Z! F: j5 o+ _  Q
  38. #include <stdbool.h>
    $ g; s/ f" x9 G2 y
  39. #include <stdlib.h>
    0 c. n; H# N: U# x

  40. : h  R3 T6 h4 f; K
  41. /* Macro definitions ---------------------------------------------------------*/1 M7 \/ ]* n3 J! E1 H* u
  42. #define RING_BUFFER_MALLOC(size)  malloc(size); L4 `( F; ^! V
  43. #define RING_BUFFER_FREE(block)   free(block)/ B; s9 |* H/ n% g

  44. 2 O  p8 [" C! @) l
  45. /* Type definitions ----------------------------------------------------------*/
    ( j3 H, y  i: W' {: I5 H) @
  46. typedef struct( l6 E7 f  F% E' |* P4 Z$ ~
  47. {4 c$ w, [) Z( w# _6 h1 W
  48.   uint8_t *buffer;; a- a0 R) z; s9 u4 x0 t3 E
  49.   uint32_t size;
    , p$ X, S- P  |! j: i; X1 J
  50.   uint32_t in;
    7 c- o8 X2 ~) o( {, @8 s! Y9 J
  51.   uint32_t out;
    6 I) E# i, `+ }/ o; C1 M0 G. R7 G
  52. }RingBuffer;
    ; D( F- R2 i+ h, l. b# a- x1 U

  53. ; D  X# q/ r  N: ~  @& f
  54. /* Variable declarations -----------------------------------------------------*/  h8 f6 J# q9 r3 [/ w8 f/ ]
  55. /* Variable definitions ------------------------------------------------------*/
    . [( |  |9 M6 N3 C; @! r
  56. /* Function declarations -----------------------------------------------------*/
    : K& b$ l/ r2 t% O# W* o1 r
  57. RingBuffer *RingBuffer_Malloc(uint32_t size);2 h" T6 ~$ q0 w. J
  58. void RingBuffer_Free(RingBuffer *fifo);% r; B- N1 I# k. f" F0 E

  59. " A3 I  l' h9 |6 S$ a4 v5 Y
  60. uint32_t RingBuffer_In(RingBuffer *fifo, void *in, uint32_t len);/ u! c1 p5 F2 x, Z  I' J( b
  61. uint32_t RingBuffer_Out(RingBuffer *fifo, void *out, uint32_t len);" @+ O' O$ e0 ]/ w/ C) W

  62. 1 I* P$ i/ S+ v. }; B3 h, ~
  63. /* Function definitions ------------------------------------------------------*/# f' d' q5 G3 [( u6 _5 [

  64. & B4 ]7 y5 q1 [: V8 K8 r- T2 _: D
  65. /**
    0 u! K6 D, g. n" P* Q
  66.   * @brief  Removes the entire FIFO contents.
    + S; t! I. k; a- K- P2 V  @4 Q
  67.   * @param  [in] fifo: The fifo to be emptied.
    7 U' v0 R' R3 M4 ]; K/ ~
  68.   * @return None.
    3 i" b% j* M/ H2 B
  69.   */1 ]6 X' h+ `; ?8 P- t! C! i
  70. static inline void RingBuffer_Reset(RingBuffer *fifo)
    5 j8 U) v6 j4 ]0 r1 R
  71. {
    9 g2 |" k: \3 a. }+ q3 ~, @$ K! x! ?
  72.   fifo->in = fifo->out = 0;0 B' O# r  ?3 _
  73. }
    4 r9 Q' o  x0 f; N$ Z- F$ w
  74. ( p# c. H! a7 h
  75. /**
    # d* Y) p, u) s; T9 I8 X7 w: U
  76.   * @brief  Returns the size of the FIFO in bytes.
    % |' o( w- K0 b$ F& e4 ^
  77.   * @param  [in] fifo: The fifo to be used.
    2 P$ [5 |9 K1 R2 i/ c" q7 t0 ~
  78.   * @return The size of the FIFO.. h! w( |$ a1 ]& ]7 s) S* C
  79.   */
    % k' X9 r8 Q; I3 x4 X
  80. static inline uint32_t RingBuffer_Size(RingBuffer *fifo)) K4 U* g2 t# J7 H7 ^' x+ n# o$ n
  81. {- q  ^5 U8 k3 u$ ]- [
  82.   return fifo->size;4 X( }( r" p! C
  83. }! `2 l+ J( t' Y1 U8 G5 m

  84. ' G' i3 D% a0 P- `* v8 f+ M
  85. /**
    + }$ Y# {9 f! q* y) e
  86.   * @brief  Returns the number of used bytes in the FIFO.
    + s9 q4 ~' M6 V) j3 C8 _
  87.   * @param  [in] fifo: The fifo to be used.+ D. p8 O( b  Z  Z; W. E
  88.   * @return The number of used bytes.4 H; k/ k% U% q8 `0 O- l2 @
  89.   */
    7 J/ \6 `: u; n2 U) d( ?. F
  90. static inline uint32_t RingBuffer_Len(RingBuffer *fifo)
    + ^9 v7 R% }4 I0 I  b
  91. {
    1 o2 I# Z# A7 V/ n; `- j
  92.   return fifo->in - fifo->out;9 D; x, _% C2 r4 F; \
  93. }' p; D  z6 h3 i1 o* P
  94.   p- I! b: o+ c5 I4 G0 Q  L' o
  95. /**
    ( C* n/ Q1 ^4 M1 k. h: ^% G2 l1 m
  96.   * @brief  Returns the number of bytes available in the FIFO.
    # E6 c  F! @- {  o& D
  97.   * @param  [in] fifo: The fifo to be used.& Z+ e# ^. E; @
  98.   * @return The number of bytes available.0 q* L6 v' z: j
  99.   *// _8 F8 K$ ]! n1 y
  100. static inline uint32_t RingBuffer_Avail(RingBuffer *fifo)
    5 |0 X+ x8 v' Q# n; L7 K5 f4 F& Z
  101. {
    ) B  m6 X3 a8 `8 E! U
  102.   return RingBuffer_Size(fifo) - RingBuffer_Len(fifo);  D) D- X- _) O! }7 x; S6 U( O
  103. }2 s/ t) }; J* I2 B

  104. 7 y2 N  {% ~6 \. f  z9 N
  105. /**: `/ a7 t9 j+ |
  106.   * @brief  Is the FIFO empty?8 T7 j3 Z1 r5 `6 I; i
  107.   * @param  [in] fifo: The fifo to be used.) A7 b4 t7 ]) g: h* I4 _+ y
  108.   * @retval true:      Yes.' x2 q; M9 ?3 E) @, w$ V8 T
  109.   * @retval false:     No.
    ; k& C! u1 i! X& I
  110.   */
    & y0 \' h" L/ J7 H0 G
  111. static inline bool RingBuffer_IsEmpty(RingBuffer *fifo)! a3 B  ], Z9 Z
  112. {% Q2 `7 |& B( V7 r' b4 C& O
  113.   return RingBuffer_Len(fifo) == 0;
    / E: x- W4 l+ C1 b& ?' r2 g+ @
  114. }
    1 I; ~3 ~6 h; h6 h8 o% w
  115. , T+ F7 O! x4 R$ @9 X1 f0 o
  116. /**" }6 d$ l: ~* j) @
  117.   * @brief  Is the FIFO full?
    - G# j8 S  {2 w! c
  118.   * @param  [in] fifo: The fifo to be used.- b' ?: M' \- k2 s. B# r. S
  119.   * @retval true:      Yes.
    , h$ q3 G7 A0 ]; R+ m3 q
  120.   * @retval false:     No." D+ I7 r# f* Q6 }) T6 f3 P+ e$ L: b
  121.   */
    ! k+ A7 H- m, i4 g
  122. static inline bool RingBuffer_IsFull(RingBuffer *fifo)* k; T- ~, i+ M) ~: y7 z! z3 L0 T2 F
  123. {: S$ B; P5 H' n5 q
  124.   return RingBuffer_Avail(fifo) == 0;5 A8 k2 w* r% Q% o9 E) n" j
  125. }
    ( ]5 t4 N* Y+ @0 n1 R& G: t5 a
  126. : S. p' V) K3 P
  127. #ifdef __cplusplus+ D+ h2 E2 I8 P4 l+ H
  128. }/ h4 j' h; J* r. K/ ~! \6 x
  129. #endif1 c) l* B9 ^/ ~
  130. ) D0 |9 U' @, I
  131. #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
  1. <div>/**8 A8 ^. F8 A% X& d7 K8 s  n5 ^
  2.   ******************************************************************************
    ) S7 L  T, F5 \' a
  3.   * @file    RingBuffer.c/ Q4 M8 @% ?" \% B8 i
  4.   * @author  XinLi0 t. K5 z5 A$ m6 _1 T
  5.   * @version v1.1! p) Z2 e$ N2 X$ i2 i6 ^3 g$ W
  6.   * @date    15-January-20189 k% D  |# _: X1 Y' F2 t7 S
  7.   * @brief   Ring buffer module source file.7 k( ~3 J* i) h9 W4 w
  8.   ******************************************************************************3 x& O$ a: r* M/ ~% v* _7 o
  9.   * @attention) f# v. X! E! N3 G" O2 Z5 q9 o
  10.   *
    5 t) ]& h  }3 |- x2 p
  11.   * <h2><center>Copyright © 2018 XinLi</center></h2>
    3 x5 r- m0 F% Y- L& g
  12.   *6 a) E/ W" o+ q7 ?6 W( z; P
  13.   * This program is free software: you can redistribute it and/or modify2 @5 l# }5 w3 _! h: r
  14.   * it under the terms of the GNU General Public License as published by
    ; J* S# _+ B# `5 l
  15.   * the Free Software Foundation, either version 3 of the License, or# G. j/ H! K5 U
  16.   * (at your option) any later version.3 @( s# A* Y" S4 D8 a  \  g* G8 P& v
  17.   *
    ! X& \2 ]5 s, V
  18.   * This program is distributed in the hope that it will be useful,
    0 \, x3 V* |" O
  19.   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    / C$ H- W) }! u
  20.   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the7 G* `9 F4 m, _* n( d7 h
  21.   * GNU General Public License for more details.; b0 [# I0 i8 b5 N; d3 J
  22.   *
    0 \& J' l7 d, H# |  y" M; g, [
  23.   * You should have received a copy of the GNU General Public License
    ) n$ o: W/ @4 Y$ g5 n
  24.   * 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 ^
  25.   *$ e1 G! ^" U5 N; U9 K3 R, N" I. @
  26.   ******************************************************************************
    ' E9 F; g/ ^- V5 G0 f+ |$ _
  27.   */" [- l5 {3 A6 G
  28. 2 q8 m8 Z* N$ U) ^
  29. /* Header includes -----------------------------------------------------------*/  ]6 y' v- x$ w
  30. #include "RingBuffer.h"
    : ?" P' D0 m$ T3 J1 b* T! ]0 J( P% N
  31. #include <string.h>
    1 z% a* W0 R, G: `2 U
  32. & c4 L/ k; w$ Q3 `+ q
  33. /* Macro definitions ---------------------------------------------------------*/
    0 u9 w& A1 c- N9 \4 G; o8 Y9 \* j
  34. #define min(a, b)  (((a) < (b)) ? (a) : (b))
    3 j* Y& S3 q' N9 n/ [

  35. ; f6 L, t& e. C: H
  36. /* Type definitions ----------------------------------------------------------*/
    4 |4 h& R; _- Y( C' H
  37. /* Variable declarations -----------------------------------------------------*/
    3 ]6 \: G! R: q" C7 s
  38. /* Variable definitions ------------------------------------------------------*/
    ! C' E- {0 x+ w" |6 H4 E
  39. /* Function declarations -----------------------------------------------------*/
    ; t8 V% q6 e9 y: O/ ~
  40. static bool is_power_of_2(uint32_t x);
    # X+ S' C- ^! D- V
  41. static uint32_t roundup_pow_of_two(uint32_t x);
    ! |% \3 G4 X1 C+ I* Y% A
  42. 9 }3 U  }# n, x; g" M
  43. /* Function definitions ------------------------------------------------------*/+ ]' X7 U: M6 A" A- p$ L4 M
  44. 1 k) F" [) R  ?1 c: S
  45. /**! }$ C# E% i. {- p3 [9 E4 r9 F
  46.   * @brief  Allocates a new FIFO and its internal buffer.$ v- G& m" k1 C1 O/ H( m0 C
  47.   * @param  [in] size: The size of the internal buffer to be allocated.% `! m+ ^  j5 E
  48.   * @note   The size will be rounded-up to a power of 2.
    ! p4 F  A3 q& j$ [$ p3 {: I6 d
  49.   * @return RingBuffer pointer.% e5 @8 h! e4 d% F" l6 [
  50.   */
    3 h! L4 q7 F8 R1 c* ^' G
  51. RingBuffer *RingBuffer_Malloc(uint32_t size)
    " E& X, R1 O& F7 B2 P2 U; M
  52. {
    ) _- q+ F; N6 P8 w- s$ L
  53.   RingBuffer *fifo = RING_BUFFER_MALLOC(sizeof(RingBuffer));
      h  b7 A7 q" ?! M( s; l

  54. ' L2 \6 Z0 k7 V& C3 j1 e* ^
  55.   if(fifo != NULL)
    $ y+ g9 u/ H% w
  56.   {
    ! W4 w$ n! T5 o1 k! ]( U' v) G
  57.     if(is_power_of_2(size) != true)
    * I9 a; ^. @% M
  58.     {2 \( L9 P2 I4 W6 w1 e! C: [
  59.       if(size > 0x80000000UL)
    0 a& ~2 L  D% P- `$ Y4 C3 h+ a
  60.       {* k1 ^( s, t+ r
  61.         RING_BUFFER_FREE(fifo);& r' L7 S$ X) ]; y) d) F% D
  62.         return NULL;
    ( _" _& B5 X1 H3 U! M0 Q# ^
  63.       }
    0 a$ e/ t; p3 f! q% g2 W! ]" P

  64. 4 P1 H8 o+ W. w( a6 S
  65.       size = roundup_pow_of_two(size);
    & J, Y/ j. L# k' w& K0 z! i( l
  66.     }; R5 J3 R1 E* o* t; O4 F

  67. ' V) I- ^- F! r5 I) W0 J) Q
  68.     fifo->buffer = RING_BUFFER_MALLOC(size);- E. n: ]5 }  k' U- M
  69. ! G5 C. f) _& o) b+ h8 w
  70.     if(fifo->buffer == NULL)
    8 {8 t6 ^' J4 n  r7 k
  71.     {
    9 l$ G- K3 Q& r2 U+ ?9 n( Q
  72.       RING_BUFFER_FREE(fifo);
    % c5 ^; e. z4 }* u
  73.       return NULL;8 |& J- [; @+ S- x) Q5 f
  74.     }7 o1 S2 Y1 e$ ?2 z8 l* c
  75. $ N3 O. n/ n6 u8 z. Q
  76.     fifo->size = size;( g. W. G! E" y# L6 k' t% k
  77.     fifo->in = fifo->out = 0;: ?' I2 h$ M5 v
  78.   }
      k: o9 A7 ]2 a/ M( z5 q+ x! k4 ?
  79. 7 @+ V9 s$ q5 b4 R
  80.   return fifo;% d8 z0 f$ N' l( ?4 }: Y
  81. }
    4 e- D" [; y) ^9 @9 K& v
  82. 9 P! R  n' L3 @6 Q+ G2 @6 K/ a
  83. /**; O3 z! m. R/ I# A, Z( [# B2 w5 V! S
  84.   * @brief  Frees the FIFO.- u8 ]0 H* @8 ~& c, T8 `
  85.   * @param  [in] fifo: The fifo to be freed./ b/ S5 a( u& H5 |' ~
  86.   * @return None.
    * w$ J6 v% g3 O% _* f3 |. F
  87.   */8 Q: H/ T+ j/ H% e+ ~
  88. void RingBuffer_Free(RingBuffer *fifo)5 O% a  y0 d# G0 q7 j
  89. {. _% ^- l# D. s" ?9 L
  90.   RING_BUFFER_FREE(fifo->buffer);0 r* [6 A# x! T+ ?  ~3 X- L
  91.   RING_BUFFER_FREE(fifo);
    ) h4 x& B% \& v7 \: w% R5 H% ~
  92. }
    ( \) g. P. ]2 u& a8 @
  93. $ e# E) j+ s' {1 B+ q
  94. /**
    " C; `! s, l3 d
  95.   * @brief  Puts some data into the FIFO.1 A- a. A& B1 u$ ~, H& g% g8 y2 l
  96.   * @param  [in] fifo: The fifo to be used.
    % w; z6 p# B# p0 ]* u
  97.   * @param  [in] in:   The data to be added.% Z; K  A3 k4 q+ {! K: o, B/ M; a4 a
  98.   * @param  [in] len:  The length of the data to be added.
      q5 l* I# I# s. S1 |& L) n
  99.   * @return The number of bytes copied.
    , A( M2 D  b+ W
  100.   * @note   This function copies at most @len bytes from the @in into
    * f9 X1 g0 \  ^% m( p+ T5 R
  101.   *         the FIFO depending on the free space, and returns the number
    6 R5 s0 N: x4 Z7 F- i9 M# b
  102.   *         of bytes copied.* S( c9 Q! V7 N9 u( n9 a8 s
  103.   */
    ' M4 D% [) V9 Y  \4 q
  104. uint32_t RingBuffer_In(RingBuffer *fifo, void *in, uint32_t len)1 z7 b  X( F( ^) p1 a+ C- v
  105. {
    / n3 w0 a8 o4 z6 b0 W
  106.   len = min(len, RingBuffer_Avail(fifo));
    4 k1 K: o: E  ]' }' _* D" y

  107. " _  z# h% u% L! \
  108.   /* First put the data starting from fifo->in to buffer end. */; m  K; e5 d/ @2 V* U. N( }! U
  109.   uint32_t l = min(len, fifo->size - (fifo->in & (fifo->size - 1)));
    9 _4 s4 K3 y" p' z
  110.   memcpy(fifo->buffer + (fifo->in & (fifo->size - 1)), in, l);8 _9 a: U0 D, ?6 L  r3 T

  111. & {" f2 b7 w! i* }$ @
  112.   /* Then put the rest (if any) at the beginning of the buffer. */: w9 e( H" m2 ^( W* `) X, X2 H
  113.   memcpy(fifo->buffer, (uint8_t *)in + l, len - l);
    $ P  C3 _; N/ x1 v0 {. W) h% |

  114. 1 f; _4 e9 R3 D" A' D( X
  115.   fifo->in += len;
    $ Q) l8 J, @6 k7 R( Z; E5 H5 s

  116. : ?& T5 N6 ]1 b4 Z# q) }4 T
  117.   return len;7 J6 \" Q! A4 x
  118. }" g& [# g! Y2 S
  119. 2 o/ m  O, H1 G) e6 F2 y6 m% G
  120. /**
    9 ~  B. R2 e$ k2 F4 Q
  121.   * @brief  Gets some data from the FIFO.4 C( \/ _6 |7 r, k5 T) b
  122.   * @param  [in] fifo: The fifo to be used.
    6 o2 ]8 H: c1 c' V- ]% |5 ^
  123.   * @param  [in] out:  Where the data must be copied.
    - [5 Q$ F& H+ b0 M& o  U! A, l
  124.   * @param  [in] len:  The size of the destination buffer.& \( Z6 ?, I) t" g5 j
  125.   * @return The number of copied bytes.3 m4 `- u8 S/ e, b4 J. U" L& i
  126.   * @note   This function copies at most @len bytes from the FIFO into
    & O' F1 \# E: ~
  127.   *         the @out and returns the number of copied bytes.
    # Z; j2 _# P% ~; r4 Y# `/ @, c
  128.   */1 @6 s2 a$ F$ @- X- _0 `
  129. uint32_t RingBuffer_Out(RingBuffer *fifo, void *out, uint32_t len)! b) U' s+ a8 V, ~
  130. {4 B6 _2 I5 K8 K1 v# m
  131.   len = min(len, RingBuffer_Len(fifo));
    ( E$ y. J/ w$ X! J
  132. 0 Z8 {  }( X# l0 [, i/ R  [) F
  133.   /* First get the data from fifo->out until the end of the buffer. */
    4 ?5 f; ~! G4 |" Y6 H
  134.   uint32_t l = min(len, fifo->size - (fifo->out & (fifo->size - 1)));
    6 G  C% ]3 N5 {# r
  135.   memcpy(out, fifo->buffer + (fifo->out & (fifo->size - 1)), l);5 e- I$ n. g% v; [3 U+ p

  136. 5 P/ e) h" D8 L
  137.   /* Then get the rest (if any) from the beginning of the buffer. */
    5 @5 x- `5 F$ C  v3 K- L; M& ~* T
  138.   memcpy((uint8_t *)out + l, fifo->buffer, len - l);
    $ ^* E. P- R: |5 W

  139. , ~- \3 E* o. C: x4 r6 X7 T. b4 m- V
  140.   fifo->out += len;4 w+ W+ `( [# |8 E0 f1 r( ?

  141. + K, y& i0 f1 z3 M
  142.   return len;1 r0 r- s- c: U
  143. }
    ; ?; ?. z6 C6 i

  144. 9 T, {! _. @! q+ V
  145. /**. j( S& P2 C% r1 e5 [8 {1 D! U" y
  146.   * @brief  Determine whether some value is a power of two.
    ' J7 G1 K: x1 Z* ]  e! c
  147.   * @param  [in] x: The number to be confirmed.
    2 m6 R9 U7 j( r! `* m
  148.   * @retval true:   Yes.. F* \& K2 `2 o( X* |
  149.   * @retval false:  No.. Z9 m, ~5 `* V, h
  150.   * @note   Where zero is not considered a power of two.
    2 X1 [( T% M' Y  ?/ x7 D. \& g
  151.   */. |2 N! f- D; y8 c" I+ Q
  152. static bool is_power_of_2(uint32_t x)
    6 |* ~' w+ k- W# ^; y  c& A2 j
  153. {
    : x/ r! t, H# I& Z& R) C1 M4 ]
  154.   return (x != 0) && ((x & (x - 1)) == 0);
    . E% @4 O: r" Q( M9 H# J6 m
  155. }
    ( e- |$ S5 g! b6 i! A+ A9 {

  156. ) f+ e4 w& l$ u  {$ q: M) g
  157. /**
    * {$ i2 a" `9 C, r% y, E% J# g& Q
  158.   * @brief  Round the given value up to nearest power of two.
    % x* |7 s6 B' L6 K/ k0 ~
  159.   * @param  [in] x: The number to be converted.$ G& `8 A' T! @$ c& z
  160.   * @return The power of two.! r7 A+ {* v' _3 U. n/ i
  161.   */5 p! M7 ]& Q- [- f4 _" [
  162. static uint32_t roundup_pow_of_two(uint32_t x)
    $ [- V. f6 v& `8 V- X- F; y
  163. {+ Q. n& f2 e) o+ _
  164.   uint32_t b = 0;1 @0 `) }9 b. q* c2 Z0 l, \
  165. ' N) ^2 m; q) F8 S! V
  166.   for(int i = 0; i < 32; i++)
    1 y  E# V" D, x7 m, Z
  167.   {
    $ j& r4 X9 |) X: w3 M
  168.     b = 1UL << i;2 B& t0 A3 `' J- z: x. G
  169. ' Y* Z; j& C) P+ h$ L
  170.     if(x <= b)! `2 W$ f# U; e
  171.     {
    & w9 w5 @: r/ T, X2 m
  172.       break;3 t' N5 n: G* i8 B3 \
  173.     }, W# K2 J7 t* Y) A9 R4 a, t+ P
  174.   }9 \( V# W7 l5 ~2 P' f

  175. 5 a/ \* r+ M2 y- Q" D
  176.   return b;
    - W/ Y) b% z  Y3 \
  177. }</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
  1. /**
    ( @9 g/ C) R8 N0 I, G
  2.   ******************************************************************************' D% O# z& \+ {
  3.   * @file    CAN.h
    / M* N2 v# t' i2 d
  4.   * @author  XinLi$ Y: h# ~9 Z, ~
  5.   * @version v1.0. ]' J+ i- Q5 S
  6.   * @date    24-June-2018
    6 @0 i2 A* u0 E2 S
  7.   * @brief   Header file for CAN.c module.
    % p9 v: |% {3 R" I( {  \! I+ b: n
  8.   ******************************************************************************4 F7 i) v- ]# o0 J0 y8 B
  9.   * @attention
    9 [8 k# a/ S- S
  10.   *
    . f+ v" @2 [" d2 F2 H! N) {5 l0 |
  11.   * <h2><center>Copyright © 2018 XinLi</center></h2>& ]9 E' X$ K. f- _! n
  12.   *
    7 l8 u9 Y* [  r  C
  13.   * This program is free software: you can redistribute it and/or modify
    & A( ^* K  Y2 Y% q0 i0 c
  14.   * it under the terms of the GNU General Public License as published by( V# Y# A% N, L. l4 i8 w7 Z' M
  15.   * the Free Software Foundation, either version 3 of the License, or3 C7 e9 R5 M# Z# v1 w: N
  16.   * (at your option) any later version.
    ( `# ^  D7 t  @- [
  17.   *9 G! B- N; [/ i  k0 D. a/ K6 e
  18.   * This program is distributed in the hope that it will be useful,, q5 J* j, E- _
  19.   * but WITHOUT ANY WARRANTY; without even the implied warranty of6 u7 [, B0 y, |7 B+ w
  20.   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the) R% K+ r# A1 `5 i* @1 t- B
  21.   * GNU General Public License for more details.
    6 D1 @: s8 Z" a
  22.   *9 d6 U8 G! g3 n+ }3 r% \' _
  23.   * You should have received a copy of the GNU General Public License
    7 t% `+ r7 B( I. E# r6 r+ Q+ e: ~
  24.   * 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
  25.   */ ?5 h2 R* u3 m9 M% s3 B- H3 I: ], j
  26.   ******************************************************************************4 y5 l2 O+ q- O& B, ^
  27.   */  s2 x- V1 z4 d5 f/ A
  28. 7 o0 V7 m, n- k: R
  29. #ifndef __CAN_H
    / f- [0 ]! {$ Q7 E
  30. #define __CAN_H
    $ z$ W- q  _3 W4 q6 z8 |3 H$ Z  u
  31. * f3 P. H* k% S% D) m
  32. #ifdef __cplusplus
    ' R% d' O4 H, H2 b# L
  33. extern "C" {
      F/ q; P: h+ i! ^. d; [5 g. y
  34. #endif
    9 _9 \. W8 a! f+ V

  35. + d+ b+ q- M" l4 {9 n
  36. /* Header includes -----------------------------------------------------------*/
    # R0 c8 \4 F5 S
  37. #include "stm32f10x.h"
    ' p% \6 Y1 c7 x% T. c; H  x
  38. #include <stdbool.h>- }0 e4 V, Q" ~8 e5 W9 b3 m

  39. $ B' V0 i1 S7 j
  40. /* Macro definitions ---------------------------------------------------------*/
    & u0 }+ k- |/ S4 @+ Q9 q& R" \

  41. 5 `+ W% C; V9 {
  42. /******************************* CAN1 Configure *******************************/
    ; {) S6 F$ t' K, ~4 Q
  43. #define CAN1_TX_BUFFER_SIZE        (16)# k- j1 N' B% Q$ |1 K
  44. #define CAN1_RX_BUFFER_SIZE        (16)& I) A, K1 |1 F6 v% W3 D; J2 p

  45. $ B3 \, h1 c& D# P" P% [" O
  46. #define CAN1_TX_GPIO_CLOCK         RCC_APB2Periph_GPIOB
    1 E& Z+ T* M6 Z
  47. #define CAN1_RX_GPIO_CLOCK         RCC_APB2Periph_GPIOB
      V3 D8 v$ t/ R: ?7 X
  48. # A+ l7 b- F' ~% I4 o1 u
  49. #define CAN1_TX_GPIO_PORT          GPIOB/ F& D) H- x  S+ R8 t+ r
  50. #define CAN1_RX_GPIO_PORT          GPIOB+ v* F; l3 H, X- C: Z% U! Z2 @

  51. / Z. i, @  I- O! B2 i
  52. #define CAN1_TX_GPIO_PIN           GPIO_Pin_98 A# d( Y% r6 G6 y5 S; N6 g' `* O2 D
  53. #define CAN1_RX_GPIO_PIN           GPIO_Pin_8  U  \* E9 j, o  F4 {& z# z
  54. : {8 d# `( e% T) G, D* ^
  55. #define CAN1_IRQ_PREEMPT_PRIORITY  (0)0 `% ?0 G* E, m. Y6 w( F
  56. #define CAN1_IRQ_SUB_PRIORITY      (0)4 m/ g' m9 Y) z3 v" Y" }' N

  57. - H# i9 m( Q0 r1 Y/ S  _
  58. #define CAN1_PORT_REMAP()          GPIO_PinRemapConfig(GPIO_Remap1_CAN1 , ENABLE)% J9 u6 {- I$ s5 i- Y! f; s  F6 Q- }
  59. /******************************************************************************/& g0 q; S* {, B$ R& a: F
  60. % H0 z. q6 f$ O' L9 G9 S# ^6 ^3 T
  61. #ifdef STM32F10X_CL
    3 ^' y2 U0 [; e  n4 x
  62. /******************************* CAN2 Configure *******************************/- m$ C2 l1 t6 g9 G
  63. #define CAN2_TX_BUFFER_SIZE        (16)
      o! k6 J1 k) D& i- H% M9 t/ X
  64. #define CAN2_RX_BUFFER_SIZE        (16)* `$ w6 P+ h7 Z: c- P- n# f

  65. 5 v6 M/ Z; L  h9 c' x
  66. #define CAN2_TX_GPIO_CLOCK         RCC_APB2Periph_GPIOB! A  q+ R2 r5 r/ Z
  67. #define CAN2_RX_GPIO_CLOCK         RCC_APB2Periph_GPIOB
    ) l; T4 r0 a4 G% \

  68. 7 o1 I5 g4 P0 j* z. s- [; P
  69. #define CAN2_TX_GPIO_PORT          GPIOB0 j; U. J7 |4 ?6 w3 O8 _3 @# J
  70. #define CAN2_RX_GPIO_PORT          GPIOB
    ! r7 r2 J' L, d; b* Y

  71. " t' T0 W4 G0 a# d
  72. #define CAN2_TX_GPIO_PIN           GPIO_Pin_13& e/ J) D8 `5 y; e0 b( N
  73. #define CAN2_RX_GPIO_PIN           GPIO_Pin_12
    ' Q" E+ x7 D) `5 `6 \; u+ w
  74. + y7 e5 k+ o. n" K* |  P
  75. #define CAN2_IRQ_PREEMPT_PRIORITY  (0)
    : R2 [3 X9 a+ _6 r: }0 X# J
  76. #define CAN2_IRQ_SUB_PRIORITY      (0)$ {7 c. {; z5 w# D! U. T

  77. 5 K" n5 }3 o: ]8 p' b7 ~- B
  78. #define CAN2_PORT_REMAP()          GPIO_PinRemapConfig(GPIO_Remap_CAN2 , DISABLE)0 `! J- d5 Z! |7 Y9 j  R3 a; M
  79. /******************************************************************************/0 K: ?0 B8 C. o( i5 o5 N
  80. #endif /* STM32F10X_CL */+ F4 r9 H8 e. O" d

  81. : {) @- G" J% F+ b1 S' \" J9 @
  82. /* Type definitions ----------------------------------------------------------*/
    . F5 O! B1 P$ X5 G- C, P( J) \- ]8 @
  83. typedef enum/ G0 U4 X9 b' W) R8 E7 i; i
  84. {
    5 P! v, s" u( W2 o5 L
  85.   CAN_WorkModeNormal   = CAN_Mode_Normal,' l4 N! L/ }! Y# p# C
  86.   CAN_WorkModeLoopBack = CAN_Mode_LoopBack: `. X$ Z" v* j3 P
  87. }CAN_WorkMode;
    / h! G- J% |" O1 R! _

  88.   ]% \4 s5 t# ?3 q6 {6 T" V
  89. typedef enum$ o& ~9 C" p3 N9 C2 {
  90. {
    - X* k" }& R/ F3 W& }
  91.   CAN_BaudRate1000K = 6,. ^0 p* S6 l& Y( |  U! Y% l4 i0 H
  92.   CAN_BaudRate500K  = 12,
    # M$ c  C' M! y0 S3 w7 b: h/ [
  93.   CAN_BaudRate250K  = 24,- ~% Z2 {8 O( V$ F8 _. K+ C7 ]' D8 ~
  94.   CAN_BaudRate125K  = 48,. }; ^6 S; e* ], Q1 g- D3 A. s
  95.   CAN_BaudRate100K  = 60,7 z! i9 E+ X, b1 f
  96.   CAN_BaudRate50K   = 120,% m% b* {: M" W( Y. D
  97.   CAN_BaudRate20K   = 300,
    2 U9 Z) K, R; J; _4 M2 @+ I7 x% C
  98.   CAN_BaudRate10K   = 600
    ) N2 H5 C. {+ [$ k
  99. }CAN_BaudRate;
      F- E  M# J( u  N$ ~

  100. * A; \/ R, e) o; n0 y. U
  101. /* Variable declarations -----------------------------------------------------*/7 ]' z' |. u, E1 n
  102. /* Variable definitions ------------------------------------------------------*/
    * J4 o+ ~" v2 D# ^* M
  103. /* Function declarations -----------------------------------------------------*/
    + O9 b5 F( w) R% ?" ~; ^
  104. 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
  105. void CAN_Unconfigure(CAN_TypeDef *CANx);+ N) K) P$ r  }% l1 ^2 B

  106. ' c2 i9 t3 F6 Z8 ]( f
  107. void CAN_SetTransmitFinishCallback(CAN_TypeDef *CANx, void (*Callback)(void));
    2 B1 [7 L. l) \! f6 @- [6 g6 e
  108. void CAN_SetReceiveFinishCallback(CAN_TypeDef *CANx, void (*Callback)(void));
    2 ^  g. C1 b0 K4 F6 y6 c

  109. ( l, r5 F  I: j  K
  110. uint32_t CAN_SetTransmitMessage(CAN_TypeDef *CANx, CanTxMsg *Message, uint32_t Number);3 ~, o/ P* ]0 S. x+ N- O
  111. uint32_t CAN_GetReceiveMessage(CAN_TypeDef *CANx, CanRxMsg *Message, uint32_t Number);
    & c5 K: o! a, F  N

  112. / A5 j2 ], G1 h5 j% `
  113. uint32_t CAN_GetUsedTransmitBufferSize(CAN_TypeDef *CANx);; H% I: J* M  l- s
  114. uint32_t CAN_GetUsedReceiveBufferSize(CAN_TypeDef *CANx);
    ' i! m) Q# J/ Y1 g( o
  115. uint32_t CAN_GetUnusedTransmitBufferSize(CAN_TypeDef *CANx);
    : D* B4 W: J8 t
  116. uint32_t CAN_GetUnusedReceiveBufferSize(CAN_TypeDef *CANx);
    & ]! Z8 q; a. x$ w, T4 M1 e

  117. : W- B3 o) y; C1 T
  118. bool CAN_IsTransmitBufferEmpty(CAN_TypeDef *CANx);
    # G% e9 d2 t0 J. k7 M6 L4 A" d1 Q
  119. bool CAN_IsReceiveBufferEmpty(CAN_TypeDef *CANx);9 G9 _, @* A3 f
  120. bool CAN_IsTransmitBufferFull(CAN_TypeDef *CANx);
    , Q  _  j( J% m; t2 H
  121. bool CAN_IsReceiveBufferFull(CAN_TypeDef *CANx);
    $ y! j8 A) [, K4 s: t: }: X5 W4 r
  122. ' c: W; T: E# Y) t/ z$ Y8 w6 ]/ F
  123. void CAN_ClearTransmitBuffer(CAN_TypeDef *CANx);  u, W- `! o* ^2 T+ U* x% X, J  x, d
  124. void CAN_ClearReceiveBuffer(CAN_TypeDef *CANx);4 Z3 ]; @, ^5 B) l1 Y2 ]) }9 u/ n6 f
  125. - E8 ^0 s- E- z1 r. Z: c( P
  126. bool CAN_IsTransmitMessage(CAN_TypeDef *CANx);
    3 ]' I, e& S9 V# s( P  l& w

  127. / e6 k* x4 l* [5 G
  128. /* Function definitions ------------------------------------------------------*/$ q  s; Z5 A; J! }- C& ?! M

  129. # y& O# I7 `$ H1 _2 |  T. X  q
  130. #ifdef __cplusplus
    3 L% l8 k; \& `5 s, R
  131. }# ~" C8 Q- t6 ?) s/ h: {
  132. #endif# J4 N/ H& q; _3 ~, N& R+ i, t: F
  133. ( X& l1 B6 p. B/ L
  134. #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
  1. /**5 T+ r/ {9 h9 p6 l
  2.   ******************************************************************************6 {7 G! J7 O+ }& K- p: `. n
  3.   * @file    CAN.c+ b8 B6 J% N4 m" {: W
  4.   * @author  XinLi
    % |2 w" o5 |4 ]' x( R$ T
  5.   * @version v1.0
      H) J  c9 w1 O4 |9 ^
  6.   * @date    24-June-2018. U: c9 d4 s# S" @
  7.   * @brief   CAN module driver.1 G3 X4 x2 l& J1 ], P  H. l3 g, o
  8.   ******************************************************************************
    0 {& c8 x9 q: N; X3 ^8 X' r3 o8 |
  9.   * @attention
    & H0 F$ ]4 ]) E
  10.   *
    . S5 k4 J. c. M1 D: v) M' M2 ~
  11.   * <h2><center>Copyright © 2018 XinLi</center></h2>7 n% n  Z1 E" S4 j
  12.   *! A% G. x) U" k$ F) o
  13.   * This program is free software: you can redistribute it and/or modify
    + [3 l. N7 l- }1 H
  14.   * it under the terms of the GNU General Public License as published by' W; a8 p5 t, J8 _) A
  15.   * the Free Software Foundation, either version 3 of the License, or
    % m7 S' F+ @$ c2 ~6 F- w
  16.   * (at your option) any later version.# Q& Z# F' x' B6 K
  17.   *
    5 ^; D1 K! u) R+ _4 T+ s
  18.   * This program is distributed in the hope that it will be useful,7 r( _7 i+ n# w$ Z
  19.   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    ; _8 d" |& F! W# u
  20.   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    % d+ l. a! x1 V( t/ Q+ P
  21.   * GNU General Public License for more details., f5 i  z3 m8 T: q
  22.   *6 R, X; L8 n" o  r7 U/ P
  23.   * You should have received a copy of the GNU General Public License  k$ X/ Z8 k  i# u! `
  24.   * 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
  25.   *2 l2 \6 j" _. }+ ]
  26.   ******************************************************************************
    4 R& c8 W+ c9 I& b5 p4 I+ |) o/ \
  27.   */8 Q2 K. d. S$ @4 C& u+ e5 S

  28. ; x( s" [( x- n
  29. /* Header includes -----------------------------------------------------------*/
    " J% k/ C& p5 t* }: f6 r& P; }+ H
  30. #include "CAN.h"/ R* s5 S2 N$ w' I$ C; A
  31. #include "RingBuffer.h"
    $ @1 l2 y8 j5 r: x% S4 q+ T( p

  32. 2 Y) y- v0 P9 w  Q; D
  33. /* Macro definitions ---------------------------------------------------------*/
    . ~. f* x$ O! b' e$ ~6 e$ z: {4 z' U3 a
  34. /* Type definitions ----------------------------------------------------------*/7 i: i' O% E& ~2 w' G7 ?
  35. /* Variable declarations -----------------------------------------------------*/+ S/ L; Y  G% n: b) e/ Z& O
  36. static volatile bool can1InitFlag     = false;. F6 T8 W& K2 R# H: P$ l
  37. static volatile bool can1TransmitFlag = false;  }2 [" Y1 q% b. r+ X) M# x5 o

  38. 1 g- F! k6 _  v+ ~0 |+ K5 u; n" U
  39. static volatile void (*can1TransmitFinishCallback)(void) = 0;: _' i1 m( {2 z% x- R
  40. static volatile void (*can1ReceiveFinishCallback)(void)  = 0;& E) _" i/ p6 b. x0 L+ e( N

  41. # `3 J7 b9 Q) Q" p8 |1 k2 V3 C
  42. static RingBuffer *can1TxBuffer = 0;
    , Z" o2 G3 Q8 ~6 `( ^3 b
  43. static RingBuffer *can1RxBuffer = 0;
    - x* E% D, i2 |

  44. 7 W: Y5 U& D+ W5 I2 W/ x
  45. #ifdef STM32F10X_CL
      T- R+ U5 k1 h& V4 h
  46. static volatile bool can2InitFlag     = false;
      X/ O: y3 n- o
  47. static volatile bool can2TransmitFlag = false;7 R! _! S5 h) J% m" G' w8 p. i

  48. 8 O, k6 ]+ D, D; G8 ~3 d
  49. static volatile void (*can2TransmitFinishCallback)(void) = 0;$ q/ t% P$ r7 X$ ^: Q" U5 B
  50. static volatile void (*can2ReceiveFinishCallback)(void)  = 0;# O! o- H& b7 R" Q9 B  J! R# C& W
  51. ; l/ ^: L: Z" `+ R1 l
  52. static RingBuffer *can2TxBuffer = 0;/ r- W6 H0 D( T4 l
  53. static RingBuffer *can2RxBuffer = 0;
    7 |, q+ B9 L+ `
  54. #endif /* STM32F10X_CL */
    % `0 |: ^6 Q- C) C; t1 _2 c

  55. ; r5 [0 M3 ^: r) c  m& f4 w: e
  56. /* Variable definitions ------------------------------------------------------*/8 K* N6 }: F; m. d2 I" X
  57. /* Function declarations -----------------------------------------------------*/
    4 K/ L; T6 }5 o! l
  58. /* Function definitions ------------------------------------------------------*/" B9 \2 N! N0 d9 O! I8 c, g% k/ R

  59. - {, }& u% Y& i3 ]
  60. /**/ z0 @) \  t, n8 T' z
  61.   * @brief  CAN configure.4 t- ]9 r' u& J
  62.   * @param  [in] CANx:     Where x can be 1 or 2 to select the CAN peripheral.
    8 M* G# E3 w5 [& c" z! ^/ e" R
  63.   * @param  [in] WorkMode: Work mode.
    * ?) A- u1 t7 i: G
  64.   * @param  [in] BaudRate: Communication baud rate.
    ' q# c8 M$ H' d' g
  65.   * @param  [in] StdId:    Filter standard frame ID.
    " Q6 `, E6 f$ b
  66.   * @param  [in] ExtId:    Filter extended frame ID." B. ?4 k# r' u- d0 |: @, B
  67.   * @return None.  S% I, L. h  N9 T* B% W' s
  68.   */
    ! ~# y, e! j( |9 |: U: {/ W3 j
  69. 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 @
  70. {
    : j& y1 {( u3 e& n; L9 d( Z
  71.   GPIO_InitTypeDef      GPIO_InitStructure      = {0};+ b9 A2 y( F, U& x7 E; y  B
  72.   CAN_InitTypeDef       CAN_InitStructure       = {0};
    9 Z; k. f! N1 n5 \: H9 g
  73.   CAN_FilterInitTypeDef CAN_FilterInitStructure = {0};2 ?' K! l; w/ R, I) N" n
  74.   NVIC_InitTypeDef      NVIC_InitStructure      = {0};
    5 a! {9 u0 \* ]3 I. n! z
  75. % N8 ~5 H+ z$ f( S$ b
  76.   if(CANx == CAN1)
    9 i) f5 u) E$ d  O3 q5 w
  77.   {
    3 i. i. V4 n& K3 f
  78.     if(can1InitFlag == false)
      G" \; g5 e# w/ Q& J8 O) P
  79.     {
    6 c$ e0 Z) e8 s; ?. e
  80.       can1InitFlag = true;
    * y! K1 l2 E" U2 ~$ P

  81. / {( E5 I& W5 F, ~1 f& B4 @; X
  82.       can1TransmitFlag = false;  W8 E  p0 ]  t& s

  83. - \! S8 h, U& e; n  o. @$ E
  84.       can1TransmitFinishCallback = 0;
    ) J4 y! _( q6 Z1 O* S
  85.       can1ReceiveFinishCallback  = 0;
    5 c2 ]; O. A; s2 E

  86. ' X: |; {! T. T9 u4 s( h) t
  87.       can1TxBuffer = RingBuffer_Malloc(sizeof(CanTxMsg) * CAN1_TX_BUFFER_SIZE);! S1 X, I: @0 d5 T4 c% O' L5 E( ^
  88.       can1RxBuffer = RingBuffer_Malloc(sizeof(CanRxMsg) * CAN1_RX_BUFFER_SIZE);6 a  S, _+ H8 b* X8 Z

  89. ' _1 Q* Z. l$ t* i& e" @, l
  90. #ifdef STM32F10X_CL
    : i9 j' K. i6 {. O5 [
  91.       if(can2InitFlag == false); M" P' y5 f( e0 S1 ^" B
  92. #endif /* STM32F10X_CL */
    * |* V! m2 Z0 K6 D! p/ V
  93.       {; b* D2 J  X3 T3 I) B
  94.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);5 @+ t% C" Q& T3 p4 K  O3 D: Q0 @
  95.       }
    + Q3 i" m# L6 O" b9 Q- ~  Z8 o

  96. & A7 b# D1 V' v) }6 M
  97.       RCC_APB2PeriphClockCmd(CAN1_TX_GPIO_CLOCK | CAN1_RX_GPIO_CLOCK | RCC_APB2Periph_AFIO, ENABLE);
    ; }7 j' m/ G2 E

  98. ! T  a4 G8 J. Z) Y( W4 T$ x
  99.       GPIO_InitStructure.GPIO_Pin   = CAN1_TX_GPIO_PIN;
    / D* \' D% ?9 ^0 P
  100.       GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF_PP;
    8 {% s* M; S1 p3 P
  101.       GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;$ H' r4 ]: j+ Z# v! v' N& L
  102.       GPIO_Init(CAN1_TX_GPIO_PORT, &GPIO_InitStructure);
    / @) E' p' Z4 a* d
  103. ! w% Q4 l% `# v2 ^! R% @
  104.       GPIO_InitStructure.GPIO_Pin   = CAN1_RX_GPIO_PIN;$ R, T1 @3 i4 p. K
  105.       GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IPU;( k/ u1 A1 h& n1 l) S& c
  106.       GPIO_Init(CAN1_RX_GPIO_PORT, &GPIO_InitStructure);
    & |) d: a6 |' y- l% t

  107. # @& o, U/ G! K6 Y
  108.       CAN1_PORT_REMAP();
    ) P3 P. k0 i' |2 e# ^, `

  109. & c) ]9 f  `5 g4 E( P
  110.       CAN_InitStructure.CAN_Prescaler = BaudRate;
    $ [. W/ g+ o* b8 v& E+ Q0 {
  111.       CAN_InitStructure.CAN_Mode      = WorkMode;
    $ N: D* p5 O# {  F2 q* L0 `! M, [2 o
  112.       CAN_InitStructure.CAN_SJW       = CAN_SJW_1tq;4 k2 j" n* Q9 Q- L
  113.       CAN_InitStructure.CAN_BS1       = CAN_BS1_3tq;
    % n) B0 {4 ~3 m, o: d$ A. Z
  114.       CAN_InitStructure.CAN_BS2       = CAN_BS2_2tq;* x! l' ]: s" O5 i: L
  115.       CAN_InitStructure.CAN_TTCM      = DISABLE;
    6 v7 j8 R9 Q! [( y3 E/ W$ j- `4 s
  116.       CAN_InitStructure.CAN_ABOM      = ENABLE;- O4 [  |2 J5 ^- _
  117.       CAN_InitStructure.CAN_AWUM      = DISABLE;
      h! H, D4 l+ i" K# B3 s
  118.       CAN_InitStructure.CAN_NART      = ENABLE;
    ( }/ P: [9 E" [: ?! j1 k: F
  119.       CAN_InitStructure.CAN_RFLM      = DISABLE;
    . O8 g$ E1 A: E* W5 `7 t+ c
  120.       CAN_InitStructure.CAN_TXFP      = ENABLE;2 u7 }% `" V) Y  D) w9 f9 |6 f
  121. $ J9 R8 j5 Q; o# n
  122.       CAN_DeInit(CAN1);
    ! p3 p5 f- x6 ^; o( ?! b: r2 v' z
  123.       CAN_Init(CAN1, &CAN_InitStructure);
    + j$ y. n! k* u7 {, }5 R

  124. - K' T9 @$ \7 m6 j3 k$ }
  125.       CAN_FilterInitStructure.CAN_FilterIdHigh         = (uint16_t)((((StdId<<18)|ExtId)<<3)>>16);( t* ~' I6 x' |/ p/ i
  126.       CAN_FilterInitStructure.CAN_FilterIdLow          = (uint16_t)(((StdId<<18)|ExtId)<<3);
    7 l' @0 C  y' `- a
  127.       CAN_FilterInitStructure.CAN_FilterMaskIdHigh     = (~((uint16_t)((((StdId<<18)|ExtId)<<3)>>16)))&0xFFFF;
      ]/ {2 X9 r2 A
  128.       CAN_FilterInitStructure.CAN_FilterMaskIdLow      = (~((uint16_t)(((StdId<<18)|ExtId)<<3)))&0xFFF8;
    5 Z1 I9 g4 `/ ?& r
  129.       CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0;5 F/ ]/ t1 u0 A" p8 a
  130.       CAN_FilterInitStructure.CAN_FilterNumber         = 0;
    1 E3 R4 e) `5 e) }) p" _9 T
  131.       CAN_FilterInitStructure.CAN_FilterMode           = CAN_FilterMode_IdMask;/ v, r6 f- h9 t
  132.       CAN_FilterInitStructure.CAN_FilterScale          = CAN_FilterScale_32bit;+ h( L9 ~& ?" d
  133.       CAN_FilterInitStructure.CAN_FilterActivation     = ENABLE;- y* F& u- ~! `  i, |4 c; W
  134.       CAN_FilterInit(&CAN_FilterInitStructure);
    # u5 [" [. i) [* X7 P$ t, T3 g8 O
  135.   q. B* `8 X$ M9 ^7 F: z$ b- \
  136.       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
  137.                          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
  138.       CAN_ITConfig(CAN1, CAN_IT_TME | CAN_IT_FMP0, ENABLE);
    0 h' ~" f8 a) ~6 f

  139. $ q. b! i6 }( ?  A, c; d, T
  140. #ifdef STM32F10X_CL
    # T# k, }" [! Z$ e8 F$ e/ [- T
  141.       NVIC_InitStructure.NVIC_IRQChannel                   = CAN1_TX_IRQn;
    " }4 r& N! W+ O
  142. #else$ o4 u: y$ L3 ~% p
  143.       NVIC_InitStructure.NVIC_IRQChannel                   = USB_HP_CAN1_TX_IRQn;- u' ^& }) S& J( F$ Y& o
  144. #endif /* STM32F10X_CL */, P( L& u$ d# p% M

  145. " K, t: i5 v1 a; g( E' g9 I
  146.       NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = CAN1_IRQ_PREEMPT_PRIORITY;
    & A$ u6 i0 c* o  X1 J8 e
  147.       NVIC_InitStructure.NVIC_IRQChannelSubPriority        = CAN1_IRQ_SUB_PRIORITY;6 v) s5 Y* }( P$ ]- `1 X1 _
  148.       NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;, U# y: C5 O) a" {! E& ?! G
  149.       NVIC_Init(&NVIC_InitStructure);8 _0 S2 b4 A! h1 e
  150. - r/ p4 h! c4 `0 w. H; g/ }4 o! |: E" C) F
  151. #ifdef STM32F10X_CL
    " r# p: p& R9 F3 w
  152.       NVIC_InitStructure.NVIC_IRQChannel                   = CAN1_RX0_IRQn;
    % ?5 U# y' x, ?% b/ {' J: B1 E5 J
  153. #else" q3 S# h$ p* D; ?  N
  154.       NVIC_InitStructure.NVIC_IRQChannel                   = USB_LP_CAN1_RX0_IRQn;
    - @: [8 P9 q; p. }
  155. #endif /* STM32F10X_CL */
    % }# q; [1 ^& h: U
  156. 4 r4 C% e# S' f( x* \
  157.       NVIC_Init(&NVIC_InitStructure);
    2 X. M8 I6 m  Y" m; ]
  158.     }
    ( H0 ]. m5 _8 ^
  159.   }
    3 R& a- l) [" @9 ]# ?4 o
  160.   |) F: `+ \; {
  161. #ifdef STM32F10X_CL
    4 U' D# R1 |: }  m  K
  162.   if(CANx == CAN2)/ }5 d* s& J# P0 \
  163.   {
    5 [- V: h4 i7 a. d/ J
  164.     if(can2InitFlag == false)3 `6 B, `% b* H( `) b' `
  165.     {
    ( A, P" r$ K% T% V- F& K0 X
  166.       can2InitFlag = true;5 u$ b& {: {$ \! [3 ~( m
  167. . y' b2 J; `0 X6 E  j* D
  168.       can2TransmitFlag = false;2 i8 z8 L1 [. B0 X8 _" G- E4 b
  169. & S' A9 I: I: R9 K7 r' J
  170.       can2TransmitFinishCallback = 0;. t/ Q, y* N8 {5 ^" V) c
  171.       can2ReceiveFinishCallback  = 0;. B. v; ^4 a- Q& N' S
  172. / H; e4 L- H+ a, o4 {& i; Q$ I
  173.       can2TxBuffer = RingBuffer_Malloc(sizeof(CanTxMsg) * CAN2_TX_BUFFER_SIZE);
    5 D4 k& N- D/ P" q! O: y! p
  174.       can2RxBuffer = RingBuffer_Malloc(sizeof(CanRxMsg) * CAN2_RX_BUFFER_SIZE);
    ( ]( N+ j, g0 Z9 {! [2 b/ j
  175. 5 |2 G! {9 }5 S2 I! g6 Q  n, F
  176.       if(can1InitFlag == false)
    7 m8 I. p. G" A+ A& w
  177.       {
    2 B$ [9 [6 ]) ]& G6 L
  178.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);, Z8 y0 ?2 u) c# I3 R
  179.       }
    8 i7 E3 {$ u& @

  180. / X- ]6 o. U7 Z
  181.       RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2, ENABLE);
    * ?* k  F' C) `1 y7 R' r" n
  182.       RCC_APB2PeriphClockCmd(CAN2_TX_GPIO_CLOCK | CAN2_RX_GPIO_CLOCK | RCC_APB2Periph_AFIO, ENABLE);0 ^% {; V( _( X6 S6 }, g7 N& X
  183. ! n9 r/ x( k9 n" J% e3 T& `
  184.       GPIO_InitStructure.GPIO_Pin   = CAN2_TX_GPIO_PIN;$ H) m% M! p- V0 g2 o& ?
  185.       GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF_PP;) ]( L# ^0 S8 `5 B3 X
  186.       GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    0 A) ~" A( t% [7 z
  187.       GPIO_Init(CAN2_TX_GPIO_PORT, &GPIO_InitStructure);
    ; M6 D# y4 e' G2 A) t1 I0 Q* o  K

  188. ! ]% R! K, h9 A' U2 W- x
  189.       GPIO_InitStructure.GPIO_Pin   = CAN2_RX_GPIO_PIN;
    - o) b* [+ W& O- M2 h+ D# @
  190.       GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IPU;
    1 c* d8 g3 j3 d
  191.       GPIO_Init(CAN2_RX_GPIO_PORT, &GPIO_InitStructure);* l8 e5 i; U9 f3 C2 K

  192. - s2 s5 ]0 y4 H5 T" t1 H' q% i9 I5 U+ x
  193.       CAN2_PORT_REMAP();& p- ?8 n7 O7 D2 A* J9 s
  194. 1 ^: s9 G9 W- W
  195.       CAN_InitStructure.CAN_Prescaler = BaudRate;$ {8 b- [" l+ f% q/ ?
  196.       CAN_InitStructure.CAN_Mode      = WorkMode;6 _0 p  A3 W  A
  197.       CAN_InitStructure.CAN_SJW       = CAN_SJW_1tq;
    1 w8 b* f5 C  V$ F. U0 b, Z- j
  198.       CAN_InitStructure.CAN_BS1       = CAN_BS1_3tq;
    ) `/ N" f) G2 f" y0 i1 v/ l
  199.       CAN_InitStructure.CAN_BS2       = CAN_BS2_2tq;/ {3 y8 _- i: i' G6 ~! I# m
  200.       CAN_InitStructure.CAN_TTCM      = DISABLE;# ?/ U& k9 @% d  q9 Z6 Q
  201.       CAN_InitStructure.CAN_ABOM      = ENABLE;4 D( W! w4 Q& L4 Y9 k7 I- @
  202.       CAN_InitStructure.CAN_AWUM      = DISABLE;  A" Q6 M7 H3 @: v3 u3 }
  203.       CAN_InitStructure.CAN_NART      = ENABLE;
    5 I' H8 p, ]: H
  204.       CAN_InitStructure.CAN_RFLM      = DISABLE;+ a8 j7 q3 V9 u3 ~9 @
  205.       CAN_InitStructure.CAN_TXFP      = ENABLE;
    0 `" S# o, l# S; _1 Z
  206. 9 v( l# g( |" A4 B8 ^9 p) A, K
  207.       CAN_DeInit(CAN2);% T+ }- a+ I! \  Y
  208.       CAN_Init(CAN2, &CAN_InitStructure);" j* s7 q/ b; o# \

  209. ! n. ~: \$ X6 G- Y3 n
  210.       CAN_FilterInitStructure.CAN_FilterIdHigh         = (uint16_t)((((StdId<<18)|ExtId)<<3)>>16);9 f* a9 B; T# J1 B: V: c8 d
  211.       CAN_FilterInitStructure.CAN_FilterIdLow          = (uint16_t)(((StdId<<18)|ExtId)<<3);$ h8 o! D$ c( u8 U/ o
  212.       CAN_FilterInitStructure.CAN_FilterMaskIdHigh     = (~((uint16_t)((((StdId<<18)|ExtId)<<3)>>16)))&0xFFFF;
    6 ?/ K3 R# h4 n3 G. b3 l4 v3 W2 I
  213.       CAN_FilterInitStructure.CAN_FilterMaskIdLow      = (~((uint16_t)(((StdId<<18)|ExtId)<<3)))&0xFFF8;% ~5 d3 w5 A* J6 j
  214.       CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0;
    . U9 b: l1 [$ d; y9 Q  m
  215.       CAN_FilterInitStructure.CAN_FilterNumber         = 14;
    - y5 }& A3 S# i& x5 s' d2 I
  216.       CAN_FilterInitStructure.CAN_FilterMode           = CAN_FilterMode_IdMask;* {+ f8 O6 F5 I% j
  217.       CAN_FilterInitStructure.CAN_FilterScale          = CAN_FilterScale_32bit;6 f, \1 `4 {" x! d  V0 n6 z
  218.       CAN_FilterInitStructure.CAN_FilterActivation     = ENABLE;
    % t1 ?' I6 a8 |, j5 v4 W- r
  219.       CAN_FilterInit(&CAN_FilterInitStructure);
    ; ]1 s/ f, z! {- B5 w, E! b

  220. + D; }& U. P. @. S. F9 {
  221.       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: _
  222.                          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
  223.       CAN_ITConfig(CAN2, CAN_IT_TME | CAN_IT_FMP0, ENABLE);- v0 U' r, g. _6 _- C

  224. 5 e' B' _1 o" N9 v( Y9 Z5 H( O
  225.       NVIC_InitStructure.NVIC_IRQChannel                   = CAN2_TX_IRQn;6 G0 ^" m0 `* g
  226.       NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = CAN2_IRQ_PREEMPT_PRIORITY;
    : _( M" m( B7 p8 G& b7 |
  227.       NVIC_InitStructure.NVIC_IRQChannelSubPriority        = CAN2_IRQ_SUB_PRIORITY;9 u0 O+ b& r4 s6 Z9 Q( P9 T1 l
  228.       NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;3 o, P" E$ X/ m: p( P7 D1 d) k# w3 S
  229.       NVIC_Init(&NVIC_InitStructure);8 r2 s; F1 F6 ~% H" X
  230. 7 ~1 M5 J9 x, T( y4 j* t6 N
  231.       NVIC_InitStructure.NVIC_IRQChannel                   = CAN2_RX0_IRQn;/ X$ W/ L- K; ~. S  H" K& u( n  G4 k
  232.       NVIC_Init(&NVIC_InitStructure);
    # {8 \% W8 S3 O7 p
  233.     }8 q0 p+ |6 \# C. v
  234.   }- f$ c* U! u, D/ _5 W- Q
  235. #endif /* STM32F10X_CL */
    . ^! J& x0 S1 m5 {% r* C7 R
  236. }
    # N' @6 A/ R' r! p

  237. 2 I3 i+ H& X5 j8 e) w" Y  p
  238. /**3 K- Q( Q. j8 W8 b& _- D9 d0 S
  239.   * @brief  CAN unconfigure.0 M: x- x7 }1 [, C: t9 T
  240.   * @param  [in] CANx: Where x can be 1 or 2 to select the CAN peripheral.
    # f$ c# A( N4 E6 G% r
  241.   * @return None.- z4 [) k( L7 ]# V
  242.   */8 T% Q) Q4 B! Q3 @& ^0 H3 T% ^
  243. void CAN_Unconfigure(CAN_TypeDef *CANx); R' [. F0 y* ?* J( F0 l
  244. {
    ; g3 K& \, Y1 O& o
  245.   NVIC_InitTypeDef NVIC_InitStructure = {0};
      `( M; i+ x; o  A

  246. ! W& C8 f1 c( }4 U' V& [
  247.   if(CANx == CAN1)
    + |  {1 A8 f* P" ?! n
  248.   {' \4 t! U, q' Z0 t: o! S/ T1 H
  249.     if(can1InitFlag == true)
    - N1 @9 @7 i7 x. H% F6 F
  250.     {
    ( R( h! l7 J1 g* W) _
  251.       can1InitFlag = false;# t& v. O+ L% H: c
  252.   b+ g. K6 o- x
  253. #ifdef STM32F10X_CL- J4 s4 T/ ~4 o# h$ l
  254.       NVIC_InitStructure.NVIC_IRQChannel                   = CAN1_TX_IRQn;: H( D, R% j+ z' _' O; c  i4 z
  255. #else3 h, T# E6 s# g. q6 W7 s
  256.       NVIC_InitStructure.NVIC_IRQChannel                   = USB_HP_CAN1_TX_IRQn;5 w- b+ M, S$ p$ ?$ ?7 n  [
  257. #endif /* STM32F10X_CL */( P% E6 l; `) o5 I) Y  h: O' Q

  258. ! a7 W4 S7 [5 s* q2 E9 S  R3 D
  259.       NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = CAN1_IRQ_PREEMPT_PRIORITY;& _$ J, {4 h0 Z* W, B
  260.       NVIC_InitStructure.NVIC_IRQChannelSubPriority        = CAN1_IRQ_SUB_PRIORITY;$ y6 b( Q" ~7 ]
  261.       NVIC_InitStructure.NVIC_IRQChannelCmd                = DISABLE;
    ! n0 G( X8 W3 l1 }
  262.       NVIC_Init(&NVIC_InitStructure);
    8 y- ~+ f* T/ q" z8 ~4 W3 R( z7 Z5 B; h
  263. % ^  h7 X: g" Y: ?* Y* l: p
  264. #ifdef STM32F10X_CL
    ( r$ j/ e* I" m
  265.       NVIC_InitStructure.NVIC_IRQChannel                   = CAN1_RX0_IRQn;
    : H# V  e' Y/ y+ Y4 ]4 p+ l! ]
  266. #else6 y. i1 D# y6 \
  267.       NVIC_InitStructure.NVIC_IRQChannel                   = USB_LP_CAN1_RX0_IRQn;
    1 l, q7 A. K# ]
  268. #endif /* STM32F10X_CL */
    8 I, ^: }; f: Q3 t9 J
  269. , W. ]  \* l8 \& F0 A" `
  270.       NVIC_Init(&NVIC_InitStructure);
    7 i( Z4 Y5 k  {: u5 x
  271. 1 B/ ]% c+ l% d* [% n2 o% r2 T$ ^
  272.       CAN_DeInit(CAN1);
      A3 [7 s0 i0 ?8 p6 _

  273. 6 p; R3 V2 M" o8 f' T) j0 `" w
  274. #ifdef STM32F10X_CL
    7 Y$ O6 s9 F8 k! t, A: A' e$ C( _
  275.       if(can2InitFlag == false)
    8 ~. K) d# a' ?$ b' p
  276. #endif /* STM32F10X_CL */( i. O+ T% G' p" W/ E2 p: f
  277.       {. i% N0 {0 N7 T. x& [8 y8 f/ m4 K- h
  278.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, DISABLE);
    $ I& h: x9 `5 {5 S+ y
  279.       }+ D( I, w7 B. H) @1 |1 q' D

  280.   @2 q+ L  ~$ Q9 R; V+ v, a
  281.       can1TransmitFlag = false;
    ( {9 ~/ \: O$ v9 q2 ~
  282. + S. Z6 [: V; P
  283.       can1TransmitFinishCallback = 0;$ r+ t- m  b" j  G: g
  284.       can1ReceiveFinishCallback  = 0;
    0 n5 {/ t+ a6 O
  285. 9 A* S3 K! W4 v/ P4 s9 v" e+ t& i
  286.       RingBuffer_Free(can1TxBuffer);% K3 z, K3 J) d. b3 W0 t" G+ s
  287.       RingBuffer_Free(can1RxBuffer);) [. A# F& Y3 I  g: B
  288.     }+ v$ |6 d  U8 e7 L" }7 v/ n
  289.   }- F3 c# \. `: i& `+ c! ~

  290. ) U, C: v) z! J
  291. #ifdef STM32F10X_CL
    ! N: \6 D6 l+ u/ H+ b& d9 q% V
  292.   if(CANx == CAN2)
    7 s& o5 q! a- o1 r. Q, l
  293.   {
    " O) i9 S& d6 H! |. h
  294.     if(can2InitFlag == true)6 w* ]8 q6 q9 [8 Q/ k5 D4 R
  295.     {1 ^& p% c6 b4 Q9 N
  296.       can2InitFlag = false;
    5 P* w9 R% X4 X# x, Y2 p
  297. % R  A/ z9 n6 `( d- i4 `2 B4 m! J
  298.       NVIC_InitStructure.NVIC_IRQChannel                   = CAN2_TX_IRQn;: i  V+ m% m# N- G6 t) {
  299.       NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = CAN2_IRQ_PREEMPT_PRIORITY;
    1 q* v1 E$ R7 Y+ C: m1 `
  300.       NVIC_InitStructure.NVIC_IRQChannelSubPriority        = CAN2_IRQ_SUB_PRIORITY;& ~4 f& s6 `- S4 e4 \: ?
  301.       NVIC_InitStructure.NVIC_IRQChannelCmd                = DISABLE;
    ( B% J1 N9 }/ ^3 x9 n4 l* `0 y" k
  302.       NVIC_Init(&NVIC_InitStructure);& Y' q$ ]. E: ~
  303. ( P) W, j6 j2 {1 x
  304.       NVIC_InitStructure.NVIC_IRQChannel                   = CAN2_RX0_IRQn;2 b7 ?# p8 H" m( _
  305.       NVIC_Init(&NVIC_InitStructure);0 y+ r' a% r/ ~& p- Y. D% ~
  306. & Z( Z$ s3 t" W& p9 V
  307.       CAN_DeInit(CAN2);) t( k0 m+ x6 S' ~0 U
  308. * v2 r$ z0 x& u" {1 L' O" m/ ~
  309.       if(can1InitFlag == false)
    7 S% _+ o3 }& `$ T* @2 y! K& P
  310.       {( r- A0 k3 B3 s1 \. {
  311.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, DISABLE);
    ! @0 M7 d+ K# G' I5 {4 c$ ^
  312.       }
    0 k% `* {2 Y/ K2 J! _- y; q7 S/ v" L5 P

  313. 6 t4 y1 k" n/ f8 u' x, a
  314.       RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2, DISABLE);
    0 r! Q) M9 T# t! e  \# l8 P/ y
  315. $ L& |' z  q0 c3 `2 r
  316.       can2TransmitFlag = false;* ^2 y7 W5 r9 I8 X3 i# r9 l

  317. ) q  H' O- Q- n- M5 I% J% E
  318.       can2TransmitFinishCallback = 0;3 K2 a. e: m1 M! ]$ e' `8 F2 g: X
  319.       can2ReceiveFinishCallback  = 0;
    ( }: |' ]. O9 |8 P* x: j+ f

  320. ' R  k* d  h- B) b7 c* K
  321.       RingBuffer_Free(can2TxBuffer);* X- k# U7 e1 n9 f- f, q, z
  322.       RingBuffer_Free(can2RxBuffer);' E. r4 ]$ F7 |, d( K
  323.     }0 G5 `7 t6 }5 A+ A& n
  324.   }) t) o3 }& a! K
  325. #endif /* STM32F10X_CL */9 E  R: N- d( G2 s0 y; F' v9 [
  326. }
    * u' j2 N, Y2 D9 P! W2 ]
  327. $ \0 b! X/ {( S& \4 X
  328. /**: i; ]1 c. q$ a$ ]# E5 T
  329.   * @brief  CAN set transmit finish callback.
    4 l$ v* C. ^4 n& Y+ ^! ]
  330.   * @param  [in] CANx:     Where x can be 1 or 2 to select the CAN peripheral.
    ' {& o& b* L' P4 @) N$ i3 @
  331.   * @param  [in] Callback: Callback.2 m$ g/ F  T0 t, B, `
  332.   * @return None.
    6 u2 N" r# R0 V: P
  333.   */
    / ~  K. w$ @. y3 |
  334. void CAN_SetTransmitFinishCallback(CAN_TypeDef *CANx, void (*Callback)(void))
    8 t- P  V8 o$ ^2 J0 X/ d
  335. {
    . g8 H0 B9 H* w/ N' p9 |
  336.   if(CANx == CAN1)- J1 V# |, M- r. K& [1 `+ F, k
  337.   {
    $ v  \! `" L% f, R
  338.     if(can1InitFlag == true)
    # ]6 \5 m" M) c+ i
  339.     {  g+ a6 W5 G0 ]; R
  340.       can1TransmitFinishCallback = (volatile void (*)(void))Callback;
    5 m/ J# _6 c4 {1 u# K6 q. T
  341.     }
    # N$ g& G5 b7 \3 ]0 m# P8 x  P, D
  342.   }
    6 {# q8 P1 N9 \# H* h; i6 G1 r
  343. ! Q: \8 A, C- m2 A& b9 p
  344. #ifdef STM32F10X_CL
    ) o+ }8 \* {5 o1 Z& o1 u* b
  345.   if(CANx == CAN2)
    1 |9 ]( W3 t8 N; g# B
  346.   {5 I$ e2 q. Y& \
  347.     if(can2InitFlag == true)
    ! L& I3 V8 z, }  b& N
  348.     {
    2 O* H) P/ o* }' y; d, u) R! w* G2 A
  349.       can2TransmitFinishCallback = (volatile void (*)(void))Callback;6 e$ R  K- E: ]  y# l, @2 }
  350.     }
    ' {" e0 {* d! d  s+ N) r
  351.   }7 [# }0 N* {4 l3 ^
  352. #endif /* STM32F10X_CL */8 t/ H. `" G% C( A' ]  @4 j2 M
  353. }
    3 J0 U# U, h! p5 `/ W' F
  354. 1 P2 l1 Q5 Z8 ]7 a  e+ a  \' o
  355. /**, a9 W, s2 A; {5 X; m  \
  356.   * @brief  CAN set receive finish callback.1 X. `6 L2 ^4 k5 Z. L
  357.   * @param  [in] CANx:     Where x can be 1 or 2 to select the CAN peripheral.
    $ d: Y( h. r! k5 T
  358.   * @param  [in] Callback: Callback.1 C0 t. D1 B" z4 C: g5 v- I
  359.   * @return None.* K. p- L: A! k' h
  360.   */
    " W1 C# V* }2 C5 \' f+ B
  361. void CAN_SetReceiveFinishCallback(CAN_TypeDef *CANx, void (*Callback)(void))
    2 N( i+ t3 Q' }
  362. {/ G$ t8 X7 w2 {7 u8 n5 R
  363.   if(CANx == CAN1)
    # d# f& h+ C% Q) j7 s0 j
  364.   {
    5 G9 }2 {0 k2 }
  365.     if(can1InitFlag == true)+ p- ^& V0 i( ?
  366.     {
    - m, `7 K. _* {& N9 L
  367.       can1ReceiveFinishCallback = (volatile void (*)(void))Callback;$ T5 o! |- s9 ~; K! m: m8 q
  368.     }4 p+ f/ I9 h7 @2 ]: B6 W
  369.   }+ g3 r$ A! q- w& O, ~" ~

  370. - \! Z+ S- u0 ^8 ~
  371. #ifdef STM32F10X_CL! F# k7 B2 v4 _+ s) X
  372.   if(CANx == CAN2)
    $ i4 Z- U- s  I2 x8 p' D- H
  373.   {
    % \( Q- A2 z4 `9 X3 G5 p
  374.     if(can2InitFlag == true)
    5 ?( a  b; X7 T$ S- L
  375.     {
    * a& M* k0 h0 v& X
  376.       can2ReceiveFinishCallback = (volatile void (*)(void))Callback;+ G2 T( V# N9 K9 {
  377.     }  J5 k+ V" }2 G2 n
  378.   }
    $ @, i; Z2 I+ K' |# C8 D  B5 a
  379. #endif /* STM32F10X_CL */2 M& F- J+ |2 c( T" Q$ g3 o
  380. }0 _, e9 u. ]; F0 l: V; h
  381. & X, e. `6 ?& i: ?0 q
  382. /**
    ( T8 e5 u% Q( n8 l  S6 ~6 Z; J' |
  383.   * @brief  CAN set transmit message.
    ( b7 t0 a2 C6 c' P
  384.   * @param  [in] CANx:    Where x can be 1 or 2 to select the CAN peripheral.
    - @% a& Z& J3 o# e/ q
  385.   * @param  [in] Message: The address of the message to be transmit.. U; L+ z! S7 i
  386.   * @param  [in] Number:  The number of the message to be transmit.
    ) q" d- U9 Q4 E
  387.   * @return The number of message transmit.
    5 ]/ A+ u2 m. |; C. k6 J1 t
  388.   */
    - q9 M2 q1 T; S/ s( z
  389. uint32_t CAN_SetTransmitMessage(CAN_TypeDef *CANx, CanTxMsg *Message, uint32_t Number)
    2 G/ E  r6 j' \1 ^. {2 o$ T% s
  390. {
    1 j: s8 Y3 p1 h2 k
  391.   if(CANx == CAN1). c1 F6 F* y0 g$ g9 [2 m9 W
  392.   {8 Q5 ]) O6 ~# y: Z* v
  393.     if(can1InitFlag == true)
    + [6 x4 W/ m  E" a. W
  394.     {. }8 i9 ~! P9 V6 u3 E
  395.       uint32_t available = RingBuffer_Avail(can1TxBuffer) / sizeof(CanTxMsg);+ R1 k. t* v& U: m8 Y

  396. + M% a8 |+ N9 ^3 r* i4 [
  397.       if(available > Number)/ A8 Z& p' j% o
  398.       {
    * b0 @8 E; h* `/ [' \+ Z
  399.         Number = RingBuffer_In(can1TxBuffer, Message, sizeof(CanTxMsg) * Number) / sizeof(CanTxMsg);
    / }. Z2 O1 Q) z' ]$ q1 }! q
  400.       }
    0 y* e* b; K+ F' i2 V* L
  401.       else  w; i+ r3 h* U  s) {5 {' Z
  402.       {4 ^( M  W1 j/ [/ y" y( t; W& G
  403.         Number = RingBuffer_In(can1TxBuffer, Message, sizeof(CanTxMsg) * available) / sizeof(CanTxMsg);
    $ H! [$ H, o/ j
  404.       }
    $ |& F! Q1 d7 L4 n( G

  405. + w4 S! @+ U- n. l9 u$ b, d
  406.       if(Number > 0)( v. j4 e6 z( _* A8 s* _7 v$ `' d
  407.       {' K9 M+ F* B0 ]- j
  408.         if(can1TransmitFlag == false)5 \: j  ?6 m0 j/ i' t
  409.         {
    : B7 v( \0 t/ w% r3 ^' a
  410.           can1TransmitFlag = true;, d  W* L) T; ]
  411.   Z5 v: G0 B: n! K8 O
  412.           CanTxMsg canTxMsg = {0};
    2 [0 W5 C' P, N1 P. M
  413.           RingBuffer_Out(can1TxBuffer, &canTxMsg, sizeof(canTxMsg));
    ' H  v6 q6 N( J# B, j
  414.           CAN_Transmit(CAN1, &canTxMsg);
    9 s! `6 g' e0 f6 m: w
  415.         }
    : p" l0 _; x5 v( h, C6 |* p1 ?& [
  416.       }
    1 p) l: [& l7 m. X# K+ y* g

  417. 6 F! N% r! w$ x1 B# ?
  418.       return Number;
    1 s4 |" w5 L: L+ J
  419.     }
    $ G% L/ d4 P% i; P. Y
  420.   }
    2 Y5 H( o2 o0 p; n

  421. 3 w1 K7 q! z' q; b: z2 v
  422. #ifdef STM32F10X_CL0 G1 A: u! v3 p  S& z6 q; Q8 P
  423.   if(CANx == CAN2)9 j5 I  `* q% g* x. S8 B( w
  424.   {, N$ v6 e, z5 O, j/ X  H
  425.     if(can2InitFlag == true)3 c2 e7 h$ ?, |! v5 ^6 d
  426.     {
    . ^5 |8 ^3 U7 C0 S: G" Z# S! }
  427.       uint32_t available = RingBuffer_Avail(can2TxBuffer) / sizeof(CanTxMsg);
    & L% H) M3 p" ^, J# S8 d: f( L! r4 C

  428. 6 F5 M& t# H# N0 o* U" h( c, r  `
  429.       if(available > Number)
    5 J" O9 D; [% R( g/ c# {
  430.       {1 _, y4 J9 c, n% Z- Y4 I
  431.         Number = RingBuffer_In(can2TxBuffer, Message, sizeof(CanTxMsg) * Number) / sizeof(CanTxMsg);; b8 i+ S0 y# R7 o2 q: s0 o
  432.       }7 p# r1 k9 g2 T7 V" b' f# Y
  433.       else# p" n* G+ q" m$ V. A& t. |: r% j# F
  434.       {$ M# e# e& w& @  C
  435.         Number = RingBuffer_In(can2TxBuffer, Message, sizeof(CanTxMsg) * available) / sizeof(CanTxMsg);& ?7 L/ L" A9 ~4 B: _
  436.       }
    * _% L0 H4 W  V+ W; I8 X6 d
  437. * G/ C% L0 W1 h! {
  438.       if(Number > 0)
    ! i" p8 b5 `; F; f- y
  439.       {
    , i7 @" {5 a! z1 g4 C) P
  440.         if(can2TransmitFlag == false)
    4 U6 D. U* O* E5 G$ q  N4 m% z8 Y
  441.         {4 x' Y( K" ]' f: s6 d% I
  442.           can2TransmitFlag = true;! {3 ?$ U* P; h8 {
  443.   l+ ?* [- V3 \% H0 o. P; B
  444.           CanTxMsg canTxMsg = {0};# @6 t3 q7 ^0 N5 t7 H7 N
  445.           RingBuffer_Out(can2TxBuffer, &canTxMsg, sizeof(canTxMsg));
    " S* @) {* X: h8 u7 \' Y
  446.           CAN_Transmit(CAN2, &canTxMsg);
    3 ~2 b8 j+ Q6 a! f+ E/ s
  447.         }% U7 K7 m9 T" y$ L% B! W, w5 z
  448.       }! [3 M7 D1 y! G* E3 }2 O

  449. & ^( v0 G4 w; I0 s; u1 j% D, ~
  450.       return Number;
    + f3 G/ ?! O" O9 ~  ?. z8 G: Z
  451.     }
    ( u& g$ q! |/ b( X! U
  452.   }
    . I8 Z3 c; m$ a* L
  453. #endif /* STM32F10X_CL */
    ! {0 H# G& n! D4 Z& l) G) ^# Y8 A3 h2 h
  454. . L1 d2 R7 |  Z
  455.   return 0;
    9 J+ [/ Y, R6 O: w) \7 H. r# X  B8 A  X
  456. }
    : }1 k% K# J. V+ B  S. e6 ^
  457. $ h8 O( S' J; m$ p' V" c! v! {
  458. /**
    ; n& I5 c" V5 u# B- V
  459.   * @brief  CAN get receive message.4 C, @2 a, a" ], I" p8 E
  460.   * @param  [in] CANx:    Where x can be 1 or 2 to select the CAN peripheral.
    - S9 V+ ?( [+ B$ ~$ @2 H+ o6 A
  461.   * @param  [in] Message: To store the address of the receive message.
    6 L2 W$ V) s  {3 H8 `
  462.   * @param  [in] Number:  To read the number of the received message.  c5 G' F( V- |  K
  463.   * @return The number of message obtained.
    - H7 T1 n% x6 U( O
  464.   */
    * F; U5 N3 n- d9 i' Y/ o4 O
  465. uint32_t CAN_GetReceiveMessage(CAN_TypeDef *CANx, CanRxMsg *Message, uint32_t Number)
    # Z5 r5 m, M' n' W4 j5 s/ _
  466. {
    % V' a: Q0 w  e* C: C  R* W, ^$ Z
  467.   if(CANx == CAN1)
    3 F0 t! c+ p$ R. m3 V! q
  468.   {
    & L0 f2 J: @4 A& T' E
  469.     if(can1InitFlag == true)% Q6 n( J3 |: Q* |( j& x  b9 p
  470.     {
    , L1 ~" m% w; C5 c8 D% i
  471.       return RingBuffer_Out(can1RxBuffer, Message, sizeof(CanRxMsg) * Number) / sizeof(CanRxMsg);
    ! B# Z8 M4 t2 X- `) s$ D
  472.     }
    8 O1 T2 y" h8 ]9 ^7 W/ q; k+ D
  473.   }; Z7 u4 T! m# j1 c3 A

  474. & v+ U, m7 C. |( t$ D$ N
  475. #ifdef STM32F10X_CL7 S1 O7 I$ K/ Z; o) q
  476.   if(CANx == CAN2)
      ?- X" p: f# y' n! z6 e$ D
  477.   {4 V$ a. \) s" b" d7 |5 `
  478.     if(can2InitFlag == true)
    & o. l! C6 P% u3 o9 C
  479.     {/ G; L- s8 y* k9 L% ]
  480.       return RingBuffer_Out(can2RxBuffer, Message, sizeof(CanRxMsg) * Number) / sizeof(CanRxMsg);$ h6 w+ F2 K" I1 J- Y% W* ]- T/ f
  481.     }
    ; ?# X( U+ b+ H  u6 P! \! R
  482.   }
    / s/ U. ]& L, k9 t; y, Y& t
  483. #endif /* STM32F10X_CL */7 ^$ s' h" u- |2 Z) g  ?% N
  484. 9 Q3 g! w8 p7 P
  485.   return 0;
    2 i2 a& |- m: ?  q% [4 Q  Y# p
  486. }& L" z1 U7 r+ p' s4 @  u, E1 ?0 _
  487. 6 r1 ]6 G2 J* Z% K2 ^2 Q6 S
  488. /**
    % I  @; G' g7 U) o' o( v" T$ ^
  489.   * @brief  Get the size of the CAN transmit buffer used.
    & J2 W6 R4 w/ z1 Q4 X
  490.   * @param  [in] CANx: Where x can be 1 or 2 to select the CAN peripheral.* t# v  E( `  F/ P
  491.   * @return Used the size of the transmit buffer.
    ; A. x& \* m: F! P* V$ P
  492.   */
    ' X* i. e5 @9 ]7 V1 X+ n  `
  493. uint32_t CAN_GetUsedTransmitBufferSize(CAN_TypeDef *CANx)+ t( n" T" s& w9 G" A0 E) M% ?$ k9 H
  494. {' ]! R9 W/ u0 |1 f1 l5 Z
  495.   if(CANx == CAN1)) C' w/ ]: y7 N( o" w- w! ~
  496.   {
    7 @. W9 O: w5 l. w) P1 g
  497.     if(can1InitFlag == true)2 n: u/ l) O/ |2 t
  498.     {
    % P  s# Y/ m) ~1 [+ l5 R
  499.       return RingBuffer_Len(can1TxBuffer) / sizeof(CanTxMsg);
      o1 m. R1 j7 e. e8 M5 W
  500.     }1 E3 C* ^- h) H: o0 x
  501.   }5 v& {' w& w& n' V8 m  R& A. V
  502. . p; ~; z: a5 _( B3 l
  503. #ifdef STM32F10X_CL( @0 `- ?  L! e, Y8 b: v
  504.   if(CANx == CAN2)
    + y+ c8 Q7 ?8 z5 M; K8 ^$ G7 {
  505.   {, O: K4 K1 Q9 I& M8 R
  506.     if(can2InitFlag == true)# @% n. f; Z* O1 l
  507.     {
    5 |7 L7 T# a$ @' L$ A7 S
  508.       return RingBuffer_Len(can2TxBuffer) / sizeof(CanTxMsg);
    & R* {0 d1 \9 d: R, T: a& b  C- E
  509.     }
    " l) I  W5 P2 {
  510.   }
    , t, S! m9 M+ ?; `' \! [, r# ]. K
  511. #endif /* STM32F10X_CL */
    * i; w2 _5 r+ r1 N% y5 S8 E

  512. % |: R9 s8 t6 h5 p2 G5 P4 m
  513.   return 0;( m7 b0 ]+ p) B$ E$ s9 I& ]% o
  514. }
    ; d7 z- K6 n( M6 E/ n; N' j

  515. 0 P  Z. |4 _6 s7 q" n2 o
  516. /*** q  R9 h* q+ K( o: z4 @- Q
  517.   * @brief  Get the size of the CAN receive buffer used.
    ( ?0 p5 l1 U( K2 r  ^& n
  518.   * @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
  519.   * @return Used the size of the receive buffer.' {5 \( A4 n! s& l  D- @$ E1 I. |
  520.   */  H6 b: W4 o2 M+ |, Z
  521. uint32_t CAN_GetUsedReceiveBufferSize(CAN_TypeDef *CANx)# a( H  i( H6 I7 p; `
  522. {
    ) w8 F4 [6 f; P
  523.   if(CANx == CAN1)0 ?: A1 c! p4 H+ `
  524.   {
    ) D  u' N: k# v- ~/ |* r& u
  525.     if(can1InitFlag == true)
    & F! Y. z) T7 [$ L* p9 ~
  526.     {
    $ S: d& r9 K/ N% V* J1 _
  527.       return RingBuffer_Len(can1RxBuffer) / sizeof(CanRxMsg);
    & I5 @$ Z4 P: d: G, O
  528.     }
    " t5 B* B& x6 l& [- C/ I4 C
  529.   }3 ~3 L3 t3 i7 n1 N) p% S

  530. 4 F7 }3 F  Q  d' j9 A; C! h* l4 [6 `' F
  531. #ifdef STM32F10X_CL
    ) P0 W0 v# D$ U. X* l4 l2 d
  532.   if(CANx == CAN2)
    # q' i$ X0 l5 `. y. y( E) n
  533.   {/ z0 u' [% q3 A5 i
  534.     if(can2InitFlag == true)
    9 i% b7 I5 e; l" c+ w
  535.     {
    / R, v  |: \! s+ f; ^
  536.       return RingBuffer_Len(can2RxBuffer) / sizeof(CanRxMsg);$ _  @3 o: m7 A, O1 w
  537.     }) y- O% K" M" J- d; }
  538.   }! ?0 @9 Q* P* o
  539. #endif /* STM32F10X_CL */$ O$ T3 \- X; n6 u  b7 B& q
  540. % _' B0 N- v# H7 j4 `
  541.   return 0;" J0 U. r% s+ I' l& t! c
  542. }
    3 t+ ]' n8 _( q
  543. 5 b  w/ b$ r( b8 I
  544. /**5 g" o0 W7 K" K  [- q
  545.   * @brief  Get the size of the CAN transmit buffer unused.
    9 g/ [6 M+ E6 M) P6 T! Z
  546.   * @param  [in] CANx: Where x can be 1 or 2 to select the CAN peripheral.
    2 j0 k4 w& s8 c& h
  547.   * @return Unused the size of the transmit buffer.' s. c7 c) Y- j8 N0 s
  548.   */
    : ^5 D6 g  R' S0 D0 j- `# ^
  549. uint32_t CAN_GetUnusedTransmitBufferSize(CAN_TypeDef *CANx)5 t4 ?. _% ~6 y  p" b
  550. {7 m! y) `/ d+ H( f  U. I$ o( P
  551.   if(CANx == CAN1)
    6 P% B$ _9 Z5 l7 |
  552.   {  @7 ]9 q# v+ U7 I1 u- q
  553.     if(can1InitFlag == true)) j7 z  y8 l  n4 W9 X
  554.     {
    / M7 h0 E1 D; P$ Q& W& f
  555.       return RingBuffer_Avail(can1TxBuffer) / sizeof(CanTxMsg);
    - i1 I3 Y$ a4 [0 F: }! Z
  556.     }
    8 C* T; b+ {8 c/ K* B
  557.   }
    4 X' {3 M" B1 O
  558.   c/ q% e4 E6 B7 W$ s3 E7 }! \3 t
  559. #ifdef STM32F10X_CL& B1 s# ?9 _0 Z/ w. Z
  560.   if(CANx == CAN2)7 g8 U: |; a8 H5 K: {% ]
  561.   {' |/ J5 ^$ K9 X7 J
  562.     if(can2InitFlag == true)
    - B4 e+ d# e) b7 {3 ^' E/ W
  563.     {
    ! R" S7 ?- h' y
  564.       return RingBuffer_Avail(can2TxBuffer) / sizeof(CanTxMsg);
    + S8 N( a1 }5 m: J$ E! P# I2 Q8 f
  565.     }
    ( H" }! M( X" _% A9 |, R
  566.   }
    ' ^; J5 q, I8 t
  567. #endif /* STM32F10X_CL */
    ! w+ d! G# c% ^7 Z: }% g$ Z

  568. 1 S9 ]* d1 g. t+ _  h; e
  569.   return 0;1 Z, e$ E( ^: T4 }
  570. }
      b  a: C  h, ~( h8 c% ~4 l

  571. ( t: f( U: ^$ G- q; w1 t# w
  572. /**3 z- Q: \6 }& V4 N5 l3 h0 d: `/ W* q
  573.   * @brief  Get the size of the CAN receive buffer unused.
    7 A3 z" S3 Z$ v
  574.   * @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
  575.   * @return Unused the size of the receive buffer.( S. }  L7 Y; D( H( j0 \
  576.   */  |5 A5 ^; u! w
  577. uint32_t CAN_GetUnusedReceiveBufferSize(CAN_TypeDef *CANx)) }! B9 m# ^# i5 J
  578. {5 Z0 c0 }8 n7 s. n
  579.   if(CANx == CAN1)
    4 F$ p& @+ E6 v( V8 ^/ L: z; M  M2 Q
  580.   {. O* S. ]& E" q" Q8 h# S# `
  581.     if(can1InitFlag == true)
    ! X1 X; J/ y" h
  582.     {
    3 U$ A) X+ @8 p, j
  583.       return RingBuffer_Avail(can1RxBuffer) / sizeof(CanRxMsg);
    1 h) S/ ?" p- p7 C# ^
  584.     }7 X4 i  B# A' t2 G- {7 j
  585.   }8 p1 [  Y. Y6 x9 S1 x; N4 c

  586. 7 S/ e( Q* L9 f& s: |
  587. #ifdef STM32F10X_CL0 B1 W$ Q: b8 x: I" _
  588.   if(CANx == CAN2)
    0 Y( Y3 ~% f) V8 u* c
  589.   {
    6 h. o+ X" W3 A; ?$ J- |
  590.     if(can2InitFlag == true)
    0 t# B  t7 F7 L) T$ Q8 \# V9 V
  591.     {
    - P: T- n! J0 r6 R
  592.       return RingBuffer_Avail(can2RxBuffer) / sizeof(CanRxMsg);( D7 P' }0 g' ]
  593.     }% {: B1 ~4 d; ~# _9 K
  594.   }% z& ]" v) H1 a5 f6 m+ ]2 E
  595. #endif /* STM32F10X_CL */, E# u( V; j; k+ y  C4 o

  596. 0 g* N. H( q$ D. H0 J+ x' F
  597.   return 0;
    . W+ X! u' L7 d) R  |+ L2 |' Z2 e9 z
  598. }
    . D# D2 I. ?6 P2 R5 I( E' {6 O

  599. ) Q: r. G$ N, a4 m( A+ x
  600. /**! f( M6 }* x2 j$ ]7 ?( y/ ?
  601.   * @brief  Is the CAN transmit buffer empty?
    ! p! [% M3 ^" N$ Q+ }
  602.   * @param  [in] CANx: Where x can be 1 or 2 to select the CAN peripheral.. M" w! h  \0 a6 V  y. a5 m
  603.   * @retval true:      The transmit buffer is empty.
    : X  T# V) {$ ^) C2 E$ K
  604.   * @retval false:     The transmit buffer is not empty.
    ! x9 B# w$ R+ v  y5 c* {1 I/ T6 P4 o
  605.   *// M. K) o% Q6 y8 e3 Z( @% T& e- n& s! o
  606. bool CAN_IsTransmitBufferEmpty(CAN_TypeDef *CANx)& g* O' A/ j' Y$ S$ C
  607. {
    , O' u4 j0 m9 M. z
  608.   if(CANx == CAN1)2 c$ H; o. Z, L' z6 T4 m' C
  609.   {
    ; \. Y+ \0 J$ `( r' l. ?% f" z
  610.     if(can1InitFlag == true)% G/ Z$ [* u5 @5 G/ E8 U
  611.     {
    4 t" j* e8 p& m3 N
  612.       return !(RingBuffer_Len(can1TxBuffer) / sizeof(CanTxMsg));+ O- r  v5 B* O7 g
  613.     }. G+ ~8 S) E6 v' I' G; r0 j; I. O. {
  614.   }) V7 T: O5 g: s: A- B
  615. 4 v) A9 D4 t- e6 b5 a5 w! O
  616. #ifdef STM32F10X_CL+ C4 _7 o1 K4 u1 T/ _- C1 J$ f
  617.   if(CANx == CAN2)# R5 R1 Q1 g6 |3 c
  618.   {& \3 K7 Z  D, z$ Z. Y
  619.     if(can2InitFlag == true)+ ~! I9 ]7 \* U' \, M+ F- c  p1 E" F
  620.     {
    & `2 d- h+ w9 R) R
  621.       return !(RingBuffer_Len(can2TxBuffer) / sizeof(CanTxMsg));
    ; E: N! Z% f0 c5 \7 i
  622.     }- ?' v! F1 T# f* l6 w
  623.   }
    1 ]0 d0 S% Y; G" t) w
  624. #endif /* STM32F10X_CL */
    / b1 ?: H# X( o8 _: ?5 P* q

  625. 8 K6 a2 L( A% M" F
  626.   return false;
    2 x! p9 P. y% Z& D9 i8 j" `- t
  627. }, W; A: [& `) K  r) @

  628. : U& p% T  f4 X9 `$ C* H7 `
  629. /**# F" f! K5 h* x8 P5 l
  630.   * @brief  Is the CAN receive buffer empty?
    1 F8 d8 [. l8 S6 v+ O
  631.   * @param  [in] CANx: Where x can be 1 or 2 to select the CAN peripheral.+ d) R3 w9 n7 X$ F' B; l
  632.   * @retval true:      The receive buffer is empty.
    0 a2 ^! z5 K+ K9 L- l* G/ z
  633.   * @retval false:     The receive buffer is not empty.9 W% `* j4 F; @3 N" H. [" v* @' E. t) }
  634.   */
    ; K" x" T0 M4 e1 c
  635. bool CAN_IsReceiveBufferEmpty(CAN_TypeDef *CANx)3 i, c& z/ d$ c0 Y& C
  636. {
      a* u* _, m! d& O  c( I$ a) Z
  637.   if(CANx == CAN1)
    $ |/ R9 b( u1 e
  638.   {/ \; E7 w  A7 L7 G: b
  639.     if(can1InitFlag == true)9 C- S3 e- y' h" c0 t4 p
  640.     {
    1 c* L8 d" d# g5 b
  641.       return !(RingBuffer_Len(can1RxBuffer) / sizeof(CanRxMsg));& x' G& }- R0 T# Q/ E
  642.     }; O& z' G& H, g$ l# d
  643.   }
    4 [: R( W* s. a" E

  644.   A9 N4 u% {6 S( b) Q- Y/ i
  645. #ifdef STM32F10X_CL
    " N7 S1 f+ f+ f7 r2 q
  646.   if(CANx == CAN2)
    $ B+ O) w. @3 z6 t( x& H2 o" T
  647.   {2 q) A$ h0 Z( n
  648.     if(can2InitFlag == true)' W; y2 ]6 u4 j9 f0 z
  649.     {
    % L% A' h  G/ F; l, b
  650.       return !(RingBuffer_Len(can2RxBuffer) / sizeof(CanRxMsg));
    $ Q, V4 N% _, U+ N0 _' }. X
  651.     }
    % {; A- ~% n; R5 R3 _( ?  L
  652.   }* s0 _: l! N9 {
  653. #endif /* STM32F10X_CL */* Q/ K/ b1 j! {' w4 i/ h
  654. ' E, D- I3 i0 u# v$ v0 T/ o
  655.   return false;
    ; ^! |+ W6 b1 ?6 |! }, L0 W; V! r; @
  656. }4 r5 T; R' F+ P3 l/ u1 q% V
  657. 6 p" @0 M$ A5 H; V0 O4 i( S) m' u
  658. /**
    # ~6 [# q& |. A; b6 ?5 Q: X
  659.   * @brief  Is the CAN transmit buffer full?
    2 n- s$ H2 d* g( t
  660.   * @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
  661.   * @retval true:      The transmit buffer is full.
    1 n6 K0 u% i; F- W* M
  662.   * @retval false:     The transmit buffer is not full.9 R4 J% S$ X/ u+ V6 j) T1 [
  663.   */7 `6 C( }0 z- x7 m/ w/ W
  664. bool CAN_IsTransmitBufferFull(CAN_TypeDef *CANx)% U" E& o; T6 p+ z/ h
  665. {
    * V- U& t% D% s$ _) J
  666.   if(CANx == CAN1)1 P- p; q2 r- @
  667.   {. v3 ~/ Z& R- d: p+ h" `
  668.     if(can1InitFlag == true)) u* X3 i. C  u' h
  669.     {$ q% c2 U6 L3 J2 u# ~; B
  670.       return !(RingBuffer_Avail(can1TxBuffer) / sizeof(CanTxMsg));9 g& k8 Y4 F2 \) T
  671.     }
    5 D! b# S+ I9 f+ E
  672.   }+ ?7 k4 j$ g( E* {8 q& ]; C
  673. 9 S1 l$ E, a+ O1 g' m1 @
  674. #ifdef STM32F10X_CL" ~" Y7 T, l' d7 \8 n( ^
  675.   if(CANx == CAN2)
    4 H+ u$ {; A* N% _- u$ ]1 s
  676.   {9 _- V8 [! @0 s! l' z1 ?
  677.     if(can2InitFlag == true)
    ( ]' ^. k. Y. P, w- X
  678.     {
    5 V$ ?/ o  M4 h9 C. m* q
  679.       return !(RingBuffer_Avail(can2TxBuffer) / sizeof(CanTxMsg));+ A' S# Z$ W) Z
  680.     }
    , f, s6 }' s. ?6 v8 y5 [; m
  681.   }! F. x8 G* |/ R0 K9 g/ x
  682. #endif /* STM32F10X_CL */
    5 D. B# N& \/ W: _, n

  683. $ _' y# X" H1 k- J: N
  684.   return false;7 v" S" M" D* w. P
  685. }
    9 A2 {( Q) s. D' f
  686. & `' p7 a9 Z8 w8 q
  687. /**
    9 E. j* w- e9 }  c& w
  688.   * @brief  Is the CAN receive buffer full?
    , i! {0 V. r9 J) M* n$ D+ l
  689.   * @param  [in] CANx: Where x can be 1 or 2 to select the CAN peripheral./ l. N9 ]% E% p% c
  690.   * @retval true:      The receive buffer is full.- h3 G4 B, H$ J: J! ~4 A
  691.   * @retval false:     The receive buffer is not full.
    - V% o% V7 G' M8 C. g! N
  692.   */" h3 L$ d+ `% Z* p% a) ?
  693. bool CAN_IsReceiveBufferFull(CAN_TypeDef *CANx)' E7 H7 L% I  `# `) m9 i* f
  694. {' T4 p, i& C& |
  695.   if(CANx == CAN1)
    3 C6 r5 w5 x1 O2 O3 `
  696.   {
    . [- H" P( H  _8 k: H+ F& G4 [  N
  697.     if(can1InitFlag == true): O' z3 M1 Y" v3 l2 ~
  698.     {
    8 j$ V- V, y9 Z9 ?
  699.       return !(RingBuffer_Avail(can1RxBuffer) / sizeof(CanRxMsg));
    5 T0 E: j- s; j
  700.     }8 k" Y# x4 l- C
  701.   }. [8 t6 K( k+ `
  702. , {8 u. E# f% m8 E
  703. #ifdef STM32F10X_CL
    / y7 \* y) B( i+ a
  704.   if(CANx == CAN2)
    9 F' Z! M" s1 J  g7 D! V. ~
  705.   {% H0 B* t. M0 K5 f) l" |
  706.     if(can2InitFlag == true)
    % H, v; S9 ~, ?9 h+ n
  707.     {3 K9 {8 W9 m3 d& {. @0 [
  708.       return !(RingBuffer_Avail(can2RxBuffer) / sizeof(CanRxMsg));
    , Q$ `: {5 F; o# J' p  h  L6 f
  709.     }
    ; |$ G% O9 r/ b. g
  710.   }
    & _) r# ~5 J7 E
  711. #endif /* STM32F10X_CL */
    5 g9 w8 p& _* }  Z; M& s; U

  712. / `* D/ ^/ T4 ~* }
  713.   return false;
    + p; Z4 z+ C* ^" W5 Y) d1 A
  714. }& G1 {  l) X: K- s: y  m

  715. 8 f% p% P4 P* q! N5 S0 F8 q
  716. /**
    5 k+ O  s5 [# ^2 y3 \4 V' g
  717.   * @brief  Clear the CAN transmit buffer.* c% f# I9 T5 L* [
  718.   * @param  [in] CANx: Where x can be 1 or 2 to select the CAN peripheral.
    : d8 C6 S& W& ]  z4 Z
  719.   * @return None.- f% s. r" P8 s  [* u1 O3 W/ \
  720.   */
    2 _* O; e8 L, J* C4 P: E
  721. void CAN_ClearTransmitBuffer(CAN_TypeDef *CANx)3 v- t& d0 c1 Z  X
  722. {
    + v) R5 i, p6 _% {; L
  723.   if(CANx == CAN1)( s# g: b7 `1 v' m: A" o$ [
  724.   {
    + K, N1 |: C- \; }; L4 g  v
  725.     if(can1InitFlag == true). {5 N" G7 f- _' k( P0 s# S
  726.     {
    ! D) i6 `& \) J& R) b% V/ F
  727.       RingBuffer_Reset(can1TxBuffer);" q% V0 S& t* Q& D% z
  728.     }
    1 t$ g. r) I$ b% G  z) @+ f1 c. `
  729.   }
    ; Q! W8 {/ b! `$ a  y2 V' c/ n

  730. 5 {& w3 J1 V" k
  731. #ifdef STM32F10X_CL' H: h" b" Q2 G$ D7 z' j
  732.   if(CANx == CAN2)# }% D" ?& o4 n% @/ _1 l" b
  733.   {5 |/ z' w$ E7 h/ |5 E7 `
  734.     if(can2InitFlag == true)
    . x/ A% U& P: Q9 X/ L+ z  H
  735.     {
    1 d/ h) D' N1 d2 {
  736.       RingBuffer_Reset(can2TxBuffer);3 y& E7 i  K  g2 P8 h) U
  737.     }
    4 ]# D# r5 P) q; {
  738.   }+ M, H8 D$ n8 F- x* O
  739. #endif /* STM32F10X_CL */
    9 j; a- `* H7 E) K1 d$ [/ P
  740. }+ W4 Z2 v# [4 P% M: P

  741. 9 B4 L. F, K/ R% @
  742. /**
    % s4 A1 l: b% m! F: P% x3 M
  743.   * @brief  Clear the CAN receive buffer.
    % O) t% B2 |2 G3 S3 l+ s, b
  744.   * @param  [in] CANx: Where x can be 1 or 2 to select the CAN peripheral.
    / i. Y2 ~9 U* Q( ]" ]
  745.   * @return None.
    % r, n& w$ e* g+ w! N6 V
  746.   */
    8 `1 M6 p7 Y' Y& p! _9 U6 q; E
  747. void CAN_ClearReceiveBuffer(CAN_TypeDef *CANx)* o8 L7 H# {- D
  748. {, _8 j9 I/ b- {. b' W
  749.   if(CANx == CAN1)4 Q& k5 K( Z6 K9 a
  750.   {4 [. a4 Y7 ~8 @
  751.     if(can1InitFlag == true)0 ^( a7 B/ ?2 v# g9 o; Z  q1 c
  752.     {) N$ P) v4 F* ]( a( G7 A" [0 V
  753.       RingBuffer_Reset(can1RxBuffer);* M) f9 F* ]  ^  O" E4 A
  754.     }0 ?' f  c7 r5 [: L6 U) t! m
  755.   }: u' E" ?1 v6 ^) m& P* h
  756. : m' x( I, c' K9 f
  757. #ifdef STM32F10X_CL
    ) ~2 p. H* v- Z5 M/ s
  758.   if(CANx == CAN2)) S  C: i% A. G- A) u( q
  759.   {
    & D0 w9 D/ Q. _. L. X; H- e. r9 @
  760.     if(can2InitFlag == true)
    $ B1 D/ d' c* k
  761.     {
    / g( \; C- J5 J" g
  762.       RingBuffer_Reset(can2RxBuffer);" I9 z/ U3 M- _! v% ^
  763.     }
    7 r2 _" O2 i' ?& p
  764.   }
    ; z% B4 c% z2 B1 Z/ M' D5 T0 j
  765. #endif /* STM32F10X_CL */& a, z! c  h5 f. }" C9 C% w9 `
  766. }- l6 S; f2 o+ _% F% @1 z7 H
  767. 3 b9 f( |# ?8 _. K& |
  768. /**0 I3 M8 Z2 m7 i5 A
  769.   * @brief  Is the CAN transmit a message?6 M% Y0 k2 [- m
  770.   * @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
  771.   * @retval true:      Is transmit a message.$ f! z( s7 M  E, m3 m
  772.   * @retval false:     Not transmit a message.
    3 ?# |7 \% |/ }' n
  773.   */
    2 n4 Q, y5 v1 V/ h1 C
  774. bool CAN_IsTransmitMessage(CAN_TypeDef *CANx)  D0 a. J! N) u( i) k' t3 L+ E$ X
  775. {5 d+ s8 [2 [  |2 a, g
  776.   if(CANx == CAN1)- b# c/ Y$ W0 [4 N' |" g
  777.   {" p( s( L: o/ @2 V. A
  778.     if(can1InitFlag == true)2 G  u, Z. O, y& u3 m
  779.     {
    ) x" x4 n) v3 [- M
  780.       return can1TransmitFlag;
    - R+ t4 {" s, s+ E' S3 M# e! C1 O
  781.     }
    9 ?& g: K, p& k: L
  782.   }
    8 M* U3 l+ L: t' ^4 \
  783. ! M* o) r5 j7 A. a; @/ q  q& a0 @
  784. #ifdef STM32F10X_CL6 v) K% N8 F; s& M: u0 o
  785.   if(CANx == CAN2)
    : O% b* {0 C) L7 e$ f  y6 \- u
  786.   {$ |$ l- ^, g$ u( c2 b8 Q  w
  787.     if(can2InitFlag == true)
    / o5 z' F: X; f" r- I7 L, w; r
  788.     {
    % c6 A( c4 b3 ^  O
  789.       return can2TransmitFlag;
    $ h7 X: ~% q: W
  790.     }
    ( X* }' Y' m( m9 E( J
  791.   }
    , }% z3 j, p4 C7 ]7 E3 Y
  792. #endif /* STM32F10X_CL */
      }- @# Z% ]) Y9 v( x- ?2 |' i

  793. 3 A9 E% K/ y) p. @/ `; S
  794.   return false;
    3 [0 \4 o; B  q- t$ r
  795. }+ }3 U! Y/ f  z' Z2 i* r" M& A

  796. 2 B8 D' U0 v8 ]/ z5 ?4 f
  797. /**( u7 I( u9 |/ U! P5 X; a0 Z
  798.   * @brief  This function handles CAN1 TX Handler.8 G$ T2 v! N/ y
  799.   * @param  None.; u( h0 O& j* a: [, x) k: S( O
  800.   * @return None.; B1 n4 @2 R( ?; N1 e
  801.   */+ j8 J. w" W7 g+ [' O% ]# i% N2 z
  802. #ifdef STM32F10X_CL* y- y6 c) J9 Q( o- V8 |4 r
  803. void CAN1_TX_IRQHandler(void)2 H- k$ m+ u; [! O& {
  804. #else
    % K# S; G6 u9 m
  805. void USB_HP_CAN1_TX_IRQHandler(void)7 m# j/ J+ |! ?# r  V1 S
  806. #endif /* STM32F10X_CL */
    0 y9 Y2 ]' G, z7 O5 T. y% l
  807. {
    , _, J! |0 K% m  j" a0 ^
  808.   if(CAN_GetITStatus(CAN1, CAN_IT_TME) != RESET)
    * M' p7 q) r! d! F
  809.   {6 e, T8 e& u& z/ f2 [+ G3 V
  810.     CAN_ClearITPendingBit(CAN1, CAN_IT_TME);# Y- R8 d7 q8 [

  811. . s' W& q4 A$ }7 x! _
  812.     CanTxMsg canTxMsg = {0};
    ' Y  ?3 ]1 j& Y# q' [
  813.     uint8_t  number   = RingBuffer_Out(can1TxBuffer, &canTxMsg, sizeof(canTxMsg)) / sizeof(canTxMsg);
    4 ~0 c5 J) a2 }9 [1 @/ o# I: G8 [
  814. : h2 p' S' O  r" H7 i
  815.     if(number > 0)
    4 R" }3 b. u, F  K7 I$ n
  816.     {  Z1 A- k8 O) |7 ?
  817.       CAN_Transmit(CAN1, &canTxMsg);6 S) x4 }2 w, D4 B" ~8 `/ O: |
  818.     }1 ^" J5 c# Y5 D3 W" v
  819.     else( Z7 f3 C% W* X1 @
  820.     {
    1 n: r- Y: F; A; P! E/ Q9 Z3 @
  821.       can1TransmitFlag = false;7 d6 t2 l( z( f1 i6 B  X  `, j

  822. 5 w3 J- V8 Z1 D, k
  823.       if(can1TransmitFinishCallback != 0). {7 J% b" y6 e! J
  824.       {+ R' _3 x6 ~6 c- I1 \
  825.         can1TransmitFinishCallback();8 i8 y9 D0 n* G" t% h
  826.       }7 J, b6 v/ N. E+ X
  827.     }
    : B% P! r4 g* F! ^1 U1 Q5 b# P
  828.   }& |1 N% E' l" p4 B  X9 k! M
  829. }
    : y% L1 j' m8 o) j2 h
  830. - K( O+ C: [! G$ ]
  831. /**
    : ?2 @9 x; M- U- K/ E$ B. r0 S- P
  832.   * @brief  This function handles CAN1 RX0 Handler.
    - U' H) Y) R; B; s
  833.   * @param  None.; d8 Y4 g& |0 K* k
  834.   * @return None.$ {2 i: o% _) z; }: E* o
  835.   */
    " D( H6 S& @% t. |" w  e
  836. #ifdef STM32F10X_CL! B6 S" A: z, x2 V5 o$ Y
  837. void CAN1_RX0_IRQHandler(void)) M2 q: u# s: n) m/ K
  838. #else( k" p# O* s' w  a0 r) ~
  839. void USB_LP_CAN1_RX0_IRQHandler(void)
      C9 G# L( m+ X! r/ a
  840. #endif /* STM32F10X_CL */
    % [$ @: R6 ^) |# u; U& m1 [
  841. {2 g9 ^$ L3 N2 O* ?" b5 K" [0 d- k
  842.   if(CAN_GetITStatus(CAN1, CAN_IT_FMP0) != RESET)
    ! i% h0 ]8 M$ Q8 K$ M8 u5 m3 d  [
  843.   {/ b. Y5 O# J3 Q5 h9 r6 s
  844.     CAN_ClearITPendingBit(CAN1, CAN_IT_FMP0);
    7 V7 T" ?5 o/ V
  845. 8 ~! F, |( m: l' B, V+ k
  846.     CanRxMsg canRxMsg = {0};
    7 z$ c+ w7 I* h2 l/ _
  847.     CAN_Receive(CAN1, CAN_FIFO0, &canRxMsg);
    % |, v; |5 H  {# u" I/ b6 }( O
  848. ; o- O: ^$ l  s# [- c  @* o5 R
  849.     if(RingBuffer_Avail(can1RxBuffer) / sizeof(CanRxMsg) > 0)
    9 P+ Z3 q: y5 D1 H9 @% ]
  850.     {' ~" d0 o! z0 T- T4 H
  851.       RingBuffer_In(can1RxBuffer, &canRxMsg, sizeof(canRxMsg));
    $ z% F1 @& I1 K( e) U
  852.     }
    6 ^6 @5 }: C" }2 Q7 y- f
  853. : U) q0 D6 N5 m/ _- E: \  n
  854.     if(can1ReceiveFinishCallback != 0)
    ; R) \( h. f3 M' L0 v8 u/ e
  855.     {8 h. l9 {+ _/ }0 r/ T
  856.       can1ReceiveFinishCallback();
    # J- K8 ^7 u8 w
  857.     }7 p- Z' ?: {# ]1 a
  858.   }
    2 A% h% u. f' R$ t2 `# |! y
  859. }
    # J( O$ `' z7 P1 s  z+ n$ f
  860. + d# T3 Y! F9 _! T" C$ k
  861. #ifdef STM32F10X_CL
    1 ~0 z: |$ m8 Z) L9 O+ x3 R- O
  862. /**$ u0 ~% W- n! f6 P! m9 |
  863.   * @brief  This function handles CAN2 TX Handler.
    , q9 @/ k" A! m" V
  864.   * @param  None./ `- I/ u# g) ?! ^- `
  865.   * @return None.
    ) X8 [# i  J3 u
  866.   */7 x4 q2 Z4 Z  Z  \
  867. void CAN2_TX_IRQHandler(void)' b$ V/ I9 |- E* H
  868. {0 X) w2 m$ W0 A, w* Y: A
  869.   if(CAN_GetITStatus(CAN2, CAN_IT_TME) != RESET). S9 {. S$ u8 x! Y0 t) j7 m
  870.   {/ |. Y7 P: g) l
  871.     CAN_ClearITPendingBit(CAN2, CAN_IT_TME);3 d& n3 Y. X% q7 k% m2 E6 u5 {

  872. 6 d/ V4 O0 }" d) f& E, K* p
  873.     CanTxMsg canTxMsg = {0};; n7 H* R$ ]9 F( m
  874.     uint8_t  number   = RingBuffer_Out(can2TxBuffer, &canTxMsg, sizeof(canTxMsg)) / sizeof(canTxMsg);
    . I, C' q) s0 p

  875. & ]8 x7 ]# }( s% f4 M
  876.     if(number > 0)
    % ?1 G9 @, T8 `5 w$ H1 t7 h
  877.     {
    / a) u/ p! ?  s! Q9 d
  878.       CAN_Transmit(CAN2, &canTxMsg);
    ' p( C% m. C; q. Y" e4 |
  879.     }
    . J" E$ ^, U5 X* f& u7 C
  880.     else
    & V* [* U3 s7 I8 a- }4 i, M2 E
  881.     {$ c: g$ A5 j, t
  882.       can2TransmitFlag = false;) U4 b" k  D/ z
  883. ' f8 h: x$ c; y6 P' n
  884.       if(can2TransmitFinishCallback != 0)
    & j+ q( C6 A" _! X) a# `+ J* m# K
  885.       {) K* E( \/ z- M2 t* b3 T1 Y  ~
  886.         can2TransmitFinishCallback();5 @4 ]7 q" _  t8 u; f9 W# q$ v
  887.       }
    ' b# E+ Q& |: h/ x) U
  888.     }! S: b+ p. n8 f; e& l/ ~; L
  889.   }
    / n. U) \4 R3 c
  890. }& s" F1 C) p" ~' c0 C+ Z* S. p
  891. 7 Y( A5 J" [7 E
  892. /**) ^: g* P1 z# r" [  [$ Y* x
  893.   * @brief  This function handles CAN2 RX0 Handler.4 E7 N& V( r7 S
  894.   * @param  None.  ?/ K5 S1 s' k/ A2 I0 P" s
  895.   * @return None.2 `  l$ c1 D: }5 u0 P& e( i& U
  896.   */
    ' j) r$ t$ v+ J8 Q; p# I) i, X% s
  897. void CAN2_RX0_IRQHandler(void)' S1 q0 R9 T/ q  d* \% O- p" H
  898. {% W' m# {  C% A# b
  899.   if(CAN_GetITStatus(CAN2, CAN_IT_FMP0) != RESET)6 B" r- N5 H7 o
  900.   {4 g7 J  T* a- }5 O4 E
  901.     CAN_ClearITPendingBit(CAN2, CAN_IT_FMP0);) |# j& Y9 o$ F3 [: Z; k

  902. $ S; i/ c9 i0 b3 k) P. Q
  903.     CanRxMsg canRxMsg = {0};
    ; o# G9 I5 U: ?6 e1 O" p  E9 L0 W
  904.     CAN_Receive(CAN2, CAN_FIFO0, &canRxMsg);
    ( P5 d- Y9 y3 g) x% Z: J/ R

  905. 2 F' [8 w& _- L* ]# t1 \! h, K
  906.     if(RingBuffer_Avail(can2RxBuffer) / sizeof(CanRxMsg) > 0)! |0 R" l- d, j( V; K
  907.     {8 L; b0 }" u+ ^
  908.       RingBuffer_In(can2RxBuffer, &canRxMsg, sizeof(canRxMsg));
    4 N2 z/ {- g) R/ h. f
  909.     }. B; I5 m( O. x' U) }, w, |; H' B

  910. 1 g2 u- p7 z$ s, D6 q4 @
  911.     if(can2ReceiveFinishCallback != 0)5 H7 p6 u5 M% ?4 C2 m' V, i
  912.     {0 U/ Z7 I4 L/ {0 V, J
  913.       can2ReceiveFinishCallback();
    9 U. D' F/ q& a" @# E+ s+ |
  914.     }  q4 t& a$ w+ u! i5 l
  915.   }$ `* {* ?" ^/ F' _0 \/ l; @9 S3 f3 p
  916. }( n) {  |4 ?' `: u3 W
  917. #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
  1. /**
    ! a( [" [6 ^2 k* ]0 \# f
  2.   ******************************************************************************
    ' o2 D5 p! ?6 c2 I0 n
  3.   * @file    main.h+ B* h+ H: \5 S2 ^* g& n
  4.   * @author  XinLi
    7 F9 E! d) F  a; T6 m6 B* s
  5.   * @version v1.0
    * @' f# Y( l* J" ^# }) o5 o9 e% L9 H
  6.   * @date    24-June-2018" a" e* |: _/ h0 r8 d" H
  7.   * @brief   Header file for main.c module.
    / u5 i4 @& [" l. W0 Y9 G
  8.   ******************************************************************************
    % t; @" t$ Z' c1 ^, a
  9.   * @attention9 E" v. U, y8 _
  10.   *
    9 N# @/ K$ E6 D
  11.   * <h2><center>Copyright © 2018 XinLi</center></h2>0 Q: Y/ H9 R$ X. K; h
  12.   *% v2 T; b0 i- f/ A1 O
  13.   * This program is free software: you can redistribute it and/or modify9 h) U" ]3 f  k; F% C
  14.   * it under the terms of the GNU General Public License as published by# }9 W/ Z9 v8 Y0 e  U6 h- Y' j( A
  15.   * the Free Software Foundation, either version 3 of the License, or
    " [6 C* l: a+ U9 I4 a
  16.   * (at your option) any later version.( o( G& r' p* H  k/ J% V( g
  17.   *; r* H3 {1 c0 I( ~2 d, V
  18.   * This program is distributed in the hope that it will be useful,
    , N! t* t5 Q, w5 Q
  19.   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    9 P! `* T* M1 U) Z4 ^- H
  20.   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    8 o! j: z& Q9 M. Q. c( v
  21.   * GNU General Public License for more details.4 t3 z8 ]5 P+ I7 y- C$ K; r
  22.   *  k) g7 L! a+ J7 k+ U
  23.   * You should have received a copy of the GNU General Public License
    " o" G* R1 y& b8 H; G3 ~
  24.   * 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
  25.   *7 L  d' E: H2 A
  26.   ******************************************************************************
    1 S' W: e; H( Q% ]- K6 i5 H# N, q
  27.   */
    + I) i$ L- L$ i7 P; b
  28. + r+ E# H8 _/ X) y0 h8 l, G
  29. #ifndef __MAIN_H+ e0 s; _  i4 E5 _  U6 _/ @% R3 i8 H
  30. #define __MAIN_H7 n4 E3 I8 y) a7 ^

  31. % p' n! i. k( a$ x' R; \
  32. #ifdef __cplusplus9 F- z. h1 X0 W7 `& u
  33. extern "C" {
    / z0 \) r' G: W* ]% M2 I& ]. N0 P
  34. #endif5 |/ L' |* q# ]6 _' D

  35. - p9 e% j! y3 i" i. ^. X
  36. /* Header includes -----------------------------------------------------------*/% V, e( x3 D  s1 {8 u
  37. #include "stm32f10x.h"
    3 Q  S4 l8 n- h
  38. , [# |- F6 G# Z/ W. X  ~
  39. /* Macro definitions ---------------------------------------------------------*/
    " X% j1 R! K1 y/ Z
  40. /* Type definitions ----------------------------------------------------------*/
    6 ~) n( L  u* C0 \
  41. /* Variable declarations -----------------------------------------------------*/
    / X# }, t0 N# ?, U
  42. /* Variable definitions ------------------------------------------------------*/
    + R3 X' X5 p* _% C! c/ |; e
  43. /* Function declarations -----------------------------------------------------*/- M& O2 p# Q9 m; z
  44. /* Function definitions ------------------------------------------------------*/
    - I; a3 V- t1 u/ B
  45. " H+ n5 C; ?/ e% W0 R9 P; E  j
  46. #ifdef __cplusplus; O% G5 a0 p/ q
  47. }( Z. w7 g7 Q6 x+ J3 L$ A4 [. e7 a
  48. #endif
    9 f. w4 U% w! w7 ^8 y$ T

  49. # U. Y1 O7 w( ~" d9 T
  50. #endif /* __MAIN_H */
复制代码

) X* g( `* W6 |/ n; q2 [" j3 L  m4 O; r! W2 r; t0 ?
main.c 文件
! \  G$ V$ u: N( l" {( G
  1. /**5 ^: A( D  \+ Y/ y8 y% J* s  V
  2.   ******************************************************************************
    # g0 o5 ~1 n" H3 O! d# j
  3.   * @file    main.c
    , s% }/ Y  o! z( ~- b# W
  4.   * @author  XinLi
    ) I( D: d6 h, a5 f* E
  5.   * @version v1.0
    8 b- t% j, X! x- N4 K
  6.   * @date    24-June-2018
    $ i: S* M0 p5 d8 o
  7.   * @brief   Main program body.8 C+ G9 ^. a1 }' e+ Q! ?
  8.   ******************************************************************************/ X  b3 s, O1 F8 Y$ `: B$ V- Y9 R: {
  9.   * @attention7 {: A$ A- U( [) K) C, A. C
  10.   *9 o4 n& b, M. D# Q: B
  11.   * <h2><center>Copyright © 2018 XinLi</center></h2># C- x) N- w% a; N0 a
  12.   *
    % A, k1 r# i; {5 J+ e0 ]5 S
  13.   * This program is free software: you can redistribute it and/or modify1 ^# h2 K. H, Q, A" B9 F
  14.   * it under the terms of the GNU General Public License as published by
    6 C  F9 o. b+ q. R: n1 B9 Y- a
  15.   * the Free Software Foundation, either version 3 of the License, or% t  S9 ?( o( \
  16.   * (at your option) any later version.9 x& d2 ?  \7 Y8 i/ H
  17.   *
    1 F( t' @+ T+ t& ]
  18.   * This program is distributed in the hope that it will be useful,# D, P% j5 ~9 L# j/ V: m
  19.   * but WITHOUT ANY WARRANTY; without even the implied warranty of: g0 x( p8 s8 v, A
  20.   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the) f6 P; O0 j8 U, U. E: l
  21.   * GNU General Public License for more details.
    4 B7 J! ^: p& Z3 n: C
  22.   *
    " @' z$ o  |+ k8 V
  23.   * You should have received a copy of the GNU General Public License
    1 H4 w8 J* h/ n% y5 s! l, Y
  24.   * 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
  25.   *: U3 B( F$ L5 P. r! \# O7 m) d
  26.   ******************************************************************************
    5 _6 e3 H) L4 |, B' T  {
  27.   */  Z5 M/ K2 l8 B6 {4 @/ P

  28. 6 C3 c6 f3 i; ~$ b. |4 \
  29. /* Header includes -----------------------------------------------------------*/( U5 r& v! |; \; C5 F8 p
  30. #include "main.h"
    - q4 p1 m5 ]. V! c
  31. #include "CAN.h"( b4 U2 v. z7 b, [% [1 d9 p0 d

  32. & N3 n% w4 N" G5 n9 R
  33. #ifdef _RTE_! y: s- @7 ^$ x' d" Y% L: L$ W* ~. k+ V
  34. #include "RTE_Components.h"
    9 @. u" Z% Q7 k8 I
  35. #endif) S6 v; l2 \+ _" K4 N1 D* V. }$ V

  36.   e* x8 o  g7 W7 s, T- I, p& V8 p
  37. #ifdef RTE_CMSIS_RTOS2. |% f6 [0 q# G% s2 Q0 C
  38. #include "cmsis_os2.h"
    8 r# I4 K1 b  j. W7 Z
  39. #endif" o4 @( j" o- T& r
  40. ( D+ L! ^9 h# ~1 S' V3 d& |7 n
  41. /* Macro definitions ---------------------------------------------------------*/( G& z6 L% |) c9 A% m3 V3 j8 J
  42. /* Type definitions ----------------------------------------------------------*/
    " V- }: K9 \- }
  43. /* Variable declarations -----------------------------------------------------*/0 f% Y1 m% G9 d9 V  I$ p
  44. /* Variable definitions ------------------------------------------------------*/* f. K  B' n7 ~
  45. static CanTxMsg canTxMsg = {0};" a# O4 ~! ^, {; f" A4 J! s4 d; s
  46. static CanRxMsg canRxMsg = {0};$ W* q+ {* {& `6 k

  47. " Y  f# i$ x2 t: Z9 c0 J
  48. /* Function declarations -----------------------------------------------------*/
    " x, `2 d9 v# d3 t( s# f
  49. static void SystemClock_Config(void);& L2 Z$ Y: N; J3 `

  50. % W8 t) I) d6 G" ]
  51. /* Function definitions ------------------------------------------------------*/
    : A  q, N5 A# x  S& l/ Y6 m

  52. 9 z0 @1 l' P9 t. @* l! T
  53. /**
    2 W3 _5 `; ~5 {4 q
  54.   * @brief  Main program.
    " r2 y& S; [/ F: y2 @/ M! ?: r. F
  55.   * @param  None.. s8 p" z1 K; s+ |0 k8 ], Y
  56.   * @return None.4 {& R4 R2 h* N, q1 P: ~
  57.   */
    ! {" H% R& J- N' `
  58. int main(void)- W; Y- j6 Q0 L* x# z) V
  59. {
    # A& n  M- k1 s$ W% @
  60.   /* Configure the system clock to 72 MHz */1 ?1 A8 B0 N7 i$ R2 u. _# |- \
  61.   SystemClock_Config();
    + R- ]0 `: h2 `, T1 M$ {/ l+ ~+ l
  62.   SystemCoreClockUpdate();
    4 F+ t7 X; C- v, r( E2 {2 L
  63.   v& l' K/ j  n: ^% k4 K' h
  64.   /* Add your application code here */+ [# m  h5 ^# q4 K
  65.   CAN_Configure(CAN1, CAN_WorkModeLoopBack, CAN_BaudRate250K, 0xAA55, 0x55AA);
    5 X0 Y( g& T( M/ K. {
  66. ' h: v5 H: W. e* _+ b) a
  67. #ifdef RTE_CMSIS_RTOS2
      f3 a+ \# Q% \6 ]1 G& f8 p
  68.   /* Initialize CMSIS-RTOS2 */1 v: i' l0 x9 \
  69.   osKernelInitialize();
    5 A& o6 W2 p4 s1 H6 n
  70. 2 k9 c' \' e( E/ Y3 T; n+ r
  71.   /* Create thread functions that start executing,
    % W* g0 W8 G0 n9 D) ]
  72.   Example: osThreadNew(app_main, NULL, NULL); */
    ' ^( K1 \2 J. g# F# J- m

  73. - W$ S7 ]3 k" G4 k% L& O
  74.   /* Start thread execution */
    & v* ?/ R, l. L: ?: m6 \; ?! P* r
  75.   osKernelStart();1 ?. T2 X& h: F
  76. #endif
    ! l1 K+ K1 f7 K  S

  77. ' v" j7 r& W7 C! K
  78.   /* Infinite loop */7 }# g. s, y3 P6 ~7 i2 y
  79.   while(1)
    ; Z" K) \" ?( I' t
  80.   {- P5 y+ _. m& b9 S  `
  81.     canTxMsg.StdId = 0xAA55;$ F" C: F/ n& T0 p& R5 c
  82.     canTxMsg.ExtId = 0x55AA;% O( b* V+ T. T3 D
  83.     canTxMsg.IDE   = CAN_ID_STD;
    - B* a6 d) w6 v7 \4 \
  84.     canTxMsg.RTR   = CAN_RTR_DATA;( F3 V* i2 Z+ w* S5 C7 K5 q4 S
  85.     canTxMsg.DLC   = 8;- s3 ]7 j7 b3 H, S5 A  p$ h8 `

  86. - v( f% t# O$ S
  87.     canTxMsg.Data[0]++;. {& _4 _4 Z  g
  88.     canTxMsg.Data[1]++;2 b7 F3 K: o3 B# @% c
  89.     canTxMsg.Data[2]++;
    $ G- l2 T% j5 a8 c' G, F
  90.     canTxMsg.Data[3]++;) ?# y$ C; j% D# H+ m
  91.     canTxMsg.Data[4]++;
    ! @$ ^. Z3 K+ q, {$ U. y2 u1 E
  92.     canTxMsg.Data[5]++;0 R$ T2 R3 `- g  s! @2 i8 z
  93.     canTxMsg.Data[6]++;
    / M, N9 q: A( p! U: i. H
  94.     canTxMsg.Data[7]++;- Y/ k+ i5 a9 E$ ~! z1 K
  95. : c+ z7 h% D' C8 O
  96.     CAN_SetTransmitMessage(CAN1, &canTxMsg, 1);
    5 [: L5 F4 G* N8 J
  97. & _9 t3 C% p3 R8 q" s* p1 T6 Z
  98.     while(CAN_IsReceiveBufferEmpty(CAN1) == true);+ E4 p* x3 \0 |* {' K

  99. $ o/ @3 f) I: ?& q; O1 y
  100.     CAN_GetReceiveMessage(CAN1, &canRxMsg, 1);6 R: h3 R% R$ D8 |( x
  101.   }9 `; m& ?# i" G# L+ k) z
  102. }
    ' x8 n$ K! X0 [) b8 f
  103. ) `, _9 T2 V0 d0 K+ v1 j% M& Z
  104. /**! {7 t. V4 Z  R! P) R  B
  105.   * @brief  System Clock Configuration.0 q+ e5 X& }* `4 k4 m
  106.   *         The system Clock is configured as follow :
    9 L+ b1 n8 U0 |6 ?/ q, c% |
  107.   *            System Clock source            = PLL (HSE)
    9 e; N1 c) m0 s4 \+ z; j; J
  108.   *            SYSCLK(Hz)                     = 720000007 t" z& O4 M- T: g
  109.   *            HCLK(Hz)                       = 72000000
    ( c$ X. X. R# G
  110.   *            AHB Prescaler                  = 1
    , P2 ^4 i8 E5 _% ?  N+ Q
  111.   *            APB1 Prescaler                 = 25 v9 s* q3 h+ k8 j2 Z/ a) }' z
  112.   *            APB2 Prescaler                 = 1
    ' U, ~9 p' n- U+ U
  113.   *            HSE Frequency(Hz)              = 8000000
    6 t8 z* y$ b" U, R
  114.   *            HSE PREDIV1                    = 1
    9 ?, D( h; b( e' Y+ y( l- N2 g( n0 O
  115.   *            PLLMUL                         = 9
    : P7 J0 U$ i/ E1 z" u+ r/ ~$ ^
  116.   *            Flash Latency(WS)              = 23 D" S/ |; g8 S* A' G' i
  117.   * @param  None." |% Q0 J1 d7 e" `4 P
  118.   * @return None.4 t  `, q8 j8 R* u; k. p
  119.   */
    $ t. J6 d! @* l5 P; s) {& d1 C
  120. static void SystemClock_Config(void)
    8 B/ C( i- @) @. B
  121. {' @- J+ x# @2 y3 ], x8 x
  122.   /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration */+ y2 ~, f+ i4 T5 a: \6 X
  123.   /* RCC system reset */
    $ [( R  g& S: s/ |; ?" O$ ^
  124.   RCC_DeInit();
    , l# x! {2 T1 ~9 K: d" P- n9 ?  ~& P

  125. $ Z: D. |% l& w+ E. G
  126.   /* Enable HSE */
    9 F# x7 o8 w% L2 z/ b& N: y* _
  127.   RCC_HSEConfig(RCC_HSE_ON);# L' U- h1 N! Q" b) |

  128. . _: r7 ^* S0 r
  129.   /* Wait till HSE is ready */+ n- Y& R8 s1 F- m7 W
  130.   ErrorStatus HSEStartUpStatus = RCC_WaitForHSEStartUp();! ^' H1 L/ z1 Z# J
  131. : k: Q0 [8 R0 E8 r1 T, X; k, P
  132.   if(HSEStartUpStatus == SUCCESS)
    3 T. i( Y$ Y6 V6 R
  133.   {! h% B7 X: G; z5 e; m: o
  134.     /* Enable Prefetch Buffer */
    : }9 E& m. B6 ?) D) ^- f
  135.     FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
    ' R) p: I2 E! p* o6 \

  136. * ~0 V! E6 G4 d$ N1 S
  137.     /* Flash 2 wait state */
    3 z. [" F) M4 r2 A$ b
  138.     FLASH_SetLatency(FLASH_Latency_2);
    7 |9 w6 c4 t: G/ \% Q
  139. 0 s, O- l2 s9 f0 Y
  140.     /* HCLK = SYSCLK */
    3 ]& ?% h+ w) m0 P' a
  141.     RCC_HCLKConfig(RCC_SYSCLK_Div1);
    1 u5 b; O0 Q& o+ p! K
  142. 8 n6 c/ j$ s; b( ^
  143.     /* PCLK2 = HCLK */
    / b/ E: t" @! e, O6 v7 r( `/ N
  144.     RCC_PCLK2Config(RCC_HCLK_Div1); ( U6 A, s1 s/ ^" \- A$ \
  145. 4 U1 \9 D$ `; d: s5 n" i
  146.     /* PCLK1 = HCLK / 2 */6 w7 M7 p% e& R. X+ Z' c6 m
  147.     RCC_PCLK1Config(RCC_HCLK_Div2);  I- E4 P: L3 Q/ _

  148. 3 X! o9 J0 X/ a
  149.     /* Configure PLLs */
    4 E1 D7 q# }  N3 R; s1 ~, v# _
  150. #ifdef STM32F10X_CL. }9 _/ ~6 k0 v& c( P
  151.     /* PLL2 configuration: PLL2CLK = (HSE(8MHz) / 2) * 10 = 40MHz */
    % W6 X" A; W( m9 o6 {
  152.     RCC_PREDIV2Config(RCC_PREDIV2_Div2);& q  K/ z/ ?' k+ H0 M) [
  153.     RCC_PLL2Config(RCC_PLL2Mul_10);+ B# f4 t. t5 \! D
  154. ! o, a" q8 I! f- x0 p: B. g
  155.     /* Enable PLL2 */
    / `& G# Y: y) @) ^
  156.     RCC_PLL2Cmd(ENABLE);# u8 [2 z0 Z: z& |' \

  157. ' U3 \* L1 l/ e' N. _$ G
  158.     /* Wait till PLL2 is ready */
    - w6 I1 k: m' S/ X" m! |% X8 O
  159.     while(RCC_GetFlagStatus(RCC_FLAG_PLL2RDY) == RESET);
    : e" R4 e2 j2 Z: e" s* f$ {
  160. $ a7 ?0 w9 \9 M  h: [
  161.     /* PLL configuration: PLLCLK = (PLL2(40MHz) / 5) * 9 = 72MHz */
    ! v6 l2 e. b$ g, @- H7 Q2 u
  162.     RCC_PREDIV1Config(RCC_PREDIV1_Source_PLL2, RCC_PREDIV1_Div5);
    " s" X. q# X) V3 M( i. S- q
  163.     RCC_PLLConfig(RCC_PLLSource_PREDIV1, RCC_PLLMul_9);; G1 I0 G# f% V7 ~6 _
  164. #else1 U; V! E. S0 n  u- X
  165.     /* PLLCLK = HSE(8MHz) * 9 = 72MHz */; X& ?$ U2 h/ p7 J3 r% e  N
  166.     RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);! q5 r# W# k; k# K: c2 `. b
  167. #endif
    & Z  h) p0 S# ^6 j0 }
  168. & m9 W& c5 U) b% S) w1 K, ^: h
  169.     /* Enable PLL */% f. G, G* G1 R2 ^
  170.     RCC_PLLCmd(ENABLE);) w0 r9 u6 G3 g
  171. 3 c+ U% B( h# ~7 l4 l. T. C
  172.     /* Wait till PLL is ready */0 R8 G. g( z9 B5 n6 y
  173.     while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);% e: z2 y# ]$ u4 o: m. Z

  174.   a# T! ^/ {# C& P" F
  175.     /* Select PLL as system clock source */
    + k2 S. L! G2 `+ p
  176.     RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);, ?' b& z5 P2 s3 k3 y
  177. 9 m/ m6 w7 |( @9 `9 V9 m, ~3 ?. |
  178.     /* Wait till PLL is used as system clock source */6 ]# G+ g( C7 b; C
  179.     while(RCC_GetSYSCLKSource() != 0x08);
    # E% n  `) N( ?* n
  180.   }
    1 |! r- k) @0 f: ?# X' H  _
  181.   else
    % t! h( _6 O/ u/ v: e
  182.   {. p# f( l3 c" ?% X
  183.     /* Disable HSE */1 Q. I# K9 f! p7 w. T* u* L9 A2 g
  184.     RCC_HSEConfig(RCC_HSE_OFF);  i0 D1 Y$ w) u+ K; M
  185. 2 I0 M# U) `- M* {" }; m
  186.     /* Enable HSI */% `4 w, m5 N4 J4 z3 h) |! V" p4 c6 D
  187.     RCC_HSICmd(ENABLE);
    " q/ z8 G) I( x+ ~

  188. 2 c: @7 u" V7 j& s, f
  189.     /* Enable Prefetch Buffer */$ d* x! F# h7 B) ~: |" h' e
  190.     FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
    2 j( r% @. q- F! b, N

  191. + Y* z. V: m* e; a
  192.     /* Flash 1 wait state */; ^3 I' u$ c" S0 h5 s( Z
  193.     FLASH_SetLatency(FLASH_Latency_1);4 h# c9 o4 l( x, {

  194. ) B- @5 Q& o4 @+ J% Q& I2 M2 @
  195.     /* HCLK = SYSCLK */
    5 J& a2 R0 I' [4 @) d( c; x6 i
  196.     RCC_HCLKConfig(RCC_SYSCLK_Div1);
    5 l6 a% Q3 N  a' {9 D* X: a
  197. % v" l+ p) r" c4 P' g, B
  198.     /* PCLK2 = HCLK */- Y- a4 z- l% u$ W# s
  199.     RCC_PCLK2Config(RCC_HCLK_Div1); 2 L0 m& @4 x! e- k3 R; @, g. C

  200. * r$ f+ E% ]% |0 w
  201.     /* PCLK1 = HCLK */7 j" `$ a: o) X" c0 B( G
  202.     RCC_PCLK1Config(RCC_HCLK_Div1);8 o* V! T0 w3 s; p  T/ _

  203. , H! \' V5 s4 u) ~
  204.     /* Configure PLLs */7 D2 n0 d3 W3 a: x
  205.     /* PLLCLK = HSI(8MHz) / 2 * 9 = 36MHz */
    4 Q* \% ~) K+ G- _$ U% E) Y1 Z
  206.     RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_PLLMul_9);
    / L2 _! N; ~6 _9 E# \6 z6 R
  207.   Q6 ]: z; I# z# [( o. [
  208.     /* Enable PLL */1 Q1 d& W; O$ W1 e  V" }
  209.     RCC_PLLCmd(ENABLE);" f0 k& F+ U# I' C# i0 P
  210. - U, V; f0 ^" Q- B4 W' W* F; J
  211.     /* Wait till PLL is ready */6 b5 n3 h4 |7 C" b, [% \3 g7 {6 H
  212.     while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);% I/ Z- o4 z  v' G. E  V0 T' n, G: R

  213. 1 O; {; [7 O7 }7 |* d
  214.     /* Select PLL as system clock source */
    4 z( E, |! Q/ K' E
  215.     RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);, U1 J! N' _) T+ K' D! `

  216. " X8 o6 t$ O9 _" |* Q7 B& P+ w
  217.     /* Wait till PLL is used as system clock source */$ x+ f7 |$ i% H( R7 {
  218.     while(RCC_GetSYSCLKSource() != 0x08);9 _+ Z: Q9 a/ Y: e. c
  219.   }
    ( C% F/ v4 @1 E' f
  220. }
    ; n4 g& R4 U  _  ]+ ^3 q( e

  221. 1 n! J' S* z( o1 A! l- R5 `
  222. #ifdef USE_FULL_ASSERT
    2 A' v! g/ U" x
  223. /**; t! m% o9 K, U, x% ~
  224.   * @brief  Reports the name of the source file and the source line number
    8 T3 |9 K/ |% u
  225.   *         where the assert_param error has occurred.
    ( E* v; \% I) a# Q7 L$ C
  226.   * @param  file: pointer to the source file name.& `5 d! |6 S6 V. \
  227.   * @param  line: assert_param error line source number.0 y2 P* S! n% f$ j6 D
  228.   * @return None.
    ; P; t: X$ y2 w4 y9 N1 V
  229.   */
    . ]' F& J: S2 p- @
  230. void assert_failed(uint8_t *file, uint32_t line)1 a. ~( S/ z, y2 V5 @# V* X
  231. {% Y8 e3 l' y& W
  232.   /* User can add his own implementation to report the file name and line number,( S2 l' C+ v" _9 o3 w6 k/ Z4 i
  233.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    - r* T& x' h  a
  234. $ t, w$ n6 r7 y  }( y8 j, g
  235.   /* Infinite loop */0 x9 f& q9 ]* U/ b. |
  236.   while(1)+ |' x. x. n& ]. ?
  237.   {! U! ~. |5 h! [' l, K
  238.   }
    $ R$ R% d# @6 j* L2 R
  239. }
    & a9 l: R# w. s4 x4 D
  240. #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
赞 收藏 评论0 发布时间:2021-11-26 17:00

举报

0个回答

所属标签

相似技术帖

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
人形机器人运动控制、感知与智能配电
半导体创新技术与应用方向
EE架构与软件定义汽车
12V/48V 汽车智能配电(SPD)
区域控制单元(ZCU)与分区架构
关注我们
st-img 微信公众号
st-img 手机版