Control AD5262 via HAL SPI Library
Hi, I am writing a driver for the AD5262 digital potentiometer, which uses SPI type serial but requires a 9 bit data format. I would much rather use the HAL SPI library (which has worked on similar AD family pots) and not use bitbanging. However the pot is not responsive to my messages, when I've tried a variety of methods to format this data.
Here is my driver code:
extern SPI_HandleTypeDef hspi2;
static void delay(void) {
static volatile int x = 0;
for (int i = 0; i < 100; i++) {
x += 1;
}
}
void ad5262_init(void) {
HAL_GPIO_WritePin(CS_DP_GPIO_Port, CS_DP_Pin, GPIO_PIN_SET);
}
static uint8_t pot_targets[2];
static uint8_t pot_vals[2];
static void ad5262_write_spi(uint8_t pot, uint8_t val){
uint8_t spi_block[2];
spi_block[0] = (pot << 7) | (val >> 1);
spi_block[1] = val << 7;
// spi_block[0] = pot;
// spi_block[1] = val;
HAL_StatusTypeDef res;
delay();
HAL_GPIO_WritePin(CS_DP_GPIO_Port, CS_DP_Pin, GPIO_PIN_RESET);
delay();
res = HAL_SPI_Transmit(&hspi2, (uint8_t*) spi_block, sizeof(spi_block), 5);
if(res != HAL_OK){
delay();
}
delay();
HAL_GPIO_WritePin(CS_DP_GPIO_Port, CS_DP_Pin, GPIO_PIN_SET);
delay();
}
void ad5262_set(uint8_t pot, uint8_t val) {
assert(pot < 2);
pot_targets[pot & 0x1] = val;
}
void ad5262_set_both(uint8_t val1, uint8_t val2) {
pot_targets[0] = val1;
pot_targets[1] = val2;
}
void ad5262_service(void) {
static uint64_t last = 0;
if (now() < last + 1) {
return;
}
last = now();
static uint8_t pot = 0;
bool changed = false;
for (int i = 0; i < 2; i++) {
if(i == pot){
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_write_spi(pot, pot_vals[pot]);
}
pot = (pot + 1) % 2;
}
The SPI write returns HAL_OK every time. Is there anything else I could be missing?
The one weird thing I notice is that the MX config for this project doesn't have options for the SPI data format like normal (motorola, MSB first). Is there anything that could be going on in that regard?

Please advise! I found this other post on using this pot as well but neither way I formatted the data into 2 8 bit writes worked for me: https://community.st.com/t5/stm32cubemx-mcus/idea-needed-for-9-bit-spi-transfer-using-hal-library/td-p/459375
