I am trying to set up UART communication between the nRF9160 and nRF52840 that are found in the Fanstel BLG840XE. Currently I am sending a test string of "abcd" from the nRF52840 to the nRF9160, however the nRF9160 is not receiving what is being sent. Here is the RTT terminal output:
| nRF9160 | nRF52840 |
[nRF9160] No data available (ret=-1) [nRF9160] No data available (ret=-1) [nRF9160] No data available (ret=-1) [nRF9160] No data available (ret=-1) [nRF9160] No data available (ret=-1) [nRF9160] No data available (ret=-1) [nRF9160] No data available (ret=-1) [nRF9160] No data available (ret=-1) |
[nRF52840] Starting UART test - sending 'abcd' [nRF52840] Sent char: 'a' [nRF52840] Sent char: 'b' [nRF52840] Sent char: 'c' [nRF52840] Sent char: 'd' [nRF52840] Sent: abcd + CR [nRF52840] No data received (ret=-1) [nRF52840] Starting UART test - sending 'abcd' [nRF52840] Sent char: 'a' [nRF52840] Sent char: 'b' [nRF52840] Sent char: 'c' [nRF52840] Sent char: 'd' [nRF52840] Sent: abcd + CR [nRF52840] No data received (ret=-1) |
This is the schematic that I have been following from Fanstel's open source.
nRF9160 Code
main.c
#include <string.h>
#include <zephyr/kernel.h>
#include <zephyr/drivers/uart.h>
#define RX_BUF_SIZE 1024
static uint8_t rx_buf[RX_BUF_SIZE];
static volatile size_t rx_pos = 0;
void uart_echo_test(void)
{
const struct device *uart = DEVICE_DT_GET(DT_NODELABEL(uart0));
printk("[nRF9160] UART device: %s\n", uart->name);
char c;
while (1) {
int ret = uart_poll_in(uart, &c);
if (ret == 0) {
printk("[nRF9160] Received char: '%c' (0x%02X)\n", c, (unsigned char)c);
uart_poll_out(uart, c); // Echo back
printk("[nRF9160] Echoed char: '%c'\n", c);
} else {
printk("[nRF9160] No data available (ret=%d)\n", ret);
}
k_msleep(1);
}
}
int main(void)
{
printk("nRF9160: Starting RTT test\n");
// Wait for nRF52840 to boot up
printk("nRF9160: Waiting 3 seconds for nRF52840 to boot...\n");
k_sleep(K_SECONDS(3));
uart_echo_test();
return 0;
}
overlay
&uart0 {
current-speed = <115200>;
status = "okay";
pinctrl-0 = <&uart0_default>;
pinctrl-1 = <&uart0_sleep>;
pinctrl-names = "default", "sleep";
hw-flow-control;
};
&pinctrl {
uart0_default: uart0_default {
group1 {
psels = <NRF_PSEL(UART_TX, 0, 29)>, // P0.29 - APP1_TXD
<NRF_PSEL(UART_RX, 0, 28)>, // P0.28 - APP1_RXD
<NRF_PSEL(UART_RTS, 0, 27)>, // P0.27 - APP1_RTS
<NRF_PSEL(UART_CTS, 0, 26)>; // P0.26 - APP1_CTS
};
};
uart0_sleep: uart0_sleep {
group1 {
psels = <NRF_PSEL(UART_TX, 0, 29)>, // P0.29 - APP1_TXD
<NRF_PSEL(UART_RX, 0, 28)>, // P0.28 - APP1_RXD
<NRF_PSEL(UART_RTS, 0, 27)>, // P0.27 - APP1_RTS
<NRF_PSEL(UART_CTS, 0, 26)>; // P0.26 - APP1_CTS
low-power-enable;
};
};
};
nRF52840 Code
main.c
#include "observer.h"
#include <zephyr/sys/printk.h>
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/kernel.h>
int main(void)
{
printk("Starting UART test...\n");
k_sleep(K_SECONDS(1)); // Small delay to let things settle
while (1) {
uart_test_abcd();
k_sleep(K_SECONDS(2)); // Send every 2 seconds
}
return 0;
}
observer.c
#include "observer.h"
#include <zephyr/sys/printk.h>
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/hci.h>
#include <zephyr/kernel.h>
#include <zephyr/drivers/uart.h>
#define UART_BUF_SIZE 128
char uart_rx_buf[UART_BUF_SIZE];
int uart_rx_len = 0;
void uart_test_abcd(void)
{
const struct device *uart = DEVICE_DT_GET(DT_NODELABEL(uart0));
printk("[nRF52840] Starting UART test - sending 'abcd'\n");
char test_msg[] = "abcd";
for (int i = 0; test_msg[i] != '\0'; i++) {
uart_poll_out(uart, test_msg[i]);
printk("[nRF52840] Sent char: '%c'\n", test_msg[i]);
}
uart_poll_out(uart, '\r'); // Add carriage return
printk("[nRF52840] Sent: abcd + CR\n");
k_msleep(100); // Wait for response
char received_char;
int ret = uart_poll_in(uart, &received_char);
if (ret == 0) {
printk("[nRF52840] Received char: '%c' (0x%02X)\n", received_char, (unsigned char)received_char);
} else {
printk("[nRF52840] No data received (ret=%d)\n", ret);
}
}
observer.h
#ifndef OBSERVER_H #define OBSERVER_H #include <zephyr/bluetooth/bluetooth.h> void uart_test_abcd(void); #endif
overlay
&uart0 {
current-speed = <115200>;
status = "okay";
pinctrl-0 = <&uart0_default>;
pinctrl-1 = <&uart0_sleep>;
pinctrl-names = "default", "sleep";
hw-flow-control;
};
&pinctrl {
uart0_default: uart0_default {
group1 {
psels = <NRF_PSEL(UART_TX, 0, 3)>, // P0.03 (from schematic)
<NRF_PSEL(UART_RX, 0, 5)>, // P0.05 (from schematic)
<NRF_PSEL(UART_RTS, 1, 8)>, // P1.08 (from schematic)
<NRF_PSEL(UART_CTS, 0, 7)>; // P0.07 (from schematic)
};
};
uart0_sleep: uart0_sleep {
group1 {
psels = <NRF_PSEL(UART_TX, 0, 3)>, // P0.03
<NRF_PSEL(UART_RX, 0, 5)>, // P0.05
<NRF_PSEL(UART_RTS, 1, 8)>, // P1.08
<NRF_PSEL(UART_CTS, 0, 7)>; // P0.07
low-power-enable;
};
};
};