Skip to content

Instantly share code, notes, and snippets.

@NSBum
Created January 3, 2026 01:37
Show Gist options
  • Select an option

  • Save NSBum/c3ad47e166db191bf5a498b37794621c to your computer and use it in GitHub Desktop.

Select an option

Save NSBum/c3ad47e166db191bf5a498b37794621c to your computer and use it in GitHub Desktop.
AD5200 example
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/spi_master.h"
#include "esp_log.h"
#include "esp_err.h"
static const char *TAG = "AD5200";
#define PIN_NUM_MOSI 23
#define PIN_NUM_CLK 18
#define PIN_NUM_CS 5
#define SPI_HOST VSPI_HOST
// If you don’t get sane behavior, the first thing to try is SPI mode 1.
#define AD5200_SPI_MODE 0
static spi_device_handle_t g_ad5200;
static esp_err_t ad5200_write(uint8_t code)
{
spi_transaction_t t;
memset(&t, 0, sizeof(t));
t.length = 8; // bits
t.tx_buffer = &code;
return spi_device_transmit(g_ad5200, &t);
}
static esp_err_t ad5200_init(void)
{
spi_bus_config_t buscfg = {
.mosi_io_num = PIN_NUM_MOSI,
.miso_io_num = -1, // AD5200 is write-only
.sclk_io_num = PIN_NUM_CLK,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
.max_transfer_sz = 4,
};
spi_device_interface_config_t devcfg = {
.clock_speed_hz = 1 * 1000 * 1000, // 1 MHz to start
.mode = AD5200_SPI_MODE,
.spics_io_num = PIN_NUM_CS,
.queue_size = 1,
};
ESP_ERROR_CHECK(spi_bus_initialize(SPI_HOST, &buscfg, SPI_DMA_CH_AUTO));
ESP_ERROR_CHECK(spi_bus_add_device(SPI_HOST, &devcfg, &g_ad5200));
ESP_LOGI(TAG, "SPI ready");
return ESP_OK;
}
void app_main(void)
{
ESP_ERROR_CHECK(ad5200_init());
// Example: set midscale (0x80), then ramp.
ESP_ERROR_CHECK(ad5200_write(0x80));
vTaskDelay(pdMS_TO_TICKS(300));
for (int v = 0; v <= 255; v += 32) {
ESP_ERROR_CHECK(ad5200_write((uint8_t)v));
vTaskDelay(pdMS_TO_TICKS(200));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment