/* This function sends multiple bytes to the USB host. */ status_t USBVC001_SendData(const char* const DataBuffer, const uint16_t Length) { status_t Status = (uint32_t)DAVEApp_SUCCESS; do{ if(Length == 0) { Status = USBVC001_ERROR; break; } /* Send data to USB host.*/ if(CDC_Device_SendData(&USBVC001_CDCInterface, (const char *)DataBuffer, Length) != ENDPOINT_RWSTREAM_NoError) { Status = USBVC001_USBCDC001_ERROR; } else if(CDC_Device_Flush(&USBVC001_CDCInterface) != ENDPOINT_READYWAIT_NoError) { Status = USBVC001_USBCDC001_ERROR; } }while(0); return Status; }
void CDC_Device_USBTask(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo) { if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS)) return; CDC_Device_Flush(CDCInterfaceInfo); }
int main(void) { uint8_t i = 1; uint8_t count = 3; setup(); blink(3); _delay_ms(100); while (1) { // Print greeting at most count times. if (i <= count) { if (USB_DeviceState == DEVICE_STATE_Configured && ok_to_send) { blink(i); CDC_Device_SendString(&VirtualSerial_CDC_Interface, "Hello World! "); CDC_Device_SendByte(&VirtualSerial_CDC_Interface, '0' + i); CDC_Device_SendString(&VirtualSerial_CDC_Interface, "\r\n"); CDC_Device_Flush(&VirtualSerial_CDC_Interface); i++; } } if (USB_DeviceState == DEVICE_STATE_Configured) { /* Must throw away unused bytes from the host, or it will lock up while waiting for the device */ CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface); } CDC_Device_USBTask(&VirtualSerial_CDC_Interface); USB_USBTask(); } }
void CDC_Device_USBTask(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo) { if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS)) return; #if !defined(NO_CLASS_DRIVER_AUTOFLUSH) CDC_Device_Flush(CDCInterfaceInfo); #endif }
void sendData(){ for(uint16_t i=0; i<sizeof(random_data); i++){ //CDC_Device_SendByte(&cdcif, random_data[i]); } if(CDC_Device_SendString(&cdcif, "Fnord!\n") == ENDPOINT_RWSTREAM_NoError){ PORTD |= 0x30; }else{ PORTD &= 0xCF; } CDC_Device_Flush(&cdcif); }
void CDC_Device_USBTask(USB_ClassInfo_CDC_Device_t* CDCInterfaceInfo) { if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS)) return; Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataOUTEndpointNumber); if (Endpoint_IsOUTReceived() && !(Endpoint_BytesInEndpoint())) Endpoint_ClearOUT(); CDC_Device_Flush(CDCInterfaceInfo); }
void CDC_Device_USBTask(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo) { if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS)) return; #if !defined(NO_CLASS_DRIVER_AUTOFLUSH) Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataINEndpoint.Address); if (Endpoint_IsINReady()) CDC_Device_Flush(CDCInterfaceInfo); #endif }
void printPacket(MRF_packet_t *rx_packet) { // Print a label for the packet type switch (rx_packet->type) { case PACKET_TYPE_SERIAL: sendStringP(typeSerialString); break; case PACKET_TYPE_SERIAL_ECC: sendStringP(typeSerialECCString); break; case PACKET_TYPE_PACKET: sendStringP(typePacketString); break; case PACKET_TYPE_PACKET_ECC: sendStringP(typePacketECCString); break; default: sendStringP(typeUnknownString); break; } // Print the packet length sendStringP(packetLengthString); print_dec(rx_packet->payloadSize); CDC_Device_SendByte(&CDC_interface, ':'); CDC_Device_SendByte(&CDC_interface, ' '); // Print the packet contents for (int i = 0; i < rx_packet->payloadSize; i++) { if (i % 10 == 0) { CDC_Device_SendByte(&CDC_interface, '\n'); CDC_Device_SendByte(&CDC_interface, '\r'); } uint8_t byte = rx_packet->payload[i]; CDC_Device_SendByte(&CDC_interface, byte); // if (byte & 0xF0 > 0x90) { // CDC_Device_SendByte(&CDC_interface, ((byte & 0xF0) >> 4) + 'A'); // } else { // CDC_Device_SendByte(&CDC_interface, ((byte & 0xF0) >> 4) + '0'); // } // // if (byte & 0x0F > 0x09) { // CDC_Device_SendByte(&CDC_interface, (byte & 0x0F) + 'A'); // } else { // CDC_Device_SendByte(&CDC_interface, (byte & 0x0F) + '0'); // } } CDC_Device_Flush(&CDC_interface); }
//TODO: this is just to test tranmitting. the encoders are not hooked up yet (5/19/2012) void transmitEncoderState(){ // uint8_t payload[4]; // fill in payload // payload[0] = 13; CDC_Device_Flush(&VirtualSerial_CDC_Interface); packet_t* pkt = PKT_Create(PKTYPE_STATUS_ENCODER_RAW, seq++, echoback_payload, 4); uint8_t len = PKT_ToBuffer(pkt, txBuffer); for(int i=0; i < len; i++) { sendByte(txBuffer[i]); handleUSB(); } free(pkt); }
void transmitGyroState(){ uint8_t payload[8]; // fill in payload ReadGyro(payload); CDC_Device_Flush(&VirtualSerial_CDC_Interface); packet_t* pkt = PKT_Create(PKTYPE_STATUS_3GYRO_RAW, seq++, payload, 8); uint8_t len = PKT_ToBuffer(pkt, txBuffer); for(int i=0; i < len; i++) { sendByte(txBuffer[i]); handleUSB(); } free(pkt); }
/* This function sends a byte to the USB host. */ status_t USBVC001_SendByte(const uint8_t DataByte) { status_t Status = (uint32_t)DAVEApp_SUCCESS; /* Send a byte to the host. */ if(CDC_Device_SendByte(&USBVC001_CDCInterface, DataByte) != ENDPOINT_RWSTREAM_NoError) { Status = USBVC001_USBCDC001_ERROR; } if(CDC_Device_Flush(&USBVC001_CDCInterface) != ENDPOINT_READYWAIT_NoError) { Status = USBVC001_USBCDC001_ERROR; } return Status; }
void transmitMotorState(){ uint8_t payload[4]; // fill in payload payload[0] = curr1; payload[1] = curr2; payload[2] = temp1; payload[3] = temp2; CDC_Device_Flush(&VirtualSerial_CDC_Interface); packet_t* pkt = PKT_Create(PKTYPE_STATUS_MOTOR_STATE, seq++, payload, 4); uint8_t len = PKT_ToBuffer(pkt, txBuffer); for(int i=0; i < len; i++) { sendByte(txBuffer[i]); handleUSB(); } free(pkt); }
void transmitMotorBuffer(){ // uint8_t payload[4]; uint8_t bsize = copyToBuffer(); if(bsize<=100) return; // fill in payload tbuffer[100] = bsize; tbuffer[101] = wbuffcount-rbuffcount; CDC_Device_Flush(&VirtualSerial_CDC_Interface); packet_t* pkt = PKT_Create(PKTYPE_STATUS_MOTOR_STATE, seq++, tbuffer, 102); uint8_t len = PKT_ToBuffer(pkt, txBuffer); for(int i=0; i < len; i++) { sendByte(txBuffer[i]); handleUSB(); } free(pkt); }
/* This function sends string data to the USB host. */ status_t USBVC001_SendString(const char* const DataString) { status_t Status = (uint32_t)DAVEApp_SUCCESS; do{ /* Send string to the host */ if(CDC_Device_SendString(&USBVC001_CDCInterface, DataString) != ENDPOINT_RWSTREAM_NoError) { Status = USBVC001_USBCDC001_ERROR; } else if(CDC_Device_Flush(&USBVC001_CDCInterface) != ENDPOINT_READYWAIT_NoError) { Status = USBVC001_USBCDC001_ERROR; } }while(0); return Status; }
int main(void) { uint8_t byte; // Initalize the system init(); // Load the saved registers applySavedRegisters(); MRF_reset(); // Get the default boot state mode = getBootState(); // Loop here forever while (true) { // Process menu actions as long as we're in the menu or test modes // New packets received while in menu mode are ignored if (mode == MENU || mode == TEST_ALT || mode == TEST_ZERO || mode == TEST_ONE || mode == CAPTURE || mode == TEST_PING ) { // Handle any new bytes from the USB system // These functions can be assumed to return immediately. if (CDC_Device_BytesReceived(&CDC_interface) > 0) { byte = CDC_Device_ReceiveByte(&CDC_interface); menuHandleByte(byte); } // Test for a new packet MRF_packet_t *rx_packet = MRF_receive_packet(); // Was the packet correctly received? if (rx_packet != NULL) { switch (mode) { case TEST_PING: MRF_transmit_packet(rx_packet); sendStringP(pingString); printPacket(rx_packet); break; case CAPTURE: printPacket(rx_packet); break; default: // Other menu modes would end up here, ignore the packet break; } } } // These modes are responsible for actually using the RF interface else { switch (mode) { case PACKET: case PACKET_ECC: packetMainLoop(); break; case SERIAL: case SERIAL_ECC: serialMainLoop(); break; case USB_SERIAL: usbSerialMainLoop(); break; default: // This would catch any weird modes sendStringP(invalidModeString); print_dec(mode); CDC_Device_Flush(&CDC_interface); mode = MENU; break; } } } }
void usb_serial_flush_output(void) { CDC_Device_Flush(&VirtualSerial_CDC_Interface); }