/* * (c) Copyright 2022 NXP * * NXP Confidential. This software is owned or controlled by NXP and may only be used strictly * in accordance with the applicable license terms. By expressly accepting * such terms or by downloading, installing, activating and/or otherwise using * the software, you are agreeing that you have read, and that you agree to * comply with and are bound by, such license terms. If you do not agree to * be bound by the applicable license terms, then you may not retain, * install, activate or otherwise use the software. * * This file contains sample code only. It is not part of the production code deliverables. */ #ifdef __cplusplus extern "C" { #endif #define SBC_FS26_PRESENT STD_ON /*================================================================================================== * INCLUDE FILES * 1) system and project includes * 2) needed interfaces from external units * 3) internal and external interfaces from this unit ==================================================================================================*/ #include "CDD_Bcc_774a.h" #include "CDD_Bcc_774a_SL.h" #if(SBC_FS26_PRESENT == STD_ON) #include "CDD_Sbc_fs26.h" #endif #include "Mcu.h" #include "Platform.h" #include "Spi.h" #include "Gpt.h" #include "Mcl.h" #include "Port.h" #include "Icu.h" #include "CDD_Uart.h" #include "Dio.h" #include "Lpuart_Uart_Ip_Irq.h" #include "CDD_Rm.h" /* Including FreeMASTER Driver */ #include "freemaster.h" #include "freemaster_cfg.h" #include "freemaster_s32k3xx_lpuart.h" /* Including project libraries */ #include "Common.h" #include "Cmu.h" #include "Bjb.h" #include "Printf.h" /*================================================================================================== * LOCAL TYPEDEFS (STRUCTURES, UNIONS, ENUMS) ==================================================================================================*/ /*================================================================================================== * LOCAL MACROS ==================================================================================================*/ #define CMU_INIT 0x01U #define BJB_INIT 0x02U #define CMU_MEASUREMENTS 0x03U #define BJB_MEASUREMENTS 0x04U #define FREEMASTER_COM 0x05U #define LOOPBACK_RECOVERY 0x06U #define DELAY_FOR_SBC_REFRESH 10000 /*================================================================================================== * LOCAL CONSTANTS ==================================================================================================*/ /*================================================================================================== * LOCAL VARIABLES ==================================================================================================*/ /*================================================================================================== * GLOBAL CONSTANTS ==================================================================================================*/ /*================================================================================================== * GLOBAL VARIABLES ==================================================================================================*/ /***** Error monitoring *****/ /* General system */ extern HVBMS_Errors GeneralError; // General error status extern HVBMS_States BmsState; // Bms State /* CMU status */ extern uint8 ChainBrokenLocation; // Loopback feature: location of broken chain, if any extern bool isCmuChainBroken; // Loopback feature: status of the main chain extern bool isDebugModeOn; /*Is SBC in debug mode */ /*================================================================================================== * LOCAL FUNCTION PROTOTYPES ==================================================================================================*/ /*================================================================================================== * LOCAL FUNCTIONS ==================================================================================================*/ void start_core_cm7_2(void); /*================================================================================================== * GLOBAL FUNCTIONS ==================================================================================================*/ /** * @brief Main function of the example * @details Initializes the used drivers and runs an application sending data to FreeMASTER */ int main(void) { uint8 core = 0; volatile Std_ReturnType Status = E_OK; volatile uint8 StateMachineCounter = 0; uint8 phy_retryCnt = 3; volatile uint8 CmuErrorDebouncing = 5; uint8 deviceIndx; GeneralError = NO_ERROR; uint8 deviceError[BCC_DEVICE_CNT+1]; /* Initialize the Mcu driver */ Mcu_Init(NULL_PTR); /* Initialize the clock tree and apply PLL as system clock */ Mcu_InitClock(McuClockSettingConfig_0); /* Wait until the System PLL is locked */ while(MCU_PLL_LOCKED != Mcu_GetPllStatus()) {} Mcu_DistributePllClock(); Mcu_SetMode(McuModeSettingConf_0); start_core_cm7_2(); /* Initialize the Platform settings */ Platform_Init(NULL_PTR); /*Init RM*/ Rm_Init(NULL_PTR); /* Initialize the Mcl driver */ Mcl_Init(NULL_PTR); /* Initialize the Gpt driver */ Gpt_Init(NULL_PTR); /* Initialize the OsIf module */ OsIf_Init(NULL_PTR); /* Initialize all pins using the Port driver */ Port_Init(NULL_PTR); /* Initialize the Spi driver */ Spi_Init(NULL_PTR); /* Configure Spi HW unit to run in interrupt mode. This is also needed for DMA usage in Async mode */ Spi_SetAsyncMode(SPI_INTERRUPT_MODE); /* Initialize the UART driver */ Uart_Init(NULL_PTR); /* Initialize FreeMASTER */ FMSTR_SerialSetBaseAddress((FMSTR_ADDR)IP_LPUART_1_BASE); FMSTR_Init(); /* Initialize FreeMASTER variables */ FMSTR_VariablesInit_Cmu(); FMSTR_VariablesInit_Bjb(); core = Sys_GetCoreID(); //Printf("core = %d\r\n", core); /* Initialize the Icu driver */ Icu_Init(&Icu_Config_VS_0); /* Wait for 100ms for BJB to power-up (BJB is powered by the BMU, not using default use case) */ Osif_Wait(100000); #if(SBC_FS26_PRESENT == STD_ON) Status = Sbc_fs26_Init(NULL); isDebugModeOn = IsSbcInDebugMode(); /*If SBC is in debug mode do not configure SBC*/ if(isDebugModeOn == FALSE) { Status |= Sbc_fs26_InitDevice(); } if(Status != E_OK) { GeneralError = ERROR_SBC_INIT; Led_Update(GeneralError, BmsState); } else #endif { /* Initialize the Phy_665a driver */ do { Status = Phy_665a_Init(PHY_665A_SPI); if((Status != E_OK)) { phy_retryCnt--; } else { phy_retryCnt = 0; } if ((Status != E_OK) && (phy_retryCnt == 0)) { GeneralError = ERROR_PHY_INIT; Status = E_NOT_OK; /* Update LED state */ Led_Update(GeneralError, BmsState); /* Stay here do nothing */ while(true); } } while(phy_retryCnt != 0); } /* Update LED state */ if(GeneralError == NO_ERROR) { while(true) { StateMachineCounter++; Led_Update(GeneralError, BmsState); switch(StateMachineCounter) { /* CMU initialization */ case CMU_INIT: /* Initialize CMU devices */ GeneralError = Cmu_Init(); if(GeneralError != NO_ERROR) { Status = E_NOT_OK; /* Update LED state */ Led_Update(GeneralError, BmsState); /* Retry init if fails */ StateMachineCounter = 0; } break; /* BJB initialization */ case BJB_INIT: /* Initialize BJB devices */ GeneralError = Bjb_Init(); if(GeneralError != NO_ERROR) { Status = E_NOT_OK; /* Update LED state */ Led_Update(GeneralError, BmsState); /* Retry init if fails */ StateMachineCounter = 1; } break; /* CMU measurements */ case CMU_MEASUREMENTS: /*Wait 15 ms to sync with SBC_FS26*/ Osif_Wait(DELAY_FOR_SBC_REFRESH); GeneralError = Cmu_StartMeas(BCC_774A_MSR_CHAIN_SYNCHRONOUS, BCC_774A_MSR_TYPE_APPLICATION); if(GeneralError == NO_ERROR) { GeneralError = Cmu_ReadMeas(BCC_774A_MSR_CHAIN_PRIMARY, BCC_774A_MSR_TYPE_APPLICATION); if(GeneralError != NO_ERROR) { /* If CMU read error */ CmuErrorDebouncing--; Status = E_NOT_OK; /* Update LED state */ Led_Update(GeneralError, BmsState); break; } else { CmuErrorDebouncing = 5; } GeneralError = Cmu_ReadMeas(BCC_774A_MSR_CHAIN_SECONDARY, BCC_774A_MSR_TYPE_APPLICATION); if(GeneralError != NO_ERROR) { /* If CMU read error */ CmuErrorDebouncing--; Status = E_NOT_OK; /* Update LED state */ Led_Update(GeneralError, BmsState); break; } else { CmuErrorDebouncing = 5; } } else { /* If CMU write error */ CmuErrorDebouncing--; Status = E_NOT_OK; /* Update LED state */ Led_Update(GeneralError, BmsState); } break; /* BJB measurements */ case BJB_MEASUREMENTS: #if(SBC_FS26_PRESENT == STD_ON) if(isDebugModeOn == FALSE) { Status = Sbc_fs26_WdRefresh(); if(Status==E_OK) { GeneralError = ERROR_SBC_INIT; } } #endif GeneralError = Bjb_StartMeas(); if(GeneralError == NO_ERROR) { GeneralError = Bjb_ReadMeasurementsResults(BJB_CURRENT_MEASUREMENT); if(GeneralError != NO_ERROR) { /* If BJB read error */ Status = E_NOT_OK; /* Update LED state */ Led_Update(GeneralError, BmsState); break; } GeneralError = Bjb_ReadMeasurementsResults(BJB_AIN_MEASUREMENT); if(GeneralError != NO_ERROR) { /* If BJB read error */ Status = E_NOT_OK; /* Update LED state */ Led_Update(GeneralError, BmsState); break; } GeneralError = Bjb_ReadMeasurementsResults(BJB_INTERNAL_TEMP_MEASUREMENT); if(GeneralError != NO_ERROR) { /* If BJB read error */ Status = E_NOT_OK; /* Update LED state */ Led_Update(GeneralError, BmsState); break; } } else { /* If BJB write error */ Status = E_NOT_OK; /* Update LED state */ Led_Update(GeneralError, BmsState); } break; /* FreeMaster Poll state keep can comm */ case FREEMASTER_COM: FMSTR_Poll(); break; /* Error state */ case LOOPBACK_RECOVERY: /* OpenChainDetection algorithm */ /* Init deviceError */ #if(SBC_FS26_PRESENT == STD_ON) if(isDebugModeOn == FALSE) { Status = Sbc_fs26_WdRefresh(); } #endif for(deviceIndx = 0; deviceIndx <=BCC_DEVICE_CNT;deviceIndx++) { deviceError[deviceIndx] = 0; } /* Check if the chain is broken */ isCmuChainBroken = BrokenChainDetection(BCC_CHAIN_ADDR,BCC_DEVICE_CNT, &deviceError[0], &ChainBrokenLocation); if(isCmuChainBroken == true) { /* Enable RX on chain 1 */ SetResetTPLPortRx(PHY_665A_TPL_PORT_1,1,&DefaultPhyTD); /* Disable RX on chain 0 */ if(ChainBrokenLocation == 1) { SetResetTPLPortRx(PHY_665A_TPL_PORT_0,0,&DefaultPhyTD); } GeneralError = NO_ERROR; BmsState = STATE_SAFE_CHAIN_BROKEN; } else { CmuErrorDebouncing--; GeneralError = ERROR_CMU_CHAIN_NOT_BROKEN; } break; default: break; } /* In case of General Error */ if(NO_ERROR != GeneralError) { /* Only 1 error case is handled in the current example, the broken chain */ if(CmuErrorDebouncing == 4) { StateMachineCounter = 5; } else { Led_Update(GeneralError, BmsState); /*Stay here exception not handled */ while(true); } } /* Reset state machine if no error happens */ if((StateMachineCounter >= 5) && (GeneralError == NO_ERROR)) { StateMachineCounter = 2; } } } else { /*Stay here init phase exception not handled */ while(true); } return (0); } void start_core_cm7_2(void){ IP_MC_ME->PRTN0_CORE4_ADDR = (uint32)0x00800000; IP_MC_ME->PRTN0_CORE4_PCONF = MC_ME_PRTN0_CORE4_PCONF_CCE_MASK; IP_MC_ME->PRTN0_CORE4_PUPD = MC_ME_PRTN0_CORE4_PUPD_CCUPD_MASK; IP_MC_ME->CTL_KEY = 0x5AF0U; IP_MC_ME->CTL_KEY = 0xA50FU; while (IP_MC_ME->PRTN0_CORE4_PUPD == 1) { }; while ((IP_MC_ME->PRTN0_CORE4_STAT & MC_ME_PRTN0_CORE4_STAT_CCS_MASK) == 0) { }; } #ifdef __cplusplus } #endif /** @} */