xiaoxiang781216 commented on code in PR #18951: URL: https://github.com/apache/nuttx/pull/18951#discussion_r3410632805
########## arch/sim/src/sim/posix/sim_ftdi_gpiochip.c: ########## @@ -0,0 +1,345 @@ +/**************************************************************************** + * arch/sim/src/sim/posix/sim_ftdi_gpiochip.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <sys/types.h> +#include <sys/ioctl.h> + +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <syslog.h> +#include <unistd.h> +#include <errno.h> +#include <fcntl.h> +#include <linux/const.h> +#include <linux/ioctl.h> +#include <linux/types.h> +#include <linux/gpio.h> +#include <ftdi.h> + +#include "sim_gpiochip.h" +#include "sim_internal.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define gpioerr(fmt, ...) \ + syslog(LOG_ERR, "sim_ftdi_gpio: " fmt "\n", ##__VA_ARGS__) +#define gpioinfo(fmt, ...) \ + syslog(LOG_ERR, "sim_ftdi_gpio: " fmt "\n", ##__VA_ARGS__) + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: host_gpiochip_direction + * + * Description: + * Provide gpio pin direction config. + * + * Input Parameters: + * priv - A pointer to instance of Linux ft2232h_gpio. + * pin - The pin number. + * input - The direction of the pin. + * + * Returned Value: + * 0 for success, other for fail. + ****************************************************************************/ + +int host_gpiochip_direction(struct host_gpiochip_dev *priv, + uint8_t pin, bool input) Review Comment: align to ( ########## arch/sim/src/sim/sim_internal.h: ########## @@ -502,10 +502,15 @@ size_t sim_stack_check(void *alloc, size_t size); void sim_stack_color(void *stackbase, size_t nbytes); #endif -#ifdef CONFIG_SIM_GPIOCHIP +#ifdef CONFIG_SIM_GPIOCHIP_LINUX int sim_gpiochip_initialize(const char *filename); struct ioexpander_dev_s *sim_gpiochip_get_ioe(void); #endif +#ifdef CONFIG_SIM_GPIOCHIP_FTDI +int sim_gpiochip_initialize(uint8_t pins_dir); +struct ioexpander_dev_s *sim_gpiochip_get_ioe(void); Review Comment: move out of CONFIG_SIM_GPIOCHIP_FTDI and remove line 507 ########## arch/sim/src/sim/posix/sim_ftdi_gpiochip.h: ########## @@ -0,0 +1,57 @@ +/**************************************************************************** + * arch/sim/src/sim/posix/sim_ftdi_gpiochip.h + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __ARCH_SIM_SRC_SIM_FTDI_GPIOCHIP_H +#define __ARCH_SIM_SRC_SIM_FTDI_GPIOCHIP_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#ifdef __SIM__ +# include "config.h" +#endif + +#include <stdint.h> +#include <ftdi.h> + +#include "sim_gpiochip.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define GPIOCHIP_LINE_BASE 20 +#define GPIOCHIP_NPINS 8 + +/**************************************************************************** + * Type Definitions + ****************************************************************************/ + +/* Host GPIOCHIP device definition */ + +struct host_gpiochip_dev Review Comment: move all definition to arch/sim/src/sim/posix/sim_ftdi_gpiochip.c and remove arch/sim/src/sim/posix/sim_ftdi_gpiochip.h ########## arch/sim/src/sim/posix/sim_ftdi_gpiochip.c: ########## @@ -0,0 +1,345 @@ +/**************************************************************************** + * arch/sim/src/sim/posix/sim_ftdi_gpiochip.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <sys/types.h> +#include <sys/ioctl.h> + +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <syslog.h> +#include <unistd.h> +#include <errno.h> +#include <fcntl.h> +#include <linux/const.h> +#include <linux/ioctl.h> +#include <linux/types.h> +#include <linux/gpio.h> +#include <ftdi.h> + +#include "sim_gpiochip.h" +#include "sim_internal.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define gpioerr(fmt, ...) \ + syslog(LOG_ERR, "sim_ftdi_gpio: " fmt "\n", ##__VA_ARGS__) +#define gpioinfo(fmt, ...) \ + syslog(LOG_ERR, "sim_ftdi_gpio: " fmt "\n", ##__VA_ARGS__) + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: host_gpiochip_direction + * + * Description: + * Provide gpio pin direction config. + * + * Input Parameters: + * priv - A pointer to instance of Linux ft2232h_gpio. + * pin - The pin number. + * input - The direction of the pin. + * + * Returned Value: + * 0 for success, other for fail. + ****************************************************************************/ + +int host_gpiochip_direction(struct host_gpiochip_dev *priv, + uint8_t pin, bool input) +{ + struct ftdi_context *ftdi = priv->ftdi; + uint8_t dir = priv->port_dir; + + if (pin >= GPIOCHIP_NPINS) + { + gpioerr("ERROR: Invalid pin %d config\n", pin); + return -EINVAL; + } + + /* FTDI uses 1 as output and 0 as input, so invert input first */ + + input = !input; + dir &= ~(input << pin); + dir |= (input << pin); + + if (ftdi_set_bitmode(ftdi, dir, BITMODE_SYNCBB) < 0) + { + gpioerr("Failed to change pin %d direction\n", pin); + return -EIO; + } + + priv->port_dir = dir; + + return 0; +} + +/**************************************************************************** + * Name: host_gpiochip_irq_request + * + * Input Parameters: + * priv - A pointer to instance of Linux gpiochip. + * pin - The pin number. + * cfgset - The config set of the pin. + * + * Returned Value: + * 0 for success, other for fail. + ****************************************************************************/ + +int host_gpiochip_irq_request(struct host_gpiochip_dev *priv, uint8_t pin, + uint16_t cfg) +{ + return 0; +} + +/**************************************************************************** + * Name: host_gpiochip_writepin + * + * Description: + * Write ft2232h_gpio pin value. + * + * Input Parameters: + * priv - A pointer to instance of Linux ft2232h_gpio. + * pin - The pin number. + * value - The value write to the pin. + * + * Returned Value: + * 0 for success, other for fail. + ****************************************************************************/ + +int host_gpiochip_writepin(struct host_gpiochip_dev *priv, + uint8_t pin, bool value) +{ + struct ftdi_context *ftdi = priv->ftdi; + uint8_t pins = priv->port_value; + + if (pin >= GPIOCHIP_NPINS) + { + gpioerr("ERROR: Invalid pin %d config\n", pin); + return -EINVAL; + } + + pins &= ~(value << pin); + pins |= (value << pin); + ftdi_write_data(ftdi, &pins, 1); + priv->port_value = pins; + + return 0; +} + +/**************************************************************************** + * Name: host_gpiochip_readpin + * + * Description: + * Read ft2232h_gpio pin value. + * + * Input Parameters: + * priv - A pointer to instance of Linux ft2232h_gpio. + * pin - The pin number. + * value - The value write to the pin. + * + * Returned Value: + * 0 for success, other for fail. + ****************************************************************************/ + +int host_gpiochip_readpin(struct host_gpiochip_dev *priv, + uint8_t pin, bool *value) +{ + struct ftdi_context *ftdi = priv->ftdi; + uint8_t pins; + + if (pin >= GPIOCHIP_NPINS) + { + gpioerr("ERROR: Invalid pin %d config\n", pin); + return -EINVAL; + } + + ftdi_read_pins(ftdi, &pins); + priv->port_value = pins; + + *value = !!(pins & (1 << pin)); + + return 0; +} + +/**************************************************************************** + * Name: host_gpiochip_irq_active + * + * Description: + * register gpio for gpiochip device + * + * Input Parameters: + * priv - A pointer to instance of Linux gpiochip. + * pin - gpio pin of Linux gpiochip device. + * + * Returned Value: + * 0 for OK. + * + ****************************************************************************/ + +bool host_gpiochip_irq_active(struct host_gpiochip_dev *priv, uint8_t pin) +{ + return false; +} + +/**************************************************************************** + * Name: host_gpiochip_get_line + * + * Description: + * Get line info from ft2232h_gpio device + * + * Input Parameters: + * priv - A pointer to instance of Linux ft2232h_gpio. + * pin - gpio line of Linux ft2232h_gpio. + * input - A pointer to direction of gpioline. + * + * Returned Value: + * 0 for OK. + * + ****************************************************************************/ + +int host_gpiochip_get_line(struct host_gpiochip_dev *priv, + uint8_t pin, bool *input) Review Comment: ditto ########## arch/sim/src/sim/posix/sim_ftdi_gpiochip.h: ########## @@ -0,0 +1,57 @@ +/**************************************************************************** + * arch/sim/src/sim/posix/sim_ftdi_gpiochip.h + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __ARCH_SIM_SRC_SIM_FTDI_GPIOCHIP_H +#define __ARCH_SIM_SRC_SIM_FTDI_GPIOCHIP_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#ifdef __SIM__ +# include "config.h" +#endif + +#include <stdint.h> +#include <ftdi.h> + +#include "sim_gpiochip.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define GPIOCHIP_LINE_BASE 20 Review Comment: remove, not used in ftdi path ########## arch/sim/src/sim/posix/sim_ftdi_gpiochip.c: ########## @@ -0,0 +1,345 @@ +/**************************************************************************** + * arch/sim/src/sim/posix/sim_ftdi_gpiochip.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <sys/types.h> +#include <sys/ioctl.h> + +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <syslog.h> +#include <unistd.h> +#include <errno.h> +#include <fcntl.h> +#include <linux/const.h> +#include <linux/ioctl.h> +#include <linux/types.h> +#include <linux/gpio.h> +#include <ftdi.h> + +#include "sim_gpiochip.h" +#include "sim_internal.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define gpioerr(fmt, ...) \ + syslog(LOG_ERR, "sim_ftdi_gpio: " fmt "\n", ##__VA_ARGS__) +#define gpioinfo(fmt, ...) \ + syslog(LOG_ERR, "sim_ftdi_gpio: " fmt "\n", ##__VA_ARGS__) + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: host_gpiochip_direction + * + * Description: + * Provide gpio pin direction config. + * + * Input Parameters: + * priv - A pointer to instance of Linux ft2232h_gpio. + * pin - The pin number. + * input - The direction of the pin. + * + * Returned Value: + * 0 for success, other for fail. + ****************************************************************************/ + +int host_gpiochip_direction(struct host_gpiochip_dev *priv, + uint8_t pin, bool input) +{ + struct ftdi_context *ftdi = priv->ftdi; + uint8_t dir = priv->port_dir; + + if (pin >= GPIOCHIP_NPINS) + { + gpioerr("ERROR: Invalid pin %d config\n", pin); + return -EINVAL; + } + + /* FTDI uses 1 as output and 0 as input, so invert input first */ + + input = !input; + dir &= ~(input << pin); + dir |= (input << pin); + + if (ftdi_set_bitmode(ftdi, dir, BITMODE_SYNCBB) < 0) + { + gpioerr("Failed to change pin %d direction\n", pin); + return -EIO; + } + + priv->port_dir = dir; + + return 0; +} + +/**************************************************************************** + * Name: host_gpiochip_irq_request + * + * Input Parameters: + * priv - A pointer to instance of Linux gpiochip. + * pin - The pin number. + * cfgset - The config set of the pin. + * + * Returned Value: + * 0 for success, other for fail. + ****************************************************************************/ + +int host_gpiochip_irq_request(struct host_gpiochip_dev *priv, uint8_t pin, + uint16_t cfg) +{ + return 0; +} + +/**************************************************************************** + * Name: host_gpiochip_writepin + * + * Description: + * Write ft2232h_gpio pin value. + * + * Input Parameters: + * priv - A pointer to instance of Linux ft2232h_gpio. + * pin - The pin number. + * value - The value write to the pin. + * + * Returned Value: + * 0 for success, other for fail. + ****************************************************************************/ + +int host_gpiochip_writepin(struct host_gpiochip_dev *priv, + uint8_t pin, bool value) +{ + struct ftdi_context *ftdi = priv->ftdi; + uint8_t pins = priv->port_value; + + if (pin >= GPIOCHIP_NPINS) + { + gpioerr("ERROR: Invalid pin %d config\n", pin); + return -EINVAL; + } + + pins &= ~(value << pin); + pins |= (value << pin); + ftdi_write_data(ftdi, &pins, 1); + priv->port_value = pins; + + return 0; +} + +/**************************************************************************** + * Name: host_gpiochip_readpin + * + * Description: + * Read ft2232h_gpio pin value. + * + * Input Parameters: + * priv - A pointer to instance of Linux ft2232h_gpio. + * pin - The pin number. + * value - The value write to the pin. + * + * Returned Value: + * 0 for success, other for fail. + ****************************************************************************/ + +int host_gpiochip_readpin(struct host_gpiochip_dev *priv, + uint8_t pin, bool *value) Review Comment: ditto ########## arch/sim/src/sim/posix/sim_linux_gpiochip.h: ########## @@ -0,0 +1,55 @@ +/**************************************************************************** + * arch/sim/src/sim/posix/sim_linux_gpiochip.h + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __ARCH_SIM_SRC_SIM_LINUX_GPIOCHIP_H +#define __ARCH_SIM_SRC_SIM_LINUX_GPIOCHIP_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#ifdef __SIM__ +# include "config.h" +#endif + +#include <stdint.h> +#include <ftdi.h> + +#include "sim_gpiochip.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define GPIOCHIP_LINE_BASE 60 + +/**************************************************************************** + * Type Definitions + ****************************************************************************/ + +/* Host GPIOCHIP device definition */ + +struct host_gpiochip_dev Review Comment: move all definition to arch/sim/src/sim/posix/sim_linux_gpiochip.c and remove arch/sim/src/sim/posix/sim_linux_gpiochip.h ########## arch/sim/src/sim/posix/sim_ftdi_gpiochip.c: ########## @@ -0,0 +1,345 @@ +/**************************************************************************** + * arch/sim/src/sim/posix/sim_ftdi_gpiochip.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <sys/types.h> +#include <sys/ioctl.h> + +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <syslog.h> +#include <unistd.h> +#include <errno.h> +#include <fcntl.h> +#include <linux/const.h> +#include <linux/ioctl.h> +#include <linux/types.h> +#include <linux/gpio.h> +#include <ftdi.h> + +#include "sim_gpiochip.h" +#include "sim_internal.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define gpioerr(fmt, ...) \ + syslog(LOG_ERR, "sim_ftdi_gpio: " fmt "\n", ##__VA_ARGS__) +#define gpioinfo(fmt, ...) \ + syslog(LOG_ERR, "sim_ftdi_gpio: " fmt "\n", ##__VA_ARGS__) + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: host_gpiochip_direction + * + * Description: + * Provide gpio pin direction config. + * + * Input Parameters: + * priv - A pointer to instance of Linux ft2232h_gpio. + * pin - The pin number. + * input - The direction of the pin. + * + * Returned Value: + * 0 for success, other for fail. + ****************************************************************************/ + +int host_gpiochip_direction(struct host_gpiochip_dev *priv, + uint8_t pin, bool input) +{ + struct ftdi_context *ftdi = priv->ftdi; + uint8_t dir = priv->port_dir; + + if (pin >= GPIOCHIP_NPINS) + { + gpioerr("ERROR: Invalid pin %d config\n", pin); + return -EINVAL; + } + + /* FTDI uses 1 as output and 0 as input, so invert input first */ + + input = !input; + dir &= ~(input << pin); + dir |= (input << pin); + + if (ftdi_set_bitmode(ftdi, dir, BITMODE_SYNCBB) < 0) + { + gpioerr("Failed to change pin %d direction\n", pin); + return -EIO; + } + + priv->port_dir = dir; + + return 0; +} + +/**************************************************************************** + * Name: host_gpiochip_irq_request + * + * Input Parameters: + * priv - A pointer to instance of Linux gpiochip. + * pin - The pin number. + * cfgset - The config set of the pin. + * + * Returned Value: + * 0 for success, other for fail. + ****************************************************************************/ + +int host_gpiochip_irq_request(struct host_gpiochip_dev *priv, uint8_t pin, + uint16_t cfg) +{ + return 0; +} + +/**************************************************************************** + * Name: host_gpiochip_writepin + * + * Description: + * Write ft2232h_gpio pin value. + * + * Input Parameters: + * priv - A pointer to instance of Linux ft2232h_gpio. + * pin - The pin number. + * value - The value write to the pin. + * + * Returned Value: + * 0 for success, other for fail. + ****************************************************************************/ + +int host_gpiochip_writepin(struct host_gpiochip_dev *priv, + uint8_t pin, bool value) Review Comment: align too -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
