2 c; a" s7 j7 j+ ?# \ {9 X6 n
收到板卡,先点个灯,没什么问题就进入正式测试阶段。仔细阅读H533的Datasheet,是一款资源很丰富的MCU,发现其有两个FDCAN接口,这样就丰富了板卡通讯。这种总线通信方式当前使用的比较多,就先测试下这个外设功能。( y4 g4 ~' E& d7 C
首先搭建测试硬件条件,使用板卡上的PB12和PB13作为通讯接口,连接到TJA1044 驱动芯片板卡上,按照datasheet对应上即可。如图1所示:
4 a4 D8 L: V Y* M) Q
( ?( ~% ^- w6 O7 x, Z
- n: P2 Q# x* O/ e+ k图1; q+ m; ` b1 w3 {& b, _
3 b% v! w6 ]2 Q
8 k: i+ ?) \2 ^$ E: X
- O. {! c$ G/ X* ^7 t" B) j
图2( h6 O& D& Y; G: W
( `4 g. d% Y) S7 o
然后是使用Cubemx软件对MCU的时钟,外设等进行配置,并生成所需要的项目框架。时钟配置如图3所示:+ y) x3 P" L" i6 `0 e% }5 P; V
, n; F5 `( u" J8 f2 |$ o4 e& Q, b+ D. R( q
图37 A5 r% F. h3 A( d" ^/ [
FDCAN的时钟使用24M,不倍频,波特率为1Mbps,数据波特率为2Mbps,外设的配置如图4所示:7 ~2 J3 j# C; j! B( W5 H' B* ^2 I3 O W
& E# b8 V8 D) u$ @" g# @8 h3 ^$ T- j, {8 v4 M, W' j# F" i, ?- z
图4
6 m5 A3 t7 f5 }$ ], W7 i1 U 使用板载的LED作为接收数据的指示灯,可在图上进行配置,如图5所示:
; { L" \/ F/ s9 }$ f" f7 @
9 M5 p' N" V2 Y' z
+ p; N: l( ~9 w( f* W' |' b图5
7 h% D% R6 \6 |& q6 T/ u- ^ 这样配置完后,生成所需要的项目框架。编写接收和发送数据的初始化函数,如下所示:% o5 C5 }) B* W( _# K
j% [3 f3 z/ |' D+ l" x
- /* Prepare Tx message Header */
- K, o, l: X0 h4 r/ t' M - txHeader.Identifier = Tx_ID;1 x' m5 {- g% D) o3 i8 g
- txHeader.IdType = FDCAN_STANDARD_ID;6 z- n# R) w5 g5 X8 ~1 {0 G
- txHeader.TxFrameType = FDCAN_DATA_FRAME;, ~1 H; O' c( j; ~- v4 d
- txHeader.DataLength = FDCAN_DLC_BYTES_64;( O9 }) ?* \- ?! Y( C0 b
- txHeader.ErrorStateIndicator = FDCAN_ESI_ACTIVE;3 z' T# R" Z) R3 }8 i
- txHeader.BitRateSwitch = FDCAN_BRS_ON;
+ h. N# ]( o! C8 B - txHeader.FDFormat = FDCAN_FD_CAN;
2 Q. X4 u5 U! S+ O' a - txHeader.TxEventFifoControl = FDCAN_NO_TX_EVENTS;
3 { ?8 e8 S) Q1 q - txHeader.MessageMarker = 0U;
^+ c. N+ L' q$ _9 u - ' P; k$ s) G# A1 f, P0 P& n
- /**
) s) B/ v4 R% x& Q% ` - * Configure and enable Tx Delay Compensation, required for BRS mode.7 Z7 }' F c/ D, w8 }: `6 X
- * TdcOffset default recommended value: DataTimeSeg1 * DataPrescaler
0 L$ H" X6 Z2 E3 a7 a - * TdcFilter default recommended value: 0
( s4 {# i" y! u' {4 j - */
9 H/ j9 m# L3 X p& C6 J ^+ Z+ U' R7 ] - if (HAL_FDCAN_ConfigTxDelayCompensation(&hfdcan2,7 N5 @% t+ A- q8 { ]
- (hfdcan2.Init.DataPrescaler * hfdcan2.Init.DataTimeSeg1), 0U) != HAL_OK)
: q7 R. {. p" \ ^8 I( u* o - {& Z. f$ d; ^9 m9 g& h! E$ g/ g( B
- Error_Handler();
! g# Y7 w& ^7 O2 S1 E - }0 \0 K7 f' e } F( J
6 X2 s1 }$ ^! M6 l- if (HAL_FDCAN_EnableTxDelayCompensation(&hfdcan2) != HAL_OK)- p/ c, k2 q9 T2 c N- r" A0 O
- {6 G: z( \8 c8 u
- Error_Handler();4 J/ p. p; l5 s) w# A! g6 z, A' [
- }7 c' p) y ~- C7 F
- ! A* W0 a5 c, _; }' \' @
- /* Start FDCAN controller */+ Z, `- E, k7 j5 b9 h5 O7 }5 Q
- if (HAL_FDCAN_Start(&hfdcan2) != HAL_OK), s& t7 K# W$ s$ Q6 |6 Y0 V
- {
7 ]4 f0 U5 h, ]& ^ - Error_Handler();& L) y* M# y: i/ H
- }
复制代码- void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo0ITs)
; ~& D0 d& {- r2 I! K7 T - {
% S* a( ~( V- M - if ((RxFifo0ITs & FDCAN_IT_RX_FIFO0_NEW_MESSAGE) != 0U)
( }) E' e7 Y5 }$ z - {
3 |- d3 `, _; c; z5 F - /* Retrieve Rx messages from RX FIFO0 */
8 r4 a; E7 T6 `8 ~, u6 C0 g" s - if (HAL_FDCAN_GetRxMessage(hfdcan, FDCAN_RX_FIFO0, &rxHeader, rxData) != HAL_OK)
& }. I, ^8 ?! D9 I. n, x - {( P$ r( f5 h% `% C, z
- Error_Handler();4 v( h9 ^# @2 O* ? T2 o
- }+ X+ y, }) m. u: u' K
- + x0 c( J+ T* K" g# a4 y
- /* Compare received RX message to expected data. Ignore if not matching. */
4 [9 I- Z- q) M2 o t8 T! R/ P$ [ - if ((rxHeader.Identifier == Rx_ID) &&* }4 e3 k) z5 ~6 l& @/ d: k
- (rxHeader.IdType == FDCAN_STANDARD_ID) &&
$ }$ y: }: H& v* e5 y0 N1 Y - (rxHeader.DataLength == FDCAN_DLC_BYTES_64))
l9 I, {( z( ]. d4 p j+ P - {
6 D% k2 _- ?( z( T7 C0 _9 S
6 V3 }; o2 c: G6 X6 e+ k- HAL_GPIO_TogglePin (GPIOA,GPIO_PIN_5);9 x9 [5 Y2 S k; V. c. u" h' _
- }# I+ s5 ]' P/ q5 s2 o% n
- } F# c+ W2 f- \$ o
- }
复制代码- if (HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan2, &txHeader, txData) != HAL_OK)! Z/ g2 q2 K+ a7 T
- {
) U# Z+ L7 O5 ~! P; S( z% _ - Error_Handler();, M- ^& m* n% d4 n' K) L% X; @
- }
7 c, i! P9 |! q2 I" H
# R; O4 a" Q1 a2 R+ N- /* Delay for simple button debounce */ M$ ?" B3 l- U A" ]
- HAL_Delay(1000U);
复制代码 |