Files
LFP_48V_mBMS_FW/APP_R3221/App/Source/can_comm.c

276 lines
6.8 KiB
C

/**
******************************************************************************
* @file can_comm.c
* @author AMG Energy Solution Team
* @version V1.0.0
* @date 07-Feb-2022
* @brief This file provides all the codes of can communication functions.
******************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>&copy; COPYRIGHT 2022 AMG</center></h2>
******************************************************************************
*/
/** @addtogroup mBMS2
* @{
*/
/* Includes ------------------------------------------------------------------*/
#include <includes.h>
#include "can.h"
#include "can_comm.h"
#include "can_data_process.h"
#include "can_update_process.h"
#include "can_inv_data_process.h"
#include "console.h"
#include "asutil.h"
//#include "modbus.h"
#include "malloc.h"
#include "delay.h"
#include "device_param.h"
#include "brtc.h"
#include "temp.h"
#include "date_calc.h"
/* Private define ------------------------------------------------------------*/
#define CAN_TASK_TIME 1 // 20ms TASK
/* Private typedef -----------------------------------------------------------*/
typedef struct
{
stc_can_rxframe_t *Buffer;
uint16_t Tick;
uint8_t Current_ID;
uint8_t Mode;
uint8_t TxFlag;
uint8_t TxDetail;
uint8_t RxFlag;
uint8_t MsgPrint;
uint8_t DataPrint;
uint8_t BakMsgPrint;
uint8_t BakDataPrint;
} TCanConfig, *PCanConfig;
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
TCanConfig CanConfig;
/* Private function prototypes -----------------------------------------------*/
void CanCommProcess(void);
void CanSendData(void *data, uint16_t len);
void CanPacketInit(void);
uint8_t GetCanPacket(void);
void CanRxProcess(void);
void SendPacket(uint32_t SendId, uint8_t *data);
#ifdef CONSOLE_DEBUG
void CAN_RX_DATA_PRINT(uint16_t port, stc_can_rxframe_t* buf);
void CAN_TX_DATA_PRINT(uint16_t port, stc_can_txframe_t* buf);
#endif
/* Private functions ---------------------------------------------------------*/
void CanCommInit(void)
{
can_init(250);
memset(&CanConfig, 0x00, sizeof(TCanConfig));
CanConfig.Buffer = mymalloc(sizeof(stc_can_rxframe_t));
}
/**
* @brief Initialize the Ethernet Module communication application
* @param None
* @retval None
*/
void App_TaskCanComm(void)
{
CanCommInit();
while (DEF_TRUE)
{
CanCommProcess();
delay_os_ms(CAN_TASK_TIME);
}
}
void CanCommProcess(void)
{
/* Drain a bounded batch, then yield so protection tasks remain runnable. */
for (uint32_t i = 0; i < 32U; i++) {
if (GetCanPacket() != true) {
break;
}
CanRxProcess();
}
}
void CanRxProcess(void)
{
#ifdef CONSOLE_DEBUG
if ((CanConfig.MsgPrint == true)&&(screenmode == DIAG_MODE))
CAN_RX_DATA_PRINT(1, CanConfig.Buffer);
#endif
switch (CheckCanPacket(CanConfig.Buffer)) {
case 1: // F/W Update Packet
FwUpdateProcess(CanConfig.Buffer);
break;
case 2: // Normal Comm. Packet
CanRxDataProcess(CanConfig.Buffer);
break;
case 3: // Inventory Data Packet
CanRxInvDataProcess(CanConfig.Buffer);
break;
}
}
void SendPacket(uint32_t SendId, uint8_t *data)
{
stc_can_txframe_t sendobj;
sendobj.StdID = 0x0;
sendobj.ExtID = SendId;
sendobj.Control_f.RTR = 0;
sendobj.Control_f.IDE = 1;
sendobj.Control_f.DLC = 8;// data size
sendobj.enBufferSel = CanPTBSel; // (en_can_buffer_sel_t)0U;
for (int i = 0; i < 8; i++)
{
sendobj.Data[i] = data[i];
}
CAN_SendPacket(&sendobj);
#ifdef CONSOLE_DEBUG
if ((CanConfig.DataPrint == true)&&(screenmode == DIAG_MODE))
CAN_TX_DATA_PRINT(1, &sendobj);
#endif
}
uint8_t GetCanPacket(void)
{
if (CAN_GetPacket(CanConfig.Buffer) == true)
return true;
else
return false;
}
#ifdef CONSOLE_DEBUG
void CAN_RX_DATA_PRINT(uint16_t port, stc_can_rxframe_t* buf)
{
if (screenmode != DIAG_MODE) return;
printf("\r\nCAN%d RX: ID(0x%08X): ", port, buf->ExtID);
printf(" [%04d/%02d/%02d %02d:%02d:%02d %03d]\r\n",
DEVICE_TIME.year + UTC_YEAR,
DEVICE_TIME.month,
DEVICE_TIME.date,
DEVICE_TIME.hour,
DEVICE_TIME.min,
DEVICE_TIME.sec,
DEVICE_TIME.sub_sec
);
TCanHeader CanHeader;
CanHeader.LV = buf->ExtID;
printf(" - Index: %d\r\n", CanHeader.BV.Index);
printf(" - R : %d\r\n", CanHeader.BV.R);
printf(" - DP : %d\r\n", CanHeader.BV.DP);
printf(" - PF : %d\r\n", CanHeader.BV.PF);
printf(" - PS : %d\r\n", CanHeader.BV.PS);
printf(" - SA : %d\r\n", CanHeader.BV.SA);
data_print(buf->Data, 8);
}
void CAN_TX_DATA_PRINT(uint16_t port, stc_can_txframe_t* buf)
{
if (screenmode != DIAG_MODE) return;
printf("\r\nCAN%d RX: ID(0x%08X): ", port, buf->ExtID);
printf(" [%04d/%02d/%02d %02d:%02d:%02d %03d]\r\n",
DEVICE_TIME.year + UTC_YEAR,
DEVICE_TIME.month,
DEVICE_TIME.date,
DEVICE_TIME.hour,
DEVICE_TIME.min,
DEVICE_TIME.sec,
DEVICE_TIME.sub_sec
);
data_print(buf->Data, 8);
}
void DDisplay_CanComm(uint8_t p)
{
if (p ||(CanConfig.BakMsgPrint != CanConfig.MsgPrint))
{
if (CanConfig.MsgPrint) GreenOutXY ( 27, 8, (uint8_t *)"ENABLE ");
else NormalOutXY( 27, 8, (uint8_t *)"DISABLE");
CanConfig.BakMsgPrint = CanConfig.MsgPrint;
}
if (p ||(CanConfig.BakDataPrint != CanConfig.DataPrint))
{
if (CanConfig.DataPrint)GreenOutXY ( 71, 8, (uint8_t *)"ENABLE ");
else NormalOutXY( 71, 8, (uint8_t *)"DISABLE");
CanConfig.BakDataPrint = CanConfig.DataPrint;
}
}
uint8_t DCanCommCommand(uint8_t cmd)
{
uint8_t result = 1;
switch (cmd)
{
case '2':
// CAN COMM MSG MONITOR
if (CanConfig.MsgPrint == true)
{
CanConfig.MsgPrint = false;
UserMessage("[MESSAGE]: CAN MSG PRINT DISABLE");
}
else
{
CanConfig.MsgPrint = true;
UserMessage("[MESSAGE]: CAN MSG PRINT ENABLE");
}
break;
case '3':
// CAN COMM DATA MONITOR
if (CanConfig.DataPrint == true)
{
CanConfig.DataPrint = false;
UserMessage("[MESSAGE]: CAN DATA PRINT DISABLE");
}
else
{
CanConfig.DataPrint = true;
UserMessage("[MESSAGE]: CAN DATA PRINT ENABLE");
}
break;
default:
result = 0;
break;
}
return result;
}
#endif // #ifdef CONSOLE_DEBUG
/*******************(C)COPYRIGHT 2022 AMG ********************END OF FILE******/