1 /**
2   ******************************************************************************
3   * @file    lib_adc_tiny.c
4   * @author  Application Team
5   * @version V1.1.0
6   * @date    2019-10-28
7   * @brief   ADC_TINY library.
8   ******************************************************************************
9   * @attention
10   *
11   ******************************************************************************
12   */
13 #include "lib_adc_tiny.h"
14 
15 #define ANA_REGF_RSTValue (0U)
16 
17 /**
18   * @brief  Initializes the Tiny ADC peripheral registers to their default reset values.
19   * @param  None
20   * @retval None
21   */
TADC_DeInit(void)22 void TADC_DeInit(void)
23 {
24   ANA->REGF = ANA_REGF_RSTValue;
25   ANA->INTSTS = ANA_INTSTS_INTSTS13;
26   ANA->INTEN &= ~ANA_INTEN_INTEN13;
27   ANA->MISC &= ~ANA_MISC_TADCTH;
28 }
29 
30 /**
31   * @brief  Fills each TADC_InitStruct member with its default value.
32   * @param  TADC_InitStruct: pointer to an TADCInitType structure which will be initialized.
33   * @retval None
34   */
TADC_StructInit(TADCInitType * TADC_InitStruct)35 void TADC_StructInit(TADCInitType* TADC_InitStruct)
36 {
37   /*--------------- Reset TADC init structure parameters values ---------------*/
38   /* Initialize the SignalSel member */
39   TADC_InitStruct->SignalSel = ADCTINY_SIGNALSEL_IOE6;
40   /* Initialize the ADTREF1 member */
41   TADC_InitStruct->ADTREF1 = ADCTINY_REF1_0_9;
42   /* Initialize the ADTREF2 member */
43   TADC_InitStruct->ADTREF2 = ADCTINY_REF2_1_8;
44   /* Initialize the ADTREF3 member */
45   TADC_InitStruct->ADTREF3 = ADCTINY_REF3_2_7;
46 }
47 
48 /**
49   * @brief  Initializes Tiny ADC.
50   * @param  TADC_InitStruct
51                 SelADT:
52                     ADCTINY_SIGNALSEL_IOE6
53                     ADCTINY_SIGNALSEL_IOE7
54                 ADTREF1:
55                     ADCTINY_REF1_0_9
56                     ADCTINY_REF1_0_7
57                 ADTREF2:
58                     ADCTINY_REF2_1_8
59                     ADCTINY_REF2_1_6
60                 ADTREF3:
61                     ADCTINY_REF3_2_7
62                     ADCTINY_REF3_2_5
63   * @retval None
64   */
TADC_Init(TADCInitType * TADC_InitStruct)65 void TADC_Init(TADCInitType* TADC_InitStruct)
66 {
67   uint32_t tmp;
68 
69   /* Check parameters */
70   assert_parameters(IS_ADCTINY_SELADT(TADC_InitStruct->SignalSel));
71   assert_parameters(IS_ADCTINY_ADTREF1(TADC_InitStruct->ADTREF1));
72   assert_parameters(IS_ADCTINY_ADTREF2(TADC_InitStruct->ADTREF2));
73   assert_parameters(IS_ADCTINY_ADTREF3(TADC_InitStruct->ADTREF3));
74 
75   tmp = ANA->REGF;
76   tmp &= ~(ANA_REGF_ADTSEL     \
77           |ANA_REGF_ADTREF1SEL\
78           |ANA_REGF_ADTREF2SEL\
79           |ANA_REGF_ADTREF3SEL);
80   tmp |= (TADC_InitStruct->SignalSel \
81           |TADC_InitStruct->ADTREF1\
82           |TADC_InitStruct->ADTREF2\
83           |TADC_InitStruct->ADTREF3);
84   ANA->REGF = tmp;
85 }
86 
87 /**
88   * @brief  Enables or disables Tiny ADC .
89   * @param  NewState
90                 ENABLE
91                 DISABLE
92   * @retval None
93   */
TADC_Cmd(uint32_t NewState)94 void TADC_Cmd(uint32_t NewState)
95 {
96   /* Check parameters */
97   assert_parameters(IS_FUNCTIONAL_STATE(NewState));
98   if (NewState == ENABLE)
99     ANA->REGF |= ANA_REGF_ADTPDN;
100   else
101     ANA->REGF &= ~ANA_REGF_ADTPDN;
102 }
103 
104 /**
105   * @brief  Gets Tiny ADC output value.
106   * @param  None
107   * @retval Output of Tiny ADC(0 ~ 3).
108   */
TADC_GetOutput(void)109 uint8_t TADC_GetOutput(void)
110 {
111   return ((ANA->CMPOUT & ANA_CMPOUT_TADCO) >> ANA_CMPOUT_TADCO_Pos);
112 }
113 
114 /**
115   * @brief  Configures Tiny ADC interrupt threshold.
116   * @param  THSel:
117                 ADCTINY_THSEL_0
118                 ADCTINY_THSEL_1
119                 ADCTINY_THSEL_2
120                 ADCTINY_THSEL_3
121   * @retval None.
122   */
TADC_IntTHConfig(uint32_t THSel)123 void TADC_IntTHConfig(uint32_t THSel)
124 {
125   uint32_t tmp;
126 
127   /* Check parameters */
128   assert_parameters(IS_ADCTINY_THSEL(THSel));
129 
130   tmp = ANA->MISC;
131   tmp &= ~ANA_MISC_TADCTH;
132   tmp |= THSel;
133   ANA->MISC = tmp;
134 }
135 
136 /**
137   * @brief  Enables or disables Tiny ADC interrupt.
138   * @param  NewState
139                 ENABLE
140                 DISABLE
141   * @retval None
142   */
TADC_INTConfig(uint32_t NewState)143 void TADC_INTConfig(uint32_t NewState)
144 {
145   /* Check parameters */
146   assert_parameters(IS_FUNCTIONAL_STATE(NewState));
147   if (NewState == ENABLE)
148     ANA->INTEN |= ANA_INTEN_INTEN13;
149   else
150     ANA->INTEN &= ~ANA_INTEN_INTEN13;
151 }
152 
153 /**
154   * @brief  Gets Tiny ADC interrupt status.
155   * @param  None
156   * @retval Interrupt status.
157   */
TADC_GetINTStatus(void)158 uint8_t TADC_GetINTStatus(void)
159 {
160   if (ANA->INTSTS & ANA_INTSTS_INTSTS13)
161     return 1;
162   else
163     return 0;
164 }
165 
166 /**
167   * @brief  Clears Tiny ADC interrupt status.
168   * @param  None
169   * @retval None
170   */
TADC_ClearINTStatus(void)171 void TADC_ClearINTStatus(void)
172 {
173   ANA->INTSTS = ANA_INTSTS_INTSTS13;
174 }
175 
176 /*********************************** END OF FILE ******************************/
177