1 /*!
2     \file    gd32l23x_vref.c
3     \brief   VREF driver
4 
5     \version 2021-08-04, V1.0.0, firmware for GD32L23x
6 */
7 
8 /*
9     Copyright (c) 2021, GigaDevice Semiconductor Inc.
10 
11     Redistribution and use in source and binary forms, with or without modification,
12 are permitted provided that the following conditions are met:
13 
14     1. Redistributions of source code must retain the above copyright notice, this
15        list of conditions and the following disclaimer.
16     2. Redistributions in binary form must reproduce the above copyright notice,
17        this list of conditions and the following disclaimer in the documentation
18        and/or other materials provided with the distribution.
19     3. Neither the name of the copyright holder nor the names of its contributors
20        may be used to endorse or promote products derived from this software without
21        specific prior written permission.
22 
23     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32 OF SUCH DAMAGE.
33 */
34 
35 #include "gd32l23x_vref.h"
36 
37 /*!
38     \brief      deinitialize the VREF
39     \param[in]  none
40     \param[out] none
41     \retval     none
42 */
vref_deinit(void)43 void vref_deinit(void)
44 {
45     /* reset the value of all the VREF registers */
46     VREF_CS = (uint32_t)0x00000002U;
47 }
48 
49 /*!
50     \brief      enable VREF
51     \param[in]  none
52     \param[out] none
53     \retval     none
54 */
vref_enable(void)55 void vref_enable(void)
56 {
57     VREF_CS |= VREF_EN;
58 }
59 
60 /*!
61     \brief      disable VREF
62     \param[in]  none
63     \param[out] none
64     \retval     none
65 */
vref_disable(void)66 void vref_disable(void)
67 {
68     VREF_CS &= (uint32_t)~VREF_EN;
69 }
70 
71 /*!
72     \brief      enable VREF high impendance mode
73     \param[in]  none
74     \param[out] none
75     \retval     none
76 */
vref_high_impedance_mode_enable(void)77 void vref_high_impedance_mode_enable(void)
78 {
79     VREF_CS |= VREF_HIGH_IMPEDANCE_MODE;
80 }
81 
82 /*!
83     \brief      disable VREF high impendance mode
84     \param[in]  none
85     \param[out] none
86     \retval     none
87 */
vref_high_impedance_mode_disable(void)88 void vref_high_impedance_mode_disable(void)
89 {
90     VREF_CS &= (uint32_t)~VREF_HIGH_IMPEDANCE_MODE;
91 }
92 
93 /*!
94     \brief      get the status of VREF
95     \param[in]  none
96     \param[out] none
97     \retval     the status of VREF output
98       \arg        SET: the VREF output is ready
99       \arg        RESET: the VREF output is not ready
100 */
vref_status_get(void)101 FlagStatus vref_status_get(void)
102 {
103     uint32_t temp_status = VREF_CS;
104     temp_status = temp_status >> 3U;
105     if((temp_status & 0x01U) != 0x00U) {
106         return SET;
107     } else {
108         return RESET;
109     }
110 }
111 
112 /*!
113     \brief      set the calibration value of VREF
114     \param[in]  value: calibration value (0x00 - 0x3F)
115     \param[out] none
116     \retval     none
117 */
vref_calib_value_set(uint8_t value)118 void vref_calib_value_set(uint8_t value)
119 {
120     VREF_CALIB |= (uint32_t)value;
121 }
122 
123 /*!
124     \brief      get the calibration value of VREF
125     \param[in]  none
126     \param[out] none
127     \retval     calibration value (0x00 - 0x3F)
128 */
vref_calib_value_get(void)129 uint8_t vref_calib_value_get(void)
130 {
131     uint8_t temp = (uint8_t)VREF_CALIB;
132     return temp;
133 }
134