#include <cr_section_macros.h>

#include <stdio.h>

#define IAP_LOCATION 0x1FFF1FF1
#define IAP_CMD_SUCCESS 0
#define IAP_INVALID_COMMAND 1
#define IAP_SRC_ADDR_ERROR 2
#define IAP_DST_ADDR_ERROR 3
#define IAP_SRC_ADDR_NOT_MAPPED 4
#define IAP_DST_ADDR_NOT_MAPPED 5
#define IAP_COUNT_ERROR 6
#define IAP_INVALID_SECTOR 7
#define IAP_SECTOR_NOT_BLANK 8
#define IAP_SECTOR_NOT_PREPARED_FOR_WRITE_OPERATION 9
#define IAP_COMPARE_ERROR 10
#define IAP_BUSY 11
#define IAP_CLOCK_100M 100000


typedef enum _error_code {
    ok, general_error, invalid_command, global_checksum,
    local_checksum, erase_error, write_error, invalid_size,
    invalid_offset, invalid_arguments
} error_code;

typedef void (*IAP)(unsigned int[], unsigned int[]);


void iap_prepare_sectors(error_code* status, int start, int end);

void iap_erase_sectors(error_code* status, int start, int end);

void iap_copy_to_flash(error_code* status, int dst_flash, uint32_t* src_ram, int num_bytes);

uint32_t iap_read_part_id(error_code* status);

void iap_read_serial(error_code* status, uint32_t* res);

