WAG (波形发生器)

简介

WAG(Wave Generator)模块通过软件配置产生各种特定波形,并由指定的 DAC 输出。主要用于测试场景。

功能概述

  • 支持产生静态波、递增递减波(RAMP波)、三角波/锯齿波、方波和正弦波。

  • 支持错误状态上报

  • 支持加载模式选择

  • 支持触发模式选择

方波功能

通过配置上下限 WAG_setSquareRangeH()WAG_setSquareRangeL() 预设2个输出码值,通过触发信号 WAG_startSwTrigger() 改变翻转DAC输出电平。

../_images/Square-wave.png

除支持单次触发模式外,还支持连续输出模式,触发一次后无需软件设置触发信号即可连续输出对应波形,直到触发终止信号 WAG_stopSwTrigger() 到达,方波回到下门限停止翻转。

../_images/Square-wave-auto.png

备注

上下门限传入参数范围为0~4095,且上限要大于下限,否则会触发异常告警

RAMP波功能

通过配置上/下行步进 WAG_setRampUpStep() / WAG_setRampDownStep() 和上下门限 WAG_setRampHThrd() / WAG_setRampLThrd() ,当触发启动信号有效 加载相关配置,从下/上门限启动,每个计数时钟累加/减上/下行步进,直到达到上/下门限后保持(如累加/减超过 DAC 接口位宽,则进行饱和操作,下同), 计数期间,如有新的触发启动有效,则按新的加载参数运行,期间如触发终止信号有效,回到当前下/上门限等待下一次触发启动。

../_images/Ramp-up.png ../_images/Ramp-down.png

三角/锯齿波功能

触发启动信号有效,加载相关配置,从下/上门限启动,每个计数时钟累加/减上下行步进,直到达到上/下门限后开始以每个计数时钟累减/加下/上行步进,直到达到下/上门限后保持。 期间如收到新的触发启动,处理与递增递减波形处理类似。 期间如果收到触发终止信号,需要等待一个上下行周期结束后才停止:

    1. 触发终止信号的优先级大于触发启动优先级。在触发终止信号未被响应前,触发启动信号均被忽略。

    1. 当到达下限之后,触发终止信号被响应的同时,触发启动信号到来,此时触发启动信号会被响应。

../_images/triangle.png

三角波形通过设置上/下步进,可得到锯齿波。如:上步进不做改变,下步进改为ceil((上门限-下门限)/下门限)*下门限, 则触发启动信号有效时,从下门限启动,每个计数时钟按照上步进进行累加,到达上门限之后,下一个计数时钟减下步进,回到下门限后保持。

../_images/jagged-edge.png

正弦波功能

仅支持类似三角波和锯齿波的连续输出模式。

正弦波通过配置的初相、频率控制字(相位累加值)和输出幅值来产生,然后叠加配置的直流幅值(仅支持 1/2^N 幅度调整,N=0,…,12)后输出。正弦波频率 fsin与系统时钟fsys、分频比、频率控制字 FCW 和输出位宽 N 的关系如下:

fsin = fsys* FCW / (prescaler + 1) / 2^N  (HZ)

备注

由于仅支持 1/2^N 幅度调整,N=0,…,12,故输出位宽N为12

波形在触发启动后开始输出,触发终止后输出配置直流电平幅值。采用 CORDIC 算法 IP 产生正弦波。

正弦波参数配置函数如下:
  • WAG_setSineFrq() — 频率控制字FCW。步进越小频率越低,但分辨率固定为 f_clk/4096。

  • WAG_setSinePhase() — 初相,0~4095 线性对应 0°~360°。

  • WAG_setSineRangeScale() — 幅度乘法系数,范围 0~2047(实际满幅约 2047/1.6),决定 sin 的摆幅。1024 ≈ ±1.65V 满摆幅。

  • WAG_setSineAmplitudeScale() — 对上面结果整体右移 N 位(幅度减半 N 次),0~12。作用是粗调/对齐量程,相当于把波形除以 2^N( 1/2^N 幅度调整)。

  • WAG_setSineAmplitudeShift() — 移位后叠加的直流分量,0~4095 对应 0~Vref。

软件配置流程

正弦波配置流程如下:

  1. 配置DAC

    DAC相关配置参考: 数模转换(DAC)

    备注

    输出配置的数据加载选择从WAG:

    output.srcSel = DAC_SRC_SEL_WAG;
    DAC_configOutput(DACx, &output);
    
  2. WAG模块配置

    调用 WAG_config() 传入参数配置结构体指针

    备注

    参数主要包含:

    • 波形选择(正弦波、三角波等)

    • 迭代周期

    • 触发模式(通常使用软件触发)

    • 输出通道

  3. 使能模块

    调用 WAG_enableModule() 使能WAG模块

  4. 关闭寄存器同步加载

    调用 WAG_disableGlobalLoad() 传入通道和输出配置结构体指针

  5. 设置波形参数

    对于正弦波:

    • 调用 WAG_setSineFrq() 传入参数value,设置频率。

      备注

      实际频率计算公式:

      实际频率 = system_clock(200M) / (prescaler + 1) / 4096 * value (HZ)
      
    • 调用 WAG_setSinePhase() 传入参数,设置初相位。

    • 调用 WAG_setSineRangeScale() 传入参数,设置幅值。

  6. 开始触发

    调用 WAG_startTriggerStartSingle() 触发产生正弦波。

其他波形配置流程和正弦波配置流程相似,差异在于步骤 5, 其他波形修改此部分为对应操作即可。(模块配置时参数选择也有所区别)

例如:配置三角波,修改模块配置参数结构体,步骤 5 改为调用对应函数配置上下阈值及步进即可。

应用示例

API Reference

Header File

Functions

void WAG_enableModule(void)

Enable the WAG module.

void WAG_disableModule(void)

Disable the WAG module.

void WAG_config(WAG_ConfigParam *config)

Configures the WAG module parameters.

This function configures the parameters of the WAG module using the provided configuration structure. It ensures that the parameters are within the allowable range and writes them to the corresponding registers of the WAG module.

参数:

config – A pointer to the WAG_ConfigParam structure containing the parameters to be configured. The structure includes:

  • prescaler: The prescaler value for the WAG module clock.

  • cordicIterationCycle: The number of CORDIC iteration cycles.

  • loadmode: The load mode for the WAG module.

  • startTriggerMode: The start trigger mode.

  • stopTriggerMode: The stop trigger mode.

  • startTriggerInvertEn: Enable/disable start trigger inversion.

  • stopTriggerInvertEn: Enable/disable stop trigger inversion.

  • waveType: The type of waveform to generate.

  • staticWaveLoadMode: The load mode for static wave (if applicable).

  • squareWaveOutputMode: The output mode for square wave (if applicable).

  • rampWaveMode: The mode for ramp wave (if applicable).

  • triangleStartDirSel: The start direction for triangle wave (if applicable).

  • triangleWaveOutputMode: The output mode for triangle wave (if applicable).

void WAG_setTriangleUpStep(uint16_t value)

Set the triangle wave rising step.

备注

The value is 0~4095

参数:

value – The rising step value of the triangle wave

void WAG_setTriangleDownStep(uint16_t value)

Set the triangle wave falling step.

备注

The value is 0~4095

参数:

value – The falling step value of the triangle wave

void WAG_setTriangleHThrd(uint16_t value)

Set the triangle wave high threshold.

备注

The value is 0~4095

参数:

value – The high threshold value of the triangle wave

void WAG_setTriangleLThrd(uint16_t value)

Set the triangle wave low threshold.

备注

The value is 0~4095

参数:

value – The low threshold value of the triangle wave

void WAG_setRampUpStep(uint16_t value)

Set the ramp wave rising step.

备注

The value is 0~4095

参数:

value – The rising step value of the ramp wave

void WAG_setRampDownStep(uint16_t value)

Set the ramp wave falling step.

备注

The value is 0~4095

参数:

value – The falling step value of the ramp wave

void WAG_setRampHThrd(uint16_t value)

Set the high threshold of the ramp wave.

备注

The value is 0~4095

参数:

value – The high threshold value of the ramp wave

void WAG_setRampLThrd(uint16_t value)

Set the low threshold of the ramp wave.

备注

The value is 0~4095

参数:

value – The low threshold value of the ramp wave

void WAG_setSineFrq(uint16_t value)

Set the frequency of the sine wave.

备注

Actual frequency calculations:system_clock(200M) / (prescaler + 1) / 4096 * value (HZ)

参数:

value – The frequency value of the sine wave

void WAG_setSinePhase(uint16_t value)

Set the phase of the sine wave.

备注

The value is 0~4095. correspondence to 0° ~ 360°

参数:

value – The phase value of the sine wave

void WAG_setSineAmplitudeShift(uint16_t value)

Set the amplitude shift of the sine wave.

备注

The value is 0~4095. correspondence to 0 ~ Verf. y = Output_sine_wave + value.

参数:

value – The dc component of the sine wave output superposition

void WAG_setSineRangeScale(uint16_t value)

Set the range scale of the sine wave.

备注

The value is (0~ 2047) / K. K is about 1.6. Output_sine_wave = value * sin(ωx + φ) + 2048 >> N

参数:

value – The output range of the sine wave amplitude

void WAG_setSineAmplitudeScale(uint16_t value)

Set the amplitude scale of the sine wave.

备注

The value is 0 ~ 12. correspondence to 0 ~ Verf / 2. Output_sine_wave = A * sin(ωx + φ) + 2048 >> value.

参数:

value – The amplitude scale value of the sine wave

void WAG_setStaticWaveAmplitude(uint16_t value)

Set the amplitude of the static wave.

备注

The value is 0 ~ 4095. correspondence to 0 ~ Verf.

参数:

value – The amplitude value of the static wave

void WAG_setSquareRangeH(uint16_t value)

Set the high range of the square wave.

备注

The value is 0 ~ 4095. correspondence to 0 ~ Verf.

参数:

value – The high range value of the square wave

void WAG_setSquareRangeL(uint16_t value)

Set the low range of the square wave.

备注

The value is 0 ~ 4095. correspondence to 0 ~ Verf.

参数:

value – The low range value of the square wave

void WAG_enableGlobalLoad(void)

Enables the global load for the WAG peripheral.

void WAG_disableGlobalLoad(void)

Disables the global load for the WAG peripheral.

void WAG_setTriggerDelay(uint16_t triggerDelay)

Sets the trigger delay for the WAG (Waveform Generation) module.

参数:

triggerDelay – The delay value to be set, in the range of 0 to 255.

void WAG_configOutputHold(uint16_t codeValue, WAG_OutputHoldMode holdMode)

Configures the output hold mode for the WAG (Waveform Generation) module.

参数:
  • codeValue – The code value to be set, in the range of 0 ~ 4095.

  • holdMode – The hold mode to be set, selected from the WAG_OutputHoldMode enum.

void WAG_startTriggerStartSingle(void)

Starts the trigger for the WAG (Waveform Generation) module.

void WAG_startTriggerStopSingle(void)

Stops the trigger for the WAG (Waveform Generation) module.

void WAG_setR03LoadReady(void)

Sets the R0~R3 registers load ready single when global load is enable.

备注

When global load is enabled, R0~R3 registers load ready single need be set to notice HW

bool WAG_isR03LoadComplete(void)

Checks if the R0~R3 registers load is complete when global load is enabled.

This function checks the status of the WAG_R3_WAG_R3_Msk bit in the WAG->R3 register. When the global load is enabled, the R0 ~ R3 registers are loaded at the same time. This function returns true if the load is complete (WAG_R3_WAG_R3_Msk bit is cleared), indicating that the R0~R3 registers have been loaded into the hardware.

返回:

bool - Returns true if the R0~R3 registers load is complete, false otherwise.

uint16_t WAG_getStatus(void)

Reads the status register of the WAG (Waveform Generation) module.

返回:

uint16_t - The status of the WAG module.

Structures

struct WAG_ConfigParam

Enumerations

enum WAG_LoadMode

Values:

enumerator WAG_LOADMODE_SHADOW
enumerator WAG_LOADMODE_DIRECT
enum WAG_OutputHoldMode

Values:

enumerator WAG_OUTPUTHOLD_VALUE_REG
enumerator WAG_OUTPUTHOLD_VALUE_CURRENT
enum WAG_TriggerMode

Values:

enumerator WAG_TRIGGERMODE_SW
enumerator WAG_TRIGGERMODE_HW
enum WAG_WaveType

Values:

enumerator WAG_WAVETYPE_STATIC
enumerator WAG_WAVETYPE_SQUARE
enumerator WAG_WAVETYPE_RAMP
enumerator WAG_WAVETYPE_TRIANGLE
enumerator WAG_WAVETYPE_SINE
enum WAG_OutputMode

Values:

enumerator WAG_OUTPUTMODE_ONCE
enumerator WAG_OUTPUTMODE_CONTINOUS
enum WAG_RampWaveMode

Values:

enumerator WAG_RAMP_WAVE_DECREMENT
enumerator WAG_RAMP_WAVE_INCREMENT
enum WAG_TriangleStartDir

Values:

enumerator WAG_TRIANGLE_WAVE_START_L_THRD
enumerator WAG_TRIANGLE_WAVE_START_H_THRD