Skip to content

Commit

Permalink
[USB] 尝试移植DAP
Browse files Browse the repository at this point in the history
  • Loading branch information
jim-kirisame committed Mar 16, 2020
1 parent c489909 commit bafea8d
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 50 deletions.
6 changes: 3 additions & 3 deletions usb/DAP.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@
__XDATA DAP_Data_t DAP_Data; // DAP Data
volatile uint8_t DAP_TransferAbort; // Transfer Abort Flag

const uint8_t* request;
uint8_t* response;
static const uint8_t* request;
static uint8_t* response;

// static const char DAP_FW_Ver [] = DAP_FW_VER;

Expand Down Expand Up @@ -968,7 +968,7 @@ uint16_t DAP_ProcessCommand(const uint8_t* req, uint8_t* resp)
case ID_DAP_Info:
num = DAP_Info(*request, response + 1);
*response = (uint8_t)num;
return (((uint32_t)2U << 8) + 2U + num);
return (((uint16_t)2U << 8) + 2U + num);

case ID_DAP_HostStatus:
num = DAP_HostStatus();
Expand Down
2 changes: 1 addition & 1 deletion usb/DAP_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Provides definitions about:
/// This configuration settings is used to optimized the communication performance with the
/// debugger and depends on the USB peripheral. For devices with limited RAM or USB buffer the
/// setting can be reduced (valid range is 1 .. 255). Change setting to 4 for High-Speed USB.
#define DAP_PACKET_COUNT 4 ///< Buffers: 64 = Full-Speed, 4 = High-Speed.
#define DAP_PACKET_COUNT 1 ///< Buffers: 64 = Full-Speed, 4 = High-Speed.

/// Indicate that UART Serial Wire Output (SWO) trace is available.
/// This information is returned by the command \ref DAP_Info as part of <b>Capabilities</b>.
Expand Down
30 changes: 28 additions & 2 deletions usb/DAP_hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@
#define EP4_OUT_BUF (&Ep0Buffer[64])
#define EP4_IN_BUF (&EP4_OUT_BUF[64])

volatile static uint8_t ep4_len = 0;

void EP4_OUT()
{
if (ep4_len > 0) {
EP_OUT_RESP(4, UEP_T_RES_NAK);
return;
}

if (USB_RX_LEN == 0) {
return;
}
Expand All @@ -17,6 +24,25 @@ void EP4_OUT()
return;
}

// execute and store to DAP_queue
UEP4_T_LEN = DAP_ExecuteCommand(EP4_OUT_BUF, EP4_IN_BUF) & 0xFF;
EP_OUT_RESP(4, UEP_T_RES_ACK);
ep4_len = USB_RX_LEN;
}

void Dap_Init()
{
DAP_Setup();
}

void Dap_Routine()
{
if (ep4_len > 0 && !usb_state.is_busy) {
uint8_t len = DAP_ExecuteCommand(EP4_OUT_BUF, EP4_IN_BUF) & 0xFF;
if (len <= MAX_PACKET_SIZE) {
usb_state.is_busy = true;
UEP4_T_LEN = MAX_PACKET_SIZE;
EP_IN_RESP(4, UEP_T_RES_ACK);
}

ep4_len = 0;
}
}
4 changes: 4 additions & 0 deletions usb/DAP_hid.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once

void Dap_Init(void);
void Dap_Routine(void);
6 changes: 3 additions & 3 deletions usb/dap_strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "descriptor.h"
#include <stdint.h>
#include <string.h>

/** Get Vendor ID string.
\param str Pointer to buffer to store the string.
Expand All @@ -46,7 +47,6 @@
uint8_t DAP_GetFirmwareVersionString(char* str)
{
const char* data = "0254";
uint8_t length = (uint8_t)strlen(data) + 1;
memcpy(str, data, length);
return length;
memcpy(str, data, sizeof(data));
return sizeof(data);
}
41 changes: 1 addition & 40 deletions usb/endpoints.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,6 @@ static uint8_t keyboard_idle = 0;

static uint8_t ClassRequestHandler(PUSB_SETUP_REQ packet);

/**
* 设置指定Endpoint的状态
*
* @param num: Endpoint号
* @param data: 指定DATA0或DATA1
* @param resp: 默认应答
**/
#define EP_SET(num, data, resp) (UEP##num##_CTRL = ((data) | (resp)) & 0xFF)
#define EP_IN_RESP(num, resp) (UEP##num##_CTRL = UEP##num##_CTRL & (~MASK_UEP_T_RES) | (resp))
#define EP_OUT_RESP(num, resp) (UEP##num##_CTRL = UEP##num##_CTRL & (~MASK_UEP_R_RES) | ((resp) << 2))
/**
* 重置端点0的状态。
* SETUP/OUT ACK
Expand All @@ -88,35 +78,6 @@ static uint8_t ClassRequestHandler(PUSB_SETUP_REQ packet);
*/
#define EP0_DATA1_ACK() EP_SET(0, (bUEP_R_TOG | bUEP_T_TOG), (UEP_R_RES_ACK | UEP_T_RES_ACK))

/**
* 设置端点IN NAK
*/
#define EP_IN_NAK(num) (UEP##num##_CTRL = UEP##num##_CTRL & ~MASK_UEP_T_RES | UEP_T_RES_NAK)

/**
* 端点 IN 后续处理
* 清空发送长度,并设置NAK
*/
#define EP_IN_FINISH(num) \
UEP##num##_T_LEN = 0; \
EP_IN_NAK(num)

/**
* 设置端点 IN NAK 响应并反转 DATA
*/
#define EP_IN_NAK_TOG(num) (UEP##num##_CTRL = UEP##num##_CTRL & ~(bUEP_T_TOG | MASK_UEP_T_RES) | UEP_T_RES_NAK)
/**
* 设置端点 OUT ACK 响应并反转 DATA
*/
#define EP_OUT_ACK_TOG(num) (UEP##num##_CTRL = UEP##num##_CTRL & ~(bUEP_R_TOG | MASK_UEP_R_RES) | UEP_R_RES_ACK)
/**
* 设置端点 IN STALL 响应并反转 DATA
*/
#define EP_IN_STALL_TOG(num) (UEP##num##_CTRL = UEP##num##_CTRL & (~bUEP_T_TOG) | UEP_T_RES_STALL)
/**
* 设置端点 OUT STALL 响应并反转 DATA
*/
#define EP_OUT_STALL_TOG(num) (UEP##num##_CTRL = UEP##num##_CTRL & (~bUEP_R_TOG) | UEP_R_RES_STALL)
/**
* @brief 为SETUP请求响应STALL
*
Expand Down Expand Up @@ -465,7 +426,7 @@ void USBDeviceInit()
USB_CTRL = 0x00; // 先设定USB设备模式

UEP0_DMA = (uint16_t)Ep0Buffer; //端点0数据传输地址
UEP4_1_MOD |= ~(bUEP4_RX_EN | bUEP4_TX_EN); //端点0单64字节收发缓冲区, 端点4单64字节收发缓冲区
UEP4_1_MOD |= bUEP4_RX_EN | bUEP4_TX_EN; //端点0单64字节收发缓冲区, 端点4单64字节收发缓冲区
UEP0_CTRL = UEP_R_RES_ACK | UEP_T_RES_NAK; //OUT事务返回ACK,IN事务返回NAK

UEP1_DMA = (uint16_t)Ep1Buffer; //端点1数据传输地址
Expand Down
64 changes: 64 additions & 0 deletions usb/endpoints.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,67 @@ extern void nop();
#define USB_SOF_EP2 nop
#define USB_SOF_EP3 nop
#define USB_SOF_EP4 nop

/**
* 设置指定Endpoint的状态
*
* @param num: Endpoint号
* @param data: 指定DATA0或DATA1
* @param resp: 默认应答
**/
#define EP_SET(num, data, resp) (UEP##num##_CTRL = ((data) | (resp)) & 0xFF)

/**
* @brief 设置指定Endpoint IN的响应
*
* @param num: Endpoint号
* @param resp: 默认应答
*/
#define EP_IN_RESP(num, resp) (UEP##num##_CTRL = UEP##num##_CTRL & (~MASK_UEP_T_RES) | (resp))

/**
* @brief 设置指定Endpoint OUT的响应
*
* @param num: Endpoint号
* @param resp: 默认应答
*/
#define EP_OUT_RESP(num, resp) (UEP##num##_CTRL = UEP##num##_CTRL & (~MASK_UEP_R_RES) | ((resp) << 2))

/**
* 设置端点IN NAK
*/
#define EP_IN_NAK(num) (UEP##num##_CTRL = UEP##num##_CTRL & ~MASK_UEP_T_RES | UEP_T_RES_NAK)

/**
* 设置端点OUT NAK
*/
#define EP_OUT_NAK(num) (UEP##num##_CTRL = UEP##num##_CTRL & ~MASK_UEP_R_RES | UEP_R_RES_NAK)

/**
* 端点 IN 后续处理
* 清空发送长度,并设置NAK
*/
#define EP_IN_FINISH(num) \
UEP##num##_T_LEN = 0; \
EP_IN_NAK(num)

/**
* 设置端点 IN NAK 响应并反转 DATA
*/
#define EP_IN_NAK_TOG(num) (UEP##num##_CTRL = UEP##num##_CTRL & ~(bUEP_T_TOG | MASK_UEP_T_RES) | UEP_T_RES_NAK)
/**
* 设置端点 OUT ACK 响应并反转 DATA
*/
#define EP_OUT_ACK_TOG(num) (UEP##num##_CTRL = UEP##num##_CTRL & ~(bUEP_R_TOG | MASK_UEP_R_RES) | UEP_R_RES_ACK)
/**
* 设置端点 IN STALL 响应并反转 DATA
*/
#define EP_IN_STALL_TOG(num) (UEP##num##_CTRL = UEP##num##_CTRL & (~bUEP_T_TOG) | UEP_T_RES_STALL)
/**
* 设置端点 OUT STALL 响应并反转 DATA
*/
#define EP_OUT_STALL_TOG(num) (UEP##num##_CTRL = UEP##num##_CTRL & (~bUEP_R_TOG) | UEP_R_RES_STALL)
/**
* 设置端点 OUT NAK 响应并反转 DATA
*/
#define EP_OUT_NAK_TOG(num) (UEP##num##_CTRL = UEP##num##_CTRL & (~bUEP_R_TOG) | UEP_R_RES_NAK)
6 changes: 5 additions & 1 deletion usb/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include "CH554_SDCC.h"
#include "DAP_hid.h"
#include "app_timer.h"
#include "compiler.h"
#include "endpoints.h"
Expand Down Expand Up @@ -177,7 +178,8 @@ static void FeedWatchDog()
/** 定义定时器 */
const timer_info timers[] = {
TIMER_DEF(&FeedWatchDog, 500),
TIMER_DEF(&uart_check, 1)
TIMER_DEF(&uart_check, 1),
TIMER_DEF(&Dap_Routine, 1)
};
TIMER_INIT(timer, timers)

Expand Down Expand Up @@ -208,10 +210,12 @@ static void main()

USBDeviceInit(); //USB设备模式初始化
EnableWatchDog();
Dap_Init();
EA = 1; //允许单片机中断
UEP1_T_LEN = 0; //预使用发送长度一定要清空
UEP2_T_LEN = 0; //预使用发送长度一定要清空
UEP3_T_LEN = 0;
UEP4_T_LEN = 0;

// 拉低P1.5,通知主控使用UART接收
P1_MOD_OC -= (P1_MOD_OC & bMOSI);
Expand Down

0 comments on commit bafea8d

Please sign in to comment.