AVR-Variable-Attributes.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2017 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Funding Free Software", the Front-Cover
  8. Texts being (a) (see below), and with the Back-Cover Texts being (b)
  9. (see below). A copy of the license is included in the section entitled
  10. "GNU Free Documentation License".
  11. (a) The FSF's Front-Cover Text is:
  12. A GNU Manual
  13. (b) The FSF's Back-Cover Text is:
  14. You have freedom to copy and modify this GNU Manual, like GNU
  15. software. Copies published by the Free Software Foundation raise
  16. funds for GNU development. -->
  17. <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <title>Using the GNU Compiler Collection (GCC): AVR Variable Attributes</title>
  20. <meta name="description" content="Using the GNU Compiler Collection (GCC): AVR Variable Attributes">
  21. <meta name="keywords" content="Using the GNU Compiler Collection (GCC): AVR Variable Attributes">
  22. <meta name="resource-type" content="document">
  23. <meta name="distribution" content="global">
  24. <meta name="Generator" content="makeinfo">
  25. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  26. <link href="index.html#Top" rel="start" title="Top">
  27. <link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="Variable-Attributes.html#Variable-Attributes" rel="up" title="Variable Attributes">
  30. <link href="Blackfin-Variable-Attributes.html#Blackfin-Variable-Attributes" rel="next" title="Blackfin Variable Attributes">
  31. <link href="Common-Variable-Attributes.html#Common-Variable-Attributes" rel="prev" title="Common Variable Attributes">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.smallquotation {font-size: smaller}
  36. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.indentedblock {margin-left: 3.2em}
  39. div.lisp {margin-left: 3.2em}
  40. div.smalldisplay {margin-left: 3.2em}
  41. div.smallexample {margin-left: 3.2em}
  42. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  43. div.smalllisp {margin-left: 3.2em}
  44. kbd {font-style:oblique}
  45. pre.display {font-family: inherit}
  46. pre.format {font-family: inherit}
  47. pre.menu-comment {font-family: serif}
  48. pre.menu-preformatted {font-family: serif}
  49. pre.smalldisplay {font-family: inherit; font-size: smaller}
  50. pre.smallexample {font-size: smaller}
  51. pre.smallformat {font-family: inherit; font-size: smaller}
  52. pre.smalllisp {font-size: smaller}
  53. span.nocodebreak {white-space:nowrap}
  54. span.nolinebreak {white-space:nowrap}
  55. span.roman {font-family:serif; font-weight:normal}
  56. span.sansserif {font-family:sans-serif; font-weight:normal}
  57. ul.no-bullet {list-style: none}
  58. -->
  59. </style>
  60. </head>
  61. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  62. <a name="AVR-Variable-Attributes"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="Blackfin-Variable-Attributes.html#Blackfin-Variable-Attributes" accesskey="n" rel="next">Blackfin Variable Attributes</a>, Previous: <a href="Common-Variable-Attributes.html#Common-Variable-Attributes" accesskey="p" rel="prev">Common Variable Attributes</a>, Up: <a href="Variable-Attributes.html#Variable-Attributes" accesskey="u" rel="up">Variable Attributes</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  66. </div>
  67. <hr>
  68. <a name="AVR-Variable-Attributes-1"></a>
  69. <h4 class="subsection">6.32.2 AVR Variable Attributes</h4>
  70. <dl compact="compact">
  71. <dt><code>progmem</code></dt>
  72. <dd><a name="index-progmem-variable-attribute_002c-AVR"></a>
  73. <p>The <code>progmem</code> attribute is used on the AVR to place read-only
  74. data in the non-volatile program memory (flash). The <code>progmem</code>
  75. attribute accomplishes this by putting respective variables into a
  76. section whose name starts with <code>.progmem</code>.
  77. </p>
  78. <p>This attribute works similar to the <code>section</code> attribute
  79. but adds additional checking.
  80. </p>
  81. <dl compact="compact">
  82. <dt>&bull;&nbsp; Ordinary AVR cores with 32 general purpose registers:</dt>
  83. <dd><p><code>progmem</code> affects the location
  84. of the data but not how this data is accessed.
  85. In order to read data located with the <code>progmem</code> attribute
  86. (inline) assembler must be used.
  87. </p><div class="smallexample">
  88. <pre class="smallexample">/* Use custom macros from <a href="http://nongnu.org/avr-libc/user-manual/">AVR-LibC</a><!-- /@w --> */
  89. #include &lt;avr/pgmspace.h&gt;
  90. /* Locate var in flash memory */
  91. const int var[2] PROGMEM = { 1, 2 };
  92. int read_var (int i)
  93. {
  94. /* Access var[] by accessor macro from avr/pgmspace.h */
  95. return (int) pgm_read_word (&amp; var[i]);
  96. }
  97. </pre></div>
  98. <p>AVR is a Harvard architecture processor and data and read-only data
  99. normally resides in the data memory (RAM).
  100. </p>
  101. <p>See also the <a href="Named-Address-Spaces.html#AVR-Named-Address-Spaces">AVR Named Address Spaces</a> section for
  102. an alternate way to locate and access data in flash memory.
  103. </p>
  104. </dd>
  105. <dt>&bull;&nbsp;Reduced AVR Tiny cores like ATtiny40:</dt>
  106. <dd><p>The compiler adds <code>0x4000</code>
  107. to the addresses of objects and declarations in <code>progmem</code> and locates
  108. the objects in flash memory, namely in section <code>.progmem.data</code>.
  109. The offset is needed because the flash memory is visible in the RAM
  110. address space starting at address <code>0x4000</code>.
  111. </p>
  112. <p>Data in <code>progmem</code> can be accessed by means of ordinary C&nbsp;code,
  113. no special functions or macros are needed.
  114. </p>
  115. <div class="smallexample">
  116. <pre class="smallexample">/* var is located in flash memory */
  117. extern const int var[2] __attribute__((progmem));
  118. int read_var (int i)
  119. {
  120. return var[i];
  121. }
  122. </pre></div>
  123. <p>Please notice that on these devices, there is no need for <code>progmem</code>
  124. at all. Just use an appropriate linker description file like outlined below.
  125. </p>
  126. <div class="smallexample">
  127. <pre class="smallexample"> .text :
  128. { ...
  129. } &gt; text
  130. /* Leave .rodata in flash and add an offset of 0x4000 to all
  131. addresses so that respective objects can be accessed by
  132. LD instructions and open coded C/C++. This means there
  133. is no need for progmem in the source and no overhead by
  134. read-only data in RAM. */
  135. .rodata ADDR(.text) + SIZEOF (.text) + 0x4000 :
  136. {
  137. *(.rodata)
  138. *(.rodata*)
  139. *(.gnu.linkonce.r*)
  140. } AT&gt; text
  141. /* No more need to put .rodata into .data:
  142. Removed all .rodata entries from .data. */
  143. .data :
  144. { ...
  145. </pre></div>
  146. </dd>
  147. </dl>
  148. </dd>
  149. <dt><code>io</code></dt>
  150. <dt><code>io (<var>addr</var>)</code></dt>
  151. <dd><a name="index-io-variable-attribute_002c-AVR"></a>
  152. <p>Variables with the <code>io</code> attribute are used to address
  153. memory-mapped peripherals in the io address range.
  154. If an address is specified, the variable
  155. is assigned that address, and the value is interpreted as an
  156. address in the data address space.
  157. Example:
  158. </p>
  159. <div class="smallexample">
  160. <pre class="smallexample">volatile int porta __attribute__((io (0x22)));
  161. </pre></div>
  162. <p>The address specified in the address in the data address range.
  163. </p>
  164. <p>Otherwise, the variable it is not assigned an address, but the
  165. compiler will still use in/out instructions where applicable,
  166. assuming some other module assigns an address in the io address range.
  167. Example:
  168. </p>
  169. <div class="smallexample">
  170. <pre class="smallexample">extern volatile int porta __attribute__((io));
  171. </pre></div>
  172. </dd>
  173. <dt><code>io_low</code></dt>
  174. <dt><code>io_low (<var>addr</var>)</code></dt>
  175. <dd><a name="index-io_005flow-variable-attribute_002c-AVR"></a>
  176. <p>This is like the <code>io</code> attribute, but additionally it informs the
  177. compiler that the object lies in the lower half of the I/O area,
  178. allowing the use of <code>cbi</code>, <code>sbi</code>, <code>sbic</code> and <code>sbis</code>
  179. instructions.
  180. </p>
  181. </dd>
  182. <dt><code>address</code></dt>
  183. <dt><code>address (<var>addr</var>)</code></dt>
  184. <dd><a name="index-address-variable-attribute_002c-AVR"></a>
  185. <p>Variables with the <code>address</code> attribute are used to address
  186. memory-mapped peripherals that may lie outside the io address range.
  187. </p>
  188. <div class="smallexample">
  189. <pre class="smallexample">volatile int porta __attribute__((address (0x600)));
  190. </pre></div>
  191. </dd>
  192. <dt><code>absdata</code></dt>
  193. <dd><a name="index-absdata-variable-attribute_002c-AVR"></a>
  194. <p>Variables in static storage and with the <code>absdata</code> attribute can
  195. be accessed by the <code>LDS</code> and <code>STS</code> instructions which take
  196. absolute addresses.
  197. </p>
  198. <ul>
  199. <li> This attribute is only supported for the reduced AVR Tiny core
  200. like ATtiny40.
  201. </li><li> You must make sure that respective data is located in the
  202. address range <code>0x40</code>&hellip;<code>0xbf</code> accessible by
  203. <code>LDS</code> and <code>STS</code>. One way to achieve this as an
  204. appropriate linker description file.
  205. </li><li> If the location does not fit the address range of <code>LDS</code>
  206. and <code>STS</code>, there is currently (Binutils 2.26) just an unspecific
  207. warning like
  208. <blockquote>
  209. <p><code>module.c:(.text+0x1c): warning: internal error: out of range error</code>
  210. </p></blockquote>
  211. </li></ul>
  212. <p>See also the <samp>-mabsdata</samp> <a href="AVR-Options.html#AVR-Options">command-line option</a>.
  213. </p>
  214. </dd>
  215. </dl>
  216. <hr>
  217. <div class="header">
  218. <p>
  219. Next: <a href="Blackfin-Variable-Attributes.html#Blackfin-Variable-Attributes" accesskey="n" rel="next">Blackfin Variable Attributes</a>, Previous: <a href="Common-Variable-Attributes.html#Common-Variable-Attributes" accesskey="p" rel="prev">Common Variable Attributes</a>, Up: <a href="Variable-Attributes.html#Variable-Attributes" accesskey="u" rel="up">Variable Attributes</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  220. </div>
  221. </body>
  222. </html>