ssd1306_i2c.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /****************************************************************************
  2. * drivers/lcd/ssd1306_i2c.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 <string.h>
  28. #include <nuttx/kmalloc.h>
  29. #include <nuttx/i2c/i2c_master.h>
  30. #include <nuttx/lcd/ssd1306.h>
  31. #include "ssd1306.h"
  32. #if defined(CONFIG_LCD_SSD1306) && defined(CONFIG_LCD_SSD1306_I2C)
  33. /****************************************************************************
  34. * Public Functions
  35. ****************************************************************************/
  36. /****************************************************************************
  37. * Name: ssd1306_sendbyte
  38. *
  39. * Description:
  40. * Write an 8-bit value into SSD1306
  41. *
  42. ****************************************************************************/
  43. int ssd1306_sendbyte(FAR struct ssd1306_dev_s *priv, uint8_t regval)
  44. {
  45. /* 8-bit data read sequence:
  46. *
  47. * Start - I2C_Write_Address - SSD1306_Reg_Address - SSD1306_Write_Data
  48. * - STOP
  49. */
  50. struct i2c_msg_s msg;
  51. uint8_t txbuffer[2];
  52. int ret;
  53. #ifdef CONFIG_LCD_SSD1306_REGDEBUG
  54. _err("-> 0x%02x\n", regval);
  55. #endif
  56. /* Setup to the data to be transferred. Two bytes: The SSD1306 register
  57. * address followed by one byte of data.
  58. */
  59. txbuffer[0] = 0x00;
  60. txbuffer[1] = regval;
  61. /* Setup 8-bit SSD1306 address write message */
  62. msg.frequency = CONFIG_SSD1306_I2CFREQ; /* I2C frequency */
  63. msg.addr = priv->addr; /* 7-bit address */
  64. msg.flags = 0; /* Write transaction, beginning with START */
  65. msg.buffer = txbuffer; /* Transfer from this address */
  66. msg.length = 2; /* Send two bytes following the address
  67. * then STOP */
  68. /* Perform the transfer */
  69. ret = I2C_TRANSFER(priv->i2c, &msg, 1);
  70. if (ret < 0)
  71. {
  72. lcderr("ERROR: I2C_TRANSFER failed: %d\n", ret);
  73. }
  74. return ret;
  75. }
  76. /****************************************************************************
  77. * Name: ssd1306_sendblk
  78. *
  79. * Description:
  80. * Write an array of bytes to SSD1306
  81. *
  82. ****************************************************************************/
  83. int ssd1306_sendblk(FAR struct ssd1306_dev_s *priv, uint8_t *data,
  84. uint8_t len)
  85. {
  86. struct i2c_msg_s msg[2];
  87. uint8_t transfer_mode;
  88. int ret;
  89. /* 8-bit data read sequence:
  90. *
  91. * Start - I2C_Write_Address - Data transfer select - SSD1306_Write_Data
  92. * - STOP
  93. */
  94. /* Send the SSD1306 register address (with no STOP) */
  95. transfer_mode = 0x40; /* Select data transfer */
  96. msg[0].frequency = CONFIG_SSD1306_I2CFREQ; /* I2C frequency */
  97. msg[0].addr = priv->addr; /* 7-bit address */
  98. msg[0].flags = I2C_M_NOSTOP; /* Write transaction, beginning with START */
  99. msg[0].buffer = &transfer_mode; /* Transfer mode send */
  100. msg[0].length = 1; /* Send the one byte register address */
  101. /* Followed by the SSD1306 write data (with no RESTART, then STOP) */
  102. msg[1].frequency = CONFIG_SSD1306_I2CFREQ; /* I2C frequency */
  103. msg[1].addr = priv->addr; /* 7-bit address */
  104. msg[1].flags = I2C_M_NOSTART; /* Write transaction with no RESTART */
  105. msg[1].buffer = data; /* Transfer from this address */
  106. msg[1].length = len; /* Send the data, then STOP */
  107. ret = I2C_TRANSFER(priv->i2c, msg, 2);
  108. if (ret < 0)
  109. {
  110. lcderr("ERROR: I2C_TRANSFER failed: %d\n", ret);
  111. }
  112. return ret;
  113. }
  114. #endif /* CONFIG_LCD_SSD1306 &7 CONFIG_LCD_SSD1306_I2C */