1 /**
2 * @file xmc1_gpio.c
3 * @date 2019-02-19
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 * 2019-02-19:
49 * - Fix initialization of initial output level in case pin is an analog pin and configured as push pull or open drain
50 *
51 * @endcond
52 *
53 */
54
55 #include "xmc_gpio.h"
56
57 #if UC_FAMILY == XMC1
58
59 /*******************************************************************************
60 * MACROS
61 *******************************************************************************/
62
63 #define PORT_PHCR_Msk PORT0_PHCR0_PH0_Msk
64 #define PORT_PHCR_Size PORT0_PHCR0_PH0_Msk
65 #define PORT_HWSEL_Msk PORT0_HWSEL_HW0_Msk
66 #define XMC_GPIO_MODE_OE XMC_GPIO_MODE_OUTPUT_PUSH_PULL
67
68 /*******************************************************************************
69 * API IMPLEMENTATION
70 *******************************************************************************/
71
XMC_GPIO_Init(XMC_GPIO_PORT_t * const port,const uint8_t pin,const XMC_GPIO_CONFIG_t * const config)72 void XMC_GPIO_Init(XMC_GPIO_PORT_t *const port, const uint8_t pin, const XMC_GPIO_CONFIG_t *const config)
73 {
74 XMC_ASSERT("XMC_GPIO_Init: Invalid port", XMC_GPIO_CHECK_PORT(port));
75 XMC_ASSERT("XMC_GPIO_Init: Invalid mode", XMC_GPIO_IsModeValid(config->mode));
76 XMC_ASSERT("XMC_GPIO_Init: Invalid input hysteresis", XMC_GPIO_CHECK_INPUT_HYSTERESIS(config->input_hysteresis));
77
78 /* Switch to input */
79 port->IOCR[pin >> 2U] &= ~(uint32_t)((uint32_t)PORT_IOCR_PC_Msk << (PORT_IOCR_PC_Size * (pin & 0x3U)));
80
81 /* HW port control is disabled */
82 port->HWSEL &= ~(uint32_t)((uint32_t)PORT_HWSEL_Msk << ((uint32_t)pin << 1U));
83
84 /* Set input hysteresis */
85 port->PHCR[(uint32_t)pin >> 3U] &= ~(uint32_t)((uint32_t)PORT_PHCR_Msk << ((uint32_t)PORT_PHCR_Size * ((uint32_t)pin & 0x7U)));
86 port->PHCR[(uint32_t)pin >> 3U] |= (uint32_t)config->input_hysteresis << ((uint32_t)PORT_PHCR_Size * ((uint32_t)pin & 0x7U));
87
88 /* Enable digital input */
89 if (XMC_GPIO_CHECK_ANALOG_PORT(port))
90 {
91 port->PDISC &= ~(uint32_t)((uint32_t)0x1U << pin);
92 }
93
94 if ((config->mode & XMC_GPIO_MODE_OE) != 0)
95 {
96 /* If output is enabled */
97
98 /* Set output level */
99 port->OMR = (uint32_t)config->output_level << pin;
100 }
101
102 /* Set mode */
103 port->IOCR[pin >> 2U] |= (uint32_t)config->mode << (PORT_IOCR_PC_Size * (pin & 0x3U));
104 }
105
XMC_GPIO_SetInputHysteresis(XMC_GPIO_PORT_t * const port,const uint8_t pin,const XMC_GPIO_INPUT_HYSTERESIS_t hysteresis)106 void XMC_GPIO_SetInputHysteresis(XMC_GPIO_PORT_t *const port,
107 const uint8_t pin,
108 const XMC_GPIO_INPUT_HYSTERESIS_t hysteresis)
109 {
110 XMC_ASSERT("XMC_GPIO_SetInputHysteresis: Invalid port", XMC_GPIO_CHECK_PORT(port));
111 XMC_ASSERT("XMC_GPIO_SetInputHysteresis: Invalid input hysteresis", XMC_GPIO_CHECK_INPUT_HYSTERESIS(hysteresis));
112
113 port->PHCR[(uint32_t)pin >> 3U] &= ~(uint32_t)((uint32_t)PORT_PHCR_Msk << ((uint32_t)PORT_PHCR_Size * ((uint32_t)pin & 0x7U)));
114 port->PHCR[(uint32_t)pin >> 3U] |= (uint32_t)hysteresis << ((uint32_t)PORT_PHCR_Size * ((uint32_t)pin & 0x7U));
115 }
116
117 #endif /* UC_FAMILY == XMC1 */
118