dac7571.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /****************************************************************************
  2. * arch/drivers/analog/dac7571.c
  3. *
  4. * Copyright (C) 2010, 2016, 2018 Gregory Nutt. All rights reserved.
  5. * Copyright (C) 2018 Daniel P. Carvalho. All rights reserved.
  6. * Author: Daniel P. Carvalho <danieloak@gmail.com>
  7. *
  8. * This file is a part of NuttX:
  9. *
  10. * Copyright (C) 2010 Gregory Nutt. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. *
  16. * 1. Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. * 2. Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in
  20. * the documentation and/or other materials provided with the
  21. * distribution.
  22. * 3. Neither the name NuttX nor the names of its contributors may be
  23. * used to endorse or promote products derived from this software
  24. * without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  29. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  30. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  31. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  32. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  33. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  34. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  36. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37. * POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. ****************************************************************************/
  40. /****************************************************************************
  41. * Included Files
  42. ****************************************************************************/
  43. #include <nuttx/config.h>
  44. #include <stdio.h>
  45. #include <sys/types.h>
  46. #include <stdint.h>
  47. #include <stdbool.h>
  48. #include <semaphore.h>
  49. #include <errno.h>
  50. #include <debug.h>
  51. #include <nuttx/arch.h>
  52. #include <nuttx/analog/dac.h>
  53. #include <nuttx/i2c/i2c_master.h>
  54. /****************************************************************************
  55. * Preprocessor definitions
  56. ****************************************************************************/
  57. #if !defined(CONFIG_I2C)
  58. # error I2C Support Required.
  59. #endif
  60. #if defined(CONFIG_DAC7571)
  61. /* Operating modes - not controllable by user */
  62. #define DAC7571_CONFIG_OPMODE_SHIFT 4
  63. #define DAC7571_CONFIG_OPMODE_MASK (0x0F << DAC7571_CONFIG_OPMODE_SHIFT)
  64. #define DAC7571_CONFIG_OPMODE_NORMAL (0 << DAC7571_CONFIG_OPMODE_SHIFT)
  65. #define DAC7571_CONFIG_OPMODE_1K_PWD (1 << DAC7571_CONFIG_OPMODE_SHIFT)
  66. #define DAC7571_CONFIG_OPMODE_100K_PWD (2 << DAC7571_CONFIG_OPMODE_SHIFT)
  67. #define DAC7571_CONFIG_OPMODE_HZ_PWD (3 << DAC7571_CONFIG_OPMODE_SHIFT)
  68. #ifndef CONFIG_DAC7571_I2C_FREQUENCY
  69. # define CONFIG_DAC7571_I2C_FREQUENCY 400000
  70. #endif
  71. #define I2C_NOSTARTSTOP_MSGS 2
  72. #define I2C_NOSTARTSTOP_ADDRESS_MSG_INDEX 0
  73. #define I2C_NOSTARTSTOP_DATA_MSG_INDEX 1
  74. /****************************************************************************
  75. * Private Types
  76. ****************************************************************************/
  77. struct dac7571_dev_s
  78. {
  79. FAR struct i2c_master_s *i2c; /* I2C interface */
  80. uint8_t addr; /* I2C address */
  81. uint16_t state; /* DAC7571 current state */
  82. };
  83. /****************************************************************************
  84. * Private Function Prototypes
  85. ****************************************************************************/
  86. /* I2C Helpers */
  87. /* DAC methods */
  88. static void dac7571_reset(FAR struct dac_dev_s *dev);
  89. static int dac7571_setup(FAR struct dac_dev_s *dev);
  90. static void dac7571_shutdown(FAR struct dac_dev_s *dev);
  91. static void dac7571_txint(FAR struct dac_dev_s *dev, bool enable);
  92. static int dac7571_send(FAR struct dac_dev_s *dev,
  93. FAR struct dac_msg_s *msg);
  94. static int dac7571_ioctl(FAR struct dac_dev_s *dev, int cmd,
  95. unsigned long arg);
  96. /****************************************************************************
  97. * Private Data
  98. ****************************************************************************/
  99. static struct dac7571_dev_s g_dacpriv;
  100. static const struct dac_ops_s g_dacops =
  101. {
  102. .ao_reset = dac7571_reset,
  103. .ao_setup = dac7571_setup,
  104. .ao_shutdown = dac7571_shutdown,
  105. .ao_txint = dac7571_txint,
  106. .ao_send = dac7571_send,
  107. .ao_ioctl = dac7571_ioctl,
  108. };
  109. static struct dac_dev_s g_dacdev =
  110. {
  111. .ad_ops = &g_dacops,
  112. .ad_priv = &g_dacpriv,
  113. };
  114. /****************************************************************************
  115. * Private Functions
  116. ****************************************************************************/
  117. /****************************************************************************
  118. * Name: dac7571_reset
  119. *
  120. * Description:
  121. * Reset the DAC device. Called early to initialize the hardware. This
  122. * is called, before ao_setup() and on error conditions.
  123. *
  124. ****************************************************************************/
  125. static void dac7571_reset(FAR struct dac_dev_s *dev)
  126. {
  127. }
  128. /****************************************************************************
  129. * Name: dac7571_setup
  130. *
  131. * Description:
  132. * Configure the DAC. This method is called the first time that the DAC
  133. * device is opened. This will occur when the port is first opened.
  134. * This setup includes configuring and attaching DAC interrupts. Interrupts
  135. * are all disabled upon return.
  136. *
  137. ****************************************************************************/
  138. static int dac7571_setup(FAR struct dac_dev_s *dev)
  139. {
  140. return OK;
  141. }
  142. /****************************************************************************
  143. * Name: dac7571_shutdown
  144. *
  145. * Description:
  146. * Disable the DAC. This method is called when the DAC device is closed.
  147. * This method reverses the operation the setup method.
  148. *
  149. ****************************************************************************/
  150. static void dac7571_shutdown(FAR struct dac_dev_s *dev)
  151. {
  152. }
  153. /****************************************************************************
  154. * Name: dac7571_txint
  155. *
  156. * Description:
  157. * Call to enable or disable TX interrupts
  158. *
  159. ****************************************************************************/
  160. static void dac7571_txint(FAR struct dac_dev_s *dev, bool enable)
  161. {
  162. }
  163. /****************************************************************************
  164. * Name: dac7571_send
  165. ****************************************************************************/
  166. static int dac7571_send(FAR struct dac_dev_s *dev, FAR struct dac_msg_s *msg)
  167. {
  168. FAR struct dac7571_dev_s *priv = (FAR struct dac7571_dev_s *)dev->ad_priv;
  169. struct i2c_config_s config;
  170. int ret;
  171. /* Sanity check */
  172. DEBUGASSERT(priv->i2c != NULL);
  173. /* Set up message to send */
  174. ainfo("value: %08x\n", msg->am_data);
  175. uint8_t const BUFFER_SIZE = 2;
  176. uint8_t buffer[BUFFER_SIZE];
  177. buffer[0] = (uint8_t)(msg->am_data >> 8);
  178. buffer[0] &= ~(DAC7571_CONFIG_OPMODE_MASK); /* only normal op mode supported */
  179. buffer[1] = (uint8_t)(msg->am_data);
  180. /* Set up the I2C configuration */
  181. config.frequency = CONFIG_DAC7571_I2C_FREQUENCY;
  182. config.address = priv->addr;
  183. config.addrlen = 7;
  184. /* Then perform the transfer. */
  185. ret = i2c_write(priv->i2c, &config, buffer, BUFFER_SIZE);
  186. if (ret < 0)
  187. {
  188. aerr("ERROR: dac7571_send failed code:%d\n", ret);
  189. }
  190. dac_txdone(&g_dacdev);
  191. return ret;
  192. }
  193. /****************************************************************************
  194. * Name: dac7571_ioctl
  195. ****************************************************************************/
  196. static int dac7571_ioctl(FAR struct dac_dev_s *dev, int cmd,
  197. unsigned long arg)
  198. {
  199. return -ENOTTY;
  200. }
  201. /****************************************************************************
  202. * Public Functions
  203. ****************************************************************************/
  204. /****************************************************************************
  205. * Name: dac7571_initialize
  206. *
  207. * Description:
  208. * Initialize DAC
  209. *
  210. * Input Parameters:
  211. * Port number (for hardware that has multiple DAC interfaces)
  212. *
  213. * Returned Value:
  214. * Valid DAC7571 device structure reference on success; a NULL on failure
  215. *
  216. ****************************************************************************/
  217. FAR struct dac_dev_s *dac7571_initialize(FAR struct i2c_master_s *i2c,
  218. uint8_t addr)
  219. {
  220. FAR struct dac7571_dev_s *priv;
  221. /* Sanity check */
  222. DEBUGASSERT(i2c != NULL);
  223. /* Initialize the DAC7571 device structure */
  224. priv = (FAR struct dac7571_dev_s *)g_dacdev.ad_priv;
  225. priv->i2c = i2c;
  226. priv->addr = addr;
  227. return &g_dacdev;
  228. }
  229. #endif /* CONFIG_DAC7571 */