-
Notifications
You must be signed in to change notification settings - Fork 5.4k
[bsp](stm32h7)修复了fdcan驱动的一些bug #11257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
takuya60
wants to merge
3
commits into
RT-Thread:master
Choose a base branch
from
takuya60:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+39
−2
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| * 2020-02-24 heyuan the first version | ||
| * 2020-08-17 malongwei Fix something | ||
| * 2025-10-27 pandafeng Fix some bugs | ||
| * 2026-03-16 sayaZhao Fix some bugs | ||
| */ | ||
|
|
||
| #include "drv_fdcan.h" | ||
|
|
@@ -54,12 +55,30 @@ static const stm32_fdcan_timing_t st_FDCAN_ArbTiming[] = | |
| static const stm32_fdcan_timing_t st_FDCAN_DataTiming[] = | ||
| { | ||
| {CAN1MBaud * 8, {1, 3, 1, 1, 0}}, /* 8Mbps */ | ||
| {CAN1MBaud * 5, {1, 5, 2, 1, 0}}, /* 5Mbps */ | ||
| {CAN500kBaud *8, {1, 7, 2, 1, 0}}, /* 4Mbps */ | ||
| {CAN250kBaud * 8, {4, 3, 1, 1, 0}}, /* 2Mbps */ | ||
| {CAN125kBaud *8, {1, 31, 8, 1, 0}}, /* 1Mkbps */ | ||
| {CAN100kBaud*8, {2, 19, 5, 1, 0}}, /* 800kbps */ | ||
| {CAN50kBaud *8, {5, 15, 4, 1, 0}}, /* 400kbps */ | ||
| }; | ||
|
|
||
| /** | ||
| * @brief Convert CAN-FD DLC (Data Length Code) back to actual frame length | ||
| * @param dlc DLC code (0~15) | ||
| * @return Actual length in bytes (0~64) | ||
| */ | ||
| static uint8_t dlc_to_length(uint32_t dlc) | ||
| { | ||
| const uint8_t dlc_to_len_table[16] = { | ||
| 0, 1, 2, 3, 4, 5, 6, 7, | ||
| 8, 12, 16, 20, 24, 32, 48, 64 | ||
| }; | ||
|
|
||
| if (dlc > 15) return 8; | ||
| return dlc_to_len_table[dlc]; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Convert CAN-FD frame length to DLC (Data Length Code) | ||
| * | ||
|
|
@@ -208,8 +227,18 @@ static rt_err_t _inline_can_config(struct rt_can_device *can, struct can_configu | |
| { | ||
| return -RT_ERROR; | ||
| } | ||
| /* Transceiver Delay Compensation */ | ||
| #ifdef RT_CAN_USING_CANFD | ||
| if (cfg->enable_canfd) | ||
| { | ||
| HAL_FDCAN_ConfigTxDelayCompensation(&pdrv_can->fdcanHandle, 0x0C, 0x00); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这段代码作用是什么? |
||
| HAL_FDCAN_EnableTxDelayCompensation(&pdrv_can->fdcanHandle); | ||
| } | ||
| #endif | ||
| /* default filter config */ | ||
| HAL_FDCAN_ConfigFilter(&pdrv_can->fdcanHandle , &pdrv_can->FilterConfig); | ||
| /* FIFO RX INT */ | ||
| HAL_FDCAN_ActivateNotification(&pdrv_can->fdcanHandle, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0); | ||
| /*init fdcan tx header*/ | ||
| pdrv_can->TxHeader.Identifier = 0x000000; | ||
| pdrv_can->TxHeader.IdType = FDCAN_EXTENDED_ID; | ||
|
|
@@ -352,7 +381,7 @@ static rt_err_t _inline_can_control(struct rt_can_device *can, int cmd, void *ar | |
| argval = (rt_uint32_t) arg; | ||
| uint32_t arb_idx = _inline_get_ArbBaudIndex(argval); | ||
| if (arb_idx == (uint32_t) -1) { | ||
| return -RT_ERROR; | ||
| return -RT_ERROR; | ||
| } | ||
| if (argval != pdrv_can->device.config.baud_rate) { | ||
| pdrv_can->device.config.baud_rate = argval; | ||
|
|
@@ -452,6 +481,12 @@ static int _inline_can_sendmsg(struct rt_can_device *can, const void *buf, rt_ui | |
| pdrv_can->TxHeader.FDFormat = FDCAN_CLASSIC_CAN; | ||
| } | ||
|
|
||
| if (pmsg->brs == 1) { | ||
| pdrv_can->TxHeader.BitRateSwitch = FDCAN_BRS_ON; | ||
| } else { | ||
| pdrv_can->TxHeader.BitRateSwitch = FDCAN_BRS_OFF; | ||
| } | ||
|
|
||
| if(HAL_FDCAN_AddMessageToTxBuffer(&pdrv_can->fdcanHandle, &pdrv_can->TxHeader, pmsg->data, FDCAN_TX_BUFFER0 + box_num) != HAL_OK) | ||
| { | ||
| return -RT_ERROR; | ||
|
|
@@ -499,7 +534,9 @@ static int _inline_can_recvmsg(struct rt_can_device *can, void *buf, rt_uint32_t | |
| } | ||
| pmsg->id = pdrv_can->RxHeader.Identifier; | ||
|
|
||
| pmsg->len = pdrv_can->RxHeader.DataLength; | ||
| uint32_t actual_dlc = pdrv_can->RxHeader.DataLength; | ||
| pmsg->len = dlc_to_length(actual_dlc); | ||
|
|
||
| pmsg->hdr_index = pdrv_can->RxHeader.FilterIndex; | ||
|
|
||
| #ifdef RT_CAN_USING_CANFD | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不需要使用这个宏来区分吧,如果使用了CANFD这个会使能的