int setupTransferInfo(TransferInfo * tInfo, unsigned long byteLength) { unsigned char cmd = 0; char data[8]; if (byteLength != 9) { return false; } /* check the command */ USBBufferRead(&receiveBuffer, &cmd, 1); if (cmd != TRANSFER_INIT) { return false; } /* set up the information */ USBBufferRead(&receiveBuffer, (unsigned char *)data, 8); unsigned short result = 0; result = (result<<8) + data[1]; // heigh byte result = (result<<8) + data[0]; // low byte tInfo->sampleCount = result; result = (result<<8) + data[3]; // heigh byte result = (result<<8) + data[2]; // low byte tInfo->fx1 = result; result = (result<<8) + data[5]; // heigh byte result = (result<<8) + data[4]; // low byte tInfo->fx2 = result; result = (result<<8) + data[7]; // heigh byte result = (result<<8) + data[6]; // low byte tInfo->tempo = result; //sscanf(data, "%hd%hd%hd%hd", &tInfo->sampleCount, &tInfo->fx1, &tInfo->fx2, &tInfo->tempo); tInfo->currentSample = 0; return true; }
// Sub rotina que limba o buffer de entrada para UART2 void clear_buffer_usb(void){ unsigned char ucChar; while (USBBufferDataAvailable(&g_sRxBuffer))// Testa se há bytes no buffer de entrada { USBBufferRead(&g_sRxBuffer, &ucChar, 1); // Retira os dados } }
int setupSampleInfo(SampleInfo * tInfo, unsigned long byteLength) { unsigned char cmd = 0, m = 0, l = 0; if (byteLength != 13) { return false; } /* check the command */ USBBufferRead(&receiveBuffer, &cmd, 1); if (cmd != TRANSFER_CMD) { return false; } /* get the filename */ USBBufferRead(&receiveBuffer, (unsigned char *)tInfo->fileName, 2); tInfo->fileName[2] = '\0'; // null terminate /* get btn mode */ USBBufferRead(&receiveBuffer, &m, 1); tInfo->btnMode = m - '0'; /* get loop interval */ USBBufferRead(&receiveBuffer, &l, 1); tInfo->loopInterval = l - '0'; /* get the file size */ USBBufferRead(&receiveBuffer, (unsigned char *)tInfo->fileSize.chars, 8); return true; }
//***************************************************************************** // // Take as many bytes from the transmit buffer as we have space for and move // them into the USB UART's transmit FIFO. // //***************************************************************************** static void USBUARTPrimeTransmit(uint32_t ui32Base) { uint32_t ui32Read; uint8_t ui8Char; // // If we are currently sending a break condition, don't receive any // more data. We will resume transmission once the break is turned off. // if(g_bSendingBreak) { return; } // // If there is space in the UART FIFO, try to read some characters // from the receive buffer to fill it again. // while(ROM_UARTSpaceAvail(ui32Base)) { // // Get a character from the buffer. // ui32Read = USBBufferRead((tUSBBuffer *)&g_sRxBuffer, &ui8Char, 1); // // Did we get a character? // if(ui32Read) { // // Place the character in the UART transmit FIFO. // ROM_UARTCharPutNonBlocking(ui32Base, ui8Char); // // Update our count of bytes transmitted via the UART. // g_ui32UARTTxCount++; } else { // // We ran out of characters so exit the function. // return; } } }
/****************************************************************************** * * * \brief Take as many bytes from the transmit buffer as we have space for and* * move them into the USB UART's transmit FIFO. \n * * * * \param ulBase Base address of the UART instance.\n * * * * \return none. * * * ******************************************************************************/ static void USBUARTPrimeTransmit(unsigned int ulBase) { unsigned int ulRead; unsigned char ucChar; /* If we are currently sending a break condition, don't receive any more data. We will resume transmission once the break is turned off. */ if(g_bSendingBreak) { return; } /* If there is space in the UART FIFO, try to read some characters from the receive buffer to fill it again. */ while(UARTSpaceAvail(ulBase)) { /* Get a character from the buffer. */ ulRead = USBBufferRead((tUSBBuffer *)&g_sRxBuffer, &ucChar, 1); /* Did we get a character? */ if(ulRead) { /* Place the character in the UART transmit FIFO. */ UARTCharPutNonBlocking(ulBase, ucChar); /* Update our count of bytes transmitted via the UART. */ g_ulUARTTxCount++; } else { /* We ran out of characters so exit the function. */ return; } } }
static int cdc_uart_recv( timer_data_type timeout ) { unsigned char data; unsigned long read; // Try to read one byte from buffer, if none available return -1 or // retry if timeout // FIXME: Respect requested timeout do { read = USBBufferRead(&g_sRxBuffer, &data, 1); } while( read == 0 && timeout != 0 ); if( read == 0 ) return -1; else return data; }
//***************************************************************************** // // Take as many bytes from the transmit buffer as there is space for and move // them into the USB UART's transmit FIFO. // //***************************************************************************** static void USBUARTPrimeTransmit(void) { unsigned long ulRead; unsigned char ucChar; // // If a break condition is currently being sent, don't receive any more // data. Transmission will resume once the break is turned off. // if(g_bSendingBreak) { return; } // // If there is space in the UART FIFO, try to read some characters // from the receive buffer to fill it again. // while(ROM_UARTSpaceAvail(UART0_BASE)) { // // Get a character from the buffer. // ulRead = USBBufferRead(&g_sRxBuffer, &ucChar, 1); // // Was a character read? // if(ulRead) { // // Place the character in the UART transmit FIFO. // ROM_UARTCharPutNonBlocking(UART0_BASE, ucChar); } else { // // There are no more characters so exit the function. // return; } } }
//***************************************************************************** // // Reads a character from the USB interface. // //***************************************************************************** unsigned char USBIFRead(void) { unsigned char ucChar; // // Return a NUL if the USB is not connected or there is no data in the // receive buffer. // if(!g_bUSBConnected || (USBBufferDataAvailable(&g_sRxBuffer) == 0)) { return(0); } // // Read the next byte from the receive buffer. // USBBufferRead(&g_sRxBuffer, &ucChar, 1); // // Return the byte that was read. // return(ucChar); }
//**************************************************************************** // // Handles CDC driver notifications related to the receive channel (data from // the USB host). // // \param pvCBData is the client-supplied callback data value for this // channel. // \param ulEvent identifies the event we are being notified about. // \param ulMsgValue is an event-specific value. // \param pvMsgData is an event-specific pointer. // // This function is called by the CDC driver to notify us of any events // related to operation of the receive data channel (the OUT channel carrying // data from the USB host). // // \return The return value is event-specific. // //**************************************************************************** unsigned long RxHandler(void *pvCBData, unsigned long ulEvent, unsigned long ulMsgValue, void *pvMsgData) { unsigned char ucChar; const tUSBDCDCDevice *psCDCDevice; const tUSBBuffer *pBufferRx; const tUSBBuffer *pBufferTx; // // Which event are we being sent? // switch(ulEvent) { // // A new packet has been received. // case USB_EVENT_RX_AVAILABLE: { // // Create a device pointer. // psCDCDevice = (const tUSBDCDCDevice *)pvCBData; pBufferRx = (const tUSBBuffer *)psCDCDevice->pvRxCBData; pBufferTx = (const tUSBBuffer *)psCDCDevice->pvTxCBData; // // Keep reading and processing characters as long as there are new // ones in the buffer. // while(USBBufferRead(pBufferRx, (unsigned char *)&g_pcCmdBuf[ulCmdIdx], 1)) { // // If this is a backspace character, erase the last thing typed // assuming there's something there to type. // if(g_pcCmdBuf[ulCmdIdx] == 0x08) { // // If our current command buffer has any characters in it, // erase the last one. // if(ulCmdIdx) { // // Delete the last character. // ulCmdIdx--; // // Send a backspace, a space and a further backspace so // that the character is erased from the terminal too. // USBBufferWrite(pBufferTx, (unsigned char *)g_pcBackspace, 3); } } else { // // Feed some the new character into the UART TX FIFO. // USBBufferWrite(pBufferTx, (unsigned char *)&g_pcCmdBuf[ulCmdIdx], 1); // // If this was a line feed then put out a carriage return // as well. // if(g_pcCmdBuf[ulCmdIdx] == 0xd) { // // Set a line feed. // ucChar = 0xa; USBBufferWrite(pBufferTx, &ucChar, 1); // // Indicate that a command has been received. // HWREGBITW(&g_ulFlags, FLAG_COMMAND_RECEIVED) = 1; g_pcCmdBuf[ulCmdIdx] = 0; ulCmdIdx = 0; } // // Only increment if the index has not reached the end of // the buffer and continually overwrite the last value if // the buffer does attempt to overflow. // else if(ulCmdIdx < CMD_BUF_SIZE) { ulCmdIdx++; } } } break; } // // We are being asked how much unprocessed data we have still to // process. We return 0 if the UART is currently idle or 1 if it is // in the process of transmitting something. The actual number of // bytes in the UART FIFO is not important here, merely whether or // not everything previously sent to us has been transmitted. // case USB_EVENT_DATA_REMAINING: { // // Get the number of bytes in the buffer and add 1 if some data // still has to clear the transmitter. // return(0); } // // We are being asked to provide a buffer into which the next packet // can be read. We do not support this mode of receiving data so let // the driver know by returning 0. The CDC driver should not be // sending this message but this is included just for illustration and // completeness. // case USB_EVENT_REQUEST_BUFFER: { return(0); } // // We don't expect to receive any other events. Ignore any that show // up in a release build or hang in a debug build. // default: { break; } } return(0); }
//***************************************************************************** // // Handles CDC driver notifications related to the receive channel (data from // the USB host). // // \param ulCBData is the client-supplied callback data value for this channel. // \param ulEvent identifies the notification event. // \param ulMsgValue is an event-specific value. // \param pvMsgData is an event-specific pointer. // // This function is called by the CDC driver to notify us of any events // related to operation of the receive data channel (the OUT channel carrying // data from the USB host). // // \return The return value is event-specific. // //***************************************************************************** static unsigned long RxHandler(void *pvCBData, unsigned long ulEvent, unsigned long ulMsgValue, void *pvMsgData) { // // Which event was sent? // switch(ulEvent) { // // A new packet has been received. // case USB_EVENT_RX_AVAILABLE: { // // Since data has been received, see if the game is already being // played. // if(g_ulGameIF == GAME_IF_NONE) { // // The game is not being played, so select the USB interface. // g_ulGameIF = GAME_IF_USB; // // Read and discard the character used to activate the game. // USBBufferRead(&g_sRxBuffer, (unsigned char *)&ulEvent, 1); } // // Otherwise, see if the game is being played on an interface other // than USB. // else if(g_ulGameIF != GAME_IF_USB) { // // Write the error message to the USB interface. // USBBufferWrite(&g_sTxBuffer, g_pucErrorMessage, sizeof(g_pucErrorMessage)); // // Read and discard all data that was received over USB. // while(USBBufferDataAvailable(&g_sRxBuffer) != 0) { USBBufferRead(&g_sRxBuffer, (unsigned char *)&ulEvent, 1); } } break; } // // This is a request for how much unprocessed data is still waiting to // be processed. Return 0 if the UART is currently idle or 1 if it is // in the process of transmitting something. The actual number of // bytes in the UART FIFO is not important here, merely whether or // not everything previously sent to us has been transmitted. // case USB_EVENT_DATA_REMAINING: { // // Get the number of bytes in the buffer and add 1 if some data // still has to clear the transmitter. // return(0); } // // This is a request for a buffer into which the next packet can be // read. This mode of receiving data is not supported so let the // driver know by returning 0. The CDC driver should not be sending // this message but this is included just for illustration and // completeness. // case USB_EVENT_REQUEST_BUFFER: { return(0); } // // Other events can be safely ignored. // default: { break; } } return(0); }
// * USBChalkBusPoll ********************************************************** // * Handle communication requests from ChalkBus master. * // * * // * Communication protocol based off of a subset of the Modicon Modbus RTU * // * protocol developed by Modicon, Inc. * // * * // * Note: Among other changes, multi-byte value endianness has been changed * // * to little endian in the ChalkBus implementation for efficient * // * compatibility with the Cortex-M4 core's native little-endian byte * // * order. * // * * // * See structures above (cbus_*_t) for definition of packet structures for * // * different functions. * // * * // * Function codes: * // * 0x03: read registers (pc reading from lm4f) * // * 0x10: multiple register set (pc writing to lm4f) * // * * // * Exception signaling: * // * Offending function code or-ed with 0x80 (msb set), see structure above * // * (cbus_slave_exception_t) for return format. * // * * // * Register read/write from ChalkBusRegs[]. * // * Note: This driver is not written to handle multi-packet transmissions, * // * overall package size (header, data) must fit within a 64B USB bulk * // * transmission. * // **************************************************************************** void USBChalkBusPoll(void) { unsigned long buffer_bytes; // bytes received from/accepted by buffer cbus_function_read_write_t* cbus_rw_header; // header for reading/writing a if(!g_bUSBRxAvailable) // if no Rx is available { return; // exit now (we only send solicited responses) } // else new Rx is in the buffer, let's handle it buffer_bytes = USBBufferRead((tUSBBuffer *)&g_sRxBuffer, (unsigned char*)ChalkBusBuff, CBUS_BUFFER_SIZE); //buffer_bytes = USBDCDCPacketRead(&g_sCDCDevice,unsigned char *pcData,unsigned long ulLength,tBoolean bLast) // read data in from the receive buffer g_bUSBRxAvailable = false; // we have read the new Rx, no more available if(buffer_bytes == 0) // cancel, there wasn't actually any data { return; } // find out what kind of packet this is, validate it cbus_rw_header = (cbus_function_read_write_t*)ChalkBusBuff; // guess that this function is either read or write to save doing this cast in both cases switch(*((unsigned char*)ChalkBusBuff)) // read function code { case CBUS_FN_CODE_READ: // function read case CBUS_FN_CODE_WRITE: // function write, same validation steps { if((CBUS_FN_HDR_SIZE_RD_WR > buffer_bytes) || (CBUS_FN_HDR_SIZE_RD_WR + cbus_rw_header->data_bytes != buffer_bytes)) { USBChalkBusSendException(cbus_rw_header->function_code, CBUS_EX_CODE_NAK); // throw exception, too few bytes even for header or bytes expected != bytes received return; } if(cbus_rw_header->reg_count > CBUS_MAX_REG_COUNT) // more registers requested than are allowed in a single request { USBChalkBusSendException(cbus_rw_header->function_code, CBUS_EX_CODE_NAK); // throw exception, more registers requested than are allowed return; } if((cbus_rw_header->function_code == CBUS_FN_CODE_WRITE) && (cbus_rw_header->reg_count * CBUS_DATA_WORD_SIZE != cbus_rw_header->data_bytes)) { USBChalkBusSendException(cbus_rw_header->function_code, CBUS_EX_CODE_NAK); // throw exception, register count not consistent with data bytes count return; } if( (cbus_rw_header->starting_reg >= CBUS_REG_COUNT) || ((cbus_rw_header->starting_reg + cbus_rw_header->reg_count) > CBUS_REG_COUNT)) { // if starting register or ending register beyond last register USBChalkBusSendException(cbus_rw_header->function_code, CBUS_EX_CODE_ADDRESS); // throw exception, an address is out of range return; } } break; default: // function unknown { USBChalkBusSendException(*((unsigned char*)ChalkBusBuff), CBUS_EX_CODE_FUNCTION); // throw exception, this function code is unknown return; // done } } // handle now validated function switch(*((unsigned char*)ChalkBusBuff)) // read function code { case CBUS_FN_CODE_READ: // function read { unsigned short count; // used to copy data unsigned long* read_ptr; // source pointer (holding registers) read_ptr = ChalkBusRegs + (cbus_rw_header->starting_reg); // initialize starting read pointer for(count = 0; count < (cbus_rw_header->reg_count); count++) { // copy regs a register at a time cbus_rw_header->data[count] = *read_ptr; // copy data at read_ptr into outgoing packet read_ptr++; // increment read_ptr to next register } cbus_rw_header->data_bytes = cbus_rw_header->reg_count * CBUS_DATA_WORD_SIZE; // load number of bytes written into header before returning buffer_bytes = CBUS_FN_HDR_SIZE_RD_WR + cbus_rw_header->data_bytes; // load count of bytes to be queued for tx to host below } break; case CBUS_FN_CODE_WRITE: // function write { unsigned short count; // used to copy data unsigned long* write_ptr; // destination pointer (holding registers) write_ptr = ChalkBusRegs + (cbus_rw_header->starting_reg); // initialize starting write pointer for(count = 0; count < (cbus_rw_header->reg_count); count++) { // copy regs a register at a time *write_ptr = cbus_rw_header->data[count]; // copy data from incoming packet to write_ptr write_ptr++; // increment write_ptr to next register } cbus_rw_header->data_bytes = 0; // returning zero data in response buffer_bytes = CBUS_FN_HDR_SIZE_RD_WR; // acknowledge packet's only bytes to return is header with 0 data byte count } break; default: // function unknown { return; // unknown functions should never get here } } if(g_bUSBDevConnected) // send response if there is a device to listen { USBBufferWrite((tUSBBuffer *)&g_sTxBuffer,(unsigned char*)ChalkBusBuff, buffer_bytes); // queue the data to send } }
//***************************************************************************** // // Handles CDC driver notifications related to the receive channel (data from // the USB host). // // \param ulCBData is the client-supplied callback data value for this channel. // \param ulEvent identifies the event we are being notified about. // \param ulMsgValue is an event-specific value. // \param pvMsgData is an event-specific pointer. // // This function is called by the CDC driver to notify us of any events // related to operation of the receive data channel (the OUT channel carrying // data from the USB host). // // \return The return value is event-specific. // //***************************************************************************** unsigned long USB_RxHandler(void *pvCBData, unsigned long ulEvent, unsigned long ulMsgValue, void *pvMsgData) { unsigned long ulCount; unsigned char ucCharArr[USB_RX_LENGTH]; unsigned char ucBytesRead; unsigned char nextByte; // // Which event are we being sent? // switch(ulEvent) { // // A new packet has been received. // case USB_EVENT_RX_AVAILABLE: { //Increment Packet Counter g_ulUSBRxCount++; if(g_ulReceiveMode == REC_MODE_STREAM){ //Buffer reading handled in stream interrupt #ifdef __DEBUG__ UARTprintf("\nUSB Stream Mode Rx Event"); #ifdef __DEBUG_TRACEBACK__ UARTprintf(" File: %s Line: %d", __FILE__,__LINE__); #endif #endif } else{ //allocate packet array ucBytesRead = USBBufferRead((tUSBBuffer *)&g_sRxBuffer, ucCharArr, USB_RX_LENGTH-1); ucCharArr[ucBytesRead] = NULL; //Null terminate string #ifdef __DEBUG__ UARTprintf("\nUSB Buffer Dump (length = %d):\n%s\n",ucBytesRead, ucCharArr); #endif for(ulCount=0;ulCount<ucBytesRead;ulCount++){ nextByte = ucCharArr[ulCount]; #ifdef __DEBUG__ #ifdef __DEBUG_USB_EVERYCHAR__ UARTprintf("\nUSB nextByte = %c, ",nextByte); #ifdef __DEBUG_TRACEBACK__ UARTprintf(" File: %s Line: %d", __FILE__,__LINE__); #endif #endif #endif //Ignore non-printing and whitespace characters if(nextByte <= ' '){ #ifdef __DEBUG__ UARTprintf("\nUSB Ignored Character = %c, ",nextByte); #ifdef __DEBUG_TRACEBACK__ UARTprintf(" File: %s Line: %d", __FILE__,__LINE__); #endif #endif continue; } if(nextByte == '#'){ if(g_ucReadMode == COMMENT){ g_ucReadMode = WAITING; g_ulCharIndex = 0; g_ulParamIndex = 0; #ifdef __DEBUG__ UARTprintf("\ng_ucReadMode = FUNCTION"); #ifdef __DEBUG_TRACEBACK__ UARTprintf(" File: %s Line: %d", __FILE__,__LINE__); #endif #endif continue; } else{ g_ucReadMode = COMMENT; #ifdef __DEBUG__ UARTprintf("\ng_ucReadMode = COMMENT"); #ifdef __DEBUG_TRACEBACK__ UARTprintf(" File: %s Line: %d", __FILE__,__LINE__); #endif #endif continue; } } switch(g_ucReadMode){ case WAITING: if(nextByte < 'A' || nextByte > 'Z'){ continue; } else{ g_ucReadMode = FUNCTION; } //DON'T PUT A BREAK HERE! case FUNCTION: if(nextByte == '('){ //end of Function section g_strAssembler[g_ulCharIndex] = NULL; //Terminate string g_strFuncName = malloc(strlen((char *)g_strAssembler) + 1); //allocate memory for function name strcpy((char *)g_strFuncName,(char *)g_strAssembler); //Copy function name g_ulCharIndex = 0; g_ucReadMode = PARAMETER; #ifdef __DEBUG__ #ifdef __DEBUG_USB_EVERYCHAR__ UARTprintf("\ng_ucReadMode = PARAMETER"); #ifdef __DEBUG_TRACEBACK__ UARTprintf(" File: %s Line: %d", __FILE__,__LINE__); #endif #endif #endif continue; } else{ g_strAssembler[g_ulCharIndex] = nextByte; g_ulCharIndex ++; if(g_ulCharIndex > MAX_FUNC_LENGTH){ //Error Checking g_ucReadMode = WAITING; g_ulCharIndex = 0; g_ulParamIndex = 0; #ifdef __DEBUG__ UARTprintf("\nFUNCTION LENGTH EXCEEDED, g_ucReadMode = WAITING"); #ifdef __DEBUG_TRACEBACK__ UARTprintf(" File: %s Line: %d", __FILE__,__LINE__); #endif #endif continue; } } break; case PARAMETER: if(nextByte == ')'){ //end of input g_strAssembler[g_ulCharIndex] = NULL; g_ulParams[g_ulParamIndex] = str2ul(g_strAssembler); if(g_ulParamIndex > 0){ g_ulParamIndex ++; } parseUSB(g_strFuncName,g_ulParams,g_ulParamIndex); g_ucReadMode = WAITING; g_ulCharIndex = 0; g_ulParamIndex = 0; free(g_strFuncName); #ifdef __DEBUG__ #ifdef __DEBUG_USB_EVERYCHAR__ UARTprintf("\ng_ucReadMode = WAITING"); #ifdef __DEBUG_TRACEBACK__ UARTprintf(" File: %s Line: %d", __FILE__,__LINE__); #endif #endif #endif continue; } else{ if(nextByte == ' ' || nextByte == ','){ //next parameter g_strAssembler[g_ulCharIndex] = NULL; g_ulParams[g_ulParamIndex] = str2ul(g_strAssembler); g_ulParamIndex ++; g_ulCharIndex = 0; continue; } else{ g_strAssembler[g_ulCharIndex] = nextByte; g_ulCharIndex ++; if(g_ulCharIndex > MAX_PARAM_LENGTH){ //Error Checking g_ucReadMode = WAITING; g_ulCharIndex = 0; g_ulParamIndex = 0; #ifdef __DEBUG__ #ifdef __DEBUG_USB_EVERYCHAR__ UARTprintf("\nPARAMEtER LENGTH EXCEEDED, g_ucReadMode = WAITING"); #ifdef __DEBUG_TRACEBACK__ UARTprintf(" File: %s Line: %d", __FILE__,__LINE__); #endif #endif #endif continue; } } } break; default: break; } } } } // We are being asked how much unprocessed data we have still to // process. We return 0 if the UART is currently idle or 1 if it is // in the process of transmitting something. The actual number of // bytes in the UART FIFO is not important here, merely whether or // not everything previously sent to us has been transmitted. // case USB_EVENT_DATA_REMAINING: { // // For now just return 0 // #ifdef __DEBUG__ UARTprintf("\nUSB_EVENT_DATA_REMAINING"); #ifdef __DEBUG_TRACEBACK__ UARTprintf(" File: %s Line: %d", __FILE__,__LINE__); #endif #endif ulCount = 0; return(ulCount); } // // We are being asked to provide a buffer into which the next packet // can be read. We do not support this mode of receiving data so let // the driver know by returning 0. The CDC driver should not be sending // this message but this is included just for illustration and // completeness. // case USB_EVENT_REQUEST_BUFFER: { #ifdef __DEBUG__ UARTprintf("\nUSB_EVENT_REQUEST_BUFFER"); #ifdef __DEBUG_TRACEBACK__ UARTprintf(" File: %s Line: %d", __FILE__,__LINE__); #endif #endif return(0); } // // We don't expect to receive any other events. Ignore any that show // up in a release build or hang in a debug build. // default: return(0); } }