Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

AVR-Variable-Attributes.html 10KB

3 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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-2020 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 6.5, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  20. <title>AVR Variable Attributes (Using the GNU Compiler Collection (GCC))</title>
  21. <meta name="description" content="AVR Variable Attributes (Using the GNU Compiler Collection (GCC))">
  22. <meta name="keywords" content="AVR Variable Attributes (Using the GNU Compiler Collection (GCC))">
  23. <meta name="resource-type" content="document">
  24. <meta name="distribution" content="global">
  25. <meta name="Generator" content="makeinfo">
  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="ARC-Variable-Attributes.html#ARC-Variable-Attributes" rel="prev" title="ARC Variable Attributes">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.indentedblock {margin-right: 0em}
  36. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  37. blockquote.smallquotation {font-size: smaller}
  38. div.display {margin-left: 3.2em}
  39. div.example {margin-left: 3.2em}
  40. div.lisp {margin-left: 3.2em}
  41. div.smalldisplay {margin-left: 3.2em}
  42. div.smallexample {margin-left: 3.2em}
  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.nolinebreak {white-space: nowrap}
  54. span.roman {font-family: initial; font-weight: normal}
  55. span.sansserif {font-family: sans-serif; font-weight: normal}
  56. ul.no-bullet {list-style: none}
  57. -->
  58. </style>
  59. </head>
  60. <body lang="en">
  61. <a name="AVR-Variable-Attributes"></a>
  62. <div class="header">
  63. <p>
  64. Next: <a href="Blackfin-Variable-Attributes.html#Blackfin-Variable-Attributes" accesskey="n" rel="next">Blackfin Variable Attributes</a>, Previous: <a href="ARC-Variable-Attributes.html#ARC-Variable-Attributes" accesskey="p" rel="prev">ARC 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>
  65. </div>
  66. <hr>
  67. <a name="AVR-Variable-Attributes-1"></a>
  68. <h4 class="subsection">6.34.3 AVR Variable Attributes</h4>
  69. <dl compact="compact">
  70. <dt><code>progmem</code></dt>
  71. <dd><a name="index-progmem-variable-attribute_002c-AVR"></a>
  72. <p>The <code>progmem</code> attribute is used on the AVR to place read-only
  73. data in the non-volatile program memory (flash). The <code>progmem</code>
  74. attribute accomplishes this by putting respective variables into a
  75. section whose name starts with <code>.progmem</code>.
  76. </p>
  77. <p>This attribute works similar to the <code>section</code> attribute
  78. but adds additional checking.
  79. </p>
  80. <dl compact="compact">
  81. <dt>&bull;&nbsp; Ordinary AVR cores with 32 general purpose registers:</dt>
  82. <dd><p><code>progmem</code> affects the location
  83. of the data but not how this data is accessed.
  84. In order to read data located with the <code>progmem</code> attribute
  85. (inline) assembler must be used.
  86. </p><div class="smallexample">
  87. <pre class="smallexample">/* Use custom macros from <a href="http://nongnu.org/avr-libc/user-manual/">AVR-LibC</a><!-- /@w --> */
  88. #include &lt;avr/pgmspace.h&gt;
  89. /* Locate var in flash memory */
  90. const int var[2] PROGMEM = { 1, 2 };
  91. int read_var (int i)
  92. {
  93. /* Access var[] by accessor macro from avr/pgmspace.h */
  94. return (int) pgm_read_word (&amp; var[i]);
  95. }
  96. </pre></div>
  97. <p>AVR is a Harvard architecture processor and data and read-only data
  98. normally resides in the data memory (RAM).
  99. </p>
  100. <p>See also the <a href="Named-Address-Spaces.html#AVR-Named-Address-Spaces">AVR Named Address Spaces</a> section for
  101. an alternate way to locate and access data in flash memory.
  102. </p>
  103. </dd>
  104. <dt>&bull;&nbsp; AVR cores with flash memory visible in the RAM address range:</dt>
  105. <dd><p>On such devices, there is no need for attribute <code>progmem</code> or
  106. <a href="Named-Address-Spaces.html#AVR-Named-Address-Spaces"><code>__flash</code></a> qualifier at all.
  107. Just use standard C / C++. The compiler will generate <code>LD*</code>
  108. instructions. As flash memory is visible in the RAM address range,
  109. and the default linker script does <em>not</em> locate <code>.rodata</code> in
  110. RAM, no special features are needed in order not to waste RAM for
  111. read-only data or to read from flash. You might even get slightly better
  112. performance by
  113. avoiding <code>progmem</code> and <code>__flash</code>. This applies to devices from
  114. families <code>avrtiny</code> and <code>avrxmega3</code>, see <a href="AVR-Options.html#AVR-Options">AVR Options</a> for
  115. an overview.
  116. </p>
  117. </dd>
  118. <dt>&bull;&nbsp;Reduced AVR Tiny cores like ATtiny40:</dt>
  119. <dd><p>The compiler adds <code>0x4000</code>
  120. to the addresses of objects and declarations in <code>progmem</code> and locates
  121. the objects in flash memory, namely in section <code>.progmem.data</code>.
  122. The offset is needed because the flash memory is visible in the RAM
  123. address space starting at address <code>0x4000</code>.
  124. </p>
  125. <p>Data in <code>progmem</code> can be accessed by means of ordinary C&nbsp;code,
  126. no special functions or macros are needed.
  127. </p>
  128. <div class="smallexample">
  129. <pre class="smallexample">/* var is located in flash memory */
  130. extern const int var[2] __attribute__((progmem));
  131. int read_var (int i)
  132. {
  133. return var[i];
  134. }
  135. </pre></div>
  136. <p>Please notice that on these devices, there is no need for <code>progmem</code>
  137. at all.
  138. </p>
  139. </dd>
  140. </dl>
  141. </dd>
  142. <dt><code>io</code></dt>
  143. <dt><code>io (<var>addr</var>)</code></dt>
  144. <dd><a name="index-io-variable-attribute_002c-AVR"></a>
  145. <p>Variables with the <code>io</code> attribute are used to address
  146. memory-mapped peripherals in the io address range.
  147. If an address is specified, the variable
  148. is assigned that address, and the value is interpreted as an
  149. address in the data address space.
  150. Example:
  151. </p>
  152. <div class="smallexample">
  153. <pre class="smallexample">volatile int porta __attribute__((io (0x22)));
  154. </pre></div>
  155. <p>The address specified in the address in the data address range.
  156. </p>
  157. <p>Otherwise, the variable it is not assigned an address, but the
  158. compiler will still use in/out instructions where applicable,
  159. assuming some other module assigns an address in the io address range.
  160. Example:
  161. </p>
  162. <div class="smallexample">
  163. <pre class="smallexample">extern volatile int porta __attribute__((io));
  164. </pre></div>
  165. </dd>
  166. <dt><code>io_low</code></dt>
  167. <dt><code>io_low (<var>addr</var>)</code></dt>
  168. <dd><a name="index-io_005flow-variable-attribute_002c-AVR"></a>
  169. <p>This is like the <code>io</code> attribute, but additionally it informs the
  170. compiler that the object lies in the lower half of the I/O area,
  171. allowing the use of <code>cbi</code>, <code>sbi</code>, <code>sbic</code> and <code>sbis</code>
  172. instructions.
  173. </p>
  174. </dd>
  175. <dt><code>address</code></dt>
  176. <dt><code>address (<var>addr</var>)</code></dt>
  177. <dd><a name="index-address-variable-attribute_002c-AVR"></a>
  178. <p>Variables with the <code>address</code> attribute are used to address
  179. memory-mapped peripherals that may lie outside the io address range.
  180. </p>
  181. <div class="smallexample">
  182. <pre class="smallexample">volatile int porta __attribute__((address (0x600)));
  183. </pre></div>
  184. </dd>
  185. <dt><code>absdata</code></dt>
  186. <dd><a name="index-absdata-variable-attribute_002c-AVR"></a>
  187. <p>Variables in static storage and with the <code>absdata</code> attribute can
  188. be accessed by the <code>LDS</code> and <code>STS</code> instructions which take
  189. absolute addresses.
  190. </p>
  191. <ul>
  192. <li> This attribute is only supported for the reduced AVR Tiny core
  193. like ATtiny40.
  194. </li><li> You must make sure that respective data is located in the
  195. address range <code>0x40</code>&hellip;<code>0xbf</code> accessible by
  196. <code>LDS</code> and <code>STS</code>. One way to achieve this as an
  197. appropriate linker description file.
  198. </li><li> If the location does not fit the address range of <code>LDS</code>
  199. and <code>STS</code>, there is currently (Binutils 2.26) just an unspecific
  200. warning like
  201. <blockquote>
  202. <p><code>module.c:(.text+0x1c): warning: internal error: out of range error</code>
  203. </p></blockquote>
  204. </li></ul>
  205. <p>See also the <samp>-mabsdata</samp> <a href="AVR-Options.html#AVR-Options">command-line option</a>.
  206. </p>
  207. </dd>
  208. </dl>
  209. <hr>
  210. <div class="header">
  211. <p>
  212. Next: <a href="Blackfin-Variable-Attributes.html#Blackfin-Variable-Attributes" accesskey="n" rel="next">Blackfin Variable Attributes</a>, Previous: <a href="ARC-Variable-Attributes.html#ARC-Variable-Attributes" accesskey="p" rel="prev">ARC 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>
  213. </div>
  214. </body>
  215. </html>