Update repo
This commit is contained in:
1 parent
7c6fbea774
commit
0afe39c8fe
10 files changed
+324
No files matched your search
@@ -0,0 +1,110 @@
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/devicetree.h>
|
||||
#include <zephyr/sys/printk.h>
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
#include <zephyr/drivers/uart.h>
|
||||
#include <zephyr/drivers/adc.h>
|
||||
|
||||
|
||||
#define ADC_NODE DT_NODELABEL(adc1)
|
||||
#define ADC_SAMPLE_PERIOD K_MSEC(50)
|
||||
|
||||
#if !DT_NODE_HAS_STATUS(ADC_NODE, okay)
|
||||
#error "ADC1 not available; enable it in a board overlay if needed."
|
||||
#endif
|
||||
|
||||
static const struct device *const adc_dev =
|
||||
DEVICE_DT_GET(ADC_NODE);
|
||||
|
||||
static void adc_sample_work_handler(struct k_work *work);
|
||||
|
||||
static enum adc_action adc_cb(const struct device *dev,
|
||||
const struct adc_sequence *sequence,
|
||||
uint16_t sampling_index);
|
||||
|
||||
static K_WORK_DELAYABLE_DEFINE(adc_sample_work, adc_sample_work_handler);
|
||||
|
||||
|
||||
static uint16_t sample;
|
||||
|
||||
|
||||
static struct adc_sequence_options seq_opts = {
|
||||
.interval_us = 0, // Let driver decide
|
||||
.extra_samplings = 0, // One-shot, resubmit work continuously
|
||||
.user_data = NULL,
|
||||
.callback = adc_cb
|
||||
|
||||
};
|
||||
|
||||
|
||||
// Channel Configuration
|
||||
static struct adc_channel_cfg ch1_cfg = {
|
||||
.gain = ADC_GAIN_1,
|
||||
.reference = ADC_REF_INTERNAL,
|
||||
.acquisition_time = ADC_ACQ_TIME_DEFAULT,
|
||||
.channel_id = 1,
|
||||
.differential = 0,
|
||||
};
|
||||
|
||||
|
||||
static struct adc_sequence seq = {
|
||||
.channels = BIT(1), // Channel bitmap
|
||||
.buffer = &sample,
|
||||
.buffer_size = sizeof(sample),
|
||||
.resolution = 12,
|
||||
.options = &seq_opts
|
||||
};
|
||||
|
||||
|
||||
static enum adc_action adc_cb(const struct device *dev,
|
||||
const struct adc_sequence *sequence,
|
||||
uint16_t sampling_index)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
ARG_UNUSED(sequence);
|
||||
ARG_UNUSED(sampling_index);
|
||||
|
||||
// This callback runs in interrupt context
|
||||
return ADC_ACTION_FINISH;
|
||||
}
|
||||
|
||||
|
||||
static void adc_sample_work_handler(struct k_work *work)
|
||||
{
|
||||
ARG_UNUSED(work);
|
||||
|
||||
int rc = adc_read(adc_dev, &seq);
|
||||
|
||||
if (rc == 0) {
|
||||
printk("Sensor = %u\r\n", (unsigned int)sample);
|
||||
} else {
|
||||
printk("ADC read failed: %d\r\n", rc);
|
||||
}
|
||||
|
||||
// Schedule the next conversion without flooding the CPU/UART
|
||||
(void)k_work_reschedule(&adc_sample_work, ADC_SAMPLE_PERIOD);
|
||||
}
|
||||
|
||||
|
||||
int main(void)
|
||||
{
|
||||
if(!device_is_ready(adc_dev)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Configure the channel
|
||||
if(adc_channel_setup(adc_dev, &ch1_cfg) != 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
printk("\r\n[ADC IRQ] PA1(ADC1_IN1)\r\n");
|
||||
|
||||
(void)k_work_schedule(&adc_sample_work, K_NO_WAIT);
|
||||
|
||||
while (1) {
|
||||
k_sleep(K_FOREVER);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user