loragw_ad5338r.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. / _____) _ | |
  3. ( (____ _____ ____ _| |_ _____ ____| |__
  4. \____ \| ___ | (_ _) ___ |/ ___) _ \
  5. _____) ) ____| | | || |_| ____( (___| | | |
  6. (______/|_____)_|_|_| \__)_____)\____)_| |_|
  7. (C)2020 Semtech
  8. Description:
  9. Basic driver for Analog AD5338R DAC.
  10. License: Revised BSD License, see LICENSE.TXT file include in the project
  11. */
  12. #include <stdint.h> /* C99 types */
  13. #include <stdbool.h> /* bool type */
  14. #include <stdio.h> /* printf fprintf */
  15. #include "loragw_i2c.h"
  16. #include "loragw_ad5338r.h"
  17. /* -------------------------------------------------------------------------- */
  18. /* --- PRIVATE MACROS ------------------------------------------------------- */
  19. #if DEBUG_I2C == 1
  20. #define DEBUG_MSG(str) fprintf(stdout, str)
  21. #define DEBUG_PRINTF(fmt, args...) fprintf(stdout,"%s:%d: "fmt, __FUNCTION__, __LINE__, args)
  22. #define CHECK_NULL(a) if(a==NULL){fprintf(stderr,"%s:%d: ERROR: NULL POINTER AS ARGUMENT\n", __FUNCTION__, __LINE__);return LGW_I2C_ERROR;}
  23. #else
  24. #define DEBUG_MSG(str)
  25. #define DEBUG_PRINTF(fmt, args...)
  26. #define CHECK_NULL(a) if(a==NULL){return LGW_I2C_ERROR;}
  27. #endif
  28. /* -------------------------------------------------------------------------- */
  29. /* --- PUBLIC FUNCTIONS DEFINITION ------------------------------------------ */
  30. int ad5338r_configure(int i2c_fd, uint8_t i2c_addr) {
  31. int err;
  32. uint8_t cmd_soft_reset[AD5338R_CMD_SIZE] = {0x69, 0x00, 0x00};
  33. uint8_t cmd_power_up_dn[AD5338R_CMD_SIZE] = {0x40, 0x00, 0x00};
  34. uint8_t cmd_internal_ref[AD5338R_CMD_SIZE] = {0x70, 0x00, 0x00};
  35. /* Check Input Params */
  36. if (i2c_fd <= 0) {
  37. printf("ERROR: invalid I2C file descriptor\n");
  38. return LGW_I2C_ERROR;
  39. }
  40. DEBUG_PRINTF("INFO: configuring AD5338R DAC on 0x%02X...\n", i2c_addr);
  41. /* Sofwtare reset of DAC A & B */
  42. /* TODO: LSB data bytes seems not acknoledged by AD5338R, leading to an error if sent.
  43. Only send MSB data byte */
  44. err = i2c_linuxdev_write(i2c_fd, i2c_addr, cmd_soft_reset[0], cmd_soft_reset[1]);
  45. if (err != 0) {
  46. printf("ERROR: AD5338R software reset failed\n");
  47. return LGW_I2C_ERROR;
  48. }
  49. /* Normal operation */
  50. err = ad5338r_write(i2c_fd, i2c_addr, cmd_power_up_dn);
  51. if (err != 0) {
  52. printf("ERROR: AD5338R failed to set to normal operation\n");
  53. return LGW_I2C_ERROR;
  54. }
  55. /* Internal reference ON */
  56. err = ad5338r_write(i2c_fd, i2c_addr, cmd_internal_ref);
  57. if (err != 0) {
  58. printf("ERROR: AD5338R failed to set internal reference ON\n");
  59. return LGW_I2C_ERROR;
  60. }
  61. printf("INFO: AD5338R is configured\n");
  62. return LGW_I2C_SUCCESS;
  63. }
  64. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  65. int ad5338r_write(int i2c_fd, uint8_t i2c_addr, uint8_t buf[static AD5338R_CMD_SIZE]) {
  66. int err;
  67. /* Write AD5338R command buffer */
  68. err = i2c_linuxdev_write_buffer(i2c_fd, i2c_addr, buf, AD5338R_CMD_SIZE);
  69. if (err != 0) {
  70. printf("ERROR: failed to write AD5338R command\n");
  71. return LGW_I2C_ERROR;
  72. }
  73. return LGW_I2C_SUCCESS;
  74. }