canbus: Move canbus uuid calculation to canbus.c

Move the uuid hash calculation to canbus.c and call canbus_set_uuid()
from src/stm32/chipid.c .  This simplifies the low-level canbus
hardware code.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2022-06-12 11:55:46 -04:00
parent 3f7d05dd18
commit fc7838855f
7 changed files with 21 additions and 25 deletions

View File

@ -56,16 +56,11 @@ config USB_VENDOR_ID
config USB_DEVICE_ID config USB_DEVICE_ID
default 0x614e default 0x614e
config USB_SERIAL_NUMBER_CHIPID config USB_SERIAL_NUMBER_CHIPID
depends on HAVE_CHIPID depends on HAVE_CHIPID && USBSERIAL
default y default y
config USB_SERIAL_NUMBER config USB_SERIAL_NUMBER
default "12345" default "12345"
# Generic configuration options for CANbus
config CANBUS_FREQUENCY
int "CAN bus speed" if LOW_LEVEL_OPTIONS && CANSERIAL
default 500000
menu "USB ids" menu "USB ids"
depends on USBSERIAL && LOW_LEVEL_OPTIONS depends on USBSERIAL && LOW_LEVEL_OPTIONS
config USB_VENDOR_ID config USB_VENDOR_ID
@ -78,6 +73,12 @@ config USB_SERIAL_NUMBER
string "USB serial number" if !USB_SERIAL_NUMBER_CHIPID string "USB serial number" if !USB_SERIAL_NUMBER_CHIPID
endmenu endmenu
# Generic configuration options for CANbus
config CANBUS_FREQUENCY
int "CAN bus speed" if LOW_LEVEL_OPTIONS && CANSERIAL
default 500000
# Support setting gpio state at startup
config INITIAL_PINS config INITIAL_PINS
string "GPIO pins to set at micro-controller startup" string "GPIO pins to set at micro-controller startup"
depends on LOW_LEVEL_OPTIONS depends on LOW_LEVEL_OPTIONS

View File

@ -13,8 +13,11 @@
#include "board/misc.h" // console_sendf #include "board/misc.h" // console_sendf
#include "canbus.h" // canbus_set_uuid #include "canbus.h" // canbus_set_uuid
#include "command.h" // DECL_CONSTANT #include "command.h" // DECL_CONSTANT
#include "fasthash.h" // fasthash64
#include "sched.h" // sched_wake_task #include "sched.h" // sched_wake_task
#define CANBUS_UUID_LEN 6
// Global storage // Global storage
static struct canbus_data { static struct canbus_data {
uint32_t assigned_id; uint32_t assigned_id;
@ -323,9 +326,10 @@ command_get_canbus_id(uint32_t *args)
DECL_COMMAND_FLAGS(command_get_canbus_id, HF_IN_SHUTDOWN, "get_canbus_id"); DECL_COMMAND_FLAGS(command_get_canbus_id, HF_IN_SHUTDOWN, "get_canbus_id");
void void
canbus_set_uuid(void *uuid) canbus_set_uuid(uint8_t *raw_uuid, uint32_t raw_uuid_len)
{ {
memcpy(CanData.uuid, uuid, sizeof(CanData.uuid)); uint64_t hash = fasthash64(raw_uuid, raw_uuid_len, 0xA16231A7);
memcpy(CanData.uuid, &hash, sizeof(CanData.uuid));
canbus_notify_rx(); canbus_notify_rx();
} }

View File

@ -5,7 +5,6 @@
#define CANBUS_ID_ADMIN 0x3f0 #define CANBUS_ID_ADMIN 0x3f0
#define CANBUS_ID_ADMIN_RESP 0x3f1 #define CANBUS_ID_ADMIN_RESP 0x3f1
#define CANBUS_UUID_LEN 6
struct canbus_msg { struct canbus_msg {
uint32_t id; uint32_t id;
@ -28,6 +27,6 @@ void canbus_set_filter(uint32_t id);
// canbus.c // canbus.c
void canbus_notify_tx(void); void canbus_notify_tx(void);
void canbus_process_data(struct canbus_msg *msg); void canbus_process_data(struct canbus_msg *msg);
void canbus_set_uuid(void *data); void canbus_set_uuid(uint8_t *raw_uuid, uint32_t raw_uuid_len);
#endif // canbus.h #endif // canbus.h

View File

@ -62,7 +62,7 @@ src-$(CONFIG_SERIAL) += $(serial-src-y) generic/serial_irq.c
canbus-src-y := generic/canbus.c ../lib/fast-hash/fasthash.c canbus-src-y := generic/canbus.c ../lib/fast-hash/fasthash.c
canbus-src-$(CONFIG_HAVE_STM32_CANBUS) += stm32/can.c canbus-src-$(CONFIG_HAVE_STM32_CANBUS) += stm32/can.c
canbus-src-$(CONFIG_HAVE_STM32_FDCANBUS) += stm32/fdcan.c canbus-src-$(CONFIG_HAVE_STM32_FDCANBUS) += stm32/fdcan.c
src-$(CONFIG_CANSERIAL) += $(canbus-src-y) src-$(CONFIG_CANSERIAL) += $(canbus-src-y) stm32/chipid.c
src-$(CONFIG_HAVE_GPIO_HARD_PWM) += stm32/hard_pwm.c src-$(CONFIG_HAVE_GPIO_HARD_PWM) += stm32/hard_pwm.c
# Binary output file rules # Binary output file rules

View File

@ -10,7 +10,6 @@
#include "autoconf.h" // CONFIG_MACH_STM32F1 #include "autoconf.h" // CONFIG_MACH_STM32F1
#include "board/irq.h" // irq_disable #include "board/irq.h" // irq_disable
#include "command.h" // DECL_CONSTANT_STR #include "command.h" // DECL_CONSTANT_STR
#include "fasthash.h" // fasthash64
#include "generic/armcm_boot.h" // armcm_enable_irq #include "generic/armcm_boot.h" // armcm_enable_irq
#include "generic/canbus.h" // canbus_notify_tx #include "generic/canbus.h" // canbus_notify_tx
#include "internal.h" // enable_pclock #include "internal.h" // enable_pclock
@ -272,9 +271,5 @@ can_init(void)
if (CAN_RX0_IRQn != CAN_TX_IRQn) if (CAN_RX0_IRQn != CAN_TX_IRQn)
armcm_enable_irq(CAN_IRQHandler, CAN_TX_IRQn, 0); armcm_enable_irq(CAN_IRQHandler, CAN_TX_IRQn, 0);
SOC_CAN->IER = CAN_IER_FMPIE0; SOC_CAN->IER = CAN_IER_FMPIE0;
// Convert unique 96-bit chip id into 48 bit representation
uint64_t hash = fasthash64((uint8_t*)UID_BASE, 12, 0xA16231A7);
canbus_set_uuid(&hash);
} }
DECL_INIT(can_init); DECL_INIT(can_init);

View File

@ -4,6 +4,7 @@
// //
// This file may be distributed under the terms of the GNU GPLv3 license. // This file may be distributed under the terms of the GNU GPLv3 license.
#include "generic/canbus.h" // canbus_set_uuid
#include "generic/usb_cdc.h" // usb_fill_serial #include "generic/usb_cdc.h" // usb_fill_serial
#include "generic/usbstd.h" // usb_string_descriptor #include "generic/usbstd.h" // usb_string_descriptor
#include "internal.h" // UID_BASE #include "internal.h" // UID_BASE
@ -25,9 +26,10 @@ usbserial_get_serialid(void)
void void
chipid_init(void) chipid_init(void)
{ {
if (!CONFIG_USB_SERIAL_NUMBER_CHIPID) if (CONFIG_USB_SERIAL_NUMBER_CHIPID)
return; usb_fill_serial(&cdc_chipid.desc, ARRAY_SIZE(cdc_chipid.data)
usb_fill_serial(&cdc_chipid.desc, ARRAY_SIZE(cdc_chipid.data) , (void*)UID_BASE);
, (void*)UID_BASE); if (CONFIG_CANSERIAL)
canbus_set_uuid((void*)UID_BASE, CHIP_UID_LEN);
} }
DECL_INIT(chipid_init); DECL_INIT(chipid_init);

View File

@ -10,7 +10,6 @@
#include "autoconf.h" // CONFIG_MACH_STM32F1 #include "autoconf.h" // CONFIG_MACH_STM32F1
#include "board/irq.h" // irq_disable #include "board/irq.h" // irq_disable
#include "command.h" // DECL_CONSTANT_STR #include "command.h" // DECL_CONSTANT_STR
#include "fasthash.h" // fasthash64
#include "generic/armcm_boot.h" // armcm_enable_irq #include "generic/armcm_boot.h" // armcm_enable_irq
#include "generic/canbus.h" // canbus_notify_tx #include "generic/canbus.h" // canbus_notify_tx
#include "generic/serial_irq.h" // serial_rx_byte #include "generic/serial_irq.h" // serial_rx_byte
@ -260,9 +259,5 @@ can_init(void)
armcm_enable_irq(CAN_IRQHandler, CAN_IT0_IRQn, 0); armcm_enable_irq(CAN_IRQHandler, CAN_IT0_IRQn, 0);
SOC_CAN->ILE = FDCAN_ILE_EINT0; SOC_CAN->ILE = FDCAN_ILE_EINT0;
SOC_CAN->IE = FDCAN_IE_RF0NE | FDCAN_IE_TC; SOC_CAN->IE = FDCAN_IE_RF0NE | FDCAN_IE_TC;
// Convert unique 96-bit chip id into 48 bit representation
uint64_t hash = fasthash64((uint8_t*)UID_BASE, 12, 0xA16231A7);
canbus_set_uuid(&hash);
} }
DECL_INIT(can_init); DECL_INIT(can_init);