/*
 * eps_fw_update.h
 *
 *  Created on: Aug 11, 2023
 *      Author: Invcuser_75
 */

#ifndef EXO_SERVICES_EPS_CTRLR_INC_EPS_BTLR_DRIVER_H_
#define EXO_SERVICES_EPS_CTRLR_INC_EPS_BTLR_DRIVER_H_





#include "file_transfer.h"
#include "notify_apps.h"
#include "exo_common.h"
#include "eps_config.h"

#define BOOT0_PIN		          GPIO_PIN_2
#define MCU_RESET_PIN	          GPIO_PIN_10
#define BOOT_PORT                 GPIOC
#define NRST_PORT                 GPIOF
#define SLAVE_ADDRESS             0x72
#define ACK_BYTE                  0x79
#define NACK_BYTE                  0x1F
#define FOUR_BYTE_ALLIGNMENT      4
#define PAGE_READ_SIZE            256
#define PAGE_WRITE_SIZE           256
#define EPS_I2C_BUS_TIMOUT_DELAY  500
#define ACK_READ_FRAME_SIZE       1
#define COMMAND_FRAME_SIZE        2
#define ADDRESS_FRAME_SIZE        5
#define SECTOR_CNT_FRAME_SIZE     3
#define SECTOR_ID_FRAME_SIZE      3

/*
 * @brief Get command enumeration
 */
typedef enum
{
	GET_COMMAND     = 0x00,
	GET_VERSION     = 0x01,
	GET_ID          = 0x02,
	WRITE_COMMAND   = 0x31,
	ERASE_COMMAND   = 0x44,
	GO_COMMAND      = 0X21,
	READ_COMMAND    = 0x11

} eps_get_command;


/*
 * @brief error status of boot
 */
typedef enum
{
	BOOT_OK =0,
	BOOT_CMD_REJECTED,
	BOOT_COMMAND_NACK,
	BOOT_DATA_NACK,
	BOOT_COMMAND_READ_FAIL,
	BOOT_IMPROPER_COMMAND,
	BOOT_WRITE_NACK,
	BOOT_DATA_TRANSMIT_FAIL,
	BOOT_ADDRESS_NACK,
	BOOT_ADDRESS_TRANSMIT_FAIL,
	BOOT_READ_FAIL,
	BOOT_READ_NACK,
	BOOT_STARTUP_FAIL,
	BOOT_SECTOR_CNT_NACK,
	BOOT_SECTOR_CNT_FAIL,
	BOOT_SECTOR_ID_NACK,
	BOOT_SECTOR_ID_FAIL,
	BOOT_WRITE_ADDRESS_NOT_ALIGNED,
	BOOT_READ_ADDRESS_NOT_ALIGNED,
	BOOT_ERROR,
	BOOT_READ_WRITE_MISMATCH,
	BOOT_SECTOR_CNT_INVLD_ACK,
	BOOT_I2C_COMM_FAIL,
	BOOT_SECTOR_ID_INVALID_ACK,
	BOOT_WRITE_INVLD_ACK,
	BOOT_READ_INVLD_ACK

} eps_boot_error;


/* BOOT MODE GET COMMAD API
 * This API is used to get information from the slave MCU after making it to enter bootmode and
 * sending following get commands.
 * 1.Get command - This command can be sent to get commands for which the MCU will support and respond.
 * 2.Get ID      - This command can be sent to get product ID of the MCU.
 * 3.Get version - This command can be sent to get the version details.
 */


uint8_t eps_boot_get_command(uint8_t command, uint8_t *rx_buff, uint8_t *read_bytes_count);

/* BOOT STARTUP API
 * This API is used to make the MCU enter into bootmode and check whether it enters into bootmode
 * by sending GET_ID command. This API performs following functions
 * 1.Boot is set high.
 * 2.MCU reset done.
 * 3.Check by sending GET_ID command.
 */

uint8_t eps_boot_startup(void);
#if 0
/* MCU RESET API
 * This API is used to reset the slave my keeping NRST pin low for 50ms and releasing
 * it to high. Initially by default it will in high position
 */

void eps_mcu_reset(void);
#endif
/*BOOT PRINT API
 * This API is used to print the buffer received while sending commands in boot mode
 */
void eps_boot_rx_buf_print(uint8_t *rx_buf_ptr, uint8_t rx_buf_len);

/*BOOT SEND COMMAND API
 * This API is used to MCU jumps to the memory location specified in the address
 */

uint8_t boot_send_command(uint8_t boot_command);

/*BOOT SEND ADDRESS COMMAND API
 * This API is used to send address location to MCU
 */

uint8_t boot_send_address(uint32_t address);
#if 0
/*BOOT GO COMMAND API
 * This API is used to MCU jumps to the memory location specified in the address
 */
uint8_t eps_btlr_run_firmware(uint32_t go_address);
#endif
/*BOOTMODE ERASE API
 * This API is used to erase memory sectors in boot mode
*/
uint8_t eps_btlr_sector_erase(uint8_t sector_id);

/*BOOTMODE WRITE FILE API
 * This API is used to write files in bootmode
*/
uint8_t eps_btlr_program_page_write(uint32_t memory_address, uint8_t *write_data, uint32_t write_length);

/*BOOTMODE READ FILE API
 * This API is used to read files in bootmode
*/
uint8_t eps_btlr_page_read(uint32_t memory_addres, uint8_t *read_data, uint32_t read_length);
/* BOOT ERASE PAGE API
 * This API is used to erase sectors specified from the start address till the size mentioned in the firmware size
 */
uint8_t eps_btlr_page_erase(uint32_t erase_start_address, uint32_t firmware_size);
/*
 * @brief This API update the firmware in boot mode
 */
uint8_t eps_firmware_update(uint32_t source_address, uint32_t destination_address, uint32_t firmware_size, uint32_t fw_crc);
/*
 * @brief This API load the firmware in flash memory.
 * Readback the data from flash memory for CRC calculation
 */
uint8_t eps_firmware_loading(uint32_t source_address, uint32_t destination_address, uint32_t firmware_size, uint32_t fw_crc);
#if 0
uint8_t eps_run_firmware(uint32_t destination_address);
#endif
uint8_t crc( const uint8_t *input_str, uint32_t num_bytes );
/*
 *  Application API which will be invoked during file transmission by FT framework
 *  it handles all FTM related notification published by FTM module
 */
void eps_ft_receiver_notificataion_clbk(ft_notification_info *ft_sts);
/*
 *  Application API called during eps_init to read firmware availablity, checksum and size.
 *
 */
void eps_fw_upt_init(void);
/*
 *  Application API to write firmware CRC, size and status of converter board in NVM
 *
 */
void eps_fw_conv_info_nvm_write(uint8_t fr_status, uint32_t fr_size, uint32_t fr_crc);
/*
 *  Application API to write firmware CRC, size and status of battery board in NVM
 *
 */
void eps_fw_op_chnl_info_nvm_write(uint8_t fr_status, uint32_t fr_size, uint32_t fr_crc );
void eps_pwr_dwn_all_chnl();
void eps_check_chnl_pwr_dwn_state();
void eps_pwr_up_chnl_after_fw_upd();
void eps_fw_mcu2_upd_check(obc_pck_data_eps *eps_fw);
void eps_fw_mcu1_upd_check();
void eps_bckup_fw_conv_info_nvm_write(uint8_t fr_status, uint32_t fr_size, uint32_t fr_crc);
void eps_bckupfw_op_chnl_info_nvm_write(uint8_t fr_status, uint32_t fr_size, uint32_t fr_crc);
#endif /* EXO_SERVICES_EPS_CTRLR_INC_EPS_BTLR_DRIVER_H_ */
