Software UART through emulated GPIO pins for nrf52833.

Hi there,

Currently, I'm working on a solution where I need to design a software UART through emulated GPIO pins for nrf52833.

The requirements are that, solution would be all in bare-metal, I mean, without using the SDK.

Then, if you could help me with a good example or snippet of code to starting, I would appreciate your help.

Whatever advice you leave, it'll be received.
Thanks in advance.

  • Hi,

    ChatGPT gives the following reference code, and I don't test it, maybe not work. Just for your reference.

    #include "nrf_gpio.h"
    #include "nrf_timer.h"
    #include "nrf_drv_timer.h"
    
    #define TX_PIN 17
    #define RX_PIN 18
    
    volatile uint8_t tx_buffer = 0;
    volatile uint8_t rx_buffer = 0;
    volatile bool tx_ready = true;
    volatile bool rx_ready = false;
    
    void timer_handler(void) {
        if (tx_ready) {
            nrf_gpio_pin_toggle(TX_PIN);
            tx_ready = false;
        }
        if (rx_ready) {
            rx_ready = false;
            rx_buffer = nrf_gpio_pin_read(RX_PIN);
        }
    }
    
    void uart_send(uint8_t byte) {
        tx_buffer = byte;
        tx_ready = true;
    }
    
    uint8_t uart_receive(void) {
        while (!rx_ready) {
            // Wait for data to be ready
        }
        return rx_buffer;
    }
    
    void setup(void) {
        nrf_gpio_cfg_output(TX_PIN);
        nrf_gpio_cfg_input(RX_PIN, NRF_GPIO_PIN_PULLUP);
        nrf_drv_timer_init(&timer, NRF_TIMER_MODE_COMPARE, false, timer_handler);
        nrf_drv_timer_start(&timer, NRF_TIMER_TICKS_PER_US, false);
    }
    
    int main(void) {
        setup();
        while (1) {
            uart_send(0x55); // Send a byte
            uint8_t received_byte = uart_receive(); // Receive a byte
        }
    }
    

Related