#include "system_init.h"
#include "common.h"
#include "can_data_handle.h"
#include "usart_data_handle.h"
#include "main_task.h"
#include "main.h"
#include "soft_can.h"
#include "common.h"
#include "soft_flash.h"
#include "soft_can.h"
#include "soft_version.h"
#include "config.h"


/**
  * @file    thread_can_handle
  * @brief   初始化处理can线程
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
static struct rt_thread can_task;
static rt_uint8_t can_task_stack[5120];
void thread_can_handle()
{
    rt_err_t result;

    result = rt_thread_init(&can_task,
                            "cantask",
                            thread_can_task_entry,
                            RT_NULL,
                            &can_task_stack[0],
                            sizeof(can_task_stack),
                            RT_THREAD_PRIORITY_MAX - 28,
                            100);

    if (result == RT_EOK) 
        rt_thread_startup(&can_task);
}

/**
  * @file    thread_usart_handle
  * @brief   初始化处理串口线程
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
static struct rt_thread usart_task;
static rt_uint8_t usart_task_stack[5120];
void thread_usart_handle()
{
    rt_err_t result;

    result = rt_thread_init(&usart_task,
                            "usarttask",
                            thread_usart_task_entry,
                            RT_NULL,
                            &usart_task_stack[0],
                            sizeof(usart_task_stack),
                            RT_THREAD_PRIORITY_MAX - 28,
                            100);

    if (result == RT_EOK) 
        rt_thread_startup(&usart_task);   
}


/**
  * @file    thread_task_handle
  * @brief   初始化处理主任务线程
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
static struct rt_thread main_task;
static rt_uint8_t main_task_stack[5120];

void thread_task_handle()
{
    rt_err_t result;

    result = rt_thread_init(&main_task,
                            "maintask",
                            thread_main_task_handle,
                            RT_NULL,
                            &main_task_stack[0],
                            sizeof(main_task_stack),
                            RT_THREAD_PRIORITY_MAX - 27,
                            100);

    if (result == RT_EOK) 
        rt_thread_startup(&main_task);   
}

/**
  * @file    creat_sem_init
  * @brief   初始化处理主任务线程
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
struct rt_semaphore uart1_sem;
struct rt_semaphore can1_sem;
struct rt_semaphore can2_sem;
void creat_sem_init(void)
{
    rt_err_t result = 0;
    result  = rt_sem_init(&uart1_sem, "usartsem", 0, RT_IPC_FLAG_FIFO);
    if (result != RT_EOK)
    {
        //rt_kprintf("init uart sem failed.\n");
        return;
    }

    // result  = rt_sem_init(&can1_sem, "can1sem", 0, RT_IPC_FLAG_FIFO);
    // if (result != RT_EOK)
    // {
    //     //rt_kprintf("init can1 sem failed.\n");
    //     return;
    // }

    // result  = rt_sem_init(&can2_sem, "can2sem", 0, RT_IPC_FLAG_FIFO);
    // if (result != RT_EOK)
    // {
    //     //rt_kprintf("init can2 sem failed.\n");
    //     return;
    // }
}

/**
  * @file    user_init
  * @brief   用户系统初始化
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
uint8_t can1_buffer[1024];
uint8_t can2_buffer[1024];
uint8_t uart_buffer[512];
uint8_t send_uart_buffer[1024];
rkfifo_t uart_rkfifo;
rkfifo_t can1_rkfifo;
rkfifo_t can2_rkfifo;
rkfifo_t send_uart_rkfifo;
extern UART_HandleTypeDef huart1;
extern CAN_HandleTypeDef hcan1;
extern CAN_HandleTypeDef hcan2;
void user_init(void)
{   
    //串口初始化
    __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);
	HAL_UART_Receive_DMA(&huart1, (uint8_t *)FMU_uart_buf, MAX_UART_BUF);
	__HAL_UART_ENABLE_IT(&huart1, UART_IT_ERR);

    //CAN1初始化
    CAN1_filter_init();
    //HAL_NVIC_EnableIRQ(CAN1_SCE_IRQn);
//     __HAL_CAN_ENABLE_IT(&hcan1, CAN_IT_ERROR |  CAN_IT_ERROR_WARNING | CAN_IT_ERROR_PASSIVE | CAN_IT_BUSOFF | CAN_IT_LAST_ERROR_CODE);
    __HAL_CAN_ENABLE_IT(&hcan1, CAN_IT_RX_FIFO0_FULL | CAN_IT_RX_FIFO0_OVERRUN);
    HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING);
    HAL_CAN_Start(&hcan1);
    HAL_GPIO_WritePin(GPIOC, C0_CAN1_ENABLE_Pin, GPIO_PIN_RESET);

    //CAN2初始化
    CAN2_filter_init();
    
    //HAL_NVIC_EnableIRQ(CAN2_SCE_IRQn);
    // __HAL_CAN_ENABLE_IT(&hcan2, CAN_IT_ERROR | CAN_IT_ERROR_WARNING | CAN_IT_ERROR_PASSIVE | CAN_IT_BUSOFF | CAN_IT_LAST_ERROR_CODE);
    __HAL_CAN_ENABLE_IT(&hcan2, CAN_IT_RX_FIFO1_FULL | CAN_IT_RX_FIFO1_OVERRUN);
    HAL_CAN_ActivateNotification(&hcan2, CAN_IT_RX_FIFO1_MSG_PENDING);
    HAL_CAN_Start(&hcan2);
    HAL_GPIO_WritePin(GPIOC, C9_CAN2_ENABLE_Pin, GPIO_PIN_RESET);

    //PWM初始化
    // 开启PWM
    HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1); //pump
    HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2); //pump
    HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1); //nozzle
    HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2); //nozzle
    HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_1); //nozzle
    HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_2); //nozzle
    
    // 设置占空比
    __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, 1000); // 1
    __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, 1000); // 2
    __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, 1000); // 3
    __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_2, 1000); // 4
    __HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_1, 1000); // 4
    __HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_2, 1000); // 4

    //定时器4初始化
    HAL_TIM_Base_Start_IT(&htim4);
    HAL_TIM_Base_Start(&htim4);

    //rkfifo初始化
    rkfifo_init(&uart_rkfifo, uart_buffer, sizeof(uart_buffer), 1);
    //rkfifo_init(&can1_rkfifo, can1_buffer, sizeof(can1_buffer), 1);
    //rkfifo_init(&can2_rkfifo, can2_buffer, sizeof(can2_buffer), 1);
    rkfifo_init(&send_uart_rkfifo, send_uart_buffer, sizeof(send_uart_buffer), 1);

    //读flash
    flash_init();
 
    //注册PMU
    Int2String(cur_par.pmu_serial,dev_pmu.sn,9);
    Int2String(APP_VERSION,dev_pmu.soft_serial,6);
    regist_dev_info(&dev_pmu,DEVICE_PMU,false,dev_pmu.sn,9,dev_pmu.soft_serial,6,NULL,0,"vk",3);
}