send.c File Reference

UDP send test program. More...

#include <BoardSupport/inc/stdtypes.h>
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <inttypes.h>
#include <time.h>
#include <string.h>
#include <Libs/NETlib/net.h>
#include <BoardSupport/inc/BoardSpecific.h>
#include <Common/Common.h>
#include <Common/uartio.h>
#include <Common/timer.h>
#include <Common/CPrintf.h>
#include <BoardSupport/config/netconfig.c>

Macros

#define PC_IP_ADDR   "192.168.168.255" /* activate, if you want to send broadcast messages*/
 
#define PC_PORT   5062
 
#define DSP_PORT   5032
 

Functions

void SendTimeStamp (void)
 
int main (void)
 

Variables

char * program_name = "send"
 
SOCKET * udp_socket
 
char udp_data [12]
 

Detailed Description

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

This is a simple DSP program for sending a time stamp to the specified pc address and port using UDP. For proper function you have to specify your PC IP- address by definition:

 #define PC_IP_ADDR      "192.168.168.8"

Or if you want to send broadcast messages use

 #define PC_IP_ADDR      BROADCAST

Macro Definition Documentation

#define PC_IP_ADDR   "192.168.168.255" /* activate, if you want to send broadcast messages*/
Examples:
Send.c.
#define PC_PORT   5062
Examples:
Send.c.
#define DSP_PORT   5032

Function Documentation

void SendTimeStamp ( void  )
Examples:
Send.c.
165 {
166  /***************************************************************************
167  locals
168  ***************************************************************************/
169  //~ char udp_data[12]; /* !!data buffer must not reside on the stack !! */
170  int32_t ret;
171  struct tm *local_time; /* structure to hold time and date */
172 
173  if ( udp_socket-> error_code || (get_ip_address (0) == UINT32_C(0)) || !_link)
174  {
175  /***********************************************************************
176  in case of errors or no link do nothing
177  ***********************************************************************/
178  return;
179  } // if
180 
181  /***************************************************************************
182  generate a new time stamp each second
183  ***************************************************************************/
184  local_time = localtime ((const time_t *)&RecentEpoch);
185 
186  /***************************************************************************
187  fill send buffer with time stamp data
188  ***************************************************************************/
189  udp_data[0] = (local_time->tm_hour / 10) + '0';
190  udp_data[1] = (local_time->tm_hour % 10) + '0';
191  udp_data[3] = (local_time->tm_min / 10) + '0';
192  udp_data[4] = (local_time->tm_min % 10) + '0';
193  udp_data[6] = (local_time->tm_sec / 10) + '0';
194  udp_data[7] = (local_time->tm_sec % 10) + '0';
195 
196  /***************************************************************************
197  send time stamp to PC
198  ***************************************************************************/
199  ret = net_send (udp_socket, /* socket */
200  udp_data, /* data to send */
201  10); /* length of data */
202  if (ret == INT32_C(0))
203  {
204  if (( udp_socket-> error_code & SO_ERROR_MASK) == SO_NO_ARP_ADDRESS)
205  {
206  net_print_error (udp_socket-> error_code, CPrintf);
207  // Destination Address not resolved. Packet was passed to the TCP/IP stack
208  // and will be transmitted automatically when address resolution
209  // in background was successful. This happens always with the first packet
210  // to a new destination or when the ARP cache is cleared after a certain time.
211  // Make sure to trigger the net_isq() for address resolution.
212  // Note: don't change data content until this packet was transmitted
213  // by the TCP/IP stack. This constraint exists due to the zero-copy mechanism;
214  // the stack needs to access the data pointer later.
215  // This is also the reason for the globally defined array udp_data[]. If this
216  // buffer would have been defined locally, the data content is invalid after
217  // return from this function, since all local data is stored on the stack
218  }
219  }
220 }
char udp_data[12]
Definition: send.c:147
volatile uint16_t _link
Definition: BoardSpecific.c:143
#define INT32_C(value)
Definition: stdint.h:209
#define SO_ERROR_MASK
Definition: net.h:480
#define UINT32_C(value)
Definition: stdint.h:210
int CPrintf(const char *_format,...)
Custom printf function.
Definition: cprintf.c:708
SOCKET * udp_socket
Definition: send.c:145
timeval RecentEpoch
Definition: timer.c:81
int32_t net_send(SOCKET *so, void *data, uint16_t len)
Send a message via the specified socket.
uint32_t get_ip_address(uint16_t dev_nr)
Get configured IP address.
#define SO_NO_ARP_ADDRESS
Definition: net.h:463
int int32_t
Definition: stdint.h:46
int main ( void  )
231 {
232  /***************************************************************************
233  locals
234  ***************************************************************************/
235  int main_loop = 1; /* main loop switch, set to 0 to exit */
236  timeval stamp1, stamp2, delta; /* used to determine startup time */
237  time_t seconds;
238  char buffer[20]; /* small buffer for ip-address conversion */
239  struct tm *local_time; /* structure to hold time and date */
240 
241  /***************************************************************************
242  initialize application (e.g. timer clocks, PLL settings, EMIF etc.)
243  (ref. \Common\Common.c)
244  ***************************************************************************/
245  AppInit (GET_CLOCK);
246 
247  /***************************************************************************
248  select output device for CPrintf (ref. \Common\cprintf.c)
249  possible settings:
250  CPRINTF_UART_OUTPUT -> output to UART
251  CPRINTF_CCS_OUTPUT -> output to CCS
252  CPRINTF_UART_OUTPUT | CPRINTF_CCS_OUTPUT -> output to UART and CCS
253  ***************************************************************************/
254  CPrintf_select_output (CPRINTF_DEFAULT_OUTPUT); /* default outputs */
255 
256  /***************************************************************************
257  print a start up message
258  ***************************************************************************/
259  START_UP_MESSAGE (BLANK_REV NETLIB_REV);
260 
261  /**************************************************************************/
262  // CPrintfProgress (" Heap check ");
263  // at least 0x2000 bytes required for this app
264  /**************************************************************************/
265  ASSERT_HEAP (initial_heap_size, 0x2000);
266  // CPrintfProgressSuccess();
267 
268  /**************************************************************************/
269  CPrintfProgress (" Setup system time ");
270  // 1 milli seconds resolution
271  /**************************************************************************/
274  CPrintf (" *** timer %d mapped to CPU int %d ***\r\n",
276 
277  /**************************************************************************/
278  CPrintfProgress (" Enable interrupts ");
279  /**************************************************************************/
282 
283  /**************************************************************************/
284  CPrintfProgress (" Start system timer ");
285  /**************************************************************************/
286  StartSystemTimer ();
288  CPrintf (" *** timer %d running at %"PRId32" Hz ***\r\n", SystemTimerDev, RES_SECONDS/GetSystemTimerRes());
289 
290  /***************************************************************************
291  measure network initialization time
292  ***************************************************************************/
293  stamp1 = GetTimeStamp();
294 
295  /**************************************************************************/
296  CPrintfProgress (" Initialize network ");
297  /**************************************************************************/
298  InitializeNetwork ( 64); // 64 bytes for ping
299 
300  /***************************************************************************
301  open socket
302  ***************************************************************************/
303  udp_socket = socket_open (PC_IP_ADDR, /* fix pc address */
304  PC_PORT, /* destination port */
305  DSP_PORT, /* source port */
306  DATATYPE_CHAR, /* data type char */
307  UDP_INIT_FUNC); /* udp protocol */
308  if ( udp_socket == INVALID_SOCKET )
309  {
310  prg_exit ("socket_open() failed"); /* possibly insufficient heap */
311  } // if
312 
314 
315  stamp2 = GetTimeStamp();
316 
317  tv_interval (&delta, &stamp1, &stamp2);
318  CPuts (" network startup time [sec]: ");
320  "%"PRId32".%03"PRId32"\r\n"
322  delta.tv_sec,
323  delta.tv_usec/1000);
324 
325  udp_data[2] = ':';
326  udp_data[5] = ':';
327  udp_data[8] = '\r';
328  udp_data[9] = '\n';
329  udp_data[10] = '\0';
330 
331  CPrintf (" Sending time stamp to address %s on port %d\r\n", inet_ntoa (udp_socket->dest_addr, buffer), udp_socket->dest_port);
332  local_time = localtime ((const time_t *)&RecentEpoch);
333  CPrintf (" System start time: %s\r", asctime (local_time));
334 
335  LED_on (0);
336  LED_off (1);
337 
338  /***************************************************************************
339  main program loop: set main_loop to 0 to exit loop
340  ***************************************************************************/
341  CPuts ("\r\n Entering main loop ...\r\n");
342  while ( main_loop )
343  {
344  /***********************************************************************
345  process net_isq()
346  ***********************************************************************/
347  net_isq (); // process ISQ
348 
349  /***********************************************************************
350  monitor link status
351  ***********************************************************************/
353 
354  /***********************************************************************
355  try to detect IP assignment
356  if DHCP is used, the assigned IP address may change
357  ***********************************************************************/
359 
360  /***********************************************************************
361  since no receive function is used in this example, some error codes
362  for receive events may block the send process.
363  ***********************************************************************/
364  udp_socket -> error_code = UINT32_C(0);
365  if (seconds != RecentEpoch.tv_sec)
366  {
367  seconds = RecentEpoch.tv_sec;
368  LED_toggle (0);
369  LED_toggle (1);
370 
371  /*******************************************************************
372  send time stamp each second
373  *******************************************************************/
374  SendTimeStamp();
375  }
376 
377  /***********************************************************************
378  show that the program is running, perform symbol animation
379  ***********************************************************************/
381  }
382 
383  /***************************************************************************
384  exit program, shut down peripherals
385  ***************************************************************************/
386  return(0);
387 }
#define prg_exit(s)
Definition: Common.h:267
void BoardEnableInterrupts(void)
global enable interrupts
Definition: BoardSpecific.c:365
char udp_data[12]
Definition: send.c:147
#define ANIMATE_SYMBOLS_COUNT
Definition: BoardSpecific.h:268
void StartSystemTimer(void)
start system timer
Definition: timer.c:447
#define RES_MSECONDS
Definition: timer.h:67
char * inet_ntoa(uint32_t i_addr, char *s)
Convert IP-address from 0xbbaaddcc to "aaa.bbb.ccc.ddd".
uint16_t CPrintf_select_output(uint16_t device)
Definition: cprintf.c:206
#define ASSERT_HEAP(i, h)
Definition: Common.h:262
void SendTimeStamp(void)
Definition: send.c:164
void LED_on(unsigned int ledNum)
Definition: BoardSpecific.c:1302
time_t GetSystemTimerRes(void)
Definition: timer.c:160
#define UINT32_C(value)
Definition: stdint.h:210
#define GET_CLOCK
Definition: BoardSpecific.h:258
#define UDP_INIT_FUNC
Definition: net.h:500
int CPrintf(const char *_format,...)
Custom printf function.
Definition: cprintf.c:708
#define VT100_DEFAULT
Definition: cprintf.h:150
void SetupSystemTime(int32_t cpuint, int port, time_t resolution)
Setup System Time.
Definition: timer.c:392
void AppInit(uint32_t dsp_clock)
Initialize application.
Definition: Common.c:230
uint32_t initial_heap_size
Definition: Common.c:140
void LED_off(unsigned int ledNum)
Definition: BoardSpecific.c:1284
SOCKET * udp_socket
Definition: send.c:145
timeval RecentEpoch
Definition: timer.c:81
#define CPRINTF_DEFAULT_OUTPUT
Definition: BoardSpecific.h:129
#define DSP_PORT
Definition: send.c:129
#define VT100_RED
Definition: cprintf.h:143
uint16_t CPrintAnimatedSymbol(char *c, time_t period, size_t size)
Definition: cprintf.c:816
int InitializeNetwork(uint16_t icmp_size)
Initialize MAC, sockets and protocols.
Definition: BoardSpecific.c:597
static char buffer[100]
Definition: blocksend.c:158
char sym_animate[]
Definition: BoardSpecific.c:129
SOCKET * socket_open(char *dest_addr, uint16_t dest_port, uint16_t src_port, uint8_t data_type, int32_t(*init_func)(SOCKET *))
Create a new socket.
Definition: timer.h:75
#define GetTimeStamp()
Definition: timer.h:81
void tv_interval(timeval *e, timeval *t1, timeval *t2)
compute elapsed time
Definition: timer.c:336
void LED_toggle(unsigned int ledNum)
Definition: BoardSpecific.c:1320
#define SYSTEM_TIMER_INT
Definition: BoardSpecific.h:174
#define SYSTEM_TIMER
Definition: BoardSpecific.h:167
#define DATATYPE_CHAR
Definition: net.h:489
void net_isq(void)
The main polling function for processing sockets, must be periodically called in the main application...
#define BLANK_REV
Definition: BoardSpecific.h:191
Uint16 SystemTimerDev
#define INVALID_SOCKET
Definition: net.h:540
uint16_t monitor_ip_address(tpOutputFunc pLog)
monitor IP assignment
Definition: BoardSpecific.c:545
#define PC_PORT
Definition: send.c:128
#define START_UP_MESSAGE(rev)
Definition: BoardSpecific.h:192
#define RES_SECONDS
Definition: timer.h:70
uint16_t monitor_link_status(tpOutputFunc pLog)
monitor link status change
Definition: BoardSpecific.c:497
int CPuts(const char *_ptr)
Definition: cprintf.c:399
time_t tv_sec
Definition: timer.h:77
#define CPrintfProgress(s)
Definition: cprintf.h:230
time_t tv_usec
Definition: timer.h:78
#define PC_IP_ADDR
Definition: send.c:127
#define CPrintfProgressSuccess()
Definition: cprintf.h:263

Variable Documentation

char* program_name = "send"
SOCKET* udp_socket
char udp_data[12]