// From USB/Host to Arduino/Serial void MIDI_To_Arduino(void) { // Device must be connected and configured for the task to run if (USB_DeviceState != DEVICE_STATE_Configured) return; // Select the MIDI OUT stream Endpoint_SelectEndpoint(MIDI_STREAM_OUT_EPADDR); /* Check if a MIDI command has been received */ if (Endpoint_IsOUTReceived()) { MIDI_EventPacket_t MIDIEvent; /* Read the MIDI event packet from the endpoint */ Endpoint_Read_Stream_LE(&MIDIEvent, sizeof(MIDIEvent), NULL); // Passthrough to Arduino Serial_SendByte(MIDIEvent.Data1); Serial_SendByte(MIDIEvent.Data2); Serial_SendByte(MIDIEvent.Data3); LEDs_TurnOffLEDs(LEDS_LED1); rx_ticks = TICK_COUNT; /* If the endpoint is now empty, clear the bank */ if (!(Endpoint_BytesInEndpoint())) { /* Clear the endpoint ready for new packet */ Endpoint_ClearOUT(); } } }
/*====================================================================================================*/ void XIO_SerialQscilloscope( int16_t *sendData, uint8_t lens ) { while(lens--) { printf("%d", *sendData++); if(lens == 0) Serial_SendByte('\r'); else Serial_SendByte(','); } }
static inline void handle_packet(void) { switch(packet_type) { case BYTE_TYPE: Serial_SendByte(BYTE_TYPE); Serial_SendByte(BYTE_LEN_1_BYTE); Serial_SendByte(BYTE_TYPE_DS4); break; case BYTE_STATUS: Serial_SendByte(BYTE_STATUS); Serial_SendByte(BYTE_LEN_1_BYTE); Serial_SendByte(started); break; case BYTE_START: Serial_SendByte(BYTE_START); Serial_SendByte(BYTE_LEN_1_BYTE); Serial_SendByte(started); started = 1; break; case BYTE_CONTROL_DATA: spoofReply = 1; spoofReplyLen = value_len; break; case BYTE_RESET: forceHardReset(); break; case BYTE_IN_REPORT: sendReport = 1; //no answer break; } }
/*====================================================================================================*/ void NRF51_Loop( void ) { uint8_t recvByte = 0; while(1) { recvByte = Serial_RecvByte(); if(recvByte == 0x0D) Serial_SendByte(0x0A); Serial_SendByte(recvByte); } }
uint8_t BT::send(char* str) { if(!present) return 1; char i = 0; if(!(BT_RTS) && state == BT_ST_SLEEP) { debug(STR("Waking up BT\r\n")); state = BT_ST_IDLE; while(!(BT_RTS)) { Serial_SendByte('A'); if(++i > 250) { state = BT_ST_SLEEP; break; } _delay_ms(1); } if(state == BT_ST_SLEEP) { debug(STR("ERROR: BT didn't wake up!\r\n")); return 0; // wakeup failed } } while(!(BT_RTS)) { if(++i > 250) break; _delay_ms(1); } if(BT_RTS) { char* ptr; ptr = str; while(*ptr != 0) { while(!(BT_RTS)) { if(++i > 250) break; _delay_ms(1); } Serial_SendByte(*ptr); ptr++; } } return 1; }
static inline void send_spoof_header(void) { Serial_SendByte(BYTE_CONTROL_DATA); if( USB_ControlRequest.bmRequestType & REQDIR_DEVICETOHOST ) { Serial_SendByte(sizeof(USB_ControlRequest)); } else { Serial_SendByte(sizeof(USB_ControlRequest) + (USB_ControlRequest.wLength & 0xFF)); } Serial_SendData(&USB_ControlRequest, sizeof(USB_ControlRequest)); }
/** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to * the device from the USB host before passing along unhandled control requests to the library for processing * internally. */ void EVENT_USB_Device_ControlRequest(void) { static unsigned char buffer[MAX_CONTROL_TRANSFER_SIZE]; /* Handle HID Class specific requests */ switch (USB_ControlRequest.bRequest) { case REQ_GetReport: if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) { uint8_t reportType = USB_ControlRequest.wValue >> 8; uint8_t reportId = USB_ControlRequest.wValue & 0xff; if(reportType == REPORT_TYPE_FEATURE) { switch(reportId) { default: Serial_SendByte(BYTE_DEBUG); Serial_SendByte(BYTE_LEN_1_BYTE); Serial_SendByte(reportId); break; } } } break; case REQ_SetReport: if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) { Endpoint_ClearSETUP(); Endpoint_Read_Control_Stream_LE(buffer, USB_ControlRequest.wLength); Endpoint_ClearIN(); uint8_t reportType = USB_ControlRequest.wValue >> 8; uint8_t reportId = USB_ControlRequest.wValue & 0xff; if(reportType == REPORT_TYPE_FEATURE) { switch(reportId) { default: Serial_SendByte(BYTE_DEBUG); Serial_SendByte(sizeof(USB_ControlRequest) + (USB_ControlRequest.wLength & 0xFF)); Serial_SendData(&USB_ControlRequest, sizeof(USB_ControlRequest)); Serial_SendData(buffer, USB_ControlRequest.wLength); break; } } }
/** Main program entry point. This routine contains the overall program flow, including initial * setup of all components and the main program loop. */ int main(void) { SetupHardware(); RingBuffer_InitBuffer(&USBtoUSART_Buffer, USBtoUSART_Buffer_Data, sizeof(USBtoUSART_Buffer_Data)); RingBuffer_InitBuffer(&USARTtoUSB_Buffer, USARTtoUSB_Buffer_Data, sizeof(USARTtoUSB_Buffer_Data)); LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); GlobalInterruptEnable(); for (;;) { /* Only try to read in bytes from the CDC interface if the transmit buffer is not full */ if (!(RingBuffer_IsFull(&USBtoUSART_Buffer))) { int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface); /* Read bytes from the USB OUT endpoint into the USART transmit buffer */ if (!(ReceivedByte < 0)) RingBuffer_Insert(&USBtoUSART_Buffer, ReceivedByte); } /* Check if the UART receive buffer flush timer has expired or the buffer is nearly full */ uint16_t BufferCount = RingBuffer_GetCount(&USARTtoUSB_Buffer); if (BufferCount) { Endpoint_SelectEndpoint(VirtualSerial_CDC_Interface.Config.DataINEndpoint.Address); /* Check if a packet is already enqueued to the host - if so, we shouldn't try to send more data * until it completes as there is a chance nothing is listening and a lengthy timeout could occur */ if (Endpoint_IsINReady()) { /* Never send more than one bank size less one byte to the host at a time, so that we don't block * while a Zero Length Packet (ZLP) to terminate the transfer is sent if the host isn't listening */ uint8_t BytesToSend = MIN(BufferCount, (CDC_TXRX_EPSIZE - 1)); /* Read bytes from the USART receive buffer into the USB IN endpoint */ while (BytesToSend--) { /* Try to send the next byte of data to the host, abort if there is an error without dequeuing */ if (CDC_Device_SendByte(&VirtualSerial_CDC_Interface, RingBuffer_Peek(&USARTtoUSB_Buffer)) != ENDPOINT_READYWAIT_NoError) { break; } /* Dequeue the already sent byte from the buffer now we have confirmed that no transmission error occurred */ RingBuffer_Remove(&USARTtoUSB_Buffer); } } } /* Load the next byte from the USART transmit buffer into the USART */ if (!(RingBuffer_IsEmpty(&USBtoUSART_Buffer))) Serial_SendByte(RingBuffer_Remove(&USBtoUSART_Buffer)); CDC_Device_USBTask(&VirtualSerial_CDC_Interface); USB_USBTask(); } }
void Serial_SendData(USART_t* const USART, const void* Buffer, uint16_t Length) { while (Length--) Serial_SendByte(USART, *((uint8_t*)Buffer++)); }
void Serial_SendData(const void* Buffer, uint16_t Length) { uint8_t* buf = (uint8_t*)Buffer; while (Length--) Serial_SendByte(*(buf++)); }
int Serial_printLine(char* str) { int retVal = Serial_printString(str); if (!retVal) retVal = Serial_SendByte('\n'); return retVal; }
void sendUSBtoUSART () { if (Serial_IsSendReady() && !(RingBuffer_IsEmpty(&USBtoUSART_Buffer))) { Serial_SendByte(RingBuffer_Remove(&USBtoUSART_Buffer)); } }
/** Main program entry point. This routine contains the overall program flow, including initial * setup of all components and the main program loop. */ int main(void) { SetupHardware(); RingBuffer_InitBuffer(&USBtoUSART_Buffer, USBtoUSART_Buffer_Data, sizeof(USBtoUSART_Buffer_Data)); RingBuffer_InitBuffer(&ToUSB_Buffer, ToUSB_Buffer_Data, sizeof(ToUSB_Buffer_Data)); LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); sei(); for (;;) { /* Only try to read in bytes from the CDC interface if the transmit buffer is not full */ if (!(RingBuffer_IsFull(&USBtoUSART_Buffer))) { int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface); /* Read bytes from the USB OUT endpoint into the USART transmit buffer */ if (!(ReceivedByte < 0)) RingBuffer_Insert(&USBtoUSART_Buffer, ReceivedByte); } /* Load the next byte from the USART transmit buffer into the USART */ if (!(RingBuffer_IsEmpty(&USBtoUSART_Buffer))) Serial_SendByte(RingBuffer_Remove(&USBtoUSART_Buffer)); cdc_send_USB_data(&VirtualSerial_CDC_Interface); USB_USBTask(); } }
/*====================================================================================================*/ void Serial_SendDataMATLAB( int16_t *sendData, uint8_t lens ) { uint8_t tmpData[32] = {0}; // tmpData lens >= 2 * lens + 4 uint8_t *ptrData = tmpData; uint8_t dataBytes = lens << 1; uint8_t dataLens = dataBytes + 4; uint8_t count = 0; uint16_t tmpSum = 0; tmpData[0] = 'S'; while(count < dataBytes) { tmpData[count + 1] = Byte8H(sendData[count >> 1]); tmpData[count + 2] = Byte8L(sendData[count >> 1]); count = count + 2; } for(uint8_t i = 0; i < dataBytes; i++) tmpSum += tmpData[i+1]; tmpData[dataLens - 3] = (uint8_t)(tmpSum & 0x00FF); tmpData[dataLens - 2] = '\r'; tmpData[dataLens - 1] = '\n'; do { Serial_SendByte(*ptrData++); } while(--dataLens); }
/*====================================================================================================*/ void UART_RecvEven( void ) { uint8_t recvData = Serial_RecvByte(); if(recvData == 0x0D) Serial_SendStr("\r\n"); else Serial_SendByte(recvData); }
int Serial_putchar(char DataByte, FILE *Stream) { USART_t* USART = fdev_get_udata(Stream); Serial_SendByte(USART, DataByte); return 0; }
int Serial_putchar(char DataByte, FILE *Stream) { (void)Stream; Serial_SendByte(DataByte); return 0; }
int main(void) { SetupHardware(); RingBuffer_InitBuffer(&USBtoUSART_Buffer, USBtoUSART_Buffer_Data, sizeof(USBtoUSART_Buffer_Data)); RingBuffer_InitBuffer(&USARTtoUSB_Buffer, USARTtoUSB_Buffer_Data, sizeof(USARTtoUSB_Buffer_Data)); LEDs_SetAllLEDs(0); sei(); for (;;) { if ((PINF & 0x01) == 0x01) LEDs_SetAllLEDs(2); else LEDs_SetAllLEDs(0); /* Only try to read in bytes from the CDC interface if the (outbound) transmit buffer is not full */ if (!(RingBuffer_IsFull(&USBtoUSART_Buffer))) { int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface); /* Read bytes from the USB OUT endpoint into the USART transmit buffer */ if (!(ReceivedByte < 0)) RingBuffer_Insert(&USBtoUSART_Buffer, ReceivedByte); } /* Check if the UART receive buffer flush timer has expired or the buffer is nearly full */ uint16_t BufferCount = RingBuffer_GetCount(&USARTtoUSB_Buffer); if ((TIFR0 & (1 << TOV0)) || (BufferCount > (uint8_t)(sizeof(USARTtoUSB_Buffer_Data) * .75))) { /* Clear flush timer expiry flag */ TIFR0 |= (1 << TOV0); /* Read bytes from the USART receive buffer into the USB IN endpoint */ while (BufferCount--) { /* Try to send the next byte of data to the host, abort if there is an error without dequeuing */ if (CDC_Device_SendByte(&VirtualSerial_CDC_Interface, RingBuffer_Peek(&USARTtoUSB_Buffer)) != ENDPOINT_READYWAIT_NoError) { break; } /* Dequeue the already sent byte from the buffer now we have confirmed that no transmission error occurred */ RingBuffer_Remove(&USARTtoUSB_Buffer); } } /* Load the next byte from the USART transmit buffer into the USART */ if (!(RingBuffer_IsEmpty(&USBtoUSART_Buffer))) Serial_SendByte(RingBuffer_Remove(&USBtoUSART_Buffer)); CDC_Device_USBTask(&VirtualSerial_CDC_Interface); USB_USBTask(); } }
void Serial_SendString(const char* StringPtr) { uint8_t CurrByte; while ((CurrByte = *StringPtr) != 0x00) { Serial_SendByte(CurrByte); StringPtr++; } }
void Serial_SendString_P(const char* FlashStringPtr) { uint8_t CurrByte; while ((CurrByte = pgm_read_byte(FlashStringPtr)) != 0x00) { Serial_SendByte(CurrByte); FlashStringPtr++; } }
void sendSpeakjet(char* bytesArr) { int RetVal = 0; while ((RetVal == 0) && (*bytesArr != '\0')) { RetVal = Serial_SendByte(*bytesArr++); delayUs(10000); } return RetVal; }
int Serial_printString(char* C_string) { int RetVal = 0; while ((RetVal == 0) && (*C_string != '\0')) { RetVal = Serial_SendByte(*C_string++); } return RetVal; }
/*====================================================================================================*/ void Serial_evenCallBack( void ) { uint8_t recvData = 0; LED_B_Toggle(); recvData = Serial_RecvByte(); if(recvData == 0x0D) Serial_SendStr("\r\n"); else Serial_SendByte(recvData); }
uint8_t BT::sendDATA(uint8_t id, uint8_t type, void* buffer, uint16_t bytes) { if(!present) return 1; debug(PSTR("Sending: ")); debug(id); debug(PSTR(", ")); debug(type); debug(PSTR(", ")); debug(bytes); debug_nl(); if(dataMode()) { waitRTS(); if(BT_RTS) { char* byte; char s1 = (char)(bytes & 0xff); char s2 = (char)((bytes >> 8) & 0xff); if(waitRTS()) return 1; Serial_SendByte('$'); if(waitRTS()) return 1; Serial_SendByte((char) id); if(waitRTS()) return 1; Serial_SendByte((char) type); if(waitRTS()) return 1; Serial_SendByte((char) s1); if(waitRTS()) return 1; Serial_SendByte((char) s2); if(waitRTS()) return 1; Serial_SendByte(':'); byte = (char *) buffer; if(bytes > 0) { while(bytes--) { if(waitRTS()) break; Serial_SendByte(*byte); byte++; wdt_reset(); } } return 1; } else { debug(PSTR("BT RTS Failed!\r\n")); } }
uint8_t BT::sendDATA(uint8_t id, uint8_t type, void* buffer, uint16_t bytes) { if(!present) return 1; debug(STR("Sending: ")); debug(id); debug(STR(", ")); debug(type); debug_nl(); if(dataMode()) { waitRTS(); if(BT_RTS) { char* byte; if(waitRTS()) return 1; Serial_SendByte('$'); if(waitRTS()) return 1; Serial_SendByte((char) id); if(waitRTS()) return 1; Serial_SendByte((char) type); if(waitRTS()) return 1; Serial_SendByte((char) *(&bytes)); if(waitRTS()) return 1; Serial_SendByte((char) *(&bytes + 1)); if(waitRTS()) return 1; Serial_SendByte(':'); byte = (char *) buffer; if(bytes > 0) { while(bytes--) { if(waitRTS()) break; Serial_SendByte(*byte); byte++; } } return 1; } else { debug(STR("BT RTS Failed!\r\n")); } } return 0; }
void raw_write(unsigned char sz, unsigned char const *data) { cli(); PORTD |= (1 << 4); unsigned char const *end = data + sz; while (data < end) { Serial_SendByte(*data); data++; } // wait for completion while (!(UCSR1A & (1 << UDRE1))) ; _delay_us(5); PORTD &= ~(1 << 4); sei(); }
void servo_cmd(unsigned char id, unsigned char pre_len, unsigned char const *pre, unsigned char len, unsigned char const *ptr) { PORTD |= (1 << 4); unsigned char cs = 0; Serial_SendByte(0xff); Serial_SendByte(0xff); Serial_SendByte(id); cs += id; Serial_SendByte(len + pre_len + 1); cs += len + pre_len + 1; for (unsigned char ix = 0; ix != pre_len; ++ix) { Serial_SendByte(pre[ix]); cs += pre[ix]; } for (unsigned char ix = 0; ix != len; ++ix) { Serial_SendByte(ptr[ix]); cs += ptr[ix]; } Serial_SendByte(~cs); while (!(UCSR1A & (1 << UDRE1))) ; _delay_us(5); PORTD &= ~(1 << 4); }
void Serial_SendData(const void* Buffer, uint16_t Length) { while (Length--) Serial_SendByte(*((uint8_t*)Buffer++)); }
void rfid_sendcommand_readtags(void) { Serial_SendByte(0x43); Serial_SendByte(0x03); Serial_SendByte(0x01); }
//------------------------------------------------------------------------------ uint8_t serialProcess(void) { uint8_t cmd = RingBuffer_Remove(&buffer_); uint8_t size = RingBuffer_Remove(&buffer_); if(cmd == SERIAL_START_CONFIG) { return xilinxSetupConfiguration(); } else if(cmd == SERIAL_DOWNLOAD_DATA) { uint8_t i=0; LEDs_ToggleLEDs(LEDS_LED1); for(i=0; i<size; i++) { xilinxSend(RingBuffer_Remove(&buffer_)); } if(isSetDone()) { return true; } if(!isSetInitB()) { return false; } } else if(cmd == SERIAL_USE_SPI) { SPI_Init(SPI_SPEED_FCPU_DIV_8 | SPI_SCK_LEAD_RISING | SPI_SAMPLE_LEADING | SPI_ORDER_MSB_FIRST | SPI_MODE_MASTER); DDRB |= (1 << 0); PORTB |= (1 << 0); } else if(cmd == SERIAL_SPI_DATA) { uint8_t i=0; LEDs_ToggleLEDs(LEDS_LED2); for(i=0; i<size; i++) { SPI_SendByte(RingBuffer_Remove(&buffer_)); } } else if(cmd == SERIAL_SPI_SELECT) { uint8_t prop = RingBuffer_Remove(&buffer_); if(prop) { PORTD &= ~_BV(PD3); } else { PORTD |= _BV(PD3); } } else if(cmd == SERIAL_USE_USART) { SPI_Disable(); PORTB &= ~((1 << 1) | (1 << 2)); DDRB |= (1 << 1) | (1 << 2); } else if(cmd == SERIAL_USART_DATA) { uint8_t i=0; for(i=0; i<size; i++) { Serial_SendByte(RingBuffer_Remove(&buffer_)); } } else if(cmd == SERIAL_FPGA_RESET) { PORTB |= _BV(PB1); _delay_us(5); PORTB &= ~_BV(PB1); } else if(cmd == SERIAL_FPGA_START) { PORTB |= _BV(PB2); _delay_us(5); PORTB &= ~_BV(PB2); } else { uint8_t i=0; for(i=0; i<size; i++) RingBuffer_Remove(&buffer_); return false; } return true; }