Skip to content

Commit

Permalink
feat: Support 3-wire spi
Browse files Browse the repository at this point in the history
  • Loading branch information
windowsair committed Mar 10, 2021
1 parent 8429361 commit 5ec463c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
31 changes: 29 additions & 2 deletions components/DAP/source/spi_op.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stdio.h>
#include <stdbool.h>

#include "esp8266/spi_struct.h"
#include "cmsis_compiler.h"
#include "spi_op.h"
#include "dap_configuration.h"

#define DAP_SPI SPI1

Expand Down Expand Up @@ -103,13 +106,21 @@ 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
DAP_SPI.cmd.usr = 1;
// 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];

Expand Down Expand Up @@ -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;

Expand All @@ -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;
}
Expand All @@ -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;

Expand All @@ -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];

Expand Down
5 changes: 5 additions & 0 deletions components/DAP/source/spi_switch.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "cmsis_compiler.h"
#include "spi_switch.h"
#include "dap_configuration.h"


#define DAP_SPI SPI1
Expand Down Expand Up @@ -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)



Expand All @@ -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);
Expand Down
8 changes: 7 additions & 1 deletion main/dap_configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 5ec463c

Please sign in to comment.