chg-stn-motherboard-ti-mcu/main.c

374 lines
11 KiB
C

/* @Created by VECMOCON Technology
* @Date - 26 feb 2024
* TODO - To enable the not define function please go through the project properties --> under the arm complier --> predefine symbols
* @ add (_N) at the end of definition to not define
* @ Remove (_N) from the end of definition to define
* @ Content added by AS - MCAL function for ADC, ADC with DMA, Periodic Timer, PWM, SysTick and Input Capture
*/
#include "../utils/utils.h"
#include "../Core/Include/gpio.h"
#include "../Core/Include/ivec_mcal_adc_dma.h"
#include "../Core/Include/ivec_mcal_timer.h"
#include "../Core/Include/ivec_mcal_mcan.h"
#include "../Core/Include/ivec_mcal_spi.h"
#include "../Core/Include/ivec_mcal_i2c.h"
#include "../Core/Include/ivec_mcal_uart.h"
#include "NOR_FLASH/nor_flash.h"
#include "ti_msp_dl_config.h"
#include "string.h"
#include "ivec_cmplx_queue.h"
#include "../ivec_ECU/ivec_ecu_uart/inc/ivec_ecu_uart.h"
#include "../ivec_ECU/ivec_ecu_can/inc/ivec_ecu_can.h"
#define DMA_CHANNEL 1
#define ADC_CHANNEL 2
#define SAMPLE_TIME 500
#ifdef ADC_DMA_CONFIG /*!< ADC with DMA _PA15 */
#endif
//#ifdef ADC_CONFIG /*!<ADC without DMA _PA25 */
//#define ADC_DMA_CONFIG
//#define TIMER_CONFIG
//#define TIMER_PWM_CONFIG
uint8_t c_sum[8]={0x60,0x00,0x00,0x05,0x00};
unsigned char Checksum(unsigned char *ptr, unsigned char len)
{
unsigned char i;
unsigned char checksum = 0;
for(i=0; i<len; i++)
checksum += ptr[i];
checksum = 0xff & ~checksum;
return(checksum);
}
const uint8_t crc_table[] = {
0x00, 0x07, 0x0e, 0x09, 0x1c, 0x1b, 0x12, 0x15, 0x38, 0x3f, 0x36, 0x31,
0x24, 0x23, 0x2a, 0x2d, 0x70, 0x77, 0x7e, 0x79, 0x6c, 0x6b, 0x62, 0x65,
0x48, 0x4f, 0x46, 0x41, 0x54, 0x53, 0x5a, 0x5d, 0xe0, 0xe7, 0xee, 0xe9,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a, 0x1d, 0x14, 0x13, 0xae, 0xa9, 0xa0, 0xa7,
0xb2, 0xb5, 0xbc, 0xbb, 0x96, 0x91, 0x98, 0x9f, 0x8a, 0x8d, 0x84, 0x83,
0xde, 0xd9, 0xd0, 0xd7, 0xc2, 0xc5, 0xcc, 0xcb, 0xe6, 0xe1, 0xe8, 0xef,
0xfa, 0xfd, 0xf4, 0xf3
};
uint8_t crc8fromTable(uint8_t *data, uint8_t length)
{
uint8_t crc = 0; // Initialize 8-bit CRC
for(int ijk = 0; ijk< length; ijk++)
{
crc = crc_table[crc ^ data[ijk]];
}
return crc;
}
uint8_t crc1[]={0x10,0x3E,0x90};
uint8_t crc2[]={0x10,0x3E,0x36};
uint8_t r12crc[]={0x92,0xFD};
uint8_t crc3[]={0x10,0x3E,0x92};
uint8_t crc4[]={0x10,0x60,0x00};
uint8_t crc5[]={0x10,0x3F,0x92};
/********************** Some Global variable ****************/
uint16_t gAdcResult; /*!< Use to get the raw ADC data of PA15 for ADC_DMA and PA25 for stand alone ADC */
volatile float InputVoltage;
uint32_t timerCount;
volatile uint32_t getperiod;
volatile uint32_t flag;
uint32_t Interrupt_Status;
char ErrorStatus[50];
uint8_t RxData;
uint8_t u8TxPacket[8]={0x14};
uint8_t u8cfg_upd[8]={0x3E,0x90,0x00,0X00,0X00};//,0x00};
uint8_t u8cfg_upd2[8]={0x3F,0x00};
uint8_t u8REG12_CFG[8]={0x3E,0x36,0x00,0x92,0x00,0xFD,0x00};//,0x92,0xFD};
uint8_t u8REG12_CFG2[8]={0x3F,0x92};
uint8_t u8cfg_exit[8]={0x3E,0x92,0x00,0x00,0x00};//,0x00};
uint8_t u8cfg_exit2[8]={0x3F,0x00};
#define BUFFER_SIZE 100U
//#define SIZE 12U
//,0x01,0x02};
uint8_t u8RxData[I2C_PACKETSIZE];
uint8_t u8SPI_TxDum[SPI_PKT_SIZE]={0x9F};//,0xFF,0xFF,0x00};
uint8_t u8SPI_RxDum[SPI_PKT_SIZE];
uint8_t u8SPI_MID[SPI_PKT_SIZE];
uint8_t u8SPI_DID[SPI_PKT_SIZE];
uint8_t u8SPI_ID1[SPI_PKT_SIZE];
uint16_t u16SPI_ID2[SPI_PKT_SIZE];
//uint8_t u8txdata[SIZE]="Hello World";
//uint8_t u8rxdata;
unsigned char pucData;
//uint32_t u32rxbuffer_size=sizeof(u8rxdata);;
//uint32_t u32data_size=sizeof(u8txdata);
uint8_t u8REG12_CFG_CS[]={0x36,0x92,0xFD};
xCoreStatus_t xStatus1;
DL_MCAN_RxBufElement RxMsg;
flashStatus_t ret_UID,ret_MID;
McalUartHandle_s prvUartHandle;
static volatile int count = 0;
void UARTDataProcess()
{
PacketRetCode_t retCode = PACKET_FAIL;
uint8_t pucBuf[MAX_PACKET_LENGTH] = {0};
uint32_t ulId = 0xffffffff;
retCode= xECU_ReadCANDataOverUART(&prvUartHandle,pucBuf,&ulId);
if(retCode > -1)
{
if(ulId == 0x00)
{
vECU_InitiateUartToCanTransmit(&prvUartHandle, ulId, pucBuf, 0);
}
else{
xECU_WriteDataOverCAN(pucBuf, ulId, retCode, 0);
}
}
}
void CANDataProcess()
{
IVEC_EcuCommonErr_e retCode = commonECU_FAIL;
can_buff_t xBuff = { 0x00 };
retCode = xECU_CANGetData(&xBuff);
if(retCode == commonECU_SUCCESS)
{
vECU_InitiateUartToCanTransmit(&prvUartHandle, (uint32_t)xBuff.id, (uint8_t*)&xBuff.data[0], (uint8_t)xBuff.length);
}
}
int main(void)
{
xMCAL_McuInit();
printf("hii\n");
xMCAL_SYSCTL_INIT(HFXT,STANDBY0);
xMCAL_SYSTICK_INIT(Period_1ms);
// Initialize the timer with the desired timeout value
//xMCAL_TimerInit(TIMG0); // Initialize your timer instance (e.g., TIMG0)
xECU_UARTInit(&prvUartHandle);
xECU_CANInit(CANFD0,BAUD_500);
while(1)
{
UARTDataProcess();
CANDataProcess();
}
//
// //u8SPI_TxDum[];
// xMCAL_McuInit();
// xMCAL_SYSCTL_INIT(SYSOSC,STANDBY0);
// xMCAL_SYSTICK_INIT(Period_1ms);
//
//// //SYSCFG_DL_init();
////
////
////// xMCAL_SPI_init(SPI0,CS_1Mhz);
////// // SYSCFG_DL_MCAN0_init();
// xMCAL_MCANInit(CANFD0,BAUD_500);
//////// xMCAL_I2C_init(I2C0, BAUD_100Khz);
// xStatus1=xMCAL_uart_init(UART1,BAUD_115200);
//////// DL_UART_Main_reset(UART0);
//////// DL_UART_Main_enablePower(UART0);
//////
// DL_MCAN_TxBufElement txMsg; // Added by Akshat
//
// txMsg.id = ((uint32_t)(0x9)) << 18U;//Added by Akshat
// /* Transmit data frame. */
// txMsg.rtr = 0U;
// /* 11-bit standard identifier. */
// txMsg.xtd = 0U;
// /* ESI bit in CAN FD format depends only on error passive flag. */
// txMsg.esi = 0U;
// /* Transmitting 4 bytes. */
// txMsg.dlc = 1U;
// /* CAN FD frames transmitted with bit rate switching. */
// txMsg.brs = 0U;
// /* Frame transmitted in CAN FD format. */
// txMsg.fdf = 0U;
// /* Store Tx events. */
// txMsg.efc = 1U;
// /* Message Marker. */
// txMsg.mm = 0xAAU;
////
//#ifdef ADC_DMA_CONFIG
// xMCAL_DmaInit();
// flag = xMCAL_AdcInit(ADC1); /*!< Configure ADC with DMA */
//
// xMCAL_AdcStart(ADC1);
//#endif
//
//#ifdef TIMER_CONFIG
// xMCAL_TimerInit(TIMG0);
// xMCAL_TimerstartCounter();
//#endif
//
//
//
//#ifdef TIMER_PWM_CONFIG
// xMCAL_PwmInit(TIMG0);
// xMCAL_PwmStart();
//#endif
//
//
//
//
//#ifdef TIMER_INPUTCAPTURE_CONFIG
// xMCAL_InputCaptureInit(TIMA0);
// xMCAL_InputCaptureStart();
//#endif
//
//
// //ret_UID=flash_getUID(u8SPI_TxDum);
// //ret_MID=flash_getMID(u8SPI_MID,u8SPI_DID);
//// xStatus1 = xMCU_i2cDevReadyMcal(I2C0,0x08);
//// while(1)
//// {
//// //vMCAL_DelayTicks(500);
////
////
////
//// if(STATUS_SUCCESS==xStatus1)
//// {
//// //vMCAL_DelayTicks(500);
//// xMCU_i2cWriteMcal(I2C0 ,0x08 ,u8TxPacket, 1);
//// //vMCAL_DelayTicks( 500);
////
//// xMCU_i2cReadMcal(I2C0, 0x08, 4);
//// xMcal_I2C_getData(I2C0,u8RxData,4);
//// }
//// }
//// ret_UID=flash_getUID(u8SPI_TxDum);
// //DL_GPIO_setPins(GPIOA,DL_GPIO_PIN_13);
// //xMCAL_uart_transmit(UART1,u8txdata,u32data_size);
//// while(1)
//// {
////// //xStatus1=xMCAL_SPI_TxData(SPI0, u8SPI_TxDum, u8SPI_RxDum,21);
////// //ret_UID=flash_getUID(u8SPI_RxDum);
//////// xMCU_i2cWriteMcal(I2C0 ,0x08 ,u8TxPacket, 1);
//////// xMCU_i2cReadMcal(I2C0, 0x08, 4);
//////// xMcal_I2C_getData(I2C0,u8RxData,4);
////// // vMCAL_DelayTicks(100);
////// //ret_MID=flash_getMID(u8SPI_MID,u8SPI_DID);
////// //DL_UART_Main_disableInterrupt(UART3,DL_UART_MAIN_INTERRUPT_RX);
//// xMCAL_uart_transmit(UART1,u8txdata,u32data_size);
////// //DL_UART_Main_enableInterrupt(UART3,DL_UART_MAIN_INTERRUPT_RX);
//// delay(10000000);
//////
//// //xMCAL_uart_getdata(UART1,u8rxdata,u32rxbuffer_size);
////// //vMCAL_DelayTicks(1000);
//// }
// //}
// //}
// /*u8cfg_upd[2]=crc8fromTable(crc1,3); //LDO 5V Enable begins
// u8REG12_CFG[2]=crc8fromTable(crc2,3);
// u8REG12_CFG[4]=crc8fromTable(&r12crc[0],1);
// u8REG12_CFG[6]=crc8fromTable(&r12crc[1],1);
// u8cfg_exit[2]=crc8fromTable(crc3,3);
//
// xMCU_i2cWriteMcal(I2C0 ,0x08 ,u8cfg_upd, 5);//write set_CFG_UPD
//
// vMCAL_DelayTicks(2);//wait for CFG_UPD mode
//
// xMCU_i2cWriteMcal(I2C0,0x08,u8REG12_CFG,7);//write REG12_CFG
//
// vMCAL_DelayTicks(2);
//
// c_sum[1]=Checksum(u8REG12_CFG_CS, 3);//crc8fromTable(crc4,2);
// crc4[2]=c_sum[1];
// c_sum[2]=crc8fromTable(crc4,3);
// c_sum[4]=crc8fromTable(&c_sum[3],1);
//
// vMCAL_DelayTicks(1);
//
// xMCU_i2cWriteMcal(I2C0,0x08,c_sum,5);
//
// vMCAL_DelayTicks(1);
//
// xMCU_i2cWriteMcal(I2C0,0x08,u8cfg_exit,5);//write CFG_exit
//
// vMCAL_DelayTicks(1);//wait for CFG_exit*/ //LDO 5V Enable Ends
////
//
// while(1)
// {
//// xMCAL_getMCAN_ErrorStatus(ErrorStatus);
//// xMCAL_getMCAN_InterruptLine1Status(&Interrupt_Status);
// xMCAL_MCANTx(CANFD0, &txMsg ,&idx, 0, 1);
// vMCAL_DelayTicks(500);
// xMCAL_uart_transmit(UART1,u8txdata,u32data_size);
// delay(1000000);
// xMCAL_MCANRx(CANFD0,&RxMsg ,0, &RxData,1);
////
// vMCAL_DelayTicks(500);
////
//// ret_UID=flash_getUID(u8SPI_TxDum);
//// ret_MID=flash_getMID(u8SPI_MID,u8SPI_DID);
////
//// vMCAL_DelayTicks(500);
////
//// xMCU_i2cWriteMcal(I2C0 ,0x08 ,u8TxPacket, 1);
//// xMCU_i2cReadMcal(I2C0, 0x08, 4);
//// xMcal_I2C_getData(I2C0,u8RxData,4);
////
//// vMCAL_DelayTicks(500);
//// vMCAL_DelayTicks(400);
////
//// if(RxData[0]!=0)
//// break;
//// __asm("nop");
////
////
////#ifdef TIMER_CONFIG
//// xMCAL_TimergetCount(&timerCount);
////#endif
////
////#ifdef ADC_DMA_CONFIG
//// xMCAL_DmaStart(DMA_CHANNEL,&gAdcResult);
//// InputVoltage = (gAdcResult) * (3.3/4096);
////#endif
//
//}
}
//void vPacketRun(void)
//{
// PacketRetCode_t retCode = PACKET_FAIL;
// uint8_t pucBuf[MAX_PACKET_LENGTH] = {0};
// uint32_t ulId = 0xffffffff;
// retCode = xGetPkt(&xHandle, pucBuf, &ulId);
//}