/**
* Head of code common to all C targets
**/
#include "beremiz.h"
#include <string.h>
/*
* Prototypes of functions provided by generated C softPLC
**/
void config_run__(unsigned long tick);
void config_init__(void);
/*
* Prototypes of functions provided by generated target C code
* */
long long AtomicCompareExchange64(long long*, long long, long long);
void __init_debug(void);
void __cleanup_debug(void);
/*void __retrieve_debug(void);*/
void __publish_debug(void);
/*
* Variables used by generated C softPLC and plugins
**/
IEC_TIME __CURRENT_TIME;
IEC_BOOL __DEBUG = 0;
unsigned long __tick = 0;
char *PLC_ID = NULL;
/*
* Variable generated by C softPLC and plugins
**/
extern unsigned long greatest_tick_count__;
/* Help to quit cleanly when init fail at a certain level */
static int init_level = 0;
/*
* Prototypes of functions exported by plugins
**/
/*
* Retrieve input variables, run PLC and publish output variables
**/
void __run(void)
{
__tick++;
if (greatest_tick_count__)
__tick %= greatest_tick_count__;
/*__retrieve_debug();*/
config_run__(__tick);
__publish_debug();
}
/*
* Initialize variables according to PLC's default values,
* and then init plugins with that values
**/
int __init(int argc,char **argv)
{
int res = 0;
init_level = 0;
/* Effective tick time with 1ms default value */
if(!common_ticktime__)
common_ticktime__ = 1000000;
config_init__();
__init_debug();
return res;
}
/*
* Calls plugin cleanup proc.
**/
void __cleanup(void)
{
__cleanup_debug();
}
void PLC_GetTime(IEC_TIME *CURRENT_TIME);
void PLC_SetTimer(unsigned long long next, unsigned long long period);
/**
* Linux specific code
**/
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <signal.h>
#include <stdlib.h>
#include <errno.h>
#include <pthread.h>
#include <locale.h>
#include <semaphore.h>
#ifdef REALTIME_LINUX
#include <sys/mman.h>
#endif
#define _Log(level,text,...) \
{\
char mstr[256];\
snprintf(mstr, 255, text, ##__VA_ARGS__);\
LogMessage(LOG_CRITICAL, mstr, strlen(mstr));\
}
#define _LogError(text,...) _Log(LOG_CRITICAL, text, ##__VA_ARGS__)
#define _LogWarning(text,...) _Log(LOG_WARNING, text, ##__VA_ARGS__)
static unsigned long __debug_tick;
static pthread_t PLC_thread;
static pthread_mutex_t python_wait_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t python_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t debug_wait_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t debug_mutex = PTHREAD_MUTEX_INITIALIZER;
static int PLC_shutdown = 0;
long AtomicCompareExchange(long* atomicvar,long compared, long exchange)
{
return __sync_val_compare_and_swap(atomicvar, compared, exchange);
}
long long AtomicCompareExchange64(long long* atomicvar, long long compared, long long exchange)
{
return __sync_val_compare_and_swap(atomicvar, compared, exchange);
}
void PLC_GetTime(IEC_TIME *CURRENT_TIME)
{
struct timespec tmp;
clock_gettime(CLOCK_REALTIME, &tmp);
CURRENT_TIME->tv_sec = tmp.tv_sec;
CURRENT_TIME->tv_nsec = tmp.tv_nsec;
}
static long long period_ns = 0;
struct timespec next_cycle_time;
static void inc_timespec(struct timespec *ts, unsigned long long value_ns)
{
long long next_ns = ((long long) ts->tv_sec * 1000000000) + ts->tv_nsec + value_ns;
#ifdef __lldiv_t_defined
lldiv_t next_div = lldiv(next_ns, 1000000000);
ts->tv_sec = next_div.quot;
ts->tv_nsec = next_div.rem;
#else
ts->tv_sec = next_ns / 1000000000;
ts->tv_nsec = next_ns % 1000000000;
#endif
}
void PLC_SetTimer(unsigned long long next, unsigned long long period)
{
/*
printf("SetTimer(%lld,%lld)\n",next, period);
*/
period_ns = period;
clock_gettime(CLOCK_MONOTONIC, &next_cycle_time);
inc_timespec(&next_cycle_time, next);
// interrupt clock_nanpsleep
pthread_kill(PLC_thread, SIGUSR1);
}
void catch_signal(int sig)
{
// signal(SIGTERM, catch_signal);
signal(SIGINT, catch_signal);
printf("Got Signal %d\n",sig);
exit(0);
}
void PLCThreadSignalHandler(int sig)
{
if (sig == SIGUSR2)
pthread_exit(NULL);
}
int ForceSaveRetainReq(void) {
return PLC_shutdown;
}
#define MAX_JITTER period_ns/10
#define MIN_IDLE_TIME_NS 1000000 /* 1ms */
/* Macro to compare timespec, evaluate to True if a is past b */
#define timespec_gt(a,b) (a.tv_sec > b.tv_sec || (a.tv_sec == b.tv_sec && a.tv_nsec > b.tv_nsec))
void PLC_thread_proc(void *arg)
{
/* initialize next occurence and period */
period_ns = common_ticktime__;
clock_gettime(CLOCK_MONOTONIC, &next_cycle_time);
while (!PLC_shutdown) {
int res;
struct timespec plc_end_time;
int periods = 0;
#ifdef REALTIME_LINUX
struct timespec deadline_time;
struct timespec plc_start_time;
#endif
// BEREMIZ_TEST_CYCLES is defined in tests that need to emulate time:
// - all BEREMIZ_TEST_CYCLES cycles are executed in a row with no pause
// - __CURRENT_TIME is incremented each cycle according to emulated cycle period
#ifndef BEREMIZ_TEST_CYCLES
// Sleep until next PLC run
res = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &next_cycle_time, NULL);
if(res==EINTR){
continue;
}
if(res!=0){
_LogError("PLC thread timer returned error %d \n", res);
return;
}
#endif // BEREMIZ_TEST_CYCLES
#ifdef REALTIME_LINUX
// timer overrun detection
clock_gettime(CLOCK_MONOTONIC, &plc_start_time);
deadline_time=next_cycle_time;
inc_timespec(&deadline_time, MAX_JITTER);
if(timespec_gt(plc_start_time, deadline_time)){
_LogWarning("PLC thread woken up too late. PLC cyclic task interval is too small.\n");
}
#endif
#ifdef BEREMIZ_TEST_CYCLES
#define xstr(s) str(s)
#define str(arg) #arg
// fake current time
__CURRENT_TIME.tv_sec = next_cycle_time.tv_sec;
__CURRENT_TIME.tv_nsec = next_cycle_time.tv_nsec;
// exit loop when enough cycles
if(__tick >= BEREMIZ_TEST_CYCLES) {
_LogWarning("TEST PLC thread ended after "xstr(BEREMIZ_TEST_CYCLES)" cycles.\n");
// After pre-defined test cycles count, PLC thread exits.
// Remaining PLC runtime is expected to be cleaned-up/killed by test script
return;
}
#else
PLC_GetTime(&__CURRENT_TIME);
#endif
__run();
#ifndef BEREMIZ_TEST_CYCLES
// ensure next PLC cycle occurence is in the future
clock_gettime(CLOCK_MONOTONIC, &plc_end_time);
while(timespec_gt(plc_end_time, next_cycle_time))
#endif
{
periods += 1;
inc_timespec(&next_cycle_time, period_ns);
}
// plc execution time overrun detection
if(periods > 1) {
// Mitigate CPU hogging, in case of too small cyclic task interval:
// - since cycle deadline already missed, better keep system responsive
// - test if next cycle occurs after minimal idle
// - enforce minimum idle time if not
struct timespec earliest_possible_time = plc_end_time;
inc_timespec(&earliest_possible_time, MIN_IDLE_TIME_NS);
while(timespec_gt(earliest_possible_time, next_cycle_time)){
periods += 1;
inc_timespec(&next_cycle_time, period_ns);
}
// increment tick count anyhow, so that task scheduling keeps consistent
__tick+=periods-1;
_LogWarning("PLC execution time is longer than requested PLC cyclic task interval. %d cycles skipped\n", periods);
}
}
pthread_exit(0);
}
#define maxval(a,b) ((a>b)?a:b)
int startPLC(int argc,char **argv)
{
int ret;
pthread_attr_t *pattr = NULL;
#ifdef REALTIME_LINUX
struct sched_param param;
pthread_attr_t attr;
/* Lock memory */
ret = mloc
openplc Linux 地址映射io,读写驱动数据等使用记录
96 浏览量
2024-04-03
16:48:17
上传
评论
收藏 55KB ZIP 举报
闲来阅遍花影
- 粉丝: 94
- 资源: 3
最新资源
- 基于Python皮肤电信号的情绪识别算法源码+模型+PPT+详细文档+全部数据资料.zip
- 基于Python皮肤电信号的情绪识别算法源码+模型+PPT+详细文档+全部数据资料.zip
- 区块链毕业设计 基于区块链的慈善募捐平台的智能合约源码+详细文档+全部资料(高分项目).zip
- 区块链毕业设计 基于区块链的慈善募捐平台的智能合约源码+详细文档+全部资料(高分项目).zip
- 区块链毕业设计 基于区块链的慈善募捐平台的智能合约源码+详细文档+全部资料(高分项目).zip
- 免费听歌软件,支持无损音乐下载,支持收藏音乐同步
- 锐起RDV-5.0 build 6503
- ngrok-stable-windows-amd64.zip
- 狗脸检测数据集VOC+YOLO格式6154张1类别.7z
- 使用C++制作一个头像框
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈