charlcd_at91.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * Copyright (C) 2007 by egnite Software GmbH. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. *
  32. */
  33. /*
  34. * $Log$
  35. * Revision 1.6 2009/01/17 11:26:37 haraldkipp
  36. * Getting rid of two remaining BSD types in favor of stdint.
  37. * Replaced 'u_int' by 'unsinged int' and 'uptr_t' by 'uintptr_t'.
  38. *
  39. * Revision 1.5 2008/08/11 06:59:13 haraldkipp
  40. * BSD types replaced by stdint types (feature request #1282721).
  41. *
  42. * Revision 1.4 2008/06/06 10:28:21 haraldkipp
  43. * ST7036 LCD controller settings moved from source to configuration files.
  44. *
  45. * Revision 1.3 2008/02/15 16:59:02 haraldkipp
  46. * Spport for AT91SAM7SE512 added.
  47. *
  48. * Revision 1.2 2007/10/04 20:01:50 olereinhardt
  49. * Support for SAM7S256 added
  50. *
  51. * Revision 1.1 2007/02/15 16:09:07 haraldkipp
  52. * Tested with EA DOG-M LCDs.
  53. *
  54. */
  55. #include <cfg/arch.h>
  56. #include <cfg/arch/gpio.h>
  57. #include <cfg/lcd.h>
  58. #include <stdlib.h>
  59. #include <string.h>
  60. #include <sys/nutconfig.h>
  61. #include <dev/st7036.h>
  62. #include <dev/term.h>
  63. #include <sys/timer.h>
  64. #ifndef LCD_ROWS
  65. #define LCD_ROWS 2
  66. #endif
  67. #ifndef LCD_COLS
  68. #define LCD_COLS 16
  69. #endif
  70. #ifndef LCD_E2E_DLY
  71. #define LCD_E2E_DLY 80
  72. #endif
  73. #ifndef LCD_LONG_DELAY
  74. #define LCD_LONG_DELAY 1000
  75. #endif
  76. #if !defined(LCD_IF_8BIT) && !defined(LCD_IF_4BIT)
  77. #define LCD_IF_4BIT
  78. #endif
  79. /*
  80. * Fallback definition for PIO_IDs and bits
  81. */
  82. #ifndef LCD_EN_PIO_ID
  83. #if defined(MCU_AT91SAM7X) || defined (MCU_AT91SAM7S256) || defined (MCU_AT91SAM7SE512)
  84. #define LCD_EN_PIO_ID PIOA_ID
  85. #elif defined(MCU_AT91SAM9260) || defined(MCU_AT91SAM9XE512)
  86. #define LCD_CS_PIO_ID PIOB_ID
  87. #else
  88. #define LCD_CS_PIO_ID PIO_ID
  89. #endif
  90. #endif
  91. #ifndef LCD_EN_PIO_ID
  92. #if defined(MCU_AT91SAM7X) || defined (MCU_AT91SAM7S256) || defined (MCU_AT91SAM7SE512)
  93. #define LCD_EN_PIO_ID PIOA_ID
  94. #elif defined(MCU_AT91SAM9260) || defined(MCU_AT91SAM9XE512)
  95. #define LCD_EN_PIO_ID PIOB_ID
  96. #else
  97. #define LCD_EN_PIO_ID PIO_ID
  98. #endif
  99. #endif
  100. #ifndef LCD_RW_PIO_ID
  101. #if defined(MCU_AT91SAM7X) || defined (MCU_AT91SAM7S256) || defined (MCU_AT91SAM7SE512)
  102. #define LCD_RW_PIO_ID PIOA_ID
  103. #elif defined(MCU_AT91SAM9260) || defined(MCU_AT91SAM9XE512)
  104. #define LCD_RW_PIO_ID PIOB_ID
  105. #else
  106. #define LCD_RW_PIO_ID PIO_ID
  107. #endif
  108. #endif
  109. #ifndef LCD_RS_PIO_ID
  110. #if defined(MCU_AT91SAM7X) || defined (MCU_AT91SAM7S256) || defined (MCU_AT91SAM7SE512)
  111. #define LCD_RS_PIO_ID PIOA_ID
  112. #elif defined(MCU_AT91SAM9260) || defined(MCU_AT91SAM9XE512)
  113. #define LCD_RS_PIO_ID PIOB_ID
  114. #else
  115. #define LCD_RS_PIO_ID PIO_ID
  116. #endif
  117. #endif
  118. #ifndef LCD_DATA_PIO_ID
  119. #if defined(MCU_AT91SAM7X) || defined (MCU_AT91SAM7S256) || defined (MCU_AT91SAM7SE512)
  120. #define LCD_DATA_PIO_ID PIOA_ID
  121. #elif defined(MCU_AT91SAM9260) || defined(MCU_AT91SAM9XE512)
  122. #define LCD_DATA_PIO_ID PIOB_ID
  123. #else
  124. #define LCD_DATA_PIO_ID PIO_ID
  125. #endif
  126. #endif
  127. /*
  128. * Control lines abstraction.
  129. */
  130. #ifdef LCD_EN_BIT
  131. #define LCD_CS _BV(LCD_EN_BIT)
  132. #if LCD_EN_PIO_ID == PIOA_ID
  133. #define LCD_EN_SET() { outr(PIOA_PER, LCD_CS); outr(PIOA_SODR, LCD_CS); outr(PIOA_OER, LCD_CS); }
  134. #define LCD_EN_CLR() { outr(PIOA_PER, LCD_CS); outr(PIOA_CODR, LCD_CS); outr(PIOA_OER, LCD_CS); }
  135. #elif LCD_EN_PIO_ID == PIOB_ID
  136. #define LCD_EN_SET() { outr(PIOB_PER, LCD_CS); outr(PIOB_SODR, LCD_CS); outr(PIOB_OER, LCD_CS); }
  137. #define LCD_EN_CLR() { outr(PIOB_PER, LCD_CS); outr(PIOB_CODR, LCD_CS); outr(PIOB_OER, LCD_CS); }
  138. #elif LCD_EN_PIO_ID == PIOC_ID
  139. #define LCD_EN_SET() { outr(PIOC_PER, LCD_CS); outr(PIOC_SODR, LCD_CS); outr(PIOC_OER, LCD_CS); }
  140. #define LCD_CS_CLR() { outr(PIOC_PER, LCD_CS); outr(PIOC_CODR, LCD_CS); outr(PIOC_OER, LCD_CS); }
  141. #else
  142. #define LCD_EN_SET() { outr(PIO_PER, LCD_CS); outr(PIO_SODR, LCD_CS); outr(PIO_OER, LCD_CS); }
  143. #define LCD_EN_CLR() { outr(PIO_PER, LCD_CS); outr(PIO_CODR, LCD_CS); outr(PIO_OER, LCD_CS); }
  144. #endif /* LCD_EN_PIO_ID */
  145. #else /* LCD_EN_BIT */
  146. #define LCD_EN_SET()
  147. #define LCD_EN_CLR()
  148. #endif /* LCD_EN_BIT */
  149. #ifdef LCD_RS_BIT
  150. #define LCD_RS _BV(LCD_RS_BIT)
  151. #if LCD_RS_PIO_ID == PIOA_ID
  152. #define LCD_RS_SET() { outr(PIOA_PER, LCD_RS); outr(PIOA_SODR, LCD_RS); outr(PIOA_OER, LCD_RS); }
  153. #define LCD_RS_CLR() { outr(PIOA_PER, LCD_RS); outr(PIOA_CODR, LCD_RS); outr(PIOA_OER, LCD_RS); }
  154. #elif LCD_RS_PIO_ID == PIOB_ID
  155. #define LCD_RS_SET() { outr(PIOB_PER, LCD_RS); outr(PIOB_SODR, LCD_RS); outr(PIOB_OER, LCD_RS); }
  156. #define LCD_RS_CLR() { outr(PIOB_PER, LCD_RS); outr(PIOB_CODR, LCD_RS); outr(PIOB_OER, LCD_RS); }
  157. #elif LCD_RS_PIO_ID == PIOC_ID
  158. #define LCD_RS_SET() { outr(PIOC_PER, LCD_RS); outr(PIOC_SODR, LCD_RS); outr(PIOC_OER, LCD_RS); }
  159. #define LCD_RS_CLR() { outr(PIOC_PER, LCD_RS); outr(PIOC_CODR, LCD_RS); outr(PIOC_OER, LCD_RS); }
  160. #else
  161. #define LCD_RS_SET() { outr(PIO_PER, LCD_RS); outr(PIO_SODR, LCD_RS); outr(PIO_OER, LCD_RS); }
  162. #define LCD_RS_CLR() { outr(PIO_PER, LCD_RS); outr(PIO_CODR, LCD_RS); outr(PIO_OER, LCD_RS); }
  163. #endif /* LCD_RS_PIO_ID */
  164. #else /* LCD_RS_BIT */
  165. #define LCD_RS_SET()
  166. #define LCD_RS_CLR()
  167. #endif /* LCD_RS_BIT */
  168. #ifdef LCD_RW_BIT
  169. #define LCD_RW _BV(LCD_RW_BIT)
  170. #if LCD_RS_PIO_ID == PIOA_ID
  171. #define LCD_RW_SET() { outr(PIOA_PER, LCD_RS); outr(PIOA_SODR, LCD_RS); outr(PIOA_OER, LCD_RS); }
  172. #define LCD_RW_CLR() { outr(PIOA_PER, LCD_RS); outr(PIOA_CODR, LCD_RS); outr(PIOA_OER, LCD_RS); }
  173. #elif LCD_RS_PIO_ID == PIOB_ID
  174. #define LCD_RW_SET() { outr(PIOB_PER, LCD_RS); outr(PIOB_SODR, LCD_RS); outr(PIOB_OER, LCD_RS); }
  175. #define LCD_RW_CLR() { outr(PIOB_PER, LCD_RS); outr(PIOB_CODR, LCD_RS); outr(PIOB_OER, LCD_RS); }
  176. #elif LCD_RS_PIO_ID == PIOC_ID
  177. #define LCD_RW_SET() { outr(PIOC_PER, LCD_RS); outr(PIOC_SODR, LCD_RS); outr(PIOC_OER, LCD_RS); }
  178. #define LCD_RW_CLR() { outr(PIOC_PER, LCD_RS); outr(PIOC_CODR, LCD_RS); outr(PIOC_OER, LCD_RS); }
  179. #else
  180. #define LCD_RW_SET() { outr(PIO_PER, LCD_RS); outr(PIO_SODR, LCD_RS); outr(PIO_OER, LCD_RS); }
  181. #define LCD_RW_CLR() { outr(PIO_PER, LCD_RS); outr(PIO_CODR, LCD_RS); outr(PIO_OER, LCD_RS); }
  182. #endif /* LCD_RW_PIO_ID */
  183. #else /* LCD_RW_BIT */
  184. #define LCD_RW_SET()
  185. #define LCD_RW_CLR()
  186. #endif /* LCD_RW_BIT */
  187. #ifdef LCD_EN2_BIT
  188. #define LCD_EN2 _BV(LCD_EN2_BIT)
  189. #if LCD_EN2_PIO_ID == PIOA_ID
  190. #define LCD_EN2_SET() { outr(PIOA_PER, LCD_EN2); outr(PIOA_SODR, LCD_EN2); outr(PIOA_OER, LCD_EN2); }
  191. #define LCD_EN2_CLR() { outr(PIOA_PER, LCD_EN2); outr(PIOA_CODR, LCD_EN2); outr(PIOA_OER, LCD_EN2); }
  192. #elif LCD_EN2_PIO_ID == PIOB_ID
  193. #define LCD_EN2_SET() { outr(PIOB_PER, LCD_EN2); outr(PIOB_SODR, LCD_EN2); outr(PIOB_OER, LCD_EN2); }
  194. #define LCD_EN2_CLR() { outr(PIOB_PER, LCD_EN2); outr(PIOB_CODR, LCD_EN2); outr(PIOB_OER, LCD_EN2); }
  195. #elif LCD_EN2_PIO_ID == PIOC_ID
  196. #define LCD_EN2_SET() { outr(PIOC_PER, LCD_EN2); outr(PIOC_SODR, LCD_EN2); outr(PIOC_OER, LCD_EN2); }
  197. #define LCD_EN2_CLR() { outr(PIOC_PER, LCD_EN2); outr(PIOC_CODR, LCD_EN2); outr(PIOC_OER, LCD_EN2); }
  198. #else
  199. #define LCD_EN2_SET() { outr(PIO_PER, LCD_EN2); outr(PIO_SODR, LCD_EN2); outr(PIO_OER, LCD_EN2); }
  200. #define LCD_EN2_CLR() { outr(PIO_PER, LCD_EN2); outr(PIO_CODR, LCD_EN2); outr(PIO_OER, LCD_EN2); }
  201. #endif /* LCD_EN2_PIO_ID */
  202. #else /* LCD_EN2_BIT */
  203. #define LCD_EN2_SET()
  204. #define LCD_EN2_CLR()
  205. #endif /* LCD_EN2_BIT */
  206. #ifdef LCD_RST_BIT
  207. #define LCD_RST _BV(LCD_RST_BIT)
  208. #if LCD_RST_PIO_ID == PIOA_ID
  209. #define LCD_RST_SET() { outr(PIOA_PER, LCD_RST); outr(PIOA_SODR, LCD_RST); outr(PIOA_OER, LCD_RST); }
  210. #define LCD_RST_CLR() { outr(PIOA_PER, LCD_RST); outr(PIOA_CODR, LCD_RST); outr(PIOA_OER, LCD_RST); }
  211. #elif LCD_RST_PIO_ID == PIOB_ID
  212. #define LCD_RST_SET() { outr(PIOB_PER, LCD_RST); outr(PIOB_SODR, LCD_RST); outr(PIOB_OER, LCD_RST); }
  213. #define LCD_RST_CLR() { outr(PIOB_PER, LCD_RST); outr(PIOB_CODR, LCD_RST); outr(PIOB_OER, LCD_RST); }
  214. #elif LCD_RST_PIO_ID == PIOC_ID
  215. #define LCD_RST_SET() { outr(PIOC_PER, LCD_RST); outr(PIOC_SODR, LCD_RST); outr(PIOC_OER, LCD_RST); }
  216. #define LCD_RST_CLR() { outr(PIOC_PER, LCD_RST); outr(PIOC_CODR, LCD_RST); outr(PIOC_OER, LCD_RST); }
  217. #else
  218. #define LCD_RST_SET() { outr(PIO_PER, LCD_RST); outr(PIO_SODR, LCD_RST); outr(PIO_OER, LCD_RST); }
  219. #define LCD_RST_CLR() { outr(PIO_PER, LCD_RST); outr(PIO_CODR, LCD_RST); outr(PIO_OER, LCD_RST); }
  220. #endif /* LCD_RST_PIO_ID */
  221. #else /* LCD_RST_BIT */
  222. #define LCD_RST_SET()
  223. #define LCD_RST_CLR()
  224. #endif /* LCD_RST_BIT */
  225. #if LCD_DATA_PIO_ID == PIOA_ID
  226. #define LCD_DATA_BASE PIOA_BASE
  227. #elif LCD_DATA_PIO_ID == PIOB_ID
  228. #define LCD_DATA_BASE PIOB_BASE
  229. #elif LCD_DATA_PIO_ID == PIOC_ID
  230. #define LCD_DATA_BASE PIOC_BASE
  231. #else /* LCD_DATA_PIO_ID */
  232. #define LCD_DATA_BASE PIO_BASE
  233. #endif
  234. /*!
  235. * \addtogroup charlcd
  236. */
  237. /*@{*/
  238. #if defined(LCD_HD44780) || defined(LCD_KS0066) || defined(LCD_KS0073)
  239. #include "hd44780_at91.c"
  240. #elif defined(LCD_ST7036)
  241. #include "st7036_at91.c"
  242. #endif
  243. /*@}*/