Post Go back to editing

AD5262 Channel 2 Not Working in Daisy Chain Configuration

Category: Hardware
Product Number: AD5262

Hi, I am working on a product design using 2 AD5262 digital potentiometer ICs in daisy chain configuration. Pot wiper 1 is working on both ICs, and we are seeing SPI signals for pot wiper 2 at the appropriate times. This seems to suggest that something with the SPI data format for writing Pot 2 is formatted incorrectly. In my code I am packing the 18 bits of data necessary to write to 2 ICs to into 24 bits for the 8-bit SPI format and I am thinking that is the source of error. This is working for pot 1 but the address bit for pot 2 is somehow getting read incorrectly. 

Is there any issue with how I am formatting these SPI writes? Is there any other issue I should check that might be causing this error?

SPI formatting:

static void ad5262_daisy_write_spi(uint8_t alt){

// Each command is 9 bits: 1-bit pot select + 8-bit value
uint16_t cmd1 = ((alt & 0x01) << 8) | pot_vals[alt+2]; // goes to far-end chip (sent first)
uint16_t cmd2 = ((alt & 0x01) << 8) | pot_vals[alt]; // goes to near-end chip (sent second)

// Pack both 9-bit commands into the lowest 18 bits of a uint32_t
uint32_t spi_word = ((uint32_t)cmd1 << 9) | cmd2;

// No shift — leave the 18 bits in LSBs and extract top 3 bytes MSB-first
uint8_t spi_data[3];
spi_data[0] = (spi_word >> 16) & 0xFF; // bits 23..16
spi_data[1] = (spi_word >> 8) & 0xFF; // bits 15..8
spi_data[2] = spi_word & 0xFF; // bits 7..0

// SPI transaction
ad5262_daisy_delay();
HAL_GPIO_WritePin(AD5262_CS_GPIO_Port, AD5262_CS_Pin, GPIO_PIN_RESET);
ad5262_daisy_delay();

HAL_SPI_Transmit(&hspi2, spi_data, 3, HAL_MAX_DELAY);

ad5262_daisy_delay();
HAL_GPIO_WritePin(AD5262_CS_GPIO_Port, AD5262_CS_Pin, GPIO_PIN_SET);
ad5262_daisy_delay();

}


Full Code:

<>
#include <assert.h>
#include <stdbool.h>

#include "main.h"
#include "driver-ad5262-daisy.h"
#include "driver-tick64.h"

#if (defined(STM32L0xx_HAL_SPI_H) || defined(STM32L1xx_HAL_SPI_H)) && defined(AD5262_CS_GPIO_Port)

extern SPI_HandleTypeDef hspi2;

static void ad5262_daisy_delay(void) {
static volatile int x = 0;
for (int i = 0; i < 100; i++) {
x += 1;
}
}

void ad5262_daisy_init(void) {
HAL_GPIO_WritePin(AD5262_CS_GPIO_Port, AD5262_CS_Pin, GPIO_PIN_SET);
}

static uint8_t pot_targets[4]; // Four wipers (2 per AD5262)
static uint8_t pot_vals[4];

static void ad5262_daisy_write_spi(uint8_t alt){

// Each command is 9 bits: 1-bit pot select + 8-bit value
uint16_t cmd1 = ((alt & 0x01) << 8) | pot_vals[alt+2]; // goes to far-end chip (sent first)
uint16_t cmd2 = ((alt & 0x01) << 8) | pot_vals[alt]; // goes to near-end chip (sent second)

// Pack both 9-bit commands into the lowest 18 bits of a uint32_t
uint32_t spi_word = ((uint32_t)cmd1 << 9) | cmd2;

// No shift — leave the 18 bits in LSBs and extract top 3 bytes MSB-first
uint8_t spi_data[3];
spi_data[0] = (spi_word >> 16) & 0xFF; // bits 23..16
spi_data[1] = (spi_word >> 8) & 0xFF; // bits 15..8
spi_data[2] = spi_word & 0xFF; // bits 7..0

// SPI transaction
ad5262_daisy_delay();
HAL_GPIO_WritePin(AD5262_CS_GPIO_Port, AD5262_CS_Pin, GPIO_PIN_RESET);
ad5262_daisy_delay();

HAL_SPI_Transmit(&hspi2, spi_data, 3, HAL_MAX_DELAY);

ad5262_daisy_delay();
HAL_GPIO_WritePin(AD5262_CS_GPIO_Port, AD5262_CS_Pin, GPIO_PIN_SET);
ad5262_daisy_delay();

}

void ad5262_daisy_set(uint8_t pot, uint8_t val) {
assert(pot < 4);
pot_targets[pot] = val;
}

void ad5262_daisy_set_all(uint8_t val1, uint8_t val2, uint8_t val3, uint8_t val4) {
pot_targets[0] = val1;
pot_targets[1] = val2;
pot_targets[2] = val3;
pot_targets[3] = val4;
}

void ad5262_daisy_service(void) {
static uint64_t last = 0;
static uint8_t alt = 0;
if (now() < last + 1) {
return;
}
last = now();

bool changed = false;
for (uint8_t i = alt; i < 4; i+=2) {
if (pot_targets[i] > pot_vals[i]) {
pot_vals[i]++;
changed = true;
} else if (pot_targets[i] < pot_vals[i]) {
pot_vals[i]--;
changed = true;
}
}

if (changed) {
ad5262_daisy_write_spi(alt);
}

alt ^= 0x01;
}

#endif
</>

More Content

Before You Switch


Switching languages will make ADI Explorer unavailable. Resume your session by switching back to English and reopening ADI Explorer.