#include "dac.h" #include #include #include "ti/driverlib/dl_i2c.h" #include "ti_msp_dl_config.h" //The device updates all DAC analog output(vout) at the same time void DAC_UpdateOutput() { uint8_t general_call_command = 0x08; // General Call Update Command while (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS); DL_I2C_fillControllerTXFIFO(I2C_controller_INST, &general_call_command, 1); // Start I2C transaction DL_I2C_startControllerTransfer(I2C_controller_INST, 0x00, DL_I2C_CONTROLLER_DIRECTION_TX, 1); while (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS); printf("DAC Outputs Updated via General Call Software Update!\n"); } /**Function for FAST write command, sending values over I2c to every channel of DAC**/ bool DAC_fastWrite(uint16_t channel_a_value){ /*DAC has a 12 bit resolution that ranges from 0 to 4095. 0x00: sets the Power Mode to NORMAL for Channel A (channel_a_value >> 8): shifts the value to 8 places right and gives upper 4 bits VoutA channel, rest channels are powered down */ uint8_t output_buffer[8]; output_buffer[0]= (0x00)|(channel_a_value >> 8)&0x0F; output_buffer[1]= channel_a_value & 0xFF; output_buffer[2]= 0x40; // Power down for the other channels output_buffer[3]= 0x00; output_buffer[4]= 0x40; output_buffer[5]= 0x00; output_buffer[6]= 0x40; output_buffer[7]= 0x00; if (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_ERROR) { printf("I2C Write Error: Failed to write to DAC channels for FAST write mode!\n"); DL_I2C_resetControllerTransfer(I2C_controller_INST); // Reset bus if stuck return false; // Return failure if there was an error } // **Wait for I2C Bus to be Free** while (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS); // **Start I2C Write Transaction** DL_I2C_startControllerTransfer(I2C_controller_INST, DAC_TARGET_BASE_ADDRESS, DL_I2C_CONTROLLER_DIRECTION_TX, 8); // **Load Configuration Byte into TX FIFO** DL_I2C_fillControllerTXFIFO(I2C_controller_INST, (uint8_t*)&output_buffer, 8); // **Ensure STOP Condition is Sent** while (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS); printf("DAC Fast write Successful!\n"); DAC_UpdateOutput(); return true; } /* LDAC pin is set to HIGH or LOW LDAC "HIGH": until the end of the positive pulse of the 8th clock of the 2nd byte 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 LDAC pin resumes normal function after STOP bit Device address of 0x60 till 0x67, default is 0x60 */ void ldac_pin_init(){ //Default set to HIGH DL_GPIO_setPins(LDAC_PIN_PORT, LDAC_PIN_PIN_PA8_PIN); } void set_ldac(bool level){ if(level){ // Set LDAC Pins HIGH DL_GPIO_setPins(LDAC_PIN_PORT, LDAC_PIN_PIN_PA8_PIN); } else{ // Set LDAC Pins LOW DL_GPIO_clearPins(LDAC_PIN_PORT, LDAC_PIN_PIN_PA8_PIN); } } /*General Call Read Address: Command to read I2C address bits of the device*/ uint8_t DAC_ReadCurrentAddress(){ uint8_t current_address= 0; //General call address bits uint8_t command= 0xC3; while (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS); //Send General Call Read Address Command DL_I2C_startControllerTransfer(I2C_controller_INST, I2C_GENERAL_CALL_ADDR, DL_I2C_CONTROLLER_DIRECTION_TX, 1); DL_I2C_fillControllerTXFIFO(I2C_controller_INST, &command, 1); while (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS); //Read the response (Current DAC I2C address) DL_I2C_startControllerTransfer(I2C_controller_INST, I2C_GENERAL_CALL_ADDR, DL_I2C_CONTROLLER_DIRECTION_RX, 1); current_address= DL_I2C_receiveControllerData(I2C_controller_INST); //Debug printf("Current DAC I2C Address: 0x%02X\n", current_address); return current_address; }