/*
 * flash.h
 *
 *  Created on: Jun 23, 2023
 *      Author: true
 */

#ifndef CODE_INC_FLASH_H_
#define CODE_INC_FLASH_H_


#include "hk32f030m.h"



#define FLASH_SIZE					16384								// size of overall flash available on MCU
#define FLASH_ERASE_PAGE_SIZE   	((uint16_t)0x80)					// erase page size
#define FLASH_ERASE_PAGE_MASK		0x7f								// bitmask of page size

#define USER_APP_START_ADDR			((uint32_t)FLASH_BASE + 0x800)		// 2048 bytes (16 pages) rsvd for bootloader
#define USER_APP_END_ADDR			((uint32_t)FLASH_BASE + FLASH_SIZE)



/* Status report for the functions. */
typedef enum {
  FLASH_OK              = 0x00u, /**< The action was successful. */
  FLASH_ERROR_SIZE      = 0x01u, /**< The binary is too big. */
  FLASH_ERROR_WRITE     = 0x02u, /**< Writing failed. */
  FLASH_ERROR_VERIFY    = 0x04u, /**< Writing was successful, but the content of the memory is wrong. */
  FLASH_ERROR           = 0xFFu  /**< Generic error. */
} BL_flash_status;



extern uint32_t bootcheck;



BL_flash_status flash_erase_page(uint32_t addr);
BL_flash_status flash_write(uint32_t addr, uint8_t *data, uint16_t length);

void jump_to_user_app();



#endif /* CODE_INC_FLASH_H_ */
