This bits going to be quick large! This part is the receiving,sending and CRC functions are performed. Note that this would be a great point to be able to edit that 12byte maximum send/receive!
/*
**---------------------------------------------------------------------------
**
** Abstract: Receive J1850 frame (max 12 bytes)
**
** Parameters: Pointer to frame buffer
**
** Returns: Number of received bytes OR in case of error, error code with
** bit 7 set as error indication
**
**---------------------------------------------------------------------------
*/
uint8_t j1850_recv_msg(uint8_t *msg_buf )
//as seen in j1850.h file.. declared function : Arduino form = byte j1850_recv_msg(byte *msgbuf)
{
uint8_t nbits;
// bit position counter within a byte - byte nbits
uint8_t nbytes;
// number of received bytes -byte nbytes
uint8_t bit_state;
// used to compare bit state, active or passive -byte bit_state
/*
wait for responds
*/
timer1_start();
//Start the timer
while(!is_j1850_active())
// run as long bus is passive (IDLE) //Run While bus is idle - Idle = No Information passing through!!!!!
{
if(TCNT1 >= WAIT_100us)
// check for 100us // check if timer has reached 100us
{
timer1_stop();
//Once reached 100us, stop timer
return J1850_RETURN_CODE_NO_DATA | 0x80;
// error, no responds within 100us - No response after 100us.. NO DATA!
}
}
// wait for SOF -Start of frame
timer1_start();
// restart timer1 - Restart timer, TCNT1 will reset to 0
while(is_j1850_active())
// run as long bus is active (SOF is an active symbol) - Keep running while there is data available
{
if(TCNT1 >= RX_SOF_MAX) return J1850_RETURN_CODE_BUS_ERROR | 0x80;
// error on SOF timeout - If TCNT1 is greater that the SOF RX max time.. Error
}
timer1_stop();
// Stop timer
if(TCNT1 < RX_SOF_MIN) return J1850_RETURN_CODE_BUS_ERROR | 0x80;
// error, symbole was not SOF - If TCNT1 is less than min SOF.. Error
bit_state = is_j1850_active();
// store actual bus state - store bus state into variable
timer1_start(); - Start Timer again
for(nbytes = 0; nbytes < 12; ++nbytes)
// Do this process for for 12 consecutive bytes. (get total frame)
{
nbits = 8;
//set nbits = 8
do
//While loop start here.. do below while(--nbits);// end 8 bit while loop
{
*msg_buf <<= 1;
//msg buffer next byte
while(is_j1850_active() == bit_state)
// compare last with actual bus state, wait for change - Check if bitstate has changed for next section
{
if(TCNT1 >= RX_EOD_MIN )
// check for EOD symbol - If same state and TCNT1 is greater then EOD minimum.. return nbytes
{
timer1_stop();
//stop timer
return nbytes;
// return number of received bytes -return found nbytes
}
}
bit_state = is_j1850_active();
// store actual bus state - store new bus state
uint16_t tcnt1_buf = TCNT1;
//int tcnt1_buf = TCNT1 - this stores the timer process value into 16bit integer
timer1_start();
//Restart timer again
if( tcnt1_buf < RX_SHORT_MIN) return J1850_RETURN_CODE_BUS_ERROR | 0x80;
// error, pulse was to short - If the RX pulse was shorted than minimum.. error.
// check for short active pulse = "1" bit
if( (tcnt1_buf < RX_SHORT_MAX) && !is_j1850_active() )
//if tcnt1_buf value is less than the RX short max, and J1850 is not active
*msg_buf |= 1; //Store this bit
// check for long passive pulse = "1" bit
if( (tcnt1_buf > RX_LONG_MIN) && (tcnt1_buf < RX_LONG_MAX) && is_j1850_active() )
//if tcnt1_buf is greater than RXlongmin, less than RX Long Max and is active
*msg_buf |= 1;
//store this bit
} while(--nbits);
// end 8 bit while loop //loop through all 8 bits.
++msg_buf;
// store next byte in message buffer
}
// end 12 byte for loop - Loop through for 12bytes.
// return after a maximum of 12 bytes
timer1_stop();
//stop timer
return nbytes;
//return nbytes
}