1 /*!
2     \file    gd32e50x_dbg.c
3     \brief   DBG driver
4 
5     \version 2020-03-10, V1.0.0, firmware for GD32E50x
6     \version 2020-08-26, V1.1.0, firmware for GD32E50x
7     \version 2021-03-23, V1.2.0, firmware for GD32E50x
8 */
9 
10 /*
11     Copyright (c) 2021, GigaDevice Semiconductor Inc.
12 
13     Redistribution and use in source and binary forms, with or without modification,
14 are permitted provided that the following conditions are met:
15 
16     1. Redistributions of source code must retain the above copyright notice, this
17        list of conditions and the following disclaimer.
18     2. Redistributions in binary form must reproduce the above copyright notice,
19        this list of conditions and the following disclaimer in the documentation
20        and/or other materials provided with the distribution.
21     3. Neither the name of the copyright holder nor the names of its contributors
22        may be used to endorse or promote products derived from this software without
23        specific prior written permission.
24 
25     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
34 OF SUCH DAMAGE.
35 */
36 
37 #include "gd32e50x_dbg.h"
38 
39 #define DBG_RESET_VAL       ((uint32_t)0x00000000U)   /*!< DBG reset value */
40 
41 /*!
42     \brief      deinitialize the DBG
43     \param[in]  none
44     \param[out] none
45     \retval     none
46 */
dbg_deinit(void)47 void dbg_deinit(void)
48 {
49     DBG_CTL = DBG_RESET_VAL;
50 }
51 
52 /*!
53     \brief      read DBG_ID code register
54     \param[in]  none
55     \param[out] none
56     \retval     DBG_ID code
57 */
dbg_id_get(void)58 uint32_t dbg_id_get(void)
59 {
60     return DBG_ID;
61 }
62 
63 /*!
64     \brief      enable low power behavior when the mcu is in debug mode
65     \param[in]  dbg_low_power:
66                 one or more parameters can be selected which are shown as below:
67       \arg        DBG_LOW_POWER_SLEEP: keep debugger connection during sleep mode
68       \arg        DBG_LOW_POWER_DEEPSLEEP: keep debugger connection during deepsleep mode
69       \arg        DBG_LOW_POWER_STANDBY: keep debugger connection during standby mode
70     \param[out] none
71     \retval     none
72 */
dbg_low_power_enable(uint32_t dbg_low_power)73 void dbg_low_power_enable(uint32_t dbg_low_power)
74 {
75     DBG_CTL |= dbg_low_power;
76 }
77 
78 /*!
79     \brief      disable low power behavior when the mcu is in debug mode
80     \param[in]  dbg_low_power:
81                 one or more parameters can be selected which are shown as below:
82       \arg        DBG_LOW_POWER_SLEEP: do not keep debugger connection during sleep mode
83       \arg        DBG_LOW_POWER_DEEPSLEEP: do not keep debugger connection during deepsleep mode
84       \arg        DBG_LOW_POWER_STANDBY: do not keep debugger connection during standby mode
85     \param[out] none
86     \retval     none
87 */
dbg_low_power_disable(uint32_t dbg_low_power)88 void dbg_low_power_disable(uint32_t dbg_low_power)
89 {
90     DBG_CTL &= ~dbg_low_power;
91 }
92 
93 /*!
94     \brief      enable peripheral behavior when the mcu is in debug mode
95     \param[in]  dbg_periph: refer to dbg_periph_enum
96                 only one parameter can be selected which is shown as below:
97       \arg        DBG_FWDGT_HOLD : hold FWDGT counter when core is halted
98       \arg        DBG_WWDGT_HOLD : hold WWDGT counter when core is halted
99       \arg        DBG_CANx_HOLD (x=0,1,2 CAN2 is only available for CL series): hold CANx receive register counter when core is halted
100       \arg        DBG_I2Cx_HOLD (x=0,1,2): hold I2Cx smbus timeout when core is halted
101       \arg        DBG_TIMERx_HOLD (x=0,1,2,3,4,5,6,7,8,9,10,11,12,13,TIMER8..13 are not available for GD32EPRT series): hold TIMERx counter when core is halted
102       \arg        DBG_SHRTIMER_HOLD : hold SHRTIMER counter when core is halted, except for GD32EPRT series
103     \param[out] none
104     \retval     none
105 */
dbg_periph_enable(dbg_periph_enum dbg_periph)106 void dbg_periph_enable(dbg_periph_enum dbg_periph)
107 {
108     DBG_REG_VAL(dbg_periph) |= BIT(DBG_BIT_POS(dbg_periph));
109 }
110 
111 /*!
112     \brief      disable peripheral behavior when the mcu is in debug mode
113     \param[in]  dbg_periph: refer to dbg_periph_enum
114                 only one parameter can be selected which is shown as below:
115       \arg        DBG_FWDGT_HOLD : hold FWDGT counter when core is halted
116       \arg        DBG_WWDGT_HOLD : hold WWDGT counter when core is halted
117       \arg        DBG_CANx_HOLD (x=0,1,2 CAN2 is only available for CL series): hold CANx receive register counter when core is halted
118       \arg        DBG_I2Cx_HOLD (x=0,1,2): hold I2Cx smbus timeout when core is halted
119       \arg        DBG_TIMERx_HOLD (x=0,1,2,3,4,5,6,7,8,9,10,11,12,13,TIMER8..13 are not available for GD32EPRT series): hold TIMERx counter when core is halted
120       \arg        DBG_SHRTIMER_HOLD : hold SHRTIMER counter when core is halted, except for GD32EPRT series
121     \param[out] none
122     \retval     none
123 */
dbg_periph_disable(dbg_periph_enum dbg_periph)124 void dbg_periph_disable(dbg_periph_enum dbg_periph)
125 {
126     DBG_REG_VAL(dbg_periph) &= ~BIT(DBG_BIT_POS(dbg_periph));
127 }
128 
129 /*!
130     \brief      enable trace pin assignment
131     \param[in]  none
132     \param[out] none
133     \retval     none
134 */
dbg_trace_pin_enable(void)135 void dbg_trace_pin_enable(void)
136 {
137     DBG_CTL |= DBG_CTL_TRACE_IOEN;
138 }
139 
140 /*!
141     \brief      disable trace pin assignment
142     \param[in]  none
143     \param[out] none
144     \retval     none
145 */
dbg_trace_pin_disable(void)146 void dbg_trace_pin_disable(void)
147 {
148     DBG_CTL &= ~DBG_CTL_TRACE_IOEN;
149 }
150 
151 /*!
152     \brief      set trace pin mode
153     \param[in]  trace_mode:
154                 only one parameter can be selected which is shown as below:
155       \arg        TRACE_MODE_ASYNC: trace pin used for async mode
156       \arg        TRACE_MODE_SYNC_DATASIZE_1: trace pin used for sync mode and data size is 1
157       \arg        TRACE_MODE_SYNC_DATASIZE_2: trace pin used for sync mode and data size is 2
158       \arg        TRACE_MODE_SYNC_DATASIZE_4: trace pin used for sync mode and data size is 4
159     \param[out] none
160     \retval     none
161 */
dbg_trace_pin_mode_set(uint32_t trace_mode)162 void dbg_trace_pin_mode_set(uint32_t trace_mode)
163 {
164     DBG_CTL &= ~DBG_CTL_TRACE_MODE;
165     DBG_CTL |= trace_mode;
166 }
167