libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
syscon.c
Go to the documentation of this file.
1/** @defgroup syscon_file SYSCON peripheral API
2 * @ingroup peripheral_apis
3 * @brief SWM050 SYSCON API.
4 * LGPL License Terms @ref lgpl_license
5 * @author @htmlonly © @endhtmlonly 2019
6 * Icenowy Zheng <icenowy@aosc.io>
7 * @author @htmlonly &copy; @endhtmlonly 2019
8 * Caleb Szalacinski <contact@skiboy.net>
9 */
10/*
11 * This file is part of the libopencm3 project.
12 *
13 * Copyright (C) 2019 Icenowy Zheng <icenowy@aosc.io>
14 * Copyright (C) 2019 Caleb Szalacinski <contact@skiboy.net>
15 *
16 * This library is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU Lesser General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
20 *
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU Lesser General Public License for more details.
25 *
26 * You should have received a copy of the GNU Lesser General Public License
27 * along with this library. If not, see <http://www.gnu.org/licenses/>.
28 */
29/**@{*/
32
33/*---------------------------------------------------------------------------*/
34/** @brief Select the alternative function of a Group of Pins
35
36Select the alternative function of one or more pins of GPIO.
37
38@param[in] gpios Pin identifiers @ref gpio_pin_id
39 If multiple pins are to be changed, use bitwise OR '|' to separate
40 them.
41@param[in] af_en Whether alternative function is selected
42*/
43void syscon_sel_af(uint16_t gpios, bool af_en)
44{
45 uint32_t masked_reg32;
46
47 if (gpios & GPIO0) {
48 masked_reg32 = SYSCON_PORTA_SEL & (~0x3);
49 SYSCON_PORTA_SEL = masked_reg32 | (af_en ? 0x1 : 0x0);
50 }
51 if (gpios & GPIO1) {
52 masked_reg32 = SYSCON_PORTA_SEL & (~0xc);
53 SYSCON_PORTA_SEL = masked_reg32 | (af_en ? 0x4 : 0x0);
54 }
55 if (gpios & GPIO2) {
56 masked_reg32 = SYSCON_PORTA_SEL & (~0x30);
57 SYSCON_PORTA_SEL = masked_reg32 | (af_en ? 0x10 : 0x0);
58 }
59 if (gpios & GPIO7) {
60 masked_reg32 = SYSCON_PORTA_SEL & (~0xc000);
61 SYSCON_PORTA_SEL = masked_reg32 | (af_en ? 0x4000 : 0x0);
62 }
63}
64
65/*---------------------------------------------------------------------------*/
66/** @brief Enable the internal pull-up of a Group of Pins
67
68Enable or disable the internal pull-up of one or more pins of GPIO.
69
70@param[in] gpios Pin identifiers @ref gpio_pin_id
71 If multiple pins are to be changed, use bitwise OR '|' to separate
72 them.
73@param[in] en True to enable pull-up, false to disable.
74*/
75void syscon_pullup(uint16_t gpios, bool en)
76{
77 if (en) {
78 SYSCON_PORTA_PULLUP |= gpios;
79 } else {
80 SYSCON_PORTA_PULLUP &= ~gpios;
81 }
82}
83
84/*---------------------------------------------------------------------------*/
85/** @brief Enable the input function of a Group of Pins
86
87Enable or disable the input function of one or more pins of GPIO. Disabling
88the input function of pins decreases the power usage of the MCU.
89
90@param[in] gpios Pin identifiers @ref gpio_pin_id
91 If multiple pins are to be changed, use bitwise OR '|' to separate
92 them.
93@param[in] en True to enable input function.
94*/
95void syscon_input_enable(uint16_t gpios, bool en)
96{
97 if (en) {
98 SYSCON_PORTA_INEN &= ~gpios;
99 } else {
100 SYSCON_PORTA_INEN |= gpios;
101 }
102}
103
104/*---------------------------------------------------------------------------*/
105/** @brief Select the SWD function of GPIO 1/2
106
107Enable or disable the SWD debugging port at GPIO 1/2. When SWD debugging port
108is enabled, GPIO and AF of the SWD pins will be both unavailable.
109
110@param[in] en True to enable SWD.
111*/
112void syscon_sel_swd(bool en)
113{
114 SYSCON_SWD_SEL = en;
115}
116/**@}*/
#define GPIO0
Definition: gpio.h:37
#define GPIO7
Definition: gpio.h:44
#define GPIO2
Definition: gpio.h:39
#define GPIO1
Definition: gpio.h:38
void syscon_sel_af(uint16_t gpios, bool af_en)
Select the alternative function of a Group of Pins.
Definition: syscon.c:43
void syscon_pullup(uint16_t gpios, bool en)
Enable the internal pull-up of a Group of Pins.
Definition: syscon.c:75
void syscon_sel_swd(bool en)
Select the SWD function of GPIO 1/2.
Definition: syscon.c:112
void syscon_input_enable(uint16_t gpios, bool en)
Enable the input function of a Group of Pins.
Definition: syscon.c:95
#define SYSCON_SWD_SEL
SWD Enable register.
Definition: syscon.h:38
#define SYSCON_PORTA_SEL
Pin Alternate function selection register.
Definition: syscon.h:40
#define SYSCON_PORTA_PULLUP
Pin Pull up register.
Definition: syscon.h:42
#define SYSCON_PORTA_INEN
Pin Input enable register.
Definition: syscon.h:44