Commit f8f17f48 authored by Damien George's avatar Damien George

stm32/usbdev: Put all state for the USB device driver in a struct.

parent 35e3435f
......@@ -111,7 +111,7 @@ bool pyb_usb_dev_init(uint16_t vid, uint16_t pid, usb_device_mode_t mode, USBD_H
// configure the VID, PID and the USBD mode (interfaces it will expose)
USBD_SetVIDPIDRelease(vid, pid, 0x0200, mode == USBD_MODE_CDC);
if (USBD_SelectMode(mode, hid_info) != 0) {
if (USBD_SelectMode(&usbd_cdc_msc_hid_state, mode, hid_info) != 0) {
return false;
}
......@@ -121,6 +121,7 @@ bool pyb_usb_dev_init(uint16_t vid, uint16_t pid, usb_device_mode_t mode, USBD_H
usbd->dev_state = USBD_STATE_DEFAULT;
usbd->pDesc = (USBD_DescriptorsTypeDef*)&USBD_Descriptors;
usbd->pClass = &USBD_CDC_MSC_HID;
usbd_cdc_msc_hid_state.pdev = usbd;
usbd_cdc_msc_hid_state.cdc = &usbd_cdc_itf;
usbd_cdc_msc_hid_state.hid = &usbd_hid_itf;
usbd->pClassData = &usbd_cdc_msc_hid_state;
......@@ -128,11 +129,11 @@ bool pyb_usb_dev_init(uint16_t vid, uint16_t pid, usb_device_mode_t mode, USBD_H
switch (pyb_usb_storage_medium) {
#if MICROPY_HW_HAS_SDCARD
case PYB_USB_STORAGE_MEDIUM_SDCARD:
USBD_MSC_RegisterStorage(usbd, (USBD_StorageTypeDef*)&USBD_SDCARD_STORAGE_fops);
USBD_MSC_RegisterStorage(&usbd_cdc_msc_hid_state, (USBD_StorageTypeDef*)&USBD_SDCARD_STORAGE_fops);
break;
#endif
default:
USBD_MSC_RegisterStorage(usbd, (USBD_StorageTypeDef*)&USBD_FLASH_STORAGE_fops);
USBD_MSC_RegisterStorage(&usbd_cdc_msc_hid_state, (USBD_StorageTypeDef*)&USBD_FLASH_STORAGE_fops);
break;
}
......@@ -225,7 +226,7 @@ STATIC mp_obj_t pyb_usb_mode(size_t n_args, const mp_obj_t *pos_args, mp_map_t *
#if defined(USE_HOST_MODE)
return MP_OBJ_NEW_QSTR(MP_QSTR_host);
#elif defined(USE_DEVICE_MODE)
uint8_t mode = USBD_GetMode();
uint8_t mode = USBD_GetMode(&usbd_cdc_msc_hid_state);
switch (mode) {
case USBD_MODE_CDC:
return MP_OBJ_NEW_QSTR(MP_QSTR_VCP);
......@@ -614,7 +615,7 @@ STATIC mp_obj_t pyb_usb_hid_send(mp_obj_t self_in, mp_obj_t report_in) {
}
// send the data
if (USBD_OK == USBD_HID_SendReport(&hUSBDDevice, bufinfo.buf, bufinfo.len)) {
if (USBD_OK == USBD_HID_SendReport(&usbd_cdc_msc_hid_state, bufinfo.buf, bufinfo.len)) {
return mp_obj_new_int(bufinfo.len);
} else {
return mp_obj_new_int(0);
......@@ -646,7 +647,7 @@ STATIC mp_uint_t pyb_usb_hid_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_
if ((flags & MP_STREAM_POLL_RD) && usbd_hid_rx_num(&usbd_hid_itf) > 0) {
ret |= MP_STREAM_POLL_RD;
}
if ((flags & MP_STREAM_POLL_WR) && USBD_HID_CanSendReport(&hUSBDDevice)) {
if ((flags & MP_STREAM_POLL_WR) && USBD_HID_CanSendReport(&usbd_cdc_msc_hid_state)) {
ret |= MP_STREAM_POLL_WR;
}
} else {
......
......@@ -56,8 +56,8 @@
#define CDC_SET_CONTROL_LINE_STATE 0x22
#define CDC_SEND_BREAK 0x23
uint8_t *usbd_cdc_init(usbd_cdc_itf_t *cdc, USBD_HandleTypeDef *pdev) {
cdc->usb = pdev;
uint8_t *usbd_cdc_init(usbd_cdc_itf_t *cdc, usbd_cdc_msc_hid_state_t *usbd) {
cdc->usbd = usbd;
cdc->rx_buf_put = 0;
cdc->rx_buf_get = 0;
cdc->tx_buf_ptr_in = 0;
......@@ -181,7 +181,7 @@ void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) {
buffptr = cdc->tx_buf_ptr_out_shadow;
if (USBD_CDC_TransmitPacket(hpcd->pData, buffsize, &cdc->tx_buf[buffptr]) == USBD_OK) {
if (USBD_CDC_TransmitPacket(cdc->usbd, buffsize, &cdc->tx_buf[buffptr]) == USBD_OK) {
cdc->tx_buf_ptr_out_shadow += buffsize;
if (cdc->tx_buf_ptr_out_shadow == USBD_CDC_TX_DATA_SIZE) {
cdc->tx_buf_ptr_out_shadow = 0;
......@@ -219,7 +219,7 @@ int8_t usbd_cdc_receive(usbd_cdc_itf_t *cdc, size_t len) {
}
// initiate next USB packet transfer
USBD_CDC_ReceivePacket(cdc->usb, cdc->rx_packet_buf);
USBD_CDC_ReceivePacket(cdc->usbd, cdc->rx_packet_buf);
return USBD_OK;
}
......
......@@ -35,7 +35,7 @@
#define USBD_CDC_TX_DATA_SIZE (1024) // I think this can be any value (was 2048)
typedef struct _usbd_cdc_itf_t {
USBD_HandleTypeDef *usb; // the parent USB device
usbd_cdc_msc_hid_state_t *usbd; // the parent USB device
uint8_t rx_packet_buf[CDC_DATA_FS_MAX_PACKET_SIZE]; // received data from USB OUT endpoint is stored in this buffer
uint8_t rx_user_buf[USBD_CDC_RX_DATA_SIZE]; // received data is buffered here until the user reads it
......
......@@ -42,8 +42,8 @@
#include "irq.h"
#include "usb.h"
uint8_t *usbd_hid_init(usbd_hid_itf_t *hid, USBD_HandleTypeDef *pdev) {
hid->usb = pdev;
uint8_t *usbd_hid_init(usbd_hid_itf_t *hid, usbd_cdc_msc_hid_state_t *usbd) {
hid->usbd = usbd;
hid->current_read_buffer = 0;
hid->last_read_len = 0;
hid->current_write_buffer = 0;
......@@ -59,9 +59,9 @@ int8_t usbd_hid_receive(usbd_hid_itf_t *hid, size_t len) {
hid->current_write_buffer = !hid->current_write_buffer;
hid->last_read_len = len;
// initiate next USB packet transfer, to append to existing data in buffer
USBD_HID_ReceivePacket(hid->usb, hid->buffer[hid->current_write_buffer]);
USBD_HID_ReceivePacket(hid->usbd, hid->buffer[hid->current_write_buffer]);
// Set NAK to indicate we need to process read buffer
USBD_HID_SetNAK(hid->usb);
USBD_HID_SetNAK(hid->usbd);
return USBD_OK;
}
......@@ -98,7 +98,7 @@ int usbd_hid_rx(usbd_hid_itf_t *hid, size_t len, uint8_t *buf, uint32_t timeout)
hid->current_read_buffer = !hid->current_read_buffer;
// Clear NAK to indicate we are ready to read more data
USBD_HID_ClearNAK(hid->usb);
USBD_HID_ClearNAK(hid->usbd);
// Success, return number of bytes read
return hid->last_read_len;
......
......@@ -7,7 +7,7 @@
#include "usbd_cdc_msc_hid.h"
typedef struct _usbd_hid_itf_t {
USBD_HandleTypeDef *usb; // the parent USB device
usbd_cdc_msc_hid_state_t *usbd; // the parent USB device
uint8_t buffer[2][HID_DATA_FS_MAX_PACKET_SIZE]; // pair of buffers to read individual packets into
int8_t current_read_buffer; // which buffer to read from
......
......@@ -6,6 +6,10 @@
#include "usbd_msc_scsi.h"
#include "usbd_ioreq.h"
// Needed for the CDC+MSC+HID state and should be maximum of all template
// config descriptors defined in usbd_cdc_msc_hid.c
#define MAX_TEMPLATE_CONFIG_DESC_SIZE (107)
// CDC, MSC and HID packet sizes
#define CDC_DATA_FS_MAX_PACKET_SIZE (64) // endpoint IN & OUT packet size
#define MSC_MEDIA_PACKET (2048) // was 8192; how low can it go whilst still working?
......@@ -70,7 +74,39 @@ typedef struct {
uint32_t scsi_blk_len;
} USBD_MSC_BOT_HandleTypeDef;
typedef enum {
HID_IDLE = 0,
HID_BUSY,
} HID_StateTypeDef;
typedef struct {
uint32_t Protocol;
uint32_t IdleState;
uint32_t AltSetting;
HID_StateTypeDef state;
} USBD_HID_HandleTypeDef;
typedef struct _usbd_cdc_msc_hid_state_t {
USBD_HandleTypeDef *pdev;
uint8_t usbd_mode;
uint8_t cdc_iface_num;
uint8_t hid_in_ep;
uint8_t hid_out_ep;
uint8_t hid_iface_num;
uint8_t usbd_config_desc_size;
uint8_t *hid_desc;
const uint8_t *hid_report_desc;
USBD_StorageTypeDef *MSC_fops;
USBD_CDC_HandleTypeDef CDC_ClassData;
USBD_MSC_BOT_HandleTypeDef MSC_BOT_ClassData;
USBD_HID_HandleTypeDef HID_ClassData;
// RAM to hold the current configuration descriptor, which we configure on the fly
__ALIGN_BEGIN uint8_t usbd_config_desc[MAX_TEMPLATE_CONFIG_DESC_SIZE] __ALIGN_END;
void *cdc;
void *msc;
void *hid;
......@@ -89,30 +125,30 @@ extern const uint8_t USBD_HID_KEYBOARD_ReportDesc[USBD_HID_KEYBOARD_REPORT_DESC_
extern const USBD_ClassTypeDef USBD_CDC_MSC_HID;
// returns 0 on success, -1 on failure
int USBD_SelectMode(uint32_t mode, USBD_HID_ModeInfoTypeDef *hid_info);
int USBD_SelectMode(usbd_cdc_msc_hid_state_t *usbd, uint32_t mode, USBD_HID_ModeInfoTypeDef *hid_info);
// returns the current usb mode
uint8_t USBD_GetMode();
uint8_t USBD_GetMode(usbd_cdc_msc_hid_state_t *usbd);
uint8_t USBD_CDC_ReceivePacket(USBD_HandleTypeDef *pdev, uint8_t *buf);
uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev, size_t len, const uint8_t *buf);
uint8_t USBD_CDC_ReceivePacket(usbd_cdc_msc_hid_state_t *usbd, uint8_t *buf);
uint8_t USBD_CDC_TransmitPacket(usbd_cdc_msc_hid_state_t *usbd, size_t len, const uint8_t *buf);
uint8_t USBD_MSC_RegisterStorage(USBD_HandleTypeDef *pdev, USBD_StorageTypeDef *fops);
uint8_t USBD_MSC_RegisterStorage(usbd_cdc_msc_hid_state_t *usbd, USBD_StorageTypeDef *fops);
uint8_t USBD_HID_ReceivePacket(USBD_HandleTypeDef *pdev, uint8_t *buf);
int USBD_HID_CanSendReport(USBD_HandleTypeDef *pdev);
uint8_t USBD_HID_SendReport(USBD_HandleTypeDef *pdev, uint8_t *report, uint16_t len);
uint8_t USBD_HID_SetNAK(USBD_HandleTypeDef *pdev);
uint8_t USBD_HID_ClearNAK(USBD_HandleTypeDef *pdev);
uint8_t USBD_HID_ReceivePacket(usbd_cdc_msc_hid_state_t *usbd, uint8_t *buf);
int USBD_HID_CanSendReport(usbd_cdc_msc_hid_state_t *usbd);
uint8_t USBD_HID_SendReport(usbd_cdc_msc_hid_state_t *usbd, uint8_t *report, uint16_t len);
uint8_t USBD_HID_SetNAK(usbd_cdc_msc_hid_state_t *usbd);
uint8_t USBD_HID_ClearNAK(usbd_cdc_msc_hid_state_t *usbd);
// These are provided externally to implement the CDC interface
struct _usbd_cdc_itf_t;
uint8_t *usbd_cdc_init(struct _usbd_cdc_itf_t *cdc, USBD_HandleTypeDef *pdev);
uint8_t *usbd_cdc_init(struct _usbd_cdc_itf_t *cdc, usbd_cdc_msc_hid_state_t *usbd);
int8_t usbd_cdc_control(struct _usbd_cdc_itf_t *cdc, uint8_t cmd, uint8_t* pbuf, uint16_t length);
int8_t usbd_cdc_receive(struct _usbd_cdc_itf_t *cdc, size_t len);
// These are provided externally to implement the HID interface
struct _usbd_hid_itf_t;
uint8_t *usbd_hid_init(struct _usbd_hid_itf_t *hid, USBD_HandleTypeDef *pdev);
uint8_t *usbd_hid_init(struct _usbd_hid_itf_t *hid, usbd_cdc_msc_hid_state_t *usbd);
int8_t usbd_hid_receive(struct _usbd_hid_itf_t *hid, size_t len);
#endif // _USB_CDC_MSC_CORE_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment