2 * Copyright (c) 2013 - 2014, Freescale Semiconductor, Inc.
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
8 * o Redistributions of source code must retain the above copyright notice, this list
9 * of conditions and the following disclaimer.
11 * o Redistributions in binary form must reproduce the above copyright notice, this
12 * list of conditions and the following disclaimer in the documentation and/or
13 * other materials provided with the distribution.
15 * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from this
17 * software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef __FSL_ENET_RTCS_ADAPTOR_H__
32 #define __FSL_ENET_RTCS_ADAPTOR_H__
34 #include "fsl_enet_hal.h"
43 * @addtogroup enet_rtcs_adaptor
47 /*******************************************************************************
49 ******************************************************************************/
50 /*! @brief Definitions of the task parameter*/
52 extern unsigned long _RTCSTASK_priority
;
54 #define ENET_RECEIVE_TASK_PRIO (1)
55 #define ENET_TASK_STACK_SIZE (800)
56 #define ENET_PCB_NUM (16)
58 /*! @brief Definitions of the configuration parameter*/
59 #define ENET_RXBD_NUM (8)
60 #define ENET_TXBD_NUM (4)
61 #define ENET_EXTRXBD_NUM (4)
62 #define ENET_RXBuff_SIZE (kEnetMaxFrameSize)
63 #define ENET_TXBuff_SIZE (kEnetMaxFrameSize)
64 #define ENET_RXRTCSBUFF_NUM (8)
65 #define ENET_RX_BUFFER_ALIGNMENT (16)
66 #define ENET_TX_BUFFER_ALIGNMENT (16)
67 #define ENET_BD_ALIGNMENT (16)
68 #define ENET_RXBuffSizeAlign(n) ENET_ALIGN(n, ENET_RX_BUFFER_ALIGNMENT)
69 #define ENET_TXBuffSizeAlign(n) ENET_ALIGN(n, ENET_TX_BUFFER_ALIGNMENT)
70 #define ENET_MII_CLOCK (2500000L)
71 #if FSL_FEATURE_ENET_SUPPORT_PTP
72 #define ENET_PTP_TXTS_RING_LEN (25)
73 #define ENET_PTP_RXTS_RING_LEN (25)
76 /*! @brief Definitions of the error codes */
78 #define ENET_ERROR (0xff) /* General ENET error */
80 #define ENETERR_INVALID_DEVICE (kStatus_ENET_InvalidDevice) /* Device number out of range */
81 #define ENETERR_INIT_DEVICE (kStatus_ENET_Initialized) /* Device already initialized */
83 /*! @brief Definitions of the ENET protocol parameter*/
84 #define ENETPROT_IP 0x0800
85 #define ENETPROT_ARP 0x0806
86 #define ENETPROT_8021Q 0x8100
87 #define ENETPROT_IP6 0x86DD
88 #define ENETPROT_ETHERNET 0x88F7
89 #define ENET_OPT_8023 0x0001
90 #define ENET_OPT_8021QTAG 0x0002
91 #define ENET_SETOPT_8021QPRIO(p) (ENET_OPT_8021QTAG | (((uint_32)(p) & 0x7) << 2))
92 #define ENET_GETOPT_8021QPRIO(f) ((((unsigned int)f) >> 2) & 0x7)
94 /*! @brief Definitions of the ENET option macro*/
95 #define ENET_OPTION_HW_TX_IP_CHECKSUM 0x00001000
96 #define ENET_OPTION_HW_TX_PROTOCOL_CHECKSUM 0x00002000
97 #define ENET_OPTION_HW_RX_IP_CHECKSUM 0x00004000
98 #define ENET_OPTION_HW_RX_PROTOCOL_CHECKSUM 0x00008000
99 #define ENET_OPTION_HW_RX_MAC_ERR 0x00010000
101 /*! @brief Definitions of the ENET default Mac*/
102 #define ENET_DEFAULT_MAC_ADD { 0x00, 0x00, 0x5E, 0, 0, 0 }
103 #define PCB_MINIMUM_SIZE (sizeof(PCB2))
104 #define PCB_free(pcb_ptr) ((pcb_ptr)->FREE(pcb_ptr))
106 /*! @brief Definitions of the macro for byte-swap*/
107 #if SYSTEM_LITTLE_ENDIAN
108 #define RTCS_HTONS(n) BSWAP_16(n)
109 #define RTCS_HTONL(n) BSWAP_32(n)
110 #define RTCS_NTOHS(n) BSWAP_16(n)
111 #define RTCS_NTOHL(n) BSWAP_32(n)
113 #define RTCS_HTONS(n) (n)
114 #define RTCS_HTONL(n) (n)
115 #define RTCS_NTOHS(n) (n)
116 #define RTCS_NTOHL(n) (n)
120 #define htonl(p,x) (((uint_8_ptr)(p))[0] = ((x) >> 24) & 0xFF, \
121 ((uint_8_ptr)(p))[1] = ((x) >> 16) & 0xFF, \
122 ((uint_8_ptr)(p))[2] = ((x) >> 8) & 0xFF, \
123 ((uint_8_ptr)(p))[3] = (x) & 0xFF, \
126 #define htons(p,x) (((uint_8_ptr)(p))[0] = ((x) >> 8) & 0xFF, \
127 ((uint_8_ptr)(p))[1] = (x) & 0xFF, \
130 #define htonc(p,x) (((uint_8_ptr)(p))[0] = (x) & 0xFF, \
134 (((uint_32)(((uint_8_ptr)(p))[0])) << 24) | \
135 (((uint_32)(((uint_8_ptr)(p))[1])) << 16) | \
136 (((uint_32)(((uint_8_ptr)(p))[2])) << 8) | \
137 ( (uint_32)(((uint_8_ptr)(p))[3])) \
141 (((uint_16)(((uint_8_ptr)(p))[0])) << 8) | \
142 ( (uint_16)(((uint_8_ptr)(p))[1])) \
145 #define ntohc(p) ((uint_8)(((uint_8_ptr)(p))[0]))
147 #define htone(p,x) ((p)[0] = (x)[0], \
155 #define ntohe(p,x) ((x)[0] = (p)[0] & 0xFF, \
156 (x)[1] = (p)[1] & 0xFF, \
157 (x)[2] = (p)[2] & 0xFF, \
158 (x)[3] = (p)[3] & 0xFF, \
159 (x)[4] = (p)[4] & 0xFF, \
160 (x)[5] = (p)[5] & 0xFF \
163 /*! @brief Definitions of the add to queue*/
164 #define QUEUEADD(head,tail,pcb) \
165 if ((head) == NULL) { \
168 (tail)->PRIVATE = (pcb); \
171 (pcb)->PRIVATE = NULL
173 /*! @brief Definitions of the get from queue*/
174 #define QUEUEGET(head,tail,pcb) \
177 (head) = (head)->PRIVATE; \
178 if ((head) == NULL) { \
183 /*! @brief Definition for ENET six-byte Mac type*/
184 typedef unsigned char _enet_address
[6];
186 /*! @brief Definition of the IPCFG structure*/
187 typedef void * _enet_handle
;
191 typedef void (* PCB_FREE_FPTR
)(struct pcb
*);
194 /*! @brief Definition of the Ethernet packet header structure*/
195 typedef struct enet_header
197 _enet_address DEST
; /*!< destination Mac address*/
198 _enet_address SOURCE
; /*!< source Mac address*/
199 unsigned char TYPE
[2]; /*!< protocol type*/
200 } ENET_HEADER
, * ENET_HEADER_PTR
;
204 /*! @brief Definition of the fragment PCB structure*/
205 typedef struct pcb_fragment
207 uint32_t LENGTH
; /*!< Packet fragment length*/
208 unsigned char *FRAGMENT
; /*!< brief Pointer to fragment*/
209 } PCB_FRAGMENT
, * PCB_FRAGMENT_PTR
;
211 /*! @brief Definition of the PCB structure for the RTCS adaptor*/
214 PCB_FREE_FPTR FREE
; /*!< Function that frees PCB*/
215 void *PRIVATE
; /*!< Private PCB information*/
216 PCB_FRAGMENT FRAG
[1]; /*!< Pointer to PCB fragment*/
219 /*! @brief Definition of the two fragment PCB structure*/
222 PCB_FREE_FPTR FREE
; /*!< Function that frees PCB*/
223 void *PRIVATE
; /*!< Private PCB information*/
224 PCB_FRAGMENT FRAG
[2]; /*!< Pointers to two PCB fragments*/
229 /*! @brief Definition of the two fragment PCB structure*/
230 typedef struct pcb_queue
232 PCB
*pcbHead
; /*!< PCB buffer head*/
233 PCB
*pcbTail
; /*!< PCB buffer tail*/
236 /*! @brief Definition of the ECB structure, which contains the protocol type and it's related service function*/
237 typedef struct ENETEcbStruct
240 void (* SERVICE
)(PCB_PTR
, void *);
242 struct ENETEcbStruct
*NEXT
;
245 /*! @brief Definition of the 8022 header*/
246 typedef struct enet_8022_header
248 uint8_t dsap
[1]; /*!< DSAP region*/
249 uint8_t ssap
[1]; /*!< SSAP region*/
250 uint8_t command
[1]; /*!< Command region*/
251 uint8_t oui
[3]; /*!< OUI region*/
252 uint16_t type
; /*!< type region*/
253 }enet_8022_header_t
, *enet_8022_header_ptr
;
255 /*! @brief Definition of the common status structure*/
256 typedef struct enet_commom_stats_struct
{
257 uint32_t ST_RX_TOTAL
; /*!< Total number of received packets*/
258 uint32_t ST_RX_MISSED
; /*!< Number of missed packets*/
259 uint32_t ST_RX_DISCARDED
; /*!< Discarded a protocol that was not recognized*/
260 uint32_t ST_RX_ERRORS
; /*!< Discarded error during reception*/
261 uint32_t ST_TX_TOTAL
; /*!< Total number of transmitted packets*/
262 uint32_t ST_TX_MISSED
; /*!< Discarded transmit ring full*/
263 uint32_t ST_TX_DISCARDED
; /*!< Discarded bad packet*/
264 uint32_t ST_TX_ERRORS
; /*!< Error during transmission*/
265 } ENET_COMMON_STATS_STRUCT
, * ENET_COMMON_STATS_STRUCT_PTR
;
267 typedef struct enet_stats
{
268 ENET_COMMON_STATS_STRUCT COMMON
; /*!< Common status structure*/
269 uint32_t ST_RX_ALIGN
; /*!< Frame Alignment error*/
270 uint32_t ST_RX_FCS
; /*!< CRC error */
271 uint32_t ST_RX_RUNT
; /*!< Runt packet received */
272 uint32_t ST_RX_GIANT
; /*!< Giant packet received*/
273 uint32_t ST_RX_LATECOLL
; /*!< Late collision */
274 uint32_t ST_RX_OVERRUN
; /*!< DMA overrun*/
275 uint32_t ST_TX_SQE
; /*!< Heartbeat lost*/
276 uint32_t ST_TX_DEFERRED
; /*!< Transmission deferred*/
277 uint32_t ST_TX_LATECOLL
; /*!< Late collision*/
278 uint32_t ST_TX_EXCESSCOLL
; /*!< Excessive collisions*/
279 uint32_t ST_TX_CARRIER
; /*!< Carrier sense lost*/
280 uint32_t ST_TX_UNDERRUN
; /*!< DMA underrun*/
281 /* Following stats are collected by the Ethernet driver */
282 uint32_t ST_RX_COPY_SMALL
; /*!< Driver had to copy packet */
283 uint32_t ST_RX_COPY_LARGE
; /*!< Driver had to copy packet */
284 uint32_t ST_TX_COPY_SMALL
; /*!< Driver had to copy packet */
285 uint32_t ST_TX_COPY_LARGE
; /*!< Driver had to copy packet */
286 uint32_t RX_FRAGS_EXCEEDED
;
287 uint32_t RX_PCBS_EXHAUSTED
;
288 uint32_t RX_LARGE_BUFFERS_EXHAUSTED
;
290 uint32_t TX_ALL_ALIGNED
;
291 #if BSPCFG_ENABLE_ENET_HISTOGRAM
292 uint32_t RX_HISTOGRAM
[ENET_HISTOGRAM_ENTRIES
];
293 uint32_t TX_HISTOGRAM
[ENET_HISTOGRAM_ENTRIES
];
296 } ENET_STATS
, * ENET_STATS_PTR
;
298 /*******************************************************************************
300 ******************************************************************************/
302 #if defined(__cplusplus)
307 * @name ENET RTCS ADAPTOR
312 * @brief Initializes the ENET device.
314 * @param device The ENET device number.
315 * @param address The hardware address.
316 * @param flag The flag for upper layer.
317 * @param handle The address pointer for ENET device structure.
318 * @return The execution status.
320 uint32_t ENET_initialize(uint32_t device
, _enet_address address
,uint32_t flag
, _enet_handle
*handle
);
323 * @brief Opens the ENET device.
325 * @param handle The address pointer for ENET device structure.
326 * @param type The ENET protocol type.
327 * @param service The service function for type.
328 * @param private The private data for ENET device.
329 * @return The execution status.
331 uint32_t ENET_open(_enet_handle handle
, uint16_t type
, void (* service
)(PCB_PTR
, void *), void *private);
334 * @brief Shuts down the ENET device.
336 * @param handle The address pointer for ENET device structure.
337 * @return The execution status.
339 uint32_t ENET_shutdown(_enet_handle handle
);
340 #if !ENET_RECEIVE_ALL_INTERRUPT
342 * @brief ENET frame receive.
344 * @param enetIfPtr The address pointer for ENET device structure.
346 static void ENET_receive(task_param_t param
);
349 * @brief ENET frame transmit.
351 * @param handle The address pointer for ENET device structure.
352 * @param packet The ENET packet buffer.
353 * @param type The ENET protocol type.
354 * @param dest The destination hardware address.
355 * @param flag The flag for upper layer.
356 * @return The execution status.
358 uint32_t ENET_send(_enet_handle handle
, PCB_PTR packet
, uint32_t type
, _enet_address dest
, uint32_t flags
) ;
361 * @brief The ENET gets the address with the initialized device.
363 * @param handle The address pointer for ENET device structure.
364 * @param address The destination hardware address.
365 * @return The execution status.
367 uint32_t ENET_get_address(_enet_handle handle
, _enet_address address
);
370 * @brief The ENET gets the address with an uninitialized device.
372 * @param handle The address pointer for ENET device structure.
373 * @param value The value to change the last three bytes of hardware.
374 * @param address The destination hardware address.
375 * @return True if the execution status is success else false.
377 uint32_t ENET_get_mac_address(uint32_t device
, uint32_t value
, _enet_address address
);
379 * @brief The ENET joins a multicast group address.
381 * @param handle The address pointer for ENET device structure.
382 * @param type The ENET protocol type.
383 * @param address The destination hardware address.
384 * @return The execution status.
386 uint32_t ENET_join(_enet_handle handle
, uint16_t type
, _enet_address address
);
389 * @brief The ENET leaves a multicast group address.
391 * @param handle The address pointer for ENET device structure.
392 * @param type The ENET protocol type.
393 * @param address The destination hardware address.
394 * @return The execution status.
396 uint32_t ENET_leave(_enet_handle handle
, uint16_t type
, _enet_address address
);
397 #if BSPCFG_ENABLE_ENET_STATS
399 * @brief The ENET gets the packet statistic.
401 * @param handle The address pointer for ENET device structure.
402 * @return The statistic.
404 ENET_STATS_PTR
ENET_get_stats(_enet_handle handle
);
407 * @brief The ENET gets the link status.
409 * @param handle The address pointer for ENET device structure.
410 * @return The link status.
412 bool ENET_link_status(_enet_handle handle
);
415 * @brief The ENET gets the link speed.
417 * @param handle The address pointer for ENET device structure.
418 * @return The link speed.
420 uint32_t ENET_get_speed(_enet_handle handle
);
423 * @brief The ENET gets the MTU.
425 * @param handle The address pointer for ENET device structure.
426 * @return The link MTU
428 uint32_t ENET_get_MTU(_enet_handle handle
);
431 * @brief Gets the ENET PHY registers.
433 * @param handle The address pointer for ENET device structure.
434 * @param numRegs The number of registers.
435 * @param regPtr The buffer for data read from PHY registers.
436 * @return True if all numRegs registers are read succeed else false.
438 bool ENET_phy_registers(_enet_handle handle
, uint32_t numRegs
, uint32_t *regPtr
);
441 * @brief Gets ENET options.
443 * @param handle The address pointer for ENET device structure.
444 * @return ENET options.
446 uint32_t ENET_get_options(_enet_handle handle
);
449 * @brief Unregisters a protocol type on an Ethernet channel.
451 * @param handle The address pointer for ENET device structure.
452 * @return ENET options.
454 uint32_t ENET_close(_enet_handle handle
, uint16_t type
);
457 * @brief ENET mediactl.
459 * @param handle The address pointer for ENET device structure.
460 * @param The command ID.
461 * @param The buffer for input or output parameters.
462 * @return ENET options.
464 uint32_t ENET_mediactl(_enet_handle handle
, uint32_t commandId
, void *inOutParam
);
467 * @brief Gets the next ENET device handle address.
469 * @param handle The address pointer for ENET device structure.
470 * @return The address of next ENET device handle.
472 _enet_handle
ENET_get_next_device_handle(_enet_handle handle
);
477 * @param packet The buffer address.
479 void ENET_free(PCB_PTR packet
);
482 * @brief ENET error description.
484 * @param error The ENET error code.
485 * @return The error string.
487 const char * ENET_strerror(uint32_t error
);
493 #if defined(__cplusplus)
499 #endif /* MBED_NO_ENET */
501 #endif /* __FSL_ENET_RTCS_ADAPTOR_H__ */
502 /*******************************************************************************
504 ******************************************************************************/