diff --git a/boards/arm/stm32/common/include/stm32_lcd_backpack.h b/boards/arm/stm32/common/include/stm32_lcd_backpack.h new file mode 100644 index 00000000000..440c7cc192d --- /dev/null +++ b/boards/arm/stm32/common/include/stm32_lcd_backpack.h @@ -0,0 +1,83 @@ +/**************************************************************************** + * boards/arm/stm32/common/include/stm32_lcd_backpack.h + * + * 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. + * + ****************************************************************************/ + +#ifndef __STM32_LCD_BACKPACK_H +#define __STM32_LCD_BACKPACK_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Type Definitions + ****************************************************************************/ + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +#ifdef __cplusplus +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +/**************************************************************************** + * Inline Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Name: board_lcd_backpack_init + * + * Description: + * Initialize the LCD1602 display controlled by Backpack with PCF8574 + * + * Input Parameters: + * devpath - The full path to the driver to register. E.g., "/dev/slcd0" + * + * Returned Value: + * Zero (OK) on success; a negated errno value on failure. + * + ****************************************************************************/ + +int board_lcd_backpack_init(int devno, int busno, int rows, int cols); + +#undef EXTERN +#ifdef __cplusplus +} +#endif + +#endif /* __STM32_LCD_BACKPACK_H */ diff --git a/boards/arm/stm32/common/src/Make.defs b/boards/arm/stm32/common/src/Make.defs index 531757f4908..2f5ac431cde 100644 --- a/boards/arm/stm32/common/src/Make.defs +++ b/boards/arm/stm32/common/src/Make.defs @@ -46,6 +46,10 @@ ifeq ($(CONFIG_AUDIO_TONE),y) CSRCS += stm32_tone.c endif +ifeq ($(CONFIG_LCD_BACKPACK),y) + CSRCS += stm32_lcd_backpack.c +endif + ifeq ($(CONFIG_LCD_SSD1306),y) CSRCS += stm32_ssd1306.c endif diff --git a/boards/arm/stm32/common/src/stm32_lcd_backpack.c b/boards/arm/stm32/common/src/stm32_lcd_backpack.c new file mode 100644 index 00000000000..bb1e58939be --- /dev/null +++ b/boards/arm/stm32/common/src/stm32_lcd_backpack.c @@ -0,0 +1,97 @@ +/**************************************************************************** + * boards/arm/stm32/common/src/stm32_lcd_backpack.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 "stm32_i2c.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: board_lcd_backpack_init + * + * Description: + * Initialize the LCD1602 display controlled by Backpack with PCF8574 + * + * Input Parameters: + * devpath - The full path to the driver to register. E.g., "/dev/slcd0" + * + * Returned Value: + * Zero (OK) on success; a negated errno value on failure. + * + ****************************************************************************/ + +int board_lcd_backpack_init(int devno, int busno, int rows, int cols) +{ + FAR struct pcf8574_lcd_backpack_config_s cfg = + LCD_I2C_BACKPACK_CFG_SAINSMART; + FAR struct i2c_master_s *i2c; + char devpath[12]; + int ret; + + /* Setup the LCD row and cols size. + * Note: We are using the LCD_I2C_BACKPACK_CFG_SAINSMART config that + * defined the I2C Address to 0x27 to PCF8574. Double check if all + * the bits (pins) from PCF8574 connected to the LCD controller + * are correct with this LCD CFG definition. + */ + + cfg.rows = rows; + cfg.cols = cols; + + /* Initialize the I2C1 */ + + i2c = stm32_i2cbus_initialize(busno); + if (i2c == NULL) + { + return -ENODEV; + } + + /* Regiter the Segment LCD */ + + snprintf(devpath, 12, "/dev/slcd%d", devno); + ret = pcf8574_lcd_backpack_register(devpath, i2c, &cfg); + if (ret < 0) + { + lcderr("ERROR: pcf8574_lcd_backpack_register(%s) failed: %d\n", + devpath, ret); + return ret; + } + + return OK; +} diff --git a/boards/arm/stm32/stm32f103-minimum/src/Make.defs b/boards/arm/stm32/stm32f103-minimum/src/Make.defs index cc86fd21590..c8d75ea8821 100644 --- a/boards/arm/stm32/stm32f103-minimum/src/Make.defs +++ b/boards/arm/stm32/stm32f103-minimum/src/Make.defs @@ -67,10 +67,6 @@ ifeq ($(CONFIG_PWM),y) CSRCS += stm32_pwm.c endif -ifeq ($(CONFIG_LCD_BACKPACK),y) - CSRCS += stm32_lcd_backpack.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 c0729ef582c..3c1c0332e6b 100644 --- a/boards/arm/stm32/stm32f103-minimum/src/stm32_bringup.c +++ b/boards/arm/stm32/stm32f103-minimum/src/stm32_bringup.c @@ -137,6 +137,10 @@ #include "board_qencoder.h" #endif +#ifdef CONFIG_LCD_BACKPACK +#include "stm32_lcd_backpack.h" +#endif + #ifdef CONFIG_USBADB #include #endif @@ -224,7 +228,9 @@ int stm32_bringup(void) #endif #ifdef CONFIG_LCD_BACKPACK - ret = stm32_lcd_backpack_init("/dev/slcd0"); + /* slcd:0, i2c:1, rows=2, cols=16 */ + + ret = board_lcd_backpack_init(0, 1, 2, 16); if (ret < 0) { syslog(LOG_ERR, "Failed to initialize PCF8574 LCD, error %d\n", ret); diff --git a/boards/arm/stm32/stm32f103-minimum/src/stm32_lcd_backpack.c b/boards/arm/stm32/stm32f103-minimum/src/stm32_lcd_backpack.c deleted file mode 100644 index 5a3fb13fdc8..00000000000 --- a/boards/arm/stm32/stm32f103-minimum/src/stm32_lcd_backpack.c +++ /dev/null @@ -1,126 +0,0 @@ -/**************************************************************************** - * boards/arm/stm32/stm32f103-minimum/src/stm32_lcd_backpack.c - * - * Copyright (C) 2019 Alan Carvalho de Assis. All rights reserved. - * Author: Alan Carvalho de Assis - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * 3. Neither the name NuttX nor the names of its contributors may be - * used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN - * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - ****************************************************************************/ - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include - -#include -#include - -#include -#include - -#include "stm32.h" -#include "stm32f103_minimum.h" - -#if defined(CONFIG_I2C) && defined(CONFIG_LCD_BACKPACK) - -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -#define LCDBP_I2C_PORTNO 1 /* PCF8574 connected to I2C1 */ - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: stm32_lcd_backpack_init - * - * Description: - * Initialize the LCD1602 display controlled by Backpack with PCF8574 - * - * Input Parameters: - * devpath - The full path to the driver to register. E.g., "/dev/slcd0" - * - * Returned Value: - * Zero (OK) on success; a negated errno value on failure. - * - ****************************************************************************/ - -int stm32_lcd_backpack_init(FAR const char *devpath) -{ - FAR struct pcf8574_lcd_backpack_config_s cfg = - LCD_I2C_BACKPACK_CFG_SAINSMART; - FAR struct i2c_master_s *i2c; - int ret; - - /* Setup the LCD row and cols size. - * Note: We are using the LCD_I2C_BACKPACK_CFG_SAINSMART config that - * defined the I2C Address to 0x27 to PCF8574. Double check if all - * the bits (pins) from PCF8574 connected to the LCD controller - * are correct with this LCD CFG definition. - */ - - cfg.rows = 2; - cfg.cols = 16; - - /* Initialize the I2C1 */ - - i2c = stm32_i2cbus_initialize(LCDBP_I2C_PORTNO); - if (i2c == NULL) - { - return -ENODEV; - } - -#ifdef CONFIG_I2C_DRIVER - /* Register the I2C to get the "nsh> i2c bus" command working */ - - ret = i2c_register(i2c, LCDBP_I2C_PORTNO); - if (ret < 0) - { - rtcerr("ERROR: Failed to register I2C driver: %d\n", ret); - return -ENODEV; - } -#endif - - /* Register the LCD BACKPACK PCF8574 driver at the specified location. */ - - ret = pcf8574_lcd_backpack_register(devpath, i2c, &cfg); - if (ret < 0) - { - lcderr("ERROR: pcf8574_lcd_backpack_register(%s) failed: %d\n", - devpath, ret); - return ret; - } - - return OK; -} - -#endif /* CONFIG_I2C && CONFIG_LCD_BACKPACK */ diff --git a/boards/arm/stm32/stm32f103-minimum/src/stm32f103_minimum.h b/boards/arm/stm32/stm32f103-minimum/src/stm32f103_minimum.h index 9e352cfaab9..31d5c2da41c 100644 --- a/boards/arm/stm32/stm32f103-minimum/src/stm32f103_minimum.h +++ b/boards/arm/stm32/stm32f103-minimum/src/stm32f103_minimum.h @@ -317,18 +317,6 @@ int stm32_rgbled_setup(void); int stm32_mcp2515initialize(FAR const char *devpath); #endif -/**************************************************************************** - * Name: stm32_lcd_backpack_init - * - * Description: - * Initialize and register the PCF8574 LCD Backpack driver. - * - ****************************************************************************/ - -#ifdef CONFIG_LCD_BACKPACK -int stm32_lcd_backpack_init(FAR const char *devpath); -#endif - /**************************************************************************** * Name: stm32_usbinitialize *