static void trigger_state2_process() { struct __options *option = (struct __options *)&(get_S2E_Packet_pointer()->options); int len = 0; if(trigger_flag == 2) { trigger_state = TRIG_STATE_NONE; #ifdef __TRIG_DEBUG__ printf("[%s] TRIG_STATE_NONE #1\r\n", __func__); #endif trigger_flag = uart_size_prev = 0; return; } if((len = RingBuffer_SerachPattern(&rxring, pattern_offset - 2, option->serial_trigger, 3))) { if((pattern_cnt + 1) == len) { trigger_state = TRIG_STATE_3; #ifdef __TRIG_DEBUG__ printf("TRIG_STATE_3\r\n"); #endif trigger_time = 0; pattern_offset = pattern_cnt = len; uart_size_prev = RingBuffer_GetCount(&rxring); return; } } if(pattern_cnt < RingBuffer_GetCount(&rxring)) { trigger_state = TRIG_STATE_NONE; #ifdef __TRIG_DEBUG__ printf("[%s] TRIG_STATE_NONE #2\r\n", __func__); #endif trigger_flag = trigger_time = uart_size_prev = 0; } }
int RFpushDataToBuffer(unsigned char *data, int count) { int ret = 0; /* Add additional data to transmit ring buffer if possible */ while (ret < count) { int inserted = RingBuffer_InsertMult(&nordicTxBuffer, (data + ret), (count - ret)); if (inserted == 0) { break; } ret += inserted; //If the buffer has enough data to be sent, try to send it right away while (RingBuffer_GetCount(&nordicTxBuffer) >= NRF24L01P_TX_FIFO_SIZE) { if (!RFmoveBufferToTransmission(NRF24L01P_PIPE_P0)) { break; } } } if (RingBuffer_GetCount(&nordicTxBuffer) > 0) { //If there is data to be sent, start a timeout for transmission //If there was a previous transmission no need to do this if (nRF24L01P.mode & NRF24L01P_MODE_TX) { nRF24L01P.transmissionTimeout = NRF24L01P_TX_TIMEOUT; } else if (nRF24L01P.mode & NRF24L01P_MODE_RX) { nRF24L01P.transmissionTimeout = NRF24L01P_TX_TIMEOUT_ACK; } } return count - ret; }
static void trigger_none_process(uint8_t sock_state) { struct __network_info *net = (struct __network_info *)get_S2E_Packet_pointer()->network_info; if(trigger_flag == 2) { trigger_state = TRIG_STATE_READY; #ifdef __TRIG_DEBUG__ printf("TRIG_STATE_READY\r\n"); #endif trigger_flag = 0; uart_size_prev = RingBuffer_GetCount(&rxring); return; } if(uart_size_prev == RingBuffer_GetCount(&rxring)) { // UART ?�신 ?�이?��? ?�으�? if(trigger_flag == 0) trigger_flag = 1; } else { trigger_flag = trigger_time = 0; uart_size_prev = RingBuffer_GetCount(&rxring); if((sock_state != SOCK_ESTABLISHED) && (sock_state != SOCK_UDP) && (net->working_mode != TCP_MIXED_MODE)) { UART_buffer_flush(&rxring); uart_size_prev = 0; } } }
//------------------------------------------------------------------------------ uint8_t serialAddByte(uint8_t byte) { uint16_t size; size = RingBuffer_GetCount(&buffer_); if(size == 0) { if(byte >= SERIAL_END_COMMANDS) { return false; } } else if(size == 1) { frame_size_ = byte+2; } RingBuffer_Insert(&buffer_, byte); size = RingBuffer_GetCount(&buffer_); if(size == frame_size_) { frame_size_ = 0; return true; } return false; }
/* UCOM interrupt EP_IN and EP_OUT endpoints handler */ static ErrorCode_t UCOM_int_hdlr(USBD_HANDLE_T hUsb, void *data, uint32_t event) { switch (event) { case USB_EVT_IN: /* USB_EVT_IN occurs when HW completes sending IN packet. So clear the busy flag for main loop to queue next packet. */ g_usb.usbTxFlags &= ~UCOM_TX_BUSY; if (RingBuffer_GetCount(&usb_txrb) >= 1) { g_usb.usbTxFlags |= UCOM_TX_BUSY; RingBuffer_Pop(&usb_txrb, g_usb.usbTx_buff); USBD_API->hw->WriteEP(g_usb.hUsb, HID_EP_IN, g_usb.usbTx_buff, AVAM_P_COUNT); } break; case USB_EVT_OUT: g_usb.usbRx_count = USBD_API->hw->ReadEP(hUsb, HID_EP_OUT, g_usb.usbRx_buff); #ifdef DEBUG_VERBOSE if (RingBuffer_GetCount(&usb_rxrb) == RX_BUF_CNT) { debug32("E:(%d-%x %x %x %x) usb_rxrb overflow evt out\n", g_usb.usbRx_count, g_usb.usbRx_buff[0], g_usb.usbRx_buff[1], g_usb.usbRx_buff[2], g_usb.usbRx_buff[3]); } #endif if (g_usb.usbRx_count >= AVAM_P_COUNT) { RingBuffer_Insert(&usb_rxrb, g_usb.usbRx_buff); g_usb.usbRx_count -= AVAM_P_COUNT; } if (g_usb.usbRxFlags & UCOM_RX_BUF_QUEUED) { g_usb.usbRxFlags &= ~UCOM_RX_BUF_QUEUED; if (g_usb.usbRx_count != 0) g_usb.usbRxFlags |= UCOM_RX_BUF_FULL; } break; case USB_EVT_OUT_NAK: /* queue free buffer for RX */ if ((g_usb.usbRxFlags & (UCOM_RX_BUF_FULL | UCOM_RX_BUF_QUEUED)) == 0) { g_usb.usbRx_count = USBD_API->hw->ReadReqEP(hUsb, HID_EP_OUT, g_usb.usbRx_buff, UCOM_RX_BUF_SZ); #ifdef DEBUG_VERBOSE if (RingBuffer_GetCount(&usb_rxrb) == RX_BUF_CNT) debug32("E: usb_rxrb overflow evt nak\n"); #endif if (g_usb.usbRx_count >= AVAM_P_COUNT) { RingBuffer_Insert(&usb_rxrb, g_usb.usbRx_buff); g_usb.usbRx_count -= AVAM_P_COUNT; } g_usb.usbRxFlags |= UCOM_RX_BUF_QUEUED; } break; default: break; } return LPC_OK; }
/** HID class driver callback function for the creation of HID reports to the host. * * \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced * \param[in,out] ReportID Report ID requested by the host if non-zero, otherwise callback should set to the generated report ID * \param[in] ReportType Type of the report to create, either REPORT_ITEM_TYPE_In or REPORT_ITEM_TYPE_Feature * \param[out] ReportData Pointer to a buffer where the created report should be stored * \param[out] ReportSize Number of bytes written in the report (or zero if no report is to be sent * * \return Boolean true to force the sending of the report, false to let the library determine if it needs to be sent */ bool CALLBACK_HID_Device_CreateHIDReport( USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo, uint8_t* const ReportID, const uint8_t ReportType, void* ReportData, uint16_t* const ReportSize) { USB_JoystickReport_Data_t *reportp = (USB_JoystickReport_Data_t*)ReportData; RingBuff_Count_t BufferCount = RingBuffer_GetCount(&USARTtoUSB_Buffer); /* If there's a new report from the Arduino, copy it in and send that. * If not then the last report is sent again. */ if (BufferCount >= sizeof(joyReport)) { uint8_t ind; for (ind=0; ind<sizeof(joyReport); ind++) { ((uint8_t *)&joyReport)[ind] = RingBuffer_Remove(&USARTtoUSB_Buffer); } LEDs_TurnOnLEDs(LEDS_LED1); led1_ticks = LED_ON_TICKS; } *reportp = joyReport; *ReportSize = sizeof(joyReport); return false; }
bool RFmoveBufferToTransmission(int pipe) { if (nRF24L01P.payloadsinTXFIFO >= NRF24L01P_TX_FIFO_COUNT) { return false; //We have pushed the maximum amount of payload to the Nordic in RX mode } int count = RingBuffer_GetCount(&nordicTxBuffer); if (nRF24L01P.mode & NRF24L01P_MODE_TX) { if (count > NRF24L01P_TX_FIFO_SIZE) { count = NRF24L01P_TX_FIFO_SIZE; } if (nRF24L01P.mode & NRF24L01P_MODE_TX_NO_ACK) { spiBufferTx[0] = NRF24L01P_SPI_CMD_W_TX_PYLD_NO_ACK; } else { spiBufferTx[0] = NRF24L01P_SPI_CMD_WR_TX_PAYLOAD; } } else { if ((pipe < NRF24L01P_PIPE_P0) || (pipe > NRF24L01P_PIPE_P5)) { xprintf("nRF24L01P: Invalid read pipe number %d\r\n", pipe); return false; } if (count > NRF24L01P_TX_ACK_FIFO_SIZE) { count = NRF24L01P_TX_ACK_FIFO_SIZE; } spiBufferTx[0] = NRF24L01P_SPI_CMD_W_ACK_PAYLOAD | (pipe & 0x7); } RingBuffer_PopMult(&nordicTxBuffer, spiBufferTx + 1, count); writeToSPI(count + 1); nRF24L01P.payloadsinTXFIFO++; return true; }
void HandleSerial(void) { // Only try to read in bytes from the CDC interface if the transmit buffer is not full if (!(RingBuffer_IsFull(&FromHost_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(&FromHost_Buffer, ReceivedByte); CDC_Device_SendByte(&VirtualSerial_CDC_Interface, ReceivedByte); } } while (RingBuffer_GetCount(&FromHost_Buffer) > 0) { int16_t c = RingBuffer_Remove(&FromHost_Buffer); if(c == '\n' || c == '\r'){ if(cmd_cnt > 0 && (cmd[cmd_cnt-1] == '\n' || cmd[cmd_cnt-1] == '\r')) cmd_cnt--; cmd[cmd_cnt] = 0; execute_command(); cmd_cnt = 0; } else{ cmd[cmd_cnt++] = c; } } }
/* Pop multiple items from Ring buffer */ int RingBuffer_PopMult(RINGBUFF_T *RingBuff, void *data, int num) { uint8_t *ptr = RingBuff->data; int cnt1, cnt2; /* We cannot insert when queue is empty */ if (RingBuffer_IsEmpty(RingBuff)) return 0; /* Calculate the segment lengths */ cnt1 = cnt2 = RingBuffer_GetCount(RingBuff); if (RB_INDT(RingBuff) + cnt1 >= RingBuff->count) cnt1 = RingBuff->count - RB_INDT(RingBuff); cnt2 -= cnt1; cnt1 = MIN(cnt1, num); num -= cnt1; cnt2 = MIN(cnt2, num); num -= cnt2; /* Write segment 1 */ ptr += RB_INDT(RingBuff) * RingBuff->itemSz; memcpy(data, ptr, cnt1 * RingBuff->itemSz); RingBuff->tail += cnt1; /* Write segment 2 */ ptr = (uint8_t *) RingBuff->data + RB_INDT(RingBuff) * RingBuff->itemSz; data = (uint8_t *) data + cnt1 * RingBuff->itemSz; memcpy(data, ptr, cnt2 * RingBuff->itemSz); RingBuff->tail += cnt2; return cnt1 + cnt2; }
/*---------------------------------------------------------------------------*/ uint16_t getMessageLength() { uint8_t in = 0; uint8_t run = 1; uint8_t lenbuf[16]; while(run) { while(RingBuffer_GetCount(&Buffer) == 0); lenbuf[in] = RingBuffer_Remove(&Buffer); if(lenbuf[in] == ':') { run = 0; lenbuf[in] = '\0'; } else { in++; } } return StrTo16Uint(lenbuf); }
void cdc_send_USB_data( USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo ){ // process outgoing USB data Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataINEndpoint.Address); // select IN endpoint to restore its registers if ( UEINTX & (1<<TXINI) ){ // if we can write on the outgoing data bank uint8_t BufferCount = RingBuffer_GetCount(&ToUSB_Buffer); if (BufferCount) { uint8_t bank_size = CDCInterfaceInfo->Config.DataINEndpoint.Size; // if there are more bytes in the buffer than what can be put in the data bank OR there are a few bytes and they have been waiting for too long if ( BufferCount >= bank_size || (TIFR0 & (1 << TOV0)) ){ // Clear flush timer expiry flag TIFR0 |= (1 << TOV0); // load the IN data bank until full or until we loaded all the bytes we know we have uint8_t nb_to_write = min(BufferCount, bank_size ); while (nb_to_write--){ uint8_t Data = RingBuffer_Remove(&ToUSB_Buffer); Endpoint_Write_8(Data); } // if the bank is full (== we can't write to it anymore), we might need an empty packet after this one needEmptyPacket = ! Endpoint_IsReadWriteAllowed(); Endpoint_ClearIN(); // allow the hardware to send the content of the bank } } else if (needEmptyPacket) { // send an empty packet to end the transfer needEmptyPacket = false; Endpoint_ClearIN(); // allow the hardware to send the content of the bank } } }
/** 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(); } }
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(); } }
/** 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); RingBuffer_InitBuffer(&USARTtoUSB_Buffer); 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); } /* Check if the UART receive buffer flush timer has expired or the buffer is nearly full */ RingBuff_Count_t BufferCount = RingBuffer_GetCount(&USARTtoUSB_Buffer); if ((TIFR0 & (1 << TOV0)) || (BufferCount > BUFFER_NEARLY_FULL)) { TIFR0 |= (1 << TOV0); if (USARTtoUSB_Buffer.Count) { LEDs_TurnOnLEDs(LEDMASK_TX); PulseMSRemaining.TxLEDPulse = TX_RX_LED_PULSE_MS; } /* Read bytes from the USART receive buffer into the USB IN endpoint */ while (BufferCount--) CDC_Device_SendByte(&VirtualSerial_CDC_Interface, RingBuffer_Remove(&USARTtoUSB_Buffer)); /* Turn off TX LED(s) once the TX pulse period has elapsed */ if (PulseMSRemaining.TxLEDPulse && !(--PulseMSRemaining.TxLEDPulse)) LEDs_TurnOffLEDs(LEDMASK_TX); /* Turn off RX LED(s) once the RX pulse period has elapsed */ if (PulseMSRemaining.RxLEDPulse && !(--PulseMSRemaining.RxLEDPulse)) LEDs_TurnOffLEDs(LEDMASK_RX); } /* Load the next byte from the USART transmit buffer into the USART */ if (!(RingBuffer_IsEmpty(&USBtoUSART_Buffer))) { Serial_TxByte(RingBuffer_Remove(&USBtoUSART_Buffer)); LEDs_TurnOnLEDs(LEDMASK_RX); PulseMSRemaining.RxLEDPulse = TX_RX_LED_PULSE_MS; } CDC_Device_USBTask(&VirtualSerial_CDC_Interface); USB_USBTask(); } }
/** 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(&FromHost_Buffer, FromHost_Buffer_Data, sizeof(FromHost_Buffer_Data)); GlobalInterruptEnable(); for (;;) { /* Only try to read in bytes from the CDC interface if the transmit buffer is not full */ if (!(RingBuffer_IsFull(&FromHost_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(&FromHost_Buffer, ReceivedByte); } while (RingBuffer_GetCount(&FromHost_Buffer) > 0) { static uint8_t EscapePending = 0; int16_t HD44780Byte = RingBuffer_Remove(&FromHost_Buffer); if (HD44780Byte == COMMAND_ESCAPE) { if (EscapePending) { HD44780_WriteData(HD44780Byte); EscapePending = 0; } else { /* Next received character is the command byte */ EscapePending = 1; } } else { if (EscapePending) { HD44780_WriteCommand(HD44780Byte); EscapePending = 0; } else { HD44780_WriteData(HD44780Byte); } } } CDC_Device_USBTask(&VirtualSerial_CDC_Interface); USB_USBTask(); } }
static void trigger_state3_process(uint8_t sock) { if(trigger_flag == 2) { trigger_state = TRIG_STATE_NONE; #ifdef __TRIG_DEBUG__ printf("[%s] TRIG_STATE_NONE #1\r\n", __func__); #endif trigger_flag = uart_size_prev = 0; pattern_offset = 0; disconnect(sock); UART_buffer_flush(&rxring); UART_buffer_flush(&txring); Chip_UART_SendRB(UART_DATA, &txring, "\r\n\r\n\r\n[W,0]\r\n", 13); Chip_UART_SendRB(UART_DATA, &txring, "[S,0]\r\n", 7); op_mode = OP_COMMAND; close(sock); return; } if(pattern_cnt < RingBuffer_GetCount(&rxring)) { trigger_state = TRIG_STATE_NONE; #ifdef __TRIG_DEBUG__ printf("[%s] TRIG_STATE_NONE #2\r\n", __func__); #endif trigger_flag = trigger_time = uart_size_prev = 0; } if(uart_size_prev != RingBuffer_GetCount(&rxring)) { trigger_state = TRIG_STATE_NONE; #ifdef __TRIG_DEBUG__ printf("[%s] TRIG_STATE_NONE #3\r\n", __func__); #endif trigger_flag = trigger_time = uart_size_prev = 0; } }
void RFM12B_Transmit( void ) { if ( RingBuffer_GetCount( &Transmit_Buffer )) { RFM12B_SPI_Transfer( RFM12B_TXREG_WRITE + RingBuffer_Remove( &Transmit_Buffer )); } else { RFM12B_SPI_Transfer( RFM12B_CMD_RX_ON ); RFM12B_Transmit_Active = false; PORTD |= 0x20; } }
static void trigger_ready_process() { struct __options *option = (struct __options *)&(get_S2E_Packet_pointer()->options); if((pattern_cnt = RingBuffer_SerachPattern(&rxring, pattern_offset, option->serial_trigger, 1))) { pattern_offset = pattern_cnt; trigger_state = TRIG_STATE_1; #ifdef __TRIG_DEBUG__ printf("TRIG_STATE_1\r\n"); #endif trigger_time = uart_size_prev = 0; trigger_flag = 1; return; } else ready_cnt = RingBuffer_GetCount(&rxring); if(uart_size_prev != RingBuffer_GetCount(&rxring)) { trigger_state = TRIG_STATE_NONE; #ifdef __TRIG_DEBUG__ printf("[%s] TRIG_STATE_NONE\r\n", __func__); #endif trigger_flag = trigger_time = uart_size_prev = 0; } }
/* Send data to usb */ uint32_t UCOM_Write(uint8_t *pBuf) { uint32_t ret = 0; RingBuffer_Insert(&usb_txrb, pBuf); if (g_usb.usbTxFlags & UCOM_TX_CONNECTED) { if (!(g_usb.usbTxFlags & UCOM_TX_BUSY) && RingBuffer_GetCount(&usb_txrb)) { g_usb.usbTxFlags |= UCOM_TX_BUSY; RingBuffer_Pop(&usb_txrb, g_usb.usbTx_buff); ret = USBD_API->hw->WriteEP(g_usb.hUsb, HID_EP_IN, g_usb.usbTx_buff, AVAM_P_COUNT); } } return ret; }
/** ISR to manage the reception of data from the serial port, placing received bytes into a circular buffer * for later transmission to the host. */ ISR(USART1_RX_vect, ISR_BLOCK) { uint8_t ReceivedByte = UDR1; if (USB_DeviceState == DEVICE_STATE_Configured && !RingBuffer_IsFull(&USARTtoUSB_Buffer)) RingBuffer_Insert(&USARTtoUSB_Buffer, ReceivedByte); RingBuff_Count_t BufferCount = RingBuffer_GetCount(&USARTtoUSB_Buffer); if (BufferCount >= (sizeof(joyReport) + 1)) { joyReport.X = (int8_t) RingBuffer_Remove(&USARTtoUSB_Buffer); joyReport.Y = (int8_t) RingBuffer_Remove(&USARTtoUSB_Buffer); joyReport.Button = (uint8_t) RingBuffer_Remove(&USARTtoUSB_Buffer); /* Remove spacer at the end of the struct*/ RingBuffer_Remove(&USARTtoUSB_Buffer); } }
static int RingBuffer_SerachPattern(RINGBUFF_T *RingBuff, int offset, const uint8_t *pattern, uint32_t size) { int i; uint32_t buf_size = 0; if(RingBuff->itemSz != 1) return 0; buf_size = RingBuffer_GetCount(RingBuff) - offset; if(buf_size < size) return 0; for(i = 0 ; i < (buf_size - size + 1) ; i++) { if(!RingBuffer_memcmp(RingBuff, pattern, RingBuff->tail + offset + i, size)) return (i + size + offset); } return 0; }
void RFM12B_Start_Transmit( void ) { uint16_t BufferCount = RingBuffer_GetCount( &USBtoRF12_Buffer ); RingBuffer_InsertString( &Transmit_Buffer, "\xAA\xAA\x2D\x55" ); RingBuffer_Insert( &Transmit_Buffer, BufferCount & 0xFF ); while ( BufferCount-- ) { RingBuffer_Insert( &Transmit_Buffer, RingBuffer_Remove( &USBtoRF12_Buffer )); } RingBuffer_InsertString( &Transmit_Buffer, "\xAA\xAA" ); RFM12B_Transmit_Active = true; PORTD &= ~0x20; RFM12B_SPI_Transfer( RFM12B_CMD_TX_ON ); }
/* Fill tcpData array, return message length */ uint16_t waitTCPMessage_blocking() { uint16_t len; wait_for_message("+IPD,",0); wait_for_message(",",0); len = getMessageLength(); uint16_t t16; for(t16=0;t16 < len;t16++) { while(RingBuffer_GetCount(&Buffer) == 0); tcpData[t16] = RingBuffer_Remove(&Buffer); } wait_for_message("OK\r\n",10000); return len; }
/*---------------------------------------------------------------------------*/ int8_t wait_for_message(const char* checkmsg,uint32_t timeoutLimit) { uint8_t ch; uint16_t in = 0; uint8_t run = 1; uint32_t timeout = 0; while(run == 1) { while(RingBuffer_GetCount(&Buffer) == 0) { if(timeoutLimit != 0) { timeout++; _delay_us(750); if(timeout > timeoutLimit) { return -1; } } } ch = RingBuffer_Remove(&Buffer); if(ch == checkmsg[in]) { in++; } else { if(timeoutLimit != 0) { timeout++; _delay_us(750); if(timeout > timeoutLimit) { return -1; } } } if(checkmsg[in] == '\0') { run = 0; } } return 0; }
void sendUSARTtoUSB () { uint16_t BufferCount = RingBuffer_GetCount(&USARTtoUSB_Buffer); if (BufferCount) { Endpoint_SelectEndpoint(VirtualSerial_CDC_Interface.Config.DataINEndpoint.Address); if (Endpoint_IsINReady()) { uint8_t BytesToSend = MIN(BufferCount, (CDC_TXRX_EPSIZE - 1)); while (BytesToSend--) { if (CDC_Device_SendByte(&VirtualSerial_CDC_Interface, RingBuffer_Peek(&USARTtoUSB_Buffer)) != ENDPOINT_READYWAIT_NoError) { break; } RingBuffer_Remove(&USARTtoUSB_Buffer); } } } }
void UARTBridge_Task(void) { /* Must be in the configured state for the USART Bridge code to process data */ if (USB_DeviceState != DEVICE_STATE_Configured) return; /* Only try to read in bytes from the CDC interface if the transmit buffer is not full */ if (!(RingBuffer_IsFull(&USBtoUART_Buffer))) { int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface); /* Read bytes from the USB OUT endpoint into the UART transmit buffer */ if (!(ReceivedByte < 0)) RingBuffer_Insert(&USBtoUART_Buffer, ReceivedByte); } /* Check if the UART receive buffer flush timer has expired or buffer is nearly full */ uint16_t BufferCount = RingBuffer_GetCount(&UARTtoUSB_Buffer); if ((TIFR0 & (1 << TOV0)) || (BufferCount > 200)) { /* 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(&UARTtoUSB_Buffer)) != ENDPOINT_READYWAIT_NoError) { break; } /* Dequeue the already sent byte from the buffer now we have confirmed that no transmission error occurred */ RingBuffer_Remove(&UARTtoUSB_Buffer); } } CDC_Device_USBTask(&VirtualSerial_CDC_Interface); }
/*---------------------------------------------------------------------------*/ int transport_getdata(uint8_t* buf, int count) { int i; int t16; int8_t res; uint8_t sockId; while(count > RingBuffer_GetCount(&tcpBuffer)) { res = esp8266_getTCPData(ESP8266_1SecTimeout,commonBuffer,sizeof(commonBuffer),&rv,&sockId); if(res == ESP8266_TIMEOUT) { return 0; } if(sockId != 0) { esp8266_hal_rebootSystem(); } for(i=0;i<rv;i++) { if(!RingBuffer_IsFull(&tcpBuffer)) { RingBuffer_Insert(&tcpBuffer,commonBuffer[i]); } } } for(i=0;i<count;i++) { buf[i] = RingBuffer_Remove(&tcpBuffer); } return count; }
/*---------------------------------------------------------------------------*/ int8_t check_ok() { uint8_t ch; uint8_t run = 1; uint16_t in = 0; uint32_t timeout = 0; uint8_t ok_buf[4] = {'O','K','\r','\n'}; while(run == 1) { while(RingBuffer_GetCount(&Buffer) == 0) { timeout++; _delay_us(750); if(timeout > 10000) { return -1; } } ch = RingBuffer_Remove(&Buffer); if(ch == ok_buf[in]) { in++; } else { timeout++; _delay_us(750); if(timeout > 10000) { return -1; } } if(in == 4) { run = 0; } } return 0; }
/** HID class driver callback function for the creation of HID reports to the host. * * \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced * \param[in,out] ReportID Report ID requested by the host if non-zero, otherwise callback should set to the generated report ID * \param[in] ReportType Type of the report to create, either REPORT_ITEM_TYPE_In or REPORT_ITEM_TYPE_Feature * \param[out] ReportData Pointer to a buffer where the created report should be stored * \param[out] ReportSize Number of bytes written in the report (or zero if no report is to be sent * * \return Boolean true to force the sending of the report, false to let the library determine if it needs to be sent */ bool CALLBACK_HID_Device_CreateHIDReport( USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo, uint8_t* const ReportID, const uint8_t ReportType, void* ReportData, uint16_t* const ReportSize) { USB_JoystickReport_Data_t *reportp = (USB_JoystickReport_Data_t*)ReportData; RingBuff_Count_t BufferCount = RingBuffer_GetCount(&USARTtoUSB_Buffer); if (BufferCount >= (sizeof(joyReport))) { uint8_t ind; for (ind=0; ind<sizeof(joyReport); ind++) { ((uint8_t *)&joyReport)[ind] = RingBuffer_Remove(&USARTtoUSB_Buffer); } } *reportp = joyReport; *ReportSize = sizeof(USB_JoystickReport_Data_t); return false; }
int RingBuffer_Backspace(RingBuffer *rb, unsigned int skip_cnt) { int retval; #ifdef RING_BUFFER_THREAD_SAFE VOS_SmP(rb->sem, 50); #endif if (RingBuffer_GetCount(rb) < skip_cnt) { retval = RING_BUFFER_NO_SKIP; } else { rb->start = (rb->start + rb->count - skip_cnt) % rb->size; rb->count = skip_cnt; retval = RING_BUFFER_NORMAL; } #ifdef RING_BUFFER_THREAD_SAFE VOS_SmV(rb->sem); #endif return retval; }