# Copyright (c) 2024-2025 Henrik Brix Andersen <henrik@brixandersen.dk>
# SPDX-License-Identifier: Apache-2.0

menu "CANnectivity"

module = CANNECTIVITY
module-str = CANnectivity
source "subsys/logging/Kconfig.template.log_config"

configdefault CAN_FD_MODE
	default y

config CANNECTIVITY_BOOT_BANNER
	bool "CANnectivity boot banner"
	default y if BOOT_BANNER
	select PRINTK
	help
	  Print the CANnectivity firmware version during boot up.

config CANNECTIVITY_USB_MANUFACTURER
	string "USB device manufacturer string"
	default "CANnectivity"
	help
	  CANnectivity USB device manufacturer string.

config CANNECTIVITY_USB_PRODUCT
	string "USB device product string"
	default "CANnectivity USB to CAN adapter"
	help
	  CANnectivity USB device product string.

config CANNECTIVITY_USB_VID
	hex "USB device Vendor ID (VID)"
	default 0x1209
	help
	  CANnectivity USB device Vendor ID (VID).

config CANNECTIVITY_USB_PID
	hex "USB device Product ID (PID)"
	default 0xca01
	help
	  CANnectivity USB device Product ID (PID).

config CANNECTIVITY_USB_SELF_POWERED
	bool "USB device is self-powered"
	default y
	help
	  CANnectivity USB device is self-powered.

config CANNECTIVITY_USB_MAX_POWER
	int "USB device maximum power"
	default 125
	range 0 250
	help
	  CANnectivity USB maximum current draw in milliampere (mA) divided by 2.
	  A value of 125 results in a maximum current draw value of 250 mA.

config CANNECTIVITY_MAX_CHANNELS
	int "Maximum number of supported channels"
	default 1 if !$(dt_compat_enabled,cannectivity-channel)
	default $(dt_compat_enabled_num,cannectivity-channel) if $(dt_compat_enabled,cannectivity-channel)
	range 1 256
	help
	  Maximum number of supported Geschwister Schneider USB/CAN (gs_usb) channels. Each channel
	  represents one CAN controller.

	  The Linux kernel driver supports up to 3 channels. Other drivers may support just one
	  channel.

if USB_DEVICE_STACK

configdefault USB_DEVICE_MANUFACTURER
	default CANNECTIVITY_USB_MANUFACTURER

configdefault USB_DEVICE_PRODUCT
	default CANNECTIVITY_USB_PRODUCT

configdefault USB_DEVICE_VID
	default CANNECTIVITY_USB_VID

configdefault USB_DEVICE_PID
	default CANNECTIVITY_USB_PID

configdefault USB_SELF_POWERED
	default CANNECTIVITY_USB_SELF_POWERED

configdefault USB_MAX_POWER
	default CANNECTIVITY_USB_MAX_POWER

configdefault USB_DEVICE_BOS
	default y

configdefault USB_DEVICE_INITIALIZE_AT_BOOT
	default n

endif # USB_DEVICE_STACK

if USB_DEVICE_STACK_NEXT

configdefault USBD_GS_USB_MAX_CHANNELS
	default CANNECTIVITY_MAX_CHANNELS

endif # USB_DEVICE_STACK_NEXT

config CANNECTIVITY_LED
	bool "LED support"
	default y
	depends on $(dt_compat_any_has_prop,cannectivity-channel,state-led) || \
		   $(dt_compat_any_has_prop,cannectivity-channel,activity-leds) || \
		   ($(dt_alias_enabled,led0) && !$(dt_compat_enabled,cannectivity))
	select LED
	select SMF
	select SMF_ANCESTOR_SUPPORT
	select SMF_INITIAL_TRANSITION
	select POLL
	select USB_DEVICE_GS_USB_IDENTIFICATION if USB_DEVICE_GS_USB
	select USBD_GS_USB_IDENTIFICATION if USBD_GS_USB
	help
	  Enable support for CAN channel status/activity LEDs.

if CANNECTIVITY_LED

config CANNECTIVITY_LED_EVENT_MSGQ_SIZE
	int "LED event message queue size"
	default 10
	help
	  Number of per-channel LED events that can be queued for the finite-state machine.

endif # CANNECTIVITY_LED

config CANNECTIVITY_TIMESTAMP
	bool # hidden
	select USB_DEVICE_GS_USB_TIMESTAMP if USB_DEVICE_GS_USB
	select USBD_GS_USB_TIMESTAMP if USBD_GS_USB
	help
	  Enable support for hardware timestamps.

config CANNECTIVITY_TIMESTAMP_COUNTER
	bool "Hardware timestamp support (32-bit counter @ 1MHz)"
	default y
	depends on $(dt_nodelabel_has_prop,cannectivity,timestamp-counter)
	select COUNTER
	select CANNECTIVITY_TIMESTAMP
	help
	  Enable support for hardware timestamps. This requires a 32-bit counter instance running @
	  1MHz.

config CANNECTIVITY_TIMESTAMP_UPTIME
	bool "Hardware timestamp support (64-bit cycle counter)"
	default y
	depends on TIMER_HAS_64BIT_CYCLE_COUNTER
	depends on !CANNECTIVITY_TIMESTAMP_COUNTER
	select CANNECTIVITY_TIMESTAMP
	help
	  Enable support for hardware timestamps. This requires a 64-bit cycle counter.

config CANNECTIVITY_TERMINATION_GPIO
	bool "CAN bus termination resistor support"
	default y
	depends on $(dt_compat_any_has_prop,cannectivity-channel,termination-gpios)
	select USB_DEVICE_GS_USB_TERMINATION if USB_DEVICE_GS_USB
	select USBD_GS_USB_TERMINATION if USBD_GS_USB
	select GPIO
	help
	  Enable support for GPIO-controlled CAN bus termination resistors.

config CANNECTIVITY_TERMINATION_DEFAULT_ON
	bool "Enable CAN bus termination resistors on boot-up"
	default y
	depends on CANNECTIVITY_TERMINATION_GPIO
	help
	  Enable CAN bus termination resistors on boot-up.

menuconfig CANNECTIVITY_DFU_SUPPORT
	bool "Device Firmware Upgrade (DFU) support"
	default y if BOOTLOADER_MCUBOOT
	help
	  Enable Device Firmware Upgrade (DFU) support.

if CANNECTIVITY_DFU_SUPPORT

choice CANNECTIVITY_DFU_BACKEND
	bool "DFU backend"
	default CANNECTIVITY_DFU_BACKEND_NONE if !BOOTLOADER_MCUBOOT
	default CANNECTIVITY_DFU_BACKEND_MCUBOOT if BOOTLOADER_MCUBOOT && USB_DEVICE_STACK
	default CANNECTIVITY_DFU_BACKEND_MCUBOOT if BOOTLOADER_MCUBOOT && MCUBOOT_BOOTLOADER_MODE_SINGLE_APP
	default CANNECTIVITY_DFU_BACKEND_APP if BOOTLOADER_MCUBOOT && USB_DEVICE_STACK_NEXT

config CANNECTIVITY_DFU_BACKEND_NONE
	bool "No backend"
	help
	  Device Firmware Upgrade is handled by an on-chip ROM bootloader.

config CANNECTIVITY_DFU_BACKEND_MCUBOOT
	bool "MCUboot"
	depends on BOOTLOADER_MCUBOOT
	help
	  Device Firmware Upgrade is handled by rebooting into MCUboot, with the bootloader exposing
	  an USB DFU or serial recovery interface.

config CANNECTIVITY_DFU_BACKEND_APP
	bool "Application"
	depends on BOOTLOADER_MCUBOOT && !MCUBOOT_BOOTLOADER_MODE_SINGLE_APP && USB_DEVICE_STACK_NEXT
	help
	  Device Firmware Upgrade is handled by the CANnectivity firmware application. The upgrade
	  process is handled by MCUboot upon reboot.

endchoice

config CANNECTIVITY_DFU_REBOOT
	bool "Automatic reboot upon DFU download completion"
	depends on CANNECTIVITY_DFU_BACKEND_APP
	default y
	select REBOOT
	help
	  Automatically reboot device upon completion of a DFU download.

config CANNECTIVITY_DFU_REBOOT_DELAY
	int "Reboot delay"
	default 100
	range 0 60000
	depends on CANNECTIVITY_DFU_REBOOT
	help
	  Delay in milliseconds between completion of a DFU download and device reboot.

config CANNECTIVITY_USB_DFU_PRODUCT
	string "USB DFU device product string"
	default "CANnectivity USB to CAN adapter (DFU)"
	depends on CANNECTIVITY_DFU_BACKEND_APP
	help
	  CANnectivity USB device DFU product string.

config CANNECTIVITY_USB_DFU_VID
	hex "USB device DFU Vendor ID (VID)"
	default 0xffff if !CANNECTIVITY_DFU_BACKEND_APP
	default 0x1209 if CANNECTIVITY_DFU_BACKEND_APP
	help
	  CANnectivity USB device DFU Vendor ID (VID).

config CANNECTIVITY_USB_DFU_PID
	hex "USB device DFU Product ID (PID)"
	default 0xffff if !CANNECTIVITY_DFU_BACKEND_APP
	default 0xca02 if CANNECTIVITY_DFU_BACKEND_APP
	help
	  CANnectivity USB device DFU Product ID (PID).

if CANNECTIVITY_DFU_BACKEND_MCUBOOT || CANNECTIVITY_DFU_BACKEND_APP

config CANNECTIVITY_DFU_BUTTON
	bool "DFU button support"
	default y
	depends on $(dt_alias_enabled,mcuboot-button0)
	select GPIO
	select REBOOT if CANNECTIVITY_DFU_BACKEND_MCUBOOT
	help
	  Enable support for activating Device Firmware Upgrade (DFU) mode by holding the DFU button
	  (identified by the "mcuboot-button0" devicetree alias).

config CANNECTIVITY_DFU_BUTTON_HOLD_TIME
	int "DFU button hold time in seconds"
	depends on CANNECTIVITY_DFU_BUTTON
	range 1 60
	default 4
	help
	  Number of seconds the Device Firmware Upgrade (DFU) button must be held to activate DFU
	  mode.

config CANNECTIVITY_DFU_LED
	bool # hidden
	default y
	depends on $(dt_alias_enabled,mcuboot-led0)
	select LED
	help
	  Enable support for controlling the Device Firmware Upgrade (DFU) LED (identified by the
	  "mcuboot-led0" devicetree alias).

configdefault FLASH
	default y

configdefault STREAM_FLASH
	default y

configdefault FLASH_MAP
	default y

configdefault IMG_MANAGER
	default y

if CANNECTIVITY_DFU_BACKEND_APP

if ARM_MPU

configdefault MPU_ALLOW_FLASH_WRITE
	default y

endif # ARM_MPU

configdefault USBD_DFU
	default y

configdefault USBD_DFU_MANIFESTATION_TOLERANT
	default !CANNECTIVITY_DFU_REBOOT

configdefault USBD_DFU_FLASH
	default y

configdefault USBD_DFU_FLASH_SLOT0
	default n

configdefault USBD_DFU_FLASH_SLOT1
	default y

configdefault USBD_DFU_NUMOF_IMAGES
	default 1

configdefault IMG_ERASE_PROGRESSIVELY
	default y

endif # CANNECTIVITY_DFU_BACKEND_APP

endif # CANNECTIVITY_DFU_BACKEND_MCUBOOT || CANNECTIVITY_DFU_BACKEND_APP

config CANNECTIVITY_GENERATE_USB_DFU_IMAGE
	bool "Generate USB DFU image"
	select BUILD_OUTPUT_BIN
	default y
	help
	  Enabling this configuration allows automatic generation of an image with USB Device
	  Firmware Upgrade (DFU) suffix. This depends on the dfu-suffix utility from the dfu-util
	  software package.

config CANNECTIVITY_USB_DFU_SPEC_ID
	hex "USB DFU image Specification ID"
	default 0x0100
	depends on CANNECTIVITY_GENERATE_USB_DFU_IMAGE
	help
	  CANnectivity USB DFU image Specification ID.

endif # CANNECTIVITY_DFU_SUPPORT

endmenu

source "Kconfig.zephyr"
