board/stm32f401rc-rs485: Add support to ADC

Signed-off-by: Rodrigo Sim <rcsim10@gmail.com>
This commit is contained in:
Rodrigo Sim 2024-03-02 12:40:57 -03:00 committed by Alan Carvalho de Assis
parent d11f5b4763
commit 634b605476
7 changed files with 241 additions and 126 deletions

View File

@ -338,7 +338,7 @@ generated by STM32F401RC-RS485 and transmitted over RS-485::
modbus_master
-------------
Configures the NuttShell (nsh) and enables modbus in msater mode. This
Configures the NuttShell (nsh) and enables modbus in master mode. This
configuration enables a serial console on USART6. The RS-485 is connected
to USART2. Follow below precedure to use modbusmaster test aplication, you will
need a USB to RS-485 converter to connect the board to a PC via RS-485.
@ -416,3 +416,29 @@ NSH commands::
30.13 degrees Celsius
30.13 degrees Celsius
30.13 degrees Celsius
adc
---
Configures the NuttShell (nsh) over USB Serial (check usbserial configuration) and enables ADC 1 on channels 0 and 4.
NSH commands::
nsh> adc -h
Usage: adc [OPTIONS]
Arguments are "sticky". For example, once the ADC device is
specified, that device will be re-used until it is changed.
"sticky" OPTIONS include:
[-p devpath] selects the ADC device. Default: /dev/adc0 Current: /dev/adc0
[-n count] selects the samples to collect. Default: 1 Current: 0
[-h] shows this message and exits
nsh> adc -n 2
adc_main: g_adcstate.count: 2
adc_main: Hardware initialized. Opening the ADC device: /dev/adc0
Sample:
1: channel: 0 value: 2684
Sample:
1: channel: 4 value: 2682
Currently there is a bug that causes the application to always read the same value for channel 0 and 4. If you want to read the value from channel 2, you will need to enable the config "ADC1 Scan Mode".

View File

@ -0,0 +1,67 @@
#
# This file is autogenerated: PLEASE DO NOT EDIT IT.
#
# You can use "make menuconfig" to make any modifications to the installed .config file.
# You can then do "make savedefconfig" to generate a new defconfig file that includes your
# modifications.
#
# CONFIG_ARCH_FPU is not set
# CONFIG_NSH_ARGCAT is not set
# CONFIG_NSH_CMDOPT_HEXDUMP is not set
# CONFIG_NSH_DISABLE_IFCONFIG is not set
# CONFIG_NSH_DISABLE_PS is not set
CONFIG_ADC=y
CONFIG_ANALOG=y
CONFIG_ARCH="arm"
CONFIG_ARCH_BOARD="stm32f401rc-rs485"
CONFIG_ARCH_BOARD_STM32F401RC_RS485=y
CONFIG_ARCH_BUTTONS=y
CONFIG_ARCH_CHIP="stm32"
CONFIG_ARCH_CHIP_STM32=y
CONFIG_ARCH_CHIP_STM32F401RC=y
CONFIG_ARCH_INTERRUPTSTACK=2048
CONFIG_ARCH_IRQBUTTONS=y
CONFIG_ARCH_STACKDUMP=y
CONFIG_BOARDCTL_USBDEVCTRL=y
CONFIG_BOARD_LOOPSPERMSEC=8499
CONFIG_BUILTIN=y
CONFIG_CDCACM=y
CONFIG_CDCACM_CONSOLE=y
CONFIG_EXAMPLES_ADC=y
CONFIG_EXAMPLES_ADC_SWTRIG=y
CONFIG_EXAMPLES_BUTTONS=y
CONFIG_EXAMPLES_BUTTONS_NAME0="SW3"
CONFIG_EXAMPLES_BUTTONS_NAME1="SW4"
CONFIG_EXAMPLES_BUTTONS_NAME2="SW5"
CONFIG_EXAMPLES_BUTTONS_NAMES=y
CONFIG_EXAMPLES_BUTTONS_QTD=3
CONFIG_HAVE_CXX=y
CONFIG_HAVE_CXXINITIALIZE=y
CONFIG_INIT_ENTRYPOINT="nsh_main"
CONFIG_INPUT=y
CONFIG_INPUT_BUTTONS=y
CONFIG_INPUT_BUTTONS_LOWER=y
CONFIG_INTELHEX_BINARY=y
CONFIG_NSH_ARCHINIT=y
CONFIG_NSH_BUILTIN_APPS=y
CONFIG_NSH_FILEIOSIZE=512
CONFIG_NSH_LINELEN=64
CONFIG_NSH_READLINE=y
CONFIG_PREALLOC_TIMERS=4
CONFIG_RAM_SIZE=98304
CONFIG_RAM_START=0x20000000
CONFIG_RAW_BINARY=y
CONFIG_RR_INTERVAL=200
CONFIG_SCHED_WAITPID=y
CONFIG_SPI=y
CONFIG_START_DAY=5
CONFIG_START_MONTH=5
CONFIG_START_YEAR=2014
CONFIG_STM32_ADC1=y
CONFIG_STM32_JTAG_SW_ENABLE=y
CONFIG_STM32_OTGFS=y
CONFIG_STM32_PWR=y
CONFIG_STM32_USART6=y
CONFIG_SYSTEM_NSH=y
CONFIG_TASK_NAME_SIZE=0
CONFIG_USBDEV=y

View File

@ -36,6 +36,10 @@ if(CONFIG_ARCH_BUTTONS)
list(APPEND SRCS stm32_buttons.c)
endif()
if(CONFIG_ADC)
list(APPEND SRCS stm32_adc.c)
endif()
if(CONFIG_STM32_SDIO)
list(APPEND SRCS stm32_sdio.c)
endif()

View File

@ -39,6 +39,10 @@ ifeq ($(CONFIG_ARCH_BUTTONS),y)
CSRCS += stm32_buttons.c
endif
ifeq ($(CONFIG_ADC),y)
CSRCS += stm32_adc.c
endif
ifeq ($(CONFIG_STM32_SDIO),y)
CSRCS += stm32_sdio.c
endif

View File

@ -0,0 +1,117 @@
/****************************************************************************
* boards/arm/stm32/stm32f401rc-rs485/src/stm32_adc.c
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#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"
#include "arm_internal.h"
#include "stm32_pwm.h"
#include "stm32_adc.h"
#include "stm32f401rc-rs485.h"
#ifdef CONFIG_STM32_ADC1
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* The number of ADC channels in the conversion list */
#define ADC1_NCHANNELS 2
/****************************************************************************
* Private Data
****************************************************************************/
/* Identifying number of each ADC channel. */
/* There are two trimpots on the board connected to ADC1_IN0 and ADC1_IN4 */
static const uint8_t g_adc1_chanlist[ADC1_NCHANNELS] =
{
0, 4
};
/* Configurations of pins used byte each ADC channels */
static const uint32_t g_adc1_pinlist[ADC1_NCHANNELS] =
{
GPIO_ADC1_IN0,
GPIO_ADC1_IN4
};
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: stm32_adc_setup
*
* Description:
* Initialize ADC and register the ADC driver.
*
****************************************************************************/
int stm32_adc_setup(void)
{
struct adc_dev_s *adc;
int ret;
int i;
/* Configure the pins as analog inputs for the selected channels */
for (i = 0; i < ADC1_NCHANNELS; i++)
{
stm32_configgpio(g_adc1_pinlist[i]);
}
/* Call stm32_adcinitialize() to get an instance of the ADC interface */
adc = stm32_adcinitialize(1, g_adc1_chanlist, ADC1_NCHANNELS);
if (adc == NULL)
{
aerr("ERROR: Failed to get ADC interface\n");
return -ENODEV;
}
/* Register the ADC driver at "/dev/adc0" */
ret = adc_register("/dev/adc0", adc);
if (ret < 0)
{
aerr("ERROR: adc_register failed: %d\n", ret);
return ret;
}
return OK;
}
#endif /* CONFIG_STM32_ADC1 */

View File

@ -169,6 +169,16 @@ int stm32_bringup(void)
}
#endif
#ifdef CONFIG_ADC
/* Initialize ADC and register the ADC driver. */
ret = stm32_adc_setup();
if (ret < 0)
{
syslog(LOG_ERR, "ERROR: stm32_adc_setup failed: %d\n", ret);
}
#endif
#ifdef HAVE_SDIO
/* Initialize the SDIO block driver */

View File

@ -101,24 +101,6 @@
#define GPIO_SPI1_SCK_OFF (GPIO_INPUT | GPIO_PULLDOWN | \
GPIO_PORTA | GPIO_PIN5)
/* SSD1306 */
#define GPIO_SSD1306_CS (GPIO_OUTPUT|GPIO_OTYPER_PP(0)|GPIO_SPEED_2MHz|\
GPIO_OUTPUT_SET|GPIO_PORTB|GPIO_PIN6)
#define GPIO_SSD1306_CMD (GPIO_OUTPUT|GPIO_OTYPER_PP(0)|GPIO_OSPEED_2MHz|\
GPIO_OUTPUT_SET|GPIO_PORTC|GPIO_PIN7)
#define GPIO_SSD1306_RST (GPIO_OUTPUT|GPIO_OTYPER_PP(0)|GPIO_SPEED_2MHz|\
GPIO_OUTPUT_SET|GPIO_PORTA|GPIO_PIN9)
/* MCP2551 */
#define GPIO_MCP2515_CS (GPIO_OUTPUT|GPIO_OTYPER_PP(0)|GPIO_SPEED_2MHz|\
GPIO_OUTPUT_SET|GPIO_PORTA|GPIO_PIN4)
#define GPIO_MCP2515_IRQ (GPIO_INPUT|GPIO_FLOAT|GPIO_PORTA|GPIO_PIN1)
#ifdef HAVE_MMCSD
# define GPIO_SPI_CS_SD_CARD_OFF \
(GPIO_INPUT | GPIO_PULLDOWN | GPIO_SPEED_2MHz | \
@ -131,89 +113,6 @@
GPIO_OUTPUT_SET | GPIO_PORTB | GPIO_PIN5)
#endif
/* Devices on the onboard bus.
*
* Note that these are unshifted addresses.
*/
#define NUCLEO_I2C_OBDEV_LED 0x55
#define NUCLEO_I2C_OBDEV_HMC5883 0x1e
/* Itead Joystick Shield
*
* See http://imall.iteadstudio.com/im120417014.html for more information
* about this joystick.
*
* --------- ----------------- ---------------------------------
* ARDUINO ITEAD NUCLEO-F4x1
* PIN NAME SIGNAL SIGNAL
* --------- ----------------- ---------------------------------
* D3 Button E Output PB3
* D4 Button D Output PB5
* D5 Button C Output PB4
* D6 Button B Output PB10
* D7 Button A Output PA8
* D8 Button F Output PA9
* D9 Button G Output PC7
* A0 Joystick Y Output PA0 ADC1_0
* A1 Joystick X Output PA1 ADC1_1
* --------- ----------------- ---------------------------------
*
* All buttons are pulled on the shield. A sensed low value indicates
* when the button is pressed.
*
* NOTE: Button F cannot be used with the default USART1 configuration
* because PA9 is configured for USART1_RX by default. Use select
* different USART1 pins in the board.h file or select a different
* USART or select CONFIG_NUCLEO_F401RE_AJOY_MINBUTTONS which will
* eliminate all but buttons A, B, and C.
*/
#define ADC_XOUPUT 1 /* X output is on ADC channel 1 */
#define ADC_YOUPUT 0 /* Y output is on ADC channel 0 */
#define GPIO_BUTTON_A \
(GPIO_INPUT | GPIO_PULLUP |GPIO_EXTI | GPIO_PORTA | GPIO_PIN8)
#define GPIO_BUTTON_B \
(GPIO_INPUT | GPIO_PULLUP |GPIO_EXTI | GPIO_PORTB | GPIO_PIN10)
#define GPIO_BUTTON_C \
(GPIO_INPUT | GPIO_PULLUP |GPIO_EXTI | GPIO_PORTB | GPIO_PIN4)
#define GPIO_BUTTON_D \
(GPIO_INPUT | GPIO_PULLUP |GPIO_EXTI | GPIO_PORTB | GPIO_PIN5)
#define GPIO_BUTTON_E \
(GPIO_INPUT | GPIO_PULLUP |GPIO_EXTI | GPIO_PORTB | GPIO_PIN3)
#define GPIO_BUTTON_F \
(GPIO_INPUT | GPIO_PULLUP |GPIO_EXTI | GPIO_PORTA | GPIO_PIN9)
#define GPIO_BUTTON_G \
(GPIO_INPUT | GPIO_PULLUP |GPIO_EXTI | GPIO_PORTC | GPIO_PIN7)
/* Itead Joystick Signal interpretation:
*
* --------- ----------------------- ---------------------------
* BUTTON TYPE NUTTX ALIAS
* --------- ----------------------- ---------------------------
* Button A Large button A JUMP/BUTTON 3
* Button B Large button B FIRE/BUTTON 2
* Button C Joystick select button SELECT/BUTTON 1
* Button D Tiny Button D BUTTON 6
* Button E Tiny Button E BUTTON 7
* Button F Large Button F BUTTON 4
* Button G Large Button G BUTTON 5
* --------- ----------------------- ---------------------------
*/
#define GPIO_BUTTON_1 GPIO_BUTTON_C
#define GPIO_BUTTON_2 GPIO_BUTTON_B
#define GPIO_BUTTON_3 GPIO_BUTTON_A
#define GPIO_BUTTON_4 GPIO_BUTTON_F
#define GPIO_BUTTON_5 GPIO_BUTTON_G
#define GPIO_BUTTON_6 GPIO_BUTTON_D
#define GPIO_BUTTON_7 GPIO_BUTTON_E
#define GPIO_SELECT GPIO_BUTTON_1
#define GPIO_FIRE GPIO_BUTTON_2
#define GPIO_JUMP GPIO_BUTTON_3
/****************************************************************************
* Public Data
****************************************************************************/
@ -286,30 +185,6 @@ void stm32_usbinitialize(void);
int stm32_adc_setup(void);
#endif
/****************************************************************************
* Name: board_ajoy_initialize
*
* Description:
* Initialize and register the button joystick driver
*
****************************************************************************/
#ifdef CONFIG_INPUT_AJOYSTICK
int board_ajoy_initialize(void);
#endif
/****************************************************************************
* Name: stm32_mcp2515initialize
*
* Description:
* Initialize and register the MCP2515 CAN driver.
*
****************************************************************************/
#ifdef CONFIG_CAN_MCP2515
int stm32_mcp2515initialize(const char *devpath);
#endif
/****************************************************************************
* Name: stm32_sdio_initialize
*
@ -342,4 +217,16 @@ struct i2c_master_s *stm32_i2cbus_initialize(int port);
int stm32_at24_init(char *path);
/****************************************************************************
* Name: stm32_adc_setup
*
* Description:
* Initialize ADC and register the ADC driver.
*
****************************************************************************/
#ifdef CONFIG_ADC
int stm32_adc_setup(void);
#endif
#endif /* __BOARDS_ARM_STM32_STM32F401RC_RS485_SRC_STM32F401RC_RS485_H */