-
Notifications
You must be signed in to change notification settings - Fork 149
Add TWIS module #196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add TWIS module #196
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6766892
Add TWIS module
kalkyl 9c94e9d
Change reset and check event functions to take a TwiEvent param
kalkyl 3da7922
Merge upstream
kalkyl 0ee8e09
Update ORC doc comment
kalkyl 54f3025
Rename read/write to rx/tx, clean up twis-demo buffer init, add pin p…
kalkyl 66d209b
Remove unnecessary copy_from_slice, fix formatting
kalkyl 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 hidden or 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 hidden or 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,17 @@ | ||
| [package] | ||
| name = "twim-demo" | ||
| version = "0.1.0" | ||
| authors = ["Henrik Alsér"] | ||
| edition = "2018" | ||
|
|
||
| # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
|
||
| [dependencies] | ||
| cortex-m = "0.6.2" | ||
| cortex-m-rtic = "0.5.3" | ||
| rtt-target = {version = "0.2.0", features = ["cortex-m"] } | ||
| nrf52840-hal = { features = ["rt"], path = "../../nrf52840-hal" } | ||
|
|
||
| [dependencies.embedded-hal] | ||
| version = "0.2.3" | ||
| features = ["unproven"] |
This file contains hidden or 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,46 @@ | ||
| [default.probe] | ||
| # The index of the probe in the connected probe list. | ||
| # probe_index = 0 | ||
| # The protocol to be used for communicating with the target. | ||
| protocol = "Swd" | ||
| # The speed in kHz of the data link to the target. | ||
| # speed = 1337 | ||
|
|
||
| [default.flashing] | ||
| # Whether or not the target should be flashed. | ||
| enabled = true | ||
| # Whether or not the target should be halted after flashing. | ||
| halt_afterwards = false | ||
| # Whether or not bytes erased but not rewritten with data from the ELF | ||
| # should be restored with their contents before erasing. | ||
| restore_unwritten_bytes = false | ||
| # The path where an SVG of the assembled flash layout should be written to. | ||
| # flash_layout_output_path = "out.svg" | ||
|
|
||
| [default.general] | ||
| # The chip name of the chip to be debugged. | ||
| chip = "nRF52840" | ||
| # A list of chip descriptions to be loaded during runtime. | ||
| chip_descriptions = [] | ||
| # The default log level to be used. | ||
| log_level = "Warn" | ||
|
|
||
| [default.rtt] | ||
| # Whether or not an RTTUI should be opened after flashing. | ||
| # This is exclusive and cannot be used with GDB at the moment. | ||
| enabled = true | ||
| # A list of channel associations to be displayed. If left empty, all channels are displayed. | ||
| channels = [ | ||
| # { up = 0, down = 0, name = "name" } | ||
| ] | ||
| # The duration in ms for which the logger should retry to attach to RTT. | ||
| timeout = 3000 | ||
| # Whether timestamps in the RTTUI are enabled | ||
| show_timestamps = true | ||
|
|
||
| [default.gdb] | ||
| # Whether or not a GDB server should be opened after flashing. | ||
| # This is exclusive and cannot be used with RTT at the moment. | ||
| enabled = false | ||
| # The connection string in host:port format wher the GDB server will open a socket. | ||
| # gdb_connection_string |
This file contains hidden or 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,127 @@ | ||
| #![no_std] | ||
| #![no_main] | ||
|
|
||
| use embedded_hal::digital::v2::InputPin; | ||
| use { | ||
| core::{ | ||
| panic::PanicInfo, | ||
| sync::atomic::{compiler_fence, Ordering}, | ||
| }, | ||
| hal::{ | ||
| gpio::{p0::Parts, Input, Pin, PullUp}, | ||
| gpiote::Gpiote, | ||
| pac::TWIM0, | ||
| twim::*, | ||
| }, | ||
| nrf52840_hal as hal, | ||
| rtic::cyccnt::U32Ext, | ||
| rtt_target::{rprintln, rtt_init_print}, | ||
| }; | ||
|
|
||
| #[rtic::app(device = crate::hal::pac, peripherals = true, monotonic = rtic::cyccnt::CYCCNT)] | ||
| const APP: () = { | ||
| struct Resources { | ||
| twim: Twim<TWIM0>, | ||
| gpiote: Gpiote, | ||
| btn1: Pin<Input<PullUp>>, | ||
| btn2: Pin<Input<PullUp>>, | ||
| btn3: Pin<Input<PullUp>>, | ||
| btn4: Pin<Input<PullUp>>, | ||
| } | ||
|
|
||
| #[init] | ||
| fn init(mut ctx: init::Context) -> init::LateResources { | ||
| let _clocks = hal::clocks::Clocks::new(ctx.device.CLOCK).enable_ext_hfosc(); | ||
| ctx.core.DCB.enable_trace(); | ||
| ctx.core.DWT.enable_cycle_counter(); | ||
| rtt_init_print!(); | ||
|
|
||
| let p0 = Parts::new(ctx.device.P0); | ||
| let scl = p0.p0_30.into_floating_input().degrade(); | ||
| let sda = p0.p0_31.into_floating_input().degrade(); | ||
| let btn1 = p0.p0_11.into_pullup_input().degrade(); | ||
| let btn2 = p0.p0_12.into_pullup_input().degrade(); | ||
| let btn3 = p0.p0_24.into_pullup_input().degrade(); | ||
| let btn4 = p0.p0_25.into_pullup_input().degrade(); | ||
|
|
||
| let gpiote = Gpiote::new(ctx.device.GPIOTE); | ||
| gpiote.port().input_pin(&btn1).low(); | ||
| gpiote.port().input_pin(&btn2).low(); | ||
| gpiote.port().input_pin(&btn3).low(); | ||
| gpiote.port().input_pin(&btn4).low(); | ||
| gpiote.port().enable_interrupt(); | ||
|
|
||
| let twim = Twim::new(ctx.device.TWIM0, Pins { scl, sda }, Frequency::K100); | ||
|
|
||
| init::LateResources { | ||
| twim, | ||
| gpiote, | ||
| btn1, | ||
| btn2, | ||
| btn3, | ||
| btn4, | ||
| } | ||
| } | ||
|
|
||
| #[idle] | ||
| fn idle(_: idle::Context) -> ! { | ||
| rprintln!("Press button 1 to READ from addr 0x1A"); | ||
| rprintln!("Press button 2 to WRITE to addr 0x1A"); | ||
| rprintln!("Press button 3 to READ from addr 0x1B"); | ||
| rprintln!("Press button 4 to WRITE from addr 0x1B"); | ||
| loop { | ||
| cortex_m::asm::wfi(); | ||
| } | ||
| } | ||
|
|
||
| #[task(binds = GPIOTE, resources = [gpiote], schedule = [debounce])] | ||
| fn on_gpiote(ctx: on_gpiote::Context) { | ||
| ctx.resources.gpiote.reset_events(); | ||
| ctx.schedule.debounce(ctx.start + 3_000_000.cycles()).ok(); | ||
| } | ||
|
|
||
| #[task(resources = [twim, gpiote, btn1, btn2, btn3, btn4])] | ||
| fn debounce(ctx: debounce::Context) { | ||
| let twim = ctx.resources.twim; | ||
| if ctx.resources.btn1.is_low().unwrap() { | ||
| rprintln!("\nREAD from address 0x1A"); | ||
| let rx_buf = &mut [0; 8][..]; | ||
| let res = twim.read(0x1A, rx_buf); | ||
| rprintln!("Result: {:?}\n{:?}", res, rx_buf); | ||
| } | ||
| if ctx.resources.btn2.is_low().unwrap() { | ||
| rprintln!("\nWRITE to address 0x1A"); | ||
| let mut tx_buf = [0u8; 8]; | ||
| tx_buf.copy_from_slice(&[1, 2, 3, 4, 5, 6, 7, 8]); | ||
|
kalkyl marked this conversation as resolved.
Outdated
|
||
| let res = twim.write(0x1A, &tx_buf[..]); | ||
| rprintln!("Result: {:?}\n{:?}", res, tx_buf); | ||
| } | ||
| if ctx.resources.btn3.is_low().unwrap() { | ||
| rprintln!("\nREAD from address 0x1B"); | ||
| let rx_buf = &mut [0; 4][..]; | ||
| let res = twim.read(0x1B, rx_buf); | ||
| rprintln!("Result: {:?}\n{:?}", res, rx_buf); | ||
| } | ||
| if ctx.resources.btn4.is_low().unwrap() { | ||
| rprintln!("\nWRITE to address 0x1B"); | ||
| let mut tx_buf = [0u8; 4]; | ||
| tx_buf.copy_from_slice(&[9, 10, 11, 12]); | ||
|
kalkyl marked this conversation as resolved.
Outdated
|
||
| let res = twim.write(0x1B, &tx_buf[..]); | ||
| rprintln!("Result: {:?}\n{:?}", res, tx_buf); | ||
| } | ||
| } | ||
|
|
||
| extern "C" { | ||
| fn SWI0_EGU0(); | ||
| } | ||
| }; | ||
|
|
||
| #[inline(never)] | ||
| #[panic_handler] | ||
| fn panic(info: &PanicInfo) -> ! { | ||
| cortex_m::interrupt::disable(); | ||
| rprintln!("{}", info); | ||
| loop { | ||
| compiler_fence(Ordering::SeqCst); | ||
| } | ||
| } | ||
This file contains hidden or 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,17 @@ | ||
| [package] | ||
| name = "twis-demo" | ||
| version = "0.1.0" | ||
| authors = ["Henrik Alsér"] | ||
| edition = "2018" | ||
|
|
||
| # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
|
||
| [dependencies] | ||
| cortex-m = "0.6.2" | ||
| cortex-m-rtic = "0.5.3" | ||
| rtt-target = {version = "0.2.0", features = ["cortex-m"] } | ||
| nrf52840-hal = { features = ["rt"], path = "../../nrf52840-hal" } | ||
|
|
||
| [dependencies.embedded-hal] | ||
| version = "0.2.3" | ||
| features = ["unproven"] |
This file contains hidden or 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,46 @@ | ||
| [default.probe] | ||
| # The index of the probe in the connected probe list. | ||
| # probe_index = 0 | ||
| # The protocol to be used for communicating with the target. | ||
| protocol = "Swd" | ||
| # The speed in kHz of the data link to the target. | ||
| # speed = 1337 | ||
|
|
||
| [default.flashing] | ||
| # Whether or not the target should be flashed. | ||
| enabled = true | ||
| # Whether or not the target should be halted after flashing. | ||
| halt_afterwards = false | ||
| # Whether or not bytes erased but not rewritten with data from the ELF | ||
| # should be restored with their contents before erasing. | ||
| restore_unwritten_bytes = false | ||
| # The path where an SVG of the assembled flash layout should be written to. | ||
| # flash_layout_output_path = "out.svg" | ||
|
|
||
| [default.general] | ||
| # The chip name of the chip to be debugged. | ||
| chip = "nRF52840" | ||
| # A list of chip descriptions to be loaded during runtime. | ||
| chip_descriptions = [] | ||
| # The default log level to be used. | ||
| log_level = "Warn" | ||
|
|
||
| [default.rtt] | ||
| # Whether or not an RTTUI should be opened after flashing. | ||
| # This is exclusive and cannot be used with GDB at the moment. | ||
| enabled = true | ||
| # A list of channel associations to be displayed. If left empty, all channels are displayed. | ||
| channels = [ | ||
| # { up = 0, down = 0, name = "name" } | ||
| ] | ||
| # The duration in ms for which the logger should retry to attach to RTT. | ||
| timeout = 3000 | ||
| # Whether timestamps in the RTTUI are enabled | ||
| show_timestamps = true | ||
|
|
||
| [default.gdb] | ||
| # Whether or not a GDB server should be opened after flashing. | ||
| # This is exclusive and cannot be used with RTT at the moment. | ||
| enabled = false | ||
| # The connection string in host:port format wher the GDB server will open a socket. | ||
| # gdb_connection_string |
This file contains hidden or 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,120 @@ | ||
| #![no_std] | ||
| #![no_main] | ||
|
|
||
| use cortex_m::singleton; | ||
| use embedded_hal::digital::v2::OutputPin; | ||
| use { | ||
| core::{ | ||
| panic::PanicInfo, | ||
| sync::atomic::{compiler_fence, Ordering}, | ||
| }, | ||
| hal::{ | ||
| gpio::{p0::Parts, Level, Output, Pin, PushPull}, | ||
| pac::TWIS0, | ||
| twis::*, | ||
| }, | ||
| nrf52840_hal as hal, | ||
| rtt_target::{rprintln, rtt_init_print}, | ||
| }; | ||
|
|
||
| const ADDR0: u8 = 0x1A; | ||
| const ADDR1: u8 = 0x1B; | ||
| const BUF0_SZ: usize = 8; | ||
| const BUF1_SZ: usize = 4; | ||
|
|
||
| #[rtic::app(device = crate::hal::pac, peripherals = true)] | ||
| const APP: () = { | ||
| struct Resources { | ||
| twis: Twis<TWIS0>, | ||
| buffer0: &'static mut [u8; BUF0_SZ], | ||
| buffer1: &'static mut [u8; BUF1_SZ], | ||
| led: Pin<Output<PushPull>>, | ||
| } | ||
|
|
||
| #[init] | ||
| fn init(ctx: init::Context) -> init::LateResources { | ||
| let _clocks = hal::clocks::Clocks::new(ctx.device.CLOCK).enable_ext_hfosc(); | ||
| rtt_init_print!(); | ||
|
|
||
| let p0 = Parts::new(ctx.device.P0); | ||
| let led = p0.p0_06.into_push_pull_output(Level::High).degrade(); | ||
| let scl = p0.p0_14.into_floating_input().degrade(); | ||
| let sda = p0.p0_16.into_floating_input().degrade(); | ||
|
|
||
| let twis = Twis::new(ctx.device.TWIS0, Pins { scl, sda }, ADDR0); | ||
| twis.address1(ADDR1) // Add a secondary i2c address | ||
| .enable_interrupt(TwiEvent::Write) // Trigger interrupt on WRITE command | ||
| .enable_interrupt(TwiEvent::Read) // Trigger interrupt on READ command | ||
| .enable(); | ||
|
|
||
| let buffer0 = singleton!(: [u8; BUF0_SZ] = [0; BUF0_SZ]).unwrap(); | ||
| let buffer1 = singleton!(: [u8; BUF1_SZ] = [0; BUF1_SZ]).unwrap(); | ||
|
kalkyl marked this conversation as resolved.
Outdated
|
||
| init::LateResources { | ||
| twis, | ||
| buffer0, | ||
| buffer1, | ||
| led, | ||
| } | ||
| } | ||
|
|
||
| #[idle] | ||
| fn idle(_: idle::Context) -> ! { | ||
| rprintln!("Waiting for commands from controller..."); | ||
| loop { | ||
| cortex_m::asm::wfi(); | ||
| } | ||
| } | ||
|
|
||
| #[task(binds = SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0, resources = [twis, buffer0, buffer1, led])] | ||
| fn on_twis(ctx: on_twis::Context) { | ||
| let twis = ctx.resources.twis; | ||
| let buffer0 = ctx.resources.buffer0; | ||
| let buffer1 = ctx.resources.buffer1; | ||
| let led = ctx.resources.led; | ||
|
|
||
| if twis.is_event_triggered(TwiEvent::Read) { | ||
| twis.reset_event(TwiEvent::Read); | ||
| led.set_low().ok(); | ||
| rprintln!("\nREAD cmd received on addr 0x{:x}", twis.address_match()); | ||
| rprintln!("Writing data to controller..."); | ||
| match twis.address_match() { | ||
| ADDR0 => { | ||
| let res = twis.write(&buffer0[..]); | ||
| rprintln!("Result: {:?}\n{:?}", res, buffer0); | ||
| } | ||
| ADDR1 => { | ||
| let res = twis.write(&buffer1[..]); | ||
| rprintln!("Result: {:?}\n{:?}", res, buffer1); | ||
| } | ||
| _ => unreachable!(), | ||
| } | ||
| } | ||
| if twis.is_event_triggered(TwiEvent::Write) { | ||
| twis.reset_event(TwiEvent::Write); | ||
| led.set_high().ok(); | ||
| rprintln!("\nWRITE cmd received on addr 0x{:x}", twis.address_match()); | ||
| rprintln!("Reading data from controller..."); | ||
| match twis.address_match() { | ||
| ADDR0 => { | ||
| let res = twis.read(&mut buffer0[..]); | ||
| rprintln!("Result: {:?}\n{:?}", res, buffer0); | ||
| } | ||
| ADDR1 => { | ||
| let res = twis.read(&mut buffer1[..]); | ||
| rprintln!("Result: {:?}\n{:?}", res, buffer1); | ||
| } | ||
| _ => unreachable!(), | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| #[inline(never)] | ||
| #[panic_handler] | ||
| fn panic(info: &PanicInfo) -> ! { | ||
| cortex_m::interrupt::disable(); | ||
| rprintln!("{}", info); | ||
| loop { | ||
| compiler_fence(Ordering::SeqCst); | ||
| } | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.