summaryrefslogtreecommitdiffstats
path: root/bsps/powerpc/mpc55xxevb/start/emios.c
blob: f83054199b5f7307d432dce4daf6078a9a43209e (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
110
111
112
113
114
115
116
117
118
/* SPDX-License-Identifier: BSD-2-Clause */

/**
 * @file
 *
 * @ingroup RTEMSBSPsPowerPCMPC55XX
 *
 * @brief Enhanced Modular Input Output Subsystem (eMIOS).
 */

/*
 * Copyright (C) 2009, 2011 embedded brains GmbH & Co. KG
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <mpc55xx/emios.h>

#ifdef MPC55XX_HAS_EMIOS

/**
 * @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;
}

#endif /* MPC55XX_HAS_EMIOS */