This repository has been archived by the owner on Feb 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
RaspberryPi Pico W support #69
Draft
vesim987
wants to merge
5
commits into
main
Choose a base branch
from
pico_w
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ae763df
Add set_schmitt, set_drive_strength, set_slew_rate to gpio.Pin
vesim987 1a67566
Add set_input_sync_bypass to pio.Pio
vesim987 647711d
Add read fifo funuctions to pio.Pio
vesim987 c6150de
Add helper functions to pio.Pio
vesim987 769776d
Add SPI bitbanged using PIO for cyw43
vesim987 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub const PioSpi = @import("cyw43/PioSpi.zig").PioSpi; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
const std = @import("std"); | ||
const rp2040 = @import("microzig").hal; | ||
const gpio = rp2040.gpio; | ||
const Pio = rp2040.pio.Pio; | ||
const StateMachine = rp2040.pio.StateMachine; | ||
|
||
pub const PioSpi = @This(); | ||
|
||
cs: gpio.Pin, | ||
pio: Pio, | ||
sm: StateMachine, | ||
|
||
const logger = std.log.scoped(.pio_spi); | ||
|
||
const program = blk: { | ||
@setEvalBranchQuota(5000); | ||
break :blk rp2040.pio.assemble( | ||
\\ .program prog | ||
\\ .side_set 1 | ||
\\ .wrap_target | ||
\\ write_begin: | ||
\\ out pins, 1 side 0 | ||
\\ jmp x-- write_begin side 1 | ||
\\ set pindirs, 0 side 0 | ||
\\ nop side 0 | ||
\\ read_begin: | ||
\\ in pins, 1 side 1 | ||
\\ jmp y-- read_begin side 0 | ||
\\ wait 1 pin 0 side 0 | ||
\\ irq 0 side 0 | ||
\\ .wrap | ||
, .{}).get_program_by_name("prog"); | ||
}; | ||
|
||
pub fn init(pio: Pio, sm: StateMachine, cs: gpio.Pin, dio: gpio.Pin, clk: gpio.Pin) !PioSpi { | ||
pio.gpio_init(dio); | ||
pio.set_input_sync_bypass(dio, true); | ||
dio.set_schmitt(true); | ||
dio.set_pull(null); | ||
dio.set_drive_strength(.@"12mA"); | ||
dio.set_slew_rate(.fast); | ||
|
||
pio.gpio_init(clk); | ||
clk.set_drive_strength(.@"12mA"); | ||
clk.set_slew_rate(.fast); | ||
|
||
const offset = try pio.add_program(program); | ||
std.debug.assert(offset == 0); | ||
|
||
pio.sm_init(sm, offset, .{ | ||
.clkdiv = .{ .int = 5 }, | ||
.shift = .{ | ||
.autopush = true, | ||
.autopull = true, | ||
.in_shiftdir = .left, | ||
.out_shiftdir = .left, | ||
}, | ||
.pin_mappings = .{ | ||
.out = .{ | ||
.base = @intFromEnum(dio), | ||
.count = 1, | ||
}, | ||
.set = .{ | ||
.base = @intFromEnum(dio), | ||
.count = 1, | ||
}, | ||
.side_set = .{ | ||
.base = @intFromEnum(clk), | ||
.count = 1, | ||
}, | ||
.in_base = @intFromEnum(dio), | ||
}, | ||
.exec = .{ | ||
.wrap = program.wrap.?, | ||
.wrap_target = program.wrap_target.?, | ||
.side_set_optional = program.side_set.?.optional, | ||
.side_pindir = program.side_set.?.pindir, | ||
}, | ||
}); | ||
|
||
pio.sm_set_pindirs(sm, &.{ clk, dio }, .out); | ||
pio.sm_set_pins(sm, &.{ clk, dio }, 0); | ||
|
||
pio.sm_set_enabled(sm, true); | ||
|
||
return .{ | ||
.cs = cs, | ||
.pio = pio, | ||
.sm = sm, | ||
}; | ||
} | ||
|
||
pub fn read(self: *PioSpi, out: []const u32, in: []u32) !void { | ||
self.cs.put(0); | ||
defer self.cs.put(1); | ||
|
||
const write_bits = out.len - 1; | ||
const read_bits = in.len * 32 + 32 - 1; | ||
|
||
logger.debug("write={}({}) read={}({})", .{ | ||
std.fmt.fmtSliceHexLower(std.mem.sliceAsBytes(out)), | ||
write_bits, | ||
std.fmt.fmtSliceHexLower(std.mem.sliceAsBytes(in)), | ||
read_bits, | ||
}); | ||
|
||
self.pio.sm_set_enabled(self.sm, false); | ||
|
||
self.pio.sm_set_pindir(self.sm, .out); | ||
self.pio.sm_set_x(self.sm, write_bits); | ||
self.pio.sm_set_y(self.sm, read_bits); | ||
self.pio.sm_jmp(self.sm, program.wrap_target.?); | ||
|
||
self.pio.sm_set_enabled(self.sm, true); | ||
|
||
for (out) |v| { | ||
self.pio.sm_blocking_write(self.sm, v); | ||
} | ||
|
||
for (in) |*v| { | ||
v.* = self.pio.sm_blocking_read(self.sm); | ||
} | ||
|
||
const status = self.pio.sm_blocking_read(self.sm); | ||
|
||
logger.debug("status = {}", .{status}); | ||
} | ||
|
||
pub fn write(self: *PioSpi, out: []const u32) !void { | ||
self.cs.put(0); | ||
|
||
const write_bits = out.len * 32 - 1; | ||
const read_bits = 32 - 1; | ||
|
||
logger.debug("write={}({})", .{ | ||
std.fmt.fmtSliceHexLower(std.mem.sliceAsBytes(out)), | ||
write_bits, | ||
}); | ||
|
||
self.pio.sm_set_enabled(self.sm, false); | ||
|
||
self.pio.sm_set_pindir(self.sm, .out); | ||
self.pio.sm_set_x(self.sm, write_bits); | ||
self.pio.sm_set_y(self.sm, read_bits); | ||
self.pio.sm_jmp(self.sm, program.wrap_target.?); | ||
|
||
self.pio.sm_set_enabled(self.sm, true); | ||
|
||
for (out) |v| { | ||
self.pio.sm_blocking_write(self.sm, v); | ||
} | ||
const status = self.pio.sm_blocking_read(self.sm); | ||
|
||
logger.debug("status = {}", .{status}); | ||
self.cs.put(1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you're going to do this, you might as well do an
@intFromEnum()
and assign it to raw.