From 4b8a5c5e3834a9773c059f09b67c922150a2aced Mon Sep 17 00:00:00 2001 From: zvecr Date: Fri, 15 Oct 2021 17:31:27 +0100 Subject: [PATCH 01/12] Move existing usb2422 code out the way --- keyboards/massdrop/alt/alt.h | 2 +- keyboards/massdrop/ctrl/ctrl.h | 2 +- tmk_core/protocol/arm_atsam.mk | 2 +- tmk_core/protocol/arm_atsam/arm_atsam_protocol.h | 2 +- tmk_core/protocol/arm_atsam/main_arm_atsam.c | 4 ++-- tmk_core/protocol/arm_atsam/usb/{usb2422.c => usb_hub.c} | 4 ++-- tmk_core/protocol/arm_atsam/usb/{usb2422.h => usb_hub.h} | 4 ++-- 7 files changed, 10 insertions(+), 10 deletions(-) rename tmk_core/protocol/arm_atsam/usb/{usb2422.c => usb_hub.c} (99%) rename tmk_core/protocol/arm_atsam/usb/{usb2422.h => usb_hub.h} (99%) diff --git a/keyboards/massdrop/alt/alt.h b/keyboards/massdrop/alt/alt.h index 03960954506a..c3761bee8bab 100644 --- a/keyboards/massdrop/alt/alt.h +++ b/keyboards/massdrop/alt/alt.h @@ -7,7 +7,7 @@ #include "i2c_master.h" #include "md_rgb_matrix.h" //For led keycodes #include "usb/udi_cdc.h" -#include "usb/usb2422.h" +#include "usb/usb_hub.h" #define LAYOUT_65_ansi_blocker( \ K01, K02, K03, K04, K05, K06, K07, K08, K09, K10, K11, K12, K13, K14, K15, \ diff --git a/keyboards/massdrop/ctrl/ctrl.h b/keyboards/massdrop/ctrl/ctrl.h index 1650beb9a444..a3d66ba77551 100644 --- a/keyboards/massdrop/ctrl/ctrl.h +++ b/keyboards/massdrop/ctrl/ctrl.h @@ -7,7 +7,7 @@ #include "i2c_master.h" #include "md_rgb_matrix.h" //For led keycodes #include "usb/udi_cdc.h" -#include "usb/usb2422.h" +#include "usb/usb_hub.h" #define LAYOUT( \ K01, K02, K03, K04, K05, K06, K07, K08, K09, K10, K11, K12, K13, K14, K15, \ diff --git a/tmk_core/protocol/arm_atsam.mk b/tmk_core/protocol/arm_atsam.mk index 5bb45d658e51..4201b10d2cd0 100644 --- a/tmk_core/protocol/arm_atsam.mk +++ b/tmk_core/protocol/arm_atsam.mk @@ -19,9 +19,9 @@ SRC += $(ARM_ATSAM_DIR)/usb/udi_hid.c SRC += $(ARM_ATSAM_DIR)/usb/udi_hid_kbd.c SRC += $(ARM_ATSAM_DIR)/usb/udi_hid_kbd_desc.c SRC += $(ARM_ATSAM_DIR)/usb/ui.c -SRC += $(ARM_ATSAM_DIR)/usb/usb2422.c SRC += $(ARM_ATSAM_DIR)/usb/usb.c SRC += $(ARM_ATSAM_DIR)/usb/usb_device_udd.c +SRC += $(ARM_ATSAM_DIR)/usb/usb_hub.c SRC += $(ARM_ATSAM_DIR)/usb/usb_util.c # Search Path diff --git a/tmk_core/protocol/arm_atsam/arm_atsam_protocol.h b/tmk_core/protocol/arm_atsam/arm_atsam_protocol.h index d126c66e7d11..8e688a9ac07b 100644 --- a/tmk_core/protocol/arm_atsam/arm_atsam_protocol.h +++ b/tmk_core/protocol/arm_atsam/arm_atsam_protocol.h @@ -29,7 +29,7 @@ along with this program. If not, see . #include "i2c_master.h" #include "spi.h" -#include "./usb/usb2422.h" +#include "./usb/usb_hub.h" #ifndef MD_BOOTLOADER diff --git a/tmk_core/protocol/arm_atsam/main_arm_atsam.c b/tmk_core/protocol/arm_atsam/main_arm_atsam.c index 858b4cd9fc02..1df5112ed8a6 100644 --- a/tmk_core/protocol/arm_atsam/main_arm_atsam.c +++ b/tmk_core/protocol/arm_atsam/main_arm_atsam.c @@ -296,7 +296,7 @@ int main(void) { matrix_init(); - USB2422_init(); + USB_Hub_init(); DBGC(DC_MAIN_UDC_START_BEGIN); udc_start(); @@ -306,7 +306,7 @@ int main(void) { CDC_init(); DBGC(DC_MAIN_CDC_INIT_COMPLETE); - while (USB2422_Port_Detect_Init() == 0) { + while (USB_Hub_Port_Detect_Init() == 0) { } DBG_LED_OFF; diff --git a/tmk_core/protocol/arm_atsam/usb/usb2422.c b/tmk_core/protocol/arm_atsam/usb/usb_hub.c similarity index 99% rename from tmk_core/protocol/arm_atsam/usb/usb2422.c rename to tmk_core/protocol/arm_atsam/usb/usb_hub.c index a878cb6b7cab..d866d3964851 100644 --- a/tmk_core/protocol/arm_atsam/usb/usb2422.c +++ b/tmk_core/protocol/arm_atsam/usb/usb_hub.c @@ -69,7 +69,7 @@ void USB_write2422_block(void) { DBGC(DC_USB_WRITE2422_BLOCK_COMPLETE); } -void USB2422_init(void) { +void USB_Hub_init(void) { Gclk * pgclk = GCLK; Mclk * pmclk = MCLK; Port * pport = PORT; @@ -314,7 +314,7 @@ void USB_set_host_by_voltage(void) { DBGC(DC_USB_SET_HOST_BY_VOLTAGE_COMPLETE); } -uint8_t USB2422_Port_Detect_Init(void) { +uint8_t USB_Hub_Port_Detect_Init(void) { uint32_t port_detect_retry_ms; uint32_t tmod; diff --git a/tmk_core/protocol/arm_atsam/usb/usb2422.h b/tmk_core/protocol/arm_atsam/usb/usb_hub.h similarity index 99% rename from tmk_core/protocol/arm_atsam/usb/usb2422.h rename to tmk_core/protocol/arm_atsam/usb/usb_hub.h index b4830b5bc8fa..b996004dd2a2 100644 --- a/tmk_core/protocol/arm_atsam/usb/usb2422.h +++ b/tmk_core/protocol/arm_atsam/usb/usb_hub.h @@ -389,13 +389,13 @@ extern uint8_t usb_extra_state; extern uint8_t usb_extra_manual; extern uint8_t usb_gcr_auto; -void USB2422_init(void); +void USB_Hub_init(void); +uint8_t USB_Hub_Port_Detect_Init(void); void USB_reset(void); void USB_configure(void); uint16_t USB_active(void); void USB_set_host_by_voltage(void); uint16_t adc_get(uint8_t muxpos); -uint8_t USB2422_Port_Detect_Init(void); void USB_HandleExtraDevice(void); void USB_ExtraSetState(uint8_t state); From cdeadd09d137171b52582f3cda01ab0bd03ac91e Mon Sep 17 00:00:00 2001 From: zvecr Date: Fri, 15 Oct 2021 18:04:22 +0100 Subject: [PATCH 02/12] Move existing funcs to drivers/ --- drivers/usb2422.c | 379 ++++++++++++++++++++++ drivers/usb2422.h | 20 ++ tmk_core/protocol/arm_atsam.mk | 2 + tmk_core/protocol/arm_atsam/usb/usb_hub.c | 90 +---- tmk_core/protocol/arm_atsam/usb/usb_hub.h | 348 -------------------- 5 files changed, 404 insertions(+), 435 deletions(-) create mode 100644 drivers/usb2422.c create mode 100644 drivers/usb2422.h diff --git a/drivers/usb2422.c b/drivers/usb2422.c new file mode 100644 index 000000000000..e33be0035b05 --- /dev/null +++ b/drivers/usb2422.c @@ -0,0 +1,379 @@ +#include "usb2422.h" +#include "i2c_master.h" +#include "wait.h" +#include + +/* -------- USB2422_VID : (USB2422L Offset: 0x00) (R/W 16) Vendor ID -------- */ +typedef union { + struct { + uint16_t VID_LSB : 8; + uint16_t VID_MSB : 8; + } bit; /*!< Structure used for bit access */ + uint16_t reg; /*!< Type used for register access */ +} USB2422_VID_Type; + +/* -------- USB2422_PID : (USB2422L Offset: 0x02) (R/W 16) Product ID -------- */ +typedef union { + struct { + uint16_t PID_LSB : 8; + uint16_t PID_MSB : 8; + } bit; /*!< Structure used for bit access */ + uint16_t reg; /*!< Type used for register access */ +} USB2422_PID_Type; + +/* -------- USB2422_DID : (USB2422L Offset: 0x04) (R/W 16) Device ID -------- */ +typedef union { + struct { + uint16_t DID_LSB : 8; + uint16_t DID_MSB : 8; + } bit; /*!< Structure used for bit access */ + uint16_t reg; /*!< Type used for register access */ +} USB2422_DID_Type; + +/* -------- USB2422_CFG1 : (USB2422L Offset: 0x06) (R/W 8) Configuration Data Byte 1-------- */ +typedef union { + struct { + uint8_t PORT_PWR : 1; + uint8_t CURRENT_SNS : 2; + uint8_t EOP_DISABLE : 1; + uint8_t MTT_ENABLE : 1; + uint8_t HS_DISABLE : 1; + uint8_t : 1; + uint8_t SELF_BUS_PWR : 1; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ +} USB2422_CFG1_Type; + +/* -------- USB2422_CFG2 : (USB2422L Offset: 0x07) (R/W 8) Configuration Data Byte 2-------- */ +typedef union { + struct { + uint8_t : 3; + uint8_t COMPOUND : 1; + uint8_t OC_TIMER : 2; + uint8_t : 1; + uint8_t DYNAMIC : 1; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ +} USB2422_CFG2_Type; + +/* -------- USB2422_CFG3 : (USB2422L Offset: 0x08) (R/W 16) Configuration Data Byte 3-------- */ +typedef union { + struct { + uint8_t STRING_EN : 1; + uint8_t : 2; + uint8_t PRTMAP_EN : 1; + uint8_t : 4; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ +} USB2422_CFG3_Type; + +/* -------- USB2422_NRD : (USB2422L Offset: 0x09) (R/W 8) Non Removable Device -------- */ +typedef union { + struct { + uint8_t : 5; + uint8_t PORT2_NR : 1; + uint8_t PORT1_NR : 1; + uint8_t : 1; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ +} USB2422_NRD_Type; + +/* -------- USB2422_PDS : (USB2422L Offset: 0x0A) (R/W 8) Port Diable for Self-Powered Operation -------- */ +typedef union { + struct { + uint8_t : 1; + uint8_t PORT1_DIS : 1; + uint8_t PORT2_DIS : 1; + uint8_t : 5; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ +} USB2422_PDS_Type; + + +/* -------- USB2422_PDB : (USB2422L Offset: 0x0B) (R/W 8) Port Diable for Bus-Powered Operation -------- */ + +typedef union { + struct { + uint8_t : 1; + uint8_t PORT1_DIS : 1; + uint8_t PORT2_DIS : 1; + uint8_t : 5; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ +} USB2422_PDB_Type; + +/* -------- USB2422_MAXPS : (USB2422L Offset: 0x0C) (R/W 8) Max Power for Self-Powered Operation -------- */ +typedef union { + struct { + uint8_t MAX_PWR_SP : 8; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ +} USB2422_MAXPS_Type; + +/* -------- USB2422_MAXPB : (USB2422L Offset: 0x0D) (R/W 8) Max Power for Bus-Powered Operation -------- */ +typedef union { + struct { + uint8_t MAX_PWR_BP : 8; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ +} USB2422_MAXPB_Type; + +/* -------- USB2422_HCMCS : (USB2422L Offset: 0x0E) (R/W 8) Hub Controller Max Current for Self-Powered Operation -------- */ +typedef union { + struct { + uint8_t HC_MAX_C_SP : 8; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ +} USB2422_HCMCS_Type; + +/* -------- USB2422_HCMCB : (USB2422L Offset: 0x0F) (R/W 8) Hub Controller Max Current for Bus-Powered Operation -------- */ +typedef union { + struct { + uint8_t HC_MAX_C_BP : 8; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ +} USB2422_HCMCB_Type; + +/* -------- USB2422_PWRT : (USB2422L Offset: 0x10) (R/W 8) Power On Time -------- */ +typedef union { + struct { + uint8_t POWER_ON_TIME : 8; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ +} USB2422_PWRT_Type; + +/* -------- USB2422_LANGID LSB : (USB2422L Offset: 0x11) (R/W 16) Language ID -------- */ +typedef union { + struct { + uint8_t LANGID_LSB : 8; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ +} USB2422_LANGID_LSB_Type; + +/* -------- USB2422_LANGID MSB : (USB2422L Offset: 0x12) (R/W 16) Language ID -------- */ +typedef union { + struct { + uint8_t LANGID_MSB : 8; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ +} USB2422_LANGID_MSB_Type; + +/* -------- USB2422_MFRSL : (USB2422L Offset: 0x13) (R/W 8) Manufacturer String Length -------- */ +typedef union { + struct { + uint8_t MFR_STR_LEN : 8; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ +} USB2422_MFRSL_Type; + +/* -------- USB2422_PRDSL : (USB2422L Offset: 0x14) (R/W 8) Product String Length -------- */ +typedef union { + struct { + uint8_t PRD_STR_LEN : 8; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ +} USB2422_PRDSL_Type; + +/* -------- USB2422_SERSL : (USB2422L Offset: 0x15) (R/W 8) Serial String Length -------- */ +typedef union { + struct { + uint8_t SER_STR_LEN : 8; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ +} USB2422_SERSL_Type; + +/* -------- USB2422_MFRSTR : (USB2422L Offset: 0x16-53) (R/W 8) Maufacturer String -------- */ +typedef uint16_t USB2422_MFRSTR_Type; + +/* -------- USB2422_PRDSTR : (USB2422L Offset: 0x54-91) (R/W 8) Product String -------- */ +typedef uint16_t USB2422_PRDSTR_Type; + +/* -------- USB2422_SERSTR : (USB2422L Offset: 0x92-CF) (R/W 8) Serial String -------- */ +typedef uint16_t USB2422_SERSTR_Type; + +/* -------- USB2422_BCEN : (USB2422L Offset: 0xD0) (R/W 8) Battery Charging Enable -------- */ + +typedef union { + struct { + uint8_t : 1; + uint8_t PORT1_BCE : 1; + uint8_t PORT2_BCE : 1; + uint8_t : 5; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ +} USB2422_BCEN_Type; + +/* -------- USB2422_BOOSTUP : (USB2422L Offset: 0xF6) (R/W 8) Boost Upstream -------- */ +typedef union { + struct { + uint8_t BOOST : 2; + uint8_t : 6; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ +} USB2422_BOOSTUP_Type; + +/* -------- USB2422_BOOSTDOWN : (USB2422L Offset: 0xF8) (R/W 8) Boost Downstream -------- */ +typedef union { + struct { + uint8_t BOOST1 : 2; + uint8_t BOOST2 : 2; + uint8_t : 4; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ +} USB2422_BOOSTDOWN_Type; + +/* -------- USB2422_PRTSP : (USB2422L Offset: 0xFA) (R/W 8) Port Swap -------- */ +typedef union { + struct { + uint8_t : 1; + uint8_t PORT1_SP : 1; + uint8_t PORT2_SP : 1; + uint8_t : 5; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ +} USB2422_PRTSP_Type; + +/* -------- USB2422_PRTR12 : (USB2422L Offset: 0xFB) (R/W 8) Port 1/2 Remap -------- */ +typedef union { + struct { + uint8_t PORT1_REMAP : 4; + uint8_t PORT2_REMAP : 4; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ +} USB2422_PRTR12_Type; + +#define USB2422_PRTR12_DISABLE 0 +#define USB2422_PRT12_P2TOL1 1 +#define USB2422_PRT12_P2XTOL2 2 +#define USB2422_PRT12_P1TOL1 1 +#define USB2422_PRT12_P1XTOL2 2 + +/* -------- USB2422_STCD : (USB2422L Offset: 0xFF) (R/W 8) Status Command -------- */ +typedef union { + struct { + uint8_t USB_ATTACH : 1; + uint8_t RESET : 1; + uint8_t INTF_PWRDN : 1; + uint8_t : 5; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ +} USB2422_STCD_Type; + +/** \brief USB2422 device hardware registers */ +typedef struct { + USB2422_VID_Type VID; /**< \brief Offset: 0x00*/ + USB2422_PID_Type PID; /**< \brief Offset: 0x02*/ + USB2422_DID_Type DID; /**< \brief Offset: 0x04*/ + USB2422_CFG1_Type CFG1; /**< \brief Offset: 0x06*/ + USB2422_CFG2_Type CFG2; /**< \brief Offset: 0x07*/ + USB2422_CFG3_Type CFG3; /**< \brief Offset: 0x08*/ + USB2422_NRD_Type NRD; /**< \brief Offset: 0x09*/ + USB2422_PDS_Type PDS; /**< \brief Offset: 0x0A*/ + USB2422_PDB_Type PDB; /**< \brief Offset: 0x0B*/ + USB2422_MAXPS_Type MAXPS; /**< \brief Offset: 0x0C*/ + USB2422_MAXPB_Type MAXPB; /**< \brief Offset: 0x0D*/ + USB2422_HCMCS_Type HCMCS; /**< \brief Offset: 0x0E*/ + USB2422_HCMCB_Type HCMCB; /**< \brief Offset: 0x0F*/ + USB2422_PWRT_Type PWRT; /**< \brief Offset: 0x10*/ + USB2422_LANGID_LSB_Type LANGID_LSB; /**< \brief Offset: 0x11*/ + USB2422_LANGID_MSB_Type LANGID_MSB; /**< \brief Offset: 0x12*/ + USB2422_MFRSL_Type MFRSL; /**< \brief Offset: 0x13*/ + USB2422_PRDSL_Type PRDSL; /**< \brief Offset: 0x14*/ + USB2422_SERSL_Type SERSL; /**< \brief Offset: 0x15*/ + USB2422_MFRSTR_Type MFRSTR[31]; /**< \brief Offset: 0x16*/ + USB2422_PRDSTR_Type PRDSTR[31]; /**< \brief Offset: 0x54*/ + USB2422_SERSTR_Type SERSTR[31]; /**< \brief Offset: 0x92*/ + USB2422_BCEN_Type BCEN; /**< \brief Offset: 0xD0*/ + uint8_t Reserved1[0x25]; + USB2422_BOOSTUP_Type BOOSTUP; /**< \brief Offset: 0xF6*/ + uint8_t Reserved2[0x1]; + USB2422_BOOSTDOWN_Type BOOSTDOWN; /**< \brief Offset: 0xF8*/ + uint8_t Reserved3[0x1]; + USB2422_PRTSP_Type PRTSP; /**< \brief Offset: 0xFA*/ + USB2422_PRTR12_Type PRTR12; /**< \brief Offset: 0xFB*/ + uint8_t Reserved4[0x3]; + USB2422_STCD_Type STCD; /**< \brief Offset: 0xFF*/ +} Usb2422; + +#ifndef USB2422_ADDRESS +# define USB2422_ADDRESS 0x58 +#endif + +Usb2422 USB2422_shadow; +unsigned char i2c0_buf[34]; + +static const uint16_t MFRNAME[] = {'M', 'a', 's', 's', 'd', 'r', 'o', 'p', ' ', 'I', 'n', 'c', '.'}; // Massdrop Inc. +static const uint16_t PRDNAME[] = {'M', 'a', 's', 's', 'd', 'r', 'o', 'p', ' ', 'H', 'u', 'b'}; // Massdrop Hub +static const uint16_t SERNAME[] = {'U', 'n', 'a', 'v', 'a', 'i', 'l', 'a', 'b', 'l', 'e'}; // Unavailable + +static void USB2422_write_block(void) { + unsigned char *dest = i2c0_buf; + unsigned char *src; + unsigned char *base = (unsigned char *)&USB2422_shadow; + + for (src = base; src < base + 256; src += 32) { + dest[0] = src - base; + dest[1] = 32; + memcpy(&dest[2], src, 32); + i2c0_transmit(USB2422_ADDRESS, dest, 34, 50000); + SERCOM0->I2CM.CTRLB.bit.CMD = 0x03; + while (SERCOM0->I2CM.SYNCBUSY.bit.SYSOP) {} + wait_us(100); + } +} + +void USB2422_init() { + //i2c_init(); + i2c0_init(); // IC2 clk must be high at USB2422 reset release time to signal SMB configuration +} + +void USB2422_configure(){ + Usb2422 *pusb2422 = &USB2422_shadow; + memset(pusb2422, 0, sizeof(Usb2422)); + + uint16_t *serial_use = (uint16_t *)SERNAME; // Default to use SERNAME from this file + uint8_t serial_length = sizeof(SERNAME) / sizeof(uint16_t); // Default to use SERNAME from this file +// #ifndef MD_BOOTLOADER +// uint32_t serial_ptrloc = (uint32_t)&_srom - 4; +// #else // MD_BOOTLOADER +// uint32_t serial_ptrloc = (uint32_t)&_erom - 4; +// #endif // MD_BOOTLOADER +// uint32_t serial_address = *(uint32_t *)serial_ptrloc; // Address of bootloader's serial number if available + +// if (serial_address != 0xFFFFFFFF && serial_address < serial_ptrloc) // Check for factory programmed serial address +// { +// if ((serial_address & 0xFF) % 4 == 0) // Check alignment +// { +// serial_use = (uint16_t *)(serial_address); +// serial_length = 0; +// while ((*(serial_use + serial_length) > 32 && *(serial_use + serial_length) < 127) && serial_length < BOOTLOADER_SERIAL_MAX_SIZE) { +// serial_length++; +// DBGC(DC_USB_CONFIGURE_GET_SERIAL); +// } +// } +// } + + // configure Usb2422 registers + pusb2422->VID.reg = 0x04D8; // from Microchip 4/19/2018 + pusb2422->PID.reg = 0xEEC5; // from Microchip 4/19/2018 = Massdrop, Inc. USB Hub + pusb2422->DID.reg = 0x0101; // BCD 01.01 + pusb2422->CFG1.bit.SELF_BUS_PWR = 1; // self powered for now + pusb2422->CFG1.bit.HS_DISABLE = 1; // full or high speed + // pusb2422->CFG2.bit.COMPOUND = 0; // compound device + pusb2422->CFG3.bit.STRING_EN = 1; // strings enabled + // pusb2422->NRD.bit.PORT2_NR = 0; // MCU is non-removable + pusb2422->MAXPB.reg = 20; // 0mA + pusb2422->HCMCB.reg = 20; // 0mA + pusb2422->MFRSL.reg = sizeof(MFRNAME) / sizeof(uint16_t); + pusb2422->PRDSL.reg = sizeof(PRDNAME) / sizeof(uint16_t); + pusb2422->SERSL.reg = serial_length; + memcpy(pusb2422->MFRSTR, MFRNAME, sizeof(MFRNAME)); + memcpy(pusb2422->PRDSTR, PRDNAME, sizeof(PRDNAME)); + memcpy(pusb2422->SERSTR, serial_use, serial_length * sizeof(uint16_t)); + // pusb2422->BOOSTUP.bit.BOOST=3; //upstream port + // pusb2422->BOOSTDOWN.bit.BOOST1=0; // extra port + // pusb2422->BOOSTDOWN.bit.BOOST2=2; //MCU is close + pusb2422->STCD.bit.USB_ATTACH = 1; + + USB2422_write_block(); +} diff --git a/drivers/usb2422.h b/drivers/usb2422.h new file mode 100644 index 000000000000..fa0d2d248ee3 --- /dev/null +++ b/drivers/usb2422.h @@ -0,0 +1,20 @@ +/* Copyright 2021 QMK + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once + +void USB2422_init(void); + +void USB2422_configure(void); diff --git a/tmk_core/protocol/arm_atsam.mk b/tmk_core/protocol/arm_atsam.mk index 4201b10d2cd0..d6d5a16502ac 100644 --- a/tmk_core/protocol/arm_atsam.mk +++ b/tmk_core/protocol/arm_atsam.mk @@ -24,5 +24,7 @@ SRC += $(ARM_ATSAM_DIR)/usb/usb_device_udd.c SRC += $(ARM_ATSAM_DIR)/usb/usb_hub.c SRC += $(ARM_ATSAM_DIR)/usb/usb_util.c +SRC += $(DRIVER_PATH)/usb2422.c + # Search Path VPATH += $(TMK_DIR)/$(ARM_ATSAM_DIR) diff --git a/tmk_core/protocol/arm_atsam/usb/usb_hub.c b/tmk_core/protocol/arm_atsam/usb/usb_hub.c index d866d3964851..23ee44a1bb92 100644 --- a/tmk_core/protocol/arm_atsam/usb/usb_hub.c +++ b/tmk_core/protocol/arm_atsam/usb/usb_hub.c @@ -16,25 +16,9 @@ along with this program. If not, see . */ #include "arm_atsam_protocol.h" +#include "drivers/usb2422.h" #include -Usb2422 USB2422_shadow; -unsigned char i2c0_buf[34]; - -const uint16_t MFRNAME[] = {'M', 'a', 's', 's', 'd', 'r', 'o', 'p', ' ', 'I', 'n', 'c', '.'}; // Massdrop Inc. -const uint16_t PRDNAME[] = {'M', 'a', 's', 's', 'd', 'r', 'o', 'p', ' ', 'H', 'u', 'b'}; // Massdrop Hub -#ifndef MD_BOOTLOADER -// Serial number reported stops before first found space character or at last found character -const uint16_t SERNAME[] = {'U', 'n', 'a', 'v', 'a', 'i', 'l', 'a', 'b', 'l', 'e'}; // Unavailable -#else -// In production, this field is found, modified, and offset noted as the last 32-bit word in the bootloader space -// The offset allows the application to use the factory programmed serial (which may differ from the physical serial label) -// Serial number reported stops before first found space character or when max size is reached -__attribute__((__aligned__(4))) const uint16_t SERNAME[BOOTLOADER_SERIAL_MAX_SIZE] = {'M', 'D', 'H', 'U', 'B', 'B', 'O', 'O', 'T', 'L', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'}; -// NOTE: Serial replacer will not write a string longer than given here as a precaution, so give enough -// space as needed and adjust BOOTLOADER_SERIAL_MAX_SIZE to match amount given -#endif // MD_BOOTLOADER - uint8_t usb_host_port; #ifndef MD_BOOTLOADER @@ -47,28 +31,6 @@ uint8_t usb_gcr_auto; uint16_t adc_extra; -void USB_write2422_block(void) { - unsigned char *dest = i2c0_buf; - unsigned char *src; - unsigned char *base = (unsigned char *)&USB2422_shadow; - - DBGC(DC_USB_WRITE2422_BLOCK_BEGIN); - - for (src = base; src < base + 256; src += 32) { - dest[0] = src - base; - dest[1] = 32; - memcpy(&dest[2], src, 32); - i2c0_transmit(USB2422_ADDR, dest, 34, 50000); - SERCOM0->I2CM.CTRLB.bit.CMD = 0x03; - while (SERCOM0->I2CM.SYNCBUSY.bit.SYSOP) { - DBGC(DC_USB_WRITE2422_BLOCK_SYNC_SYSOP); - } - wait_us(100); - } - - DBGC(DC_USB_WRITE2422_BLOCK_COMPLETE); -} - void USB_Hub_init(void) { Gclk * pgclk = GCLK; Mclk * pmclk = MCLK; @@ -149,7 +111,7 @@ void USB_Hub_init(void) { pport->Group[USB2422_HUB_ACTIVE_GROUP].PINCFG[USB2422_HUB_ACTIVE_PIN].bit.INEN = 1; - i2c0_init(); // IC2 clk must be high at USB2422 reset release time to signal SMB configuration + USB2422_init(); sr_exp_data.bit.HUB_CONNECT = 1; // connect signal sr_exp_data.bit.HUB_RESET_N = 1; // reset high @@ -181,55 +143,9 @@ void USB_reset(void) { } void USB_configure(void) { - Usb2422 *pusb2422 = &USB2422_shadow; - memset(pusb2422, 0, sizeof(Usb2422)); - - uint16_t *serial_use = (uint16_t *)SERNAME; // Default to use SERNAME from this file - uint8_t serial_length = sizeof(SERNAME) / sizeof(uint16_t); // Default to use SERNAME from this file -#ifndef MD_BOOTLOADER - uint32_t serial_ptrloc = (uint32_t)&_srom - 4; -#else // MD_BOOTLOADER - uint32_t serial_ptrloc = (uint32_t)&_erom - 4; -#endif // MD_BOOTLOADER - uint32_t serial_address = *(uint32_t *)serial_ptrloc; // Address of bootloader's serial number if available - DBGC(DC_USB_CONFIGURE_BEGIN); - if (serial_address != 0xFFFFFFFF && serial_address < serial_ptrloc) // Check for factory programmed serial address - { - if ((serial_address & 0xFF) % 4 == 0) // Check alignment - { - serial_use = (uint16_t *)(serial_address); - serial_length = 0; - while ((*(serial_use + serial_length) > 32 && *(serial_use + serial_length) < 127) && serial_length < BOOTLOADER_SERIAL_MAX_SIZE) { - serial_length++; - DBGC(DC_USB_CONFIGURE_GET_SERIAL); - } - } - } - - // configure Usb2422 registers - pusb2422->VID.reg = 0x04D8; // from Microchip 4/19/2018 - pusb2422->PID.reg = 0xEEC5; // from Microchip 4/19/2018 = Massdrop, Inc. USB Hub - pusb2422->DID.reg = 0x0101; // BCD 01.01 - pusb2422->CFG1.bit.SELF_BUS_PWR = 1; // self powered for now - pusb2422->CFG1.bit.HS_DISABLE = 1; // full or high speed - // pusb2422->CFG2.bit.COMPOUND = 0; // compound device - pusb2422->CFG3.bit.STRING_EN = 1; // strings enabled - // pusb2422->NRD.bit.PORT2_NR = 0; // MCU is non-removable - pusb2422->MAXPB.reg = 20; // 0mA - pusb2422->HCMCB.reg = 20; // 0mA - pusb2422->MFRSL.reg = sizeof(MFRNAME) / sizeof(uint16_t); - pusb2422->PRDSL.reg = sizeof(PRDNAME) / sizeof(uint16_t); - pusb2422->SERSL.reg = serial_length; - memcpy(pusb2422->MFRSTR, MFRNAME, sizeof(MFRNAME)); - memcpy(pusb2422->PRDSTR, PRDNAME, sizeof(PRDNAME)); - memcpy(pusb2422->SERSTR, serial_use, serial_length * sizeof(uint16_t)); - // pusb2422->BOOSTUP.bit.BOOST=3; //upstream port - // pusb2422->BOOSTDOWN.bit.BOOST1=0; // extra port - // pusb2422->BOOSTDOWN.bit.BOOST2=2; //MCU is close - pusb2422->STCD.bit.USB_ATTACH = 1; - USB_write2422_block(); + USB2422_configure(); adc_extra = 0; diff --git a/tmk_core/protocol/arm_atsam/usb/usb_hub.h b/tmk_core/protocol/arm_atsam/usb/usb_hub.h index b996004dd2a2..386b28dbed3e 100644 --- a/tmk_core/protocol/arm_atsam/usb/usb_hub.h +++ b/tmk_core/protocol/arm_atsam/usb/usb_hub.h @@ -20,357 +20,9 @@ along with this program. If not, see . #define REV_USB2422 0x100 -#define USB2422_ADDR 0x58 // I2C device address, one instance - #define USB2422_HUB_ACTIVE_GROUP 0 // PA #define USB2422_HUB_ACTIVE_PIN 18 // 18 -/* -------- USB2422_VID : (USB2422L Offset: 0x00) (R/W 16) Vendor ID -------- */ -#if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) -typedef union { - struct { - uint16_t VID_LSB : 8; - uint16_t VID_MSB : 8; - } bit; /*!< Structure used for bit access */ - uint16_t reg; /*!< Type used for register access */ -} USB2422_VID_Type; -#endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ - -/* -------- USB2422_PID : (USB2422L Offset: 0x02) (R/W 16) Product ID -------- */ -#if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) -typedef union { - struct { - uint16_t PID_LSB : 8; - uint16_t PID_MSB : 8; - } bit; /*!< Structure used for bit access */ - uint16_t reg; /*!< Type used for register access */ -} USB2422_PID_Type; -#endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ - -/* -------- USB2422_DID : (USB2422L Offset: 0x04) (R/W 16) Device ID -------- */ -#if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) -typedef union { - struct { - uint16_t DID_LSB : 8; - uint16_t DID_MSB : 8; - } bit; /*!< Structure used for bit access */ - uint16_t reg; /*!< Type used for register access */ -} USB2422_DID_Type; -#endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ - -/* -------- USB2422_CFG1 : (USB2422L Offset: 0x06) (R/W 8) Configuration Data Byte 1-------- */ -#if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) -typedef union { - struct { - uint8_t PORT_PWR : 1; - uint8_t CURRENT_SNS : 2; - uint8_t EOP_DISABLE : 1; - uint8_t MTT_ENABLE : 1; - uint8_t HS_DISABLE : 1; - uint8_t : 1; - uint8_t SELF_BUS_PWR : 1; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ -} USB2422_CFG1_Type; -#endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ - -/* -------- USB2422_CFG2 : (USB2422L Offset: 0x07) (R/W 8) Configuration Data Byte 2-------- */ -#if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) -typedef union { - struct { - uint8_t : 3; - uint8_t COMPOUND : 1; - uint8_t OC_TIMER : 2; - uint8_t : 1; - uint8_t DYNAMIC : 1; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ -} USB2422_CFG2_Type; -#endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ - -/* -------- USB2422_CFG3 : (USB2422L Offset: 0x08) (R/W 16) Configuration Data Byte 3-------- */ -#if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) -typedef union { - struct { - uint8_t STRING_EN : 1; - uint8_t : 2; - uint8_t PRTMAP_EN : 1; - uint8_t : 4; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ -} USB2422_CFG3_Type; -#endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ - -/* -------- USB2422_NRD : (USB2422L Offset: 0x09) (R/W 8) Non Removable Device -------- */ -#if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) -typedef union { - struct { - uint8_t : 5; - uint8_t PORT2_NR : 1; - uint8_t PORT1_NR : 1; - uint8_t : 1; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ -} USB2422_NRD_Type; -#endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ - -/* -------- USB2422_PDS : (USB2422L Offset: 0x0A) (R/W 8) Port Diable for Self-Powered Operation -------- */ -#if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) -typedef union { - struct { - uint8_t : 1; - uint8_t PORT1_DIS : 1; - uint8_t PORT2_DIS : 1; - uint8_t : 5; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ -} USB2422_PDS_Type; -#endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ - -/* -------- USB2422_PDB : (USB2422L Offset: 0x0B) (R/W 8) Port Diable for Bus-Powered Operation -------- */ -#if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) -typedef union { - struct { - uint8_t : 1; - uint8_t PORT1_DIS : 1; - uint8_t PORT2_DIS : 1; - uint8_t : 5; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ -} USB2422_PDB_Type; -#endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ - -/* -------- USB2422_MAXPS : (USB2422L Offset: 0x0C) (R/W 8) Max Power for Self-Powered Operation -------- */ -#if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) -typedef union { - struct { - uint8_t MAX_PWR_SP : 8; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ -} USB2422_MAXPS_Type; -#endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ - -/* -------- USB2422_MAXPB : (USB2422L Offset: 0x0D) (R/W 8) Max Power for Bus-Powered Operation -------- */ -#if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) -typedef union { - struct { - uint8_t MAX_PWR_BP : 8; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ -} USB2422_MAXPB_Type; -#endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ - -/* -------- USB2422_HCMCS : (USB2422L Offset: 0x0E) (R/W 8) Hub Controller Max Current for Self-Powered Operation -------- */ -#if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) -typedef union { - struct { - uint8_t HC_MAX_C_SP : 8; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ -} USB2422_HCMCS_Type; -#endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ - -/* -------- USB2422_HCMCB : (USB2422L Offset: 0x0F) (R/W 8) Hub Controller Max Current for Bus-Powered Operation -------- */ -#if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) -typedef union { - struct { - uint8_t HC_MAX_C_BP : 8; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ -} USB2422_HCMCB_Type; -#endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ - -/* -------- USB2422_PWRT : (USB2422L Offset: 0x10) (R/W 8) Power On Time -------- */ -#if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) -typedef union { - struct { - uint8_t POWER_ON_TIME : 8; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ -} USB2422_PWRT_Type; -#endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ - -/* -------- USB2422_LANGID LSB : (USB2422L Offset: 0x11) (R/W 16) Language ID -------- */ -#if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) -typedef union { - struct { - uint8_t LANGID_LSB : 8; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ -} USB2422_LANGID_LSB_Type; -#endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ - -/* -------- USB2422_LANGID MSB : (USB2422L Offset: 0x12) (R/W 16) Language ID -------- */ -#if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) -typedef union { - struct { - uint8_t LANGID_MSB : 8; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ -} USB2422_LANGID_MSB_Type; -#endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ - -/* -------- USB2422_MFRSL : (USB2422L Offset: 0x13) (R/W 8) Manufacturer String Length -------- */ -#if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) -typedef union { - struct { - uint8_t MFR_STR_LEN : 8; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ -} USB2422_MFRSL_Type; -#endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ - -/* -------- USB2422_PRDSL : (USB2422L Offset: 0x14) (R/W 8) Product String Length -------- */ -#if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) -typedef union { - struct { - uint8_t PRD_STR_LEN : 8; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ -} USB2422_PRDSL_Type; -#endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ - -/* -------- USB2422_SERSL : (USB2422L Offset: 0x15) (R/W 8) Serial String Length -------- */ -#if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) -typedef union { - struct { - uint8_t SER_STR_LEN : 8; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ -} USB2422_SERSL_Type; -#endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ - -/* -------- USB2422_MFRSTR : (USB2422L Offset: 0x16-53) (R/W 8) Maufacturer String -------- */ -#if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) -typedef uint16_t USB2422_MFRSTR_Type; -#endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ - -/* -------- USB2422_PRDSTR : (USB2422L Offset: 0x54-91) (R/W 8) Product String -------- */ -#if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) -typedef uint16_t USB2422_PRDSTR_Type; -#endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ - -/* -------- USB2422_SERSTR : (USB2422L Offset: 0x92-CF) (R/W 8) Serial String -------- */ -#if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) -typedef uint16_t USB2422_SERSTR_Type; -#endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ - -/* -------- USB2422_BCEN : (USB2422L Offset: 0xD0) (R/W 8) Battery Charging Enable -------- */ -#if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) -typedef union { - struct { - uint8_t : 1; - uint8_t PORT1_BCE : 1; - uint8_t PORT2_BCE : 1; - uint8_t : 5; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ -} USB2422_BCEN_Type; -#endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ - -/* -------- USB2422_BOOSTUP : (USB2422L Offset: 0xF6) (R/W 8) Boost Upstream -------- */ -#if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) -typedef union { - struct { - uint8_t BOOST : 2; - uint8_t : 6; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ -} USB2422_BOOSTUP_Type; -#endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ - -/* -------- USB2422_BOOSTDOWN : (USB2422L Offset: 0xF8) (R/W 8) Boost Downstream -------- */ -#if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) -typedef union { - struct { - uint8_t BOOST1 : 2; - uint8_t BOOST2 : 2; - uint8_t : 4; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ -} USB2422_BOOSTDOWN_Type; -#endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ - -/* -------- USB2422_PRTSP : (USB2422L Offset: 0xFA) (R/W 8) Port Swap -------- */ -#if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) -typedef union { - struct { - uint8_t : 1; - uint8_t PORT1_SP : 1; - uint8_t PORT2_SP : 1; - uint8_t : 5; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ -} USB2422_PRTSP_Type; -#endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ - -/* -------- USB2422_PRTR12 : (USB2422L Offset: 0xFB) (R/W 8) Port 1/2 Remap -------- */ -#if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) -typedef union { - struct { - uint8_t PORT1_REMAP : 4; - uint8_t PORT2_REMAP : 4; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ -} USB2422_PRTR12_Type; -#endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ -#define USB2422_PRTR12_DISABLE 0 -#define USB2422_PRT12_P2TOL1 1 -#define USB2422_PRT12_P2XTOL2 2 -#define USB2422_PRT12_P1TOL1 1 -#define USB2422_PRT12_P1XTOL2 2 - -/* -------- USB2422_STCD : (USB2422L Offset: 0xFF) (R/W 8) Status Command -------- */ -#if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) -typedef union { - struct { - uint8_t USB_ATTACH : 1; - uint8_t RESET : 1; - uint8_t INTF_PWRDN : 1; - uint8_t : 5; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ -} USB2422_STCD_Type; -#endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ - -/** \brief USB2422 device hardware registers */ -#if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) -typedef struct { - USB2422_VID_Type VID; /**< \brief Offset: 0x00*/ - USB2422_PID_Type PID; /**< \brief Offset: 0x02*/ - USB2422_DID_Type DID; /**< \brief Offset: 0x04*/ - USB2422_CFG1_Type CFG1; /**< \brief Offset: 0x06*/ - USB2422_CFG2_Type CFG2; /**< \brief Offset: 0x07*/ - USB2422_CFG3_Type CFG3; /**< \brief Offset: 0x08*/ - USB2422_NRD_Type NRD; /**< \brief Offset: 0x09*/ - USB2422_PDS_Type PDS; /**< \brief Offset: 0x0A*/ - USB2422_PDB_Type PDB; /**< \brief Offset: 0x0B*/ - USB2422_MAXPS_Type MAXPS; /**< \brief Offset: 0x0C*/ - USB2422_MAXPB_Type MAXPB; /**< \brief Offset: 0x0D*/ - USB2422_HCMCS_Type HCMCS; /**< \brief Offset: 0x0E*/ - USB2422_HCMCB_Type HCMCB; /**< \brief Offset: 0x0F*/ - USB2422_PWRT_Type PWRT; /**< \brief Offset: 0x10*/ - USB2422_LANGID_LSB_Type LANGID_LSB; /**< \brief Offset: 0x11*/ - USB2422_LANGID_MSB_Type LANGID_MSB; /**< \brief Offset: 0x12*/ - USB2422_MFRSL_Type MFRSL; /**< \brief Offset: 0x13*/ - USB2422_PRDSL_Type PRDSL; /**< \brief Offset: 0x14*/ - USB2422_SERSL_Type SERSL; /**< \brief Offset: 0x15*/ - USB2422_MFRSTR_Type MFRSTR[31]; /**< \brief Offset: 0x16*/ - USB2422_PRDSTR_Type PRDSTR[31]; /**< \brief Offset: 0x54*/ - USB2422_SERSTR_Type SERSTR[31]; /**< \brief Offset: 0x92*/ - USB2422_BCEN_Type BCEN; /**< \brief Offset: 0xD0*/ - uint8_t Reserved1[0x25]; - USB2422_BOOSTUP_Type BOOSTUP; /**< \brief Offset: 0xF6*/ - uint8_t Reserved2[0x1]; - USB2422_BOOSTDOWN_Type BOOSTDOWN; /**< \brief Offset: 0xF8*/ - uint8_t Reserved3[0x1]; - USB2422_PRTSP_Type PRTSP; /**< \brief Offset: 0xFA*/ - USB2422_PRTR12_Type PRTR12; /**< \brief Offset: 0xFB*/ - uint8_t Reserved4[0x3]; - USB2422_STCD_Type STCD; /**< \brief Offset: 0xFF*/ -} Usb2422; -#endif - #define PORT_DETECT_RETRY_INTERVAL 2000 #define USB_EXTRA_ADC_THRESHOLD 900 From a3eaf128ab581259f823584c6c1fdf8e1008f536 Mon Sep 17 00:00:00 2001 From: zvecr Date: Fri, 15 Oct 2021 18:16:33 +0100 Subject: [PATCH 03/12] refactor --- drivers/usb2422.c | 60 +++++++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/drivers/usb2422.c b/drivers/usb2422.c index e33be0035b05..530067651848 100644 --- a/drivers/usb2422.c +++ b/drivers/usb2422.c @@ -293,23 +293,32 @@ typedef struct { USB2422_PRTR12_Type PRTR12; /**< \brief Offset: 0xFB*/ uint8_t Reserved4[0x3]; USB2422_STCD_Type STCD; /**< \brief Offset: 0xFF*/ -} Usb2422; +} Usb2422_t; #ifndef USB2422_ADDRESS # define USB2422_ADDRESS 0x58 #endif +#ifndef USB2422_VENDOR_ID +# define USB2422_VENDOR_ID 0x04D8 +#endif +#ifndef USB2422_PRODUCT_ID +# define USB2422_PRODUCT_ID 0xEEC5 +#endif +#ifndef USB2422_DEVICE_VER +# define USB2422_DEVICE_VER 0x0101 +#endif -Usb2422 USB2422_shadow; -unsigned char i2c0_buf[34]; +static Usb2422_t config; static const uint16_t MFRNAME[] = {'M', 'a', 's', 's', 'd', 'r', 'o', 'p', ' ', 'I', 'n', 'c', '.'}; // Massdrop Inc. static const uint16_t PRDNAME[] = {'M', 'a', 's', 's', 'd', 'r', 'o', 'p', ' ', 'H', 'u', 'b'}; // Massdrop Hub static const uint16_t SERNAME[] = {'U', 'n', 'a', 'v', 'a', 'i', 'l', 'a', 'b', 'l', 'e'}; // Unavailable static void USB2422_write_block(void) { + static unsigned char i2c0_buf[34]; unsigned char *dest = i2c0_buf; unsigned char *src; - unsigned char *base = (unsigned char *)&USB2422_shadow; + unsigned char *base = (unsigned char *)&config; for (src = base; src < base + 256; src += 32) { dest[0] = src - base; @@ -328,8 +337,7 @@ void USB2422_init() { } void USB2422_configure(){ - Usb2422 *pusb2422 = &USB2422_shadow; - memset(pusb2422, 0, sizeof(Usb2422)); + memset(&config, 0, sizeof(Usb2422_t)); uint16_t *serial_use = (uint16_t *)SERNAME; // Default to use SERNAME from this file uint8_t serial_length = sizeof(SERNAME) / sizeof(uint16_t); // Default to use SERNAME from this file @@ -354,26 +362,26 @@ void USB2422_configure(){ // } // configure Usb2422 registers - pusb2422->VID.reg = 0x04D8; // from Microchip 4/19/2018 - pusb2422->PID.reg = 0xEEC5; // from Microchip 4/19/2018 = Massdrop, Inc. USB Hub - pusb2422->DID.reg = 0x0101; // BCD 01.01 - pusb2422->CFG1.bit.SELF_BUS_PWR = 1; // self powered for now - pusb2422->CFG1.bit.HS_DISABLE = 1; // full or high speed - // pusb2422->CFG2.bit.COMPOUND = 0; // compound device - pusb2422->CFG3.bit.STRING_EN = 1; // strings enabled - // pusb2422->NRD.bit.PORT2_NR = 0; // MCU is non-removable - pusb2422->MAXPB.reg = 20; // 0mA - pusb2422->HCMCB.reg = 20; // 0mA - pusb2422->MFRSL.reg = sizeof(MFRNAME) / sizeof(uint16_t); - pusb2422->PRDSL.reg = sizeof(PRDNAME) / sizeof(uint16_t); - pusb2422->SERSL.reg = serial_length; - memcpy(pusb2422->MFRSTR, MFRNAME, sizeof(MFRNAME)); - memcpy(pusb2422->PRDSTR, PRDNAME, sizeof(PRDNAME)); - memcpy(pusb2422->SERSTR, serial_use, serial_length * sizeof(uint16_t)); - // pusb2422->BOOSTUP.bit.BOOST=3; //upstream port - // pusb2422->BOOSTDOWN.bit.BOOST1=0; // extra port - // pusb2422->BOOSTDOWN.bit.BOOST2=2; //MCU is close - pusb2422->STCD.bit.USB_ATTACH = 1; + config.VID.reg = USB2422_VENDOR_ID; + config.PID.reg = USB2422_PRODUCT_ID; + config.DID.reg = USB2422_DEVICE_VER; // BCD format, eg 01.01 + config.CFG1.bit.SELF_BUS_PWR = 1; // self powered for now + config.CFG1.bit.HS_DISABLE = 1; // full or high speed + // config.CFG2.bit.COMPOUND = 0; // compound device + config.CFG3.bit.STRING_EN = 1; // strings enabled + // config.NRD.bit.PORT2_NR = 0; // MCU is non-removable + config.MAXPB.reg = 20; // 0mA + config.HCMCB.reg = 20; // 0mA + config.MFRSL.reg = sizeof(MFRNAME) / sizeof(uint16_t); + config.PRDSL.reg = sizeof(PRDNAME) / sizeof(uint16_t); + config.SERSL.reg = serial_length; + memcpy(config.MFRSTR, MFRNAME, sizeof(MFRNAME)); + memcpy(config.PRDSTR, PRDNAME, sizeof(PRDNAME)); + memcpy(config.SERSTR, serial_use, serial_length * sizeof(uint16_t)); + // config.BOOSTUP.bit.BOOST=3; //upstream port + // config.BOOSTDOWN.bit.BOOST1=0; // extra port + // config.BOOSTDOWN.bit.BOOST2=2; //MCU is close + config.STCD.bit.USB_ATTACH = 1; USB2422_write_block(); } From 1f9fe71c8f8e1eef88f23a9dad2b89237d082ae3 Mon Sep 17 00:00:00 2001 From: zvecr Date: Fri, 15 Oct 2021 19:09:02 +0100 Subject: [PATCH 04/12] refactor --- drivers/usb2422.c | 84 +++++++++--------------- drivers/usb2422.h | 21 ++++++ tmk_core/protocol/arm_atsam/i2c_master.c | 19 ++++++ tmk_core/protocol/arm_atsam/i2c_master.h | 7 ++ 4 files changed, 77 insertions(+), 54 deletions(-) diff --git a/drivers/usb2422.c b/drivers/usb2422.c index 530067651848..2e4d7d3b8490 100644 --- a/drivers/usb2422.c +++ b/drivers/usb2422.c @@ -1,7 +1,22 @@ +/* Copyright 2021 QMK + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include #include "usb2422.h" #include "i2c_master.h" #include "wait.h" -#include /* -------- USB2422_VID : (USB2422L Offset: 0x00) (R/W 16) Vendor ID -------- */ typedef union { @@ -89,7 +104,6 @@ typedef union { uint8_t reg; /*!< Type used for register access */ } USB2422_PDS_Type; - /* -------- USB2422_PDB : (USB2422L Offset: 0x0B) (R/W 8) Port Diable for Bus-Powered Operation -------- */ typedef union { @@ -295,78 +309,40 @@ typedef struct { USB2422_STCD_Type STCD; /**< \brief Offset: 0xFF*/ } Usb2422_t; -#ifndef USB2422_ADDRESS -# define USB2422_ADDRESS 0x58 -#endif -#ifndef USB2422_VENDOR_ID -# define USB2422_VENDOR_ID 0x04D8 -#endif -#ifndef USB2422_PRODUCT_ID -# define USB2422_PRODUCT_ID 0xEEC5 -#endif -#ifndef USB2422_DEVICE_VER -# define USB2422_DEVICE_VER 0x0101 -#endif - static Usb2422_t config; -static const uint16_t MFRNAME[] = {'M', 'a', 's', 's', 'd', 'r', 'o', 'p', ' ', 'I', 'n', 'c', '.'}; // Massdrop Inc. -static const uint16_t PRDNAME[] = {'M', 'a', 's', 's', 'd', 'r', 'o', 'p', ' ', 'H', 'u', 'b'}; // Massdrop Hub -static const uint16_t SERNAME[] = {'U', 'n', 'a', 'v', 'a', 'i', 'l', 'a', 'b', 'l', 'e'}; // Unavailable - static void USB2422_write_block(void) { static unsigned char i2c0_buf[34]; - unsigned char *dest = i2c0_buf; - unsigned char *src; - unsigned char *base = (unsigned char *)&config; + unsigned char * dest = i2c0_buf; + unsigned char * src; + unsigned char * base = (unsigned char *)&config; for (src = base; src < base + 256; src += 32) { dest[0] = src - base; dest[1] = 32; memcpy(&dest[2], src, 32); - i2c0_transmit(USB2422_ADDRESS, dest, 34, 50000); - SERCOM0->I2CM.CTRLB.bit.CMD = 0x03; - while (SERCOM0->I2CM.SYNCBUSY.bit.SYSOP) {} + i2c_transmit(USB2422_ADDRESS, dest, 34, 50000); wait_us(100); } } void USB2422_init() { - //i2c_init(); - i2c0_init(); // IC2 clk must be high at USB2422 reset release time to signal SMB configuration + i2c_init(); // IC2 clk must be high at USB2422 reset release time to signal SMB configuration } -void USB2422_configure(){ - memset(&config, 0, sizeof(Usb2422_t)); +void USB2422_configure() { + static const uint16_t MFRNAME[] = USB2422_MANUFACTURER; + static const uint16_t PRDNAME[] = USB2422_PRODUCT; + static const uint16_t SERNAME[] = {'U', 'n', 'a', 'v', 'a', 'i', 'l', 'a', 'b', 'l', 'e'}; - uint16_t *serial_use = (uint16_t *)SERNAME; // Default to use SERNAME from this file - uint8_t serial_length = sizeof(SERNAME) / sizeof(uint16_t); // Default to use SERNAME from this file -// #ifndef MD_BOOTLOADER -// uint32_t serial_ptrloc = (uint32_t)&_srom - 4; -// #else // MD_BOOTLOADER -// uint32_t serial_ptrloc = (uint32_t)&_erom - 4; -// #endif // MD_BOOTLOADER -// uint32_t serial_address = *(uint32_t *)serial_ptrloc; // Address of bootloader's serial number if available - -// if (serial_address != 0xFFFFFFFF && serial_address < serial_ptrloc) // Check for factory programmed serial address -// { -// if ((serial_address & 0xFF) % 4 == 0) // Check alignment -// { -// serial_use = (uint16_t *)(serial_address); -// serial_length = 0; -// while ((*(serial_use + serial_length) > 32 && *(serial_use + serial_length) < 127) && serial_length < BOOTLOADER_SERIAL_MAX_SIZE) { -// serial_length++; -// DBGC(DC_USB_CONFIGURE_GET_SERIAL); -// } -// } -// } + memset(&config, 0, sizeof(Usb2422_t)); // configure Usb2422 registers config.VID.reg = USB2422_VENDOR_ID; config.PID.reg = USB2422_PRODUCT_ID; config.DID.reg = USB2422_DEVICE_VER; // BCD format, eg 01.01 - config.CFG1.bit.SELF_BUS_PWR = 1; // self powered for now - config.CFG1.bit.HS_DISABLE = 1; // full or high speed + config.CFG1.bit.SELF_BUS_PWR = 1; // self powered for now + config.CFG1.bit.HS_DISABLE = 1; // full or high speed // config.CFG2.bit.COMPOUND = 0; // compound device config.CFG3.bit.STRING_EN = 1; // strings enabled // config.NRD.bit.PORT2_NR = 0; // MCU is non-removable @@ -374,10 +350,10 @@ void USB2422_configure(){ config.HCMCB.reg = 20; // 0mA config.MFRSL.reg = sizeof(MFRNAME) / sizeof(uint16_t); config.PRDSL.reg = sizeof(PRDNAME) / sizeof(uint16_t); - config.SERSL.reg = serial_length; + config.SERSL.reg = sizeof(SERNAME) / sizeof(uint16_t);; memcpy(config.MFRSTR, MFRNAME, sizeof(MFRNAME)); memcpy(config.PRDSTR, PRDNAME, sizeof(PRDNAME)); - memcpy(config.SERSTR, serial_use, serial_length * sizeof(uint16_t)); + memcpy(config.SERSTR, SERNAME, sizeof(SERNAME)); // config.BOOSTUP.bit.BOOST=3; //upstream port // config.BOOSTDOWN.bit.BOOST1=0; // extra port // config.BOOSTDOWN.bit.BOOST2=2; //MCU is close diff --git a/drivers/usb2422.h b/drivers/usb2422.h index fa0d2d248ee3..36785f2def9f 100644 --- a/drivers/usb2422.h +++ b/drivers/usb2422.h @@ -15,6 +15,27 @@ */ #pragma once +#ifndef USB2422_ADDRESS +# define USB2422_ADDRESS 0x58 +#endif + +#ifndef USB2422_VENDOR_ID +# define USB2422_VENDOR_ID 0x04D8 +#endif +#ifndef USB2422_PRODUCT_ID +# define USB2422_PRODUCT_ID 0xEEC5 +#endif +#ifndef USB2422_DEVICE_VER +# define USB2422_DEVICE_VER 0x0101 +#endif + +#ifndef USB2422_MANUFACTURER +# define USB2422_MANUFACTURER { 'Q', 'M', 'K' } +#endif +#ifndef USB2422_PRODUCT +# define USB2422_PRODUCT { 'Q', 'M', 'K', ' ', 'H', 'u', 'b' } +#endif + void USB2422_init(void); void USB2422_configure(void); diff --git a/tmk_core/protocol/arm_atsam/i2c_master.c b/tmk_core/protocol/arm_atsam/i2c_master.c index dda2f85b0019..35e422e8d173 100644 --- a/tmk_core/protocol/arm_atsam/i2c_master.c +++ b/tmk_core/protocol/arm_atsam/i2c_master.c @@ -564,4 +564,23 @@ uint8_t i2c_led_q_run(void) { return 1; } + +__attribute__((weak)) void i2c_init(void) { + static bool is_initialised = false; + if (!is_initialised) { + is_initialised = true; + + i2c0_init(); + } +} + +i2c_status_t i2c_transmit(uint8_t address, const uint8_t* data, uint16_t length, uint16_t timeout) { + uint8_t ret = i2c0_transmit(address, (uint8_t*)data, length, timeout); + SERCOM0->I2CM.CTRLB.bit.CMD = 0x03; + while (SERCOM0->I2CM.SYNCBUSY.bit.SYSOP) { + DBGC(DC_USB_WRITE2422_BLOCK_SYNC_SYSOP); + } + return ret; +} + #endif // !defined(MD_BOOTLOADER) && defined(RGB_MATRIX_ENABLE) diff --git a/tmk_core/protocol/arm_atsam/i2c_master.h b/tmk_core/protocol/arm_atsam/i2c_master.h index 68773f213fbe..5545bc7a546a 100644 --- a/tmk_core/protocol/arm_atsam/i2c_master.h +++ b/tmk_core/protocol/arm_atsam/i2c_master.h @@ -101,4 +101,11 @@ void i2c0_init(void); uint8_t i2c0_transmit(uint8_t address, uint8_t *data, uint16_t length, uint16_t timeout); void i2c0_stop(void); + +// Terrible interface compatiblity... +typedef int16_t i2c_status_t; + +void i2c_init(void); +i2c_status_t i2c_transmit(uint8_t address, const uint8_t* data, uint16_t length, uint16_t timeout); + #endif // _I2C_MASTER_H_ From 749bc635460eb2e7f34b435b243616a96bf837a3 Mon Sep 17 00:00:00 2001 From: zvecr Date: Fri, 15 Oct 2021 19:17:13 +0100 Subject: [PATCH 05/12] Add back ALT/CTRL config --- drivers/usb2422.h | 6 +++--- keyboards/massdrop/alt/config.h | 8 ++++++++ keyboards/massdrop/ctrl/config.h | 8 ++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/drivers/usb2422.h b/drivers/usb2422.h index 36785f2def9f..4df72cf76a82 100644 --- a/drivers/usb2422.h +++ b/drivers/usb2422.h @@ -20,13 +20,13 @@ #endif #ifndef USB2422_VENDOR_ID -# define USB2422_VENDOR_ID 0x04D8 +# define USB2422_VENDOR_ID 0xFEED #endif #ifndef USB2422_PRODUCT_ID -# define USB2422_PRODUCT_ID 0xEEC5 +# define USB2422_PRODUCT_ID 0x0001 #endif #ifndef USB2422_DEVICE_VER -# define USB2422_DEVICE_VER 0x0101 +# define USB2422_DEVICE_VER 0x0001 #endif #ifndef USB2422_MANUFACTURER diff --git a/keyboards/massdrop/alt/config.h b/keyboards/massdrop/alt/config.h index d28094c49239..a28164f8018b 100644 --- a/keyboards/massdrop/alt/config.h +++ b/keyboards/massdrop/alt/config.h @@ -94,6 +94,14 @@ along with this program. If not, see . #define DEBUG_BOOT_TRACING_PORT PB #define DEBUG_BOOT_TRACING_PIN 23 +/* USB2422 config */ +#define USB2422_ADDRESS 0x58 +#define USB2422_VENDOR_ID 0x04D8 +#define USB2422_PRODUCT_ID 0xEEC5 +#define USB2422_DEVICE_VER 0x0101 +#define USB2422_MANUFACTURER {'M', 'a', 's', 's', 'd', 'r', 'o', 'p', ' ', 'I', 'n', 'c', '.'} +#define USB2422_PRODUCT {'M', 'a', 's', 's', 'd', 'r', 'o', 'p', ' ', 'H', 'u', 'b'} + /* Debounce reduces chatter (unintended double-presses) - set 0 if debouncing is not needed */ #define DEBOUNCE 5 diff --git a/keyboards/massdrop/ctrl/config.h b/keyboards/massdrop/ctrl/config.h index 3dbd9b887700..1cd6f0de9e68 100644 --- a/keyboards/massdrop/ctrl/config.h +++ b/keyboards/massdrop/ctrl/config.h @@ -93,6 +93,14 @@ along with this program. If not, see . #define DEBUG_BOOT_TRACING_PORT PB #define DEBUG_BOOT_TRACING_PIN 23 +/* USB2422 config */ +#define USB2422_ADDRESS 0x58 +#define USB2422_VENDOR_ID 0x04D8 +#define USB2422_PRODUCT_ID 0xEEC5 +#define USB2422_DEVICE_VER 0x0101 +#define USB2422_MANUFACTURER {'M', 'a', 's', 's', 'd', 'r', 'o', 'p', ' ', 'I', 'n', 'c', '.'} +#define USB2422_PRODUCT {'M', 'a', 's', 's', 'd', 'r', 'o', 'p', ' ', 'H', 'u', 'b'} + /* Debounce reduces chatter (unintended double-presses) - set 0 if debouncing is not needed */ #define DEBOUNCE 5 From afecbd0a395ac0430461b881fab1b8e073cd0c09 Mon Sep 17 00:00:00 2001 From: zvecr Date: Fri, 15 Oct 2021 19:24:16 +0100 Subject: [PATCH 06/12] clang --- drivers/usb2422.c | 5 +++-- drivers/usb2422.h | 6 ++++-- tmk_core/protocol/arm_atsam/i2c_master.c | 4 ++-- tmk_core/protocol/arm_atsam/i2c_master.h | 3 +-- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/drivers/usb2422.c b/drivers/usb2422.c index 2e4d7d3b8490..158c92f4aa8e 100644 --- a/drivers/usb2422.c +++ b/drivers/usb2422.c @@ -327,7 +327,7 @@ static void USB2422_write_block(void) { } void USB2422_init() { - i2c_init(); // IC2 clk must be high at USB2422 reset release time to signal SMB configuration + i2c_init(); // IC2 clk must be high at USB2422 reset release time to signal SMB configuration } void USB2422_configure() { @@ -350,7 +350,8 @@ void USB2422_configure() { config.HCMCB.reg = 20; // 0mA config.MFRSL.reg = sizeof(MFRNAME) / sizeof(uint16_t); config.PRDSL.reg = sizeof(PRDNAME) / sizeof(uint16_t); - config.SERSL.reg = sizeof(SERNAME) / sizeof(uint16_t);; + config.SERSL.reg = sizeof(SERNAME) / sizeof(uint16_t); + ; memcpy(config.MFRSTR, MFRNAME, sizeof(MFRNAME)); memcpy(config.PRDSTR, PRDNAME, sizeof(PRDNAME)); memcpy(config.SERSTR, SERNAME, sizeof(SERNAME)); diff --git a/drivers/usb2422.h b/drivers/usb2422.h index 4df72cf76a82..78bff2f4881c 100644 --- a/drivers/usb2422.h +++ b/drivers/usb2422.h @@ -30,10 +30,12 @@ #endif #ifndef USB2422_MANUFACTURER -# define USB2422_MANUFACTURER { 'Q', 'M', 'K' } +# define USB2422_MANUFACTURER \ + { 'Q', 'M', 'K' } #endif #ifndef USB2422_PRODUCT -# define USB2422_PRODUCT { 'Q', 'M', 'K', ' ', 'H', 'u', 'b' } +# define USB2422_PRODUCT \ + { 'Q', 'M', 'K', ' ', 'H', 'u', 'b' } #endif void USB2422_init(void); diff --git a/tmk_core/protocol/arm_atsam/i2c_master.c b/tmk_core/protocol/arm_atsam/i2c_master.c index 35e422e8d173..c78894030d0e 100644 --- a/tmk_core/protocol/arm_atsam/i2c_master.c +++ b/tmk_core/protocol/arm_atsam/i2c_master.c @@ -574,8 +574,8 @@ __attribute__((weak)) void i2c_init(void) { } } -i2c_status_t i2c_transmit(uint8_t address, const uint8_t* data, uint16_t length, uint16_t timeout) { - uint8_t ret = i2c0_transmit(address, (uint8_t*)data, length, timeout); +i2c_status_t i2c_transmit(uint8_t address, const uint8_t *data, uint16_t length, uint16_t timeout) { + uint8_t ret = i2c0_transmit(address, (uint8_t *)data, length, timeout); SERCOM0->I2CM.CTRLB.bit.CMD = 0x03; while (SERCOM0->I2CM.SYNCBUSY.bit.SYSOP) { DBGC(DC_USB_WRITE2422_BLOCK_SYNC_SYSOP); diff --git a/tmk_core/protocol/arm_atsam/i2c_master.h b/tmk_core/protocol/arm_atsam/i2c_master.h index 5545bc7a546a..4f225d1fbc99 100644 --- a/tmk_core/protocol/arm_atsam/i2c_master.h +++ b/tmk_core/protocol/arm_atsam/i2c_master.h @@ -101,11 +101,10 @@ void i2c0_init(void); uint8_t i2c0_transmit(uint8_t address, uint8_t *data, uint16_t length, uint16_t timeout); void i2c0_stop(void); - // Terrible interface compatiblity... typedef int16_t i2c_status_t; void i2c_init(void); -i2c_status_t i2c_transmit(uint8_t address, const uint8_t* data, uint16_t length, uint16_t timeout); +i2c_status_t i2c_transmit(uint8_t address, const uint8_t *data, uint16_t length, uint16_t timeout); #endif // _I2C_MASTER_H_ From c5cae9ed1f76563ad80f0616b72c67b813b3cb57 Mon Sep 17 00:00:00 2001 From: zvecr Date: Fri, 15 Oct 2021 20:15:51 +0100 Subject: [PATCH 07/12] Use strings --- drivers/usb2422.c | 23 +++++++++++++---------- drivers/usb2422.h | 6 ++---- keyboards/massdrop/alt/config.h | 4 ++-- keyboards/massdrop/ctrl/config.h | 4 ++-- 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/drivers/usb2422.c b/drivers/usb2422.c index 158c92f4aa8e..ec256ac4b2eb 100644 --- a/drivers/usb2422.c +++ b/drivers/usb2422.c @@ -311,6 +311,12 @@ typedef struct { static Usb2422_t config; +static void USB2422_strcpy(const char* str, USB2422_MFRSTR_Type* dest, uint8_t len){ + for(uint8_t i = 0; i < len; i++){ + dest[i] = str[i]; + } +} + static void USB2422_write_block(void) { static unsigned char i2c0_buf[34]; unsigned char * dest = i2c0_buf; @@ -331,9 +337,7 @@ void USB2422_init() { } void USB2422_configure() { - static const uint16_t MFRNAME[] = USB2422_MANUFACTURER; - static const uint16_t PRDNAME[] = USB2422_PRODUCT; - static const uint16_t SERNAME[] = {'U', 'n', 'a', 'v', 'a', 'i', 'l', 'a', 'b', 'l', 'e'}; + static const char SERNAME[] = "Unavailable"; memset(&config, 0, sizeof(Usb2422_t)); @@ -348,13 +352,12 @@ void USB2422_configure() { // config.NRD.bit.PORT2_NR = 0; // MCU is non-removable config.MAXPB.reg = 20; // 0mA config.HCMCB.reg = 20; // 0mA - config.MFRSL.reg = sizeof(MFRNAME) / sizeof(uint16_t); - config.PRDSL.reg = sizeof(PRDNAME) / sizeof(uint16_t); - config.SERSL.reg = sizeof(SERNAME) / sizeof(uint16_t); - ; - memcpy(config.MFRSTR, MFRNAME, sizeof(MFRNAME)); - memcpy(config.PRDSTR, PRDNAME, sizeof(PRDNAME)); - memcpy(config.SERSTR, SERNAME, sizeof(SERNAME)); + config.MFRSL.reg = sizeof(USB2422_MANUFACTURER); + config.PRDSL.reg = sizeof(USB2422_PRODUCT); + config.SERSL.reg = sizeof(SERNAME); + USB2422_strcpy(USB2422_MANUFACTURER, config.MFRSTR, sizeof(USB2422_MANUFACTURER)); + USB2422_strcpy(USB2422_PRODUCT, config.PRDSTR, sizeof(USB2422_PRODUCT)); + USB2422_strcpy(SERNAME, config.SERSTR, sizeof(SERNAME)); // config.BOOSTUP.bit.BOOST=3; //upstream port // config.BOOSTDOWN.bit.BOOST1=0; // extra port // config.BOOSTDOWN.bit.BOOST2=2; //MCU is close diff --git a/drivers/usb2422.h b/drivers/usb2422.h index 78bff2f4881c..fec7e29ef958 100644 --- a/drivers/usb2422.h +++ b/drivers/usb2422.h @@ -30,12 +30,10 @@ #endif #ifndef USB2422_MANUFACTURER -# define USB2422_MANUFACTURER \ - { 'Q', 'M', 'K' } +# define USB2422_MANUFACTURER "QMK" #endif #ifndef USB2422_PRODUCT -# define USB2422_PRODUCT \ - { 'Q', 'M', 'K', ' ', 'H', 'u', 'b' } +# define USB2422_PRODUCT "QMK Hub" #endif void USB2422_init(void); diff --git a/keyboards/massdrop/alt/config.h b/keyboards/massdrop/alt/config.h index a28164f8018b..1f7f6ef80482 100644 --- a/keyboards/massdrop/alt/config.h +++ b/keyboards/massdrop/alt/config.h @@ -99,8 +99,8 @@ along with this program. If not, see . #define USB2422_VENDOR_ID 0x04D8 #define USB2422_PRODUCT_ID 0xEEC5 #define USB2422_DEVICE_VER 0x0101 -#define USB2422_MANUFACTURER {'M', 'a', 's', 's', 'd', 'r', 'o', 'p', ' ', 'I', 'n', 'c', '.'} -#define USB2422_PRODUCT {'M', 'a', 's', 's', 'd', 'r', 'o', 'p', ' ', 'H', 'u', 'b'} +#define USB2422_MANUFACTURER "Massdrop Inc." +#define USB2422_PRODUCT "Massdrop Hub" /* Debounce reduces chatter (unintended double-presses) - set 0 if debouncing is not needed */ #define DEBOUNCE 5 diff --git a/keyboards/massdrop/ctrl/config.h b/keyboards/massdrop/ctrl/config.h index 1cd6f0de9e68..e54dea6c466b 100644 --- a/keyboards/massdrop/ctrl/config.h +++ b/keyboards/massdrop/ctrl/config.h @@ -98,8 +98,8 @@ along with this program. If not, see . #define USB2422_VENDOR_ID 0x04D8 #define USB2422_PRODUCT_ID 0xEEC5 #define USB2422_DEVICE_VER 0x0101 -#define USB2422_MANUFACTURER {'M', 'a', 's', 's', 'd', 'r', 'o', 'p', ' ', 'I', 'n', 'c', '.'} -#define USB2422_PRODUCT {'M', 'a', 's', 's', 'd', 'r', 'o', 'p', ' ', 'H', 'u', 'b'} +#define USB2422_MANUFACTURER "Massdrop Inc." +#define USB2422_PRODUCT "Massdrop Hub" /* Debounce reduces chatter (unintended double-presses) - set 0 if debouncing is not needed */ #define DEBOUNCE 5 From bfad3d03541b0dc86a4c8d6db451559b2efff758 Mon Sep 17 00:00:00 2001 From: zvecr Date: Fri, 15 Oct 2021 20:19:30 +0100 Subject: [PATCH 08/12] clang --- drivers/usb2422.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/drivers/usb2422.c b/drivers/usb2422.c index ec256ac4b2eb..d0ddc618fafe 100644 --- a/drivers/usb2422.c +++ b/drivers/usb2422.c @@ -311,17 +311,17 @@ typedef struct { static Usb2422_t config; -static void USB2422_strcpy(const char* str, USB2422_MFRSTR_Type* dest, uint8_t len){ - for(uint8_t i = 0; i < len; i++){ +static void USB2422_strcpy(const char* str, USB2422_MFRSTR_Type* dest, uint8_t len) { + for (uint8_t i = 0; i < len; i++) { dest[i] = str[i]; } } static void USB2422_write_block(void) { static unsigned char i2c0_buf[34]; - unsigned char * dest = i2c0_buf; - unsigned char * src; - unsigned char * base = (unsigned char *)&config; + unsigned char* dest = i2c0_buf; + unsigned char* src; + unsigned char* base = (unsigned char*)&config; for (src = base; src < base + 256; src += 32) { dest[0] = src - base; From 8a909634e4aa85c46f98cca182dca40e04fff3b8 Mon Sep 17 00:00:00 2001 From: zvecr Date: Fri, 15 Oct 2021 21:57:23 +0100 Subject: [PATCH 09/12] reset && active --- drivers/usb2422.c | 23 +++++++++++++++++++++++ drivers/usb2422.h | 6 ++++++ keyboards/massdrop/alt/config.h | 1 + keyboards/massdrop/ctrl/config.h | 1 + tmk_core/protocol/arm_atsam/usb/usb_hub.c | 4 +--- tmk_core/protocol/arm_atsam/usb/usb_hub.h | 3 --- 6 files changed, 32 insertions(+), 6 deletions(-) diff --git a/drivers/usb2422.c b/drivers/usb2422.c index d0ddc618fafe..630ca7888455 100644 --- a/drivers/usb2422.c +++ b/drivers/usb2422.c @@ -333,6 +333,13 @@ static void USB2422_write_block(void) { } void USB2422_init() { +#ifdef USB2422_RESET_PIN + setPinOutput(USB2422_RESET_PIN); +#endif +#ifdef USB2422_ACTIVE_PIN + setPinInput(USB2422_ACTIVE_PIN); +#endif + i2c_init(); // IC2 clk must be high at USB2422 reset release time to signal SMB configuration } @@ -365,3 +372,19 @@ void USB2422_configure() { USB2422_write_block(); } + +void USB2422_reset() { +#ifdef USB2422_RESET_PIN + writePinLow(USB2422_RESET_PIN); + wait_us(2); + writePinHigh(USB2422_RESET_PIN); +#endif +} + +bool USB2422_active() { +#ifdef USB2422_ACTIVE_PIN + return read_pin(USB2422_ACTIVE_PIN); +#else + return 1; +#endif +} diff --git a/drivers/usb2422.h b/drivers/usb2422.h index fec7e29ef958..84318ae945b9 100644 --- a/drivers/usb2422.h +++ b/drivers/usb2422.h @@ -15,6 +15,8 @@ */ #pragma once +#include + #ifndef USB2422_ADDRESS # define USB2422_ADDRESS 0x58 #endif @@ -39,3 +41,7 @@ void USB2422_init(void); void USB2422_configure(void); + +void USB2422_reset(void); + +bool USB2422_active(void); diff --git a/keyboards/massdrop/alt/config.h b/keyboards/massdrop/alt/config.h index 1f7f6ef80482..6ae96bc77c3c 100644 --- a/keyboards/massdrop/alt/config.h +++ b/keyboards/massdrop/alt/config.h @@ -101,6 +101,7 @@ along with this program. If not, see . #define USB2422_DEVICE_VER 0x0101 #define USB2422_MANUFACTURER "Massdrop Inc." #define USB2422_PRODUCT "Massdrop Hub" +#define USB2422_ACTIVE_PIN A18 /* Debounce reduces chatter (unintended double-presses) - set 0 if debouncing is not needed */ #define DEBOUNCE 5 diff --git a/keyboards/massdrop/ctrl/config.h b/keyboards/massdrop/ctrl/config.h index e54dea6c466b..044a5412c062 100644 --- a/keyboards/massdrop/ctrl/config.h +++ b/keyboards/massdrop/ctrl/config.h @@ -100,6 +100,7 @@ along with this program. If not, see . #define USB2422_DEVICE_VER 0x0101 #define USB2422_MANUFACTURER "Massdrop Inc." #define USB2422_PRODUCT "Massdrop Hub" +#define USB2422_ACTIVE_PIN A18 /* Debounce reduces chatter (unintended double-presses) - set 0 if debouncing is not needed */ #define DEBOUNCE 5 diff --git a/tmk_core/protocol/arm_atsam/usb/usb_hub.c b/tmk_core/protocol/arm_atsam/usb/usb_hub.c index 23ee44a1bb92..c5fd284aab76 100644 --- a/tmk_core/protocol/arm_atsam/usb/usb_hub.c +++ b/tmk_core/protocol/arm_atsam/usb/usb_hub.c @@ -109,8 +109,6 @@ void USB_Hub_init(void) { pusb->DEVICE.QOSCTRL.bit.DQOS = 2; pusb->DEVICE.QOSCTRL.bit.CQOS = 2; - pport->Group[USB2422_HUB_ACTIVE_GROUP].PINCFG[USB2422_HUB_ACTIVE_PIN].bit.INEN = 1; - USB2422_init(); sr_exp_data.bit.HUB_CONNECT = 1; // connect signal @@ -152,7 +150,7 @@ void USB_configure(void) { DBGC(DC_USB_CONFIGURE_COMPLETE); } -uint16_t USB_active(void) { return (PORT->Group[USB2422_HUB_ACTIVE_GROUP].IN.reg & (1 << USB2422_HUB_ACTIVE_PIN)) != 0; } +uint16_t USB_active(void) { return USB2422_active(); } void USB_set_host_by_voltage(void) { // UP is upstream device (HOST) diff --git a/tmk_core/protocol/arm_atsam/usb/usb_hub.h b/tmk_core/protocol/arm_atsam/usb/usb_hub.h index 386b28dbed3e..76b1e0a32633 100644 --- a/tmk_core/protocol/arm_atsam/usb/usb_hub.h +++ b/tmk_core/protocol/arm_atsam/usb/usb_hub.h @@ -20,9 +20,6 @@ along with this program. If not, see . #define REV_USB2422 0x100 -#define USB2422_HUB_ACTIVE_GROUP 0 // PA -#define USB2422_HUB_ACTIVE_PIN 18 // 18 - #define PORT_DETECT_RETRY_INTERVAL 2000 #define USB_EXTRA_ADC_THRESHOLD 900 From 2947dc27dfc27b485bb4e593c0ad0545beebd30e Mon Sep 17 00:00:00 2001 From: zvecr Date: Fri, 15 Oct 2021 22:10:57 +0100 Subject: [PATCH 10/12] fix use of gpio --- drivers/usb2422.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/usb2422.c b/drivers/usb2422.c index 630ca7888455..1992b5d9f939 100644 --- a/drivers/usb2422.c +++ b/drivers/usb2422.c @@ -17,6 +17,7 @@ #include "usb2422.h" #include "i2c_master.h" #include "wait.h" +#include "gpio.h" /* -------- USB2422_VID : (USB2422L Offset: 0x00) (R/W 16) Vendor ID -------- */ typedef union { @@ -383,7 +384,7 @@ void USB2422_reset() { bool USB2422_active() { #ifdef USB2422_ACTIVE_PIN - return read_pin(USB2422_ACTIVE_PIN); + return readPin(USB2422_ACTIVE_PIN); #else return 1; #endif From 93df55354c75581c62e5eab4003386c5b9f0b325 Mon Sep 17 00:00:00 2001 From: zvecr Date: Sat, 16 Oct 2021 00:01:03 +0100 Subject: [PATCH 11/12] tidy --- drivers/usb2422.c | 17 ++++++++++++++--- drivers/usb2422.h | 12 ++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/drivers/usb2422.c b/drivers/usb2422.c index 1992b5d9f939..62b919093b96 100644 --- a/drivers/usb2422.c +++ b/drivers/usb2422.c @@ -310,19 +310,28 @@ typedef struct { USB2422_STCD_Type STCD; /**< \brief Offset: 0xFF*/ } Usb2422_t; +// *************************************************************** + static Usb2422_t config; +// *************************************************************** + +/** \brief Handle the conversion to allow simple strings + */ static void USB2422_strcpy(const char* str, USB2422_MFRSTR_Type* dest, uint8_t len) { for (uint8_t i = 0; i < len; i++) { dest[i] = str[i]; } } +/** \brief Handle the conversion to allow simple strings + */ static void USB2422_write_block(void) { static unsigned char i2c0_buf[34]; - unsigned char* dest = i2c0_buf; - unsigned char* src; - unsigned char* base = (unsigned char*)&config; + + unsigned char* dest = i2c0_buf; + unsigned char* src; + unsigned char* base = (unsigned char*)&config; for (src = base; src < base + 256; src += 32) { dest[0] = src - base; @@ -333,6 +342,8 @@ static void USB2422_write_block(void) { } } +// *************************************************************** + void USB2422_init() { #ifdef USB2422_RESET_PIN setPinOutput(USB2422_RESET_PIN); diff --git a/drivers/usb2422.h b/drivers/usb2422.h index 84318ae945b9..2e435b02bc97 100644 --- a/drivers/usb2422.h +++ b/drivers/usb2422.h @@ -38,10 +38,22 @@ # define USB2422_PRODUCT "QMK Hub" #endif +/** \brief Initialises the dependent subsystems */ void USB2422_init(void); +/** \brief Push configuration to the USB2422 device */ void USB2422_configure(void); +/** \brief Reset the chip (RESET_N) + * + * NOTE: + * Depends on a valid USB2422_RESET_PIN configuration + */ void USB2422_reset(void); +/** \brief Indicates the USB state of the hub (SUSP_IND) + * + * NOTE: + * Depends on a valid USB2422_ACTIVE_PIN configuration + */ bool USB2422_active(void); From 146f0be4b228db2cdc048610c8c830bdd1034ab5 Mon Sep 17 00:00:00 2001 From: zvecr Date: Sun, 17 Oct 2021 18:45:59 +0100 Subject: [PATCH 12/12] Fix return code --- tmk_core/protocol/arm_atsam/i2c_master.c | 2 +- tmk_core/protocol/arm_atsam/i2c_master.h | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/tmk_core/protocol/arm_atsam/i2c_master.c b/tmk_core/protocol/arm_atsam/i2c_master.c index c78894030d0e..af046625f433 100644 --- a/tmk_core/protocol/arm_atsam/i2c_master.c +++ b/tmk_core/protocol/arm_atsam/i2c_master.c @@ -580,7 +580,7 @@ i2c_status_t i2c_transmit(uint8_t address, const uint8_t *data, uint16_t length, while (SERCOM0->I2CM.SYNCBUSY.bit.SYSOP) { DBGC(DC_USB_WRITE2422_BLOCK_SYNC_SYSOP); } - return ret; + return ret ? I2C_STATUS_SUCCESS : I2C_STATUS_ERROR; } #endif // !defined(MD_BOOTLOADER) && defined(RGB_MATRIX_ENABLE) diff --git a/tmk_core/protocol/arm_atsam/i2c_master.h b/tmk_core/protocol/arm_atsam/i2c_master.h index 4f225d1fbc99..e11235d4476a 100644 --- a/tmk_core/protocol/arm_atsam/i2c_master.h +++ b/tmk_core/protocol/arm_atsam/i2c_master.h @@ -102,6 +102,9 @@ uint8_t i2c0_transmit(uint8_t address, uint8_t *data, uint16_t length, uint16_t void i2c0_stop(void); // Terrible interface compatiblity... +#define I2C_STATUS_SUCCESS (0) +#define I2C_STATUS_ERROR (-1) + typedef int16_t i2c_status_t; void i2c_init(void);