/*
 * uart.c
 *
 *  Created on: 2019. 5. 27.
 *      Author: parkyj
 */

#include "uart.h"
#include "CRC16.h"
#include "MBIC_Bootloader.h"
UARTQUEUE TerminalQueue;
UARTQUEUE WifiQueue;

void InitUartQueue(pUARTQUEUE pQueue)
{
    pQueue->data = pQueue->head = pQueue->tail = 0;

    if (HAL_UART_Receive_DMA(&hTerminal, TerminalQueue.Buffer, 1) != HAL_OK)
    {
      //_Error_Handler(__FILE__, __LINE__);
    }


    //HAL_UART_Receive_DMA(&hTerminal,  TerminalQueue.Buffer, 1);
    //HAL_UART_Receive_IT(hTerminal, pQueue->Buffer + pQueue->head, 1);
}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    pUARTQUEUE pQueue;
//    printf("Function : %s : \r\n",__func__);
    UartTimerCnt = 0;
    pQueue = &TerminalQueue;
    pQueue->head++;
    if (pQueue->head >= QUEUE_BUFFER_LENGTH) pQueue->head = 0;
    pQueue->data++;
    if (pQueue->data >= QUEUE_BUFFER_LENGTH)
        GetDataFromUartQueue(huart);
    HAL_UART_Receive_DMA(&hTerminal, pQueue->Buffer + pQueue->head, 1);
   // Set_UartRcv(true);
}
void PutDataToUartQueue(UART_HandleTypeDef *huart, uint8_t data)
{
    pUARTQUEUE pQueue = &TerminalQueue;
    if (pQueue->data >= QUEUE_BUFFER_LENGTH)
        GetDataFromUartQueue(huart);
    pQueue->Buffer[pQueue->head++] = data;
    if (pQueue->head == QUEUE_BUFFER_LENGTH) pQueue->head = 0;
    pQueue->data++;
   // HAL_UART_Receive_DMA(&hTerminal,  pQueue->Buffer + pQueue->head, 10);
}

void GetDataFromUartQueue(UART_HandleTypeDef *huart)
{
    volatile static uint8_t update_data_buf[QUEUE_BUFFER_LENGTH];
    volatile static int cnt;
    uint16_t Length = 0;
    uint16_t CrcChk = 0;
    UART_HandleTypeDef *dst = (huart->Instance == USART2 ? &hTest:&hTerminal);
//    UART_HandleTypeDef *dst = &hTerminal;
    pUARTQUEUE pQueue = &TerminalQueue;
//    if (HAL_UART_Transmit(dst, pQueue->Buffer + pQueue->tail, 1, 3000) != HAL_OK)
//    {
//       _Error_Handler(__FILE__, __LINE__);
//    }
    update_data_buf[cnt++] = *(pQueue->Buffer + pQueue->tail);        

    pQueue->tail++;
    if (pQueue->tail >= QUEUE_BUFFER_LENGTH) pQueue->tail = 0;
    pQueue->data--;
    
    if(pQueue->data == 0){
        //HAL_UART_Transmit_DMA(dst, &temp_buf[BLUECELL_HEADER00], 11);
#if 0 // PYJ.2019.07.15_BEGIN -- 
            for(int i = 0; i < 128; i++){
                printf("%02x",update_data_buf[i]);
            }
#endif // PYJ.2019.07.15_END -- 
        cnt = 0;
        if(update_data_buf[0] == 0xbe){
            FirmwareUpdateStart(&update_data_buf[0]);
        }
        else if(update_data_buf[0] == MBIC_PREAMBLE0
               &&update_data_buf[1] == MBIC_PREAMBLE1
               &&update_data_buf[2] == MBIC_PREAMBLE2
               &&update_data_buf[3] == MBIC_PREAMBLE3){
                   
               if(Chksum_Check(update_data_buf,MBIC_HEADER_SIZE - 4,update_data_buf[MBIC_CHECKSHUM_INDEX])){
                Length = ((update_data_buf[MBIC_LENGTH_0] << 8) | update_data_buf[MBIC_LENGTH_1]);
                CrcChk = ((update_data_buf[MBIC_PAYLOADSTART + Length + 1] << 8) | (update_data_buf[MBIC_PAYLOADSTART + Length + 2]));
                    if(CRC16_Check(&update_data_buf[MBIC_PAYLOADSTART], Length,CrcChk)){
                       MBIC_Bootloader_FirmwareUpdate(&update_data_buf[0]);
                    }else{
                        printf("CRC ERR %x \r\n",CrcChk);
                    }
               }
               else{
                    printf("CHECK SUM ERR %x \r\n",update_data_buf[MBIC_CHECKSHUM_INDEX]);
               }
            }
        for(int i  = 0; i < QUEUE_BUFFER_LENGTH; i++)
            update_data_buf[i] = 0;
        
        FirmwareTimerCnt = 0;
       // HAL_Delay(1);
    }

}

void Uart1_Data_Send(uint8_t* data,uint8_t size){
	HAL_UART_Transmit_DMA(&huart1, data,size);
}

