Skip to content

Instantly share code, notes, and snippets.

@gergoerdi
Created July 25, 2017 12:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gergoerdi/c609e839d4ffea5a9ca73787026f7e4c to your computer and use it in GitHub Desktop.
Save gergoerdi/c609e839d4ffea5a9ca73787026f7e4c to your computer and use it in GitHub Desktop.
#![feature(lang_items)]
#![feature(fundamental)]
#![feature(intrinsics)]
#![feature(on_unimplemented)]
#![feature(optin_builtin_traits)]
#![feature(unboxed_closures)]
#![feature(associated_type_defaults)]
#![feature(asm)]
#![feature(abi_avr_interrupt)]
#![feature(unwind_attributes)]
#![no_std]
#![no_main]
#![feature(never_type)]
#![feature(rustc_attrs)]
#![feature(core_intrinsics)]
#![feature(prelude_import)]
#![feature(staged_api)]
#![allow(unused_variables)]
#![allow(dead_code)]
#![allow(unused_imports)]
#![allow(unused_unsafe)]
pub mod std {
#[lang = "eh_personality"]
#[no_mangle]
pub unsafe extern "C" fn rust_eh_personality(state: (), exception_object: *mut (), context: *mut ()) -> () {
}
#[lang = "panic_fmt"]
#[unwind]
pub extern fn rust_begin_panic(msg: (), file: &'static str, line: u32) -> ! {
loop{}
}
}
mod avr;
use avr::attiny13a::*;
pub mod timer;
use timer::*;
use core::intrinsics::{volatile_load, volatile_store};
const OUT : u8 = DDB1;
const MANCHESTER_DELAY: u16 = 500;
unsafe fn out_low() {
volatile_store(PORTB, volatile_load(PORTB) & !OUT);
}
unsafe fn out_high() {
volatile_store(PORTB, volatile_load(PORTB) | OUT);
}
unsafe fn manchester_out(bit: bool) {
if bit {
out_high();
delay_us(MANCHESTER_DELAY);
out_low();
delay_us(MANCHESTER_DELAY);
} else {
out_low();
delay_us(MANCHESTER_DELAY);
out_high();
delay_us(MANCHESTER_DELAY);
}
}
#[no_mangle]
pub unsafe extern fn main() {
volatile_store(ADMUX, MUX1); // ADC2 (PB4)
// enable ADC with freq 9.6 MHz / 8 (CKDIV8) / 16 (ADPS) = 75 kHz
volatile_store(ADCSRA, ADPS2 | ADEN);
// Set as output.
volatile_store(DDRB, OUT);
out_low();
loop {
// Start a conversion and wait for it to finish.
volatile_store(ADCSRA, volatile_load(ADCSRA) | ADSC);
while volatile_load(ADCSRA) & ADSC != 0 {}
// Read the ADC result.
let mut reading = volatile_load(ADCL) as u16 |
((volatile_load(ADCH) as u16) << 8) |
0xb400; // Add a recognizable signature
// Ouput the value in Manchester code with a leading '1' bit preamble.
manchester_out(true);
for _ in 1..16 {
manchester_out(reading & 1 != 0);
reading >>= 1;
}
// Restore the data line to the 'low' state
out_low();
delay_ms(100);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment