/*
 * usart_biz.c
 *
 *  Created on: 2026-02-26
 *      Author: wangfq
 */

#include "config.h"
#include "cmcng.h"
#include <string.h>
#include "dbn_ble_srv.h"

void USART1_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void USART2_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));


void uart_init(void){
    GPIO_InitTypeDef  GPIO_InitStructure = {0};
    USART_InitTypeDef USART_InitStructure = {0};
    NVIC_InitTypeDef  NVIC_InitStructure  = {0};

    // usart1 : peripheral / DEBUG


    //usart2 :loop mcu
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;         // Tx
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;   // Rx
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    USART_InitStructure.USART_BaudRate            = 192000;//115200;
    USART_InitStructure.USART_WordLength          = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits            = USART_StopBits_1;
    USART_InitStructure.USART_Parity              = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode                = USART_Mode_Tx | USART_Mode_Rx;

    USART_Init(USART2, &USART_InitStructure);
    // USART_ITConfig(USART2, USART_IT_IDLE, ENABLE);  // 配合DMA，但测试不能接收，原因未知。
    USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);

    NVIC_InitStructure.NVIC_IRQChannel                   = USART2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    USART_Cmd(USART2, ENABLE);


    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//GPIO_Mode_IPU;   // Key
    GPIO_Init(GPIOA, &GPIO_InitStructure);

}




/*********************************************************************
 * @fn      USART1_IRQHandler
 *
 * @brief   This function handles USART1 global interrupt request.
 *
 * @return  none
 */
void USART1_IRQHandler(void)
{

}



/*********************************************************************
 * @fn      USART2_IRQHandler
 *
 * @brief   This function handles USART2 global interrupt request.
 *
 * @return  none
 */
void USART2_IRQHandler(void)
{
    if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
    {
        uint8_t _dat =  USART_ReceiveData(USART2);
        if(g_pkg_uart_2.offset == 0){
            if(_dat != 0){
                g_pkg_uart_2.pkg[g_pkg_uart_2.offset++] = _dat;
            }            
        }
        else{
            if(g_pkg_uart_2.offset < BUFF_STACK_SIZE){
                g_pkg_uart_2.pkg[g_pkg_uart_2.offset++] = _dat;
            }
            else{
                g_pkg_uart_2.flag = 1;
            }
        }
        
        if(g_pkg_uart_2.offset){
            g_pkg_uart_2.tick = 0;
        }        

    }
}


void UART2_SendString(uint8_t *buf, uint16_t len)
{
    uint16_t _len = len;
    while(_len){
        while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
        USART_SendData(USART2, *buf++);
        _len--;
    }
    while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
}

void UART1_SendString(uint8_t *buf, uint16_t len)
{
    uint16_t _len = len;
    while(_len){
        while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
        USART_SendData(USART1, *buf++);
        _len--;
    }
}


void uart_srv(void)
{
    uint8_t i;
    uint8_t _report_flag = 0;

    if(g_pkg_uart_2.flag){
        
        if(g_flag_counter_ota.flag == 0){
            if(g_pkg_uart_2.pkg[0] == 0x7F){
                if(g_pkg_uart_2.pkg[3] == 0xC0 && g_pkg_uart_2.pkg[4] == 0x0C)
                {
                    if(g_dbn_ble_state_acs_enable.flag == 0){
                        _report_flag = 1;
                    }
                    else{
                        g_pkg_uart_2.pkg[0] = 0x8F;
                    }
                }
                
                for(i = 0; i < g_pkg_uart_2.offset; i++){
                    PRINT(" %02X", g_pkg_uart_2.pkg[i]);
                }
                PRINT("\n");

                if(_report_flag){
                    InitPkgUart(&g_pkg_uart_2);
                }
            }
            else {
                PRINT("Rcv_len:%d,dat: %s\n", g_pkg_uart_2.offset, g_pkg_uart_2.pkg);
            }
        }
        else{
            // PRINT("From_Loop: ");
            // for(i = 0; i < g_pkg_uart_2.offset; i++){
            //     PRINT(" %02X", g_pkg_uart_2.pkg[i]);
            // }
            // PRINT("\n");
        }
        

        if(g_flag_bt_state){
            g_flag_notify_temp = set_response_tran_to_notify(g_pkg_uart_2.pkg, g_pkg_uart_2.offset, &g_notify_buftemp);
        }
        else{
            g_dbn_ble_state_acs_enable.flag = 0;
        }
        InitPkgUart(&g_pkg_uart_2);
    }
}

