From 5ec463ccfde14115241363975c5116768aba7789 Mon Sep 17 00:00:00 2001 From: windowsair Date: Wed, 10 Mar 2021 20:52:20 +0800 Subject: [PATCH] feat: Support 3-wire spi --- components/DAP/source/spi_op.c | 31 ++++++++++++++++++++++++++++-- components/DAP/source/spi_switch.c | 5 +++++ main/dap_configuration.h | 8 +++++++- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/components/DAP/source/spi_op.c b/components/DAP/source/spi_op.c index f5dba3d..3d95880 100644 --- a/components/DAP/source/spi_op.c +++ b/components/DAP/source/spi_op.c @@ -4,17 +4,20 @@ * @brief Using SPI for common transfer operations * @change: 2020-11-25 first version * 2021-2-11 Support SWD sequence - * @version 0.2 - * @date 2021-2-11 + * 2021-3-10 Support 3-wire SPI + * @version 0.3 + * @date 2021-3-10 * * @copyright Copyright (c) 2021 * */ #include +#include #include "esp8266/spi_struct.h" #include "cmsis_compiler.h" #include "spi_op.h" +#include "dap_configuration.h" #define DAP_SPI SPI1 @@ -103,6 +106,10 @@ void DAP_SPI_ReadBits(const uint8_t count, uint8_t *buf) { DAP_SPI.user.usr_mosi = 0; DAP_SPI.user.usr_miso = 1; +#if (USE_SPI_SIO == 1) + DAP_SPI.user.sio = true; +#endif + DAP_SPI.user1.usr_miso_bitlen = count - 1U; // Start transmission @@ -110,6 +117,10 @@ void DAP_SPI_ReadBits(const uint8_t count, uint8_t *buf) { // Wait for reading to complete while (DAP_SPI.cmd.usr) continue; +#if (USE_SPI_SIO == 1) + DAP_SPI.user.sio = false; +#endif + data_buf[0] = DAP_SPI.data_buf[0]; data_buf[1] = DAP_SPI.data_buf[1]; @@ -139,6 +150,10 @@ __FORCEINLINE void DAP_SPI_Send_Header(const uint8_t packetHeaderData, uint8_t * DAP_SPI.user.usr_miso = 1; +#if (USE_SPI_SIO == 1) + DAP_SPI.user.sio = true; +#endif + // 1 bit Trn(Before ACK) + 3bits ACK + TrnAferACK - 1(prescribed) DAP_SPI.user1.usr_miso_bitlen = 1U + 3U + TrnAfterACK - 1U; @@ -150,6 +165,10 @@ __FORCEINLINE void DAP_SPI_Send_Header(const uint8_t packetHeaderData, uint8_t * // Wait for sending to complete while (DAP_SPI.cmd.usr) continue; +#if (USE_SPI_SIO == 1) + DAP_SPI.user.sio = false; +#endif + dataBuf = DAP_SPI.data_buf[0]; *ack = (dataBuf >> 1) & 0b111; } @@ -169,6 +188,10 @@ __FORCEINLINE void DAP_SPI_Read_Data(uint32_t *resData, uint8_t *resParity) DAP_SPI.user.usr_mosi = 0; DAP_SPI.user.usr_miso = 1; +#if (USE_SPI_SIO == 1) + DAP_SPI.user.sio = true; +#endif + // 1 bit Trn(End) + 3bits ACK + 32bis data + 1bit parity - 1(prescribed) DAP_SPI.user1.usr_miso_bitlen = 1U + 32U + 1U - 1U; @@ -177,6 +200,10 @@ __FORCEINLINE void DAP_SPI_Read_Data(uint32_t *resData, uint8_t *resParity) // Wait for sending to complete while (DAP_SPI.cmd.usr) continue; +#if (USE_SPI_SIO == 1) + DAP_SPI.user.sio = false; +#endif + pU32Data[0] = DAP_SPI.data_buf[0]; pU32Data[1] = DAP_SPI.data_buf[1]; diff --git a/components/DAP/source/spi_switch.c b/components/DAP/source/spi_switch.c index 09a7236..418faaf 100644 --- a/components/DAP/source/spi_switch.c +++ b/components/DAP/source/spi_switch.c @@ -18,6 +18,7 @@ #include "cmsis_compiler.h" #include "spi_switch.h" +#include "dap_configuration.h" #define DAP_SPI SPI1 @@ -106,11 +107,13 @@ void DAP_SPI_Init() pin_reg.pullup = 0; WRITE_PERI_REG(GPIO_PIN_REG(13), pin_reg.val); +#if (USE_SPI_SIO != 1) PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, FUNC_HSPIQ_MISO); // GPIO12 is SPI MISO pin (Master Data In) // esp8266 in is always connected pin_reg.val = READ_PERI_REG(GPIO_PIN_REG(12)); pin_reg.pullup = 0; WRITE_PERI_REG(GPIO_PIN_REG(12), pin_reg.val); +#endif // (USE_SPI_SIO != 1) @@ -136,10 +139,12 @@ __FORCEINLINE void DAP_SPI_Deinit() { PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTMS_U, FUNC_GPIO14); PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, FUNC_GPIO13); // MOSI +#if (USE_SPI_SIO != 1) PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12); // MISO // disable MISO output connect GPIO.enable_w1tc |= (0x1 << 12); +#endif // (USE_SPI_SIO != 1) gpio_pin_reg_t pin_reg; GPIO.enable_w1ts |= (0x1 << 13); diff --git a/main/dap_configuration.h b/main/dap_configuration.h index 07298cf..4779c4c 100644 --- a/main/dap_configuration.h +++ b/main/dap_configuration.h @@ -3,10 +3,16 @@ /** * @brief Specify the use of WINUSB - * + * */ #define USE_WINUSB 1 +/** + * @brief Enable this option, no need to physically connect MOSI and MISO + * + */ +#define USE_SPI_SIO 1 + /// Maximum Package Size for Command and Response data. /// This configuration settings is used to optimize the communication performance with the /// debugger and depends on the USB peripheral. Typical vales are 64 for Full-speed USB HID or WinUSB,