/*
 * H_Common_STM32F4_FLASH.h
 *
 *  Created on: 2024/11/18
 *      Author: egaono-electronics
 */

#ifndef H_COMMON_STM32F4_FLASH_H_
#define H_COMMON_STM32F4_FLASH_H_

#include "Dev_Conf.h"

#include def_Header_HAL



/* Base address of the Flash sectors */
#define def_ADDR_FLASH_SECTOR_0     ((uint32_t)0x08000000)
                            /* Base @ of Sector 0, 16 Kbytes */
#define def_ADDR_FLASH_SECTOR_1     ((uint32_t)0x08004000)
                            /* Base @ of Sector 1, 16 Kbytes */
#define def_ADDR_FLASH_SECTOR_2     ((uint32_t)0x08008000)
                            /* Base @ of Sector 2, 16 Kbytes */
#define def_ADDR_FLASH_SECTOR_3     ((uint32_t)0x0800C000)
                            /* Base @ of Sector 3, 16 Kbytes */
#define def_ADDR_FLASH_SECTOR_4     ((uint32_t)0x08010000)
                            /* Base @ of Sector 4, 64 Kbytes */
#define def_ADDR_FLASH_SECTOR_5     ((uint32_t)0x08020000)
                            /* Base @ of Sector 5, 128 Kbytes */
#define def_ADDR_FLASH_SECTOR_6     ((uint32_t)0x08040000)
                            /* Base @ of Sector 6, 128 Kbytes */
#define def_ADDR_FLASH_SECTOR_7     ((uint32_t)0x08060000)
                            /* Base @ of Sector 7, 128 Kbytes */
#define def_ADDR_FLASH_SECTOR_8     ((uint32_t)0x08080000)
                            /* Base @ of Sector 8, 128 Kbytes */
#define def_ADDR_FLASH_SECTOR_9     ((uint32_t)0x080A0000)
                            /* Base @ of Sector 9, 128 Kbytes */
#define def_ADDR_FLASH_SECTOR_10    ((uint32_t)0x080C0000)
                            /* Base @ of Sector 10, 128 Kbytes */
#define def_ADDR_FLASH_SECTOR_11    ((uint32_t)0x080E0000)
                            /* Base @ of Sector 11, 128 Kbytes */

#ifndef def_USE_FLASH_1M
#define def_ADDR_FLASH_END  ((uint32_t)0x0807FFFF)
#else
#define def_ADDR_FLASH_END  ((uint32_t)0x080FFFFF)
#endif  // def_USE_FLASH_1M




//----------------------------------------------------------
// 指定Addressから属するsectorを割り出す。
//----------------------------------------------------------
// 引数;
// uint32_t uint32_Address : 指定Address


// 戻り値; 指定Addressが属するsector
//----------------------------------------------------------
uint32_t GetSector(uint32_t uint32_Address);



//----------------------------------------------------------
// Erase FLASH
//----------------------------------------------------------
// 引数;
// uint32_t uint32_StartAddress : Erase開始Address
// uint32_t uint32_EndAddress : Erase終了Address


// 戻り値;
//    0 : OK
//    1 : NG
//----------------------------------------------------------
int16_t EraseFLASH(uint32_t uint32_StartAddress,
                   uint32_t uint32_EndAddress);



//----------------------------------------------------------
// Write Data to Flash Memory
//----------------------------------------------------------
// 引数;
// uint32_t uint32_StartAddress(4の倍数) :
//                                  書き込み開始Address
// uint32_t uint32_Length : 書き込みデータ数
// uint32_t *puint32_aData(4byte単位) :
//              書き込みデータが格納されたBufferのポインタ


// 戻り値;
//    0 : OK
//    1 : NG
//----------------------------------------------------------
int16_t WriteFLASH(uint32_t uint32_StartAddress,
                   uint32_t uint32_Length,
                   uint32_t *puint32_aData);



//----------------------------------------------------------
// Set Data and Write Flash Memory
//----------------------------------------------------------
// 引数;
// uint32_t uint32_StartAddress : 書き込み開始アドレス
// int16_t int16_Length : 書き込みbyte数
// uint8_t *puint8_aData :
//              書き込みデータが格納されたBufferのポインタ


// 戻り値;
//    0 : OK
//    1以上 : NG
//----------------------------------------------------------
int16_t SetDataAndWriteFLASH(uint32_t uint32_StartAddress,
                             int16_t int16_Length,
                             uint8_t *puint8_aData);



#endif /* H_COMMON_STM32F4_FLASH_H_ */
