-
Notifications
You must be signed in to change notification settings - Fork 53
/
main_part163.c
330 lines (262 loc) · 10.5 KB
/
main_part163.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*Copyright (c) 2017, Adam Taylor
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project*/
#include <stdio.h>
#include "platform.h"
#include "xgpiops.h"
#include "xil_types.h"
#include "Xscugic.h"
#include "Xil_exception.h"
#include "unistd.h"
#include "xpseudo_asm.h"
#include "xreg_cortexa9.h"
#define wfi() __asm__("wfi")
#define GPIO_DEVICE_ID XPAR_XGPIOPS_0_DEVICE_ID
#define INTC_DEVICE_ID XPAR_SCUGIC_SINGLE_DEVICE_ID
#define GPIO_INTERRUPT_ID XPS_GPIO_INT_ID
#define l2cpl310_reg15 0xF8F02F80
#define scu_control_reg 0xF8F00000
#define topsw_clk 0xF800016C
#define slcr_unlock 0xF8000008
#define ddrc_ctrl_reg1 0xF8006060
#define ddrc_para_reg3 0xF8006020
#define ddr_clk_ctrl 0xF8000124
#define dci_clk_ctrl 0xF8000128
#define arm_pll_ctrl 0xF8000100
#define ddr_pll_ctrl 0xF8000104
#define io_pll_ctrl 0xF8000108
#define arm_clk_ctrl 0xF8000120
#define gpio_int_en0 0xE000A210
#define pll_status 0xF800010C
#define uart_sel 0xF8000154
#define aper_reg 0xF800012C
#define ledpin 47
#define pbsw 51
static XScuGic Intc; /* The Instance of the Interrupt Controller Driver */
static XGpioPs Gpio; /* The driver instance for GPIO Device. */
int toggle;//used to toggle the LED
static void SetupInterruptSystem(XScuGic *GicInstancePtr, XGpioPs *Gpio, u16 GpioIntrId);
static void IntrHandler(void *CallBackRef, int Bank, u32 Status);
int main()
{
int Status;
XGpioPs_Config *GPIOConfigPtr;
init_platform();
printf("low power example\n\r");
//GPIO Initilization
GPIOConfigPtr = XGpioPs_LookupConfig(XPAR_XGPIOPS_0_DEVICE_ID);
Status = XGpioPs_CfgInitialize(&Gpio, GPIOConfigPtr,GPIOConfigPtr->BaseAddr);
if (Status != XST_SUCCESS) {
print("GPIO INIT FAILED\n\r");
return XST_FAILURE;
}
//set direction and enable output
XGpioPs_SetDirectionPin(&Gpio, ledpin, 1);
XGpioPs_SetOutputEnablePin(&Gpio, ledpin, 1);
//set direction input pin
XGpioPs_SetDirectionPin(&Gpio, pbsw, 0x0);
SetupInterruptSystem(&Intc, &Gpio, GPIO_INTERRUPT_ID);
u32 data;
XGpioPs_WritePin(&Gpio, ledpin, 1);
//cpsidf();
Xil_Out32(slcr_unlock,0xDF0D);//unlock SLCR registers for writing first
data = Xil_In32(aper_reg); //clock gate unused peripherals
printf("aper_reg = %x\n\r", (unsigned int) data);
data = 0x1600001;
Xil_Out32(aper_reg,data);
data = Xil_In32(aper_reg);
printf("aper_reg = %x\n\r", (unsigned int) data);
data = Xil_In32(arm_clk_ctrl); //clock gate unused peripherals
printf("arm_clk_ctrl = %x\n\r", (unsigned int) data);
//data = Xil_In32(uart_sel);
//printf("uart_sel = %x\n\r", (unsigned int) data);
//data |= 0x00000030;
//Xil_Out32(uart_sel,data);
data = Xil_In32(uart_sel);
printf("uart_sel = %x\n\r", (unsigned int) data);
usleep(1000000);
data = Xil_In32(arm_clk_ctrl);
printf("arm_clk_ctrl = %x\n\r", (unsigned int) data);
//data = Xil_In32(gpio_int_en0);
data = 0x00000400;
Xil_Out32(gpio_int_en0,data);
data = Xil_In32(gpio_int_en0);
printf("gpio_int_en0 = %x\n\r", (unsigned int) data);
usleep(1000000);
//configure cache for low power mode
data = Xil_In32(l2cpl310_reg15);
printf("ls cache reg 15 power = %x\n\r", (unsigned int) data);
data = 0x3; //enable standby mode and dynamic clock gating
Xil_Out32(l2cpl310_reg15,data);
data = Xil_In32(l2cpl310_reg15);
printf("ls cache reg 15 power = %x\n\r", (unsigned int) data);
usleep(1000000);
//configure scu
data = Xil_In32(scu_control_reg);
printf("scu control reg = %x\n\r", (unsigned int) data);
data |= 0x00000020; //enable standby mode and dynamic clock gating
Xil_Out32(scu_control_reg,data);
data = Xil_In32(scu_control_reg);
printf("scu control reg = %x\n\r", (unsigned int) data);
usleep(1000000);
//configure slcr
data = Xil_In32(topsw_clk);
printf("slcr control reg = %x\n\r", (unsigned int) data);
data |= 0x00000001; //enable standby mode and dynamic clock gating
Xil_Out32(topsw_clk,data);
data = Xil_In32(topsw_clk);
printf("slcr control reg = %x\n\r", (unsigned int) data);
usleep(1000000);
//set CP15
data = mfcp(XREG_CP15_POWER_CTRL);
printf("cp15 Reg = %x\n\r", (unsigned int) data);
mtcp(XREG_CP15_POWER_CTRL,0x701);
data = mfcp(XREG_CP15_POWER_CTRL);
printf("cp15 Reg = %x\n\r", (unsigned int) data);
usleep(1000000);
//set up ddr
data = Xil_In32(ddrc_ctrl_reg1);
data |= 0x00001000; //enable standby mode and dynamic clock gating
Xil_Out32(ddrc_ctrl_reg1,data);
data = Xil_In32(ddrc_para_reg3);
data |= 0x00100000; //enable standby mode and dynamic clock gating
Xil_Out32(ddrc_para_reg3,data);
data = Xil_In32(ddr_clk_ctrl);
data &= 0xFFFFFFF0; //enable standby mode and dynamic clock gating
Xil_Out32(ddr_clk_ctrl,data);
data = Xil_In32(dci_clk_ctrl);
data &= 0xFFFFFFF0; //enable standby mode and dynamic clock gating
Xil_Out32(dci_clk_ctrl,data);
// arm_pll_ctrl
data = Xil_In32(arm_pll_ctrl);
data |= 0x00000010; //enable standby mode and dynamic clock gating
Xil_Out32(arm_pll_ctrl,data);
data = Xil_In32(ddr_pll_ctrl);
data |= 0x00000010; //enable standby mode and dynamic clock gating
Xil_Out32(ddr_pll_ctrl,data);
data = Xil_In32(io_pll_ctrl);
data |= 0x00000010; //enable standby mode and dynamic clock gating
Xil_Out32(io_pll_ctrl,data);
data = Xil_In32(arm_pll_ctrl);
data |= 0x00000002; //enable standby mode and dynamic clock gating
Xil_Out32(arm_pll_ctrl,data);
data = Xil_In32(ddr_pll_ctrl);
data |= 0x00000002; //enable standby mode and dynamic clock gating
Xil_Out32(ddr_pll_ctrl,data);
data = Xil_In32(io_pll_ctrl);
data |= 0x00000002; //enable standby mode and dynamic clock gating
Xil_Out32(io_pll_ctrl,data);
data = Xil_In32(arm_clk_ctrl);
data |= 0x0003f00; //enable standby mode and dynamic clock gating
Xil_Out32(arm_clk_ctrl,data);
wfi();
XGpioPs_WritePin(&Gpio, ledpin, 0);
while(1){
toggle = !toggle;
//XGpioPs_WritePin(&Gpio, ledpin, toggle);
//printf("hello world\n\r");
usleep(1000000);
}
return 0;
}
static void SetupInterruptSystem(XScuGic *GicInstancePtr, XGpioPs *Gpio, u16 GpioIntrId)
{
XScuGic_Config *IntcConfig; /* Instance of the interrupt controller */
Xil_ExceptionInit();
/*
* Initialize the interrupt controller driver so that it is ready to
* use.
*/
IntcConfig = XScuGic_LookupConfig(INTC_DEVICE_ID);
XScuGic_CfgInitialize(GicInstancePtr, IntcConfig,
IntcConfig->CpuBaseAddress);
/*
* Connect the interrupt controller interrupt handler to the hardware
* interrupt handling logic in the processor.
*/
Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
(Xil_ExceptionHandler)XScuGic_InterruptHandler,
GicInstancePtr);
/*
* Connect the device driver handler that will be called when an
* interrupt for the device occurs, the handler defined above performs
* the specific interrupt processing for the device.
*/
XScuGic_Connect(GicInstancePtr, GpioIntrId,
(Xil_ExceptionHandler)XGpioPs_IntrHandler,
(void *)Gpio);
//Enable interrupts for all the pins in bank 1.
XGpioPs_SetIntrTypePin(Gpio, pbsw, XGPIOPS_IRQ_TYPE_LEVEL_HIGH);
//Set the handler for gpio interrupts.
XGpioPs_SetCallbackHandler(Gpio, (void *)Gpio, IntrHandler);
XGpioPs_IntrClear(Gpio, 1,0xFFFFFFFF);
//Enable the GPIO interrupts of Bank 1.
XGpioPs_IntrEnablePin(Gpio, pbsw);
//Enable the interrupt for the GPIO device.
XScuGic_Enable(GicInstancePtr, GpioIntrId);
// Enable interrupts in the Processor.
Xil_ExceptionEnableMask(XIL_EXCEPTION_ALL);
}
static void IntrHandler(void *CallBackRef, int Bank, u32 Status)
{
u32 ddrc;
XGpioPs_WritePin(&Gpio, ledpin, 0);
ddrc = 0x1177cb4; //enable standby mode and dynamic clock gating
Xil_Out32(arm_clk_ctrl,ddrc);
ddrc = Xil_In32(io_pll_ctrl);
//printf("io_pll_ctrl = %x\n\r", (unsigned int) ddrc);
ddrc &= ~0x00000002; //remove standby mode and dynamic clock gating
Xil_Out32(io_pll_ctrl,ddrc);
ddrc = Xil_In32(ddr_pll_ctrl);
//printf("ddr_pll_ctrl = %x\n\r", (unsigned int) ddrc);
ddrc &= ~0x00000002; //remove standby mode and dynamic clock gating
Xil_Out32(ddr_pll_ctrl,ddrc);
ddrc = Xil_In32(arm_pll_ctrl);
//printf("arm_pll_ctrl = %x\n\r", (unsigned int) ddrc);
ddrc &= ~0x00000002; //remove standby mode and dynamic clock gating
Xil_Out32(arm_pll_ctrl,ddrc);
ddrc = Xil_In32(pll_status);
while(ddrc != 0x0000003f){ //wait for DLL to lock and be stable
ddrc = Xil_In32(pll_status);
}
ddrc = Xil_In32(arm_pll_ctrl);
//printf("arm_pll_ctrl = %x\n\r", (unsigned int) ddrc);
ddrc &= ~0x00000010; //enable standby mode and dynamic clock gating
Xil_Out32(arm_pll_ctrl,ddrc);
ddrc = Xil_In32(arm_pll_ctrl);
//printf("arm_pll_ctrl = %x\n\r", (unsigned int) ddrc);
ddrc = Xil_In32(ddr_pll_ctrl);
//printf("ddr_pll_ctrl = %x\n\r", (unsigned int) ddrc);
ddrc &= ~0x00000010; //enable standby mode and dynamic clock gating
Xil_Out32(ddr_pll_ctrl,ddrc);
ddrc = Xil_In32(ddr_pll_ctrl);
//printf("ddr_pll_ctrl = %x\n\r", (unsigned int) ddrc);
ddrc = Xil_In32(io_pll_ctrl);
ddrc &= ~0x00000010; //enable standby mode and dynamic clock gating
Xil_Out32(io_pll_ctrl,ddrc);
ddrc = Xil_In32(io_pll_ctrl);
XGpioPs_IntrDisablePin(&Gpio, pbsw);
//printf("****button pressed****\n\r");
//usleep(1000000);
XGpioPs_IntrClear(&Gpio, 1,0xFFFFFFFF); //clear interrupts after delay to remove bounce
XGpioPs_IntrEnablePin(&Gpio, pbsw);
}