*
* @copyright © 2010 - 2026, Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* We kindly request you to use one or more of the following phrases to refer to
* foxBMS in your hardware, software, documentation or advertising materials:
*
* - "This product uses parts of foxBMS®"
* - "This product includes parts of foxBMS®"
* - "This product is derived from foxBMS®"
*
*/
* @file sys.h
* @author foxBMS Team
* @date 2020-02-24 (date of creation)
* @updated 2026-04-20 (date of last update)
* @version v1.11.0
* @ingroup ENGINE
* @prefix SYS
*
* @brief Sys driver header
* @details TODO
*/
#ifndef FOXBMS__SYS_H_
#define FOXBMS__SYS_H_
#include "sys_cfg.h"
#include "fstd_types.h"
#include <stdint.h>
typedef enum {
SYS_FSM_STATE_DUMMY,
SYS_FSM_STATE_HAS_NEVER_RUN,
SYS_FSM_STATE_UNINITIALIZED,
SYS_FSM_STATE_INITIALIZATION,
SYS_FSM_STATE_PRE_RUNNING,
SYS_FSM_STATE_RUNNING,
SYS_FSM_STATE_ERROR,
} SYS_FSM_STATES_e;
typedef enum {
SYS_FSM_SUBSTATE_DUMMY,
SYS_FSM_SUBSTATE_ENTRY,
SYS_FSM_CHECK_DEEP_DISCHARGE,
SYS_FSM_SUBSTATE_START_INITIALIZATION_SBC,
SYS_FSM_SUBSTATE_INITIALIZE_INTERLOCK,
SYS_FSM_SUBSTATE_WAIT_INITIALIZATION_SBC,
SYS_FSM_SUBSTATE_INITIALIZATION_CAN,
SYS_FSM_SUBSTATE_INITIALIZATION_RTC,
SYS_FSM_SUBSTATE_START_UP_BIST,
SYS_FSM_SUBSTATE_SEND_BOOT_MESSAGE,
SYS_FSM_SUBSTATE_START_INITIALIZATION_BAL,
SYS_FSM_SUBSTATE_WAIT_INITIALIZATION_BAL,
SYS_FSM_SUBSTATE_WAIT_INITIALIZATION_BAL_GLOBAL_ENABLE,
SYS_FSM_SUBSTATE_START_FIRST_MEASUREMENT_CYCLE,
SYS_FSM_SUBSTATE_WAIT_FIRST_MEASUREMENT_CYCLE,
SYS_FSM_SUBSTATE_START_CURRENT_SENSOR_PRESENCE_CHECK,
SYS_FSM_SUBSTATE_WAIT_CURRENT_SENSOR_PRESENCE_CHECK,
SYS_FSM_SUBSTATE_INITIALIZATION_MISC,
SYS_FSM_SUBSTATE_INITIALIZATION_IMD,
SYS_FSM_SUBSTATE_START_INITIALIZATION_BMS,
SYS_FSM_SUBSTATE_WAIT_INITIALIZATION_BMS,
SYS_FSM_SUBSTATE_SBC_INITIALIZATION_ERROR,
SYS_FSM_SUBSTATE_BAL_INITIALIZATION_ERROR,
SYS_FSM_SUBSTATE_BAL_GLOBAL_INITIALIZATION_ERROR,
SYS_FSM_SUBSTATE_FIRST_MEAS_INITIALIZATION_ERROR,
SYS_FSM_SUBSTATE_CURRENT_SENSOR_PRESENCE_ERROR,
SYS_FSM_SUBSTATE_IMD_INITIALIZATION_ERROR,
SYS_FSM_SUBSTATE_BMS_INITIALIZATION_ERROR,
} SYS_FSM_SUBSTATES_e;
typedef enum {
SYS_STATE_INITIALIZATION_REQUEST,
SYS_STATE_ERROR_REQUEST,
SYS_STATE_NO_REQUEST,
} SYS_STATE_REQUEST_e;
typedef enum {
SYS_OK,
SYS_BUSY_OK,
SYS_REQUEST_PENDING,
SYS_ILLEGAL_REQUEST,
SYS_ALREADY_INITIALIZED,
} SYS_RETURN_TYPE_e;
typedef enum {
SYS_MULTIPLE_CALLS_NO,
SYS_MULTIPLE_CALLS_YES,
} SYS_CHECK_MULTIPLE_CALLS_e;
* This structure contains all the variables relevant for the CONT state machine.
* The user can get the current state of the CONT state machine with this variable
*/
typedef struct {
uint16_t timer;
SYS_STATE_REQUEST_e stateRequest;
SYS_FSM_STATES_e nextState;
SYS_FSM_SUBSTATES_e nextSubstate;
SYS_FSM_STATES_e currentState;
SYS_FSM_SUBSTATES_e currentSubstate;
SYS_FSM_STATES_e previousState;
SYS_FSM_SUBSTATES_e previousSubstate;
uint32_t illegalRequestsCounter;
uint16_t initializationTimeout;
uint8_t triggerEntry;
} SYS_STATE_s;
extern SYS_STATE_s sys_state;
* @brief sets the current state request of the state variable sys_state.
* @details This function is used to make a state request to the state machine,
* e.g., start voltage measurement, read result of voltage
* measurement, re-initialization. It calls #SYS_CheckStateRequest()
* to check if the request is valid. The state request is rejected if
* is not valid. The result of the check is returned immediately, so
* that the requester can act in case it made a non-valid state
* request.
* @param stateRequest state requested to set
* @return If the request was successfully set, it returns the SYS_OK, else
* the current state of requests (type #SYS_STATE_REQUEST_e)
*/
extern SYS_RETURN_TYPE_e SYS_SetStateRequest(SYS_STATE_REQUEST_e stateRequest);
* @brief tick function, call this to advance the state machine
* @details This function contains the sequence of events in the SYS state
* machine. It must be called time-triggered, every 10ms.
*/
extern STD_RETURN_TYPE_e SYS_Trigger(SYS_STATE_s *pSystemState);
* @brief getter function for the current system state
* @details This function returns the current system state of pSystemState.
*
* @return Returns the current system state
*/
extern SYS_FSM_STATES_e SYS_GetSystemState(void);
* @brief getter function for the current system substate
* @details This function returns the current system substate of pSystemState.
*
* @return Returns the current system substate
*/
extern SYS_FSM_SUBSTATES_e SYS_GetSystemSubstate(void);
#ifdef UNITY_UNIT_TEST
STD_RETURN_TYPE_e TEST_SYS_RunStateMachine(SYS_STATE_s *pSystemState);
STD_RETURN_TYPE_e TEST_SYS_CheckStateRequest(SYS_STATE_REQUEST_e stateRequest);
SYS_CHECK_MULTIPLE_CALLS_e TEST_SYS_CheckMultipleCalls(SYS_STATE_s *pSystemState);
void TEST_SYS_SetSubstate(SYS_STATE_s *pSystemState, SYS_FSM_SUBSTATES_e nextState, uint16_t idleTime);
void TEST_SYS_SetState(
SYS_STATE_s *pSysState,
SYS_FSM_STATES_e nextState,
SYS_FSM_SUBSTATES_e nextSubstate,
uint16_t idleTime);
SYS_FSM_STATES_e TEST_SYS_ProcessInitializationState(SYS_STATE_s *pSystemState);
void TEST_SYS_GeneralMacroBist(void);
#endif
#endif