ether2ser 0.1.0
Ethernet <-> synchronous V.24 bridge firmware for RP2040 + W5500
Loading...
Searching...
No Matches
pio_tx_rx_driver.c File Reference
#include "pio_tx_rx_driver.h"
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "hardware/gpio.h"
#include "hardware/pio.h"
#include "hardware/platform_defs.h"
#include "hardware/regs/pio.h"
#include "pico/time.h"
#include "pico/types.h"
#include "drivers/gpio_driver.h"
#include "drivers/v24_config.h"
#include "platform/pinmap.h"
#include "system/common.h"
#include "led_activity_mirror.pio.h"
#include "rck_rxd.pio.h"
#include "tck_txd.pio.h"
#include "xck_txd.pio.h"
Include dependency graph for pio_tx_rx_driver.c:

Go to the source code of this file.

Macros

#define V24_RTS_MIN_HOLDOFF   200U
 Minimum RTS holdoff in microseconds used as a safety floor.
 
#define V24_RTS_HOLDOFF_MARGIN   41U
 RTS holdoff multiplier in bit-times. The final holdoff is: tx_rts_holdoff_us = margin * t_bit_us. This value was empirically determined to be the most stable for this system.
 
#define LED_MIRROR_PIO   pio0
 

Functions

const v24_runtime_tget_v24_runtime (void)
 Returns a pointer to the v24 runtime.
 
uint32_t tx_clock_get_cts_toggle_seq (void)
 Read current CTS edge sequence counter.
 
static void cts_raw_irq_handler (void)
 
static void cts_irq_init (void)
 
static gpio_function_t pio_gpio_func (PIO pio)
 
static float baud_to_clockdiv (V24_BAUDRATE_T baudrate)
 
void led_mirror_init (void)
 Initialize optional LED activity mirror PIO program.
 
void reinit_v24_config (V24_CONFIG_T *config, V24_BAUDRATE_T baudrate)
 Reinitialize V.24 runtime configuration and derived timing values.
 
void init_v24_config (V24_CONFIG_T *config, V24_BAUDRATE_T baudrate)
 Initialize V.24 runtime configuration structure.
 
bool rx_clock_poll_stall (void)
 Poll RX FIFO stall events and update the provided event counter.
 
bool rx_get (uint8_t *data)
 Read one received byte from RX PIO FIFO.
 
void rx_clock_update_settings (V24_RX_POLARITIES_T *polarities)
 Apply RX runtime settings to an already configured RX PIO SM. Take note that this function relies on tx_clock_init being called before. This is asserted in debug builds but otherwise UB in production.
 
void rx_clock_hard_reset (void)
 Disable RX Path, clear fifos, restart SMs and CLKDIV and enable again.
 
void rx_clock_init (PIO pio, uint pio_sm, V24_RX_POLARITIES_T *polarities)
 Initialize RX clock/data PIO state machine.
 
bool tx_poll (void)
 Poll TX completion/holdoff state and manage RTS release.
 
bool tx_put (uint8_t data)
 Queue one byte to the TX PIO FIFO.
 
void tx_clock_hard_reset (void)
 Hard-reset TX SM state to discard queued/in-flight bytes.
 
void tx_clock_update_settings (V24_CONFIG_T *config)
 Apply TX runtime settings to an already configured TX PIO SM. Take note that this function relies on tx_clock_init being called before. This is asserted in debug builds but otherwise UB in production.
 
static void tx_clock_init_tck (PIO pio, uint pio_sm, V24_CONFIG_T *config)
 
static void tx_clock_init_xck (PIO pio, uint pio_sm, V24_CONFIG_T *config)
 
void tx_clock_init (PIO pio, uint pio_sm, V24_CONFIG_T *config)
 Initialize TX clock/data PIO state machine.
 

Variables

v24_runtime_t v24_runtime
 

Macro Definition Documentation

◆ LED_MIRROR_PIO

#define LED_MIRROR_PIO   pio0

Definition at line 112 of file pio_tx_rx_driver.c.

◆ V24_RTS_HOLDOFF_MARGIN

#define V24_RTS_HOLDOFF_MARGIN   41U

RTS holdoff multiplier in bit-times. The final holdoff is: tx_rts_holdoff_us = margin * t_bit_us. This value was empirically determined to be the most stable for this system.

Definition at line 51 of file pio_tx_rx_driver.c.

◆ V24_RTS_MIN_HOLDOFF

#define V24_RTS_MIN_HOLDOFF   200U

Minimum RTS holdoff in microseconds used as a safety floor.

Definition at line 45 of file pio_tx_rx_driver.c.

Function Documentation

◆ baud_to_clockdiv()

static float baud_to_clockdiv ( V24_BAUDRATE_T  baudrate)
static

Definition at line 107 of file pio_tx_rx_driver.c.

References TX_PIO_CYCLES_PER_BIT.

Referenced by tx_clock_init_tck(), and tx_clock_update_settings().

Here is the caller graph for this function:

◆ cts_irq_init()

static void cts_irq_init ( void  )
static

Definition at line 77 of file pio_tx_rx_driver.c.

References cts_raw_irq_handler(), and V24_CTS.

Referenced by tx_clock_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ cts_raw_irq_handler()

static void cts_raw_irq_handler ( void  )
static

Definition at line 66 of file pio_tx_rx_driver.c.

References v24_runtime_t::cts_toggle_seq, V24_CTS, and v24_runtime.

Referenced by cts_irq_init().

Here is the caller graph for this function:

◆ get_v24_runtime()

const v24_runtime_t * get_v24_runtime ( void  )

Returns a pointer to the v24 runtime.

Returns
Pointer to the v24 runtime.

Definition at line 56 of file pio_tx_rx_driver.c.

References v24_runtime.

Referenced by print_status_event().

Here is the caller graph for this function:

◆ init_v24_config()

void init_v24_config ( V24_CONFIG_T config,
V24_BAUDRATE_T  baudrate 
)

Initialize V.24 runtime configuration structure.

Parameters
configDestination configuration object.
baudrateInitial baudrate.

Definition at line 164 of file pio_tx_rx_driver.c.

References init_polarities(), V24_CONFIG_T::polarities, and reinit_v24_config().

Referenced by init_app().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ led_mirror_init()

void led_mirror_init ( void  )

Initialize optional LED activity mirror PIO program.

Definition at line 113 of file pio_tx_rx_driver.c.

References LED_MIRROR_PIO, LOG_ERROR, LOG_INFO, V24_RXD, V24_STATUS_LED, and V24_TXD.

◆ pio_gpio_func()

static gpio_function_t pio_gpio_func ( PIO  pio)
static

Definition at line 93 of file pio_tx_rx_driver.c.

◆ reinit_v24_config()

void reinit_v24_config ( V24_CONFIG_T config,
V24_BAUDRATE_T  baudrate 
)

Reinitialize V.24 runtime configuration and derived timing values.

Parameters
configDestination configuration object.
baudrateNew baudrate.

Definition at line 152 of file pio_tx_rx_driver.c.

References V24_CONFIG_T::baudrate, v24_runtime_t::rts_set, v24_runtime_t::tx_rts_holdoff_us, US_PER_SECOND, V24_BAUD_1200, V24_RTS_HOLDOFF_MARGIN, V24_RTS_MIN_HOLDOFF, and v24_runtime.

Referenced by ev_set_v24_settings(), init_app(), and init_v24_config().

Here is the caller graph for this function:

◆ rx_clock_hard_reset()

void rx_clock_hard_reset ( void  )

Disable RX Path, clear fifos, restart SMs and CLKDIV and enable again.

Definition at line 213 of file pio_tx_rx_driver.c.

References v24_runtime_t::rx_pio, v24_runtime_t::rx_sm, and v24_runtime.

Referenced by poll_hdlc_no_progress().

Here is the caller graph for this function:

◆ rx_clock_init()

void rx_clock_init ( PIO  pio,
uint  pio_sm,
V24_RX_POLARITIES_T polarities 
)

Initialize RX clock/data PIO state machine.

Parameters
pioPIO instance.
pio_smState machine index.
polaritiesRX signal polarity configuration.

Definition at line 225 of file pio_tx_rx_driver.c.

References LOG_INFO, rx_clock_update_settings(), v24_runtime_t::rx_pio, v24_runtime_t::rx_sm, v24_runtime, V24_RXC, and V24_RXD.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ rx_clock_poll_stall()

bool rx_clock_poll_stall ( void  )

Poll RX FIFO stall events and update the provided event counter.

Returns
True if stalled, false otherwise.

Definition at line 170 of file pio_tx_rx_driver.c.

References v24_runtime_t::rx_pio, v24_runtime_t::rx_sm, and v24_runtime.

Referenced by event_loop().

Here is the caller graph for this function:

◆ rx_clock_update_settings()

void rx_clock_update_settings ( V24_RX_POLARITIES_T polarities)

Apply RX runtime settings to an already configured RX PIO SM. Take note that this function relies on tx_clock_init being called before. This is asserted in debug builds but otherwise UB in production.

Parameters
polaritiesRX polarity settings.

Definition at line 195 of file pio_tx_rx_driver.c.

References V24_RX_POLARITIES_T::dcd_inverted, v24_runtime_t::rx_pio, v24_runtime_t::rx_sm, V24_RX_POLARITIES_T::rxc_inverted, V24_RX_POLARITIES_T::rxd_inverted, V24_DCD, v24_runtime, V24_RXC, and V24_RXD.

Referenced by ev_set_v24_settings(), and rx_clock_init().

Here is the caller graph for this function:

◆ rx_get()

bool rx_get ( uint8_t *  data)

Read one received byte from RX PIO FIFO.

Parameters
dataDestination pointer for the received byte.
Returns
true if a byte was read, false if RX FIFO is empty.

Definition at line 184 of file pio_tx_rx_driver.c.

References v24_runtime_t::rx_pio, RX_SHIFT_TO_LSB, v24_runtime_t::rx_sm, and v24_runtime.

Referenced by drain_rx_until_empty().

Here is the caller graph for this function:

◆ tx_clock_get_cts_toggle_seq()

uint32_t tx_clock_get_cts_toggle_seq ( void  )

Read current CTS edge sequence counter.

Returns
Monotonic CTS edge sequence value.

Definition at line 61 of file pio_tx_rx_driver.c.

References v24_runtime_t::cts_toggle_seq, and v24_runtime.

Referenced by tx_queue_drain().

Here is the caller graph for this function:

◆ tx_clock_hard_reset()

void tx_clock_hard_reset ( void  )

Hard-reset TX SM state to discard queued/in-flight bytes.

Definition at line 322 of file pio_tx_rx_driver.c.

References v24_runtime_t::tx_pio, v24_runtime_t::tx_sm, and v24_runtime.

Referenced by tx_queue_drain().

Here is the caller graph for this function:

◆ tx_clock_init()

void tx_clock_init ( PIO  pio,
uint  pio_sm,
V24_CONFIG_T config 
)

Initialize TX clock/data PIO state machine.

Parameters
pioPIO instance.
pio_smState machine index.
configV.24 configuration.

Definition at line 453 of file pio_tx_rx_driver.c.

References cts_irq_init(), V24_CONFIG_T::external_clock, tx_clock_init_tck(), and tx_clock_init_xck().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tx_clock_init_tck()

static void tx_clock_init_tck ( PIO  pio,
uint  pio_sm,
V24_CONFIG_T config 
)
static

Definition at line 376 of file pio_tx_rx_driver.c.

References baud_to_clockdiv(), V24_CONFIG_T::baudrate, LOG_DEBUG, LOG_INFO, tx_clock_update_settings(), v24_runtime_t::tx_pio, v24_runtime_t::tx_rts_holdoff_us, v24_runtime_t::tx_sm, V24_CTS, v24_runtime, V24_TXC_DTE, and V24_TXD.

Referenced by tx_clock_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tx_clock_init_xck()

static void tx_clock_init_xck ( PIO  pio,
uint  pio_sm,
V24_CONFIG_T config 
)
static

Definition at line 415 of file pio_tx_rx_driver.c.

References LOG_DEBUG, LOG_INFO, tx_clock_update_settings(), v24_runtime_t::tx_pio, v24_runtime_t::tx_rts_holdoff_us, v24_runtime_t::tx_sm, V24_CTS, v24_runtime, V24_TXC_DCE, and V24_TXD.

Referenced by tx_clock_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tx_clock_update_settings()

void tx_clock_update_settings ( V24_CONFIG_T config)

Apply TX runtime settings to an already configured TX PIO SM. Take note that this function relies on tx_clock_init being called before. This is asserted in debug builds but otherwise UB in production.

Parameters
configV.24 configuration.

Definition at line 337 of file pio_tx_rx_driver.c.

References baud_to_clockdiv(), V24_CONFIG_T::baudrate, V24_TX_POLARITIES_T::cts_inverted, V24_TX_POLARITIES_T::dtr_inverted, V24_CONFIG_T::external_clock, V24_CONFIG_T::polarities, V24_TX_POLARITIES_T::rts_inverted, v24_runtime_t::tx_pio, V24_POLARITIES_T::tx_polarities, v24_runtime_t::tx_sm, V24_TX_POLARITIES_T::txc_inverted, V24_TX_POLARITIES_T::txd_inverted, V24_CTS, V24_DTR, V24_RTS, v24_runtime, V24_TXC_DCE, V24_TXC_DTE, and V24_TXD.

Referenced by ev_set_v24_settings(), tx_clock_init_tck(), and tx_clock_init_xck().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tx_poll()

bool tx_poll ( void  )

Poll TX completion/holdoff state and manage RTS release.

Returns
true if TX path is idle after polling, false otherwise.

Definition at line 257 of file pio_tx_rx_driver.c.

References v24_runtime_t::rts_set, v24_runtime_t::tx_pio, v24_runtime_t::tx_rts_holdoff_us, v24_runtime_t::tx_sm, V24_RTS, and v24_runtime.

Referenced by poll_tx_pipeline().

Here is the caller graph for this function:

◆ tx_put()

bool tx_put ( uint8_t  data)

Queue one byte to the TX PIO FIFO.

Parameters
dataByte to transmit.
Returns
true if accepted, false if FIFO/full-state prevents enqueue.

Definition at line 299 of file pio_tx_rx_driver.c.

References v24_runtime_t::rts_set, v24_runtime_t::tx_pio, v24_runtime_t::tx_sm, V24_RTS, and v24_runtime.

Referenced by tx_queue_drain_bytes().

Here is the caller graph for this function:

Variable Documentation

◆ v24_runtime