dac7554.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /****************************************************************************
  2. * arch/drivers/analog/dac7554.c
  3. *
  4. * Copyright (C) 2010, 2016, 2018 Gregory Nutt. All rights reserved.
  5. * Copyright (C) 2018 Daniel P. Carvalho. All rights reserved.
  6. * Copyright (C) 2019 Augusto Fraga Giachero. All rights reserved.
  7. * Author: Augusto Fraga Giachero <afg@augustofg.net>
  8. *
  9. * This file is a part of NuttX:
  10. *
  11. * Copyright (C) 2019 Gregory Nutt. All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. *
  17. * 1. Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * 2. Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in
  21. * the documentation and/or other materials provided with the
  22. * distribution.
  23. * 3. Neither the name NuttX nor the names of its contributors may be
  24. * used to endorse or promote products derived from this software
  25. * without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  30. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  31. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  32. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  33. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  34. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  35. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. ****************************************************************************/
  41. /****************************************************************************
  42. * Included Files
  43. ****************************************************************************/
  44. #include <nuttx/config.h>
  45. #include <stdio.h>
  46. #include <sys/types.h>
  47. #include <stdint.h>
  48. #include <stdbool.h>
  49. #include <semaphore.h>
  50. #include <errno.h>
  51. #include <debug.h>
  52. #include <nuttx/kmalloc.h>
  53. #include <nuttx/arch.h>
  54. #include <nuttx/analog/dac.h>
  55. #include <nuttx/spi/spi.h>
  56. /****************************************************************************
  57. * Preprocessor definitions
  58. ****************************************************************************/
  59. #if !defined(CONFIG_SPI)
  60. # error SPI Support Required.
  61. #endif
  62. #if defined(CONFIG_DAC7554)
  63. #ifndef CONFIG_DAC7554_SPI_FREQUENCY
  64. # define CONFIG_DAC7554_SPI_FREQUENCY 1000000
  65. #endif
  66. #define DAC7554_SPI_MODE (SPIDEV_MODE2) /* SPI Mode 2: CPOL=1,CPHA=0 */
  67. /****************************************************************************
  68. * Private Types
  69. ****************************************************************************/
  70. struct dac7554_dev_s
  71. {
  72. FAR struct spi_dev_s *spi; /* SPI interface */
  73. int spidev; /* SPI Chip Select number */
  74. };
  75. /****************************************************************************
  76. * Private Function Prototypes
  77. ****************************************************************************/
  78. /* I2C Helpers */
  79. /* DAC methods */
  80. static void dac7554_reset(FAR struct dac_dev_s *dev);
  81. static int dac7554_setup(FAR struct dac_dev_s *dev);
  82. static void dac7554_shutdown(FAR struct dac_dev_s *dev);
  83. static void dac7554_txint(FAR struct dac_dev_s *dev, bool enable);
  84. static int dac7554_send(FAR struct dac_dev_s *dev,
  85. FAR struct dac_msg_s *msg);
  86. static int dac7554_ioctl(FAR struct dac_dev_s *dev, int cmd,
  87. unsigned long arg);
  88. /****************************************************************************
  89. * Private Data
  90. ****************************************************************************/
  91. static const struct dac_ops_s g_dacops =
  92. {
  93. .ao_reset = dac7554_reset,
  94. .ao_setup = dac7554_setup,
  95. .ao_shutdown = dac7554_shutdown,
  96. .ao_txint = dac7554_txint,
  97. .ao_send = dac7554_send,
  98. .ao_ioctl = dac7554_ioctl,
  99. };
  100. /****************************************************************************
  101. * Private Functions
  102. ****************************************************************************/
  103. /****************************************************************************
  104. * Name: dac7554_configspi
  105. *
  106. * Description:
  107. * Configure the SPI interface
  108. *
  109. ****************************************************************************/
  110. static inline void dac7554_configspi(FAR struct spi_dev_s *spi)
  111. {
  112. SPI_SETMODE(spi, DAC7554_SPI_MODE);
  113. SPI_SETBITS(spi, 8);
  114. SPI_HWFEATURES(spi, 0);
  115. SPI_SETFREQUENCY(spi, CONFIG_DAC7554_SPI_FREQUENCY);
  116. }
  117. /****************************************************************************
  118. * Name: dac7554_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 dac7554_reset(FAR struct dac_dev_s *dev)
  126. {
  127. }
  128. /****************************************************************************
  129. * Name: dac7554_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 dac7554_setup(FAR struct dac_dev_s *dev)
  139. {
  140. return OK;
  141. }
  142. /****************************************************************************
  143. * Name: dac7554_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 dac7554_shutdown(FAR struct dac_dev_s *dev)
  151. {
  152. }
  153. /****************************************************************************
  154. * Name: dac7554_txint
  155. *
  156. * Description:
  157. * Call to enable or disable TX interrupts
  158. *
  159. ****************************************************************************/
  160. static void dac7554_txint(FAR struct dac_dev_s *dev, bool enable)
  161. {
  162. }
  163. /****************************************************************************
  164. * Name: dac7554_send
  165. ****************************************************************************/
  166. static int dac7554_send(FAR struct dac_dev_s *dev, FAR struct dac_msg_s *msg)
  167. {
  168. FAR struct dac7554_dev_s *priv = (FAR struct dac7554_dev_s *)dev->ad_priv;
  169. uint16_t data;
  170. /* Sanity check */
  171. DEBUGASSERT(priv->SPI != NULL);
  172. /* Set up message to send */
  173. ainfo("value: %08x\n", msg->am_data);
  174. SPI_LOCK(priv->spi, true);
  175. dac7554_configspi(priv->spi);
  176. data = ((msg->am_data >> 16) & 0x0fff) | ((msg->am_channel & 0x03) << 12) |
  177. (1 << 15);
  178. SPI_SELECT(priv->spi, priv->spidev, true);
  179. SPI_SEND(priv->spi, (data >> 8));
  180. SPI_SEND(priv->spi, (data & 0xff));
  181. SPI_SELECT(priv->spi, priv->spidev, false);
  182. SPI_LOCK(priv->spi, false);
  183. dac_txdone(dev);
  184. return 0;
  185. }
  186. /****************************************************************************
  187. * Name: dac7554_ioctl
  188. ****************************************************************************/
  189. static int dac7554_ioctl(FAR struct dac_dev_s *dev, int cmd,
  190. unsigned long arg)
  191. {
  192. return -ENOTTY;
  193. }
  194. /****************************************************************************
  195. * Public Functions
  196. ****************************************************************************/
  197. /****************************************************************************
  198. * Name: dac7554_initialize
  199. *
  200. * Description:
  201. * Initialize DAC
  202. *
  203. * Input Parameters:
  204. * Port number (for hardware that has multiple DAC interfaces)
  205. *
  206. * Returned Value:
  207. * Valid DAC7554 device structure reference on success; a NULL on failure
  208. *
  209. ****************************************************************************/
  210. FAR struct dac_dev_s *dac7554_initialize(FAR struct spi_dev_s *spi,
  211. int spidev)
  212. {
  213. FAR struct dac7554_dev_s *priv;
  214. FAR struct dac_dev_s *g_dacdev;
  215. /* Sanity check */
  216. DEBUGASSERT(i2c != NULL);
  217. /* Initialize the DAC7554 device structure */
  218. priv = (FAR struct dac7554_dev_s *)kmm_malloc(sizeof(struct dac7554_dev_s));
  219. priv->spi = spi;
  220. priv->spidev = spidev;
  221. g_dacdev = (FAR struct dac_dev_s *)kmm_malloc(sizeof(struct dac_dev_s));
  222. g_dacdev->ad_ops = &g_dacops;
  223. g_dacdev->ad_priv = priv;
  224. return g_dacdev;
  225. }
  226. #endif /* CONFIG_DAC7554 */