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.

57 lines
2.2KB

  1. # Programming AVR chips
  2. (In this documentation, you are expected to have an AVR binary
  3. ready to send. To assemble an AVR binary from source, see
  4. asm.txt)
  5. To program AVR chips, you need a device that provides the SPI
  6. protocol. The device built in the rc2014/sdcard recipe fits the
  7. bill. Make sure you can override the SPI clock because the sys-
  8. tem clock will be too fast for most AVR chips, which are usually
  9. running at 1MHz. Because the SPI clock needs to be a 4th of
  10. that, a safe frequency for SPI communication would be 250kHz.
  11. The AVR programmer device is really simple: Wire SPI connections
  12. to proper AVR pins as described in the MCU's datasheet. Note
  13. that this device will be the same as the one you'll use for any
  14. modern SPI-based AVR programmer, with RESET replacing SS.
  15. The AVR programming code is at B160.
  16. Before you begin programming the chip, the device must be desel-
  17. ected. Ensure with "0 (spie)".
  18. Then, you initiate programming mode with "asp$", and then issue
  19. your commands.
  20. Each command will verify that it's in sync, that is, that its
  21. 3rd exchange echoes the byte that was sent in the 2nd exchange.
  22. If it doesn't, the command aborts with "AVR err".
  23. # Access fuses
  24. You get/set they values with "aspfx@/aspfx!", x being one of "l"
  25. (low fuse), "h" (high fuse), "e" (extended fuse).
  26. # Access flash
  27. Writing to AVR's flash is done in batch mode, page by page. To
  28. this end, the chip has a buffer which is writable byte-by-byte.
  29. Writing to the flash begins with a call to asperase, which
  30. erases the whole chip. It seems possible to erase flash page-by-
  31. page through parallel programming, but the SPI protocol doesn't
  32. expose it, we have to erase the whole chip. Then, you write to
  33. the buffer using aspfb! and then write to a page using aspfp!.
  34. Example to write 0x1234 to the first byte of the first page:
  35. asperase 0x1234 0 aspfb! 0 aspfp!
  36. Please note that aspfb! deals with *words*, not bytes. If, for
  37. example, you want to hook it to A!*, make sure you use AMOVEW
  38. instead of AMOVE. You will need to create a wrapper word around
  39. aspfb! that divides dst addr by 2 because AMOVEW use byte-based
  40. addresses but aspfb! uses word-based ones. You also have to make
  41. sure that A@* points to @ (or another word-based fetcher)
  42. instead of its default value of C@.