X
RSS YouTube LinkedIn Twitter XCore IRC

Search




Post new topic Reply to topic   [ 6 posts ] 
Author Message
PostPosted: Sun Mar 11, 2012 5:49 pm 
User avatar
Member++
Joined: Wed Dec 28, 2011 4:15 pm
Posts: 29
Hello,

I'm trying to use a C/C++ library in my xmos project with an XK-1A. The #include call of the time.h library works very well, but the __XC__ preprocessed variable is defined and unables me to access to time.h generic functions.

My code is the following

Code:
/*
 * ============================================================================
 * Name        : uart-loopback.xc
 * Description : UART loopback example
 * ============================================================================
 */

#include <xs1.h>
#include <print.h>
#include <platform.h>
#include <time.h>

#define BIT_RATE 115200
//#define BIT_RATE 9600
#define BIT_TIME XS1_TIMER_HZ / BIT_RATE

void txBytes(out port txd, char bytes[], int numBytes);
void txByte(out port txd, int byte);
void rxBytes(in port rxd, char bytes[], int numBytes);
char rxByte(in port rxd);


out port txd = XS1_PORT_1H; // xd23
in port rxd = XS1_PORT_1G; // xd22

int main()
{
   char transmit[] = "HelloWorld\n";
   char receive[] = { 0, 0, 0 };

   // Drive port high (inactive) to begin
   txd <: 1;

   par {
      while(1){
         txBytes(txd, transmit, sizeof(transmit));
         //printstr(transmit);
         //rxBytes(rxd, receive, sizeof(receive));
      }
   }
   
   return 0;
}

void txBytes(out port txd, char bytes[], int numBytes)
{
   for (int i = 0; i < numBytes; i += 1) {
      txByte(txd, bytes[i]);
   }
}

void txByte(out port txd, int byte)
{
   unsigned time;

   // Output start bit
   txd <: 0 @ time; // Endpoint A

   // Output data bits
   for (int i = 0; i < 8; i++) {
      time += BIT_TIME;
      txd @ time <: >> byte; // Endpoint B
   }

   // Output stop bit
   time += BIT_TIME;
   txd @ time <: 1; // Endpoint C

   // Hold stop bit
   time += BIT_TIME;
   txd @ time <: 1; // Endpoint D
}

void rxBytes(in port rxd, char bytes[], int numBytes)
{
   for (int i = 0; i < numBytes; i += 1) {
      bytes[i] = rxByte(rxd);
   }
}

char rxByte(in port rxd)
{
   unsigned byte, time;

   // Wait for start bit
   rxd when pinseq (0) :> void @ time;
   time += BIT_TIME / 2;
   
   // Input data bits
   for (int i = 0; i < 8; i++) {
      time += BIT_TIME;
      rxd @ time :> >> byte;
   }

   // Input stop bit
   time += BIT_TIME;
   rxd @ time :> void;

   return (byte >> 24);
}


Now, in the time.h file, __XC__ is defined and unables me to access the generic functions as follow

Code:
/*
 * time.h
 *
 * Struct and function declarations for dealing with time.
 */

#ifndef _TIME_H_
#define _TIME_H_

#include "_ansi.h"

#ifndef __XC__

#include <sys/reent.h>

#ifndef NULL
#define   NULL   0
#endif

#endif /* __XC__ */

/* Get _CLOCKS_PER_SEC_ */
#include <machine/time.h>

#ifndef _CLOCKS_PER_SEC_
#define _CLOCKS_PER_SEC_ 1000
#endif

#define CLOCKS_PER_SEC _CLOCKS_PER_SEC_
#define CLK_TCK CLOCKS_PER_SEC
#define __need_size_t
#include <stddef.h>

#include <sys/types.h>

#ifndef __XC__

_BEGIN_STD_C

struct tm
{
  int   tm_sec;
  int   tm_min;
  int   tm_hour;
  int   tm_mday;
  int   tm_mon;
  int   tm_year;
  int   tm_wday;
  int   tm_yday;
  int   tm_isdst;
};

clock_t      _EXFUN(clock,    (void));
double      _EXFUN(difftime, (time_t _time2, time_t _time1));
time_t      _EXFUN(mktime,   (struct tm *_timeptr));
time_t      _EXFUN(time,     (time_t *_timer));
char     *_EXFUN(asctime,  (const struct tm *_tblock));
char     *_EXFUN(ctime,    (const time_t *_time));
struct tm *_EXFUN(gmtime,   (const time_t *_timer));
struct tm *_EXFUN(localtime,(const time_t *_timer));
size_t      _EXFUN(strftime, (char *_s, size_t _maxsize, const char *_fmt, const struct tm *_t));

char     *_EXFUN(asctime_r,   (const struct tm *, char *));
char     *_EXFUN(ctime_r,   (const time_t *, char *));
struct tm *_EXFUN(gmtime_r,   (const time_t *, struct tm *));
struct tm *_EXFUN(localtime_r,   (const time_t *, struct tm *));

_END_STD_C

#ifdef __cplusplus
extern "C" {
#endif

#ifndef __STRICT_ANSI__
char      *_EXFUN(strptime,     (const char *, const char *, struct tm *));
_VOID      _EXFUN(tzset,   (_VOID));
_VOID      _EXFUN(_tzset_r,   (struct _reent *));

typedef struct __tzrule_struct
{
  char ch;
  int m;
  int n;
  int d;
  int s;
  time_t change;
  long offset; /* Match type of _timezone. */
} __tzrule_type;

typedef struct __tzinfo_struct
{
  int __tznorth;
  int __tzyear;
  __tzrule_type __tzrule[2];
} __tzinfo_type;

__tzinfo_type *_EXFUN (__gettzinfo, (_VOID));

/* getdate functions */

#ifdef HAVE_GETDATE
#define getdate_err (*__getdate_err())
int *_EXFUN(__getdate_err,(_VOID));

struct tm *   _EXFUN(getdate, (const char *));
/* getdate_err is set to one of the following values to indicate the error.
     1  the DATEMSK environment variable is null or undefined,
     2  the template file cannot be opened for reading,
     3  failed to get file status information,
     4  the template file is not a regular file,
     5  an error is encountered while reading the template file,
     6  memory allication failed (not enough memory available),
     7  there is no line in the template that matches the input,
     8  invalid input specification  */

/* getdate_r returns the error code as above */
int      _EXFUN(getdate_r, (const char *, struct tm *));
#endif /* HAVE_GETDATE */

/* defines for the opengroup specifications Derived from Issue 1 of the SVID.  */
extern __IMPORT long _timezone;
extern __IMPORT int _daylight;
extern __IMPORT char *_tzname[2];

/* POSIX defines the external tzname being defined in time.h */
#ifndef tzname
#define tzname _tzname
#endif
#endif /* !__STRICT_ANSI__ */

#ifdef __cplusplus
}
#endif

#include <sys/features.h>

#ifdef __CYGWIN__
#include <cygwin/time.h>
#endif /*__CYGWIN__*/

#if defined(_POSIX_TIMERS)

#include <signal.h>

#ifdef __cplusplus
extern "C" {
#endif

/* Clocks, P1003.1b-1993, p. 263 */

int _EXFUN(clock_settime, (clockid_t clock_id, const struct timespec *tp));
int _EXFUN(clock_gettime, (clockid_t clock_id, struct timespec *tp));
int _EXFUN(clock_getres,  (clockid_t clock_id, struct timespec *res));

/* Create a Per-Process Timer, P1003.1b-1993, p. 264 */

int _EXFUN(timer_create,
  (clockid_t clock_id, struct sigevent *evp, timer_t *timerid));

/* Delete a Per_process Timer, P1003.1b-1993, p. 266 */

int _EXFUN(timer_delete, (timer_t timerid));

/* Per-Process Timers, P1003.1b-1993, p. 267 */

int _EXFUN(timer_settime,
  (timer_t timerid, int flags, const struct itimerspec *value,
   struct itimerspec *ovalue));
int _EXFUN(timer_gettime, (timer_t timerid, struct itimerspec *value));
int _EXFUN(timer_getoverrun, (timer_t timerid));

/* High Resolution Sleep, P1003.1b-1993, p. 269 */

int _EXFUN(nanosleep, (const struct timespec  *rqtp, struct timespec *rmtp));

#ifdef __cplusplus
}
#endif
#endif /* _POSIX_TIMERS */

#ifdef __cplusplus
extern "C" {
#endif

/* CPU-time Clock Attributes, P1003.4b/D8, p. 54 */

/* values for the clock enable attribute */

#define CLOCK_ENABLED  1  /* clock is enabled, i.e. counting execution time */
#define CLOCK_DISABLED 0  /* clock is disabled */

/* values for the pthread cputime_clock_allowed attribute */

#define CLOCK_ALLOWED    1 /* If a thread is created with this value a */
                           /*   CPU-time clock attached to that thread */
                           /*   shall be accessible. */
#define CLOCK_DISALLOWED 0 /* If a thread is created with this value, the */
                           /*   thread shall not have a CPU-time clock */
                           /*   accessible. */

/* Manifest Constants, P1003.1b-1993, p. 262 */

#define CLOCK_REALTIME (clockid_t)1

/* Flag indicating time is "absolute" with respect to the clock
   associated with a time.  */

#define TIMER_ABSTIME   4

/* Manifest Constants, P1003.4b/D8, p. 55 */

#if defined(_POSIX_CPUTIME)

/* When used in a clock or timer function call, this is interpreted as
   the identifier of the CPU_time clock associated with the PROCESS
   making the function call.  */

#define CLOCK_PROCESS_CPUTIME (clockid_t)2

#endif

#if defined(_POSIX_THREAD_CPUTIME)

/*  When used in a clock or timer function call, this is interpreted as
    the identifier of the CPU_time clock associated with the THREAD
    making the function call.  */

#define CLOCK_THREAD_CPUTIME (clockid_t)3

#endif

#if defined(_POSIX_CPUTIME)

/* Accessing a Process CPU-time CLock, P1003.4b/D8, p. 55 */

int _EXFUN(clock_getcpuclockid, (pid_t pid, clockid_t *clock_id));

#endif /* _POSIX_CPUTIME */

#if defined(_POSIX_CPUTIME) || defined(_POSIX_THREAD_CPUTIME)

/* CPU-time Clock Attribute Access, P1003.4b/D8, p. 56 */

int _EXFUN(clock_setenable_attr, (clockid_t clock_id, int attr));
int _EXFUN(clock_getenable_attr, (clockid_t clock_id, int *attr));

#endif /* _POSIX_CPUTIME or _POSIX_THREAD_CPUTIME */

#ifdef __cplusplus
}
#endif

#endif /* __XC__ */

#endif /* _TIME_H_ */



I would like to use the time() function call. How could that be possible in an xmos project?


Last edited by mifay on Sun Mar 11, 2012 7:04 pm, edited 1 time in total.

Top
Offline Profile View all posts by this user  
 
PostPosted: Sun Mar 11, 2012 6:00 pm 
User avatar
XCore Moderator
Joined: Thu Dec 10, 2009 6:56 pm
Posts: 545
The time function call requires a pointer to a time_t variable, but xc does not support pointers.
One way to deal with it is write a wrapper function in C that can be called from XC.


Top
Offline Profile View all posts by this user  
 
PostPosted: Sun Mar 11, 2012 7:03 pm 
User avatar
Member++
Joined: Wed Dec 28, 2011 4:15 pm
Posts: 29
Do you know another alternative to get the current time besides using time.h?


Top
Offline Profile View all posts by this user  
 
PostPosted: Sun Mar 11, 2012 7:39 pm 
User avatar
XCore Moderator
Joined: Thu Dec 10, 2009 6:56 pm
Posts: 545
You can use a hardware timer in XC (i believe this is exactly what the time lib does, since there is no "system time").

In XC this would be for example:

Code:
unsigned int get_the_time()
{
  timer t;
  unsigned int time;
  t :> time;
  return time;
}


This will read the current timer value of a 32-bit hardware timer (each core has 8 hardware timers)
The resolution by default is 10ns (100MHz).

There is quite some overhead by using a function, if you need better accuracy or need to get the timer value a lot it is better to implement the above code in your function which avoids allocating and deallocating the timer resource (and function call overhead) for every timer read.

Please note that at 100MHz the timer wraps every ~42 seconds.


Top
Offline Profile View all posts by this user  
 
PostPosted: Sun Mar 11, 2012 9:29 pm 
User avatar
Member++
Joined: Wed Dec 28, 2011 4:15 pm
Posts: 29
Thanks a lot! This should do the work.

My collegue also suggested me to use this code on p19 of the XC Programming Guide for delays and such

Code:
# include <xs1.h>
# define DELAY 50000000
out port p = XS1_PORT_1A ;
int main ( void ) {
unsigned state = 1, time ;
timer t;
t :> time ;
while (1) {
p <: state ;
time += DELAY ;
t when timerafter ( time ) :> void ;
state = ! state ;
}
}


Top
Offline Profile View all posts by this user  
 
PostPosted: Sun Mar 11, 2012 9:38 pm 
User avatar
XCore Moderator
Joined: Thu Dec 10, 2009 6:56 pm
Posts: 545
Yes the timerafter construct is very useful for delays


Top
Offline Profile View all posts by this user  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic   [ 6 posts ] 


Who is online

Users browsing this forum: No registered users and 1 guest



Search for:
Jump to: