generic.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. #ifndef _TOOLCHAIN_GENERIC_H_
  2. #define _TOOLCHAIN_GENERIC_H_
  3. /*
  4. * Copyright (C) 2012 by egnite GmbH
  5. * Copyright (C) 2001-2005 by egnite Software GmbH
  6. *
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. Neither the name of the copyright holders nor the names of
  19. * contributors may be used to endorse or promote products derived
  20. * from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  29. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  30. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  31. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  32. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33. * SUCH DAMAGE.
  34. *
  35. * For additional information see http://www.ethernut.de/
  36. */
  37. #ifndef _TOOLCHAIN_H_
  38. #error Do not include this file directly, include <toolchain.h> instead.
  39. #endif
  40. #include <stdint.h>
  41. /*!
  42. * \name Convenience Macros
  43. */
  44. /*@{*/
  45. #ifndef _BV
  46. /*!
  47. * \brief Get bit mask for a given bit number.
  48. *
  49. * Using the convenience macro _BV(bit) is equivalent to (1 << bit),
  50. * but often makes the code more readable.
  51. *
  52. * Initially it had been provided by avr-libc, but was declared
  53. * deprecated and finally removed in later versions. Among Nut/OS
  54. * developers it is still preferred to the bitwise left shift and
  55. * used on all platforms.
  56. */
  57. #define _BV(bit) (1 << (bit))
  58. #endif
  59. #ifndef BV
  60. /* Deprecated version. */
  61. #define BV(bit) _BV(bit)
  62. #endif
  63. /*@}*/
  64. #ifndef INLINE
  65. /*!
  66. * \brief Deprecated inline function macro.
  67. *
  68. * This macro is functionally equivalent to \ref NUT_INLINE_FUNC and
  69. * kept for backward compatibility.
  70. */
  71. #define INLINE NUT_INLINE_FUNC
  72. #endif
  73. /*!
  74. * \name Traditional Register Access Macros
  75. */
  76. /*@{*/
  77. #ifndef _SFR_MEM8
  78. #define _SFR_MEM8(addr) (addr)
  79. #endif
  80. #ifndef _SFR_MEM16
  81. #define _SFR_MEM16(addr) (addr)
  82. #endif
  83. #ifndef outb
  84. /*!
  85. * \brief Write an 8-bit value to an I/O register.
  86. *
  87. * The default version assumes memory mapped registers and therefore
  88. * writes to a memory location. This default behavior may be overridden
  89. * by a target specific implementation.
  90. *
  91. * A special version is provided for 8-bit AVR targets to generate
  92. * optimized instruction code for registers in IO space. However,
  93. * to make this work, the register address must be a constant.
  94. *
  95. * \todo Although heavily used in Nut/OS, it should be replaced in long
  96. * term by XXX to avoid problems with compiler optimization.
  97. */
  98. #define outb(_reg, _val) (*((volatile unsigned char *)(_reg)) = (_val))
  99. #endif
  100. #ifndef outw
  101. /*!
  102. * \brief Write an 16-bit value to an I/O register.
  103. *
  104. * The default version assumes memory mapped registers and therefore
  105. * writes to a memory location. This default behavior may be overridden
  106. * by a target specific implementation.
  107. *
  108. * Note, that this instruction is not atomic on 8-bit targets. It may
  109. * be required to disable interrupts first.
  110. *
  111. * A special version is provided for AVR targets to make sure that
  112. * the low byte is written first. Also see the AVR specific note
  113. * for \ref outb.
  114. *
  115. * \todo Although heavily used in Nut/OS, it should be replaced in long
  116. * term by XXX to avoid problems with compiler optimization.
  117. */
  118. #define outw(_reg, _val) (*((volatile unsigned short *)(_reg)) = (_val))
  119. #endif
  120. #ifndef outr
  121. /*!
  122. * \brief Write to an I/O register.
  123. *
  124. * The default version assumes memory mapped registers and therefore
  125. * writes to a memory location. This default behavior may be overridden
  126. * by a target specific implementation.
  127. *
  128. * The size of the value should be equal to the native number of register
  129. * bits on the target. However, this is currently implemented on 32-bit
  130. * targets only. Do not use this function on other architectures.
  131. *
  132. * \todo Although heavily used in Nut/OS, it should be replaced in long
  133. * term by XXX to avoid problems with compiler optimization.
  134. */
  135. #define outr(_reg, _val) (*((volatile unsigned long *)(_reg)) = (_val))
  136. #endif
  137. #ifndef inb
  138. /*!
  139. * \brief Read an 8-bit value from an I/O register.
  140. *
  141. * The default version reads from a memory location, but may be
  142. * overridden by a target specific implementation.
  143. *
  144. * A special version is provided for 8-bit AVR targets to generate
  145. * optimized instruction code for registers in IO space. However,
  146. * to make this work, the register address must be a constant.
  147. *
  148. * \todo Although heavily used in Nut/OS, it should be replaced in long
  149. * term by XXX to avoid problems with compiler optimization.
  150. */
  151. #define inb(_reg) (*((volatile unsigned char *)(_reg)))
  152. #endif
  153. #ifndef inw
  154. /*!
  155. * \brief Read a 16-bit value from an I/O register.
  156. *
  157. * The default version reads from a memory location, but may be
  158. * overridden by a target specific implementation.
  159. *
  160. * See the target specific note for \ref outw.
  161. *
  162. * \todo Although heavily used in Nut/OS, it should be replaced in long
  163. * term by XXX to avoid problems with compiler optimization.
  164. */
  165. #define inw(_reg) (*((volatile unsigned short *)(_reg)))
  166. #endif
  167. #ifndef inr
  168. /*!
  169. * \brief Read from an I/O register.
  170. *
  171. * The default version reads from a memory location, but may be
  172. * overridden by a target specific implementation.
  173. *
  174. * The size of the value should be equal to the native number of register
  175. * bits on the target. However, this is currently implemented on 32-bit
  176. * targets only. Do not use this function on other architectures.
  177. *
  178. * \todo Although heavily used in Nut/OS, it should be replaced in long
  179. * term by XXX to avoid problems with compiler optimization.
  180. */
  181. #define inr(_reg) (*((volatile unsigned long *)(_reg)))
  182. #endif
  183. #ifndef sbi
  184. /*!
  185. * \brief Set bit in an I/O register.
  186. *
  187. * The default version assumes memory mapped 32-bit registers and reads
  188. * the current value from a memory location, sets the given bit to 1 and
  189. * writes the 32-bit value back to the same memory location. This default
  190. * behavior may be overridden by a target specific implementation.
  191. *
  192. * A special version is provided for 8-bit AVR targets to generate
  193. * an atomic bit set instruction for registers in IO space. However, to
  194. * make this work, the register address must be a constant.
  195. */
  196. #define sbi(_reg, _bit) outr(_reg, inr(_reg) | _BV(_bit))
  197. #endif
  198. #ifndef cbi
  199. /*!
  200. * \brief Clear bit in an I/O register.
  201. *
  202. * The default version assumes memory mapped 32-bit registers and reads
  203. * the current value from a memory location, clears the given bit to 0
  204. * and writes the 32-bit value back to the same memory location. This
  205. * default behavior may be overridden by a target specific
  206. * implementation.
  207. *
  208. * A special version is provided for 8-bit AVR targets to generate
  209. * an atomic bit set instruction for registers in IO space. However,
  210. * to make this work, the register address must be a constant.
  211. */
  212. #define cbi(_reg, _bit) outr(_reg, inr(_reg) & ~_BV(_bit))
  213. #endif
  214. #ifndef bit_is_set
  215. /*!
  216. * \brief Test bit in an I/O register.
  217. *
  218. * Returns 1 if the bit is set to 1, or 0 if the bit is cleared to 0.
  219. *
  220. * The default version assumes memory mapped 32-bit registers and reads
  221. * the current value from a memory location to check the given bit. This
  222. * default behavior may be overridden by a target specific
  223. * implementation.
  224. *
  225. * A special version is provided for 8-bit AVR targets to generate a
  226. * bit test instruction for registers in IO space. However, to make
  227. * this work, the register address must be a constant.
  228. */
  229. #define bit_is_set(_reg, _bit) ((inr(_reg) & _BV(_bit)) != 0)
  230. #endif
  231. #ifndef mem_wr
  232. /*!
  233. * \brief Write unsigned integer value to memory location.
  234. *
  235. * The function typically is used to access memory mapped hardware
  236. * registers.
  237. *
  238. * \note Although fairly unlikely, the compiler may re-order memory
  239. * accesses. If the order is important, you must use mem_wr_mb()
  240. * instead.
  241. */
  242. static NUT_INLINE_FUNC void mem_wr(unsigned int reg, unsigned int val)
  243. {
  244. *(volatile unsigned int *) reg = val;
  245. }
  246. #endif
  247. #ifndef mem_wr8
  248. /*!
  249. * \brief Write unsigned 8-bit value to memory location.
  250. *
  251. * See \ref mem_wr
  252. */
  253. static NUT_INLINE_FUNC void mem_wr8(unsigned int reg, uint8_t val)
  254. {
  255. *(volatile uint8_t *) reg = val;
  256. }
  257. #endif
  258. #ifndef mem_wr16
  259. /*!
  260. * \brief Write unsigned 16-bit value to memory location.
  261. *
  262. * See \ref mem_wr
  263. */
  264. static NUT_INLINE_FUNC void mem_wr16(unsigned int reg, uint16_t val)
  265. {
  266. *(volatile uint16_t *) reg = val;
  267. }
  268. #endif
  269. #ifndef mem_wr32
  270. /*!
  271. * \brief Write unsigned 32-bit value to memory location.
  272. *
  273. * See \ref mem_wr
  274. */
  275. static NUT_INLINE_FUNC void mem_wr32(unsigned int reg, uint32_t val)
  276. {
  277. *(volatile uint32_t *) reg = val;
  278. }
  279. #endif
  280. #ifndef mem_rd
  281. /*!
  282. * \brief Read unsigned integer value from memory location.
  283. *
  284. * The function typically is used to access memory mapped hardware
  285. * registers.
  286. *
  287. * \note Although fairly unlikely, the compiler may re-order memory
  288. * accesses. If the order is important, you must use mem_rd_mb()
  289. * instead.
  290. */
  291. static NUT_INLINE_FUNC unsigned int mem_rd(unsigned int reg)
  292. {
  293. return *(const volatile unsigned int *) reg;
  294. }
  295. #endif
  296. #ifndef mem_rd8
  297. /*!
  298. * \brief Read unsigned 8-bit value from memory location.
  299. *
  300. * See \ref mem_rd.
  301. */
  302. static NUT_INLINE_FUNC uint8_t mem_rd8(unsigned int reg)
  303. {
  304. return *(const volatile uint8_t *) reg;
  305. }
  306. #endif
  307. #ifndef mem_rd16
  308. /*!
  309. * \brief Read unsigned 16-bit value from memory location.
  310. *
  311. * See \ref mem_rd.
  312. */
  313. static NUT_INLINE_FUNC uint16_t mem_rd16(unsigned int reg)
  314. {
  315. return *(const volatile uint16_t *) reg;
  316. }
  317. #endif
  318. #ifndef mem_rd32
  319. /*!
  320. * \brief Read unsigned 32-bit value from memory location.
  321. *
  322. * See \ref mem_rd.
  323. */
  324. static NUT_INLINE_FUNC uint32_t mem_rd32(unsigned int reg)
  325. {
  326. return *(const volatile uint32_t *) reg;
  327. }
  328. #endif
  329. #ifndef mem_wr_mb
  330. /*!
  331. * \brief Sequentially write unsigned integer value to memory location.
  332. *
  333. * The function typically is used to access memory mapped hardware
  334. * registers in a serialized way. It makes sure, that all preceding
  335. * reads and writes have completed before writing to the specified
  336. * address.
  337. */
  338. static NUT_INLINE_FUNC void mem_wr_mb(unsigned int reg, unsigned int val)
  339. {
  340. mem_barrier();
  341. mem_wr(reg, val);
  342. }
  343. #endif
  344. #ifndef mem_wr8_mb
  345. /*!
  346. * \brief Sequentially write unsigned 8-bit value to memory location.
  347. *
  348. * See \ref mem_wr_mb.
  349. */
  350. static NUT_INLINE_FUNC void mem_wr8_mb(unsigned int reg, uint8_t val)
  351. {
  352. mem_barrier();
  353. mem_wr8(reg, val);
  354. }
  355. #endif
  356. #ifndef mem_wr16_mb
  357. /*!
  358. * \brief Sequentially write unsigned 16-bit value to memory location.
  359. *
  360. * See \ref mem_wr_mb.
  361. */
  362. static NUT_INLINE_FUNC void mem_wr16_mb(unsigned int reg, uint16_t val)
  363. {
  364. mem_barrier();
  365. mem_wr16(reg, val);
  366. }
  367. #endif
  368. #ifndef mem_wr32_mb
  369. /*!
  370. * \brief Sequentially write unsigned 32-bit value to memory location.
  371. *
  372. * See \ref mem_wr_mb.
  373. */
  374. static NUT_INLINE_FUNC void mem_wr32_mb(unsigned int reg, uint32_t val)
  375. {
  376. mem_barrier();
  377. mem_wr32(reg, val);
  378. }
  379. #endif
  380. #ifndef mem_rd_mb
  381. /*!
  382. * \brief Immediately read unsigned integer value from memory location.
  383. *
  384. * The function typically is used to access memory mapped hardware
  385. * registers in a serialized way. It makes sure, that this read and
  386. * all preceding reads and writes have completed before the function
  387. * returns.
  388. */
  389. static NUT_INLINE_FUNC unsigned int mem_rd_mb(unsigned int reg)
  390. {
  391. unsigned int rc = mem_rd(reg);
  392. mem_barrier();
  393. return rc;
  394. }
  395. #endif
  396. #ifndef mem_rd8_mb
  397. /*!
  398. * \brief Immediately read unsigned 8-bit value from memory location.
  399. *
  400. * See \ref mem_rd_mb.
  401. */
  402. static NUT_INLINE_FUNC uint8_t mem_rd8_mb(unsigned int reg)
  403. {
  404. uint8_t rc = mem_rd8(reg);
  405. mem_barrier();
  406. return rc;
  407. }
  408. #endif
  409. #ifndef mem_rd16_mb
  410. /*!
  411. * \brief Immediately read unsigned 16-bit value from memory location.
  412. *
  413. * See \ref mem_rd_mb.
  414. */
  415. static NUT_INLINE_FUNC uint16_t mem_rd16_mb(unsigned int reg)
  416. {
  417. uint16_t rc = mem_rd16(reg);
  418. mem_barrier();
  419. return rc;
  420. }
  421. #endif
  422. #ifndef mem_rd32_mb
  423. /*!
  424. * \brief Immediately read unsigned 32-bit value from memory location.
  425. *
  426. * See \ref mem_rd_mb.
  427. */
  428. static NUT_INLINE_FUNC uint32_t mem_rd32_mb(unsigned int reg)
  429. {
  430. uint32_t rc = mem_rd32(reg);
  431. mem_barrier();
  432. return rc;
  433. }
  434. #endif
  435. #ifndef PSTR
  436. /*!
  437. * \brief Declare static pointer to a string in program space.
  438. *
  439. * This macro is used to place string literals in code space. This
  440. * makes a lot of sense on Harvard architectures, where program space
  441. * is typically much larger than data space.
  442. *
  443. * Example:
  444. * \code
  445. * printf_P(PSTR("Hello world!\n"));
  446. * \endcode
  447. *
  448. * Unfortunately this will only work, if the compiler allows to
  449. * declare static pointers inside expressions, which is a non-
  450. * standard C extension.
  451. *
  452. * On other architectures, this macro will simply return a pointer
  453. * to the given string literal. Because printf_P will be directly
  454. * mapped to printf, on non-Harvard architectures the example above
  455. * will become
  456. *
  457. * \code
  458. * printf(("Hello world!\n"));
  459. * \endcode
  460. */
  461. #define PSTR(p) (p)
  462. #endif
  463. #ifndef PRG_RDB
  464. /*!
  465. * \brief Read data byte from program space.
  466. *
  467. * This macro simply returns the data byte located at the given address.
  468. * However, if used on Harvard architectures, the memory address is
  469. * expected to be located in code space.
  470. */
  471. #define PRG_RDB(p) (*((const char *)(p)))
  472. #ifndef prog_char
  473. /*!
  474. * \brief Declare a character variable located in program space.
  475. *
  476. * This macro affects variables on Harvard architectures only. On other
  477. * architectures it simply declares a constant character variable.
  478. */
  479. //#define prog_char const char
  480. #endif
  481. #ifndef PGM_P
  482. /*!
  483. * \brief Declare a pointer variable to a string in program space.
  484. *
  485. * This macro affects variables on Harvard architectures only. On other
  486. * architectures it simply declares a pointer to a constant character.
  487. */
  488. #define PGM_P prog_char *
  489. #endif
  490. #ifndef __GNUC__
  491. /*!
  492. * \brief Remove object attributes.
  493. *
  494. * Unfortunately the Nut/OS code uses GCC attributes. Until we replaced
  495. * them entirely by our new feature macros, this macro removes them for
  496. * all other compilers.
  497. */
  498. #define __attribute__(x)
  499. #endif
  500. #endif
  501. #ifndef __HARVARD_ARCH__
  502. #ifndef strlen_P
  503. #define strlen_P(x) strlen(x)
  504. #endif
  505. #ifndef strcpy_P
  506. #define strcpy_P(x, y) strcpy(x,y)
  507. #endif
  508. #ifndef strcat_P
  509. #define strcat_P(x, y) strcat(x, y)
  510. #endif
  511. #ifndef strcmp_P
  512. #define strcmp_P(x, y) strcmp(x, y)
  513. #endif
  514. #ifndef memcpy_P
  515. #define memcpy_P(x, y, z) memcpy(x, y, z)
  516. #endif
  517. #endif
  518. #endif