aboutsummaryrefslogtreecommitdiffstats
path: root/os/io/platforms/AT91SAM7/pal_lld.c
blob: 9545c976c84e6e8b48f149a0d71e2cfdf8a867b9 (plain)
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
119
120
121
122
123
124
125
/*
    ChibiOS/RT - Copyright (C) 2006-2007 Giovanni Di Sirio.

    This file is part of ChibiOS/RT.

    ChibiOS/RT is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    ChibiOS/RT is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * @file AT91SAM7/pal_lld.c
 * @brief AT91SAM7 PIO low level driver code
 * @addtogroup AT91SAM7_PAL
 * @{
 */

#include <ch.h>
#include <pal.h>

#include "board.h"

/**
 * @brief AT91SAM7 I/O ports configuration.
 * @details PIO registers initialization.
 *
 * @param[in] config the AT91SAM7 ports configuration
 */
void _pal_lld_init(const AT91SAM7PIOConfig *config) {

  unsigned int ports = (1 << AT91C_ID_PIOA);
#if defined(SAM7X128) || defined(SAM7X256) || defined(SAM7X512)
  ports |= (1 << AT91C_ID_PIOB);
#endif
  AT91C_BASE_PMC->PMC_PCER = ports;

  /*
   * PIOA setup.
   */
  AT91C_BASE_PIOA->PIO_PPUER  = config->P0Data.pusr;    /* Pull-up as spec.*/
  AT91C_BASE_PIOA->PIO_PPUDR  = ~config->P0Data.pusr;
  AT91C_BASE_PIOA->PIO_PER  = 0xFFFFFFFF;               /* PIO enabled.*/
  AT91C_BASE_PIOA->PIO_ODSR = config->P0Data.odsr;      /* Data as specified.*/
  AT91C_BASE_PIOA->PIO_OER  = config->P0Data.osr;       /* Dir. as specified.*/
  AT91C_BASE_PIOA->PIO_ODR  = ~config->P0Data.osr;
  AT91C_BASE_PIOA->PIO_IFDR = 0xFFFFFFFF;               /* Filter disabled.*/
  AT91C_BASE_PIOA->PIO_IDR  = 0xFFFFFFFF;               /* Int. disabled.*/
  AT91C_BASE_PIOA->PIO_MDDR = 0xFFFFFFFF;               /* Push Pull drive.*/
  AT91C_BASE_PIOA->PIO_ASR  = 0xFFFFFFFF;               /* Peripheral A.*/
  AT91C_BASE_PIOA->PIO_OWER = 0xFFFFFFFF;               /* Write enabled.*/

  /*
   * PIOB setup.
   */
#if defined(SAM7X128) || defined(SAM7X256) || defined(SAM7X512)
  AT91C_BASE_PIOB->PIO_PPUER  = config->P1Data.pusr;    /* Pull-up as spec.*/
  AT91C_BASE_PIOB->PIO_PPUDR  = ~config->P1Data.pusr;
  AT91C_BASE_PIOB->PIO_PER  = 0xFFFFFFFF;               /* PIO enabled.*/
  AT91C_BASE_PIOB->PIO_ODSR = config->P1Data.odsr;      /* Data as specified.*/
  AT91C_BASE_PIOB->PIO_OER  = config->P1Data.osr;       /* Dir. as specified.*/
  AT91C_BASE_PIOB->PIO_ODR  = ~config->P1Data.osr;
  AT91C_BASE_PIOB->PIO_IFDR = 0xFFFFFFFF;               /* Filter disabled.*/
  AT91C_BASE_PIOB->PIO_IDR  = 0xFFFFFFFF;               /* Int. disabled.*/
  AT91C_BASE_PIOB->PIO_MDDR = 0xFFFFFFFF;               /* Push Pull drive.*/
  AT91C_BASE_PIOB->PIO_ASR  = 0xFFFFFFFF;               /* Peripheral A.*/
  AT91C_BASE_PIOB->PIO_OWER = 0xFFFFFFFF;               /* Write enabled.*/
#endif
}

/**
 * @brief Pads mode setup.
 * @details This function programs a pads group belonging to the same port
 *          with the specified mode.
 *
 * @param[in] port the port identifier
 * @param[in] mask the group mask
 * @param[in] mode the mode
 *
 * @note This function is not meant to be invoked directly by the application
 *       code.
 * @note @p PAL_MODE_RESET is implemented as input with pull-up.
 * @note @p PAL_MODE_UNCONNECTED is implemented as push pull output with high
 *       state.
 * @note @p PAL_MODE_OUTPUT_OPENDRAIN also enables the pull-up resistor.
 */
void _pal_lld_setgroupmode(ioportid_t port,
                           ioportmask_t mask,
                           uint_fast8_t mode) {

  switch (mode & PAL_MODE_MASK) {
  case PAL_MODE_RESET:
  case PAL_MODE_INPUT_PULLUP:
    port->PIO_PPUER = mask;
    port->PIO_ODR = mask;
    break;
  case PAL_MODE_INPUT:
  case PAL_MODE_INPUT_ANALOG:
    port->PIO_PPUDR = mask;
    port->PIO_ODR = mask;
    break;
  case PAL_MODE_UNCONNECTED:
    port->PIO_SODR = mask;
    /* Falls in */
  case PAL_MODE_OUTPUT_PUSHPULL:
    port->PIO_PPUDR = mask;
    port->PIO_OER = mask;
    port->PIO_MDDR = mask;
    break;
  case PAL_MODE_OUTPUT_OPENDRAIN:
    port->PIO_PPUER = mask;
    port->PIO_OER = mask;
    port->PIO_MDER = mask;
  }
}

/** @} */