123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- /*
- * flash.c
- *
- * Created on: 2019. 7. 15.
- * Author: parkyj
- */
- #include "flash.h"
- #include "MBIC_Bootloader.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(); // 留ㅼ씤???占쏙옙癒몌옙?? ?占쏙옙占�??占쏙옙?占쏙옙?占쏙옙
- printf("boot loader start\n"); //硫붿꽭占�? 異쒕젰
- 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();
- }
- void Flash_InitRead(void) // ?占쏙옙湲고븿?占쏙옙
- {
- uint32_t Address = 0;
- Address = FLASH_USER_BANK1_START_ADDR;
- for(uint32_t i = 0; i < 300; i++ ){
- printf("%08x : %02X \n",Address ,*(uint8_t*)Address);
- Address++;
- }
- #if 0 // PYJ.2019.03.27_BEGIN --
- for(uint32_t i = 0; i < 13848; i++ ){
- printf("%08x : %02X \n",Address ,*(uint8_t*)Address);
- Address++;
- }
- Address = StartAddr;
- for(uint32_t i = 0; i < 13848; i++ ){
- printf("%02X ",*(uint8_t*)Address);
- Address++;
- }
- #endif // PYJ.2019.03.27_END --
- }
- uint8_t Flash_Data_Write(uint8_t* data){
- uint16_t Firmdata = 0;
- uint8_t ret = 0;
- int i = 0;
- static uint8_t Errcnt = 0;
- static uint32_t Curr_Download_DataIndex = 0;
- static uint32_t Prev_Download_DataIndex = 0;
-
- Curr_Download_DataIndex = data[MBIC_PAYLOADSTART + 4] << 24;
- Curr_Download_DataIndex += data[MBIC_PAYLOADSTART + 5] << 16;
- Curr_Download_DataIndex += data[MBIC_PAYLOADSTART + 6] << 8;
- Curr_Download_DataIndex += data[MBIC_PAYLOADSTART + 7];
- // data[MBIC_PAYLOADSTART + 12 +i];
-
- returntoFirst:
- for(i= 0; i <= Curr_Download_DataIndex - Prev_Download_DataIndex; i+=2){
- Firmdata = ((data[MBIC_PAYLOADSTART + 12 +i]) & 0x00FF);
- Firmdata += ((data[MBIC_PAYLOADSTART + 12 +i + 1] << 8) & 0xFF00);
- if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,UserAddress , (uint16_t)Firmdata) != HAL_OK)
- {
- printf("HAL NOT OK \n");
- ret = 1;
- HAL_Delay(1000);
- if(Errcnt == 0xFF)
- return;
-
- Errcnt++;
- goto returntoFirst;
- }else{
- Errcnt = 0; //Err Cnt Init;
- UserAddress += 2;
- }
- }
- Prev_Download_DataIndex = Curr_Download_DataIndex + 1;
- return ret;
- }
- uint8_t API_Flash_Data_Write(uint8_t* data){
- uint16_t Firmdata = 0;
- uint8_t ret = 0;
- int i = 0;
- static uint8_t Errcnt = 0;
- static uint32_t Curr_Download_DataIndex = 0;
- static uint32_t Prev_Download_DataIndex = 0;
-
- Curr_Download_DataIndex = data[MBIC_PAYLOADSTART + 4] << 24;
- Curr_Download_DataIndex += data[MBIC_PAYLOADSTART + 5] << 16;
- Curr_Download_DataIndex += data[MBIC_PAYLOADSTART + 6] << 8;
- Curr_Download_DataIndex += data[MBIC_PAYLOADSTART + 7];
- // data[MBIC_PAYLOADSTART + 12 +i];
-
- returntoFirst:
- for(i= 0; i <= Curr_Download_DataIndex - Prev_Download_DataIndex; i+=2){
- Firmdata = ((data[MBIC_PAYLOADSTART + 12 +i]) & 0x00FF);
- Firmdata += ((data[MBIC_PAYLOADSTART + 12 +i + 1] << 8) & 0xFF00);
- if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,UserAddress , (uint16_t)Firmdata) != HAL_OK)
- {
- printf("HAL NOT OK \n");
- ret = 1;
- HAL_Delay(1000);
- if(Errcnt == 0xFF)
- return;
-
- Errcnt++;
- goto returntoFirst;
- }else{
- Errcnt = 0; //Err Cnt Init;
- UserAddress += 2;
- }
- }
- Prev_Download_DataIndex = Curr_Download_DataIndex + 1;
- return ret;
- }
- uint8_t Bank_Flash_write(uint8_t* data,uint32_t StartBankAddress) // ?占쏙옙湲고븿?占쏙옙
- {
- /*Variable used for Erase procedure*/
- static FLASH_EraseInitTypeDef EraseInitStruct;
- static uint32_t PAGEError = 0;
- uint8_t ret = 0;
- HAL_FLASH_Unlock(); // lock ??占�?
- if(flashinit == 0){
-
- flashinit= 1;
- /* Fill EraseInit structure*/
- switch(StartBankAddress){
- case FLASH_USER_START_ADDR:
- EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
- EraseInitStruct.PageAddress = FLASH_USER_START_ADDR - 128;
- EraseInitStruct.NbPages = (FLASH_USER_END_ADDR - FLASH_USER_START_ADDR) / FLASH_PAGE_SIZE;
- break;
- case FLASH_USER_BANK1_START_ADDR:
- EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
- EraseInitStruct.PageAddress = FLASH_USER_BANK1_START_ADDR - 128;
- EraseInitStruct.NbPages = (FLASH_USER_END_ADDR - FLASH_USER_BANK1_START_ADDR) / FLASH_PAGE_SIZE;
- break;
- case FLASH_USER_BANK2_START_ADDR:
- EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
- EraseInitStruct.PageAddress = FLASH_USER_BANK2_START_ADDR - 128;
- EraseInitStruct.NbPages = (FLASH_USER_END_ADDR - FLASH_USER_BANK2_START_ADDR) / FLASH_PAGE_SIZE;
- break;
- }
-
- UserAddress = EraseInitStruct.PageAddress;
- //FLASH_PageErase(StartAddr);
- if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK){
- printf("Erase Failed \r\n");
- }
- }
- ret = Flash_Data_Write(&data[MBIC_PREAMBLE_0]);
- HAL_FLASH_Lock(); // lock ?占쏙옙洹멸린
- return ret;
- }
- #define DATA_32 ((uint32_t)0x33333333)
- __IO bool EraseInit = false;
- void MBIC_APIFLASH_test(uint8_t* data,uint32_t size){
- static FLASH_EraseInitTypeDef EraseInitStruct;
- static uint32_t Address = 0, PAGEError = 0;
-
- static __IO uint32_t data32 = 0 , MemoryProgramStatus = 0;
- HAL_FLASH_Unlock();
-
- /* Erase the user Flash area
- (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
-
- /* Fill EraseInit structure*/
- if(EraseInit == false){
- EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
- EraseInitStruct.PageAddress = FLASH_USER_BANK1_START_ADDR;
- EraseInitStruct.NbPages = ((FLASH_USER_BANK1_START_ADDR + ((uint32_t)0x000FFFF)) - FLASH_USER_BANK1_START_ADDR) / FLASH_PAGE_SIZE;
- 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");
- }
- /* Program the user Flash area word by word
- (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
-
- Address = FLASH_USER_BANK1_START_ADDR;
- printf("HAL_FLASH_Program Start\r\n");
- while (Address < ((FLASH_USER_BANK1_START_ADDR + 12)))
- {
- if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, Address, DATA_32) == HAL_OK)
- {
- Address = Address + 4;
- }
- else
- {
- /* Error occurred while writing data in Flash memory.
- User can add here some code to deal with this error */
- while (1)
- {
- /* Make LED2 blink (100ms on, 2s off) to indicate error in Write operation */
- /* Make LED2 blink (100ms on, 2s off) to indicate error in Erase operation */
- printf("HAL_FLASH_Program Error\r\n");
- }
- }
- }
- printf("HAL_FLASH_Program END\r\n");
- /* Lock the Flash to disable the flash control register access (recommended
- to protect the FLASH memory against possible unwanted operation) *********/
- HAL_FLASH_Lock();
-
- /* Check if the programmed data is OK
- MemoryProgramStatus = 0: data programmed correctly
- MemoryProgramStatus != 0: number of words not programmed correctly ******/
- Address = FLASH_USER_BANK1_START_ADDR;
- MemoryProgramStatus = 0x0;
-
- // while (Address < ((FLASH_USER_BANK1_START_ADDR + 12)))
- // {
- // data32 = *(__IO uint32_t *)Address;
- // printf("%08x : %02X \n",Address ,*(uint8_t*)Address);
- // if (data32 != DATA_32)
- // {
- // MemoryProgramStatus++;
- // }
- // Address = Address + 4;
- // }
- //
- // /*Check if there is an issue to program data*/
- // if (MemoryProgramStatus == 0)
- // {
- // printf("DATA OK \r\n");
- // /* No error detected. Switch on LED2*/
- // }
- // else
- // {
- // /* Error detected. LED2 will blink with 1s period */
- // while (1)
- // {
- // printf("Read Error\r\n");
- // }
- // }
- }
|