Move kfile interface to the io/ directory.
[bertos.git] / bertos / cpu / cortex-m3 / drv / gpio_lm3s.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief LM3S1968 GPIO control interface.
34  *
35  * \author Andrea Righi <arighi@develer.com>
36  */
37
38 #include <cfg/compiler.h>
39 #include <cfg/debug.h>
40 #include <io/lm3s.h>
41 #include "gpio_lm3s.h"
42
43 /* Set the pin(s) direction and mode */
44 INLINE int lm3s_gpioPinConfigMode(uint32_t port, uint8_t pins, uint32_t mode)
45 {
46         if (mode == GPIO_DIR_MODE_IN)
47         {
48                 HWREG(port + GPIO_O_DIR)   &= ~pins;
49                 HWREG(port + GPIO_O_AFSEL) &= ~pins;
50         }
51         else if (mode == GPIO_DIR_MODE_OUT)
52         {
53                 HWREG(port + GPIO_O_DIR)   |= pins;
54                 HWREG(port + GPIO_O_AFSEL) &= ~pins;
55         }
56         else if (mode == GPIO_DIR_MODE_HW)
57         {
58                 HWREG(port + GPIO_O_DIR)   &= ~pins;
59                 HWREG(port + GPIO_O_AFSEL) |= pins;
60         }
61         else
62         {
63                 ASSERT(0);
64                 return -1;
65         }
66         return 0;
67 }
68
69 /* Set the pin(s) output strength */
70 INLINE int
71 lm3s_gpioPinConfigStrength(uint32_t port, uint8_t pins, uint32_t strength)
72 {
73         if (strength == GPIO_STRENGTH_2MA)
74         {
75                 HWREG(port + GPIO_O_DR2R) |= pins;
76                 HWREG(port + GPIO_O_DR4R) &= ~pins;
77                 HWREG(port + GPIO_O_DR8R) &= ~pins;
78                 HWREG(port + GPIO_O_SLR)  &= ~pins;
79         }
80         else if (strength == GPIO_STRENGTH_4MA)
81         {
82                 HWREG(port + GPIO_O_DR2R) &= ~pins;
83                 HWREG(port + GPIO_O_DR4R) |= pins;
84                 HWREG(port + GPIO_O_DR8R) &= ~pins;
85                 HWREG(port + GPIO_O_SLR)  &= ~pins;
86         }
87         else if (strength == GPIO_STRENGTH_8MA)
88         {
89                 HWREG(port + GPIO_O_DR2R) &= ~pins;
90                 HWREG(port + GPIO_O_DR4R) &= ~pins;
91                 HWREG(port + GPIO_O_DR8R) |= pins;
92                 HWREG(port + GPIO_O_SLR)  &= ~pins;
93         }
94         else if (strength == GPIO_STRENGTH_8MA_SC)
95         {
96                 HWREG(port + GPIO_O_DR2R) &= ~pins;
97                 HWREG(port + GPIO_O_DR4R) &= ~pins;
98                 HWREG(port + GPIO_O_DR8R) |= pins;
99                 HWREG(port + GPIO_O_SLR)  |= pins;
100         }
101         else
102         {
103                 ASSERT(0);
104                 return -1;
105         }
106         return 0;
107 }
108
109 /* Set the pin(s) type */
110 INLINE int lm3s_gpioPinConfigType(uint32_t port, uint8_t pins, uint32_t type)
111 {
112         if (type == GPIO_PIN_TYPE_STD)
113         {
114                 HWREG(port + GPIO_O_ODR)   &= ~pins;
115                 HWREG(port + GPIO_O_PUR)   &= ~pins;
116                 HWREG(port + GPIO_O_PDR)   &= ~pins;
117                 HWREG(port + GPIO_O_DEN)   |= pins;
118                 HWREG(port + GPIO_O_AMSEL) &= ~pins;
119         }
120         else if (type == GPIO_PIN_TYPE_STD_WPU)
121         {
122                 HWREG(port + GPIO_O_ODR)   &= ~pins;
123                 HWREG(port + GPIO_O_PUR)   |= pins;
124                 HWREG(port + GPIO_O_PDR)   &= ~pins;
125                 HWREG(port + GPIO_O_DEN)   |= pins;
126                 HWREG(port + GPIO_O_AMSEL) &= ~pins;
127         }
128         else if (type == GPIO_PIN_TYPE_STD_WPD)
129         {
130                 HWREG(port + GPIO_O_ODR)   &= ~pins;
131                 HWREG(port + GPIO_O_PUR)   &= ~pins;
132                 HWREG(port + GPIO_O_PDR)   |= pins;
133                 HWREG(port + GPIO_O_DEN)   |= pins;
134                 HWREG(port + GPIO_O_AMSEL) &= ~pins;
135         }
136         else if (type == GPIO_PIN_TYPE_OD)
137         {
138                 HWREG(port + GPIO_O_ODR)   |= pins;
139                 HWREG(port + GPIO_O_PUR)   &= ~pins;
140                 HWREG(port + GPIO_O_PDR)   &= ~pins;
141                 HWREG(port + GPIO_O_DEN)   |= pins;
142                 HWREG(port + GPIO_O_AMSEL) &= ~pins;
143         }
144         else if (type == GPIO_PIN_TYPE_OD_WPU)
145         {
146                 HWREG(port + GPIO_O_ODR)   |= pins;
147                 HWREG(port + GPIO_O_PUR)   |= pins;
148                 HWREG(port + GPIO_O_PDR)   &= ~pins;
149                 HWREG(port + GPIO_O_DEN)   |= pins;
150                 HWREG(port + GPIO_O_AMSEL) &= ~pins;
151         }
152         else if (type == GPIO_PIN_TYPE_OD_WPD)
153         {
154                 HWREG(port + GPIO_O_ODR)   |= pins;
155                 HWREG(port + GPIO_O_PUR)   &= pins;
156                 HWREG(port + GPIO_O_PDR)   |= pins;
157                 HWREG(port + GPIO_O_DEN)   |= pins;
158                 HWREG(port + GPIO_O_AMSEL) &= ~pins;
159         }
160         else if (type == GPIO_PIN_TYPE_ANALOG)
161         {
162                 HWREG(port + GPIO_O_ODR)   &= ~pins;
163                 HWREG(port + GPIO_O_PUR)   &= ~pins;
164                 HWREG(port + GPIO_O_PDR)   &= ~pins;
165                 HWREG(port + GPIO_O_DEN)   &= ~pins;
166                 HWREG(port + GPIO_O_AMSEL) |= pins;
167         }
168         else
169         {
170                 ASSERT(0);
171                 return -1;
172         }
173         return 0;
174 }
175
176 /**
177  * Configure a GPIO pin
178  *
179  * \param port Base address of the GPIO port
180  * \param pins Bit-packed representation of the pin(s)
181  * \param mode Pin(s) configuration mode
182  * \param strength Output drive strength
183  * \param type Pin(s) type
184  *
185  * Return 0 on success, otherwise a negative value.
186  */
187 int lm3s_gpioPinConfig(uint32_t port, uint8_t pins,
188                 uint32_t mode, uint32_t strength, uint32_t type)
189 {
190         int ret;
191
192         ret = lm3s_gpioPinConfigMode(port, pins, mode);
193         if (UNLIKELY(ret < 0))
194                 return ret;
195         ret = lm3s_gpioPinConfigStrength(port, pins, strength);
196         if (UNLIKELY(ret < 0))
197                 return ret;
198         ret = lm3s_gpioPinConfigType(port, pins, type);
199         if (UNLIKELY(ret < 0))
200                 return ret;
201         return 0;
202 }