1 //*****************************************************************************
2 //
3 // sdram.c - Example demonstrating how to configure the EPI bus in SDRAM
4 // mode.
5 //
6 // Copyright (c) 2010 Texas Instruments Incorporated. All rights reserved.
7 // Software License Agreement
8 //
9 // Texas Instruments (TI) is supplying this software for use solely and
10 // exclusively on TI's microcontroller products. The software is owned by
11 // TI and/or its suppliers, and is protected under applicable copyright
12 // laws. You may not combine this software with "viral" open-source
13 // software in order to form a larger program.
14 //
15 // THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
16 // NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
17 // NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 // A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
19 // CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
20 // DAMAGES, FOR ANY REASON WHATSOEVER.
21 //
22 // This is part of revision 5961 of the Stellaris Firmware Development Package.
23 //
24 //*****************************************************************************
25
26 #include <rthw.h>
27 #include <rtthread.h>
28 #include <board.h>
29
30 #include "inc/hw_memmap.h"
31 #include "inc/hw_types.h"
32 #include "inc/hw_epi.h"
33 #include "inc/hw_gpio.h"
34 #include "driverlib/epi.h"
35 #include "driverlib/gpio.h"
36 #include "driverlib/sysctl.h"
37
38 //*****************************************************************************
39 //
40 //! \addtogroup epi_examples_list
41 //! <h1>EPI SDRAM Mode (sdram)</h1>
42 //!
43 //! This example shows how to configure the EPI bus in SDRAM mode. This
44 //! example has been written to be compatible with the Texas Instruments 8MB
45 //! SDRAM expansion card for the DK-LM3S9B96.
46 //!
47 //! For the EPI SDRAM mode, the pinout is as follows:
48 //! Address11:0 - EPI0S11:0
49 //! Bank1:0 - EPI0S14:13
50 //! Data15:0 - EPI0S15:0
51 //! DQML - EPI0S16
52 //! DQMH - EPI0S17
53 //! /CAS - EPI0S18
54 //! /RAS - EPI0S19
55 //! /WE - EPI0S28
56 //! /CS - EPI0S29
57 //! SDCKE - EPI0S30
58 //! SDCLK - EPI0S31
59 //!
60 //! This example uses the following peripherals and I/O signals. You must
61 //! review these and change as needed for your own board:
62 //! - EPI0 peripheral
63 //! - GPIO Port C peripheral (for EPI0 pins)
64 //! - GPIO Port E peripheral (for EPI0 pins)
65 //! - GPIO Port F peripheral (for EPI0 pins)
66 //! - GPIO Port G peripheral (for EPI0 pins)
67 //! - GPIO Port H peripheral (for EPI0 pins)
68 //! - GPIO Port J peripheral (for EPI0 pins)
69 //! - EPI0S0 - PH3
70 //! - EPI0S1 - PH2
71 //! - EPI0S2 - PC4
72 //! - EPI0S3 - PC5
73 //! - EPI0S4 - PC6
74 //! - EPI0S5 - PC7
75 //! - EPI0S6 - PH0
76 //! - EPI0S7 - PH1
77 //! - EPI0S8 - PE0
78 //! - EPI0S9 - PE1
79 //! - EPI0S10 - PH4
80 //! - EPI0S11 - PH5
81 //! - EPI0S12 - PF4
82 //! - EPI0S13 - PG0
83 //! - EPI0S14 - PG1
84 //! - EPI0S15 - PF5
85 //! - EPI0S16 - PJ0
86 //! - EPI0S17 - PJ1
87 //! - EPI0S18 - PJ2
88 //! - EPI0S19 - PJ3
89 //! - EPI0S28 - PJ4
90 //! - EPI0S29 - PJ5
91 //! - EPI0S30 - PJ6
92 //! - EPI0S31 - PG7
93 //!
94 //! The following UART signals are configured only for displaying console
95 //! messages for this example. These are not required for operation of EPI0.
96 //! - UART0 peripheral
97 //! - GPIO Port A peripheral (for UART0 pins)
98 //! - UART0RX - PA0
99 //! - UART0TX - PA1
100 //!
101 //! This example uses the following interrupt handlers. To use this example
102 //! in your own application you must add these interrupt handlers to your
103 //! vector table.
104 //! - None.
105 //!
106 //
107 //*****************************************************************************
108
109 //*****************************************************************************
110 //
111 // Use the following to specify the GPIO pins used by the SDRAM EPI bus.
112 //
113 //*****************************************************************************
114 #define EPI_PORTC_PINS (GPIO_PIN_7 | GPIO_PIN_6 | GPIO_PIN_5 | GPIO_PIN_4)
115 #define EPI_PORTE_PINS (GPIO_PIN_1 | GPIO_PIN_0)
116 #define EPI_PORTF_PINS (GPIO_PIN_5 | GPIO_PIN_4)
117 #define EPI_PORTG_PINS (GPIO_PIN_7 | GPIO_PIN_1 | GPIO_PIN_0)
118 #define EPI_PORTH_PINS (GPIO_PIN_5 | GPIO_PIN_4 | GPIO_PIN_3 | GPIO_PIN_2 | \
119 GPIO_PIN_1 | GPIO_PIN_0)
120 #define EPI_PORTJ_PINS (GPIO_PIN_6 | GPIO_PIN_5 | GPIO_PIN_4 | GPIO_PIN_3 | \
121 GPIO_PIN_2 | GPIO_PIN_1 | GPIO_PIN_0)
122
123 //*****************************************************************************
124 //
125 // The starting and ending address for the 8MB SDRAM chip (4Meg x 16bits) on
126 // the SDRAM daughter board.
127 //
128 //*****************************************************************************
129 #define SDRAM_START_ADDRESS 0x000000
130 #define SDRAM_END_ADDRESS 0x3FFFFF
131
132 //*****************************************************************************
133 //
134
135 //*****************************************************************************
136 //
137 // Configure EPI0 in SDRAM mode. The EPI memory space is setup using an a
138 // simple C array. This example shows how to read and write to an SDRAM card
139 // using the EPI bus in SDRAM mode.
140 //
141 //*****************************************************************************
rt_hw_sdram_init(void)142 void rt_hw_sdram_init(void)
143 {
144 //
145 // The EPI0 peripheral must be enabled for use.
146 //
147 SysCtlPeripheralEnable(SYSCTL_PERIPH_EPI0);
148
149 //
150 // For this example EPI0 is used with multiple pins on PortC, E, F, G, H,
151 // and J. The actual port and pins used may be different on your part,
152 // consult the data sheet for more information.
153 // TODO: change this to whichever GPIO port you are using.
154 //
155 SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
156 SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
157 SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
158 SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG);
159 SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOH);
160 SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOJ);
161
162 //
163 // This step configures the internal pin muxes to set the EPI pins for use
164 // with EPI. This step is only required because the default function of
165 // these pins may not be to function in EPI mode. Please reference the
166 // datasheet for more information about pin muxing. Note that EPI0S27:20
167 // are not used for the EPI SDRAM implementation.
168 // TODO: change this to select the port/pin you are using.
169 //
170 GPIOPinConfigure(GPIO_PH3_EPI0S0);
171 GPIOPinConfigure(GPIO_PH2_EPI0S1);
172 GPIOPinConfigure(GPIO_PC4_EPI0S2);
173 GPIOPinConfigure(GPIO_PC5_EPI0S3);
174 GPIOPinConfigure(GPIO_PC6_EPI0S4);
175 GPIOPinConfigure(GPIO_PC7_EPI0S5);
176 GPIOPinConfigure(GPIO_PH0_EPI0S6);
177 GPIOPinConfigure(GPIO_PH1_EPI0S7);
178 GPIOPinConfigure(GPIO_PE0_EPI0S8);
179 GPIOPinConfigure(GPIO_PE1_EPI0S9);
180 GPIOPinConfigure(GPIO_PH4_EPI0S10);
181 GPIOPinConfigure(GPIO_PH5_EPI0S11);
182 GPIOPinConfigure(GPIO_PF4_EPI0S12);
183 GPIOPinConfigure(GPIO_PG0_EPI0S13);
184 GPIOPinConfigure(GPIO_PG1_EPI0S14);
185 GPIOPinConfigure(GPIO_PF5_EPI0S15);
186 GPIOPinConfigure(GPIO_PJ0_EPI0S16);
187 GPIOPinConfigure(GPIO_PJ1_EPI0S17);
188 GPIOPinConfigure(GPIO_PJ2_EPI0S18);
189 GPIOPinConfigure(GPIO_PJ3_EPI0S19);
190 GPIOPinConfigure(GPIO_PJ4_EPI0S28);
191 GPIOPinConfigure(GPIO_PJ5_EPI0S29);
192 GPIOPinConfigure(GPIO_PJ6_EPI0S30);
193 GPIOPinConfigure(GPIO_PG7_EPI0S31);
194
195 //
196 // Configure the GPIO pins for EPI mode. All the EPI pins require 8mA
197 // drive strength in push-pull operation. This step also gives control of
198 // pins to the EPI module.
199 // TODO: change this to select the port/pin you are using.
200 //
201 GPIOPinTypeEPI(GPIO_PORTC_BASE, EPI_PORTC_PINS);
202 GPIOPinTypeEPI(GPIO_PORTE_BASE, EPI_PORTE_PINS);
203 GPIOPinTypeEPI(GPIO_PORTF_BASE, EPI_PORTF_PINS);
204 GPIOPinTypeEPI(GPIO_PORTG_BASE, EPI_PORTG_PINS);
205 GPIOPinTypeEPI(GPIO_PORTH_BASE, EPI_PORTH_PINS);
206 GPIOPinTypeEPI(GPIO_PORTJ_BASE, EPI_PORTJ_PINS);
207
208 //
209 // Sets the clock divider for the EPI module. In this case set the
210 // divider to 0, making the EPIClock = SysClk.
211 //
212 EPIDividerSet(EPI0_BASE, 1);
213
214 //
215 // Sets the usage mode of the EPI module. For this example we will use
216 // the SDRAM mode to talk to the external 8MB SDRAM daughter card.
217 //
218 EPIModeSet(EPI0_BASE, EPI_MODE_SDRAM);
219
220 //
221 // Configure the SDRAM mode. We configure the SDRAM according to our core
222 // clock frequency, in this case we are in the 15 MHz < clk <= 30 MHz
223 // range (i.e 16Mhz crystal). We will use the normal (or full power)
224 // operating state which means we will not use the low power self-refresh
225 // state. Set the SDRAM size to 8MB (or 64Mb) with a refresh counter of
226 // 1024 clock ticks.
227 // TODO: change this to select the proper clock frequency and SDRAM
228 // refresh counter.
229 //
230 EPIConfigSDRAMSet(EPI0_BASE, EPI_SDRAM_CORE_FREQ_15_30 |
231 EPI_SDRAM_FULL_POWER | EPI_SDRAM_SIZE_64MBIT, 1024);
232
233 //
234 // Set the address map. The EPI0 is mapped from 0x60000000 to 0xCFFFFFFF.
235 // For this example, we will start from a base address of 0x60000000 with
236 // a size of 16MB. We use 16MB so we have the ability to access the
237 // entire 8MB SDRAM daughter card. Since there is no 8MB option, so we
238 // use the next closest one. If you attempt to access an address higher
239 // than 4Meg (since SDRAM mode uses 16-bit data, you have 4Meg of
240 // of addresses by 16-bits of data) a fault will not occur since we
241 // configured the EPI for 16MB addressability. In the case that you do
242 // access an address higher than 0x3FFFFF, the MSb of the address gets
243 // ignored.
244 //
245 EPIAddressMapSet(EPI0_BASE, EPI_ADDR_RAM_SIZE_16MB | EPI_ADDR_RAM_BASE_6);
246
247 //
248 // Wait for the SDRAM wake-up to complete by polling the SDRAM
249 // initialization sequence bit. This bit is true when the SDRAM interface
250 // is going through the initialization and false when the SDRAM interface
251 // it is not in a wake-up period.
252 //
253 while(HWREG(EPI0_BASE + EPI_O_STAT) & EPI_STAT_INITSEQ)
254 {
255 }
256 }
257