source: rtems/bsps/arm/lpc176x/start/io.c @ e945b049

5
Last change on this file since e945b049 was e945b049, checked in by Sebastian Huber <sebastian.huber@…>, on 04/25/18 at 08:43:38

bsp/lpc176x: Move source files to bsps

This patch is a part of the BSP source reorganization.

Update #3285.

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