mirror of
https://github.com/polhenarejos/pico-keys-sdk
synced 2026-05-08 14:06:11 +02:00
More and more fixes.
This commit is contained in:
@@ -149,7 +149,7 @@ static inline uint32_t usb_buffer_offset(volatile uint8_t *buf) {
|
||||
* @param ep
|
||||
*/
|
||||
void usb_setup_endpoint(const struct usb_endpoint_configuration *ep) {
|
||||
printf("Set up endpoint 0x%x with buffer address 0x%p\n", ep->descriptor->bEndpointAddress, ep->data_buffer);
|
||||
//printf("Set up endpoint 0x%x with buffer address 0x%p\n", ep->descriptor->bEndpointAddress, ep->data_buffer);
|
||||
|
||||
// EP0 doesn't have one so return if that is the case
|
||||
if (!ep->endpoint_control) {
|
||||
@@ -243,7 +243,7 @@ void usb_start_transfer(struct usb_endpoint_configuration *ep, uint8_t *buf, uin
|
||||
// For multi packet transfers see the tinyusb port.
|
||||
assert(len <= 64);
|
||||
|
||||
printf("Start transfer of len %d on ep addr 0x%x\n", len, ep->descriptor->bEndpointAddress);
|
||||
//printf("Start transfer of len %d on ep addr 0x%x\n", len, ep->descriptor->bEndpointAddress);
|
||||
|
||||
// Prepare buffer control register value
|
||||
uint32_t val = len | USB_BUF_CTRL_AVAIL;
|
||||
@@ -331,12 +331,6 @@ bool usb_is_configured(void) {
|
||||
return configured;
|
||||
}
|
||||
|
||||
uint32_t usb_write(uint8_t *buffer, size_t buffer_size) {
|
||||
struct usb_endpoint_configuration *ep = usb_get_endpoint_configuration(EP2_IN_ADDR);
|
||||
usb_start_transfer(ep, buffer, MIN(buffer_size, 64));
|
||||
return MIN(buffer_size, 64);
|
||||
}
|
||||
|
||||
void usb_init() {
|
||||
usb_device_init();
|
||||
|
||||
@@ -491,7 +485,7 @@ static void usb_handle_ep_buff_done(struct usb_endpoint_configuration *ep) {
|
||||
*/
|
||||
static void usb_handle_buff_done(uint ep_num, bool in) {
|
||||
uint8_t ep_addr = ep_num | (in ? USB_DIR_IN : 0);
|
||||
printf("EP %d (in = %d) done\n", ep_num, in);
|
||||
//printf("EP %d (in = %d) done\n", ep_num, in);
|
||||
for (uint i = 0; i < USB_NUM_ENDPOINTS; i++) {
|
||||
struct usb_endpoint_configuration *ep = &dev_config.endpoints[i];
|
||||
if (ep->descriptor && ep->handler) {
|
||||
@@ -551,7 +545,7 @@ void isr_usbctrl(void) {
|
||||
|
||||
// Bus is reset
|
||||
if (status & USB_INTS_BUS_RESET_BITS) {
|
||||
printf("BUS RESET\n");
|
||||
//printf("BUS RESET\n");
|
||||
handled |= USB_INTS_BUS_RESET_BITS;
|
||||
usb_hw_clear->sie_status = USB_SIE_STATUS_BUS_RESET_BITS;
|
||||
usb_bus_reset();
|
||||
@@ -585,37 +579,72 @@ void ep0_out_handler(uint8_t *buf, uint16_t len) {
|
||||
;
|
||||
}
|
||||
|
||||
#define DEBUG_PAYLOAD(p,s) { \
|
||||
printf("Payload %s (%d bytes):\r\n", #p,s);\
|
||||
for (int i = 0; i < s; i += 16) {\
|
||||
printf("%07Xh : ",i+p);\
|
||||
for (int j = 0; j < 16; j++) {\
|
||||
if (j < s-i) printf("%02X ",(p)[i+j]);\
|
||||
else printf(" ");\
|
||||
if (j == 7) printf(" ");\
|
||||
} printf(": "); \
|
||||
for (int j = 0; j < MIN(16,s-i); j++) {\
|
||||
printf("%c",(p)[i+j] == 0x0a || (p)[i+j] == 0x0d ? '\\' : (p)[i+j]);\
|
||||
if (j == 7) printf(" ");\
|
||||
}\
|
||||
printf("\r\n");\
|
||||
} printf("\r\n"); \
|
||||
}
|
||||
|
||||
// Device specific functions
|
||||
static uint8_t rx_buffer[4096];
|
||||
static uint8_t rx_buffer[4096], tx_buffer[4096];
|
||||
static uint16_t w_offset = 0, r_offset = 0;
|
||||
|
||||
void ep1_out_handler(uint8_t *buf, uint16_t len) {
|
||||
printf("RX %d bytes from host\n", len);
|
||||
DEBUG_PAYLOAD(buf,len);
|
||||
// Send data back to host
|
||||
uint16_t size = MIN(sizeof(rx_buffer)-w_offset,len);
|
||||
if (size > 0) {
|
||||
memcpy(rx_buffer+w_offset, buf, size);
|
||||
w_offset += size;
|
||||
}
|
||||
/*
|
||||
do {
|
||||
uint16_t size = MIN(sizeof(rx_buffer)-w_offset,len);
|
||||
memcpy(rx_buffer+w_offset, buf, size);
|
||||
len -= size;
|
||||
w_offset += size;
|
||||
if (w_offset == sizeof(rx_buffer))
|
||||
w_offset = 0;
|
||||
buf += size;
|
||||
} while (len > 0);
|
||||
*/
|
||||
//printf("rx_w_offset %d, rx_r_offset %d\r\n",w_offset, r_offset);
|
||||
}
|
||||
|
||||
uint32_t usb_write(uint16_t size) {
|
||||
return usb_write_offset(size, 0);
|
||||
}
|
||||
|
||||
uint32_t usb_write_offset(uint16_t size, uint16_t roffset) {
|
||||
if (size > sizeof(tx_buffer))
|
||||
size = sizeof(tx_buffer);
|
||||
struct usb_endpoint_configuration *ep = usb_get_endpoint_configuration(EP2_IN_ADDR);
|
||||
for (uint16_t offset = roffset; offset-roffset < size; offset += 64) {
|
||||
usb_start_transfer(ep, tx_buffer+offset, MIN(size-offset, 64));
|
||||
}
|
||||
usb_start_transfer(usb_get_endpoint_configuration(EP1_OUT_ADDR), NULL, 64);
|
||||
return size;
|
||||
}
|
||||
uint16_t usb_read_available() {
|
||||
return w_offset - r_offset;
|
||||
}
|
||||
|
||||
uint8_t *usb_get_rx() {
|
||||
return rx_buffer;
|
||||
}
|
||||
uint8_t *usb_get_tx() {
|
||||
return tx_buffer;
|
||||
}
|
||||
|
||||
void usb_clear_rx() {
|
||||
w_offset = r_offset = 0;
|
||||
}
|
||||
|
||||
|
||||
uint16_t usb_read(uint8_t *buffer, size_t buffer_size) {
|
||||
if (w_offset-r_offset > 0) {
|
||||
uint16_t size = MIN(buffer_size, w_offset-r_offset);
|
||||
uint16_t size = MIN(buffer_size, w_offset-r_offset);
|
||||
if (size > 0) {
|
||||
memcpy(buffer, rx_buffer+r_offset, size);
|
||||
r_offset += size;
|
||||
if (r_offset == w_offset) {
|
||||
@@ -627,9 +656,9 @@ uint16_t usb_read(uint8_t *buffer, size_t buffer_size) {
|
||||
}
|
||||
|
||||
void ep2_in_handler(uint8_t *buf, uint16_t len) {
|
||||
printf("Sent %d bytes to host\n", len);
|
||||
//printf("Sent %d bytes to host\n", len);
|
||||
// Get ready to rx again from host
|
||||
usb_start_transfer(usb_get_endpoint_configuration(EP1_OUT_ADDR), NULL, 64);
|
||||
//usb_start_transfer(usb_get_endpoint_configuration(EP1_OUT_ADDR), NULL, 64);
|
||||
}
|
||||
/*
|
||||
int main(void) {
|
||||
|
||||
@@ -317,9 +317,14 @@ static const unsigned char *descriptor_strings[] = {
|
||||
|
||||
extern uint16_t usb_read(uint8_t *buffer, size_t buffer_size);
|
||||
extern uint16_t usb_read_available();
|
||||
extern uint32_t usb_write(uint8_t *buffer, size_t buffer_size);
|
||||
extern uint32_t usb_write_offset(uint16_t size, uint16_t offset);
|
||||
extern uint32_t usb_write(uint16_t size);
|
||||
extern bool usb_is_configured();
|
||||
extern void usb_init();
|
||||
extern uint8_t *usb_get_rx();
|
||||
extern uint32_t usb_send_tx_buffer();
|
||||
extern uint8_t *usb_get_tx();
|
||||
extern void usb_clear_rx();
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Reference in New Issue
Block a user