summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libcpu/powerpc/mpc55xx/emios/emios.c
blob: 90cbcd3664bbf4817420576ab8949c06df363e50 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
 * @file
 *
 * @ingroup mpc55xx
 *
 * @brief Enhanced Modular Input Output Subsystem (eMIOS).
 */

/*
 * Copyright (c) 2009
 * embedded brains GmbH
 * Obere Lagerstr. 30
 * D-82178 Puchheim
 * Germany
 * <rtems@embedded-brains.de>
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rtems.com/license/LICENSE.
 */

#include <mpc55xx/regs.h>
#include <mpc55xx/emios.h>
#include <mpc55xx/mpc55xx.h>

#include <bsp/irq.h>
#include <bsp/utility.h>

#define RTEMS_STATUS_CHECKS_USE_PRINTK

#include <rtems/status-checks.h>

/**
 * @brief Initialize the eMIOS module.
 *
 * The module is enabled.  It is configured to use the internal clock.  The
 * global prescaler value is set to @a prescaler.  If the value is greater than
 * the maximum the maxium value will be used instead.  A prescaler value of
 * zero disables the clock.
 *
 * @note No protection against concurrent execution.
 */
void mpc55xx_emios_initialize( unsigned prescaler)
{
  union EMIOS_MCR_tag mcr = MPC55XX_ZERO_FLAGS;

  /* Enable module */
  mcr.B.MDIS = 0;

  /* Disable debug mode */
  mcr.B.FRZ = 1;

  /* Enable global time base */
  mcr.B.GTBE = 1;

  /* Disable global prescaler (= disable clock) */
  mcr.B.GPREN = 0;

  /* Set MCR */
  EMIOS.MCR.R = mcr.R;

  /* Set OUDR */
  EMIOS.OUDR.R = 0;

  /* Set global prescaler value */
  mpc55xx_emios_set_global_prescaler( prescaler);
}

/**
 * @brief Returns the global prescaler value of the eMIOS module.
 */
unsigned mpc55xx_emios_global_prescaler( void)
{
  union EMIOS_MCR_tag mcr = EMIOS.MCR;

  if (mcr.B.GPREN != 0) {
    return mcr.B.GPRE + 1;
  } else {
    return 0;
  }
}

/**
 * @brief Sets the global prescaler value of the eMIOS module.
 *
 * The global prescaler value is set to @a prescaler.  If the value is greater
 * than the maximum the maxium value will be used instead.  A prescaler value
 * of zero disables the clock.
 *
 * @note No protection against concurrent execution.
 */
void mpc55xx_emios_set_global_prescaler( unsigned prescaler)
{
  union EMIOS_MCR_tag mcr = EMIOS.MCR;

  /* Enable or disable the global prescaler */
  mcr.B.GPREN = prescaler > 0 ? 1 : 0;

  /* Set global prescaler value */
  if (prescaler > 256) {
    prescaler = 256;
  } else if (prescaler < 1) {
    prescaler = 1;
  }
  mcr.B.GPRE = prescaler - 1;

  /* Set MCR */
  EMIOS.MCR.R = mcr.R;
}