rcbench
Servo and ESC test bench: panel, IOmcu and shared C
Loading...
Searching...
No Matches
tone_cap.c File Reference

The phase tap's capture. More...

#include "tone_cap.h"
#include <string.h>
#include "hardware/clocks.h"
#include "hardware/dma.h"
#include "hardware/gpio.h"
#include "hardware/pio.h"
#include "pico/stdlib.h"
#include "tone_cap.pio.h"
#include "tone_svc.h"

Macros

#define RING_BYTES   (TONE_RING_WORDS * 4u)
 16kB, on a 16kB boundary: the DMA wraps its write address on it.
#define RING_BITS   14u

Functions

void tone_cap_rest (uint8_t pin)
 The pin as an input with its pull-down on, driven by nothing: a tap that is off.
bool tone_cap_start (uint8_t pin)
 Claim a state machine and a DMA channel, load the program, start the capture on pin.
bool tone_cap_pause (void)
 Mark the capture not running, so the next order tells core 1 to read nothing from it; the state machine and the channel stay, and tone_cap_wr() and tone_cap_stalled() still read them for a pass already under way.
void tone_cap_resume (void)
 Mark a paused capture running again, when core 1 did not acknowledge the pause and nothing was taken down.
void tone_cap_stop (void)
 Stop the capture and give back what it claimed; the pin is left an input with its pull-down on.
bool tone_cap_running (void)
 Whether the capture runs.
uint64_t tone_cap_start_us (void)
 The microsecond timer when the counter began.
const uint32_t * tone_cap_ring (void)
 The ring.
uint32_t tone_cap_wr (void)
 The slot the DMA writes next; 0 when the capture does not run.
bool tone_cap_stalled (void)
 Whether the state machine dropped a word (its FIFO was full) since the last call; clears the flag.

Variables

static uint32_t s_ring [TONE_RING_WORDS]
static volatile bool s_running
 Written by core 0 while the capture is stopped or starting, read by core 1: running is set last and cleared first.
static PIO s_pio
static uint s_sm
static uint s_offset
static int s_dma = -1
static volatile uint32_t s_wr_base
static uint8_t s_pin
static uint64_t s_start_us

Detailed Description

The phase tap's capture.

See tone_cap.h.

SPDX-License-Identifier: MIT

Function Documentation

◆ tone_cap_pause()

bool tone_cap_pause ( void )

Mark the capture not running, so the next order tells core 1 to read nothing from it; the state machine and the channel stay, and tone_cap_wr() and tone_cap_stalled() still read them for a pass already under way.

True when it was running. The first step of taking the capture down: tell core 1, wait for it to acknowledge, then tone_cap_stop().

Returns
true when it was running.

◆ tone_cap_rest()

void tone_cap_rest ( uint8_t pin)

The pin as an input with its pull-down on, driven by nothing: a tap that is off.

Not while the capture runs.

Parameters
pinGPIO number of the phase input.

◆ tone_cap_ring()

const uint32_t * tone_cap_ring ( void )

The ring.

Returns
The ring of edge words.

◆ tone_cap_running()

bool tone_cap_running ( void )

Whether the capture runs.

Returns
true while the capture runs.

◆ tone_cap_stalled()

bool tone_cap_stalled ( void )

Whether the state machine dropped a word (its FIFO was full) since the last call; clears the flag.

Called from core 1 only.

Returns
true when a word was dropped since the last call.

◆ tone_cap_start()

bool tone_cap_start ( uint8_t pin)

Claim a state machine and a DMA channel, load the program, start the capture on pin.

False, with nothing claimed, when no PIO block that reaches the pin has room for the program and a state machine, no DMA channel is free, or the system clock is not 150MHz.

Parameters
pinGPIO number of the phase input.
Returns
true when the capture runs.

◆ tone_cap_start_us()

uint64_t tone_cap_start_us ( void )

The microsecond timer when the counter began.

Returns
The microsecond timer value when the counter began.

◆ tone_cap_stop()

void tone_cap_stop ( void )

Stop the capture and give back what it claimed; the pin is left an input with its pull-down on.

Safe when it is not running. Core 1 is not reading the ring or the channel at that moment (tone_cap_pause()).

◆ tone_cap_wr()

uint32_t tone_cap_wr ( void )

The slot the DMA writes next; 0 when the capture does not run.

Returns
The slot the DMA writes next, 0 when the capture does not run.

Variable Documentation

◆ s_pin

uint8_t s_pin
static

the capture pin, GPIO

◆ s_start_us

uint64_t s_start_us
static

the capture's start, µs

◆ s_wr_base

volatile uint32_t s_wr_base
static

the ring's address