From 424b41c40e07e3011368a6f9fb47effdc8461aa6 Mon Sep 17 00:00:00 2001 From: Alexandre Bard Date: Mon, 8 Apr 2019 17:31:46 +0200 Subject: [PATCH] gnss-can support : First implementation --- board/nm/netbird_v2/Makefile | 2 +- board/nm/netbird_v2/board.c | 12 ++ board/nm/netbird_v2/shield.c | 2 +- board/nm/netbird_v2/shield_gnss_can.c | 222 ++++++++++++++++++++++++++ board/nm/netbird_v2/shield_gnss_can.h | 9 ++ 5 files changed, 245 insertions(+), 2 deletions(-) create mode 100644 board/nm/netbird_v2/shield_gnss_can.c create mode 100644 board/nm/netbird_v2/shield_gnss_can.h diff --git a/board/nm/netbird_v2/Makefile b/board/nm/netbird_v2/Makefile index c4b21a1d3a..6bafd37eec 100644 --- a/board/nm/netbird_v2/Makefile +++ b/board/nm/netbird_v2/Makefile @@ -10,4 +10,4 @@ ifeq ($(CONFIG_SKIP_LOWLEVEL_INIT),) obj-y := mux.o endif -obj-y += board.o ../common/bdparser.o ../common/board_descriptor.o shield.o shield_can.o shield_comio.o fileaccess.o +obj-y += board.o ../common/bdparser.o ../common/board_descriptor.o shield.o shield_can.o shield_comio.o shield_gnss_can.o fileaccess.o diff --git a/board/nm/netbird_v2/board.c b/board/nm/netbird_v2/board.c index 283b8bd151..cfcfab52dc 100644 --- a/board/nm/netbird_v2/board.c +++ b/board/nm/netbird_v2/board.c @@ -40,6 +40,7 @@ #include "shield.h" #include "shield_can.h" #include "shield_comio.h" +#include "shield_gnss_can.h" #include "fileaccess.h" DECLARE_GLOBAL_DATA_PTR; @@ -82,6 +83,7 @@ static BD_Context bdctx[3]; /* The descriptor context */ #define SHIELD_COM_IO 0 #define SHIELD_DUALCAN 1 +#define SHIELD_GNSS_CAN 2 static int _bd_init(void) { @@ -591,6 +593,16 @@ static struct shield_command known_shield_commands[] = { "fdt set $can1 status okay;", can_shield_init }, + { + SHIELD_GNSS_CAN, + "gnss-can", + "shield gnss-can gnss-mode uart can termination off", + "fdt get value serial0 /aliases serial0;" \ + "fdt set $serial0 status okay;" \ + "fdt get value can1 /aliases d_can1;" \ + "fdt set $can1 status okay;", + gnss_can_shield_init + }, }; static const struct shield_command* get_shield_command(int shield_id) diff --git a/board/nm/netbird_v2/shield.c b/board/nm/netbird_v2/shield.c index 97f1394a9d..5c39a682c3 100644 --- a/board/nm/netbird_v2/shield.c +++ b/board/nm/netbird_v2/shield.c @@ -65,7 +65,7 @@ static int do_shieldmode(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv } U_BOOT_CMD( - shield, 6, 1, do_shieldmode, + shield, 7, 1, do_shieldmode, "Set the shield mode", "dualcan termination [on|off] [on|off]\n" "shield comio mode [rs232|rs485] termination [on|off]\n" diff --git a/board/nm/netbird_v2/shield_gnss_can.c b/board/nm/netbird_v2/shield_gnss_can.c new file mode 100644 index 0000000000..7cfe5d152e --- /dev/null +++ b/board/nm/netbird_v2/shield_gnss_can.c @@ -0,0 +1,222 @@ +#define DEBUG + +#include +#include +#include + +#include "shield.h" +#include "board.h" + +#define NETBIRD_GPIO_RST_SHIELD_N GPIO_TO_PIN(0, 27) +#define NETBIRD_GPIO_LATCH GPIO_TO_PIN(0, 7) +#define NETBIRD_GPIO_MODE_1 GPIO_TO_PIN(1, 10) + +static int shield_slot_initialized = 0; + + +static struct module_pin_mux can_shield_netbird_pin_mux_config[] = { + /* Leave UART0 unconfigured because we want to configure it as needed by linux (can/spi/uart/etc) */ + {OFFSET(uart0_ctsn), (MODE(7) | PULLUDEN | PULLUP_EN)}, /* CAN1 tx */ + //{OFFSET(uart0_rxd), (MODE(7) | PULLUDEN | PULLUP_EN)}, /* CAN0 tx */ + {OFFSET(ecap0_in_pwm0_out), (MODE(7) | PULLUDEN | PULLUP_EN)}, /* Latch EN */ + {-1}, +}; + +static struct module_pin_mux can_shield_netbird_pin_mux_final[] = { + /* Leave UART0 unconfigured because we want to configure it as needed by linux (can/spi/uart/etc) */ + {OFFSET(uart0_ctsn), (MODE(2) | PULLUDEN | PULLUP_EN)}, /* E18 CAN1 tx */ + {OFFSET(uart0_rtsn), (MODE(2) | PULLUDDIS | RXACTIVE)}, /* E17 CAN1 rx */ + {OFFSET(uart0_rxd), (MODE(7) | PULLUDDIS)}, /* (E15) UART0_RXD */ + {OFFSET(uart0_txd), (MODE(7) | PULLUDEN | PULLUP_EN)}, /* (E16) UART0_TXD */ + //{OFFSET(uart0_txd), (MODE(2) | PULLUDDIS | RXACTIVE)}, /* CAN0 rx */ + //{OFFSET(uart0_rxd), (MODE(2) | PULLUDEN | PULLUP_EN)}, /* CAN0 tx */ + {-1}, +}; + +static struct module_pin_mux gnss_shield_netbird_pin_mux_uart[] = { + /* Leave UART0 unconfigured because we want to configure it as needed by linux (can/spi/uart/etc) */ + {OFFSET(uart0_rxd), (MODE(7) | PULLUDDIS)}, /* (E15) UART0_RXD */ + {OFFSET(uart0_txd), (MODE(7) | PULLUDEN | PULLUP_EN)}, /* (E16) UART0_TXD */ + {-1}, +}; + +static struct module_pin_mux gnss_shield_netbird_pin_mux_i2c[] = { + {OFFSET(uart0_rxd), (MODE(3) | PULLUDDIS)}, /* (E15) UART0_RXD - I2C2_SDA */ + {OFFSET(uart0_txd), (MODE(3) | PULLUDEN | PULLUP_EN)}, /* (E16) UART0_TXD - I2C_SCL */ + {-1}, +}; + +static int request_gpios(void) +{ + int ret; + + debug("Shield configure gpios\n"); + ret = shield_gpio_request_as_input(NETBIRD_GPIO_RST_SHIELD_N, "shield-rst"); + if ((ret < 0)) + return -1; + ret = shield_gpio_request_as_input(NETBIRD_GPIO_LATCH, "shield-load"); + if ((ret < 0)) + return -1; + /* + ret = shield_gpio_request_as_input(NETBIRD_GPIO_MODE_0, "shield-mode0"); + if ((ret < 0)) + return -1; + */ + ret = shield_gpio_request_as_input(NETBIRD_GPIO_MODE_1, "shield-mode1"); + if ((ret < 0)) + return -1; + + shield_slot_initialized = 1; + return 0; +} + +static int configure_can_termination(int termination) +{ + int ret; + + + debug ("Set can termination to %d\n", termination); + + if (!shield_slot_initialized) { + if (request_gpios()) { + puts("Failed to request gpios\n"); + return -1; + } + } + + debug("Configure shield pin muxing for configuration\n"); + configure_module_pin_mux(can_shield_netbird_pin_mux_config); + + debug("Make sure shield module is in reset\n"); + ret = gpio_direction_output(NETBIRD_GPIO_RST_SHIELD_N, 0); + if (ret < 0) { + puts("Can not set shield-rst as output\n"); + return -1; + } + udelay(10); + + debug("Set latch to high\n"); + ret = gpio_direction_output(NETBIRD_GPIO_LATCH, 1); + if (ret < 0) { + puts("Can not set shield-load as output\n"); + return -1; + } + udelay(10); + + debug("Write termination to GPIOs\n"); + /* + ret = gpio_direction_output(NETBIRD_GPIO_MODE_0, mode & 0x01); + if (ret < 0) { + puts("Can not set shield-mode0 as output\n"); + return -1; + } + */ + ret = gpio_direction_output(NETBIRD_GPIO_MODE_1, termination); + if (ret < 0) { + puts("Can not set shield-mode1 as output\n"); ///////////////// 1 + return -1; + } + udelay(10); + + debug("Set latch to low\n"); + gpio_set_value(NETBIRD_GPIO_LATCH, 0); + udelay(10); + + debug("Set mode0 and mode1 to highz again\n"); +/* ret = gpio_direction_input(NETBIRD_GPIO_MODE_0); + if ((ret < 0)) { + puts("Could not configure shield slot mode0 gpio as input\n"); + return -1; + } +*/ + ret = gpio_direction_input(NETBIRD_GPIO_MODE_1); + if ((ret < 0)) { + puts("Could not configure shield slot mode1 gpio as input\n"); + return -1; + } + udelay(10); + + debug("Take shield out of reset\n"); + gpio_set_value(NETBIRD_GPIO_RST_SHIELD_N, 1); + udelay(10); + + debug("Set final can shield muxing\n"); + configure_module_pin_mux(can_shield_netbird_pin_mux_final); + + return 0; + +} + +static int get_termination(const char* termination) +{ + if (strcmp("on", termination) == 0) { + return 1; + } + else if (strcmp("off", termination) == 0) { + return 0; + } + + debug ("Invalid termination mode %s (falling back to off)", termination); + return -1; +} + + +static int get_termination_from_args(char * const argv[], int argc) +{ + int termination; + + if(argc != 2){ + debug("2 arguments are needed for can configuration\n"); + return -1; + } + if (strcmp ("termination", argv[0])) { + debug("The only option for can is terminations\n"); + return -1; + } + + termination = get_termination(argv[1]); + if (termination < 0) { + return -1; + } + + /* Termination is inverse */ + return !termination; +} + +// shield gnss-can gnss-mode uart can termination off +static int set_shieldmode(char * const argv[], int argc) +{ + int ret = 0; + if (argc != 5) { + printf("Too few arguments for gnss-can\n"); + return -1; + } + + if (strcmp ("gnss-mode", argv[0])) { + printf("gnss can module want a gnss-mode\n"); + return -1; + } + ret = 0;//configure_gnss_mode(argv[1]); + if(ret != 0) return ret; + + + if (strcmp ("can", argv[2])) { + printf("gnss can module wants a gnss-mode\n"); + return -1; + } + printf("configure_can_termination"); + ret = configure_can_termination(get_termination_from_args(&argv[3], 2)); + if(ret != 0) return ret; + + return 0; +} + +struct shield_t gnss_can_shield = { + "gnss-can", set_shieldmode +}; + +void gnss_can_shield_init(void) +{ + shield_register(&gnss_can_shield); +} + diff --git a/board/nm/netbird_v2/shield_gnss_can.h b/board/nm/netbird_v2/shield_gnss_can.h new file mode 100644 index 0000000000..1cda70b6ea --- /dev/null +++ b/board/nm/netbird_v2/shield_gnss_can.h @@ -0,0 +1,9 @@ +#ifndef SHIELD_GNSS_CAN_H +#define SHIELD_GNSS_CAN_H + +int shield_gnss_can_init(void); +int shield_gnss_can_setmode(int mode); + +void gnss_can_shield_init(void); + +#endif // SHIELD_GNSS_CAN_H