aboutsummaryrefslogtreecommitdiffstats
path: root/testhal/STM32F1xx/MAC/main.c
blob: 976636b1798c527b828d74710243779da5b933c5 (plain)
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
/*
    ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
                 2011,2012 Giovanni Di Sirio.

    This file is part of ChibiOS/RT.

    ChibiOS/RT is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    ChibiOS/RT is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "ch.h"
#include "hal.h"

#include "evtimer.h"

#define PERIODIC_TIMER_ID       1
#define FRAME_RECEIVED_ID       2

static const MACConfig mac_config = {NULL};

static uint8_t frame[STM32_MAC_BUFFERS_SIZE];

/*
 * Application entry point.
 */
int main(void) {
  EvTimer evt;
  EventListener el0, el1;

  /*
   * System initializations.
   * - HAL initialization, this also initializes the configured device drivers
   *   and performs the board-specific initializations.
   * - Kernel initialization, the main() function becomes a thread and the
   *   RTOS is active.
   */
  halInit();
  chSysInit();

  /*
   * Activates the MAC driver 1.
   */
  macStart(&ETHD1, &mac_config);

  /* Setup event sources.*/
  evtInit(&evt, S2ST(5));
  evtStart(&evt);
  chEvtRegisterMask(&evt.et_es, &el0, PERIODIC_TIMER_ID);
  chEvtRegisterMask(macGetReceiveEventSource(&ETHD1), &el1, FRAME_RECEIVED_ID);
  chEvtAddFlags(PERIODIC_TIMER_ID | FRAME_RECEIVED_ID);

  /*
   * Normal main() thread activity, the Ethernet status is polled every
   * 5 seconds, incoming frames are read.
   */
  while (TRUE) {
    eventmask_t mask = chEvtWaitAny(ALL_EVENTS);
    if (mask & PERIODIC_TIMER_ID)
      (void)macPollLinkStatus(&ETHD1);
    if (mask & FRAME_RECEIVED_ID) {
      MACReceiveDescriptor rd;

      if (macWaitReceiveDescriptor(&ETHD1, &rd, TIME_IMMEDIATE) == RDY_OK) {
        macReadReceiveDescriptor(&rd, frame, STM32_MAC_BUFFERS_SIZE);
        macReleaseReceiveDescriptor(&rd);
      }
    }
  }
}