Skip to content

Commit

Permalink
add uart example (#266)
Browse files Browse the repository at this point in the history
Co-authored-by: RecursiveError <[email protected]>
Co-authored-by: Felix Queißner <[email protected]>
  • Loading branch information
3 people authored Oct 27, 2024
1 parent 03a313d commit a7bf277
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
3 changes: 2 additions & 1 deletion examples/raspberrypi/rp2xxx/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const rp2040_only_examples = [_]Example{
.{ .target = rp2xxx.boards.raspberrypi.pico, .name = "pico_random", .file = "src/rp2040_only/random.zig" },
.{ .target = rp2xxx.boards.raspberrypi.pico, .name = "pico_spi-host", .file = "src/rp2040_only/spi_host.zig" },
.{ .target = rp2xxx.boards.raspberrypi.pico, .name = "pico_squarewave", .file = "src/rp2040_only/squarewave.zig" },
.{ .target = rp2xxx.boards.raspberrypi.pico, .name = "pico_uart", .file = "src/rp2040_only/uart.zig" },
.{ .target = rp2xxx.boards.raspberrypi.pico, .name = "pico_uart_echo", .file = "src/rp2040_only/uart_echo.zig" },
.{ .target = rp2xxx.boards.raspberrypi.pico, .name = "pico_uart_log", .file = "src/rp2040_only/uart_log.zig" },
.{ .target = rp2xxx.boards.raspberrypi.pico, .name = "pico_usb-hid", .file = "src/rp2040_only/usb_hid.zig" },
.{ .target = rp2xxx.boards.raspberrypi.pico, .name = "pico_usb-cdc", .file = "src/rp2040_only/usb_cdc.zig" },
.{ .target = rp2xxx.boards.raspberrypi.pico, .name = "pico_ws2812", .file = "src/rp2040_only/ws2812.zig" },
Expand Down
41 changes: 41 additions & 0 deletions examples/raspberrypi/rp2xxx/src/rp2040_only/uart_echo.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const std = @import("std");
const microzig = @import("microzig");

const rp2040 = microzig.hal;
const uart = rp2040.uart;
const gpio = rp2040.gpio;
const time = rp2040.time;
const clocks = rp2040.clocks;

const echo_uart = uart.instance.num(0);
const baud_rate = 115200;
const uart_tx_pin = gpio.num(0);
const uart_rx_pin = gpio.num(1);

pub fn main() !void {
inline for (&.{ uart_tx_pin, uart_rx_pin }) |pin| {
pin.set_function(.uart);
}

echo_uart.apply(.{
.baud_rate = baud_rate,
.clock_config = rp2040.clock_config,
//.parity = .none,
//.stop_bits = .one,
//.flow_control = .none
});

var data: [1]u8 = .{0};
while (true) {
//read one byte, timeout disabled
echo_uart.read_blocking(&data, null) catch {
echo_uart.clear_errors(); //You need to clear UART errors before making a new transaction
continue;
};

//tries to write one byte with 100ms timeout
echo_uart.write_blocking(&data, time.Duration.from_ms(100)) catch {
echo_uart.clear_errors();
};
}
}

0 comments on commit a7bf277

Please sign in to comment.