dac.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #include "dac.h"
  2. #include "src/interfaces/i2c_controller.h"
  3. #include "ti/driverlib/dl_i2c.h"
  4. #include "ti_msp_dl_config.h"
  5. #include "src/battery_data/battery.h"
  6. #include <stdint.h>
  7. #include <stdio.h>
  8. #include "src/config.h"
  9. bool DAC_SingleWrite(uint8_t slot, uint16_t channel_value) {
  10. if(channel_value > MAX_DAC_VALUE) {
  11. #ifdef DEBUG_DAC
  12. printf("DAC Error: channel_value out of range. Must be between 0 and %d\n", MAX_DAC_VALUE);
  13. #endif
  14. *battery_slots[slot].state = SLOT_WARN_DAC_INVALID_VALUE;
  15. return false;
  16. }
  17. controllerTxPackage.len = 3;
  18. controllerTxPackage.packet[0] = (0b01000000 | slot << 1);
  19. controllerTxPackage.packet[1] = (0x10) | ((channel_value >> 8) & 0x0F);
  20. controllerTxPackage.packet[2] = (channel_value & 0xFF);
  21. #ifdef DEBUG_DAC
  22. // Log data being sent
  23. printf("Sending to DAC: 0x%02X 0x%02X 0x%02X\n",
  24. controllerTxPackage.packet[0],
  25. controllerTxPackage.packet[1],
  26. controllerTxPackage.packet[2]);
  27. #endif
  28. // Write data to DAC
  29. if (!i2c_hal.write(DAC_TARGET_ADDRESS)) {
  30. #ifdef DEBUG_DAC
  31. printf("I2C DAC Write Error: Failed to write to DAC.\n");
  32. #endif
  33. *battery_slots[slot].state = SLOT_ERR_DAC_WRITE_FAILED;
  34. return false;
  35. }
  36. return true;
  37. }