Beispiel #1
0
// Called in ISR context called when a data is received
bool USBMSD::EP2_OUT_callback() {
    uint32_t size = 0;
    uint8_t buf[MAX_PACKET_SIZE_EPBULK];
    readEP(EPBULK_OUT, buf, &size, MAX_PACKET_SIZE_EPBULK);
    switch (stage) {
            // the device has to decode the CBW received
        case READ_CBW:
            CBWDecode(buf, size);
            break;

            // the device has to receive data from the host
        case PROCESS_CBW:
            switch (cbw.CB[0]) {
                case WRITE10:
                case WRITE12:
                    memoryWrite(buf, size);
                    break;
                case VERIFY10:
                    memoryVerify(buf, size);
                    break;
            }
            break;

            // an error has occured: stall endpoint and send CSW
        default:
            stallEndpoint(EPBULK_OUT);
            csw.Status = CSW_ERROR;
            sendCSW();
            break;
    }

    //reactivate readings on the OUT bulk endpoint
    readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
    return true;
}
Beispiel #2
0
bool USBAudio::EP3_OUT_callback() {
    uint32_t size = 0;
    interruptOUT = true;
    if (buf_stream_in != NULL) {
        readEP(EP3OUT, (uint8_t *)buf_stream_in, &size, PACKET_SIZE_ISO_IN);
        available = true;
        buf_stream_in = NULL;
    }
    readStart(EP3OUT, PACKET_SIZE_ISO_IN);
    return false;
}
Beispiel #3
0
bool USBMIDI::EP2_OUT_callback() {
    uint8_t buf[64];
    uint32_t len;
    readEP(EPBULK_OUT, buf, &len, 64);

    if (midi_evt != NULL) {
        for (int i=0; i<len; i+=4) {
            midi_evt(MIDIMessage(buf+i));
        }
    }

    // We reactivate the endpoint to receive next characters
    readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
    return true;
}
Beispiel #4
0
bool USBCDCMSC::EP2_OUT_callback() {
    uint8_t c[65];
    uint16_t size = 0;

    //we read the packet received and put it on the circular buffer
    readEP(c, &size);
    for (int i = 0; i < size; i++) {
        cdcbuf.queue(c[i]);
    }

    //call a potential handler
    rx.call();

    // We reactivate the endpoint to receive next characters
    readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
    return true;
}
Beispiel #5
0
bool USBMIDI::EPBULK_OUT_callback() {
    uint8_t buf[64];
    uint32_t len;
    readEP(EPBULK_OUT, buf, &len, 64);

    if (midi_evt != NULL) {
        for (uint32_t i=0; i<len; i+=4) {   
            uint8_t data_read;
            data_end=true;
            switch(buf[i]) {
            case 0x2:
                // Two-bytes System Common Message - undefined in USBMidi 1.0
                data_read=2;
                break;
            case 0x4:
                // SysEx start or continue
                data_end=false;
                data_read=3;
                break;
            case 0x5:
                 // Single-byte System Common Message or SysEx end with one byte
                data_read=1;
                break;
            case 0x6:
                // SysEx end with two bytes
                data_read=2;
                break;
            case 0xC:
                // Program change
                data_read=2;
                break;
            case 0xD:
                // Channel pressure
                data_read=2;
                break;      
            case 0xF:
                // Single byte
                data_read=1;
                break;    
            default:
                // Others three-bytes messages
                data_read=3;
                break;      
            } 
        
            for(uint8_t j=1;j<data_read+1;j++) {
                data[cur_data]=buf[i+j];
                cur_data++;
            }
        
            if(data_end) {
                 midi_evt(MIDIMessage(data,cur_data));
                 cur_data=0;            
            }
       }
    }

    // We reactivate the endpoint to receive next characters
    readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
    return true;
}
Beispiel #6
0
bool USBSerial::USBEvent_EPOut(uint8_t bEP, uint8_t bEPStatus)
{
    /*
     * Called in ISR context
     */

    bool r = true;

    iprintf("USBSerial:EpOut\n");
    if (bEP != CDC_BulkOut.bEndpointAddress)
        return false;

    if (rxbuf.free() < MAX_PACKET_SIZE_EPBULK)
    {
//         usb->endpointSetInterrupt(bEP, false);
        return false;
    }

    uint8_t c[MAX_PACKET_SIZE_EPBULK];
    uint32_t size = 64;

    //we read the packet received and put it on the circular buffer
    readEP(c, &size);
    iprintf("Read %ld bytes:\n\t", size);
    for (uint8_t i = 0; i < size; i++) {

        if (flush_to_nl == false)
            rxbuf.queue(c[i]);

        if (c[i] >= 32 && c[i] < 128)
        {
            iprintf("%c", c[i]);
        }
        else
        {
            iprintf("\\x%02X", c[i]);
        }

        if (c[i] == '\n' || c[i] == '\r')
        {
            if (flush_to_nl)
                flush_to_nl = false;
            else
                nl_in_rx++;
        }
        else if (rxbuf.isFull() && (nl_in_rx == 0))
        {
            // to avoid a deadlock with very long lines, we must dump the buffer
            // and continue flushing to the next newline
            rxbuf.flush();
            flush_to_nl = true;
        }
    }
    iprintf("\nQueued, %d empty\n", rxbuf.free());

    if (rxbuf.free() < MAX_PACKET_SIZE_EPBULK)
    {
        // if buffer is full, stall endpoint, do not accept more data
        r = false;

        if (nl_in_rx == 0)
        {
            // we have to check for long line deadlock here too
            flush_to_nl = true;
            rxbuf.flush();

            // and since our buffer is empty, we can accept more data
            r = true;
        }
    }

    usb->readStart(CDC_BulkOut.bEndpointAddress, MAX_PACKET_SIZE_EPBULK);
    iprintf("USBSerial:EpOut Complete\n");
    return r;
}