lcd_test.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright (C) 2014 Ole Reinhardt <ole.reinhardt@embedded-it.de>
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. *
  34. */
  35. #include <toolchain.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <io.h>
  39. #include <ctype.h>
  40. #include <cfg/arch.h>
  41. #include <dev/gpio.h>
  42. #include <sys/thread.h>
  43. #include <sys/time.h>
  44. #include <sys/timer.h>
  45. #include <sys/types.h>
  46. #include <sys/stat.h>
  47. #include <fcntl.h>
  48. #include <dev/debug.h>
  49. #include <dev/usart_lpc17xx.h>
  50. #include <dev/spibus_lpc17xx_ssp.h>
  51. #include <dev/spi_lcd_st7565r.h>
  52. #include <dev/framebuffer.h>
  53. #define DBG_BAUDRATE 115200
  54. #define DEV_CONSOLE devUsartLpc17xx_0
  55. #define LED1 18
  56. #define LED2 20
  57. #define LED3 21
  58. #define LED4 23
  59. #define DISPLAY_WIDTH 128
  60. #define DISPLAY_HEIGHT 32
  61. #define FRAMEBUFFER_SIZE (DISPLAY_WIDTH * DISPLAY_HEIGHT / 8)
  62. #define ST7565R_SPI_CHIPSELECT_GPIO (0 * 32 + 18) /* GPIO PORT 0, PIN 18 */
  63. ST7565R_DCB st7565r_dcb = {
  64. .a0_port = NUTGPIO_PORT0,
  65. .a0_pin = 6,
  66. .reset_port = NUTGPIO_PORT0,
  67. .reset_pin = 8,
  68. };
  69. FBINFO st7565r_fb = {
  70. .width = 128,
  71. .height = 32,
  72. };
  73. THREAD(Thread1, arg)
  74. {
  75. int i = 0;
  76. while(1) {
  77. /* Blink the MBED board LEDs */
  78. GpioPinSet(NUTGPIO_PORT1, LED1, i & 0x01);
  79. GpioPinSet(NUTGPIO_PORT1, LED2, i & 0x02);
  80. GpioPinSet(NUTGPIO_PORT1, LED3, i & 0x04);
  81. GpioPinSet(NUTGPIO_PORT1, LED4, i & 0x08);
  82. NutSleep(250);
  83. i ++;
  84. }
  85. }
  86. int lcd_framebuffer_init(void)
  87. {
  88. NUTSPINODE *node;
  89. int rc;
  90. /* Setup custom settings of the framebuffer device. */
  91. /* Get the SPI node settings to configure address- and reset line and the spi node chipselect */
  92. node = devSt7565rFb0.dev_icb;
  93. node->node_dcb = &st7565r_dcb;
  94. /* Configure framebuffer / LCD resolution */
  95. devSt7565rFb0.dev_dcb = &st7565r_fb;
  96. /* Register the SPI bus node. The LPC17xx SSP driver needs the chip select
  97. * parameter to be given as GPIO pin number in format: GPIO_PORT * 32 + PIN
  98. */
  99. printf("Registering SPI bus and framebuffer device... ");
  100. rc = NutRegisterSpiDevice(&devSt7565rFb0, &spiBus1Lpc17xxSsp, ST7565R_SPI_CHIPSELECT_GPIO);
  101. if (rc != 0){
  102. /* If it fails, leave a message and end here */
  103. printf("FAILED\n");
  104. fflush(stdout);
  105. while(1);
  106. }
  107. printf("OK\n");
  108. return 0;
  109. }
  110. /*!
  111. * \brief Main application routine.
  112. *
  113. */
  114. int main(void)
  115. {
  116. uint8_t *fb;
  117. uint8_t *tmp_fb;
  118. uint32_t baud = 115200;
  119. int fb_fd;
  120. int i;
  121. FILE *uart;
  122. GpioPinConfigSet(NUTGPIO_PORT1, LED1, GPIO_CFG_OUTPUT);
  123. GpioPinConfigSet(NUTGPIO_PORT1, LED2, GPIO_CFG_OUTPUT);
  124. GpioPinConfigSet(NUTGPIO_PORT1, LED3, GPIO_CFG_OUTPUT);
  125. GpioPinConfigSet(NUTGPIO_PORT1, LED4, GPIO_CFG_OUTPUT);
  126. NutRegisterDevice(&DEV_CONSOLE, 0, 0);
  127. uart = fopen(DEV_CONSOLE.dev_name, "r+");
  128. _ioctl(_fileno(uart), UART_SETSPEED, &baud);
  129. freopen(DEV_CONSOLE.dev_name, "w", stdout);
  130. freopen(DEV_CONSOLE.dev_name, "w", stderr);
  131. freopen(DEV_CONSOLE.dev_name, "r", stdin);
  132. puts ("ST7565r framebuffer test - Nut/OS");
  133. lcd_framebuffer_init();
  134. NutThreadCreate("t1", Thread1, 0, 512);
  135. fb_fd = _open(devSt7565rFb0.dev_name, _O_RDWR);
  136. fb = malloc(FRAMEBUFFER_SIZE);
  137. tmp_fb = malloc(FRAMEBUFFER_SIZE);
  138. /* Slowly fill the display with some (stripe) pattern. Always write 8 (new)
  139. pixels, then update the display, until it is fully written.
  140. */
  141. for (i = 0; i < FRAMEBUFFER_SIZE; i++) {
  142. fb[i] = 0x33;
  143. _write(fb_fd, fb, FRAMEBUFFER_SIZE);
  144. }
  145. /* Now read the pattern from the framebuffer, and alternating show a
  146. * blank display, or the saved pattern.
  147. */
  148. while (1) {
  149. /* Read back the framebuffer */
  150. _read(fb_fd, tmp_fb, FRAMEBUFFER_SIZE);
  151. /* Clean the current frame buffer and update the display */
  152. memset(fb, 0, FRAMEBUFFER_SIZE);
  153. _write(fb_fd, fb, FRAMEBUFFER_SIZE);
  154. NutSleep(1000);
  155. /* Copy back the saved content and update the display */
  156. memcpy(fb, tmp_fb, FRAMEBUFFER_SIZE);
  157. _write(fb_fd, fb, FRAMEBUFFER_SIZE);
  158. NutSleep(1000);
  159. }
  160. /* Cleanup everything (never reached, but shall show how to close the
  161. framebuffer device and cleanup allocated ressources
  162. */
  163. _close(fb_fd);
  164. free(fb);
  165. free(tmp_fb);
  166. return 0;
  167. }