Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions zephyr/modules/owntech_adc_driver/zephyr/public_api/adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* @date 2024
* @date 2025
*
* @author Clément Foucher <clement.foucher@laas.fr>
*/
Expand All @@ -45,10 +45,11 @@
* Local variables
*/

static adc_ev_src_t adc_trigger_sources[NUMBER_OF_ADCS] = {0};
static uint32_t adc_discontinuous_mode[NUMBER_OF_ADCS] = {0};
static uint32_t enabled_channels_count[NUMBER_OF_ADCS] = {0};
static bool enable_dma[NUMBER_OF_ADCS] = {0};
static adc_ev_src_t adc_trigger_sources[NUMBER_OF_ADCS] = {0};
static uint32_t adc_discontinuous_mode[NUMBER_OF_ADCS] = {0};
static adc_sampling_time_t adc_sampling_times[NUMBER_OF_ADCS] = {0};
static uint32_t enabled_channels_count[NUMBER_OF_ADCS] = {0};
static bool enable_dma[NUMBER_OF_ADCS] = {0};

static uint32_t
enabled_channels[NUMBER_OF_ADCS][NUMBER_OF_CHANNELS_PER_ADC] = {0};
Expand All @@ -75,6 +76,15 @@ void adc_configure_discontinuous_mode(uint8_t adc_number,
adc_discontinuous_mode[adc_number-1] = discontinuous_count;
}

void adc_configure_sampling_time(uint8_t adc_number,
adc_sampling_time_t sampling_time)
{
if ( (adc_number == 0) || (adc_number > NUMBER_OF_ADCS) )
return;

adc_sampling_times[adc_number-1] = sampling_time;
}

void adc_add_channel(uint8_t adc_number, uint8_t channel)
{
if ( (adc_number == 0) || (adc_number > NUMBER_OF_ADCS) )
Expand Down Expand Up @@ -156,6 +166,13 @@ void adc_start()
for (uint8_t adc_num = 1 ; adc_num <= NUMBER_OF_ADCS ; adc_num++)
{
uint8_t adc_index = adc_num-1;

adc_sampling_time_t sampling_time = adc_sampling_times[adc_index];
if (sampling_time == adc_st_default)
{
sampling_time = adc_st_294ns;
}

if (enabled_channels_count[adc_index] > 0)
{
for (int channel_index = 0;
Expand All @@ -165,10 +182,15 @@ void adc_start()
if (enabled_channels[adc_index][channel_index] == 0)
break;

adc_core_configure_channel(
adc_core_set_channel_rank(
adc_num,
enabled_channels[adc_index][channel_index],
channel_index+1);

adc_core_set_channel_sampling_time(
adc_num,
enabled_channels[adc_index][channel_index],
sampling_time);
}
}
}
Expand Down
49 changes: 20 additions & 29 deletions zephyr/modules/owntech_adc_driver/zephyr/public_api/adc.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* @date 2024
* @date 2025
*
* @author Clément Foucher <clement.foucher@laas.fr>
*
Expand All @@ -42,39 +42,14 @@
#include <stdint.h>
#include <stdbool.h>

/* Owntech */
#include "adc_enums.h"


#ifdef __cplusplus
extern "C" {
#endif


/**
* Public enums
*/

/**
* @brief Defines types of hrtim events to which the ADC connects:
*
* - software - software events
*
* - `hrtim_ev1` to `hrtim_ev9` - hrtim driven events
*
*/
typedef enum
{
software = 0,
hrtim_ev1 = 1,
hrtim_ev2 = 2,
hrtim_ev3 = 3,
hrtim_ev4 = 4,
hrtim_ev5 = 5,
hrtim_ev6 = 6,
hrtim_ev7 = 7,
hrtim_ev8 = 8,
hrtim_ev9 = 9
} adc_ev_src_t;


/* Public API */

/**
Expand Down Expand Up @@ -104,6 +79,22 @@ void adc_configure_trigger_source(uint8_t adc_number,
void adc_configure_discontinuous_mode(uint8_t adc_number,
uint32_t discontinuous_count);

/**
* @brief Registers the samping time to use for an ADC.
*
* This will only be applied when ADC is started.
* If ADC is already started, it must be stopped
* then started again.
*
* Default samping time is 294 ns.
*
* @param adc_number Number of the ADC to configure.
* @param discontinuous_count Number of channels to acquire on each
* trigger event. 0 to disable discontinuous mode (default).
*/
void adc_configure_sampling_time(uint8_t adc_number,
adc_sampling_time_t sampling_time);

/**
* @brief Adds a channel to the list of channels to be acquired
* for an ADC.
Expand Down
90 changes: 90 additions & 0 deletions zephyr/modules/owntech_adc_driver/zephyr/public_api/adc_enums.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright (c) 2025-present LAAS-CNRS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: LGPL-2.1
*/

/*
* @date 2025
*
* @author Clément Foucher <clement.foucher@laas.fr>
*/

#ifndef ADC_ENUMS_H_
#define ADC_ENUMS_H_

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief Defines types of hrtim events to which the ADC connects:
*
* - software - software events
*
* - `hrtim_ev1` to `hrtim_ev9` - hrtim driven events
*
*/
typedef enum
{
software = 0,
hrtim_ev1 = 1,
hrtim_ev2 = 2,
hrtim_ev3 = 3,
hrtim_ev4 = 4,
hrtim_ev5 = 5,
hrtim_ev6 = 6,
hrtim_ev7 = 7,
hrtim_ev8 = 8,
hrtim_ev9 = 9
} adc_ev_src_t;

/**
* @brief Defines the sampling time for the ADC.
* The clock source is 170 MHz, and ADC prescaler is set
* to 4, which makes the ADC clock 42.5 MHz.
* Cycle duration is thus ≈ 23.5 ns.
*
* Available sampling times (in cycles) are:
* - 2.5 => 58.75 ns
* - 6.5 => 152.75 ns
* - 12.5 => 293.75 ns
* - 24.5 => 575.75 ns
* - 47.5 => 1116.25 ns
* - 92.5 => 2173.75 ns
* - 247.5 => 5816.25 ns
* - 640.5 => 15051.75 ns
*
* Let's round to the ns here.
*/
typedef enum
{
adc_st_default = 0,
adc_st_59ns,
adc_st_153ns,
adc_st_294ns,
adc_st_576ns,
adc_st_1116ns,
adc_st_2174ns,
adc_st_5817ns,
adc_st_15052ns
} adc_sampling_time_t;

#ifdef __cplusplus
}
#endif

#endif /* ADC_ENUMS_H_ */
Loading