Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Serial communication with AVR atmel xmega A

Status
Not open for further replies.

RoboWanabe

Member
I i was wondering if some one could explain whats going on in this pice of code: For e.g. :

1) what if some one has sent data on the line but the function serial_get_char() has already been passed? I presume that a flag gets set and waits till it is called again?

2) If some one could just explain how the data comes in where it is stored and how it is analysed? it looks like it only takes one character so how does it build up a word? with some kind of buffer?

3) just some general serial operational knowledge would be great thank you :) im struggling getting my head round it

Code:
//*************************************************************************//
void mesh_commands_serve(void)
{
  static unsigned char flag_delimiter = TRUE;
  char c = 0;
  char *sPtr;
  char textbuffer[40];
  signed long timeout_check;

  // fetch a byte from serial receive stream
  c = mesh_serial_get_char();


  // if delimiter has been received, and c is not a delimiter
  if ( (flag_delimiter != FALSE) && (c != '\r') )
  {
    // and if c is a valid message character
    if (c >= 0x20)
    {
      // then flag message not delimited
      flag_delimiter = FALSE;
      // clear the message buffer
      mesh_message_clear();
      // and write c to message buffer
      mesh_message_append(c);

    }
  }
  else
  {
    // otherwise check for delimiter
    if (c == '\r')
    {
#ifdef MESH_DEBUG
console_put_string("[");
console_put_string(mesh_message_buffer);
console_put_string("]\r\n");
#endif
      // flag message as delimited
      flag_delimiter = TRUE;
      // process message string
      sPtr = strchr(mesh_message_buffer, ':');
      if (sPtr != 0)
      {
        sPtr++;
        mesh_message_parse(sPtr);
      }
    }
    else
    {
      // otherwise check if c is a valid message character
      if (c >= 0x20)
      {
        // then append c to message buffer
        mesh_message_append(c);
      }
    }
  }

  // transmit status beacon
  if (state_mesh_radio == RADIO_STATE_ONLINE)
  {
	  if ((time - mesh_beacon_time) >= 0)
	  {

  	  // Format is !R=nuuuussss
  	  // where
  	  //   !R= is the message identifier
  	  //     n is the index (LHA number, 1 to 4)
  	  //  uuuu is the 16-bit node address (in hex)
  	  //  ssss is the reported status (in hex)
      //     p is the current  on/off state
      //     i is the current  intensity (1 to 5)
  	  //
  	  // Example !R=3C4D700081312
  	  // means LHA 3 has node address 0xC4D7, reports a glideslope fault,
      // is currently set to on at intensity 2.

     /* sprintf(textbuffer, "!R=%01X%04X%04X%01X%01X",
	      mesh_settings.link_address,
        mesh_settings.nodeaddress,
        system_status,
        (system_light_on & 0x01),
        ((ram_data.runtime.opmode + 1) & 0x07));  */

#ifdef MESH_DEBUG
//	sprintf(textbuffer+6, "%02X%04X", debug_status_id++, system_status);
console_put_string(textbuffer);
console_put_string("\r\n");
#endif
//	    link_broadcast(textbuffer);
	    mesh_transmit(MESH_COORDINATOR, textbuffer);
	    mesh_beacon_time = time + MESH_BEACON_PERIOD;
	  }
	}

  // check for external coordinator presence
  atomic_begin();
    timeout_check = (time - mesh_coordinator_message_timeout);
  atomic_end();
  if ( (ram_data.config.option_radio != FALSE)
    && (timeout_check >= 0)
     )
  {
    atomic_begin();
      mesh_coordinator_message_timeout = time + COORDINATOR_MESSAGE_TIMEOUT;
    atomic_end();
#ifdef MESH_DEBUG
sprintf(textbuffer,"time=%li",time - mesh_coordinator_message_timeout);
console_put_string(CRLF);
console_put_string(textbuffer);
console_put_string("NETWORK GONE!\r\n");
#else
    mesh_radio_send("AT+WLEAVE");
    mesh_state_set(mesh_state_offline);
#endif
  }

}
//*************************************************************************//
 
Last edited by a moderator:
based on the call "c = mesh_serial_get_char();" it returns the char in the variable c. It will get it from the serial buffer (the input register of the UART device). If it is an interrupt driven routine, there may be a buffer, depends on the interrupt service routine. If polled, it will be the last char received (management cannot be responsible for missed data). The routine needs to be called faster than data is received, or even the interrupt buffer will overflow. I would have to assume (not knowing what mesh_serial_get_char does) it returns a null if there has been no data received. It is up to you to build the character string from the characters returned from the routine, which is done here:

// if delimiter has been received, and c is not a delimiter
if ( (flag_delimiter != FALSE) && (c != '\r') )
{
// and if c is a valid message character
if (c >= 0x20)
{
// then flag message not delimited
flag_delimiter = FALSE;
// clear the message buffer
mesh_message_clear();
// and write c to message buffer
mesh_message_append(c);
with the mesh_message_append(c) command, after it checks for a "delimiter" (non message flag byte).
 
Last edited:
Hi thanks guys i managed to sort it in the end. I does uses an interupt routine that is called when ever the serial transmit or receive flag is set which then ether empty's the data buffer or fills it with data to send.

It turned out to be alot more simpler than i had previously thought :)

Cheers for you help anyway.
 
Status
Not open for further replies.

Latest threads

Back
Top