#include "platform.h"
#include "Z20K11xM.h"
#include "Z20K11xM_drv.h"
#include "Z20K11xM_sysctrl.h"
#include "Z20K11xM_clock.h"
#include "Z20K11xM_wdog.h"
#include "Z20K11xM_srmc.h"
#include "Z20K11xM_stim.h"
#include "Z20K11xM_gpio.h"
#include "Z20K11xM_can.h"
#include "Z20K11xM_uart.h"
#include "Z20k11xM_dma.h"
#include "wdog_reg.h"
#include "Z20K11xM_flash.h"
#include "can_msg_def.h"
#include "crc8.h"
#include "boot_bsp.h"
#include "includes.h"
#define WATCHDOG_ENABLE /*看门狗开关*/
#define WATCHDOG_TIMEOUT 3//unit second
/* 全局定义,确定烧写硬件为实际左灯 / 右灯 */
static uint8_t s_board_type = BOARD_TYPE_LEFT;
/******Jump to APP delay time when have not received 301 message*******/
#define BOOT_WAIT_TIMEOUT_MS (20u)
static bool is_rec_rlc_bootcmd_msg = false;
static void bsp_set_board_type(void);
/*RCL区分左右板*/
uint8_t bsp_get_board_type(void)
{
return s_board_type;
}
/*app的总空间大小40KB-256KB*/
const BlockInfo_t gs_astBlockNumA[] = {
{0x0000A000u, 0x00040000u}, /*App A information is in the block.*/
};
static void boot_version_number_init(void);
void check_appRun(void)
{
/*第二次进入跳转之后要判断完全升级成功之后在进行跳转*/
if((CheckStayInBootFlag() != SET))
{
uint8_t head_info[4]={0};
/*版本号的初始化*/
boot_version_number_init();
FlashReadWithPadding(APP_INFO_ADDR, head_info , 4);
if(*(uint32_t *)head_info == APP_INFO_HEAD)
{
SysDeinit();
JumpToApp();
}
}else
{
ClearStayInBootFlag();
}
}
static bsp_can_event_handle s_can0_handle = NULL;
static bsp_can_send_task s_can_send_task = NULL;
static CAN_MsgBuf_t s_can0_temp;
static volatile uint32_t s_lpit_tick = 0;
void can0_handle(uint32_t mbIdx)
{
if (mbIdx == CAN1MSG_BUFFIDX_RX_RLC_BootCmd_ID)
{
is_rec_rlc_bootcmd_msg = true;
}
if (s_can0_handle)
{
if ( mbIdx == CAN1MSG_BUFFIDX_TX_SubNode_FlowCtrl_ID || mbIdx == CAN1MSG_BUFFIDX_TX_HLLFeedback_ID || mbIdx == CAN1MSG_BUFFIDX_TX_HLRFeedback_ID)
{
/*发送完成中断*/
s_can0_handle(BSP_CAN_EVENT_TX_COMPLETE, mbIdx, \
s_can0_temp.msgId, \
s_can0_temp.data, \
s_can0_temp.dataLen);
}
else
{
/*read rx buffer*/
CAN_GetMsgBuff(CAN_ID_0, mbIdx, &s_can0_temp);
s_can0_handle(BSP_CAN_EVENT_RX_COMPLETE, mbIdx, \
s_can0_temp.msgId, \
s_can0_temp.data, \
s_can0_temp.dataLen);
}
}
}
static void STIMTEST_IntCallBack(void)
{
//1ms
s_lpit_tick++;
// if(s_can_send_task)
// s_can_send_task();
}
/*喂狗操作*/
void bsp_watchdog_feed(void)
{
#ifdef WATCHDOG_ENABLE
WDOG_Refresh();
#endif
}
uint32_t bsp_get_tick(void)
{
return s_lpit_tick;
}
static void board_init(void)
{
/* disable wdog */
//SYSCTRL_EnableModule(SYSCTRL_WDOG);
//WDOG_Disable();
/* enable osc clock */
CLK_OSC40MEnable2(CLK_OSC_FREQ_MODE_HIGH, ENABLE, CLK_OSC_XTAL);
/* choose osc clock as system clock*/
CLK_SysClkSrc(CLK_SYS_OSC40M);
CLK_SetClkDivider(CLK_CORE, CLK_DIV_1); //40M
CLK_SetClkDivider(CLK_BUS, CLK_DIV_1); //40M
CLK_SetClkDivider(CLK_SLOW, CLK_DIV_5); //8M
}
static void gpio_init(void)
{
SYSCTRL_EnableModule(SYSCTRL_PORTA);
SYSCTRL_EnableModule(SYSCTRL_PORTB);
SYSCTRL_EnableModule(SYSCTRL_PORTC);
SYSCTRL_EnableModule(SYSCTRL_PORTD);
SYSCTRL_EnableModule(SYSCTRL_PORTE);
SYSCTRL_EnableModule(SYSCTRL_GPIO);
CLK_ModuleSrc(CLK_PORTA, CLK_SRC_OSC40M);
CLK_ModuleSrc(CLK_PORTB, CLK_SRC_OSC40M);
CLK_ModuleSrc(CLK_PORTC, CLK_SRC_OSC40M);
CLK_ModuleSrc(CLK_PORTD, CLK_SRC_OSC40M);
CLK_ModuleSrc(CLK_PORTE, CLK_SRC_OSC40M);
/*EN0*/
PORT_PinmuxConfig(PORT_C, GPIO_17, PTC17_GPIO);
GPIO_SetPinDir(PORT_C, GPIO_17, GPIO_OUTPUT);
GPIO_SetPinOutput(PORT_C, GPIO_17);
/*SL/SEN*/
PORT_PinmuxConfig(PORT_D, GPIO_4, PTD4_GPIO);
GPIO_SetPinDir(PORT_D, GPIO_4, GPIO_OUTPUT);
GPIO_ClearPinOutput(PORT_D, GPIO_4);
/*TBL/SEN*/
PORT_PinmuxConfig(PORT_B, GPIO_12, PTB12_GPIO);
GPIO_SetPinDir(PORT_B, GPIO_12, GPIO_OUTPUT);
GPIO_ClearPinOutput(PORT_B, GPIO_12);
/*PL/SEN*/
PORT_PinmuxConfig(PORT_D, GPIO_3, PTD3_GPIO);
GPIO_SetPinDir(PORT_D, GPIO_3, GPIO_OUTPUT);
GPIO_ClearPinOutput(PORT_D, GPIO_3);
/*区分左右板的gpio*/
PORT_PinmuxConfig(PORT_A, GPIO_1, PTA1_GPIO);
GPIO_SetPinDir(PORT_A, GPIO_1, GPIO_INPUT);
/*DC/EN1*/
PORT_PinmuxConfig(PORT_A, GPIO_6, PTC6_GPIO);
GPIO_SetPinDir(PORT_A, GPIO_6, GPIO_OUTPUT);
GPIO_ClearPinsOutput(PORT_A, GPIO_6);
/*DC/EN2*/
PORT_PinmuxConfig(PORT_A, GPIO_7, PTC7_GPIO);
GPIO_SetPinDir(PORT_A, GPIO_7, GPIO_OUTPUT);
GPIO_ClearPinsOutput(PORT_A, GPIO_7);
}
/* STIM configuration */
const STIM_Config_t stimConfig =
{
.workMode = STIM_FREE_COUNT,
.compareValue = 40000, /*counter clock is 40M, compare value =40000, period = 1ms*/
.countResetMode = STIM_INCREASE_FROM_0,
.clockSource = STIM_FUNCTION_CLOCK,
};
static void stim_init(void)
{
/* Configure STIM function clock*/
CLK_ModuleSrc(CLK_STIM, CLK_SRC_OSC40M);
/* Enable STIM module */
SYSCTRL_EnableModule(SYSCTRL_STIM);
/* Enable STIM NVIC IRQ*/
NVIC_EnableIRQ(STIM_IRQn);
/* Init STIM_0*/
STIM_Init(STIM_0, &stimConfig);
/* Install interrupt callback function */
STIM_InstallCallBackFunc(STIM_0, STIM_INT,STIMTEST_IntCallBack);
/* Enable STIM_0 interrupt*/
STIM_IntCmd(STIM_0, ENABLE);
/*Enable STIM*/
STIM_Enable(STIM_0);
}
CAN_MessageInfo_t rx_info =
{
.idType = CAN_MSG_ID_STD, /*msg type: standard frame*/
.dataLen = 8, /*max date length*/
.remoteFlag = RESET, /*isn't remote frame */
.fdEn = DISABLE, /*FD enabled*/
.fdPadding = 0, /*Use zeros for FD padding*/
.brsEn = ENABLE /*Bit rate switch enabled*/
};
CAN_MessageInfo_t tx_info =
{
.idType = CAN_MSG_ID_STD,
.dataLen = 8,
.remoteFlag = RESET,
.fdEn = DISABLE,
.fdPadding = 0x00U,
.brsEn = DISABLE
};
static void can0_init(void)
{
static CAN_Config_t can_cfg =
{
.mbMaxNum = 14,
.rxFifoEn = DISABLE,
.mode = CAN_MODE_NORMAL,
.fdEn = DISABLE,
.bitTiming =
{
.propSeg = 4,
.phaseSeg1 = 3,
.phaseSeg2 = 2,
.preDivider = 4,
.rJumpwidth = 1
},
.payload0 = CAN_PAYLOAD_SIZE_8,
.payload1 = CAN_PAYLOAD_SIZE_8,
.bitTimingFdData =
{
.propSeg = 4,
.phaseSeg1 = 3,
.phaseSeg2 = 2,
.preDivider = 1,
.rJumpwidth = 1
},
};
/* Clock init */
// CLK_OSC40MEnable(40, ENABLE, CLK_OSC_XTAL);
/* Disable wdog */
// WDOG_Disable();
/* Set module clock */
CLK_ModuleSrc(CLK_CAN0, CLK_SRC_OSC40M);
CLK_SetClkDivider(CLK_CAN0, CLK_DIV_2);
// CLK_ModuleSrc(CLK_PORTB, CLK_SRC_OSC40M);
// CLK_ModuleSrc(CLK_PORTE, CLK_SRC_OSC40M);
// CLK_ModuleSrc(CLK_PORTC, CLK_SRC_OSC40M);
/* Enable module */
SYSCTRL_ResetModule(SYSCTRL_CAN0);
SYSCTRL_EnableModule(SYSCTRL_CAN0);
/* Set pin mux */
PORT_PinmuxConfig(PORT_C, G