SOEM EtherCAT Master library for STM Nucleo F767ZI

Dependents:   EasyCAT_LAB_simple EasyCAT_LAB_very_simple EasyCAT_LAB

  • It has been developed for the EasyCAT LAB , a complete educational and experimental EtherCAT® system, composed of one master and two slaves .

Warning

/media/uploads/EasyCAT/easycat_lab.jpg

Committer:
EasyCAT
Date:
Tue Jun 11 10:29:09 2019 +0000
Revision:
0:543d6784d4cc
SOEM EtherCAT Master Library for STM Nucleo F767ZI

Who changed what in which revision?

UserRevisionLine numberNew contents of line
EasyCAT 0:543d6784d4cc 1 /*
EasyCAT 0:543d6784d4cc 2 * Licensed under the GNU General Public License version 2 with exceptions. See
EasyCAT 0:543d6784d4cc 3 * LICENSE file in the project root for full license information
EasyCAT 0:543d6784d4cc 4 */
EasyCAT 0:543d6784d4cc 5
EasyCAT 0:543d6784d4cc 6 #include "mbed.h"
EasyCAT 0:543d6784d4cc 7
EasyCAT 0:543d6784d4cc 8 #include <osal.h>
EasyCAT 0:543d6784d4cc 9 #include <config.h>
EasyCAT 0:543d6784d4cc 10
EasyCAT 0:543d6784d4cc 11
EasyCAT 0:543d6784d4cc 12 uint64_t tick = 0;
EasyCAT 0:543d6784d4cc 13 uint64_t last_tick = 0;
EasyCAT 0:543d6784d4cc 14
EasyCAT 0:543d6784d4cc 15
EasyCAT 0:543d6784d4cc 16 #define timercmp(a, b, CMP) \
EasyCAT 0:543d6784d4cc 17 (((a)->tv_sec == (b)->tv_sec) ? \
EasyCAT 0:543d6784d4cc 18 ((a)->tv_usec CMP (b)->tv_usec) : \
EasyCAT 0:543d6784d4cc 19 ((a)->tv_sec CMP (b)->tv_sec))
EasyCAT 0:543d6784d4cc 20
EasyCAT 0:543d6784d4cc 21
EasyCAT 0:543d6784d4cc 22 #define timeradd(a, b, result) \
EasyCAT 0:543d6784d4cc 23 do{ \
EasyCAT 0:543d6784d4cc 24 (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
EasyCAT 0:543d6784d4cc 25 (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
EasyCAT 0:543d6784d4cc 26 if ((result)->tv_usec >= 1000000) \
EasyCAT 0:543d6784d4cc 27 { \
EasyCAT 0:543d6784d4cc 28 ++(result)->tv_sec; \
EasyCAT 0:543d6784d4cc 29 (result)->tv_usec -= 1000000; \
EasyCAT 0:543d6784d4cc 30 } \
EasyCAT 0:543d6784d4cc 31 } while (0)
EasyCAT 0:543d6784d4cc 32 #define timersub(a, b, result) \
EasyCAT 0:543d6784d4cc 33 do { \
EasyCAT 0:543d6784d4cc 34 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
EasyCAT 0:543d6784d4cc 35 (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
EasyCAT 0:543d6784d4cc 36 if ((result)->tv_usec < 0) { \
EasyCAT 0:543d6784d4cc 37 --(result)->tv_sec; \
EasyCAT 0:543d6784d4cc 38 (result)->tv_usec += 1000000; \
EasyCAT 0:543d6784d4cc 39 } \
EasyCAT 0:543d6784d4cc 40 } while (0)
EasyCAT 0:543d6784d4cc 41
EasyCAT 0:543d6784d4cc 42
EasyCAT 0:543d6784d4cc 43 #define CFG_TICKS_PER_SECOND 1000000
EasyCAT 0:543d6784d4cc 44 #define USECS_PER_SEC 1000000
EasyCAT 0:543d6784d4cc 45 #define USECS_PER_TICK (USECS_PER_SEC / CFG_TICKS_PER_SECOND)
EasyCAT 0:543d6784d4cc 46
EasyCAT 0:543d6784d4cc 47
EasyCAT 0:543d6784d4cc 48
EasyCAT 0:543d6784d4cc 49 int gettimeofday_(struct timeval *tp, void *tzp)
EasyCAT 0:543d6784d4cc 50 {
EasyCAT 0:543d6784d4cc 51 //tick_t tick = tick_get();
EasyCAT 0:543d6784d4cc 52 uint32_t actual_tick = us_ticker_read();
EasyCAT 0:543d6784d4cc 53 tick += (actual_tick - last_tick);
EasyCAT 0:543d6784d4cc 54 last_tick = actual_tick;
EasyCAT 0:543d6784d4cc 55
EasyCAT 0:543d6784d4cc 56 //tick_t ticks_left;
EasyCAT 0:543d6784d4cc 57 uint64_t ticks_left;
EasyCAT 0:543d6784d4cc 58
EasyCAT 0:543d6784d4cc 59 tp->tv_sec = tick / CFG_TICKS_PER_SECOND;
EasyCAT 0:543d6784d4cc 60 ticks_left = tick % CFG_TICKS_PER_SECOND;
EasyCAT 0:543d6784d4cc 61 tp->tv_usec = ticks_left * USECS_PER_TICK;
EasyCAT 0:543d6784d4cc 62
EasyCAT 0:543d6784d4cc 63 return 0;
EasyCAT 0:543d6784d4cc 64 }
EasyCAT 0:543d6784d4cc 65
EasyCAT 0:543d6784d4cc 66 int osal_usleep (uint32 usec)
EasyCAT 0:543d6784d4cc 67 {
EasyCAT 0:543d6784d4cc 68 wait_us(usec);
EasyCAT 0:543d6784d4cc 69 return 0;
EasyCAT 0:543d6784d4cc 70 }
EasyCAT 0:543d6784d4cc 71
EasyCAT 0:543d6784d4cc 72
EasyCAT 0:543d6784d4cc 73 int osal_gettimeofday(struct timeval *tv, struct timezone *tz)
EasyCAT 0:543d6784d4cc 74 {
EasyCAT 0:543d6784d4cc 75 return gettimeofday_(tv, tz);
EasyCAT 0:543d6784d4cc 76 }
EasyCAT 0:543d6784d4cc 77
EasyCAT 0:543d6784d4cc 78
EasyCAT 0:543d6784d4cc 79 ec_timet osal_current_time (void)
EasyCAT 0:543d6784d4cc 80 {
EasyCAT 0:543d6784d4cc 81 struct timeval current_time;
EasyCAT 0:543d6784d4cc 82 ec_timet return_value;
EasyCAT 0:543d6784d4cc 83
EasyCAT 0:543d6784d4cc 84 gettimeofday_ (&current_time, 0);
EasyCAT 0:543d6784d4cc 85 return_value.sec = current_time.tv_sec;
EasyCAT 0:543d6784d4cc 86 return_value.usec = current_time.tv_usec;
EasyCAT 0:543d6784d4cc 87 return return_value;
EasyCAT 0:543d6784d4cc 88 }
EasyCAT 0:543d6784d4cc 89
EasyCAT 0:543d6784d4cc 90 void osal_timer_start (osal_timert * self, uint32 timeout_usec)
EasyCAT 0:543d6784d4cc 91 {
EasyCAT 0:543d6784d4cc 92 struct timeval start_time;
EasyCAT 0:543d6784d4cc 93 struct timeval timeout;
EasyCAT 0:543d6784d4cc 94 struct timeval stop_time;
EasyCAT 0:543d6784d4cc 95
EasyCAT 0:543d6784d4cc 96 gettimeofday_ (&start_time, 0);
EasyCAT 0:543d6784d4cc 97 timeout.tv_sec = timeout_usec / USECS_PER_SEC;
EasyCAT 0:543d6784d4cc 98 timeout.tv_usec = timeout_usec % USECS_PER_SEC;
EasyCAT 0:543d6784d4cc 99 timeradd (&start_time, &timeout, &stop_time);
EasyCAT 0:543d6784d4cc 100
EasyCAT 0:543d6784d4cc 101 self->stop_time.sec = stop_time.tv_sec;
EasyCAT 0:543d6784d4cc 102 self->stop_time.usec = stop_time.tv_usec;
EasyCAT 0:543d6784d4cc 103 }
EasyCAT 0:543d6784d4cc 104
EasyCAT 0:543d6784d4cc 105 boolean osal_timer_is_expired (osal_timert * self)
EasyCAT 0:543d6784d4cc 106 {
EasyCAT 0:543d6784d4cc 107 struct timeval current_time;
EasyCAT 0:543d6784d4cc 108 struct timeval stop_time;
EasyCAT 0:543d6784d4cc 109 int is_not_yet_expired;
EasyCAT 0:543d6784d4cc 110
EasyCAT 0:543d6784d4cc 111 gettimeofday_ (&current_time, 0);
EasyCAT 0:543d6784d4cc 112 stop_time.tv_sec = self->stop_time.sec;
EasyCAT 0:543d6784d4cc 113 stop_time.tv_usec = self->stop_time.usec;
EasyCAT 0:543d6784d4cc 114 is_not_yet_expired = timercmp (&current_time, &stop_time, <);
EasyCAT 0:543d6784d4cc 115
EasyCAT 0:543d6784d4cc 116 return is_not_yet_expired == false;
EasyCAT 0:543d6784d4cc 117 }