-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from RossPorter506/master
Add I2C, SPI, and ADC functionality, Take 2
- Loading branch information
Showing
25 changed files
with
2,749 additions
and
249 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#![no_main] | ||
#![no_std] | ||
|
||
use embedded_hal::digital::v2::*; | ||
use msp430_rt::entry; | ||
use msp430fr2x5x_hal::{ | ||
adc::{AdcConfig, ClockDivider, Predivider, Resolution, SampleTime, SamplingRate}, | ||
gpio::Batch, | ||
pmm::Pmm, | ||
watchdog::Wdt, | ||
}; | ||
use nb::block; | ||
use panic_msp430 as _; | ||
|
||
// If pin 1.1 is between 1V and 2V, the LED on pin 1.0 should light up. | ||
#[entry] | ||
fn main() -> ! { | ||
// Take peripherals and disable watchdog | ||
let periph = msp430fr2355::Peripherals::take().unwrap(); | ||
let _wdt = Wdt::constrain(periph.WDT_A); | ||
|
||
// Configure GPIO | ||
let pmm = Pmm::new(periph.PMM); | ||
let port1 = Batch::new(periph.P1).split(&pmm); | ||
let mut led = port1.pin0.to_output(); | ||
let mut adc_pin = port1.pin1.to_alternate3(); | ||
|
||
// ADC setup | ||
let mut adc = AdcConfig::new( | ||
ClockDivider::_1, | ||
Predivider::_1, | ||
Resolution::_8BIT, | ||
SamplingRate::_50KSPS, | ||
SampleTime::_4, | ||
) | ||
.use_modclk() | ||
.configure(periph.ADC); | ||
|
||
loop { | ||
// Get ADC voltage, assuming the ADC reference voltage is 3300mV | ||
// It's infallible besides nb::WouldBlock, so it's safe to unwrap after block!() | ||
// If you want a raw count use adc.read() instead. | ||
let reading_mv = block!( adc.read_voltage_mv(&mut adc_pin, 3300) ).unwrap(); | ||
|
||
// Turn on LED if voltage between 1000 and 2000mV | ||
if (1000..=2000).contains(&reading_mv) { | ||
led.set_high().ok(); | ||
} else { | ||
led.set_low().ok(); | ||
} | ||
} | ||
} | ||
|
||
// The compiler will emit calls to the abort() compiler intrinsic if debug assertions are | ||
// enabled (default for dev profile). MSP430 does not actually have meaningful abort() support | ||
// so for now, we create our own in each application where debug assertions are present. | ||
#[no_mangle] | ||
extern "C" fn abort() -> ! { | ||
panic!(); | ||
} |
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
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
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,41 @@ | ||
#![no_main] | ||
#![no_std] | ||
|
||
use embedded_hal::digital::v2::*; | ||
use msp430_rt::entry; | ||
use msp430fr2x5x_hal::{gpio::Batch, pmm::Pmm, watchdog::Wdt}; | ||
use panic_msp430 as _; | ||
|
||
// Green onboard LED should go on when P2.3 button is pressed | ||
#[entry] | ||
fn main() -> ! { | ||
let periph = msp430fr2355::Peripherals::take().unwrap(); | ||
let _wdt = Wdt::constrain(periph.WDT_A); | ||
|
||
let pmm = Pmm::new(periph.PMM); | ||
let p2 = Batch::new(periph.P2) | ||
.config_pin3(|p| p.pullup()) | ||
.split(&pmm); | ||
let p6 = Batch::new(periph.P6) | ||
.config_pin6(|p| p.to_output()) | ||
.split(&pmm); | ||
|
||
let p2_3 = p2.pin3; | ||
let mut p6_6 = p6.pin6; | ||
|
||
loop { | ||
if p2_3.is_high().unwrap() { | ||
p6_6.set_low().ok(); | ||
} else { | ||
p6_6.set_high().ok(); | ||
} | ||
} | ||
} | ||
|
||
// The compiler will emit calls to the abort() compiler intrinsic if debug assertions are | ||
// enabled (default for dev profile). MSP430 does not actually have meaningful abort() support | ||
// so for now, we create our own in each application where debug assertions are present. | ||
#[no_mangle] | ||
extern "C" fn abort() -> ! { | ||
panic!(); | ||
} |
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
Oops, something went wrong.