From 89f3b3c59309c1e708d35197f0c948ee0fd620ef Mon Sep 17 00:00:00 2001 From: Hans-Erik Floryd Date: Thu, 10 Oct 2019 10:47:08 +0200 Subject: [PATCH] Update rt-kernel port Add a method for choosing L2 driver and add support for sabresd board. Change-Id: Ib5bbcb53990ffdbe4345cf99f69c44192250e0d8 --- README.md | 11 +++++ cmake/rt-kernel.cmake | 1 - osal/rtk/osal.c | 50 ++++++-------------- oshw/rtk/fec/fec_ecat.c | 98 +++++++++++++++++++++++---------------- oshw/rtk/fec/fec_ecat.h | 38 +++++---------- oshw/rtk/lw_mac/lw_emac.c | 44 ++++++++---------- oshw/rtk/lw_mac/lw_emac.h | 12 ----- oshw/rtk/nicdrv.c | 11 ++--- oshw/rtk/nicdrv.h | 2 + oshw/rtk/oshw.h | 3 ++ 10 files changed, 124 insertions(+), 146 deletions(-) delete mode 100644 oshw/rtk/lw_mac/lw_emac.h diff --git a/README.md b/README.md index e9e5cd6..4f29ead 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,17 @@ Linux & macOS * `cmake ..` * `make` +rt-kernel +--------- + + * `mkdir build` + * `cd build` + * `export COMPILERS=/opt/rt-tools/compilers` + * `export RTK=/path/to/rt-kernel` + * `export BSP=` + * `cmake .. -DCMAKE_TOOLCHAIN_FILE=../cmake/toolchain/rt-kernel-.cmake -G "Unix Makefiles"` + * `make` + ERIKA Enterprise RTOS --------------------- diff --git a/cmake/rt-kernel.cmake b/cmake/rt-kernel.cmake index 14d413e..51067f9 100644 --- a/cmake/rt-kernel.cmake +++ b/cmake/rt-kernel.cmake @@ -9,7 +9,6 @@ target_sources(soem PRIVATE oshw/rtk/nicdrv.c oshw/rtk/nicdrv.h oshw/rtk/lw_mac/lw_emac.c - oshw/rtk/lw_mac/lw_emac.h oshw/rtk/fec/fec_ecat.c oshw/rtk/fec/fec_ecat.h ) diff --git a/osal/rtk/osal.c b/osal/rtk/osal.c index a519bb7..73bce16 100644 --- a/osal/rtk/osal.c +++ b/osal/rtk/osal.c @@ -9,44 +9,9 @@ #include #include -#define timercmp(a, b, CMP) \ - (((a)->tv_sec == (b)->tv_sec) ? \ - ((a)->tv_usec CMP (b)->tv_usec) : \ - ((a)->tv_sec CMP (b)->tv_sec)) -#define timeradd(a, b, result) \ - do { \ - (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \ - (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \ - if ((result)->tv_usec >= 1000000) \ - { \ - ++(result)->tv_sec; \ - (result)->tv_usec -= 1000000; \ - } \ - } while (0) -#define timersub(a, b, result) \ - do { \ - (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \ - (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \ - if ((result)->tv_usec < 0) { \ - --(result)->tv_sec; \ - (result)->tv_usec += 1000000; \ - } \ - } while (0) - #define USECS_PER_SEC 1000000 #define USECS_PER_TICK (USECS_PER_SEC / CFG_TICKS_PER_SECOND) - -/* Workaround for rt-labs defect 776. - * Default implementation of udelay() didn't work correctly when tick was - * shorter than one millisecond. - */ -void udelay (uint32_t us) -{ - tick_t ticks = (us / USECS_PER_TICK) + 1; - task_delay (ticks); -} - int gettimeofday(struct timeval *tp, void *tzp) { tick_t tick = tick_get(); @@ -65,7 +30,8 @@ int gettimeofday(struct timeval *tp, void *tzp) int osal_usleep (uint32 usec) { - udelay(usec); + tick_t ticks = (usec / USECS_PER_TICK) + 1; + task_delay (ticks); return 0; } @@ -85,6 +51,18 @@ ec_timet osal_current_time (void) return return_value; } +void osal_time_diff(ec_timet *start, ec_timet *end, ec_timet *diff) +{ + if (end->usec < start->usec) { + diff->sec = end->sec - start->sec - 1; + diff->usec = end->usec + 1000000 - start->usec; + } + else { + diff->sec = end->sec - start->sec; + diff->usec = end->usec - start->usec; + } +} + void osal_timer_start (osal_timert * self, uint32 timeout_usec) { struct timeval start_time; diff --git a/oshw/rtk/fec/fec_ecat.c b/oshw/rtk/fec/fec_ecat.c index de4b5ac..2ec5f36 100644 --- a/oshw/rtk/fec/fec_ecat.c +++ b/oshw/rtk/fec/fec_ecat.c @@ -1,24 +1,19 @@ -/****************************************************************************** -* * *** *** -* *** *** *** -* *** **** ********** *** ***** *** **** ***** -* ********* ********** *** ********* ************ ********* -* **** *** *** *** *** **** *** -* *** *** ****** *** *********** *** **** ***** -* *** *** ****** *** ************* *** **** ***** -* *** **** **** *** *** *** **** *** -* *** ******* ***** ************** ************* ********* -* *** ***** *** ******* ** ** ****** ***** -* t h e r e a l t i m e t a r g e t e x p e r t s -* -* http://www.rt-labs.com -* Copyright (C) 2007. rt-labs AB, Sweden. All rights reserved. -*------------------------------------------------------------------------------ -* $Id: fec_ecat.c 138 2014-04-11 09:11:48Z rtlfrm $ -*------------------------------------------------------------------------------ -*/ +/********************************************************************* + * _ _ _ + * _ __ | |_ _ | | __ _ | |__ ___ + * | '__|| __|(_)| | / _` || '_ \ / __| + * | | | |_ _ | || (_| || |_) |\__ \ + * |_| \__|(_)|_| \__,_||_.__/ |___/ + * + * http://www.rt-labs.com + * Copyright 2011 rt-labs AB, Sweden. + * See LICENSE file in the project root for full license information. + ********************************************************************/ + +#if defined (sabresd) || defined (twrk60f) #include "fec_ecat.h" +#include "oshw.h" #include #include @@ -65,6 +60,20 @@ /* Note that the core clock and the system clock has the same clock frequency */ #define FEC_MODULE_CLOCK_Hz CFG_IPG_CLOCK + +#if defined (sabresd) +extern phy_t * ar8031_init (const phy_cfg_t * cfg); + +#define PHY_INIT ar8031_init +#define PHY_INTERFACE FEC_PHY_RGMII +#endif + +#if defined (twrk60f) +#define PHY_INIT mii_init +#define PHY_INTERFACE FEC_PHY_RMII +#endif + + //----------------------------------------------------------------------------// typedef struct fec_mac_t @@ -223,6 +232,7 @@ COMPILETIME_ASSERT (offsetof (reg_fec_t, smac[3]) == 0x518); /* Bit definitions and macros for FEC_ECR */ #define FEC_ECR_DBSWP (0x00000100) +#define FEC_ECR_SPEED (0x00000020) #define FEC_ECR_ETHER_EN (0x00000002) #define FEC_ECR_RESET (0x00000001) @@ -258,7 +268,7 @@ COMPILETIME_ASSERT (offsetof (reg_fec_t, smac[3]) == 0x518); #define FEC_RCR_RMII_10T (0x00000200) #define FEC_RCR_RMII_MODE (0x00000100) #define FEC_RCR_SGMII_ENA (0x00000080) -#define FEC_RCR_RGMII_ENA (0x00000040) +#define FEC_RCR_RGMII_EN (0x00000040) #define FEC_RCR_FCE (0x00000020) #define FEC_RCR_BC_REJ (0x00000010) #define FEC_RCR_PROM (0x00000008) @@ -514,17 +524,19 @@ static void fec_ecat_init_hw (const fec_mac_address_t * mac_address) fec->base->mscr = (fec->base->mscr & (~0x7E)) | (mii_speed << 1); // Receive control register - fec->base->rcr = FEC_RCR_MAX_FL(PKT_MAXBUF_SIZE) | FEC_RCR_MII_MODE; + fec->base->rcr = FEC_RCR_MAX_FL(PKT_MAXBUF_SIZE); - // set RMII mode in RCR register. - if (fec->phy_interface == FEC_PHY_RMII) + switch(fec->phy_interface) { - fec->base->rcr |= FEC_RCR_RMII_MODE; - } - else if(fec->phy_interface == FEC_PHY_RGMII) - { - fec->base->rcr &= ~(FEC_RCR_RMII_MODE | FEC_RCR_MII_MODE ); - fec->base->rcr |= (FEC_RCR_RGMII_ENA | FEC_RCR_MII_MODE); + case FEC_PHY_MII: + fec->base->rcr |= FEC_RCR_MII_MODE; + break; + case FEC_PHY_RMII: + fec->base->rcr |= (FEC_RCR_MII_MODE | FEC_RCR_RMII_MODE); + break; + case FEC_PHY_RGMII: + fec->base->rcr |= (FEC_RCR_MII_MODE | FEC_RCR_RGMII_EN); + break; } // Reset phy @@ -556,7 +568,7 @@ static void fec_ecat_init_hw (const fec_mac_address_t * mac_address) fec->phy->ops->start (fec->phy); } -int fec_ecat_send (const void *payload, size_t tot_len) +int oshw_mac_send (const void *payload, size_t tot_len) { fec_buffer_bd_t * bd; @@ -587,6 +599,7 @@ int fec_ecat_send (const void *payload, size_t tot_len) fec_buffer_produce_tx (bd); +#if defined (twrk60f) /* Wait for previous transmissions to complete. * * This is a workaround for Freescale Kinetis errata e6358. @@ -596,6 +609,7 @@ int fec_ecat_send (const void *payload, size_t tot_len) { ; } +#endif /* Transmit frame */ fec->base->tdar = 1; @@ -603,7 +617,7 @@ int fec_ecat_send (const void *payload, size_t tot_len) return tot_len; } -int fec_ecat_recv (void * buffer, size_t buffer_length) +int oshw_mac_recv (void * buffer, size_t buffer_length) { fec_buffer_bd_t * bd; int return_value; @@ -686,7 +700,7 @@ static void fec_ecat_hotplug (void) fec->base->rcr |= FEC_RCR_DRT; } - /* Set RMII 10-Mbps mode. */ + /* Set link speed */ if (link_state & PHY_LINK_10MBIT) { fec->base->rcr |= FEC_RCR_RMII_10T; @@ -750,8 +764,7 @@ static dev_state_t fec_ecat_probe (void) COMPILETIME_ASSERT (FEC_MODULE_CLOCK_Hz >= FEC_MIN_MODULE_CLOCK_Hz); -int fec_ecat_init (const fec_mac_address_t * mac_address, - bool phy_loopback_mode) +int oshw_mac_init (const uint8_t * mac_address) { dev_state_t state; static const fec_cfg_t eth_cfg = @@ -760,17 +773,16 @@ int fec_ecat_init (const fec_mac_address_t * mac_address, .clock = FEC_MODULE_CLOCK_Hz, .tx_bd_base = fec_tx_bd, .rx_bd_base = fec_rx_bd, - .phy_interface = FEC_PHY_RMII, + .phy_interface = PHY_INTERFACE, }; static phy_cfg_t phy_cfg = { .address = ETH_PHY_ADDRESS, .read = NULL, /* Set by MAC driver */ .write = NULL, /* Set by MAC driver */ + .loopback_mode = false, }; - phy_cfg.loopback_mode = phy_loopback_mode; - /* Initialise buffers and buffer descriptors.*/ fec_buffer_init_tx (fec_tx_bd, fec_tx_data, NUM_BUFFERS); fec_buffer_init_rx (fec_rx_bd, fec_rx_data, NUM_BUFFERS); @@ -778,19 +790,25 @@ int fec_ecat_init (const fec_mac_address_t * mac_address, fec = malloc (sizeof (fec_t)); UASSERT (fec != NULL, EMEM); +#if defined (sabresd) + gpio_set (GPIO_RGMII_nRST, 0); + task_delay (500); + gpio_set (GPIO_RGMII_nRST, 1); +#endif + /* Initialise driver state */ fec->rx_bd_base = eth_cfg.rx_bd_base; fec->tx_bd_base = eth_cfg.tx_bd_base; fec->clock = eth_cfg.clock; fec->base = (reg_fec_t *)eth_cfg.base; fec->phy_interface = eth_cfg.phy_interface; - fec->phy = mii_init (&phy_cfg); + fec->phy = PHY_INIT (&phy_cfg); fec->phy->arg = fec; fec->phy->read = fec_ecat_read_phy; fec->phy->write = fec_ecat_write_phy; /* Initialize hardware */ - fec_ecat_init_hw (mac_address); + fec_ecat_init_hw ((fec_mac_address_t *)mac_address); state = StateDetached; while (state == StateDetached) { @@ -800,3 +818,5 @@ int fec_ecat_init (const fec_mac_address_t * mac_address, return 0; } + +#endif diff --git a/oshw/rtk/fec/fec_ecat.h b/oshw/rtk/fec/fec_ecat.h index aded90d..d297539 100644 --- a/oshw/rtk/fec/fec_ecat.h +++ b/oshw/rtk/fec/fec_ecat.h @@ -1,25 +1,17 @@ -/****************************************************************************** -* * *** *** -* *** *** *** -* *** **** ********** *** ***** *** **** ***** -* ********* ********** *** ********* ************ ********* -* **** *** *** *** *** **** *** -* *** *** ****** *** *********** *** **** ***** -* *** *** ****** *** ************* *** **** ***** -* *** **** **** *** *** *** **** *** -* *** ******* ***** ************** ************* ********* -* *** ***** *** ******* ** ** ****** ***** -* t h e r e a l t i m e t a r g e t e x p e r t s -* -* http://www.rt-labs.com -* Copyright (C) 2007. rt-labs AB, Sweden. All rights reserved. -*------------------------------------------------------------------------------ -* $Id: fec_ecat.h 91 2014-04-02 13:32:29Z rtlfrm $ -*------------------------------------------------------------------------------ -*/ +/********************************************************************* + * _ _ _ + * _ __ | |_ _ | | __ _ | |__ ___ + * | '__|| __|(_)| | / _` || '_ \ / __| + * | | | |_ _ | || (_| || |_) |\__ \ + * |_| \__|(_)|_| \__,_||_.__/ |___/ + * + * http://www.rt-labs.com + * Copyright 2011 rt-labs AB, Sweden. + * See LICENSE file in the project root for full license information. + ********************************************************************/ /** - * \defgroup fec EtherCat Ethernet MAC driver for Frescale K60 SoCs. + * \defgroup fec EtherCat Ethernet MAC driver for Frescale SoCs. * * \{ */ @@ -39,12 +31,6 @@ typedef struct fec_mac_address uint8_t octet[6]; } fec_mac_address_t; -int fec_ecat_init (const fec_mac_address_t * mac_address, bool phy_loopback_mode); - -int fec_ecat_send (const void *payload, size_t tot_len); - -int fec_ecat_recv (void * buffer, size_t buffer_length); - #ifdef __cplusplus } #endif diff --git a/oshw/rtk/lw_mac/lw_emac.c b/oshw/rtk/lw_mac/lw_emac.c index 46d3928..cee44fc 100644 --- a/oshw/rtk/lw_mac/lw_emac.c +++ b/oshw/rtk/lw_mac/lw_emac.c @@ -1,24 +1,16 @@ -/****************************************************************************** - * * *** *** - * *** *** *** - * *** **** ********** *** ***** *** **** ***** - * ********* ********** *** ********* ************ ********* - * **** *** *** *** *** **** *** - * *** *** ****** *** *********** *** **** ***** - * *** *** ****** *** ************* *** **** ***** - * *** **** **** *** *** *** **** *** - * *** ******* ***** ************** ************* ********* - * *** ***** *** ******* ** ** ****** ***** - * t h e r e a l t i m e t a r g e t e x p e r t s +/********************************************************************* + * _ _ _ + * _ __ | |_ _ | | __ _ | |__ ___ + * | '__|| __|(_)| | / _` || '_ \ / __| + * | | | |_ _ | || (_| || |_) |\__ \ + * |_| \__|(_)|_| \__,_||_.__/ |___/ * * http://www.rt-labs.com - * Copyright (C) 2006. rt-labs AB, Sweden. All rights reserved. - *------------------------------------------------------------------------------ - * $Id: lw_emac.c 348 2012-10-18 16:41:14Z rtlfrm $ - * - * - *------------------------------------------------------------------------------ - */ + * Copyright 2010 rt-labs AB, Sweden. + * See LICENSE file in the project root for full license information. + ********************************************************************/ + +#if defined (stamp537) #include #include @@ -26,7 +18,7 @@ #include #include -#include "lw_emac.h" +#include "oshw.h" /* MII standard registers. See IEEE Std 802.3-2005 clause 22: @@ -187,7 +179,7 @@ static uint32_t lw_emac_read_phy_reg(uint8_t phy_addr, uint8_t reg_addr) { } /* Internal function that sets the MAC address */ -static void lw_emac_set_mac_addr(uint8_t * ethAddr) +static void lw_emac_set_mac_addr(const uint8_t * ethAddr) { pEth->addrlo = ethAddr[0] | @@ -199,7 +191,7 @@ static void lw_emac_set_mac_addr(uint8_t * ethAddr) ethAddr[5] << 8; } -static uint8_t lw_emac_init_registers(uint8_t * ethAddr) { +static uint8_t lw_emac_init_registers(const uint8_t * ethAddr) { uint32_t clock_divisor, sysctl_mdcdiv, phy_stadat, counter; /* CONFIGURE MAC MII PINS */ @@ -285,7 +277,7 @@ static uint8_t lw_emac_init_registers(uint8_t * ethAddr) { return 0; } -int bfin_EMAC_init (uint8_t *ethAddr) +int oshw_mac_init (const uint8_t * ethAddr) { rxIdx = txIdx = 0; @@ -351,7 +343,8 @@ int bfin_EMAC_init (uint8_t *ethAddr) return 0; } -int bfin_EMAC_send (void *packet, int length) + +int oshw_mac_send (const void *packet, size_t length) { UASSERT(length > 0, EARG); UASSERT(length < ETH_FRAME_SIZE, EARG); @@ -381,7 +374,7 @@ int bfin_EMAC_send (void *packet, int length) return 0; } -int bfin_EMAC_recv (uint8_t * packet, size_t size) +int oshw_mac_recv (void * packet, size_t size) { uint32_t length; uint32_t status = rxStatusWord[rxIdx]; @@ -421,3 +414,4 @@ int bfin_EMAC_recv (uint8_t * packet, size_t size) return length; } +#endif diff --git a/oshw/rtk/lw_mac/lw_emac.h b/oshw/rtk/lw_mac/lw_emac.h deleted file mode 100644 index 24f98df..0000000 --- a/oshw/rtk/lw_mac/lw_emac.h +++ /dev/null @@ -1,12 +0,0 @@ -/* - * author: Tomas Vestelind - */ - -#ifndef LWIP_MAC_H -#define LWIP_MAC_H - -int bfin_EMAC_init(uint8_t *enetaddr); -int bfin_EMAC_send(void *packet, int length); -int bfin_EMAC_recv(uint8_t * packet, size_t size); - -#endif /* LWIP_MAC_H */ diff --git a/oshw/rtk/nicdrv.c b/oshw/rtk/nicdrv.c index 537127d..15625f2 100644 --- a/oshw/rtk/nicdrv.c +++ b/oshw/rtk/nicdrv.c @@ -38,9 +38,6 @@ #include "osal.h" #include "oshw.h" - -#include "lw_mac/lw_emac.h" - #ifndef MAX #define MAX(a,b) (((a) > (b)) ? (a) : (b)) #define MIN(a,b) (((a) < (b)) ? (a) : (b)) @@ -95,7 +92,7 @@ int ecx_setupnic(ecx_portt *port, const char *ifname, int secondary) port->tx_mutex = mtx_create(); port->rx_mutex = mtx_create(); - rVal = bfin_EMAC_init((uint8_t *)priMAC); + rVal = oshw_mac_init((uint8_t *)priMAC); if (rVal != 0) return 0; @@ -263,7 +260,7 @@ int ecx_outframe(ecx_portt *port, int idx, int stacknumber) } lp = (*stack->txbuflength)[idx]; (*stack->rxbufstat)[idx] = EC_BUF_TX; - rval = bfin_EMAC_send((*stack->txbuf)[idx], lp); + rval = oshw_mac_send((*stack->txbuf)[idx], lp); return rval; } @@ -299,7 +296,7 @@ int ecx_outframe_red(ecx_portt *port, int idx) // OBS! redundant not ACTIVE for BFIN, just added to compile ASSERT (0); port->redport->rxbufstat[idx] = EC_BUF_TX; - bfin_EMAC_send(&(port->txbuf2), port->txbuflength2); + oshw_mac_send(&(port->txbuf2), port->txbuflength2); mtx_unlock (port->tx_mutex); } @@ -325,7 +322,7 @@ static int ecx_recvpkt(ecx_portt *port, int stacknumber) stack = &(port->redport->stack); } lp = sizeof(port->tempinbuf); - bytesrx = bfin_EMAC_recv((*stack->tempbuf), lp); + bytesrx = oshw_mac_recv((*stack->tempbuf), lp); port->tempinbufs = bytesrx; return (bytesrx > 0); diff --git a/oshw/rtk/nicdrv.h b/oshw/rtk/nicdrv.h index 900b3f5..a2aee3a 100644 --- a/oshw/rtk/nicdrv.h +++ b/oshw/rtk/nicdrv.h @@ -11,6 +11,8 @@ #ifndef _nicdrvh_ #define _nicdrvh_ +#include + /** pointer structure to Tx and Rx stacks */ typedef struct { diff --git a/oshw/rtk/oshw.h b/oshw/rtk/oshw.h index 0eb3c0b..2ecdfaf 100644 --- a/oshw/rtk/oshw.h +++ b/oshw/rtk/oshw.h @@ -21,6 +21,9 @@ extern "C" #include "nicdrv.h" #include "ethercatmain.h" +int oshw_mac_init (const uint8_t * mac_address); +int oshw_mac_send (const void *payload, size_t tot_len); +int oshw_mac_recv (void * buffer, size_t buffer_length); uint16 oshw_htons(uint16 host); uint16 oshw_ntohs(uint16 network);