hd44780_at91.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*
  2. * Copyright (C) 2001-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.12 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.11 2008/08/11 06:59:09 haraldkipp
  40. * BSD types replaced by stdint types (feature request #1282721).
  41. *
  42. * Revision 1.10 2008/08/06 12:51:00 haraldkipp
  43. * Added support for Ethernut 5 (AT91SAM9XE reference design).
  44. *
  45. * Revision 1.9 2008/02/15 16:58:41 haraldkipp
  46. * Spport for AT91SAM7SE512 added.
  47. *
  48. * Revision 1.8 2007/10/04 19:57:54 olereinhardt
  49. * Support for SAM7S256 added
  50. *
  51. * Revision 1.7 2007/02/15 16:05:29 haraldkipp
  52. * Port usage is now configurable. Data bits no longer need four consecutive
  53. * port bits. Added delays in read for better reliability with some slow
  54. * displays.
  55. *
  56. * Revision 1.6 2006/10/05 17:11:16 haraldkipp
  57. * Fixes bug #1567813. Should now work after power on and after reset without
  58. * power loss. Many thanks to Klaus-Dieter Sohn.
  59. *
  60. * Revision 1.5 2006/08/31 19:02:25 haraldkipp
  61. * Added support for AT91SAM9260.
  62. * Some displays fail after reset. An additional nibble sent
  63. * during 4-bit initialization seems to fix this. However,
  64. * a user reported that his 3.3V driven LCD now fails during
  65. * power on.
  66. *
  67. * Revision 1.4 2006/07/15 11:15:31 haraldkipp
  68. * Initialization flag removed. It is not required because the driver doesn't
  69. * poll the busy flag during initialization.
  70. * Bug fixed, which let the driver fail to properly initialize displays with
  71. * two lines.
  72. *
  73. * Revision 1.3 2006/06/28 17:23:19 haraldkipp
  74. * Significantly extend delay time to allow running slow 3.3V LCDs with fast
  75. * CPUs. Not a nice fix, but it works.
  76. *
  77. * Revision 1.2 2006/05/15 11:44:06 haraldkipp
  78. * Added delays for more reliable initialization.
  79. *
  80. * Revision 1.1 2006/04/07 13:50:15 haraldkipp
  81. * ARM driver for HD44780 LCD controller added.
  82. *
  83. */
  84. #include <cfg/arch.h>
  85. #include <cfg/arch/armpio.h>
  86. #include <cfg/lcd.h>
  87. #include <stdlib.h>
  88. #include <string.h>
  89. //#include <stdio.h>
  90. #include <sys/nutconfig.h>
  91. #include <dev/hd44780.h>
  92. #include <dev/term.h>
  93. #include <sys/timer.h>
  94. #ifdef LCD_IF_4BIT
  95. #ifdef LCD_DATA_LSB
  96. #define LCD_DATA (0xF << LCD_DATA_LSB)
  97. #else /* LCD_DATA_LSB */
  98. #define LCD_D0 _BV(LCD_DATA_BIT0)
  99. #define LCD_D1 _BV(LCD_DATA_BIT1)
  100. #define LCD_D2 _BV(LCD_DATA_BIT2)
  101. #define LCD_D3 _BV(LCD_DATA_BIT3)
  102. #define LCD_DATA (LCD_D0 | LCD_D1 | LCD_D2 | LCD_D3)
  103. #endif /* LCD_DATA_LSB */
  104. #else /* LCD_IF_4BIT */
  105. #define LCD_DATA (0xFF << LCD_DATA_LSB)
  106. #endif /* LCD_IF_4BIT */
  107. /*!
  108. * \addtogroup xgDisplay
  109. */
  110. /*@{*/
  111. /*!
  112. * \brief Wait until controller will be ready again
  113. *
  114. * If LCD_WR_BIT is defined we will wait until the ready bit is set, otherwise
  115. * We will either busy loop with NutDelay or sleep with NutSleep. The second
  116. * option will be used if we have defined NUT_CPU_FREQ. In this case we have a higher
  117. * timer resolution.
  118. *
  119. * \param xt Delay time in milliseconds
  120. */
  121. #ifdef LCD_PW_EH
  122. void LcdNanoDelay( uint_fast16_t n)
  123. {
  124. while (n--) {
  125. _NOP();
  126. }
  127. }
  128. #else
  129. #define LcdNanoDelay( a)
  130. #endif
  131. static void INLINE LcdSetBits(unsigned int mask)
  132. {
  133. outr(LCD_DATA_BASE+PIO_SODR_OFF, mask);
  134. outr(LCD_DATA_BASE+PIO_OER_OFF, mask);
  135. }
  136. static void INLINE LcdClrBits(unsigned int mask)
  137. {
  138. outr(LCD_DATA_BASE+PIO_CODR_OFF, mask);
  139. outr(LCD_DATA_BASE+PIO_OER_OFF, mask);
  140. }
  141. #ifdef LCD_RW_BIT
  142. static unsigned int LcdReadNibble(void)
  143. {
  144. unsigned int rc;
  145. LCD_EN_SET();
  146. LcdDelay(LCD_SHORT_DELAY);
  147. rc = inr(LCD_DATA_PIO_ID+PIO_PDSR_OFF) & LCD_DATA;
  148. LCD_EN_CLR();
  149. LcdDelay(LCD_SHORT_DELAY);
  150. #ifdef LCD_DATA_LSB
  151. rc >>= LCD_DATA_LSB
  152. #else
  153. {
  154. unsigned int val = 0;
  155. if (rc & LCD_D0) {
  156. val |= 0x01;
  157. }
  158. if (rc & LCD_D1) {
  159. val |= 0x02;
  160. }
  161. if (rc & LCD_D2) {
  162. val |= 0x04;
  163. }
  164. if (rc & LCD_D3) {
  165. val |= 0x08;
  166. }
  167. rc = val;
  168. }
  169. #endif
  170. return rc;
  171. }
  172. /*!
  173. * \brief Read byte from LCD controller.
  174. */
  175. static unsigned int LcdReadByte(void)
  176. {
  177. outr(LCD_DATA_PIO_ID+PIO_ODR_OFF, LCD_DATA);
  178. LcdDelay(LCD_SHORT_DELAY);
  179. LCD_RW_SET();
  180. LcdDelay(LCD_SHORT_DELAY);
  181. return (LcdReadNibble() << 4) | LcdReadNibble();
  182. }
  183. /*!
  184. * \brief Read status byte from LCD controller.
  185. */
  186. static unsigned int LcdReadStatus(void)
  187. {
  188. /* RS low selects status register. */
  189. LCD_RS_CLR();
  190. return LcdReadByte();
  191. }
  192. #endif /* HD44_RW_BIT */
  193. #if 0
  194. /* This function is a bit critical as some chipsets are known to
  195. * release the redy bit some time early. So after rady goes low,
  196. * another fixed delay has to be added before soing the next access.
  197. */
  198. static void LcdWaitReady(unsigned int delay)
  199. {
  200. while (delay--) {
  201. #if defined(HD44_RW_BIT)
  202. if ((LcdReadStatus() & _BV(LCD_BUSY)) == 0) {
  203. break;
  204. }
  205. #endif
  206. NutMicroDelay(1);
  207. }
  208. }
  209. #endif
  210. /*!
  211. * \brief Send half byte to LCD controller.
  212. *
  213. * \param nib The four least significant bits are sent.
  214. */
  215. static void LcdWriteNibble(unsigned int nib)
  216. {
  217. #ifdef LCD_DATA_LSB
  218. nib <<= LCD_DATA_LSB;
  219. #else
  220. {
  221. unsigned int val = 0;
  222. if (nib & 0x01) {
  223. val |= LCD_D0;
  224. }
  225. if (nib & 0x02) {
  226. val |= LCD_D1;
  227. }
  228. if (nib & 0x04) {
  229. val |= LCD_D2;
  230. }
  231. if (nib & 0x08) {
  232. val |= LCD_D3;
  233. }
  234. nib = val;
  235. }
  236. #endif
  237. LcdSetBits(nib & LCD_DATA);
  238. LcdClrBits(~nib & LCD_DATA);
  239. /* Create Enable Pulse:
  240. * For HD44780 Displays we need:
  241. * Vcc = 5.0V -> PWeh >= 230ns
  242. * Vcc = 3.3V -> PWeh >= 500ns
  243. */
  244. LCD_EN_SET();
  245. LcdNanoDelay(LCD_PW_EH);
  246. LCD_EN_CLR();
  247. }
  248. /*!
  249. * \brief Send byte to LCD controller.
  250. *
  251. * \param data Byte to send.
  252. */
  253. static void LcdWriteByte(unsigned int data)
  254. {
  255. /* If configured set RW low */
  256. #ifdef LCD_RW_BIT
  257. LCD_RW_CLR();
  258. #endif
  259. /* If using 4-bit access, write two nibbles now */
  260. #ifdef LCD_IF_4BIT
  261. LcdWriteNibble(data >> 4);
  262. LcdNanoDelay(LCD_PW_EH);
  263. LcdWriteNibble(data);
  264. #else
  265. /* else write one byte */
  266. data <<= LCD_DATA_LSB;
  267. LcdSetBits(data & LCD_DATA);
  268. LcdClrBits(~data & LCD_DATA);
  269. #endif
  270. /* If configured, let the task sleep before next character */
  271. #if defined(LCD_SLEEP_DLY)
  272. NutSleep(1);
  273. #else
  274. /* or add a fixed delay and immediately process next char */
  275. NutMicroDelay(LCD_E2E_DLY);
  276. #endif
  277. }
  278. /*!
  279. * \brief Send command byte to LCD controller.
  280. *
  281. * \param cmd Byte to send.
  282. */
  283. static void LcdWriteCmd(uint8_t cmd)
  284. {
  285. /* RS low selects instruction register. */
  286. LCD_RS_CLR();
  287. LcdWriteByte(cmd);
  288. }
  289. static void LcdWriteInstruction(uint8_t cmd, uint8_t xt)
  290. {
  291. LcdWriteCmd(cmd);
  292. }
  293. /*!
  294. * \brief Send data byte to LCD controller.
  295. *
  296. * \param data Byte to send.
  297. */
  298. static void LcdWriteData(uint8_t data)
  299. {
  300. /* RS high selects data register. */
  301. LCD_RS_SET();
  302. LcdWriteByte(data);
  303. }
  304. static void LcdSetCursor(uint8_t pos)
  305. {
  306. uint8_t offset[] = {
  307. #ifdef LCD_KS0073
  308. 0x00, 0x20, 0x40, 0x60
  309. #elif (LCD_COLS >= 20)
  310. 0x00, 0x40, 0x14, 0x54
  311. #else
  312. 0x00, 0x40, 0x10, 0x50
  313. #endif
  314. };
  315. pos = offset[(pos / LCD_COLS) % LCD_ROWS] + pos % LCD_COLS;
  316. LcdWriteCmd(1 << LCD_DDRAM | pos);
  317. }
  318. static void LcdCursorHome(void)
  319. {
  320. LcdWriteCmd(1 << LCD_HOME);
  321. NutSleep(2);
  322. }
  323. static void LcdCursorLeft(void)
  324. {
  325. LcdWriteCmd(1 << LCD_MOVE);
  326. }
  327. static void LcdCursorRight(void)
  328. {
  329. LcdWriteCmd(1 << LCD_MOVE | 1 << LCD_MOVE_RIGHT);
  330. }
  331. static void LcdClear(void)
  332. {
  333. LcdWriteCmd(_BV(LCD_CLR));
  334. NutSleep(2);
  335. }
  336. static void LcdCursorMode(uint8_t on)
  337. {
  338. LcdWriteCmd(1 << LCD_ON_CTRL | on ? 1 << LCD_ON_CURSOR : 0x00);
  339. }
  340. static int LcdInit(NUTDEVICE * dev)
  341. {
  342. #if defined(PMC_PCER)
  343. outr(PMC_PCER, _BV(LCD_RS_PIO_ID) | _BV(LCD_EN_PIO_ID));
  344. #endif
  345. /* Initialize GPIO lines. */
  346. #ifdef LCD_RW_BIT
  347. outr(PMC_PCER, _BV(LCD_RW_PIO_ID));
  348. LCD_RW_CLR();
  349. #endif
  350. #ifdef LCD_EN2_BIT
  351. outr(PMC_PCER, _BV(LCD_EN2_PIO_ID));
  352. LCD_EN2_CLR();
  353. #endif
  354. #ifdef LCD_RST_BIT
  355. outr(PMC_PCER, _BV(LCD_RST_PIO_ID));
  356. LCD_RST_CLR();
  357. #endif
  358. LCD_RS_CLR();
  359. LCD_RW_CLR();
  360. LcdClrBits(LCD_DATA);
  361. NutMicroDelay(30);
  362. LCD_EN_CLR();
  363. NutMicroDelay(30);
  364. /* Initial delay. Actually only required after power on. */
  365. NutSleep(16);
  366. /* This initialization will make sure, that the LCD is switched
  367. * to 8-bit mode, no matter which mode we start from or we finally
  368. * need.
  369. */
  370. LcdWriteNibble((_BV(LCD_FUNCTION) | _BV(LCD_FUNCTION_8BIT)) >> 4);
  371. NutSleep(15);
  372. LcdWriteNibble((_BV(LCD_FUNCTION) | _BV(LCD_FUNCTION_8BIT)) >> 4);
  373. NutSleep(4);
  374. LcdWriteNibble((_BV(LCD_FUNCTION) | _BV(LCD_FUNCTION_8BIT)) >> 4);
  375. NutSleep(2);
  376. #ifdef LCD_IF_4BIT
  377. /* We now switch to 4-bit mode */
  378. LcdWriteNibble(_BV(LCD_FUNCTION) >> 4);
  379. NutSleep(2);
  380. // TODO: Add support for large font in single line displays
  381. /* Set number of lines and font. Can't be changed later. */
  382. #if (LCD_ROWS == 2) || (LCD_ROWS==4)
  383. LcdWriteNibble((_BV(LCD_FUNCTION) | _BV(LCD_FUNCTION_2LINES)) >> 4);
  384. LcdWriteNibble(_BV(LCD_FUNCTION) | _BV(LCD_FUNCTION_2LINES));
  385. #else
  386. LcdWriteNibble(_BV(LCD_FUNCTION) >> 4);
  387. LcdWriteNibble(_BV(LCD_FUNCTION) );
  388. #endif
  389. #else /* LCD_IF_4BIT */
  390. LcdWriteCmd(_BV(LCD_FUNCTION) | _BV(LCD_FUNCTION_8BIT));
  391. #endif /* LCD_IF_4BIT */
  392. NutSleep(2);
  393. /* Switch display and cursor off. */
  394. LcdWriteNibble(_BV(LCD_ON_CTRL) >> 4);
  395. LcdWriteNibble(_BV(LCD_ON_CTRL));
  396. NutSleep(2);
  397. /* Clear display. */
  398. LcdClear();
  399. /* Set entry mode. */
  400. LcdWriteCmd(_BV(LCD_ENTRY_MODE) | _BV(LCD_ENTRY_INC));
  401. /* Switch display on. */
  402. LcdWriteCmd(_BV(LCD_ON_CTRL) | _BV(LCD_ON_DISPLAY));
  403. /* Move cursor home. */
  404. LcdCursorHome();
  405. /* Set data address to zero. */
  406. LcdWriteCmd(_BV(LCD_DDRAM));
  407. return 0;
  408. }
  409. /*!
  410. * \brief Terminal device control block structure.
  411. */
  412. TERMDCB dcb_term = {
  413. LcdInit, /*!< \brief Initialize display subsystem, dss_init. */
  414. LcdWriteData, /*!< \brief Write display character, dss_write. */
  415. LcdWriteInstruction, /*!< \brief Write display command, dss_command. */
  416. LcdClear, /*!< \brief Clear display, dss_clear. */
  417. LcdSetCursor, /*!< \brief Set display cursor, dss_set_cursor. */
  418. LcdCursorHome, /*!< \brief Set display cursor home, dss_cursor_home. */
  419. LcdCursorLeft, /*!< \brief Move display cursor left, dss_cursor_left. */
  420. LcdCursorRight, /*!< \brief Move display cursor right, dss_cursor_right. */
  421. LcdCursorMode, /*!< \brief Switch cursor on/off, dss_cursor_mode. */
  422. 0, /*!< \brief Mode flags. */
  423. 0, /*!< \brief Status flags. */
  424. LCD_ROWS, /*!< \brief Number of rows. */
  425. LCD_COLS, /*!< \brief Number of columns per row. */
  426. LCD_COLS, /*!< \brief Number of visible columns. */
  427. 0, /*!< \brief Cursor row. */
  428. 0, /*!< \brief Cursor column. */
  429. 0 /*!< \brief Display shadow memory. */
  430. };
  431. /*!
  432. * \brief LCD device information structure.
  433. */
  434. NUTDEVICE devLcd = {
  435. 0, /*!< Pointer to next device. */
  436. {'c', 'h', 'a', 'r', 'l', 'c', 'd', 0, 0}, /*!< Unique device name. */
  437. IFTYP_STREAM, /*!< Type of device. */
  438. 0, /*!< Base address. */
  439. 0, /*!< First interrupt number. */
  440. 0, /*!< Interface control block. */
  441. &dcb_term, /*!< Driver control block. */
  442. TermInit, /*!< Driver initialization routine. */
  443. TermIOCtl, /*!< Driver specific control function. */
  444. 0,
  445. TermWrite,
  446. TermOpen,
  447. TermClose,
  448. 0,
  449. 0, /*!< Select function, optional, not yet implemented */
  450. };
  451. /*@}*/