Setting up your project - Abathargh/avr_io GitHub Wiki

There are some steps that must be followed in order to set up a project for an AVR microcontroller:

  • The cpu compiler option must be set to avr
  • The os compiler option must be set to standalone.
  • A panicoverride.nim must be provided, implementing some basic procedures necessary to compile for the standalone target.

Optionally, you may want to:

  • Pass none as the gc option, if you do not want to deal with dynamic memory.
  • Otherwise --gc:arc, --gc:orc and -d:useMalloc are not a bad choice.

using avrman

avrman (avr manager) is a tool you can use to setup nim and c (make or cmake based) avr projects. You can install via:

nimble install avrman

And just use it as follows:

$ avrman init --help
Initializes an avr project.

    avrman init [options] PROJECT_NAME

Options:
  -f, --fcpu        specifies the selected frequency
  -m, --mcu         specifies the microcontroller part number
  -d, --device      specifies the device used to program the microcontroller
  -l, --license     specifies the license for the nimble project, ignored in
                    combination with --cproject
  -p, --programmer  specifies the programmer to use
  -P, --port        specifies the port to use to program the device
  -s, --supported   prints a list of supported microcontroller part numbers
  -h, --help        shows this help message
  --cproject        initializes a C project instead of a nim one
  --cmake           uses CMake instead of plain make; checked only if using 
                    --cproject

For example, the following command will initialize a project for an ATMega644 running at 8KHz, using atmelice as its programmer:

avrman init -m:atmega644 -f:8000000 -p:"atmelice" test644

Note that:

  • The m/mcu option is required. A complete list of supported microcontrollers can be obtained with the -s flag.
  • The frequency defaults to 16MHz if not specified.
  • If no prog string is provided, no flash* nimble targets will be generated.

You can also specify devices, that avrman may know about. In that case it will take care of mostly everything by itself (even port discovery):

avrman init --device:uno test_arduino

config.nims

The following is a template config.nims file containing various flags that can be used as a reference to set up a project.

Note that you should pass the -mmcu and -DF_CPU flags to the c compiler according to the MCU you are using and the clock frequncy that you have set up.

You must also define the symbol USING_XXX where XXX is the name of the MCU you want to use.

For example, in order to use an ATMega644 MCU, the USING_ATMEGA664 symbol should be defined in the config.nims file (NOTE: This syntax could change in the future).

switch("os", "standalone")
switch("cpu", "avr")
switch("gc", "none")
switch("define", "release")
switch("define", "USING_ATMEGA328P")
switch("nimcache", ".nimcache")

switch("avr.standalone.gcc.options.linker", "-static")
switch("avr.standalone.gcc.exe", "avr-gcc")
switch("avr.standalone.gcc.linkerexe", "avr-gcc")

when defined(windows):
  switch("gcc.options.always", "")

There's currently a bug in nim on windows, when cross-compiling for AVR systems, that requires the following option to be specified:

switch("gcc.options.always", "")

panicoverride.nim

When compiling with --os:standalone you must provide a panicoverride.nim file.

If you are not going to use echo, you do not need to provide a printf implementation, and the following panicoverride (a modified version of the original from the tests/avr directory within the nim compiler sources) is more than enough:

proc exit(code: int) {.importc, header: "<stdlib.h>", cdecl.}

{.push stack_trace: off, profiler:off.}

proc rawoutput(s: string) = discard

proc panic(s: string) =
  rawoutput(s)
  exit(1)

{.pop.}

Note that any error caused by a failing bound-check, overflow, etc. will cause a call to the panic proc defined within the panicoverride.nim file. This makes it possible to use the panic proc for diagnostic purposes in debug builds.

Using --define:danger removes those checks.

NOTE: as of nim v2.2.6, the languages does not support the full extent of its features within to be used within a panicoverride.nim file, so you cannot directly user avr_io as macros don't work in panicoverride because of this. Nim v2.3.1 (devel) has a fix for this, which should be readily available in nim v2.4.0 and onwards.

⚠️ **GitHub.com Fallback** ⚠️