* 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.
#include "iec_types_all.h"
#define BUFFER_SIZE %(buffer_size)d
#define MAX_SUBSCRIBTION %(subscription_table_count)d
/* Atomically accessed variable for buffer state */
static long buffer_state = BUFFER_FREE;
char debug_buffer[BUFFER_SIZE];
static char* buffer_cursor = debug_buffer;
%(programs_declarations)s
* Declare global variables from resources and conf
%(extern_variables_declarations)s
static int subscription_table[MAX_SUBSCRIBTION];
static int* latest_subscription = subscription_table;
static int* subscription_cursor = subscription_table;
struct_plcvar variable_table[%(variables_pointer_type_table_count)d];
%(variables_pointer_type_table_initializer)s
buffer_state = BUFFER_FREE;
void __cleanup_debug(void)
void __retrieve_debug(void)
extern int TryEnterDebugSection(void);
extern void LeaveDebugSection(void);
extern long AtomicCompareExchange(long*, long, long);
extern void InitiateDebugTransfer(void);
extern unsigned long __tick;
void __publish_debug(void)
/* Check there is no running debugger re-configuration */
if(TryEnterDebugSection()){
long latest_state = AtomicCompareExchange(
if(latest_state == BUFFER_FREE)
/* Reset buffer cursor */
buffer_cursor = debug_buffer;
/* iterate over subscriptions */
for(subscription=subscription_table;
subscription < latest_subscription;
/* get variable descriptor */
struct_plcvar* my_var = &variable_table[*subscription];
USINT size = __get_type_enum_size(my_var->type);
/* compute next cursor positon*/
next_cursor = buffer_cursor + size;
if(next_cursor <= debug_buffer + BUFFER_SIZE)
/* copy data to the buffer */
memcpy(buffer_cursor, my_var->ptrvalue, size);
/* increment cursor according size*/
buffer_cursor = next_cursor;
/*TODO : signal overflow*/
/* Reset buffer cursor again (for IterDebugData)*/
buffer_cursor = debug_buffer;
subscription_cursor = subscription_table;
* Trigger asynchronous transmission
* (returns immediately) */
InitiateDebugTransfer(); /* size */
void RegisterDebugVariable(int idx)
/*If subscription table not full */
if(latest_subscription - subscription_table < MAX_SUBSCRIBTION)
*(latest_subscription++) = idx;
/* TODO pre-calc buffer size and signal overflow*/
/*TODO : signal subscription overflow*/
void ResetDebugVariables(void)
latest_subscription = subscription_table;
/* atomically mark buffer as free */
latest_state = AtomicCompareExchange(
void* IterDebugData(int* idx, const char **type_name)
if(subscription_cursor < latest_subscription){
char* old_cursor = buffer_cursor;
*idx = *subscription_cursor;
my_var = &variable_table[*(subscription_cursor++)];
*type_name = __get_type_enum_name(my_var->type);
size = __get_type_enum_size(my_var->type);
/* compute next cursor position*/
buffer_cursor = buffer_cursor + size;
if(old_cursor < debug_buffer + BUFFER_SIZE)
//printf("%%d > %%d\n", old_cursor - debug_buffer, BUFFER_SIZE);