/*
 * flash.c
 *
 *  Created on: 2019. 7. 15.
 *      Author: parkyj
 */
#include <stdio.h>
#include "flash.h"
uint8_t flashinit = 0;
uint32_t Address = FLASH_USER_START_ADDR;
volatile static uint32_t UserAddress;

typedef void (*fptr)(void);
fptr jump_to_app;
uint32_t jump_addr;
void Jump_App(void){
    __HAL_RCC_TIM6_CLK_DISABLE(); // Flash Timer Disable
    printf("boot loader start\n");               //BootLoader Jump Function Test Print
    jump_addr = *(__IO uint32_t*) (APPLICATION_ADDRESS + 4);
    jump_to_app = (fptr) jump_addr;
    
    /* init user app's sp */
    printf("jump!\n");
    __set_MSP(*(__IO uint32_t*) APPLICATION_ADDRESS);
    jump_to_app();
}
bool EraseInit = false;

void DataErase_Func(uint32_t User_Address,uint32_t size){
    static FLASH_EraseInitTypeDef EraseInitStruct;
    static uint32_t PAGEError = 0;

    HAL_FLASH_Unlock();

    EraseInitStruct.TypeErase   = FLASH_TYPEERASE_PAGES;
    EraseInitStruct.PageAddress = FLASH_USER_USE_START_ADDR;
    EraseInitStruct.NbPages     = ((FLASH_USER_END_ADDR - FLASH_USER_USE_START_ADDR) / FLASH_PAGE_SIZE) + 1;
    UserAddress = User_Address;
    printf("NbPages : %x \r\n",EraseInitStruct.NbPages );
    printf("EraseInitStruct.PageAddress : %x \r\n",EraseInitStruct.PageAddress);

    printf("Erase Start\r\n");
    if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK)
    {
      /*
        Error occurred while page erase.
        User can add here some code to deal with this error.
        PAGEError will contain the faulty page and then to know the code error on this page,
        user can call function 'HAL_FLASH_GetError()'
      */
      /* Infinite loop */
      while (1)
      {
        /* Make LED2 blink (100ms on, 2s off) to indicate error in Erase operation */
        printf("HAL_FLASHEx_Erase Error\r\n");
        HAL_Delay(2000);
      }
    }
    EraseInit = true;
    printf("Erase End\r\n");
     HAL_FLASH_Lock();

}

uint8_t FLASH_Write_Func(uint8_t* data,uint32_t size){
    //static FLASH_EraseInitTypeDef EraseInitStruct;
    //static uint32_t PAGEError = 0;
    static uint32_t DownloadIndex;
    static __IO uint32_t data32 = 0 , MemoryProgramStatus = 0;
    int dataindex = 0;
    uint32_t writedata = 0;
    uint32_t  CurrApiAddress = 0;
    uint8_t ret = 0;
    CurrApiAddress = FLASH_USER_USE_START_ADDR;

    uint8_t* Currdata = (uint8_t*)CurrApiAddress;
    printf("HAL_FLASH_Program Start\r\n");
    DownloadIndex += size;
    printf("User_Address : %x \r\n",UserAddress);


     HAL_FLASH_Unlock();
     for(int downindex = 0; downindex < size; downindex+=4)
     {
       writedata = data[downindex + 0] ;
       writedata += data[downindex + 1] << 8 ;
       writedata += data[downindex + 2] << 16;
       writedata += data[downindex + 3] << 24;      
       if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, UserAddress,writedata) == HAL_OK)
       {
         UserAddress +=  4;
       }
       else
       {
           printf("HAL_FLASH_Program Error\r\n");
           printf("Flash Failed %x \r\n",UserAddress);
       }
     }

     printf("HAL_FLASH_Program END   %x \r\n",UserAddress);
    /* Lock the Flash to disable the flash control register access (recommended
       to protect the FLASH memory against possible unwanted operation) *********/
    HAL_FLASH_Lock();
    return 0;
    /* Check if the programmed data is OK
        MemoryProgramStatus = 0: data programmed correctly
        MemoryProgramStatus != 0: number of words not programmed correctly ******/
}
    
void Flash_InitRead() // ?占쏙옙湲고븿?占쏙옙
{
    uint32_t  CurrApiAddress = 0;
    uint32_t i = 0;
    //uint8_t ret = 0;
    CurrApiAddress = FLASH_USER_USE_START_ADDR;
    
    uint8_t* Currdata = (uint8_t*)CurrApiAddress;


    printf("Flash Init \r\n");
    uint8_t* pdata;

    for(int i = 0; i < 200; i++){
        printf("%02x ",Currdata[i]);
    }
  

}


