/*
 * Bootloader.c
 *
 *  Created on: Jul 10, 2019
 *      Author: parkyj
 */
#include "Bootloader.h"
//Blueprotocol_t * UpdateFiledata;
uint16_t updatecnt = 0;
/***
 * Header Check Function
 * ***/
#define Bluecell_BootStart 0x0b


void Firmware_BootStart_Signal(){
    uint8_t bootdata[5] = {0xbe,Bluecell_BootStart,0x02,0,0xeb};
    bootdata[bluecell_crc] = STH30_CreateCrc(&bootdata[bluecell_type],bootdata[bluecell_length]);
    Uart1_Data_Send(&bootdata[bluecell_stx],bootdata[bluecell_length] + 3); 
}

void FirmwareUpdateStart(uint8_t* data){
    uint8_t ret = 0,crccheck = 0;
    uint8_t tempdata[5] = {0xbe,FirmwareUpdataAck,0x02,0,0xbe};
    crccheck = STH30_CheckCrc(&data[bluecell_type],data[bluecell_length],data[data[bluecell_length] + 1]);
    if(crccheck == NO_ERROR){
        tempdata[bluecell_type] = FirmwareUpdataAck;
        if(data[bluecell_type] == 0xDD || data[bluecell_type] == 0xEE)//Start Firmware byte
            ret = Flash_write(&data[0]);
        if(ret == 1)
            tempdata[bluecell_type] = FirmwareUpdataNak;
    }else{
        for(uint8_t i = 0; i < data[bluecell_length] + 3; i++)
            printf("%02x ",data[i]);
        printf("Check Sum error \n");
        tempdata[bluecell_type] = FirmwareUpdataNak;
    }
    tempdata[bluecell_crc] = STH30_CreateCrc(&tempdata[bluecell_type],tempdata[bluecell_length]);
    if(data[bluecell_type] != 0xEE && data[bluecell_type] != Bluecell_Reset){
        Uart1_Data_Send(&tempdata[bluecell_stx],tempdata[bluecell_length] + 3); 
    }
    if(data[bluecell_type] == 0xEE)
        printf("update Complete \n");
}

