/************************************************************************************
 * This file was auto-generated by CIDEA                           .                *
 * Please do not modify the contents of this file manually.                         *
 ***********************************************************************************/

#ifndef __BOOTLOADER3_H
#define __BOOTLOADER3_H

#include "adcs_cube_dtypes.h"

#ifdef __cplusplus
extern "C" {
#endif

/***************************************************************************//**
* @addtogroup CubeComponents
* @{
******************************************************************************/

/***************************************************************************//**
* @addtogroup CubeComputer4FlashBootLoader
* @brief Flash Bootloader Interface API
* @{
******************************************************************************/

/// CubeComputer4FlashBootLoader Telemetry enumeration
typedef enum {
    CopyToIntFlashProgress = 233, ///< Progress of copy to internal flash operation
    ProgramInfo = 232, ///< Program information including file size and CRC
    State = 132, ///< Status flags for bootloader
} BOOTLOADER_Telemetry_t; 

/// BootSetProgramsList enumeration
typedef enum {
    BootSetProgramsList_RunInternalFlashProgram = 1, ///< Internal Flash Program
} BOOTLOADER_BootSetProgramsList_t;

/// IntFlashCopyState enumeration
typedef enum {
    IntFlashCopyState_WaitingDma = 0, ///< Waiting for DMA
    IntFlashCopyState_WaitingMcuStatus = 1, ///< Waiting for MCU Status
} BOOTLOADER_IntFlashCopyState_t;

/// ProgramsList enumeration
typedef enum {
    ProgramsList_Bootloader = 0, ///< Bootloader
    ProgramsList_InternalFlash = 1, ///< Internal Flash Program
    ProgramsList_Eeprom = 2, ///< EEPROM
    ProgramsList_ExtFlash1 = 3, ///< External Flash Program 1
    ProgramsList_ExtFlash2 = 4, ///< External Flash Program 2
    ProgramsList_ExtFlash3 = 5, ///< External Flash Program 3
    ProgramsList_ExtFlash4 = 6, ///< External Flash Program 4
    ProgramsList_ExtFlash5 = 7, ///< External Flash Program 5
    ProgramsList_ExtFlash6 = 8, ///< External Flash Program 6
    ProgramsList_ExtFlash7 = 9, ///< External Flash Program 7
    ProgramsList_SdUser1 = 10, ///< SD User file 1
    ProgramsList_SdUser2 = 11, ///< SD User file 2
    ProgramsList_SdUser3 = 12, ///< SD User file 3
    ProgramsList_SdUser4 = 13, ///< SD User file 4
    ProgramsList_SdUser5 = 14, ///< SD User file 5
    ProgramsList_SdUser6 = 15, ///< SD User file 6
    ProgramsList_SdUser7 = 16, ///< SD User file 7
    ProgramsList_SdUser8 = 17, ///< SD User file 8
} BOOTLOADER_ProgramsList_t;

/*******************************************************************************
 *******************************   STRUCTS   ***********************************
 ******************************************************************************/

/**
 * @brief NodeDef Information Structure
 * @details Contains NodeDef-specific Information
 */
typedef struct  {
    U8 nodeTypeId;
    U8 interfaceVersion;
} BOOTLOADER_NodeDefInfo_t;

/**
 * @brief Copy Program to Internal Flash message structure
 * @details Copy Program to Internal Flash
 */
typedef struct  {
    BOOTLOADER_ProgramsList_t srcIndex;    /**< Source Program Index  */
    U8 bootloaderOverwrite;                /**< Bootloader overwrite flag. Set to 0x5A to overwrite the boot segment. USE WITH CAUTION!  */
} BOOTLOADER_CopyToInternalFlash_t;

/**
 * @brief Copy To Internal Flash Progress message structure
 * @details Progress of copy to internal flash operation
 */
typedef struct  {
    Boolean busy;     /**< Busy  */
    Boolean error;    /**< Error  */
} BOOTLOADER_CopyToIntFlashProgress_t;

/**
 * @brief Program Information message structure
 * @details Program information including file size and CRC
 */
typedef struct  {
    BOOTLOADER_ProgramsList_t programIndex;    /**< Program Index  */
    Boolean busy;                              /**< Busy reading  */
    U32 fileSize;                              /**< File Size (bytes)  */
    U16 crc16;                                 /**< CRC16 Checksum  */
} BOOTLOADER_ProgramInfo_t;

/**
 * @brief Bootloader State message structure
 * @details Status flags for bootloader
 */
typedef struct  {
    U16 runtime;                     /**< Uptime  (measurment unit is [s]) */
    Boolean sram1Enabled;            /**< SRAM1 is enabled  */
    Boolean sram2Enabled;            /**< SRAM2 is enabled  */
    Boolean sramLatchup;             /**< SRAM Latch-up Error occurred and could not be recovered  */
    Boolean sramLatchupRecovered;    /**< SRAM Latch-up Occurred but recovered after power cycle  */
    Boolean sdInitError;             /**< Flag to indicate that the SD card failed to initialise  */
    Boolean sdReadError;             /**< Flag to indicate that a read operation from the SD card failed  */
    Boolean sdWriteError;            /**< Flag to indicate that a write operation to the SD card failed  */
    Boolean extFlashError;           /**< External Flash Erase/write error occurred  */
    Boolean intFlashError;           /**< Internal Flash Erase/write error occurred  */
    Boolean eepromError;             /**< EEPROM Write error occurred  */
    Boolean bootRegCorrupt;          /**< Boot Register contained invalid data  */
    Boolean radioCommsError;         /**< Communications Error with Radio  */
} BOOTLOADER_State_t;


/*******************************************************************************
 *****************************   PROTOTYPES   **********************************
 ******************************************************************************/

/***************************************************************************//**
Return the expected length of a telemetry frame
 *
 * @param[in] tlm
 *   Enumeration of specified telemetry request
 * @return
 *   Returns the length of the telemetry frame in bytes
 ******************************************************************************/
int16_t BOOTLOADER_GetTlmLen(BOOTLOADER_Telemetry_t tlm);

/***************************************************************************//**
 * Decode a Copy To Internal Flash Progress telemetry frame from the Flash Bootloader
 *
 * @param[in] rxBuffer
 *   Buffer containing raw bytes
 * @param[in] returnVal
 *   Pointer to a BOOTLOADER_CopyToIntFlashProgress_t struct in which the telemetry will be returned.
 * @return
 *   Returns a value from the CUBEADCS_Result_t enumeration:
 *      CubeLibOk if the telemetry was decoded without error
 *      PointerIsNull if the input parameter, returnVal, was NULL
 ******************************************************************************/
CUBEADCS_Result_t BOOTLOADER_ExtractCopyToIntFlashProgressTlm(uint8_t* rxBuffer, BOOTLOADER_CopyToIntFlashProgress_t* returnVal);

/***************************************************************************//**
 * Decode a Program Information telemetry frame from the Flash Bootloader
 *
 * @param[in] rxBuffer
 *   Buffer containing raw bytes
 * @param[in] returnVal
 *   Pointer to a BOOTLOADER_ProgramInfo_t struct in which the telemetry will be returned.
 * @return
 *   Returns a value from the CUBEADCS_Result_t enumeration:
 *      CubeLibOk if the telemetry was decoded without error
 *      PointerIsNull if the input parameter, returnVal, was NULL
 ******************************************************************************/
CUBEADCS_Result_t BOOTLOADER_ExtractProgramInfoTlm(uint8_t* rxBuffer, BOOTLOADER_ProgramInfo_t* returnVal);

/***************************************************************************//**
 * Decode a Bootloader State telemetry frame from the Flash Bootloader
 *
 * @param[in] rxBuffer
 *   Buffer containing raw bytes
 * @param[in] returnVal
 *   Pointer to a BOOTLOADER_State_t struct in which the telemetry will be returned.
 * @return
 *   Returns a value from the CUBEADCS_Result_t enumeration:
 *      CubeLibOk if the telemetry was decoded without error
 *      PointerIsNull if the input parameter, returnVal, was NULL
 ******************************************************************************/
CUBEADCS_Result_t BOOTLOADER_ExtractStateTlm(uint8_t* rxBuffer, BOOTLOADER_State_t* returnVal);

/***************************************************************************//**
 * Send a Set Boot Index command to the Flash Bootloader
 *
 * @param[in] nodeid
 *   identifier of the node on the communications bus
 * @param[in] programIndex
 *   Program Index
 * @return
 *   Returns a value from the uint16_t enumeration:
 *      CubeLibOk if the command was sent without error
 *      One of the I2C enumeration values in case of I2C communications error
 *      TcInvalidParam if the input parameter, programIndex, was not a valid BOOTLOADER_BootSetProgramsList_t enumeration
 ******************************************************************************/
uint16_t BOOTLOADER_FormatBootIndexCmd(uint8_t* tcBuffer, BOOTLOADER_BootSetProgramsList_t programIndex);

/***************************************************************************//**
 * Send a Clear Error Flags command to the Flash Bootloader
 *
 * @param[in] nodeid
 *   identifier of the node on the communications bus
 * @return
 *   Returns a value from the uint16_t enumeration:
 *      CubeLibOk if the command was sent without error
 *      One of the I2C enumeration values in case of I2C communications error
 ******************************************************************************/
uint16_t BOOTLOADER_FormatClearErrorsCmd(uint8_t* tcBuffer);

/***************************************************************************//**
 * Send a Copy Program to Internal Flash command to the Flash Bootloader
 *
 * @param[in] nodeid
 *   identifier of the node on the communications bus
 * @param[in] srcIndex
 *   Source Program Index
 * @param[in] bootloaderOverwrite
 *   Bootloader overwrite flag. Set to 0x5A to overwrite the boot segment. USE WITH CAUTION!
 * @return
 *   Returns a value from the uint16_t enumeration:
 *      CubeLibOk if the command was sent without error
 *      One of the I2C enumeration values in case of I2C communications error
 *      TcInvalidParam if the input parameter, srcIndex, was not a valid BOOTLOADER_ProgramsList_t enumeration
 ******************************************************************************/
uint16_t BOOTLOADER_FormatCopyToInternalFlashCmd(uint8_t* tcBuffer, BOOTLOADER_ProgramsList_t srcIndex, U8 bootloaderOverwrite);

/***************************************************************************//**
 * Send a Read Program Information command to the Flash Bootloader
 *
 * @param[in] nodeid
 *   identifier of the node on the communications bus
 * @param[in] programIndex
 *   Program Index
 * @return
 *   Returns a value from the uint16_t enumeration:
 *      CubeLibOk if the command was sent without error
 *      One of the I2C enumeration values in case of I2C communications error
 *      TcInvalidParam if the input parameter, programIndex, was not a valid BOOTLOADER_ProgramsList_t enumeration
 ******************************************************************************/
uint16_t BOOTLOADER_FormatReadProgramInfoCmd(uint8_t* tcBuffer, BOOTLOADER_ProgramsList_t programIndex);

/***************************************************************************//**
 * Send a Run Selected Program command to the Flash Bootloader
 *
 * @param[in] nodeid
 *   identifier of the node on the communications bus
 * @return
 *   Returns a value from the uint16_t enumeration:
 *      CubeLibOk if the command was sent without error
 *      One of the I2C enumeration values in case of I2C communications error
 ******************************************************************************/
uint16_t BOOTLOADER_FormatRunSelectedProgramCmd(uint8_t* tcBuffer);


#endif  // define __CUBECOMPUTER4FLASHBOOTLOADER3_H
