Common.h File Reference

Common Support Functions: Definitions and Prototypes More...

Data Structures

struct  PACKET
 
struct  T_ComDevice
 

Macros

#define COMMON_CODE_SECTION   ".commontext"
 
#define PRAGMA(x)   _Pragma(#x)
 
#define LOOP_COUNT_INFO(min, multiple)   PRAGMA(MUST_ITERATE(min,,multiple))
 
#define VAR_IN_SECTION(var, section)   PRAGMA(DATA_SECTION(var, #section))
 
#define PTR_IS_ALIGNED(ptr, pow2)   _nassert(((uint32_t) (ptr) & (pow2)-1) == 0);
 
#define CSPRAGMA(f, s)   PRAGMA(CODE_SECTION(f, s))
 
#define _CPP_extern_open_
 
#define _CPP_extern_close_
 
#define NULL   ((void *)UINT32_C(0))
 
#define _CODE_ACCESS   far
 
#define _DATA_ACCESS   far
 
#define UNREFERENCED_PARAMETER(P)    ((P)=(P)) \
 
#define UNUSED_PARAMETER(P)    ((void)(P)) \
 
#define BUILD_YEAR
 
#define BUILD_DAY   ((__DATE__ [4] == ' ' ? 0 : __DATE__ [4] - '0') * 10 + (__DATE__ [5] - '0')) \
 
#define BUILD_MONTH
 
#define BUILD_HOUR   ( (__TIME__ [0] - '0')*10 + (__TIME__ [1] - '0') )
 
#define BUILD_MINUTE   ( (__TIME__ [3] - '0')*10 + (__TIME__ [4] - '0') )
 
#define BUILD_SECOND   ( (__TIME__ [6] - '0')*10 + (__TIME__ [7] - '0') )
 
#define MAX(a, b)   ((a) > (b) ? (a) : (b))
 
#define MIN(a, b)   ((a) < (b) ? a : b)
 
#define BLOCK_OVERHEAD   sizeof(PACKET)
 
#define BLOCK_OFFSET   sizeof(PACKET)
 
#define SIZE_ACTUAL(x)   ((x) & ~0x1)
 
#define SIZE_IS_USED(x)   ((x) & 0x1)
 
#define HEAP_END(x, y)   ((PACKET *)((char *)(x)+((y))-sizeof(PACKET)))
 
#define ASSERT_HEAP(i, h)    if ( (i) < (h) ) prg_exit ("Insufficient heap");
 
#define prg_exit(s)
 
#define tpFormatOutputFunc   tpFormatOutputFunc
 
#define tpOutputFunc   tpOutputFunc
 
#define tpWriteString   tpWriteString
 
#define tpWriteChar   tpWriteChar
 
#define tpReadChar   tpReadChar
 
#define MessageLogFxn_type   tpFormatOutputFunc
 
#define T_Handle   T_Handle
 
#define COM_DEV_CCS   0
 
#define COM_DEV_UART   1
 
#define COM_DEV_USB   2
 
#define COM_DEV_NET   3
 
#define COM_DEV_TELNET   4
 
#define COM_DEV_FILE   5
 
#define COM_DEV_CDC   6
 
#define T_ComDevice   T_ComDevice
 
#define tpInputParser   tpInputParser
 
#define BUFFEROVERFLOW_ERROR   1
 

Typedefs

typedef int(* tpFormatOutputFunc) (const char *,...)
 
typedef int(* tpOutputFunc) (const char *,...)
 
typedef void(* tpWriteString) (const char *)
 
typedef int32_t(* tpWriteChar) (char)
 
typedef int32_t(* tpReadChar) (char *)
 
typedef void * T_Handle
 
typedef int16_t(* tpInputParser) (T_ComDevice *, char)
 

Enumerations

enum  FUNC_RETURN { FR_OK = 0, FR_SKIPPED, FR_ERROR, FR_TIMEDOUT }
 

Functions

void config_std_output (void *output)
 configure standard output stream More...
 
void PrintExitCode (void)
 Show a descriptive error message on exit. More...
 
void AppInit (uint32_t dsp_clock)
 Initialize application. More...
 
void AppCleanup (void)
 Disable interrupts, DMA, timer etc. More...
 
uint32_t progress (uint32_t done, uint32_t left, uint32_t bad)
 
char * num2str (int32_t num, char *buffer, int32_t min, int hexmode)
 convert a 32bit integer to string More...
 
_CODE_ACCESS void memmap (uint32_t heap_mem_size)
 show current dynamic memory allocation map More...
 
uint32_t GetFreeHeapSize (void)
 determine free heap size More...
 

Variables

_DATA_ACCESS int _SYSMEM_SIZE
 
uint32_t exit_code
 
const char * exit_string
 
char * exit_file
 
uint32_t exit_line
 
char * program_name
 
const char * FRMessages []
 
PACKET _sys_memory
 
_DATA_ACCESS int _memory_size
 
uint32_t initial_heap_size
 

Detailed Description

                          _         _             _
                       __| |    ___(_) ____ _ __ | |_
                      / _` |   / __| |/ _` | '_ \| __|
                     | (_| | _ \__ \ | (_| | | | | |_
                      \__,_|(_) ___/_|\__, |_| |_|\__|
                     Signalprocessing |___/ Technology
Author
D.SignT GmbH & Co. KG, Claus Hermbusche
Version
2.91
Date
2019-04-17

Macro Definition Documentation

#define COMMON_CODE_SECTION   ".commontext"
#define PRAGMA (   x)    _Pragma(#x)
#define LOOP_COUNT_INFO (   min,
  multiple 
)    PRAGMA(MUST_ITERATE(min,,multiple))
#define VAR_IN_SECTION (   var,
  section 
)    PRAGMA(DATA_SECTION(var, #section))
#define PTR_IS_ALIGNED (   ptr,
  pow2 
)    _nassert(((uint32_t) (ptr) & (pow2)-1) == 0);
#define CSPRAGMA (   f,
 
)    PRAGMA(CODE_SECTION(f, s))
#define _CPP_extern_open_
#define _CPP_extern_close_
#define NULL   ((void *)UINT32_C(0))
#define _CODE_ACCESS   far
#define _DATA_ACCESS   far
#define UNREFERENCED_PARAMETER (   P)    ((P)=(P)) \

suppresses warnings about params not used

Examples:
Blocksend.c, BoardSpecific.c, Echo.c, Multicast.c, NetTest.c, and Receive.c.
#define UNUSED_PARAMETER (   P)    ((void)(P)) \

suppresses warnings about params not used

#define BUILD_YEAR
Value:
((((__DATE__ [7] - '0') * 10 + (__DATE__ [8] - '0')) * 10 \
+ (__DATE__ [9] - '0')) * 10 + (__DATE__ [10] - '0')) \

build year macro. use to initialize timer variables or for descriptive outputs

#define BUILD_DAY   ((__DATE__ [4] == ' ' ? 0 : __DATE__ [4] - '0') * 10 + (__DATE__ [5] - '0')) \

build day macro. use to initialize timer variables or for descriptive outputs

#define BUILD_MONTH
Value:
( __DATE__ [2] == 'n' ? (__DATE__ [1] == 'a' ? 1 : 6) \
: __DATE__ [2] == 'b' ? 2 \
: __DATE__ [2] == 'r' ? (__DATE__ [0] == 'M' ? 3 : 4) \
: __DATE__ [2] == 'y' ? 5 \
: __DATE__ [2] == 'l' ? 7 \
: __DATE__ [2] == 'g' ? 8 \
: __DATE__ [2] == 'p' ? 9 \
: __DATE__ [2] == 't' ? 10 \
: __DATE__ [2] == 'v' ? 11 : 12 ) \

build month macro. use to initialize timer variables or for descriptive outputs

#define BUILD_HOUR   ( (__TIME__ [0] - '0')*10 + (__TIME__ [1] - '0') )

build hour macro. use to initialize timer variables or for descriptive outputs

#define BUILD_MINUTE   ( (__TIME__ [3] - '0')*10 + (__TIME__ [4] - '0') )

build minute macro. use to initialize timer variables or for descriptive outputs

#define BUILD_SECOND   ( (__TIME__ [6] - '0')*10 + (__TIME__ [7] - '0') )

build second macro. use to initialize timer variables or for descriptive outputs

#define MAX (   a,
 
)    ((a) > (b) ? (a) : (b))
#define MIN (   a,
 
)    ((a) < (b) ? a : b)
#define BLOCK_OVERHEAD   sizeof(PACKET)
#define BLOCK_OFFSET   sizeof(PACKET)
#define SIZE_ACTUAL (   x)    ((x) & ~0x1)
#define SIZE_IS_USED (   x)    ((x) & 0x1)
#define HEAP_END (   x,
 
)    ((PACKET *)((char *)(x)+((y))-sizeof(PACKET)))
#define ASSERT_HEAP (   i,
 
)    if ( (i) < (h) ) prg_exit ("Insufficient heap");
#define prg_exit (   s)
Value:
{ \
exit_string = s; \
exit_line = __LINE__; \
exit_file = __FILE__; \
exit (1); \
}

we use a macro rather than a function to log file and line

Examples:
Blocksend.c, BoardSpecific.c, Chat.c, Echo.c, FTPServer.c, HTTPdynamic.c, HTTPjava.c, HTTPsimple.c, Multicast.c, NetTest.c, Receive.c, Send.c, SMTP.c, and Telnet.c.
#define tpFormatOutputFunc   tpFormatOutputFunc
#define tpOutputFunc   tpOutputFunc
#define tpWriteString   tpWriteString
#define tpWriteChar   tpWriteChar
#define tpReadChar   tpReadChar
#define MessageLogFxn_type   tpFormatOutputFunc
#define T_Handle   T_Handle
#define COM_DEV_CCS   0
#define COM_DEV_UART   1
Examples:
BoardSpecific.c.
#define COM_DEV_USB   2
#define COM_DEV_NET   3
#define COM_DEV_TELNET   4
#define COM_DEV_FILE   5
#define COM_DEV_CDC   6
#define T_ComDevice   T_ComDevice
#define tpInputParser   tpInputParser
#define BUFFEROVERFLOW_ERROR   1
Examples:
BoardSpecific.c.

Typedef Documentation

typedef int(* tpFormatOutputFunc) (const char *,...)

Message log function. E.g. printf() or CPrintf()

typedef int(* tpOutputFunc) (const char *,...)
typedef void(* tpWriteString) (const char *)
typedef int32_t(* tpWriteChar) (char)
typedef int32_t(* tpReadChar) (char *)
typedef void* T_Handle

a general handle to a peripheral

typedef int16_t(* tpInputParser) (T_ComDevice *, char)

Enumeration Type Documentation

function return value

Enumerator
FR_OK 
FR_SKIPPED 
FR_ERROR 
FR_TIMEDOUT 
331 {

Function Documentation

void config_std_output ( void *  output)
Parameters
output- new output stream
Returns
nothing
161 {
162  _out_stream = (tpOutputFunc)(uint32_t)output;
163 }
#define tpOutputFunc
Definition: net.h:907
unsigned int uint32_t
Definition: stdint.h:47
static tpOutputFunc _out_stream
Definition: Common.c:142
void PrintExitCode ( void  )
Parameters
-
Returns
nothing

Print descriptive exit message. This function uses global pointer to exit string. Exit string is set by function prg_exit()

See also
exit_code, exit_string, prg_exit()
173 {
174 #ifdef _CPRINTF_H_
176 #endif
177 
178  /***************************************************************************
179  in case of cprintf buffer overflow a printf is not possible
180  set LEDs to signal exit status
181  ***************************************************************************/
183  {
184  /***********************************************************************
185  print exit code
186  ***********************************************************************/
187 
188  if (exit_string!=NULL)
190 
192 
193  /***********************************************************************
194  if necessary switch LED on to signal certain errors
195  ***********************************************************************/
196  // if ( (exit_code == OUT_OF_MEMORY_ERROR )
197  // {
198  // LED_on (1);
199  // }
200 
201  }
202  else
203  {
204  fputs (COMMON_BUFFER_OVERFLOW, stderr);
205 
206  /***********************************************************************
207  switch all LEDs on if printing is impossible
208  ***********************************************************************/
209  LED_on (0);
210  LED_on (1);
211  LED_on (2);
212  LED_on (3);
213  }
214 
215  /***************************************************************************
216  clean-up application, stop timers, DMA, close handles etc.
217  ***************************************************************************/
218  AppCleanup();
219 
220  return;
221 }
#define BUFFEROVERFLOW_ERROR
Definition: Common.h:383
#define NULL
Definition: net.h:126
void LED_on(unsigned int ledNum)
Definition: BoardSpecific.c:1302
#define COMMON_PROGRAM_HALTED
void CPrintfActivateOutput(void)
activate all initialized output devices
Definition: cprintf.c:387
const char * exit_string
Definition: Common.c:110
char * exit_file
Definition: Common.c:116
void AppCleanup(void)
Disable interrupts, DMA, timer etc.
Definition: Common.c:290
#define COMMON_EXIT
static tpOutputFunc _out_stream
Definition: Common.c:142
#define COMMON_BUFFER_OVERFLOW
uint32_t exit_code
Definition: Common.c:104
uint32_t exit_line
Definition: Common.c:122
void AppInit ( uint32_t  dsp_clock)
Parameters
dsp_clock[Hz]
Returns
nothing

Application Initialization Code
Put all your initialization code into this central function. To make the CPrintf() function work as well in your initialization code called from here CPrintf_select_output() should be moved here too.

Examples:
Blocksend.c, Chat.c, DHCPTest.c, DNSTest.c, Echo.c, FTPClient.c, FTPServer.c, HTTPdynamic.c, HTTPjava.c, HTTPsimple.c, Multicast.c, NetTest.c, Ping.c, Ping2.c, PServer.c, Receive.c, Send.c, SMTP.c, SNTPTest.c, and Telnet.c.
231 {
232  /***************************************************************************
233  locals
234  ***************************************************************************/
235 
236  /***************************************************************************
237  global disable interrupts
238  ***************************************************************************/
239  _disable_interrupts();
240 
241  /***************************************************************************
242  calculate configured heap size
243  ***************************************************************************/
244 #if (defined(USE_DSPBIOS) || defined(USE_SYSBIOS))
246 #else
248 #endif
249 
250  /***************************************************************************
251  basic initialisation
252  ***************************************************************************/
253  BoardAppInit (dsp_clock);
254 
255  /***************************************************************************
256  install exit function
257  ***************************************************************************/
258  atexit (PrintExitCode );
259 
260  /***************************************************************************
261  initialize board peripherals
262  ***************************************************************************/
264 
265  /***************************************************************************
266  initialize LEDs
267  ***************************************************************************/
268  LED_init ();
269 
270  /***************************************************************************
271  initialize KEYs
272  ***************************************************************************/
273  KEY_init ();
274 
275 #ifdef INC_CPRINTF
276  /***************************************************************************
277  let the standard outputs go to our CPrintf device
278  ***************************************************************************/
279  config_std_output((void *)CPrintf);
280 #endif
281 }
void BoardPeripheralInit(void)
board specific peripheral initialization
Definition: BoardSpecific.c:266
uint32_t initial_heap_size
Definition: Common.c:140
int CPrintf(const char *_format,...)
Custom printf function.
Definition: cprintf.c:708
_DATA_ACCESS int _SYSMEM_SIZE
unsigned int uint32_t
Definition: stdint.h:47
void config_std_output(void *output)
configure standard output stream
Definition: Common.c:160
void LED_init()
Definition: BoardSpecific.c:1270
void BoardAppInit(uint32_t dsp_clock)
board specific initialization
Definition: BoardSpecific.c:231
void KEY_init(void)
Definition: BoardSpecific.c:1358
uint32_t GetFreeHeapSize(void)
determine free heap size
Definition: Common.c:1147
void PrintExitCode(void)
Show a descriptive error message on exit.
Definition: Common.c:172
void AppCleanup ( void  )
Parameters
-
Returns
nothing

Application Cleanup Code at exit
This function is called on exit to cleanup running processes like interrupts and DMA, flush opened files, close handles

Examples:
FTPServer.c.
291 {
292 
293  /***************************************************************************
294  close all peripherals
295  ***************************************************************************/
297 
298  /***************************************************************************
299  disable interrupt
300  ***************************************************************************/
301  _disable_interrupts();
302 }
void BoardPeripheralClose(void)
close board specific peripherals
Definition: BoardSpecific.c:304
uint32_t progress ( uint32_t  done,
uint32_t  left,
uint32_t  bad 
)
894 {
895  /***************************************************************************
896  locals
897  ***************************************************************************/
898  static uint32_t old = UINT32_C(101);
899  int32_t temp;
900  int32_t total = (int32_t)done + (int32_t)left;
901 
902  if (total==INT32_C(0))
903  {
904  old = UINT32_C(101);
905  return (UINT32_C(0)); // prevent division by zero
906  }
907  temp = (int32_t)(done *100.0 / (float)(total));
908  if ( old != temp )
909  {
910  old = temp;
911  _out_stream ("\b\b\b\b\b\b %3d %%", old );
912  if ( old == INT32_C(100) )
913  {
914  temp = (int32_t)bad;
915  if ( temp > 0 ) _out_stream (" - %d bad blocks - ", bad);
916  old = UINT32_C(101);
917  } // if
918  } // if
919 
920  return (UINT32_C(1));
921 }
#define INT32_C(value)
Definition: stdint.h:209
#define UINT32_C(value)
Definition: stdint.h:210
unsigned int uint32_t
Definition: stdint.h:47
int int32_t
Definition: stdint.h:46
static tpOutputFunc _out_stream
Definition: Common.c:142
char* num2str ( int32_t  num,
char *  buffer,
int32_t  min,
int  hexmode 
)
Parameters
num- number to convert
*buffer- pointer to string buffer
min- reserved number of digits in string, 0 means automatic
hexmode- 1: switch to hex mode
Returns
pointer to string buffer
1004 {
1005  /***************************************************************************
1006  Locals
1007  ***************************************************************************/
1008  char* s = buffer;
1009  char const digit[] = "0123456789abcdef";
1010  uint32_t shifter;
1011  int base = hexmode?16:10;
1012  uint32_t i = (uint32_t)num;
1013 
1014  /***************************************************************************
1015  process sign and build absolute value, if not in hex mode
1016  ***************************************************************************/
1017  if ( ( !hexmode ) && ( num < INT32_C(0) ) )
1018  {
1019  s++;
1020  i = (uint32_t)(num * -1);
1021  } // if
1022 
1023  /***************************************************************************
1024  move to end
1025  ***************************************************************************/
1026  shifter = i;
1027  do
1028  {
1029  ++s;
1030  } while ( shifter /= base );
1031 
1032  /***************************************************************************
1033  check reserved number of digits if not zero
1034  ***************************************************************************/
1035  if ( min )
1036  {
1037  if ( (s-buffer) < min )
1038  {
1039  s = buffer + min;
1040  } // if
1041  } // if
1042 
1043  /***************************************************************************
1044  convert to string
1045  ***************************************************************************/
1046  *s = '\0';
1047  do
1048  {
1049  *--s = digit[i%base];
1050  } while ( i /= base );
1051 
1052  /***************************************************************************
1053  insert sign
1054  ***************************************************************************/
1055  if ( ( !hexmode ) && ( num < INT32_C(0) ) )
1056  {
1057  *--s = '-';
1058  } // if
1059 
1060  /***************************************************************************
1061  fill in leading blanks, if necessary
1062  ***************************************************************************/
1063  while ( s > buffer )
1064  {
1065  *--s = ' ';
1066  } // if
1067 
1068 
1069  return (buffer);
1070 }
#define INT32_C(value)
Definition: stdint.h:209
unsigned int uint32_t
Definition: stdint.h:47
static char buffer[100]
Definition: blocksend.c:158
_CODE_ACCESS void memmap ( uint32_t  heap_mem_size)

This function is useful to show memory fragmentation and to detect memory leaks

Parameters
heap_mem_size- configured heap size
Returns
-
Note
Taken from TI's RTS source
1088 {
1089  /***************************************************************************
1090  Locals
1091  ***************************************************************************/
1092  PACKET *current;
1093  PACKET *heap_end;
1094  uint32_t free_block_num = UINT32_C(0);
1095  uint32_t free_block_space = UINT32_C(0);
1096  uint32_t free_block_max = UINT32_C(0);
1097  uint32_t used_block_num = UINT32_C(0);
1098  uint32_t used_block_space = UINT32_C(0);
1099  uint32_t used_block_max = UINT32_C(0);
1100  volatile uint32_t size;
1101  volatile int used;
1102  current = (PACKET *)&_sys_memory;
1103  heap_end = HEAP_END(current,heap_mem_size);
1104 
1105  /*-----------------------------------------------------------------------*/
1106  /* LOOP THROUGH ALL PACKETS */
1107  /*-----------------------------------------------------------------------*/
1108  while (current < heap_end)
1109  {
1110  size = (uint32_t)(SIZE_ACTUAL(current->packet_size));
1111  used = (int)(SIZE_IS_USED(current->packet_size));
1112 
1113  _out_stream (">> Used:%d size:%"PRId32" addr:%"PRIx32"\r\n", used, size, (char*)current+ BLOCK_OVERHEAD);
1114 
1115  if (used)
1116  {
1117  used_block_num++;
1118  used_block_space += size;
1119  used_block_max = MAX(used_block_max, size);
1120  }
1121  else
1122  {
1123  free_block_num++;
1124  free_block_space += size;
1125  free_block_max = MAX(free_block_max, size);
1126  }
1127  current = (PACKET *)((char *)current + size + BLOCK_OFFSET);
1128  }
1129 
1130  _out_stream ("fr_nm:%"PRIx32" fr_sp:%"PRIx32" fr_mx:%"PRIx32" us_nm:%"PRIx32" us_sp:%"PRIx32" us_mx:%"PRIx32" ovr:%"PRIx32"\r\n",
1131  free_block_num, free_block_space, free_block_max,
1132  used_block_num, used_block_space, used_block_max,
1133  (free_block_num + used_block_num) * BLOCK_OVERHEAD);
1134 }
#define BLOCK_OVERHEAD
Definition: Common.h:245
unsigned int packet_size
Definition: Common.h:254
#define UINT32_C(value)
Definition: stdint.h:210
#define SIZE_IS_USED(x)
Definition: Common.h:249
PACKET _sys_memory
Definition: Common.h:252
unsigned int uint32_t
Definition: stdint.h:47
#define MAX(a, b)
Definition: Common.h:160
#define SIZE_ACTUAL(x)
Definition: Common.h:248
#define BLOCK_OFFSET
Definition: Common.h:246
static tpOutputFunc _out_stream
Definition: Common.c:142
#define HEAP_END(x, y)
Definition: Common.h:250
uint32_t GetFreeHeapSize ( void  )
Parameters
-
Returns
free heap size

This function determines the largest free heap block

Parameters
-
Returns
free heap size
Examples:
DHCPTest.c.
1148 {
1149  /***************************************************************************
1150  locals
1151  ***************************************************************************/
1152  uint32_t sz = UINT32_C(1);
1153  void* p;
1154  int m = 0, i;
1155 
1156  for (;;)
1157  {
1158  p = malloc (sz);
1159  if ( p == NULL )
1160  break;
1161  free (p);
1162  m++;
1163  sz <<= 1;
1164  }
1165 
1166  if ( !m )
1167  return (0);
1168 
1169  m--;
1170  sz = ((uint32_t)1) << m;
1171 
1172  for ( i=m-1; i>=0; i--)
1173  {
1174  uint32_t s = ((uint32_t)1) << i;
1175  p = malloc (sz | s);
1176  if ( p != NULL )
1177  {
1178  free (p);
1179  sz |= s;
1180  }
1181  }
1182  return (sz);
1183 }
#define NULL
Definition: net.h:126
#define UINT32_C(value)
Definition: stdint.h:210
unsigned int uint32_t
Definition: stdint.h:47

Variable Documentation

_DATA_ACCESS int _SYSMEM_SIZE
exit_code

Variable to hold exit code

exit codes => system error codes to signal fatal errors

See also
prg_exit(), exit_string, PrintExitCode()
Examples:
BoardSpecific.c.
const char* exit_string

exit string => global variable set in prg_exit() to the current exit message

See also
prg_exit(), exit_code, PrintExitCode()
char* exit_file

exit file => global variable set in prg_exit() to the current exit file

See also
prg_exit(), exit_code, PrintExitCode()
uint32_t exit_line

exit line => global variable set in prg_exit() to the current exit line

See also
prg_exit(), exit_code, PrintExitCode()
const char* FRMessages[]

function return messages

Examples:
SMTP.c.
PACKET _sys_memory
_DATA_ACCESS int _memory_size