forked from zackxue/ipc
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathapp_gpio.c
146 lines (131 loc) · 3.59 KB
/
app_gpio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "app_gpio.h"
#include "sdk/sdk_api.h"
#include "app_debug.h"
#define kAPP_GPIO_ENTRY_AVAIALBE (32)
typedef struct APP_GPIO_ENTRY {
char name[16];
uint32_t dir_addr32, dir_mask32, dir_in_val32, dir_out_val32;
uint32_t data_addr32, data_mask32;
}stAPP_GPIO_ENTRY, *lpAPP_GPIO_ENTRY;
typedef struct APP_GPIO {
stAPP_GPIO_ENTRY entry[kAPP_GPIO_ENTRY_AVAIALBE];
}stAPP_GPIO, *lpAPP_GPIO;
static stAPP_GPIO _app_gpio = {
};
static int app_gpio_lookup_byname(const char *name)
{
int i = 0;
for(i = 0; i < kAPP_GPIO_ENTRY_AVAIALBE; ++i){
lpAPP_GPIO_ENTRY const gpio_entry = &_app_gpio.entry[i];
if(0 == strcasecmp(name, gpio_entry->name)
&& strlen(name) == strlen(gpio_entry->name)){
return i;
}
}
return -1;
}
int APP_GPIO_add(const char *name, uint32_t conf_addr32, uint32_t conf_mask32, uint32_t conf_val32,
uint32_t dir_addr32, uint32_t dir_mask32, uint32_t dir_in_val32, uint32_t dir_out_val32,
uint32_t data_addr32, uint32_t data_mask32)
{
int id = 0;
if(NULL != name && strlen(name) > 0){
id = app_gpio_lookup_byname(name);
if(id >= 0){
// a existed name found
APP_TRACE("GPIO \"%s\" has existed!", name);
return -1;
}
// lookup a position
id = app_gpio_lookup_byname("");
if(id >= 0){
lpAPP_GPIO_ENTRY const gpio_entry = &_app_gpio.entry[id];
if(0 == conf_addr32 // ignore the configurate
|| (NULL != sdk_sys && 0 == sdk_sys->write_mask_reg(conf_addr32, conf_mask32, conf_val32))){
// add a new one
strcpy(gpio_entry->name, name);
gpio_entry->dir_addr32 = dir_addr32;
gpio_entry->dir_mask32 = dir_mask32;
gpio_entry->dir_in_val32 = dir_in_val32;
gpio_entry->dir_out_val32 = dir_out_val32;
gpio_entry->data_addr32 = data_addr32;
gpio_entry->data_mask32 = data_mask32;
return 0;
}
APP_TRACE("GPIO \"%s\" conf(%08x) failed", name, conf_addr32);
return -1;
}
}
APP_TRACE("GPIO too many");
return 1;
}
int APP_GPIO_del(const char *name)
{
int id = 0;
if(NULL != name && strlen(name) > 0){
id = app_gpio_lookup_byname(name);
if(id >= 0){
lpAPP_GPIO_ENTRY const gpio_entry = &_app_gpio.entry[id];
// delete this gpio entry
gpio_entry->name[0] = '\0';
return 0;
}
}
return -1;
}
int APP_GPIO_set_pin(const char *name, bool pin_high)
{
if(NULL != name && strlen(name) > 0){
int const id = app_gpio_lookup_byname(name);
lpAPP_GPIO_ENTRY const gpio_entry = &_app_gpio.entry[id];
if(NULL != sdk_sys){
// set direction
if(0 == sdk_sys->write_mask_reg(gpio_entry->dir_addr32, gpio_entry->dir_mask32, gpio_entry->dir_out_val32)){
// set data
if(0 == sdk_sys->write_mask_reg(gpio_entry->data_addr32, gpio_entry->data_mask32, pin_high ? 0xffffffff : 0)){
return 0;
}
}
}
}
return -1;
}
int APP_GPIO_get_pin(const char *name, bool *pin_high)
{
if(NULL != name && strlen(name) > 0){
int const id = app_gpio_lookup_byname(name);
lpAPP_GPIO_ENTRY const gpio_entry = &_app_gpio.entry[id];
if(NULL != sdk_sys){
// set direction
if(0 == sdk_sys->write_mask_reg(gpio_entry->dir_addr32, gpio_entry->dir_mask32, gpio_entry->dir_in_val32)){
// get data
uint32_t val32_r = 0;
if(0 == sdk_sys->read_mask_reg(gpio_entry->data_addr32, gpio_entry->data_mask32, &val32_r)){
if(pin_high){
*pin_high = val32_r ? true : false;
return 0;
}
}
}
}
}
return -1;
}
static void app_gpio_cleanup()
{
int i = 0;
for(i = 0; i < kAPP_GPIO_ENTRY_AVAIALBE; ++i){
lpAPP_GPIO_ENTRY const gpio_entry = &_app_gpio.entry[i];
gpio_entry->name[0] = '\0';
}
}
int APP_GPIO_init()
{
app_gpio_cleanup();
// FIXEME:
return 0;
}
void APP_GPIO_destroy()
{
app_gpio_cleanup();
}