### Initialize Sequencer Command Generator Source: https://context7.com/analogdevicesinc/ad5940lib/llms.txt Initializes the sequencer command generator and starts recording AFE register writes. Captured commands can be fetched and written to AFE SRAM for autonomous execution. Ensure to stop recording before fetching commands. ```c #include "ad5940.h" static uint32_t seq_buffer[64]; /* Workspace for command + register-shadow data */ static uint32_t seq_commands[32]; static uint32_t seq_len = 0; void BuildSequence(void) { const uint32_t **p_seq = NULL; AD5940_SEQGenInit(seq_buffer, 64); /* Initialize generator workspace */ AD5940_SEQGenCtrl(bTRUE); /* Start recording */ /* All WriteReg calls below are captured as sequencer commands */ AD5940_AFECtrlS(AFECTRL_ADCPWR, bTRUE); AD5940_AFECtrlS(AFECTRL_ADCCNV, bTRUE); AD5940_SEQGenInsert(SEQ_WAIT(200)); /* Wait 200 ACLK cycles */ AD5940_AFECtrlS(AFECTRL_ADCCNV, bFALSE); AD5940_SEQGenInsert(SEQ_INT0()); /* Generate custom interrupt 0 */ AD5940_SEQGenInsert(SEQ_STOP()); /* End sequence */ AD5940_SEQGenCtrl(bFALSE); /* Stop recording */ /* Retrieve generated commands */ AD5940Err err = AD5940_SEQGenFetchSeq((const uint32_t **)&p_seq, &seq_len); if (err == AD5940ERR_OK && seq_len > 0) { /* Write commands to AFE SRAM at address 0, register as Sequence 0 */ SEQInfo_Type seq_info = { .SeqId = SEQID_0, .SeqRamAddr = 0, .SeqLen = seq_len, .WriteSRAM = bTRUE, .pSeqCmd = (const uint32_t *)p_seq, }; AD5940_SEQInfoCfg(&seq_info); } } ``` -------------------------------- ### AD5940_GetFreqParameters Source: https://context7.com/analogdevicesinc/ad5940lib/llms.txt Returns recommended DFT source, SINC3/SINC2 OSR, DFT point count, and power mode for a given signal frequency, simplifying EIS application setup. ```APIDOC ## AD5940_GetFreqParameters ### Description Returns the recommended DFT source, SINC3/SINC2 OSR, DFT point count, and power mode for a given signal frequency, simplifying EIS application setup. ### Function Signature `FreqParams_Type AD5940_GetFreqParameters(float signal_freq)` ### Parameters - **signal_freq** (float) - The signal frequency for which to determine optimal parameters. ### Return Value - **FreqParams_Type** - A structure containing optimal configuration parameters: - **DftSrc** (DFT Source Type) - Recommended DFT source. - **DftNum** (uint32_t) - Recommended DFT point count. - **ADCSinc3Osr** (uint32_t) - Recommended SINC3 OSR. - **ADCSinc2Osr** (uint32_t) - Recommended SINC2 OSR. - **HighPwrMode** (BOOL) - Indicates if high power mode is recommended. ``` -------------------------------- ### Power Management with AD5940 WakeUp, EnterSleepS, ShutDownS Source: https://context7.com/analogdevicesinc/ad5940lib/llms.txt Manage AFE power states using AD5940_WakeUp, AD5940_EnterSleepS, and AD5940_ShutDownS. AD5940_EnterSleepS puts the AFE into hibernate while keeping the LP loop alive, and AD5940_WakeUp polls for the AFE to become responsive. ```c #include "ad5940.h" void PowerCycle(void) { /* Allow AFE to enter sleep via sequencer SEQ_SLP() command */ AD5940_SleepKeyCtrlS(SLPKEY_UNLOCK); /* Put AFE to hibernate (LP loop stays on) */ AD5940_EnterSleepS(); /* Later: wake up from MCU interrupt handler */ uint32_t result = AD5940_WakeUp(10); /* Try up to 10 SPI reads */ if (result == 0) { /* WakeUp failed – chip not responding */ } else { AD5940_Initialize(); /* Re-initialize after wakeup */ } } ``` -------------------------------- ### Configure AD5940 DSP Signal Chain Source: https://context7.com/analogdevicesinc/ad5940lib/llms.txt Configures the ADC, filters, and DFT engine. Ensure AD5940_StructInit is called before this function. ```c #include "ad5940.h" void ConfigureDSP(void) { DSPCfg_Type dsp_cfg; AD5940_StructInit(&dsp_cfg, sizeof(dsp_cfg)); /* ADC: measure HSTIA output vs. 1.1V internal ref, PGA gain = 1.5 */ dsp_cfg.ADCBaseCfg.ADCMuxP = ADCMUXP_HSTIA_P; dsp_cfg.ADCBaseCfg.ADCMuxN = ADCMUXN_VSET1P1; dsp_cfg.ADCBaseCfg.ADCPga = ADCPGA_1P5; /* Filter: SINC3 OSR=4, SINC2 OSR=667, 800 kHz ADC rate, bypass Notch */ dsp_cfg.ADCFilterCfg.ADCSinc3Osr = ADCSINC3OSR_4; dsp_cfg.ADCFilterCfg.ADCSinc2Osr = ADCSINC2OSR_667; dsp_cfg.ADCFilterCfg.ADCRate = ADCRATE_800KHZ; dsp_cfg.ADCFilterCfg.BpNotch = bTRUE; dsp_cfg.ADCFilterCfg.BpSinc3 = bFALSE; dsp_cfg.ADCFilterCfg.Sinc2NotchEnable = bTRUE; dsp_cfg.ADCFilterCfg.Sinc2NotchClkEnable = bTRUE; dsp_cfg.ADCFilterCfg.Sinc3ClkEnable = bTRUE; dsp_cfg.ADCFilterCfg.DFTClkEnable = bTRUE; dsp_cfg.ADCFilterCfg.WGClkEnable = bTRUE; /* DFT: 1024-point, source = SINC2+Notch, Hanning window enabled */ dsp_cfg.DftCfg.DftNum = DFTNUM_1024; dsp_cfg.DftCfg.DftSrc = DFTSRC_SINC2NOTCH; dsp_cfg.DftCfg.HanWinEn = bTRUE; /* Digital comparator: disabled (all zero after StructInit) */ /* Statistics: disabled */ AD5940_DSPCfgS(&dsp_cfg); } ``` -------------------------------- ### Initialize AD5940 AFE Chip Source: https://context7.com/analogdevicesinc/ad5940lib/llms.txt Must be called after any reset. Initializes critical registers and detects silicon revision. Requires user-provided SPI glue functions. ```c #include "ad5940.h" /* User-provided SPI glue (examples in ad5940-examples repository) */ void AD5940_CsClr(void) { GPIO_CsLow(); } void AD5940_CsSet(void) { GPIO_CsHigh(); } void AD5940_RstClr(void) { GPIO_RstLow(); } void AD5940_RstSet(void) { GPIO_RstHigh(); } void AD5940_Delay10us(uint32_t t) { DelayUs(t * 10); } void AD5940_ReadWriteNBytes(unsigned char *tx, unsigned char *rx, unsigned long len) { SPI_Transfer(tx, rx, len); } int main(void) { BoardInit(); /* MCU peripheral initialization */ AD5940_HWReset(); /* Toggle RESET pin to ensure clean state */ AD5940_Initialize(); /* MUST be called before any other AD5940 API */ uint32_t id = AD5940_GetChipID(); /* Should be non-zero for valid silicon */ /* id == 0x5500 → S1 silicon, 0x5501/0x5502 → S2/S3 silicon */ return 0; } ``` -------------------------------- ### AD5940_Initialize Source: https://context7.com/analogdevicesinc/ad5940lib/llms.txt Initializes the AFE chip. This function must be called after any reset event to set critical internal registers and detect the silicon revision. ```APIDOC ## AD5940_Initialize ### Description Initializes the AFE chip. This function must be called after every power-on reset, hardware reset, or software reset. It sets critical internal register values and detects the silicon revision. ### Function Signature ```c void AD5940_Initialize(void); ``` ### Usage Example ```c /* main.c – platform: AD5940 on ADICUP3029 */ #include "ad5940.h" /* User-provided SPI glue functions */ void AD5940_CsClr(void) { GPIO_CsLow(); } void AD5940_CsSet(void) { GPIO_CsHigh(); } void AD5940_RstClr(void) { GPIO_RstLow(); } void AD5940_RstSet(void) { GPIO_RstHigh(); } void AD5940_Delay10us(uint32_t t) { DelayUs(t * 10); } void AD5940_ReadWriteNBytes(unsigned char *tx, unsigned char *rx, unsigned long len) { SPI_Transfer(tx, rx, len); } int main(void) { BoardInit(); /* MCU peripheral initialization */ AD5940_HWReset(); /* Toggle RESET pin to ensure clean state */ AD5940_Initialize(); /* MUST be called before any other AD5940 API */ uint32_t id = AD5940_GetChipID(); /* Should be non-zero for valid silicon */ /* id == 0x5500 → S1 silicon, 0x5501/0x5502 → S2/S3 silicon */ return 0; } ``` ``` -------------------------------- ### Configure High-Speed Measurement Loop with AD5940_HSLoopCfgS Source: https://context7.com/analogdevicesinc/ad5940lib/llms.txt Configures the entire high-speed measurement path, including HSDAC, switch matrix, waveform generator, and HSTIA. Set parameters like gain, update rate, routing, waveform type, frequency, amplitude, and TIA settings. ```c #include "ad5940.h" void ConfigureEISLoop(void) { HSLoopCfg_Type hs_cfg; AD5940_StructInit(&hs_cfg, sizeof(hs_cfg)); /* HSDAC: full gain, update rate divider = 7 */ hs_cfg.HsDacCfg.ExcitBufGain = EXCITBUFGAIN_2; hs_cfg.HsDacCfg.HsDacGain = HSDACGAIN_1; hs_cfg.HsDacCfg.HsDacUpdateRate = 7; /* Switch matrix: CE0→D, RE0→P, SE0→N, T9 on */ hs_cfg.SWMatCfg.Dswitch = SWD_CE0; hs_cfg.SWMatCfg.Pswitch = SWP_RE0; hs_cfg.SWMatCfg.Nswitch = SWN_SE0; hs_cfg.SWMatCfg.Tswitch = SWT_TRTIA | SWT_T9; /* Waveform generator: 10 kHz sine wave */ hs_cfg.WgCfg.WgType = WGTYPE_SIN; hs_cfg.WgCfg.SinCfg.SinFreqWord = AD5940_WGFreqWordCal(10000.0f, 16000000.0f); hs_cfg.WgCfg.SinCfg.SinAmplitudeWord = 599; /* ~200 mV peak */ hs_cfg.WgCfg.SinCfg.SinOffsetWord = 0; hs_cfg.WgCfg.SinCfg.SinPhaseWord = 0; /* HSTIA: 10 kΩ RTIA, 31 pF CTIA */ hs_cfg.HsTiaCfg.HstiaRtiaSel = HSTIARTIA_10K; hs_cfg.HsTiaCfg.HstiaCtia = 31; hs_cfg.HsTiaCfg.HstiaDeRtia = HSTIADERTIA_OPEN; hs_cfg.HsTiaCfg.HstiaDeRload = HSTIADERLOAD_0R; hs_cfg.HsTiaCfg.HstiaBias = HSTIABIAS_1P1; hs_cfg.HsTiaCfg.DiodeClose = bFALSE; AD5940_HSLoopCfgS(&hs_cfg); } ``` -------------------------------- ### Configure and Handle Interrupts Source: https://context7.com/analogdevicesinc/ad5940lib/llms.txt Configures interrupt sources for the AFE interrupt controller (INTC0/INTC1) and provides a handler for testing and clearing interrupt flags. Multiple sources can be OR-combined for efficient interrupt management. ```c #include "ad5940.h" void ConfigureInterrupts(void) { /* Enable FIFO threshold and end-of-sequence interrupts on INTC0 */ AD5940_INTCCfg(AFEINTC_0, AFEINTSRC_DATAFIFOTHRESH | AFEINTSRC_ENDSEQ, bTRUE); } /* Called from MCU external interrupt handler triggered by GP0 */ void AFE_InterruptHandler(void) { if (AD5940_INTCTestFlag(AFEINTC_0, AFEINTSRC_DATAFIFOTHRESH)) { uint32_t cnt = AD5940_FIFOGetCnt(); /* Read FIFO data … */ AD5940_INTCClrFlag(AFEINTSRC_DATAFIFOTHRESH); } if (AD5940_INTCTestFlag(AFEINTC_0, AFEINTSRC_ENDSEQ)) { /* Sequence completed */ AD5940_INTCClrFlag(AFEINTSRC_ENDSEQ); } } ``` -------------------------------- ### Wake-up Timer Configuration Source: https://context7.com/analogdevicesinc/ad5940lib/llms.txt Configures the wake-up timer for autonomous duty-cycle operation, allowing the AFE to periodically wake, trigger sequencer slots, and return to hibernate. ```APIDOC ## AD5940_WUPTCfg — Configure the wake-up timer for autonomous duty-cycle operation Programs the hardware wake-up timer to periodically wake the AFE, trigger sequencer slots, and return to hibernate — enabling MCU-free low-power sampling loops. ### Usage Example: ```c #include "ad5940.h" /* Sleep/wake times are in units of 1/32768 Hz ≈ 30.5 µs */ #define WUPT_SLEEP_TIME (uint32_t)(1.0f / 30.5e-6f) /* ~1 s sleep */ #define WUPT_WAKE_TIME (uint32_t)(250e-6f / 30.5e-6f) /* 250 µs active */ void ConfigureWakeupTimer(void) { WUPTCfg_Type wupt_cfg; AD5940_StructInit(&wupt_cfg, sizeof(wupt_cfg)); wupt_cfg.WuptEndSeq = WUPTENDSEQ_A; /* Only use slot A */ wupt_cfg.WuptOrder[0] = SEQID_0; /* Slot A triggers Sequence 0 */ wupt_cfg.SeqxWakeupTime[0] = WUPT_WAKE_TIME; wupt_cfg.SeqxSleepTime[0] = WUPT_SLEEP_TIME; wupt_cfg.WuptEn = bTRUE; AD5940_WUPTCfg(&wupt_cfg); /* AFE now autonomously wakes, runs Sequence 0, sleeps, and repeats */ } ``` ``` -------------------------------- ### Initialize AD5940 Source: https://github.com/analogdevicesinc/ad5940lib/blob/master/README.md This function initializes the AD5940 chip with non-default register values. It is crucial to call this function whenever the AFE is reset (Power-On-Reset, hardware reset, or soft-reset). ```c void AD5940_Initialize(void); ``` -------------------------------- ### AD5940_WakeUp / AD5940_EnterSleepS / AD5940_ShutDownS Source: https://context7.com/analogdevicesinc/ad5940lib/llms.txt Manages the power states of the Analog Front End (AFE). `AD5940_WakeUp` attempts to bring the AFE out of a low-power state by polling. `AD5940_EnterSleepS` places the AFE into a hibernate state while keeping the Low Power (LP) loop active. `AD5940_ShutDownS` performs a complete power-down of the AFE. ```APIDOC ## Power Management Functions ### Description Controls AFE active/hibernate power states. `AD5940_WakeUp` polls by register read. `AD5940_EnterSleepS` puts the AFE to hibernate while keeping the LP loop alive. `AD5940_ShutDownS` fully powers down. ### Functions - **AD5940_WakeUp** - **AD5940_EnterSleepS** - **AD5940_ShutDownS** ### Parameters #### `AD5940_WakeUp` - **max_polls** (*uint32_t*): The maximum number of SPI read attempts to wake the device. #### `AD5940_EnterSleepS` No parameters. #### `AD5940_ShutDownS` No parameters. ### Example Usage ```c #include "ad5940.h" void PowerCycle(void) { /* Allow AFE to enter sleep via sequencer SEQ_SLP() command */ AD5940_SleepKeyCtrlS(SLPKEY_UNLOCK); /* Put AFE to hibernate (LP loop stays on) */ AD5940_EnterSleepS(); /* Later: wake up from MCU interrupt handler */ uint32_t result = AD5940_WakeUp(10); /* Try up to 10 SPI reads */ if (result == 0) { /* WakeUp failed – chip not responding */ } else { AD5940_Initialize(); /* Re-initialize after wakeup */ } } ``` ``` -------------------------------- ### Auto-configure EIS parameters for a given frequency Source: https://context7.com/analogdevicesinc/ad5940lib/llms.txt Call AD5940_GetFreqParameters to obtain optimal DFT source, OSR, DFT point count, and power mode for a specified signal frequency. Apply these parameters to the AFE and DFT configurations. ```c #include "ad5940.h" void AutoConfigure(float signal_freq) { FreqParams_Type params = AD5940_GetFreqParameters(signal_freq); /* Apply returned parameters to DSP and power configuration */ AD5940_AFEPwrBW(params.HighPwrMode ? AFEPWR_HP : AFEPWR_LP, AFEBW_AUTOSET); DFTCfg_Type dft_cfg; dft_cfg.DftNum = params.DftNum; dft_cfg.DftSrc = params.DftSrc; dft_cfg.HanWinEn = bTRUE; AD5940_DFTCfgS(&dft_cfg); /* Also apply params.ADCSinc3Osr and params.ADCSinc2Osr to ADCFilterCfg … */ } ``` -------------------------------- ### Configure Reference Voltage Buffers with AD5940_REFCfgS Source: https://context7.com/analogdevicesinc/ad5940lib/llms.txt Enables or disables high-power and low-power bandgap references and their output buffers. Ensure correct buffer enablement based on ADC/DAC subsystem requirements. ```c #include "ad5940.h" void ConfigureReferences(void) { AFERefCfg_Type ref_cfg; AD5940_StructInit(&ref_cfg, sizeof(ref_cfg)); ref_cfg.HpBandgapEn = bTRUE; /* Enable high-power bandgap (required for HSDAC/HSTIA) */ ref_cfg.Hp1V8BuffEn = bTRUE; /* Enable 1.8V HP reference buffer for ADC */ ref_cfg.Hp1V1BuffEn = bTRUE; /* Enable 1.1V HP reference buffer */ ref_cfg.LpBandgapEn = bTRUE; /* Enable LP bandgap (required for LPDAC/LPTIA) */ ref_cfg.LpRefBufEn = bTRUE; /* Enable 2.5V LP reference buffer */ ref_cfg.LpRefBoostEn = bFALSE; AD5940_REFCfgS(&ref_cfg); } ``` -------------------------------- ### Low-Level Register Access for AD5940 Source: https://context7.com/analogdevicesinc/ad5940lib/llms.txt Provides unified register I/O, transparently handling SPI or D2D access. Can record writes for the sequencer generator. ```c #include "ad5940.h" void DirectRegisterExample(void) { /* Read chip identification */ uint32_t adiid = AD5940_ReadReg(REG_AFECON_ADIID); /* Expected: 0x4144 */ uint32_t chipid = AD5940_ReadReg(REG_AFECON_CHIPID); /* Manually trigger sequence 0 via MMR */ AD5940_WriteReg(REG_AFECON_TRIGSEQ, BITM_AFECON_TRIGSEQ_TRIG0); /* Read current FIFO count */ uint32_t cnt = AD5940_ReadReg(REG_AFE_FIFOCNTSTA) >> BITP_AFE_FIFOCNTSTA_DATAFIFOCNTSTA; } ``` -------------------------------- ### Clock Configuration Source: https://context7.com/analogdevicesinc/ad5940lib/llms.txt Configures system and ADC clocks by selecting clock sources and their dividers for the AFE system bus and ADC core. ```APIDOC ## AD5940_CLKCfg — Configure system and ADC clocks Selects clock sources (internal HFOSC, LFOSC, external XTAL, or GPIO clock) and their dividers for both the AFE system bus and the ADC core. ### Usage Example: ```c #include "ad5940.h" void ConfigureClocks(void) { CLKCfg_Type clk_cfg; AD5940_StructInit(&clk_cfg, sizeof(clk_cfg)); clk_cfg.HFOSCEn = bTRUE; clk_cfg.HfOSC32MHzMode = bFALSE; /* Use 16 MHz */ clk_cfg.LFOSCEn = bTRUE; /* Enable 32 kHz for wake-up timer */ clk_cfg.HFXTALEn = bFALSE; clk_cfg.SysClkSrc = SYSCLKSRC_HFOSC; clk_cfg.ADCCLkSrc = ADCCLKSRC_HFOSC; clk_cfg.SysClkDiv = SYSCLKDIV_1; /* 16 MHz system clock */ clk_cfg.ADCClkDiv = ADCCLKDIV_1; /* 16 MHz ADC clock → 800 kSPS */ AD5940_CLKCfg(&clk_cfg); } ``` ``` -------------------------------- ### Configure Wake-up Timer for Autonomous Operation Source: https://context7.com/analogdevicesinc/ad5940lib/llms.txt Programs the wake-up timer to periodically wake the AFE, trigger a specified sequencer slot, and return to hibernate. This enables MCU-free low-power sampling loops. Wake and sleep times are defined in units of 1/32768 Hz. ```c #include "ad5940.h" /* Sleep/wake times are in units of 1/32768 Hz ≈ 30.5 µs */ #define WUPT_SLEEP_TIME (uint32_t)(1.0f / 30.5e-6f) /* ~1 s sleep */ #define WUPT_WAKE_TIME (uint32_t)(250e-6f / 30.5e-6f) /* 250 µs active */ void ConfigureWakeupTimer(void) { WUPTCfg_Type wupt_cfg; AD5940_StructInit(&wupt_cfg, sizeof(wupt_cfg)); wupt_cfg.WuptEndSeq = WUPTENDSEQ_A; /* Only use slot A */ wupt_cfg.WuptOrder[0] = SEQID_0; /* Slot A triggers Sequence 0 */ wupt_cfg.SeqxWakeupTime[0] = WUPT_WAKE_TIME; wupt_cfg.SeqxSleepTime[0] = WUPT_SLEEP_TIME; wupt_cfg.WuptEn = bTRUE; AD5940_WUPTCfg(&wupt_cfg); /* AFE now autonomously wakes, runs Sequence 0, sleeps, and repeats */ } ``` -------------------------------- ### Configure and Read AD5940 Data FIFO Source: https://context7.com/analogdevicesinc/ad5940lib/llms.txt Configures the FIFO's operating mode, size, source, and threshold. Reads data efficiently using burst SPI. ```c #include "ad5940.h" #define FIFO_THRESH 4 /* Interrupt when 4 DFT result pairs are ready */ void SetupFIFO(void) { FIFOCfg_Type fifo_cfg; AD5940_StructInit(&fifo_cfg, sizeof(fifo_cfg)); fifo_cfg.FIFOEn = bTRUE; fifo_cfg.FIFOMode = FIFOMODE_FIFO; fifo_cfg.FIFOSize = FIFOSIZE_4KB; /* 4 kB FIFO, 2 kB for sequencer */ fifo_cfg.FIFOSrc = FIFOSRC_DFT; /* Store DFT real+imag pairs */ fifo_cfg.FIFOThresh = FIFO_THRESH; AD5940_FIFOCfg(&fifo_cfg); /* Enable FIFO threshold interrupt on INTC0 */ AD5940_INTCCfg(AFEINTC_0, AFEINTSRC_DATAFIFOTHRESH, bTRUE); } void ReadFIFOData(void) { uint32_t cnt = AD5940_FIFOGetCnt(); /* How many words are waiting */ uint32_t buf[256]; if (cnt > 0 && cnt <= 256) { AD5940_FIFORd(buf, cnt); /* Optimized burst SPI read */ AD5940_INTCClrFlag(AFEINTSRC_DATAFIFOTHRESH); for (uint32_t i = 0; i + 1 < cnt; i += 2) { int32_t real = (int32_t)buf[i]; int32_t imag = (int32_t)buf[i + 1]; /* Process impedance point … */ } } } ``` -------------------------------- ### SPI Low-Level Interface Functions Source: https://github.com/analogdevicesinc/ad5940lib/blob/master/README.md These functions provide the basic SPI low-level interface required for the library to operate with AD594x devices. They must be implemented by the user to match their specific hardware. ```c void AD5940_CsClr(void); ``` ```c void AD5940_CsSet(void); ``` ```c void AD5940_RstClr(void); ``` ```c void AD5940_RstSet(void); ``` ```c void AD5940_Delay10us(uint32_t time); ``` ```c void AD5940_ReadWriteNBytes(unsigned char *pSendBuffer,unsigned char *pRecvBuff,unsigned long length); ``` -------------------------------- ### Configure System and ADC Clocks Source: https://context7.com/analogdevicesinc/ad5940lib/llms.txt Selects clock sources and dividers for the AFE system bus and ADC core. This configuration is essential for setting the operating frequency and sampling rate. Ensure appropriate clock sources are enabled. ```c #include "ad5940.h" void ConfigureClocks(void) { CLKCfg_Type clk_cfg; AD5940_StructInit(&clk_cfg, sizeof(clk_cfg)); clk_cfg.HFOSCEn = bTRUE; clk_cfg.HfOSC32MHzMode = bFALSE; /* Use 16 MHz */ clk_cfg.LFOSCEn = bTRUE; /* Enable 32 kHz for wake-up timer */ clk_cfg.HFXTALEn = bFALSE; clk_cfg.SysClkSrc = SYSCLKSRC_HFOSC; clk_cfg.ADCCLkSrc = ADCCLKSRC_HFOSC; clk_cfg.SysClkDiv = SYSCLKDIV_1; /* 16 MHz system clock */ clk_cfg.ADCClkDiv = ADCCLKDIV_1; /* 16 MHz ADC clock → 800 kSPS */ AD5940_CLKCfg(&clk_cfg); } ``` -------------------------------- ### Interrupt Controller Source: https://context7.com/analogdevicesinc/ad5940lib/llms.txt Enables, tests, and clears interrupt sources on INTC0 and INTC1. Multiple interrupt sources can be OR-combined. ```APIDOC ## AD5940_INTCCfg / AD5940_INTCTestFlag / AD5940_INTCClrFlag — Interrupt controller Enables, tests, and clears interrupt sources on INTC0 (mapped to GPIO0/hardware interrupt) and INTC1. Multiple interrupt sources can be OR-combined. ### Usage Example: ```c #include "ad5940.h" void ConfigureInterrupts(void) { /* Enable FIFO threshold and end-of-sequence interrupts on INTC0 */ AD5940_INTCCfg(AFEINTC_0, AFEINTSRC_DATAFIFOTHRESH | AFEINTSRC_ENDSEQ, bTRUE); } /* Called from MCU external interrupt handler triggered by GP0 */ void AFE_InterruptHandler(void) { if (AD5940_INTCTestFlag(AFEINTC_0, AFEINTSRC_DATAFIFOTHRESH)) { uint32_t cnt = AD5940_FIFOGetCnt(); /* Read FIFO data … */ AD5940_INTCClrFlag(AFEINTSRC_DATAFIFOTHRESH); } if (AD5940_INTCTestFlag(AFEINTC_0, AFEINTSRC_ENDSEQ)) { /* Sequence completed */ AD5940_INTCClrFlag(AFEINTSRC_ENDSEQ); } } ``` ``` -------------------------------- ### AD5940_DSPCfgS Source: https://context7.com/analogdevicesinc/ad5940lib/llms.txt Configures the full DSP signal chain including ADC MUX, PGA gain, filter cascade, DFT engine, and statistics block. ```APIDOC ## AD5940_DSPCfgS — Configure the full DSP signal chain ### Description Configures ADC input MUX, PGA gain, SINC3/SINC2/Notch filter cascade, DFT engine, and statistics block in a single call. ### Function Signature ```c void AD5940_DSPCfgS(DSPCfg_Type *dsp_cfg) ``` ### Parameters * **dsp_cfg** (*DSPCfg_Type*) A pointer to a `DSPCfg_Type` structure containing the desired DSP configuration settings. ### Example Usage ```c DSPCfg_Type dsp_cfg; AD5940_StructInit(&dsp_cfg, sizeof(dsp_cfg)); /* Configure ADC, Filter, and DFT settings */ dsp_cfg.ADCBaseCfg.ADCMuxP = ADCMUXP_HSTIA_P; dsp_cfg.ADCBaseCfg.ADCMuxN = ADCMUXN_VSET1P1; dsp_cfg.ADCBaseCfg.ADCPga = ADCPGA_1P5; dsp_cfg.ADCFilterCfg.ADCSinc3Osr = ADCSINC3OSR_4; dsp_cfg.ADCFilterCfg.ADCSinc2Osr = ADCSINC2OSR_667; dsp_cfg.ADCFilterCfg.ADCRate = ADCRATE_800KHZ; dsp_cfg.ADCFilterCfg.BpNotch = bTRUE; dsp_cfg.ADCFilterCfg.BpSinc3 = bFALSE; dsp_cfg.ADCFilterCfg.Sinc2NotchEnable = bTRUE; dsp_cfg.ADCFilterCfg.Sinc2NotchClkEnable = bTRUE; dsp_cfg.ADCFilterCfg.Sinc3ClkEnable = bTRUE; dsp_cfg.ADCFilterCfg.DFTClkEnable = bTRUE; dsp_cfg.ADCFilterCfg.WGClkEnable = bTRUE; dsp_cfg.DftCfg.DftNum = DFTNUM_1024; dsp_cfg.DftCfg.DftSrc = DFTSRC_SINC2NOTCH; dsp_cfg.DftCfg.HanWinEn = bTRUE; AD5940_DSPCfgS(&dsp_cfg); ``` ``` -------------------------------- ### Calculate Sequencer Wait Clocks with AD5940_ClksCalculate Source: https://context7.com/analogdevicesinc/ad5940lib/llms.txt Use AD5940_ClksCalculate to determine the required system clock cycles for a SEQ_WAIT command. This is crucial for ensuring correct sequencer timing based on filter chain output samples. ```c #include "ad5940.h" void CalculateWaitTime(void) { ClksCalInfo_Type clk_info; uint32_t num_clks = 0; clk_info.DataType = DATATYPE_DFT; clk_info.DataCount = 1; /* Need 1 DFT result */ clk_info.DftSrc = DFTSRC_SINC2NOTCH; clk_info.ADCSinc3Osr = ADCSINC3OSR_4; clk_info.ADCSinc2Osr = ADCSINC2OSR_667; clk_info.ADCRate = ADCRATE_800KHZ; clk_info.BpNotch = bTRUE; clk_info.DftNum = DFTNUM_1024; /* 1024-point DFT */ clk_info.RatioSys2AdcClk = 1.0f; /* sys_clk == adc_clk */ AD5940_ClksCalculate(&clk_info, &num_clks); /* num_clks is used in SEQ_WAIT(num_clks) inside the sequencer program */ } ``` -------------------------------- ### Configure Low-Power Amperometric Loop with AD5940_LPLoopCfgS Source: https://context7.com/analogdevicesinc/ad5940lib/llms.txt Configures the LPDAC and LPTIA/PA amplifiers for low-current biopotential or amperometric sensing. This involves setting DAC sources, output bits, reference voltage, and amplifier configurations. ```c #include "ad5940.h" void ConfigureAmperometric(void) { LPLoopCfg_Type lp_cfg; AD5940_StructInit(&lp_cfg, sizeof(lp_cfg)); /* LPDAC0: 12-bit output → Vzero, 6-bit → Vbias; source = MMR */ lp_cfg.LpDacCfg.LpdacSel = LPDAC0; lp_cfg.LpDacCfg.LpDacSrc = LPDACSRC_MMR; lp_cfg.LpDacCfg.LpDacVzeroMux = LPDACVZERO_12BIT; lp_cfg.LpDacCfg.LpDacVbiasMux = LPDACVBIAS_6BIT; lp_cfg.LpDacCfg.LpDacRef = LPDACREF_2P5V; lp_cfg.LpDacCfg.PowerEn = bTRUE; lp_cfg.LpDacCfg.DataRst = bFALSE; lp_cfg.LpDacCfg.DacData12Bit = 0x800; /* Midscale ≈ 1.1 V */ lp_cfg.LpDacCfg.DacData6Bit = 0x1F; /* Near full-scale */ lp_cfg.LpDacCfg.LpDacSW = LPDACSW_VZERO2LPTIA | LPDACSW_VZERO2PIN | LPDACSW_VBIAS2PIN; /* LPTIA0: 10 kΩ RTIA, normal power */ lp_cfg.LpAmpCfg.LpAmpSel = LPAMP0; lp_cfg.LpAmpCfg.LpTiaRtia = LPTIARTIA_10K; lp_cfg.LpAmpCfg.LpTiaRload = LPTIARLOAD_100R; lp_cfg.LpAmpCfg.LpTiaRf = LPTIARF_1M; lp_cfg.LpAmpCfg.LpAmpPwrMod = LPAMPPWR_NORM; lp_cfg.LpAmpCfg.LpPaPwrEn = bTRUE; lp_cfg.LpAmpCfg.LpTiaPwrEn = bTRUE; lp_cfg.LpAmpCfg.LpTiaSW = LPTIASW(5) | LPTIASW(9); AD5940_LPLoopCfgS(&lp_cfg); } ``` -------------------------------- ### AD5940_WriteReg / AD5940_ReadReg Source: https://context7.com/analogdevicesinc/ad5940lib/llms.txt Provides low-level register access, transparently handling SPI or D2D communication and recording writes for the sequencer generator. ```APIDOC ## AD5940_WriteReg / AD5940_ReadReg ### Description Unified register I/O that transparently routes through SPI (AD594x) or direct D2D access (ADuCM355), and records writes into the sequencer generator when it is active. ### Function Signatures ```c void AD5940_WriteReg(uint32_t regAddr, uint32_t value); uint32_t AD5940_ReadReg(uint32_t regAddr); ``` ### Usage Example ```c #include "ad5940.h" void DirectRegisterExample(void) { /* Read chip identification */ uint32_t adiid = AD5940_ReadReg(REG_AFECON_ADIID); /* Expected: 0x4144 */ uint32_t chipid = AD5940_ReadReg(REG_AFECON_CHIPID); /* Manually trigger sequence 0 via MMR */ AD5940_WriteReg(REG_AFECON_TRIGSEQ, BITM_AFECON_TRIGSEQ_TRIG0); /* Read current FIFO count */ uint32_t cnt = AD5940_ReadReg(REG_AFE_FIFOCNTSTA) >> BITP_AFE_FIFOCNTSTA_DATAFIFOCNTSTA; } ``` ``` -------------------------------- ### Control Waveform Generator Frequency with AD5940_WGFreqCtrlS Source: https://context7.com/analogdevicesinc/ad5940lib/llms.txt Updates the sine wave frequency at runtime without reconfiguring the entire waveform generator. The companion function AD5940_WGFreqWordCal converts Hz to the required hardware frequency word. ```c #include "ad5940.h" void SweepFrequency(void) { float wg_clock = 16000000.0f; /* 16 MHz HFOSC */ /* Calculate frequency words for a 3-point sweep */ float freq_points[] = {1000.0f, 10000.0f, 100000.0f}; for (int i = 0; i < 3; i++) { uint32_t word = AD5940_WGFreqWordCal(freq_points[i], wg_clock); AD5940_WGFreqCtrlS(freq_points[i], wg_clock); /* writes REG_AFE_AFE_WGFCW */ /* Trigger measurement … */ } } ``` -------------------------------- ### AD5940_FIFOCfg / AD5940_FIFORd Source: https://context7.com/analogdevicesinc/ad5940lib/llms.txt Configures the data FIFO and reads data from it. ```APIDOC ## AD5940_FIFOCfg / AD5940_FIFORd — Configure and read the data FIFO ### Description Configures the FIFO data source, memory allocation, operating mode, and threshold. `AD5940_FIFORd` reads data efficiently from the FIFO. ### `AD5940_FIFOCfg` Function Signature ```c void AD5940_FIFOCfg(FIFOCfg_Type *fifo_cfg) ``` ### `AD5940_FIFOCfg` Parameters * **fifo_cfg** (*FIFOCfg_Type*) A pointer to a `FIFOCfg_Type` structure containing the FIFO configuration settings. ### `AD5940_FIFOCfg` Example Usage ```c FIFOCfg_Type fifo_cfg; AD5940_StructInit(&fifo_cfg, sizeof(fifo_cfg)); fifo_cfg.FIFOEn = bTRUE; fifo_cfg.FIFOMode = FIFOMODE_FIFO; fifo_cfg.FIFOSize = FIFOSIZE_4KB; fifo_cfg.FIFOSrc = FIFOSRC_DFT; fifo_cfg.FIFOThresh = 4; /* Interrupt when 4 DFT result pairs are ready */ AD5940_FIFOCfg(&fifo_cfg); ``` ### `AD5940_FIFORd` Function Signature ```c void AD5940_FIFORd(uint32_t *buf, uint32_t cnt) ``` ### `AD5940_FIFORd` Parameters * **buf** (*uint32_t*) A pointer to a buffer where the FIFO data will be stored. * **cnt** (*uint32_t*) The number of words to read from the FIFO. ### `AD5940_FIFORd` Example Usage ```c uint32_t buf[256]; uint32_t cnt = AD5940_FIFOGetCnt(); /* Get number of words in FIFO */ if (cnt > 0 && cnt <= 256) { AD5940_FIFORd(buf, cnt); /* Read data from FIFO */ AD5940_INTCClrFlag(AFEINTSRC_DATAFIFOTHRESH); /* Clear interrupt flag */ /* Process data in buf */ for (uint32_t i = 0; i + 1 < cnt; i += 2) { int32_t real = (int32_t)buf[i]; int32_t imag = (int32_t)buf[i + 1]; /* Process impedance point */ } } ``` ``` -------------------------------- ### Sequencer Command Generator Source: https://context7.com/analogdevicesinc/ad5940lib/llms.txt Intercepts AD5940_WriteReg calls to record them as sequencer command words. The generated command array can be written to AFE SRAM for autonomous execution. ```APIDOC ## AD5940_SEQGenInit / AD5940_SEQGenCtrl / AD5940_SEQGenFetchSeq — Sequencer command generator Intercepts all `AD5940_WriteReg` calls while active and records them as sequencer command words in a user-supplied buffer. The generated command array can then be written to AFE SRAM and executed autonomously. ### Usage Example: ```c #include "ad5940.h" static uint32_t seq_buffer[64]; /* Workspace for command + register-shadow data */ static uint32_t seq_commands[32]; static uint32_t seq_len = 0; void BuildSequence(void) { const uint32_t **p_seq = NULL; AD5940_SEQGenInit(seq_buffer, 64); /* Initialize generator workspace */ AD5940_SEQGenCtrl(bTRUE); /* Start recording */ /* All WriteReg calls below are captured as sequencer commands */ AD5940_AFECtrlS(AFECTRL_ADCPWR, bTRUE); AD5940_AFECtrlS(AFECTRL_ADCCNV, bTRUE); AD5940_SEQGenInsert(SEQ_WAIT(200)); /* Wait 200 ACLK cycles */ AD5940_AFECtrlS(AFECTRL_ADCCNV, bFALSE); AD5940_SEQGenInsert(SEQ_INT0()); /* Generate custom interrupt 0 */ AD5940_SEQGenInsert(SEQ_STOP()); /* End sequence */ AD5940_SEQGenCtrl(bFALSE); /* Stop recording */ /* Retrieve generated commands */ AD5940Err err = AD5940_SEQGenFetchSeq((const uint32_t **)&p_seq, &seq_len); if (err == AD5940ERR_OK && seq_len > 0) { /* Write commands to AFE SRAM at address 0, register as Sequence 0 */ SEQInfo_Type seq_info = { .SeqId = SEQID_0, .SeqRamAddr = 0, .SeqLen = seq_len, .WriteSRAM = bTRUE, .pSeqCmd = (const uint32_t *)p_seq, }; AD5940_SEQInfoCfg(&seq_info); } } ``` ``` -------------------------------- ### Control AD5940 Analog Front-End Blocks Source: https://context7.com/analogdevicesinc/ad5940lib/llms.txt Enables or disables individual AFE functional blocks using a bitmask. Use bTRUE to enable and bFALSE to disable. ```c #include "ad5940.h" void RunMeasurement(void) { /* Power up ADC and waveform generator together */ AD5940_AFECtrlS(AFECTRL_ADCPWR | AFECTRL_WG, bTRUE); /* Start ADC conversion */ AD5940_AFECtrlS(AFECTRL_ADCCNV, bTRUE); /* … wait for FIFO threshold interrupt … */ /* Stop conversion and power down to save energy */ AD5940_AFECtrlS(AFECTRL_ADCCNV | AFECTRL_ADCPWR | AFECTRL_WG, bFALSE); } ``` -------------------------------- ### Advance Software Frequency Sweep with AD5940_SweepNext Source: https://context7.com/analogdevicesinc/ad5940lib/llms.txt Employ AD5940_SweepNext to calculate the subsequent frequency point in a software-managed sweep. This function handles logarithmic or linear sweeps and wraps automatically. ```c #include "ad5940.h" static SoftSweepCfg_Type sweep = { .SweepEn = bTRUE, .SweepStart = 100.0f, /* 100 Hz */ .SweepStop = 100000.0f, /* 100 kHz */ .SweepPoints = 50, .SweepLog = bTRUE, /* Logarithmic spacing */ .SweepIndex = 0, }; void NextMeasurementPoint(void) { float next_freq = 0.0f; AD5940_SweepNext(&sweep, &next_freq); AD5940_WGFreqCtrlS(next_freq, 16000000.0f); /* Set up and trigger measurement at next_freq … */ } ``` -------------------------------- ### AD5940_AFEPwrBW Source: https://context7.com/analogdevicesinc/ad5940lib/llms.txt Sets the AFE power mode (low-power or high-power) and the system bandwidth for the excitation loop. ```APIDOC ## AD5940_AFEPwrBW ### Description Selects between low-power (LP) and high-power (HP) AFE modes and sets the excitation loop system bandwidth. HP mode is required for signals above approximately 80 kHz. ### Function Signature ```c void AD5940_AFEPwrBW(enum AD5940PowerMode PowerMode, enum AD5940Bandwidth Bandwidth); ``` ### Parameters #### Path Parameters - **PowerMode** (enum AD5940PowerMode) - Required - Specifies the power mode: `AFEPWR_LP` (Low Power) or `AFEPWR_HP` (High Power). - **Bandwidth** (enum AD5940Bandwidth) - Required - Specifies the system bandwidth: `AFEBW_250KHZ`, `AFEBW_62K5HZ`, `AFEBW_15K6HZ`, `AFEBW_3K9HZ`, `AFEBW_AUTOSET`. ### Usage Example ```c #include "ad5940.h" void SetHighSpeedMode(void) { /* High power, 200 kHz bandwidth – required for EIS above 80 kHz */ AD5940_AFEPwrBW(AFEPWR_HP, AFEBW_250KHZ); } void SetLowPowerMode(void) { /* Low power, auto bandwidth based on WG frequency word */ AD5940_AFEPwrBW(AFEPWR_LP, AFEBW_AUTOSET); } ``` ``` -------------------------------- ### Set AD5940 AFE Power Mode and Bandwidth Source: https://context7.com/analogdevicesinc/ad5940lib/llms.txt Configures AFE power mode (LP/HP) and system bandwidth. HP mode is necessary for signals above approximately 80 kHz. ```c #include "ad5940.h" void SetHighSpeedMode(void) { /* High power, 200 kHz bandwidth – required for EIS above 80 kHz */ AD5940_AFEPwrBW(AFEPWR_HP, AFEBW_250KHZ); } void SetLowPowerMode(void) { /* Low power, auto bandwidth based on WG frequency word */ AD5940_AFEPwrBW(AFEPWR_LP, AFEBW_AUTOSET); } ```