Blame view

RIOT/tests/driver_at86rf2xx/main.c 2.76 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
  /*
   * Copyright (C) 2015 Freie Universitรคt Berlin
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @ingroup     tests
   * @{
   *
   * @file
   * @brief       Test application for AT86RF2xx network device driver
   *
   * @author      Hauke Petersen <hauke.petersen@fu-berlin.de>
   *
   * @}
   */
  
  #include <stdio.h>
  
  #include "net/netdev.h"
  #include "shell.h"
  #include "shell_commands.h"
  #include "thread.h"
  #include "xtimer.h"
  
  #include "common.h"
  
  #define _STACKSIZE      (THREAD_STACKSIZE_DEFAULT + THREAD_EXTRA_STACKSIZE_PRINTF)
  #define MSG_TYPE_ISR    (0x3456)
  
  static char stack[_STACKSIZE];
  static kernel_pid_t _recv_pid;
  
  at86rf2xx_t devs[AT86RF2XX_NUM];
  
  static const shell_command_t shell_commands[] = {
      { "ifconfig", "Configure netdev", ifconfig },
      { "txtsnd", "Send IEEE 802.15.4 packet", txtsnd },
      { NULL, NULL, NULL }
  };
  
  static void _event_cb(netdev_t *dev, netdev_event_t event)
  {
      if (event == NETDEV_EVENT_ISR) {
          msg_t msg;
  
          msg.type = MSG_TYPE_ISR;
          msg.content.ptr = dev;
  
          if (msg_send(&msg, _recv_pid) <= 0) {
              puts("gnrc_netdev: possibly lost interrupt.");
          }
      }
      else {
          switch (event) {
              case NETDEV_EVENT_RX_COMPLETE:
              {
                  recv(dev);
  
                  break;
              }
              default:
                  puts("Unexpected event received");
                  break;
          }
      }
  }
  
  void *_recv_thread(void *arg)
  {
      while (1) {
          msg_t msg;
          msg_receive(&msg);
          if (msg.type == MSG_TYPE_ISR) {
              netdev_t *dev = msg.content.ptr;
              dev->driver->isr(dev);
          }
          else {
              puts("unexpected message type");
          }
      }
  }
  
  int main(void)
  {
      puts("AT86RF2xx device driver test");
      xtimer_init();
  
      for (unsigned i = 0; i < AT86RF2XX_NUM; i++) {
          const at86rf2xx_params_t *p = &at86rf2xx_params[i];
          netdev_t *dev = (netdev_t *)(&devs[i]);
  
          printf("Initializing AT86RF2xx radio at SPI_%d\n", p->spi);
          at86rf2xx_setup(&devs[i], (at86rf2xx_params_t*) p);
          dev->event_callback = _event_cb;
          dev->driver->init(dev);
      }
  
      _recv_pid = thread_create(stack, sizeof(stack), THREAD_PRIORITY_MAIN - 1,
                                THREAD_CREATE_STACKTEST, _recv_thread, NULL,
                                "recv_thread");
  
      if (_recv_pid <= KERNEL_PID_UNDEF) {
          puts("Creation of receiver thread failed");
          return 1;
      }
  
      /* start the shell */
      puts("Initialization successful - starting the shell now");
  
      char line_buf[SHELL_DEFAULT_BUFSIZE];
      shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE);
  
      return 0;
  }