HAL
HAL layer above libopencm3 library.
 All Files Functions Macros Groups Pages
pin_v0.h
Go to the documentation of this file.
1 /*
2  * This file is part of the HAL project, inline library above libopencm3.
3  *
4  * This library is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library. If not, see <http://www.gnu.org/licenses/>.
16  */
17 #ifndef HAL_PIN_STM32_V0_H_INCLUDED
18 #define HAL_PIN_STM32_V0_H_INCLUDED
19 
20 #if !defined(HAL_PIN_H_INCLUDED)
21 # error please do not include HAL library internals directly
22 #endif
23 
25 
26 BEGIN_DECLS
27 
28 /*****************************************************************************/
29 
30 INLINE uint32_t _pin_port(const uint32_t pin)
31 {
32  return pin & ~15;
33 }
34 
35 INLINE uint32_t _pin_pin(const uint32_t pin)
36 {
37  return 1 << (pin & 15);
38 }
39 
40 INLINE uint32_t _pin_pinno(const uint32_t pin)
41 {
42  return pin & 15;
43 }
44 
45 INLINE void _pin_setmode(uint32_t pin, const uint32_t mode)
46 {
47  const uint32_t pinid = (_pin_pinno(pin) < 8) ? (_pin_pin(pin)*4) : ((_pin_pin(pin)-8)*4);
48 
49  if (_pin_pinno(pin) < 8) {
50  GPIO_CRL(_pin_port(pin)) = (mode << pinid) |
51  (GPIO_CRL(_pin_port(pin)) & ~(0x0f << pinid));
52  }
53  else {
54  GPIO_CRH(_pin_port(pin)) = (mode << pinid) |
55  (GPIO_CRH(_pin_port(pin)) & ~(0x0f << pinid));
56  }
57 }
58 
59 INLINE void _pin_setspd(uint32_t pin, const uint32_t mode)
60 {
61  if (_pin_pinno(pin) < 8) {
62  const uint32_t bit = _pin_pinno(pin)*4;
63  GPIO_CRL(_pin_port(pin)) = (mode << bit) |
64  (GPIO_CRL(_pin_port(pin)) & ~(0x04 << bit));
65  }
66  else {
67  const uint32_t bit = _pin_pinno(pin)*4 - 8*4;
68  GPIO_CRH(_pin_port(pin)) = (mode << bit) |
69  (GPIO_CRH(_pin_port(pin)) & ~(0x04 << bit));
70  }
71 }
72 
73 /******************************************************************************/
74 
75 INLINE bool pin_get(const uint32_t pin)
76 {
77  return (GPIO_IDR(_pin_port(pin)) & _pin_pin(pin)) != 0;
78 }
79 
80 INLINE void pin_set(const uint32_t pin, bool val)
81 {
82  GPIO_BSRR(_pin_port(pin)) = (val) ? _pin_pin(pin) : (_pin_pin(pin) << 16);
83 }
84 
85 INLINE void pin_toggle(const uint32_t pin)
86 {
87  uint32_t val = GPIO_ODR(_pin_port(pin));
88  GPIO_BSRR(_pin_port(pin)) = ((val & _pin_pin(pin)) << 16) | (~val & _pin_pin(pin));
89 }
90 
91 /******************************************************************************/
92 /* these are valid only when pin is input */
93 
94 INLINE void pin_pull_disable(const uint32_t pin)
95 {
96  _pin_setmode(pin, GPIO_MODE_INPUT | (GPIO_CNF_INPUT_FLOAT << 2));
97 }
98 
99 INLINE void pin_pull_down(const uint32_t pin)
100 {
101  pin_set(pin, false);
102  _pin_setmode(pin, GPIO_MODE_INPUT | (GPIO_CNF_INPUT_PULL_UPDOWN << 2));
103 }
104 
105 INLINE void pin_pull_up(const uint32_t pin)
106 {
107  pin_set(pin, true);
108  _pin_setmode(pin, GPIO_MODE_INPUT | (GPIO_CNF_INPUT_PULL_UPDOWN << 2));
109 }
110 
111 /******************************************************************************/
112 
113 INLINE void pin_output_pushpull(const uint32_t pin, const bool initial_state)
114 {
115  _pin_setmode(pin, GPIO_MODE_OUTPUT_50_MHZ | (GPIO_CNF_OUTPUT_PUSHPULL << 2));
116 }
117 
118 INLINE void pin_output_opendrain(const uint32_t pin)
119 {
120  _pin_setmode(pin, GPIO_MODE_OUTPUT_50_MHZ | (GPIO_CNF_OUTPUT_OPENDRAIN << 2));
121 }
122 
123 INLINE void pin_af_pushpull(const uint32_t pin)
124 {
125  _pin_setmode(pin, GPIO_MODE_OUTPUT_50_MHZ | (GPIO_CNF_OUTPUT_ALTFN_PUSHPULL << 2));
126 }
127 
128 INLINE void pin_af_opendrain(const uint32_t pin)
129 {
130  _pin_setmode(pin, GPIO_MODE_OUTPUT_50_MHZ | (GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN << 2));
131 }
132 
133 INLINE void pin_input(const uint32_t pin)
134 {
135  _pin_setmode(pin, GPIO_MODE_INPUT | (GPIO_CNF_INPUT_FLOAT << 2));
136 }
137 
138 INLINE void pin_analog(const uint32_t pin)
139 {
140  _pin_setmode(pin, GPIO_MODE_INPUT | (GPIO_CNF_INPUT_ANALOG << 2));
141 }
142 
143 /******************************************************************************/
144 /* these are valid only when pin is output */
145 
146 INLINE void pin_speed_low(const uint32_t pin)
147 {
148  _pin_setspd(pin, GPIO_MODE_OUTPUT_2_MHZ);
149 }
150 
151 INLINE void pin_speed_medium(const uint32_t pin)
152 {
153  _pin_setspd(pin, GPIO_MODE_OUTPUT_10_MHZ);
154 }
155 
156 INLINE void pin_speed_fast(const uint32_t pin)
157 {
158  _pin_setspd(pin, GPIO_MODE_OUTPUT_50_MHZ);
159 }
160 
161 INLINE void pin_speed_high(const uint32_t pin)
162 {
163  _pin_setspd(pin, GPIO_MODE_OUTPUT_50_MHZ);
164 }
165 
166 /******************************************************************************/
167 
168 INLINE void pin_af_map(const uint32_t pin, const uint32_t af)
169 {
170  /* Makes no sense on this architecture */
171  (void)pin;
172  (void)af;
173 }
174 
175 END_DECLS
176 
177 
178 #endif /* HAL_PIN_STM32_V1_H_INCLUDED */