Ce site contient essentiellement des notes de travail. Le contenu est en constante évolution, et loin d'être achevé. (+ d'infos)
La plupart des documentations informatiques sont orientées Debian / Ubuntu.

Electronique/Hardware/Communication/XRF/Loopback tester

De Ordinoscope.net
Sauter à la navigation Sauter à la recherche

Introduction

En dépit de ne pouvoir obtenir un niveau de signal (RSSI) avec mes modules XRF v1.3, j'ai écrit ce testeur de loopback, une sorte de ping avec un caractère aléatoire renvoyé par le loopback. Fonctionne sur un Arduino UNO ou équivalent, et un écran LCD sériel pour le master.

Code

Loopback

#include <NewSoftSerial.h>

#define PIN_LED    13
#define PIN_XRF_RX 11
#define PIN_XRF_TX 12

#define PRINT   0
#define NOPRINT 1
#define NOLN    2

NewSoftSerial XRF (PIN_XRF_RX, PIN_XRF_TX);

void xrf_print_info () {
  String r;
  
  Serial.println ();
  Serial.println ("Reading XRF settings");
  
  xrf_send ("+++", NOLN|NOPRINT);
  
  r = xrf_send ("ATNT", NOPRINT);
  Serial.print ("Node type: ");
  Serial.println (r);

  r = xrf_send ("ATID", NOPRINT);
  Serial.print ("PAN ID: ");
  Serial.println (r);

  r = xrf_send ("ATCH", NOPRINT);
  Serial.print ("Frequency: ");
  Serial.println (r);

  r = xrf_send ("ATPL", NOPRINT);
  Serial.print ("Power level: ");
  Serial.println (r);

  r = xrf_send ("ATEE", NOPRINT);
  Serial.print ("Encryption: ");
  Serial.println (r);
  
  r = xrf_send ("ATDN", NOPRINT);

  delay (1000);
  Serial.println ();
}

String xrf_send (String msg, int opts) {
  unsigned char c;
  String r;
  
  while (XRF.available ()) XRF.read ();
  
  if (!(opts & NOPRINT)) Serial.print ("Sending [");
  
  if ((opts & NOLN)) {
    if (!(opts & NOPRINT)) Serial.print (msg);
    XRF.print (msg);
  } else {
    if (!(opts & NOPRINT)) {
      Serial.print (msg);
      Serial.print ("\\n");
    }
    XRF.println (msg);
  }

  if (!(opts & NOPRINT)) Serial.println ("]");
  
  while (!XRF.available ());
  while (c != 0xD) {
    if (XRF.available ()) {
      c = (unsigned char) XRF.read ();
      r += c;
    }
  }
  if (!(opts & NOPRINT)) Serial.println (r);
  return r;
}

void setup () {
  
  // init LED
  pinMode (PIN_LED, OUTPUT);

  // init serial
  Serial.begin (115200);
  Serial.println ("XRF tester - loopback");
  
  // init XRF
  pinMode (PIN_XRF_RX, INPUT);
  pinMode (PIN_XRF_TX, OUTPUT);
  XRF.begin (9600);
  delay (5000);
  xrf_print_info ();
}

void loop () {
  byte c;
  
  while (XRF.available ()) {
    digitalWrite (PIN_LED, HIGH);
    c = XRF.read ();
    Serial.println (c, HEX);
    delay (50);
    XRF.print (c);
    digitalWrite (PIN_LED, LOW);
  }
}

Master

#include <NewSoftSerial.h>

#define PIN_LCD    9
#define PIN_LED    13
#define PIN_XRF_RX 11
#define PIN_XRF_TX 12

#define PRINT   0
#define NOPRINT 1
#define NOLN    2

NewSoftSerial XRF (PIN_XRF_RX, PIN_XRF_TX);
NewSoftSerial LCD (PIN_LCD, PIN_LCD);

void lcdClear () {
  LCD.write (0xFE);
  LCD.write (0x01);
  delay(10);
}

//position = line 1: 0-15, line 2: 16-31, 31+ defaults back to 0
void lcdGoto (int position) {
  if (position < 16) {
    LCD.write (0xFE);
    LCD.write (position + 128);
  } else if (position<32) {
    LCD.write (0xFE);
    LCD.write (position + 48 + 128);
  } else {
    lcdGoto (0);
  }
  delay(10);
}

void lcdPrintLeft (int pos, String msg, int length) {
  int i;
  int l = length - msg.length ();
  lcdGoto (pos);
  LCD.print (msg);
  for (i=0; i<l; i++) LCD.print (" ");
}

void lcdPrintRight (int pos, String msg, int length) {
  int i;
  int l = length - msg.length ();
  lcdGoto (pos);
  for (i=0; i<l; i++) LCD.print (" ");
  LCD.print (msg);
}

void xrf_print_info () {
  String r;
  
  Serial.println ();
  Serial.println ("Reading XRF settings");
  
  xrf_send ("+++", NOLN|NOPRINT);
  
  r = xrf_send ("ATNT", NOPRINT);
  Serial.print ("Node type: ");
  Serial.println (r);

  r = xrf_send ("ATID", NOPRINT);
  Serial.print ("PAN ID: ");
  Serial.println (r);

  r = xrf_send ("ATCH", NOPRINT);
  Serial.print ("Frequency: ");
  Serial.println (r);

  r = xrf_send ("ATPL", NOPRINT);
  Serial.print ("Power level: ");
  Serial.println (r);

  r = xrf_send ("ATEE", NOPRINT);
  Serial.print ("Encryption: ");
  Serial.println (r);
  
  r = xrf_send ("ATDN", NOPRINT);

  delay (1000);
  Serial.println ();
}

String xrf_send (String msg, int opts) {
  unsigned char c;
  String r;
  
  while (XRF.available ()) XRF.read ();
  
  if (!(opts & NOPRINT)) Serial.print ("Sending [");
  
  if ((opts & NOLN)) {
    if (!(opts & NOPRINT)) Serial.print (msg);
    XRF.print (msg);
  } else {
    if (!(opts & NOPRINT)) {
      Serial.print (msg);
      Serial.print ("\\n");
    }
    XRF.println (msg);
  }

  if (!(opts & NOPRINT)) Serial.println ("]");
  
  while (!XRF.available ());
  while (c != 0xD) {
    if (XRF.available ()) {
      c = (unsigned char) XRF.read ();
      r += c;
    }
  }
  if (!(opts & NOPRINT)) Serial.println (r);
  return r;
}

void setup () {
  
  // init serial
  Serial.begin (115200);
  Serial.println ("XRF tester - master");
  
  // init LED
  pinMode (PIN_LED, OUTPUT);
  
  // init LCD
  pinMode (PIN_LCD, OUTPUT);
  LCD.begin (9600);
  lcdClear ();
  lcdGoto (0);
  lcdPrintLeft (0, "XRF tester", 16);
  
  // init XRF
  lcdPrintLeft (16, "Initializing XRF", 16);
  pinMode (PIN_XRF_RX, INPUT);
  pinMode (PIN_XRF_TX, OUTPUT);
  XRF.begin (9600);
  delay (5000);
  xrf_print_info ();

  // set screen layout
  lcdGoto (0);
  lcdPrintLeft (0, "XRF tester     %", 16);
  lcdPrintLeft (16, "G:      B:", 16);
}

void loop () {
  static int good = 0;
  static int bad  = 0;
  int timeout = 1000;
  int time = 0;
  int pct;
  byte c;
  byte r = random (0, 254);
  
  while (XRF.available ()) XRF.read ();
  XRF.print (r);
  
  while (!(XRF.available ()) && time < timeout) {
    time += 10;
    delay (10);
  }
  
  if (!(XRF.available ())) {
    Serial.print ("timeout - ");
    bad++;
  } else {
    digitalWrite (PIN_LED, HIGH);
    c = XRF.read ();
    if (c == r) {
      Serial.print ("OK - ");
      good++;
    } else {
      Serial.print ("ERR: ");
      Serial.print (r, HEX);
      Serial.print (" -> ");
      Serial.print (c, HEX);
      Serial.print (" - ");
      bad++;
    }
  }
  
  lcdPrintRight (18, String (good), 5);
  lcdPrintRight (26, String (bad), 5);

  pct = (float) good / ((float) good + (float) bad) * 100.0;
  lcdPrintRight (12, String (pct), 3);
  Serial.print (pct);
  Serial.println ("%");
  digitalWrite (PIN_LED, LOW);

  delay (1000);
}