VSF Documented
Main Page
Related Pages
Topics
Namespaces
Data Structures
Files
File List
Globals
source
hal
utilities
io_mapper
vsf_io_mapper.h
Go to the documentation of this file.
1
/*****************************************************************************
2
* Copyright(C)2009-2022 by VSF Team *
3
* *
4
* Licensed under the Apache License, Version 2.0 (the "License"); *
5
* you may not use this file except in compliance with the License. *
6
* You may obtain a copy of the License at *
7
* *
8
* http://www.apache.org/licenses/LICENSE-2.0 *
9
* *
10
* Unless required by applicable law or agreed to in writing, software *
11
* distributed under the License is distributed on an "AS IS" BASIS, *
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13
* See the License for the specific language governing permissions and *
14
* limitations under the License. *
15
* *
16
****************************************************************************/
17
18
#ifndef __VSF_IO_MAPPER_H__
19
#define __VSF_IO_MAPPER_H__
20
21
/*============================ INCLUDES ======================================*/
22
23
#if VSF_HW_GPIO_COUNT > 0
24
25
#ifdef __cplusplus
26
extern
"C"
{
27
#endif
28
29
/*============================ MACROS ========================================*/
30
31
// default is 2^5 = 32-bit IO
32
#define VSF_HW_IO_MAPPER_PORT_BITS_LOG2 5
33
34
/*============================ MACROFIED FUNCTIONS ===========================*/
35
36
#define __VSF_IO_MAPPER_INIT(__PORT_NUM, __PORT_BITS_LOG2) \
37
.port_num = (__PORT_NUM), \
38
.port_bits_log2 = (__PORT_BITS_LOG2), \
39
.pin_mask = (1 << (__PORT_BITS_LOG2)) - 1,
40
#define VSF_IO_MAPPER_INIT(__PORT_NUM, __PORT_BITS_LOG2) \
41
__VSF_IO_MAPPER_INIT((__PORT_NUM), (__PORT_BITS_LOG2))
42
43
#define __vsf_io_mapper_type(__name) __name##_io_mapper_t
44
#define vsf_io_mapper_type(__name) __vsf_io_mapper_type(__name)
45
46
#define __define_io_mapper(__name, __port_num) \
47
typedef struct vsf_io_mapper_type(__name) { \
48
uint8_t port_num;
/* num of ports */
\
49
uint8_t port_bits_log2;
/* num of io bits per port in log2 */
\
50
uint8_t pin_mask; \
51
void * io[(__port_num)]; \
52
} vsf_io_mapper_type(__name);
53
#define define_io_mapper(__name, __port_num) __define_io_mapper(__name, __port_num)
54
#define def_io_mapper(__name, __port_num) define_io_mapper(__name, __port_num)
55
56
#define __describe_io_mapper(__name, __port_num, __port_bits_log2, ...) \
57
define_io_mapper(__name, __port_num) \
58
vsf_io_mapper_type(__name) __name = { \
59
VSF_IO_MAPPER_INIT(__port_num, __port_bits_log2) \
60
__io = { \
61
__VA_ARGS__ \
62
} \
63
};
64
#define describe_io_mapper(__name, __port_num, __port_bits_log2) \
65
__describe_io_mapper(__name, (__port_num), (__port_bits_log2))
66
67
68
#define vsf_io_mapper_pin(__io_mapper, __port_idx, __pin_idx) \
69
((__pin_idx) | ((__port_idx) << (__io_mapper)->port_bits_log2))
70
#define vsf_io_mapper_get_port(__io_mapper, __pin) \
71
((__pin) >> (__io_mapper)->port_bits_log2)
72
#define vsf_io_mapper_get_pin(__io_mapper, __pin) \
73
((__pin) & (__io_mapper)->pin_mask)
74
75
#define vsf_io_mapper_config(__io_mapper, __pin, __feature) ({ \
76
uint8_t VSF_MACRO_SAFE_NAME(port_idx) = vsf_io_mapper_get_port(__io_mapper, __pin);\
77
uint8_t VSF_MACRO_SAFE_NAME(pin_idx) = vsf_io_mapper_get_pin(__io_mapper, __pin);\
78
VSF_HAL_ASSERT(VSF_MACRO_SAFE_NAME(port_idx) < (__io_mapper)->port_num);\
79
vsf_gpio_cfg_t cfg = { \
80
.mode = (__feature), \
81
}; \
82
vsf_gpio_port_config_pins((__io_mapper)->io[VSF_MACRO_SAFE_NAME(port_idx)],\
83
1 << VSF_MACRO_SAFE_NAME(pin_idx), &cfg);\
84
})
85
#define vsf_io_mapper_set_direction(__io_mapper, __dir, __pin) ({ \
86
uint8_t VSF_MACRO_SAFE_NAME(port_idx) = vsf_io_mapper_get_port(__io_mapper, __pin);\
87
uint8_t VSF_MACRO_SAFE_NAME(pin_idx) = vsf_io_mapper_get_pin(__io_mapper, __pin);\
88
VSF_HAL_ASSERT(VSF_MACRO_SAFE_NAME(port_idx) < (__io_mapper)->port_num);\
89
vsf_gpio_set_direction((__dir) ? (1 << VSF_MACRO_SAFE_NAME(pin_idx)) : 0, 1 << VSF_MACRO_SAFE_NAME(pin_idx),\
90
(__io_mapper)->io[VSF_MACRO_SAFE_NAME(port_idx)]);\
91
})
92
#define vsf_io_mapper_get_direction(__io_mapper, __pin) ({ \
93
uint8_t VSF_MACRO_SAFE_NAME(port_idx) = vsf_io_mapper_get_port(__io_mapper, __pin);\
94
uint8_t VSF_MACRO_SAFE_NAME(pin_idx) = vsf_io_mapper_get_pin(__io_mapper, __pin);\
95
VSF_HAL_ASSERT(VSF_MACRO_SAFE_NAME(port_idx) < (__io_mapper)->port_num);\
96
vsf_gpio_get_direction((__io_mapper)->io[VSF_MACRO_SAFE_NAME(port_idx)], 1 << VSF_MACRO_SAFE_NAME(pin_idx));\
97
})
98
#define vsf_io_mapper_switch_direction(__io_mapper, __pin) ({ \
99
uint8_t VSF_MACRO_SAFE_NAME(port_idx) = vsf_io_mapper_get_port(__io_mapper, __pin);\
100
uint8_t VSF_MACRO_SAFE_NAME(pin_idx) = vsf_io_mapper_get_pin(__io_mapper, __pin);\
101
VSF_HAL_ASSERT(VSF_MACRO_SAFE_NAME(port_idx) < (__io_mapper)->port_num);\
102
vsf_gpio_switch_direction((__io_mapper)->io[VSF_MACRO_SAFE_NAME(port_idx)], 1 << VSF_MACRO_SAFE_NAME(pin_idx));\
103
})
104
#define vsf_io_mapper_set_input(__io_mapper, __pin) ({ \
105
uint8_t VSF_MACRO_SAFE_NAME(port_idx) = vsf_io_mapper_get_port(__io_mapper, __pin);\
106
uint8_t VSF_MACRO_SAFE_NAME(pin_idx) = vsf_io_mapper_get_pin(__io_mapper, __pin);\
107
VSF_HAL_ASSERT(VSF_MACRO_SAFE_NAME(port_idx) < (__io_mapper)->port_num);\
108
vsf_gpio_set_input((__io_mapper)->io[VSF_MACRO_SAFE_NAME(port_idx)], 1 << VSF_MACRO_SAFE_NAME(pin_idx));\
109
})
110
#define vsf_io_mapper_set_output(__io_mapper, __pin) ({ \
111
uint8_t VSF_MACRO_SAFE_NAME(port_idx) = vsf_io_mapper_get_port(__io_mapper, __pin);\
112
uint8_t VSF_MACRO_SAFE_NAME(pin_idx) = vsf_io_mapper_get_pin(__io_mapper, __pin);\
113
VSF_HAL_ASSERT(VSF_MACRO_SAFE_NAME(port_idx) < (__io_mapper)->port_num);\
114
vsf_gpio_set_output((__io_mapper)->io[VSF_MACRO_SAFE_NAME(port_idx)], 1 << VSF_MACRO_SAFE_NAME(pin_idx));\
115
})
116
#define vsf_io_mapper_read(__io_mapper, __pin) ({ \
117
uint8_t VSF_MACRO_SAFE_NAME(port_idx) = vsf_io_mapper_get_port(__io_mapper, __pin);\
118
uint8_t VSF_MACRO_SAFE_NAME(pin_idx) = vsf_io_mapper_get_pin(__io_mapper, __pin);\
119
VSF_HAL_ASSERT(VSF_MACRO_SAFE_NAME(port_idx) < (__io_mapper)->port_num);\
120
uint32_t pin_value = vsf_gpio_read((__io_mapper)->io[VSF_MACRO_SAFE_NAME(port_idx)]);\
121
((pin_value >> VSF_MACRO_SAFE_NAME(pin_idx)) & 1); \
122
})
123
#define vsf_io_mapper_set(__io_mapper, __pin) ({ \
124
uint8_t VSF_MACRO_SAFE_NAME(port_idx) = vsf_io_mapper_get_port(__io_mapper, __pin);\
125
uint8_t VSF_MACRO_SAFE_NAME(pin_idx) = vsf_io_mapper_get_pin(__io_mapper, __pin);\
126
VSF_HAL_ASSERT(VSF_MACRO_SAFE_NAME(port_idx) < (__io_mapper)->port_num);\
127
vsf_gpio_set((__io_mapper)->io[VSF_MACRO_SAFE_NAME(port_idx)], 1 << VSF_MACRO_SAFE_NAME(pin_idx));\
128
})
129
#define vsf_io_mapper_clear(__io_mapper, __pin) ({ \
130
uint8_t VSF_MACRO_SAFE_NAME(port_idx) = vsf_io_mapper_get_port(__io_mapper, __pin);\
131
uint8_t VSF_MACRO_SAFE_NAME(pin_idx) = vsf_io_mapper_get_pin(__io_mapper, __pin);\
132
VSF_HAL_ASSERT(VSF_MACRO_SAFE_NAME(port_idx) < (__io_mapper)->port_num);\
133
vsf_gpio_clear((__io_mapper)->io[VSF_MACRO_SAFE_NAME(port_idx)], 1 << VSF_MACRO_SAFE_NAME(pin_idx));\
134
})
135
#define vsf_io_mapper_toggle(__io_mapper, __pin) ({ \
136
uint8_t VSF_MACRO_SAFE_NAME(port_idx) = vsf_io_mapper_get_port(__io_mapper, __pin);\
137
uint8_t VSF_MACRO_SAFE_NAME(pin_idx) = vsf_io_mapper_get_pin(__io_mapper, __pin);\
138
VSF_HAL_ASSERT(VSF_MACRO_SAFE_NAME(port_idx) < (__io_mapper)->port_num);\
139
vsf_gpio_toggle((__io_mapper)->io[VSF_MACRO_SAFE_NAME(port_idx)], 1 << VSF_MACRO_SAFE_NAME(pin_idx));\
140
})
141
142
143
#define vsf_hw_io_mapper_pin(__port_idx, __pin_idx) \
144
((__pin_idx) | ((__port_idx) << VSF_HW_IO_MAPPER_PORT_BITS_LOG2))
145
146
/*============================ TYPES =========================================*/
147
148
typedef
struct
vsf_io_mapper_t {
149
uint8_t
port_num;
// num of ports
150
uint8_t
port_bits_log2;
// num of io bits per port in log2
151
uint8_t
pin_mask;
152
void
*io[0];
153
} vsf_io_mapper_t;
154
155
define_io_mapper(vsf_hw,
VSF_HW_GPIO_COUNT
)
156
157
/*============================ GLOBAL VARIABLES ==============================*/
158
159
extern
const
vsf_io_mapper_type(vsf_hw) vsf_hw_io_mapper;
160
161
/*============================ PROTOTYPES ====================================*/
162
163
164
165
#ifdef __cplusplus
166
}
167
#endif
168
169
#endif
// VSF_HW_GPIO_COUNT > 0
170
#endif
// __VSF_IO_MAPPER_H__
VSF_HW_GPIO_COUNT
#define VSF_HW_GPIO_COUNT
Definition
device.h:80
uint8_t
unsigned char uint8_t
Definition
lvgl.h:40
Generated by
1.9.8