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**

  * [avr_io](https://github.com/Abathargh/avr_io)
  * [examples](https://github.com/Abathargh/avr_io/tree/master/examples)
  * [documentation](https://github.com/Abathargh/avr_io/wiki)



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()
    
    
    
    Run

Reply via email to