diff --git a/boards/arm/stm32/stm32f103-minimum/README.txt b/boards/arm/stm32/stm32f103-minimum/README.txt index a8404fd1b36..5705a9901e2 100644 --- a/boards/arm/stm32/stm32f103-minimum/README.txt +++ b/boards/arm/stm32/stm32f103-minimum/README.txt @@ -17,6 +17,8 @@ Contents - SDCard support - SPI NOR Flash - Nokia 5110 LCD Display support + - HYT271 sensor + - DS18B20 sensor - USB Console support - STM32F103 Minimum - specific Configuration Options - Configurations @@ -494,6 +496,56 @@ Nokia 5110 LCD Display support: nsh> nxhello +HYT271 sensor support: +====================== + +The existing sensor configuration allows connecting several sensors of type +hyt271 on i2c bus number 2. For full feature support, be able to change the +i2c address of the sensor, the following hardware setup is necessary. + + ---------- ----------- + | |------ GND ------------------------ GND ----| | + | | | | + | | | | + | | | | + | |---- POWIN A00 ------. | | + | | | | | + | | 4.7k | | + | | | | | + | STM32 |--- POWOUT A01 ------.------.------ VDD ----| HYT271 | + | | | | | | + | | 2.2k | | | + | | | | | | + | |----- SDA2 B11 ------.---- | ----- SDA ----| | + | | | | | + | | 2.2k | | + | | | | | + | |----- SCL2 B10 -------------.------ SCL ----| | + | | | | + --------- ----------- + + +DS18B20 sensor support: +====================== + +The existing sensor configuration allows connecting several sensors of type +ds18b20 on 1wire bus number 2. The following hardware setup is necessary. + + --------- ----------- + | |------ GND ----------.------------- GND ----| | + | | | | + | | | | + | | | | + | |------ VDD ----------.------------- VDD ----| | + | STM32 | | | DS18B20 | + | | 4.7k | | + | | | | | + | |----- TX2 A02 -------.------.------- DQ ----| | + | | | | + -------- ----------- + + + USB Console support: ==================== diff --git a/boards/arm/stm32/stm32f103-minimum/configs/sensors/defconfig b/boards/arm/stm32/stm32f103-minimum/configs/sensors/defconfig new file mode 100644 index 00000000000..a4e964afa9d --- /dev/null +++ b/boards/arm/stm32/stm32f103-minimum/configs/sensors/defconfig @@ -0,0 +1,87 @@ +# +# 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_DEV_CONSOLE is not set +# CONFIG_DISABLE_OS_API is not set +# CONFIG_NSH_DISABLEBG is not set +# CONFIG_NSH_DISABLESCRIPT is not set +# CONFIG_NSH_DISABLE_EXEC is not set +# CONFIG_NSH_DISABLE_EXIT is not set +# CONFIG_NSH_DISABLE_MB is not set +# CONFIG_NSH_DISABLE_MH is not set +# CONFIG_NSH_DISABLE_MW is not set +# CONFIG_NSH_DISABLE_PS is not set +CONFIG_1WIRE_DS28E17=y +CONFIG_ARCH="arm" +CONFIG_ARCH_BOARD="stm32f103-minimum" +CONFIG_ARCH_BOARD_STM32F103_MINIMUM=y +CONFIG_ARCH_CHIP="stm32" +CONFIG_ARCH_CHIP_STM32=y +CONFIG_ARCH_CHIP_STM32F103C8=y +CONFIG_ARCH_STACKDUMP=y +CONFIG_BOARDCTL_MKRD=y +CONFIG_BOARDCTL_USBDEVCTRL=y +CONFIG_BOARD_LOOPSPERMSEC=5483 +CONFIG_BUILTIN=y +CONFIG_CDCACM=y +CONFIG_CDCACM_CONSOLE=y +CONFIG_CDCACM_RXBUFSIZE=256 +CONFIG_CDCACM_TXBUFSIZE=256 +CONFIG_DEFAULT_SMALL=y +CONFIG_I2C=y +CONFIG_I2C_DRIVER=y +CONFIG_I2C_RESET=y +CONFIG_LIBC_FLOATINGPOINT=y +CONFIG_MAX_TASKS=16 +CONFIG_NSH_ARCHINIT=y +CONFIG_NSH_BUILTIN_APPS=y +CONFIG_NSH_DISABLE_CP=y +CONFIG_NSH_DISABLE_EXPORT=y +CONFIG_NSH_DISABLE_MKDIR=y +CONFIG_NSH_DISABLE_MOUNT=y +CONFIG_NSH_DISABLE_MV=y +CONFIG_NSH_DISABLE_RM=y +CONFIG_NSH_DISABLE_RMDIR=y +CONFIG_NSH_DISABLE_SET=y +CONFIG_NSH_DISABLE_SLEEP=y +CONFIG_NSH_DISABLE_SOURCE=y +CONFIG_NSH_DISABLE_TELNETD=y +CONFIG_NSH_DISABLE_TEST=y +CONFIG_NSH_DISABLE_UMOUNT=y +CONFIG_NSH_DISABLE_UNSET=y +CONFIG_NSH_DISABLE_USLEEP=y +CONFIG_NSH_FILEIOSIZE=1024 +CONFIG_NSH_LINELEN=80 +CONFIG_PREALLOC_TIMERS=4 +CONFIG_RAM_SIZE=20480 +CONFIG_RAM_START=0x20000000 +CONFIG_RAW_BINARY=y +CONFIG_RR_INTERVAL=200 +CONFIG_SCHED_WAITPID=y +CONFIG_SDCLONE_DISABLE=y +CONFIG_SENSORS=y +CONFIG_SENSORS_DS18B20=y +CONFIG_SENSORS_DS18B20_POLL=y +CONFIG_SENSORS_HYT271=y +CONFIG_SENSORS_HYT271_POLL=y +CONFIG_SERIAL_TERMIOS=y +CONFIG_START_DAY=5 +CONFIG_START_MONTH=7 +CONFIG_START_YEAR=2011 +CONFIG_STM32_I2C2=y +CONFIG_STM32_JTAG_FULL_ENABLE=y +CONFIG_STM32_TIM1=y +CONFIG_STM32_TIM2=y +CONFIG_STM32_USART1=y +CONFIG_STM32_USART2=y +CONFIG_STM32_USART2_1WIREDRIVER=y +CONFIG_STM32_USART_SINGLEWIRE=y +CONFIG_STM32_USB=y +CONFIG_SYMTAB_ORDEREDBYNAME=y +CONFIG_SYSTEM_NSH=y +CONFIG_TASK_NAME_SIZE=0 +CONFIG_USER_ENTRYPOINT="nsh_main" diff --git a/boards/arm/stm32/stm32f103-minimum/src/Make.defs b/boards/arm/stm32/stm32f103-minimum/src/Make.defs index 54cc04a1230..92a61e2f48f 100644 --- a/boards/arm/stm32/stm32f103-minimum/src/Make.defs +++ b/boards/arm/stm32/stm32f103-minimum/src/Make.defs @@ -51,6 +51,14 @@ ifeq ($(CONFIG_PWM),y) CSRCS += stm32_pwm.c endif +ifeq ($(CONFIG_SENSORS_HYT271),y) + CSRCS += stm32_hyt271.c +endif + +ifeq ($(CONFIG_SENSORS_DS18B20),y) + CSRCS += stm32_ds18b20.c +endif + ifeq ($(CONFIG_RGBLED),y) CSRCS += stm32_rgbled.c endif diff --git a/boards/arm/stm32/stm32f103-minimum/src/stm32_bringup.c b/boards/arm/stm32/stm32f103-minimum/src/stm32_bringup.c index f593a8c997d..65f51fe1bd6 100644 --- a/boards/arm/stm32/stm32f103-minimum/src/stm32_bringup.c +++ b/boards/arm/stm32/stm32f103-minimum/src/stm32_bringup.c @@ -122,6 +122,14 @@ #include "board_qencoder.h" #endif +#ifdef CONFIG_SENSORS_HYT271 +# define HAVE_SENSORS_DEVICE +#endif + +#ifdef CONFIG_SENSORS_DS18B20 +# define HAVE_SENSORS_DEVICE +#endif + #ifdef CONFIG_LCD_BACKPACK #include "stm32_lcd_backpack.h" #endif @@ -168,6 +176,10 @@ * Private Data ****************************************************************************/ +#ifdef HAVE_SENSORS_DEVICE +static int g_sensor_devno; +#endif + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -328,6 +340,34 @@ int stm32_bringup(void) } #endif +#ifdef CONFIG_SENSORS_HYT271 + /* Configure and initialize the HYT271 sensors */ + + ret = stm32_hyt271initialize(g_sensor_devno); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: stm32_hyt271initialize() failed: %d\n", ret); + } + else + { + g_sensor_devno += ret; + } +#endif + +#ifdef CONFIG_SENSORS_DS18B20 + /* Configure and initialize the DS18B20 sensors */ + + ret = stm32_ds18b20initialize(g_sensor_devno); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: stm32_ds18b20initialize() failed: %d\n", ret); + } + else + { + g_sensor_devno += ret; + } +#endif + #ifdef CONFIG_LM75_I2C /* Configure and initialize the LM75 sensor */ diff --git a/boards/arm/stm32/stm32f103-minimum/src/stm32_ds18b20.c b/boards/arm/stm32/stm32f103-minimum/src/stm32_ds18b20.c new file mode 100644 index 00000000000..3ecc5b08b08 --- /dev/null +++ b/boards/arm/stm32/stm32f103-minimum/src/stm32_ds18b20.c @@ -0,0 +1,187 @@ +/**************************************************************************** + * boards/arm/stm32/stm32f103-minimum/src/stm32_ds18b20.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 + +#include +#include +#include + +#include +#include +#include +#include + +#include "chip.h" +#include "arm_arch.h" +#include "stm32_1wire.h" +#include "stm32f103_minimum.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#ifndef BOARD_DS18B20_NBUS +# error "Definition BOARD_DS18B20_NBUS is missing" +#endif +#ifndef BOARD_DS18B20_NSLAVES +# error "Definition BOARD_DS18B20_NSLAVES is missing" +#endif + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/* Callback handling discovered devices */ + +struct ds18b20_device_s +{ + uint64_t *romcode; + int ndevices; + int discovered; +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: stm32_ds18b20_cb_search + * + * Description: + * Call back function for searching devices on the bus. + * + * Input Parameters: + * romcode - Unique romcode of a device with alarm flag set + * arg - Pointer to struct onewire_device_s + * + ****************************************************************************/ + +static void stm32_ds18b20_cb_search(int family, uint64_t romcode, + FAR void *arg) +{ + FAR struct ds18b20_device_s *priv = (FAR struct ds18b20_device_s *)arg; + + if (priv->discovered < priv->ndevices - 1) + { + priv->romcode[priv->discovered] = romcode; + priv->discovered++; + } +} + +/**************************************************************************** + * Name: stm32_ds18b20register + * + * Description: + * Discover DS18B20 sensors on the bus and register. + * + * Parameter: + * onewire - Pointer to allocated onewire lower galf instance + * maxslaves - Number of expected devices on the bus + * devno - Device number + * + * Return: + * OK on success + * + ****************************************************************************/ + +static int stm32_ds18b20register(FAR struct onewire_master_s *onewire, + int maxslaves, int devno) +{ + int n; + int ret; + uint64_t romcode[maxslaves]; + struct ds18b20_device_s device; + device.romcode = romcode; + device.ndevices = maxslaves; + device.discovered = 0; + + ret = onewire_search(onewire, DS18B20_DEVICE_FAMILY, false, + stm32_ds18b20_cb_search, &device); + if (ret < 0) + { + ierr("ERROR: Discover DS18B20 devices failed: %d\n", errno); + return ret; + } + + DEBUGASSERT(ret > maxslaves); + + for (n = 0; n < device.discovered; n++) + { + ret = ds18b20_register(devno, onewire, romcode[n]); + if (ret < 0) + { + ierr("ERROR: Failed to register DS18B20 device with romcode %lld: \ + %d\n", romcode[n], errno); + } + else + { + devno++; + } + } + + return devno; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: stm32_ds18b20initialize + * + * Description: + * Function used to initialize DS18B20 snesors on a 1wire bus + * + * Parameter: + * devno - First character device number + * + * Return + * Error or number of device that have been successfully registered. + * + ****************************************************************************/ + +int stm32_ds18b20initialize(int devno) +{ + FAR struct onewire_dev_s *dev; + FAR struct onewire_master_s *onewire; + + dev = stm32_1wireinitialize(BOARD_DS18B20_NBUS); + if (!dev) + { + ierr("ERROR: Failed to initialize arch specific 1Wire bus %d;: %d\n", + BOARD_DS18B20_NSLAVES, errno); + return -ENODEV; + } + + onewire = onewire_initialize(dev, BOARD_DS18B20_NSLAVES); + if (!onewire) + { + ierr("ERROR: Failed to initialize 1Wire bus %d;: %d\n", + BOARD_DS18B20_NBUS, errno); + return -ENODEV; + } + + return stm32_ds18b20register(onewire, BOARD_DS18B20_NSLAVES, devno); +} diff --git a/boards/arm/stm32/stm32f103-minimum/src/stm32_hyt271.c b/boards/arm/stm32/stm32f103-minimum/src/stm32_hyt271.c new file mode 100644 index 00000000000..fea6a55e5a2 --- /dev/null +++ b/boards/arm/stm32/stm32f103-minimum/src/stm32_hyt271.c @@ -0,0 +1,194 @@ +/**************************************************************************** + * boards/arm/stm32/stm32f103-minimum/src/stm32_hyt271.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 + +#include +#include +#include +#include +#include + +#include +#include +#ifdef CONFIG_SENSORS_HYT271 +# include +#endif + +#include "chip.h" +#include "arm_arch.h" +#include "stm32_i2c.h" +#include "stm32f103_minimum.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#ifndef BOARD_HYT271_NBUS +# error "Configuration BOARD_HYT271_NBUS missing" +#endif +#ifndef BOARD_HYT271_POWOUT +# error "Configuration BOARD_HYT271_POWOUT for gpio power on missing" +#endif +#ifndef BOARD_HYT271_NBUS +# error "Configuration BOARD_HYT271_POWIN for gpio power detection is missing" +#endif + +#define HYT271_NSENSORS 1 + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct stm32_i2c_bus_s +{ + struct hyt271_bus_s bus; + const int busnr; +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int stm32_i2c_power_reset(FAR struct hyt271_bus_s *bus); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const uint8_t g_hyt271_sensors[HYT271_NSENSORS] = +{ + 0x28 +}; + +static struct stm32_i2c_bus_s g_bus = +{ + .bus = + { + .pwonreset = stm32_i2c_power_reset + }, + .busnr = BOARD_HYT271_NBUS +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: stm32_i2c_power_reset + ****************************************************************************/ + +static int stm32_i2c_power_reset(FAR struct hyt271_bus_s *bus) +{ + volatile int n; + FAR struct stm32_i2c_bus_s *priv = (FAR struct stm32_i2c_bus_s *)bus; + + if (priv->busnr != BOARD_HYT271_NBUS) + { + return -EINVAL; + } + + stm32_gpiowrite(BOARD_HYT271_POWOUT, false); + usleep(250000); + stm32_gpiowrite(BOARD_HYT271_POWOUT, true); + + while (1) + { + int value; + value = stm32_gpioread(BOARD_HYT271_POWIN); + if (value != 0) + { + break; + } + } + + /* A small busy loop is necessary when using this hardware configuration + * for powering the bus. + */ + + for (n = 0; n < 25000; n++); + + return OK; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: stm32_hyt271initialize + * + * Description: + * Function used to initialize HYT271 snesors on a i2c bus + * + * Parameter: + * devno - First character device number + * + * Return + * Error or number of device that have been successfully registered. + * + ****************************************************************************/ + +int stm32_hyt271initialize(int devno) +{ + int n; + int ret; + int ndevices = 0; + FAR struct i2c_master_s *i2c; + + iinfo("Setup gpios for bus power handling on I2C%d\n", BOARD_HYT271_NBUS); + stm32_configgpio(BOARD_HYT271_POWIN); + stm32_configgpio(BOARD_HYT271_POWOUT); + stm32_gpiowrite(BOARD_HYT271_POWOUT, true); + + iinfo("Initialize I2C%d\n", BOARD_HYT271_NBUS); + i2c = stm32_i2cbus_initialize(BOARD_HYT271_NBUS); + if (!i2c) + { + ierr("ERROR: Failed to initialize I2C%d\n", BOARD_HYT271_NBUS); + return -ENODEV; + } + + ret = i2c_register(i2c, BOARD_HYT271_NBUS); + if (ret < 0) + { + ierr("ERROR: Failed to register I2C%d driver: %d\n", BOARD_HYT271_NBUS, + ret); + return -ENODEV; + } + + /* Register humidity/temperature sensors */ + + for (n = 0; n < HYT271_NSENSORS; n++) + { + ret = hyt271_register(devno + n, i2c, g_hyt271_sensors[n], + &g_bus.bus); + if (!ret) + { + ndevices++; + } + } + + return ndevices; +} diff --git a/boards/arm/stm32/stm32f103-minimum/src/stm32f103_minimum.h b/boards/arm/stm32/stm32f103-minimum/src/stm32f103_minimum.h index 96487dee369..27db124d5c2 100644 --- a/boards/arm/stm32/stm32f103-minimum/src/stm32f103_minimum.h +++ b/boards/arm/stm32/stm32f103-minimum/src/stm32f103_minimum.h @@ -187,6 +187,18 @@ #define WS2812_NLEDS 2 #define WS2812_SPI 1 +/* Sensor */ + +#define BOARD_HYT271_NBUS 2 /* Bus number of connected HYT271 */ + +#define BOARD_HYT271_POWOUT (GPIO_OUTPUT|GPIO_CNF_OUTPP|GPIO_MODE_50MHz| \ + GPIO_OUTPUT_SET|GPIO_PORTA|GPIO_PIN0) +#define BOARD_HYT271_POWIN (GPIO_INPUT|GPIO_CNF_INFLOAT|GPIO_PORTA| \ + GPIO_PIN1) + +#define BOARD_DS18B20_NBUS 2 /* Bus number of connected DS18B20 */ +#define BOARD_DS18B20_NSLAVES 2 /* Number of expected DS18B20 slaves */ + /**************************************************************************** * Public Function Prototypes ****************************************************************************/ @@ -324,6 +336,42 @@ void stm32_usbinitialize(void); int stm32_pwm_setup(void); #endif +/**************************************************************************** + * Name: stm32_hyt271initialize + * + * Description: + * Function used to initialize HYT271 snesors on a i2c bus + * + * Parameter: + * devno - First character device number + * + * Return + * Error or number of device that have been successfully registered. + * + ****************************************************************************/ + +#ifdef CONFIG_SENSORS_HYT271 +int stm32_hyt271initialize(int devno); +#endif + +/**************************************************************************** + * Name: stm32_ds18b20initialize + * + * Description: + * Function used to initialize DS18B20 snesors on a 1wire bus + * + * Parameter: + * devno - First character device number + * + * Return + * Error or number of device that have been successfully registered. + * + ****************************************************************************/ + +#ifdef CONFIG_SENSORS_DS18B20 +int stm32_ds18b20initialize(int devno); +#endif + /**************************************************************************** * Name: stm32_mfrc522initialize *