ssd1306_spi.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /****************************************************************************
  2. * drivers/lcd/ssd1306_spi.c
  3. *
  4. * Copyright (C) 2015 Alan Carvalho de Assis. All rights reserved.
  5. * Author: Alan Carvalho de Assis <acassis@gmail.com>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <unistd.h>
  40. #include <errno.h>
  41. #include <debug.h>
  42. #include <nuttx/kmalloc.h>
  43. #include <nuttx/lcd/ssd1306.h>
  44. #include <nuttx/spi/spi.h>
  45. #include "ssd1306.h"
  46. #if defined(CONFIG_LCD_SSD1306) && defined(CONFIG_LCD_SSD1306_SPI)
  47. /****************************************************************************
  48. * Public Functions
  49. ****************************************************************************/
  50. /****************************************************************************
  51. * Name: ssd1306_configspi
  52. *
  53. * Description:
  54. *
  55. ****************************************************************************/
  56. void ssd1306_configspi(FAR struct spi_dev_s *spi)
  57. {
  58. lcdinfo("Mode: %d Bits: 8 Frequency: %d\n",
  59. CONFIG_SSD1306_SPIMODE, CONFIG_SSD1306_FREQUENCY);
  60. /* Configure SPI for the SSD1306 */
  61. SPI_SETMODE(spi, CONFIG_SSD1306_SPIMODE);
  62. SPI_SETBITS(spi, 8);
  63. (void)SPI_HWFEATURES(spi, 0);
  64. (void)SPI_SETFREQUENCY(spi, CONFIG_SSD1306_FREQUENCY);
  65. }
  66. /****************************************************************************
  67. * Name: ssd1306_sendbyte
  68. *
  69. * Description:
  70. * Write a value to an 8-bit value to SSD1306
  71. *
  72. ****************************************************************************/
  73. int ssd1306_sendbyte(FAR struct ssd1306_dev_s *priv, uint8_t regval)
  74. {
  75. #ifdef CONFIG_LCD_SSD1306_REGDEBUG
  76. _err("->0x%02x\n", regval);
  77. #endif
  78. /* Send byte value to display */
  79. (void)SPI_SEND(priv->spi, regval);
  80. return OK;
  81. }
  82. /****************************************************************************
  83. * Name: ssd1306_sendblk
  84. *
  85. * Description:
  86. * Write an array of bytes to SSD1306
  87. *
  88. ****************************************************************************/
  89. int ssd1306_sendblk(FAR struct ssd1306_dev_s *priv, uint8_t *data, uint8_t len)
  90. {
  91. /* Send byte value to display */
  92. (void)SPI_SNDBLOCK(priv->spi, data, len);
  93. return OK;
  94. }
  95. /****************************************************************************
  96. * Name: ssd1306_select
  97. *
  98. * Description:
  99. * Enable/Disable SSD1306 SPI CS
  100. *
  101. ****************************************************************************/
  102. void ssd1306_select(FAR struct ssd1306_dev_s *priv, bool cs)
  103. {
  104. /* If we are selecting the device */
  105. if (cs == true)
  106. {
  107. /* If SPI bus is shared then lock and configure it */
  108. (void)SPI_LOCK(priv->spi, true);
  109. ssd1306_configspi(priv->spi);
  110. }
  111. /* Select/deselect SPI device */
  112. SPI_SELECT(priv->spi, SPIDEV_DISPLAY(0), cs);
  113. /* If we are deselecting the device */
  114. if (cs == false)
  115. {
  116. /* Unlock bus */
  117. (void)SPI_LOCK(priv->spi, false);
  118. }
  119. }
  120. /****************************************************************************
  121. * Name: ssd1306_cmddata
  122. *
  123. * Description:
  124. * Select Command/Data mode for SSD1306
  125. *
  126. ****************************************************************************/
  127. void ssd1306_cmddata(FAR struct ssd1306_dev_s *priv, bool cmd)
  128. {
  129. /* Select command transfer */
  130. SPI_CMDDATA(priv->spi, SPIDEV_DISPLAY(0), cmd);
  131. }
  132. #endif /* CONFIG_LCD_SSD1306 && CONFIG_LCD_SSD1306_SPI */