Measuring time from dsPIC powerup

Thread Starter

FrenchEdwood

Joined Nov 20, 2017
1
Hi all,

It's my first query here so I'll try to break a minimum of posting rules.

I have a Embedded programming test for a job application which consists in creating a getTime function. Here is the context :

- a project code is created on a dsPIC33EP512MU814 microcontroller (with a 16MHz quartz) based on an custom API from the company :

Code:
#include "System.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
// HARDWARE CONFIG
///////////////////////////////////////////////////////////////////////////////////////////////////
// Config hardware boot (mask are only AND defined)
#if defined(__dsPIC33EP512MU810__) || defined(__dsPIC33EP512MU814__)
_FGS(GWRP_OFF & GSS_OFF & GSSK_OFF);
_FOSCSEL(FNOSC_FRC & IESO_ON);
_FOSC(POSCMD_HS & OSCIOFNC_ON & IOL1WAY_OFF & FCKSM_CSECME);
_FWDT(FWDTEN_OFF & WINDIS_OFF & PLLKEN_ON);
_FPOR(FPWRT_PWR1 & BOREN_OFF);
_FICD(ICS_PGD1 & RSTPRI_PF & JTAGEN_OFF);
_FAS(AWRP_OFF & APL_OFF & APLK_OFF);
#else
#error processeur non supporté
#endif

///////////////////////////////////////////////////////////////////////////////////////////////////
// RAM allocation
///////////////////////////////////////////////////////////////////////////////////////////////////
/// structure contenant toutes les variables du système
System sys __attribute__((__near__));
///////////////////////////////////////////////////////////////////////////////////////////////////
// SYSTEM boot
///////////////////////////////////////////////////////////////////////////////////////////////////
int8 systemPowerUp() {
  // switch frequency => quartz 16Mhz
  PLLFBD = 15 - 2;
 CLKDIVbits.PLLPOST = 0;
  CLKDIVbits.PLLPRE = 0;
  OSCTUN = 0;
  RCONbits.SWDTEN = 0;
 __builtin_write_OSCCONH(0x03); // Initiate Clock Switch to Primary Oscillator with PLL (XTPLL, HSPLL, ECPLL)
 __builtin_write_OSCCONL(0x01); // Start clock switching while (OSCCONbits.COSC != 0b001);
  while (OSCCONbits.COSC != 0x03); // Wait for Clock switch to occur
 while (OSCCONbits.LOCK != 1);
  
  // Watchdog disabled
  RCONbits.SWDTEN = 0;
  //enable multi level interrupt
  INTCON1bits.NSTDIS = 0;
  // disable analog pin
  ANSELA = 0;
  ANSELB = 0;
  ANSELC = 0;
  ANSELD = 0;
  ANSELE = 0;
  ANSELG = 0;
  return RETURN_SUCCESS;
}//powerUpSequence

///////////////////////////////////////////////////////////////////////////////////////////////////
// SYSTEM initialisation
///////////////////////////////////////////////////////////////////////////////////////////////////
int8 systemInit() {
  
  return RETURN_SUCCESS;
}//systemInit
///////////////////////////////////////////////////////////////////////////////////////////////////
// SYSTEM starting
///////////////////////////////////////////////////////////////////////////////////////////////////
uint16 cpt;
int8 systemStart() {
 cpt = 0;
 return RETURN_SUCCESS;
}//systemStart
///////////////////////////////////////////////////////////////////////////////////////////////////
// SYSTEM main loop
///////////////////////////////////////////////////////////////////////////////////////////////////
int8 systemLoop() {
 if (!cpt)
 {
 Time time = getTime();
 }
 cpt++;
 return RETURN_SUCCESS;
}//systemLoop

///////////////////////////////////////////////////////////////////////////////////////////////////
// SYSTEM shutdown
///////////////////////////////////////////////////////////////////////////////////////////////////
int8 systemShutDown() {
  return RETURN_SUCCESS;
}//systemShutDown
This code basically sets up the system and calls on and on a getTime function. Note that the "time" type is uint8 : unsigned char.

- I have a few files from this API ( handling main loop, peripheral includes and type definitions) and the peripherals Library for this microcontroller.

I have to write the getTime time handling function in this API with some constraints :
- time t=0 corresponds to the system powerup
- it's a dating function that can be called a great number of times from project code or peripherals.
- 1us per LSB in resolution
- 100us of maximum dating error between the getTime call and the value return.
- 10h of maximal use of the system.

My problem is that I can't figure out how to respect all these constraints : the resolution forbids the use of the RTC I guess, and using a Timer may not be relevant as we have to count from the powerup. And 10h of system use makes the "time" type too small for this counting

I am not familiar with all the peripherals of this device and I only made a few programs in embedded systems.

I am just looking for a starting point, an idea or a useful ressource I missed somewhere.

Thanks in advance.
 
Top