Skip to content

Commit

Permalink
Zephyr fix for SPI only: accommodate Zephyr 3.4 SPI structure change. (
Browse files Browse the repository at this point in the history
…#150)

The ever-evolving Zephyr SPI implementation has changed structure again in Zephyr 3.4; the new structure is now accommodated.  Note that previous Zephyr versions still work and that ubxlib still only tests the version of Zephyr that comes with nRFConnect 2.3.0 (Zephyr version 3.2.99).

Our thanks to both PetervdPerk-NXP and chrsag for pointing out the problem and providing/testing fixes.

Signed-off-by: Christian Spinnler <christian.spinnler@siemens.com>
  • Loading branch information
chrsag authored Oct 19, 2023
1 parent b3aa942 commit 4e50685
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions port/platform/zephyr/src/u_port_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2023 Siemens AG for zephyr v3.4.0 spi api change fix
*/

/** @file
Expand Down Expand Up @@ -318,7 +320,16 @@ static int32_t setSpiConfig(int32_t spi, uPortSpiCfg_t *pSpiCfg,
pSpiCfg->spiConfig.operation = operation;
pSpiCfg->spiConfig.frequency = pDevice->frequencyHertz;

#if KERNEL_VERSION_MAJOR < 3
pSpiCfg->spiConfig.cs = NULL;
pSpiCfg->spiCsControl.gpio_dev = NULL;
#else
# if KERNEL_VERSION_MAJOR == 3 && KERNEL_VERSION_MINOR < 4
pSpiCfg->spiConfig.cs = NULL;
# endif
pSpiCfg->spiCsControl.gpio.port = NULL;
#endif

if ((pDevice->pinSelect >= 0) || (pDevice->indexSelect >= 0)) {
#if KERNEL_VERSION_MAJOR < 3
pSpiCfg->spiCsControl.gpio_dev = pUPortPrivateGetGpioDevice(pinSelect);
Expand All @@ -338,7 +349,11 @@ static int32_t setSpiConfig(int32_t spi, uPortSpiCfg_t *pSpiCfg,
&pSpiCfg->spiCsControl);
if (errorCode == (int32_t) U_ERROR_COMMON_SUCCESS) {
// pinSelect/index matched a CS pin for this SPI controller
#if ((KERNEL_VERSION_MAJOR < 3) || (KERNEL_VERSION_MAJOR == 3 && KERNEL_VERSION_MINOR < 4))
pSpiCfg->spiConfig.cs = &pSpiCfg->spiCsControl;
#else
pSpiCfg->spiConfig.cs = pSpiCfg->spiCsControl;
#endif
} else if ((errorCode == (int32_t) U_ERROR_COMMON_NOT_FOUND) &&
(pDevice->pinSelect >= 0)) {
errorCode = (int32_t) U_ERROR_COMMON_PLATFORM;
Expand All @@ -354,7 +369,11 @@ static int32_t setSpiConfig(int32_t spi, uPortSpiCfg_t *pSpiCfg,
pSpiCfg->spiCsControl.gpio.port = pGpioPort;
pSpiCfg->spiCsControl.gpio.pin = pinSelect;
pSpiCfg->spiCsControl.gpio.dt_flags = gpioFlags;
#if ((KERNEL_VERSION_MAJOR < 3) || (KERNEL_VERSION_MAJOR == 3 && KERNEL_VERSION_MINOR < 4))
pSpiCfg->spiConfig.cs = &pSpiCfg->spiCsControl;
#else
pSpiCfg->spiConfig.cs = pSpiCfg->spiCsControl;
#endif
errorCode = (int32_t) U_ERROR_COMMON_SUCCESS;
}
}
Expand All @@ -368,6 +387,12 @@ static int32_t setSpiConfig(int32_t spi, uPortSpiCfg_t *pSpiCfg,
offsetDuration = pDevice->stopOffsetNanoseconds;
}
pSpiCfg->spiCsControl.delay = offsetDuration / 1000;
#if ((KERNEL_VERSION_MAJOR < 3) || (KERNEL_VERSION_MAJOR == 3 && KERNEL_VERSION_MINOR < 4))
pSpiCfg->spiConfig.cs = &pSpiCfg->spiCsControl;
#else
pSpiCfg->spiConfig.cs = pSpiCfg->spiCsControl;
#endif

}
}

Expand Down Expand Up @@ -548,16 +573,20 @@ int32_t uPortSpiControllerGetDevice(int32_t handle,
pDevice->pinSelect = -1;
pDevice->startOffsetNanoseconds = 0;
#if KERNEL_VERSION_MAJOR >= 3
#if ((KERNEL_VERSION_MAJOR == 3 && KERNEL_VERSION_MINOR < 4))
if (pSpiCfg->spiConfig.cs != NULL) {
// Have a chip select pin, work out what it is
#endif // ((KERNEL_VERSION_MAJOR == 3 && KERNEL_VERSION_MINOR < 4))
// Have a chip select pin, work out what it is
pDevice->pinSelect = uPortPrivateGetGpioPort(pSpiCfg->spiCsControl.gpio.port,
pSpiCfg->spiCsControl.gpio.pin);
if (!(pSpiCfg->spiCsControl.gpio.dt_flags & GPIO_ACTIVE_LOW)) {
pDevice->pinSelect |= U_COMMON_SPI_PIN_SELECT_INVERTED;
}
pDevice->startOffsetNanoseconds = pSpiCfg->spiCsControl.delay * 1000;
#if ((KERNEL_VERSION_MAJOR == 3 && KERNEL_VERSION_MINOR < 4))
}
#endif
#endif // ((KERNEL_VERSION_MAJOR == 3 && KERNEL_VERSION_MINOR < 4))
#endif // KERNEL_VERSION_MAJOR >= 3
pDevice->stopOffsetNanoseconds = pDevice->startOffsetNanoseconds;
pDevice->sampleDelayNanoseconds = 0; // Not an option in Zephyr
pDevice->frequencyHertz = pSpiCfg->spiConfig.frequency;
Expand Down

0 comments on commit 4e50685

Please sign in to comment.