summaryrefslogtreecommitdiff
path: root/bsp/nuvoton/libraries/ma35/StdDriver/src/nu_adc.c
blob: d8dfdf1342c070805bb84ce4a2af5fa4e55ae2f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**************************************************************************//**
 * @file     adc.c
 * @brief    ADC driver source file
 *
 * SPDX-License-Identifier: Apache-2.0
 * @copyright(C) 2020 Nuvoton Technology Corp. All rights reserved.
 *****************************************************************************/
#include "NuMicro.h"

/** @addtogroup Standard_Driver Standard Driver
  @{
*/

/** @addtogroup ADC_Driver ADC Driver
  @{
*/


/** @addtogroup ADC_EXPORTED_FUNCTIONS ADC Exported Functions
  @{
*/

/**
  * @brief This API configures ADC module to be ready for convert the input from selected channel
  * @param[in] adc Base address of ADC module
  * @param[in] u32InputMode Input mode. Valid values are:
  *                     - \ref ADC_INPUT_MODE_NORMAL_CONV
  *                     - \ref ADC_INPUT_MODE_4WIRE_TOUCH
  *                     - \ref ADC_INPUT_MODE_5WIRE_TOUCH
  * @param[in] u32OpMode Could be
  *                     - \ref ADC_HIGH_SPEED_MODE
  *                     - \ref ADC_NORMAL_SPEED_MODE
  * @param[in] u32ChMask Channel enable bit. Each bit corresponds to a input channel. Bit 0 is channel 0, bit 1 is channel 1...
  *                      This parameter is only used while u32InputMode set to ADC_INPUT_MODE_NORMAL_CONV.
  * @return  None
  * @note ADC can only convert 1 channel at a time. If more than 1 channels are enabled, only channel
  *       with smallest number will be convert.
  * @note This API does not turn on ADC power nor does trigger ADC conversion
  */
void ADC_Open(ADC_T *adc,
              uint32_t u32InputMode,
              uint32_t u32OpMode,
              uint32_t u32ChMask)
{
    uint32_t u32Ch = 0, i;

    if (u32InputMode == ADC_INPUT_MODE_NORMAL_CONV)
    {
        for (i = 0; i < ADC_CH_NUM; i++)
        {
            if (u32ChMask & (1 << i))
            {
                u32Ch = i;
                break;
            }
        }
        adc->CONF = (u32Ch << ADC_CONF_CHSEL_Pos) | u32OpMode | ADC_CONF_NACEN_Msk;
    }
    else if (u32InputMode == ADC_INPUT_MODE_4WIRE_TOUCH)
    {
        adc->CONF = 0;
    }
    else // 5-wire mode
    {
        adc->CTL |= ADC_CTL_WMSWCH_Msk;
        adc->CONF = 0;
    }
}

/**
  * @brief Disable ADC module
  * @param[in] adc Base address of ADC module
  * @return None
  */
void ADC_Close(ADC_T *adc)
{
    adc->CTL = 0;
    adc->CONF = 0;
    adc->IER = 0;
    adc->ISR = adc->ISR;
}

/*@}*/ /* end of group ADC_EXPORTED_FUNCTIONS */

/*@}*/ /* end of group ADC_Driver */

/*@}*/ /* end of group Standard_Driver */