Microchip® Advanced Software Framework

Quick Start Guide for TSENS - Callback

In this use case, the TSENS will measure the temperature using interrupt driven conversion.

When the temperature value has been measured, a callback will be called that signals the main application that the conversion is complete.

The TSENS will be set up as follows:

  • GCLK generator 0 (GCLK main) clock source
  • Free running disabled
  • Run in standby
  • Window monitor disabled
  • All events (input and generation) disabled
  • Calibration value which read from NVM or user set

Setup

Prerequisites

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

Code

Add to the main application source file, outside of any functions:

struct tsens_module tsens_instance;
int32_t tsens_result;

Callback function:

volatile bool tsens_read_done = false;
static void tsens_complete_callback(enum tsens_callback i)
{
tsens_read_done = true;
}

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

static void configure_tsens(void)
{
struct tsens_config config_tsens;
tsens_get_config_defaults(&config_tsens);
tsens_init(&config_tsens);
}
static void configure_tsens_callbacks(void)
{
tsens_register_callback(&tsens_instance,
tsens_complete_callback, TSENS_CALLBACK_RESULT_READY);
}

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

configure_tsens();
configure_tsens_callbacks();

Workflow

  1. Create a module software instance structure for the TSENS module to store the TSENS driver state while it is in use.
    struct tsens_module tsens_instance;
    Note
    This should never go out of scope as long as the module is in use. In most cases, this should be global.
  2. Create a variable for the TSENS sample to be stored in by the driver asynchronously.
    int32_t tsens_result;
  3. Create a callback function that will be called each time the TSENS completes an asynchronous read job.
    volatile bool tsens_read_done = false;
    static void tsens_complete_callback(enum tsens_callback i)
    {
    tsens_read_done = true;
    }
  4. Configure the TSENS module.
    1. Create a TSENS module configuration struct, which can be filled out to adjust the configuration of a physical TSENS peripheral.
      struct tsens_config config_tsens;
    2. Initialize the TSENS configuration struct with the module's default values.
      tsens_get_config_defaults(&config_tsens);
      Note
      This should always be performed before using the configuration struct to ensure that all values are initialized to known default settings.
    3. Set TSENS configurations.
      tsens_init(&config_tsens);
    4. Enable the TSENS module so that conversions can be made.
  5. Register and enable the TSENS read complete callback handler.
    1. Register the user-provided read complete callback function with the driver, so that it will be run when an asynchronous read job completes.
      tsens_register_callback(&tsens_instance,
      tsens_complete_callback, TSENS_CALLBACK_RESULT_READY);
    2. Enable the read complete callback so that it will generate callbacks.

Use Case

Code

Copy-paste the following code to your user application:

system_interrupt_enable(SYSTEM_INTERRUPT_MODULE_TSENS);
tsens_read_job(&tsens_instance, &tsens_result);
while (tsens_read_done == false) {
/* Wait for asynchronous TSENS read to complete */
}
while (1) {
/* Infinite loop */
}

Workflow

  1. Enable interrupts, so that callbacks can be generated by the driver.
    system_interrupt_enable(SYSTEM_INTERRUPT_MODULE_TSENS);
  2. Start an asynchronous TSENS conversion, to store TSENS sample into the variable and generate a callback when complete.
    tsens_read_job(&tsens_instance, &tsens_result);
  3. Wait until the asynchronous conversion is complete.
    while (tsens_read_done == false) {
    /* Wait for asynchronous TSENS read to complete */
    }
  4. Enter an infinite loop once the conversion is complete.
    while (1) {
    /* Infinite loop */
    }