 
#include "main.h"
#include "cmsis_os.h"
#include "usart.h"
#include "tim.h"
#include "bsp_uart.h"
#include "bsp_i2c.h"

#include "elog.h"

#include "base.h"

// #include "PH.h"
// #include "temperature.h"

#if 1

// #define   huart3_helper  ph_uart_helper
#define   huart3_helper temp_uart_helper
#define   hi2c1_helper ads1115_i2c_helper

extern osSemaphoreId_t elog_dma_lockHandle;


void USART1_IRQHandler(void)
{
    if (__HAL_UART_GET_FLAG(&huart1, UART_FLAG_TC) != RESET)
    {
        osSemaphoreRelease(elog_dma_lockHandle);
        huart1.gState = HAL_UART_STATE_READY;
        __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_TC);
    }
    HAL_UART_IRQHandler(&huart1);
}

void USART3_IRQHandler(void)
{

    if (__HAL_UART_GET_FLAG(&huart3, UART_FLAG_TC) != RESET)
    {
        log_i(" usart 3 it tc ... ");
        huart3.gState = HAL_UART_STATE_READY;
        // max3160_485_receive_mode();
        //  TODO 回调函数，带obj
        UART_HELPER_Trans_TxCplt_Callback( huart3_helper  );

        __HAL_UART_CLEAR_FLAG(&huart3, UART_FLAG_TC);
    }

    // if (__HAL_UART_GET_FLAG(&huart3, UART_FLAG_RXNE) != RESET)
    // {
    //     log_i( " usart3 it rxne....");
    //     __HAL_UART_CLEAR_FLAG(&huart3, UART_FLAG_TC);
    // }

    if (__HAL_UART_GET_FLAG( &huart3, UART_FLAG_IDLE ) != RESET){
        log_i(" usart 3 it idle ... ");
        uint16_t count = __HAL_DMA_GET_COUNTER( huart3.hdmarx );
        // //  判断是否有数据  count 为剩余未接收的字节数
        if (huart3_helper->rcv_size != count) 
        {
            UART_HELPER_RCV_IDLE_Callback( huart3_helper , (huart3_helper->rcv_size-count));
        }
        __HAL_UART_CLEAR_IDLEFLAG( &huart3 );
    }


    HAL_UART_IRQHandler(&huart3);
}

void USART6_IRQHandler(void)
{
    if (__HAL_UART_GET_FLAG(&huart6, UART_FLAG_TC) != RESET)
    {
        huart6.gState = HAL_UART_STATE_READY;
 
        log_i(" usart 6 it ... ");
        __HAL_UART_CLEAR_FLAG(&huart6, UART_FLAG_TC);
    }
    if (__HAL_UART_GET_FLAG(&huart6, UART_FLAG_RXNE) != RESET)
    {
        log_i( " huart6 it....");
        __HAL_UART_CLEAR_FLAG(&huart6, UART_FLAG_TC);
    }
    HAL_UART_IRQHandler(&huart6);
}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{

    if (huart->Instance == huart3.Instance)
    {
        log_i("....huart3 rx callback....  " );
        UART_HELPER_RCV_Cplt_Callback( huart3_helper );
        __HAL_UART_CLEAR_FLAG(&huart3, UART_FLAG_RXNE);
    }
}

// void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
// {

//   if (huart->Instance == huart3.Instance)
//   {
//     /* 拷贝数据 到菜单 */
//     log_i("huart3   idle -> ****** HAL_UARTEx_RxEventCallback ");
//     __HAL_UART_CLEAR_IDLEFLAG(&huart3);
//   }
// }

// void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
// {
//     //   if (huart->Instance == huart3.Instance)
//     //   {
//     //     // max3160_485_receive_mode();
//     //     // huart3.gState = HAL_UART_STATE_READY;
//     //     __HAL_UART_CLEAR_FLAG(&huart3, UART_FLAG_TC);
//     //   }
// }


/*******************************  I2C  CallBack  *********************************/
void I2C1_EV_IRQHandler(void)
{
    HAL_I2C_EV_IRQHandler(&hi2c1);
}
void I2C1_ER_IRQHandler(void)
{
  HAL_I2C_ER_IRQHandler(&hi2c1);
}

void HAL_I2C_MemTxCpltCallback(I2C_HandleTypeDef *hi2c)
{
    log_i( " HAL_I2C_Mem  TxCpltCallback  ------------------- ");
    if (hi2c  == &hi2c1)
    {
       log_i( " HAL_I2C_Mem  TxCpltCallback  --- hi2c1 ---------------- ");
        I2C_HELPER_Trans_TxCplt_Callback(hi2c1_helper);
    }
}   

void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c)
{
    log_i( " HAL_I2C_Mem  RxCpltCallback  ------------------- ");
    {
       log_i( " HAL_I2C_Mem  RxCpltCallback  --- hi2c1 ---------------- ");
        I2C_HELPER_Trans_TxCplt_Callback(hi2c1_helper);
    }
}

void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c)
{
    log_i( " i2c master send cplt  ------------------- ");
}
 
void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c)
{
    log_i( " i2c master rx cplt  ------------------- ");
}
 
void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
{
    log_i( " HAL_I2C_ErrorCallback  ------------------- ");
}
#endif
 

void TIM1_UP_TIM10_IRQHandler(void)
{
  /* USER CODE BEGIN TIM1_UP_TIM10_IRQn 0 */

  /* USER CODE END TIM1_UP_TIM10_IRQn 0 */
  HAL_TIM_IRQHandler(&htim10);
  /* USER CODE BEGIN TIM1_UP_TIM10_IRQn 1 */

  /* USER CODE END TIM1_UP_TIM10_IRQn 1 */
}