From 39d1507f060dfa16ec0ab66b64c3f882bef4040a Mon Sep 17 00:00:00 2001 From: Mattia Moffa Date: Fri, 13 Mar 2026 17:59:47 +0100 Subject: [PATCH] Address Copilot remarks --- hal/nrf54l.c | 36 +++++++++++++++++++++++++++--------- hal/nrf54l.h | 31 +++++++++++++++++++++++++------ hal/spi/spi_drv_nrf54l.c | 28 ++++++++-------------------- hal/spi/spi_drv_nrf54l.h | 4 ++++ 4 files changed, 64 insertions(+), 35 deletions(-) diff --git a/hal/nrf54l.c b/hal/nrf54l.c index 26124a5d..df3bd698 100644 --- a/hal/nrf54l.c +++ b/hal/nrf54l.c @@ -22,6 +22,7 @@ #ifdef TARGET_nrf54l #include +#include #include @@ -67,8 +68,8 @@ static void uart_init_device(int device, uint32_t bitrate, uint8_t data, char pa ((port << UART_PSEL_RXD_PORT_Pos) & UART_PSEL_RXD_PORT_Msk); UART_PSEL_CTS(device) = UART_PSEL_CTS_CONNECT_Disconnected; UART_PSEL_RTS(device) = UART_PSEL_RTS_CONNECT_Disconnected; - UART_BAUDRATE(device) = UART_BAUDRATE_BAUDRATE_Baud115200; - UART_CONFIG(device) = UART_CONFIG_8N1; /* 8N1, no HW flow control */ + UART_BAUDRATE(device) = UART_BAUDRATE_VALUE(bitrate); + UART_CONFIG(device) = UART_CONFIG_VALUE(data, parity, stop); UART_ENABLE(device) = UART_ENABLE_ENABLE_Enabled; } @@ -99,6 +100,12 @@ void uart_write_raw(int device, const char* buffer, unsigned int sz) (UART_EVENTS_DMA_TX_BUSERROR(device) == 0)) ; + if (UART_EVENTS_DMA_TX_BUSERROR(device) != 0) { + UART_TASKS_DMA_TX_STOP(device) = + UART_TASKS_DMA_TX_STOP_STOP_Trigger; + break; + } + sz -= xfer; buffer += xfer; } @@ -109,13 +116,18 @@ void uart_write_device(int device, const char* buf, unsigned int sz) static char buffer[UART_WRITE_BUF_SIZE]; int bufsz = 0; - for(int i=0; i<(int)sz && bufsz < UART_WRITE_BUF_SIZE; i++) - { + for (int i = 0; i < (int)sz && bufsz < UART_WRITE_BUF_SIZE; i++) { char ch = (char) buf[i]; - if(ch == '\r') + + if (ch == '\r') continue; - if(ch == '\n') + + if (ch == '\n') { + if (bufsz >= (UART_WRITE_BUF_SIZE - 1)) + break; + buffer[bufsz++] = '\r'; + } buffer[bufsz++] = ch; } uart_write_raw(device, buffer, bufsz); @@ -299,6 +311,9 @@ void uart_init(void) static uintptr_t ext_flash_addr_calc(uintptr_t address) { /* offset external flash addresses by the update partition address */ + if (address < WOLFBOOT_PARTITION_UPDATE_ADDRESS) { + return 0; + } address -= WOLFBOOT_PARTITION_UPDATE_ADDRESS; return address; } @@ -307,7 +322,8 @@ int ext_flash_write(uintptr_t address, const uint8_t *data, int len) { #ifdef DEBUG_FLASH uintptr_t addr = ext_flash_addr_calc(address); - wolfBoot_printf("Ext Write: Len %d, Addr 0x%x (off 0x%x) -> 0x%x\n", + wolfBoot_printf("Ext Write: Len %d, Addr 0x%" PRIxPTR " (off 0x%" PRIxPTR + ") -> %p\n", len, address, addr, data); #endif return 0; @@ -317,7 +333,8 @@ int ext_flash_read(uintptr_t address, uint8_t *data, int len) { #ifdef DEBUG_FLASH uintptr_t addr = ext_flash_addr_calc(address); - wolfBoot_printf("Ext Read: Len %d, Addr 0x%x (off 0x%x) -> %p\n", + wolfBoot_printf("Ext Read: Len %d, Addr 0x%" PRIxPTR " (off 0x%" PRIxPTR + ") -> %p\n", len, address, addr, data); #endif memset(data, FLASH_BYTE_ERASED, len); @@ -328,7 +345,8 @@ int ext_flash_erase(uintptr_t address, int len) { #ifdef DEBUG_FLASH uintptr_t addr = ext_flash_addr_calc(address); - wolfBoot_printf("Ext Erase: Len %d, Addr 0x%x (off 0x%x)\n", + wolfBoot_printf("Ext Erase: Len %d, Addr 0x%" PRIxPTR " (off 0x%" PRIxPTR + ")\n", len, address, addr); #endif return 0; diff --git a/hal/nrf54l.h b/hal/nrf54l.h index 51ce397e..82b3f20b 100644 --- a/hal/nrf54l.h +++ b/hal/nrf54l.h @@ -379,9 +379,13 @@ static inline int hal_uart_pin_num_rx(int device) #define UART_PSEL_CTS_CONNECT_Disconnected (0x1UL << 31) #define UART_PSEL_RTS_CONNECT_Disconnected (0x1UL << 31) -#define UART_CONFIG_FRAMESIZE_Pos 9UL -#define UART_CONFIG_FRAMESIZE_8bit 0x8UL -#define UART_CONFIG_8N1 (UART_CONFIG_FRAMESIZE_8bit << UART_CONFIG_FRAMESIZE_Pos) +#define UART_CONFIG_VALUE(framesize, parity, stop) \ + ((framesize << 9UL) | \ + ((parity == 'E') ? 0x00E : \ + (parity == 'O') ? 0x10E : \ + 0x000) | \ + ((stop == 2) ? 0x10 : \ + 0x00)) #define UART_TASKS_DMA_TX_START_START_Trigger 0x1UL #define UART_TASKS_DMA_TX_STOP_STOP_Trigger 0x1UL @@ -389,9 +393,24 @@ static inline int hal_uart_pin_num_rx(int device) #define UART_TASKS_DMA_RX_START_START_Trigger 0x1UL #define UART_TASKS_DMA_RX_STOP_STOP_Trigger 0x1UL -#define UART_BAUDRATE_BAUDRATE_Baud115200 0x01D60000UL - -#define BAUD_115200 UART_BAUDRATE_BAUDRATE_Baud115200 +#define UART_BAUDRATE_VALUE(rate) (rate == 1200) ? 0x0004F000 : \ + (rate == 2400) ? 0x0009D000 : \ + (rate == 4800) ? 0x0013B000 : \ + (rate == 9600) ? 0x00275000 : \ + (rate == 14400) ? 0x003AF000 : \ + (rate == 19200) ? 0x004EA000 : \ + (rate == 28800) ? 0x0075C000 : \ + (rate == 31250) ? 0x00800000 : \ + (rate == 38400) ? 0x009D0000 : \ + (rate == 56000) ? 0x00E50000 : \ + (rate == 57600) ? 0x00EB0000 : \ + (rate == 76800) ? 0x013A9000 : \ + (rate == 115200) ? 0x01D60000 : \ + (rate == 230400) ? 0x03B00000 : \ + (rate == 250000) ? 0x04000000 : \ + (rate == 460800) ? 0x07400000 : \ + (rate == 921600) ? 0x0F000000 : \ + (rate == 1000000) ? 0x10000000 : 0 /* Nordic PMIC */ #define PMIC_TWIM_PORT 1 diff --git a/hal/spi/spi_drv_nrf54l.c b/hal/spi/spi_drv_nrf54l.c index 3c234849..c734a113 100644 --- a/hal/spi/spi_drv_nrf54l.c +++ b/hal/spi/spi_drv_nrf54l.c @@ -27,7 +27,7 @@ #ifdef TARGET_nrf54l -#if defined(SPI_FLASH) || defined(WOLFBOOT_TPM) +#if defined(SPI_FLASH) #include "hal/nrf54l.h" #include "hal/spi/spi_drv_nrf54l.h" @@ -85,10 +85,14 @@ void RAMFUNCTION spi_write(const char byte) SPI_DMA_TX_LIST = 0; SPI_TASKS_START = SPIM_TASKS_START_TASKS_START_Trigger; - while (SPI_EVENTS_END == 0) + while (SPI_EVENTS_END == 0 && + SPI_EVENTS_DMA_RX_BUSERROR == 0 && + SPI_EVENTS_DMA_TX_BUSERROR == 0) ; SPI_TASKS_STOP = SPIM_TASKS_STOP_TASKS_STOP_Trigger; - while (SPI_EVENTS_STOPPED == 0) + while (SPI_EVENTS_STOPPED == 0 && + SPI_EVENTS_DMA_RX_BUSERROR == 0 && + SPI_EVENTS_DMA_TX_BUSERROR == 0) ; SPI_EVENTS_STOPPED = 0; spi_rx_ready = 1; @@ -144,21 +148,5 @@ void spi_release(void) } -#ifdef WOLFBOOT_TPM -int spi_xfer(int cs, const uint8_t* tx, uint8_t* rx, uint32_t sz, int flags) -{ - uint32_t i; - spi_cs_on(SPI_CS_TPM_PIO_BASE, cs); - for (i = 0; i < sz; i++) { - spi_write((const char)tx[i]); - rx[i] = spi_read(); - } - if (!(flags & SPI_XFER_FLAG_CONTINUE)) { - spi_cs_off(SPI_CS_TPM_PIO_BASE, cs); - } - return 0; -} -#endif /* WOLFBOOT_TPM */ - -#endif /* SPI_FLASH || WOLFBOOT_TPM */ +#endif /* SPI_FLASH */ #endif /* TARGET_nrf54l */ diff --git a/hal/spi/spi_drv_nrf54l.h b/hal/spi/spi_drv_nrf54l.h index a110e4a3..f9c8507e 100644 --- a/hal/spi/spi_drv_nrf54l.h +++ b/hal/spi/spi_drv_nrf54l.h @@ -48,6 +48,10 @@ #define SPI_MISO_PIN 27 #endif +#ifndef SPI_CS_TPM + #define SPI_CS_TPM SPI_CS_PIN +#endif + #define SPI_CS_FLASH SPI_CS_PIN #define SPI_CS_PIO_BASE SPI_CS_PORT #define SPI_CS_TPM_PIO_BASE SPI_CS_PORT