1 /** mbed Microcontroller Library
2   ******************************************************************************
3   * @file    gpio_api.c
4   * @author
5   * @version V1.0.0
6   * @date    2016-08-01
7   * @brief   This file provides mbed API for GPIO.
8   ******************************************************************************
9   * @attention
10   *
11   * This module is a confidential and proprietary property of RealTek and
12   * possession or use of this module requires written permission of RealTek.
13   *
14   * Copyright(c) 2016, Realtek Semiconductor Corporation. All rights reserved.
15   ******************************************************************************
16   */
17 #include "objects.h"
18 #include "pinmap.h"
19 
20 #include "gpio_api.h"
21 
22 /** @addtogroup AmebaD_Mbed_API
23   * @{
24   */
25 
26 /** @defgroup MBED_GPIO
27  *  @brief      MBED_GPIO driver modules.
28  *  @{
29  */
30 
31 /** @defgroup MBED_GPIO_Exported_Functions MBED_GPIO Exported Functions
32   * @{
33   */
34 
35 /**
36   * @brief  Set the given pin as GPIO.
37   * @param  pin: PinName according to pinmux spec.
38   * @retval : The given pin with GPIO function
39   */
gpio_set(PinName pin)40 uint32_t gpio_set(PinName pin)
41 {
42 	u32 ip_pin;
43 
44 	assert_param(pin != (PinName)NC);
45 	pin_function(pin, 0);
46 
47 	ip_pin = pin;
48 
49 	return ip_pin;
50 }
51 
52 /**
53   * @brief  Initializes the GPIO device, include mode/direction/pull control registers.
54   * @param  obj: gpio object define in application software.
55   * @param  pin: PinName according to pinmux spec.
56   * @retval none
57   */
gpio_init(gpio_t * obj,PinName pin)58 void gpio_init(gpio_t *obj, PinName pin)
59 {
60 	GPIO_InitTypeDef  GPIO_InitStruct;
61 
62 	if (pin == (PinName)NC)
63 		return;
64 
65 	obj->pin = pin;
66 
67 	GPIO_InitStruct.GPIO_Pin = obj->pin;
68 	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
69 	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
70 
71 	GPIO_Init(&GPIO_InitStruct);
72 }
73 
74 /**
75   * @brief  Set GPIO mode.
76   * @param  obj: gpio object define in application software.
77   * @param  mode: this parameter can be one of the following values:
78   *		@arg PullNone: HighZ, user can input high or low use this pin
79   *		@arg PullDown: pull down
80   *		@arg PullUp: pull up
81   * @retval none
82   */
gpio_mode(gpio_t * obj,PinMode mode)83 void gpio_mode(gpio_t *obj, PinMode mode)
84 {
85 	u32 GPIO_PuPd;
86 
87 	switch (mode) {
88 		case PullNone:/* No driver -> Input & High Impendance */
89 			GPIO_PuPd = GPIO_PuPd_NOPULL;
90 			//GPIO_Direction(obj->pin, GPIO_Mode_IN);
91 		break;
92 
93 		case PullDown:
94 			GPIO_PuPd = GPIO_PuPd_DOWN;
95 		break;
96 
97 		case PullUp:
98 			GPIO_PuPd = GPIO_PuPd_UP;
99 		break;
100 
101 		default:
102 			GPIO_PuPd = GPIO_PuPd_NOPULL;
103 		break;
104 	}
105 
106 	PAD_PullCtrl(obj->pin, GPIO_PuPd);
107 }
108 
109 /**
110   * @brief  Set GPIO direction.
111   * @param  obj: gpio object define in application software.
112   * @param  direction: this parameter can be one of the following values:
113   *		@arg PIN_INPUT: this pin is input
114   *		@arg PIN_OUTPUT: this pin is output
115   * @retval none
116   */
gpio_dir(gpio_t * obj,PinDirection direction)117 void gpio_dir(gpio_t *obj, PinDirection direction)
118 {
119 	assert_param(obj->pin != (PinName)NC);
120 
121 	if (direction == PIN_OUTPUT) {
122 		GPIO_Direction(obj->pin, GPIO_Mode_OUT);
123 	} else {
124 		GPIO_Direction(obj->pin, GPIO_Mode_IN);
125 	}
126 }
127 
128 /**
129   * @brief  Set GPIO direction.
130   * @param  obj: gpio object define in application software.
131   * @param  direction: this parameter can be one of the following values:
132   *		@arg PIN_INPUT: this pin is input
133   *		@arg PIN_OUTPUT: this pin is output
134   * @retval none
135   */
gpio_change_dir(gpio_t * obj,PinDirection direction)136 void gpio_change_dir(gpio_t *obj, PinDirection direction)
137 {
138 	gpio_dir(obj, direction);
139 }
140 
141 /**
142   * @brief  Sets value to the selected output port pin.
143   * @param  obj: gpio object define in application software.
144   * @param  value: specifies the value to be written to the selected pin
145   * 	This parameter can be one of the following values:
146   *		@arg 0: Pin state set to low
147   *		@arg 1: Pin state set to high
148   * @retval none
149   */
gpio_write(gpio_t * obj,int value)150 void gpio_write(gpio_t *obj, int value)
151 {
152 	assert_param(obj->pin != (PinName)NC);
153 
154 	GPIO_WriteBit(obj->pin, value);
155 }
156 
157 /**
158   * @brief  Sets value to the selected output port pin.
159   * @param  obj: gpio object define in application software.
160   * @param  value: specifies the value to be written to the selected pin
161   * 	This parameter can be one of the following values:
162   *		@arg 0: Pin state set to low
163   *		@arg 1: Pin state set to high
164   * @retval none
165   */
gpio_direct_write(gpio_t * obj,BOOL value)166 void gpio_direct_write(gpio_t *obj, BOOL value)
167 {
168 	gpio_write(obj, value);
169 }
170 
171 /**
172   * @brief  Reads the specified gpio port pin.
173   * @param  obj: gpio object define in application software.
174   * @retval state of the specified gpio port pin
175   *          - 1: pin state is high
176   *          - 0: pin state is low
177   */
gpio_read(gpio_t * obj)178 int gpio_read(gpio_t *obj)
179 {
180 	assert_param(obj->pin != (PinName)NC);
181 
182 	return GPIO_ReadDataBit(obj->pin);
183 }
184 
185 /**
186   * @brief  Sets pull type to the selected pin.
187   * @param  obj: gpio object define in application software.
188   * @param  pull_type: this parameter can be one of the following values:
189   *		@arg PullNone: HighZ, user can input high or low use this pin
190   *		@arg PullDown: pull down
191   *		@arg PullUp: pull up
192   * @retval none
193   */
gpio_pull_ctrl(gpio_t * obj,PinMode pull_type)194 void gpio_pull_ctrl(gpio_t *obj, PinMode pull_type)
195 {
196 	u32 GPIO_PuPd;
197 
198 	switch (pull_type) {
199 		case PullNone:/* No driver -> Input & High Impendance */
200 			GPIO_PuPd = GPIO_PuPd_NOPULL;
201 		break;
202 
203 		case PullDown:
204 			GPIO_PuPd = GPIO_PuPd_DOWN;
205 		break;
206 
207 		case PullUp:
208 			GPIO_PuPd = GPIO_PuPd_UP;
209 		break;
210 
211 		default:
212 			GPIO_PuPd = GPIO_PuPd_NOPULL;
213 		break;
214 	}
215 
216 	PAD_PullCtrl(obj->pin, GPIO_PuPd);
217 }
218 
219 /**
220   * @brief  Deinitializes the GPIO device, include mode/direction/pull control registers.
221   * @param  obj: gpio object define in application software.
222   * @retval none
223   */
gpio_deinit(gpio_t * obj)224 void gpio_deinit(gpio_t *obj)
225 {
226 	GPIO_DeInit(obj->pin);
227 }
228 /**
229   * @}
230   */
231 
232 /**
233   * @}
234   */
235 
236 /**
237   * @}
238   */
239 /******************* (C) COPYRIGHT 2016 Realtek Semiconductor *****END OF FILE****/
240