Common.c File Reference

Common Support Functions: Implementation More...

#include "..\includes.h"

Macros

#define INC_COMMON
 
#define COMMON_PROGRAM_HALTED   CommonMessages[0]
 
#define COMMON_EXIT   CommonMessages[1]
 
#define COMMON_BUFFER_OVERFLOW   CommonMessages[1]
 

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

const char * FRMessages []
 
uint32_t exit_code = UINT32_C(0)
 
const char * exit_string = NULL
 
char * exit_file = NULL
 
uint32_t exit_line = UINT32_C(0)
 
const char * CommonMessages []
 
uint32_t initial_heap_size = UINT32_C(0)
 
static tpOutputFunc _out_stream =printf
 

Detailed Description

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

This module contains common support functions for application start-up, clean-up on exit, descriptive error and exit strings, LED signalling.

Macro Definition Documentation

#define INC_COMMON
#define COMMON_PROGRAM_HALTED   CommonMessages[0]
#define COMMON_EXIT   CommonMessages[1]
#define COMMON_BUFFER_OVERFLOW   CommonMessages[1]

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.

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

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  )

This function determines the largest free heap block

Parameters
-
Returns
free heap size
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

const char* FRMessages[]
Initial value:
= {
VT100_GREEN" success\r\n"VT100_DEFAULT,
VT100_BLUE" skipped\r\n"VT100_DEFAULT,
VT100_BLUE" timed out\r\n"VT100_DEFAULT,
""
}
#define VT100_DEFAULT
Definition: cprintf.h:150
#define VT100_BLUE
Definition: cprintf.h:146
#define VT100_RED
Definition: cprintf.h:143
#define VT100_GREEN
Definition: cprintf.h:144

function return messages

uint32_t exit_code = UINT32_C(0)

exit codes => system error codes to signal fatal errors

See also
prg_exit(), exit_string, PrintExitCode()
const char* exit_string = NULL

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

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

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

See also
prg_exit(), exit_code, PrintExitCode()
uint32_t exit_line = UINT32_C(0)

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

See also
prg_exit(), exit_code, PrintExitCode()
const char* CommonMessages[]
Initial value:
= {
" Program halted!",
#define COMMON_PROGRAM_HALTED
VT100_RED"\r\nEXIT: %s line %"PRId32": %s\r\n"VT100_DEFAULT,
#define COMMON_EXIT
"CPrintf buffer overflow error, program halted\r\n"
#define COMMON_BUFFER_OVERFLOW
}
#define VT100_DEFAULT
Definition: cprintf.h:150
#define VT100_RED
Definition: cprintf.h:143
uint32_t initial_heap_size = UINT32_C(0)

configured heap size

tpOutputFunc _out_stream =printf
static