Files

221 lines
5.1 KiB
C

/**
******************************************************************************
* File Name : can_comm.c
* Description : This file provides code for the configuration
* of CAN communication.
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
* SLA0044, the "License"; You may not use this file except in compliance with
* the License. You may obtain a copy of the License at:
* www.st.com/SLA0044
*
******************************************************************************
*/
/* 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"
/* Private define ------------------------------------------------------------*/
#define CAN_TASK_TIME 1 // 20ms TASK
#define CAN_RX_TIMEOUT 500 // 500ms TimeOut
#define CAN_RX_DELAY 5
#define CAN_TX_DELAY 50
/* Private typedef -----------------------------------------------------------*/
typedef struct
{
CanRxMsg *Buffer;
u16 Tick;
u8 Current_ID;
u8 Mode;
u8 TxFlag;
u8 TxDetail;
u8 RxFlag;
u8 MsgPrint;
u8 DataPrint;
u8 BakMsgPrint;
u8 BakDataPrint;
} TCanConfig, *PCanConfig;
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
TCanConfig CanConfig;
/* Private function prototypes -----------------------------------------------*/
void CanCommProcess(void);
void CanSendData(void *data, u16 len);
void CanPacketInit(void);
u8 GetCanPacket(void);
void CanRxProcess(void);
void SendPacket(u32 SendId, u8 *data);
/* Private functions ---------------------------------------------------------*/
void CanCommInit(void)
{
can1_init(250000);
memset(&CanConfig, 0x00, sizeof(TCanConfig));
CanConfig.Buffer = mymalloc(sizeof(CanRxMsg));
}
/**
* @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() != SUCCESS) {
break;
}
CanRxProcess();
}
}
void CanRxProcess(void)
{
if ((CanConfig.MsgPrint == true)&&(screenmode == DIAG_MODE))
CAN_DATA_PRINT(1, CanConfig.Buffer, RX_PRINT);
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(u32 SendId, u8 *data)
{
CanTxMsg sendobj;
sendobj.StdId = 0x0;
sendobj.ExtId = SendId;
sendobj.RTR = CAN_RTR_DATA;
sendobj.IDE = CAN_ID_EXT;
sendobj.DLC = 8;// data size
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
}
u8 GetCanPacket(void)
{
if (CAN_GetPacket(CanConfig.Buffer) == SUCCESS)
return SUCCESS;
else
return ERROR;
}
#ifdef CONSOLE_DEBUG
void DDisplay_CanComm(u8 p)
{
if (p ||(CanConfig.BakMsgPrint != CanConfig.MsgPrint))
{
if (CanConfig.MsgPrint) GreenOutXY ( 27, 8, (u8 *)"ENABLE ");
else NormalOutXY( 27, 8, (u8 *)"DISABLE");
CanConfig.BakMsgPrint = CanConfig.MsgPrint;
}
if (p ||(CanConfig.BakDataPrint != CanConfig.DataPrint))
{
if (CanConfig.DataPrint)GreenOutXY ( 71, 8, (u8 *)"ENABLE ");
else NormalOutXY( 71, 8, (u8 *)"DISABLE");
CanConfig.BakDataPrint = CanConfig.DataPrint;
}
}
u8 DCanCommCommand(u8 cmd)
{
u8 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 2015 STMicroelectronics *****END OF FILE****/