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.

113 lines
4.3KB

  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 programmer device
  12. The AVR programmer device is really simple: Wire SPI connections
  13. to proper AVR pins as described in the MCU's datasheet. Note
  14. that this device will be the same as the one you'll use for any
  15. modern SPI-based AVR programmer, with RESET replacing SS.
  16. This device should have an on/off switch that controls the
  17. chip's power for a very simple reason: Because we can't control
  18. what's on the chip, it could mess up your whole SPI bus when
  19. RESET is not held low. This means that as long as it's connected
  20. and powered, it is likely to mess up your other devices, such as
  21. the SD card.
  22. You could put the AVR chip behind a buffer to avoid this, but
  23. an on/off switch also does the trick and satisfies the low-tech
  24. lover in you.
  25. # Programming software
  26. The AVR programming code is at B160.
  27. Before you begin programming the chip, the device must be desel-
  28. ected. Ensure with "0 (spie)".
  29. Then, you initiate programming mode with "asp$", and then issue
  30. your commands.
  31. Each command will verify that it's in sync, that is, that its
  32. 3rd exchange echoes the byte that was sent in the 2nd exchange.
  33. If it doesn't, the command aborts with "AVR err".
  34. # Ensuring reliability
  35. The reliability of your communication depends a lot on the
  36. soundness of your SPI relay design. If it's good, you will sel-
  37. dom see those "AVR err".
  38. However, there are worse things than "AVR err": wrong data. Sync
  39. checks ensure communication reliability at every command, but
  40. in the case of commands getting data, you might be out-of-sync
  41. when you receive your result without knowing it! To ensure that
  42. you're still in sync, you need to issue a command, which might
  43. spit "AVR err". If it does, your previous result is unreliable.
  44. Here's an example word that reliably prints the high fuse value
  45. from SPI devid 1:
  46. : get 1 asp$ asprdy aspfh@ asprdy .x 0 (spie) ;
  47. Another very important matter is clock speed. As mentioned
  48. above, the safe clock speed is 250kHz. If you use the SPI design
  49. in rc2014/sdcard recipe, this means that your input clock speed
  50. can theoretically be 500kHz because the '161 divides it by 2.
  51. In practice, however, you can't really do that because depending
  52. on the timing of your SPI write, the first "bump" of the SPI
  53. clock might end up being nearly 500kHz, which will result in oc-
  54. casional communication errors.
  55. The simplest and safest way to avoid this is to reduce your
  56. raw input clock by 2, which will reduce your effective communi-
  57. cation speed by 2. There certainly are options allowing you to
  58. keep optimal speed, but they're significantly more complex than
  59. accepting slower speed.
  60. # Access fuses
  61. You get/set they values with "aspfx@/aspfx!", x being one of "l"
  62. (low fuse), "h" (high fuse), "e" (extended fuse).
  63. # Access flash
  64. Writing to AVR's flash is done in batch mode, page by page. To
  65. this end, the chip has a buffer which is writable byte-by-byte.
  66. Writing to the flash begins with a call to asperase, which
  67. erases the whole chip. It seems possible to erase flash page-by-
  68. page through parallel programming, but the SPI protocol doesn't
  69. expose it, we have to erase the whole chip. Then, you write to
  70. the buffer using aspfb! and then write to a page using aspfp!.
  71. Example to write 0x1234 to the first byte of the first page:
  72. asperase 0x1234 0 aspfb! 0 aspfp!
  73. Please note that aspfb! deals with *words*, not bytes. If, for
  74. example, you want to hook it to C!*, make sure you use MOVEW
  75. instead of MOVE. You will need to create a wrapper word around
  76. aspfb! that divides dst addr by 2 because MOVEW use byte-based
  77. addresses but aspfb! uses word-based ones. You also have to make
  78. sure that C@* points to @ (or another word-based fetcher)
  79. instead of its default value of C@.
  80. # Access EEPROM
  81. Accessing EEPROM is simple and is done byte-by-byte with words
  82. aspe@ and aspe!. Example:
  83. 0x42 0 aspe! 0 aspe@ .x ( prints 42 )