Hi everyone! This is a simple library I wrote, that provides registers mappings, interrupt service routine facilities, progmem and peripheral support for AVR chips in nim. For now it is limited to a certain subset of chips from the ATMega family (mainly what I could get my hands on), and the small peripheral abstraction layer supports USARTs only.
Any feedback is highly appreciated!
Links
One year ago I started learning nim, and when I realized how it worked in relation to the C codegen, I immediately thought it could be nice to use it for writing firmware for embedded targets. I have been working on and off on this for a while, and right now I reached a version that I feel confident to release: I have successfully ported a couple of personal projects to nim thanks through this library.
A simple application targeting an Arduino Uno, showcasing many of the currently implemented functionalities:
import avr_io
import volatile
progmem(on_state, "new state: true\n")
progmem(off_state, "new state: false\n")
const builtinLed = 5'u8
var state = false
proc initTimer0() =
OCR0A[] = 250
TCCR0A.setBit(1)
TCCR0B.setBit(2)
TIMSK0.setBit(1)
proc timer0_compa_isr() {.isr(Timer0CompAVect).} =
volatileStore(addr state, not volatileLoad(addr state))
proc loop() =
const baud = baudRate(9600'u32)
usart0.initUart(baud, {}, {txen}, {ucsz1, ucsz0})
portB.asOutputPin(builtinLed)
initTimer0()
sei()
var lastState = state
while true:
let curr = volatileLoad(addr state)
if lastState != curr:
portB.togglePin(builtinLed)
lastState = curr
if lastState:
usart0.sendString(on_state[])
else:
usart0.sendString(off_state[])
when isMainModule:
loop()
Hi, I did not know about that, it seems like a wonderful project I will for sure take a look at it in more detail! Am i right in understanding it is geared towards programming on let's say, boards more than specific microcontrollers in general?
What were you interested in, that it's implemented in avr_io?
Well currently the design is board-oriented. But I think it should maybe have another level of abstraction in-between to make it easier to define boards using the same microcontroller.
I was primarily looking at the USART, MappedIO, and interrupts, but of course adding the list of chips would be nice as well :) Not sure how much inspiration I can take though before I have to modify the MIT license I use for Ratel to include your license.