# Project Name：Adc_PwmTrg_Demo

---
## Function description

This project is designed to demonstrate how to trigger ADC conversion by PWM in MCAL:

- **Adc**: 
  - Configure two groups (Adc0_HwTrgGroup and Adc0_SwTrgGroup).The Adc0_HwTrgGroup is configured to hardware trigger and one short mode. The Adc0_SwTrgGroup is configured to software trigger and continuous mode. The detail information of each group is as follows:

|Group Name|Transfer Type|Trigger Type|Conversion and Access Mode|Channel List|
|----|----|----|----|----|
|Adc0_HwTrgGroup|Interrupt|Hardware|One short conversion and single access|ADC0_SE0, ADC0_SE1, PMC VREF, TEMP, VREFH|
|Adc0_SwTrgGroup|Interrupt|Software|Continuous conversion and single access|ADC0_SE0, ADC0_SE1, PMC VREF, TEMP, VREFH|


- **Pwm**:
  - Configure eTMR0_CH0 to output a 2kHz 50% PWM wave and enable eTMR0 'PwmEtmrMidTrgEn' to trigger Adc0.

- **CddTrg**
  - Configure the trigger source for 'TMU_TRIG_TARGET_ADC0_EXT_TRIG0' to be 'TMU_TRIG_SOURCE_eTMR0_EXT_TRIG' and configure 'TMU_TRIG_TARGET_TMU_OUT4' to observe the trigger signal.

- **Adc_PwmTrig_Demo HW connection diagram**

  | Function | MCU Pin | EVB pin header|
  | ---- | ------- | ------ |
  | ADC0_SE0 | PTA0 | H4_4P |
  | ADC0_SE1 | PTA1 | H4_3P |
  | eTMR0_CH2 | PTC0 | H2_15P |
  | TMU_OUT4 | PTE10 | H1_6P |
 
 In this demo, Adc0_HwTrgGroup and Adc0_SwTrgGroup alternate in performing data conversion. After the MCU resets and completes initialization, it enables Adc0_HwTrgGroup, Adc0_SwTrgGroup, and the PWM output. The  Adc0_HwTrgGroup is converted firstly when the hardware trigger signal is generated by PWM. The Adc0_HwTrgGroup is disabled and the Adc0_SwTrgGroup start converting in Adc0_HwTrgGroup's completion notification function  After Adc0_SwTrgGroup completes conversion, it enables Adc0_HwTrgGroup again. At this point, Adc0_HwTrgGroup waits for the PWM trigger signal and begins conversion when the signal is generated. The process repeats. In the While(1) loop, the results of both groups are checked to see if they are consistent.

This demo code defines the following variables to observe the acquisition results and the program's running status:
- **Variables for ADC Conversion Results**
  - `Adc0_HwTrigGroupResultPhys`: Array to store the conversion results of the Adc0_HwTrgGroup group, with the unit in millivolts.
  - `Adc0_SwTrigGroupResultPhys`: Array to store the conversion results of the Adc0_SwTrgGroup, with the unit in millivolts.
- **Variables for Counting Conversion Times**
  - `Adc0_HwTrigGroupConCompCnt`: Counts the number of completed conversions for Adc0_HwTrgGroup.
  - `Adc0_SwTrigGroupConCompCnt`: Counts the number of completed conversions for Adc0_SwTrgGroup.
  - `Adc0_ResultNotMachCnt`: The count of conversion result mismatches between Adc0_HwTrgGroup and Adc0_SwTrgGroup on the same input channels.
- **Expected values for the conversion results of each Channel** (VDD supply voltage is 5V):
  
|Channel|Description |Expected Value (mV)|
|----|----|----|
|ADC0_SE0~ADC0_SE1|ADC0 external input channels  |External input value  |
|PMC VREF|PMC 1.2V reference voltage  |1200  |
|VREFH|VREFH voltage value |5000  |
|TEMP|MCU internal temperature sensor  |Uncertain values, related to the ambient temperature |

**Note**: In this Demo, the MCU is powered by 5V. If you need to use 3.3V power supply, please modify the calculation of the physical values of the ADC conversion results by yourself.

---

## Setup

+ ***Step1***: Open the `Adc_PwmTrg_Demo.yct` with the YCT tool, config the toolchain per your required(by default, the VS code + GCC are used)
+ ***Step2***：Generate the codes， you can change the configurations if needed(to adapt to your own HW design), if so, please ensure all the modifications are OK with no any errors exist before generating the code.
+ ***Step3***: Open the generated project with VS code/IAR/Keil IDE per your selected.
+ ***Step4***: Compile the project, power on the EVB and download it to the EVB via the available debugger, such as a J-Link debugger probe.
+ ***Step5***: At last, enjoy the demo example.
  

---

## Hardware requirement
+ **EVB**: YTM32B1MD1-EVB-Q100-Rev.A/B
+ **Debugger**: J-Link HW v9 or later with YUNTU `YTM32B1MD1` patch(support package) installed.

**Note**: Please check the version number at the bottom of the EVB. If it is `YTM32B1MD1-EVB-Q100-Rev.A/B`, please use the example projects ending with `_RevA` for `Icu_Demo`.
---

## Software requirement

+ **Config & Code Generation Tool**
  + **MCAL**: Yuntu Config Tool(YCT) v2.7.0 or later version
+ **Compiler Toolchain**
  + **GCC**: GNU GCC v10.3 or later version
  + **Keil/MDK**: ARM Keil/MDK v5.36 or later version(`armclang v6.18.2` is recommended to be used as the compiler)
  + **IAR**: IAR v9.50 or later version
  + **VS Code**: Microsoft Visual Studio Code IDE v1.76 or later version(C/C++ support, GNU toolchain, CMake v3.25, ninja, Cortex-Debug extensions installation required)
+ **Debugger driver**
    + **J-Link**: SEGGER J-Link v7.82 or later version, YUNTU `YTM32B1MD1` patch(support package) installation required.
    + **Ozone Debugger tool**: SEGGER Ozone v3.28 or later version.

    ***Note***: **YTM32B1MD1** patch install required which can be obtained via **YCT** or contact ***YUNTU sales/FAE team***.

---