From dc270372afaf17395502dc6bed7bed8417f6a14e Mon Sep 17 00:00:00 2001 From: Martin Jaeger <17674105+martinjaeger@users.noreply.github.com> Date: Tue, 26 Nov 2019 12:12:09 +0100 Subject: [PATCH] OLED display integrated with Zephyr - Some fixes in DTS and pinmux settings - Supporting different I2C address definitions of mbed and zephyr - Thread for ext_mgr added --- .../arm/mppt1210hus_0v4/mppt1210hus_0v4.dts | 4 +- boards/arm/mppt1210hus_0v4/pinmux.c | 17 +++++- lib/Adafruit_GFX/OledSSD1306.cpp | 60 +++++++++++++++++-- lib/Adafruit_GFX/OledSSD1306.h | 47 ++++++++------- platformio.ini | 4 +- src/ext/oled.cpp | 24 +++++--- src/main_zephyr.cpp | 43 +++++++++---- zephyr/CMakeLists.txt | 39 +++++++----- zephyr/prj.conf | 3 +- 9 files changed, 173 insertions(+), 68 deletions(-) diff --git a/boards/arm/mppt1210hus_0v4/mppt1210hus_0v4.dts b/boards/arm/mppt1210hus_0v4/mppt1210hus_0v4.dts index f12bbaa..7adc46c 100755 --- a/boards/arm/mppt1210hus_0v4/mppt1210hus_0v4.dts +++ b/boards/arm/mppt1210hus_0v4/mppt1210hus_0v4.dts @@ -66,12 +66,12 @@ status = "okay"; }; -arduino_i2c: &i2c1 { +&i2c1 { status = "okay"; clock-frequency = ; }; -arduino_spi: &spi1 { +&spi1 { status = "okay"; }; diff --git a/boards/arm/mppt1210hus_0v4/pinmux.c b/boards/arm/mppt1210hus_0v4/pinmux.c index eb63b0d..c096877 100755 --- a/boards/arm/mppt1210hus_0v4/pinmux.c +++ b/boards/arm/mppt1210hus_0v4/pinmux.c @@ -12,7 +12,18 @@ #include -/* pin assignments for Libre Solar MPPT board */ +// preliminary fix until PR for pinmux_stm32l0.h got accepted and merged to Zephyr master branch, +// see also here: https://github.com/zephyrproject-rtos/zephyr/pull/20997 +#ifndef STM32L0_PINMUX_FUNC_PB6_I2C1_SCL +#define STM32L0_PINMUX_FUNC_PB6_I2C1_SCL \ + (STM32_PINMUX_ALT_FUNC_1 | STM32_OPENDRAIN_PULLUP) +#endif +#ifndef STM32L0_PINMUX_FUNC_PB7_I2C1_SDA +#define STM32L0_PINMUX_FUNC_PB7_I2C1_SDA \ + (STM32_PINMUX_ALT_FUNC_1 | STM32_OPENDRAIN_PULLUP) +#endif + +/* pin assignments for NUCLEO-L073RZ board */ static const struct pin_config pinconf[] = { #ifdef CONFIG_UART_1 {STM32_PIN_PA9, STM32L0_PINMUX_FUNC_PA9_USART1_TX}, @@ -23,8 +34,8 @@ static const struct pin_config pinconf[] = { {STM32_PIN_PA3, STM32L0_PINMUX_FUNC_PA3_USART2_RX}, #endif /* CONFIG_UART_2 */ #ifdef CONFIG_I2C_1 - {STM32_PIN_PB8, STM32L0_PINMUX_FUNC_PB8_I2C1_SCL}, - {STM32_PIN_PB9, STM32L0_PINMUX_FUNC_PB9_I2C1_SDA}, + {STM32_PIN_PB6, STM32L0_PINMUX_FUNC_PB6_I2C1_SCL}, + {STM32_PIN_PB7, STM32L0_PINMUX_FUNC_PB7_I2C1_SDA}, #endif /* CONFIG_I2C_1 */ #ifdef CONFIG_SPI_1 {STM32_PIN_PA5, STM32L0_PINMUX_FUNC_PA5_SPI1_SCK}, diff --git a/lib/Adafruit_GFX/OledSSD1306.cpp b/lib/Adafruit_GFX/OledSSD1306.cpp index f911b43..2a6a83f 100644 --- a/lib/Adafruit_GFX/OledSSD1306.cpp +++ b/lib/Adafruit_GFX/OledSSD1306.cpp @@ -9,7 +9,6 @@ * Significant modifications were done by Martin Jäger (11/2019) to use it also in Zephyr */ -#include "mbed.h" #include "OledSSD1306.h" #include "logo.h" @@ -35,10 +34,31 @@ #define SSD1306_SEGREMAP 0xA0 #define SSD1306_CHARGEPUMP 0x8D -OledSSD1306::OledSSD1306(I2C &i2c, uint8_t i2cAddress, uint8_t brightness) : +#if defined(__MBED__) + +OledSSD1306::OledSSD1306(I2C &i2c, uint8_t i2cAddress) : Adafruit_GFX(SSD1306_WIDTH, SSD1306_HEIGHT), - mi2c(i2c), - mi2cAddress(i2cAddress) + _i2c(i2c), + _i2cAddress(i2cAddress) +{ + ; +} + +#elif defined(__ZEPHYR__) + +OledSSD1306::OledSSD1306(const char *i2c_name, uint8_t i2cAddress) : + Adafruit_GFX(SSD1306_WIDTH, SSD1306_HEIGHT), + _i2cAddress(i2cAddress) +{ + _i2c = device_get_binding(i2c_name); + if (!_i2c) { + printk("I2C: Device driver not found.\n"); + } +} + +#endif + +void OledSSD1306::init(uint8_t brightness) { command(SSD1306_DISPLAYOFF); command(SSD1306_SETDISPLAYCLOCKDIV); @@ -146,9 +166,33 @@ void OledSSD1306::splash(void) std::copy(&logo[0], &logo[0] + sizeof(logo), buffer.begin()); } +void OledSSD1306::command(uint8_t c) +{ + uint8_t buf[2]; + buf[0] = 0; // Command Mode + buf[1] = c; +#if defined(__MBED__) + _i2c.write(_i2cAddress, (char *)buf, sizeof(buf)); +#elif defined(__ZEPHYR__) + i2c_write(_i2c, buf, sizeof(buf), _i2cAddress); +#endif +} + +void OledSSD1306::data(uint8_t c) +{ + uint8_t buf[2]; + buf[0] = 0x40; // Data Mode + buf[1] = c; +#if defined(__MBED__) + _i2c.write(_i2cAddress, (char *)buf, sizeof(buf)); +#elif defined(__ZEPHYR__) + i2c_write(_i2c, buf, sizeof(buf), _i2cAddress); +#endif +}; + void OledSSD1306::sendBuffer() { - char buf[17]; + uint8_t buf[17]; buf[0] = 0x40; // Data Mode // send display buffer in 16 byte chunks @@ -160,6 +204,10 @@ void OledSSD1306::sendBuffer() buf[x] = buffer[pos]; } } - mi2c.write(mi2cAddress, buf, sizeof(buf)); +#if defined(__MBED__) + _i2c.write(_i2cAddress, (char *)buf, sizeof(buf)); +#elif defined(__ZEPHYR__) + i2c_write(_i2c, buf, sizeof(buf), _i2cAddress); +#endif } }; diff --git a/lib/Adafruit_GFX/OledSSD1306.h b/lib/Adafruit_GFX/OledSSD1306.h index fac9d41..c3cf914 100644 --- a/lib/Adafruit_GFX/OledSSD1306.h +++ b/lib/Adafruit_GFX/OledSSD1306.h @@ -9,16 +9,22 @@ * Significant modifications were done by Martin Jäger (11/2019) to use it also in Zephyr */ -#ifndef _OledSSD1306_H_ -#define _OledSSD1306_H_ +#ifndef OLEDSSD1306_H_ +#define OLEDSSD1306_H_ +#if defined(__MBED__) #include "mbed.h" +#elif defined(__ZEPHYR__) +#include +#include +#endif + #include "Adafruit_GFX.h" #include #include -#define SSD1306_I2C_ADDRESS 0x78 +#define SSD1306_I2C_ADDRESS 0x3c // needs to be left-shifted by 1 for mbed (=0x78) #define SSD1306_WIDTH 128 #define SSD1306_HEIGHT 64 @@ -37,7 +43,13 @@ public: * @param i2cAddress The i2c address of the display * @param brightness Sets contrast between 0x01-0xFF or 1-255 */ - OledSSD1306(I2C &i2c, uint8_t i2cAddress = SSD1306_I2C_ADDRESS, uint8_t brightness = 0x01); +#if defined(__MBED__) + OledSSD1306(I2C &i2c, uint8_t i2cAddress = (SSD1306_I2C_ADDRESS << 1)); +#elif defined(__ZEPHYR__) + OledSSD1306(const char *i2c_name, uint8_t i2cAddress = SSD1306_I2C_ADDRESS); +#endif + + void init(uint8_t brightness = 0x01); void drawPixel(int16_t x, int16_t y, uint16_t color); @@ -51,29 +63,22 @@ public: /// Fill the buffer with the logo splash screen. void splash(); - void command(uint8_t c) - { - char buff[2]; - buff[0] = 0; // Command Mode - buff[1] = c; - mi2c.write(mi2cAddress, buff, sizeof(buff)); - } + void command(uint8_t c); - void data(uint8_t c) - { - char buff[2]; - buff[0] = 0x40; // Data Mode - buff[1] = c; - mi2c.write(mi2cAddress, buff, sizeof(buff)); - }; + void data(uint8_t c); protected: void sendBuffer(); std::array buffer; - I2C &mi2c; - uint8_t mi2cAddress; +#if defined(__MBED__) + I2C &_i2c; +#elif defined(__ZEPHYR__) + struct device *_i2c; +#endif + + uint8_t _i2cAddress; }; -#endif \ No newline at end of file +#endif // OLEDSSD1306_H_ diff --git a/platformio.ini b/platformio.ini index 8dd0881..b4e4143 100644 --- a/platformio.ini +++ b/platformio.ini @@ -109,8 +109,8 @@ build_flags = -fsingle-precision-constant -Wl,-Map,memory.map platform = https://github.com/platformio/platform-ststm32.git -lib_ignore = ESP32, Adafruit_GFX -extra_scripts = +lib_ignore = ESP32 +extra_scripts = generate_version_file.py # https://github.com/LibreSolar/PWM-2420-LUS [env:pwm-2420-lus-v0.2] diff --git a/src/ext/oled.cpp b/src/ext/oled.cpp index 0e9ee7e..4da3df5 100644 --- a/src/ext/oled.cpp +++ b/src/ext/oled.cpp @@ -14,12 +14,14 @@ * limitations under the License. */ -#ifdef __MBED__ +#ifndef UNIT_TEST #include "config.h" #ifdef OLED_ENABLED // otherwise don't compile code to reduce firmware size +#include + #include "ext/ext.h" #include "main.h" @@ -56,12 +58,12 @@ const unsigned char bmp_disconnected [] = { 0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; -I2C i2c(PIN_UEXT_SDA, PIN_UEXT_SCL); - -#ifdef OLED_BRIGHTNESS -OledSSD1306 oled(i2c, 0x78, OLED_BRIGHTNESS); -#else -OledSSD1306 oled(i2c, 0x78); // use default (lowest brightness) +#if defined(__MBED__) +I2C i2c_dev(PIN_UEXT_SDA, PIN_UEXT_SCL); +OledSSD1306 oled(i2c_dev); +#elif defined(__ZEPHYR__) +const char i2c_dev[] = "I2C_1"; +OledSSD1306 oled(i2c_dev); #endif void ExtOled::enable() @@ -70,6 +72,12 @@ void ExtOled::enable() DigitalOut uext_dis(PIN_UEXT_DIS); uext_dis = 0; #endif + +#ifdef OLED_BRIGHTNESS + oled.init(OLED_BRIGHTNESS); +#else + oled.init(); // use default (lowest brightness) +#endif } void ExtOled::process_1s() @@ -195,4 +203,4 @@ void ExtOled::process_1s() #endif /* OLED_ENABLED */ -#endif /* __MBED__ */ +#endif /* UNIT_TEST */ diff --git a/src/main_zephyr.cpp b/src/main_zephyr.cpp index b04606b..5d23a7e 100644 --- a/src/main_zephyr.cpp +++ b/src/main_zephyr.cpp @@ -37,7 +37,6 @@ #include "leds.h" // LED switching using charlieplexing #include "device_status.h" // log data (error memory, min/max measurements, etc.) #include "data_objects.h" // for access to internal data via ThingSet -//#include "thingset_serial.h" // UART or USB serial communication PowerPort lv_terminal; // low voltage terminal (battery for typical MPPT) @@ -82,26 +81,50 @@ time_t timestamp; // current unix timestamp (independent of time(NULL), as it void main(void) { - u32_t cnt = 0; - struct device *dev_usb_en; + u32_t cnt = 0; + struct device *dev_usb_en; - dev_usb_en = device_get_binding(USB_PWR_EN_PORT); - gpio_pin_configure(dev_usb_en, USB_PWR_EN_PIN, GPIO_DIR_OUT); + dev_usb_en = device_get_binding(USB_PWR_EN_PORT); + gpio_pin_configure(dev_usb_en, USB_PWR_EN_PIN, GPIO_DIR_OUT); gpio_pin_write(dev_usb_en, USB_PWR_EN_PIN, 1); printf("Booting Libre Solar Charge Controller: %s\n", CONFIG_BOARD); + // initialize all extensions and external communication interfaces + ext_mgr.enable_all(); + + k_sleep(2000); // safety feature: be able to re-flash before starting + while (1) { - gpio_pin_write(dev_usb_en, USB_PWR_EN_PIN, cnt % 2); - cnt++; - k_sleep(5000); + //gpio_pin_write(dev_usb_en, USB_PWR_EN_PIN, cnt % 2); + + leds_update_1s(); + leds_update_soc(charger.soc, dev_stat.has_error(ERR_LOAD_LOW_SOC)); + + cnt++; + k_sleep(1000); } } -//K_THREAD_DEFINE(ts_serial_id, 4096, thingset_serial_thread, NULL, NULL, NULL, 5, 0, K_NO_WAIT); +void ext_mgr_thread() +{ + u32_t cnt = 0; + uint32_t last_call = 0; + while (1) { + uint32_t now = k_uptime_get() / 1000; + ext_mgr.process_asap(); // approx. every millisecond + if (now >= last_call + 1) { + last_call = now; + ext_mgr.process_1s(); + printf("%d\n", now); + } + cnt++; + k_sleep(1); + } +} K_THREAD_DEFINE(leds_id, 256, leds_update_thread, NULL, NULL, NULL, 4, 0, K_NO_WAIT); -//K_THREAD_DEFINE(uext_thread, 1024, uext_process_1s, NULL, NULL, NULL, 6, 0, K_NO_WAIT); +K_THREAD_DEFINE(ext_thread, 1024, ext_mgr_thread, NULL, NULL, NULL, 6, 0, 1000); #endif // __ZEPHYR__ diff --git a/zephyr/CMakeLists.txt b/zephyr/CMakeLists.txt index c4f5b93..3d29fdb 100644 --- a/zephyr/CMakeLists.txt +++ b/zephyr/CMakeLists.txt @@ -1,6 +1,8 @@ # SPDX-License-Identifier: Apache-2.0 -#set(BOARD mppt1210hus_0v4) +option(USE_PLATFORMIO "Use PlatformIO build system" OFF) + +set(BOARD mppt1210hus_0v4) set(BOARD_ROOT ${CMAKE_SOURCE_DIR}/..) cmake_minimum_required(VERSION 3.13.1) @@ -10,26 +12,33 @@ project(libre_solar_charge_controller) add_definitions(-DMPPT_1210_HUS_0V4 -D__ZEPHYR__ -DDEBUG_PRINT_FLAGS=7) include_directories(../src) -#include_directories(../.pio/libdeps/mppt-1210-hus-v0.7/ThingSet/src) +include_directories(../lib/Adafruit_GFX) -#target_sources(app PRIVATE ../src/adc_dma.cpp) +target_sources(app PRIVATE ../src/adc_dma.cpp) target_sources(app PRIVATE ../src/bat_charger.cpp) -#target_sources(app PRIVATE ../src/data_objects.cpp) +target_sources(app PRIVATE ../src/data_objects.cpp) target_sources(app PRIVATE ../src/device_status.cpp) target_sources(app PRIVATE ../src/dcdc.cpp) -#target_sources(app PRIVATE ../src/eeprom.cpp) +target_sources(app PRIVATE ../src/eeprom.cpp) target_sources(app PRIVATE ../src/half_bridge.cpp) -#target_sources(app PRIVATE ../src/hardware.cpp) -#target_sources(app PRIVATE ../src/helper.cpp) -#target_sources(app PRIVATE ../src/leds.cpp) -#target_sources(app PRIVATE ../src/load.cpp) +target_sources(app PRIVATE ../src/hardware.cpp) +target_sources(app PRIVATE ../src/leds.cpp) +target_sources(app PRIVATE ../src/load.cpp) target_sources(app PRIVATE ../src/main_zephyr.cpp) target_sources(app PRIVATE ../src/power_port.cpp) target_sources(app PRIVATE ../src/pwm_switch.cpp) -#target_sources(app PRIVATE ../src/thingset_serial.cpp) +target_sources(app PRIVATE ../src/ext/ext.cpp) +target_sources(app PRIVATE ../src/ext/oled.cpp) -#target_sources(app PRIVATE ../.pio/libdeps/mppt-1210-hus-v0.7/ThingSet/src/thingset.cpp) -#target_sources(app PRIVATE ../.pio/libdeps/mppt-1210-hus-v0.7/ThingSet/src/thingset_cbor.cpp) -#target_sources(app PRIVATE ../.pio/libdeps/mppt-1210-hus-v0.7/ThingSet/src/cbor.c) -#target_sources(app PRIVATE ../.pio/libdeps/mppt-1210-hus-v0.7/ThingSet/src/thingset_json.cpp) -#target_sources(app PRIVATE ../.pio/libdeps/mppt-1210-hus-v0.7/ThingSet/src/jsmn.c) +target_sources(app PRIVATE ../lib/Adafruit_GFX/Adafruit_GFX.cpp) +target_sources(app PRIVATE ../lib/Adafruit_GFX/OledSSD1306.cpp) + +if (NOT USE_PLATFORMIO) + # manually include ThingSet library (you need to run platformio once to download library) + include_directories(../.pio/libdeps/mppt-1210-hus-v0.4-zephyr/ThingSet/src) + target_sources(app PRIVATE ../.pio/libdeps/mppt-1210-hus-v0.7/ThingSet/src/thingset.cpp) + target_sources(app PRIVATE ../.pio/libdeps/mppt-1210-hus-v0.7/ThingSet/src/thingset_cbor.cpp) + target_sources(app PRIVATE ../.pio/libdeps/mppt-1210-hus-v0.7/ThingSet/src/cbor.c) + target_sources(app PRIVATE ../.pio/libdeps/mppt-1210-hus-v0.7/ThingSet/src/thingset_json.cpp) + target_sources(app PRIVATE ../.pio/libdeps/mppt-1210-hus-v0.7/ThingSet/src/jsmn.c) +endif() diff --git a/zephyr/prj.conf b/zephyr/prj.conf index fbb65a8..15784e3 100644 --- a/zephyr/prj.conf +++ b/zephyr/prj.conf @@ -2,12 +2,13 @@ CONFIG_STDOUT_CONSOLE=y # default is 1024 -CONFIG_HEAP_MEM_POOL_SIZE=0 +CONFIG_HEAP_MEM_POOL_SIZE=1024 CONFIG_GPIO=y CONFIG_SERIAL=y CONFIG_UART_1=y CONFIG_I2C=y +CONFIG_I2C_1=y # for OLED display #CONFIG_DISPLAY=y