/*
 * 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


uint8_t BootStartdata[6] = {0xbe,Bluecell_BootStart,0x03,4,0,0xeb};
void Firmware_BootStart_Signal(){
	BootStartdata[bluecell_crc] = STH30_CreateCrc(&BootStartdata[bluecell_type],BootStartdata[bluecell_length]);
    Uart1_Data_Send(&BootStartdata[bluecell_stx],BootStartdata[bluecell_length] + 3);
}

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

