/**************************************************************************************************/ /** * @file : main.c * @brief : Can example source file * - Platform: Z20K14xM * - Autosar Version: 4.6.0 * @version : 1.2.0 * @author : Zhixin Semiconductor * * @note : This example contains sample code for customer evaluation purpose only. It is not * part of the production code deliverables. The example code is only tested under defined * environment with related context of the whole example project. Therefore, it is not guaranteed * that the example always works under user environment due to the diversity of hardware and * software environment. Do not use pieces copy of the example code without prior and separate * verification and validation in user environment. * * @copyright : Copyright (c) 2021-2023 Zhixin Semiconductor Ltd. All rights reserved. **************************************************************************************************/ /** @addtogroup Can * @brief Can Example * @{ */ #ifdef __cplusplus extern "C" { #endif #include "Mcu.h" #include "Port.h" #include "Can.h" #include "Gpt.h" #include "Platform.h" #include "SchM_Can.h" #include "CDD_Dma.h" #include "Wdg.h" #define CAN_START_SEC_VAR_INIT_BOOLEAN #include "Can_MemMap.h" static boolean Ex_Can_TxFlag = FALSE; static boolean Ex_Can_RxFlagCase2 = FALSE; static boolean Ex_Can_RxFlagCase3 = FALSE; static boolean Ex_Can_FlagCase4 = FALSE; #define CAN_STOP_SEC_VAR_INIT_BOOLEAN #include "Can_MemMap.h" #define CAN_START_SEC_VAR_INIT_8 #include "Can_MemMap.h" static uint8 Ex_Can_PeriodTime = 0; #define CAN_STOP_SEC_VAR_INIT_8 #include "Can_MemMap.h" #define CAN_START_SEC_CODE #include "Can_MemMap.h" /** * @brief This function used to check Rx message data for use case 2. */ static void Ex_Can_CheckDmaRxResult(const Can_HwType *HwObj, const PduInfoType *PduInfoPtr); /** * @brief This function used to check Rx message data. */ static void Ex_Can_CheckPollingRxResult(const Can_HwType *HwObj, const PduInfoType *PduInfoPtr); #define CAN_STOP_SEC_CODE #include "Can_MemMap.h" #define CAN_START_SEC_CODE #include "Can_MemMap.h" /** * @brief CanIf stub function. */ extern void CanIf_ControllerBusOff(uint8 ControllerId) { (void)ControllerId; } /** * @brief CanIf stub function. */ void CanIf_ControllerModeIndication(uint8 ControllerId, Can_ControllerStateType ControllerMode) { (void)ControllerId; (void)ControllerMode; } /** * @brief CanIf stub function. */ void CanIf_TxConfirmation(PduIdType CanTxPduId) { Ex_Can_TxFlag = TRUE; (void)CanTxPduId; (void)Ex_Can_TxFlag; } /** * @brief CanIf stub function. */ void CanIf_RxIndication(const Can_HwType *HwObj, const PduInfoType *PduInfoPtr) { Ex_Can_CheckDmaRxResult(HwObj, PduInfoPtr); Ex_Can_CheckPollingRxResult(HwObj, PduInfoPtr); } /** * @brief Stim callback function. */ void Ex_Can_StimCallback(void) { static uint32 interruptCount = 0U; /* interrupt */ if (++interruptCount == (CAN_MAINFUNCTION_MODE_PERIOD * 1000)) { /* period task flag */ interruptCount = 0U; Ex_Can_PeriodTime = 1; } } /** * @brief This function used to check Rx message data for use case 2. */ static void Ex_Can_CheckDmaRxResult(const Can_HwType *HwObj, const PduInfoType *PduInfoPtr) { if ((0x734U == HwObj->CanId) && (CanHardwareObject_0 == HwObj->Hoh) && (CanController_0 == HwObj->ControllerId)) { if ((8U == PduInfoPtr->SduLength) && (0x11 == PduInfoPtr->SduDataPtr[0U]) && (0x22 == PduInfoPtr->SduDataPtr[1U]) && (0x33 == PduInfoPtr->SduDataPtr[2U]) && (0x44 == PduInfoPtr->SduDataPtr[3U]) && (0x55 == PduInfoPtr->SduDataPtr[4U]) && (0x66 == PduInfoPtr->SduDataPtr[5U]) && (0x77 == PduInfoPtr->SduDataPtr[6U]) && (0x88 == PduInfoPtr->SduDataPtr[7U])) { Ex_Can_RxFlagCase2 = TRUE; } } } /** * @brief This function used to check Rx message data. */ static void Ex_Can_CheckPollingRxResult(const Can_HwType *HwObj, const PduInfoType *PduInfoPtr) { if ((0x80001314 == HwObj->CanId) && (CanHardwareObject_1 == HwObj->Hoh) && (CanController_0 == HwObj->ControllerId)) { if ((8U == PduInfoPtr->SduLength) && (0x01 == PduInfoPtr->SduDataPtr[0U]) && (0x02 == PduInfoPtr->SduDataPtr[1U]) && (0x03 == PduInfoPtr->SduDataPtr[2U]) && (0x04 == PduInfoPtr->SduDataPtr[3U]) && (0x05 == PduInfoPtr->SduDataPtr[4U]) && (0x06 == PduInfoPtr->SduDataPtr[5U]) && (0x07 == PduInfoPtr->SduDataPtr[6U]) && (0x08 == PduInfoPtr->SduDataPtr[7U])) { Ex_Can_RxFlagCase3 = TRUE; } } } void BusFault_Handler(void) { if(E_OK == Can_ClearInjectError(0)) { Ex_Can_FlagCase4 = TRUE; } } /** * @brief This use case will send CAN standard frame by polling mode via CAN0. * */ static void Ex_Can_UseCase_01(void) { Can_PduType PduInfo; uint8 SendData[] = {0x12, 0x34, 0x56, 0x78, 0x87, 0x65, 0x43, 0x21}; uint8 Cnt = 0U; uint32 Index; PduInfo.id = 0x733; PduInfo.sdu = SendData; PduInfo.swPduHandle = 0; PduInfo.length = 8; Can_SetControllerMode(CanController_0, CAN_CS_STARTED); Can_EnableControllerInterrupts(CanController_0); Gpt_StartTimer(0, 3000); while (1U) { Wdg_Service(); if (1 == Ex_Can_PeriodTime) { ++Cnt; if (Cnt == 5U) { break; } Can_Write(CanHardwareObject_2, &PduInfo); /* Delay to ensure send out data */ for (Index = 0U; Index < 10000U; ++Index) { } Can_MainFunction_Write(); Ex_Can_PeriodTime = 0; } } } /** * @brief This use case will receive CAN standard frame with FIFO DMA mode by interrupt mode via * CAN0. * */ static void Ex_Can_UseCase_02(void) { Can_SetControllerMode(CanController_0, CAN_CS_STARTED); Can_EnableControllerInterrupts(CanController_0); while (1U) { Wdg_Service(); if (TRUE == Ex_Can_RxFlagCase2) { break; } } } /** * @brief This use case will receive CAN extended frame by polling mode via CAN0. * */ static void Ex_Can_UseCase_03(void) { Can_SetControllerMode(CanController_0, CAN_CS_STARTED); Can_EnableControllerInterrupts(CanController_0); while (1) { Wdg_Service(); Can_MainFunction_Read(); if (TRUE == Ex_Can_RxFlagCase3) { break; } } } /** * @brief This use case will show ecc injection . * */ static void Ex_Can_UseCase_04(void) { Can_SetControllerMode(CanController_0, CAN_CS_STARTED); Can_DisableControllerInterrupts(CanController_0); Can_InjectError(CanController_0); while (1) { Wdg_Service(); if (TRUE == Ex_Can_FlagCase4) { break; } } } /** * @brief This use case will send CAN extended frame by interrupt mode via CAN0. * */ static void Ex_Can_UseCase_05(void) { Can_PduType PduInfo; uint8 SendData[] = {0x87, 0x65, 0x43, 0x21, 0x12, 0x34, 0x56, 0x78}; PduInfo.id = 0x80001122; PduInfo.sdu = SendData; PduInfo.swPduHandle = 0; PduInfo.length = 8; Can_SetControllerMode(CanController_0, CAN_CS_STARTED); Can_EnableControllerInterrupts(CanController_0); Gpt_StopTimer(0); Gpt_StartTimer(0, 10000); while (1) { Wdg_Service(); if (1 == Ex_Can_PeriodTime) { if(E_OK != Can_Write(CanHardwareObject_3, &PduInfo)) { /* user could add code to process */ } Ex_Can_PeriodTime = 0; } } } int main(void) { McalLib_Init(); #if (MCU_PRECOMPILE_SUPPORT == STD_ON) Mcu_Init(NULL_PTR); #else Mcu_Init(&Mcu_Config); #endif /* (MCU_PRECOMPILE_SUPPORT == STD_ON) */ Mcu_InitClock(0); #if (CAN_PRECOMPILE_SUPPORT == STD_ON) Can_Init(NULL_PTR); #else Can_Init(&Can_Config); #endif /* (CAN_PRECOMPILE_SUPPORT == STD_ON) */ #if (WDG_PRECOMPILE_SUPPORT == STD_ON) Wdg_Init(NULL_PTR); #else Wdg_Init(&Wdg_Config); #endif /* (WDG_PRECOMPILE_SUPPORT == STD_ON) */ #if (PORT_PRECOMPILE_SUPPORT == STD_ON) Port_Init(NULL_PTR); #else Port_Init(&Port_Config); #endif /* (PORT_PRECOMPILE_SUPPORT == STD_ON) */ #if (GPT_PRECOMPILE_SUPPORT == STD_ON) Gpt_Init(NULL_PTR); #else Gpt_Init(&Gpt_Config); #endif /* (GPT_PRECOMPILE_SUPPORT == STD_ON) */ Gpt_EnableNotification(0); #if (GPT_PRECOMPILE_SUPPORT == STD_ON) Dma_Init(NULL_PTR); #else Dma_Init(&Dma_Config); #endif /* (GPT_PRECOMPILE_SUPPORT == STD_ON) */ Dma_EnableChannelHwRequest(0U, TRUE); Platform_Init(NULL_PTR); Ex_Can_UseCase_01(); Ex_Can_UseCase_02(); Ex_Can_UseCase_03(); Ex_Can_UseCase_04(); Ex_Can_UseCase_05(); while (1U) { Wdg_Service(); } } #define CAN_STOP_SEC_CODE #include "Can_MemMap.h" #ifdef __cplusplus } #endif /** @} end of group Can_Example */