This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

NRF51 - Hardware UART - hints needed

Hi

I'm using the NRF51422 with hardware UART. Brief outline of the configuration: 115200 Baud, 8-N-1, no HW flowcontrol SoftDevice S130 V2.0.1 lib: app uart fifo

The main problem seems to be the SoC trying to send over UART while BLE timeslot is started.

My question: How do I manage to send data, when the SD is not in a BLE timeslot and if there is more data resume sending remaining data over UART when the transmission was interrupted by a timeslot to push all data to the PC. I already looked at all examples, but non of them handles large data over UART and fifo send resume...

Any hint is appreciated.

If it helps, this is my uart send function:

inline void uart_put_string(const char *str, ...){
    	char buffer[1024];
    	va_list args;
        va_start(args, str);
        vsprintf((char*)buffer, (char*)str, args);
        va_end(args);
    
    	int len = strlen(buffer);
    
    	for(int i=0; i<len;i++){
    		while(app_uart_put(buffer[i]) != NRF_SUCCESS);
    	}
    }
  • Hi Martin

    The problem with the attached code is that you will essentially lock up the system when the UART is full, since you keep trying to add more data to the UART driver.

    I would suggest a more asynchronous approach where you push data to the UART in a non blocking manner.

    For instance, you could do something like this:

    static char buffer[1024];
    static uint32_t buffer_pos = 0, buffer_length = 0;
    
    static void uart_put_string(const char *str, ...)
    {
        va_list args;
        va_start(args, str);
        vsprintf((char*)buffer, (char*)str, args);
        va_end(args);
    
        buffer_length = strlen(buffer);
        buffer_pos = 0;
    }
    
    
    // while loop in main()
    while(1)
    {
        ..
        
        while(buffer_length != 0 && app_uart_put(buffer[buffer_pos++]) == NRF_SUCCESS)
        {
            if(buffer_pos >= buffer_length) 
            { 
                buffer_length = 0;
            }
        }
        
        power_down();
    
    }
    

    Then you will be able to go to sleep or do other things when the UART is full, and as soon as bytes are cleared up from the UART the chip should be woken up from sleep and you can put more data in.

    Best regards
    Torbjørn

Related