/** 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();
	}
}
Example #2
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();
	}
}
Example #3
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);
			}
		}
	}
}
Example #4
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);
}
/** 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(&USARTtoUSB_Buffer, USARTtoUSB_Buffer_Data, sizeof(USARTtoUSB_Buffer_Data));

	sei();

	for (;;)
	{
		/* Echo bytes from the host to the target via the hardware USART */
		if ((UCSR1A & (1 << UDRE1)) && CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface))
		{
			UDR1 = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);

			LEDs_TurnOnLEDs(LEDMASK_TX);
			PulseMSRemaining.TxLEDPulse = TX_RX_LED_PULSE_MS;
		}

		/* Check if the millisecond timer has elapsed */
		if (TIFR0 & (1 << OCF0A))
		{
			/* Clear flush timer expiry flag */
			TIFR0 |= (1 << TOV0);

			/* Check if the reset pulse period has elapsed, if so tristate the target reset line */
			if (PulseMSRemaining.ResetPulse && !(--PulseMSRemaining.ResetPulse))
			{
				LEDs_TurnOffLEDs(LEDMASK_BUSY);
				AVR_RESET_LINE_DDR &= ~AVR_RESET_LINE_MASK;
			}

			/* Check if the LEDs should be ping-ponging (during enumeration) */
			if (PulseMSRemaining.PingPongLEDPulse && !(--PulseMSRemaining.PingPongLEDPulse))
			{
				LEDs_ToggleLEDs(LEDMASK_TX | LEDMASK_RX);
				PulseMSRemaining.PingPongLEDPulse = PING_PONG_LED_PULSE_MS;
			}

			/* 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);

			/* Check if the receive buffer flush period has expired */
			uint16_t BufferCount = RingBuffer_GetCount(&USARTtoUSB_Buffer);
			if (!(--FlushPeriodRemaining) || (BufferCount > 200))
			{
				FlushPeriodRemaining = RECEIVE_BUFFER_FLUSH_MS;

				/* Start RX LED indicator pulse */
				if (BufferCount)
				{
					LEDs_TurnOnLEDs(LEDMASK_RX);
					PulseMSRemaining.RxLEDPulse = TX_RX_LED_PULSE_MS;
				}

				/* Echo bytes from the target to the host via the virtual serial port */
				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);
				}
			}
		}

		CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
		USB_USBTask();
	}
}
Example #6
0
//------------------------------------------------------------------------------
static uint8_t serialProcess(Application *app)
{
    uint16_t count = RingBuffer_GetCount(&app->buffer_);
    if(count == 0)
        return true;

    uint8_t cmd = RingBuffer_Peek(&app->buffer_);
    switch(cmd)
    {
        case 'L':
            RingBuffer_Remove(&app->buffer_);
            LEDs_ToggleLEDs(LEDS_LED1);
            break;

        case 'R':
            RingBuffer_Remove(&app->buffer_);
            asicReset();
            asicResetSpi();
            app->state.state = STATE_RESET;
            sendCmdReply(app, cmd, (uint8_t *)&app->state, sizeof(State));
            break;

        case 'I':
            RingBuffer_Remove(&app->buffer_);
            sendCmdReply(app, cmd, (uint8_t *)&ID, sizeof(ID));
            break;

        case 'A':
            RingBuffer_Remove(&app->buffer_);
            app->state.state = STATE_RESET;
            asicStop();
            sendCmdReply(app, cmd, (uint8_t *)&app->state, sizeof(State));
            break;

        case 'S':
            RingBuffer_Remove(&app->buffer_);
            sendCmdReply(app, cmd, (uint8_t *)&app->state, sizeof(State));
            break;

        case 'W':
            if(count > 44) // 32 bytes midstate + 12 bytes data
            {
                RingBuffer_Remove(&app->buffer_);

                uint8_t i = 0;
                uint8_t *midstate = (uint8_t *)app->worktask.midstate;
                uint8_t *data = (uint8_t *)app->worktask.data;
                for(i=0; i<32; i++)
                {
                    midstate[i] = RingBuffer_Remove(&app->buffer_);
                }
                for(i=0; i<12; i++)
                {
                    data[i] = RingBuffer_Remove(&app->buffer_);
                }

                asicPrecalc(app->worktask.midstate, app->worktask.data, app->worktask.precalc);

                app->state.state = STATE_WORKING;
                app->state.nonce_valid = 0;
                app->state.nonce = 0;

                asicReset();
                asicResetSpi();
                pushWork(app, &app->worktask);
                timerSet(&app->work_timer, 16000);

                sendCmdReply(app, cmd, (uint8_t *)&app->state, sizeof(State));
            }
            break;

        default:
            RingBuffer_Remove(&app->buffer_);
            break;
    }

    return true;
}
Example #7
0
void parse_SER_buffer(uint8_t EGT_H, uint8_t EGT_L, uint8_t CHT_H, uint8_t CHT_L, uint32_t total_time_RPM)
{
	uint8_t OBD_headers = 0;
	uint16_t RPM_calc;
	uint8_t temp_ringer;
	uint8_t temp_mode;
	uint16_t tempEGTCHT1;
	uint16_t tempEGTCHT2;

	uint8_t ascii_1;
	uint8_t ascii_2;
	uint8_t ascii_3;
	uint8_t ascii_4;
	
	if( (!(RingBuffer_IsEmpty(&BTtoFF_Buffer))))
	{
		
		temp_ringer = RingBuffer_Remove(&BTtoFF_Buffer);
		
		if(temp_ringer == 'A')
		{
			temp_ringer = RingBuffer_Remove(&BTtoFF_Buffer);
			
			if(temp_ringer == 'T')	// We now have an AT command to parse
			{
				temp_ringer = RingBuffer_Remove(&BTtoFF_Buffer);
				
				if(temp_ringer == 'Z')	// Reset command, fake it
				{
					//Serial_SendString_P(ELM327_ID);
					RingBuffer_Insert(&FFtoBT_Buffer,'E');
					RingBuffer_Insert(&FFtoBT_Buffer,'L');
					RingBuffer_Insert(&FFtoBT_Buffer,'M');
					RingBuffer_Insert(&FFtoBT_Buffer,'3');
					RingBuffer_Insert(&FFtoBT_Buffer,'2');
					RingBuffer_Insert(&FFtoBT_Buffer,'7');
					RingBuffer_Insert(&FFtoBT_Buffer,0X20);	// space
					RingBuffer_Insert(&FFtoBT_Buffer,'v');
					RingBuffer_Insert(&FFtoBT_Buffer,'1');
					RingBuffer_Insert(&FFtoBT_Buffer,'.');
					RingBuffer_Insert(&FFtoBT_Buffer,'3');
					send_ELM327_CR();
					send_ELM327_prompt();
					temp_ringer = RingBuffer_Remove(&BTtoFF_Buffer);	// PULLS CR out of buffer
					CR_flag = 0;	// Clears CR flag
				}
				else if(temp_ringer == 'E')	// Echo Command
				{
					temp_ringer = RingBuffer_Remove(&BTtoFF_Buffer);
					send_ELM327_OK();
					send_ELM327_prompt();
					temp_ringer = RingBuffer_Remove(&BTtoFF_Buffer);	// PULLS CR out of buffer
					CR_flag = 0;	// Clears CR flag
				
				}
				else if(temp_ringer == 'M')	// Protocol Memory Command
				{
					temp_ringer = RingBuffer_Remove(&BTtoFF_Buffer);
					send_ELM327_OK();
					send_ELM327_prompt();
					temp_ringer = RingBuffer_Remove(&BTtoFF_Buffer);	// PULLS CR out of buffer
					CR_flag = 0;	// Clears CR flag
				
				}
				else if(temp_ringer == 'L')	// Line Feed Command
				{
					temp_ringer = RingBuffer_Remove(&BTtoFF_Buffer);
					send_ELM327_OK();
					send_ELM327_prompt();
					temp_ringer = RingBuffer_Remove(&BTtoFF_Buffer);	// PULLS CR out of buffer
					CR_flag = 0;	// Clears CR flag
				
				}
				else if(temp_ringer == 'S')	// Blank Spaces or Store Protocol Command
				{
					temp_ringer = RingBuffer_Remove(&BTtoFF_Buffer);
					if(temp_ringer == 'H')	// Set Header
					{
						temp_ringer = RingBuffer_Remove(&BTtoFF_Buffer);
						temp_ringer = RingBuffer_Remove(&BTtoFF_Buffer);
						temp_ringer = RingBuffer_Remove(&BTtoFF_Buffer);
						temp_ringer = RingBuffer_Remove(&BTtoFF_Buffer);
						temp_ringer = RingBuffer_Remove(&BTtoFF_Buffer);
						temp_ringer = RingBuffer_Remove(&BTtoFF_Buffer);
					}
					else if(temp_ringer == 'P')	// Set Protocol
					{
						temp_ringer = RingBuffer_Remove(&BTtoFF_Buffer);	// 0 or 1
					}
					
					send_ELM327_OK();
					send_ELM327_prompt();
					temp_ringer = RingBuffer_Remove(&BTtoFF_Buffer);	// PULLS CR out of buffer		
					CR_flag = 0;	// Clears CR flag
				
				}
				else if(temp_ringer == 'H')	// Headers Command
				{
					temp_ringer = RingBuffer_Remove(&BTtoFF_Buffer);
					//SendSERBuffer[temp_USB_indexer++]  = temp_ringer;
					if(temp_ringer == '1')
					{
						OBD_headers = 1;
					}
					else
					{
						OBD_headers = 0;
					}
					send_ELM327_OK();
					send_ELM327_prompt();
					temp_ringer = RingBuffer_Remove(&BTtoFF_Buffer);	// PULLS CR out of buffer
					CR_flag = 0;	// Clears CR flag
				
				}
				
				else if(temp_ringer == 'R')	// Responses Command
				{
					temp_ringer = RingBuffer_Remove(&BTtoFF_Buffer);
					//SendSERBuffer[temp_USB_indexer++]  = temp_ringer;
					if(temp_ringer == 'V')
					{
						//Serial_SendString_P(FAKE_VOLTAGE);
						RingBuffer_Insert(&FFtoBT_Buffer,'1');
						RingBuffer_Insert(&FFtoBT_Buffer,'2');
						RingBuffer_Insert(&FFtoBT_Buffer,'.');
						RingBuffer_Insert(&FFtoBT_Buffer,'6');
						RingBuffer_Insert(&FFtoBT_Buffer,'V');
						send_ELM327_CR();
						send_ELM327_prompt();
					}
					else
					{					
						send_ELM327_OK();
						send_ELM327_prompt();
					}
					temp_ringer = RingBuffer_Remove(&BTtoFF_Buffer);	// PULLS CR out of buffer
					CR_flag = 0;	// Clears CR flag
				
				}
				else if(temp_ringer == 'V')	// Variable DLC Command
				{
					temp_ringer = RingBuffer_Remove(&BTtoFF_Buffer);
					//SendSERBuffer[temp_USB_indexer++]  = temp_ringer;
					send_ELM327_OK();
					send_ELM327_prompt();
					temp_ringer = RingBuffer_Remove(&BTtoFF_Buffer);	// PULLS CR out of buffer
					CR_flag = 0;	// Clears CR flag
					
				}
				else if(temp_ringer == '1')	// ATAT1 Adaptive headers Command
				{
					send_ELM327_OK();
					send_ELM327_prompt();
					temp_ringer = RingBuffer_Remove(&BTtoFF_Buffer);	// PULLS CR out of buffer
					CR_flag = 0;	// Clears CR flagg
				
				}
				else if(temp_ringer == '@')	// Blank Spaces Command
				{
					temp_ringer = RingBuffer_Remove(&BTtoFF_Buffer);
					//SendSERBuffer[temp_USB_indexer++]  = temp_ringer;
					if(temp_ringer == '1')	// Descriptor
					{
						//Serial_SendString_P(FF_mini);
						RingBuffer_Insert(&FFtoBT_Buffer,'F');
						RingBuffer_Insert(&FFtoBT_Buffer,'F');
						RingBuffer_Insert(&FFtoBT_Buffer,'m');
						RingBuffer_Insert(&FFtoBT_Buffer,'i');
						RingBuffer_Insert(&FFtoBT_Buffer,'n');
						RingBuffer_Insert(&FFtoBT_Buffer,'i');
						send_ELM327_CR();
						send_ELM327_prompt();
						temp_ringer = RingBuffer_Remove(&BTtoFF_Buffer);	// PULLS CR out of buffer
						CR_flag = 0;	// Clears CR flag
					}
					else
					{
						send_ELM327_OK();
						send_ELM327_prompt();
						temp_ringer = RingBuffer_Remove(&BTtoFF_Buffer);	// PULLS CR out of buffer
						CR_flag = 0;	// Clears CR flag
					}
				
				}
				else if(temp_ringer == 'I')	// ID Yourself command
				{
					//Serial_SendString_P(ELM327_ID);
					RingBuffer_Insert(&FFtoBT_Buffer,'E');
					RingBuffer_Insert(&FFtoBT_Buffer,'L');
					RingBuffer_Insert(&FFtoBT_Buffer,'M');
					RingBuffer_Insert(&FFtoBT_Buffer,'3');
					RingBuffer_Insert(&FFtoBT_Buffer,'2');
					RingBuffer_Insert(&FFtoBT_Buffer,'7');
					RingBuffer_Insert(&FFtoBT_Buffer,0X20);	// space
					RingBuffer_Insert(&FFtoBT_Buffer,'v');
					RingBuffer_Insert(&FFtoBT_Buffer,'1');
					RingBuffer_Insert(&FFtoBT_Buffer,'.');
					RingBuffer_Insert(&FFtoBT_Buffer,'3');
					send_ELM327_CR();
					send_ELM327_prompt();
					temp_ringer = RingBuffer_Remove(&BTtoFF_Buffer);	// PULLS CR out of buffer
					CR_flag = 0;	// Clears CR flag
				
				}
			
			}
		}
	//***********************************************************
	//	Didnt find AT command, so look for OBD command
	//***********************************************************
		else if (temp_ringer == '0')
		{
			temp_mode = RingBuffer_Remove(&BTtoFF_Buffer);
			//SendSERBuffer[temp_USB_indexer++]  = temp_mode;
			switch (temp_mode)
			{
				case '1':	// Mode 01
				temp_ringer = RingBuffer_Remove(&BTtoFF_Buffer);
				//SendSERBuffer[temp_USB_indexer++]  = temp_ringer;
				if ((RingBuffer_Peek(&BTtoFF_Buffer)== '0'))
				{
					RingBuffer_Remove(&BTtoFF_Buffer);	// Removes peeked value above
					//SendSERBuffer[temp_USB_indexer++]  = '0';
					if(temp_ringer == '0')	// What PIDS are supported	0100
					{
						//Serial_SendByte('4');
						//Serial_SendByte('1');
						RingBuffer_Insert(&FFtoBT_Buffer,'4');
						RingBuffer_Insert(&FFtoBT_Buffer,'1');
						
						if(OBD_headers)
						{
							send_ELM327_header();		
						}
						//Serial_SendByte('0');
						//Serial_SendByte('0');
						RingBuffer_Insert(&FFtoBT_Buffer,'0');
						RingBuffer_Insert(&FFtoBT_Buffer,'0');
						// Supported PIDS below
						//Serial_SendByte('0');
						//Serial_SendByte('0');
						//Serial_SendByte('1');	// Support for RPM PID 0x0C
						//Serial_SendByte('0');
						//Serial_SendByte('0');
						//Serial_SendByte('0');
						//Serial_SendByte('0');
						//Serial_SendByte('0');
						RingBuffer_Insert(&FFtoBT_Buffer,'0');
						RingBuffer_Insert(&FFtoBT_Buffer,'0');
						RingBuffer_Insert(&FFtoBT_Buffer,'1');
						RingBuffer_Insert(&FFtoBT_Buffer,'0');
						RingBuffer_Insert(&FFtoBT_Buffer,'0');
						RingBuffer_Insert(&FFtoBT_Buffer,'0');
						RingBuffer_Insert(&FFtoBT_Buffer,'0');
						RingBuffer_Insert(&FFtoBT_Buffer,'0');
						send_ELM327_CR();
						send_ELM327_prompt();
						temp_ringer = RingBuffer_Remove(&BTtoFF_Buffer);	// PULLS CR out of buffer
						CR_flag = 0;	// Clears CR flag
					}
					else if (temp_ringer == '2')	// 0120, PIDS 21-40 supported bit mask
					{
						//Serial_SendByte('4');
						//Serial_SendByte('1');
						//Serial_SendByte('2');
						//Serial_SendByte('0');
						RingBuffer_Insert(&FFtoBT_Buffer,'4');
						RingBuffer_Insert(&FFtoBT_Buffer,'1');
						RingBuffer_Insert(&FFtoBT_Buffer,'2');
						RingBuffer_Insert(&FFtoBT_Buffer,'0');						
						// Supported PIDS below
						//Serial_SendByte('0');
						//Serial_SendByte('0');
						//Serial_SendByte('0');
						//Serial_SendByte('0');
						//Serial_SendByte('0');
						//Serial_SendByte('0');	// Support for EGT bank PID 0x78
						//Serial_SendByte('1');
						//Serial_SendByte('8');
						RingBuffer_Insert(&FFtoBT_Buffer,'0');
						RingBuffer_Insert(&FFtoBT_Buffer,'0');
						RingBuffer_Insert(&FFtoBT_Buffer,'0');
						RingBuffer_Insert(&FFtoBT_Buffer,'0');
						RingBuffer_Insert(&FFtoBT_Buffer,'0');
						RingBuffer_Insert(&FFtoBT_Buffer,'0');
						RingBuffer_Insert(&FFtoBT_Buffer,'1');
						RingBuffer_Insert(&FFtoBT_Buffer,'8');
						send_ELM327_CR();
						send_ELM327_prompt();
						temp_ringer = RingBuffer_Remove(&BTtoFF_Buffer);	// PULLS CR out of buffer
						CR_flag = 0;	// Clears CR flag
					}
				
					else    // kind of assuming its asking for what we support
					{
						//Serial_SendByte('4');
						//Serial_SendByte('1');
						//Serial_SendByte(temp_ringer);
						//Serial_SendByte('0');
						RingBuffer_Insert(&FFtoBT_Buffer,'4');
						RingBuffer_Insert(&FFtoBT_Buffer,'1');
						RingBuffer_Insert(&FFtoBT_Buffer,temp_ringer);
						RingBuffer_Insert(&FFtoBT_Buffer,'0');
						// Supported PIDS below
						//Serial_SendByte('0');
						//Serial_SendByte('0');
						//Serial_SendByte('0');
						//Serial_SendByte('0');
						//Serial_SendByte('0');
						//Serial_SendByte('0');
						//Serial_SendByte('0');
						//Serial_SendByte('0');
						RingBuffer_Insert(&FFtoBT_Buffer,'0');
						RingBuffer_Insert(&FFtoBT_Buffer,'0');
						RingBuffer_Insert(&FFtoBT_Buffer,'0');
						RingBuffer_Insert(&FFtoBT_Buffer,'0');
						RingBuffer_Insert(&FFtoBT_Buffer,'0');
						RingBuffer_Insert(&FFtoBT_Buffer,'0');
						RingBuffer_Insert(&FFtoBT_Buffer,'0');
						RingBuffer_Insert(&FFtoBT_Buffer,'0');
						send_ELM327_CR();	//	CR
						send_ELM327_prompt();
						temp_ringer = RingBuffer_Remove(&BTtoFF_Buffer);	// PULLS CR out of buffer
						CR_flag = 0;	// Clears CR flag
					}
				
				}
			
				else if (temp_ringer == '0')	// 010x
				{
					temp_ringer = RingBuffer_Remove(&BTtoFF_Buffer);
					
					if(temp_ringer == 'C')		// 010C
					{
						// RPM request
						//if((!first_cycle)&&(total_time_RPM))
						if(total_time_RPM)
						{
							//	This takes the total_time = timer1_OVF + timer1_HIGH + timer1_LOW
							//	and converts to the format required for OBD msg
							//	AND IT NEEDS TO BE OPTIMIZED TOO LARGE!!
							RPM_calc = (uint16_t)(((0xE4E1C000)/total_time_RPM));	
							ascii_4 =  pgm_read_byte(&hex_map[RPM_calc&0x000F]);
							RPM_calc >>=0x04;
							ascii_3 =  pgm_read_byte(&hex_map[RPM_calc&0x000F]);
							RPM_calc >>=0x04;
							ascii_2 =  pgm_read_byte(&hex_map[RPM_calc&0x000F]);
							RPM_calc >>=0x04;
							ascii_1 =  pgm_read_byte(&hex_map[RPM_calc&0x000F]);
						}
						else
						{
							// IDLE
							ascii_1 = '0';
							ascii_2 = '0';
							ascii_3 = '0';
							ascii_4 = '0';
						}
						//Serial_SendByte('4');
						//Serial_SendByte('1');
						//Serial_SendByte('0');
						//Serial_SendByte('C');
						RingBuffer_Insert(&FFtoBT_Buffer,'4');
						RingBuffer_Insert(&FFtoBT_Buffer,'1');
						RingBuffer_Insert(&FFtoBT_Buffer,'0');
						RingBuffer_Insert(&FFtoBT_Buffer,'C');
						//Serial_SendByte(ascii_1);
						//Serial_SendByte(ascii_2);
						//Serial_SendByte(ascii_3);
						//Serial_SendByte(ascii_4);
						RingBuffer_Insert(&FFtoBT_Buffer,ascii_1);
						RingBuffer_Insert(&FFtoBT_Buffer,ascii_2);
						RingBuffer_Insert(&FFtoBT_Buffer,ascii_3);
						RingBuffer_Insert(&FFtoBT_Buffer,ascii_4);
						send_ELM327_CR();
						send_ELM327_prompt();
						temp_ringer = RingBuffer_Remove(&BTtoFF_Buffer);	// PULLS CR out of buffer
						CR_flag = 0;	// Clears CR flag
					}
				}
Example #8
0
int main(void)
{
    SetupHardware();

    RingBuffer_InitBuffer(&RF12toUSB_Buffer, RF12toUSB_Buffer_Data, sizeof(RF12toUSB_Buffer_Data));
    RingBuffer_InitBuffer(&USBtoRF12_Buffer, USBtoRF12_Buffer_Data, sizeof(USBtoRF12_Buffer_Data));
    RingBuffer_InitBuffer(&Transmit_Buffer,  Transmit_Buffer_Data,  sizeof(Transmit_Buffer_Data));

    sei();

    for (;;)
    {
        uint16_t RFM12B_status = RFM12B_SPI_Transfer(0);
        
        if (( RFM12B_status & RFM12B_STATUS_RSSI )
           && !RFM12B_Transmit_Active )
        {
            PORTD &= ~0x40;
        }
        else
        {
            PORTD |= 0x40;
        }
        
        /* Only try to read in bytes from the CDC interface if the transmit buffer is not full */
        if (  !RingBuffer_IsFull( &USBtoRF12_Buffer )
           && !RFM12B_Transmit_Active )
        {
            int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);

            /* Read bytes from the USB OUT endpoint into the USART transmit buffer */
            if (!(ReceivedByte < 0)) 
            {  
               if ( ReceivedByte != END_OF_PACKET )
               {
                   RingBuffer_Insert( &USBtoRF12_Buffer, ReceivedByte );
               }
               else
               {
                   /* TODO : Need to implement LBT */
                   RFM12B_Start_Transmit();
               }
            }
        }

        /* Check to see if there's an RGIT or an FFIT bit set in the status
           register indicating that we need to send or receive a byte */
        if ( RFM12B_status & RFM12B_STATUS_RGIT )
        {
            if ( RFM12B_Transmit_Active )
            {
                RFM12B_Transmit();
            }
            else
            {
                RFM12B_Receive();
            }
        }
        
        /* Check the flush timer.  This is used to timeout incoming packets
           that don't arrive in time or to flush the data to the USB host.
           First, check to see if we are in the middle of receiving a packet */
        if ( RxLength )
        {
            /* A packet is being received. Check the flush timer
               and timeout the packet if it takes too long */
            if ( TIFR0 & _BV(TOV0))
            {
                /* Clear flush timer expiry flag */
                TIFR0 |= _BV(TOV0);

                /* Flush timer overflows every 4ms.  256 bytes at ~50k
                   should take approximately 42ms so we allow 12 counts
                   for overflow.  This should probably be calculated
                   based on the bit rate and max message length */
                if ( ++RxTimeout > 12 )
                {
                    RingBuffer_Insert( &RF12toUSB_Buffer, END_OF_PACKET );
                    RFM12B_SPI_Transfer( 0xCA81 );
                    RFM12B_SPI_Transfer( 0xCA83 );
                    RxLength = 0;
                }
            }
        }
        else
        {
            /* No packet is being received.  Check if the UART receive 
               buffer flush timer has expired or the buffer is nearly full */
            uint16_t BufferCount = RingBuffer_GetCount(&RF12toUSB_Buffer);
            if ((TIFR0 & (1 << TOV0)) || (BufferCount > (uint8_t)(sizeof(RF12toUSB_Buffer_Data) * .75)))
            {
                /* Clear flush timer expiry flag */
                TIFR0 |= _BV(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(&RF12toUSB_Buffer)) != ENDPOINT_READYWAIT_NoError)
                    {
                        break;
                    }
    
                    /* Dequeue the already sent byte from the buffer now we have confirmed that no transmission error occurred */
                    RingBuffer_Remove(&RF12toUSB_Buffer);
                }
            }
        }
        
        CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
        USB_USBTask();
    }
}