forked from bristlemouth/bm_protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_gpio.c
More file actions
164 lines (136 loc) · 4.34 KB
/
debug_gpio.c
File metadata and controls
164 lines (136 loc) · 4.34 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* FreeRTOS includes. */
#include "FreeRTOS.h"
#include "task.h"
/* FreeRTOS+CLI includes. */
#include "FreeRTOS_CLI.h"
#include <string.h>
#include <inttypes.h>
#include "debug.h"
#include "bsp.h"
#include "gpio.h"
#include "debug_gpio.h"
#include "i2c.h"
#include "bsp.h"
// #include "resourceManager.h"
static BaseType_t debugGpioCommand( char *writeBuffer,
size_t writeBufferLen,
const char *commandString);
static const DebugGpio_t *pins;
static uint32_t ulTotalPins;
static const CLI_Command_Definition_t cmdGpio = {
// Command string
"gpio",
// Help string
"gpio:\n"
" * gpio list - show available pins\n"
" * gpio <set|clr|get> <pin name>\n",
// Command function
debugGpioCommand,
// Number of parameters (variable)
-1
};
void debugGpioInit(const DebugGpio_t *pinList, uint32_t numberOfPins) {
configASSERT(pinList != NULL);
configASSERT(numberOfPins > 0);
pins = pinList;
ulTotalPins = numberOfPins;
FreeRTOS_CLIRegisterCommand( &cmdGpio );
}
const DebugGpio_t * findGpio(const char *name, uint32_t nameLen) {
const DebugGpio_t *pin = NULL;
for(uint32_t index = 0; index < ulTotalPins; index++) {
if(strncmp(pins[index].name, name, nameLen) == 0) {
pin = &pins[index];
break;
}
}
return pin;
}
static void listGpios() {
for(uint32_t index = 0; index < ulTotalPins; index++) {
const DebugGpio_t *pin = &pins[index];
printf("[%s] %s\n", pin->mode ? "OUT" : "IN ", pin->name);
}
}
static void readGpio(const DebugGpio_t * pin) {
uint8_t value;
// TODO - check rval
IORead(pin->handle, &value);
printf("OK %" PRIi16 "\n", value);
}
static void writeGpio(const DebugGpio_t * pin, uint8_t value) {
#ifdef UPPER_DECK
if (pin->handle == &UD_LED_EN) {
resourceManagerResource_t *power5VResource = getPower5VResource();
if (value == 0) {
resourceManagerReleaseResource(power5VResource);
} else {
resourceManagerRequestResource(power5VResource);
}
}
#endif
IOWrite(pin->handle, value);
printf("OK\n");
}
static BaseType_t debugGpioCommand(char *writeBuffer,
size_t writeBufferLen,
const char *commandString) {
const char *parameter;
BaseType_t parameterStringLength;
( void ) writeBuffer;
( void ) writeBufferLen;
do {
parameter = FreeRTOS_CLIGetParameter(
commandString,
1, // Get the first parameter (command)
¶meterStringLength);
if(parameter == NULL) {
printf("ERR Invalid paramters\n");
break;
}
if (strcmp("list", parameter) == 0) {
listGpios();
} else if (strncmp("get", parameter, parameterStringLength) == 0) {
const char *name = FreeRTOS_CLIGetParameter(
commandString,
2, // Get the second parameter (name)
¶meterStringLength);
const DebugGpio_t * pin = findGpio(name, parameterStringLength);
if (pin != NULL) {
readGpio(pin);
} else {
printf("ERR Invalid pin name.\n");
}
} else if (strncmp("set", parameter, parameterStringLength) == 0) {
const char *name = FreeRTOS_CLIGetParameter(
commandString,
2, // Get the second parameter (name)
¶meterStringLength);
const DebugGpio_t * pin = findGpio(name, parameterStringLength);
if (pin == NULL) {
printf("ERR Invalid pin name.\n");
} else if (pin->mode == GPIO_IN) {
printf("ERR Cannot set input pin.\n");
} else {
writeGpio(pin, 1);
}
} else if (strncmp("clr", parameter, parameterStringLength) == 0) {
const char *name = FreeRTOS_CLIGetParameter(
commandString,
2, // Get the second parameter (name)
¶meterStringLength);
const DebugGpio_t * pin = findGpio(name, parameterStringLength);
if (pin == NULL) {
printf("ERR Invalid pin name.\n");
} else if (pin->mode == GPIO_IN) {
printf("ERR Cannot set input pin.\n");
} else {
writeGpio(pin, 0);
}
} else {
printf("ERR Invalid paramters\n");
break;
}
} while(0);
return pdFALSE;
}