From eefa3d51b1b78488e06a45f321814af6d8d9809e Mon Sep 17 00:00:00 2001 From: sunbeam Date: Sat, 21 Jun 2025 17:19:31 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A5=E6=94=B6=E6=8A=A5=E6=96=87OK?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/c_cpp_properties.json | 20 ++ .vscode/settings.json | 8 + .vscode/tasks.json | 17 ++ ASW/UartDiagApp/Uart2CanDiag/CanDrv.c | 92 +++++++ ASW/UartDiagApp/Uart2CanDiag/CanDrv.h | 37 +++ .../Uart2CanDiag/Uart2CanDiagApp.c | 228 ++++++++++++++++++ .../Uart2CanDiag/Uart2CanDiagApp.h | 6 +- ASW/UartDiagApp/ud_66.c | 43 +++- ASW/UartDiagApp/ud_66.h | 2 +- ASW/appTask.c | 12 +- ASW/uartapp.c | 10 +- CMakeLists.txt | 1 + 12 files changed, 463 insertions(+), 13 deletions(-) create mode 100644 .vscode/c_cpp_properties.json create mode 100644 .vscode/settings.json create mode 100644 .vscode/tasks.json create mode 100644 ASW/UartDiagApp/Uart2CanDiag/CanDrv.c create mode 100644 ASW/UartDiagApp/Uart2CanDiag/CanDrv.h diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..ddc4e3d --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,20 @@ +{ + "configurations": [ + { + "name": "Win32", + "includePath": [ + "${workspaceFolder}/**" + ], + "defines": [ + "_DEBUG", + "UNICODE", + "_UNICODE" + ], + "configurationProvider": "ms-vscode.cmake-tools", + "compileCommands": [ + "${workspaceFolder}/build/Debug/compile_commands.json" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..b6d67c0 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,8 @@ +{ + "commentTranslate.source": "naomi233.comment-translate-transmart-transmart", + "files.associations": { + "candrv.h": "c", + "stm32l4xx_hal.h": "c", + "uart2candiagapp.h": "c" + } +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..141f33b --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,17 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "type": "cmake", + "label": "build", + "command": "build", + "targets": [ + "all" + ], + "preset": "${command:cmake.activeBuildPresetName}", + "group": "build", + "problemMatcher": [], + "detail": "CMake 模板 生成 任务" + } + ] +} \ No newline at end of file diff --git a/ASW/UartDiagApp/Uart2CanDiag/CanDrv.c b/ASW/UartDiagApp/Uart2CanDiag/CanDrv.c new file mode 100644 index 0000000..4ec5db8 --- /dev/null +++ b/ASW/UartDiagApp/Uart2CanDiag/CanDrv.c @@ -0,0 +1,92 @@ +/******************************************************************************* +* the includes +******************************************************************************/ +#include "CanDrv.h" +#include + +/******************************************************************************* +* the defines +******************************************************************************/ +#define TEST_ID 0x222 + + +/******************************************************************************* +* the typedefs +******************************************************************************/ + + +/******************************************************************************* +* the globals +******************************************************************************/ +extern CAN_HandleTypeDef hcan1; +static CAN_TxHeaderTypeDef txhead; +static uint32_t TxMailbox = CAN_TX_MAILBOX0; +/******************************************************************************* +* the const +******************************************************************************/ + + + +/******************************************************************************* +* the functions +******************************************************************************/ +uint8_t CanDrv_Init(void) +{ + txhead.StdId = TEST_ID; + txhead.IDE = CAN_ID_STD; + txhead.RTR = CAN_RTR_DATA; + txhead.TransmitGlobalTime = DISABLE; + txhead.DLC = 8; + HAL_CAN_Start(&hcan1); + CAN_FilterTypeDef filter; + filter.FilterMode = CAN_FILTERMODE_IDMASK; + filter.FilterIdHigh = 0x7B2 << 5; // 注意:标准 ID 要左移 5 位 + filter.FilterMaskIdHigh = 0x7FF << 5; + filter.FilterFIFOAssignment = CAN_RX_FIFO0; + filter.FilterBank = 0; + + filter.FilterScale = CAN_FILTERSCALE_32BIT; + filter.FilterActivation = CAN_FILTER_ENABLE; + + + HAL_CAN_ConfigFilter(&hcan1,&filter); + HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING); + return 0; +} + +uint8_t CanDrv_Txmsg(uint16_t id,uint8_t * data) +{ + txhead.StdId = id; + HAL_StatusTypeDef ret = HAL_CAN_AddTxMessage(&hcan1,&txhead,data,&TxMailbox); + //printf("ret=%d",ret); + return ret; +} + +uint8_t txtestdata[8] = {1,2,3,4,5,6,7,8}; +void CanDrv_Test() +{ + CanDrv_Txmsg(TEST_ID,txtestdata); +} + +uint8_t CanDrv_GetCANTxEmpty(void) +{ + uint32_t ret = HAL_CAN_IsTxMessagePending(&hcan1,CAN_TX_MAILBOX0|CAN_TX_MAILBOX1|CAN_TX_MAILBOX2); + if (ret == 0) + { //空闲 + return 1; + } + return 0; +} + +void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan) +{ + CAN_RxHeaderTypeDef rxmsg; + uint8_t data[8]; + HAL_CAN_GetRxMessage(hcan,CAN_RX_FIFO0,&rxmsg,data); + if (rxmsg.StdId >= 0x600) + { + printf("get id:0x%03x",rxmsg.StdId); + } + + +} \ No newline at end of file diff --git a/ASW/UartDiagApp/Uart2CanDiag/CanDrv.h b/ASW/UartDiagApp/Uart2CanDiag/CanDrv.h new file mode 100644 index 0000000..102cfe3 --- /dev/null +++ b/ASW/UartDiagApp/Uart2CanDiag/CanDrv.h @@ -0,0 +1,37 @@ +#ifndef __CANDRV_H__ +#define __CANDRV_H__ + +/******************************************************************************* +* the includes +******************************************************************************/ +#include "stm32l4xx_hal.h" + +/******************************************************************************* +* the defines +******************************************************************************/ + + + +/******************************************************************************* +* the typedefs +******************************************************************************/ + + + +/******************************************************************************* +* the globals +******************************************************************************/ + + + +/******************************************************************************* +* the functions +******************************************************************************/ + +uint8_t CanDrv_Init(void); +uint8_t CanDrv_Txmsg(uint16_t id,uint8_t * data); +void CanDrv_Test(void); +uint8_t CanDrv_GetCANTxEmpty(void); + + +#endif diff --git a/ASW/UartDiagApp/Uart2CanDiag/Uart2CanDiagApp.c b/ASW/UartDiagApp/Uart2CanDiag/Uart2CanDiagApp.c index e69de29..5970db4 100644 --- a/ASW/UartDiagApp/Uart2CanDiag/Uart2CanDiagApp.c +++ b/ASW/UartDiagApp/Uart2CanDiag/Uart2CanDiagApp.c @@ -0,0 +1,228 @@ +/******************************************************************************* +* the includes +******************************************************************************/ +#include "Uart2CanDiagApp.h" +#include +#include +#include "CanDrv.h" +/******************************************************************************* +* the defines +******************************************************************************/ +#define TX_BUF_SIZE 2050 +#define N_Bs 150 +#define N_As 70 +#define P2_SERVER 50 +/******************************************************************************* +* the typedefs +******************************************************************************/ +typedef enum +{ + U2CD_TASK_idle=0, + U2CD_TASK_WaitFC,//流控 + U2CD_TASK_TxCF, + U2CD_TASK_WaitResp, + U2CD_TASK_NUM +}U2CD_TASK_STATE; + +/******************************************************************************* +* the globals +******************************************************************************/ +static uint16_t U2CD_pid; +static uint16_t U2CD_fid; +static uint16_t U2CD_rid; +static uint8_t U2CD_stmin; +static uint8_t U2CD_fill; +static uint8_t U2CD_cfgFlag; +static uint8_t U2CD_TTFlag; +static uint16_t U2CD_TTLen; +static uint8_t U2CD_Txbuf[TX_BUF_SIZE]; +static uint16_t U2CD_TxCnt;//发送数据指针 +static uint8_t U2CD_TxSN; +static uint8_t U2CD_TxTimeCounter; + +static uint8_t U2CD_FCFlag = 0; +static uint8_t U2CD_Framebuf[8]; +static U2CD_TASK_STATE task_state; +/******************************************************************************* +* the const +******************************************************************************/ + + + +/******************************************************************************* +* the functions +******************************************************************************/ +static void U2CD_TxSF(uint16_t id,uint8_t* data,uint16_t len); +static void U2CD_TxFF(uint16_t id,uint8_t* data,uint16_t len); +static void U2CD_TxCF(uint16_t id,uint8_t* data,uint16_t len); + +void U2CD_Init(void) +{ + U2CD_cfgFlag = 0; + U2CD_TTFlag = 0; + task_state = U2CD_TASK_idle; +} +//pid,fid,rid,stmin,fill +void U2CD_cfg(uint16_t pid,uint16_t fid,uint16_t rid,uint8_t stmin,uint8_t fill) +{ + if (pid<0x7ff&&fid<0x7ff&&rid<0x7ff) + { + U2CD_pid = pid; + U2CD_fid = fid; + U2CD_rid = rid; + U2CD_stmin = stmin; + U2CD_fill = fill; + U2CD_cfgFlag = 1; + } + else + { + U2CD_cfgFlag = 0; + printf("err:配置错误\n"); + } + +} +void U2CD_TickTask(void) +{ + U2CD_TxTimeCounter++; +} +void U2CD_Task(void) +{ + switch (task_state) + { + case U2CD_TASK_idle: + if (U2CD_TTFlag == 1) + { + if (U2CD_TTLen <= 7) + { + U2CD_TxSF(U2CD_pid,U2CD_Txbuf,U2CD_TTLen); + task_state = U2CD_TASK_WaitResp; + U2CD_TxTimeCounter = 0; + } + else + { + U2CD_TxFF(U2CD_pid,U2CD_Txbuf,U2CD_TTLen); + task_state = U2CD_TASK_WaitFC; + U2CD_TxTimeCounter = 0; + U2CD_FCFlag = 0; + } + + } + + break; + case U2CD_TASK_WaitFC: + if (U2CD_TxTimeCounter > N_Bs) + { + printf("err:N_Bs timeout\n"); + task_state = U2CD_TASK_idle; + } + if (U2CD_FCFlag == 1) + { + U2CD_TxTimeCounter = 0; + task_state = U2CD_TASK_TxCF; + } + + + break; + case U2CD_TASK_TxCF: + + if (U2CD_TxTimeCounter > U2CD_stmin) + { + if (CanDrv_GetCANTxEmpty() == 1) + { + U2CD_TxTimeCounter = 0; + if (U2CD_TxCnt+6 < U2CD_TTLen) + { + U2CD_TxCF(U2CD_pid,&U2CD_Txbuf[U2CD_TxCnt],6); + U2CD_TxCnt += 6; + } + else + { + //最后一帧 + U2CD_TxCF(U2CD_pid,&U2CD_Txbuf[U2CD_TxCnt],(U2CD_TTLen-U2CD_TxCnt)); + task_state = U2CD_TASK_WaitResp; + } + + } + + } + if (U2CD_TxTimeCounter>N_As) + { + printf("err:N_As timeout\n"); + task_state = U2CD_TASK_idle; + } + break; + case U2CD_TASK_WaitResp: + if (U2CD_TxTimeCounter>P2_SERVER) + { + printf("err:P2_SERVER timeout\n"); + task_state = U2CD_TASK_idle; + } + break; + default: + break; + } +} + +void U2CD_TTreq(uint8_t* data,uint16_t len) +{ + if (len > TX_BUF_SIZE) + { + printf("err:len too long"); + return; + } + if (U2CD_cfgFlag == 0) + { + printf("err:no cfg"); + return; + } + if (task_state != U2CD_TASK_idle) + { + printf("err:busy"); + return; + } + + + memcpy(U2CD_Txbuf,data,len); + U2CD_TTLen = len; + U2CD_TTFlag = 1; +} + +static void U2CD_TxSF(uint16_t id,uint8_t* data,uint16_t len) +{ + U2CD_Framebuf[0] = len; + memcpy(&U2CD_Framebuf[1],data,len); + if (len < 7) + { + for (uint8_t i = (len+1); i < 8; i++) + { + U2CD_Framebuf[i] = U2CD_fill; + } + } + CanDrv_Txmsg(id,U2CD_Framebuf); +} + +static void U2CD_TxFF(uint16_t id,uint8_t* data,uint16_t len) +{ + U2CD_Framebuf[0] = 0x10 + (len>>8); + U2CD_Framebuf[1] = len&0xff; + memcpy(&U2CD_Framebuf[2],data,6); + CanDrv_Txmsg(id,U2CD_Framebuf); + U2CD_TxCnt+=6; + U2CD_TxSN = 1; +} + +static void U2CD_TxCF(uint16_t id,uint8_t* data,uint16_t len) +{ + U2CD_Framebuf[0] = 0x20 + (U2CD_TxSN&0x0f); + memcpy(&U2CD_Framebuf[1],data,len); + if (len < 7) + { + for (uint8_t i = (len+1); i < 8; i++) + { + U2CD_Framebuf[i] = U2CD_fill; + } + } + + CanDrv_Txmsg(id,U2CD_Framebuf); + U2CD_TxSN++; +} \ No newline at end of file diff --git a/ASW/UartDiagApp/Uart2CanDiag/Uart2CanDiagApp.h b/ASW/UartDiagApp/Uart2CanDiag/Uart2CanDiagApp.h index deaa883..6b3f550 100644 --- a/ASW/UartDiagApp/Uart2CanDiag/Uart2CanDiagApp.h +++ b/ASW/UartDiagApp/Uart2CanDiag/Uart2CanDiagApp.h @@ -27,8 +27,10 @@ /******************************************************************************* * the functions ******************************************************************************/ - - +void U2CD_Init(void); +void U2CD_cfg(uint16_t pid,uint16_t fid,uint16_t rid,uint8_t stmin,uint8_t fill); +void U2CD_Task(void); +void U2CD_TickTask(void); diff --git a/ASW/UartDiagApp/ud_66.c b/ASW/UartDiagApp/ud_66.c index fb8cdbe..33357c9 100644 --- a/ASW/UartDiagApp/ud_66.c +++ b/ASW/UartDiagApp/ud_66.c @@ -2,7 +2,7 @@ * the includes ******************************************************************************/ #include "ud_66.h" - +#include "Uart2CanDiagApp.h" /******************************************************************************* * the defines @@ -15,8 +15,8 @@ ******************************************************************************/ typedef enum { - CMD66_CFG,//配置P - + CMD66_CFG,//配置pid,fid,rid,stmin + CMD66_TTreq,//TT – Transparent Transmission透明传输 CMD66_NUM, } CMD66_type; @@ -35,7 +35,44 @@ typedef enum /******************************************************************************* * the functions ******************************************************************************/ +static void ud66_cfg(uint8_t* data, uint16_t len); +void uartapp_cmd66(uint8_t* data, uint16_t len) +{ + if (len < 1) + { + return; + } + CMD66_type subid = data[0]; + switch (subid) + { + case CMD66_CFG: + + ud66_cfg(data+1,len-1); + + break; + + default: + break; + } +} + + +static void ud66_cfg(uint8_t* data, uint16_t len) +{ + //pid,fid,rid,stmin,fill + //2+2+2+1+rev1 + if (len == 8) + { + uint16_t pid = (data[0]<<8) + data[1]; + uint16_t fid = (data[2]<<8) + data[3]; + uint16_t rid = (data[4]<<8) + data[5]; + uint8_t stmin = data[6]; + uint8_t fill = data[7]; + U2CD_cfg(pid,fid,rid,stmin,fill); + } + +} diff --git a/ASW/UartDiagApp/ud_66.h b/ASW/UartDiagApp/ud_66.h index 2f6dc9a..a365483 100644 --- a/ASW/UartDiagApp/ud_66.h +++ b/ASW/UartDiagApp/ud_66.h @@ -27,7 +27,7 @@ /******************************************************************************* * the functions ******************************************************************************/ - +void uartapp_cmd66(uint8_t* data, uint16_t len); diff --git a/ASW/appTask.c b/ASW/appTask.c index 1003e28..6e151b9 100644 --- a/ASW/appTask.c +++ b/ASW/appTask.c @@ -31,12 +31,14 @@ /*--------------------------------------------------------------------------- - I N C L U D E F I L E S ----------------------------------------------------------------------------*/ +#include #include "appTask.h" #include "hardware.h" #include "stdio.h" #include "uartapp.h" #include "projcfg.h" -#include +#include "CanDrv.h" +#include "Uart2CanDiagApp.h" /*--------------------------------------------------------------------------- - D E F I N E S / M A C R O S ----------------------------------------------------------------------------*/ @@ -87,7 +89,8 @@ void apptask_init(void) timebase_event = 0; - + CanDrv_Init(); + U2CD_Init(); projcfg_init(); printf("app:apptask_init finashed\n"); ENABLE_INT(); @@ -104,7 +107,7 @@ void apptask_maintask(void) //1mstsk uartapp_maintask(); - + U2CD_TickTask(); if (timebase_counter >= 1000000) { timebase_counter = 0; @@ -114,6 +117,7 @@ void apptask_maintask(void) if (timebase_counter % 100 == 0) { //toggle_led(); + //CanDrv_Test(); } if (timebase_counter %1000 == 0) @@ -124,7 +128,7 @@ void apptask_maintask(void) } } - + U2CD_Task();//fast speed } /* [] END OF FILE */ diff --git a/ASW/uartapp.c b/ASW/uartapp.c index 3c87833..f4a4023 100644 --- a/ASW/uartapp.c +++ b/ASW/uartapp.c @@ -37,10 +37,11 @@ #include #include "ud_31.h" #include "ud_10.h" +#include "ud_66.h" /*--------------------------------------------------------------------------- - D E F I N E S / M A C R O S ----------------------------------------------------------------------------*/ - +#define RX_BUF_SIZE 5000 /*--------------------------------------------------------------------------- - T Y P E D E F I N I T I O N S ----------------------------------------------------------------------------*/ @@ -59,7 +60,7 @@ typedef enum static uartapp_mainstate_type task_state; static uint8_t rxCpltFlag = 0; -static uint8_t rxbuf[512]; +static uint8_t rxbuf[RX_BUF_SIZE]; static uint16_t rxsize = 0, timeout_count = 0; @@ -111,7 +112,7 @@ void uartapp_init(void) static void maintask_gotoidle(void) { task_state = UARTAPP_IDLE; - HAL_UARTEx_ReceiveToIdle_DMA(&huart1, rxbuf, 512); + HAL_UARTEx_ReceiveToIdle_DMA(&huart1, rxbuf, RX_BUF_SIZE); rxCpltFlag = 0; rxsize = 0; timeout_count = 0; @@ -182,6 +183,9 @@ static void uartapp_cmdpro(uint8_t* data, uint16_t len) case 0x10: uartapp_cmd10(data + 1, len - 1); break; + case 0x66: + uartapp_cmd66(data + 1, len - 1); + break; default: break; } diff --git a/CMakeLists.txt b/CMakeLists.txt index f180a96..b37357d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -75,6 +75,7 @@ target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE # Add user defined include paths ${PROJECT_SOURCE_DIR}/asw/ ${PROJECT_SOURCE_DIR}/asw/UartDiagApp/ + ${PROJECT_SOURCE_DIR}/asw/UartDiagApp/Uart2CanDiag ) # Add project symbols (macros)