# Project Name：Adc_HwTrig_Demo

---
## Function description

This project is designed to demonstrate the configuration and usage of the MCAL Adc module:
- **Adc**: 
  - This demo showcases the mixed usage of software triggered and hardware triggered groups within a single ADC.
  - There are two groups configured in the demo,  one group triggered by hardware and the other group triggered by software. The detail information of each group is as follows:

|Group Name|Transfer Type|Trigger Type|Conversion and Access Mode|Channel List|
|----|----|----|----|----|
|SwTrigGroup|Interrupt|Software|Continuous Conversion and Circular Streaming Access|ADC0_SE0, ADC0_SE1, PMC VREF, TEMP, VREFH|
|HwTrigGroup|Interrupt|Hardware|One Short Conversion and Single Access|ADC0_SE0, ADC0_SE1, PMC VREF, TEMP, VREFH|

  - The PTB4 is configured to GPIO out, and the PTB5 is configured to TMU_IN0 which used to trigger ADC0 HwTrigGroup conversion. The PTB4 is short to PTB5 as a trigger signal for the hardware triggered group. The ADC0 HwTrigGroup will convert once when PTB4 outputs a pulse signal.

- **Adc_HwTrig_Demo HW connection diagram**

| Function | MCU Pin | EVB pin header|
| ---- | ------- | ------ |
| GPIO | PTB4 | H2_3P |
| TMU_IN0 | PTB5 | H2_2P |
**PTB4(H2_3P) and PTB5(H2_2P) short connect.**

After reset, the demo project will initialize the Adc driver , set up result buffers for all group , enable notification for the necessary groups, The groups operated as following:
  - **SwTrigGroup:** Triggered by software,read the results in while(1),auto restart the group conversion after read results when the hw trigger group is disabled.
  - **HwTrigGroup:** Triggered by TMU_IN0 which is connected to PTB4,read the results and disabled the group in callback notification.The group will be restarted after the results verified.
  - The SE0_ADCH0 and TEMP channel conversion results from all groups are mutually checked repeatedly.

This demo code defines the following variables to observe the acquisition results and the program's running status:
- **Variables for Saving ADC Conversion Results**
- `Adc0_SwTrigGroupResultPhys`: Array to store the conversion results of the SwTrigGroup group, with the unit in millivolts.
- `Adc0_HwTrigGroupResultPhys`: Array to store the conversion results of the HwTrigGroup, with the unit in millivolts.
- **Variables for Counting Conversion Times**
- `Adc0_SwTrigGroupConCompCnt`: Counts the number of completed conversions
- `Adc0_HwTrigGroupConCompCnt`: Counts the number of completed conversions
- `Adc0_HwTrigGroupTrigCnt`: Counts the number of hardware trigger events
- `Adc0_ResultNotMatchCnt`: The count of conversion result mismatches between SwTrigGroup and HwTrigGroup on the same input channels.
- `Adc0_HwTrigTimeNotMatch`: The flag to indicate whether the hardware trigger time is consistent with the HwTrigGroup conversion time: 0 means consistent, 1 means inconsistent.

- **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_HwTrig_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***.

---