nrf52832自动唤醒功能的串口

1.0实现的接口

#include 
#include 
#include "stdint.h"
#include "app_uart.h"
#include "simple_uart.h"
#include "nrf_uarte.h"
#include "app_timer.h"
#include "nrf_drv_gpiote.h"
#include "nrf_delay.h"

/*

Function introduction
Serial port has the function of Automatic wake-up 

receive data : other device send any data--->other device wait 2ms for device wake up --->other device send data
send data: direct send data

*/

void uart_init(uint32_t io_tx,uint32_t io_rx,uint8_t baud_select);/*Power on initialization primary serial port*/
void active_uart(void);/*Call once when receiving a frame of serial port data to prevent sleep*/
void task_uart_power_manage(bool en_sleep,uint16_t sec_to_sleep);/*Put it into the main loop. If it is enabled to sleep, it will shut down the serial port in sec_to_sleep) seconds without serial port activity*/
void uart_send(uint8_t *str,uint16_t len);
uint32_t app_uart_get(uint8_t * p_byte);



#endif
#include "simple_uart.h"

static uint8_t uart_en_flg=0;
static uint32_t tx_pin,rx_pin,baud;

static void uart_error_handle(app_uart_evt_t * p_event)
{
    if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR)
    {
//        APP_ERROR_HANDLER(p_event->data.error_communication);
    }
    else if (p_event->evt_type == APP_UART_FIFO_ERROR)
    {
//        APP_ERROR_HANDLER(p_event->data.error_code);
	}
}

		


void uart_init(uint32_t io_tx,uint32_t io_rx,uint8_t baud_select)
{
	uint32_t err_code;
	app_uart_comm_params_t comm_params;
	#define UART_RX_BUF_SIZE   128
	#define UART_TX_BUF_SIZE   128

	uint32_t bb[]={NRF_UART_BAUDRATE_115200,\
	NRF_UART_BAUDRATE_57600,\
	NRF_UART_BAUDRATE_38400,\
	NRF_UART_BAUDRATE_19200,\
	NRF_UART_BAUDRATE_9600
	};

	comm_params.flow_control=APP_UART_FLOW_CONTROL_DISABLED;
	comm_params.use_parity=false;
	comm_params.baud_rate=bb[baud_select%5];
	comm_params.rx_pin_no=io_tx;
	comm_params.tx_pin_no=io_rx;

	tx_pin=io_tx;
	rx_pin=io_rx;
	baud=baud_select;
	
	APP_UART_FIFO_INIT(&comm_params,
	UART_RX_BUF_SIZE,
	UART_TX_BUF_SIZE,
	uart_error_handle,
	APP_IRQ_PRIORITY_LOWEST,
	err_code);
    APP_ERROR_CHECK(err_code);
	uart_en_flg=1;
}



void uint_simple_uart(void)
{
	app_uart_close();
	uart_en_flg=0;
}





void uart_send(uint8_t *str,uint16_t len)
{
	uint16_t i,counter;
	active_uart();
	for(i=0;i=sec_to_sleep))
        {
            uint_simple_uart();
            enable_pin_irq(rx_pin,true);
            ticktime=NRF_RTC1->COUNTER;
			 nrf_delay_ms(1);
            rx_irq_flg=0;
           
        }

    } else
    {


        if((rx_irq_flg)&&(get_time_escape_ms(NRF_RTC1->COUNTER,ticktime)<=20))
        {
            rx_irq_flg=0;
        }


        if((false == en_sleep)||(rx_irq_flg))
        {
            //disirq
            enable_pin_irq(rx_pin,false);
            uart_init(tx_pin,rx_pin,baud);
            uart_power_on_sec=0;
            nrf_delay_ms(1);
        }

    }


}








你可能感兴趣的:(嵌入式软件)