2 * Copyright © 2018 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 #include "ao_usb_gen.h"
21 static uint8_t ao_usb_ep0_state;
23 /* Pending EP0 IN data */
24 static const uint8_t *ao_usb_ep0_in_data; /* Remaining data */
25 static uint8_t ao_usb_ep0_in_len; /* Remaining amount */
27 /* Temp buffer for smaller EP0 in data */
28 static uint8_t ao_usb_ep0_in_buf[2];
30 /* Pending EP0 OUT data */
31 static uint8_t *ao_usb_ep0_out_data;
32 static uint8_t ao_usb_ep0_out_len;
34 /* System ram shadow of USB buffer; writing individual bytes is
35 * too much of a pain (sigh) */
36 static uint8_t ao_usb_tx_buffer[AO_USB_IN_SIZE];
37 static uint8_t ao_usb_tx_count;
39 static uint8_t ao_usb_rx_buffer[AO_USB_OUT_SIZE];
40 static uint8_t ao_usb_rx_count, ao_usb_rx_pos;
43 * End point register indices
46 #define AO_USB_CONTROL_EPR 0
47 #define AO_USB_INT_EPR 1
48 #define AO_USB_OUT_EPR 2
49 #define AO_USB_IN_EPR 3
51 /* Marks when we don't need to send an IN packet.
52 * This happens only when the last IN packet is not full,
53 * otherwise the host will expect to keep seeing packets.
54 * Send a zero-length packet as required
56 static uint8_t ao_usb_in_flushed;
58 /* Marks when we have delivered an IN packet to the hardware
59 * and it has not been received yet. ao_sleep on this address
60 * to wait for it to be delivered.
62 static uint8_t ao_usb_in_pending;
64 /* Marks when an OUT packet has been received by the hardware
65 * but not pulled to the shadow buffer.
67 static uint8_t ao_usb_out_avail;
68 uint8_t ao_usb_running;
69 static uint8_t ao_usb_configuration;
71 static uint8_t ao_usb_address;
72 static uint8_t ao_usb_address_pending;
75 * Set current device address and mark the
79 ao_usb_set_address(uint8_t address)
81 ao_usb_dev_set_address(address);
82 ao_usb_address_pending = 0;
89 #define _tx_dbg0(msg) _dbg(__LINE__,msg,0)
90 #define _tx_dbg1(msg,value) _dbg(__LINE__,msg,value)
93 #define _tx_dbg1(msg,value)
97 #define _rx_dbg0(msg) _dbg(__LINE__,msg,0)
98 #define _rx_dbg1(msg,value) _dbg(__LINE__,msg,value)
100 #define _rx_dbg0(msg)
101 #define _rx_dbg1(msg,value)
105 static void _dbg(int line, char *msg, uint32_t value);
109 * Set just endpoint 0, for use during startup
115 ao_usb_dev_ep0_init();
117 ao_usb_set_address(0);
121 /* Reset our internal state
124 ao_usb_ep0_state = AO_USB_EP0_IDLE;
126 ao_usb_ep0_in_data = NULL;
127 ao_usb_ep0_in_len = 0;
129 ao_usb_ep0_out_data = 0;
130 ao_usb_ep0_out_len = 0;
134 ao_usb_set_configuration(void)
137 /* Set up the INT end point */
138 ao_usb_bdt[AO_USB_INT_EPR].single.addr_tx = ao_usb_sram_addr;
139 ao_usb_bdt[AO_USB_INT_EPR].single.count_tx = 0;
140 ao_usb_in_tx_buffer = ao_usb_packet_buffer_addr(ao_usb_sram_addr);
141 ao_usb_sram_addr += AO_USB_INT_SIZE;
143 ao_usb_init_ep(AO_USB_INT_EPR,
146 /* Set up the OUT end point */
147 ao_usb_bdt[AO_USB_OUT_EPR].single.addr_rx = ao_usb_sram_addr;
148 ao_usb_bdt[AO_USB_OUT_EPR].single.count_rx = ((1 << STM_USB_BDT_COUNT_RX_BL_SIZE) |
149 (((AO_USB_OUT_SIZE / 32) - 1) << STM_USB_BDT_COUNT_RX_NUM_BLOCK));
150 ao_usb_out_rx_buffer = ao_usb_packet_buffer_addr(ao_usb_sram_addr);
151 ao_usb_sram_addr += AO_USB_OUT_SIZE;
153 ao_usb_init_ep(AO_USB_OUT_EPR,
155 STM_USB_EPR_EP_TYPE_BULK,
156 STM_USB_EPR_STAT_RX_VALID,
157 STM_USB_EPR_STAT_TX_DISABLED);
159 /* Set up the IN end point */
160 ao_usb_bdt[AO_USB_IN_EPR].single.addr_tx = ao_usb_sram_addr;
161 ao_usb_bdt[AO_USB_IN_EPR].single.count_tx = 0;
162 ao_usb_in_tx_buffer = ao_usb_packet_buffer_addr(ao_usb_sram_addr);
163 ao_usb_sram_addr += AO_USB_IN_SIZE;
165 ao_usb_init_ep(AO_USB_IN_EPR,
167 STM_USB_EPR_EP_TYPE_BULK,
168 STM_USB_EPR_STAT_RX_DISABLED,
169 STM_USB_EPR_STAT_TX_NAK);
172 ao_usb_in_flushed = 0;
173 ao_usb_in_pending = 0;
174 ao_wakeup(&ao_usb_in_pending);
176 ao_usb_out_avail = 0;
177 ao_usb_configuration = 0;
180 ao_wakeup(&ao_usb_running);
183 static uint16_t control_count;
184 static uint16_t in_count;
185 static uint16_t out_count;
187 static uint16_t int_count;
188 static uint16_t reset_count;
191 /* Send an IN data packet */
193 ao_usb_ep0_flush(void)
197 /* Check to see if the endpoint is still busy */
198 if (ao_usb_dev_ep0_in_busy()) {
202 this_len = ao_usb_ep0_in_len;
203 if (this_len > AO_USB_CONTROL_SIZE)
204 this_len = AO_USB_CONTROL_SIZE;
206 if (this_len < AO_USB_CONTROL_SIZE)
207 ao_usb_ep0_state = AO_USB_EP0_IDLE;
209 ao_usb_ep0_in_len -= this_len;
211 ao_usb_dev_ep0_in(ao_usb_ep0_in_data, this_len);
212 ao_usb_ep0_in_data += this_len;
215 /* Read data from the ep0 OUT fifo */
217 ao_usb_ep0_fill(void)
221 len = ao_usb_dev_ep0_out(ao_usb_ep0_out_data, ao_usb_ep0_out_len);
222 ao_usb_ep0_out_len -= len;
223 ao_usb_ep0_out_data += len;
227 ao_usb_ep0_in_reset(void)
229 ao_usb_ep0_in_data = ao_usb_ep0_in_buf;
230 ao_usb_ep0_in_len = 0;
234 ao_usb_ep0_in_queue_byte(uint8_t a)
236 if (ao_usb_ep0_in_len < sizeof (ao_usb_ep0_in_buf))
237 ao_usb_ep0_in_buf[ao_usb_ep0_in_len++] = a;
241 ao_usb_ep0_in_set(const uint8_t *data, uint8_t len)
243 ao_usb_ep0_in_data = data;
244 ao_usb_ep0_in_len = len;
248 ao_usb_ep0_out_set(uint8_t *data, uint8_t len)
250 ao_usb_ep0_out_data = data;
251 ao_usb_ep0_out_len = len;
255 ao_usb_ep0_in_start(uint16_t max)
257 /* Don't send more than asked for */
258 if (ao_usb_ep0_in_len > max)
259 ao_usb_ep0_in_len = max;
261 ao_usb_dev_ep0_in(ao_usb_ep0_in_data, ao_usb_ep0_in_len);
264 struct ao_usb_line_coding ao_usb_line_coding = {115200, 0, 0, 8};
266 /* Walk through the list of descriptors and find a match
269 ao_usb_get_descriptor(uint16_t value, uint16_t length)
271 const uint8_t *descriptor;
272 uint8_t type = value >> 8;
273 uint8_t index = value;
275 descriptor = ao_usb_descriptors;
276 while (descriptor[0] != 0) {
277 if (descriptor[1] == type && index-- == 0) {
279 if (type == AO_USB_DESC_CONFIGURATION)
285 ao_usb_ep0_in_set(descriptor, len);
288 descriptor += descriptor[0];
293 ao_usb_ep0_setup(void)
297 /* Pull the setup packet out of the fifo */
298 setup_len = ao_usb_dev_ep0_out(&ao_usb_setup, 8);
299 if (setup_len != 8) {
303 if ((ao_usb_setup.dir_type_recip & AO_USB_DIR_IN) || ao_usb_setup.length == 0)
304 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
306 ao_usb_ep0_state = AO_USB_EP0_DATA_OUT;
308 ao_usb_ep0_in_reset();
310 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_TYPE_MASK) {
311 case AO_USB_TYPE_STANDARD:
312 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_RECIP_MASK) {
313 case AO_USB_RECIP_DEVICE:
314 switch(ao_usb_setup.request) {
315 case AO_USB_REQ_GET_STATUS:
316 ao_usb_ep0_in_queue_byte(0);
317 ao_usb_ep0_in_queue_byte(0);
319 case AO_USB_REQ_SET_ADDRESS:
320 ao_usb_address = ao_usb_setup.value;
321 ao_usb_address_pending = 1;
323 case AO_USB_REQ_GET_DESCRIPTOR:
324 ao_usb_get_descriptor(ao_usb_setup.value, ao_usb_setup.length);
326 case AO_USB_REQ_GET_CONFIGURATION:
327 ao_usb_ep0_in_queue_byte(ao_usb_configuration);
329 case AO_USB_REQ_SET_CONFIGURATION:
330 ao_usb_configuration = ao_usb_setup.value;
331 ao_usb_set_configuration();
335 case AO_USB_RECIP_INTERFACE:
336 switch(ao_usb_setup.request) {
337 case AO_USB_REQ_GET_STATUS:
338 ao_usb_ep0_in_queue_byte(0);
339 ao_usb_ep0_in_queue_byte(0);
341 case AO_USB_REQ_GET_INTERFACE:
342 ao_usb_ep0_in_queue_byte(0);
344 case AO_USB_REQ_SET_INTERFACE:
348 case AO_USB_RECIP_ENDPOINT:
349 switch(ao_usb_setup.request) {
350 case AO_USB_REQ_GET_STATUS:
351 ao_usb_ep0_in_queue_byte(0);
352 ao_usb_ep0_in_queue_byte(0);
358 case AO_USB_TYPE_CLASS:
359 switch (ao_usb_setup.request) {
360 case AO_USB_SET_LINE_CODING:
361 ao_usb_ep0_out_set((uint8_t *) &ao_usb_line_coding, 7);
363 case AO_USB_GET_LINE_CODING:
364 ao_usb_ep0_in_set((const uint8_t *) &ao_usb_line_coding, 7);
366 case AO_USB_SET_CONTROL_LINE_STATE:
372 /* If we're not waiting to receive data from the host,
373 * queue an IN response
375 if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN)
376 ao_usb_ep0_in_start(ao_usb_setup.length);
380 ao_usb_ep0_handle(uint8_t receive)
382 if (receive & AO_USB_EP0_GOT_RESET) {
386 if (receive & AO_USB_EP0_GOT_SETUP) {
389 if (receive & AO_USB_EP0_GOT_RX_DATA) {
390 if (ao_usb_ep0_state == AO_USB_EP0_DATA_OUT) {
392 if (ao_usb_ep0_out_len == 0) {
393 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
394 ao_usb_ep0_in_start(0);
398 if (receive & AO_USB_EP0_GOT_TX_ACK) {
399 #if HAS_FLIGHT && AO_USB_FORCE_IDLE
400 ao_flight_force_idle = 1;
402 /* Wait until the IN packet is received from addr 0
403 * before assigning our local address
405 if (ao_usb_address_pending)
406 ao_usb_set_address(ao_usb_address);
407 if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN)
413 ao_usb_ep0_interrupt(uint8_t mask)
417 ao_usb_ep0_handle(mask);
422 ao_usb_in_interrupt(uint32_t mask)
424 if (mask & (1 << AO_USB_IN_EPR)) {
426 _tx_dbg1("TX ISR", epr);
427 ao_usb_in_pending = 0;
428 ao_wakeup(&ao_usb_in_pending);
433 ao_usb_out_interrupt(uint32_t mask)
435 if (mask & (1 << AO_USB_OUT_EPR)) {
437 _rx_dbg1("RX ISR", epr);
438 ao_usb_out_avail = 1;
439 _rx_dbg0("out avail set");
440 ao_wakeup(AO_USB_OUT_SLEEP_ADDR);
441 _rx_dbg0("stdin awoken");
446 ao_usb_int_interrupt(uint32_t mask)
452 stm_otg_fs_wkup_isr(void)
454 /* USB wakeup, just clear the bit for now */
455 // stm_usb.istr &= ~(1 << STM_USB_ISTR_WKUP);
458 /* Queue the current IN buffer for transmission */
460 _ao_usb_in_send(void)
462 _tx_dbg0("in_send start");
464 while (ao_usb_in_pending)
465 ao_sleep(&ao_usb_in_pending);
467 ao_usb_in_pending = 1;
468 if (ao_usb_tx_count != AO_USB_IN_SIZE)
469 ao_usb_in_flushed = 1;
471 ao_usb_dev_ep_in(AO_USB_IN_EPR, ao_usb_tx_buffer, ao_usb_tx_count);
474 _tx_dbg0("in_send end");
477 /* Wait for a free IN buffer. Interrupts are blocked */
479 _ao_usb_in_wait(void)
482 /* Check if the current buffer is writable */
483 if (ao_usb_tx_count < AO_USB_IN_SIZE)
486 _tx_dbg0("in_wait top");
487 /* Wait for an IN buffer to be ready */
488 while (ao_usb_in_pending)
489 ao_sleep(&ao_usb_in_pending);
490 _tx_dbg0("in_wait bottom");
500 /* Anytime we've sent a character since
501 * the last time we flushed, we'll need
502 * to send a packet -- the only other time
503 * we would send a packet is when that
504 * packet was full, in which case we now
505 * want to send an empty packet
507 ao_arch_block_interrupts();
508 while (!ao_usb_in_flushed) {
509 _tx_dbg0("flush top");
511 _tx_dbg0("flush end");
513 ao_arch_release_interrupts();
517 ao_usb_putchar(char c)
522 ao_arch_block_interrupts();
525 ao_usb_in_flushed = 0;
526 ao_usb_tx_buffer[ao_usb_tx_count++] = (uint8_t) c;
528 /* Send the packet when full */
529 if (ao_usb_tx_count == AO_USB_IN_SIZE) {
530 _tx_dbg0("putchar full");
532 _tx_dbg0("putchar flushed");
534 ao_arch_release_interrupts();
538 _ao_usb_out_recv(void)
540 _rx_dbg0("out_recv top");
541 ao_usb_out_avail = 0;
543 ao_usb_rx_count = ao_usb_dev_ep_out(AO_USB_OUT_EPR, ao_usb_rx_buffer, sizeof (ao_usb_rx_buffer));
545 _rx_dbg1("out_recv count", ao_usb_rx_count);
551 _ao_usb_pollchar(void)
556 return AO_READ_AGAIN;
559 if (ao_usb_rx_pos != ao_usb_rx_count)
562 _rx_dbg0("poll check");
563 /* Check to see if a packet has arrived */
564 if (!ao_usb_out_avail) {
565 _rx_dbg0("poll none");
566 return AO_READ_AGAIN;
571 /* Pull a character out of the fifo */
572 c = ao_usb_rx_buffer[ao_usb_rx_pos++];
581 ao_arch_block_interrupts();
582 while ((c = _ao_usb_pollchar()) == AO_READ_AGAIN)
583 ao_sleep(AO_USB_OUT_SLEEP_ADDR);
584 ao_arch_release_interrupts();
588 #ifndef HAS_USB_DISABLE
589 #define HAS_USB_DISABLE 1
596 ao_usb_dev_disable();
604 ao_usb_configuration = 0;
608 struct ao_task ao_usb_echo_task;
616 c = ao_usb_getchar();
627 printf ("control: %d out: %d in: %d int: %d reset: %d\n",
628 control_count, out_count, in_count, int_count, reset_count);
631 const struct ao_cmds ao_usb_cmds[] = {
632 { ao_usb_irq, "I\0Show USB interrupt counts" },
642 ao_usb_ep0_state = AO_USB_EP0_IDLE;
644 ao_add_task(&ao_usb_echo_task, ao_usb_echo, "usb echo");
647 ao_cmd_register(&ao_usb_cmds[0]);
651 ao_add_stdio(_ao_usb_pollchar, ao_usb_putchar, ao_usb_flush);
678 #define NUM_USB_DBG 16
680 static struct ao_usb_dbg dbg[NUM_USB_DBG];
683 static void _dbg(int line, char *msg, uint32_t value)
686 dbg[dbg_i].line = line;
687 dbg[dbg_i].msg = msg;
688 dbg[dbg_i].value = value;
689 #if AO_NONMASK_INTERRUPT
690 asm("mrs %0,basepri" : "=&r" (prival));
692 asm("mrs %0,primask" : "=&r" (prival));
694 dbg[dbg_i].prival = prival;
696 dbg[dbg_i].in_count = in_count;
697 dbg[dbg_i].in_epr = stm_usb.epr[AO_USB_IN_EPR];
698 dbg[dbg_i].in_pending = ao_usb_in_pending;
699 dbg[dbg_i].tx_count = ao_usb_tx_count;
700 dbg[dbg_i].in_flushed = ao_usb_in_flushed;
703 dbg[dbg_i].rx_count = ao_usb_rx_count;
704 dbg[dbg_i].rx_pos = ao_usb_rx_pos;
705 dbg[dbg_i].out_avail = ao_usb_out_avail;
706 dbg[dbg_i].out_epr = stm_usb.epr[AO_USB_OUT_EPR];
708 if (++dbg_i == NUM_USB_DBG)