Skip to content

Commit

Permalink
fw: add missing files for USB
Browse files Browse the repository at this point in the history
  • Loading branch information
fallen committed Mar 23, 2024
1 parent c8657cd commit bb46b60
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
26 changes: 26 additions & 0 deletions software/boards/hydrausb3/hydrausb3_usb_devbulk_vid_pid.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : hydrausb3_usb_devbulk_vid_pid.h
* Author : bvernoux
* Version : V1.0
* Date : 2022/08/20
* Description :
* Copyright (c) 2022 Benjamin VERNOUX
* SPDX-License-Identifier: Apache-2.0
*******************************************************************************/
/*
Use default USB PID/VID if not configured with usb_descriptor_set_usb_vid_pid()
https://github.com/obdev/v-usb/blob/master/usbdrv/USB-IDs-for-free.txt
PID dec (hex) | VID dec (hex) | Description of use
==============+===============+============================================
1500 (0x05dc) | 5824 (0x16c0) | For Vendor Class devices with libusb
*/
// Default USB Vendor ID
// Default VID 0x16C0 "Van Ooijen Technische Informatica"
#define USB_VID_BYTE_MSB (0x16)
#define USB_VID_BYTE_LSB (0xC0)
#define USB_VID ((USB_VID_BYTE_MSB << 8) | USB_VID_BYTE_LSB)
// Default USB Product ID
// Default PID 0x05DC
#define USB_PID_BYTE_MSB (0x05)
#define USB_PID_BYTE_LSB (0xDC)
#define USB_PID ((USB_PID_BYTE_MSB << 8) | USB_PID_BYTE_LSB)
81 changes: 81 additions & 0 deletions software/boards/hydrausb3/usb_cmd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : usb_cmd.c
* Author : bvernoux
* Version : V1.0
* Date : 2022/08/20
* Description :
* Copyright (c) 2022 Benjamin VERNOUX
* SPDX-License-Identifier: Apache-2.0
*******************************************************************************/
#include "CH56x_common.h"
#include "CH56x_usb20_devbulk.h"
#include "CH56x_usb30_devbulk.h"
#include "CH56x_usb30_devbulk_LIB.h"

#include "CH56x_debug_log.h"
#include "usb_cmd.h"


/*******************************************************************************
* @fn usb_cmd_rx
*
* @brief Callback called by USB2 & USB3 endpoint 1
* - For USB3 this usb_cmd_rx() is called from IRQ(USBHS_IRQHandler)
* with rx_usb_dma_buff containing 4096 bytes (DEF_ENDP1_MAX_SIZE)
* - For USB2 this usb_cmd_rx() is called from IRQ USB30_IRQHandler->EP1_OUT_Callback)
* with rx_usb_dma_buff containing 4096 bytes (DEF_ENDP1_MAX_SIZE)
*
* @param usb_type: USB Type (USB2 HS or USB3 SS)
* @param rx_usb_dma_buff: USB RX DMA buffer containing 4096 bytes of data
* Data received from USB
* @param tx_usb_dma_buff: USB TX DMA buffer containing 4096 bytes of data
* Data to be transmitted over USB
*
* @return size of data to be sent to USB host
*/


#define RISCV_FENCE(p, s) \
__asm__ __volatile__ ("fence " #p "," #s : : : "memory")

/* These barriers need to enforce ordering on both devices or memory. */
#define mb() RISCV_FENCE(iorw,iorw)
#define rmb() RISCV_FENCE(ir,ir)
#define wmb() RISCV_FENCE(ow,ow)

extern volatile int usb_cmd_rxed;
extern volatile uint8_t usb_cmd[11];
extern volatile uint8_t usb_cmd_len;
extern volatile int ep1_out_nack;

// return 1 for busy, 0 for OK
int usb_cmd_rx(e_usb_type usb_type, uint8_t* rx_usb_dma_buff, uint8_t* tx_usb_dma_buff)
{
if (usb_cmd_rxed) { // already processing...
ep1_out_nack++;
return 1;
}
switch (rx_usb_dma_buff[0]) {
case 'U': {
if (rx_usb_dma_buff[1] == 'w') {
usb_cmd_len = rx_usb_dma_buff[2];
memcpy(usb_cmd, &rx_usb_dma_buff[1], usb_cmd_len + 2);
mb();
usb_cmd_rxed = 1;
}
if (rx_usb_dma_buff[1] == 'r') {
usb_cmd_len = rx_usb_dma_buff[2];
usb_cmd[0] = 'r';
mb();
usb_cmd_rxed = 1;
}
if (rx_usb_dma_buff[1] != 'r' && rx_usb_dma_buff[1] != 'w')
printf("Big PROTO error!\n");
break;
}
default: {
printf("Big error!\n");
}
}
return 0;
}
23 changes: 23 additions & 0 deletions software/boards/hydrausb3/usb_cmd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : usb_cmd.h
* Author : bvernoux
* Version : V1.0
* Date : 2022/08/20
* Description :
* Copyright (c) 2022 Benjamin VERNOUX
* SPDX-License-Identifier: Apache-2.0
*******************************************************************************/
#ifndef USB_CMD_H_
#define USB_CMD_H_

#ifdef __cplusplus
extern "C" {
#endif

#include "CH56x_usb_devbulk_desc_cmd.h"

#ifdef __cplusplus
}
#endif

#endif /* USB_CMD_H_ */

0 comments on commit bb46b60

Please sign in to comment.