rfLPC
A low level library for using NXP's LPC17xx SoC. Config is given for MBED prototyping board
ethernet.h
Go to the documentation of this file.
1 /* This file is part of rflpc. Copyright 2010-2011 Michael Hauspie
2  *
3  * rflpc is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  * (at your option) any later version.
7  *
8  * rflpc is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with rflpc. If not, see <http://www.gnu.org/licenses/>.
15  */
19 #ifndef __RFLPC_ETHERNET_H__
20 #define __RFLPC_ETHERNET_H__
21 
22 
23 
24 #ifdef RFLPC_CONFIG_ENABLE_ETHERNET
25 
30 /*
31  Author: Michael Hauspie <Michael.Hauspie@univ-lille1.fr>
32  Created: Jun. 28 2011
33  Time-stamp: <2014-02-14 17:09:46 (hauspie)>
34 */
35 #include <stdint.h>
36 #include "../nxp/LPC17xx.h"
37 #include "../interrupt.h"
38 #include "eth_const.h"
40 extern int rflpc_eth_init();
41 
45 extern int rflpc_eth_link_state();
46 
47 
55 #define RFLPC_ETH_LINK_MODE_SPEED_BIT (1 << 0)
56 
57 #define RFLPC_ETH_LINK_MODE_DUPLEX_BIT (1 << 1)
58 
59 #define RFLPC_ETH_LINK_MODE_100HD (RFLPC_ETH_LINK_MODE_SPEED_BIT)
60 
61 #define RFLPC_ETH_LINK_MODE_10HD (0)
62 
63 #define RFLPC_ETH_LINK_MODE_100FD (RFLPC_ETH_LINK_MODE_SPEED_BIT | RFLPC_ETH_LINK_MODE_DUPLEX_BIT)
64 
65 #define RFLPC_ETH_LINK_MODE_10FD (RFLPC_ETH_LINK_MODE_DUPLEX_BIT)
66 
77 extern void rflpc_eth_set_link_mode(int mode);
78 
90 extern int rflpc_eth_link_auto_negociate(int max_desired_mode);
91 
101 extern int rflpc_eth_get_link_mode();
102 
103 
107 typedef struct
108 {
109  uint8_t *packet;
126  volatile uint32_t control;
128 
131 typedef struct
132 {
150  volatile uint32_t status_info;
155  volatile uint32_t status_hash_crc;
157 
158 
161 typedef struct
162 {
173  volatile uint32_t status_info;
175 
176 
183 static inline uint32_t rflpc_eth_get_packet_size(uint32_t status_info)
184 {
185  return (status_info & 0x7FF) + 1;
186 }
187 
196 static inline void rflpc_eth_set_tx_control_word(uint32_t size_to_send, volatile uint32_t *control, int trigger_it, int last_fragment)
197 {
198  *control = ((size_to_send-1) & 0x7FF) | ((last_fragment & 1) << 30) | ((trigger_it & 1) << 31) |
199  (1 << 26) | /* Override MAC defaults for pad, crc and last */
200  (1 << 28) | /* Pads small frames to 64 bytes */
201  (1 << 29); /* Appends a CRC to the frame */
202 }
203 
209 extern void rflpc_eth_set_rx_base_addresses(rflpc_eth_descriptor_t *descriptors, rflpc_eth_rx_status_t *status, int count);
210 
218 {
219  if (LPC_EMAC->RxConsumeIndex == LPC_EMAC->RxProduceIndex) /* empty queue */
220  return 0;
221  *descriptor = ((rflpc_eth_descriptor_t*)LPC_EMAC->RxDescriptor) + LPC_EMAC->RxConsumeIndex;
222  *status = ((rflpc_eth_rx_status_t*)LPC_EMAC->RxStatus) + LPC_EMAC->RxConsumeIndex;
223  return 1;
224 }
225 
232 static inline int rflpc_eth_rx_available()
233 {
234  if (LPC_EMAC->RxConsumeIndex == LPC_EMAC->RxProduceIndex) /* empty queue */
235  return 0;
236  return 1;
237 }
238 
239 
246 {
247  if (LPC_EMAC->RxConsumeIndex == LPC_EMAC->RxProduceIndex) /* Queue is empty */
248  return;
249  if (LPC_EMAC->RxConsumeIndex == LPC_EMAC->RxDescriptorNumber) /* Wrap around */
250  LPC_EMAC->RxConsumeIndex = 0;
251  else
252  LPC_EMAC->RxConsumeIndex++;
253 }
254 
255 
260 extern void rflpc_eth_set_tx_base_addresses(rflpc_eth_descriptor_t *descriptos, rflpc_eth_tx_status_t *status, int count);
261 
263 #define TX_PRODUCE_INDEX_INC(inc) ((LPC_EMAC->TxProduceIndex + (inc))% (LPC_EMAC->TxDescriptorNumber+1))
264 
280 {
281  /* queue full */
282  if (TX_PRODUCE_INDEX_INC(idx) == LPC_EMAC->TxConsumeIndex - 1 ||
283  ((TX_PRODUCE_INDEX_INC(idx) == LPC_EMAC->TxDescriptorNumber) && LPC_EMAC->TxConsumeIndex == 0))
284  {
285  return 0;
286  }
287  *descriptor = ((rflpc_eth_descriptor_t*)LPC_EMAC->TxDescriptor) + TX_PRODUCE_INDEX_INC(idx);
288  *status = ((rflpc_eth_tx_status_t*)LPC_EMAC->TxStatus) + TX_PRODUCE_INDEX_INC(idx);
289  return 1;
290 }
291 
298 {
299  int idx = LPC_EMAC->TxConsumeIndex - 1;
300  if (idx < 0)
301  return LPC_EMAC->TxDescriptorNumber;
302  return idx;
303 }
304 
309 static inline void rflpc_eth_done_process_tx_packet(int count)
310 {
311  /* queue full */
312  if (TX_PRODUCE_INDEX_INC(count-1) == LPC_EMAC->TxConsumeIndex - 1 ||
313  ((TX_PRODUCE_INDEX_INC(count-1) == LPC_EMAC->TxDescriptorNumber) && LPC_EMAC->TxConsumeIndex == 0))
314  {
315  return;
316  }
317  LPC_EMAC->TxProduceIndex = TX_PRODUCE_INDEX_INC(count);
318 }
319 
322 extern void rflpc_eth_get_mac_address(uint8_t *addr);
323 
326 extern void rflpc_eth_set_mac_address(const uint8_t *addr);
327 
330 {
331  rflpc_irq_disable(ENET_IRQn);
332  rflpc_irq_set_handler(ENET_IRQn, c);
333  rflpc_irq_enable(ENET_IRQn);
334 }
335 
342 static inline void rflpc_eth_irq_enable(uint32_t irqs)
343 {
344  LPC_EMAC->IntEnable |= irqs;
345 }
346 
352 static inline void rflpc_eth_irq_disable(uint32_t irqs)
353 {
354  LPC_EMAC->IntEnable &= ~irqs;
355 }
356 
362 static inline void rflpc_eth_irq_enable_set(uint32_t irqs)
363 {
364  LPC_EMAC->IntEnable = irqs;
365 }
366 
368 static inline void rflpc_eth_irq_clear(uint32_t irqs)
369 {
370  LPC_EMAC->IntClear = irqs;
371 }
372 
376 static inline uint32_t rflpc_eth_irq_get_status()
377 {
378  return LPC_EMAC->IntStatus;
379 }
380 
388 static inline void rflpc_eth_irq_trigger(uint32_t irqs)
389 {
390  LPC_EMAC->IntSet = irqs;
391 }
392 
403 static inline void rflpc_eth_activate_rx_filter(int accept_unicast, int accept_multicast, int accept_broadcast)
404 {
405  LPC_EMAC->RxFilterCtrl = 0;
406  if (accept_unicast)
407  LPC_EMAC->RxFilterCtrl |= RFLPC_ETH_RXFILTER_UNICAST_EN;
408  if (accept_multicast)
409  LPC_EMAC->RxFilterCtrl |= RFLPC_ETH_RXFILTER_MULTICAST_EN;
410  if (accept_broadcast)
411  LPC_EMAC->RxFilterCtrl |= RFLPC_ETH_RXFILTER_BROADCAST_EN;
412  LPC_EMAC->RxFilterCtrl |= RFLPC_ETH_RXFILTER_PERFECT_EN;
413  /* activate filter */
414  LPC_EMAC->Command &= ~RFLPC_ETH_CMD_PASS_RX_FILTER;
415 }
420 static inline void rflpc_eth_deactivate_rx_filter()
421 {
422  LPC_EMAC->Command |= RFLPC_ETH_CMD_PASS_RX_FILTER;
423 }
424 
430 extern void rflpc_eth_dump_internals();
431 
432 
435 #endif /* ENABLE_ETHERNET */
436 
437 #endif
static void rflpc_eth_irq_enable_set(uint32_t irqs)
sets the irq enable register This function will disable all irqs and then only enable those given...
Definition: ethernet.h:362
static uint32_t rflpc_eth_get_packet_size(uint32_t status_info)
Returns the size of a packet from the status_info field of a rflpc_eth_tx_status_t or rflpc_eth_rx_st...
Definition: ethernet.h:183
static void rflpc_eth_activate_rx_filter(int accept_unicast, int accept_multicast, int accept_broadcast)
Activate the hardware receive filter.
Definition: ethernet.h:403
This structure holds a descriptor which describes the fragment received or sent by the ethernet DMA...
Definition: ethernet.h:107
static uint32_t rflpc_eth_irq_get_status()
gets the interrupt status.
Definition: ethernet.h:376
This structure holds the transmit status associated to a descriptor.
Definition: ethernet.h:161
Ethernet driver constants.
int rflpc_eth_init()
Inits the ethernet device.
static void rflpc_eth_irq_disable(uint32_t irqs)
disable eth interrupts This function will remove irq enable bits.
Definition: ethernet.h:352
static void rflpc_eth_irq_trigger(uint32_t irqs)
Force the generation of the given interrupt.
Definition: ethernet.h:388
static void rflpc_eth_irq_clear(uint32_t irqs)
clear given pending interrupts
Definition: ethernet.h:368
void rflpc_eth_get_mac_address(uint8_t *addr)
returns the device MAC address
static int rflpc_eth_get_current_tx_packet_descriptor(rflpc_eth_descriptor_t **descriptor, rflpc_eth_tx_status_t **status, int idx)
returns the index of the current tx packet descriptor.
Definition: ethernet.h:279
static void rflpc_eth_done_process_tx_packet(int count)
When the packet has been generated, calling this function will make it owned by the hardware and queu...
Definition: ethernet.h:309
static int rflpc_eth_get_last_sent_packet_idx()
Returns the index of the last send buffer.
Definition: ethernet.h:297
void rflpc_irq_set_handler(IRQn_Type irq, rflpc_irq_handler_t handler)
Sets an handler for the given IRQ.
static void rflpc_eth_irq_enable(uint32_t irqs)
enable eth interrupts
Definition: ethernet.h:342
int rflpc_eth_link_auto_negociate(int max_desired_mode)
Force the physical link to perform auto negociation of mode.
void rflpc_eth_set_rx_base_addresses(rflpc_eth_descriptor_t *descriptors, rflpc_eth_rx_status_t *status, int count)
Sets rx descriptors and status base address.
uint8_t * packet
pointer to buffer where the ethernet frame (or frame fragment) is stored
Definition: ethernet.h:109
static void rflpc_irq_enable(IRQn_Type irq)
Enables a specific interrupt.
Definition: interrupt.h:50
volatile uint32_t status_info
Transmission status info.
Definition: ethernet.h:173
static void rflpc_eth_set_tx_control_word(uint32_t size_to_send, volatile uint32_t *control, int trigger_it, int last_fragment)
Sets the transmission control word of a rflpc_eth_descriptor_t struct.
Definition: ethernet.h:196
void rflpc_eth_dump_internals()
Use the printf function to dump the values of the MAC registers.
static void rflpc_irq_disable(IRQn_Type irq)
Disables a specific interrupt.
Definition: interrupt.h:56
int rflpc_eth_link_state()
Returns the link state.
volatile uint32_t status_info
Receive status return flags.
Definition: ethernet.h:150
static int rflpc_eth_get_current_rx_packet_descriptor(rflpc_eth_descriptor_t **descriptor, rflpc_eth_rx_status_t **status)
Returns the pointers on the current rx packet descriptor.
Definition: ethernet.h:217
static void rflpc_eth_set_irq_handler(rflpc_irq_handler_t c)
sets the interrupt handler of the ethernet peripheral
Definition: ethernet.h:329
static void rflpc_eth_deactivate_rx_filter()
Deactivates the hardware receive filter.
Definition: ethernet.h:420
This structure holds the reception status associated to a descriptor.
Definition: ethernet.h:131
volatile uint32_t status_hash_crc
hash CRC calculated from source and destination address.
Definition: ethernet.h:155
static void rflpc_eth_done_process_rx_packet()
This function has to be called when a packet (which descriptor is returned by rflpc_eth_get_current_r...
Definition: ethernet.h:245
volatile uint32_t control
Control word where we find data buffer size and other information relative to reception/emission of t...
Definition: ethernet.h:126
#define TX_PRODUCE_INDEX_INC(inc)
Helper macro for rflpc_eth_get_current_tx_packet_descriptor.
Definition: ethernet.h:263
void rflpc_eth_set_tx_base_addresses(rflpc_eth_descriptor_t *descriptos, rflpc_eth_tx_status_t *status, int count)
Sets tx descriptors and status base address.
static int rflpc_eth_rx_available()
Returns true if a packet has been received and not yet processed available.
Definition: ethernet.h:232
void rflpc_eth_set_mac_address(const uint8_t *addr)
sets the device MAC address
int rflpc_eth_get_link_mode()
returns the current link mode.
void(* rflpc_irq_handler_t)(void)
Interrupt handler type.
Definition: interrupt.h:62
void rflpc_eth_set_link_mode(int mode)
Forces the MAC and PHY devices to operate on the given mode no matter the capability of the linked pa...