当前位置:   article > 正文

nrf52832自动唤醒功能的串口_52833唤醒方案

52833唤醒方案

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
#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);
        }

    }


}








  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/194257
推荐阅读
相关标签
  

闽ICP备14008679号