sdram.c 9.8 KB

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