1 /**
2 * \ingroup MODULMACROS
3 *
4 * \file RestoreContext_ReleaseJtag.c
5 *
6 * \brief Restore the CPU context and releast Jtag control
7 *
8 */
9 /*
10 * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
11 *
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 *
17 * Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 *
20 * Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the
23 * distribution.
24 *
25 * Neither the name of Texas Instruments Incorporated nor the names of
26 * its contributors may be used to endorse or promote products derived
27 * from this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 */
41
42 #include "error_def.h"
43 #include "arch.h"
44 #include "edt.h"
45 #include "hal.h"
46 #include "hal_ref.h"
47 #include "stream.h"
48 #include "EEM_defs.h"
49 #include "global_variables.h"
50
51 extern DeviceSettings deviceSettings;
52
53 /**
54 RestoreContext_ReleaseJtag
55 Restore the CPU context and releast Jtag control.
56 inData: <wdtAddr(16)> <wdtCtrl(16)> <PC(32)> <SR(16)><eemCtrl(16)> <mdb(16)>
57 outData: -
58 wdtAddr: watchdog timer address
59 wdtCtrl: watchdog timer control value
60 PC: program counter register (R0)
61 SR: status register (R2)
62 eemCtrl: EEM control bits (EEM_EN and optional EEM_CLK_EN)
63 mdb: value to be put on the Memory Data Bus before release (in case !0)
64 */
HAL_FUNCTION(_hal_RestoreContext_ReleaseJtag)65 HAL_FUNCTION(_hal_RestoreContext_ReleaseJtag)
66 {
67 unsigned short wdt_addr;
68 unsigned short wdt_value;
69 unsigned short pc[2];
70 unsigned short sr;
71 unsigned short control_mask;
72 unsigned short mdb;
73 unsigned short releaseJtag;
74 unsigned short* syncWithRunVarAddress = 0;
75
76 // Check all the input parameters
77 if(STREAM_get_word(&wdt_addr) != 0)
78 {
79 return (HALERR_RESTORECONTEXT_RELEASE_JTAG_NO_WDT_ADDRESS);
80 }
81 if(STREAM_get_word(&wdt_value) != 0)
82 {
83 return (HALERR_RESTORECONTEXT_RELEASE_JTAG_NO_WDT_VALUE);
84 }
85 if(STREAM_get_long((unsigned long*)&pc) != 0)
86 {
87 return (HALERR_RESTORECONTEXT_RELEASE_JTAG_NO_PC);
88 }
89 if(STREAM_get_word(&sr) != 0)
90 {
91 return (HALERR_RESTORECONTEXT_RELEASE_JTAG_NO_SR);
92 }
93 if(STREAM_get_word(&control_mask) != 0)
94 {
95 return (HALERR_RESTORECONTEXT_RELEASE_JTAG_NO_CONTROL_MASK);
96 }
97 if(STREAM_get_word(&mdb) != 0)
98 {
99 return (HALERR_RESTORECONTEXT_RELEASE_JTAG_NO_MDB);
100 }
101 if (STREAM_get_word(&releaseJtag) == -1)
102 {
103 releaseJtag = 0;
104 }
105 STREAM_discard_bytes(2);
106
107
108 // Write back Status Register
109 WriteCpuReg(2, sr);
110
111 // Restore Watchdog Control Register
112 WriteMemWord(wdt_addr, wdt_value);
113
114 // restore Program Counter
115 SetPc(pc[0]); // High part is not relevant for MSP430 original architecture
116
117 /* Workaround for MSP430F149 derivatives */
118 {
119 unsigned short backup[3] = {0};
120 eem_data_exchange();
121 SetReg_16Bits(0x03); backup[0] = SetReg_16Bits(0);
122 SetReg_16Bits(0x0B); backup[1] = SetReg_16Bits(0);
123 SetReg_16Bits(0x13); backup[2] = SetReg_16Bits(0);
124
125 SetReg_16Bits(0x02); SetReg_16Bits(0);
126 SetReg_16Bits(0x0A); SetReg_16Bits(0);
127 SetReg_16Bits(0x12); SetReg_16Bits(0);
128
129 SetReg_16Bits(0x02); SetReg_16Bits(backup[0]);
130 SetReg_16Bits(0x0A); SetReg_16Bits(backup[1]);
131 SetReg_16Bits(0x12); SetReg_16Bits(backup[2]);
132 }
133 /**/
134
135 if (deviceSettings.clockControlType != GCC_NONE)
136 {
137 if(deviceSettings.stopFLL)
138 {
139 unsigned short clkCntrl = 0;
140 // read access to EEM General Clock Control Register (GCLKCTRL)
141 eem_data_exchange();
142 SetReg_16Bits(MX_GCLKCTRL + MX_READ);
143 clkCntrl = SetReg_16Bits(0);
144
145 // added UPSF: FE427 does regulate the FLL to the upper boarder
146 // added the switch off and release of FLL (JTFLLO)
147 clkCntrl &= ~0x10;
148 eem_data_exchange();
149 SetReg_16Bits(MX_GCLKCTRL + MX_WRITE);
150 SetReg_16Bits(clkCntrl);
151 }
152 }
153
154 if (deviceSettings.clockControlType == GCC_EXTENDED)
155 {
156 eem_data_exchange();
157 SetReg_16Bits(MX_GENCNTRL + MX_WRITE); // write access to EEM General Control Register (MX_GENCNTRL)
158 SetReg_16Bits(EMU_FEAT_EN | EMU_CLK_EN | CLEAR_STOP | EEM_EN); // write into MX_GENCNTRL
159 }
160 if (deviceSettings.clockControlType == GCC_STANDARD_I)
161 {
162 eem_data_exchange();
163 SetReg_16Bits(MX_GENCNTRL + MX_WRITE); // write access to EEM General Control Register (MX_GENCNTRL)
164 SetReg_16Bits(EMU_FEAT_EN); // write into MX_GENCNTRL
165 }
166
167 // activate EEM
168 eem_write_control();
169 SetReg_16Bits(control_mask);
170
171 // Pre-initialize MDB before release if
172 if(mdb)
173 {
174 data_16bit();
175 SetReg_16Bits(mdb);
176 IHIL_Tclk(0);
177 addr_capture();
178 IHIL_Tclk(1);
179 }
180 else
181 {
182 addr_capture();
183 }
184
185 syncWithRunVarAddress = getTargetRunningVar();
186 if(syncWithRunVarAddress)
187 {
188 *syncWithRunVarAddress = 0x0001;
189 }
190
191 // release target device from JTAG control
192 cntrl_sig_release();
193
194 if(releaseJtag)
195 {
196 IHIL_Close(); // release JTAG on go
197 }
198 return (0);
199 }
200