From 3db89a7ef64570933fa152cd616c93424c8a5d50 Mon Sep 17 00:00:00 2001 From: Donald Delmar Davis Date: Mon, 22 Apr 2013 00:43:10 -0700 Subject: Start dealing with packets instead of bytes. --- libmaple/MinSysex.c | 59 ++++++++++++++++++------ libmaple/MinSysex.h | 2 +- libmaple/include/libmaple/usb_midi_device.h | 6 +-- libmaple/usb/stm32f1/usb_midi_device.c | 71 +++++++++++++++-------------- libmaple/usb_midi_device.h | 8 ++-- wirish/usb_midi.cpp | 4 +- 6 files changed, 92 insertions(+), 58 deletions(-) diff --git a/libmaple/MinSysex.c b/libmaple/MinSysex.c index 62f9327..1f035cf 100644 --- a/libmaple/MinSysex.c +++ b/libmaple/MinSysex.c @@ -32,7 +32,7 @@ 0xF7 End of SysEx */ // change this to packets -#define STANDARD_ID_RESPONSE_LENGTH 24 +#define STANDARD_ID_RESPONSE_LENGTH 7 #include #include @@ -44,6 +44,39 @@ #define MAX_SYSEX_SIZE 256 /********************************* ACHTUNG! ignores usbmidi cable ********************************/ +/*const MIDI_EVENT_PACKET_t standardIDResponse[]={ + { DEFAULT_MIDI_CABLE, + CIN_SYSEX, + MIDIv1_SYSEX_START, + USYSEX_NON_REAL_TIME, + USYSEX_ALL_CHANNELS}, + { DEFAULT_MIDI_CABLE, + CIN_SYSEX, + USYSEX_GENERAL_INFO, + USYSEX_GI_ID_RESPONSE, + LEAFLABS_MMA_VENDOR_1}, + { DEFAULT_MIDI_CABLE, + CIN_SYSEX, + LEAFLABS_MMA_VENDOR_2, // extended ID + LEAFLABS_MMA_VENDOR_3, // extended ID + 1}, // family #1 + { DEFAULT_MIDI_CABLE, + CIN_SYSEX, + 2, // family #2 + 1, // part #1 + 2}, // part #2 + { DEFAULT_MIDI_CABLE, + CIN_SYSEX, + 0, // version 1 + 0, // version 2 + 1}, // version 3 + { DEFAULT_MIDI_CABLE, + CIN_SYSEX_ENDS_IN_2, + '!', // lgl compatible + MIDIv1_SYSEX_END, + 0} +}; +*/ const uint8 standardIDResponse[]={ CIN_SYSEX, MIDIv1_SYSEX_START, @@ -110,7 +143,7 @@ void dealWithItQuickly(){ switch (sysexBuffer[3]) { case USYSEX_GENERAL_INFO: if (sysexBuffer[4]==USYSEX_GI_ID_REQUEST) { - usb_midi_tx(standardIDResponse, STANDARD_ID_RESPONSE_LENGTH); + usb_midi_tx((uint32 *) standardIDResponse, STANDARD_ID_RESPONSE_LENGTH); } } case USYSEX_REAL_TIME: @@ -153,9 +186,9 @@ void dealWithItQuickly(){ /* -----------------------------------------------------------------------------LglSysexHandler() * The idea here is to identify which Sysex's belong to us and deal with them. */ -void LglSysexHandler(uint8 *midiBufferRx,uint32 *rx_offset,uint32 *n_unread_bytes) { +void LglSysexHandler(uint32 *midiBufferRx, uint32 *rx_offset, uint32 *n_unread_packets) { MIDI_EVENT_PACKET_t * midiPackets = (MIDI_EVENT_PACKET_t *) (midiBufferRx+(*rx_offset)); - uint8 nPackets=((*n_unread_bytes)-(*rx_offset))/4; + uint8 nPackets=((*n_unread_packets)-(*rx_offset)); int cPacket; uint8 soPackets=0; /********************************* ACHTUNG! ignores usbmidi cable ********************************/ @@ -224,25 +257,25 @@ void LglSysexHandler(uint8 *midiBufferRx,uint32 *rx_offset,uint32 *n_unread_byte sysexBuffer[sysexFinger++]=packet->midi1; sysexBuffer[sysexFinger++]=packet->midi2; if (sysexState==YUP_ITS_MY_SYSEX) { - if((cPacket*4)>=(*n_unread_bytes)){ - *n_unread_bytes = soPackets; + if(cPacket>=(*n_unread_packets)){ + *n_unread_packets = soPackets; *rx_offset = soPackets; } else { - uint8 c = cPacket*4; - uint8 *s; - uint8 *d = midiBufferRx + soPackets; + uint8 c = cPacket; + uint32 *s; + uint32 *d = midiBufferRx + soPackets; for (s = midiBufferRx+c; - ((*n_unread_bytes) && (s <= midiBufferRx+USB_MIDI_RX_EPSIZE)); + ((*n_unread_packets) && (s <= midiBufferRx+(USB_MIDI_RX_EPSIZE/4))); d++,s++ ) { (*d)=(*s); - (*n_unread_bytes)--; + (*n_unread_packets)--; (*rx_offset)++; } // we need to reset the for loop variables to re process remaining data. - nPackets=((*n_unread_bytes)-(*rx_offset))/4; - cPacket=(*rx_offset)/4; + nPackets=((*n_unread_packets)-(*rx_offset)); + cPacket=(*rx_offset); } dealWithItQuickly(); diff --git a/libmaple/MinSysex.h b/libmaple/MinSysex.h index c9fa358..57e371d 100644 --- a/libmaple/MinSysex.h +++ b/libmaple/MinSysex.h @@ -30,6 +30,6 @@ extern volatile uint8 myMidiCable; extern volatile uint8 myMidiID[]; -void LglSysexHandler(uint8 *midiBufferRx,uint32 *rx_offset,uint32 *n_unread_bytes); +void LglSysexHandler(uint32 *midiBufferRx,uint32 *rx_offset,uint32 *n_unread_bytes); #endif \ No newline at end of file diff --git a/libmaple/include/libmaple/usb_midi_device.h b/libmaple/include/libmaple/usb_midi_device.h index bac63dd..1cac225 100644 --- a/libmaple/include/libmaple/usb_midi_device.h +++ b/libmaple/include/libmaple/usb_midi_device.h @@ -184,9 +184,9 @@ void usb_midi_enable(gpio_dev*, uint8); void usb_midi_disable(gpio_dev*, uint8); void usb_midi_putc(char ch); -uint32 usb_midi_tx(const uint8* buf, uint32 len); -uint32 usb_midi_rx(uint8* buf, uint32 len); -uint32 usb_midi_peek(uint8* buf, uint32 len); +uint32 usb_midi_tx(const uint32* buf, uint32 len); +uint32 usb_midi_rx(uint32* buf, uint32 len); +uint32 usb_midi_peek(uint32* buf, uint32 len); uint32 usb_midi_data_available(void); /* in RX buffer */ uint16 usb_midi_get_pending(void); diff --git a/libmaple/usb/stm32f1/usb_midi_device.c b/libmaple/usb/stm32f1/usb_midi_device.c index c38bbf1..0c26cb0 100644 --- a/libmaple/usb/stm32f1/usb_midi_device.c +++ b/libmaple/usb/stm32f1/usb_midi_device.c @@ -336,19 +336,19 @@ static ONE_DESCRIPTOR String_Descriptor[N_STRING_DESCRIPTORS] = { /* I/O state */ /* Received data */ -static volatile uint8 midiBufferRx[USB_MIDI_RX_EPSIZE]; +static volatile uint32 midiBufferRx[USB_MIDI_RX_EPSIZE/4]; /* Read index into midiBufferRx */ static volatile uint32 rx_offset = 0; /* Transmit data */ -static volatile uint8 midiBufferTx[USB_MIDI_TX_EPSIZE]; +static volatile uint32 midiBufferTx[USB_MIDI_TX_EPSIZE/4]; /* Write index into midiBufferTx */ static volatile uint32 tx_offset = 0; /* Number of bytes left to transmit */ -static volatile uint32 n_unsent_bytes = 0; +static volatile uint32 n_unsent_packets = 0; /* Are we currently sending an IN packet? */ static volatile uint8 transmitting = 0; /* Number of unread bytes */ -static volatile uint32 n_unread_bytes = 0; +static volatile uint32 n_unread_packets = 0; // eventually all of this should be in a place for settings which can be written to flash. @@ -442,16 +442,17 @@ void usb_midi_disable(gpio_dev *disc_dev, uint8 disc_bit) { gpio_write_bit(disc_dev, disc_bit, 1); } -void usb_midi_putc(char ch) { - while (!usb_midi_tx((uint8*)&ch, 1)) - ; -} +//void usb_midi_putc(char ch) { +// while (!usb_midi_tx((uint8*)&ch, 1)) +// ; +//} /* This function is non-blocking. * * It copies data from a usercode buffer into the USB peripheral TX * buffer, and returns the number of bytes copied. */ -uint32 usb_midi_tx(const uint8* buf, uint32 len) { +uint32 usb_midi_tx(const uint32* buf, uint32 packets) { + uint32 bytes=packets*4; /* Last transmission hasn't finished, so abort. */ if (usb_midi_is_transmitting()) { /* Copy to TxBuffer */ @@ -460,27 +461,28 @@ uint32 usb_midi_tx(const uint8* buf, uint32 len) { } /* We can only put USB_MIDI_TX_EPSIZE bytes in the buffer. */ - if (len > USB_MIDI_TX_EPSIZE) { - len = USB_MIDI_TX_EPSIZE; + if (bytes > USB_MIDI_TX_EPSIZE) { + bytes = USB_MIDI_TX_EPSIZE; + packets=bytes/4; } /* Queue bytes for sending. */ - if (len) { - usb_copy_to_pma(buf, len, USB_MIDI_TX_ADDR); + if (packets) { + usb_copy_to_pma((uint8 *)buf, bytes, USB_MIDI_TX_ADDR); } // We still need to wait for the interrupt, even if we're sending // zero bytes. (Sending zero-size packets is useful for flushing // host-side buffers.) - usb_set_ep_tx_count(USB_MIDI_TX_ENDP, len); - n_unsent_bytes = len; + usb_set_ep_tx_count(USB_MIDI_TX_ENDP, bytes); + n_unsent_packets = packets; transmitting = 1; usb_set_ep_tx_stat(USB_MIDI_TX_ENDP, USB_EP_STAT_TX_VALID); - return len; + return packets; } uint32 usb_midi_data_available(void) { - return n_unread_bytes; + return n_unread_packets; } uint8 usb_midi_is_transmitting(void) { @@ -488,24 +490,24 @@ uint8 usb_midi_is_transmitting(void) { } uint16 usb_midi_get_pending(void) { - return n_unsent_bytes; + return n_unsent_packets; } /* Nonblocking byte receive. * * Copies up to len bytes from our private data buffer (*NOT* the PMA) * into buf and deq's the FIFO. */ -uint32 usb_midi_rx(uint8* buf, uint32 len) { +uint32 usb_midi_rx(uint32* buf, uint32 packets) { /* Copy bytes to buffer. */ - uint32 n_copied = usb_midi_peek(buf, len); + uint32 n_copied = usb_midi_peek(buf, packets); /* Mark bytes as read. */ - n_unread_bytes -= n_copied; + n_unread_packets -= n_copied; rx_offset += n_copied; /* If all bytes have been read, re-enable the RX endpoint, which * was set to NAK when the current batch of bytes was received. */ - if (n_unread_bytes == 0) { + if (n_unread_packets == 0) { usb_set_ep_rx_count(USB_MIDI_RX_ENDP, USB_MIDI_RX_EPSIZE); usb_set_ep_rx_stat(USB_MIDI_RX_ENDP, USB_EP_STAT_RX_VALID); rx_offset = 0; @@ -517,18 +519,17 @@ uint32 usb_midi_rx(uint8* buf, uint32 len) { /* Nonblocking byte lookahead. * * Looks at unread bytes without marking them as read. */ -uint32 usb_midi_peek(uint8* buf, uint32 len) { +uint32 usb_midi_peek(uint32* buf, uint32 packets) { int i; - - if (len > n_unread_bytes) { - len = n_unread_bytes; + if (packets > n_unread_packets) { + packets = n_unread_packets; } - for (i = 0; i < len; i++) { + for (i = 0; i < packets; i++) { buf[i] = midiBufferRx[i + rx_offset]; } - return len; + return packets; } /* @@ -536,24 +537,24 @@ uint32 usb_midi_peek(uint8* buf, uint32 len) { */ static void midiDataTxCb(void) { - n_unsent_bytes = 0; + n_unsent_packets = 0; transmitting = 0; } static void midiDataRxCb(void) { usb_set_ep_rx_stat(USB_MIDI_RX_ENDP, USB_EP_STAT_RX_NAK); - n_unread_bytes = usb_get_ep_rx_count(USB_MIDI_RX_ENDP); + n_unread_packets = usb_get_ep_rx_count(USB_MIDI_RX_ENDP) / 4; /* This copy won't overwrite unread bytes, since we've set the RX * endpoint to NAK, and will only set it to VALID when all bytes * have been read. */ - usb_copy_from_pma((uint8*)midiBufferRx, n_unread_bytes, + usb_copy_from_pma((uint8*)midiBufferRx, n_unread_packets * 4, USB_MIDI_RX_ADDR); - LglSysexHandler(midiBufferRx,&rx_offset,&n_unread_bytes); + LglSysexHandler(midiBufferRx,&rx_offset,&n_unread_packets); - if (n_unread_bytes == 0) { + if (n_unread_packets == 0) { usb_set_ep_rx_count(USB_MIDI_RX_ENDP, USB_MIDI_RX_EPSIZE); usb_set_ep_rx_stat(USB_MIDI_RX_ENDP, USB_EP_STAT_RX_VALID); rx_offset = 0; @@ -619,8 +620,8 @@ static void usbReset(void) { SetDeviceAddress(0); /* Reset the RX/TX state */ - n_unread_bytes = 0; - n_unsent_bytes = 0; + n_unread_packets = 0; + n_unsent_packets = 0; rx_offset = 0; } diff --git a/libmaple/usb_midi_device.h b/libmaple/usb_midi_device.h index 67229b5..1a3f739 100644 --- a/libmaple/usb_midi_device.h +++ b/libmaple/usb_midi_device.h @@ -182,11 +182,11 @@ typedef struct { void usb_midi_enable(gpio_dev*, uint8); void usb_midi_disable(gpio_dev*, uint8); - +bogus void usb_midi_putc(char ch); -uint32 usb_midi_tx(const uint8* buf, uint32 len); -uint32 usb_midi_rx(uint8* buf, uint32 len); -uint32 usb_midi_peek(uint8* buf, uint32 len); +uint32 usb_midi_tx(const uint32* buf, uint32 len); +uint32 usb_midi_rx(const uint32* buf, uint32 len); +uint32 usb_midi_peek(const uint32* buf, uint32 len); uint32 usb_midi_data_available(void); /* in RX buffer */ uint16 usb_midi_get_pending(void); diff --git a/wirish/usb_midi.cpp b/wirish/usb_midi.cpp index f88a7c7..54ce7a9 100644 --- a/wirish/usb_midi.cpp +++ b/wirish/usb_midi.cpp @@ -84,7 +84,7 @@ void USBMidi::write(const void *buf, uint32 len) { uint32 sent = 0; while (txed < len && (millis() - start < USB_TIMEOUT)) { - sent = usb_midi_tx((const uint8*)buf + txed, len - txed); + sent = usb_midi_tx((const uint32*)buf + txed, len - txed); txed += sent; if (old_txed != txed) { start = millis(); @@ -112,7 +112,7 @@ uint32 USBMidi::read(void *buf, uint32 len) { uint32 rxed = 0; while (rxed < len) { - rxed += usb_midi_rx((uint8*)buf + rxed, len - rxed); + rxed += usb_midi_rx((uint32*)buf + rxed, len - rxed); } return rxed; -- cgit v1.2.3