Refactor: move dac output test loop to function

Refs: #8
pull/9/head
rkcf 2023-02-20 06:57:09 -06:00
parent b3f4a14697
commit c1e497c643
1 changed files with 38 additions and 30 deletions

View File

@ -53,7 +53,7 @@ static void dac_init(void)
} }
/* Tests for acknowledgment of i2c DAC. Return ESP_OK if so */ /* Tests for acknowledgment of i2c DAC. Return ESP_OK if so */
static esp_err_t dac_test(void) static esp_err_t dac_ack_test(void)
{ {
ESP_LOGI(TAG, "Testing I2C DAC"); ESP_LOGI(TAG, "Testing I2C DAC");
/* Create i2c command handle */ /* Create i2c command handle */
@ -67,6 +67,41 @@ static esp_err_t dac_test(void)
return ret; return ret;
} }
static esp_err_t dac_out_test(void)
{
esp_err_t ret;
uint16_t test_value = 0; /* The value sent to DAC input registers */
i2c_cmd_handle_t cmd;
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 */
/* TODO need to create array of returns and check them for error */
ret = i2c_master_cmd_begin(I2C_MASTER_PORT, cmd, 1000 / portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);
}
return ret;
}
/* Main application entrance */ /* Main application entrance */
void app_main(void) void app_main(void)
{ {
@ -74,42 +109,15 @@ void app_main(void)
dac_init(); dac_init();
/* Test DAC */ /* Test DAC */
esp_err_t ret = dac_test(); esp_err_t ret = dac_ack_test();
if (ret != ESP_OK){ if (ret != ESP_OK){
ESP_LOGE(TAG, "Failed to initialize DAC"); ESP_LOGE(TAG, "Failed to initialize DAC");
} else { } else {
ESP_LOGI(TAG, "Sucessfully initialized DAC"); 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 */ /* Loop to test DAC output */
while (1) { while (1) {
for (uint16_t i = 0; i < 4096; i++){ dac_out_test();
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);
}
} }
} }