nrf52832自动唤醒功能的串口

1.0实现的接口

#include <stdbool.h>
#include <stdint.h>
#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<len;i++)
	{
		counter=0;
	    while(NRF_SUCCESS!=app_uart_put(str[i]) && counter++<16000);
	}
}



//================================
static uint8_t rx_irq_flg;
static uint16_t uart_power_on_sec=0;
static void pin_irq(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
{
    rx_irq_flg++;
}



static void enable_pin_irq(uint32_t pin,bool enable_flg)
{
    uint32_t err_code;
    nrf_drv_gpiote_in_config_t config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(false);
    config.pull = NRF_GPIO_PIN_PULLUP;
    if (!nrf_drv_gpiote_is_init())
    {
        err_code = nrf_drv_gpiote_init();
        APP_ERROR_CHECK(err_code);
    }



    if(true == enable_flg)
    {
        err_code = nrf_drv_gpiote_in_init(pin, &config, pin_irq);
        APP_ERROR_CHECK(err_code);
        nrf_drv_gpiote_in_event_enable(pin, true);
    } else
    {
        nrf_drv_gpiote_in_event_disable(pin);
        nrf_drv_gpiote_in_uninit(pin);

    }



}


void active_uart(void)
{
    if(1==uart_en_flg)
    {
        uart_power_on_sec=0;
    } else
    {
        //disirq
        enable_pin_irq(rx_pin,false);
        uart_init(tx_pin,rx_pin,baud);
		  nrf_delay_ms(1);
        uart_power_on_sec=0;
      
    }
}



void task_uart_power_manage(bool en_sleep,uint16_t sec_to_sleep)
{

    static uint32_t ticktime;
    if(1==uart_en_flg)
    {
        if( (true == en_sleep)&&(uart_power_on_sec>=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);
        }

    }


}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值