Skip to content

Commit

Permalink
Updates for Zephyr version 3.x (#700)
Browse files Browse the repository at this point in the history
* Updates for Zephyr version 3.x

Some changes needed to enable builds of ubxlib when using Zephyr
versions > 2.x.
This mainly involves changes for device handling.
The changes made here are compatible with previous Zephyr versions.
Note that the test runs in ubxlib still uses the old versions of
nrf connect and Zephyr.
  • Loading branch information
plerup authored Sep 20, 2022
1 parent ab020d3 commit 484eaf4
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 11 deletions.
8 changes: 5 additions & 3 deletions port/platform/zephyr/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Note: the directory structure here differs from that in the other platform direc
# SDK Installation
`ubxlib` is tested with the version of Zephyr that comes with `nRFConnect SDK version 1.6.1` which is the recommended version.

Ubxlib has been tested to build with all newer versions nRFConnect SDK, up til 2.1.0. The test suite for ubxlib is however still only using 1.6.1. This is due to the fact that an update to 2.x requires modification of the board overlay files, which would imply a breaking change. In coming versions of ubxlib, newer version of nRFConnect will be used.

Follow the instructions to install the development tools:

- Install nRF connect. https://www.nordicsemi.com/Software-and-Tools/Development-Tools/nRF-Connect-for-desktop
Expand Down Expand Up @@ -89,13 +91,13 @@ Since pin assignment for UARTs are made in the device tree, functions such as `u
- Unless compiled for use on Linux/Posix, Zephyr usee its own internal minimal C library, not [newlib](https://sourceware.org/newlib/libc.html); if you wish to use [newlib](https://sourceware.org/newlib/libc.html) then you should add `U_CFG_ZEPHYR_USE_NEWLIB` to the conditional compilation flags passed into the build (see below for how to do this without modifying `CMakeLists.txt`).
- Always clean the build directory when upgrading to a new ubxlib version.
- You may override or provide conditional compilation flags to CMake without modifying `CMakeLists.txt`. Do this by setting an environment variable `U_FLAGS`, e.g.:

```
set U_FLAGS=-DMY_FLAG
```

...or:

```
set U_FLAGS=-DMY_FLAG -DU_CFG_APP_PIN_CELL_ENABLE_POWER=-1
```
28 changes: 23 additions & 5 deletions port/platform/zephyr/src/u_port_gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@
* COMPILE-TIME MACROS
* -------------------------------------------------------------- */

/*
**** NOTE ****
The statements below is due to a change made in Zephyr 3.x.
For some reason the gpio drive mode macros have changed from
being generic to soc specific. The old macros are still there
but marked as deprecated and hence giving build warnings.
There is currently no directly correlated macros for Nrf and
we also want to use the code in this file for other
Zephyr targets. However in coming versions of ubxlib we have
to deal with some solution for this.
*/
#undef __DEPRECATED_MACRO
#define __DEPRECATED_MACRO

/* ----------------------------------------------------------------
* TYPES
* -------------------------------------------------------------- */
Expand All @@ -52,22 +66,26 @@

static const struct device *getGpioDevice(uint32_t pin)
{
const struct device *pDev;
const struct device *pDev = NULL;
// common practice in device trees that one gpio port holds 32 pins
#if KERNEL_VERSION_MAJOR < 3
char dtDeviceNodeLabel[7];
strncpy(dtDeviceNodeLabel, "GPIO_x", sizeof(dtDeviceNodeLabel));
// replace x with corresponding port number
dtDeviceNodeLabel[5] = '0' + (pin / GPIO_MAX_PINS_PER_PORT);
dtDeviceNodeLabel[6] = 0;
pDev = device_get_binding(dtDeviceNodeLabel);
if (!pDev) {
return NULL; // no such node label
#else
int32_t port = pin / GPIO_MAX_PINS_PER_PORT;
if (port == 0) {
pDev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(gpio0));
} else if (port == 1) {
pDev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(gpio1));
}

#endif
return pDev;
}


/* ----------------------------------------------------------------
* PUBLIC FUNCTIONS
* -------------------------------------------------------------- */
Expand Down
18 changes: 16 additions & 2 deletions port/platform/zephyr/src/u_port_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
# define U_PORT_I2C_MAX_NUM 2
#endif

#ifndef I2C_MODE_CONTROLLER
// MASTER deprecated in Zephyr 3.x
# define I2C_MODE_CONTROLLER I2C_MODE_MASTER
#endif

/* ----------------------------------------------------------------
* TYPES
* -------------------------------------------------------------- */
Expand Down Expand Up @@ -128,15 +133,24 @@ static int32_t openI2c(int32_t i2c, int32_t pinSda, int32_t pinSdc,
handleOrErrorCode = (int32_t) U_ERROR_COMMON_PLATFORM;
switch (i2c) {
case 0:
#if KERNEL_VERSION_MAJOR < 3
pDevice = device_get_binding("I2C_0");
#else
pDevice = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(i2c0));
#endif
break;
case 1:
#if KERNEL_VERSION_MAJOR < 3
pDevice = device_get_binding("I2C_1");
#else
pDevice = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(i2c1));
#endif
break;
default:
break;
}
i2cDeviceCfg = I2C_SPEED_SET(clockHertzToIndex(U_PORT_I2C_CLOCK_FREQUENCY_HERTZ)) | I2C_MODE_MASTER;
i2cDeviceCfg = I2C_SPEED_SET(clockHertzToIndex(U_PORT_I2C_CLOCK_FREQUENCY_HERTZ)) |
I2C_MODE_CONTROLLER;
if ((pDevice != NULL) &&
(!adopt || (i2c_configure(pDevice, i2cDeviceCfg) == 0))) {
gI2cData[i2c].clockHertz = U_PORT_I2C_CLOCK_FREQUENCY_HERTZ;
Expand Down Expand Up @@ -272,7 +286,7 @@ int32_t uPortI2cSetClock(int32_t handle, int32_t clockHertz)
errorCode = (int32_t) U_ERROR_COMMON_NOT_SUPPORTED;
if (!gI2cData[handle].adopted) {
pDevice = gI2cData[handle].pDevice;
i2cDeviceCfg = I2C_SPEED_SET(clockIndex) | I2C_MODE_MASTER;
i2cDeviceCfg = I2C_SPEED_SET(clockIndex) | I2C_MODE_CONTROLLER;
errorCode = (int32_t) U_ERROR_COMMON_PLATFORM;
if (i2c_configure(pDevice, i2cDeviceCfg) == 0) {
gI2cData[handle].clockHertz = clockHertz;
Expand Down
19 changes: 18 additions & 1 deletion port/platform/zephyr/src/u_port_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ int32_t uPortUartInit()
for (size_t x = 0; x < sizeof(gUartData) / sizeof(gUartData[0]); x++) {
const struct device *dev = NULL;
switch (x) {
#if KERNEL_VERSION_MAJOR < 3
case 0:
dev = device_get_binding("UART_0");
break;
Expand All @@ -331,14 +332,30 @@ int32_t uPortUartInit()
break;
default:
break;
#else
case 0:
dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(uart0));
break;
case 1:
dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(uart1));
break;
case 2:
dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(uart2));
break;
case 3:
dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(uart3));
break;
default:
break;
#endif
}

gUartData[x].pDevice = dev;
gUartData[x].pBuffer = NULL;
}
}

return (int32_t) errorCode;
return (int32_t)errorCode;
}

void uPortUartDeinit()
Expand Down

0 comments on commit 484eaf4

Please sign in to comment.