Rust for AVR #
AVR Studio 7 #
https://www.microchip.com/en-us/tools-resources/develop/microchip-studio
Flip writer #
https://www.microchip.com/en-us/development-tool/flip
Toolchain #
https://www.microchip.com/en-us/tools-resources/develop/microchip-studio/gcc-compilers
rust/avrd #
https://github.com/avr-rust/avrd
https://github.com/Rahix/avr-device
https://github.com/Rahix/avr-hal
Rustコード #
main.rs
#![no_std]
#![no_main]
extern crate panic_abort;
extern crate avrd;
use avrd::at90usb1287::*;
use core::ptr::write_volatile;
#[no_mangle]
pub extern fn main() {
unsafe{write_volatile(DDRD, 0xF0);}
unsafe{write_volatile(PORTD, 0x80);}
loop {
}
}
Carg.toml
[dependencies]
avrd = { version = "1.0.0", features = ["all-mcus"]}
panic-abort = "0.3.2"
[profile.release]
opt-level = 'z'
lto = true
strip = true
panic = 'abort'
config.toml
[build]
target = "avr-unknown-gnu-atmega328"
[unstable]
build-std = ["core"]
ビルド #
cargo +nightly build --release
.elfから.hexへ変換 #
avr-objcopy target/avr-unknown-gnu-atmega328/release/blink.elf -O ihex blink.hex
blink.hexをFlipで書き込む
TIMER2割り込みを有効にする #
TIMER2は8bit(256カウント)
asm!("sei");
// Timer clock: 1MHz
/* Timer clock = I/O clock / 1024 = 1kHz */
write_volatile(TCCR2B, 0x07);
/* Clear overflow flag */
write_volatile(TIFR2, 1);
/* Enable Overflow Interrupt */
write_volatile(TIMSK2, 1);
割り込み有効ハンドラー #
1kHz/256
// TIMER2 OVF Timer/Counter2 Overflow
#[no_mangle]
pub unsafe extern "avr-interrupt" fn __vector_15() {
toggle();
}
fn toggle() {
static mut CNT:u8 = 0;
unsafe {
CNT += 1;
write_volatile(PORTD, CNT<<2);
}
}
割り込み #
Interrupt vectors in AT90USB64/128
1 $0000 RESET External pin, Power-on reset, Brown-out reset, Watchdog reset, and JTAG AVR reset
2 $0002 INT0 External Interrupt Request 0
3 $0004 INT1 External Interrupt Request 1
4 $0006 INT2 External Interrupt Request 2
5 $0008 INT3 External Interrupt Request 3
6 $000A INT4 External Interrupt Request 4
7 $000C INT5 External Interrupt Request 5
8 $000E INT6 External Interrupt Request 6
9 $0010 INT7 External Interrupt Request 7
10 $0012 PCINT0 Pin Change Interrupt Request 0
11 $0014 USB General USB General Interrupt request
12 $0016 USB Endpoint/Pipe USB ENdpoint/Pipe Interrupt request
13 $0018 WDT Watchdog Time-out Interrupt
14 $001A TIMER2 COMPA Timer/Counter2 Compare Match A
15 $001C TIMER2 COMPB Timer/Counter2 Compare Match B
16 $001E TIMER2 OVF Timer/Counter2 Overflow
17 $0020 TIMER1 CAPT Timer/Counter1 Capture Event
18 $0022 TIMER1 COMPA Timer/Counter1 Compare Match A
19 $0024 TIMER1 COMPB Timer/Counter1 Compare Match B
20 $0026 TIMER1 COMPC Timer/Counter1 Compare Match C
21 $0028 TIMER1 OVF Timer/Counter1 Overflow
22 $002A TIMER0 COMPA Timer/Counter0 Compare Match A
23 $002C TIMER0 COMPB Timer/Counter0 Compare match B
24 $002E TIMER0 OVF Timer/Counter0 Overflow
25 $0030 SPI, STC SPI Serial Transfer Complete
26 $0032 USART1 RX USART1 Rx Complete
27 $0034 USART1 UDRE USART1 Data Register Empty
28 $0036 USART1TX USART1 Tx Complete
29 $0038 ANALOG COMP Analog Comparator
30 $003A ADC ADC Conversion Complete
31 $003C EE READY ADC Conversion Complete
32 $003E TIMER3 CAPT
33 $0040 TIMER3 COMPA Timer/Counter3 Compare Match A
34 $0042 TIMER3 COMPB Timer/Counter3 Compare Match B
35 $0044 TIMER3 COMPC Timer/Counter3 Compare Match C
36 $0046 TIMER3 OVF Timer/Counter3 Overflow
37 $0048 TWI 2-wire Serial Interface
38 $004A SPM READY Store Program Memory Ready