FR8000串口接收例程



  • 例如:
    #define UART_LEN_MAX 512
    #define TIMEOUT_RX_MS 5

    volatile uint8_t uart_recving_buffer[UART_LEN_MAX] = {0};
    volatile uint16_t timer_rx_flag = 0,index_uart0 = 0;

    volatile uint32_t add_up=0;
    初始化串口
    {
    system_set_port_pull(GPIO_PD6,GPIO_PULL_UP,true);
    /* set PA2 and PA3 for AT command interface */
    system_set_port_mux(GPIO_PORT_D, GPIO_BIT_6, PORTD6_FUNC_UART1_RXD);
    system_set_port_mux(GPIO_PORT_D, GPIO_BIT_7, PORTD7_FUNC_UART1_TXD);
    Uart1_handle.UARTx = Uart1;
    Uart1_handle.Init.BaudRate = gAT_buff_env.uart_param.BaudRate;
    Uart1_handle.Init.DataLength = gAT_buff_env.uart_param.DataLength;
    Uart1_handle.Init.StopBits = gAT_buff_env.uart_param.StopBits;
    Uart1_handle.Init.Parity = gAT_buff_env.uart_param.Parity;
    Uart1_handle.Init.FIFO_Mode = UART_FIFO_ENABLE;

    uart_init_ex(&Uart1_handle);
    

    NVIC_EnableIRQ(UART1_IRQn);
    NVIC_SetPriority(UART1_IRQn, 4);
    /enable recv and line status interrupt/
    uart1_isr_int(&Uart1_handle);

    co_printf("enter at init\r\n");
    gAT_env.at_task_id = os_task_create( at_task_func );
    
     __SYSTEM_TIMER_CLK_ENABLE();
    

    pmu_set_pin_mux(GPIO_PORT_A, GPIO_BIT_6, PMU_PIN_FUNC_GPIO);
    pmu_set_pin_to_PMU(GPIO_PORT_A, CO_BIT(6));
    pmu_set_pin_dir(GPIO_PORT_A, BIT(6),GPIO_DIR_OUT);

    NVIC_ClearPendingIRQ(TIMER0_IRQn);
    NVIC_EnableIRQ(TIMER0_IRQn);
    
    NVIC_SetPriority(TIMER0_IRQn, 2);
    /* timer0 1s */
    timer_init(Timer0, 96000, TIMER_DIV_NONE);
    
    timer_start(Timer0);
    

    }

    串口中断
    {
    attribute((section("ram_code"))) void uart1_isr(void)
    {
    uint32_t isr_id;
    uint8_t rx_char;
    if(gap_get_connect_num()==1)
    {
    volatile struct_UART_t * const uart_reg_ram = (volatile struct_UART_t *)UART1_BASE;
    isr_id = uart_reg_ram->FCR_IID.IID;
    if(((isr_id & 0x04) == 0x04) || ((isr_id & 0x0c) == 0x0c)) //receciver data available or character timeout indication
    {
    while(uart_reg_ram->LSR.LSR_BIT.DR)
    {
    rx_char = uart_reg_ram->DATA_DLL.DATA;
    uart_recving_buffer[index_uart0] = rx_char; //uart_reg_ram->DATA_DLL.DATA;
    index_uart0++;
    timer_rx_flag=1;
    add_up = 1;
    pmu_set_gpio_value(GPIO_PORT_A, BIT(6),1);

    		}
    	}
    	else if((isr_id & 0x06) == 0x06)//receiver line status interrupt
    	{
    		uint32_t tmp = uart_reg_ram->LSR.LSR_DWORD;
    		uart_reg_ram->FCR_IID.FCR = isr_id;
    		uart_reg_ram->IER_DLH.IER.ELSI = 0;
    	}
    }
    else
    {
    	void uart1_isr_rom(void);
    	uart1_isr_rom();
    }
    

    }
    }
    接收超中断
    {
    attribute((section("ram_code"))) void timer0_isr(void)
    {

    	static uint32_t lu32_Count = 0;
    	
    	timer_int_clear(Timer0)
    	
    	   if(timer_rx_flag >= 1)
    	   {
    			timer_rx_flag++;
    			
    			if(timer_rx_flag >= TIMEOUT_RX_MS)
    			{
    				pmu_set_gpio_value(GPIO_PORT_A, BIT(6),0);
    				at_spss_send_data(gAT_ctrl_env.transparent_conidx, uart_recving_buffer, index_uart0);
    				timer_rx_flag = 0;
    				index_uart0 = 0;
    			}
    	   
    		}
    

    }



  • void uart1_isr_int(UART_HandleTypeDef *huart)
    {
    huart->UARTx->IER_DLH.IER.ELSI = 1;
    huart->UARTx->IER_DLH.IER.ERBFI = 1;
    }