hd44780_bus.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * Copyright (C) 2004 by Ole Reinhardt <ole.reinhardt@kernelconcepts.de>,
  3. * Kernelconcepts http://www.kernelconcepts.de
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the copyright holders nor the names of
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  21. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  22. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  23. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  24. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  25. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  26. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  27. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  28. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. *
  31. * For additional information see http://www.ethernut.de/
  32. *
  33. */
  34. /*!
  35. * \file arch/avr/dev/hd44780_bus.c
  36. * \brief Terminal device definitions for memory mapped lcd.
  37. *
  38. *
  39. * This is a terminal device driver for a memory mapped hd44780 compatible
  40. * lcd. It is connected to the databus / adressbus. A Chipselect is generated
  41. * from /rd, /wr, and the address decoder. It is connected to the lcds enable
  42. * signal. A0 is connected to the register select pin and A1 to the read/write
  43. * signal. Therefore you'll read from an address with an offset of two
  44. *
  45. * Have a look to our m-can board if you have questions.
  46. *
  47. * \verbatim
  48. * $Id: hd44780_bus.c 5472 2013-12-06 00:16:28Z olereinhardt $
  49. * \endverbatim
  50. */
  51. /* Not ported. */
  52. #ifdef __GNUC__
  53. #include <stdlib.h>
  54. #include <string.h>
  55. #include <stdio.h>
  56. #include <sys/nutconfig.h>
  57. #include <dev/hd44780_bus.h>
  58. #include <dev/term.h>
  59. #include <sys/timer.h>
  60. static uint16_t lcd_base = 0x0000;
  61. #ifndef LCD_4x20
  62. #ifndef LCD_4x16
  63. #ifndef LCD_2x40
  64. #ifndef LCD_2x20
  65. #ifndef LCD_2x16
  66. #ifndef LCD_2x8
  67. #ifndef LCD_1x20
  68. #ifndef LCD_1x16
  69. #ifndef LCD_1x8
  70. #ifndef KS0073_CONTROLLER
  71. #define LCD_2x16
  72. #endif
  73. #endif
  74. #endif
  75. #endif
  76. #endif
  77. #endif
  78. #endif
  79. #endif
  80. #endif
  81. #endif
  82. #define LCD_DELAY asm volatile ("nop"); asm volatile ("nop")
  83. /*!
  84. * \addtogroup xgDisplay
  85. */
  86. /*@{*/
  87. /*!
  88. * \brief Waits until Busy bit is reset or timeout
  89. */
  90. static inline void LcdBusyWait(void)
  91. {
  92. #if !defined(MMNET02) && !defined(MMNET03) && !defined(MMNET04) && \
  93. !defined(MMNET102) && !defined(MMNET103) && !defined(MMNET104)
  94. // wait until LCD busy bit goes to zero
  95. // do a read from control register
  96. while (*(volatile uint8_t *) (LCD_CTRL_ADDR + LCD_READ_OFFSET) & 1 << LCD_BUSY)
  97. LCD_DELAY;
  98. LCD_DELAY;
  99. LCD_DELAY;
  100. LCD_DELAY;
  101. LCD_DELAY;
  102. LCD_DELAY;
  103. LCD_DELAY;
  104. LCD_DELAY;
  105. LCD_DELAY;
  106. LCD_DELAY;
  107. LCD_DELAY;
  108. LCD_DELAY;
  109. LCD_DELAY;
  110. #else
  111. /* MMnet02 can not read the control register */
  112. NutDelay(5);
  113. #endif
  114. }
  115. /*!
  116. * \brief Write databyte to LCD controller over data bus
  117. *
  118. * \param ch Byte to send.
  119. */
  120. static void LcdWriteData(uint8_t data)
  121. {
  122. LcdBusyWait(); // wait until LCD not busy
  123. *(volatile uint8_t *) (LCD_DATA_ADDR) = data;
  124. }
  125. /*!
  126. * \brief Write command byte to LCD controller.
  127. */
  128. static void LcdWriteCmd(uint8_t cmd, uint8_t delay)
  129. {
  130. LcdBusyWait(); // wait until LCD not busy
  131. *(volatile uint8_t *) (LCD_CTRL_ADDR) = cmd;
  132. }
  133. static void LcdSetCursor(uint8_t pos)
  134. {
  135. uint8_t x = 0;
  136. uint8_t y = 0;
  137. #ifdef KS0073_CONTROLLER
  138. uint8_t offset[4] = {0x00, 0x20, 0x40, 0x60};
  139. y = pos / 20;
  140. x = pos % 20;
  141. if (y > 3) y = 3;
  142. #endif
  143. #if defined(LCD_2x40)
  144. uint8_t offset [2] = {0x00, 0x40};
  145. y = pos / 40;
  146. x = pos % 40;
  147. if (y > 1) y = 1;
  148. #endif
  149. #if defined(LCD_4x20) || defined(LCD_2x20)
  150. uint8_t offset [4] = {0x00, 0x40, 0x14, 0x54};
  151. y = pos / 20;
  152. x = pos % 20;
  153. if (y>3) y=3;
  154. #endif
  155. #if defined(LCD_4x16) || defined(LCD_2x16)
  156. uint8_t offset [4] = {0x00, 0x40, 0x10, 0x50};
  157. y = pos / 16;
  158. x = pos % 16;
  159. if (y>3) y=3;
  160. #endif
  161. #if defined(LCD_2x8)
  162. uint8_t offset [2] = {0x00, 0x40};
  163. y = pos / 8;
  164. x = pos % 8;
  165. if (y>1) y=1;
  166. #endif
  167. #if defined(LCD_1x8) || defined(LCD_1x16) || defined(LCD_1x20)
  168. uint8_t offset [1] = { 0x00 };
  169. y = 0;
  170. x = pos;
  171. #endif
  172. pos = x + offset[y];
  173. LcdWriteCmd(1 << LCD_DDRAM | pos, 0);
  174. }
  175. static void LcdCursorHome(void)
  176. {
  177. LcdWriteCmd(1 << LCD_HOME, 0);
  178. }
  179. static void LcdCursorLeft(void)
  180. {
  181. LcdWriteCmd(1 << LCD_MOVE, 0);
  182. }
  183. static void LcdCursorRight(void)
  184. {
  185. LcdWriteCmd(1 << LCD_MOVE | 1 << LCD_MOVE_RIGHT, 0);
  186. }
  187. static void LcdClear(void)
  188. {
  189. LcdWriteCmd(1 << LCD_CLR, 0);
  190. }
  191. static void LcdCursorMode(uint8_t on)
  192. {
  193. LcdWriteCmd(1 << LCD_ON_CTRL | on ? 1 << LCD_ON_CURSOR : 0x00, 0);
  194. }
  195. /*!
  196. * \brief Initialization of the LCD controller
  197. */
  198. static int LcdInit(NUTDEVICE * dev)
  199. {
  200. lcd_base = dev->dev_base;
  201. #ifdef KS0073_CONTROLLER
  202. // LCD function set with extended Register Set.
  203. LcdWriteCmd((1 << LCD_FUNCTION) | 1 << LCD_FUNCTION_8BIT | (1 << LCD_FUNCTION_RE), 0);
  204. NutDelay(50);
  205. LcdWriteCmd((1 << LCD_FUNCTION) | 1 << LCD_FUNCTION_8BIT | (1 << LCD_FUNCTION_RE), 0);
  206. NutDelay(50);
  207. LcdWriteCmd((1 << LCD_FUNCTION) | 1 << LCD_FUNCTION_8BIT | (1 << LCD_FUNCTION_RE), 0);
  208. NutDelay(50);
  209. LcdWriteCmd((1 << LCD_EXT) | ((((TERMDCB *) dev->dev_dcb)->dcb_nrows > 2) ? (1 << LCD_EXT_4LINES) : 0), 0);
  210. LcdWriteCmd((1 << LCD_FUNCTION) | 1 << LCD_FUNCTION_8BIT, 0);
  211. #else
  212. // LCD function set
  213. LcdWriteCmd((1 << LCD_FUNCTION) | 1 << LCD_FUNCTION_8BIT | (1 << LCD_FUNCTION_2LINES), 0);
  214. NutDelay(50);
  215. LcdWriteCmd((1 << LCD_FUNCTION) | 1 << LCD_FUNCTION_8BIT | (1 << LCD_FUNCTION_2LINES), 0);
  216. NutDelay(50);
  217. LcdWriteCmd((1 << LCD_FUNCTION) | 1 << LCD_FUNCTION_8BIT | (1 << LCD_FUNCTION_2LINES), 0);
  218. NutDelay(50);
  219. #endif
  220. // clear LCD
  221. LcdWriteCmd(1 << LCD_CLR, 0);
  222. // set entry mode
  223. LcdWriteCmd(1 << LCD_ENTRY_MODE | 1 << LCD_ENTRY_INC, 0);
  224. // set display to on
  225. LcdWriteCmd(1 << LCD_ON_CTRL | 1 << LCD_ON_DISPLAY, 0);
  226. // move cursor to home
  227. LcdWriteCmd(1 << LCD_HOME, 0);
  228. // set data address to 0
  229. LcdWriteCmd(1 << LCD_DDRAM | 0x00, 0);
  230. return 0;
  231. }
  232. /*!
  233. * \brief Terminal device control block structure.
  234. */
  235. TERMDCB dcb_term = {
  236. LcdInit, /*!< \brief Initialize display subsystem, dss_init. */
  237. LcdWriteData, /*!< \brief Write display character, dss_write. */
  238. LcdWriteCmd, /*!< \brief Write display command, dss_command. */
  239. LcdClear, /*!< \brief Clear display, dss_clear. */
  240. LcdSetCursor, /*!< \brief Set display cursor, dss_set_cursor. */
  241. LcdCursorHome, /*!< \brief Set display cursor home, dss_cursor_home. */
  242. LcdCursorLeft, /*!< \brief Move display cursor left, dss_cursor_left. */
  243. LcdCursorRight, /*!< \brief Move display cursor right, dss_cursor_right. */
  244. LcdCursorMode, /*!< \brief Switch cursor on/off, dss_cursor_mode. */
  245. 0, /*!< \brief Mode flags. */
  246. 0, /*!< \brief Status flags. */
  247. #ifdef KS0073_CONTROLLER
  248. 4, /*!< \brief Number of rows. */
  249. 20, /*!< \brief Number of columns per row. */
  250. 20, /*!< \brief Number of visible columns. */
  251. #endif
  252. #ifdef LCD_4x20
  253. 4, /*!< \brief Number of rows. */
  254. 20, /*!< \brief Number of columns per row. */
  255. 20, /*!< \brief Number of visible columns. */
  256. #endif
  257. #ifdef LCD_4x16
  258. 4, /*!< \brief Number of rows. */
  259. 16, /*!< \brief Number of columns per row. */
  260. 16, /*!< \brief Number of visible columns. */
  261. #endif
  262. #ifdef LCD_2x40
  263. 2, /*!< \brief Number of rows. */
  264. 40, /*!< \brief Number of columns per row. */
  265. 40, /*!< \brief Number of visible columns. */
  266. #endif
  267. #ifdef LCD_2x20
  268. 2, /*!< \brief Number of rows. */
  269. 20, /*!< \brief Number of columns per row. */
  270. 20, /*!< \brief Number of visible columns. */
  271. #endif
  272. #ifdef LCD_2x16
  273. 2, /*!< \brief Number of rows. */
  274. 16, /*!< \brief Number of columns per row. */
  275. 16, /*!< \brief Number of visible columns. */
  276. #endif
  277. #ifdef LCD_2x8
  278. 2, /*!< \brief Number of rows. */
  279. 8, /*!< \brief Number of columns per row. */
  280. 8, /*!< \brief Number of visible columns. */
  281. #endif
  282. #ifdef LCD_1x20
  283. 1, /*!< \brief Number of rows. */
  284. 20, /*!< \brief Number of columns per row. */
  285. 20, /*!< \brief Number of visible columns. */
  286. #endif
  287. #ifdef LCD_1x16
  288. 1, /*!< \brief Number of rows. */
  289. 16, /*!< \brief Number of columns per row. */
  290. 16, /*!< \brief Number of visible columns. */
  291. #endif
  292. #ifdef LCD_1x8
  293. 1, /*!< \brief Number of rows. */
  294. 8, /*!< \brief Number of columns per row. */
  295. 8, /*!< \brief Number of visible columns. */
  296. #endif
  297. 0, /*!< \brief Cursor row. */
  298. 0, /*!< \brief Cursor column. */
  299. 0 /*!< \brief Display shadow memory. */
  300. };
  301. /*!
  302. * \brief LCD device information structure.
  303. */
  304. NUTDEVICE devLcdBus = {
  305. 0, /*!< Pointer to next device. */
  306. {'l', 'c', 'd', 'b', 'u', 's', 0, 0, 0}, /*!< Unique device name. */
  307. IFTYP_STREAM, /*!< Type of device. */
  308. 0, /*!< Base address. */
  309. 0, /*!< First interrupt number. */
  310. 0, /*!< Interface control block. */
  311. &dcb_term, /*!< Driver control block. */
  312. TermInit, /*!< Driver initialization routine. */
  313. TermIOCtl, /*!< Driver specific control function. */
  314. 0,
  315. TermWrite,
  316. TermWrite_P,
  317. TermOpen,
  318. TermClose,
  319. 0,
  320. 0, /*!< Select function, optional, not yet implemented */
  321. };
  322. #else
  323. void keep_icc_happy(void)
  324. {
  325. }
  326. #endif
  327. /*@}*/