Fix SPI bugs #9 (NULL platform_ops) and #10 (missing CS toggle), widen chip_select to uint16_t

Bug #9: Both TX and RX SPI init params had platform_ops = NULL, causing
adf4382_init() -> no_os_spi_init() to fail with -EINVAL. Fixed by setting
platform_ops = &stm32_spi_ops and passing stm32_spi_extra with correct CS
port/pin for each device.

Bug #10: stm32_spi_write_and_read() never toggled chip select. Since TX
and RX ADF4382A share SPI4, every register write hit both PLLs. Rewrote
stm32_spi.c to assert CS LOW before transfer and deassert HIGH after,
using stm32_spi_extra metadata. Backward-compatible: legacy callers
(e.g., AD9523) with cs_port=NULL skip CS management.

Also widened chip_select from uint8_t to uint16_t in no_os_spi.h since
STM32 GPIO_PIN_xx values (e.g., GPIO_PIN_14=0x4000) overflow uint8_t.

10/10 tests pass (8 original + 2 new regression tests).
This commit is contained in:
Jason
2026-03-19 10:00:05 +02:00
parent 397969348e
commit 3b32f67087
11 changed files with 325 additions and 21 deletions
@@ -0,0 +1,26 @@
/* shim: stm32_spi.h -- provides stm32_spi_extra type and stm32_spi_ops mock
*
* The real stm32_spi.h includes stm32f7xx_hal.h which we can't use in tests.
* This shim provides the stm32_spi_extra struct and a mock stm32_spi_ops
* extern so that adf4382a_manager.c compiles against test infrastructure.
*/
#ifndef STM32_SPI_H_SHIM
#define STM32_SPI_H_SHIM
#include "stm32_hal_mock.h"
#include "ad_driver_mock.h"
/**
* @struct stm32_spi_extra
* @brief Platform-specific SPI data for STM32 (test mock version).
*/
typedef struct {
SPI_HandleTypeDef *hspi; /**< HAL SPI handle */
GPIO_TypeDef *cs_port; /**< GPIO port for software CS (NULL = no SW CS) */
uint16_t cs_pin; /**< GPIO pin mask for software CS */
} stm32_spi_extra;
/* Mock stm32_spi_ops -- declared in stm32_hal_mock.c */
extern const struct no_os_spi_platform_ops stm32_spi_ops;
#endif /* STM32_SPI_H_SHIM */