dac.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include "dac.h"
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include "ti/driverlib/dl_i2c.h"
  5. #include "ti_msp_dl_config.h"
  6. //The device updates all DAC analog output(vout) at the same time
  7. void DAC_UpdateOutput() {
  8. uint8_t general_call_command = 0x08; // General Call Update Command
  9. while (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
  10. DL_I2C_fillControllerTXFIFO(I2C_controller_INST, &general_call_command, 1);
  11. // Start I2C transaction
  12. DL_I2C_startControllerTransfer(I2C_controller_INST, 0x00, DL_I2C_CONTROLLER_DIRECTION_TX, 1);
  13. while (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
  14. printf("DAC Outputs Updated via General Call Software Update!\n");
  15. }
  16. /**Function for FAST write command, sending values over I2c to every channel of DAC**/
  17. bool DAC_fastWrite(uint16_t channel_a_value){
  18. /*DAC has a 12 bit resolution that ranges from 0 to 4095.
  19. 0x00: sets the Power Mode to NORMAL for Channel A
  20. (channel_a_value >> 8): shifts the value to 8 places right and gives upper 4 bits
  21. VoutA channel, rest channels are powered down
  22. */
  23. uint8_t output_buffer[8];
  24. output_buffer[0]= (0x00)|(channel_a_value >> 8)&0x0F;
  25. output_buffer[1]= channel_a_value & 0xFF;
  26. output_buffer[2]= 0x40; // Power down for the other channels
  27. output_buffer[3]= 0x00;
  28. output_buffer[4]= 0x40;
  29. output_buffer[5]= 0x00;
  30. output_buffer[6]= 0x40;
  31. output_buffer[7]= 0x00;
  32. if (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_ERROR) {
  33. printf("I2C Write Error: Failed to write to DAC channels for FAST write mode!\n");
  34. DL_I2C_resetControllerTransfer(I2C_controller_INST); // Reset bus if stuck
  35. return false; // Return failure if there was an error
  36. }
  37. // **Wait for I2C Bus to be Free**
  38. while (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
  39. // **Start I2C Write Transaction**
  40. DL_I2C_startControllerTransfer(I2C_controller_INST, DAC_TARGET_BASE_ADDRESS, DL_I2C_CONTROLLER_DIRECTION_TX, 8);
  41. // **Load Configuration Byte into TX FIFO**
  42. DL_I2C_fillControllerTXFIFO(I2C_controller_INST, (uint8_t*)&output_buffer, 8);
  43. // **Ensure STOP Condition is Sent**
  44. while (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
  45. printf("DAC Fast write Successful!\n");
  46. DAC_UpdateOutput();
  47. return true;
  48. }
  49. /*
  50. LDAC pin is set to HIGH or LOW
  51. LDAC "HIGH": until the end of the positive pulse of the 8th clock of the 2nd byte
  52. LDAC "LOW": negative pulse of the 8th clock of the 2nd byte and stays low until the rising edge of the 9th clock of the 3rd byte
  53. LDAC pin resumes normal function after STOP bit
  54. Device address of 0x60 till 0x67, default is 0x60
  55. */
  56. void ldac_pin_init(){
  57. //Default set to HIGH
  58. DL_GPIO_setPins(LDAC_PIN_PORT, LDAC_PIN_PIN_PA8_PIN);
  59. }
  60. void set_ldac(bool level){
  61. if(level){
  62. // Set LDAC Pins HIGH
  63. DL_GPIO_setPins(LDAC_PIN_PORT, LDAC_PIN_PIN_PA8_PIN);
  64. }
  65. else{
  66. // Set LDAC Pins LOW
  67. DL_GPIO_clearPins(LDAC_PIN_PORT, LDAC_PIN_PIN_PA8_PIN);
  68. }
  69. }
  70. /*General Call Read Address: Command to read I2C address bits of the device*/
  71. uint8_t DAC_ReadCurrentAddress(){
  72. uint8_t current_address= 0;
  73. //General call address bits
  74. uint8_t command= 0xC3;
  75. while (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
  76. //Send General Call Read Address Command
  77. DL_I2C_startControllerTransfer(I2C_controller_INST, I2C_GENERAL_CALL_ADDR, DL_I2C_CONTROLLER_DIRECTION_TX, 1);
  78. DL_I2C_fillControllerTXFIFO(I2C_controller_INST, &command, 1);
  79. while (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
  80. //Read the response (Current DAC I2C address)
  81. DL_I2C_startControllerTransfer(I2C_controller_INST, I2C_GENERAL_CALL_ADDR, DL_I2C_CONTROLLER_DIRECTION_RX, 1);
  82. current_address= DL_I2C_receiveControllerData(I2C_controller_INST);
  83. //Debug
  84. printf("Current DAC I2C Address: 0x%02X\n", current_address);
  85. return current_address;
  86. }