Skip to content

Commit d4f12e1

Browse files
RaduSabau1buha
authored andcommitted
drivers: platform: ftd2xx : Fix FTD2XX library GPIO driver.
If another pin from the same port is being used, the pins direction won't be remembered, therefore fix this issue by having a global variable for it. Also add global variable for values, since it encounters the same problem, especially when setting a new value for another pin, which can break the other pin values. Fix : 5a9583a ("drivers: platform: ftd2xx : Add GPIO support for FTD2XX platform:) Signed-off-by: Radu Sabau <radu.sabau@analog.com>
1 parent 189a880 commit d4f12e1

File tree

2 files changed

+34
-34
lines changed

2 files changed

+34
-34
lines changed

drivers/platform/ftd2xx/mpsse/ftd2xx_gpio.c

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
#include "libmpsse_i2c.h"
3939
#include "ftd2xx_platform.h"
4040

41+
static uint8_t ftd2xx_gpio_pins_dir[FTD2XX_MAX_PORT_NB] = { 0 };
42+
static uint8_t ftd2xx_gpio_pins_val[FTD2XX_MAX_PORT_NB] = { 0 };
43+
4144
/**
4245
* @brief Obtain the GPIO decriptor.
4346
* @param desc - The GPIO descriptor.
@@ -48,9 +51,9 @@ int32_t ftd2xx_gpio_get(struct no_os_gpio_desc **desc,
4851
const struct no_os_gpio_init_param *param)
4952
{
5053
struct ftd2xx_gpio_desc *extra_desc;
51-
struct ftd2xx_gpio_init *extra_init;
5254
struct no_os_gpio_desc *descriptor;
5355
FT_STATUS status;
56+
bool gpio_dir;
5457
int32_t ret;
5558

5659
if (!param || param->port >= FTD2XX_MAX_DEV_PER_CHIP
@@ -80,8 +83,6 @@ int32_t ftd2xx_gpio_get(struct no_os_gpio_desc **desc,
8083
extra_desc->ftHandle = ftHandle[param->port];
8184
}
8285

83-
extra_init = param->extra;
84-
extra_desc->pins_dir = extra_init->pins_dir;
8586
descriptor->extra = extra_desc;
8687
descriptor->number = param->number;
8788
descriptor->port = param->port;
@@ -149,16 +150,17 @@ int32_t ftd2xx_gpio_direction_input(struct no_os_gpio_desc *desc)
149150

150151
extra_desc = desc->extra;
151152

152-
dir = extra_desc->pins_dir;
153+
dir = ftd2xx_gpio_pins_dir[desc->port];
153154
dir &= ~NO_OS_BIT(desc->number);
154155
dir |= NO_OS_BIT(desc->number) & no_os_field_prep(NO_OS_BIT(desc->number), 0);
155-
status = FT_WriteGPIO(extra_desc->ftHandle, (UCHAR)dir, extra_desc->pins_val);
156+
status = FT_WriteGPIO(extra_desc->ftHandle, (UCHAR)dir,
157+
ftd2xx_gpio_pins_val[desc->port]);
156158
if (status != FT_OK) {
157159
ret = status;
158160
return ret;
159161
}
160162

161-
extra_desc->pins_dir = dir;
163+
ftd2xx_gpio_pins_dir[desc->port] = dir;
162164

163165
return 0;
164166
}
@@ -180,10 +182,10 @@ int32_t ftd2xx_gpio_direction_output(struct no_os_gpio_desc *desc,
180182
int32_t ret;
181183

182184
extra_desc = desc->extra;
183-
dir = extra_desc->pins_dir;
185+
dir = ftd2xx_gpio_pins_dir[desc->port];
184186
dir &= ~NO_OS_BIT(desc->number);
185-
dir |= NO_OS_BIT(desc->number) & no_os_field_prep(NO_OS_BIT(desc->number), 0);
186-
val = extra_desc->pins_val;
187+
dir |= NO_OS_BIT(desc->number) & no_os_field_prep(NO_OS_BIT(desc->number), 1);
188+
val = ftd2xx_gpio_pins_val[desc->port];
187189
val &= ~NO_OS_BIT(desc->number);
188190
val |= NO_OS_BIT(desc->number) & no_os_field_prep(NO_OS_BIT(desc->number),
189191
value);
@@ -193,8 +195,8 @@ int32_t ftd2xx_gpio_direction_output(struct no_os_gpio_desc *desc,
193195
return ret;
194196
}
195197

196-
extra_desc->pins_dir = dir;
197-
extra_desc->pins_val = val;
198+
ftd2xx_gpio_pins_dir[desc->port] = dir;
199+
ftd2xx_gpio_pins_val[desc->port] = val;
198200

199201
return 0;
200202
}
@@ -213,7 +215,8 @@ int32_t ftd2xx_gpio_get_direction(struct no_os_gpio_desc *desc,
213215
struct ftd2xx_gpio_desc *extra_desc;
214216
extra_desc = desc->extra;
215217

216-
*direction = no_os_field_get(NO_OS_BIT(desc->number), extra_desc->pins_dir);
218+
*direction = no_os_field_get(FTD2XX_GPIO_PIN(desc->number),
219+
ftd2xx_gpio_pins_dir[desc->port]);
217220

218221
return 0;
219222
}
@@ -230,19 +233,26 @@ int32_t ftd2xx_gpio_set_value(struct no_os_gpio_desc *desc, uint8_t value)
230233
{
231234
struct ftd2xx_gpio_desc *extra_desc;
232235
FT_STATUS status;
233-
uint8_t val = 0;
236+
uint8_t val;
234237
int32_t ret;
235238

239+
if (no_os_field_get(FTD2XX_GPIO_PIN(desc->number),
240+
ftd2xx_gpio_pins_dir[desc->port]) == 0)
241+
return -EINVAL;
242+
236243
extra_desc = desc->extra;
244+
245+
val = ftd2xx_gpio_pins_val[desc->port];
237246
val &= ~NO_OS_BIT(desc->number);
238247
val |= NO_OS_BIT(desc->number) & no_os_field_prep(NO_OS_BIT(desc->number),
239248
value);
240-
status = FT_WriteGPIO(extra_desc->ftHandle, extra_desc->pins_dir, val);
249+
status = FT_WriteGPIO(extra_desc->ftHandle, ftd2xx_gpio_pins_dir[desc->port],
250+
val);
241251
if (status != FT_OK) {
242252
ret = status;
243253
return ret;
244254
}
245-
extra_desc->pins_val = val;
255+
ftd2xx_gpio_pins_val[desc->port] = val;
246256

247257
return 0;
248258
}
@@ -263,19 +273,19 @@ int32_t ftd2xx_gpio_get_value(struct no_os_gpio_desc *desc, uint8_t *value)
263273
int32_t ret;
264274

265275
extra_desc = desc->extra;
266-
if (no_os_field_get(NO_OS_BIT(desc->number),
267-
extra_desc->pins_dir) == NO_OS_GPIO_IN) {
276+
if (no_os_field_get(FTD2XX_GPIO_PIN(desc->number),
277+
ftd2xx_gpio_pins_dir[desc->port]) == NO_OS_GPIO_IN) {
268278
status = FT_ReadGPIO(extra_desc->ftHandle, &val);
269279
if (status != FT_OK) {
270280
ret = status;
271281
return ret;
272282
}
273-
extra_desc->pins_val &= ~NO_OS_BIT(desc->number);
274-
extra_desc->pins_val |= NO_OS_BIT(desc->number) & no_os_field_prep(NO_OS_BIT(
275-
desc->number), no_os_field_get(NO_OS_BIT(desc->number), val));
283+
284+
*value = no_os_field_get(FTD2XX_GPIO_PIN(desc->number), val);
276285
}
277286

278-
*value = no_os_field_get(NO_OS_BIT(desc->number), extra_desc->pins_val);
287+
*value = no_os_field_get(FTD2XX_GPIO_PIN(desc->number),
288+
ftd2xx_gpio_pins_dir[desc->port]);
279289

280290
return 0;
281291
}

drivers/platform/ftd2xx/mpsse/ftd2xx_gpio.h

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,33 +38,23 @@
3838
#include "no_os_gpio.h"
3939
#include "ftd2xx.h"
4040

41+
#define FTD2XX_GPIO_PIN(x) NO_OS_BIT(x)
42+
4143
#define FTD2XX_MAX_PIN_NB 7
44+
#define FTD2XX_MAX_PORT_NB 4
4245

4346
/**
4447
* @brief ftd2xx platform specific gpio platform ops structure
4548
*/
4649
extern const struct no_os_gpio_platform_ops ftd2xx_gpio_ops;
4750

48-
/**
49-
* @struct ftd2xx_gpio_init_param
50-
* @brief Structure holding the initialization parameters for ftd2xx platform
51-
*/
52-
struct ftd2xx_gpio_init {
53-
/* Initial direction of the port pins at initialization time. */
54-
uint8_t pins_dir;
55-
};
56-
5751
/**
5852
* @struct stm32_gpio_desc
5953
* @brief stm32 platform specific gpio descriptor
6054
*/
6155
struct ftd2xx_gpio_desc {
6256
/** Specific device handle */
6357
FT_HANDLE *ftHandle;
64-
/** Pins direction. */
65-
uint8_t pins_dir;
66-
/** Pins value. */
67-
uint8_t pins_val;
6858
};
6959

7060
#endif /* FTD2XX_GPIO_H*/

0 commit comments

Comments
 (0)