Microchip® Advanced Software Framework

Quick Start Guide for FREQM - Callback

In this use case, the Frequency Meter (FREQM) module is configured for:

  • The FREQM peripheral will not be stopped in standby sleep mode.

This use case will read measurement data in interrupt mode repeatly. After reading specific size of buffer data, the board LED will be toggled.

Setup

Prerequisites

There are no special setup requirements for this use-case.

Code

Copy-paste the following setup code to your user application:

bool volatile freqm_read_done = false;
void configure_freqm(void);
void configure_freqm_callback(void);
void freqm_complete_callback(void);
/* FREQM module software instance (must not go out of scope while in use) */
static struct freqm_module freqm_instance;
void configure_freqm(void)
{
/* Create a new configuration structure for the FREQM settings
* and fill with the default module settings. */
struct freqm_config config_freqm;
freqm_get_config_defaults(&config_freqm);
config_freqm.ref_clock_circles = 255;
/* Alter any FREQM configuration settings here if required */
/* Initialize FREQM with the user settings */
freqm_init(&freqm_instance, FREQM, &config_freqm);
}
void freqm_complete_callback(void)
{
freqm_read_done = true;
}
void configure_freqm_callback(void)
{
freqm_register_callback(&freqm_instance, freqm_complete_callback,
}

Add to user application initialization (typically the start of main()):

configure_freqm();
configure_freqm_callback();
freqm_enable(&freqm_instance);

Workflow

  1. Create an FREQM device instance struct, which will be associated with an FREQM peripheral hardware instance.
    static struct freqm_module freqm_instance;
    Note
    Device instance structures shall never go out of scope when in use.
  2. Create a new function configure_freqm(), which will be used to configure the overall FREQM peripheral.
    void configure_freqm(void)
  3. Create an FREQM peripheral configuration structure that will be filled out to set the module configuration.
    struct freqm_config config_freqm;
  4. Fill the FREQM peripheral configuration structure with the default module configuration values.
    freqm_get_config_defaults(&config_freqm);
    config_freqm.ref_clock_circles = 255;
  5. Initialize the FREQM peripheral and associate it with the software instance structure that was defined previously.
    freqm_init(&freqm_instance, FREQM, &config_freqm);
  6. Create a new callback function.
    void freqm_complete_callback(void)
    {
    freqm_read_done = true;
    }
  7. Create a callback status software flag.
    bool volatile freqm_read_done = false;
  8. Let the callback function set the flag to true when read job done.
    freqm_read_done = true;
  9. Create a new function configure_freqm_callback(), which will be used to configure the callbacks.
    void configure_freqm_callback(void)
    {
    freqm_register_callback(&freqm_instance, freqm_complete_callback,
    }
  10. Register callback function.
    freqm_register_callback(&freqm_instance, freqm_complete_callback,
  11. Enable the callbacks.
  12. Enable the now initialized FREQM peripheral.
    freqm_enable(&freqm_instance);
    Note
    This should not be done until after the FREQM is setup and ready to be used.

Implementation

Code

Copy-paste the following code to your user application:

uint32_t measure_result;
enum freqm_status status;
freqm_start_measure(&freqm_instance);
while (!freqm_read_done) {
}
status = freqm_get_result_value(&freqm_instance, &measure_result);
switch(status) {
LED_On(LED_0_PIN);
while (true) {
}
freqm_clear_overflow(&freqm_instance);
while (true) {
LED_Toggle(LED_0_PIN);
volatile uint32_t delay = 50000;
while(delay--) {
}
}
default:
Assert(false);
break;
}

Workflow

  1. Start an asynchronous FREQM read job, to store measurement data into the global buffer and generate a callback when complete.
    freqm_start_measure(&freqm_instance);
  2. Wait until the asynchronous read job is complete.
    while (!freqm_read_done) {
    }
    status = freqm_get_result_value(&freqm_instance, &measure_result);
  3. The board LED on to indicate measurement data read.
    LED_On(LED_0_PIN);
    while (true) {
    }
  4. The board LED toggled to indicate measurement overflow occous.
    freqm_clear_overflow(&freqm_instance);
    while (true) {
    LED_Toggle(LED_0_PIN);
    volatile uint32_t delay = 50000;
    while(delay--) {
    }
    }