2019-08-19 17:16:08 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* boards/arm/stm32l4/nucleo-l476rg/src/stm32_adc.c
|
2016-03-10 18:21:35 +01:00
|
|
|
*
|
2021-03-19 12:39:00 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2016-03-10 18:21:35 +01:00
|
|
|
*
|
2021-03-19 12:39:00 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-03-10 18:21:35 +01:00
|
|
|
*
|
2021-03-19 12:39:00 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2016-03-10 18:21:35 +01:00
|
|
|
*
|
2019-08-19 17:16:08 +02:00
|
|
|
****************************************************************************/
|
2016-03-10 18:21:35 +01:00
|
|
|
|
2019-08-19 17:16:08 +02:00
|
|
|
/****************************************************************************
|
2016-03-10 18:21:35 +01:00
|
|
|
* Included Files
|
2019-08-19 17:16:08 +02:00
|
|
|
****************************************************************************/
|
2016-03-10 18:21:35 +01:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/board.h>
|
|
|
|
#include <nuttx/analog/adc.h>
|
|
|
|
#include <arch/board/board.h>
|
|
|
|
|
|
|
|
#include "chip.h"
|
2022-03-11 17:41:15 +01:00
|
|
|
#include "arm_internal.h"
|
2016-03-10 18:21:35 +01:00
|
|
|
#include "stm32l4_pwm.h"
|
|
|
|
#include "nucleo-l476rg.h"
|
|
|
|
|
2017-08-30 10:40:58 +02:00
|
|
|
#ifdef CONFIG_STM32L4_ADC1
|
2016-03-10 18:21:35 +01:00
|
|
|
|
2019-08-19 17:16:08 +02:00
|
|
|
/****************************************************************************
|
2016-03-10 18:21:35 +01:00
|
|
|
* Pre-processor Definitions
|
2019-08-19 17:16:08 +02:00
|
|
|
****************************************************************************/
|
2016-03-10 18:21:35 +01:00
|
|
|
|
|
|
|
/* The number of ADC channels in the conversion list */
|
|
|
|
|
|
|
|
#ifdef CONFIG_ADC_DMA
|
|
|
|
# define ADC1_NCHANNELS 2
|
|
|
|
#else
|
|
|
|
# define ADC1_NCHANNELS 1
|
|
|
|
#endif
|
|
|
|
|
2019-08-19 17:16:08 +02:00
|
|
|
/****************************************************************************
|
2016-03-10 18:21:35 +01:00
|
|
|
* Private Data
|
2019-08-19 17:16:08 +02:00
|
|
|
****************************************************************************/
|
2021-03-20 13:01:22 +01:00
|
|
|
|
2016-03-10 18:21:35 +01:00
|
|
|
/* Identifying number of each ADC channel. */
|
|
|
|
|
2021-04-08 11:33:58 +02:00
|
|
|
#ifdef CONFIG_INPUT_AJOYSTICK
|
2016-03-10 18:21:35 +01:00
|
|
|
#ifdef CONFIG_ADC_DMA
|
2017-08-30 10:40:58 +02:00
|
|
|
/* The Itead analog joystick gets inputs on ADC_IN1 and ADC_IN2 */
|
2016-03-10 18:21:35 +01:00
|
|
|
|
2021-03-20 13:01:22 +01:00
|
|
|
static const uint8_t g_adc1_chanlist[ADC1_NCHANNELS] =
|
|
|
|
{
|
|
|
|
1,
|
|
|
|
2
|
|
|
|
};
|
2016-03-10 18:21:35 +01:00
|
|
|
|
|
|
|
/* Configurations of pins used byte each ADC channels */
|
|
|
|
|
2021-03-20 13:01:22 +01:00
|
|
|
static const uint32_t g_adc1_pinlist[ADC1_NCHANNELS] =
|
|
|
|
{
|
|
|
|
GPIO_ADC1_IN1,
|
|
|
|
GPIO_ADC1_IN2
|
|
|
|
};
|
2016-03-10 18:21:35 +01:00
|
|
|
|
|
|
|
#else
|
|
|
|
/* Without DMA, only a single channel can be supported */
|
|
|
|
|
2017-08-30 10:40:58 +02:00
|
|
|
/* The Itead analog joystick gets input on ADC_IN1 */
|
2016-03-10 18:21:35 +01:00
|
|
|
|
2021-03-20 13:01:22 +01:00
|
|
|
static const uint8_t g_adc1_chanlist[ADC1_NCHANNELS] =
|
|
|
|
{
|
|
|
|
1
|
|
|
|
};
|
2016-03-10 18:21:35 +01:00
|
|
|
|
|
|
|
/* Configurations of pins used byte each ADC channels */
|
|
|
|
|
2021-03-20 13:01:22 +01:00
|
|
|
static const uint32_t g_adc1_pinlist[ADC1_NCHANNELS] =
|
|
|
|
{
|
|
|
|
GPIO_ADC1_IN1
|
|
|
|
};
|
2016-03-10 18:21:35 +01:00
|
|
|
|
|
|
|
#endif /* CONFIG_ADC_DMA */
|
2021-04-08 11:33:58 +02:00
|
|
|
#endif /* CONFIG_INPUT_AJOYSTICK */
|
2016-03-10 18:21:35 +01:00
|
|
|
|
2019-08-19 17:16:08 +02:00
|
|
|
/****************************************************************************
|
2016-03-10 18:21:35 +01:00
|
|
|
* Private Functions
|
2019-08-19 17:16:08 +02:00
|
|
|
****************************************************************************/
|
2016-03-10 18:21:35 +01:00
|
|
|
|
2019-08-19 17:16:08 +02:00
|
|
|
/****************************************************************************
|
2016-03-10 18:21:35 +01:00
|
|
|
* Public Functions
|
2019-08-19 17:16:08 +02:00
|
|
|
****************************************************************************/
|
2016-03-10 18:21:35 +01:00
|
|
|
|
2019-08-19 17:16:08 +02:00
|
|
|
/****************************************************************************
|
2017-08-30 10:40:58 +02:00
|
|
|
* Name: stm32l4_adc_setup
|
2016-03-10 18:21:35 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2016-12-06 00:06:08 +01:00
|
|
|
* Initialize ADC and register the ADC driver.
|
2016-03-10 18:21:35 +01:00
|
|
|
*
|
2019-08-19 17:16:08 +02:00
|
|
|
****************************************************************************/
|
2016-03-10 18:21:35 +01:00
|
|
|
|
2017-08-30 10:40:58 +02:00
|
|
|
int stm32l4_adc_setup(void)
|
2016-03-10 18:21:35 +01:00
|
|
|
{
|
|
|
|
struct adc_dev_s *adc;
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
2016-12-06 00:06:08 +01:00
|
|
|
/* Configure the pins as analog inputs for the selected channels */
|
2016-03-10 18:21:35 +01:00
|
|
|
|
2016-12-06 00:06:08 +01:00
|
|
|
for (i = 0; i < ADC1_NCHANNELS; i++)
|
2016-03-10 18:21:35 +01:00
|
|
|
{
|
2017-08-30 10:40:58 +02:00
|
|
|
stm32l4_configgpio(g_adc1_pinlist[i]);
|
2016-12-06 00:06:08 +01:00
|
|
|
}
|
|
|
|
|
2017-08-30 10:40:58 +02:00
|
|
|
/* Call stm32l4_adc_initialize() to get an instance of the ADC interface */
|
2016-03-10 18:21:35 +01:00
|
|
|
|
2017-08-30 10:40:58 +02:00
|
|
|
adc = stm32l4_adc_initialize(1, g_adc1_chanlist, ADC1_NCHANNELS);
|
2016-12-06 00:06:08 +01:00
|
|
|
if (adc == NULL)
|
|
|
|
{
|
|
|
|
aerr("ERROR: Failed to get ADC interface\n");
|
|
|
|
return -ENODEV;
|
2016-03-10 18:21:35 +01:00
|
|
|
}
|
|
|
|
|
2016-12-06 00:06:08 +01:00
|
|
|
/* Register the ADC driver at "/dev/adc0" */
|
2016-03-10 18:21:35 +01:00
|
|
|
|
2016-12-06 00:06:08 +01:00
|
|
|
ret = adc_register("/dev/adc0", adc);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
aerr("ERROR: adc_register failed: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2016-03-10 18:21:35 +01:00
|
|
|
|
2016-12-06 00:06:08 +01:00
|
|
|
return OK;
|
2016-03-10 18:21:35 +01:00
|
|
|
}
|
|
|
|
|
2017-08-30 10:40:58 +02:00
|
|
|
#endif /* CONFIG_STM32L4_ADC1 */
|