Mirror of CollapseOS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
3.0KB

  1. # Working with AVR microcontrollers
  2. # Assembling AVR binaries
  3. TODO
  4. # Programming AVR chips
  5. To program AVR chips, you need a device that provides the SPI protocol. The
  6. device built in the rc2014/sdcard recipe fits the bill. Make sure you can
  7. override the SPI clock because the system clock will be too fast for most AVR
  8. chips, which are usually running at 1MHz. Because the SPI clock needs to be a
  9. 4th of that, a safe frequency for SPI communication would be 250kHz.
  10. Because you will not be using your system clock, you'll also need to override
  11. SPI_DELAY in your xcomp unit: the default value for this is 2 NOP, which only
  12. works when you use the system clock.
  13. Alternatively, you could run your whole system at 250kHz, but that's going to be
  14. really slow.
  15. The AVR programmer device is really simple: Wire SPI connections to proper AVR
  16. pins as described in the MCU's datasheet. Note that this device will be the same
  17. as the one you'll use for any modern SPI-based AVR programmer, with RESET
  18. replacing SS.
  19. (TODO: design a SPI relay that supports more than one device. At the time of
  20. this writing, one has to disconnect the SD card reader before enabling the AVR
  21. programmer)
  22. The AVR programming code is at B690.
  23. Before you begin programming the chip, the device must be deselected. Ensure
  24. with "(spid)".
  25. Then, you initiate programming mode with "asp$", and then issue your commands.
  26. At this time, only fuse commands are implemented. You get/set they values with
  27. "aspfx@/aspfx!", x being one of "l" (low fuse), "h" (high fuse), "e" (extended
  28. fuse).
  29. Each command will verify that it's in sync, that is, that its 3rd exchange
  30. echoes the byte that was sent in the 2nd exchange. If it doesn't, the command
  31. aborts with "AVR err".
  32. At the time of this writing, it is recommended that you perform your commands in
  33. batch, that is, running your commands right after the "asp$" command, ending
  34. your batch with "(spid)" so that the next batch works. In my tests, interacting
  35. with the chip "live" in a single "asp$" session sometimes resulted in unreliable
  36. data that didn't properly detect sync errors. TODO: investigate further.
  37. # Writing data to Flash
  38. Writing to AVR's flash is done in batch mode, page by page. To this end, the
  39. chip has a buffer which is writable byte-by-byte. To write to the flash, you
  40. begin by writing to that buffer using aspfb! and then write to a page using
  41. aspfp!.
  42. Please note that aspfb! deals with *words*, not bytes. If, for example, you want
  43. to hook it to A!*, make sure you use AMOVEW instead of AMOVE. You will need to
  44. create a wrapper word around aspfb! that divides dst addr by 2 because AMOVEW
  45. use byte-based addresses but aspfb! uses word-based ones. You also have to make
  46. sure that A@* points to @ (or another word-based fetcher) instead of its default
  47. value of C@.
  48. Beware of bootloader sections! By default, AVR chips have a bootloader using the
  49. first few pages (by default, the ATMega328P uses 4 pages for its bootloader).
  50. Check (or modify) the BOOTSZ fuses to confirm where you whould start writing
  51. your program.