Support for BiDiB

Transmit routine (for serial link)

On a serial link, BiDiB uses a special, the so called MAGIC, for the data frame identification. To avoid a MAGIC recognition within data packets, we must prevent to transmit a date with a content equal MAGIC. This is done by changing the date and placing another special character (ESCAPE) first. Therefore, ESCAPE must be ESCAPE'd too, in order to avoid decoding the wrong data at the receiver.

Characters to be sent will be tested for MAGIC and ESCAPE bytes and occasionally an ESCAPE will be insert. The actual transmitting of a character takes place with the transfer to the output buffer (tx_fifo_write).


static void bidib_send(unsigned char c)
  {
    if ((c == BIDIB_PKT_MAGIC) || (c == BIDIB_PKT_ESCAPE) )
      {
        tx_fifo_write(BIDIB_PKT_ESCAPE);        // escape this char
        c = c ^ 0x20;                           // 'veraendern'
      }
    tx_fifo_write(c);
  }

Transmitting of a MAGIC charakter requires a separate routine, MAGIC shall be not ESCAPE'd in this case:


static void bidib_send_delimiter(void)
  {
    tx_fifo_write(BIDIB_PKT_MAGIC);
  }

Sending a BiDiB message

Based on the routines above plus CRC-calculation, we can easily build a transmission routine for BiDiB messages:


void send_bidib_message(unsigned char *message)
  {
    unsigned char i=0;
    unsigned char length;
    unsigned char tx_crc = 0;

    bidib_send_delimiter();                       // send MAGIC

    length = message[0];
    bidib_send(length);
    tx_crc = crc_array[length ^ tx_crc];          // calc CRC

    for (i=1; i<=length; i++)
      {
        bidib_send(message[i]);
        tx_crc = crc_array[message[i] ^ tx_crc];
      }
    bidib_send(tx_crc);
    bidib_send_delimiter();                       // send termination MAGIC
  }

The final MAGIC may (and should!) be the first MAGIC of the next message too.

Example: Sending a SYS_MAGIC message

This transmission routine is used to send the message, here shown by the example of MSG_SYS_MAGIC:


const unsigned char bidib_sys_magic[] =         // this is our answer for SYS_MAGIC
  { 5,              // size
    0x00,           // addr
    0x00,           // msg_num
    MSG_SYS_MAGIC,
    BIDIB_SYS_MAGIC & 0xFF,
    (BIDIB_SYS_MAGIC >> 8) & 0xFF,
  };

static void bidib_send_sys_magic(void)
  {
    send_bidib_message((unsigned char *) bidib_sys_magic);
    bidib_tx0_msg_num = 0;
  }