Feat: add i2c dac device connection test

Refs: #8
pull/9/head
rkcf 2023-02-20 04:50:47 -06:00
parent 92e5de269e
commit 0d7c2a6cc1
1 changed files with 37 additions and 4 deletions

View File

@ -1,16 +1,26 @@
#include <stdio.h> #include <stdio.h>
#include <stdint.h>
#include "driver/gpio.h" #include "driver/gpio.h"
#include "driver/i2c.h" #include "driver/i2c.h"
#include "esp_err.h" #include "esp_err.h"
#include "esp_log.h" #include "esp_log.h"
#include "freertos/portmacro.h"
#include "hal/i2c_types.h" #include "hal/i2c_types.h"
/* DAC Peripheral Config */
#define DAC_SCL_PIN 12 #define DAC_SCL_PIN 12
#define DAC_SDA_PIN 13 #define DAC_SDA_PIN 13
#define DAC_LDAC_PIN 14 #define DAC_LDAC_PIN 14
#define DAC_ADDR 0x60 /* I2C Address of DAC */ #define DAC_ADDR 0x64 /* I2C Address of DAC */
#define ACK_EN 0x1 /* Enable i2c acknowledgment check */
#define ACK_DIS 0x0 /* Disable i2c acknowledgment check */
#define ACK_VAL 0x0 /* i2c acknowledgment value */
#define NACK_VAL 0x1 /* i2c negative-acknowledgment value */
static const i2c_port_t I2C_MASTER_PORT = 0;
static const char *TAG = "EERG"; static const char *TAG = "EERG";
@ -21,7 +31,6 @@ static void dac_init(void)
ESP_LOGI(TAG, "Initializing DAC I2C connection"); ESP_LOGI(TAG, "Initializing DAC I2C connection");
/* Configure µC as I2C master */ /* Configure µC as I2C master */
i2c_port_t master_port = 0;
i2c_config_t i2c_conf = { i2c_config_t i2c_conf = {
.mode = I2C_MODE_MASTER, .mode = I2C_MODE_MASTER,
.scl_io_num = DAC_SCL_PIN, .scl_io_num = DAC_SCL_PIN,
@ -29,10 +38,10 @@ static void dac_init(void)
.master.clk_speed = 400000 /* 400kHz for i2c fast mode */ .master.clk_speed = 400000 /* 400kHz for i2c fast mode */
}; };
i2c_param_config(master_port, &i2c_conf); i2c_param_config(I2C_MASTER_PORT, &i2c_conf);
/* Install i2c driver */ /* Install i2c driver */
ESP_ERROR_CHECK(i2c_driver_install(master_port, i2c_conf.mode, 0, 0, 0)); ESP_ERROR_CHECK(i2c_driver_install(I2C_MASTER_PORT, i2c_conf.mode, 0, 0, 0));
/* Setup LDAC pin with GPIO. Tied low */ /* Setup LDAC pin with GPIO. Tied low */
gpio_reset_pin(DAC_LDAC_PIN); gpio_reset_pin(DAC_LDAC_PIN);
@ -40,8 +49,32 @@ static void dac_init(void)
gpio_set_level(DAC_LDAC_PIN, 0); gpio_set_level(DAC_LDAC_PIN, 0);
} }
/* Tests for acknowledgment of i2c DAC. Return ESP_OK if so */
static esp_err_t dac_test(void)
{
ESP_LOGI(TAG, "Testing I2C DAC");
/* Create i2c command handle */
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
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);
i2c_cmd_link_delete(cmd);
return ret;
}
/* Main application entrance */
void app_main(void) void app_main(void)
{ {
/* Initialize and configure I2C DAC */ /* Initialize and configure I2C DAC */
dac_init(); dac_init();
/* Test DAC */
esp_err_t ret = dac_test();
if (ret != ESP_OK){
ESP_LOGE(TAG, "Failed to initialize DAC");
} else {
ESP_LOGI(TAG, "Sucessfully initialized DAC");
}
} }