boot.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. /* Modified to use out for SPM access
  2. ** Peter Knight, Optiboot project http://optiboot.googlecode.com
  3. **
  4. ** Todo: Tidy up
  5. **
  6. ** "_short" routines execute 1 cycle faster and use 1 less word of flash
  7. ** by using "out" instruction instead of "sts".
  8. **
  9. ** Additional elpm variants that trust the value of RAMPZ
  10. */
  11. /* Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007 Eric B. Weddington
  12. All rights reserved.
  13. Redistribution and use in source and binary forms, with or without
  14. modification, are permitted provided that the following conditions are met:
  15. * Redistributions of source code must retain the above copyright
  16. notice, this list of conditions and the following disclaimer.
  17. * Redistributions in binary form must reproduce the above copyright
  18. notice, this list of conditions and the following disclaimer in
  19. the documentation and/or other materials provided with the
  20. distribution.
  21. * Neither the name of the copyright holders nor the names of
  22. contributors may be used to endorse or promote products derived
  23. from this software without specific prior written permission.
  24. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. POSSIBILITY OF SUCH DAMAGE. */
  35. #ifndef _AVR_BOOT_H_
  36. #define _AVR_BOOT_H_ 1
  37. /** \file */
  38. /** \defgroup avr_boot <avr/boot.h>: Bootloader Support Utilities
  39. \code
  40. #include <avr/io.h>
  41. #include <avr/boot.h>
  42. \endcode
  43. The macros in this module provide a C language interface to the
  44. bootloader support functionality of certain AVR processors. These
  45. macros are designed to work with all sizes of flash memory.
  46. Global interrupts are not automatically disabled for these macros. It
  47. is left up to the programmer to do this. See the code example below.
  48. Also see the processor datasheet for caveats on having global interrupts
  49. enabled during writing of the Flash.
  50. \note Not all AVR processors provide bootloader support. See your
  51. processor datasheet to see if it provides bootloader support.
  52. \todo From email with Marek: On smaller devices (all except ATmega64/128),
  53. __SPM_REG is in the I/O space, accessible with the shorter "in" and "out"
  54. instructions - since the boot loader has a limited size, this could be an
  55. important optimization.
  56. \par API Usage Example
  57. The following code shows typical usage of the boot API.
  58. \code
  59. #include <inttypes.h>
  60. #include <avr/interrupt.h>
  61. #include <avr/pgmspace.h>
  62. void boot_program_page (uint32_t page, uint8_t *buf)
  63. {
  64. uint16_t i;
  65. uint8_t sreg;
  66. // Disable interrupts.
  67. sreg = SREG;
  68. cli();
  69. eeprom_busy_wait ();
  70. boot_page_erase (page);
  71. boot_spm_busy_wait (); // Wait until the memory is erased.
  72. for (i=0; i<SPM_PAGESIZE; i+=2)
  73. {
  74. // Set up little-endian word.
  75. uint16_t w = *buf++;
  76. w += (*buf++) << 8;
  77. boot_page_fill (page + i, w);
  78. }
  79. boot_page_write (page); // Store buffer in flash page.
  80. boot_spm_busy_wait(); // Wait until the memory is written.
  81. // Reenable RWW-section again. We need this if we want to jump back
  82. // to the application after bootloading.
  83. boot_rww_enable ();
  84. // Re-enable interrupts (if they were ever enabled).
  85. SREG = sreg;
  86. }\endcode */
  87. #include <avr/eeprom.h>
  88. #include <avr/io.h>
  89. #include <inttypes.h>
  90. #include <limits.h>
  91. /* Check for SPM Control Register in processor. */
  92. #if defined (SPMCSR)
  93. # define __SPM_REG SPMCSR
  94. #elif defined (SPMCR)
  95. # define __SPM_REG SPMCR
  96. #else
  97. # error AVR processor does not provide bootloader support!
  98. #endif
  99. /* Check for SPM Enable bit. */
  100. #if defined(SPMEN)
  101. # define __SPM_ENABLE SPMEN
  102. #elif defined(SELFPRGEN)
  103. # define __SPM_ENABLE SELFPRGEN
  104. #else
  105. # error Cannot find SPM Enable bit definition!
  106. #endif
  107. /** \ingroup avr_boot
  108. \def BOOTLOADER_SECTION
  109. Used to declare a function or variable to be placed into a
  110. new section called .bootloader. This section and its contents
  111. can then be relocated to any address (such as the bootloader
  112. NRWW area) at link-time. */
  113. #define BOOTLOADER_SECTION __attribute__ ((section (".bootloader")))
  114. /* Create common bit definitions. */
  115. #ifdef ASB
  116. #define __COMMON_ASB ASB
  117. #else
  118. #define __COMMON_ASB RWWSB
  119. #endif
  120. #ifdef ASRE
  121. #define __COMMON_ASRE ASRE
  122. #else
  123. #define __COMMON_ASRE RWWSRE
  124. #endif
  125. /* Define the bit positions of the Boot Lock Bits. */
  126. #define BLB12 5
  127. #define BLB11 4
  128. #define BLB02 3
  129. #define BLB01 2
  130. /** \ingroup avr_boot
  131. \def boot_spm_interrupt_enable()
  132. Enable the SPM interrupt. */
  133. #define boot_spm_interrupt_enable() (__SPM_REG |= (uint8_t)_BV(SPMIE))
  134. /** \ingroup avr_boot
  135. \def boot_spm_interrupt_disable()
  136. Disable the SPM interrupt. */
  137. #define boot_spm_interrupt_disable() (__SPM_REG &= (uint8_t)~_BV(SPMIE))
  138. /** \ingroup avr_boot
  139. \def boot_is_spm_interrupt()
  140. Check if the SPM interrupt is enabled. */
  141. #define boot_is_spm_interrupt() (__SPM_REG & (uint8_t)_BV(SPMIE))
  142. /** \ingroup avr_boot
  143. \def boot_rww_busy()
  144. Check if the RWW section is busy. */
  145. #define boot_rww_busy() (__SPM_REG & (uint8_t)_BV(__COMMON_ASB))
  146. /** \ingroup avr_boot
  147. \def boot_spm_busy()
  148. Check if the SPM instruction is busy. */
  149. #define boot_spm_busy() (__SPM_REG & (uint8_t)_BV(__SPM_ENABLE))
  150. /** \ingroup avr_boot
  151. \def boot_spm_busy_wait()
  152. Wait while the SPM instruction is busy. */
  153. #define boot_spm_busy_wait() do{}while(boot_spm_busy())
  154. #define __BOOT_PAGE_ERASE (_BV(__SPM_ENABLE) | _BV(PGERS))
  155. #define __BOOT_PAGE_WRITE (_BV(__SPM_ENABLE) | _BV(PGWRT))
  156. #define __BOOT_PAGE_FILL _BV(__SPM_ENABLE)
  157. #define __BOOT_RWW_ENABLE (_BV(__SPM_ENABLE) | _BV(__COMMON_ASRE))
  158. #define __BOOT_LOCK_BITS_SET (_BV(__SPM_ENABLE) | _BV(BLBSET))
  159. #define __boot_page_fill_short(address, data) \
  160. (__extension__({ \
  161. __asm__ __volatile__ \
  162. ( \
  163. "movw r0, %3\n\t" \
  164. "out %0, %1\n\t" \
  165. "spm\n\t" \
  166. "clr r1\n\t" \
  167. : \
  168. : "i" (_SFR_IO_ADDR(__SPM_REG)), \
  169. "r" ((uint8_t)__BOOT_PAGE_FILL), \
  170. "z" ((uint16_t)address), \
  171. "r" ((uint16_t)data) \
  172. : "r0" \
  173. ); \
  174. }))
  175. #define __boot_page_fill_normal(address, data) \
  176. (__extension__({ \
  177. __asm__ __volatile__ \
  178. ( \
  179. "movw r0, %3\n\t" \
  180. "sts %0, %1\n\t" \
  181. "spm\n\t" \
  182. "clr r1\n\t" \
  183. : \
  184. : "i" (_SFR_MEM_ADDR(__SPM_REG)), \
  185. "r" ((uint8_t)__BOOT_PAGE_FILL), \
  186. "z" ((uint16_t)address), \
  187. "r" ((uint16_t)data) \
  188. : "r0" \
  189. ); \
  190. }))
  191. #define __boot_page_fill_alternate(address, data)\
  192. (__extension__({ \
  193. __asm__ __volatile__ \
  194. ( \
  195. "movw r0, %3\n\t" \
  196. "sts %0, %1\n\t" \
  197. "spm\n\t" \
  198. ".word 0xffff\n\t" \
  199. "nop\n\t" \
  200. "clr r1\n\t" \
  201. : \
  202. : "i" (_SFR_MEM_ADDR(__SPM_REG)), \
  203. "r" ((uint8_t)__BOOT_PAGE_FILL), \
  204. "z" ((uint16_t)address), \
  205. "r" ((uint16_t)data) \
  206. : "r0" \
  207. ); \
  208. }))
  209. #define __boot_page_fill_extended(address, data) \
  210. (__extension__({ \
  211. __asm__ __volatile__ \
  212. ( \
  213. "movw r0, %4\n\t" \
  214. "movw r30, %A3\n\t" \
  215. "sts %1, %C3\n\t" \
  216. "sts %0, %2\n\t" \
  217. "spm\n\t" \
  218. "clr r1\n\t" \
  219. : \
  220. : "i" (_SFR_MEM_ADDR(__SPM_REG)), \
  221. "i" (_SFR_MEM_ADDR(RAMPZ)), \
  222. "r" ((uint8_t)__BOOT_PAGE_FILL), \
  223. "r" ((uint32_t)address), \
  224. "r" ((uint16_t)data) \
  225. : "r0", "r30", "r31" \
  226. ); \
  227. }))
  228. #define __boot_page_fill_extended_short(address, data) \
  229. (__extension__({ \
  230. __asm__ __volatile__ \
  231. ( \
  232. "movw r0, %4\n\t" \
  233. "movw r30, %A3\n\t" \
  234. "out %1, %C3\n\t" \
  235. "out %0, %2\n\t" \
  236. "spm\n\t" \
  237. "clr r1\n\t" \
  238. : \
  239. : "i" (_SFR_IO_ADDR(__SPM_REG)), \
  240. "i" (_SFR_IO_ADDR(RAMPZ)), \
  241. "r" ((uint8_t)__BOOT_PAGE_FILL), \
  242. "r" ((uint32_t)address), \
  243. "r" ((uint16_t)data) \
  244. : "r0", "r30", "r31" \
  245. ); \
  246. }))
  247. #define __boot_page_erase_short(address) \
  248. (__extension__({ \
  249. __asm__ __volatile__ \
  250. ( \
  251. "out %0, %1\n\t" \
  252. "spm\n\t" \
  253. : \
  254. : "i" (_SFR_IO_ADDR(__SPM_REG)), \
  255. "r" ((uint8_t)__BOOT_PAGE_ERASE), \
  256. "z" ((uint16_t)address) \
  257. ); \
  258. }))
  259. #define __boot_page_erase_normal(address) \
  260. (__extension__({ \
  261. __asm__ __volatile__ \
  262. ( \
  263. "sts %0, %1\n\t" \
  264. "spm\n\t" \
  265. : \
  266. : "i" (_SFR_MEM_ADDR(__SPM_REG)), \
  267. "r" ((uint8_t)__BOOT_PAGE_ERASE), \
  268. "z" ((uint16_t)address) \
  269. ); \
  270. }))
  271. #define __boot_page_erase_alternate(address) \
  272. (__extension__({ \
  273. __asm__ __volatile__ \
  274. ( \
  275. "sts %0, %1\n\t" \
  276. "spm\n\t" \
  277. ".word 0xffff\n\t" \
  278. "nop\n\t" \
  279. : \
  280. : "i" (_SFR_MEM_ADDR(__SPM_REG)), \
  281. "r" ((uint8_t)__BOOT_PAGE_ERASE), \
  282. "z" ((uint16_t)address) \
  283. ); \
  284. }))
  285. #define __boot_page_erase_extended(address) \
  286. (__extension__({ \
  287. __asm__ __volatile__ \
  288. ( \
  289. "movw r30, %A3\n\t" \
  290. "sts %1, %C3\n\t" \
  291. "sts %0, %2\n\t" \
  292. "spm\n\t" \
  293. : \
  294. : "i" (_SFR_MEM_ADDR(__SPM_REG)), \
  295. "i" (_SFR_MEM_ADDR(RAMPZ)), \
  296. "r" ((uint8_t)__BOOT_PAGE_ERASE), \
  297. "r" ((uint32_t)address) \
  298. : "r30", "r31" \
  299. ); \
  300. }))
  301. #define __boot_page_erase_extended_short(address) \
  302. (__extension__({ \
  303. __asm__ __volatile__ \
  304. ( \
  305. "movw r30, %A3\n\t" \
  306. "out %1, %C3\n\t" \
  307. "out %0, %2\n\t" \
  308. "spm\n\t" \
  309. : \
  310. : "i" (_SFR_IO_ADDR(__SPM_REG)), \
  311. "i" (_SFR_IO_ADDR(RAMPZ)), \
  312. "r" ((uint8_t)__BOOT_PAGE_ERASE), \
  313. "r" ((uint32_t)address) \
  314. : "r30", "r31" \
  315. ); \
  316. }))
  317. #define __boot_page_write_short(address) \
  318. (__extension__({ \
  319. __asm__ __volatile__ \
  320. ( \
  321. "out %0, %1\n\t" \
  322. "spm\n\t" \
  323. : \
  324. : "i" (_SFR_IO_ADDR(__SPM_REG)), \
  325. "r" ((uint8_t)__BOOT_PAGE_WRITE), \
  326. "z" ((uint16_t)address) \
  327. ); \
  328. }))
  329. #define __boot_page_write_normal(address) \
  330. (__extension__({ \
  331. __asm__ __volatile__ \
  332. ( \
  333. "sts %0, %1\n\t" \
  334. "spm\n\t" \
  335. : \
  336. : "i" (_SFR_MEM_ADDR(__SPM_REG)), \
  337. "r" ((uint8_t)__BOOT_PAGE_WRITE), \
  338. "z" ((uint16_t)address) \
  339. ); \
  340. }))
  341. #define __boot_page_write_alternate(address) \
  342. (__extension__({ \
  343. __asm__ __volatile__ \
  344. ( \
  345. "sts %0, %1\n\t" \
  346. "spm\n\t" \
  347. ".word 0xffff\n\t" \
  348. "nop\n\t" \
  349. : \
  350. : "i" (_SFR_MEM_ADDR(__SPM_REG)), \
  351. "r" ((uint8_t)__BOOT_PAGE_WRITE), \
  352. "z" ((uint16_t)address) \
  353. ); \
  354. }))
  355. #define __boot_page_write_extended(address) \
  356. (__extension__({ \
  357. __asm__ __volatile__ \
  358. ( \
  359. "movw r30, %A3\n\t" \
  360. "sts %1, %C3\n\t" \
  361. "sts %0, %2\n\t" \
  362. "spm\n\t" \
  363. : \
  364. : "i" (_SFR_MEM_ADDR(__SPM_REG)), \
  365. "i" (_SFR_MEM_ADDR(RAMPZ)), \
  366. "r" ((uint8_t)__BOOT_PAGE_WRITE), \
  367. "r" ((uint32_t)address) \
  368. : "r30", "r31" \
  369. ); \
  370. }))
  371. #define __boot_page_write_extended_short(address) \
  372. (__extension__({ \
  373. __asm__ __volatile__ \
  374. ( \
  375. "movw r30, %A3\n\t" \
  376. "out %1, %C3\n\t" \
  377. "out %0, %2\n\t" \
  378. "spm\n\t" \
  379. : \
  380. : "i" (_SFR_IO_ADDR(__SPM_REG)), \
  381. "i" (_SFR_IO_ADDR(RAMPZ)), \
  382. "r" ((uint8_t)__BOOT_PAGE_WRITE), \
  383. "r" ((uint32_t)address) \
  384. : "r30", "r31" \
  385. ); \
  386. }))
  387. #define __boot_rww_enable_short() \
  388. (__extension__({ \
  389. __asm__ __volatile__ \
  390. ( \
  391. "out %0, %1\n\t" \
  392. "spm\n\t" \
  393. : \
  394. : "i" (_SFR_IO_ADDR(__SPM_REG)), \
  395. "r" ((uint8_t)__BOOT_RWW_ENABLE) \
  396. ); \
  397. }))
  398. #define __boot_rww_enable() \
  399. (__extension__({ \
  400. __asm__ __volatile__ \
  401. ( \
  402. "sts %0, %1\n\t" \
  403. "spm\n\t" \
  404. : \
  405. : "i" (_SFR_MEM_ADDR(__SPM_REG)), \
  406. "r" ((uint8_t)__BOOT_RWW_ENABLE) \
  407. ); \
  408. }))
  409. #define __boot_rww_enable_alternate() \
  410. (__extension__({ \
  411. __asm__ __volatile__ \
  412. ( \
  413. "sts %0, %1\n\t" \
  414. "spm\n\t" \
  415. ".word 0xffff\n\t" \
  416. "nop\n\t" \
  417. : \
  418. : "i" (_SFR_MEM_ADDR(__SPM_REG)), \
  419. "r" ((uint8_t)__BOOT_RWW_ENABLE) \
  420. ); \
  421. }))
  422. /* From the mega16/mega128 data sheets (maybe others):
  423. Bits by SPM To set the Boot Loader Lock bits, write the desired data to
  424. R0, write "X0001001" to SPMCR and execute SPM within four clock cycles
  425. after writing SPMCR. The only accessible Lock bits are the Boot Lock bits
  426. that may prevent the Application and Boot Loader section from any
  427. software update by the MCU.
  428. If bits 5..2 in R0 are cleared (zero), the corresponding Boot Lock bit
  429. will be programmed if an SPM instruction is executed within four cycles
  430. after BLBSET and SPMEN (or SELFPRGEN) are set in SPMCR. The Z-pointer is
  431. don't care during this operation, but for future compatibility it is
  432. recommended to load the Z-pointer with $0001 (same as used for reading the
  433. Lock bits). For future compatibility It is also recommended to set bits 7,
  434. 6, 1, and 0 in R0 to 1 when writing the Lock bits. When programming the
  435. Lock bits the entire Flash can be read during the operation. */
  436. #define __boot_lock_bits_set_short(lock_bits) \
  437. (__extension__({ \
  438. uint8_t value = (uint8_t)(~(lock_bits)); \
  439. __asm__ __volatile__ \
  440. ( \
  441. "ldi r30, 1\n\t" \
  442. "ldi r31, 0\n\t" \
  443. "mov r0, %2\n\t" \
  444. "out %0, %1\n\t" \
  445. "spm\n\t" \
  446. : \
  447. : "i" (_SFR_IO_ADDR(__SPM_REG)), \
  448. "r" ((uint8_t)__BOOT_LOCK_BITS_SET), \
  449. "r" (value) \
  450. : "r0", "r30", "r31" \
  451. ); \
  452. }))
  453. #define __boot_lock_bits_set(lock_bits) \
  454. (__extension__({ \
  455. uint8_t value = (uint8_t)(~(lock_bits)); \
  456. __asm__ __volatile__ \
  457. ( \
  458. "ldi r30, 1\n\t" \
  459. "ldi r31, 0\n\t" \
  460. "mov r0, %2\n\t" \
  461. "sts %0, %1\n\t" \
  462. "spm\n\t" \
  463. : \
  464. : "i" (_SFR_MEM_ADDR(__SPM_REG)), \
  465. "r" ((uint8_t)__BOOT_LOCK_BITS_SET), \
  466. "r" (value) \
  467. : "r0", "r30", "r31" \
  468. ); \
  469. }))
  470. #define __boot_lock_bits_set_alternate(lock_bits) \
  471. (__extension__({ \
  472. uint8_t value = (uint8_t)(~(lock_bits)); \
  473. __asm__ __volatile__ \
  474. ( \
  475. "ldi r30, 1\n\t" \
  476. "ldi r31, 0\n\t" \
  477. "mov r0, %2\n\t" \
  478. "sts %0, %1\n\t" \
  479. "spm\n\t" \
  480. ".word 0xffff\n\t" \
  481. "nop\n\t" \
  482. : \
  483. : "i" (_SFR_MEM_ADDR(__SPM_REG)), \
  484. "r" ((uint8_t)__BOOT_LOCK_BITS_SET), \
  485. "r" (value) \
  486. : "r0", "r30", "r31" \
  487. ); \
  488. }))
  489. /*
  490. Reading lock and fuse bits:
  491. Similarly to writing the lock bits above, set BLBSET and SPMEN (or
  492. SELFPRGEN) bits in __SPMREG, and then (within four clock cycles) issue an
  493. LPM instruction.
  494. Z address: contents:
  495. 0x0000 low fuse bits
  496. 0x0001 lock bits
  497. 0x0002 extended fuse bits
  498. 0x0003 high fuse bits
  499. Sounds confusing, doesn't it?
  500. Unlike the macros in pgmspace.h, no need to care for non-enhanced
  501. cores here as these old cores do not provide SPM support anyway.
  502. */
  503. /** \ingroup avr_boot
  504. \def GET_LOW_FUSE_BITS
  505. address to read the low fuse bits, using boot_lock_fuse_bits_get
  506. */
  507. #define GET_LOW_FUSE_BITS (0x0000)
  508. /** \ingroup avr_boot
  509. \def GET_LOCK_BITS
  510. address to read the lock bits, using boot_lock_fuse_bits_get
  511. */
  512. #define GET_LOCK_BITS (0x0001)
  513. /** \ingroup avr_boot
  514. \def GET_EXTENDED_FUSE_BITS
  515. address to read the extended fuse bits, using boot_lock_fuse_bits_get
  516. */
  517. #define GET_EXTENDED_FUSE_BITS (0x0002)
  518. /** \ingroup avr_boot
  519. \def GET_HIGH_FUSE_BITS
  520. address to read the high fuse bits, using boot_lock_fuse_bits_get
  521. */
  522. #define GET_HIGH_FUSE_BITS (0x0003)
  523. /** \ingroup avr_boot
  524. \def boot_lock_fuse_bits_get(address)
  525. Read the lock or fuse bits at \c address.
  526. Parameter \c address can be any of GET_LOW_FUSE_BITS,
  527. GET_LOCK_BITS, GET_EXTENDED_FUSE_BITS, or GET_HIGH_FUSE_BITS.
  528. \note The lock and fuse bits returned are the physical values,
  529. i.e. a bit returned as 0 means the corresponding fuse or lock bit
  530. is programmed.
  531. */
  532. #define boot_lock_fuse_bits_get_short(address) \
  533. (__extension__({ \
  534. uint8_t __result; \
  535. __asm__ __volatile__ \
  536. ( \
  537. "ldi r30, %3\n\t" \
  538. "ldi r31, 0\n\t" \
  539. "out %1, %2\n\t" \
  540. "lpm %0, Z\n\t" \
  541. : "=r" (__result) \
  542. : "i" (_SFR_IO_ADDR(__SPM_REG)), \
  543. "r" ((uint8_t)__BOOT_LOCK_BITS_SET), \
  544. "M" (address) \
  545. : "r0", "r30", "r31" \
  546. ); \
  547. __result; \
  548. }))
  549. #define boot_lock_fuse_bits_get(address) \
  550. (__extension__({ \
  551. uint8_t __result; \
  552. __asm__ __volatile__ \
  553. ( \
  554. "ldi r30, %3\n\t" \
  555. "ldi r31, 0\n\t" \
  556. "sts %1, %2\n\t" \
  557. "lpm %0, Z\n\t" \
  558. : "=r" (__result) \
  559. : "i" (_SFR_MEM_ADDR(__SPM_REG)), \
  560. "r" ((uint8_t)__BOOT_LOCK_BITS_SET), \
  561. "M" (address) \
  562. : "r0", "r30", "r31" \
  563. ); \
  564. __result; \
  565. }))
  566. /** \ingroup avr_boot
  567. \def boot_signature_byte_get(address)
  568. Read the Signature Row byte at \c address. For some MCU types,
  569. this function can also retrieve the factory-stored oscillator
  570. calibration bytes.
  571. Parameter \c address can be 0-0x1f as documented by the datasheet.
  572. \note The values are MCU type dependent.
  573. */
  574. #define __BOOT_SIGROW_READ (_BV(__SPM_ENABLE) | _BV(SIGRD))
  575. #define boot_signature_byte_get_short(addr) \
  576. (__extension__({ \
  577. uint16_t __addr16 = (uint16_t)(addr); \
  578. uint8_t __result; \
  579. __asm__ __volatile__ \
  580. ( \
  581. "out %1, %2\n\t" \
  582. "lpm %0, Z" "\n\t" \
  583. : "=r" (__result) \
  584. : "i" (_SFR_IO_ADDR(__SPM_REG)), \
  585. "r" ((uint8_t) __BOOT_SIGROW_READ), \
  586. "z" (__addr16) \
  587. ); \
  588. __result; \
  589. }))
  590. #define boot_signature_byte_get(addr) \
  591. (__extension__({ \
  592. uint16_t __addr16 = (uint16_t)(addr); \
  593. uint8_t __result; \
  594. __asm__ __volatile__ \
  595. ( \
  596. "sts %1, %2\n\t" \
  597. "lpm %0, Z" "\n\t" \
  598. : "=r" (__result) \
  599. : "i" (_SFR_MEM_ADDR(__SPM_REG)), \
  600. "r" ((uint8_t) __BOOT_SIGROW_READ), \
  601. "z" (__addr16) \
  602. ); \
  603. __result; \
  604. }))
  605. /** \ingroup avr_boot
  606. \def boot_page_fill(address, data)
  607. Fill the bootloader temporary page buffer for flash
  608. address with data word.
  609. \note The address is a byte address. The data is a word. The AVR
  610. writes data to the buffer a word at a time, but addresses the buffer
  611. per byte! So, increment your address by 2 between calls, and send 2
  612. data bytes in a word format! The LSB of the data is written to the lower
  613. address; the MSB of the data is written to the higher address.*/
  614. /** \ingroup avr_boot
  615. \def boot_page_erase(address)
  616. Erase the flash page that contains address.
  617. \note address is a byte address in flash, not a word address. */
  618. /** \ingroup avr_boot
  619. \def boot_page_write(address)
  620. Write the bootloader temporary page buffer
  621. to flash page that contains address.
  622. \note address is a byte address in flash, not a word address. */
  623. /** \ingroup avr_boot
  624. \def boot_rww_enable()
  625. Enable the Read-While-Write memory section. */
  626. /** \ingroup avr_boot
  627. \def boot_lock_bits_set(lock_bits)
  628. Set the bootloader lock bits.
  629. \param lock_bits A mask of which Boot Loader Lock Bits to set.
  630. \note In this context, a 'set bit' will be written to a zero value.
  631. Note also that only BLBxx bits can be programmed by this command.
  632. For example, to disallow the SPM instruction from writing to the Boot
  633. Loader memory section of flash, you would use this macro as such:
  634. \code
  635. boot_lock_bits_set (_BV (BLB11));
  636. \endcode
  637. \note Like any lock bits, the Boot Loader Lock Bits, once set,
  638. cannot be cleared again except by a chip erase which will in turn
  639. also erase the boot loader itself. */
  640. /* Normal versions of the macros use 16-bit addresses.
  641. Extended versions of the macros use 32-bit addresses.
  642. Alternate versions of the macros use 16-bit addresses and require special
  643. instruction sequences after LPM.
  644. FLASHEND is defined in the ioXXXX.h file.
  645. USHRT_MAX is defined in <limits.h>. */
  646. #if defined(__AVR_ATmega161__) || defined(__AVR_ATmega163__) \
  647. || defined(__AVR_ATmega323__)
  648. /* Alternate: ATmega161/163/323 and 16 bit address */
  649. #define boot_page_fill(address, data) __boot_page_fill_alternate(address, data)
  650. #define boot_page_erase(address) __boot_page_erase_alternate(address)
  651. #define boot_page_write(address) __boot_page_write_alternate(address)
  652. #define boot_rww_enable() __boot_rww_enable_alternate()
  653. #define boot_lock_bits_set(lock_bits) __boot_lock_bits_set_alternate(lock_bits)
  654. #elif (FLASHEND > USHRT_MAX)
  655. /* Extended: >16 bit address */
  656. #define boot_page_fill(address, data) __boot_page_fill_extended_short(address, data)
  657. #define boot_page_erase(address) __boot_page_erase_extended_short(address)
  658. #define boot_page_write(address) __boot_page_write_extended_short(address)
  659. #define boot_rww_enable() __boot_rww_enable_short()
  660. #define boot_lock_bits_set(lock_bits) __boot_lock_bits_set_short(lock_bits)
  661. #else
  662. /* Normal: 16 bit address */
  663. #define boot_page_fill(address, data) __boot_page_fill_short(address, data)
  664. #define boot_page_erase(address) __boot_page_erase_short(address)
  665. #define boot_page_write(address) __boot_page_write_short(address)
  666. #define boot_rww_enable() __boot_rww_enable_short()
  667. #define boot_lock_bits_set(lock_bits) __boot_lock_bits_set_short(lock_bits)
  668. #endif
  669. /** \ingroup avr_boot
  670. Same as boot_page_fill() except it waits for eeprom and spm operations to
  671. complete before filling the page. */
  672. #define boot_page_fill_safe(address, data) \
  673. do { \
  674. boot_spm_busy_wait(); \
  675. eeprom_busy_wait(); \
  676. boot_page_fill(address, data); \
  677. } while (0)
  678. /** \ingroup avr_boot
  679. Same as boot_page_erase() except it waits for eeprom and spm operations to
  680. complete before erasing the page. */
  681. #define boot_page_erase_safe(address) \
  682. do { \
  683. boot_spm_busy_wait(); \
  684. eeprom_busy_wait(); \
  685. boot_page_erase (address); \
  686. } while (0)
  687. /** \ingroup avr_boot
  688. Same as boot_page_write() except it waits for eeprom and spm operations to
  689. complete before writing the page. */
  690. #define boot_page_write_safe(address) \
  691. do { \
  692. boot_spm_busy_wait(); \
  693. eeprom_busy_wait(); \
  694. boot_page_write (address); \
  695. } while (0)
  696. /** \ingroup avr_boot
  697. Same as boot_rww_enable() except waits for eeprom and spm operations to
  698. complete before enabling the RWW mameory. */
  699. #define boot_rww_enable_safe() \
  700. do { \
  701. boot_spm_busy_wait(); \
  702. eeprom_busy_wait(); \
  703. boot_rww_enable(); \
  704. } while (0)
  705. /** \ingroup avr_boot
  706. Same as boot_lock_bits_set() except waits for eeprom and spm operations to
  707. complete before setting the lock bits. */
  708. #define boot_lock_bits_set_safe(lock_bits) \
  709. do { \
  710. boot_spm_busy_wait(); \
  711. eeprom_busy_wait(); \
  712. boot_lock_bits_set (lock_bits); \
  713. } while (0)
  714. #endif /* _AVR_BOOT_H_ */