Need help for the definition of these function

Thread Starter

lucky930

Joined Jan 7, 2024
16
Hello Guyz,
I've found below code that belongs to the timer declaration function but the definition is hidden by the author.
Can anyone help me to provide a definition for the same?

I want this to be utilized in a project related to ATMEGA8 using Atmel studio.

#ifndef TIMERS_H_
#define TIMERS_H_
#include <inttypes.h>

typedef struct timer_st_
{
volatile uint8_t expiry_flag:1;
volatile uint8_t enabled_flag:1;
uint16_t ticks_elapsed;
uint16_t ticks_total;
}timer_st;

void
reset_timer (timer_st *timer);

void
start_timer (timer_st *timer, uint16_t ticks);

void
stop_timer (timer_st *timer);

uint8_t
is_timer_expired (timer_st *timer);

uint8_t
is_timer_enabled (timer_st *timer);

void
process_timer (timer_st *timer);
 

Irving

Joined Jan 30, 2016
3,856
ChatGPT gives this body for that declaration. It looks reasonable but suggest you validate it. You'll probably need to add some hardware specific elements to reference a particular hardware timer and link it to the process timer callback.

Here's the C code to fulfill the body functions for the declaration:

Code:
```c
#include "timers.h"

void reset_timer(timer_st *timer) {
    timer->expiry_flag = 0;
    timer->ticks_elapsed = 0;
}

void start_timer(timer_st *timer, uint16_t ticks) {
    timer->ticks_total = ticks;
    timer->ticks_elapsed = 0;
    timer->expiry_flag = 0;
    timer->enabled_flag = 1;
}

void stop_timer(timer_st *timer) {
    timer->enabled_flag = 0;
}

uint8_t is_timer_expired(timer_st *timer) {
    return timer->expiry_flag;
}

uint8_t is_timer_enabled(timer_st *timer) {
    return timer->enabled_flag;
}

void process_timer(timer_st *timer) {
    if (timer->enabled_flag) {
        timer->ticks_elapsed++;
        if (timer->ticks_elapsed >= timer->ticks_total) {
            timer->expiry_flag = 1;
            timer->enabled_flag = 0;
        }
    }
}
```
These functions provide the functionality specified in the declaration for handling timers.
 

Thread Starter

lucky930

Joined Jan 7, 2024
16
ChatGPT gives this body for that declaration. It looks reasonable but suggest you validate it. You'll probably need to add some hardware specific elements to reference a particular hardware timer and link it to the process timer callback.

Here's the C code to fulfill the body functions for the declaration:

Code:
```c
#include "timers.h"

void reset_timer(timer_st *timer) {
    timer->expiry_flag = 0;
    timer->ticks_elapsed = 0;
}

void start_timer(timer_st *timer, uint16_t ticks) {
    timer->ticks_total = ticks;
    timer->ticks_elapsed = 0;
    timer->expiry_flag = 0;
    timer->enabled_flag = 1;
}

void stop_timer(timer_st *timer) {
    timer->enabled_flag = 0;
}

uint8_t is_timer_expired(timer_st *timer) {
    return timer->expiry_flag;
}

uint8_t is_timer_enabled(timer_st *timer) {
    return timer->enabled_flag;
}

void process_timer(timer_st *timer) {
    if (timer->enabled_flag) {
        timer->ticks_elapsed++;
        if (timer->ticks_elapsed >= timer->ticks_total) {
            timer->expiry_flag = 1;
            timer->enabled_flag = 0;
        }
    }
}
```
These functions provide the functionality specified in the declaration for handling timers.
Thanks, I'll try that.
 

BobaMosfet

Joined Jul 1, 2009
2,112
What you're trying to say is you have found the timer library, and you only see the declarations in the header for it, but can't see the actual code- it's opaque. This is by design. Not secrecy, just convenience.

If you want to see the actual code, write a program using those functions, and then disssassemble it- you can then see the actual assembly language code used.
 

Thread Starter

lucky930

Joined Jan 7, 2024
16
What you're trying to say is you have found the timer library, and you only see the declarations in the header for it, but can't see the actual code- it's opaque. This is by design. Not secrecy, just convenience.

If you want to see the actual code, write a program using those functions, and then disssassemble it- you can then see the actual assembly language code used.
Yes, I want the definitions.
 
Top