diff --git a/src/main/esp32_eurorack_generic.c b/src/main/esp32_eurorack_generic.c index 2882959..a169643 100644 --- a/src/main/esp32_eurorack_generic.c +++ b/src/main/esp32_eurorack_generic.c @@ -14,6 +14,9 @@ #define DAC_LDAC_PIN 14 #define DAC_ADDR 0x64 /* I2C Address of DAC */ +#define DAC_VREF 0x00 /* Use Vdd as reference voltage */ +#define DAC_PD 0x00 /* Full power mode */ +#define DAC_GAIN 0x00 /* Since we use external Vref, this doesn't matter */ #define ACK_EN 0x1 /* Enable i2c acknowledgment check */ #define ACK_DIS 0x0 /* Disable i2c acknowledgment check */ @@ -58,7 +61,7 @@ static esp_err_t dac_test(void) i2c_master_start(cmd); i2c_master_write_byte(cmd, (DAC_ADDR << 1) | I2C_MASTER_READ, ACK_EN); /* R/W byte is LSB of DAC address */ i2c_master_stop(cmd); - esp_err_t ret = i2c_master_cmd_begin(I2C_MASTER_PORT, cmd, 10000 / portTICK_PERIOD_MS); + esp_err_t ret = i2c_master_cmd_begin(I2C_MASTER_PORT, cmd, 1000 / portTICK_PERIOD_MS); i2c_cmd_link_delete(cmd); return ret; @@ -77,4 +80,36 @@ void app_main(void) } else { ESP_LOGI(TAG, "Sucessfully initialized DAC"); } + + uint16_t test_value = 0; /* The value sent to DAC input registers */ + i2c_cmd_handle_t cmd; + + /* Loop to test DAC output */ + while (1) { + for (uint16_t i = 0; i < 4096; i++){ + + test_value = i; + + cmd = i2c_cmd_link_create(); + i2c_master_start(cmd); + i2c_master_write_byte(cmd, (DAC_ADDR << 1) | I2C_MASTER_WRITE, ACK_EN); /* R/W byte is LSB of DAC address */ + i2c_master_write_byte(cmd, 0x50, ACK_EN); /* Multi-write to all channels */ + + /* For each channel [A-D] write 2 bytes to input register */ + /* V_ref + PD1 + PD0 + Gx + D11-D8 | D7-D0 */ + for (uint8_t i = 0; i < 4; i++) { + uint8_t bytes[2]; + bytes[0] = ((uint8_t) DAC_VREF << 7) | ((uint8_t) DAC_PD << 5) | ((uint8_t) DAC_GAIN << 4) | (test_value >> 8); + bytes[1] = test_value; + i2c_master_write_byte(cmd, bytes[0], ACK_EN); + i2c_master_write_byte(cmd, bytes[1], ACK_EN); + } + + i2c_master_stop(cmd); + + /* Issue the multi-write command */ + ret = i2c_master_cmd_begin(I2C_MASTER_PORT, cmd, 1000 / portTICK_PERIOD_MS); + i2c_cmd_link_delete(cmd); + } + } }