Skip to content

Commit

Permalink
final v1
Browse files Browse the repository at this point in the history
  • Loading branch information
Grazfather committed Feb 17, 2025
1 parent 83a8d9d commit 04c7b61
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
17 changes: 6 additions & 11 deletions examples/raspberrypi/rp2xxx/src/spi_master.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,24 @@ const SCK_PIN = 18;
// NOTE: rp2xxx doesn't label pins as MOSI/MISO. Instead a pin is always for
// either receiving or transmitting SPI data, no matter whether the chip is in
// master or slave mode.
const RX_PIN = 16;
const TX_PIN = 19;

// Communicate with another RP2040 over spi
pub fn main() !void {
// Set pin functions for CS, SCK, RX
const csn = rp2xxx.gpio.num(CS_PIN);
const miso = rp2xxx.gpio.num(RX_PIN);
const mosi = rp2xxx.gpio.num(TX_PIN);
const sck = rp2xxx.gpio.num(SCK_PIN);
inline for (&.{ csn, miso, sck }) |pin| {
inline for (&.{ csn, mosi, sck }) |pin| {
pin.set_function(.spi);
}

// 8 bit data words
try spi.apply(.{
.clock_config = rp2xxx.clock_config,
.data_width = .eight,
.baud_rate = 500_000,
});
var out_buf_eight: [BUF_LEN]u8 = .{ 'h', 'e', 'l', 'o' } ** (BUF_LEN / 4);
try spi.apply(.{ .clock_config = rp2xxx.clock_config });
var out_buf: [BUF_LEN]u8 = .{ 'h', 'e', 'y', ' ', 'y', 'o', 'u', '!' } ** (BUF_LEN / 8);

while (true) {
std.log.info("Sending some data\n", .{});
spi.write_blocking(u8, &out_buf_eight);
spi.write_blocking(u8, &out_buf);
time.sleep_ms(1 * 1000);
}
}
12 changes: 5 additions & 7 deletions examples/raspberrypi/rp2xxx/src/spi_slave.zig
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,14 @@ pub fn main() !void {
std.log.info("Setting SPI as slave device", .{});
spi.set_slave(true);

spi.apply(.{
.clock_config = rp2xxx.clock_config,
.data_width = .eight,
}) catch {};
var in_buf_eight: [BUF_LEN]u8 = undefined;
try spi.apply(.{ .clock_config = rp2xxx.clock_config });
var in_buf: [BUF_LEN]u8 = undefined;

std.log.info("Reading", .{});

while (true) {
spi.read_blocking(u8, 0, &in_buf_eight);
std.log.info("Got: {s}", .{in_buf_eight});
spi.read_blocking(u8, 0, &in_buf);
std.log.info("Got: {s}", .{in_buf});
time.sleep_ms(1 * 1000);
}
}

0 comments on commit 04c7b61

Please sign in to comment.