Beispiel #1
0
void println_C (char * str)
{
	for (uint16_t i = 0; i<strlen(str);i++)
	{
		RingBuffer_Insert(&USARTtoUSB_Buffer,str[i]);
	}
	RingBuffer_Insert(&USARTtoUSB_Buffer, 13);
}
Beispiel #2
0
void send_ELM327_OK()
{
	//Serial_SendByte('O');	//
	RingBuffer_Insert(&FFtoBT_Buffer,'O');
	//Serial_SendByte('K');	//
	RingBuffer_Insert(&FFtoBT_Buffer,'K');
	send_ELM327_CR();
}
Beispiel #3
0
/* 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;
}
Beispiel #4
0
/** 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) {
        if (eeprom_op) {
            RingBuffer_Insert(&USARTtoEEPROM_Buffer, ReceivedByte);
        } else {
            RingBuffer_Insert(&USARTtoUSB_Buffer, ReceivedByte);
        }
    }
}
/* UCOM bulk EP_IN and EP_OUT endpoints handler */
static ErrorCode_t UCOM_bulk_hdlr(USBD_HANDLE_T hUsb, void *data, uint32_t event)
{
	UCOM_DATA_T *pUcom = (UCOM_DATA_T *) data;

	switch (event) {
	/* A transfer from us to the USB host that we queued has completed. */
	case USB_EVT_IN:
		pUcom->usbTxFlags &= ~UCOM_TX_BUSY;
		break;

	/* We received a transfer from the USB host . */
	case USB_EVT_OUT:
		pUcom->usbRx_count = USBD_API->hw->ReadEP(hUsb, USB_CDC_OUT_EP, pUcom->usbRx_buff);
		if(pUcom->usbRx_count){
			if(1 == pUcom->usbRx_count){
				RingBuffer_Insert(&usb_rxrb, pUcom->usbRx_buff);
			}else
			{
				RingBuffer_InsertMult(&usb_rxrb, pUcom->usbRx_buff, pUcom->usbRx_count);
			}
		}
		if (pUcom->usbRxFlags & UCOM_RX_BUF_QUEUED) {
			pUcom->usbRxFlags &= ~UCOM_RX_BUF_QUEUED;
			if (pUcom->usbRx_count != 0) {
				pUcom->usbRxFlags |= UCOM_RX_BUF_FULL;
			}
		}
		break;

	case USB_EVT_OUT_NAK:
		/* queue free buffer for RX */
		if ((pUcom->usbRxFlags & (UCOM_RX_BUF_FULL | UCOM_RX_BUF_QUEUED)) == 0) {
			pUcom->usbRx_count = USBD_API->hw->ReadReqEP(hUsb, USB_CDC_OUT_EP, pUcom->usbRx_buff, UCOM_RX_BUF_SZ);
			if(pUcom->usbRx_count){
				if(1 == pUcom->usbRx_count){
					RingBuffer_Insert(&usb_rxrb, pUcom->usbRx_buff);
				}else
				{
					RingBuffer_InsertMult(&usb_rxrb, pUcom->usbRx_buff, pUcom->usbRx_count);
				}
			}
			pUcom->usbRxFlags |= UCOM_RX_BUF_QUEUED;
		}
		break;

	default:
		break;
	}

	return LPC_OK;
}
/** 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();
	}
}
Beispiel #7
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;
}
Beispiel #8
0
void CAN_rx(uint8_t msg_obj_num) {
	msg_obj.msgobj = msg_obj_num;
	LPC_CCAN_API->can_receive(&msg_obj);
	if (msg_obj_num == 1) {
		RingBuffer_Insert(&CAN_rx_buffer, &msg_obj);
	}
}
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;			
		}
	}
}
Beispiel #10
0
/**
 * @details Moves received on-chip CAN messages into ring buffer
 * 
 * @param msg_obj_num CAN message object that received message
 */
void CAN_rx(uint8_t msg_obj_num) {
	/* Determine which CAN message has been received */
	can_msg_obj.msgobj = msg_obj_num;
	/* Now load up the msg_obj structure with the CAN message */
	LPC_CCAN_API->can_receive(&can_msg_obj);
	RingBuffer_Insert(&rx_buffer, &can_msg_obj);
}
Beispiel #11
0
/** 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();
	}
}
/** 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_Insert(&USARTtoUSB_Buffer, ReceivedByte);
}
Beispiel #13
0
void RFM12B_Receive( void )
{
    uint8_t ch = RFM12B_SPI_Transfer( RFM12B_RX_FIFO_READ ) & 0xFF;

    if ( RxLength == 0 )
    {
        if ( ch == 0 )
        {
            RFM12B_EndOfPacket();
        }
        else
        {
            RxLength = ch;
            RxTimeout = 0;
        }
    }
    else
    {
        RingBuffer_Insert( &RF12toUSB_Buffer, ch );

        /* Decrement the RxLength for each byte of payload received.
           When it gets to zero that's the end of the packet */
           
        if ( --RxLength == 0 ) RFM12B_EndOfPacket();
    }
}
Beispiel #14
0
/* UART receive-only interrupt handler for ring buffers */
void Chip_UART_RXIntHandlerRB(LPC_USART_T *pUART, RINGBUFF_T *pRB)
{
	/* New data will be ignored if data not popped in time */
	while (Chip_UART_ReadLineStatus(pUART) & UART_LSR_RDR) {
		uint8_t ch = Chip_UART_ReadByte(pUART);
		RingBuffer_Insert(pRB, &ch);
	}
}
Beispiel #15
0
/* UART receive-only interrupt handler for ring buffers */
void Chip_UARTN_RXIntHandlerRB(LPC_USARTN_T *pUART, RINGBUFF_T *pRB)
{
    /* New data will be ignored if data not popped in time */
    while ((Chip_UARTN_GetStatus(pUART) & UARTN_STAT_RXRDY) != 0) {
        uint8_t ch = Chip_UARTN_ReadByte(pUART);
        RingBuffer_Insert(pRB, &ch);
    }
}
Beispiel #16
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 );
}
/** 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();
	}
}
Beispiel #18
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();
	}
}
Beispiel #19
0
//------------------------------------------------------------------------------
void appUsbDataReceived(Application *app)
{
    int16_t received_byte = 0;
    while((received_byte = CDC_Device_ReceiveByte(app->cdc_info)) >= 0)
    {
        RingBuffer_Insert(&app->buffer_, received_byte);
    }
    serialProcess(app);
}
Beispiel #20
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(LEDMASK_USB_NOTREADY);
	GlobalInterruptEnable();
	
	while(1)
	{
		if(RTR==1)
		{
			if (ReceivedByte==0xFF)
			{
				for (uint8_t i = 0; i < 4; i++)
				{
					if (ReceivedByte==0)
					{
						ReceivedByte=1;
					}
					buff[i] = ReceivedByte;
					RTR = 0;
					while(RTR!=1){}
				}
				uint8_t range = ((buff[1]*256+buff[2])/100);
				RingBuffer_Insert(&USARTtoUSB_Buffer,buff[1]);
				RingBuffer_Insert(&USARTtoUSB_Buffer,buff[2]);
				//RingBuffer_Insert(&USARTtoUSB_Buffer,range);
				sendUSARTtoUSB();
				_delay_ms(500);
			}
		}		
//  		if (!RingBuffer_IsFull(&USBtoUSART_Buffer))
// 		{
// 			int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
// 			if (!(ReceivedByte < 0))
// 			{
// 				sendUSBtoUSART();
// 			}
			CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
			USB_USBTask();
//		}
	}
}
Beispiel #21
0
/** 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();
	}
}
Beispiel #22
0
// Read the dial commands and send them to the modem. Wait for the expected response then move on to the next command.
// Returns true if the last dial command has been processed.
static bool LinkManagement_DialConnection(const char** DialCommands)
{
	static char* ResponsePtr = NULL;
	char* CommandPtr = NULL;
	static uint8_t CharsMatched = 0;
	char c;

	if (USB_HostState != HOST_STATE_Configured)	
		return false;

	while (!RingBuffer_IsEmpty(&Modem_ReceiveBuffer))						    // Read back the response
	{	
		c = RingBuffer_Remove(&Modem_ReceiveBuffer);
		Debug_PrintChar(c);
		
		if (c == *(ResponsePtr + CharsMatched))									// Match the response character by character with the expected response						
			CharsMatched++;
		else
			CharsMatched = 0;
			
		if (CharsMatched != 0 && CharsMatched == strlen(ResponsePtr))			// Look for the expected response
		{
			DialSteps += 2;														// Move on to the next dial command
			CharsMatched = 0;
		}
	}	
		
	if (SystemTicks > 100)														// Space each command by 1 second
	{
		CommandPtr = (char*)DialCommands[DialSteps];
		ResponsePtr = (char*)DialCommands[DialSteps + 1];

		if (CommandPtr == NULL || ResponsePtr == NULL)							// No more dial commands
		{
			DialSteps = 0;
			return true;														// Finished dialling
		}

		SystemTicks = 0;

		Debug_Print("Send: "); Debug_Print(CommandPtr);
		Debug_Print("(Expect: "); Debug_Print(ResponsePtr); Debug_Print(")\r\n");
				
		while (*CommandPtr)														
			RingBuffer_Insert(&Modem_SendBuffer, *(CommandPtr++));			// Send the command	to the modem
	}

	return false;																// Haven't finished dialling
}
Beispiel #23
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;
}
Beispiel #24
0
/** 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);
    }
}
Beispiel #25
0
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;
}
Beispiel #27
0
/**
 *	This method runs the ADC thread
 *
 * @param buff								- The RingBuff type
 */
void ADXL335_Driver::run(RingBuff_t* buff){
	
	//! Containers
	int x_axis;
	int y_axis;
	int z_axis;	
	
	//! We get all axis values
	x_axis = this->get_accel(X_AXIS);
	delay(10);
	y_axis = this->get_accel(Y_AXIS);
	delay(10);
	z_axis = this->get_accel(Z_AXIS);
	delay(10);
	
	//! Create a data container
	uint8_t _data[SIZE_OF_DATA] = {x_axis, y_axis, z_axis, 0x00};
	
	//! We put the data into the ringbuffer
	for(uint8_t i = 0; i < sizeof(_data); i++){
		RingBuffer_Insert(buff, _data[i]);
	}
}
Beispiel #28
0
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;

	/* Read bytes from the USB OUT endpoint into the UART transmit buffer */
	int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
	if (!(ReceivedByte < 0) && !(RingBuffer_IsFull(&USBtoUART_Buffer)))
	  RingBuffer_Insert(&USBtoUART_Buffer, ReceivedByte);
	
	/* Check if the UART receive buffer flush timer has expired or buffer is nearly full */
	RingBuff_Count_t BufferCount = RingBuffer_GetCount(&UARTtoUSB_Buffer);
	if ((TIFR0 & (1 << TOV0)) || (BufferCount > 200))
	{
		TIFR0 |= (1 << TOV0);

		/* Read bytes from the UART receive buffer into the USB IN endpoint */
		while (BufferCount--)
		  CDC_Device_SendByte(&VirtualSerial_CDC_Interface, RingBuffer_Remove(&UARTtoUSB_Buffer));
	}

	CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
}
Beispiel #29
0
void USBVirtualSerial::println(uint8_t val, uint8_t format) {
    RingBuffer_Insert(&HostTXSerial_Buffer, val);
    RingBuffer_Insert(&HostTXSerial_Buffer, '\r');
    RingBuffer_Insert(&HostTXSerial_Buffer, '\n');
    CDC_Task();
}
Beispiel #30
0
void USBVirtualSerial::println(const char *str) {
    for (uint16_t i = 0; i < strlen(str); i++) RingBuffer_Insert(&HostTXSerial_Buffer, str[i]);
    RingBuffer_Insert(&HostTXSerial_Buffer, '\r');
    RingBuffer_Insert(&HostTXSerial_Buffer, '\n');
    CDC_Task();
}