/*
* DEBUGGER code
*
* On "publish", when buffer is free, debugger stores arbitrary variables
* content into, and mark this buffer as filled
*
*
* Buffer content is read asynchronously, (from non real time part),
* and then buffer marked free again.
*
*
* */
#ifdef TARGET_DEBUG_AND_RETAIN_DISABLE
void __init_debug (void){}
void __cleanup_debug (void){}
void __retrieve_debug(void){}
void __publish_debug (void){}
#else
#include "iec_types_all.h"
#include "POUS.h"
/*for memcpy*/
#include <string.h>
#include <stdio.h>
#ifndef TARGET_ONLINE_DEBUG_DISABLE
#define BUFFER_SIZE 2
/* Atomically accessed variable for buffer state */
#define BUFFER_FREE 0
#define BUFFER_BUSY 1
static long buffer_state = BUFFER_FREE;
/* The buffer itself */
char debug_buffer[BUFFER_SIZE];
/* Buffer's cursor*/
static char* buffer_cursor = debug_buffer;
#endif
static unsigned int retain_offset = 0;
/***
* Declare programs
**/
extern IO_TEST RES0__INSTANCE0;
/***
* Declare global variables from resources and conf
**/
extern IO_TEST RES0__INSTANCE0;
typedef const struct {
void *ptr;
__IEC_types_enum type;
} dbgvardsc_t;
static dbgvardsc_t dbgvardsc[] = {
{&(RES0__INSTANCE0.K0), BOOL_ENUM},
{&(RES0__INSTANCE0.P0), BOOL_ENUM}
};
typedef void(*__for_each_variable_do_fp)(dbgvardsc_t*);
void __for_each_variable_do(__for_each_variable_do_fp fp)
{
unsigned int i;
for(i = 0; i < sizeof(dbgvardsc)/sizeof(dbgvardsc_t); i++){
dbgvardsc_t *dsc = &dbgvardsc[i];
if(dsc->type != UNKNOWN_ENUM)
(*fp)(dsc);
}
}
#define __Unpack_case_t(TYPENAME) \
case TYPENAME##_ENUM :\
*flags = ((__IEC_##TYPENAME##_t *)varp)->flags;\
forced_value_p = *real_value_p = &((__IEC_##TYPENAME##_t *)varp)->value;\
break;
#define __Unpack_case_p(TYPENAME)\
case TYPENAME##_O_ENUM :\
*flags = __IEC_OUTPUT_FLAG;\
case TYPENAME##_P_ENUM :\
*flags |= ((__IEC_##TYPENAME##_p *)varp)->flags;\
*real_value_p = ((__IEC_##TYPENAME##_p *)varp)->value;\
forced_value_p = &((__IEC_##TYPENAME##_p *)varp)->fvalue;\
break;
void* UnpackVar(dbgvardsc_t *dsc, void **real_value_p, char *flags)
{
void *varp = dsc->ptr;
void *forced_value_p = NULL;
*flags = 0;
/* find data to copy*/
switch(dsc->type){
__ANY(__Unpack_case_t)
__ANY(__Unpack_case_p)
default:
break;
}
if (*flags & __IEC_FORCE_FLAG)
return forced_value_p;
return *real_value_p;
}
void Remind(unsigned int offset, unsigned int count, void * p);
void RemindIterator(dbgvardsc_t *dsc)
{
void *real_value_p = NULL;
char flags = 0;
UnpackVar(dsc, &real_value_p, &flags);
if(flags & __IEC_RETAIN_FLAG){
USINT size = __get_type_enum_size(dsc->type);
/* compute next cursor positon*/
unsigned int next_retain_offset = retain_offset + size;
/* if buffer not full */
Remind(retain_offset, size, real_value_p);
/* increment cursor according size*/
retain_offset = next_retain_offset;
}
}
extern int CheckRetainBuffer(void);
extern void InitRetain(void);
void __init_debug(void)
{
/* init local static vars */
#ifndef TARGET_ONLINE_DEBUG_DISABLE
buffer_cursor = debug_buffer;
buffer_state = BUFFER_FREE;
#endif
retain_offset = 0;
InitRetain();
/* Iterate over all variables to fill debug buffer */
if(CheckRetainBuffer()){
__for_each_variable_do(RemindIterator);
}else{
char mstr[] = "RETAIN memory invalid - defaults used";
LogMessage(LOG_WARNING, mstr, sizeof(mstr));
}
retain_offset = 0;
}
extern void InitiateDebugTransfer(void);
extern void CleanupRetain(void);
extern unsigned long __tick;
void __cleanup_debug(void)
{
#ifndef TARGET_ONLINE_DEBUG_DISABLE
buffer_cursor = debug_buffer;
InitiateDebugTransfer();
#endif
CleanupRetain();
}
void __retrieve_debug(void)
{
}
void Retain(unsigned int offset, unsigned int count, void * p);
static inline void BufferIterator(dbgvardsc_t *dsc, int do_debug)
{
void *real_value_p = NULL;
void *visible_value_p = NULL;
char flags = 0;
visible_value_p = UnpackVar(dsc, &real_value_p, &flags);
if(flags & ( __IEC_DEBUG_FLAG | __IEC_RETAIN_FLAG)){
USINT size = __get_type_enum_size(dsc->type);
#ifndef TARGET_ONLINE_DEBUG_DISABLE
if(flags & __IEC_DEBUG_FLAG){
/* copy visible variable to buffer */;
if(do_debug){
/* compute next cursor positon.
No need to check overflow, as BUFFER_SIZE
is computed large enough */
if((dsc->type == STRING_ENUM) ||
(dsc->type == STRING_P_ENUM) ||
(dsc->type == STRING_O_ENUM)){
/* optimization for strings */
size = ((STRING*)visible_value_p)->len + 1;
}
char* next_cursor = buffer_cursor + size;
/* copy data to the buffer */
memcpy(buffer_cursor, visible_value_p, size);
/* increment cursor according size*/
buffer_cursor = next_cursor;
}
/* re-force real value of outputs (M and Q)*/
if((flags & __IEC_FORCE_FLAG) && (flags & __IEC_OUTPUT_FLAG)){
memcpy(real_value_p, visible_value_p, size);
}
}
#endif
if(flags & __IEC_RETAIN_FLAG){
/* compute next cursor positon*/
unsigned int next_retain_offset = retain_offset + size;
/* if buffer not full */
Retain(retain_offset, size, real_value_p);
/* increment cursor according size*/
retain_offset = next_retain_offset;
}
}
}
void DebugIterator(dbgvardsc_t *dsc){
BufferIterator(dsc, 1);
}
void RetainIterator(dbgvardsc_t *dsc){
BufferIterator(dsc, 0);
}
unsigned int retain_size = 0;
/* GetRetainSizeIterator */
void GetRetainSizeIterator(dbgvardsc_t *dsc)
{
void *real_value_p = NULL;
char flags = 0;
UnpackVar(dsc, &real_value_p, &flags);
if(flags & __IEC_RETAIN_FLAG){
USINT size = __get_type_enum_size(dsc->type);
/* Calc retain buffer size */
retain_size += size;
}
}
/* Return size of all retain variables */
unsigned int GetRetainSize(void)
{
__for_each_variable_do(GetRetainSizeIterator);
return retain_size;
}
extern void PLC_GetTime(IEC_TIME*);
extern int TryEnterDebugSection(void);
extern long AtomicCompareExchange(long*, long, long);
extern long long AtomicCompareExchange64(long long* , long long , long long);
extern void LeaveDebugSection(void);
extern void ValidateRetainBuffer(void);
extern void InValidateRetainBuffer(void);
void __publish_debug(void)
{
retain_offset = 0;
InValidateRetainBuffer();
#ifndef TARGET_ONLINE_DEBUG_DISABLE
/* Check there is no running debugger re-configuration */
if(TryEnterDebugSection()){
/* Lock buffer */
long latest_state = AtomicCompareExchange(
&buffer_state,
BUFFER_FREE,
BUFFER_BUSY);
/* If buffer was free */
if(latest_state == BUFFER_FREE)
{
/* Reset buffer cursor */
buffer_cursor = debug_buffer;
/* Iterate over all variables to fill debug buffer */
__for_each_variable_do(DebugIterator);
/* Leave debug section,
* Trigger asynchronous transmission
* (returns immediately) */
InitiateDebugTransfer(); /* size */
}else{
/* when not debugging
没有合适的资源?快使用搜索试试~ 我知道了~
PLC,IOT>ESP32 项目实践-简单入门
共17个文件
c:5个
h:4个
xml:2个
需积分: 0 4 下载量 120 浏览量
2023-09-27
08:14:58
上传
评论
收藏 30KB ZIP 举报
温馨提示
实验项目设计文件
资源推荐
资源详情
资源评论
收起资源包目录
IO.zip (17个子文件)
IO
beremiz.xml 133B
build
Config0.c 638B
VARIABLES.csv 282B
Res0.c 711B
POUS.c 379B
LOCATED_VARIABLES.h 0B
plc_main.c 9KB
plc.st 279B
extra_files
Config0.h 24B
plc.bin 5KB
lastbuildPLC.md5 32B
plc 54KB
generated_plc.st 296B
plc_debugger.c 11KB
beremiz.h 608B
POUS.h 479B
plc.xml 3KB
共 17 条
- 1
资源评论
ESP32:{PLC+IOT}
- 粉丝: 20
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功