0

I'm not really that familiar with C so please bear with me...

I'm writing some simple code for a watchdog timer on an Atmel Tiny 85, developing in AtmelStudio. I want to access the clock() function of the time.h library:

#include <time.h>

.....

void main() {
  clock_t start = clock();
  ....
}

Unfortunately the compiler complains that clock is an undefined reference. Every code example I have looked up on the web seems to be doing what I am doing. Is there some fundamental thing I am missing? Thanks!

Here is the full code:

#define F_CPU 1000000UL // 1 MHz
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <time.h>
#include <stdio.h>
#include <avr/wdt.h>
#include <stdint.h>
#include <util/atomic.h>

#define LED_PORT PB1
#define RST0_PORT PINB3
#define RST1_PORT PINB4
#define HEARTBEAT PINB0

volatile int HEARTBEAT_RECEIVED;
#define CLOCKS_PER_SECOND 1000000;
const int TIMEOUT_PERIOD = 120; //reset raspberry pi after this many seconds

void set_output();
void set_input();
void set_interrupts();
void run_timer(clock_t start);
void restart_raspberry_pi();

int main(void)
{
    clock_t start;

    HEARTBEAT_RECEIVED = false;

    //Configure IO pins
    set_output();
    set_input();
    PORTB &= ~(1 << RST0_PORT); // Set pin low (common ground pin for pi reset)
    PORTB &= ~(1 << RST1_PORT); // Set pin low (initialize for no pi reset)

    //Configure and enable interrupts
    set_interrupts();

    while (1) {
        sei();
        start = clock();
        run_timer(start);
    }

    return (0);
}


void run_timer(clock_t start) {

    double time_elapsed;

    do {
        _delay_ms(1000);

        if (HEARTBEAT_RECEIVED) { //If heartbeat detected, reset timer.
            HEARTBEAT_RECEIVED = false;
            return;
        }

        time_elapsed = ( clock() - start ) / CLOCKS_PER_SECOND;
    } while (time_elapsed < TIMEOUT_PERIOD);

    restart_raspberry_pi(); //Timeout period has elapsed, reset the pi
}

ISR(PCINT0_vect) {
    //Indicate that a heartbeat has been received
    HEARTBEAT_RECEIVED = true;
}

void restart_raspberry_pi() {
    cli();

    PORTB |= (1 << RST1_PORT); // Set pin high (sets RUN high to reset)
    _delay_ms(500);
    PORTB &= ~(1 << RST1_PORT); // Set pin low (release reset control)
}

void set_output()
{
    //The DDxn bit in the DDRx Register selects the direction of this pin.
    //If DDxn is written logic one, Pxn is configured as an output pin.
    //If DDxn is written logic zero, Pxn is configured as an input pin.

    //PORTB = (0<<PB0) | (1<<PB3);
    DDRB = (1<<DDB5) | (1<<DDB4) | (1<<DDB3) | (1<<DDB2) | (1<<DDB1); // Set pins as output.
}

void set_input()
{
    DDRB |= (0<<DDB0); // set pin 0 as input. Added |=.
}

void set_interrupts()
{
    GIMSK |= (1 << PCIE);   // pin change interrupt enable
    PCMSK |= (1 << PCINT0); // pin change interrupt enabled for PCINT0
}
  • 1
    Please post a MCVE. Are there any other compiler messages? – M.M Dec 12 '18 at 01:51
  • 1
    Is this a freestanding embedded environment that might not have support for all of the standard C library? – Shawn Dec 12 '18 at 02:05
  • I have added the full code for reference. There are no other compiler messages at this point, just this specific issue. I believe AtmelStudio supports time.h clock(). I haven't seen anything to suggest otherwise. @KenWhite thanks for the link, it's a lot to go through but I'm reading through it to see if any of it helps me. – Stephen Lam Dec 12 '18 at 02:14
  • regarding: `void main() {` There are only two valid signatures for `main()` they are: `int main( void )` and `int main( int argc, char *argv[] )` – user3629249 Dec 12 '18 at 02:38
  • regarding: `DDRB |= (0< – user3629249 Dec 12 '18 at 02:46

1 Answers1

1

@Shawn had it right....I dove into the time.h library code available to me and apparently:

Section 7.23.2.1 clock() The type clock_t, the macro CLOCKS_PER_SEC, and the function clock() are not implemented. We consider these items belong to operating system code, or to application code when no operating system is present.

So I guess that's a lesson for me. Thanks for the help.