source: rtems/c/src/lib/libbsp/arm/lpc176x/misc/io.c @ 425fe40d

4.115
Last change on this file since 425fe40d was 19260fb, checked in by Martin Boretto <martin.boretto@…>, on 06/09/14 at 14:27:18

bsp/lpc176x: New BSP

  • Property mode set to 100644
File size: 9.8 KB
Line 
1/**
2 * @file io.c
3 *
4 * @ingroup lpc176x
5 *
6 * @brief Input/output module methods.
7 */
8
9/*
10 * Copyright (c) 2014 Taller Technologies.
11 *
12 * @author  Boretto Martin    (martin.boretto@tallertechnologies.com)
13 * @author  Diaz Marcos (marcos.diaz@tallertechnologies.com)
14 * @author  Lenarduzzi Federico  (federico.lenarduzzi@tallertechnologies.com)
15 * @author  Daniel Chicco  (daniel.chicco@tallertechnologies.com)
16 *
17 * The license and distribution terms for this file may be
18 * found in the file LICENSE in this distribution or at
19 * http://www.rtems.com/license/LICENSE.
20 */
21
22#include <rtems/status-checks.h>
23#include <bsp.h>
24#include <bsp/io.h>
25#include <bsp/start.h>
26#include <bsp/system-clocks.h>
27
28/**
29 * @brief Modules table according to the LPC176x
30 */
31static const lpc176x_module_entry lpc176x_module_table[] = {
32  LPC176X_MODULE_ENTRY( LPC176X_MODULE_WD, 0, 1, 0 ),
33  LPC176X_MODULE_ENTRY( LPC176X_MODULE_ADC, 1, 1, 12 ),
34  LPC176X_MODULE_ENTRY( LPC176X_MODULE_CAN_0, 1, 1, 13 ),
35  LPC176X_MODULE_ENTRY( LPC176X_MODULE_CAN_1, 1, 1, 14 ),
36  LPC176X_MODULE_ENTRY( LPC176X_MODULE_DAC, 0, 1, 11 ),
37  LPC176X_MODULE_ENTRY( LPC176X_MODULE_GPDMA, 1, 1, 29 ),
38  LPC176X_MODULE_ENTRY( LPC176X_MODULE_GPIO, 0, 1, 15 ),
39  LPC176X_MODULE_ENTRY( LPC176X_MODULE_I2S, 1, 1, 27 ),
40  LPC176X_MODULE_ENTRY( LPC176X_MODULE_MCI, 1, 1, 28 ),
41  LPC176X_MODULE_ENTRY( LPC176X_MODULE_MCPWM, 1, 1, 17 ),
42  LPC176X_MODULE_ENTRY( LPC176X_MODULE_PCB, 0, 1, 18 ),
43  LPC176X_MODULE_ENTRY( LPC176X_MODULE_PWM_0, 1, 1, 5 ),
44  LPC176X_MODULE_ENTRY( LPC176X_MODULE_PWM_1, 1, 1, 6 ),
45  LPC176X_MODULE_ENTRY( LPC176X_MODULE_QEI, 1, 1, 18 ),
46  LPC176X_MODULE_ENTRY( LPC176X_MODULE_RTC, 1, 1, 9 ),
47  LPC176X_MODULE_ENTRY( LPC176X_MODULE_SYSCON, 0, 1, 30 ),
48  LPC176X_MODULE_ENTRY( LPC176X_MODULE_TIMER_0, 1, 1, 1 ),
49  LPC176X_MODULE_ENTRY( LPC176X_MODULE_TIMER_1, 1, 1, 2 ),
50  LPC176X_MODULE_ENTRY( LPC176X_MODULE_TIMER_2, 1, 1, 22 ),
51  LPC176X_MODULE_ENTRY( LPC176X_MODULE_TIMER_3, 1, 1, 23 ),
52  LPC176X_MODULE_ENTRY( LPC176X_MODULE_UART_0, 1, 1, 3 ),
53  LPC176X_MODULE_ENTRY( LPC176X_MODULE_UART_1, 1, 1, 4 ),
54  LPC176X_MODULE_ENTRY( LPC176X_MODULE_UART_2, 1, 1, 24 ),
55  LPC176X_MODULE_ENTRY( LPC176X_MODULE_UART_3, 1, 1, 25 ),
56  LPC176X_MODULE_ENTRY( LPC176X_MODULE_USB, 1, 0, 31 )
57};
58
59inline void lpc176x_pin_select(
60  const uint32_t             pin,
61  const lpc176x_pin_function function
62)
63{
64  assert( pin <= LPC176X_IO_INDEX_MAX
65    && function < LPC176X_PIN_FUNCTION_COUNT );
66  const uint32_t           pin_selected = LPC176X_PIN_SELECT( pin );
67  volatile uint32_t *const pinsel = &LPC176X_PINSEL[ pin_selected ];
68  const uint32_t           shift = LPC176X_PIN_SELECT_SHIFT( pin );
69  *pinsel = SET_FIELD( *pinsel, function,
70    LPC176X_PIN_SELECT_MASK << shift, shift );
71}
72
73/**
74 * @brief Checks if the module has power.
75 *
76 * @param has_power Power.
77 * @param index Index to shift.
78 * @param turn_on Turn on/off the power.
79 * @param level Interrupts value.
80 */
81static rtems_status_code check_power(
82  const bool            has_power,
83  const unsigned        index,
84  const bool            turn_on,
85  rtems_interrupt_level level
86)
87{
88  rtems_status_code status_code = RTEMS_INVALID_NUMBER;
89
90  if ( index <= LPC176X_MODULE_BITS_COUNT ) {
91    if ( has_power ) {
92      rtems_interrupt_disable( level );
93
94      if ( turn_on ) {
95        LPC176X_SCB.pconp |= 1u << index;
96      } else {
97        LPC176X_SCB.pconp &= ~( 1u << index );
98      }
99
100      rtems_interrupt_enable( level );
101    }
102
103    /* else implies that the module has not power. Also,
104       there is nothing to do. */
105
106    status_code = RTEMS_SUCCESSFUL;
107  }
108
109  /* else implies an invalid index number. Also, the function
110     does not return successful. */
111
112  return status_code;
113}
114
115/**
116 * @brief Sets the correct value according to the specific peripheral clock.
117 *
118 * @param  is_first_pclksel Represents the first pclksel.
119 * @param  clock The clock to set for this module.
120 * @param  clock_shift Value to clock shift.
121 */
122static inline void set_pclksel_value(
123  const uint32_t             pclksel,
124  const lpc176x_module_clock clock,
125  const unsigned             clock_shift
126)
127{
128  assert( pclksel < LPC176X_SCB_PCLKSEL_COUNT );
129  const uint32_t setclock = ( clock << clock_shift );
130  const uint32_t mask = ~( LPC176X_MODULE_CLOCK_MASK << clock_shift );
131  LPC176X_SCB.pclksel[ pclksel ] = ( LPC176X_SCB.pclksel[ pclksel ] & mask ) |
132                                   setclock;
133}
134
135/**
136 * @brief Checks if the module has clock.
137 *
138 * @param has_clock Clock.
139 * @param index Index to shift.
140 * @param clock The clock to set for this module.
141 * @param level Interrupts value.
142 */
143static rtems_status_code check_clock(
144  const bool                 has_clock,
145  const unsigned             index,
146  const lpc176x_module_clock clock,
147  rtems_interrupt_level      level
148)
149{
150  rtems_status_code status_code = RTEMS_INVALID_NUMBER;
151
152  if ( index <= LPC176X_MODULE_BITS_COUNT ) {
153    if ( has_clock ) {
154      unsigned clock_shift = 2u * index;
155      rtems_interrupt_disable( level );
156
157      if ( clock_shift < LPC176X_MODULE_BITS_COUNT ) {
158        /* Sets the pclksel 0. */
159        set_pclksel_value( LPC176X_SCB_PCLKSEL0, clock, clock_shift );
160      } else {
161        /* Sets the pclksel 1. */
162        clock_shift -= LPC176X_MODULE_BITS_COUNT;
163        set_pclksel_value( LPC176X_SCB_PCLKSEL1, clock, clock_shift );
164      }
165
166      rtems_interrupt_enable( level );
167    }
168
169    /* else implies that the module has not clock. Also,
170       there is nothing to do. */
171
172    status_code = RTEMS_SUCCESSFUL;
173  }
174
175  /* else implies an invalid index number. Also, the function
176     does not return successful. */
177
178  return status_code;
179}
180
181/**
182 * @brief Checks the usb module.
183 *
184 * @return RTEMS_SUCCESFUL if the usb module is correct.
185 */
186static rtems_status_code check_usb_module( void )
187{
188  rtems_status_code status_code = RTEMS_INCORRECT_STATE;
189  const uint32_t    pllclk = lpc176x_pllclk();
190  const uint32_t    usbclk = LPC176X_USB_CLOCK;
191
192  if ( pllclk % usbclk == 0u ) {
193    const uint32_t usbdiv = pllclk / usbclk;
194
195    LPC176X_SCB.usbclksel = LPC176X_SCB_USBCLKSEL_USBDIV( usbdiv ) |
196                            LPC176X_SCB_USBCLKSEL_USBSEL( 1 );
197    status_code = RTEMS_SUCCESSFUL;
198  }
199
200  /* else implies that the module has an incorrect pllclk or usbclk value.
201      Also, there is nothing to do. */
202
203  return status_code;
204}
205
206/**
207 * @brief Enables the current module.
208 *
209 * @param  module  Current module to enable/disable.
210 * @param  clock The clock to set for this module.
211 * @param enable TRUE if the module is enable.
212 * @return RTEMS_SUCCESSFULL if the module was enabled successfully.
213 */
214static rtems_status_code enable_disable_module(
215  const lpc176x_module       module,
216  const lpc176x_module_clock clock,
217  const bool                 enable
218)
219{
220  rtems_status_code     status_code;
221  rtems_interrupt_level level = 0u;
222
223  const bool     has_power = lpc176x_module_table[ module ].power;
224  const bool     has_clock = lpc176x_module_table[ module ].clock;
225  const unsigned index = lpc176x_module_table[ module ].index;
226
227  assert( index <= LPC176X_MODULE_BITS_COUNT );
228
229  /* Enable or disable module */
230  if ( enable ) {
231    status_code = check_power( has_power, index, true, level );
232    RTEMS_CHECK_SC( status_code,
233      "Checking index shift to turn on power of the module." );
234
235    if ( module != LPC176X_MODULE_USB ) {
236      status_code = check_clock( has_clock, index, clock, level );
237      RTEMS_CHECK_SC( status_code,
238        "Checking index shift to set pclksel to the current module." );
239    } else {
240      status_code = check_usb_module();
241      RTEMS_CHECK_SC( status_code,
242        "Checking pll clock to set usb clock to the current module." );
243    }
244  } else {
245    status_code = check_power( has_power, index, false, level );
246    RTEMS_CHECK_SC( status_code,
247      "Checking index shift to turn off power of the module." );
248  }
249
250  return status_code;
251}
252
253/**
254 * @brief Enables the module power and clock.
255 *
256 * @param  module Device to enable.
257 * @param  clock  The clock to set for this module.
258 * @param  enable Enable or disable the module.
259 * @return RTEMS_SUCCESSFULL if the module was enabled succesfully.
260 */
261static rtems_status_code lpc176x_module_do_enable(
262  const lpc176x_module module,
263  lpc176x_module_clock clock,
264  const bool           enable
265)
266{
267  rtems_status_code status_code = RTEMS_SUCCESSFUL;
268
269  if ( (unsigned) module >= LPC176X_MODULE_COUNT ) {
270    return RTEMS_INVALID_ID;
271  }
272
273  /* else implies that the module has a correct value. Also,
274     there is nothing to do. */
275
276  if ( clock == LPC176X_MODULE_PCLK_DEFAULT ) {
277#if ( LPC176X_PCLKDIV == 1u )
278    clock = LPC176X_MODULE_CCLK;
279#elif ( LPC176X_PCLKDIV == 2u )
280    clock = LPC176X_MODULE_CCLK_2;
281#elif ( LPC176X_PCLKDIV == 4u )
282    clock = LPC176X_MODULE_CCLK_4;
283#elif ( LPC176X_PCLKDIV == 8u )
284    clock = LPC176X_MODULE_CCLK_8;
285#else
286#error "Unexpected clock divisor."
287#endif
288  }
289
290  /* else implies that the clock has a correct divisor. */
291
292  if ( ( clock & ~LPC176X_MODULE_CLOCK_MASK ) == 0u ) {
293    status_code = enable_disable_module( module, clock, enable );
294    RTEMS_CHECK_SC( status_code, "Checking the module to enable/disable." );
295  } else {
296    status_code = RTEMS_INVALID_CLOCK;
297  }
298
299  return status_code;
300}
301
302inline rtems_status_code lpc176x_module_enable(
303  const lpc176x_module module,
304  lpc176x_module_clock clock
305)
306{
307  return lpc176x_module_do_enable( module, clock, true );
308}
309
310inline rtems_status_code lpc176x_module_disable( const lpc176x_module module )
311{
312  return lpc176x_module_do_enable( module,
313    LPC176X_MODULE_PCLK_DEFAULT,
314    false );
315}
316
317bool lpc176x_module_is_enabled( const lpc176x_module module )
318{
319  assert( (unsigned) module < LPC176X_MODULE_COUNT );
320
321  const bool has_power = lpc176x_module_table[ module ].power;
322  bool       enabled;
323
324  if ( has_power ) {
325    const unsigned index = lpc176x_module_table[ module ].index;
326    const uint32_t pconp = LPC176X_SCB.pconp;
327
328    enabled = ( pconp & ( 1u << index ) ) != 0u;
329  } else {
330    enabled = true;
331  }
332
333  return enabled;
334}
Note: See TracBrowser for help on using the repository browser.