Hello nordic team,
I am trying to build up a mesh network using openthread and reading some sensor data via i2c and broadcasting them in the network. I have code that broadcasts data with udp over openthread and i have code that reads sensor data over i2c. Both function separadly as they should but as soon as i try merging them nothing works at all. The code compiles, i even can flash it but after flashing the mcu is completely dead/frozen, no prints are shown and even the reset button doesn't react. Because of that i have no errortrace to share. As soon as I comment CONFIG_I2C=y out of the prj.conf file and together every line related to i2c in main.c openThread works again and vice versa for i2c too. I really don't understand this.
If someone could point something for me out I would really appreciate this.
GG
I am using the two nrf5340DK and nrf Connect for VS Code on Windows 10. SDK Version 2.1.1
Client prj.conf (same for Server)
# nRF board library
CONFIG_DK_LIBRARY=y
# # Enable Debug
# CONFIG_DEBUG_OPTIMIZATIONS=y
# CONFIG_DEBUG_THREAD_INFO=y
CONFIG_BUILD_WITH_TFM=y
CONFIG_OPENTHREAD_CRYPTO_PSA=y
# Configure sample logging setting
CONFIG_LOG=y
CONFIG_OPENTHREAD_LOG_LEVEL_NOTE=y
CONFIG_OPENTHREAD_DEBUG=y
# Generic networking options
CONFIG_NETWORKING=y
# L2 OpenThread enabling
CONFIG_NET_L2_OPENTHREAD=y
# Network shell
CONFIG_SHELL=y
CONFIG_OPENTHREAD_SHELL=y
CONFIG_SHELL_ARGC_MAX=26
CONFIG_SHELL_CMD_BUFF_SIZE=416
# Network sockets
CONFIG_NET_SOCKETS=y
CONFIG_NET_SOCKETS_POSIX_NAMES=y
CONFIG_NET_SOCKETS_POLL_MAX=4
# Same network Master Key for client and server
CONFIG_OPENTHREAD_MANUAL_START=n
CONFIG_OPENTHREAD_NETWORK_NAME="OT-Mesh"
CONFIG_OPENTHREAD_PANID=10000
CONFIG_OPENTHREAD_XPANID="fd:02:00:00:ab:cd:00:00"
CONFIG_OPENTHREAD_NETWORKKEY="00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff"
# Additional Options
CONFIG_MBEDTLS_SHA1_C=n
CONFIG_FPU=y
CONFIG_I2C=y # <-- this kills everything
Client dts Overlay: (no overlay for the server)
&i2c1 {
compatible = "nordic,nrf-twim";
status = "okay";
msgeq7: msgeq7@1{
compatible = "i2c-device";
reg = < 0x1 >;
label = "MSGEQ7";
};
};
Client main.c
#include <zephyr/zephyr.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/sys/printk.h>
#include <net/openthread.h>
#include <openthread/thread.h>
#include <openthread/udp.h>
#include <drivers/i2c.h>
#include <stdio.h>
/* 1000 msec = 1 sec */
#define SLEEP_TIME_MS 50
#define I2C0_NODE DT_NODELABEL(msgeq7)
static void udp_send(uint8_t payload);
void main(void) {
printk("Hello World! %s\n", CONFIG_BOARD);
static const struct i2c_dt_spec dev_i2c = I2C_DT_SPEC_GET(I2C0_NODE);
if (!device_is_ready(dev_i2c.bus)) {
printk("I2C bus %s at Adress %x is not ready! \n\r", dev_i2c.bus->name, dev_i2c.addr);
return;
}
while (1) {
uint8_t data;
int ret = i2c_read_dt(&dev_i2c, &data, sizeof(data));
if (ret != 0) {
printk("Failed to read from I2C device address %x \n", dev_i2c.addr);
}
printk("I2C - Adresse: %x - Data: %d \n", dev_i2c.addr, data);
udp_send(data);
k_msleep(SLEEP_TIME_MS);
}
}
static void udp_send(uint8_t payload)
{
otError error = OT_ERROR_NONE;
char buf[50];
sprintf(buf, "hallo thread: %d", payload);
otInstance *myInstance;
myInstance = openthread_get_default_instance();
otUdpSocket mySocket;
otMessageInfo messageInfo;
memset(&messageInfo, 0, sizeof(messageInfo));
otIp6AddressFromString("ff03::1", &messageInfo.mPeerAddr); // ff03::1 ist eine Broadcastdresse
messageInfo.mPeerPort = 2222;
do{
error = otUdpOpen(myInstance, &mySocket, NULL, NULL);
if (error != OT_ERROR_NONE) break;
otMessage *test_Message = otUdpNewMessage(myInstance, NULL);
error = otMessageAppend(test_Message, buf, (uint16_t)strlen(buf));
if (error != OT_ERROR_NONE) break;
error = otUdpSend(myInstance, &mySocket, test_Message, &messageInfo);
if (error != OT_ERROR_NONE) {printk("udpSend failed \n"); break; }
error = otUdpClose(myInstance, &mySocket);
}while(false);
if (error == OT_ERROR_NONE)
printk("%d Sent.\n", payload);
else
printk("udpSend error: %d\n", error);
}
Server main.c (this code works perfectly)
#include <zephyr/zephyr.h>
#include <zephyr/drivers/gpio.h>
#include <net/openthread.h>
#include <openthread/thread.h>
#include <openthread/udp.h>
void udpReceive_cb(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo);
static void udp_init(void);
void main(void)
{
udp_init();
while (1) {
k_msleep(1000);
}
}
void udpReceive_cb(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo) {
uint16_t payloadLength = otMessageGetLength(aMessage) - otMessageGetOffset(aMessage);
char buf[payloadLength+1];
otMessageRead(aMessage, otMessageGetOffset(aMessage), buf, payloadLength);
buf[payloadLength]='\0';
printk("Received: %s\n", buf);
}
static void udp_init(void)
{
otError error = OT_ERROR_NONE;
otInstance *myInstance = openthread_get_default_instance();
otUdpSocket mySocket;
otSockAddr mySockAddr;
memset(&mySockAddr,0,sizeof(mySockAddr));
mySockAddr.mPort = 2222; // we chose this port.
do{
error = otUdpOpen(myInstance, &mySocket, udpReceive_cb, NULL); //udp mesajı gelince tanımlanan callback funca giriyor.
if (error != OT_ERROR_NONE) break;
error = otUdpBind(myInstance, &mySocket, &mySockAddr, OT_NETIF_THREAD);
}while(false);
if (error != OT_ERROR_NONE){
printk("init_udp error: %d\n", error);
}
}