5 #include <debug-uart.h>
6 #include <usb-stm32f103.h>
10 #define PRINTF(...) printf(__VA_ARGS__)
25 #define USB_BASE (APB1PERIPH_BASE + 0x5c00)
26 #define USB_MEM_BASE (APB1PERIPH_BASE + 0x6000)
27 #define USB_MEM_SIZE (512)
28 #define USB ((volatile USB_TypeDef *) USB_BASE)
37 #define USB_EP0_BUF_SIZE (2*CTRL_EP_SIZE)
39 #define USB_EP1_BUF_SIZE (2*USB_EP1_SIZE)
41 #define USB_EP2_BUF_SIZE (2*USB_EP2_SIZE)
43 #define USB_EP3_BUF_SIZE (2*USB_EP3_SIZE)
45 #define USB_EP4_BUF_SIZE (2*USB_EP4_SIZE)
47 #define USB_EP5_BUF_SIZE (2*USB_EP5_SIZE)
49 #define USB_EP6_BUF_SIZE (2*USB_EP6_SIZE)
51 #define USB_EP7_BUF_SIZE (2*USB_EP7_SIZE)
56 #define MAX_CTRL_DATA 128
59 #define ADDR_TX_0 ADDR_TX
60 #define ADDR_TX_1 ADDR_RX
61 #define COUNT_TX_0 COUNT_TX
62 #define COUNT_TX_1 COUNT_RX
65 #define ADDR_RX_0 ADDR_TX
66 #define ADDR_RX_1 ADDR_RX
67 #define COUNT_RX_0 COUNT_TX
68 #define COUNT_RX_1 COUNT_RX
70 #define USB_EPxR_EP_TYPE_BULK 0
71 #define USB_EPxR_EP_TYPE_CONTROL USB_EP0R_EP_TYPE_0
72 #define USB_EPxR_EP_TYPE_ISO USB_EP0R_EP_TYPE_1
73 #define USB_EPxR_EP_TYPE_INTERRUPT (USB_EP0R_EP_TYPE_1|USB_EP0R_EP_TYPE_0)
75 #define USB_EPxR_EP_DBL_BUF USB_EP0R_EP_KIND
76 #define USB_EPxR_EP_STATUS_OUT USB_EP0R_EP_KIND
78 #define USB_EPxR_STAT_RX_DISABLED 0
79 #define USB_EPxR_STAT_RX_STALL USB_EP0R_STAT_RX_0
80 #define USB_EPxR_STAT_RX_NAK USB_EP0R_STAT_RX_1
81 #define USB_EPxR_STAT_RX_VALID (USB_EP0R_STAT_RX_1|USB_EP0R_STAT_RX_0)
83 #define USB_EPxR_STAT_TX_DISABLED 0
84 #define USB_EPxR_STAT_TX_STALL USB_EP0R_STAT_TX_0
85 #define USB_EPxR_STAT_TX_NAK USB_EP0R_STAT_TX_1
86 #define USB_EPxR_STAT_TX_VALID (USB_EP0R_STAT_TX_1|USB_EP0R_STAT_TX_0)
88 #define USB_EPxR_SW_BUF_TX USB_EP0R_DTOG_RX
89 #define USB_EPxR_SW_BUF_RX USB_EP0R_DTOG_TX
91 static const uint16_t ep_buffer_size[8] =
103 #define USB_EP_BUF_SIZE(ep) ep_buffer_size[ep]
104 #define USB_EP_BUF_OFFSET(ep) ep_buffer_offset[ep]
105 #define USB_EP_BUF_ADDR(ep) (u32*)(USB_MEM_BASE + ep_buffer_offset[ep]*2);
106 #define USB_EP_BUF_DESC(ep) ((USB_HW_Buffer*)(USB_MEM_BASE + 16 * (ep)))
108 #define USB_EP0_OFFSET (8*USB_MAX_ENDPOINTS)
109 #define USB_EP1_OFFSET (USB_EP0_OFFSET + USB_EP0_BUF_SIZE)
110 #define USB_EP2_OFFSET (USB_EP1_OFFSET + USB_EP1_BUF_SIZE)
111 #define USB_EP3_OFFSET (USB_EP2_OFFSET + USB_EP2_BUF_SIZE)
112 #define USB_EP4_OFFSET (USB_EP3_OFFSET + USB_EP3_BUF_SIZE)
113 #define USB_EP5_OFFSET (USB_EP4_OFFSET + USB_EP4_BUF_SIZE)
114 #define USB_EP6_OFFSET (USB_EP5_OFFSET + USB_EP5_BUF_SIZE)
115 #define USB_EP7_OFFSET (USB_EP6_OFFSET + USB_EP6_BUF_SIZE)
117 #if (USB_EP7_OFFSET+USB_EP7_BUF_SIZE) > USB_MEM_SIZE
118 #error USB endpoints buffers does not fit in USB memory
120 static const uint16_t ep_buffer_offset[8] =
132 #define USB_EP_BUF_CAPACITY(s) ((((s) <64)?((s)/2):(0x20 | ((s)/64)))<<10)
134 typedef struct _USBEndpoint USBEndpoint;
141 struct process *event_process;
146 #define USB_EP_FLAGS_TYPE_MASK 0x03
147 #define USB_EP_FLAGS_TYPE_BULK 0x00
148 #define USB_EP_FLAGS_TYPE_CONTROL 0x01
149 #define USB_EP_FLAGS_TYPE_ISO 0x02
150 #define USB_EP_FLAGS_TYPE_INTERRUPT 0x03
152 #define IS_EP_TYPE(ep, type) (((ep)->flags & USB_EP_FLAGS_TYPE_MASK) == (type))
153 #define IS_CONTROL_EP(ep) IS_EP_TYPE(ep, USB_EP_FLAGS_TYPE_CONTROL)
154 #define IS_BULK_EP(ep) IS_EP_TYPE(ep, USB_EP_FLAGS_TYPE_BULK)
155 #define IS_INTERRUPT_EP(ep) IS_EP_TYPE(ep, USB_EP_FLAGS_TYPE_INTERRUPT)
157 #define USB_EP_FLAGS_ENABLED 0x04
160 #define USB_EP_FLAGS_RECV_PENDING 0x08
162 #define USB_EP_FLAGS_SETUP_PENDING 0x10
165 #define USB_EP_FLAGS_TRANSMITTING 0x20
168 #define USB_EP_FLAGS_RECEIVING 0x40
172 #define USB_EP_FLAGS_DOUBLE 0x80
193 #define EP_INDEX(addr) ((addr) & 0x7f)
196 #define EP_STRUCT(addr) (&usb_endpoints[EP_INDEX(addr)])
199 #define EP_HW_NUM(addr) ((addr) & 0x7f)
201 #define USB_DISABLE_INT \
202 NVIC_DISABLE_INT(USB_LP_CAN_RX0_IRQChannel);\
203 NVIC_DISABLE_INT(USB_HP_CAN_TX_IRQChannel)
205 #define USB_ENABLE_INT \
206 NVIC_ENABLE_INT(USB_LP_CAN_RX0_IRQChannel);\
207 NVIC_ENABLE_INT(USB_HP_CAN_TX_IRQChannel)
209 static inline uint32_t
210 usb_save_disable_int()
212 uint32_t v = NVIC->ISER[0];
213 NVIC->ICER[0] = (1<<USB_HP_CAN_TX_IRQChannel | 1<<USB_LP_CAN_RX0_IRQChannel);
218 usb_restore_int(uint32_t v)
221 v & (1<<USB_HP_CAN_TX_IRQChannel | 1<<USB_LP_CAN_RX0_IRQChannel);
224 static USBEndpoint usb_endpoints[USB_MAX_ENDPOINTS];
225 struct process *event_process = 0;
226 volatile unsigned int events = 0;
229 notify_process(
unsigned int e)
238 notify_ep_process(USBEndpoint *ep,
unsigned int e)
241 if (ep->event_process) {
251 for (e = 0; e < USB_MAX_ENDPOINTS; e++) {
252 if (usb_endpoints[e].
flags &USB_EP_FLAGS_ENABLED) {
253 USBBuffer *buffer = usb_endpoints[e].buffer;
254 usb_endpoints[e].flags = 0;
256 buffer->flags &= ~USB_BUFFER_SUBMITTED;
257 buffer = buffer->next;
261 usb_arch_setup_control_endpoint(0);
269 RCC->APB1RSTR |= RCC_APB1RSTR_USBRST;
270 RCC->APB2ENR |= (RCC_APB2ENR_AFIOEN | RCC_APB2ENR_IOPAEN);
271 RCC->APB1ENR |= (RCC_APB1ENR_USBEN);
272 RCC->APB1RSTR &= ~RCC_APB1RSTR_USBRST;
274 GPIO_CONF_OUTPUT_PORT(A,11,ALT_PUSH_PULL,50);
275 GPIO_CONF_OUTPUT_PORT(A,12,ALT_PUSH_PULL,50);
276 GPIO_CONF_OUTPUT_PORT(A,10, PUSH_PULL, 2);
277 GPIOA->BSRR = GPIO_BSRR_BR10;
280 USB->CNTR &= ~USB_CNTR_PDWN;
282 for (i = 0; i < 24; i++)
asm(
"nop"::);
284 USB->CNTR &= ~USB_CNTR_FRES;
286 USB->ISTR = ~(USB_ISTR_PMAOVR |USB_ISTR_ERR | USB_ISTR_WKUP | USB_ISTR_SUSP
289 for(i = 0; i < USB_MAX_ENDPOINTS; i++) {
290 usb_endpoints[i].flags = 0;
291 usb_endpoints[i].event_process = 0;
296 GPIOA->BSRR = GPIO_BSRR_BS10;
297 USB->CNTR |= (USB_CNTR_CTRM | USB_CNTR_PMAOVRM | USB_CNTR_ERRM
298 | USB_CNTR_WKUPM| USB_CNTR_SUSPM | USB_CNTR_RESETM);
299 NVIC_SET_PRIORITY(USB_LP_CAN_RX0_IRQChannel, 4);
300 NVIC_ENABLE_INT(USB_LP_CAN_RX0_IRQChannel);
303 #define EPR_RW (USB_EP0R_EP_TYPE|USB_EP0R_EP_KIND|USB_EP0R_EA)
304 #define EPR_W0 (USB_EP0R_CTR_RX|USB_EP0R_CTR_TX)
305 #define EPR_TOGGLE (USB_EP0R_DTOG_RX | USB_EP0R_STAT_RX \
306 | USB_EP0R_DTOG_TX | USB_EP0R_STAT_TX)
308 #define EPR_INVARIANT(epr) ((epr & (EPR_RW)) | EPR_W0)
310 #define EPR_TOGGLE_SET(epr, mask, set) \
311 ((((epr) & (EPR_RW | (mask))) | EPR_W0) ^ (set))
314 usb_arch_setup_endpoint(
unsigned char addr)
316 USBEndpoint *ep = EP_STRUCT(addr);
318 ep->flags = USB_EP_FLAGS_ENABLED;
326 usb_arch_setup_control_endpoint(
unsigned char addr)
328 USB_HW_Buffer *buf_desc;
329 unsigned int ei = EP_HW_NUM(addr);
331 USBEndpoint *ep = EP_STRUCT(addr);
332 usb_arch_setup_endpoint(addr);
333 ep->flags |= USB_EP_FLAGS_TYPE_CONTROL;
335 buf_desc = USB_EP_BUF_DESC(ei);
336 buf_desc->ADDR_TX = USB_EP_BUF_OFFSET(ei);
337 buf_desc->COUNT_TX = USB_EP_BUF_SIZE(ei)/2;
338 buf_desc->ADDR_RX = USB_EP_BUF_OFFSET(ei) + USB_EP_BUF_SIZE(ei)/2;
339 buf_desc->COUNT_RX = USB_EP_BUF_CAPACITY(USB_EP_BUF_SIZE(ei)/2);
340 ep->xfer_size = USB_EP_BUF_SIZE(ei)/2;
341 epr = USB->EPR[EP_HW_NUM(addr)];
343 epr &= ~(USB_EP0R_CTR_RX | USB_EP0R_CTR_TX);
345 epr ^= USB_EPxR_STAT_RX_NAK | USB_EPxR_STAT_TX_NAK;
347 epr = ((epr & ~(USB_EP0R_EP_TYPE | USB_EP0R_EP_KIND))
348 | USB_EPxR_EP_TYPE_CONTROL);
350 epr = ((epr & ~USB_EP0R_EA) | addr);
351 USB->EPR[EP_HW_NUM(addr)] = epr;
355 usb_arch_setup_bulk_endpoint(
unsigned char addr)
357 USB_HW_Buffer *buf_desc;
358 unsigned int ei = EP_HW_NUM(addr);
360 USBEndpoint *ep = EP_STRUCT(addr);
361 usb_arch_setup_endpoint(addr);
362 ep->flags |= USB_EP_FLAGS_TYPE_BULK;
364 buf_desc = USB_EP_BUF_DESC(ei);
365 buf_desc->ADDR_TX = USB_EP_BUF_OFFSET(ei);
366 buf_desc->ADDR_RX = USB_EP_BUF_OFFSET(ei) + USB_EP_BUF_SIZE(ei)/2;
370 buf_desc->COUNT_TX_0 = 0;
371 buf_desc->COUNT_TX_1 = 0;
373 epr ^= USB_EPxR_STAT_TX_VALID;
376 buf_desc->COUNT_RX_0 = USB_EP_BUF_CAPACITY(USB_EP_BUF_SIZE(ei)/2);
377 buf_desc->COUNT_RX_1 = USB_EP_BUF_CAPACITY(USB_EP_BUF_SIZE(ei)/2);
380 epr ^= USB_EPxR_STAT_RX_VALID;
382 ep->xfer_size = USB_EP_BUF_SIZE(ei)/2;
384 epr &= ~(USB_EP0R_CTR_RX | USB_EP0R_CTR_TX);
386 epr = ((epr & ~(USB_EP0R_EP_TYPE | USB_EP0R_EP_KIND))
387 | USB_EPxR_EP_TYPE_BULK | USB_EPxR_EP_DBL_BUF);
389 epr = ((epr & ~USB_EP0R_EA) | addr);
395 usb_arch_setup_interrupt_endpoint(
unsigned char addr)
397 USB_HW_Buffer *buf_desc;
398 unsigned int ei = EP_HW_NUM(addr);
400 USBEndpoint *ep = EP_STRUCT(addr);
401 usb_arch_setup_endpoint(addr);
402 ep->flags |= USB_EP_FLAGS_TYPE_INTERRUPT;
404 epr = USB->EPR[EP_HW_NUM(addr)];
406 buf_desc = USB_EP_BUF_DESC(ei);
409 buf_desc->ADDR_TX = USB_EP_BUF_OFFSET(ei);
410 buf_desc->COUNT_TX = USB_EP_BUF_SIZE(ei);
411 epr ^= USB_EPxR_STAT_TX_NAK;
414 buf_desc->ADDR_RX = USB_EP_BUF_OFFSET(ei);
415 buf_desc->COUNT_RX = USB_EP_BUF_CAPACITY(USB_EP_BUF_SIZE(ei));
416 epr ^= USB_EPxR_STAT_RX_NAK;
418 ep->xfer_size = USB_EP_BUF_SIZE(ei);
420 epr &= ~(USB_EP0R_CTR_RX | USB_EP0R_CTR_TX);
422 epr = ((epr & ~(USB_EP0R_EP_TYPE | USB_EP0R_EP_KIND))
423 | USB_EPxR_EP_TYPE_INTERRUPT);
425 epr = ((epr & ~USB_EP0R_EA) | addr);
426 USB->EPR[EP_HW_NUM(addr)] = epr;
430 usb_arch_disable_endpoint(uint8_t addr)
433 USBEndpoint *ep = EP_STRUCT(addr);
434 ep->flags &= ~USB_EP_FLAGS_ENABLED;
436 epr = USB->EPR[EP_HW_NUM(addr)];
438 epr ^= USB_EPxR_STAT_TX_DISABLED | USB_EPxR_STAT_RX_DISABLED;
440 epr &= ~(USB_EP0R_CTR_RX | USB_EP0R_CTR_TX);
441 USB->EPR[EP_HW_NUM(addr)] = epr;
445 stall_bulk_in(
unsigned int hw_ep)
447 volatile uint32_t *eprp = &USB->EPR[hw_ep];
448 *eprp = (*eprp & (EPR_RW | USB_EP0R_STAT_TX_1)) | EPR_W0;
453 stall_bulk_out(
unsigned int hw_ep)
455 volatile uint32_t *eprp = &USB->EPR[hw_ep];
456 *eprp = (*eprp & ((EPR_RW | USB_EP0R_STAT_RX_1) & ~USB_EP0R_CTR_RX)) |EPR_W0;
457 PRINTF(
"HALT OUT\n");
461 #define USB_READ_BLOCK 0x01
465 #define USB_READ_NOTIFY 0x02
468 #define USB_READ_FAIL 0x04
473 ep_capacity(
unsigned int count)
475 return (((count & USB_COUNT0_RX_NUM_BLOCK)>>10)
476 * ((count & USB_COUNT0_RX_BLSIZE) ? 32 : 2));
481 skip_buffers_until(USBBuffer *buffer,
unsigned int mask,
unsigned int flags,
484 while(buffer && !((buffer->flags & mask) == flags)) {
485 USBBuffer *
next = buffer->next;
486 buffer->flags &= ~USB_BUFFER_SUBMITTED ;
487 buffer->flags |= USB_BUFFER_FAILED;
488 if (buffer->flags & USB_BUFFER_NOTIFY) *resp |= USB_READ_NOTIFY;
495 read_hw_buffer(USBBuffer *buffer,
unsigned int offset,
unsigned int len)
497 #ifdef USB_STM32F103_ENABLE_ALT_COPY
498 if (buffer->flags & USB_BUFFER_ARCH_ALT_COPY) {
499 copy_from_hw_buffer(buffer, offset, len);
503 uint8_t *data = buffer->data;
504 const uint32_t *hw_data = ((u32*)USB_MEM_BASE) + offset/2;
507 *data++ = *hw_data++ >> 8;
511 *((uint16_t*)data) = *hw_data++;
522 #define USB_WRITE_BLOCK 0x01
523 #define USB_WRITE_NOTIFY 0x02
526 write_hw_buffer(USBBuffer *buffer,
unsigned int offset,
unsigned int len)
528 #ifdef USB_STM32F103_ENABLE_ALT_COPY
529 if (buffer->flags & USB_BUFFER_ARCH_ALT_COPY) {
530 copy_to_hw_buffer(buffer, offset, len);
536 if (len == 0)
return;
538 hw_data = ((u32*)USB_MEM_BASE) + offset/2;
541 *hw_data = (*hw_data & 0xff) | (*data++ << 8);
546 *hw_data++ = *((uint16_t*)data) ;
557 get_receive_capacity(USBBuffer *buffer)
559 unsigned int capacity = 0;
560 while(buffer && !(buffer->flags & (USB_BUFFER_IN| USB_BUFFER_SETUP|USB_BUFFER_HALT))) {
561 capacity += buffer->left;
562 buffer = buffer->next;
568 handle_pending_receive(USBEndpoint *ep)
573 unsigned int res = 0;
574 unsigned int hw_offset;
575 unsigned int hw_ep = EP_HW_NUM(ep->addr);
576 USBBuffer *buffer = ep->buffer;
577 unsigned int flags = ep->flags;
578 USB_HW_Buffer *buf_desc = USB_EP_BUF_DESC(hw_ep);
579 PRINTF(
"handle_pending_receive:\n");
580 if (!(flags & USB_EP_FLAGS_ENABLED) || !buffer)
return USB_READ_BLOCK;
581 switch(flags & USB_EP_FLAGS_TYPE_MASK) {
582 case USB_EP_FLAGS_TYPE_CONTROL:
583 len = buf_desc->COUNT_RX & USB_COUNT0_RX_COUNT0_RX;
584 if (flags & USB_EP_FLAGS_SETUP_PENDING) {
587 skip_buffers_until(buffer, USB_BUFFER_SETUP, USB_BUFFER_SETUP, &res);
589 if (!buffer || buffer->left < len) {
590 res |= USB_READ_BLOCK;
594 if (buffer->left < len) {
595 buffer->flags |= USB_BUFFER_FAILED;
596 buffer->flags &= ~USB_BUFFER_SUBMITTED ;
597 if (buffer->flags & USB_BUFFER_NOTIFY) res |= USB_READ_NOTIFY;
598 ep->buffer = buffer->next;
599 res |= USB_READ_FAIL;
603 if (buffer->flags & (USB_BUFFER_SETUP|USB_BUFFER_IN)) {
604 buffer->flags |= USB_BUFFER_FAILED;
606 buffer->flags &= ~USB_BUFFER_SUBMITTED ;
607 if (buffer->flags & USB_BUFFER_NOTIFY) res |= USB_READ_NOTIFY;
608 ep->buffer = buffer->next;
609 res |= USB_READ_FAIL;
615 if (buffer->left > 0) {
616 buffer->flags |= USB_BUFFER_FAILED;
617 res |= USB_READ_FAIL;
619 buffer->flags &= ~USB_BUFFER_SUBMITTED ;
620 if (buffer->flags & USB_BUFFER_NOTIFY) res |= USB_READ_NOTIFY;
621 ep->buffer = buffer->next;
624 if (get_receive_capacity(buffer) < len)
return USB_READ_BLOCK;
626 hw_offset = buf_desc->ADDR_RX;
628 case USB_EP_FLAGS_TYPE_INTERRUPT:
629 len = buf_desc->COUNT_RX & USB_COUNT0_RX_COUNT0_RX;
630 if (get_receive_capacity(buffer) < len)
return USB_READ_BLOCK;
631 hw_offset = buf_desc->ADDR_RX;
633 case USB_EP_FLAGS_TYPE_BULK:
634 if (USB->EPR[hw_ep] & USB_EPxR_SW_BUF_RX) {
635 len = buf_desc->COUNT_RX_1 & USB_COUNT0_RX_COUNT0_RX;
636 hw_offset = buf_desc->ADDR_RX_1;
638 len = buf_desc->COUNT_RX_0 & USB_COUNT0_RX_COUNT0_RX;
639 hw_offset = buf_desc->ADDR_RX_0;
641 if (get_receive_capacity(buffer) < len)
return USB_READ_BLOCK;
643 case USB_EP_FLAGS_TYPE_ISO:
644 len = buf_desc->COUNT_RX & USB_COUNT0_RX_COUNT0_RX;
645 if (get_receive_capacity(buffer) < len)
return USB_READ_BLOCK;
646 hw_offset = buf_desc->ADDR_RX;
649 short_packet = len < ep->xfer_size;
652 if (buffer->left < len) {
658 buffer->left -= copy;
659 read_hw_buffer(buffer, hw_offset, copy);
665 buffer->flags &= ~(USB_BUFFER_SUBMITTED | USB_BUFFER_SHORT_PACKET);
666 if (buffer->flags & USB_BUFFER_NOTIFY) res |= USB_READ_NOTIFY;
668 buffer = buffer->next;
672 buffer->flags |= USB_BUFFER_SHORT_PACKET;
675 if ((buffer->left == 0)
676 || (buffer->flags & USB_BUFFER_PACKET_END)
677 || (short_packet && (buffer->flags & USB_BUFFER_SHORT_END))) {
679 buffer->flags &= ~USB_BUFFER_SUBMITTED;
680 if (buffer->flags & USB_BUFFER_NOTIFY) res |= USB_READ_NOTIFY;
682 buffer = buffer->next;
686 if (IS_BULK_EP(ep)) {
687 USB->EPR[hw_ep] = EPR_INVARIANT(USB->EPR[hw_ep]) | USB_EPxR_SW_BUF_RX;
696 start_receive(USBEndpoint *ep)
698 unsigned int hw_ep = EP_HW_NUM(ep->addr);
699 uint32_t epr = (USB->EPR[hw_ep] | EPR_W0);
700 uint32_t epr_mask = EPR_RW | EPR_W0;
701 switch(ep->flags & USB_EP_FLAGS_TYPE_MASK) {
702 case USB_EP_FLAGS_TYPE_CONTROL:
703 case USB_EP_FLAGS_TYPE_INTERRUPT:
705 unsigned int capacity = get_receive_capacity(ep->buffer);
706 if (capacity <= ep->xfer_size) {
708 epr ^= USB_EPxR_STAT_TX_NAK;
710 epr ^= USB_EPxR_STAT_TX_STALL;
712 epr ^= USB_EPxR_STAT_RX_VALID;
713 epr_mask |= USB_EP0R_STAT_TX | USB_EP0R_STAT_RX;
716 case USB_EP_FLAGS_TYPE_BULK:
717 case USB_EP_FLAGS_TYPE_ISO:
720 ep->flags |= USB_EP_FLAGS_RECEIVING;
721 USB->EPR[hw_ep] = epr & epr_mask;
725 get_transmit_length(USBBuffer *buffer)
727 unsigned int length = 0;
728 while(buffer && (buffer->flags & USB_BUFFER_IN)) {
729 length += buffer->left;
730 buffer = buffer->next;
736 start_transmit(USBEndpoint *ep)
738 unsigned int hw_start;
739 unsigned int res = 0;
740 USBBuffer *buffer = ep->buffer;
742 unsigned int hw_offset;
743 volatile uint32_t *hw_countp;
744 unsigned int hw_ep = EP_HW_NUM(ep->addr);
745 uint32_t epr = USB->EPR[hw_ep];
746 unsigned int ep_flags = ep->flags;
747 USB_HW_Buffer *buf_desc = USB_EP_BUF_DESC(hw_ep);
749 if (!(ep_flags & USB_EP_FLAGS_ENABLED) || !buffer)
return USB_WRITE_BLOCK;
751 switch(ep_flags & USB_EP_FLAGS_TYPE_MASK) {
752 case USB_EP_FLAGS_TYPE_CONTROL:
754 if (get_transmit_length(ep->buffer) <= len) {
756 USB->EPR[hw_ep] = USB_EPxR_EP_STATUS_OUT
757 | EPR_TOGGLE_SET(epr, USB_EP0R_STAT_RX, USB_EPxR_STAT_RX_NAK);
759 USB->EPR[hw_ep] = USB_EPxR_EP_STATUS_OUT
760 | EPR_TOGGLE_SET(epr, USB_EP0R_STAT_RX, USB_EPxR_STAT_RX_STALL);
762 hw_offset = buf_desc->ADDR_TX;
763 hw_countp = &buf_desc->COUNT_TX;
765 case USB_EP_FLAGS_TYPE_BULK:
766 if (buffer->flags & USB_BUFFER_HALT) {
767 if (ep->status & 0x01)
return USB_WRITE_BLOCK;
769 stall_bulk_in(hw_ep);
770 return USB_WRITE_BLOCK;
772 if (USB->EPR[hw_ep] & USB_EPxR_SW_BUF_TX) {
773 hw_offset = buf_desc->ADDR_TX_1;
774 hw_countp = &buf_desc->COUNT_TX_1;
776 hw_offset = buf_desc->ADDR_TX_0;
777 hw_countp = &buf_desc->COUNT_TX_0;
781 hw_start = hw_offset;
784 if (buffer->left < len) {
790 buffer->left -= copy;
791 write_hw_buffer(buffer, hw_offset, copy);
793 if (buffer->left == 0) {
794 if (buffer->flags & USB_BUFFER_SHORT_END) {
803 buffer->flags &= ~USB_BUFFER_SUBMITTED;
804 if (buffer->flags & USB_BUFFER_NOTIFY) res = USB_WRITE_NOTIFY;
806 buffer = buffer->next;
811 if (ep->flags & USB_EP_FLAGS_TRANSMITTING) {
812 ep->flags |= USB_EP_FLAGS_DOUBLE;
814 ep->flags |= USB_EP_FLAGS_TRANSMITTING;
816 *hw_countp = hw_offset - hw_start;
818 switch(ep->flags & USB_EP_FLAGS_TYPE_MASK) {
819 case USB_EP_FLAGS_TYPE_CONTROL:
820 case USB_EP_FLAGS_TYPE_INTERRUPT:
822 EPR_TOGGLE_SET(epr, USB_EP0R_STAT_TX, USB_EPxR_STAT_TX_VALID);
824 case USB_EP_FLAGS_TYPE_BULK:
825 USB->EPR[hw_ep] = EPR_INVARIANT(USB->EPR[hw_ep]) | USB_EPxR_SW_BUF_TX;
827 case USB_EP_FLAGS_TYPE_ISO:
835 start_transfer(USBEndpoint *ep)
839 if (!(ep->addr & 0x80) && (IS_BULK_EP(ep) || IS_INTERRUPT_EP(ep))) {
840 if (ep->buffer && (ep->buffer->flags & USB_BUFFER_HALT)) {
841 if (ep->status & 0x01) return ;
843 stall_bulk_out(EP_HW_NUM(ep->addr));
847 if (!(ep->flags & USB_EP_FLAGS_RECV_PENDING))
break;
848 res = handle_pending_receive(ep);
849 if (res & USB_READ_NOTIFY) {
850 notify_ep_process(ep, USB_EP_EVENT_NOTIFICATION);
852 if (res & USB_READ_BLOCK)
return;
853 if (ep->flags & USB_EP_FLAGS_DOUBLE) {
854 ep->flags &= ~USB_EP_FLAGS_DOUBLE;
856 ep->flags &= ~(USB_EP_FLAGS_RECV_PENDING|USB_EP_FLAGS_SETUP_PENDING);
858 if (res & USB_READ_FAIL) {
860 usb_arch_control_stall(ep->addr);
864 if (ep->addr == 0x02)
865 PRINTF(
"start EPR: %04x ep->flags: %02x\n",
866 (
unsigned int)USB->EPR[EP_HW_NUM(ep->addr)],
867 (
unsigned int)ep->flags);
868 if (ep->flags & (USB_EP_FLAGS_TRANSMITTING | USB_EP_FLAGS_RECEIVING)) {
869 if (!IS_BULK_EP(ep) || (ep->flags & USB_EP_FLAGS_DOUBLE)) {
874 if (ep->status & 0x01)
return;
876 if (ep->buffer->flags & USB_BUFFER_IN) {
877 res = start_transmit(ep);
878 if (res & USB_READ_NOTIFY) {
879 notify_ep_process(ep, USB_EP_EVENT_NOTIFICATION);
889 transfer_complete(
unsigned int hw_ep) {
890 uint32_t epr = USB->EPR[hw_ep];
891 USBEndpoint *ep = &usb_endpoints[hw_ep];
892 if (epr &USB_EP0R_CTR_RX) {
893 PRINTF(
"Received packet %lx %04x\n", USB_EP_BUF_DESC(hw_ep)->COUNT_RX, (
int)USB->EPR[hw_ep]);
894 if (epr & USB_EP0R_SETUP) {
896 ep->flags |= USB_EP_FLAGS_SETUP_PENDING;
899 if (IS_BULK_EP(ep)) {
900 if ((epr ^ (epr >> 8)) & USB_EP0R_DTOG_TX) {
901 ep->flags &= ~USB_EP_FLAGS_DOUBLE;
903 ep->flags |= USB_EP_FLAGS_DOUBLE;
904 ep->flags &= ~USB_EP_FLAGS_RECEIVING;
907 ep->flags &= ~USB_EP_FLAGS_RECEIVING;
909 ep->flags |= USB_EP_FLAGS_RECV_PENDING;
910 if (IS_CONTROL_EP(ep)) epr &= ~USB_EPxR_EP_STATUS_OUT;
911 USB->EPR[hw_ep] = EPR_INVARIANT(epr) & ~USB_EP0R_CTR_RX;
913 if (ep->flags & USB_EP_FLAGS_DOUBLE) {
920 if (epr &USB_EP0R_CTR_TX) {
921 PRINTF(
"Sent packet\n");
922 if (ep->flags & USB_EP_FLAGS_DOUBLE) {
923 ep->flags &= ~USB_EP_FLAGS_DOUBLE;
925 ep->flags &= ~USB_EP_FLAGS_TRANSMITTING;
927 USB->EPR[hw_ep] = EPR_INVARIANT(USB->EPR[hw_ep]) & ~USB_EP0R_CTR_TX;
935 usb_set_ep_event_process(
unsigned char addr,
struct process *p)
937 USBEndpoint *ep = &usb_endpoints[EP_INDEX(addr)];
938 ep->event_process = p;
943 usb_arch_set_global_event_process(
struct process *p)
949 usb_arch_get_global_events(
void)
960 usb_get_ep_events(
unsigned char addr)
963 unsigned int ei = EP_HW_NUM(addr);
965 e = usb_endpoints[ei].events;
966 usb_endpoints[ei].events = 0;
973 usb_submit_recv_buffer(
unsigned char ep_addr, USBBuffer *buffer)
976 USBEndpoint *ep = &usb_endpoints[EP_INDEX(ep_addr)];
977 if (!(ep->flags & USB_EP_FLAGS_ENABLED))
return;
981 tailp = (USBBuffer**)&ep->buffer;
983 tailp = &(*tailp)->next;
987 buffer->flags |= USB_BUFFER_SUBMITTED;
988 buffer = buffer->next;
996 usb_submit_xmit_buffer(
unsigned char ep_addr, USBBuffer *buffer)
999 USBEndpoint *ep = &usb_endpoints[EP_INDEX(ep_addr)];
1000 if (!(ep->flags & USB_EP_FLAGS_ENABLED))
return;
1003 tailp = (USBBuffer**)&ep->buffer;
1005 tailp = &(*tailp)->next;
1009 buffer->flags |= USB_BUFFER_SUBMITTED | USB_BUFFER_IN;
1010 buffer = buffer->next;
1017 usb_arch_discard_all_buffers(
unsigned char ep_addr)
1021 volatile USBEndpoint *ep = &usb_endpoints[EP_INDEX(ep_addr)];
1022 ints = usb_save_disable_int();
1023 buffer = ep->buffer;
1027 USB->EPR[EP_HW_NUM(ep_addr)] =
1028 EPR_TOGGLE_SET(USB->EPR[EP_HW_NUM(ep_addr)],
1029 USB_EP0R_STAT_RX|USB_EP0R_STAT_TX,
1030 USB_EPxR_STAT_TX_NAK| USB_EPxR_STAT_RX_NAK);
1031 ep->flags &= ~(USB_EP_FLAGS_RECV_PENDING|USB_EP_FLAGS_SETUP_PENDING);
1033 ep->flags &= ~(USB_EP_FLAGS_RECV_PENDING | USB_EP_FLAGS_SETUP_PENDING
1034 | USB_EP_FLAGS_DOUBLE);
1035 usb_restore_int(ints);
1037 buffer->flags &= ~USB_BUFFER_SUBMITTED;
1038 buffer = buffer->next;
1042 usb_arch_get_ep_status(uint8_t addr)
1044 if (EP_INDEX(addr) > USB_MAX_ENDPOINTS)
return 0;
1045 return usb_endpoints[EP_INDEX(addr)].status;
1049 usb_arch_set_configuration(uint8_t usb_configuration_value)
1055 usb_arch_control_stall(
unsigned char addr)
1057 if (EP_INDEX(addr) > USB_MAX_ENDPOINTS)
return;
1058 uint32_t epr = USB->EPR[EP_HW_NUM(addr)];
1059 USB->EPR[EP_HW_NUM(addr)] = EPR_TOGGLE_SET(epr,USB_EP0R_STAT_RX|USB_EP0R_STAT_TX, USB_EPxR_STAT_RX_STALL | USB_EPxR_STAT_TX_STALL);
1064 usb_arch_halt_endpoint(
unsigned char ep_addr,
int halt)
1066 if (EP_INDEX(ep_addr) > USB_MAX_ENDPOINTS)
return;
1067 if (!usb_endpoints[EP_INDEX(ep_addr)].flags & USB_EP_FLAGS_ENABLED)
return;
1070 if (!(usb_endpoints[EP_INDEX(ep_addr)].status & 0x01)) {
1071 usb_endpoints[EP_INDEX(ep_addr)].status |= 0x01;
1072 if (ep_addr & 0x80) {
1073 stall_bulk_in(EP_HW_NUM(ep_addr));
1075 stall_bulk_out(EP_HW_NUM(ep_addr));
1079 USBEndpoint *ep = &usb_endpoints[EP_INDEX(ep_addr)];
1080 if (ep->status & 0x01) {
1081 ep->status &= ~0x01;
1082 PRINTF(
"HALT clear restart EPR: %04x %p %p\n",
1083 (
unsigned int)USB->EPR[EP_HW_NUM(ep_addr)],
1084 ep->buffer, ep->buffer->next);
1086 if (IS_BULK_EP(ep)) {
1087 volatile uint32_t *eprp = &USB->EPR[EP_HW_NUM(ep_addr)];
1088 if (ep_addr & 0x80) {
1089 ep->flags &= ~(USB_EP_FLAGS_DOUBLE |USB_EP_FLAGS_TRANSMITTING);
1091 *eprp =(EPR_TOGGLE_SET(*eprp,(USB_EP0R_STAT_TX | USB_EP0R_DTOG_TX
1092 | USB_EPxR_SW_BUF_TX),
1093 USB_EPxR_STAT_TX_VALID));
1095 ep->flags &= ~(USB_EP_FLAGS_DOUBLE | USB_EP_FLAGS_RECEIVING
1096 | USB_EP_FLAGS_RECV_PENDING);
1098 *eprp =(EPR_TOGGLE_SET(*eprp,(USB_EP0R_STAT_RX | USB_EP0R_DTOG_RX
1099 | USB_EPxR_SW_BUF_RX),
1100 USB_EPxR_STAT_RX_VALID|USB_EPxR_SW_BUF_RX));
1101 *eprp = EPR_INVARIANT(*eprp) | USB_EPxR_SW_BUF_RX;
1106 if (ep->buffer && (ep->buffer->flags & USB_BUFFER_HALT)) {
1107 ep->buffer->flags &= ~USB_BUFFER_SUBMITTED;
1108 if (ep->buffer->flags & USB_BUFFER_NOTIFY) {
1109 notify_ep_process(ep,USB_EP_EVENT_NOTIFICATION);
1111 ep->buffer = ep->buffer->next;
1114 start_transfer(&usb_endpoints[EP_INDEX(ep_addr)]);
1115 PRINTF(
"HALT clear restart EPR: %04x %p %p\n",
1116 (
unsigned int)USB->EPR[EP_HW_NUM(ep_addr)],
1117 ep->buffer, ep->buffer->next);
1125 usb_arch_set_address(
unsigned char addr)
1127 USB->DADDR = 0x80 | addr;
1134 USB_HP_CAN_TX_handler(
void)
1136 uint32_t status = USB->ISTR;
1137 if (status & USB_ISTR_CTR) {
1138 transfer_complete(status & USB_ISTR_EP_ID);
1145 USB_LP_CAN_RX0_handler(
void)
1147 uint32_t status = USB->ISTR;
1148 if (status & USB_ISTR_CTR) {
1149 transfer_complete(status & USB_ISTR_EP_ID);
1151 }
else if (status & USB_ISTR_PMAOVR) {
1153 USB->ISTR &= ~USB_ISTR_PMAOVR;
1154 }
else if (status & USB_ISTR_ERR) {
1156 USB->ISTR &= ~USB_ISTR_ERR;
1157 }
else if (status & USB_ISTR_WKUP) {
1159 USB->ISTR &= ~USB_ISTR_WKUP;
1160 USB->CNTR &= ~USB_CNTR_FSUSP;
1161 notify_process(USB_EVENT_RESUME);
1162 }
else if (status & USB_ISTR_SUSP) {
1164 USB->ISTR &= ~USB_ISTR_SUSP;
1165 USB->CNTR |= USB_CNTR_FSUSP;
1166 notify_process(USB_EVENT_SUSPEND);
1167 }
else if (status & USB_ISTR_RESET) {
1169 USB->ISTR &= ~USB_ISTR_RESET;
1171 notify_process(USB_EVENT_RESET);
1176 usb_arch_toggle_SW_BUF_RX()
1178 USB->EPR[2] = EPR_INVARIANT(USB->EPR[2]) | USB_EPxR_SW_BUF_RX;
1182 usb_arch_send_pending(uint8_t ep_addr)
1184 return usb_endpoints[EP_INDEX(ep_addr)].flags & USB_EP_FLAGS_TRANSMITTING;