1 /**
2 * @file xmc4_gpio.c
3 * @date 2017-09-15
4 *
5 * @cond
6 *********************************************************************************************************************
7 * XMClib v2.1.24 - XMC Peripheral Driver Library
8 *
9 * Copyright (c) 2015-2019, Infineon Technologies AG
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without modification,are permitted provided that the
13 * following conditions are met:
14 *
15 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
19 * disclaimer in the documentation and/or other materials provided with the distribution.
20 *
21 * Neither the name of the copyright holders nor the names of its contributors may be used to endorse or promote
22 * products derived from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY,OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * To improve the quality of the software, users are encouraged to share modifications, enhancements or bug fixes with
33 * Infineon Technologies AG dave@infineon.com).
34 *********************************************************************************************************************
35 *
36 * Change History
37 * --------------
38 *
39 * 2015-02-20:
40 * - Initial draft <br>
41 *
42 * 2015-06-20:
43 * - Removed version macros and declaration of GetDriverVersion API
44 *
45 * 2017-09-15:
46 * - Fix side effects on output level when programming a pin as input after the same pin in another port has been configured as output
47 *
48 * @endcond
49 *
50 */
51
52 #include "xmc_gpio.h"
53
54 #if UC_FAMILY == XMC4
55
56 /*******************************************************************************
57 * MACROS
58 *******************************************************************************/
59
60 #define PORT_PDR_Msk PORT0_PDR0_PD0_Msk
61 #define PORT_PDR_Size (4U)
62 #define PORT_HWSEL_Msk PORT0_HWSEL_HW0_Msk
63 #define XMC_GPIO_MODE_OE XMC_GPIO_MODE_OUTPUT_PUSH_PULL
64
65 /*******************************************************************************
66 * API IMPLEMENTATION
67 *******************************************************************************/
68
XMC_GPIO_Init(XMC_GPIO_PORT_t * const port,const uint8_t pin,const XMC_GPIO_CONFIG_t * const config)69 void XMC_GPIO_Init(XMC_GPIO_PORT_t *const port, const uint8_t pin, const XMC_GPIO_CONFIG_t *const config)
70 {
71 XMC_ASSERT("XMC_GPIO_Init: Invalid port", XMC_GPIO_CHECK_PORT(port));
72 XMC_ASSERT("XMC_GPIO_Init: Invalid mode", XMC_GPIO_IsModeValid(config->mode));
73
74 /* Switch to input */
75 port->IOCR[pin >> 2U] &= (uint32_t)~(PORT_IOCR_PC_Msk << (PORT_IOCR_PC_Size * (pin & 0x3U)));
76
77 /* HW port control is disabled */
78 port->HWSEL &= ~(uint32_t)((uint32_t)PORT_HWSEL_Msk << ((uint32_t)pin << 1U));
79
80
81 /* Enable digital input */
82 if (XMC_GPIO_CHECK_ANALOG_PORT(port))
83 {
84 port->PDISC &= ~(uint32_t)((uint32_t)0x1U << pin);
85 }
86 else
87 {
88 if ((config->mode & XMC_GPIO_MODE_OE) != 0)
89 {
90 /* If output is enabled */
91
92 /* Set output level */
93 port->OMR = (uint32_t)config->output_level << pin;
94
95 /* Set output driver strength */
96 port->PDR[pin >> 3U] &= (uint32_t)~(PORT_PDR_Msk << ((uint32_t)PORT_PDR_Size * ((uint32_t)pin & 0x7U)));
97 port->PDR[pin >> 3U] |= (uint32_t)config->output_strength << ((uint32_t)PORT_PDR_Size * ((uint32_t)pin & 0x7U));
98 }
99 }
100
101 /* Set mode */
102 port->IOCR[pin >> 2U] |= (uint32_t)config->mode << ((uint32_t)PORT_IOCR_PC_Size * ((uint32_t)pin & 0x3U));
103 }
104
XMC_GPIO_SetOutputStrength(XMC_GPIO_PORT_t * const port,const uint8_t pin,XMC_GPIO_OUTPUT_STRENGTH_t strength)105 void XMC_GPIO_SetOutputStrength(XMC_GPIO_PORT_t *const port, const uint8_t pin, XMC_GPIO_OUTPUT_STRENGTH_t strength)
106 {
107 XMC_ASSERT("XMC_GPIO_Init: Invalid port", XMC_GPIO_CHECK_OUTPUT_PORT(port));
108 XMC_ASSERT("XMC_GPIO_Init: Invalid output strength", XMC_GPIO_CHECK_OUTPUT_STRENGTH(strength));
109
110 port->PDR[pin >> 3U] &= (uint32_t)~((uint32_t)PORT_PDR_Msk << ((uint32_t)PORT_PDR_Size * ((uint32_t)pin & 0x7U)));
111 port->PDR[pin >> 3U] |= (uint32_t)strength << ((uint32_t)PORT_PDR_Size * ((uint32_t)pin & 0x7U));
112 }
113
114 #endif /* UC_FAMILY == XMC4 */
115