dat-31r5-sp.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /****************************************************************************
  2. * drivers/rf/dat-31r5-sp.c
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. ****************************************************************************/
  17. /* Character driver for the Mini-Circuits DAT-31R5-SP+ digital step
  18. * attenuator.
  19. */
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <stdlib.h>
  25. #include <assert.h>
  26. #include <errno.h>
  27. #include <debug.h>
  28. #include <nuttx/kmalloc.h>
  29. #include <nuttx/fs/fs.h>
  30. #include <nuttx/spi/spi.h>
  31. #include <nuttx/rf/ioctl.h>
  32. #include <nuttx/rf/attenuator.h>
  33. /****************************************************************************
  34. * Pre-processor Definitions
  35. ****************************************************************************/
  36. #if defined(CONFIG_SPI) && defined(CONFIG_RF_DAT31R5SP)
  37. #ifndef CONFIG_DAT31R5SP_SPI_FREQUENCY
  38. # define CONFIG_DAT31R5SP_SPI_FREQUENCY 1000000
  39. #endif
  40. #define DAT31R5SP_SPI_MODE (SPIDEV_MODE0) /* SPI Mode 0: CPOL=0,CPHA=0 */
  41. /****************************************************************************
  42. * Private Types
  43. ****************************************************************************/
  44. struct dat31r5sp_dev_s
  45. {
  46. FAR struct spi_dev_s *spi; /* Saved SPI driver instance */
  47. int spidev;
  48. };
  49. /****************************************************************************
  50. * Private Function Prototypes
  51. ****************************************************************************/
  52. /* Character driver methods */
  53. static int dat31r5sp_open(FAR struct file *filep);
  54. static int dat31r5sp_close(FAR struct file *filep);
  55. static ssize_t dat31r5sp_read(FAR struct file *filep, FAR char *buffer,
  56. size_t buflen);
  57. static ssize_t dat31r5sp_write(FAR struct file *filep,
  58. FAR const char *buffer, size_t buflen);
  59. static int dat31r5sp_ioctl(FAR struct file *filep, int cmd,
  60. unsigned long arg);
  61. /****************************************************************************
  62. * Private Data
  63. ****************************************************************************/
  64. static const struct file_operations g_dat31r5sp_fops =
  65. {
  66. dat31r5sp_open,
  67. dat31r5sp_close,
  68. dat31r5sp_read,
  69. dat31r5sp_write,
  70. NULL,
  71. dat31r5sp_ioctl,
  72. NULL
  73. };
  74. /****************************************************************************
  75. * Private Functions
  76. ****************************************************************************/
  77. /****************************************************************************
  78. * Name: dat31r5sp_configspi
  79. *
  80. * Description:
  81. * Configure the SPI instance for to match the DAT-31R5-SP+
  82. * specifications
  83. *
  84. ****************************************************************************/
  85. static inline void dat31r5sp_configspi(FAR struct spi_dev_s *spi)
  86. {
  87. SPI_SETMODE(spi, DAT31R5SP_SPI_MODE);
  88. SPI_SETBITS(spi, 8);
  89. SPI_HWFEATURES(spi, 0);
  90. SPI_SETFREQUENCY(spi, CONFIG_DAT31R5SP_SPI_FREQUENCY);
  91. }
  92. /****************************************************************************
  93. * Name: dat31r5sp_set_attenuation
  94. *
  95. * Description:
  96. * Set the attenuation level in dB (16.16 bits fixed point).
  97. *
  98. ****************************************************************************/
  99. static void dat31r5sp_set_attenuation(FAR struct dat31r5sp_dev_s *priv,
  100. b16_t attenuation)
  101. {
  102. SPI_LOCK(priv->spi, true);
  103. dat31r5sp_configspi(priv->spi);
  104. SPI_SELECT(priv->spi, priv->spidev, false);
  105. /* Convert the attenuation value from 16.16 bits to 5.1 bits. */
  106. SPI_SEND(priv->spi, (uint8_t)(attenuation >> 15));
  107. /* Send a pulse to the LE pin */
  108. SPI_SELECT(priv->spi, priv->spidev, true);
  109. up_udelay(1);
  110. SPI_SELECT(priv->spi, priv->spidev, false);
  111. SPI_LOCK(priv->spi, false);
  112. }
  113. /****************************************************************************
  114. * Name: dat31r5sp_open
  115. *
  116. * Description:
  117. * This function is called whenever the DAT-31R5-SP+ device is
  118. * opened.
  119. *
  120. ****************************************************************************/
  121. static int dat31r5sp_open(FAR struct file *filep)
  122. {
  123. return OK;
  124. }
  125. /****************************************************************************
  126. * Name: dat31r5sp_close
  127. *
  128. * Description:
  129. * This function is called whenever the DAT-31R5-SP+ device is
  130. * closed.
  131. *
  132. ****************************************************************************/
  133. static int dat31r5sp_close(FAR struct file *filep)
  134. {
  135. return OK;
  136. }
  137. /****************************************************************************
  138. * Name: dat31r5sp_write
  139. *
  140. * Description:
  141. * Write is not permitted, only IOCTLs.
  142. ****************************************************************************/
  143. static ssize_t dat31r5sp_write(FAR struct file *filep,
  144. FAR const char *buffer,
  145. size_t buflen)
  146. {
  147. return -ENOSYS;
  148. }
  149. /****************************************************************************
  150. * Name: dat31r5sp_read
  151. *
  152. * Description:
  153. * Read is ignored.
  154. ****************************************************************************/
  155. static ssize_t dat31r5sp_read(FAR struct file *filep, FAR char *buffer,
  156. size_t buflen)
  157. {
  158. return 0;
  159. }
  160. /****************************************************************************
  161. * Name: dat31r5sp_ioctl
  162. *
  163. * Description:
  164. * The only available ICTL is RFIOC_SETATT. It expects a struct
  165. * attenuator_control* as the argument to set the attenuation
  166. * level. The channel is ignored as the DAT-31R5-SP+ has just a
  167. * single attenuator.
  168. ****************************************************************************/
  169. static int dat31r5sp_ioctl(FAR struct file *filep,
  170. int cmd,
  171. unsigned long arg)
  172. {
  173. FAR struct inode *inode = filep->f_inode;
  174. FAR struct dat31r5sp_dev_s *priv = inode->i_private;
  175. int ret = OK;
  176. switch (cmd)
  177. {
  178. case RFIOC_SETATT:
  179. {
  180. FAR struct attenuator_control *att =
  181. (FAR struct attenuator_control *)((uintptr_t)arg);
  182. DEBUGASSERT(att != NULL);
  183. dat31r5sp_set_attenuation(priv, att->attenuation);
  184. }
  185. break;
  186. default:
  187. sninfo("Unrecognized cmd: %d\n", cmd);
  188. ret = -ENOTTY;
  189. break;
  190. }
  191. return ret;
  192. }
  193. /****************************************************************************
  194. * Public Functions
  195. ****************************************************************************/
  196. /****************************************************************************
  197. * Name: dat31r5sp_register
  198. *
  199. * Description:
  200. * Register the dat31r5sp character device as 'devpath'.
  201. *
  202. ****************************************************************************/
  203. int dat31r5sp_register(FAR const char *devpath,
  204. FAR struct spi_dev_s *spi,
  205. int spidev)
  206. {
  207. FAR struct dat31r5sp_dev_s *priv;
  208. int ret;
  209. /* Sanity check */
  210. DEBUGASSERT(spi != NULL);
  211. /* Initialize the DAT-31R5-SP+ device structure */
  212. priv = (FAR struct dat31r5sp_dev_s *)
  213. kmm_malloc(sizeof(struct dat31r5sp_dev_s));
  214. if (priv == NULL)
  215. {
  216. snerr("ERROR: Failed to allocate instance\n");
  217. return -ENOMEM;
  218. }
  219. priv->spi = spi;
  220. priv->spidev = spidev;
  221. /* Clear the LE pin */
  222. SPI_SELECT(priv->spi, priv->spidev, false);
  223. /* Register the character driver */
  224. ret = register_driver(devpath, &g_dat31r5sp_fops, 0666, priv);
  225. if (ret < 0)
  226. {
  227. snerr("ERROR: Failed to register driver: %d\n", ret);
  228. kmm_free(priv);
  229. }
  230. return ret;
  231. }
  232. #endif