summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhathach <[email protected]>2023-09-09 16:50:16 +0700
committerhathach <[email protected]>2023-09-09 16:50:16 +0700
commite32c1e08bd701b5dda7d081b10ba3262e2061326 (patch)
tree244838d1f92b1cdf81a59aed6296673607e9dd79
parent605ad73ec02033aa6b66e6899e232537ca2354bb (diff)
both samd21/d51 works perfectly with max3421e
-rw-r--r--hw/bsp/samd21/boards/metro_m0_express/board.h16
-rw-r--r--hw/bsp/samd21/family.c226
-rw-r--r--hw/bsp/samd21/family.cmake1
-rw-r--r--hw/bsp/samd51/boards/metro_m4_express/board.h22
-rw-r--r--hw/bsp/samd51/family.c19
5 files changed, 232 insertions, 52 deletions
diff --git a/hw/bsp/samd21/boards/metro_m0_express/board.h b/hw/bsp/samd21/boards/metro_m0_express/board.h
index d26aae53c..6dd53e901 100644
--- a/hw/bsp/samd21/boards/metro_m0_express/board.h
+++ b/hw/bsp/samd21/boards/metro_m0_express/board.h
@@ -43,6 +43,22 @@
#define UART_RX_PIN 4
#define UART_TX_PIN 5
+// SPI for USB host shield
+#define MAX3421_SERCOM_ID 4 // SERCOM4
+#define MAX3421_SERCOM_FUNCTION 3 // function D (Sercom Alt)
+
+#define MAX3421_SCK_PIN (32+11)
+#define MAX3421_MOSI_PIN (32+10)
+#define MAX3421_MISO_PIN 12
+#define MAX3421_TX_PAD 1 // MOSI = PAD_2, SCK = PAD_3
+#define MAX3421_RX_PAD 0 // MISO = PAD_2
+
+#define MAX3421_CS_PIN 18 // D10
+
+#define MAX3421_INTR_PIN 7 // D9
+#define MAX3421_INTR_EIC_ID 7 // EIC7
+
+
#ifdef __cplusplus
}
#endif
diff --git a/hw/bsp/samd21/family.c b/hw/bsp/samd21/family.c
index 2121e028a..cfe66b8dc 100644
--- a/hw/bsp/samd21/family.c
+++ b/hw/bsp/samd21/family.c
@@ -47,30 +47,34 @@
#endif
//--------------------------------------------------------------------+
+// MACRO TYPEDEF CONSTANT ENUM DECLARATION
+//--------------------------------------------------------------------+
+
+/* Referenced GCLKs, should be initialized firstly */
+#define _GCLK_INIT_1ST (1 << 0 | 1 << 1)
+
+/* Not referenced GCLKs, initialized last */
+#define _GCLK_INIT_LAST (~_GCLK_INIT_1ST)
+
+//--------------------------------------------------------------------+
// Forward USB interrupt events to TinyUSB IRQ Handler
//--------------------------------------------------------------------+
-void USB_Handler(void)
-{
+void USB_Handler(void) {
tud_int_handler(0);
}
//--------------------------------------------------------------------+
-// UART support
+// Implementation
//--------------------------------------------------------------------+
static void uart_init(void);
-//--------------------------------------------------------------------+
-// MACRO TYPEDEF CONSTANT ENUM DECLARATION
-//--------------------------------------------------------------------+
-
-/* Referenced GCLKs, should be initialized firstly */
-#define _GCLK_INIT_1ST (1 << 0 | 1 << 1)
+#if CFG_TUH_ENABLED && defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
+#define MAX3421_SERCOM TU_XSTRCAT(SERCOM, MAX3421_SERCOM_ID)
-/* Not referenced GCLKs, initialized last */
-#define _GCLK_INIT_LAST (~_GCLK_INIT_1ST)
+static void max3421_init(void);
+#endif
-void board_init(void)
-{
+void board_init(void) {
// Clock init ( follow hpl_init.c )
hri_nvmctrl_set_CTRLB_RWS_bf(NVMCTRL, 2);
@@ -85,7 +89,7 @@ void board_init(void)
// Update SystemCoreClock since it is hard coded with asf4 and not correct
// Init 1ms tick timer (samd SystemCoreClock may not correct)
SystemCoreClock = CONF_CPU_FREQUENCY;
-#if CFG_TUSB_OS == OPT_OS_NONE
+#if CFG_TUSB_OS == OPT_OS_NONE
SysTick_Config(CONF_CPU_FREQUENCY / 1000);
#endif
@@ -103,7 +107,7 @@ void board_init(void)
uart_init();
-#if CFG_TUSB_OS == OPT_OS_FREERTOS
+#if CFG_TUSB_OS == OPT_OS_FREERTOS
// If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher )
NVIC_SetPriority(USB_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY);
#endif
@@ -134,22 +138,24 @@ void board_init(void)
gpio_set_pin_function(PIN_PA19, PINMUX_PA19F_TCC0_WO3);
_gclk_enable_channel(TCC0_GCLK_ID, GCLK_CLKCTRL_GEN_GCLK0_Val);
+
+#if CFG_TUH_ENABLED && defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
+ max3421_init();
+#endif
}
//--------------------------------------------------------------------+
// Board porting API
//--------------------------------------------------------------------+
-void board_led_write(bool state)
-{
- (void)state;
+void board_led_write(bool state) {
+ (void) state;
#ifdef LED_PIN
- gpio_set_pin_level(LED_PIN, state ? LED_STATE_ON : (1-LED_STATE_ON));
+ gpio_set_pin_level(LED_PIN, state ? LED_STATE_ON : (1 - LED_STATE_ON));
#endif
}
-uint32_t board_button_read(void)
-{
+uint32_t board_button_read(void) {
#ifdef BUTTON_PIN
return BUTTON_STATE_ACTIVE == gpio_get_pin_level(BUTTON_PIN);
#else
@@ -227,33 +233,185 @@ int board_uart_write(void const * buf, int len)
}
#else // ! defined(UART_SERCOM)
-static void uart_init(void)
-{
+static void uart_init(void) {
}
-int board_uart_read(uint8_t* buf, int len)
-{
- (void) buf; (void) len;
+int board_uart_read(uint8_t *buf, int len) {
+ (void) buf;
+ (void) len;
return 0;
}
-int board_uart_write(void const * buf, int len)
-{
- (void) buf; (void) len;
+int board_uart_write(void const *buf, int len) {
+ (void) buf;
+ (void) len;
return 0;
}
+
#endif
-#if CFG_TUSB_OS == OPT_OS_NONE
+#if CFG_TUSB_OS == OPT_OS_NONE
volatile uint32_t system_ticks = 0;
-void SysTick_Handler (void)
-{
+
+void SysTick_Handler(void) {
system_ticks++;
}
-uint32_t board_millis(void)
-{
+uint32_t board_millis(void) {
return system_ticks;
}
+
+//--------------------------------------------------------------------+
+//
+//--------------------------------------------------------------------+
+#if CFG_TUH_ENABLED && defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
+
+static void max3421_init(void) {
+ //------------- SPI Init -------------//
+ uint32_t const baudrate = 4000000u;
+
+ // Enable the APB clock for SERCOM
+ PM->APBCMASK.reg |= 1u << (PM_APBCMASK_SERCOM0_Pos + MAX3421_SERCOM_ID);
+
+ // Configure GCLK for SERCOM
+// GCLK->CLKCTRL.reg = GCLK_CLKCTRL_ID_SERCOM4_CORE | GCLK_CLKCTRL_GEN_GCLK0 | GCLK_CLKCTRL_CLKEN;
+ GCLK->CLKCTRL.reg = GCLK_CLKCTRL_ID(GCLK_CLKCTRL_ID_SERCOM0_CORE_Val+MAX3421_SERCOM_ID) |
+ GCLK_CLKCTRL_GEN_GCLK0 | GCLK_CLKCTRL_CLKEN;
+ while (GCLK->STATUS.bit.SYNCBUSY);
+
+ Sercom* sercom = MAX3421_SERCOM;
+
+ // Disable the SPI module
+ sercom->SPI.CTRLA.bit.ENABLE = 0;
+
+ // Reset the SPI module
+ sercom->SPI.CTRLA.bit.SWRST = 1;
+ while (sercom->SPI.SYNCBUSY.bit.SWRST);
+
+ // Set up SPI in master mode, MSB first, SPI mode 0
+ sercom->SPI.CTRLA.reg = SERCOM_SPI_CTRLA_DOPO(MAX3421_TX_PAD) | SERCOM_SPI_CTRLA_DIPO(MAX3421_RX_PAD) |
+ SERCOM_SPI_CTRLA_MODE(3);
+
+ sercom->SPI.CTRLB.reg = SERCOM_SPI_CTRLB_CHSIZE(0) | SERCOM_SPI_CTRLB_RXEN;
+ while (sercom->SPI.SYNCBUSY.bit.CTRLB == 1);
+
+ // Set the baud rate
+ sercom->SPI.BAUD.reg = (uint8_t) (SystemCoreClock / (2 * baudrate) - 1);
+
+ // Configure PA12 as MOSI (PAD0), PA13 as SCK (PAD1), PA14 as MISO (PAD2), function C (sercom)
+ gpio_set_pin_direction(MAX3421_SCK_PIN, GPIO_DIRECTION_OUT);
+ gpio_set_pin_pull_mode(MAX3421_SCK_PIN, GPIO_PULL_OFF);
+ gpio_set_pin_function(MAX3421_SCK_PIN, MAX3421_SERCOM_FUNCTION);
+
+ gpio_set_pin_direction(MAX3421_MOSI_PIN, GPIO_DIRECTION_OUT);
+ gpio_set_pin_pull_mode(MAX3421_MOSI_PIN, GPIO_PULL_OFF);
+ gpio_set_pin_function(MAX3421_MOSI_PIN, MAX3421_SERCOM_FUNCTION);
+
+ gpio_set_pin_direction(MAX3421_MISO_PIN, GPIO_DIRECTION_IN);
+ gpio_set_pin_pull_mode(MAX3421_MISO_PIN, GPIO_PULL_OFF);
+ gpio_set_pin_function(MAX3421_MISO_PIN, MAX3421_SERCOM_FUNCTION);
+
+ // CS pin
+ gpio_set_pin_direction(MAX3421_CS_PIN, GPIO_DIRECTION_OUT);
+ gpio_set_pin_level(MAX3421_CS_PIN, 1);
+
+ // Enable the SPI module
+ sercom->SPI.CTRLA.bit.ENABLE = 1;
+ while (sercom->SPI.SYNCBUSY.bit.ENABLE);
+
+ //------------- External Interrupt -------------//
+
+ // Enable the APB clock for EIC (External Interrupt Controller)
+ PM->APBAMASK.reg |= PM_APBAMASK_EIC;
+
+ // Configure GCLK for EIC
+ GCLK->CLKCTRL.reg = GCLK_CLKCTRL_ID_EIC | GCLK_CLKCTRL_GEN_GCLK0 | GCLK_CLKCTRL_CLKEN;
+ while (GCLK->STATUS.bit.SYNCBUSY);
+
+ // Configure PA20 as an input with function A (external interrupt)
+ gpio_set_pin_direction(MAX3421_INTR_PIN, GPIO_DIRECTION_IN);
+ gpio_set_pin_pull_mode(MAX3421_INTR_PIN, GPIO_PULL_UP);
+ gpio_set_pin_function(MAX3421_INTR_PIN, 0);
+
+ // Disable EIC
+ EIC->CTRL.bit.ENABLE = 0;
+ while (EIC->STATUS.bit.SYNCBUSY);
+
+ // Configure EIC to trigger on falling edge
+ uint8_t const sense_shift = MAX3421_INTR_EIC_ID * 4;
+ EIC->CONFIG[0].reg &= ~(7 << sense_shift);
+ EIC->CONFIG[0].reg |= 2 << sense_shift;
+
+ // Enable External Interrupt
+ EIC->INTENSET.reg = EIC_INTENSET_EXTINT(1 << MAX3421_INTR_EIC_ID);
+
+ // Enable EIC
+ EIC->CTRL.bit.ENABLE = 1;
+ while (EIC->STATUS.bit.SYNCBUSY);
+}
+
+void EIC_Handler(void) {
+ // Clear the interrupt flag
+ EIC->INTFLAG.reg = EIC_INTFLAG_EXTINT(1 << MAX3421_INTR_EIC_ID);
+
+ // Call the TinyUSB interrupt handler
+ tuh_int_handler(1);
+}
+
+void tuh_max3421_int_api(uint8_t rhport, bool enabled) {
+ (void) rhport;
+
+ if (enabled) {
+ NVIC_EnableIRQ(EIC_IRQn);
+ } else {
+ NVIC_DisableIRQ(EIC_IRQn);
+ }
+}
+
+void tuh_max3421_spi_cs_api(uint8_t rhport, bool active) {
+ (void) rhport;
+ gpio_set_pin_level(MAX3421_CS_PIN, active ? 0 : 1);
+}
+
+bool tuh_max3421_spi_xfer_api(uint8_t rhport, uint8_t const *tx_buf, size_t tx_len, uint8_t *rx_buf, size_t rx_len) {
+ (void) rhport;
+
+ Sercom* sercom = MAX3421_SERCOM;
+
+ size_t count = 0;
+ while (count < tx_len || count < rx_len) {
+ // Wait for the transmit buffer to be empty
+ while (!sercom->SPI.INTFLAG.bit.DRE);
+
+ // Write data to be transmitted
+ uint8_t data = 0x00;
+ if (count < tx_len) {
+ data = tx_buf[count];
+ }
+
+ sercom->SPI.DATA.reg = (uint32_t) data;
+
+ // Wait for the receive buffer to be filled
+ while (!sercom->SPI.INTFLAG.bit.RXC);
+
+ // Read received data
+ data = (uint8_t) sercom->SPI.DATA.reg;
+ if (count < rx_len) {
+ rx_buf[count] = data;
+ }
+
+ count++;
+ }
+
+ // wait for bus idle and clear flags
+ while (!(sercom->SPI.INTFLAG.reg & (SERCOM_SPI_INTFLAG_TXC | SERCOM_SPI_INTFLAG_DRE)));
+ sercom->SPI.INTFLAG.reg = SERCOM_SPI_INTFLAG_TXC | SERCOM_SPI_INTFLAG_DRE;
+
+ return true;
+}
+
+#endif
+
+
#endif
diff --git a/hw/bsp/samd21/family.cmake b/hw/bsp/samd21/family.cmake
index 07186934a..ffbf687a1 100644
--- a/hw/bsp/samd21/family.cmake
+++ b/hw/bsp/samd21/family.cmake
@@ -94,6 +94,7 @@ function(family_configure_example TARGET RTOS)
family_add_tinyusb(${TARGET} OPT_MCU_SAMD21 ${RTOS})
target_sources(${TARGET}-tinyusb PUBLIC
${TOP}/src/portable/microchip/samd/dcd_samd.c
+ ${TOP}/src/portable/analog/max3421/hcd_max3421.c
)
target_link_libraries(${TARGET}-tinyusb PUBLIC board_${BOARD})
diff --git a/hw/bsp/samd51/boards/metro_m4_express/board.h b/hw/bsp/samd51/boards/metro_m4_express/board.h
index aa6bdd0c8..b3b80db89 100644
--- a/hw/bsp/samd51/boards/metro_m4_express/board.h
+++ b/hw/bsp/samd51/boards/metro_m4_express/board.h
@@ -44,16 +44,20 @@
#define UART_RX_PIN 22
// SPI for USB host shield
-#define MAX3421_SERCOM_ID 2 // SERCOM2
-#define MAX3421_SCK_PIN 13
-#define MAX3421_MOSI_PIN 12
-#define MAX3421_MOSI_PAD 0
-#define MAX3421_MISO_PIN 14
-#define MAX3421_MISO_PAD 2
-#define MAX3421_CS_PIN 18 // D10
+#define MAX3421_SERCOM_ID 2 // SERCOM2
+#define MAX3421_SERCOM_FUNCTION 2 // function C
+
+#define MAX3421_SCK_PIN 13
+#define MAX3421_MOSI_PIN 12
+#define MAX3421_MISO_PIN 14
+#define MAX3421_TX_PAD 0 // MOSI = PAD_0, SCK = PAD_1
+#define MAX3421_RX_PAD 2 // MISO = PAD_2
+
+#define MAX3421_CS_PIN 18 // D10
+
+#define MAX3421_INTR_PIN 20 // D9
+#define MAX3421_INTR_EIC_ID 4 // EIC4
-#define MAX3421_INTR_PIN 20 // D9
-#define MAX3421_INTR_EIC_ID 4 // EIC4
#ifdef __cplusplus
}
diff --git a/hw/bsp/samd51/family.c b/hw/bsp/samd51/family.c
index f9e1718d0..2cf00c7af 100644
--- a/hw/bsp/samd51/family.c
+++ b/hw/bsp/samd51/family.c
@@ -77,11 +77,10 @@ void USB_3_Handler(void) {
//--------------------------------------------------------------------+
#if CFG_TUH_ENABLED && defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
-static void max3421_init(void);
-
#define MAX3421_SERCOM TU_XSTRCAT(SERCOM, MAX3421_SERCOM_ID)
#define MAX3421_EIC_Handler TU_XSTRCAT3(EIC_, MAX3421_INTR_EIC_ID, _Handler)
+static void max3421_init(void);
#endif
void board_init(void) {
@@ -223,7 +222,8 @@ static void max3421_init(void) {
while (sercom->SPI.SYNCBUSY.bit.SWRST);
// Set up SPI in master mode, MSB first, SPI mode 0
- sercom->SPI.CTRLA.reg = SERCOM_SPI_CTRLA_MODE(3) | SERCOM_SPI_CTRLA_DOPO(MAX3421_MOSI_PAD) | SERCOM_SPI_CTRLA_DIPO(MAX3421_MISO_PAD);
+ sercom->SPI.CTRLA.reg = SERCOM_SPI_CTRLA_DOPO(MAX3421_TX_PAD) | SERCOM_SPI_CTRLA_DIPO(MAX3421_RX_PAD) |
+ SERCOM_SPI_CTRLA_MODE(3);
sercom->SPI.CTRLB.reg = SERCOM_SPI_CTRLB_CHSIZE(0) | SERCOM_SPI_CTRLB_RXEN;
while (sercom->SPI.SYNCBUSY.bit.CTRLB == 1);
@@ -234,15 +234,15 @@ static void max3421_init(void) {
// Configure PA12 as MOSI (PAD0), PA13 as SCK (PAD1), PA14 as MISO (PAD2), function C (sercom)
gpio_set_pin_direction(MAX3421_SCK_PIN, GPIO_DIRECTION_OUT);
gpio_set_pin_pull_mode(MAX3421_SCK_PIN, GPIO_PULL_OFF);
- gpio_set_pin_function(MAX3421_SCK_PIN, 2);
+ gpio_set_pin_function(MAX3421_SCK_PIN, MAX3421_SERCOM_FUNCTION);
gpio_set_pin_direction(MAX3421_MOSI_PIN, GPIO_DIRECTION_OUT);
gpio_set_pin_pull_mode(MAX3421_MOSI_PIN, GPIO_PULL_OFF);
- gpio_set_pin_function(MAX3421_MOSI_PIN, 2);
+ gpio_set_pin_function(MAX3421_MOSI_PIN, MAX3421_SERCOM_FUNCTION);
gpio_set_pin_direction(MAX3421_MISO_PIN, GPIO_DIRECTION_IN);
gpio_set_pin_pull_mode(MAX3421_MISO_PIN, GPIO_PULL_OFF);
- gpio_set_pin_function(MAX3421_MISO_PIN, 2);
+ gpio_set_pin_function(MAX3421_MISO_PIN, MAX3421_SERCOM_FUNCTION);
// CS pin
gpio_set_pin_direction(MAX3421_CS_PIN, GPIO_DIRECTION_OUT);
@@ -269,7 +269,7 @@ static void max3421_init(void) {
EIC->CTRLA.bit.ENABLE = 0;
while (EIC->SYNCBUSY.bit.ENABLE);
- // Configure EXTINT4 (PA20) to trigger on falling edge
+ // Configure EIC to trigger on falling edge
volatile uint32_t * eic_config;
uint8_t sense_shift;
if ( MAX3421_INTR_EIC_ID < 8 ) {
@@ -302,10 +302,11 @@ void MAX3421_EIC_Handler(void) {
void tuh_max3421_int_api(uint8_t rhport, bool enabled) {
(void) rhport;
+ const IRQn_Type irq = EIC_0_IRQn + MAX3421_INTR_EIC_ID;
if (enabled) {
- NVIC_EnableIRQ(EIC_4_IRQn);
+ NVIC_EnableIRQ(irq);
} else {
- NVIC_DisableIRQ(EIC_4_IRQn);
+ NVIC_DisableIRQ(irq);
}
}