#include <stm32h747xx.h>
#include "usb.h"
#ifndef USB_DEVICE_H
#define USB_DEVICE_H
typedef enum {
// error states first so they are lowest
CONTROL_ERROR = -4,
UNKNOWN_CONTROL_COMMAND = -4,
ERROR = -3,
UNKNOWN_INTERRUPT = -2,
OTG_ERROR = -1, // shouldn't happen, since otg is not used
INIT = 0,
RESET = 1,
RESET_DONE = 2,
SET_ADDRESS_RCVD = 3,
ENUMERATED = 4,
} usb_device_state_t;
typedef struct {
volatile uint32_t data[128];
} usb_fifo_t;
typedef struct {
USB_OTG_GlobalTypeDef *core;
USB_OTG_DeviceTypeDef *device;
USB_OTG_OUTEndpointTypeDef *out;
USB_OTG_INEndpointTypeDef *in;
usb_fifo_t *fifos;
uint8_t endpoint_count;
usb_class_t class;
usb_device_state_t state;
// TODO: is this count field required?
uint8_t received_setup_commands_count;
uint8_t received_setup_commands_index;
usb_setup_command_t received_setup_commands[3];
} usb_device_t;
// has configuration etc
#define USB_DEVICE_SIZE sizeof(usb_device_t)
extern usb_device_t* usb1_device;
void* usb_device_init(void* peripheral_address, usb_class_t* class, void* buffer);
void usb_device_setup(void* device);
void usb_device_wait_for_handshake(void* device);
#endif // USB_DEVICE_H