source: rtems/c/src/lib/libbsp/arm/lpc176x/startup/bspstarthooks.c @ 7b35a36

4.115
Last change on this file since 7b35a36 was 19260fb, checked in by Martin Boretto <martin.boretto@…>, on Jun 9, 2014 at 2:27:18 PM

bsp/lpc176x: New BSP

  • Property mode set to 100644
File size: 5.9 KB
Line 
1/**
2 * @file bspstarthooks.c
3 *
4 * @ingroup lpc176x
5 *
6 * @brief First configurations and initializations to the correct
7 *              functionality of the board.
8 */
9
10/*
11 * Copyright (c) 2014 Taller Technologies.
12 *
13 * @author  Boretto Martin    (martin.boretto@tallertechnologies.com)
14 * @author  Diaz Marcos (marcos.diaz@tallertechnologies.com)
15 * @author  Lenarduzzi Federico  (federico.lenarduzzi@tallertechnologies.com)
16 * @author  Daniel Chicco  (daniel.chicco@tallertechnologies.com)
17 *
18 * The license and distribution terms for this file may be
19 * found in the file LICENSE in this distribution or at
20 * http://www.rtems.com/license/LICENSE.
21 */
22
23#include <bsp.h>
24#include <bsp/start.h>
25#include <bsp/io.h>
26
27/**
28 * @brief Initializes the oscillator according to the lpc176x board.
29 */
30static BSP_START_TEXT_SECTION void lpc176x_init_main_oscillator( void )
31{
32  if ( ( LPC176X_SCB.scs & LPC176X_SCB_SCS_OSC_STATUS ) == 0u ) {
33    LPC176X_SCB.scs |= LPC176X_SCB_SCS_OSC_ENABLE;
34
35    while ( ( LPC176X_SCB.scs & LPC176X_SCB_SCS_OSC_STATUS ) == 0u ) {
36      /* Wait. */
37    }
38  }
39
40  /* else implies that the oscillator is initialized. Also,
41     there is nothing to do. */
42}
43
44/**
45 * @brief Sets the PLL configuration.
46 *
47 * @param pll Value to set.
48 * @param val Set value.
49 */
50static BSP_START_TEXT_SECTION void lpc176x_pll_config( const uint32_t val )
51{
52  ( LPC176X_SCB.pll_0 ).con = val;
53  /* The two register writes must be in correct sequence. */
54  ( LPC176X_SCB.pll_0 ).feed = LPC176X_PLL0CON;
55  ( LPC176X_SCB.pll_0 ).feed = LPC176X_PLL0CFG;
56}
57
58/**
59 * @brief Sets the PLL.
60 *
61 * @param msel Multiplier value.
62 * @param psel Divider value.
63 * @param cclkdiv Divisor clock.
64 */
65static BSP_START_TEXT_SECTION void lpc176x_set_pll(
66  const unsigned msel,
67  const unsigned psel,
68  const unsigned cclkdiv
69)
70{
71  const uint32_t pllcfg = LPC176X_PLL_SEL_MSEL( msel ) |
72                          LPC176X_PLL_SEL_PSEL( psel );
73  const uint32_t pllstat = LPC176X_PLL_STAT_PLLE | LPC176X_PLL_STAT_PLOCK |
74                           pllcfg;
75  const uint32_t cclksel_cclkdiv = LPC176X_SCB_CCLKSEL_CCLKDIV( cclkdiv );
76
77  if ( ( LPC176X_SCB.pll_0 ).stat != pllstat
78       || LPC176X_SCB.cclksel != cclksel_cclkdiv
79       || LPC176X_SCB.clksrcsel != LPC176X_SCB_CLKSRCSEL_CLKSRC ) {
80    lpc176x_pll_config( ( LPC176X_SCB.pll_0 ).con & ~LPC176X_PLL_CON_PLLC );
81
82    /* Turn off USB.  */
83    LPC176X_SCB.usbclksel = 0u;
84
85    /* Disable PLL. */
86    lpc176x_pll_config( 0u );
87
88    /* Use SYSCLK for CCLK. */
89    LPC176X_SCB.cclksel = LPC176X_SCB_CCLKSEL_CCLKDIV( 0u );
90
91    /* Set the CCLK, PCLK and EMCCLK divider. */
92    LPC176X_SCB.cclksel = cclksel_cclkdiv;
93
94    /* Select main oscillator as clock source. */
95    LPC176X_SCB.clksrcsel = LPC176X_SCB_CLKSRCSEL_CLKSRC;
96
97    /* The two register writes must be in correct sequence. */
98    /* Set PLL configuration. */
99    ( LPC176X_SCB.pll_0 ).cfg = pllcfg;
100    ( LPC176X_SCB.pll_0 ).feed = LPC176X_PLL0CON;
101    ( LPC176X_SCB.pll_0 ).feed = LPC176X_PLL0CFG;
102
103    /* Enable PLL. */
104    lpc176x_pll_config( LPC176X_PLL_CON_PLLE );
105
106    /* Wait for lock. */
107    while ( ( ( LPC176X_SCB.pll_0 ).stat & LPC176X_PLL_STAT_PLOCK ) == 0u ) {
108      /* Wait */
109    }
110
111    /* Connect PLL. */
112    lpc176x_pll_config( ( LPC176X_PLL_CON_PLLE | LPC176X_PLL_CON_PLLC ) );
113
114    /* Wait for connected and enabled. */
115    while ( ( ( LPC176X_SCB.pll_0 ).stat & ( LPC176X_PLL_STAT_PLLE |
116                                             LPC176X_PLL_STAT_PLLC ) ) ==
117            0u ) {
118      /* Wait  */
119    }
120  }
121
122  /* else implies that the pll has a wrong value. Also,
123     there is nothing to do. */
124}
125
126/**
127 * @brief Pll initialization.
128 */
129static BSP_START_TEXT_SECTION void lpc176x_init_pll( void )
130{
131#if ( LPC176X_OSCILLATOR_MAIN == 12000000u )
132#if ( LPC176X_CCLK == 96000000U )
133  lpc176x_set_pll( 11u, 0u, 2u );
134#else
135#error "unexpected CCLK"
136#endif
137#else
138#error "unexpected main oscillator frequency"
139#endif
140}
141
142/**
143 * @brief Memory map initialization.
144 */
145static BSP_START_TEXT_SECTION void lpc176x_init_memory_map( void )
146{
147  LPC176X_SCB.memmap = LPC176X_SCB_MEMMAP_MAP;
148}
149
150/**
151 * @brief Memory accelerator initialization.
152 */
153static BSP_START_TEXT_SECTION void lpc176x_init_memory_accelerator( void )
154{
155#if ( LPC176X_CCLK <= 20000000U )
156  LPC176X_SCB.flashcfg = LPC176X_SCB_FLASHCFG_FLASHTIM( 0x0U );
157#elif ( LPC176X_CCLK <= 40000000U )
158  LPC176X_SCB.flashcfg = LPC176X_SCB_FLASHCFG_FLASHTIM( 0x1U );
159#elif ( LPC176X_CCLK <= 60000000U )
160  LPC176X_SCB.flashcfg = LPC176X_SCB_FLASHCFG_FLASHTIM( 0x2U );
161#elif ( LPC176X_CCLK <= 80000000U )
162  LPC176X_SCB.flashcfg = LPC176X_SCB_FLASHCFG_FLASHTIM( 0x3U );
163#elif ( LPC176X_CCLK <= 100000000U )
164  LPC176X_SCB.flashcfg = LPC176X_SCB_FLASHCFG_FLASHTIM( 0x4U );
165#else
166  LPC176X_SCB.flashcfg = LPC176X_SCB_FLASHCFG_FLASHTIM( 0x5U );
167#endif
168}
169
170/**
171 * @brief Stops the gpdma device.
172 */
173static BSP_START_TEXT_SECTION void lpc176x_stop_gpdma( void )
174{
175#ifdef LPC176X_STOP_GPDMA
176
177  bool has_power = ( LPC176X_SCB.pconp & LPC176X_SCB_PCONP_GPDMA ) != 0u;
178
179  if ( has_power ) {
180    GPDMA_CONFIG = 0u;
181    LPC176X_SCB.pconp &= ~LPC176X_SCB_PCONP_GPDMA;
182  }
183
184  /* else implies that the current module (gpdma) is turn off. Also,
185     there is nothing to do. */
186
187#endif
188}
189
190/**
191 * @brief Stops the usb device.
192 */
193static BSP_START_TEXT_SECTION void lpc176x_stop_usb( void )
194{
195#ifdef LPC176X_STOP_USB
196
197  bool has_power = ( LPC176X_SCB.pconp & LPC176X_SCB_PCONP_USB ) != 0u;
198
199  if ( has_power ) {
200    OTG_CLK_CTRL = 0u;
201
202    LPC176X_SCB.pconp &= ~LPC176X_SCB_PCONP_USB;
203    LPC176X_SCB.usbclksel = 0u;
204  }
205
206  /* else implies that the current module (usb) is turn off. Also,
207     there is nothing to do. */
208#endif
209}
210
211BSP_START_TEXT_SECTION void bsp_start_hook_0( void )
212{
213  lpc176x_init_main_oscillator();
214  lpc176x_init_pll();
215}
216
217BSP_START_TEXT_SECTION void bsp_start_hook_1( void )
218{
219  lpc176x_init_memory_map();
220  lpc176x_init_memory_accelerator();
221  lpc176x_stop_gpdma();
222  lpc176x_stop_usb();
223  bsp_start_copy_sections();
224  bsp_start_clear_bss();
225
226  /* At this point we can use objects outside the .start section  */
227}
Note: See TracBrowser for help on using the repository browser.