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

【经验分享】STM32F1(CAN)

[复制链接]
STMCU小助手 发布时间:2021-11-26 17:00
1,开发环境2 C2 @% I, _. p) V! g
. y  j8 e' H8 v
1,固件库:STM32F10x_StdPeriph_Lib_V3.5.0
' h' T! W* ^; @, t8 e% b3 w& X% f5 Z
  U. w+ ?1 J- _  u; D- u2,编译器:ARMCC V5.06
( }9 t9 d+ y4 M: f, `5 m+ s3 q  q/ B, Y) d
3,IDE:Keil uVision5
8 z5 A9 V* k5 c/ C* D4 n5 a' Y8 o$ ?$ j# I! t
4,操作系统:Windows 10 专业版
4 z9 e" I+ q% z5 H* p. e7 c' O' m# J6 g

- R& c7 N5 s  P, T, T2,程序源码2 V" v  {0 \( I0 I6 k5 c
" g7 a; W. w% \! h' d+ r
RingBuffer.h 文件
5 l) v' F( ?  r
  1. /**
    , Q1 b# G7 z4 P
  2.   ******************************************************************************8 I; P4 p* |3 v6 ~1 g; c, \
  3.   * @file    RingBuffer.h4 j( F* X, ^* Z& L1 V7 C' R+ v: W
  4.   * @author  XinLi/ c. x' T0 u8 `7 |) t
  5.   * @version v1.1$ j; c2 k* z6 P; e0 K$ w
  6.   * @date    15-January-2018
    & L) d! V, ~9 x* e( G
  7.   * @brief   Header file for RingBuffer.c module.
    2 L9 K7 L6 a+ O
  8.   ******************************************************************************$ C' P; \$ u- D7 t# _- {9 u2 B! P
  9.   * @attention: }8 a- e1 Y) j0 @* A
  10.   *" ?# A  V* S- {
  11.   * <h2><center>Copyright © 2018 XinLi</center></h2>. `+ g* n9 D: w+ f. I7 }
  12.   *
    ; U3 J+ s: M: }: D; ~% l
  13.   * This program is free software: you can redistribute it and/or modify+ S4 }; x+ v% v
  14.   * it under the terms of the GNU General Public License as published by
    " }, s, s$ N$ |
  15.   * the Free Software Foundation, either version 3 of the License, or; e) A+ k' _, r( [# J
  16.   * (at your option) any later version.
    8 p& I% \- s7 O/ S( t0 g
  17.   *+ l3 S, b  ?0 v# T
  18.   * This program is distributed in the hope that it will be useful,2 q& i9 l2 u1 c  |
  19.   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    : Z3 E, ^. _1 d1 z; m5 R
  20.   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the& N1 n8 t3 J5 Q! r" c" p" d) D  k1 }
  21.   * GNU General Public License for more details.
      W+ X  z6 v% |7 w
  22.   *0 n3 w5 @& m: ]- z
  23.   * You should have received a copy of the GNU General Public License  _1 Q3 j0 ^( w# o6 m% I- z
  24.   * along with this program.  If not, see <<a href="https://www.gnu.org/licenses/>." target="_blank">https://www.gnu.org/licenses/>.</a>9 O$ \' f/ C8 n% E' r
  25.   *0 ^' R0 @9 y' \% a4 a2 H; T
  26.   ******************************************************************************) p8 A* O! y3 q% ~0 @, _
  27.   */
    1 D  a/ C+ g# n$ t* W( k! ~

  28. ( Q$ i7 I; C+ d* i. \) @) b
  29. #ifndef __RINGBUFFER_H
    & C9 H4 R+ b; X. t" s
  30. #define __RINGBUFFER_H
    2 }# M6 [$ o% K0 ?6 H1 v& I
  31. ( G7 j7 L! V" O$ J& v/ w) z& t$ Z
  32. #ifdef __cplusplus
    4 t$ I5 \% |/ G! F
  33. extern "C" {; q7 \  U; P6 s0 `: t1 [
  34. #endif
    . h. T$ g3 f  J5 A. I  }2 C( e

  35. ! i! @0 h, U2 i& a# D" n
  36. /* Header includes -----------------------------------------------------------*/
      I9 H1 A% B$ X3 X+ L# t
  37. #include <stdint.h>
    0 ]' m/ G, `7 V# i5 a
  38. #include <stdbool.h>6 L7 ^4 c. ~7 P- _# L; N: F: F
  39. #include <stdlib.h>0 J9 Z  Z! K; T" K
  40. * d; `7 I8 y. \0 K& K
  41. /* Macro definitions ---------------------------------------------------------*/
    " I( g! ?3 r, |! q
  42. #define RING_BUFFER_MALLOC(size)  malloc(size)
    ' Z& }6 k9 J  `; i. {1 l
  43. #define RING_BUFFER_FREE(block)   free(block)
    8 O- d* t. Q% U- Q+ b8 G

  44. ! ]$ G8 B4 T" w8 o0 }$ M
  45. /* Type definitions ----------------------------------------------------------*/8 B% g0 N' H6 Q, q
  46. typedef struct. k# z& F2 h/ a& @# u8 {( m% R& `7 e
  47. {
    ) M! q& X+ S5 Y- s, N/ q, j
  48.   uint8_t *buffer;
    ) t% j  s: O5 E1 q7 k
  49.   uint32_t size;" v& N! Q7 s$ H
  50.   uint32_t in;
    ; ^- N) D! D# w) l  Y  v
  51.   uint32_t out;4 U5 J: f3 F' m& g" H& g
  52. }RingBuffer;
    ) ~' `5 o6 ^! p+ N, T: }

  53. ( I/ Z7 f4 ^5 ]- u% R; T
  54. /* Variable declarations -----------------------------------------------------*/5 m; g4 P& @" s8 L. }
  55. /* Variable definitions ------------------------------------------------------*/7 ^1 `, N# B6 `7 Q# d! P1 e1 ~
  56. /* Function declarations -----------------------------------------------------*/5 I& P( M% ]: J
  57. RingBuffer *RingBuffer_Malloc(uint32_t size);: B7 g1 w5 y  N% l& w
  58. void RingBuffer_Free(RingBuffer *fifo);: M8 b/ f8 Q: b, y' B2 q: [

  59. . M7 i) _+ _- ?
  60. uint32_t RingBuffer_In(RingBuffer *fifo, void *in, uint32_t len);% I# M$ e! P- X4 U8 \
  61. uint32_t RingBuffer_Out(RingBuffer *fifo, void *out, uint32_t len);
    4 p4 J; R  t6 [; }4 H! j
  62. & [2 ~' {; z# p1 C+ K6 I* e
  63. /* Function definitions ------------------------------------------------------*/! ?' _5 o* @9 M4 J# D( r

  64.   \, S. Y$ v% o. b0 G0 k
  65. /**( d% S$ X) y4 d0 W; R. s
  66.   * @brief  Removes the entire FIFO contents.; V6 b$ z0 k9 A; q6 A1 I
  67.   * @param  [in] fifo: The fifo to be emptied." R, ]( }/ _. l- k% j' q' C
  68.   * @return None.) b5 ^$ \) @6 x  N4 I
  69.   *// H3 t6 M! h; `
  70. static inline void RingBuffer_Reset(RingBuffer *fifo)9 T1 y1 @. ]  {$ Z; v- x
  71. {
    $ k$ K# V  y; A' X
  72.   fifo->in = fifo->out = 0;6 V5 i1 a1 n2 F$ j4 {" s
  73. }
    ( t/ T9 a% W) K& n% I4 m/ [
  74. 1 \  y4 s  b- b2 K. L
  75. /*** C8 n$ c' F0 o. ^7 Z: s, }+ T
  76.   * @brief  Returns the size of the FIFO in bytes.
    + W/ O, B. x- Q! R/ y! ^
  77.   * @param  [in] fifo: The fifo to be used.: {; P' r7 K1 q: b( w
  78.   * @return The size of the FIFO.: t& {. ^7 c, x9 t+ H
  79.   */+ \7 _" H% I/ e0 w& X
  80. static inline uint32_t RingBuffer_Size(RingBuffer *fifo)2 `. G8 r: P- \
  81. {/ _5 B4 o: ]( @5 N
  82.   return fifo->size;; r! N; p% E4 g: t  H& |  ?
  83. }
    3 q9 w3 l) L; `6 f" x  l
  84. 2 w+ x# }, P/ f* o3 ?
  85. /**/ M3 j9 V9 T# J; r
  86.   * @brief  Returns the number of used bytes in the FIFO., L& L5 w& f# K  c. ]
  87.   * @param  [in] fifo: The fifo to be used.6 v+ t$ U' m+ D4 i- m  ]9 d' ?
  88.   * @return The number of used bytes.+ Y( l: P5 Z0 {* P) j/ I( o
  89.   */
    3 t" j0 G, `$ j6 L4 B3 i$ H
  90. static inline uint32_t RingBuffer_Len(RingBuffer *fifo)
    2 u3 O+ J5 q8 S0 c
  91. {
    : {- W' i1 R* Q( ~- O) ]7 l
  92.   return fifo->in - fifo->out;! q2 y% l9 N$ a2 ^
  93. }! |& d% v. e: X! L% m: }; A8 D6 u

  94. * t- t( Q! Y1 x$ W) `% X9 u
  95. /**
    9 Y6 _! _0 T7 e0 n; O3 |' ?
  96.   * @brief  Returns the number of bytes available in the FIFO.) ]8 ?9 B9 S6 |/ D/ j
  97.   * @param  [in] fifo: The fifo to be used.) O# u# n9 O6 `+ A  B! `  D
  98.   * @return The number of bytes available.% y, u$ p% O2 p0 r# }" \, }( P8 B
  99.   */$ z' Z; N1 C% g5 H  F
  100. static inline uint32_t RingBuffer_Avail(RingBuffer *fifo)
      z( ?% h# o2 P+ a+ |
  101. {; D1 P! x1 O% j
  102.   return RingBuffer_Size(fifo) - RingBuffer_Len(fifo);
    ( D1 F4 x* A7 V
  103. }4 r. t& |! i4 D8 h" h5 q

  104. 3 P, E0 c# W" l% n
  105. /**. C# G  g  U0 a# |4 y4 H
  106.   * @brief  Is the FIFO empty?
    3 |; @4 w0 W& V. s0 @. m  X. m4 B
  107.   * @param  [in] fifo: The fifo to be used.& Z4 ^' t* j, b* M& C  Q0 H& F
  108.   * @retval true:      Yes.
    # ]) ~. P) n: M9 i$ U8 U
  109.   * @retval false:     No.
    2 s/ e5 F$ V" u8 B
  110.   */9 u" Z) y) @* U8 p4 o/ d
  111. static inline bool RingBuffer_IsEmpty(RingBuffer *fifo)
    : h: i6 v) {6 E. |
  112. {9 c6 ?. W' Q, u( ^0 H# |
  113.   return RingBuffer_Len(fifo) == 0;
    8 v0 \% M1 f* ]
  114. }# y3 L7 H* a4 Q% ?4 H: t2 r
  115. ; y5 n0 N8 t) p$ {( r8 x! E0 e5 V
  116. /**
    ' `0 x- ?( @' Y5 W, `3 a
  117.   * @brief  Is the FIFO full?  u; r! A% e) u  n
  118.   * @param  [in] fifo: The fifo to be used.
    # w# u5 |/ w- ]5 s: B. V8 M( u
  119.   * @retval true:      Yes.
    2 I% ]2 b. g; S" m0 q3 O2 L1 d  x
  120.   * @retval false:     No." d. _8 [# n. Y" h0 c% X0 L; ?6 ?8 E
  121.   */; \3 N4 @4 w. F3 N: D7 w0 `: N# g
  122. static inline bool RingBuffer_IsFull(RingBuffer *fifo)
    - s# S# o, y; D
  123. {
    . W/ E) j) G/ p/ X9 U# ?
  124.   return RingBuffer_Avail(fifo) == 0;; V2 j" E" i* r# M. }. d
  125. }
    7 M! }  t: i2 V5 f! U; ]

  126.   X2 c3 `: a  K  F$ }+ `2 b' m. E
  127. #ifdef __cplusplus
    5 N  D. O2 |+ R  X, e0 h- t
  128. }
    ! Q: D' f) Z/ Y# R4 U
  129. #endif
    8 R& |1 E) t) B4 ~( l$ i& P# ^
  130. ) ^8 h' x  f3 y
  131. #endif /* __RINGBUFFER_H */
    6 r8 Z8 S, R1 H4 Y, |* h
复制代码
. u( y! G( R  I* W
& C- q2 @& M. W6 I6 L
RingBuffer.c 文件
" S- c# D( s! a0 ]
  1. <div>/**' F4 l& F0 f6 H1 U
  2.   ******************************************************************************
    # ^) x- p! u/ O+ C7 s5 x
  3.   * @file    RingBuffer.c3 T3 V( S$ S  i/ }+ h# E# S
  4.   * @author  XinLi
    : F; g6 J3 s, N
  5.   * @version v1.1
    # L) T0 S0 e4 n# a
  6.   * @date    15-January-2018
    ; j8 X* v- p6 s% G
  7.   * @brief   Ring buffer module source file.
    % d, g1 L( U# o/ v. p, }$ n/ H# W& e
  8.   ******************************************************************************  Z* w. D" R; s5 e8 d
  9.   * @attention8 o% k( T9 {. r+ D" [
  10.   *
    6 ?/ Z- o( j5 h  T3 L" n8 f8 U
  11.   * <h2><center>Copyright © 2018 XinLi</center></h2>6 G! i8 H9 }" ]( q( x' ^; S6 m
  12.   *# ^1 X- j! U- n8 d
  13.   * This program is free software: you can redistribute it and/or modify
    4 F" Q9 \* N0 Y/ c- ~
  14.   * it under the terms of the GNU General Public License as published by
    * s4 E, B& E7 O& c% q1 X
  15.   * the Free Software Foundation, either version 3 of the License, or
    2 f* q# k9 y+ p7 J
  16.   * (at your option) any later version.
    # @9 d) f4 c% D7 f4 _
  17.   *3 e, m7 r7 [9 s" C0 F/ q" O
  18.   * This program is distributed in the hope that it will be useful,0 _3 Y' e. d) l, Q( j6 @' l
  19.   * but WITHOUT ANY WARRANTY; without even the implied warranty of7 P( t) R2 n: U+ ]4 D
  20.   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    ; |- l4 L% f" P
  21.   * GNU General Public License for more details.2 _7 J( K8 f5 {8 w' y
  22.   *
      m1 s% j9 S( B. {
  23.   * You should have received a copy of the GNU General Public License2 ~+ ^# l9 _+ A/ C$ g3 P$ z9 F
  24.   * along with this program.  If not, see <<a href="https://www.gnu.org/licenses/>." target="_blank">https://www.gnu.org/licenses/>.</a># T0 f% ]' m+ C% y" A+ x1 `# k
  25.   *
    + ]6 V6 {0 Y, n, M) L" H# g8 d
  26.   ******************************************************************************- g: s9 L% R& N; m) p
  27.   */
    ( G" H: L* ^8 U/ S$ Y
  28. ' g5 X! Y; u  C& d" r$ X
  29. /* Header includes -----------------------------------------------------------*/) t/ O6 v9 Y) _
  30. #include "RingBuffer.h"
    8 J0 Y4 |$ m" [, \0 `
  31. #include <string.h>
    & L- A- Q! B+ C' H6 ^

  32. % s7 @0 g3 G0 p+ b" L
  33. /* Macro definitions ---------------------------------------------------------*/6 n' o1 e$ n# E: ~! t
  34. #define min(a, b)  (((a) < (b)) ? (a) : (b))
    4 C  r1 l& v1 j
  35. 5 `: a6 x; ^* x" E
  36. /* Type definitions ----------------------------------------------------------*/
    ! I3 h3 x$ y* S" t' l
  37. /* Variable declarations -----------------------------------------------------*/# B$ y! {) o) _7 }6 c
  38. /* Variable definitions ------------------------------------------------------*/- v0 [/ i% ?  B% X6 [8 _
  39. /* Function declarations -----------------------------------------------------*/7 \; U3 I6 ?2 |) |% m2 Y/ T/ V
  40. static bool is_power_of_2(uint32_t x);
    & h7 h( m1 ]) L  O4 b& l
  41. static uint32_t roundup_pow_of_two(uint32_t x);, ^" e- I- n2 S( n2 {5 `
  42. 6 h/ {: U, C% h8 g5 h7 Z4 s
  43. /* Function definitions ------------------------------------------------------*/" F0 k- U; L3 ~% L1 ~1 a3 U

  44. 8 ]0 j+ O( |9 g6 U2 w0 z
  45. /**  o7 h% {2 r0 ^; P; K
  46.   * @brief  Allocates a new FIFO and its internal buffer.6 y( D/ y- z: G: |# x6 {# P
  47.   * @param  [in] size: The size of the internal buffer to be allocated.
    3 j# W6 s) E2 O" S9 v6 D. P
  48.   * @note   The size will be rounded-up to a power of 2.
    7 J5 _5 y. g# O0 v* o
  49.   * @return RingBuffer pointer.
    ! [4 E( z; L+ ~2 m, n) R6 H+ X
  50.   */. B5 W$ Q+ ?5 K
  51. RingBuffer *RingBuffer_Malloc(uint32_t size)1 H& N) `- A0 D4 m1 P; C) c4 O
  52. {
    # C" W7 Z& [3 M. e9 S
  53.   RingBuffer *fifo = RING_BUFFER_MALLOC(sizeof(RingBuffer));1 l" [& E0 [% T4 }4 V: M) S! W

  54. : s7 U$ g' G) X0 q; \: T, g9 A
  55.   if(fifo != NULL)* D$ A+ |2 B+ [/ P
  56.   {4 D+ b! i( o: U, B6 }$ T0 A
  57.     if(is_power_of_2(size) != true). G3 W; d/ a/ N# @; t# z
  58.     {% j# C, r. R  `8 V2 o; Y. {
  59.       if(size > 0x80000000UL)
    ( q: D- T' ~8 |/ S$ r# V1 O
  60.       {
    * P. A: R. ?4 l+ W8 M) @
  61.         RING_BUFFER_FREE(fifo);3 i. q0 i9 v1 s* K9 S* I3 [+ p' L
  62.         return NULL;$ @+ o( O2 o7 x9 M8 V
  63.       }
    3 z; ^$ [0 a! k
  64. # g. V3 _2 k/ n9 ?, r5 w
  65.       size = roundup_pow_of_two(size);( P3 ]# z+ m+ N
  66.     }4 ]. d# t; o% K* r* j/ T
  67. # Z: u' O6 m1 J
  68.     fifo->buffer = RING_BUFFER_MALLOC(size);6 p# a) t; M/ O

  69. + E. p, I0 F! e3 t2 P
  70.     if(fifo->buffer == NULL)( K; z& a' W3 m/ J5 ]0 W9 Y3 p
  71.     {
    4 j0 e8 g5 m/ `' x; f: l- @
  72.       RING_BUFFER_FREE(fifo);( V, R: P9 v6 D
  73.       return NULL;% z( v' V0 [" H- z
  74.     }
    9 L7 A/ u2 J3 ]$ r  L, M
  75. ! o  o' r$ D3 v1 s/ h
  76.     fifo->size = size;7 t6 i0 X" z( W; v: `
  77.     fifo->in = fifo->out = 0;
    * s6 S  Q; c3 ?! J( \) `8 l3 |
  78.   }
    9 b7 A* T( K: M6 q
  79. ; B! C, Z8 H7 z( _; K9 l( X/ p
  80.   return fifo;
    / w! _; ~4 X% _$ ^4 s* P$ p
  81. }: }9 r  ^' a! e# I/ G/ p+ g
  82. 8 @# ]% U- i- n- W& x0 }3 F) D
  83. /**# w' l6 r3 z* z7 c; k
  84.   * @brief  Frees the FIFO.  j- `) s. d6 F8 ?: q$ j# G- l
  85.   * @param  [in] fifo: The fifo to be freed.
    9 g. n7 `! H& w, Q- f# W
  86.   * @return None.# ]  n" N2 Z1 c$ r  i
  87.   */1 M+ }, M6 O  `; a6 g  I
  88. void RingBuffer_Free(RingBuffer *fifo)
    / c6 i) z1 P2 j  p+ p# N/ Q; `
  89. {
    2 [1 e. h/ j4 @% E
  90.   RING_BUFFER_FREE(fifo->buffer);
    & b7 g  G# E9 m$ X- v8 e% Z( Q
  91.   RING_BUFFER_FREE(fifo);
    . m0 V8 \: j+ i. r. q8 d5 b- W' O
  92. }7 x9 B# y/ b: k. S+ O

  93. 1 W1 t) e$ t/ Y. W) E
  94. /**
    9 _- O- j: H% j0 ^4 W' g) V6 J) _
  95.   * @brief  Puts some data into the FIFO.
    ( s. k  G$ ^9 h2 C- C
  96.   * @param  [in] fifo: The fifo to be used.
    : r$ b1 t' J# n0 Y- l4 ^! J4 h5 B1 U
  97.   * @param  [in] in:   The data to be added.
    9 U5 e5 o' h2 w, U
  98.   * @param  [in] len:  The length of the data to be added.
    . F: t1 K7 l  M) n
  99.   * @return The number of bytes copied.
    8 q$ q1 d) O) @$ ?/ v) x
  100.   * @note   This function copies at most @len bytes from the @in into
    4 W$ s3 g3 U! w% e
  101.   *         the FIFO depending on the free space, and returns the number& M) N) x$ d# h' Y
  102.   *         of bytes copied.
    7 Y. b; P+ F% d
  103.   */
    ) j8 I+ z0 Q+ K% M6 l. F8 n0 g
  104. uint32_t RingBuffer_In(RingBuffer *fifo, void *in, uint32_t len)
    & z- G, w; R' S" Z" y/ Y/ T
  105. {
    ; Q. n# t' I" p- X
  106.   len = min(len, RingBuffer_Avail(fifo));
    ! `- R$ D$ e( G( e/ d2 p
  107. ! c2 H! Q4 K& N. n9 ?
  108.   /* First put the data starting from fifo->in to buffer end. */
    3 f4 K: A; }, A" O# J$ z. y
  109.   uint32_t l = min(len, fifo->size - (fifo->in & (fifo->size - 1)));
    2 k6 t/ ?/ F- e7 O& v+ Z4 L
  110.   memcpy(fifo->buffer + (fifo->in & (fifo->size - 1)), in, l);
    : p! \# d& N, m; C1 ^% y
  111. ; t" }. H2 U$ b* z
  112.   /* Then put the rest (if any) at the beginning of the buffer. */+ |" p$ X3 ]6 J0 w1 s4 x5 x' |
  113.   memcpy(fifo->buffer, (uint8_t *)in + l, len - l);
    5 |3 B& V: q; J: P) ~; Y
  114. ( B/ b' X: T7 `
  115.   fifo->in += len;
    . c! d" J5 L2 k% H
  116.   V# C1 j' K# E4 w/ [
  117.   return len;  h0 o) V& z% B: I6 ?$ F, o9 P' I2 l* v6 h
  118. }
    ; `) p; C  s7 f7 ~: O
  119. 4 W6 w) H7 B: P4 A' t6 ^; q9 w5 b# N
  120. /**
    ! h* J5 Z3 p2 \5 J. \
  121.   * @brief  Gets some data from the FIFO.
    % c) D; e8 T, L+ C' n3 U
  122.   * @param  [in] fifo: The fifo to be used.
    ! }, K, }" f4 n' N
  123.   * @param  [in] out:  Where the data must be copied.2 Z9 c& q- h1 Y5 i
  124.   * @param  [in] len:  The size of the destination buffer.
    0 c9 a) B0 a# I. S. B1 n
  125.   * @return The number of copied bytes.& O% W' G# q6 h8 a
  126.   * @note   This function copies at most @len bytes from the FIFO into
    3 j! P7 U! h6 e
  127.   *         the @out and returns the number of copied bytes.
    1 n, o) w, L; f, O
  128.   */
    9 t; M( G, V+ H, L
  129. uint32_t RingBuffer_Out(RingBuffer *fifo, void *out, uint32_t len)/ \; U' m% M4 K8 P3 `
  130. {
    % Z+ C2 h1 d; C) }7 U* h" K+ P8 p
  131.   len = min(len, RingBuffer_Len(fifo));$ X) N2 D' o( C9 R6 H

  132. 7 W9 O3 y9 m/ D, E) w7 F) c( h
  133.   /* First get the data from fifo->out until the end of the buffer. */
    / w, H( w6 P5 H& d
  134.   uint32_t l = min(len, fifo->size - (fifo->out & (fifo->size - 1)));
    6 ^% i* S, b+ }. F6 n  U
  135.   memcpy(out, fifo->buffer + (fifo->out & (fifo->size - 1)), l);
    4 p" q( e0 W% L4 A* @3 S

  136. ( D* Q/ J( A$ o8 m7 \
  137.   /* Then get the rest (if any) from the beginning of the buffer. */0 H: h& M9 o5 G/ g$ x5 g
  138.   memcpy((uint8_t *)out + l, fifo->buffer, len - l);+ e+ U# J  l$ b+ i2 p! t

  139. ; q3 S# H# d1 t
  140.   fifo->out += len;
    0 t2 t  [( k, I* f$ n3 G
  141. - `. F7 Y; @) m* U+ E4 I# {
  142.   return len;
    ! w8 [/ {( S7 x% s# Z" G
  143. }
    0 @2 L' L, q5 [: }

  144. & Z4 w. P& S. y. N4 ~
  145. /**
    " y# U! o* p# i# e* Q
  146.   * @brief  Determine whether some value is a power of two.  f, ]4 D% p- {8 u0 h: }8 }
  147.   * @param  [in] x: The number to be confirmed.
    : q  S# H. O5 g2 d0 N
  148.   * @retval true:   Yes.- k" w1 \1 A! s- y  X; C9 X
  149.   * @retval false:  No.$ i& G; O' x9 O5 |: s6 h% m1 L
  150.   * @note   Where zero is not considered a power of two.
    ) F: S+ {; e/ t3 y* |
  151.   */
    4 C* V, P3 e. X* k7 l
  152. static bool is_power_of_2(uint32_t x)+ T, d4 M, a+ l* U$ i
  153. {6 M0 S- ]* S& G
  154.   return (x != 0) && ((x & (x - 1)) == 0);$ E0 f2 `& v0 E2 @! |, f& i
  155. }
    ; C  R% K. H; U. ]7 _. T
  156. 2 I8 r' V6 C3 O
  157. /**3 i& i0 |8 T9 Z$ n$ C
  158.   * @brief  Round the given value up to nearest power of two.  t- Z& o2 V$ s: M5 b$ [0 @/ W: Z
  159.   * @param  [in] x: The number to be converted.; J) @) B- K; `# B. D0 b
  160.   * @return The power of two.
    + I/ e9 l, w  C8 Q
  161.   */# @" z, u' g: F7 O8 O
  162. static uint32_t roundup_pow_of_two(uint32_t x)
      Q9 ^8 @& g0 b0 n/ T' T/ Y+ C* a" X
  163. {
    5 y* H+ {1 g( \$ O0 c" z7 l
  164.   uint32_t b = 0;1 ~4 j9 s( q9 M( H8 V$ _

  165. 0 \5 J' _, g! h1 K  ~' F; i
  166.   for(int i = 0; i < 32; i++)4 G$ x0 W3 Y6 l
  167.   {" [! q( `& _/ A6 z/ L# z( d8 x( C
  168.     b = 1UL << i;" d; L3 l" X) F$ q. e+ [8 X4 Q
  169. 8 P) R. t2 n/ p6 B, o
  170.     if(x <= b)3 Q, |) r! |2 F2 X9 w+ L3 d) C
  171.     {
    . l2 q1 `; _7 A5 C
  172.       break;0 T3 Y; G8 @( Z9 c/ R* u) [- _
  173.     }$ }5 O, D% p' [5 |, M8 p2 y
  174.   }
    ) O" n5 K9 |; P7 R8 i; j

  175. 7 @; z" G! ]' m1 _5 t: p
  176.   return b;1 B4 |' s( U' E1 d4 x
  177. }</div><div></div>
复制代码
2 A/ T4 T* e, g- Z9 i7 d% _9 ^2 O1 `

1 ^( H$ n( h% _6 f; G( c) BCAN.h 文件, @1 ^. g+ H" S
  1. /**
    . {' H5 H. z1 \/ y* ^, U2 Y5 L
  2.   ******************************************************************************1 _" b% x, `' ~9 `% g/ {( W0 }
  3.   * @file    CAN.h
    7 c) h2 Q' u. m0 [1 T9 @, w
  4.   * @author  XinLi6 N$ o3 r/ ?2 @6 {* t! C% K% R
  5.   * @version v1.0
    % d. r8 [+ I. m! e) M! E: D
  6.   * @date    24-June-2018: M* J- h) _& ]8 F* ~7 u& c' d
  7.   * @brief   Header file for CAN.c module.
    # Q1 n' ~+ t7 `1 E7 t' Q" `/ w
  8.   ******************************************************************************
    : S* U5 l+ B8 o  i+ {0 r- ]
  9.   * @attention
    * j1 J/ T' d+ O0 V
  10.   *7 l6 B4 K0 F+ G' d
  11.   * <h2><center>Copyright © 2018 XinLi</center></h2>
    ) c3 e% S$ M0 s6 v( n$ ]
  12.   *
    6 |5 P9 n% U( c* n- J! e. [1 G6 P
  13.   * This program is free software: you can redistribute it and/or modify
    8 }8 X7 Z6 [  u' ~& B- T* L7 h
  14.   * it under the terms of the GNU General Public License as published by( b) s8 g# D6 `" ^. C0 [- \3 J% k
  15.   * the Free Software Foundation, either version 3 of the License, or0 x6 l' w7 F2 j" A
  16.   * (at your option) any later version.6 d8 p' @* p/ x  q6 W
  17.   *
    " a7 j( ]" v; i2 `8 e8 t  B. E
  18.   * This program is distributed in the hope that it will be useful,: T& j! _# V: Z' i+ B1 J
  19.   * but WITHOUT ANY WARRANTY; without even the implied warranty of. T$ i2 O; f/ Y+ x) I! C
  20.   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    " L6 ]! U! U7 W! W
  21.   * GNU General Public License for more details.
    . J; k. C# ^( V
  22.   *
    7 W- @( u" K5 t/ ]
  23.   * You should have received a copy of the GNU General Public License
      ?- g+ v& }8 P# J! c. ?) ~3 d
  24.   * along with this program.  If not, see <<a href="https://www.gnu.org/licenses/>." target="_blank">https://www.gnu.org/licenses/>.</a>
    ) l+ [8 K9 V) O: {/ X6 b* _5 z
  25.   *4 D3 E. B" D# t( i: c
  26.   ******************************************************************************
    3 b; q. G+ S. y7 O
  27.   */
    . v3 J+ I/ @, L4 J$ Z

  28. . {- [- H$ d: _7 c9 E5 l
  29. #ifndef __CAN_H7 Y! Q  ~8 W  Q" @4 P4 R0 B
  30. #define __CAN_H. l* K: y5 Q' j7 h& D  f3 a

  31. / v. m% y& i9 Z8 x
  32. #ifdef __cplusplus
    " K) a2 H$ K+ R# U( Q& E% l, X
  33. extern "C" {
    2 ~/ {. P6 V5 r+ ?- }0 U
  34. #endif
    " d$ T( y& G. o  {1 y5 H3 N2 k- N
  35. . F0 w- X6 i' B+ \
  36. /* Header includes -----------------------------------------------------------*/
    ( l! z2 V0 y: d! d
  37. #include "stm32f10x.h"' y4 l4 ~$ A: I
  38. #include <stdbool.h>6 U( x/ u1 B; A# w; s

  39. * _5 f2 u: x! Q* L: j, |3 b& g
  40. /* Macro definitions ---------------------------------------------------------*/7 }5 e! J3 N0 \% b; ]

  41. & R6 z7 p3 Z5 u7 a9 M8 k- }
  42. /******************************* CAN1 Configure *******************************/
    " w  ]2 D' E2 g7 i* Y1 w
  43. #define CAN1_TX_BUFFER_SIZE        (16)* a& v/ V$ O5 K- y
  44. #define CAN1_RX_BUFFER_SIZE        (16)* T) _) @/ R7 _# E

  45. % E; }3 J( n8 C& I2 S* y
  46. #define CAN1_TX_GPIO_CLOCK         RCC_APB2Periph_GPIOB2 a, P4 U0 ^5 ^
  47. #define CAN1_RX_GPIO_CLOCK         RCC_APB2Periph_GPIOB
    , w/ o; w+ x- ^& f4 u! n1 I
  48. 2 d9 e3 Q/ B# f' G$ w9 I& ]2 E+ O& z
  49. #define CAN1_TX_GPIO_PORT          GPIOB; x& N; }" t+ @) ^" V$ V( Y; b; r9 A
  50. #define CAN1_RX_GPIO_PORT          GPIOB- N0 |' p0 S) \+ q- V
  51. 3 |. L# y6 H% {. Z: {0 ^, B6 |, W
  52. #define CAN1_TX_GPIO_PIN           GPIO_Pin_95 t7 ^6 W3 y6 i$ [# A
  53. #define CAN1_RX_GPIO_PIN           GPIO_Pin_8! ^: m7 h  H4 A6 p+ x0 A

  54. 5 X5 |" N$ m+ V: g% T4 z
  55. #define CAN1_IRQ_PREEMPT_PRIORITY  (0)
    8 v9 b- O# P$ |; e' y
  56. #define CAN1_IRQ_SUB_PRIORITY      (0)5 {# |: h5 K+ L, O1 e. G

  57. ! v. Z% O( ^5 }# m
  58. #define CAN1_PORT_REMAP()          GPIO_PinRemapConfig(GPIO_Remap1_CAN1 , ENABLE)# V* o  L* g9 T0 Q( N! E% P
  59. /******************************************************************************/* g' C& `4 }2 T1 [

  60. 9 E1 ~1 @/ N: B: J+ {, T
  61. #ifdef STM32F10X_CL
    0 r8 S4 x# B& W  O+ U, H; B/ p
  62. /******************************* CAN2 Configure *******************************/
    * N7 X& h2 J. K( @4 {. |
  63. #define CAN2_TX_BUFFER_SIZE        (16)
    8 s' K2 f: ?1 w4 n0 M0 ?; [
  64. #define CAN2_RX_BUFFER_SIZE        (16)' Z5 q9 N9 a! k$ B
  65. 0 t: c- L/ u% ?  J2 [( H
  66. #define CAN2_TX_GPIO_CLOCK         RCC_APB2Periph_GPIOB
    0 Z8 Y/ [5 P+ B1 m( R
  67. #define CAN2_RX_GPIO_CLOCK         RCC_APB2Periph_GPIOB
      C: Q; [, O  {- ?2 V' x

  68. # u' k4 D; V/ a1 I. ^- b
  69. #define CAN2_TX_GPIO_PORT          GPIOB
    ( B) Y  f- |. O& H) }
  70. #define CAN2_RX_GPIO_PORT          GPIOB5 j5 q+ U' Y. X8 T  m/ W3 J

  71. 3 g; h' W; V, Z: g& [/ w
  72. #define CAN2_TX_GPIO_PIN           GPIO_Pin_13
    3 I7 A  e- l) L' b
  73. #define CAN2_RX_GPIO_PIN           GPIO_Pin_12/ C" q- ?- @/ m* i& m9 D
  74. * u! U# G% P* J8 z& D
  75. #define CAN2_IRQ_PREEMPT_PRIORITY  (0)
    % e- t. A6 T, I
  76. #define CAN2_IRQ_SUB_PRIORITY      (0)9 {+ t4 ~9 @: q+ ^  C- d

  77. $ ^  m: e3 g" L9 q
  78. #define CAN2_PORT_REMAP()          GPIO_PinRemapConfig(GPIO_Remap_CAN2 , DISABLE)
    , W% a0 I; z2 }
  79. /******************************************************************************/" P9 R' I. p& B+ B+ r  K
  80. #endif /* STM32F10X_CL */
    1 Z, O/ Y8 E+ _# ?  _5 Q5 ?

  81. + \2 W. E; e* ~8 p1 F2 H
  82. /* Type definitions ----------------------------------------------------------*/$ \. D( W+ ^& I
  83. typedef enum
    % z& c8 e; j* [& _  E
  84. {7 T$ D! a9 `7 d- b3 ~5 B
  85.   CAN_WorkModeNormal   = CAN_Mode_Normal,
    6 Y+ L# m$ V: k
  86.   CAN_WorkModeLoopBack = CAN_Mode_LoopBack
    7 _( \. r7 P6 u, m5 S
  87. }CAN_WorkMode;3 j1 s* R  i, U% R3 G7 [& f

  88. 8 s$ R$ u! r$ u2 E% Q+ [: E% C) ~9 q
  89. typedef enum
    , k7 J9 `# K5 z. h- `( j
  90. {
    5 X; J  y, |$ g5 S
  91.   CAN_BaudRate1000K = 6,
    7 G. g7 ]% m/ x2 f, N
  92.   CAN_BaudRate500K  = 12,
    8 x5 T) J1 c, `6 p+ r
  93.   CAN_BaudRate250K  = 24,
    4 J: V: Z! d. v. J
  94.   CAN_BaudRate125K  = 48,
    0 I8 [/ \! {$ z) p# \0 D0 ]
  95.   CAN_BaudRate100K  = 60,, _' o6 f# g& ]7 J- J
  96.   CAN_BaudRate50K   = 120,: f  u0 [4 u+ P% e& Y- z. t
  97.   CAN_BaudRate20K   = 300,6 y. e7 h1 H$ h/ L! N3 ]( x: [. \
  98.   CAN_BaudRate10K   = 600
    / ~- [; z! s/ f- A( o& J
  99. }CAN_BaudRate;/ s4 K2 Z3 d/ N) S; N
  100. ( y, i0 H: j7 `
  101. /* Variable declarations -----------------------------------------------------*/9 y2 ?! |8 F: O. C6 }, ]* r; Y
  102. /* Variable definitions ------------------------------------------------------*/
    ) k4 T4 @1 \% f& }5 R1 L& n
  103. /* Function declarations -----------------------------------------------------*/
    : [5 h6 k. J% P
  104. void CAN_Configure(CAN_TypeDef *CANx, CAN_WorkMode WorkMode, CAN_BaudRate BaudRate, uint32_t StdId, uint32_t ExtId);7 y, c; v! U& A% c0 h- Q9 i; G: ~  _
  105. void CAN_Unconfigure(CAN_TypeDef *CANx);8 E& X1 u; K& i- a# z
  106. % A$ V* C3 Z9 G% N
  107. void CAN_SetTransmitFinishCallback(CAN_TypeDef *CANx, void (*Callback)(void));, J/ g$ F  A- N  R  X" A
  108. void CAN_SetReceiveFinishCallback(CAN_TypeDef *CANx, void (*Callback)(void));: R# f; e/ Z7 Z  @  N4 @

  109. ! g( a% ~1 D: N9 h7 a6 c
  110. uint32_t CAN_SetTransmitMessage(CAN_TypeDef *CANx, CanTxMsg *Message, uint32_t Number);) B! r7 B/ N* [3 Z; Y4 ?
  111. uint32_t CAN_GetReceiveMessage(CAN_TypeDef *CANx, CanRxMsg *Message, uint32_t Number);/ _5 B, a3 Q3 a6 Z$ z) Q

  112. # V: D/ s- _/ f' Z4 y3 B1 U# J
  113. uint32_t CAN_GetUsedTransmitBufferSize(CAN_TypeDef *CANx);
    " X! s# D0 i$ O' W
  114. uint32_t CAN_GetUsedReceiveBufferSize(CAN_TypeDef *CANx);( G% O/ \& [/ @0 [+ k) K
  115. uint32_t CAN_GetUnusedTransmitBufferSize(CAN_TypeDef *CANx);
    - ]' U1 b# e! Z1 C3 T
  116. uint32_t CAN_GetUnusedReceiveBufferSize(CAN_TypeDef *CANx);7 C# K, ~! j" X/ \# h1 U5 g) H) m

  117. 5 i: g( u9 a1 d
  118. bool CAN_IsTransmitBufferEmpty(CAN_TypeDef *CANx);8 t5 V0 G- E8 H# i
  119. bool CAN_IsReceiveBufferEmpty(CAN_TypeDef *CANx);, x- p% @& t" i, i( C
  120. bool CAN_IsTransmitBufferFull(CAN_TypeDef *CANx);
    ( d7 b  S6 A6 u7 D
  121. bool CAN_IsReceiveBufferFull(CAN_TypeDef *CANx);" p* B8 J" b" }9 o

  122. ' V7 e1 @; J! Y: D0 M# F
  123. void CAN_ClearTransmitBuffer(CAN_TypeDef *CANx);
      T& `7 B. n1 E& _  P
  124. void CAN_ClearReceiveBuffer(CAN_TypeDef *CANx);
    ' \8 J+ Z/ s. P$ F0 U

  125. 3 _1 r- Z3 W. y7 M
  126. bool CAN_IsTransmitMessage(CAN_TypeDef *CANx);
    9 z/ r/ I  r0 W) o

  127. 6 o  i* o- l) x% o0 T" Y" [- \
  128. /* Function definitions ------------------------------------------------------*/
    1 V4 o- J: I) R4 B' [( j

  129. + {( i. u. ]! d9 F
  130. #ifdef __cplusplus
    $ h/ p& x  p+ G  r/ D* w$ j! r
  131. }
    - {2 p* C8 g- W2 m7 q$ D
  132. #endif) {" c- ^* }& j& ^

  133. 5 Z0 B) y! N5 m/ g, e* `
  134. #endif /* __CAN_H */
      m8 y$ L. L2 M/ R3 f
复制代码
, A3 ~3 G) c+ k+ v1 c3 w
- p) g# {6 _! R# H: u+ K: k
9 r# u- \0 r' \+ W: c6 ?* @
CAN.c 文件
& E, j3 z7 B) c& a! H5 m2 f
9 p- ^. v2 U' l' e8 m# E
  1. /**
    4 E- w( l  j9 V+ a3 _" u3 }) D9 t
  2.   ******************************************************************************
    " {$ D- l' z5 e& w
  3.   * @file    CAN.c
    4 e# ~1 V. T  j% w' @
  4.   * @author  XinLi
    2 r' i9 a) x( F' N
  5.   * @version v1.0
    # N. u* B; m* L% t5 x
  6.   * @date    24-June-2018; U5 w0 n5 B) @. @- Z2 v
  7.   * @brief   CAN module driver.
    7 [  B( s, D; v) S$ z8 K
  8.   ******************************************************************************
    7 c' n- N3 z, q' K4 H
  9.   * @attention. W: T% _: m2 J% c) s/ |: W2 c- ?
  10.   *0 T/ P. ^0 h5 @) @; i
  11.   * <h2><center>Copyright © 2018 XinLi</center></h2>0 Q5 w7 ]3 P7 q& I: q) G' E2 ]0 b
  12.   *! }2 a; P" ]4 O9 C, r! [
  13.   * This program is free software: you can redistribute it and/or modify
    $ }4 D( Q0 Y3 W2 ?/ d2 R) ~9 c
  14.   * it under the terms of the GNU General Public License as published by9 p- D1 S) A' e
  15.   * the Free Software Foundation, either version 3 of the License, or' q; ~/ \5 m! D( `0 V3 _/ K
  16.   * (at your option) any later version.5 o5 \! G7 K+ R, P
  17.   *; d5 s! K0 V4 T) Q9 q, Z5 A
  18.   * This program is distributed in the hope that it will be useful,9 V4 P  M6 I# V+ r1 E* B8 H) s
  19.   * but WITHOUT ANY WARRANTY; without even the implied warranty of0 P2 |3 u4 ]0 ^
  20.   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    / `. b( f! p/ N, h* {( g9 Y( I
  21.   * GNU General Public License for more details.
    ) `+ D/ [8 e$ q, S! T7 F
  22.   *+ o( M: G! k/ H! H
  23.   * You should have received a copy of the GNU General Public License
    7 M! z! p+ o. u
  24.   * along with this program.  If not, see <<a href="https://www.gnu.org/licenses/>." target="_blank">https://www.gnu.org/licenses/>.</a>( u5 j( s2 v# L4 m& V
  25.   *# [6 R7 K" _( |& E* ]. y) y
  26.   ******************************************************************************
    / \) \  y8 V1 @  w# j3 w' s
  27.   */
      j" Z+ W1 I9 g6 G& Q' W: p

  28. 6 g2 c: j/ N  s' }0 u' i
  29. /* Header includes -----------------------------------------------------------*/
    ! Z$ D3 c4 ?# h7 Q
  30. #include "CAN.h"
    9 B* Q( [8 a9 Q0 P1 y% S
  31. #include "RingBuffer.h"
    + `. c/ z* T0 h0 `! }# I
  32. 5 b3 x9 q8 w2 q; }) {# ?3 p* X. R
  33. /* Macro definitions ---------------------------------------------------------*/
    , l" Y; h9 E, @1 \  _: ^
  34. /* Type definitions ----------------------------------------------------------*/) D; h2 S/ `; B& p. ^! b0 V" f
  35. /* Variable declarations -----------------------------------------------------*/: u4 N/ M5 a! S2 P& P7 w/ j) y
  36. static volatile bool can1InitFlag     = false;
    ) r6 ~2 ^2 V( Z8 ?
  37. static volatile bool can1TransmitFlag = false;
    . Z( @2 |  g6 R; a3 o2 i

  38. 2 D* G' D( _; q) W7 }; R8 l
  39. static volatile void (*can1TransmitFinishCallback)(void) = 0;. j9 }" g- o0 Z+ v$ B/ K2 u
  40. static volatile void (*can1ReceiveFinishCallback)(void)  = 0;
    ) G/ i* `7 M; q( ]: G8 P
  41. % O9 E! X% H5 b* h! {. J. c& b
  42. static RingBuffer *can1TxBuffer = 0;7 [: J4 M/ F. G
  43. static RingBuffer *can1RxBuffer = 0;/ I. |- U8 T1 ^6 E" V
  44. * c1 l$ a/ i' E* w  t! w# ]
  45. #ifdef STM32F10X_CL
    + O/ V* `& R- Q8 V
  46. static volatile bool can2InitFlag     = false;
    $ B, v: E# g, `- G0 c# a  Y
  47. static volatile bool can2TransmitFlag = false;1 P9 G6 _* |/ z% R
  48. & G; a; m: j8 ^) ~( U& s
  49. static volatile void (*can2TransmitFinishCallback)(void) = 0;
    4 P$ T" }! F/ x; ^# m! w9 @( [
  50. static volatile void (*can2ReceiveFinishCallback)(void)  = 0;
    9 A7 U( t. d3 ]  Z
  51. 9 N5 b4 E- A) x
  52. static RingBuffer *can2TxBuffer = 0;
    - e$ R) k" M  T$ x' P
  53. static RingBuffer *can2RxBuffer = 0;
    ! X" L6 H' C- [, p, \
  54. #endif /* STM32F10X_CL */
    - x6 Z( K# r- [. K* q

  55. / }4 f$ W* E9 z
  56. /* Variable definitions ------------------------------------------------------*/2 Y9 b! M3 R) Z* e$ e( c
  57. /* Function declarations -----------------------------------------------------*/
    4 ?( ~# g# x* e" h) R+ u
  58. /* Function definitions ------------------------------------------------------*/: N) h  `/ v! X1 w
  59. : r5 Y! ?- n9 j# Q+ }' Y
  60. /**
    ; I4 D' i" l) N8 m& c
  61.   * @brief  CAN configure.8 v& N6 |2 e7 z( L8 p5 L
  62.   * @param  [in] CANx:     Where x can be 1 or 2 to select the CAN peripheral.
    2 J0 L& ]5 I0 A' I% N7 t7 ?
  63.   * @param  [in] WorkMode: Work mode." q2 ?8 E2 s9 I
  64.   * @param  [in] BaudRate: Communication baud rate.& m4 A$ |/ g) m7 \- \
  65.   * @param  [in] StdId:    Filter standard frame ID.
    : c/ d+ p* s) |8 h/ G
  66.   * @param  [in] ExtId:    Filter extended frame ID., X- G$ H5 h; ~
  67.   * @return None.
    , X# l4 d- V( Z. \% C2 P& Q# e
  68.   */, ^2 I9 g/ T: a2 d3 {* o
  69. void CAN_Configure(CAN_TypeDef *CANx, CAN_WorkMode WorkMode, CAN_BaudRate BaudRate, uint32_t StdId, uint32_t ExtId)
    1 ^& s3 `5 S# V, P2 ]! I' V* b! z
  70. {
    " H: ?: U& b9 }) N5 _. a  A/ V
  71.   GPIO_InitTypeDef      GPIO_InitStructure      = {0};6 @( }* p# @$ g! O
  72.   CAN_InitTypeDef       CAN_InitStructure       = {0};! l( N0 C. ?! q( V
  73.   CAN_FilterInitTypeDef CAN_FilterInitStructure = {0};
    - s* j' A+ b  T# ^# k7 U
  74.   NVIC_InitTypeDef      NVIC_InitStructure      = {0};
    - [" d6 @  R. s$ x! g
  75. 5 u# `8 {! R! ~
  76.   if(CANx == CAN1)
    # r. W) p$ S: q$ k
  77.   {& _- u- _" I4 q( R
  78.     if(can1InitFlag == false)( |! j" C& b9 ?0 h4 d1 }
  79.     {% l/ W: w' j/ ~0 c+ Y
  80.       can1InitFlag = true;$ N0 o" ~4 d# R  b* Z! g
  81. 3 z, Z4 u$ s- y4 S! @9 B
  82.       can1TransmitFlag = false;
    - z" k  C3 ~" |: k

  83. ' y2 P( G: N1 h( x2 e9 O* Q: E
  84.       can1TransmitFinishCallback = 0;" }5 X. s; t# |
  85.       can1ReceiveFinishCallback  = 0;
    3 W8 y7 c! q' Q
  86. 3 E9 z# W  E- m" A
  87.       can1TxBuffer = RingBuffer_Malloc(sizeof(CanTxMsg) * CAN1_TX_BUFFER_SIZE);
    2 {) `' H" @1 B! ~: \# [
  88.       can1RxBuffer = RingBuffer_Malloc(sizeof(CanRxMsg) * CAN1_RX_BUFFER_SIZE);
    3 ^# p1 B. A) U9 T3 v

  89. 2 x. a3 L4 R% a4 L) f) _
  90. #ifdef STM32F10X_CL
    5 `" W& `. n; [$ R% a, C5 \
  91.       if(can2InitFlag == false)" ~- R2 G+ D' B  c8 x) J
  92. #endif /* STM32F10X_CL */
    # j* N+ `, M) V) w  p3 L
  93.       {
    9 k; l/ k4 L* @, o) p
  94.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);# \$ N$ h7 \0 s
  95.       }$ `3 s* `: o; w  Q

  96. 7 q/ _7 P/ L- B. z3 g3 w2 L- X
  97.       RCC_APB2PeriphClockCmd(CAN1_TX_GPIO_CLOCK | CAN1_RX_GPIO_CLOCK | RCC_APB2Periph_AFIO, ENABLE);1 G! {! s& @( ?2 T" X  W

  98.   d. |" N& }+ X8 [: P7 k
  99.       GPIO_InitStructure.GPIO_Pin   = CAN1_TX_GPIO_PIN;- N. _( _, P. w0 X0 ]; P9 `
  100.       GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF_PP;" T$ w9 p4 g$ ^+ @% W" A6 M
  101.       GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;- B, z9 |9 v4 @( m4 `. |
  102.       GPIO_Init(CAN1_TX_GPIO_PORT, &GPIO_InitStructure);( a5 v: C( M3 c# A

  103. % @  V: u7 A! [* m
  104.       GPIO_InitStructure.GPIO_Pin   = CAN1_RX_GPIO_PIN;8 Q! ?4 ~6 U/ `
  105.       GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IPU;
    5 w" \, ~) ^8 R2 E8 K5 u& {: T
  106.       GPIO_Init(CAN1_RX_GPIO_PORT, &GPIO_InitStructure);1 r1 j8 q7 D, K7 C; O, b2 G

  107. ) p) m0 j, O5 H
  108.       CAN1_PORT_REMAP();4 }5 o$ U( W8 N4 U4 {5 Y
  109. 8 x. ]+ T" I# m
  110.       CAN_InitStructure.CAN_Prescaler = BaudRate;
    4 [. a( M4 }" Q
  111.       CAN_InitStructure.CAN_Mode      = WorkMode;+ l+ z0 V9 m( R) [" s1 L, C
  112.       CAN_InitStructure.CAN_SJW       = CAN_SJW_1tq;
    0 f" @) Z. r" a' ^
  113.       CAN_InitStructure.CAN_BS1       = CAN_BS1_3tq;: U* H2 w3 q; g! }% n/ X
  114.       CAN_InitStructure.CAN_BS2       = CAN_BS2_2tq;
    ' s* ?- g4 N8 ^9 V
  115.       CAN_InitStructure.CAN_TTCM      = DISABLE;
    ; l0 R# [4 F3 ^5 y5 {* f
  116.       CAN_InitStructure.CAN_ABOM      = ENABLE;) S$ `$ M0 T+ o
  117.       CAN_InitStructure.CAN_AWUM      = DISABLE;
    & V. }. k3 N7 o5 @2 \/ ]0 S
  118.       CAN_InitStructure.CAN_NART      = ENABLE;
    ) s) |" p0 {2 B* j6 K) z
  119.       CAN_InitStructure.CAN_RFLM      = DISABLE;
    9 ~" _4 r+ p! y: M
  120.       CAN_InitStructure.CAN_TXFP      = ENABLE;
    ( i- n* w9 k% D6 D
  121. # b6 I6 A3 k/ Q! ]0 r3 \
  122.       CAN_DeInit(CAN1);. W" A+ Q3 C/ T; W
  123.       CAN_Init(CAN1, &CAN_InitStructure);
    + o8 Y  B1 l% A% K

  124. 0 M4 f9 e& v  Y6 d  a; W
  125.       CAN_FilterInitStructure.CAN_FilterIdHigh         = (uint16_t)((((StdId<<18)|ExtId)<<3)>>16);
    $ i* z2 c7 E/ G1 n( l3 G3 j5 [
  126.       CAN_FilterInitStructure.CAN_FilterIdLow          = (uint16_t)(((StdId<<18)|ExtId)<<3);. G7 h( M) f8 J, V+ y" I" y
  127.       CAN_FilterInitStructure.CAN_FilterMaskIdHigh     = (~((uint16_t)((((StdId<<18)|ExtId)<<3)>>16)))&0xFFFF;
    % R2 f( a! K4 l- G. v% O
  128.       CAN_FilterInitStructure.CAN_FilterMaskIdLow      = (~((uint16_t)(((StdId<<18)|ExtId)<<3)))&0xFFF8;$ v& B5 A& d: h% ^, k9 _+ F
  129.       CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0;9 C1 E* i, N+ Y9 p- V: E9 ?$ T
  130.       CAN_FilterInitStructure.CAN_FilterNumber         = 0;0 {! W% A3 Y$ J' J/ W( H
  131.       CAN_FilterInitStructure.CAN_FilterMode           = CAN_FilterMode_IdMask;( W1 f5 e+ N# ]. P
  132.       CAN_FilterInitStructure.CAN_FilterScale          = CAN_FilterScale_32bit;
    0 m8 X- h( N5 V- o, u$ M$ T1 H* p
  133.       CAN_FilterInitStructure.CAN_FilterActivation     = ENABLE;
    6 P- S- N5 ?  q& o, i1 A% `5 X
  134.       CAN_FilterInit(&CAN_FilterInitStructure);
    1 y7 p  C6 ]  i% m
  135. 6 G7 o* x4 p! }: j- k
  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 |
    " C9 `1 `  j; d, G9 m6 w7 h
  137.                          CAN_IT_WKU | CAN_IT_SLK  |CAN_IT_EWG | CAN_IT_EPV  | CAN_IT_BOF  | CAN_IT_LEC | CAN_IT_ERR, DISABLE);
    & g9 I& n8 A, ]5 H! N2 k; d
  138.       CAN_ITConfig(CAN1, CAN_IT_TME | CAN_IT_FMP0, ENABLE);
    - D6 \# S) w! f( C" A. C4 F

  139. & A2 C! V/ y% [6 m( ^5 a
  140. #ifdef STM32F10X_CL  v; B7 g* j7 I; r
  141.       NVIC_InitStructure.NVIC_IRQChannel                   = CAN1_TX_IRQn;
    # t' p* J$ @# U, @/ h  P$ i1 m
  142. #else
    1 g* s0 p4 k+ U  s% k) q) n/ `+ k0 k7 |
  143.       NVIC_InitStructure.NVIC_IRQChannel                   = USB_HP_CAN1_TX_IRQn;- _+ r. j* S, e! k' W6 n
  144. #endif /* STM32F10X_CL */1 n+ o+ _7 k. q3 z1 s
  145.   M/ w: E# t) p; X0 U- ]3 l
  146.       NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = CAN1_IRQ_PREEMPT_PRIORITY;
    6 _8 g& c6 B" e$ E7 Q, z  X
  147.       NVIC_InitStructure.NVIC_IRQChannelSubPriority        = CAN1_IRQ_SUB_PRIORITY;
    ' h4 _3 M+ ^. w
  148.       NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;+ p# a  W  C7 H+ k
  149.       NVIC_Init(&NVIC_InitStructure);. A3 d, ~4 Q' o) ?( A
  150. ) S4 f# T, y9 d8 B8 D5 T: r
  151. #ifdef STM32F10X_CL
    6 t: t# S% Q! {1 E  I+ K' p" _( Y
  152.       NVIC_InitStructure.NVIC_IRQChannel                   = CAN1_RX0_IRQn;( @5 S4 a. ]( U* v) y% e* ~3 R, T
  153. #else
    $ H  ^6 D$ z- j- X- C
  154.       NVIC_InitStructure.NVIC_IRQChannel                   = USB_LP_CAN1_RX0_IRQn;
    ; u8 |- _* o+ P' Z: o
  155. #endif /* STM32F10X_CL */
    / M2 ?) S3 [' ^/ w
  156. # |0 a: N' }  o" T5 C" g( ?" R% }
  157.       NVIC_Init(&NVIC_InitStructure);& _) H( b5 _" J6 H
  158.     }
    3 R5 h( ?- L! T) G
  159.   }
    ( @6 ?0 e# y: D# _. U$ S1 i

  160. , x0 \/ i: ]& S
  161. #ifdef STM32F10X_CL$ b/ k$ Q, x4 c6 N- q
  162.   if(CANx == CAN2)
    , S0 p6 c" y1 g: n. J
  163.   {
    ) N$ r6 g- ~9 R) y
  164.     if(can2InitFlag == false)
    ) H# _3 ?- ]4 _8 z7 V
  165.     {
    1 Y( i0 R# \1 ?/ h
  166.       can2InitFlag = true;
    " u  _7 `9 P3 T+ @: D, B1 P
  167. # g& k7 A( k( u7 \
  168.       can2TransmitFlag = false;
    , J, q" l3 J) n8 @: \; m4 u$ {8 P& Z

  169. - t- A; m' q) D4 a0 m) S- }: {: M
  170.       can2TransmitFinishCallback = 0;
    4 Z: {* @) x  ^4 [* h
  171.       can2ReceiveFinishCallback  = 0;  U# e3 p; s% I4 W5 T. Y' ?" a. M! {

  172. 3 {2 S: [: g4 i6 M. s/ @, c
  173.       can2TxBuffer = RingBuffer_Malloc(sizeof(CanTxMsg) * CAN2_TX_BUFFER_SIZE);% X: q0 I* m- A- A6 m$ |+ N
  174.       can2RxBuffer = RingBuffer_Malloc(sizeof(CanRxMsg) * CAN2_RX_BUFFER_SIZE);
    0 `6 S2 Q# y3 d" o, v4 Z6 d
  175. ; c- g/ p+ o, P6 x4 G/ m# x
  176.       if(can1InitFlag == false)
    ' ]' l( F6 f) O. `6 x: ?
  177.       {
    # ^1 C$ P& j/ N: Z
  178.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);; J5 L: R' S6 w9 D. q
  179.       }
      ?( p9 s+ }/ O8 }' d6 l" Q

  180. 6 h9 v8 @1 Y# W, N* E; \
  181.       RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2, ENABLE);  w* T7 F, c% `* H' O- Q) @! l1 i
  182.       RCC_APB2PeriphClockCmd(CAN2_TX_GPIO_CLOCK | CAN2_RX_GPIO_CLOCK | RCC_APB2Periph_AFIO, ENABLE);% c2 s; P- c3 X5 l( |) t
  183. " m$ J+ B/ I3 U( x9 F! ^  R
  184.       GPIO_InitStructure.GPIO_Pin   = CAN2_TX_GPIO_PIN;
    9 `, Z4 F4 i* u6 _9 e  `
  185.       GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF_PP;: b5 f% y- m/ ^1 l) m
  186.       GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    5 H: ~# ^. ~- }5 u
  187.       GPIO_Init(CAN2_TX_GPIO_PORT, &GPIO_InitStructure);
    1 }8 Z8 L6 _! O5 ^8 |+ c
  188. / l4 p/ w2 t* X5 g% [9 `: C% `
  189.       GPIO_InitStructure.GPIO_Pin   = CAN2_RX_GPIO_PIN;
      p. w  H& c6 ~4 l  @) F
  190.       GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IPU;
    6 O9 B! t4 Z7 t3 C5 P
  191.       GPIO_Init(CAN2_RX_GPIO_PORT, &GPIO_InitStructure);: w" a$ X) O3 O1 m7 Q

  192. / O- D  ?' @/ e: `
  193.       CAN2_PORT_REMAP();7 h8 X( i6 N6 ?5 F' h1 `3 \7 u
  194. 6 m5 l. A- T" m3 H( x5 D) g# Z0 }2 ?
  195.       CAN_InitStructure.CAN_Prescaler = BaudRate;5 _+ n/ j' Z) {/ a" p
  196.       CAN_InitStructure.CAN_Mode      = WorkMode;/ a2 |: p+ Y$ y9 v7 L9 }
  197.       CAN_InitStructure.CAN_SJW       = CAN_SJW_1tq;  ^' T2 v( Q, d3 _. l$ i& S
  198.       CAN_InitStructure.CAN_BS1       = CAN_BS1_3tq;
    / Y0 [/ B, u& H  t. M+ D
  199.       CAN_InitStructure.CAN_BS2       = CAN_BS2_2tq;
    / H. j. t, Y5 _6 {
  200.       CAN_InitStructure.CAN_TTCM      = DISABLE;) R6 p" @' R& O7 B: x
  201.       CAN_InitStructure.CAN_ABOM      = ENABLE;# l$ n/ C  t' N, N
  202.       CAN_InitStructure.CAN_AWUM      = DISABLE;
    0 d* \+ H" P- E
  203.       CAN_InitStructure.CAN_NART      = ENABLE;; o/ d: n8 ]+ L, H, N* z
  204.       CAN_InitStructure.CAN_RFLM      = DISABLE;/ ?5 ^/ |  b1 J# B7 B
  205.       CAN_InitStructure.CAN_TXFP      = ENABLE;
    + h" C+ s5 g: M% G/ _& X6 V

  206. * s& \3 h. H7 S  K% i9 ~
  207.       CAN_DeInit(CAN2);* e  A$ Y" y4 A) Y5 Y7 e& O
  208.       CAN_Init(CAN2, &CAN_InitStructure);3 g0 K) |$ h) @9 ?5 }2 ]$ L
  209. ( ^! H/ Y6 N! s9 e+ e2 f1 A
  210.       CAN_FilterInitStructure.CAN_FilterIdHigh         = (uint16_t)((((StdId<<18)|ExtId)<<3)>>16);
    ) r# k3 ~1 D# I
  211.       CAN_FilterInitStructure.CAN_FilterIdLow          = (uint16_t)(((StdId<<18)|ExtId)<<3);
    ; L1 b  D/ i- M4 I9 r/ W
  212.       CAN_FilterInitStructure.CAN_FilterMaskIdHigh     = (~((uint16_t)((((StdId<<18)|ExtId)<<3)>>16)))&0xFFFF;5 A& C6 d* B% ~
  213.       CAN_FilterInitStructure.CAN_FilterMaskIdLow      = (~((uint16_t)(((StdId<<18)|ExtId)<<3)))&0xFFF8;
    2 T0 F( N/ `' w  j7 }
  214.       CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0;6 B8 q) X  j! Y7 o* t4 A2 f- W
  215.       CAN_FilterInitStructure.CAN_FilterNumber         = 14;
    6 z: Q) Q0 _) q6 c1 h" d
  216.       CAN_FilterInitStructure.CAN_FilterMode           = CAN_FilterMode_IdMask;7 e& G' f7 I0 O8 G8 U2 t$ Q- k2 g2 j
  217.       CAN_FilterInitStructure.CAN_FilterScale          = CAN_FilterScale_32bit;
    " J: A- S' Z& F- d$ K
  218.       CAN_FilterInitStructure.CAN_FilterActivation     = ENABLE;2 C1 ]% d* x/ Z3 @
  219.       CAN_FilterInit(&CAN_FilterInitStructure);
    + i2 c( v' K, y! E
  220. 9 N$ l* D. Y" o5 ]( }8 _
  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 |
    4 i: ]- X7 ]4 M: I2 R, E* |/ S3 Q
  222.                          CAN_IT_WKU | CAN_IT_SLK  |CAN_IT_EWG | CAN_IT_EPV  | CAN_IT_BOF  | CAN_IT_LEC | CAN_IT_ERR, DISABLE);
    , K' V( F$ `, B8 N$ `( u9 x/ t
  223.       CAN_ITConfig(CAN2, CAN_IT_TME | CAN_IT_FMP0, ENABLE);
    # ^9 V/ @) w% T) q6 l( j

  224. ' @  V1 i# |6 Q+ e
  225.       NVIC_InitStructure.NVIC_IRQChannel                   = CAN2_TX_IRQn;
    , M& O6 F- w$ a% e  K
  226.       NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = CAN2_IRQ_PREEMPT_PRIORITY;+ y) ?2 W9 v, k" T# o% M" E
  227.       NVIC_InitStructure.NVIC_IRQChannelSubPriority        = CAN2_IRQ_SUB_PRIORITY;
      F* u4 e6 U& D
  228.       NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;2 m5 t/ H4 u7 c) _0 [+ J6 ~( i
  229.       NVIC_Init(&NVIC_InitStructure);% R) |) l" _# v) q' f6 r5 a
  230. * t. j) Y0 ?1 O, f2 o" u4 E9 e6 _
  231.       NVIC_InitStructure.NVIC_IRQChannel                   = CAN2_RX0_IRQn;. X3 k: c9 ^! j
  232.       NVIC_Init(&NVIC_InitStructure);& x- y/ u% K& p, g3 i" j1 e' ~+ o
  233.     }
    ' K) A+ Y# E; v9 q
  234.   }
    " w3 s& B$ @3 I, u+ P
  235. #endif /* STM32F10X_CL */& G) k: C, y' I4 b5 [) V* X
  236. }
    * M1 k: H4 K9 A" m) q

  237. 9 B9 F0 x7 L5 w2 E& `
  238. /**
    0 w' ]! k7 ^2 z5 M/ c$ f- [
  239.   * @brief  CAN unconfigure.$ P: s1 {# l, c2 S
  240.   * @param  [in] CANx: Where x can be 1 or 2 to select the CAN peripheral.5 o, p. M/ l- m3 J3 U9 U1 Q3 w& S
  241.   * @return None.' L; L8 S0 B' }+ Y4 `
  242.   */, o0 t% X+ j) l5 p! G
  243. void CAN_Unconfigure(CAN_TypeDef *CANx)
    : z7 Z7 s9 x9 O
  244. {  E* h, u6 w& b
  245.   NVIC_InitTypeDef NVIC_InitStructure = {0};) ^9 M- j7 O: s8 Q
  246. 8 w3 k2 @$ `8 Y' W9 |
  247.   if(CANx == CAN1)
    + M. n5 |( D# _2 o7 ^9 ?
  248.   {
    ! s# U2 @. r2 R% M6 D9 a6 J
  249.     if(can1InitFlag == true)& H/ x5 t8 _2 ]# h7 o" r* }# n4 g' j
  250.     {" y( y+ G* I/ |4 ~1 d: i
  251.       can1InitFlag = false;$ L' b0 q3 A- t% L
  252. 9 B( W/ b+ C6 M+ h! s. b
  253. #ifdef STM32F10X_CL+ b& g2 j+ x- W" d2 G/ {( B
  254.       NVIC_InitStructure.NVIC_IRQChannel                   = CAN1_TX_IRQn;
    3 }+ T$ Z" f, {* h) j. i; }+ `
  255. #else! F* t6 }& v& ]# X
  256.       NVIC_InitStructure.NVIC_IRQChannel                   = USB_HP_CAN1_TX_IRQn;
    ) F) ~- Q1 K# b8 a
  257. #endif /* STM32F10X_CL */
    3 x0 C+ ?! `+ u6 X' \; G

  258. 6 [' k/ S9 {! b1 A% F+ n( Z
  259.       NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = CAN1_IRQ_PREEMPT_PRIORITY;
    8 M# w" f+ p7 w# M# q
  260.       NVIC_InitStructure.NVIC_IRQChannelSubPriority        = CAN1_IRQ_SUB_PRIORITY;
    ( N7 H$ c: H/ r7 {. b$ q
  261.       NVIC_InitStructure.NVIC_IRQChannelCmd                = DISABLE;
    6 U; S2 v  t, M
  262.       NVIC_Init(&NVIC_InitStructure);
    6 y* e4 t' g1 B. H' B! [3 U1 {
  263. # X3 q. b" N0 U/ [: k
  264. #ifdef STM32F10X_CL
    - R+ S1 N0 [4 N% s2 W( A3 Z+ \
  265.       NVIC_InitStructure.NVIC_IRQChannel                   = CAN1_RX0_IRQn;
    3 Y; h' C$ m$ L
  266. #else3 @& L) A: s# R6 y8 d
  267.       NVIC_InitStructure.NVIC_IRQChannel                   = USB_LP_CAN1_RX0_IRQn;
    % B- P. F$ H! |
  268. #endif /* STM32F10X_CL */
    : ], F  t/ y& e. l# \. c3 S  W! ?

  269. 5 x  i7 b" X( Y6 X0 j7 Y' H# y
  270.       NVIC_Init(&NVIC_InitStructure);
    8 j. a/ t; v. @7 B( R/ d' c8 V; h

  271. % [) G! i- u9 R
  272.       CAN_DeInit(CAN1);3 y5 n- s1 C# _1 e/ n

  273. ( Y6 r" n: ?  o! L& W
  274. #ifdef STM32F10X_CL
    " T! ]. \" o" M. `# G5 _, q$ O- g
  275.       if(can2InitFlag == false)
    0 k  ?0 _; D. ^+ N# y
  276. #endif /* STM32F10X_CL */7 ~0 V( X9 F: O. \2 }
  277.       {% x# T, }2 R  g5 B5 W- z7 {
  278.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, DISABLE);, P' M. N* N# D( K" N
  279.       }
    5 a. a9 G1 e9 C5 Y, n, a3 Y8 q5 s
  280. + v7 {7 A% X* ^& j  }, j/ X/ x
  281.       can1TransmitFlag = false;, {8 J. x8 @3 l# M. w
  282. $ M6 B+ w/ z' O; a
  283.       can1TransmitFinishCallback = 0;2 s) }6 z7 |) R6 i: j8 p
  284.       can1ReceiveFinishCallback  = 0;
    1 ~. R, |! e$ S5 V% y, P* k* y% ^

  285. + W0 e4 F7 J* r4 |
  286.       RingBuffer_Free(can1TxBuffer);
    $ B: v4 L. w$ d
  287.       RingBuffer_Free(can1RxBuffer);
    , u& b8 o, m3 |' D3 r6 i/ L9 K' N3 h
  288.     }
    7 U0 x! |, w! J
  289.   }* s2 E/ [" Z; f! B& \# A# C
  290. " @; ?" [# N  F5 d
  291. #ifdef STM32F10X_CL1 v  r; t: j2 o
  292.   if(CANx == CAN2)
    ) k+ X' k, ]1 a! J% O
  293.   {
    " ~3 W/ q( k9 V3 \8 ?
  294.     if(can2InitFlag == true), l7 Y" z7 E+ |6 c, E% g5 H
  295.     {4 U! Z1 i0 K. C  `0 b4 E
  296.       can2InitFlag = false;
    ' T3 U* C: H9 j& k

  297. " M7 Y: O& i) o7 V2 Q0 v* ^: @
  298.       NVIC_InitStructure.NVIC_IRQChannel                   = CAN2_TX_IRQn;
    ; ~  e3 f; l# ~7 @4 \# S* ]
  299.       NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = CAN2_IRQ_PREEMPT_PRIORITY;9 g9 R% o5 p* r/ i, W5 Q6 A
  300.       NVIC_InitStructure.NVIC_IRQChannelSubPriority        = CAN2_IRQ_SUB_PRIORITY;! F# h7 W& X! B7 w
  301.       NVIC_InitStructure.NVIC_IRQChannelCmd                = DISABLE;
    ; g. L  D# \: v% ]
  302.       NVIC_Init(&NVIC_InitStructure);
    4 q) J) y  L$ `( C. j5 i
  303.   b+ y! j3 }/ T, a: H' K, Z* I
  304.       NVIC_InitStructure.NVIC_IRQChannel                   = CAN2_RX0_IRQn;* Q* M! B) S' C0 z( Y! V8 d
  305.       NVIC_Init(&NVIC_InitStructure);6 p* |" x$ ?  u- O) c- g
  306. 9 e. H1 P$ b9 u* j/ l% m  H
  307.       CAN_DeInit(CAN2);
    % y* x# u( s8 T$ t6 g

  308. 3 J; I1 ~) a! ?/ ~
  309.       if(can1InitFlag == false)
    - b  h; w8 g" d0 A9 h
  310.       {" H1 V6 J0 i3 @' p- D
  311.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, DISABLE);
    & }: [9 y) `# d9 e4 ~$ |3 |
  312.       }# y) G3 |; i+ b) q7 d6 }" ~

  313. ; m, E7 E+ N) p4 B. d- `# i
  314.       RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2, DISABLE);' ~0 T, W; L. d! `' A+ d

  315. 1 L: [+ P0 V& m
  316.       can2TransmitFlag = false;
    # Y# y: g$ r, r% {
  317. - Z/ F6 R+ w6 O# g3 y' _8 Y
  318.       can2TransmitFinishCallback = 0;; ~' m6 H/ M; c- n: W/ K
  319.       can2ReceiveFinishCallback  = 0;
    3 O. m' z9 H# S( D

  320. : w* ?9 P) \/ [& Y) g
  321.       RingBuffer_Free(can2TxBuffer);8 m4 `. }5 g8 u, Z! y: ^' q
  322.       RingBuffer_Free(can2RxBuffer);
    ; a1 _$ l4 T% }/ a3 r
  323.     }
    % [$ u4 r( ?7 I' C( [
  324.   }
    ( g" H) F+ T2 n# b
  325. #endif /* STM32F10X_CL */) j  c& E2 ~% b" M( L$ e5 n
  326. }
    $ _* ]' V, o+ |8 A

  327. 6 Q: M( V- G! n- T& |8 }* Q; w7 [) s
  328. /**7 w9 x+ \9 \. k2 E, l
  329.   * @brief  CAN set transmit finish callback.( o8 b8 J# S: j: a, C3 L# X
  330.   * @param  [in] CANx:     Where x can be 1 or 2 to select the CAN peripheral.4 @( q& y: r- R% H/ N
  331.   * @param  [in] Callback: Callback.
    : b& f7 ]5 {: m
  332.   * @return None.
    3 e/ z/ |: G# B) R% j
  333.   */
    + V( m7 u# a$ ?7 {
  334. void CAN_SetTransmitFinishCallback(CAN_TypeDef *CANx, void (*Callback)(void))
    $ |+ e8 }1 |7 P! ^2 p/ J
  335. {; u9 G; ^9 N2 a5 q; E1 v/ h
  336.   if(CANx == CAN1)9 H0 B9 l) \3 r% k5 f
  337.   {& q) Q/ |+ n8 t8 y9 H  N1 f
  338.     if(can1InitFlag == true), l# D, z$ U- X2 R5 r
  339.     {# d# a" z9 `# p& l; v) c
  340.       can1TransmitFinishCallback = (volatile void (*)(void))Callback;
    1 f9 Y+ l" G7 s8 y1 |) a
  341.     }0 k& O' s6 d! e3 R' f4 F0 K
  342.   }, b. Y- P! c$ \0 E1 y

  343. 0 u& r$ p9 @' f  u8 A0 t" S6 u# V/ |
  344. #ifdef STM32F10X_CL' b& W' Q# o' Q$ H, U  z5 y6 f5 k
  345.   if(CANx == CAN2)
    : y+ y4 S: t( F. h+ C
  346.   {
    % Y# h/ h0 Z$ \
  347.     if(can2InitFlag == true)% S1 N# D0 q5 \- A
  348.     {
    * C6 y; `# k# ?* Y& X
  349.       can2TransmitFinishCallback = (volatile void (*)(void))Callback;
    # v8 ?8 b5 u/ f5 S! D0 `$ i  p) [6 U
  350.     }" ~( q: a$ W# c  w. `: Q
  351.   }
    : o/ _" I" c& Y
  352. #endif /* STM32F10X_CL */5 X& Q1 o% m$ s# D
  353. }% H2 p- J6 `7 ^; u" A

  354. - I+ q7 N4 p& S( j  N8 i) H0 g
  355. /**+ w% l( J1 X1 g6 y) @' F1 t
  356.   * @brief  CAN set receive finish callback.
    / d0 U5 B7 V* B5 d' z! p# l1 D; x
  357.   * @param  [in] CANx:     Where x can be 1 or 2 to select the CAN peripheral.. J$ q. O0 {: w$ Z$ \+ h5 {0 d" q/ H
  358.   * @param  [in] Callback: Callback.
    4 w! [4 n* `$ u6 ~
  359.   * @return None.
    2 x1 K4 L8 X% G; q! B# U; i
  360.   */
    * G4 K& d: U4 Q/ C# V7 a& V
  361. void CAN_SetReceiveFinishCallback(CAN_TypeDef *CANx, void (*Callback)(void))
    / S5 W. `! d- ]* V) T: p7 C' x+ O( `
  362. {1 H! D+ [& X5 b% e6 }7 k
  363.   if(CANx == CAN1)
    1 _$ ?$ k3 r3 d% t% C& \
  364.   {$ V: |+ v% K: U) h8 Y; w: i
  365.     if(can1InitFlag == true)) Z5 X4 B! K* ?, G) O
  366.     {
    . Z# R! h1 ]1 _. f
  367.       can1ReceiveFinishCallback = (volatile void (*)(void))Callback;
    ; ^. k" j! @4 B1 V9 i) H2 S9 m
  368.     }
    7 w- W+ a5 U) R  m$ X  e
  369.   }
    & _# ]/ E( `8 E7 s
  370. ; X6 x; X/ y9 E1 g# z1 y
  371. #ifdef STM32F10X_CL# u* j# @5 ?) v& D1 v( E, h
  372.   if(CANx == CAN2)% ^8 R" V7 i- F: }4 X, o4 }
  373.   {
    ) K0 ?" P  y3 x: |. h, ]
  374.     if(can2InitFlag == true)# \$ P' I6 g; u5 |8 h" \
  375.     {9 J( P( J6 b/ f, B+ \& r' T
  376.       can2ReceiveFinishCallback = (volatile void (*)(void))Callback;9 Y( s5 |8 J! \! W8 z  S6 v' o
  377.     }
    + m0 J, o$ c+ s
  378.   }
    5 `3 q  c  h8 f
  379. #endif /* STM32F10X_CL */( E) C/ ~7 b5 r& B
  380. }
    ' l' G! j/ \) x, ?

  381. ) r" `% ]  H& b
  382. /**0 Y* F3 {4 P$ o$ G# Q, N! M% k" {
  383.   * @brief  CAN set transmit message.
    0 ~) l9 V+ X$ b6 H  @) e. k
  384.   * @param  [in] CANx:    Where x can be 1 or 2 to select the CAN peripheral.
    / _9 \' F7 o, w) b! U& J! m
  385.   * @param  [in] Message: The address of the message to be transmit.* k& s* y( d7 r# S
  386.   * @param  [in] Number:  The number of the message to be transmit.# t1 Z7 F# k, |
  387.   * @return The number of message transmit.
    ; Z5 v. U' [$ V) T* Q
  388.   */
    - _$ @/ E3 B. `
  389. uint32_t CAN_SetTransmitMessage(CAN_TypeDef *CANx, CanTxMsg *Message, uint32_t Number)
    8 ]: Q( ~  E0 q) r
  390. {7 }. v7 T8 t' c3 v& |
  391.   if(CANx == CAN1)1 B- z7 ]/ x1 [/ l
  392.   {9 o1 w* `# z" q" n3 w% l
  393.     if(can1InitFlag == true)
    / z3 k% W8 L& F# D
  394.     {- b! x' t3 p$ q* H; l
  395.       uint32_t available = RingBuffer_Avail(can1TxBuffer) / sizeof(CanTxMsg);+ F0 M! ?3 l5 o& n- Y; u, w% s
  396. 6 {& p4 I9 @- z* @
  397.       if(available > Number)
    + I( j9 |/ p: D! j/ Q; x
  398.       {
      U! T+ e, k* d1 b
  399.         Number = RingBuffer_In(can1TxBuffer, Message, sizeof(CanTxMsg) * Number) / sizeof(CanTxMsg);
    5 ^. f0 ]. r4 w# u# @7 F3 i' d
  400.       }( a2 x* a. t- f7 U( J
  401.       else
    5 B2 h" I" f# ~# x" Z8 F. w, }
  402.       {
    5 ~1 X2 ]- @% i  F
  403.         Number = RingBuffer_In(can1TxBuffer, Message, sizeof(CanTxMsg) * available) / sizeof(CanTxMsg);; \5 q- d  C5 |/ \7 a9 X
  404.       }
      v* Q, a% l: L

  405. * ~, G/ d: S. k% `9 L% P# v3 f' P5 U
  406.       if(Number > 0)/ [2 h( `( _( a- n2 A- t, a  }
  407.       {8 ^  n& T& C; r. U
  408.         if(can1TransmitFlag == false)4 E8 |6 L* _2 Y* D* K
  409.         {% B6 Q. h8 y3 ~& v5 Q6 C$ Q9 K' a
  410.           can1TransmitFlag = true;  R$ I/ E; v1 d2 C- x9 f" u

  411. , U- j; K) D, _9 S' j- K+ Z$ _
  412.           CanTxMsg canTxMsg = {0};7 _: Z+ G8 C/ L$ c
  413.           RingBuffer_Out(can1TxBuffer, &canTxMsg, sizeof(canTxMsg));
    0 \) x1 J0 |  v8 j
  414.           CAN_Transmit(CAN1, &canTxMsg);
    . q) o% n6 o. ?: D7 Y4 W0 Z) m
  415.         }
    2 h* V5 n, x5 N+ {1 k
  416.       }3 O2 ?2 Z, g5 X
  417. 8 Y5 A# y" X* z3 m3 G+ ~' r
  418.       return Number;" R2 U+ _. ?0 ^6 o# {
  419.     }
    7 n; D/ N; M1 O* p2 ^- G) W
  420.   }. e, O3 \( Y8 c! h; C

  421. ( T- s+ u) i0 H' j5 H
  422. #ifdef STM32F10X_CL) X7 ?" I& `5 D5 y% X
  423.   if(CANx == CAN2)* }# T" i0 w8 @' d3 B# T6 {( A
  424.   {
    , e% D9 F. I# Z- {4 w
  425.     if(can2InitFlag == true)( C9 b$ a) b4 R- x) o: L( C; g# P4 k
  426.     {
    5 l# @3 i, d5 ?( k4 h4 y3 B
  427.       uint32_t available = RingBuffer_Avail(can2TxBuffer) / sizeof(CanTxMsg);
    & a3 q) x$ P! s: L

  428. # G3 o3 w- z# T3 Z
  429.       if(available > Number)
    + |1 D0 W5 |# i5 g2 Z- W
  430.       {8 l7 r6 C$ k- M, x
  431.         Number = RingBuffer_In(can2TxBuffer, Message, sizeof(CanTxMsg) * Number) / sizeof(CanTxMsg);
    . c" ^" P9 O$ N" F  N
  432.       }
    ' R3 `2 t- J+ Z" m! ^
  433.       else
    ' u8 f3 q  M# G6 |6 b7 w- i! n+ C
  434.       {: w; u% E3 Z) g- c8 e% r, ^: I, D, `
  435.         Number = RingBuffer_In(can2TxBuffer, Message, sizeof(CanTxMsg) * available) / sizeof(CanTxMsg);
    / ?1 Q% r8 J5 Q' w; F4 \
  436.       }% c2 E# O" m+ \4 g
  437. ! _. e9 i( @! ^# l- K3 a$ j
  438.       if(Number > 0)% z( ?7 _2 e  X: k
  439.       {
    6 F1 W+ o" c1 X
  440.         if(can2TransmitFlag == false)7 L2 w+ e! Z  x1 I# k6 L" b) b6 b' V
  441.         {
    * L' r- J+ a! T" J3 F5 A
  442.           can2TransmitFlag = true;- m7 I' [: J; o- V$ C
  443. 4 b9 X" D* Y! b) b* g* d
  444.           CanTxMsg canTxMsg = {0};( N' n( O. _% |" S$ ?: C2 j2 y
  445.           RingBuffer_Out(can2TxBuffer, &canTxMsg, sizeof(canTxMsg));* e3 D2 b/ b# ~' `% Q) v4 g4 \: U
  446.           CAN_Transmit(CAN2, &canTxMsg);+ ^' w# n" Z# k, L
  447.         }
    4 m# k) `5 t# Y% _6 l( _8 B
  448.       }5 b3 f6 t( c' w4 X

  449. & u$ t$ O1 ?( K7 f2 Y
  450.       return Number;- U! m# \8 m9 p: P. V" k
  451.     }
    1 ]' T6 j6 b" W9 e) ^. n; ?+ ?
  452.   }. {' g3 \1 v& Q
  453. #endif /* STM32F10X_CL */$ \0 N: {  x' n+ u% S6 R5 h2 Q

  454. " g! K: |' N; p  `
  455.   return 0;
    5 r# T+ t- q  D
  456. }
    / V7 ?* J( Z& B  \
  457. 5 D$ h4 D  n) |/ z) ], B# |% |; f
  458. /**( O8 f2 n3 e# g- v6 |$ U
  459.   * @brief  CAN get receive message.2 ^9 y5 ~5 ^+ I
  460.   * @param  [in] CANx:    Where x can be 1 or 2 to select the CAN peripheral.
    3 r* @# {0 [/ U9 M" u
  461.   * @param  [in] Message: To store the address of the receive message.
    . \* [2 W5 e4 R+ v  T2 c
  462.   * @param  [in] Number:  To read the number of the received message.% q6 P# i' U1 t- B! J. }
  463.   * @return The number of message obtained.
    ( y7 c$ y5 n4 ?* b% @* x2 q& `
  464.   */* l- j% Q  [( V# M8 ~5 R
  465. uint32_t CAN_GetReceiveMessage(CAN_TypeDef *CANx, CanRxMsg *Message, uint32_t Number)
    8 ~. @' A$ ~: J" E( j
  466. {
    : W- {* M- K. e9 L, z% r
  467.   if(CANx == CAN1)
    0 U$ P# A% O7 P. i$ O/ ?
  468.   {
    & `" Q" a7 L* R2 s8 A
  469.     if(can1InitFlag == true)
    - ?( K' l7 T- ~! W' I  k0 }4 r
  470.     {( B2 `- [' a6 M3 b" Y/ o' C
  471.       return RingBuffer_Out(can1RxBuffer, Message, sizeof(CanRxMsg) * Number) / sizeof(CanRxMsg);
    6 m! X) b  |/ Y" W* K& |
  472.     }
    $ C. U' U& S% |/ _  A# Y
  473.   }! u# A" W- o0 i
  474. * k* k  I/ Y" x6 c. X: V
  475. #ifdef STM32F10X_CL
    ; u2 S$ Q' ~/ m8 [' F$ |
  476.   if(CANx == CAN2)
    : u# s- d& Q  z/ ]
  477.   {7 _" g; l* \2 Q; i; V& q
  478.     if(can2InitFlag == true)5 Z2 A8 e( h4 V& @
  479.     {
    $ p* k  D+ q% k9 P3 ^5 U( ^
  480.       return RingBuffer_Out(can2RxBuffer, Message, sizeof(CanRxMsg) * Number) / sizeof(CanRxMsg);+ Y7 h- L; U5 ], x& e
  481.     }
    6 @: y% W. V4 ^8 d5 e# a$ P$ s
  482.   }
    ) q5 A2 v7 r) }2 s7 J
  483. #endif /* STM32F10X_CL */9 K; M) `0 h, _, u4 L& r

  484.   S3 ~& R9 i7 ?$ o3 T+ \2 H
  485.   return 0;
    3 h/ d/ }5 d9 l$ o/ F+ O
  486. }( ~2 T" x8 N. v1 |: I3 g% f

  487. 0 G! U. h5 }! O. j3 W* e& h! D
  488. /**
    9 f) }; u7 I6 `# M8 m- g
  489.   * @brief  Get the size of the CAN transmit buffer used.  S' u2 L: F/ F: S& b/ M& X
  490.   * @param  [in] CANx: Where x can be 1 or 2 to select the CAN peripheral.
    + ^, U' f! u, L. W/ J0 j
  491.   * @return Used the size of the transmit buffer.
    # p6 B+ Z  U" v. M* t6 P9 u
  492.   */+ U  {! I  W% `" }6 A
  493. uint32_t CAN_GetUsedTransmitBufferSize(CAN_TypeDef *CANx)
    , i2 o! ~+ D! h  B( N8 {7 Y
  494. {: e- A7 K- R' {! ^. o! ~
  495.   if(CANx == CAN1)
    # s3 {% A3 N& K8 _* @, i% @5 ?
  496.   {
    2 b. i+ O6 O9 }  O
  497.     if(can1InitFlag == true)% O1 J5 [! N+ u
  498.     {
      ^7 l& `4 D, D$ j  v# I
  499.       return RingBuffer_Len(can1TxBuffer) / sizeof(CanTxMsg);( T3 d4 j8 i9 f+ X9 ]
  500.     }
    ) j& D8 s& \# c) z. M
  501.   }
    ( y5 z# }  U4 y, ^, E9 I; }& z

  502. . _5 f' f& Y" c' d) V: d( p9 D
  503. #ifdef STM32F10X_CL
    ) l6 @! n* u; |! Y9 V
  504.   if(CANx == CAN2)" ~* r9 D/ B; C: D6 u
  505.   {
    + U0 r8 T2 C6 P- t" \# f  R
  506.     if(can2InitFlag == true)9 Q4 Z: d' o; r
  507.     {2 i6 m7 t$ }: y, y/ K2 f
  508.       return RingBuffer_Len(can2TxBuffer) / sizeof(CanTxMsg);9 U; T9 Y1 H+ z/ l) B
  509.     }/ t: z7 Z" Y+ S7 k
  510.   }! G% n8 t0 x% R
  511. #endif /* STM32F10X_CL */
    # Z  u" U' z$ E1 L3 _$ T" O  q

  512. ' j- l; }8 ^+ L  E. {
  513.   return 0;; W1 ]% E6 `6 i0 C9 j: ?
  514. }# ?0 \: M" x7 p# \, F; I  y

  515. / s8 v, n: a; Y( |) F) a' `; |
  516. /**2 S8 U9 P" P- l
  517.   * @brief  Get the size of the CAN receive buffer used.0 ?; @; n- A- Y5 @! |
  518.   * @param  [in] CANx: Where x can be 1 or 2 to select the CAN peripheral.8 z( X, W4 r( q. q
  519.   * @return Used the size of the receive buffer.: e  E9 {7 p) r* @
  520.   */
    . V8 w* M" ]! s9 e5 n
  521. uint32_t CAN_GetUsedReceiveBufferSize(CAN_TypeDef *CANx)+ C  w6 r, H' _2 G1 _
  522. {/ D/ J2 o- d2 Z0 K: |- |- i
  523.   if(CANx == CAN1)" V% ?% {7 W" @* z4 i$ A
  524.   {
    1 z2 N0 A! ?) A; u& U# u9 u; J
  525.     if(can1InitFlag == true)! x; v' [" V( p. p. D& ?0 P) t
  526.     {/ ], M2 _% _' x; D7 O
  527.       return RingBuffer_Len(can1RxBuffer) / sizeof(CanRxMsg);) M0 ?8 J& H' m4 s& V* T( M
  528.     }
    ! W0 t, y0 ^) ^7 u% E
  529.   }1 }  w0 p) M0 p. }. H. d

  530. * Q6 z& l3 Q: }5 Q
  531. #ifdef STM32F10X_CL
    % P: n' e9 d2 J$ f6 e8 Z9 h. Y% W* A
  532.   if(CANx == CAN2)
    8 N9 W  f( c0 F+ q) P0 Z4 ]% d$ y
  533.   {
    # C* H" r- p: i- w
  534.     if(can2InitFlag == true). c. {/ u* W, f, t5 {6 v
  535.     {
    ; b! ?4 ]" l% d
  536.       return RingBuffer_Len(can2RxBuffer) / sizeof(CanRxMsg);! O0 {  M9 }4 @
  537.     }
    ) I. S/ ^6 k+ S3 q
  538.   }+ q" k+ N- W+ l* g& `+ @
  539. #endif /* STM32F10X_CL */1 x& X) O, L( @9 e3 e& ?8 U, n

  540. 0 _. Z& t/ {6 z3 m, @
  541.   return 0;+ V. u( @: ]1 E" |( W+ e7 t
  542. }: u2 O1 M, ~& Q$ U0 ^

  543. & _8 I% Y8 x2 I+ F  ]% U1 H
  544. /**
    ) p- W' `% Q! c% R- D/ b/ r( T
  545.   * @brief  Get the size of the CAN transmit buffer unused.' a/ M5 A% f* @- v$ i
  546.   * @param  [in] CANx: Where x can be 1 or 2 to select the CAN peripheral.+ C) ^4 ~, z8 l
  547.   * @return Unused the size of the transmit buffer.
    / C; V% b9 x) c2 K2 o
  548.   */' t: Y3 q  S: M: l5 M* P+ f. C( A
  549. uint32_t CAN_GetUnusedTransmitBufferSize(CAN_TypeDef *CANx): q  C% }6 S& f1 O
  550. {
    . g5 ]9 c/ j, Y( f% ?: I1 r
  551.   if(CANx == CAN1)! B4 y) `5 Q# g4 Y$ J
  552.   {' z. j3 ]( D' Z3 K! ~
  553.     if(can1InitFlag == true)7 p! }  H* \4 e* M0 X, q
  554.     {
    9 {1 `8 y7 A$ h% s# g2 j3 o9 ?* J& X
  555.       return RingBuffer_Avail(can1TxBuffer) / sizeof(CanTxMsg);/ W" |2 D* r- C4 f6 S/ U0 z
  556.     }3 z$ t: E+ `1 r& `3 D% ?2 n: m$ o
  557.   }
    2 D( k6 ~6 J' N! W- }% n0 u
  558.   S7 s5 O4 C( c  K. J
  559. #ifdef STM32F10X_CL
    ' ?0 ]) A6 j8 N" T: v
  560.   if(CANx == CAN2)
    & o8 ?5 |' G' X! H
  561.   {* Z; M2 q/ `3 B
  562.     if(can2InitFlag == true)
    ! C7 F$ e" q1 C5 i6 F
  563.     {' C" A2 w6 o6 I4 s6 \0 @
  564.       return RingBuffer_Avail(can2TxBuffer) / sizeof(CanTxMsg);! v/ `- C% ~8 J5 }& z4 T) I
  565.     }
    & ?0 W# C+ B1 B0 j
  566.   }
    8 C5 m4 |8 @( l/ Q4 I5 I) c
  567. #endif /* STM32F10X_CL */
    9 M; |! l+ p. b1 C

  568. . ^3 z- T* @. y4 j% Q
  569.   return 0;0 O5 e! ?9 R' P. r  u( ?$ o# R
  570. }/ `4 A3 K2 q/ \, ^/ q

  571. 0 q/ N0 g  W& R5 V) O# A: l0 z
  572. /**# k8 h, E8 u( \: C/ ^' \
  573.   * @brief  Get the size of the CAN receive buffer unused.1 s: ~: C3 G, A' w2 ]: ^
  574.   * @param  [in] CANx: Where x can be 1 or 2 to select the CAN peripheral.+ }, c- \8 B- I. [
  575.   * @return Unused the size of the receive buffer.
    + u# U2 M: n; a. E* X9 G; V
  576.   */
      g: ?8 e# T; R, `5 L: }
  577. uint32_t CAN_GetUnusedReceiveBufferSize(CAN_TypeDef *CANx)
    $ `, q' b. @. Q1 i/ b8 x
  578. {" ~/ y+ g. l: z" y$ ^2 W
  579.   if(CANx == CAN1)& {# o, T6 y: U. B! a& Q2 N
  580.   {8 \: Q8 {+ S( q
  581.     if(can1InitFlag == true)% Z0 u  B5 u5 E' Z; s: j
  582.     {
      P) Q7 R1 R  z- T  D, X
  583.       return RingBuffer_Avail(can1RxBuffer) / sizeof(CanRxMsg);
    % ]6 R* H) q; r3 x# a
  584.     }8 k' D3 C3 j3 M
  585.   }8 w+ g# f7 o+ a& x% r

  586. - n4 F4 B2 u& Y
  587. #ifdef STM32F10X_CL! n: `6 u7 u$ C+ ~9 b
  588.   if(CANx == CAN2)
    , W, U" u3 v1 Y6 a" b! B
  589.   {) g4 v' R+ o' B. A" X4 X
  590.     if(can2InitFlag == true)
    6 Z% b' W/ J  A
  591.     {  w8 E0 L1 u5 K- C4 j" ]; o% A
  592.       return RingBuffer_Avail(can2RxBuffer) / sizeof(CanRxMsg);& J5 @" }* G- m1 B/ x/ b! F. r. l
  593.     }
    6 R/ b1 ?3 v' p) I+ C: A
  594.   }
    8 d$ w! ]* g) U4 h
  595. #endif /* STM32F10X_CL */1 \; W7 u! Z1 }1 `( P% S* G
  596. ( ?1 k7 V+ {4 j
  597.   return 0;
    ( p; J2 o' b: I0 x7 T0 s; m& Y- X
  598. }+ X7 e% p- p! L# h

  599. ' j) r* G; T5 v! ]6 M. u7 ?
  600. /**
      M- b) H5 C2 d8 j& T$ k5 ^
  601.   * @brief  Is the CAN transmit buffer empty?, X5 S& J8 @% q+ g( `
  602.   * @param  [in] CANx: Where x can be 1 or 2 to select the CAN peripheral.
    ' c/ W6 `3 k) a9 Q7 x
  603.   * @retval true:      The transmit buffer is empty.
    9 a/ U' ~" q/ v6 ]4 I1 m
  604.   * @retval false:     The transmit buffer is not empty.
      V9 _+ ~9 \' \0 p( Z
  605.   */
    # i3 m) ?6 }7 W. U2 P7 V$ R
  606. bool CAN_IsTransmitBufferEmpty(CAN_TypeDef *CANx)1 D# J9 Q2 S: P
  607. {  H( d- {! {; N2 p% A+ p5 o/ l
  608.   if(CANx == CAN1)
    3 R, [3 c# v8 T$ ~- k7 K2 B6 @
  609.   {  B& \* f  L2 I: P8 f! `1 ^
  610.     if(can1InitFlag == true). S  o. r& y1 A# X. M  {
  611.     {" N) b, g0 d1 r# `$ H
  612.       return !(RingBuffer_Len(can1TxBuffer) / sizeof(CanTxMsg));
    . R5 R5 o+ \) k, h' W
  613.     }
    7 G/ o2 ]" c* d# g( a3 g2 N3 m
  614.   }
    2 h1 T6 j: ~+ ?
  615. . f  ^( z, q8 H. X8 x
  616. #ifdef STM32F10X_CL
    # k- m7 _# ?; ^# a! \7 t" r
  617.   if(CANx == CAN2)  i( S1 _  ]+ a0 A
  618.   {
    ' U. l! q! s# ]( r: Y, ~% G) N9 ^
  619.     if(can2InitFlag == true)
    $ W- `* j: e2 m+ \7 T1 ]
  620.     {
    4 C  M  g) C* W4 |3 u3 D: }
  621.       return !(RingBuffer_Len(can2TxBuffer) / sizeof(CanTxMsg));
      k5 |3 Z# @5 ?0 z' i* g
  622.     }0 r. f- r5 F- W8 F2 K$ O) Z
  623.   }1 T% n1 r" X( \4 r6 V
  624. #endif /* STM32F10X_CL */
      g9 P% [( K+ v" D$ }/ l

  625. / Q) w* r+ s0 I: A
  626.   return false;8 p1 x) ]( U" ~
  627. }' w4 A. J/ O- g( d! m
  628. 6 |$ w' ~) ^+ V' n" r, r8 h: b; V
  629. /**
    & o$ H/ F1 [; h( q, v
  630.   * @brief  Is the CAN receive buffer empty?+ }9 ]* s  w3 \
  631.   * @param  [in] CANx: Where x can be 1 or 2 to select the CAN peripheral.
    7 {6 b& P! f, x) U: b
  632.   * @retval true:      The receive buffer is empty.$ H+ H) J% ]+ i
  633.   * @retval false:     The receive buffer is not empty.( e, _; Y8 t5 e0 c! }- F; I
  634.   */
    " |/ f: `- F$ q; I7 s$ I$ K
  635. bool CAN_IsReceiveBufferEmpty(CAN_TypeDef *CANx)
    - D8 \! X9 |2 o0 o* P
  636. {( g" E/ r) y' g$ A) @
  637.   if(CANx == CAN1)
    3 {3 U2 M1 N' V) R' b3 ]# w
  638.   {
    + J5 `3 }, E: v8 @! b- c
  639.     if(can1InitFlag == true)' i- R: r- p" N* A! F1 i
  640.     {9 n! F' ]$ d% A
  641.       return !(RingBuffer_Len(can1RxBuffer) / sizeof(CanRxMsg));" ?: z' L4 ~% K, }) @% o
  642.     }' M& Z1 d# y+ J
  643.   }
    0 r) m1 ?* u/ m  A) G

  644. 2 b/ T7 |# x9 u7 \5 j' n3 f
  645. #ifdef STM32F10X_CL* b) b2 o$ {! h( j5 G6 {
  646.   if(CANx == CAN2)  E# R0 C+ s/ ]/ U3 k  \8 ^9 H
  647.   {/ D: R2 x1 v: s: E& ^
  648.     if(can2InitFlag == true)9 l3 v5 N* J/ t# F- `' I
  649.     {2 Z' j% p! k+ U2 x' N2 K
  650.       return !(RingBuffer_Len(can2RxBuffer) / sizeof(CanRxMsg));
    & [% K1 F+ I, |. h9 g9 m! F. a* p# R
  651.     }2 `) l! z5 \9 m* J% Z
  652.   }- T7 D0 y' U$ m! p0 V- ?
  653. #endif /* STM32F10X_CL */! a  y0 {' x0 F9 V1 I

  654. & ~& f* R7 D. f: F! }
  655.   return false;4 E: E/ _4 N8 W2 ]+ g% x4 R8 x
  656. }
    8 J* G7 k) R7 F' j+ V$ }
  657. # M, h& Q  x* ~: s" y
  658. /**
    * P' A8 C& b. }7 u$ h0 M7 ^5 [
  659.   * @brief  Is the CAN transmit buffer full?5 k/ q1 \% @( R6 p9 L
  660.   * @param  [in] CANx: Where x can be 1 or 2 to select the CAN peripheral.* L' M, H2 U- T0 i/ ?9 ^
  661.   * @retval true:      The transmit buffer is full.
    ! v8 z! R8 N$ m  l
  662.   * @retval false:     The transmit buffer is not full.
    4 A( \% ^6 ~/ r9 @* z
  663.   */4 H) x5 {6 ~- P
  664. bool CAN_IsTransmitBufferFull(CAN_TypeDef *CANx), y1 g% ~/ G( i# a6 M- g" w- a
  665. {
    1 ]0 Q! R5 V0 ]5 K( s
  666.   if(CANx == CAN1)
    . B6 p% [# K6 l) B
  667.   {
    / O. I) I7 ~$ p0 E
  668.     if(can1InitFlag == true)" p- v/ s" m/ e7 ?- V- Y  n/ s+ R1 b
  669.     {
    , l# v% u7 b% e# m0 ]) c
  670.       return !(RingBuffer_Avail(can1TxBuffer) / sizeof(CanTxMsg));2 o4 K: h* Q' ^
  671.     }
    ( q5 `3 O9 {" b2 U
  672.   }
      V8 H& @- i! w
  673. 7 y6 _2 B' m; L! k
  674. #ifdef STM32F10X_CL8 ]3 o, T0 D' W% m6 h4 u; l* F2 u
  675.   if(CANx == CAN2)
    0 N+ k% G4 Y, U5 _& H8 {$ }" q, B' A
  676.   {& Y) L3 E" @1 U: r4 |1 b
  677.     if(can2InitFlag == true)6 L2 k* K6 r& i5 G  b
  678.     {
    8 _- \* x0 S$ a* }
  679.       return !(RingBuffer_Avail(can2TxBuffer) / sizeof(CanTxMsg));
    % ~6 K8 l! s; `3 o% f+ b. h
  680.     }; c- ~6 T  Q8 p2 f1 g
  681.   }
    4 U1 j% H& l( B
  682. #endif /* STM32F10X_CL */1 m+ f1 }1 m# A2 B4 [; l

  683. ! f2 m6 x; D% q# `" d
  684.   return false;
    ; W) Q8 p/ V; w7 x% X
  685. }6 C: J! S) Z2 P# M, L- m+ l

  686. % g0 G) Y/ [* A. o5 ~* @/ X
  687. /**
    . h% L3 n. c7 V% J" Q) u( W
  688.   * @brief  Is the CAN receive buffer full?- ^1 [" Q2 {8 B# q0 R2 h. {( c) v
  689.   * @param  [in] CANx: Where x can be 1 or 2 to select the CAN peripheral.& T- [7 M' D+ s, Y* Y2 j
  690.   * @retval true:      The receive buffer is full.
    ' K/ `# s* g1 j' Z
  691.   * @retval false:     The receive buffer is not full.$ ]" G' V7 D  M; h! `
  692.   */5 n" d5 j* r# x
  693. bool CAN_IsReceiveBufferFull(CAN_TypeDef *CANx)  c' y' H6 ]/ _7 }; }
  694. {
    ) ~' V$ U) r( V% y8 S9 J
  695.   if(CANx == CAN1)2 H, t3 z2 }2 P# h( R1 D
  696.   {& T7 r9 a& n9 G
  697.     if(can1InitFlag == true)6 ?& B" O0 k3 C; ?8 P. c' {
  698.     {9 G- [4 @" K2 b$ }2 e+ @# z
  699.       return !(RingBuffer_Avail(can1RxBuffer) / sizeof(CanRxMsg));* n/ K7 I! C/ `
  700.     }
    $ ?& Q5 l0 A* w
  701.   }
    : z& Z# m) }3 z3 ~* p" y

  702. * i% j6 |& \1 r
  703. #ifdef STM32F10X_CL
    9 ^2 x. Y- z7 G, M  ^, ^
  704.   if(CANx == CAN2); q6 R7 \6 W- j( T; m
  705.   {
    0 |; j$ L2 w0 N' j
  706.     if(can2InitFlag == true)
    1 D# B% H: @0 C) }) T5 t; s3 J0 b1 [3 r
  707.     {
    + }5 i& H0 j4 R# ?, m) }
  708.       return !(RingBuffer_Avail(can2RxBuffer) / sizeof(CanRxMsg));0 B6 T( d8 \. o, }! a( A
  709.     }
    + h7 ?+ w/ k, q, Q3 J9 B
  710.   }
    1 V/ K/ i) |- a
  711. #endif /* STM32F10X_CL */' m7 V7 A' N8 e% l+ c6 l
  712. 5 v. C3 }- N% i# n
  713.   return false;1 C6 J# ?3 ?1 M
  714. }
    - v. N7 i. C: Z5 `7 R; D

  715. , A, r( e! _( H: T% V- ?
  716. /**# u: {! ]7 T1 }& L4 j/ a
  717.   * @brief  Clear the CAN transmit buffer.
    $ [) x9 i6 [+ S" G$ K+ j3 [: M
  718.   * @param  [in] CANx: Where x can be 1 or 2 to select the CAN peripheral.
    : \0 Z+ g6 K, a1 E' }1 ]8 S
  719.   * @return None.+ ], a6 ^( u  r7 \: R9 P
  720.   */
    , E. g1 a3 d- V: L, @  G8 ?/ E
  721. void CAN_ClearTransmitBuffer(CAN_TypeDef *CANx)1 }* g6 y& V; p& G
  722. {
    9 t* l6 n2 T( k
  723.   if(CANx == CAN1)/ c: m  ^- b8 z& s2 L2 I
  724.   {% z8 @' B$ \9 z2 \
  725.     if(can1InitFlag == true)1 k1 Y! e7 a3 E7 h
  726.     {
    6 p5 x7 n' \  N1 q% a
  727.       RingBuffer_Reset(can1TxBuffer);
    2 J/ \8 g+ l# q# Q" y
  728.     }0 a# C* X$ t; P$ \9 f4 J1 a
  729.   }( w) o* F( M1 i& \6 D) J& l4 }3 f7 ]
  730. ' a* T0 V- z* a1 R. t4 c* `+ k' Z
  731. #ifdef STM32F10X_CL
    # \2 v4 `+ N. N) h) `) x
  732.   if(CANx == CAN2)5 K6 r, Z) a+ _3 p" j
  733.   {6 ]2 J- C0 e3 V) n0 {& f/ N# v1 Z1 n8 d
  734.     if(can2InitFlag == true)
    2 U! ]$ K4 E; d- K: g/ P4 F
  735.     {6 Z. g0 e$ J8 ], n
  736.       RingBuffer_Reset(can2TxBuffer);
    . g: u, p) c$ P5 e' f
  737.     }; R1 `" w' _/ s& d1 M
  738.   }
      ?6 _( X3 x' V
  739. #endif /* STM32F10X_CL */0 Z, v3 I  [: h8 o, ^. y) X; @
  740. }3 c, o3 o4 ^7 e) N
  741. - ]) U5 `  b5 s, K1 M" @
  742. /**
    " ^! y* `% m$ c$ W) r: _
  743.   * @brief  Clear the CAN receive buffer.' Z" h/ i4 l6 Z$ p. x% w
  744.   * @param  [in] CANx: Where x can be 1 or 2 to select the CAN peripheral.
    $ G; J; E0 x8 Q9 X8 V* `
  745.   * @return None.
    % R9 T1 |6 o. @; ?
  746.   */" W3 [" ]8 K' \5 O
  747. void CAN_ClearReceiveBuffer(CAN_TypeDef *CANx)0 y4 A9 F2 D/ D' L! C0 E5 B/ X
  748. {$ K% H' L. {  |  T
  749.   if(CANx == CAN1)
    * H! u3 X& Z) }2 l- d" e( @$ l2 o
  750.   {" u, o  l) E! W% v) B6 k2 n
  751.     if(can1InitFlag == true)$ j( V; R1 }+ r& W; ]
  752.     {3 [0 B/ a+ H2 j# ~
  753.       RingBuffer_Reset(can1RxBuffer);
    % e: Z+ \. Y- b3 J
  754.     }
    # {* {5 F7 u& y5 `6 e# f  G
  755.   }
    0 Q5 N, k: ^, o0 d% |

  756. 2 F7 x2 i$ E4 Q) [1 v
  757. #ifdef STM32F10X_CL! Y- H" _: p3 x6 Q) o
  758.   if(CANx == CAN2)# `/ Z, K- |( w$ a+ u9 Z$ j5 b
  759.   {
    : \3 k3 }/ b  ^) P8 }" |
  760.     if(can2InitFlag == true)
    2 F% Q) N$ `! E& V3 \
  761.     {1 V( z+ r6 @/ N' f5 A3 H
  762.       RingBuffer_Reset(can2RxBuffer);
    ( I; o! y7 f/ m) e. ~# }
  763.     }
    . ~2 W7 N4 |2 Y! g
  764.   }, W9 H4 C. {* |/ {4 Q
  765. #endif /* STM32F10X_CL */. f$ t$ _% ^- C! ^# @: u4 H
  766. }1 e, q1 _/ M5 u
  767. 1 ~' r! U9 @8 g( d6 ]$ Q4 q
  768. /**0 Z7 A) A2 P. g) J& }. p
  769.   * @brief  Is the CAN transmit a message?
    $ k* z8 G# r% K3 ]% e
  770.   * @param  [in] CANx: Where x can be 1 or 2 to select the CAN peripheral.' H) h0 j3 m; z' k
  771.   * @retval true:      Is transmit a message.& k5 g% i8 j/ l: U! r0 J& ~
  772.   * @retval false:     Not transmit a message.' R, A6 Q) m( O1 M* X
  773.   */3 s6 C) Q. m8 o# \+ `
  774. bool CAN_IsTransmitMessage(CAN_TypeDef *CANx)
    * f5 r& T. g8 }( ]( k  s4 G8 p
  775. {
    9 w, T2 @7 g# c4 n1 W( d) ]- q
  776.   if(CANx == CAN1)( w6 _* h& Y) p& m1 p
  777.   {, z  s4 P, N3 G9 j
  778.     if(can1InitFlag == true)+ ]6 i2 W, i6 x7 @) ^6 U
  779.     {% @  G1 ]2 W, N( Z7 u# `
  780.       return can1TransmitFlag;
    % \6 t3 F# `" H$ H- L* V) M
  781.     }- @) r& u. Q% ?8 y9 g
  782.   }
    6 _; g4 q8 p- U1 D* G9 R! ]
  783. + D% e5 l6 H8 E4 j! E6 F6 q6 ]$ j$ n0 H
  784. #ifdef STM32F10X_CL
    . C+ P8 H7 ], m, y  E
  785.   if(CANx == CAN2): C" k% ]* K; @7 K
  786.   {) ^3 Z2 E# e+ M5 @! u
  787.     if(can2InitFlag == true)
    ; D2 }' `) A2 x) P( X
  788.     {' Z: Y. ?! x! s* g- F+ b% ^
  789.       return can2TransmitFlag;
    0 S" r# ], b2 y
  790.     }+ C7 s5 K. g7 B3 q8 {- D
  791.   }9 p# x2 W" I  A" p
  792. #endif /* STM32F10X_CL */
    ; t6 t* d" ^: U4 Q& O# ~9 u; t; J
  793. 1 h4 t& M9 n! T5 U( M( u8 J
  794.   return false;
    ( H$ v) r) O" C, P: q' r) L% Q9 D
  795. }; @( W" ?+ a/ }( K8 ^
  796. & j/ g9 \! L$ u& o! _
  797. /**
    $ G3 V0 u* u, ^1 e
  798.   * @brief  This function handles CAN1 TX Handler.# m: ~4 w/ v: o. `$ [
  799.   * @param  None.. e  ?9 S/ e* y  c0 e# M
  800.   * @return None.
    8 }# p6 m) h* e
  801.   */$ @, j) O2 t9 l0 C/ |% V8 E6 m
  802. #ifdef STM32F10X_CL
    0 A, }& y$ W' ?" |7 }/ ~
  803. void CAN1_TX_IRQHandler(void)6 ~  Z! D( ^3 [$ C+ d
  804. #else% x+ K# \8 \$ i. T( k$ T, l  {/ E
  805. void USB_HP_CAN1_TX_IRQHandler(void)5 v  u9 ~7 @$ m) w
  806. #endif /* STM32F10X_CL */
    - i% j# `* b# D# I4 u) ^1 @6 |
  807. {
    * J0 O) y( y( z) B& `: b5 y& [, |
  808.   if(CAN_GetITStatus(CAN1, CAN_IT_TME) != RESET)# Q! R6 P; T! M# |/ a* [
  809.   {
    4 f$ k; L) E5 p5 ^
  810.     CAN_ClearITPendingBit(CAN1, CAN_IT_TME);
    - o, E3 P9 [$ ~. f4 T6 _
  811. , i3 g& @4 x+ O2 M4 ], M/ \
  812.     CanTxMsg canTxMsg = {0};
    8 N+ b9 p3 u: s' E
  813.     uint8_t  number   = RingBuffer_Out(can1TxBuffer, &canTxMsg, sizeof(canTxMsg)) / sizeof(canTxMsg);
    ( }) ]0 u. J: V! y" M

  814. 0 v' I- T* E% z
  815.     if(number > 0)) M3 N" ^4 i, S
  816.     {4 L4 I3 q/ X0 c
  817.       CAN_Transmit(CAN1, &canTxMsg);7 @# Z7 N; u2 ]7 m' Y- s
  818.     }
    $ P1 {8 @5 h3 N$ v$ H( c
  819.     else. |9 G, [4 P* ^
  820.     {
    6 z! U& e% _- [
  821.       can1TransmitFlag = false;
    : M9 b9 X  {$ H6 {5 h( w' X

  822. 2 u3 o$ }* x" x+ P3 g
  823.       if(can1TransmitFinishCallback != 0)- c2 S$ F% V8 D/ R1 w
  824.       {
    1 F3 [( E* I+ L
  825.         can1TransmitFinishCallback();: F) ^$ Y9 V2 w: O9 [1 J
  826.       }) G# L4 G0 \* I9 ]* U. }3 E
  827.     }  `) Y/ X% [( I) V
  828.   }5 T$ H7 x* ^$ W, ]' y' _8 G; p
  829. }
    % q7 r- W8 g# }2 Y, k3 F9 a

  830. 2 i0 V" e* E: N6 C) }
  831. /**1 |& c- [) |/ p& x- g- P" S
  832.   * @brief  This function handles CAN1 RX0 Handler.
    9 d% ~, O1 Y+ J3 U/ Z/ p
  833.   * @param  None.  I6 n+ s% d; o7 X! K. i
  834.   * @return None.
    ! z8 O  g: J5 A: [
  835.   */
    & d% H) t5 n# V
  836. #ifdef STM32F10X_CL# H8 \. K) f5 O9 P# H, Q
  837. void CAN1_RX0_IRQHandler(void)
    , x3 D" K# ^3 P- _# N( ?  x
  838. #else
    & {: q; X9 E8 n/ x5 Y9 m7 R
  839. void USB_LP_CAN1_RX0_IRQHandler(void)
    0 z, v) _' k: M7 g
  840. #endif /* STM32F10X_CL */* _5 t: K8 w: E
  841. {
    , x/ Z8 D. [8 ^8 [6 X0 Z
  842.   if(CAN_GetITStatus(CAN1, CAN_IT_FMP0) != RESET)4 @. [: s4 X* B9 a$ C6 r+ ?# _7 U
  843.   {& k& }* g& S" d) H# @7 s
  844.     CAN_ClearITPendingBit(CAN1, CAN_IT_FMP0);' v. t/ f! W' \( U1 b
  845. 6 `: I; _' I! S8 g! B8 V
  846.     CanRxMsg canRxMsg = {0};
    2 j, l0 ]' Q. g% N
  847.     CAN_Receive(CAN1, CAN_FIFO0, &canRxMsg);' I% B7 H, `' [5 A+ d4 I

  848. : p5 F( }' f8 d
  849.     if(RingBuffer_Avail(can1RxBuffer) / sizeof(CanRxMsg) > 0)9 ]$ L, @" N9 T% E# L3 L
  850.     {
    ' H. y2 B3 H% p# y4 R3 `& j
  851.       RingBuffer_In(can1RxBuffer, &canRxMsg, sizeof(canRxMsg));/ k( C9 F; q4 c
  852.     }3 |7 Y; L/ c, X4 y

  853. ' Z  [1 f( f8 Y- d( B6 I" [4 u
  854.     if(can1ReceiveFinishCallback != 0)
    % {/ U6 L, D6 w) u
  855.     {
    3 I, e4 |  t6 k6 b; S
  856.       can1ReceiveFinishCallback();9 Z& I9 w0 r: A1 E6 \
  857.     }- F) |* f$ J8 D7 |+ d
  858.   }
    9 V1 U4 n0 U- ~5 U+ w/ f" J
  859. }
    " i4 w' G: {. A% i
  860. 9 Q& D2 [  Q1 Z3 ^$ ]0 h
  861. #ifdef STM32F10X_CL! a3 o; z/ }7 \+ f9 y
  862. /**) `4 a2 \9 ]) V$ P! h, v: n0 a3 u2 \
  863.   * @brief  This function handles CAN2 TX Handler.4 s, s2 ?' z) w
  864.   * @param  None.9 e6 ~) j. Y0 l6 d+ r
  865.   * @return None.: A  y4 E& ^1 O$ N& v
  866.   */5 O. R: r( f; n# z
  867. void CAN2_TX_IRQHandler(void)
    . n5 m* m% D# w/ F# N! Z
  868. {2 A* \% g: [- A4 p6 N7 l
  869.   if(CAN_GetITStatus(CAN2, CAN_IT_TME) != RESET)
    7 p. W. ?3 ?2 G# e# M2 b
  870.   {
    - T# U" w3 v% i( @* _. h& v
  871.     CAN_ClearITPendingBit(CAN2, CAN_IT_TME);
    - C* h! P# o9 }) U# n) t- ~

  872. ( N2 r5 a6 E) N1 ~
  873.     CanTxMsg canTxMsg = {0};
    / Q5 U, {$ w0 @: X# k& _. U
  874.     uint8_t  number   = RingBuffer_Out(can2TxBuffer, &canTxMsg, sizeof(canTxMsg)) / sizeof(canTxMsg);; W& [# z0 B0 P" [$ `) }1 |

  875. ( F+ L* X7 H+ s' G# M
  876.     if(number > 0)9 |0 T/ D2 ~) K5 E0 _! B
  877.     {
    0 X- g; @3 O$ t% k
  878.       CAN_Transmit(CAN2, &canTxMsg);
    9 ?" Q9 |0 q' b, Q
  879.     }$ t! s9 |; e  a; E8 I/ p# _
  880.     else6 F/ K% K5 }/ f9 S) X; p
  881.     {
    . x8 U9 j& a) w; v6 `
  882.       can2TransmitFlag = false;1 s/ n& k# I) z' \; K
  883. 5 l. z  h$ K: c9 v
  884.       if(can2TransmitFinishCallback != 0)$ }' o) e$ y, q! e6 {
  885.       {3 k) ~& f# S% H) }5 I# ?
  886.         can2TransmitFinishCallback();+ d- d; v8 |# u. s; M; R1 w+ ~
  887.       }
    2 z- G  J0 g: Z  D
  888.     }
    " I9 c) g( B. V: Z# }8 @
  889.   }& [+ g6 h. S& o+ [1 g* F/ u$ U6 X
  890. }
    $ E  p2 @$ j- z, p; [/ }: O# i7 g: L6 E
  891. ( C4 _7 m+ Z; K) O5 P
  892. /**
    0 u+ D" g1 W9 ]) A
  893.   * @brief  This function handles CAN2 RX0 Handler., J# g: f8 t* R; |# P. W1 q* u
  894.   * @param  None.
    + E: I5 z/ W/ F
  895.   * @return None.
    0 v$ e9 t  h+ S
  896.   */: i6 k5 _5 O1 a7 Z
  897. void CAN2_RX0_IRQHandler(void)
    + G5 ]* y% E+ M. |# n6 e$ g+ i
  898. {8 v2 z$ i) Q. u1 A% B
  899.   if(CAN_GetITStatus(CAN2, CAN_IT_FMP0) != RESET); T# s  n+ V, O0 Y. W! m
  900.   {
    * S) O5 G' [7 q4 u
  901.     CAN_ClearITPendingBit(CAN2, CAN_IT_FMP0);, e. d1 M( Z: ?( e. |
  902. $ H. g6 y6 B  D- P9 @
  903.     CanRxMsg canRxMsg = {0};
    . M" C# \2 v! p& z
  904.     CAN_Receive(CAN2, CAN_FIFO0, &canRxMsg);
    : h, o- |; u, U

  905. , x5 p# X' q2 n$ F5 Z) _$ n3 R
  906.     if(RingBuffer_Avail(can2RxBuffer) / sizeof(CanRxMsg) > 0)& P- {; W7 \8 C$ H, Q
  907.     {
    8 R" f% m6 k/ v# p9 k, A5 M
  908.       RingBuffer_In(can2RxBuffer, &canRxMsg, sizeof(canRxMsg));, [% Z4 U! m/ D' b! X+ K
  909.     }
    # O+ {1 @6 W) s8 _

  910. 2 y3 b) f" z' C* ?
  911.     if(can2ReceiveFinishCallback != 0)7 {) |3 `+ C8 b# }( Y; X! ~! V8 q2 l
  912.     {
    5 v& T9 ]: X+ z' x" d
  913.       can2ReceiveFinishCallback();1 E& d  ~* D! M* v4 `, a2 J- w
  914.     }; A. J, v$ Y& L+ a; o5 `
  915.   }
    / s4 y3 t+ K$ D  H+ l
  916. }
    % l: W, C$ Z6 X
  917. #endif /* STM32F10X_CL */1 B; o; I6 X& c
复制代码

% y! R- s% J, w8 ^+ F9 t4 D1 j9 O! w3 |
main.h 文件
4 E7 p5 @6 f& n: k+ \
  1. /**
    7 b! U; R7 O$ ?# v- w( Y
  2.   ******************************************************************************, ^8 O4 Q' G0 b& C9 B
  3.   * @file    main.h
    ) I1 n+ V4 B' T# l
  4.   * @author  XinLi( {4 M( t) w- O. O" ]
  5.   * @version v1.0( B) y" I9 s) H+ W* s
  6.   * @date    24-June-2018
    # }. M' o4 v: ^6 ^
  7.   * @brief   Header file for main.c module.
    ( D! _0 \& ~  i+ `/ }3 H2 e1 `$ P
  8.   ******************************************************************************
    : d* z- N! n7 l( f9 l* ^
  9.   * @attention
    / Z3 {& c$ @0 f
  10.   *
    : S2 r7 m9 b+ i+ a  @6 r
  11.   * <h2><center>Copyright © 2018 XinLi</center></h2>
    : W! K4 I1 n( c7 i1 ~
  12.   *1 }/ m3 V. _) R/ W( X$ {
  13.   * This program is free software: you can redistribute it and/or modify
    , }! D$ A" B  `
  14.   * it under the terms of the GNU General Public License as published by! Q2 h# B  R+ s4 f9 p
  15.   * the Free Software Foundation, either version 3 of the License, or
    ) r/ Q- j# ]! K; Y# d+ ?) [
  16.   * (at your option) any later version.
    - P, R/ c9 n! |6 z& Q0 i
  17.   *: g) ]7 `* g( R/ G
  18.   * This program is distributed in the hope that it will be useful,
    4 L. l8 L6 ?3 h# a
  19.   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    & ^; ^6 k/ f$ E
  20.   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    . H% c0 }* E+ M1 ]  w
  21.   * GNU General Public License for more details.) X/ r$ R, y6 ]% p2 L' G
  22.   *
    6 {; e& X; R0 v- H& p: `) M
  23.   * You should have received a copy of the GNU General Public License
    & _# s5 t9 [8 D$ ~1 H" m
  24.   * along with this program.  If not, see <<a href="https://www.gnu.org/licenses/>." target="_blank">https://www.gnu.org/licenses/>.</a>8 q( t* _1 H. M" c# Z
  25.   *
    & a1 E/ C1 n- z& b
  26.   ******************************************************************************, P' W% m& T6 y6 D
  27.   */* k: d7 @% H: w) |" e# V0 x8 `6 c8 G6 C
  28. 1 M9 |6 F% E' D( u6 R% s- i
  29. #ifndef __MAIN_H
    & t. j( F7 k4 L( z+ R; j8 ]4 @
  30. #define __MAIN_H  N+ s3 s5 v+ o# u  t& V) b/ Q6 n$ i/ r
  31. ( w# I5 c4 n, i5 {/ b! d
  32. #ifdef __cplusplus
    ' i9 l7 G1 y2 [  p
  33. extern "C" {; }) R5 w! i4 }+ Z6 g
  34. #endif. i1 J3 T0 p' [# D

  35. 4 k0 ?1 H9 J- x* C
  36. /* Header includes -----------------------------------------------------------*/3 E' ], B5 Q  I1 o
  37. #include "stm32f10x.h"( e  e# |6 n3 J" q( R" X4 i: l. V
  38. $ ]  b# k1 L6 a& F( L& W
  39. /* Macro definitions ---------------------------------------------------------*/
    . P' ^  G9 |" s: Z* o$ t
  40. /* Type definitions ----------------------------------------------------------*/- [. ?9 r- @5 d" D1 X& E
  41. /* Variable declarations -----------------------------------------------------*// k" t& z" q) q8 e- K+ W* R  p/ l
  42. /* Variable definitions ------------------------------------------------------*/
    & z) }2 }, d! b7 m9 [& V2 l! G
  43. /* Function declarations -----------------------------------------------------*/# a0 z1 n4 b9 E
  44. /* Function definitions ------------------------------------------------------*/
    / Y6 M- m/ n3 W& _* [- \- \

  45. ! J( u2 w3 d+ D& n( j
  46. #ifdef __cplusplus( |5 O6 x: [) ^" c; C9 m
  47. }! J7 y  E  r+ J% F8 p8 U  ~
  48. #endif
      }- @) |: X! E7 C# m& h+ B, H
  49. / `. t! m5 H8 n% }1 x7 j- Y0 m0 t! ^
  50. #endif /* __MAIN_H */
复制代码
# o# J+ j( e% {

9 O) B8 S' V) Qmain.c 文件
5 @! D- w! S2 z
  1. /**
    ( f( J5 q1 X- H! a: E7 m, ~
  2.   ******************************************************************************, {4 I3 ]0 l! u' o% _
  3.   * @file    main.c% m& `$ }7 X7 X! L
  4.   * @author  XinLi
    ' v) |& T' H; o7 H# u: y% \+ W
  5.   * @version v1.0- @4 `  Y) i) h- g0 c1 [
  6.   * @date    24-June-2018
    # G0 T. O' g+ \& f; a8 M0 u: G
  7.   * @brief   Main program body./ Q( v/ H7 P! d  \7 a, Z6 h
  8.   ******************************************************************************
    + z/ H% }7 z2 O" E8 l8 o6 y
  9.   * @attention
    ; E4 f3 N, W) ]7 a% |8 ~2 {5 P
  10.   *
    7 r% D( f9 ^$ C- q. M5 ^6 d
  11.   * <h2><center>Copyright © 2018 XinLi</center></h2>
    ; j& }6 Z( q# O3 ?/ M1 R) {
  12.   *9 W4 I2 t9 r- R" q0 x3 `! ?6 r
  13.   * This program is free software: you can redistribute it and/or modify1 q4 n1 \) M6 w% N* ~
  14.   * it under the terms of the GNU General Public License as published by. X* Q3 `* r# V9 M6 D1 M8 x
  15.   * the Free Software Foundation, either version 3 of the License, or% A; Z: D: p+ E+ m% c
  16.   * (at your option) any later version.
    7 S9 S% k+ I* r) T- N
  17.   *
    " n( B0 ]" o* Q2 u3 A$ a
  18.   * This program is distributed in the hope that it will be useful,
    5 x$ v* b% A3 d8 u
  19.   * but WITHOUT ANY WARRANTY; without even the implied warranty of# f4 k0 Q) a8 T$ p$ s! O4 U
  20.   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    : F9 P; ^, l" \( S
  21.   * GNU General Public License for more details.
    & b; f$ a: T5 K. I
  22.   *0 y: D+ d4 y9 ^6 r
  23.   * You should have received a copy of the GNU General Public License5 d: |. T( q# Z6 ]7 Y. O$ h( @
  24.   * along with this program.  If not, see <<a href="https://www.gnu.org/licenses/>." target="_blank">https://www.gnu.org/licenses/>.</a>
    . A: M) p! `# r
  25.   *5 s) ]1 {# S: s5 l: w* X
  26.   ******************************************************************************
    , f7 q9 \6 H) V) ]2 n( ~
  27.   */
    6 ~! p/ b& C0 e7 T  u$ l

  28. 8 V0 E6 Y7 p9 p6 L% N/ ]
  29. /* Header includes -----------------------------------------------------------*/" T+ N- ~: L' f# w# ~6 z! d
  30. #include "main.h"* X, {* B# o! C
  31. #include "CAN.h"3 p& }; a9 S  z8 i% E9 ^, R
  32. ; q6 w# J2 Z) ?  x; T/ o
  33. #ifdef _RTE_! z: q/ G* ^  p# m; h
  34. #include "RTE_Components.h"
    ; m- c2 V( B- J( n3 w/ T
  35. #endif
    4 L' }' o3 u- I2 _6 l
  36. ) F$ n! g# D- v9 T0 p
  37. #ifdef RTE_CMSIS_RTOS2
    0 S$ [' o6 w8 P/ C: p1 Q* o' ?
  38. #include "cmsis_os2.h"5 u. I' r) ]3 _" ~1 S- r- O+ t
  39. #endif
    4 O4 Q1 }% K  {, j8 K
  40. 1 t1 a7 g( `1 K7 B( Y
  41. /* Macro definitions ---------------------------------------------------------*/
    7 P. i1 z7 G3 |6 J
  42. /* Type definitions ----------------------------------------------------------*/. o$ S8 d5 }$ x( w
  43. /* Variable declarations -----------------------------------------------------*/
    : I  w3 S% C7 m; I4 G5 x
  44. /* Variable definitions ------------------------------------------------------*/
    8 m1 z7 X. k4 a* d# ~  X" j4 W
  45. static CanTxMsg canTxMsg = {0};5 i, l( j/ C1 Q* W
  46. static CanRxMsg canRxMsg = {0};2 v$ t6 i2 X3 }
  47. / L9 N, J3 e$ b* N+ @( X# [
  48. /* Function declarations -----------------------------------------------------*/
    ( U. T& N$ ^% a+ {/ W( z/ Q
  49. static void SystemClock_Config(void);
    3 s( |4 M# Q2 x+ n6 g
  50. ! W- @: r) l+ Z1 a# R: X
  51. /* Function definitions ------------------------------------------------------*/- E1 ?9 P: h3 i; N. r
  52. 5 O% D. q& I$ O
  53. /**
    % n3 o0 H9 B5 @) S2 n3 p+ _+ J3 G
  54.   * @brief  Main program.
    * N+ Z% |+ g: {- K8 h) H8 c% G
  55.   * @param  None." w  z' j0 u6 F( P$ G: f
  56.   * @return None.
    : B9 E6 e5 u' b# [1 z
  57.   */
    # T7 Y& z* u9 v4 B' ^% H
  58. int main(void)
    6 C, s+ O, T  r
  59. {
    7 p$ Z& {  k2 I' _4 X3 t9 u+ w
  60.   /* Configure the system clock to 72 MHz */0 O+ m0 Z$ ]& H" F5 m/ S3 D/ t  S: Q
  61.   SystemClock_Config();
    % F$ w( P5 T8 R7 z5 R" z2 x
  62.   SystemCoreClockUpdate();8 L* d4 @4 P: S7 C- F

  63. - ?. a# P5 V; n$ f9 t
  64.   /* Add your application code here */
      b% v! S) n( h5 d5 [
  65.   CAN_Configure(CAN1, CAN_WorkModeLoopBack, CAN_BaudRate250K, 0xAA55, 0x55AA);; h) i1 i9 X+ d' C0 J0 Y

  66. 1 p5 u* g# y; [0 J8 |
  67. #ifdef RTE_CMSIS_RTOS2
    9 v4 x# @' }' Y9 Y* f
  68.   /* Initialize CMSIS-RTOS2 */
    / T' h( f% v4 G0 I; p
  69.   osKernelInitialize();, U1 ]  t  W" T( b9 L: ~
  70. * i9 m! l! [2 F: S! ~3 J
  71.   /* Create thread functions that start executing,
    4 @# c1 Z+ Q  W* x
  72.   Example: osThreadNew(app_main, NULL, NULL); */
    ; m5 N' L0 O/ f6 i) `( S6 C1 V
  73. ' q- N5 r& {  u& f2 e# @. A
  74.   /* Start thread execution */
    : G3 r4 D) e3 ]3 w2 p9 W! u
  75.   osKernelStart();8 j6 r" T  f1 G, t7 N" E
  76. #endif
      s& K9 H  s$ T* V4 ^
  77. 7 A- x5 w! y4 e
  78.   /* Infinite loop */1 _  q# R' w$ e+ }6 g+ |
  79.   while(1)6 @1 F5 l  ^: A. C! U$ D0 z  p
  80.   {* e& t) g9 M6 W+ S) G
  81.     canTxMsg.StdId = 0xAA55;7 p  J! h4 ?% X2 |
  82.     canTxMsg.ExtId = 0x55AA;
    2 p+ n0 G( \5 U) L+ W0 [
  83.     canTxMsg.IDE   = CAN_ID_STD;
    & N0 j1 d- f( j9 e5 b" u5 C$ f" q
  84.     canTxMsg.RTR   = CAN_RTR_DATA;2 m$ j4 O) |6 [' e9 D
  85.     canTxMsg.DLC   = 8;! q+ u& m2 \3 t+ S
  86. + z6 `7 u; e& ?' O+ T5 |. z1 k
  87.     canTxMsg.Data[0]++;6 Z1 I, ]0 s3 _& b
  88.     canTxMsg.Data[1]++;) t, y  S7 M, b% s0 J1 q0 g
  89.     canTxMsg.Data[2]++;( n* ^8 w7 S8 c- V  X
  90.     canTxMsg.Data[3]++;
    9 b1 e$ E2 T" w0 I
  91.     canTxMsg.Data[4]++;! X1 G- d# Q, m# Q% R7 L# w+ p
  92.     canTxMsg.Data[5]++;3 T5 T# T* t5 C; L  {
  93.     canTxMsg.Data[6]++;
    + v" a' S; ]8 R6 r% L( h
  94.     canTxMsg.Data[7]++;
    2 H- n4 c2 S4 Y  c: f6 D
  95. / d' v- _! N# I9 Q3 w/ @  i! z+ j
  96.     CAN_SetTransmitMessage(CAN1, &canTxMsg, 1);
    / Z. R7 C; L# b3 ~2 J3 R
  97. : V1 {* }( L7 f2 m( E2 v
  98.     while(CAN_IsReceiveBufferEmpty(CAN1) == true);) W' a6 X$ A! I, u

  99. 1 z" f$ S: Z7 h8 w7 t0 k- M
  100.     CAN_GetReceiveMessage(CAN1, &canRxMsg, 1);) f+ ?$ L2 S% S0 E3 j
  101.   }: l5 I" |. G$ A! u6 A' b9 f
  102. }
    1 m8 P7 X( t: h- Y. ]
  103. 9 H# V8 T, G9 H7 r" u# s$ l; |
  104. /**; p' K* P6 x/ s
  105.   * @brief  System Clock Configuration.: q: b5 e' _$ n5 x: @2 `, ?- P
  106.   *         The system Clock is configured as follow : - j9 D" _7 J1 [
  107.   *            System Clock source            = PLL (HSE)
    ) r. p! S, b! Z9 t3 \# Q
  108.   *            SYSCLK(Hz)                     = 72000000
    " y( O- D# e" s/ G# W0 f- ]
  109.   *            HCLK(Hz)                       = 72000000- m; ?$ a( r1 e- j# y5 \0 c
  110.   *            AHB Prescaler                  = 1
    7 R. s. G4 @8 j/ L7 C
  111.   *            APB1 Prescaler                 = 28 r* G5 o4 u. s' q
  112.   *            APB2 Prescaler                 = 1  r" _  E0 y- G+ y
  113.   *            HSE Frequency(Hz)              = 8000000
    + O7 }! `1 T. A( A
  114.   *            HSE PREDIV1                    = 11 z7 Z  E" R) h/ D+ A& J8 }
  115.   *            PLLMUL                         = 9
    1 v% f8 ?: X$ v7 j0 h7 T3 q
  116.   *            Flash Latency(WS)              = 21 Z+ T8 _/ A% z; e
  117.   * @param  None.
    " |/ E7 M/ n4 k# Y
  118.   * @return None.
    7 _& e! [* u# e
  119.   */: ]$ D4 p" k4 o8 n9 [: P
  120. static void SystemClock_Config(void)
    ' E  C' D9 {, N1 L
  121. {8 Y; w. M( _6 W* U! a
  122.   /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration */
    6 g2 A6 Q0 Y# p
  123.   /* RCC system reset */) G9 q2 m3 Q2 _$ G" Q" T
  124.   RCC_DeInit();4 q8 ~! w- D; K

  125. 0 B; ~2 w: Q+ ^
  126.   /* Enable HSE */8 I$ ]5 i: }* ^! K- R- J4 W
  127.   RCC_HSEConfig(RCC_HSE_ON);# U" u1 Z, _# I3 q7 O$ @. \
  128. 3 E8 w* Q# o# a3 u) Y
  129.   /* Wait till HSE is ready */
    . K: M2 \# _4 E( k& {% a
  130.   ErrorStatus HSEStartUpStatus = RCC_WaitForHSEStartUp();
    0 c( S& k, J0 w5 y7 ~4 `4 k

  131. ! U" W0 O/ R: ]9 y: `
  132.   if(HSEStartUpStatus == SUCCESS)
    7 f" X9 R: a* J) A8 [  E
  133.   {
    ( U, B4 u1 p  s6 a; M+ x3 j
  134.     /* Enable Prefetch Buffer */& N* q  ]0 L4 u. V. |2 }/ s  R
  135.     FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
    ' u* G6 E% i3 ^) x2 s: x

  136. 1 U4 `* z/ q% }, [* M
  137.     /* Flash 2 wait state */5 y3 S, p* k$ W1 c" v8 _
  138.     FLASH_SetLatency(FLASH_Latency_2);5 z2 v/ ?( t# e! I
  139. ( F7 r; v) B: ?: C5 n8 x3 o; O
  140.     /* HCLK = SYSCLK */
    9 s! L# l# U( P; i
  141.     RCC_HCLKConfig(RCC_SYSCLK_Div1);
    & F0 ]2 D8 }2 U2 I) X3 E2 i
  142. ) D9 `; S& p0 |$ w& T
  143.     /* PCLK2 = HCLK */( U- _2 V. O: u5 V( e
  144.     RCC_PCLK2Config(RCC_HCLK_Div1); 0 k9 W! ]' g( K. ^
  145.   \) P$ M/ `8 d( }! C
  146.     /* PCLK1 = HCLK / 2 */4 }- d# |+ G* {) _- V0 P4 ^: X6 K( ^
  147.     RCC_PCLK1Config(RCC_HCLK_Div2);- {  V3 j2 ~! H8 F  ~4 i& p4 `

  148. + u. w* s; ^: u' a* |
  149.     /* Configure PLLs */& O2 J  w* e5 b$ P+ ~& q
  150. #ifdef STM32F10X_CL
    ' I" `. y& L; k9 l0 `4 }
  151.     /* PLL2 configuration: PLL2CLK = (HSE(8MHz) / 2) * 10 = 40MHz */
    ' E; k4 \) S0 X$ X* F  ]9 ]$ H
  152.     RCC_PREDIV2Config(RCC_PREDIV2_Div2);- Q, n0 O5 J0 a" z
  153.     RCC_PLL2Config(RCC_PLL2Mul_10);
    5 t. H3 r) O0 k# ^/ ~, i& f
  154. 6 H6 D7 V1 F. V$ A8 m
  155.     /* Enable PLL2 */
    / b) E, f8 y; Q  c: g/ t3 e! o
  156.     RCC_PLL2Cmd(ENABLE);
      S, ~9 i0 w* g2 c% O) L1 ^8 W1 G( O
  157. , r5 _9 m/ o1 j, i; a
  158.     /* Wait till PLL2 is ready */
    ' X+ U+ j& ?6 j5 O; }
  159.     while(RCC_GetFlagStatus(RCC_FLAG_PLL2RDY) == RESET);% h$ A) `! j: h& h9 C/ G2 f) m' P

  160. 1 ]) j& @6 t  _. }# j
  161.     /* PLL configuration: PLLCLK = (PLL2(40MHz) / 5) * 9 = 72MHz */
    0 E! V4 x4 v9 D' k
  162.     RCC_PREDIV1Config(RCC_PREDIV1_Source_PLL2, RCC_PREDIV1_Div5);
    $ f" f, N  T4 }1 z
  163.     RCC_PLLConfig(RCC_PLLSource_PREDIV1, RCC_PLLMul_9);
    9 ~  i7 o: w1 e. r5 {6 t, I6 p2 z
  164. #else! L( h) s' T! y0 p- \
  165.     /* PLLCLK = HSE(8MHz) * 9 = 72MHz */! v2 K. {- p  o# n+ J- P
  166.     RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
    . h  u" |' t+ ~! l) H
  167. #endif) ^. e, G8 y! _# v0 u" b) y+ e: X
  168. 8 P* @3 g' a: h2 n: |/ z1 B
  169.     /* Enable PLL */# g8 Z( J" K" Z
  170.     RCC_PLLCmd(ENABLE);
    ( p: y7 g, H3 F; V

  171. 4 I; I  W6 j; q# e* |. N
  172.     /* Wait till PLL is ready */
    % T' h6 C- J( N! Y$ T/ z1 x6 \
  173.     while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
    & @7 T: e6 j2 C1 @7 c3 u

  174. 8 X4 d% d) b' i" D: R! L
  175.     /* Select PLL as system clock source */6 U0 l  _& w8 r& r- f2 N' Q" p
  176.     RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
    9 {5 p% b* J1 _5 u9 \

  177. , q9 l% r, E4 `, F( d
  178.     /* Wait till PLL is used as system clock source */0 H. S# M# r7 \6 O" G& r8 z, ]
  179.     while(RCC_GetSYSCLKSource() != 0x08);
    7 E& M2 e* {0 t- o
  180.   }
    ( x3 p1 P/ O4 P( E
  181.   else4 K. _" v  k9 }
  182.   {
    . b5 Y. }7 o$ E
  183.     /* Disable HSE */
    % W% p( B7 n0 [6 t/ R. k
  184.     RCC_HSEConfig(RCC_HSE_OFF);
    * y7 e; \9 m/ D+ b, [

  185. ( `. N7 N# p  L+ s. N
  186.     /* Enable HSI */' Q1 ^5 |2 e6 J. l
  187.     RCC_HSICmd(ENABLE);
    : m' [% b3 K; N8 ?, d
  188. 5 M1 p! ^2 M! _. s! T5 Y( j: ^. G; ^
  189.     /* Enable Prefetch Buffer */
    , I1 M' `! g% ^* v$ m/ d' {3 ~  C
  190.     FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
    5 }) ]5 _/ Z/ C7 g7 {- @2 c, @
  191. 7 y9 \% L0 Z' Q) g
  192.     /* Flash 1 wait state */) J+ t! s- a2 k0 I
  193.     FLASH_SetLatency(FLASH_Latency_1);9 E9 R$ }1 F- Q( q7 m8 f; `
  194. ; I) Y6 o7 ^1 j
  195.     /* HCLK = SYSCLK */5 c0 Z+ h9 j; d: g( g9 w+ [6 u
  196.     RCC_HCLKConfig(RCC_SYSCLK_Div1);
    2 c7 @; k  _1 P" B. W$ }
  197. 9 o- q" V! X& K- i4 l! B4 y
  198.     /* PCLK2 = HCLK */2 ^( s0 G% k, {
  199.     RCC_PCLK2Config(RCC_HCLK_Div1); 2 i) A: V: i& J" Q, |+ A
  200. ! a  E% u9 j' @$ r" B
  201.     /* PCLK1 = HCLK */
    / u  L  A: a4 }/ D8 s; f* _2 f
  202.     RCC_PCLK1Config(RCC_HCLK_Div1);
    3 u7 o9 ~+ C* X* D6 v! h

  203. ; j" q4 G& |# J; z2 g9 _- @; X1 _7 C
  204.     /* Configure PLLs */: p& k6 v# G9 {
  205.     /* PLLCLK = HSI(8MHz) / 2 * 9 = 36MHz */
    8 H) [! @* t- v0 |% m8 m
  206.     RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_PLLMul_9);
    3 U0 J; B$ N2 J
  207. / Y: a  y3 r6 x6 w# R& \
  208.     /* Enable PLL */
    2 c" z0 |  U" l8 R# U
  209.     RCC_PLLCmd(ENABLE);8 x7 G$ u; e8 r& w: g$ d% c! G/ h
  210. 2 p+ a2 l7 i6 i1 h: H
  211.     /* Wait till PLL is ready */" K) }+ s" c6 O/ A
  212.     while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
    / b* z- ]- K  m; t# Y

  213. 1 K- S9 D# g0 H( |8 n' V) c
  214.     /* Select PLL as system clock source */; ~/ u" k1 t# Z+ r# [
  215.     RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);. K  T. ^8 E! A) m: i
  216. 0 [/ l/ j, o/ _
  217.     /* Wait till PLL is used as system clock source */
    5 f: O2 z$ d7 r6 ^% p
  218.     while(RCC_GetSYSCLKSource() != 0x08);9 e, a9 w. M; L6 `1 s" |6 y
  219.   }0 @' q2 t% X$ o+ R, M
  220. }
    & R. w& v, ^7 L$ E- |
  221. # T, R3 @1 h+ L1 L/ o8 m
  222. #ifdef USE_FULL_ASSERT
    3 |. H/ c% D* y+ J6 P) I
  223. /**/ F: v) A; A5 x% g7 k: Z" E
  224.   * @brief  Reports the name of the source file and the source line number' ~1 U$ Q- M1 N0 {5 q' n
  225.   *         where the assert_param error has occurred.
    ( i% n, U  m+ l+ S, _* Q/ g- v
  226.   * @param  file: pointer to the source file name./ s/ ]6 s8 t$ B/ q, W
  227.   * @param  line: assert_param error line source number.
    9 J1 {% o9 p9 w7 x+ l4 t1 i
  228.   * @return None.( `1 J, H! A+ r& ^% e( w
  229.   */  N4 [) P4 a- c3 S
  230. void assert_failed(uint8_t *file, uint32_t line)2 D5 E7 L1 ?3 V4 p% k/ u5 `1 C
  231. {
    5 ~  ?' O: [( n' Q
  232.   /* User can add his own implementation to report the file name and line number,; w* o% Z9 C) G& I) b$ Q; f; v2 V
  233.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */" R1 W0 Z% l$ u9 b9 Z6 s$ Y6 F) v
  234. + b, y. h9 @4 O9 s
  235.   /* Infinite loop */
    5 W3 j; P) J7 j; n, `' m
  236.   while(1)
    / U# |! H1 ]/ I4 R- b. R1 C* {
  237.   {. E: K  \& ]* Z" [5 X, x
  238.   }
    + G9 z7 g4 u6 J0 [+ R6 z. u. l
  239. }
    1 ^) B7 f2 h% z" D
  240. #endif
复制代码

) i, S0 R7 g/ }, ~4 x0 ^3,注意& A: M8 H; n8 ]) J% G
. I/ Q# Q5 s# f+ h0 B
CAN 消息发送缓冲区和接收缓冲区的大小,可以根据应用的需求进行修改,缓冲区使用的是堆内存,需要根据缓冲区大小和应用程序中堆内存使用情况进行配置。' v! c0 l1 J' S5 n$ V( ]# S
; I' S1 R, ^7 Q% b! H0 Z! O. x
! ?/ d3 B1 ~/ Y( a, ]

/ {& y! @5 Z, ?7 v# K7 T5 y
赞 收藏 评论0 发布时间:2021-11-26 17:00

举报

0个回答

所属标签

相似技术帖

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