Copyright © https://mongoose-os.com

Mongoose OS Forum

frame
ATTENTION! This forum has moved to:

https://community.mongoose-os.com

Do not post any new messages.

Porting the Adafruit_MPL3115A2 library for Arduino, the application fail within read8() method

ShogoShogo Osaka, Japan
edited February 10 in Mongoose OS

Hi, there. Im Shogo from Japan.

I am trying to run the Adafruit MPL31152A board on the Mongoose OS. Since Arduino library is provided from Adafruit, we attempted porting, but the Mongoose OS panics in read8() method of Adafruit_MPL3115A2 class and dump is output.

[Feb 10 19:22:05.126] mgos_init2           app3 1.0 (20190210-095517)
[Feb 10 19:22:05.131] mgos_init2           Mongoose OS 201902090151 (20190209-015104)
[Feb 10 19:22:05.137] mgos_init2           CPU: 160 MHz, heap: 290340 total, 259160 free
[Feb 10 19:22:05.142] mgos_init2           Newlib 2.2.0
[Feb 10 19:22:05.142] mgos_hal_freertos_pr ESP-IDF v3.2-r1
[Feb 10 19:22:05.148] mgos_hal_freertos_pr Boot partition: app_0; flash: 4M
[Feb 10 19:22:05.153] mg_lwip_if_init      Mongoose 6.13, LwIP 2.0.3
[Feb 10 19:22:05.160] mg_ssl_if_init       mbed TLS 2.13.1-cesanta2
[Feb 10 19:22:05.160] mgos_vfs_dev_create_ fs_0: esp32part ({"label": "fs_0"}), size 262144
[Feb 10 19:22:05.166] mgos_vfs_dev_create_ fs_1: esp32part ({"label": "fs_1"}), size 262144
[Feb 10 19:22:05.177] mgos_vfs_mount_dev   /: SPIFFS @ fs_0, opts {"bs": 4096, "ps": 256, "es": 4096}
[Feb 10 19:22:05.273] mgos_vfs_print_fs_in /: size 233681, used: 27861, free: 205820
[Feb 10 19:22:05.337] mgos_sys_config_init MAC: 30AEA41FC3E0
[Feb 10 19:22:05.353] mgos_sys_config_init WDT: 30 seconds
[Feb 10 19:22:05.447] Hello, Arduino world!
[Feb 10 19:22:05.447] Guru Meditation Error: Core  0 panic'ed (LoadProhibited). Exception was unhandled.
[Feb 10 19:22:05.452] Core 0 register dump:
[Feb 10 19:22:05.457] PC      : 0x40171a41  PS      : 0x00060530  A0      : 0x800e5ad5  A1      : 0x3ffb4c30  
[Feb 10 19:22:05.463] A2      : 0x00000001  A3      : 0x00000060  A4      : 0x00000001  A5      : 0x3ffae910  
[Feb 10 19:22:05.474] A6      : 0x00000000  A7      : 0x00000000  A8      : 0x3ffb4f60  A9      : 0x00000000  
[Feb 10 19:22:05.479] A10     : 0x3ffb4f67  A11     : 0x00000060  A12     : 0x00000001  A13     : 0x00000001  
[Feb 10 19:22:05.484] A14     : 0x00000001  A15     : 0x00000031  SAR     : 0x0000000f  EXCCAUSE: 0x0000001c  
[Feb 10 19:22:05.495] EXCVADDR: 0x00000000  LBEG    : 0x40083880  LEND    : 0x4008388a  LCOUNT  : 0x00000000  
[Feb 10 19:22:05.500] 
[Feb 10 19:22:05.500] Backtrace: 0x40171a41 0x400e5ad2 0x400e5aed 0x400e5b1e 0x400f0fd3 0x400e5be4 0x400e266f 0x400e24ba 0x40082f54
[Feb 10 19:22:05.511] 
[Feb 10 19:22:05.511] --- BEGIN CORE DUMP ---
[Feb 10 19:22:05.517] mos: catching core dump
[Feb 10 19:22:08.369] ......

read8 Method:

/**************************************************************************/
/*!
@brief read 1 byte of data at the specified address
@param a the address to read
@return the read data byte
*/
/**************************************************************************/
uint8_t Adafruit_MPL3115A2::read8(uint8_t a) {

    _i2c->beginTransmission(MPL3115A2_ADDRESS); // start transmission to device 
    _i2c->write(a); // sends register address to read from
    _i2c->endTransmission(false); // end transmission
    _i2c->requestFrom((uint8_t)MPL3115A2_ADDRESS, (uint8_t)1);// send data n-bytes read

    return _i2c->read(); // receive DATA

}

As a result of various investigations, it was dropped by the call of _i2c-> read () method. Then I could not follow. I did not know why it falls on the call of the Wire class read ().
Could you tell me the reason why someone will fall down with _i2c->read ()?

In terms of hardware, sda and scl of the Adafruit_MPL 3115A2 board are connected to IO 25 and IO 26.
We also enabled i2c with mos.yml and confirmed that execution of _i2c -> begin () was successful in begin () of Adafruit_MPL3115A2. Where the Read 8 method is dropped
Adafruit_MPL3115A2 :: begin () is the following part.

/**************************************************************************/
/*!
@brief Setups the HW (reads coefficients values, etc.)
@param twoWire Optional TwoWire I2C object
@return true on successful startup, false otherwise
*/
/**************************************************************************/
boolean Adafruit_MPL3115A2::begin(TwoWire *twoWire) {
    _i2c = twoWire;
    _i2c->begin();
    uint8_t whoami = read8(MPL3115A2_WHOAMI); ←here!
    if (whoami != 0xC4) {
        return false;
    }
 .........

I do not know how to follow the code of the Wire library and I do not know at all what the point of _i 2 c -> read () is.

Thank you for your consideration.

Comments

  • ShogoShogo Osaka, Japan
  • nliviunliviu Romania
    edited February 10

    It looks like you are using Arduino's setup function.
    setup is called in mgos_arduino_compat_init and the i2c library is not initialized yet.

    Move your setup code in mgos_app_init

    extern "C" enum mgos_app_init_result mgos_app_init(void) {
      /* your setup code here */
    
      return MGOS_APP_INIT_SUCCESS;
    }
    

    You might need to add

    #include <mgos_app.h>
    

    You also might need

    config_schema:
      - ["i2c.enable", true]
    
  • ShogoShogo Osaka, Japan

    Hi, nliviu
    Thank you for your reply! I appreciate your advice!

    When checking mgos_deps_init.c, it became as follows.

    /* This file is auto-generated by mos build, do not edit! */
    
    #include <stdbool.h>
    #include <stdio.h>
    
    #include "common/cs_dbg.h"
    
    #include "mgos_app.h"
    
    
    extern bool mgos_mongoose_init(void);
    extern bool mgos_vfs_common_init(void);
    extern bool mgos_vfs_fs_lfs_init(void);
    extern bool mgos_vfs_fs_spiffs_init(void);
    extern bool mgos_core_init(void);
    extern bool mgos_arduino_compat_init(void);
    extern bool mgos_i2c_init(void);
    extern bool mgos_atca_init(void);
    extern bool mgos_mbedtls_init(void);
    
    static const struct lib_descr {
      const char *title;
      bool (*init)(void);
    } descrs[] = {
    
        // "mongoose". deps: [ ]
        {.title = "mongoose", .init = mgos_mongoose_init},
    
        // "vfs-common". deps: [ ]
        {.title = "vfs-common", .init = mgos_vfs_common_init},
    
        // "vfs-fs-lfs". deps: [ "vfs-common" ]
        {.title = "vfs-fs-lfs", .init = mgos_vfs_fs_lfs_init},
    
        // "vfs-fs-spiffs". deps: [ "vfs-common" ]
        {.title = "vfs-fs-spiffs", .init = mgos_vfs_fs_spiffs_init},
    
        // "core". deps: [ "mongoose" "vfs-common" "vfs-fs-lfs" "vfs-fs-spiffs" ]
        {.title = "core", .init = mgos_core_init},
    
        // "arduino-compat". deps: [ "core" ]
        {.title = "arduino-compat", .init = mgos_arduino_compat_init},
    
        // "i2c". deps: [ "core" ]
        {.title = "i2c", .init = mgos_i2c_init},
    
        // "atca". deps: [ "i2c" ]
        {.title = "atca", .init = mgos_atca_init},
    
        // "mbedtls". deps: [ ]
        {.title = "mbedtls", .init = mgos_mbedtls_init},
    
    };
    
    bool mgos_deps_init(void) {
      size_t i;
      for (i = 0; i < sizeof(descrs) / sizeof(struct lib_descr); i++) {
        LOG(LL_DEBUG, ("init %s...", descrs[i].title));
        if (!descrs[i].init()) {
          LOG(LL_ERROR, ("%s init failed", descrs[i].title));
          return false;
        }
      }
    
      return true;
    }
    

    Is not this strange?

        // "arduino-compat". deps: [ "core" ]
        {.title = "arduino-compat", .init = mgos_arduino_compat_init},
    
        // "i2c". deps: [ "core" ]
        {.title = "i2c", .init = mgos_i2c_init},
    

    As a result of examination, I confirmed that the mgos_i2c_init () method will be executed after arduino-compat setup () method is executed. Therefore, since the Wire library used in the setup () method uses the i2c library, eventually the i2c module is executed without being initialized, so it seems that mongoose os was dropped.

    When you enable the arduino-compat library,
    1, mgos_arduino_compat_init
    2, mgos_i2c_init
    mgos_deps_init.c is generated in this execution order.

    Is not this strange?
    Can this be fixed by me?

  • AFAIK, you can't.

    Follow my advice and it will work.

  • ShogoShogo Osaka, Japan

    Hi, nliviu

    I modified main.cpp code.

    #include <mgos_app.h>
    #include <Arduino.h>
    #include <Wire.h>
    #include <Adafruit_MPL3115A2.h>
    
    #define LED_PIN_NO 23
    
    Adafruit_MPL3115A2 Baro = Adafruit_MPL3115A2();
    
    extern "C" enum mgos_app_init_result mgos_app_init(void) {
      pinMode(LED_PIN_NO, OUTPUT);
      printf("Hello, Arduino world!\r\n");
      //pinMode(5, OUTPUT);
    
      while (Baro.begin(&Wire) != true) {
        delay(500);
        printf(".");
      }
    
      printf("Sensor found.\r\n");
    
      return MGOS_APP_INIT_SUCCESS;
    }
    
    void setup(void){
    
    }
    
    void loop() {
      static boolean value = 0;
      digitalWrite(LED_PIN_NO, value);
      //digitalWrite(5, value);
      printf("%s\r\n", (value == 0 ? "Tick" : "Tock"));
      delay(500);
      value = (value ? 0 : 1);
    }
    

    This is log.

    [Feb 16 18:15:29.134] mgos_init2           app4 1.0 (20190216-091241)
    [Feb 16 18:15:29.134] mgos_init2           Mongoose OS 2.11.0 (20190216-091240/2.11.0-g640ebcb65)
    [Feb 16 18:15:29.134] mgos_init2           CPU: 160 MHz, heap: 290444 total, 259264 free
    [Feb 16 18:15:29.134] mgos_init2           Newlib 2.2.0
    [Feb 16 18:15:29.134] mgos_hal_freertos_pr ESP-IDF v3.2-r1
    [Feb 16 18:15:29.138] mgos_hal_freertos_pr Boot partition: app_0; flash: 4M
    [Feb 16 18:15:29.143] mg_lwip_if_init      Mongoose 6.13, LwIP 2.0.3
    [Feb 16 18:15:29.147] mg_ssl_if_init       mbed TLS 2.13.1-cesanta4
    [Feb 16 18:15:29.155] mgos_vfs_dev_create_ fs_0: esp32part ({"label": "fs_0"}), size 262144
    [Feb 16 18:15:29.161] mgos_vfs_dev_create_ fs_1: esp32part ({"label": "fs_1"}), size 262144
    [Feb 16 18:15:29.168] mgos_vfs_mount_dev   /: SPIFFS @ fs_0, opts {"bs": 4096, "ps": 256, "es": 4096}
    [Feb 16 18:15:29.265] mgos_vfs_print_fs_in /: size 233681, used: 27861, free: 205820
    [Feb 16 18:15:29.325] mgos_sys_config_init MAC: 30AEA402A8FC
    [Feb 16 18:15:29.330] mgos_sys_config_init WDT: 30 seconds
    [Feb 16 18:15:29.339] mgos_i2c_create      I2C0 init ok (SDA: 18, SCL: 19, freq: 100000)
    [Feb 16 18:15:29.344] mg_rpc_channel_uart  0x3ffc1740 UART0
    [Feb 16 18:15:29.348] mgos_wifi_setup      WiFi mode: AP
    [Feb 16 18:15:29.352] esp32_wifi_set_mode  WiFi mode: AP
    [Feb 16 18:15:29.361] I (751) wifi: wifi driver task: 3ffc2e88, prio:23, stack:3584, core=0
    [Feb 16 18:15:29.365] I (761) wifi: wifi firmware version: e6431f2
    [Feb 16 18:15:29.369] I (761) wifi: config NVS flash: enabled
    [Feb 16 18:15:29.373] I (771) wifi: config nano formating: disabled
    [Feb 16 18:15:29.391] I (781) wifi: Init dynamic tx buffer num: 32
    [Feb 16 18:15:29.396] I (791) wifi: Init data frame dynamic rx buffer num: 64
    [Feb 16 18:15:29.401] I (791) wifi: Init management frame dynamic rx buffer num: 64
    [Feb 16 18:15:29.406] I (801) wifi: Init static rx buffer size: 1600
    [Feb 16 18:15:29.410] I (801) wifi: Init static rx buffer num: 10
    [Feb 16 18:15:29.410] I (811) wifi: Init dynamic rx buffer num: 0
    [Feb 16 18:15:29.424] [0;33mW (811) phy_init: failed to load RF calibration data (0x1102), falling back to full calibration[0m
    [Feb 16 18:15:29.591] [0;32mI (981) phy: phy_version: 4000, b6198fa, Sep  3 2018, 15:11:06, 0, 2[0m
    [Feb 16 18:15:29.599] I (991) wifi: mode : softAP (30:ae:a4:02:a8:fd)
    [Feb 16 18:15:29.604] I (1001) wifi: Init max length of beacon: 752/752
    [Feb 16 18:15:29.609] I (1001) wifi: Init max length of beacon: 752/752
    [Feb 16 18:15:29.613] I (1011) wifi: Set ps type: 0
    [Feb 16 18:15:29.613] 
    [Feb 16 18:15:29.619] mgos_wifi_add_mode   cur mode: 2
    [Feb 16 18:15:29.625] mgos_wifi_dev_ap_set WiFi AP: SSID Mongoose_02A8FC, channel 6
    [Feb 16 18:15:30.486] mgos_wifi_dev_ap_set WiFi AP IP: 192.168.4.1/255.255.255.0 gw 192.168.4.1, DHCP range 192.168.4.2 - 192.168.4.100
    [Feb 16 18:15:30.492] mgos_wifi_dev_ap_set WiFi AP: SSID Mongoose_02A8FC, channel 6
    [Feb 16 18:15:30.497] Hello, Arduino world!
    [Feb 16 18:15:59.420] [0;31mE (30731) task_wdt: Task watchdog got triggered. The following tasks did not reset the watchdog in time:[0m
    [Feb 16 18:15:59.420]  - mgos (CPU 0/1), backtrace: 0x4008c824 0x40083120 0x400f0fe3 0x400e5a85 0x400e2683 0x400e24b6 0x40083168
    [Feb 16 18:15:59.420] 
    [Feb 16 18:15:59.420] [0;31mE (30731) task_wdt: Tasks currently running:[0m
    [Feb 16 18:15:59.420] [0;31mE (30731) task_wdt: CPU 0: IDLE0[0m
    [Feb 16 18:15:59.420] [0;31mE (30731) task_wdt: Aborting.[0m
    [Feb 16 18:15:59.420] abort() was called at PC 0x400d139c on core 0
    [Feb 16 18:15:59.420] 
    [Feb 16 18:15:59.420] Backtrace: 0x4008e563 0x4008e6e5 0x400d139c 0x4008168e 0x4017171f
    [Feb 16 18:15:59.420] 
    [Feb 16 18:15:59.420] --- BEGIN CORE DUMP ---
    [Feb 16 18:15:59.420] mos: catching core dump
    [Feb 16 18:16:02.266] ..--[command complete]
    

    After all, CORE DUMP is generated by Baro.begin () function. The reason seems that the watchdog timer worked.
    It seems that it is frozen in the read8 () function in the Wire library.

     [Feb 16 18:15:29.339] mgos_i2c_create I2C0 init ok (SDA: 18, SCL: 19, freq: 100000)

    This is in the log. Maybe it seems that i2c module was initialized

    [Feb 16 18:15:30.497] Hello, Arduino world!
    

    This code was executed after mgos_i2c_create().
    I guess, perhaps there is something wrong with the i2c module of Mongoose OS? I do not really understand.

    Can anyone transplant like BME 280?

    Thank you a lot, nliviu.
    I appreciate your advice!!!!!!!

  • The reason of the core dump is the while loop which never returns true which means no sensor was found.

  • ShogoShogo Osaka, Japan

    Hi, nliviu
    Thank you for your reply! I appreciate your advice!
    I will try your advice from now!

    Maybe...
    https://forum.mongoose-os.com/discussion/1402/esp32-and-i2c

  • To check the existence of a i2c device on the bus, I'd suggest:
    - comment out everything in mgos_app_init and keep only return MGOS_APP_INIT_SUCCESS;
    - comment out the loop function
    - add

    - origin: https://github.com/mongoose-os-libs/rpc-service-i2c
    

    in the libs section of your mos.yml, rebuild and reflash the firmware.
    Once the new firmaware is flashed, run mos call I2C.Scan.
    If one or more devices are found on the i2c bus, their addresses will be printed.

    Thanked by 1Shogo
  • ShogoShogo Osaka, Japan

    Hi. nliviu

    I could get i2c device address by mos call I2C.Scan command.
    -> 0x60

    Thank you.

    I knew My ESP32 could get device address.
    But read8() got 0x0 value. I want to get 0xC4 value by read8() method.

    I think, it is nessesary to change or debug i2c bit bang driver for Mongoose OS.

    https://github.com/mongoose-os-libs/i2c/blob/master/src/esp32/esp32_i2c_master.c
    -> mgos_i2c_write()

    Maybe.
    This code seems to work... but I couldn't understand this code..

        /* NAK last byte, as per spec. */
        dev->command[ci++].val =
            I2C_COMMAND_OP_READ | I2C_COMMAND_ACK_VALUE | 1;
      }
    

    See Also
    https://forum.mongoose-os.com/discussion/1402/esp32-and-i2c

  • nliviunliviu Romania

    I see you are using mos 2.11. Please upgrade to release 2.12.1 or add

    - ["i2c1.enable", false]
    

    in mos.yml and rebuild.

    Thanked by 1Shogo
  • ShogoShogo Osaka, Japan

    I upgraded mos to release 2.12.1.
    And I added - ["i2c1.enable", false] in mos.yml.
    So. I rebuild my source..
    But I couldn't get 0xC4 value from i2c device. I got 0x0 value.

    The read8 () function seems to get 0x0 somehow. The situation is improving.
    I do not understand any more. .

    I try to build it locally, I'll check out a bit more with printf debugging.

    Thank you for your advice!

Sign In or Register to comment.