Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into rp2350_pio_assembler
Browse files Browse the repository at this point in the history
  • Loading branch information
Grazfather committed Jan 16, 2025
2 parents eef3e79 + 533a26f commit f3c9a4d
Show file tree
Hide file tree
Showing 53 changed files with 18,925 additions and 3,735 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ boxzer-out
microzig-deploy/
zig-cache/
zig-out/

*.regz
52 changes: 4 additions & 48 deletions build-internals/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const Module = Build.Module;

const regz = @import("regz");
const Patch = regz.patch.Patch;
const uf2 = @import("uf2");
const FamilyId = uf2.FamilyId;

pub fn build(b: *Build) void {
_ = b.addModule("build-internals", .{
Expand Down Expand Up @@ -110,6 +112,7 @@ pub const Chip = struct {
/// The memory regions that are present in this chip.
memory_regions: []const MemoryRegion,

/// Register patches for this chip.
patches: []const Patch = &.{},
};

Expand Down Expand Up @@ -172,54 +175,7 @@ pub const BinaryFormat = union(enum) {
dfu,

/// The [USB Flashing Format (UF2)](https://github.com/microsoft/uf2) designed by Microsoft.
uf2: enum {
ATMEGA32,
SAML21,
NRF52,
ESP32,
STM32L1,
STM32L0,
STM32WL,
LPC55,
STM32G0,
GD32F350,
STM32L5,
STM32G4,
MIMXRT10XX,
STM32F7,
SAMD51,
STM32F4,
FX2,
STM32F2,
STM32F1,
NRF52833,
STM32F0,
SAMD21,
STM32F3,
STM32F407,
STM32H7,
STM32WB,
ESP8266,
KL32L2,
STM32F407VG,
NRF52840,
ESP32S2,
ESP32S3,
ESP32C3,
ESP32C2,
ESP32H2,
RP2040,
RP2XXX_ABSOLUTE,
RP2XXX_DATA,
RP2350_ARM_S,
RP2350_RISC_V,
RP2350_ARM_NS,
STM32L4,
GD32VF103,
CSK4,
CSK6,
M0SENSE,
},
uf2: uf2.FamilyId,

/// The [firmware format](https://docs.espressif.com/projects/esptool/en/latest/esp32/advanced-topics/firmware-image-format.html) used by the [esptool](https://github.com/espressif/esptool) bootloader.
esp,
Expand Down
1 change: 1 addition & 0 deletions build-internals/build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.version = "0.0.0",
.dependencies = .{
.regz = .{ .path = "../tools/regz" },
.uf2 = .{ .path = "../tools/uf2" },
},
.paths = .{
"LICENSE",
Expand Down
1 change: 1 addition & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ pub fn MicroBuild(port_select: PortSelect) type {
.imports = options.imports,
});
app_mod.addImport("microzig", core_mod);
core_mod.addImport("app", app_mod);

const fw = mb.builder.allocator.create(Firmware) catch @panic("out of memory");
fw.* = .{
Expand Down
2 changes: 2 additions & 0 deletions examples/raspberrypi/rp2xxx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ examples will eventually be able to run on either chip with no changes due to th
Shows an example of a fully custom clock configuration.
- [gpio clock output](src/gpio_clock_output.zig) on the [Pico](https://www.raspberrypi.com/products/raspberry-pi-pico/) or [Pico2](https://www.raspberrypi.com/products/raspberry-pi-pico-2/) boards
Routes the SYS clock divided by 1000 out to GPIO25.
- [watchdog timer](src/watchdog_timer.zig) on the [Pico](https://www.raspberrypi.com/products/raspberry-pi-pico/) or [Pico2](https://www.raspberrypi.com/products/raspberry-pi-pico-2/) boards
Enables a watchdog timer for 1 second, and demonstrates the chip resetting when the watchdog timer elapses

### RP2040 Only

Expand Down
4 changes: 2 additions & 2 deletions examples/raspberrypi/rp2xxx/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ pub fn build(b: *std.Build) void {
const rp2350_only_examples: []const Example = &.{};

const chip_agnostic_examples: []const ChipAgnosticExample = &.{
.{ .name = "ws2812", .file = "src/ws2812.zig" },
// .{ .name = "squarewave", .file = "src/squarewave.zig" },
// .{ .name = "ws2812", .file = "src/ws2812.zig" },
// .{ .name = "blinky", .file = "src/blinky.zig" },
// .{ .name = "gpio-clock-output", .file = "src/gpio_clock_output.zig" },
// .{ .name = "changing-system-clocks", .file = "src/changing_system_clocks.zig" },
// .{ .name = "custom-clock-config", .file = "src/custom_clock_config.zig" },
// .{ .name = "watchdog-timer", .file = "src/watchdog_timer.zig" },
};

var available_examples = std.ArrayList(Example).init(b.allocator);
Expand Down
4 changes: 2 additions & 2 deletions examples/raspberrypi/rp2xxx/src/changing_system_clocks.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ const Pin = rp2xxx.gpio.Pin;

/// The HAL provides a convenvience function for detecting which of the RP2XXX
/// family you're currently compiling for.
const cpu = rp2xxx.compatibility.cpu;
const chip = rp2xxx.compatibility.chip;

// Use the system() preset helper to change the SYS and REF clock frequencies from default
const system_clock_cfg = clocks.config.preset.system(
// Reduce the system clock by a factor of 4 (different default clock speeds for RP2350/RP2040)
switch (cpu) {
switch (chip) {
.RP2040 => 125_000_000 / 4,
.RP2350 => 150_000_000 / 4,
},
Expand Down
4 changes: 2 additions & 2 deletions examples/raspberrypi/rp2xxx/src/custom_clock_config.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const Pin = gpio.Pin;

/// The HAL provides a convenvience function for detecting which of the RP2XXX
/// family you're currently compiling for.
const cpu = rp2xxx.compatibility.cpu;
const chip = rp2xxx.compatibility.chip;

const xosc_freq = microzig.board.xosc_freq;

Expand Down Expand Up @@ -84,7 +84,7 @@ const system_clock_cfg: GlobalConfig = val: {
};

// GlobalConfig has a slight difference between RP2350 (has HSTX) and RP2040 (has RTC)
switch (cpu) {
switch (chip) {
.RP2040 => {
// Drive RTC off XOSC / 3 for a change of pace!
temp.rtc = .{
Expand Down
4 changes: 2 additions & 2 deletions examples/raspberrypi/rp2xxx/src/rp2040_only/adc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const rp2xxx = microzig.hal;
const gpio = rp2xxx.gpio;
const adc = rp2xxx.adc;
const time = rp2xxx.time;
const cpu = rp2xxx.compatibility.cpu;
const chip = rp2xxx.compatibility.chip;

const uart = rp2xxx.uart.instance.num(0);
const baud_rate = 115200;
Expand All @@ -22,7 +22,7 @@ pub fn main() void {
.temp_sensor_enabled = true,
});

switch (cpu) {
switch (chip) {
.RP2040 => inline for (&.{ uart_tx_pin, uart_rx_pin }) |pin| {
pin.set_function(.uart);
},
Expand Down
4 changes: 2 additions & 2 deletions examples/raspberrypi/rp2xxx/src/rp2040_only/flash_id.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const rp2xxx = microzig.hal;
const time = rp2xxx.time;
const gpio = rp2xxx.gpio;
const flash = rp2xxx.flash;
const cpu = rp2xxx.compatibility.cpu;
const chip = rp2xxx.compatibility.chip;

const uart = rp2xxx.uart.instance.num(0);
const baud_rate = 115200;
Expand All @@ -24,7 +24,7 @@ pub const std_options = struct {
};

pub fn main() !void {
switch (cpu) {
switch (chip) {
.RP2040 => inline for (&.{ uart_tx_pin, uart_rx_pin }) |pin| {
pin.set_function(.uart);
},
Expand Down
4 changes: 2 additions & 2 deletions examples/raspberrypi/rp2xxx/src/rp2040_only/flash_program.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const flash = rp2xxx.flash;
const time = rp2xxx.time;
const gpio = rp2xxx.gpio;
const clocks = rp2xxx.clocks;
const cpu = rp2xxx.compatibility.cpu;
const chip = rp2xxx.compatibility.chip;

const led = gpio.num(25);
const uart = rp2xxx.uart.instance.num(0);
Expand All @@ -33,7 +33,7 @@ pub fn main() !void {
led.set_direction(.out);
led.put(1);

switch (cpu) {
switch (chip) {
.RP2040 => inline for (&.{ uart_tx_pin, uart_rx_pin }) |pin| {
pin.set_function(.uart);
},
Expand Down
4 changes: 2 additions & 2 deletions examples/raspberrypi/rp2xxx/src/rp2040_only/i2c_bus_scan.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const rp2xxx = microzig.hal;
const i2c = rp2xxx.i2c;
const gpio = rp2xxx.gpio;
const peripherals = microzig.chip.peripherals;
const cpu = rp2xxx.compatibility.cpu;
const chip = rp2xxx.compatibility.chip;

pub const microzig_options = .{
.log_level = .info,
Expand All @@ -20,7 +20,7 @@ const uart_rx_pin = gpio.num(1);
const i2c0 = i2c.instance.num(0);

pub fn main() !void {
switch (cpu) {
switch (chip) {
.RP2040 => inline for (&.{ uart_tx_pin, uart_rx_pin }) |pin| {
pin.set_function(.uart);
},
Expand Down
4 changes: 2 additions & 2 deletions examples/raspberrypi/rp2xxx/src/rp2040_only/random.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const time = rp2xxx.time;
const gpio = rp2xxx.gpio;
const clocks = rp2xxx.clocks;
const rand = rp2xxx.rand;
const cpu = rp2xxx.compatibility.cpu;
const chip = rp2xxx.compatibility.chip;

const led = gpio.num(25);
const uart = rp2xxx.uart.instance.num(0);
Expand All @@ -33,7 +33,7 @@ pub fn main() !void {
led.set_direction(.out);
led.put(1);

switch (cpu) {
switch (chip) {
.RP2040 => inline for (&.{ uart_tx_pin, uart_rx_pin }) |pin| {
pin.set_function(.uart);
},
Expand Down
4 changes: 2 additions & 2 deletions examples/raspberrypi/rp2xxx/src/rp2040_only/uart_log.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const rp2xxx = microzig.hal;
const time = rp2xxx.time;
const gpio = rp2xxx.gpio;
const clocks = rp2xxx.clocks;
const cpu = rp2xxx.compatibility.cpu;
const chip = rp2xxx.compatibility.chip;

const led = gpio.num(25);
const uart = rp2xxx.uart.instance.num(0);
Expand All @@ -29,7 +29,7 @@ pub fn main() !void {
led.set_direction(.out);
led.put(1);

switch (cpu) {
switch (chip) {
.RP2040 => inline for (&.{ uart_tx_pin, uart_rx_pin }) |pin| {
pin.set_function(.uart);
},
Expand Down
15 changes: 10 additions & 5 deletions examples/raspberrypi/rp2xxx/src/rp2040_only/usb_cdc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const time = rp2xxx.time;
const gpio = rp2xxx.gpio;
const clocks = rp2xxx.clocks;
const usb = rp2xxx.usb;
const cpu = rp2xxx.compatibility.cpu;
const chip = rp2xxx.compatibility.chip;

const led = gpio.num(25);
const uart = rp2xxx.uart.instance.num(0);
Expand Down Expand Up @@ -69,7 +69,7 @@ pub fn main() !void {
led.set_direction(.out);
led.put(1);

switch (cpu) {
switch (chip) {
.RP2040 => inline for (&.{ uart_tx_pin, uart_rx_pin }) |pin| {
pin.set_function(.uart);
},
Expand Down Expand Up @@ -119,20 +119,25 @@ pub fn main() !void {

var usb_tx_buff: [1024]u8 = undefined;

// Transfer data to host
// NOTE: After each USB chunk transfer, we have to call the USB task so that bus TX events can be handled
pub fn usb_cdc_write(comptime fmt: []const u8, args: anytype) void {
const text = std.fmt.bufPrint(&usb_tx_buff, fmt, args) catch &.{};

var write_buff = text;
while (write_buff.len > 0) {
write_buff = driver_cdc.write(write_buff);
usb_dev.task(false) catch unreachable;
}
// Short messages are not sent right away; instead, they accumulate in a buffer, so we have to force a flush to send them
_ = driver_cdc.write_flush();
usb_dev.task(false) catch unreachable;
}

var usb_rx_buff: [1024]u8 = undefined;

// TODO - right now there are 2 issues with reading data from serial:
// 1. It not always work, so sometime data is not received. This is probably related to how we reset usb buffer flags.
// 2. Even when usb host sends small chunk of data cdc read returns full packet length. This require further investigation.
// Receive data from host
// NOTE: Read code was not tested extensively. In case of issues, try to call USB task before every read operation
pub fn usb_cdc_read() []const u8 {
var total_read: usize = 0;
var read_buff: []u8 = usb_rx_buff[0..];
Expand Down
4 changes: 2 additions & 2 deletions examples/raspberrypi/rp2xxx/src/rp2040_only/usb_hid.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const time = rp2xxx.time;
const gpio = rp2xxx.gpio;
const clocks = rp2xxx.clocks;
const usb = rp2xxx.usb;
const cpu = rp2xxx.compatibility.cpu;
const chip = rp2xxx.compatibility.chip;

const led = gpio.num(25);
const uart = rp2xxx.uart.instance.num(0);
Expand Down Expand Up @@ -71,7 +71,7 @@ pub fn main() !void {
led.set_direction(.out);
led.put(1);

switch (cpu) {
switch (chip) {
.RP2040 => inline for (&.{ uart_tx_pin, uart_rx_pin }) |pin| {
pin.set_function(.uart);
},
Expand Down
50 changes: 50 additions & 0 deletions examples/raspberrypi/rp2xxx/src/watchdog_timer.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const std = @import("std");
const microzig = @import("microzig");
const rp2xxx = microzig.hal;
const watchdog = rp2xxx.watchdog;
const time = rp2xxx.time;

const pin_config = rp2xxx.pins.GlobalConfiguration{
.GPIO25 = .{
.name = "led",
.direction = .out,
},
};
const pins = pin_config.pins();

pub fn main() !void {
pin_config.apply();

// Set up different blinking behavior if this reset was due to a WD trip
const wd_reset: bool = if (watchdog.caused_reboot()) |_|
true
else
false;

watchdog.apply(.{
// Set up the watchdog timer to trip after 1 second
.duration_us = 1000 * 1000,
// Watchdog timer should NOT trip if a debugger is connected
// NOTE: This doesn't appear to work with a JLink, your mileage may vary
.pause_during_debug = true,
});

var blink_time: usize = 100;
var fast_counter: usize = 0;

// Slowly decrease blink frequency until a watchdog reset is triggered from waiting too long between watchdog.update() calls
while (true) {

// "null" uses whatever the previously configured timeout period for the watchdog was
watchdog.update(null);
pins.led.toggle();
time.sleep_ms(@as(usize, @intCast(blink_time)));

// 10 fast blinks to start if the WD caused reset
if (wd_reset and fast_counter < 10) {
fast_counter += 1;
} else {
blink_time += 100;
}
}
}
3 changes: 2 additions & 1 deletion port/raspberrypi/rp2xxx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ The RP2350 is very similar to the RP2040, but has enough differences to require
- [ ] spi
- [x] time
- [ ] uart
- [ ] usb
- [ ] usb
- [x] watchdog
Loading

0 comments on commit f3c9a4d

Please sign in to comment.