/ 9_Firmware / 9_1_Microcontroller / tests / test_bug2_ad9523_double_setup.c
test_bug2_ad9523_double_setup.c
  1  /*******************************************************************************
  2   * test_bug2_ad9523_double_setup.c
  3   *
  4   * Bug #2 (FIXED): configure_ad9523() now calls ad9523_setup() only ONCE,
  5   * after AD9523_RESET_RELEASE(). The first call (before reset) was removed.
  6   *
  7   * Post-fix test:
  8   *   1. Replay the fixed configure_ad9523() call sequence.
  9   *   2. Verify ad9523_setup() is called exactly ONCE.
 10   *   3. Verify the reset-release GPIO write occurs BEFORE the setup call.
 11   ******************************************************************************/
 12  #include "stm32_hal_mock.h"
 13  #include "ad_driver_mock.h"
 14  #include <assert.h>
 15  #include <stdio.h>
 16  
 17  /* Pin defines from main.h shim */
 18  #define AD9523_RESET_Pin        GPIO_PIN_6
 19  #define AD9523_RESET_GPIO_Port  GPIOF
 20  #define AD9523_REF_SEL_Pin      GPIO_PIN_4
 21  #define AD9523_REF_SEL_GPIO_Port GPIOF
 22  
 23  /* Macro from main.cpp */
 24  #define AD9523_RESET_RELEASE() HAL_GPIO_WritePin(AD9523_RESET_GPIO_Port, AD9523_RESET_Pin, GPIO_PIN_SET)
 25  #define AD9523_REF_SEL(x)     HAL_GPIO_WritePin(AD9523_REF_SEL_GPIO_Port, AD9523_REF_SEL_Pin, (x) ? GPIO_PIN_SET : GPIO_PIN_RESET)
 26  
 27  /*
 28   * Extracted from main.cpp — FIXED version (single setup call after reset).
 29   */
 30  static int configure_ad9523_extracted(void)
 31  {
 32      struct ad9523_dev *dev = NULL;
 33      struct ad9523_platform_data pdata;
 34      struct ad9523_init_param init_param;
 35      int32_t ret;
 36  
 37      memset(&pdata, 0, sizeof(pdata));
 38      pdata.vcxo_freq = 100000000;
 39      pdata.num_channels = 0;
 40      pdata.channels = NULL;
 41  
 42      memset(&init_param, 0, sizeof(init_param));
 43      init_param.pdata = &pdata;
 44  
 45      /* Step 1: ad9523_init (fills defaults) */
 46      ad9523_init(&init_param);
 47  
 48      /* Step 2: Release reset FIRST (Bug #2 fix: removed pre-reset setup call) */
 49      AD9523_RESET_RELEASE();
 50      HAL_Delay(5);
 51  
 52      /* Step 3: Select REFB */
 53      AD9523_REF_SEL(true);
 54  
 55      /* Step 4: Single ad9523_setup() — post-reset, real config */
 56      ret = ad9523_setup(&dev, &init_param);
 57      if (ret != 0) return -1;
 58  
 59      /* Step 5: status + sync */
 60      ad9523_status(dev);
 61      ad9523_sync(dev);
 62  
 63      return 0;
 64  }
 65  
 66  int main(void)
 67  {
 68      printf("=== Bug #2 (FIXED): AD9523 single setup call ===\n");
 69  
 70      spy_reset();
 71      int ret = configure_ad9523_extracted();
 72      assert(ret == 0);
 73  
 74      /* ---- Test A: ad9523_setup was called exactly ONCE ---- */
 75      int setup_count = spy_count_type(SPY_AD9523_SETUP);
 76      printf("  SPY_AD9523_SETUP records: %d (expected 1)\n", setup_count);
 77      assert(setup_count == 1);
 78      printf("  PASS: ad9523_setup() called exactly once\n");
 79  
 80      /* ---- Test B: Reset release occurs BEFORE the setup call ---- */
 81      int setup_idx = spy_find_nth(SPY_AD9523_SETUP, 0);
 82  
 83      /* Find the GPIO write for GPIOF, AD9523_RESET_Pin, SET */
 84      int reset_gpio_idx = -1;
 85      for (int i = 0; i < setup_idx; i++) {
 86          const SpyRecord *r = spy_get(i);
 87          if (r && r->type == SPY_GPIO_WRITE &&
 88              r->port == GPIOF &&
 89              r->pin == AD9523_RESET_Pin &&
 90              r->value == GPIO_PIN_SET) {
 91              reset_gpio_idx = i;
 92              break;
 93          }
 94      }
 95  
 96      printf("  Reset release at spy index %d, setup at %d\n",
 97             reset_gpio_idx, setup_idx);
 98      assert(reset_gpio_idx >= 0);
 99      assert(reset_gpio_idx < setup_idx);
100      printf("  PASS: Reset released BEFORE setup call (correct order)\n");
101  
102      printf("=== Bug #2 (FIXED): ALL TESTS PASSED ===\n\n");
103      return 0;
104  }