1 /*!
2     \file    gd32f4xx_iref.c
3     \brief   IREF driver
4 
5     \version 2016-08-15, V1.0.0, firmware for GD32F4xx
6     \version 2018-12-12, V2.0.0, firmware for GD32F4xx
7     \version 2020-09-30, V2.1.0, firmware for GD32F4xx
8     \version 2022-03-09, V3.0.0, firmware for GD32F4xx
9 */
10 
11 /*
12     Copyright (c) 2022, GigaDevice Semiconductor Inc.
13 
14     Redistribution and use in source and binary forms, with or without modification,
15 are permitted provided that the following conditions are met:
16 
17     1. Redistributions of source code must retain the above copyright notice, this
18        list of conditions and the following disclaimer.
19     2. Redistributions in binary form must reproduce the above copyright notice,
20        this list of conditions and the following disclaimer in the documentation
21        and/or other materials provided with the distribution.
22     3. Neither the name of the copyright holder nor the names of its contributors
23        may be used to endorse or promote products derived from this software without
24        specific prior written permission.
25 
26     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
30 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
35 OF SUCH DAMAGE.
36 */
37 
38 #include "gd32f4xx_iref.h"
39 
40 /*!
41     \brief      deinitialize IREF
42     \param[in]  none
43     \param[out] none
44     \retval     none
45 */
iref_deinit(void)46 void iref_deinit(void)
47 {
48     rcu_periph_reset_enable(RCU_IREFRST);
49     rcu_periph_reset_disable(RCU_IREFRST);
50 }
51 
52 /*!
53     \brief    enable IREF
54     \param[in]  none
55     \param[out] none
56     \retval     none
57 */
iref_enable(void)58 void iref_enable(void)
59 {
60     IREF_CTL |= IREF_CTL_CREN;
61 }
62 
63 /*!
64     \brief      disable IREF
65     \param[in]  none
66     \param[out] none
67     \retval     none
68 */
iref_disable(void)69 void iref_disable(void)
70 {
71     IREF_CTL &= ~IREF_CTL_CREN;
72 }
73 
74 /*!
75     \brief      set IREF mode
76     \param[in]  step
77       \arg        IREF_MODE_LOW_POWER: 1uA step
78       \arg        IREF_MODE_HIGH_CURRENT: 8uA step
79     \param[out] none
80     \retval     none
81 */
iref_mode_set(uint32_t step)82 void iref_mode_set(uint32_t step)
83 {
84     IREF_CTL &= ~IREF_CTL_SSEL;
85     IREF_CTL |= step;
86 }
87 
88 /*!
89     \brief      set IREF precision_trim_value
90     \param[in]  precisiontrim
91       \arg        IREF_CUR_PRECISION_TRIM_X(x=0..31): (-15+ x)%
92     \param[out] none
93     \retval     none
94 */
iref_precision_trim_value_set(uint32_t precisiontrim)95 void iref_precision_trim_value_set(uint32_t precisiontrim)
96 {
97     IREF_CTL &= ~IREF_CTL_CPT;
98     IREF_CTL |= precisiontrim;
99 }
100 
101 /*!
102     \brief      set IREF sink mode
103     \param[in]  sinkmode
104       \arg        IREF_SOURCE_CURRENT : source current.
105       \arg        IREF_SINK_CURRENT: sink current
106     \param[out] none
107     \retval     none
108 */
iref_sink_set(uint32_t sinkmode)109 void iref_sink_set(uint32_t sinkmode)
110 {
111     IREF_CTL &= ~IREF_CTL_SCMOD;
112     IREF_CTL |= sinkmode;
113 }
114 
115 /*!
116     \brief      set IREF step data
117     \param[in]  stepdata
118       \arg        IREF_CUR_STEP_DATA_X:(x=0..63): step*x
119     \param[out] none
120     \retval     none
121 */
122 
iref_step_data_config(uint32_t stepdata)123 void iref_step_data_config(uint32_t stepdata)
124 {
125     IREF_CTL &= ~IREF_CTL_CSDT;
126     IREF_CTL |= stepdata;
127 }
128