ssd1306_spi.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /****************************************************************************
  2. * drivers/lcd/ssd1306_spi.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <unistd.h>
  25. #include <errno.h>
  26. #include <debug.h>
  27. #include <nuttx/kmalloc.h>
  28. #include <nuttx/lcd/ssd1306.h>
  29. #include <nuttx/spi/spi.h>
  30. #include "ssd1306.h"
  31. #if defined(CONFIG_LCD_SSD1306) && defined(CONFIG_LCD_SSD1306_SPI)
  32. /****************************************************************************
  33. * Public Functions
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Name: ssd1306_configspi
  37. *
  38. * Description:
  39. *
  40. ****************************************************************************/
  41. void ssd1306_configspi(FAR struct spi_dev_s *spi)
  42. {
  43. lcdinfo("Mode: %d Bits: 8 Frequency: %d\n",
  44. CONFIG_SSD1306_SPIMODE, CONFIG_SSD1306_FREQUENCY);
  45. /* Configure SPI for the SSD1306 */
  46. SPI_SETMODE(spi, CONFIG_SSD1306_SPIMODE);
  47. SPI_SETBITS(spi, 8);
  48. SPI_HWFEATURES(spi, 0);
  49. SPI_SETFREQUENCY(spi, CONFIG_SSD1306_FREQUENCY);
  50. }
  51. /****************************************************************************
  52. * Name: ssd1306_sendbyte
  53. *
  54. * Description:
  55. * Write a value to an 8-bit value to SSD1306
  56. *
  57. ****************************************************************************/
  58. int ssd1306_sendbyte(FAR struct ssd1306_dev_s *priv, uint8_t regval)
  59. {
  60. #ifdef CONFIG_LCD_SSD1306_REGDEBUG
  61. _err("->0x%02x\n", regval);
  62. #endif
  63. /* Send byte value to display */
  64. SPI_SEND(priv->spi, regval);
  65. return OK;
  66. }
  67. /****************************************************************************
  68. * Name: ssd1306_sendblk
  69. *
  70. * Description:
  71. * Write an array of bytes to SSD1306
  72. *
  73. ****************************************************************************/
  74. int ssd1306_sendblk(FAR struct ssd1306_dev_s *priv,
  75. uint8_t *data, uint8_t len)
  76. {
  77. /* Send byte value to display */
  78. SPI_SNDBLOCK(priv->spi, data, len);
  79. return OK;
  80. }
  81. /****************************************************************************
  82. * Name: ssd1306_select
  83. *
  84. * Description:
  85. * Enable/Disable SSD1306 SPI CS
  86. *
  87. ****************************************************************************/
  88. void ssd1306_select(FAR struct ssd1306_dev_s *priv, bool cs)
  89. {
  90. /* If we are selecting the device */
  91. if (cs == true)
  92. {
  93. /* If SPI bus is shared then lock and configure it */
  94. SPI_LOCK(priv->spi, true);
  95. ssd1306_configspi(priv->spi);
  96. }
  97. /* Select/deselect SPI device */
  98. SPI_SELECT(priv->spi, SPIDEV_DISPLAY(0), cs);
  99. /* If we are deselecting the device */
  100. if (cs == false)
  101. {
  102. /* Unlock bus */
  103. SPI_LOCK(priv->spi, false);
  104. }
  105. }
  106. /****************************************************************************
  107. * Name: ssd1306_cmddata
  108. *
  109. * Description:
  110. * Select Command/Data mode for SSD1306
  111. *
  112. ****************************************************************************/
  113. void ssd1306_cmddata(FAR struct ssd1306_dev_s *priv, bool cmd)
  114. {
  115. /* Select command transfer */
  116. SPI_CMDDATA(priv->spi, SPIDEV_DISPLAY(0), cmd);
  117. }
  118. #endif /* CONFIG_LCD_SSD1306 && CONFIG_LCD_SSD1306_SPI */