Skip to content

Commit

Permalink
implement software-based aux uart
Browse files Browse the repository at this point in the history
  • Loading branch information
Poofjunior committed Sep 18, 2024
1 parent 37b3e13 commit d7f34bb
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 68 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "firmware/lib/harp.core.rp2040"]
path = firmware/lib/harp.core.rp2040
url = [email protected]:AllenNeuralDynamics/harp.core.rp2040.git
[submodule "firmware/lib/pico.async-uart"]
path = firmware/lib/pico.async-uart
url = [email protected]:AllenNeuralDynamics/pico.async-uart.git
13 changes: 6 additions & 7 deletions firmware/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ add_definitions(-DGIT_HASH="${COMMIT_ID}") # Usable in source code.
#add_definitions(-DDEBUG) # Uncomment for debugging


#set(PICO_PLATFORM rp2040) # rp2040 is the default.

# PICO_SDK_PATH must be defined.
include(${PICO_SDK_PATH}/pico_sdk_init.cmake)

Expand All @@ -23,10 +25,7 @@ project(white_rabbit)
pico_sdk_init()

add_subdirectory(lib/harp.core.rp2040/firmware) # Path to harp.core.rp2040.

add_library(uart_nonblocking
src/uart_nonblocking.cpp
)
add_subdirectory(lib/pico.async-uart) # Path to uart_nonblocking and soft_uart

add_library(white_rabbit_app
src/white_rabbit_app.cpp
Expand All @@ -38,12 +37,12 @@ add_executable(${PROJECT_NAME}

include_directories(inc)

target_link_libraries(uart_nonblocking hardware_dma hardware_timer pico_stdlib)
target_link_libraries(white_rabbit_app harp_core harp_c_app harp_sync
hardware_divider pico_stdlib uart_nonblocking hardware_dma)
hardware_divider pico_stdlib uart_nonblocking pio_uart
hardware_pio hardware_dma soft_uart)

target_link_libraries(${PROJECT_NAME} harp_core harp_c_app harp_sync pico_stdlib
hardware_dma hardware_timer uart_nonblocking
hardware_dma hardware_timer uart_nonblocking pio_uart
white_rabbit_app)

pico_add_extra_outputs(${PROJECT_NAME})
Expand Down
2 changes: 1 addition & 1 deletion firmware/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This project uses the [Pico SDK](https://github.com/raspberrypi/pico-sdk/tree/ma
The SDK needs to be downloaded and installed to a known folder on your PC.
Note that the PICO SDK also contains submodules (including TinyUSB), so you must ensure that they are also fetched with:
````
git clone git clone git@github.com:raspberrypi/pico-sdk.git
git clone [email protected]:raspberrypi/pico-sdk.git
git submodule update --init --recursive
````

Expand Down
3 changes: 1 addition & 2 deletions firmware/inc/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@
#define MAX_AUX_SYNC_BAUDRATE (1'000'000UL) // Aux Baud rate should be slower
// than this value.
#define AUX_PIN (0)
#define AUX_SYNC_START_OFFSET_US (0) // Offset from spec to account for fixed
// delays before transmission starts.
#define AUX_SYNC_START_OFFSET_US (0)

#define LED0_PIN (24)
#define LED1_PIN (25)
Expand Down
13 changes: 0 additions & 13 deletions firmware/inc/uart_nonblocking.h

This file was deleted.

1 change: 1 addition & 0 deletions firmware/inc/white_rabbit_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <harp_c_app.h>
#include <harp_synchronizer.h>
#include <uart_nonblocking.h>
#include <soft_uart.h>
#include <core_registers.h>
#include <pico/divider.h> // for fast hardware division.
#ifdef DEBUG
Expand Down
2 changes: 1 addition & 1 deletion firmware/lib/harp.core.rp2040
1 change: 1 addition & 0 deletions firmware/lib/pico.async-uart
Submodule pico.async-uart added at d66fa5
29 changes: 0 additions & 29 deletions firmware/src/uart_nonblocking.cpp

This file was deleted.

26 changes: 13 additions & 13 deletions firmware/src/white_rabbit_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,14 @@ volatile uint32_t __not_in_flash("double_buffers") aux_clkout_seconds_b;
volatile uint32_t __not_in_flash("double_buffers") *dispatch_second;
volatile uint32_t __not_in_flash("double_buffers") *load_second;

// AUX CLKout software implementation.
SoftUART soft_uart = SoftUART(AUX_PIN);

// PPS Alarm/IRQ resources
int32_t __not_in_flash("double_buffers") pps_output_alarm_num = -1;
uint32_t __not_in_flash("double_buffers") pps_output_irq_number;


void setup_harp_clkout()
{
// Setup DMA.
Expand Down Expand Up @@ -131,22 +135,16 @@ void __not_in_flash_func(dispatch_and_reschedule_harp_clkout)()
timer_hw->inte |= (1u << harp_clkout_alarm_num);
// Arm alarm by writing the alarm time.
timer_hw->alarm[harp_clkout_alarm_num] = alarm_time_us;

}

void setup_aux_clkout()
{
// Setup DMA.
if (aux_clkout_dma_chan < 0) // Claim a DMA channel if not yet claimed.
aux_clkout_dma_chan = dma_claim_unused_channel(true);
// Setup GPIO Pins.
// Setup UART TX for periodic transmission of the time.
uart_inst_t* aux_uart_id = AUX_SYNC_UART;
uart_init(aux_uart_id, app_regs.AuxBaudRate);
uart_set_hw_flow(aux_uart_id, false, false);
uart_set_fifo_enabled(aux_uart_id, false); // Set FIFO size to 1.
uart_set_format(aux_uart_id, 8, 1, UART_PARITY_NONE);
gpio_set_function(AUX_PIN, GPIO_FUNC_UART);
// Update baud rate (if it has changed).
soft_uart.reset();
soft_uart.set_baud_rate(app_regs.AuxBaudRate);
// Setup AUX CLKOUT periodic outgoing time message.
// Setup Outgoing msg double buffer;
dispatch_second = &aux_clkout_seconds_a;
Expand All @@ -169,7 +167,7 @@ void setup_aux_clkout()
*dispatch_second = curr_harp_seconds + 1;
uint64_t next_msg_harp_time_us = (uint64_t(curr_harp_seconds) * 1'000'000UL)
+ 1'000'000UL;
// Apply offset if any.
// Apply additional offset if any.
next_msg_harp_time_us += AUX_SYNC_START_OFFSET_US;
// Schedule next time msg dispatch in system time.
// Low-level interface (fast!) to re-schedule this function.
Expand All @@ -183,8 +181,8 @@ void setup_aux_clkout()
void __not_in_flash_func(dispatch_and_reschedule_aux_clkout)()
{
// Dispatch the previously-configured time.
dispatch_uart_stream(aux_clkout_dma_chan, AUX_SYNC_UART,
(uint8_t*)dispatch_second, sizeof(dispatch_second));
soft_uart.send((uint8_t*)dispatch_second, sizeof(dispatch_second));

// Clear the latched hardware interrupt.
timer_hw->intr = (1u << aux_clkout_alarm_num);
// Compute the *next* whole harp time second.
Expand Down Expand Up @@ -230,7 +228,7 @@ void cleanup_aux_clkout()
irq_set_enabled(aux_clkout_irq_number, false);
irq_remove_handler(aux_clkout_irq_number,
dispatch_and_reschedule_aux_clkout);
uart_deinit(AUX_SYNC_UART);
soft_uart.cleanup();
gpio_deinit(AUX_PIN);
}

Expand Down Expand Up @@ -415,6 +413,8 @@ void write_aux_baud_rate(msg_t& msg)

void update_app_state()
{
if (soft_uart.requires_update())
soft_uart.update();
// Update the state of ConnectedDevices.
uint16_t old_port_raw = app_regs.ConnectedDevices;
uint32_t port_raw = gpio_get_all();
Expand Down
4 changes: 2 additions & 2 deletions software/pyharp/enable_aux_uart.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class Regs(Enum):
AuxPortBaudRate = 36 # U32


#BAUDRATE = 115200
BAUDRATE = 9600
BAUDRATE = 115200
#BAUDRATE = 9600
#BAUDRATE = 1000

# Open the device and print the info on screen
Expand Down

0 comments on commit d7f34bb

Please sign in to comment.