Skip to content

Commit 39d1507

Browse files
mattia-moffadanielinux
authored andcommitted
Address Copilot remarks
1 parent 087b251 commit 39d1507

4 files changed

Lines changed: 64 additions & 35 deletions

File tree

hal/nrf54l.c

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#ifdef TARGET_nrf54l
2323

2424
#include <stdint.h>
25+
#include <inttypes.h>
2526

2627
#include <string.h>
2728

@@ -67,8 +68,8 @@ static void uart_init_device(int device, uint32_t bitrate, uint8_t data, char pa
6768
((port << UART_PSEL_RXD_PORT_Pos) & UART_PSEL_RXD_PORT_Msk);
6869
UART_PSEL_CTS(device) = UART_PSEL_CTS_CONNECT_Disconnected;
6970
UART_PSEL_RTS(device) = UART_PSEL_RTS_CONNECT_Disconnected;
70-
UART_BAUDRATE(device) = UART_BAUDRATE_BAUDRATE_Baud115200;
71-
UART_CONFIG(device) = UART_CONFIG_8N1; /* 8N1, no HW flow control */
71+
UART_BAUDRATE(device) = UART_BAUDRATE_VALUE(bitrate);
72+
UART_CONFIG(device) = UART_CONFIG_VALUE(data, parity, stop);
7273

7374
UART_ENABLE(device) = UART_ENABLE_ENABLE_Enabled;
7475
}
@@ -99,6 +100,12 @@ void uart_write_raw(int device, const char* buffer, unsigned int sz)
99100
(UART_EVENTS_DMA_TX_BUSERROR(device) == 0))
100101
;
101102

103+
if (UART_EVENTS_DMA_TX_BUSERROR(device) != 0) {
104+
UART_TASKS_DMA_TX_STOP(device) =
105+
UART_TASKS_DMA_TX_STOP_STOP_Trigger;
106+
break;
107+
}
108+
102109
sz -= xfer;
103110
buffer += xfer;
104111
}
@@ -109,13 +116,18 @@ void uart_write_device(int device, const char* buf, unsigned int sz)
109116
static char buffer[UART_WRITE_BUF_SIZE];
110117
int bufsz = 0;
111118

112-
for(int i=0; i<(int)sz && bufsz < UART_WRITE_BUF_SIZE; i++)
113-
{
119+
for (int i = 0; i < (int)sz && bufsz < UART_WRITE_BUF_SIZE; i++) {
114120
char ch = (char) buf[i];
115-
if(ch == '\r')
121+
122+
if (ch == '\r')
116123
continue;
117-
if(ch == '\n')
124+
125+
if (ch == '\n') {
126+
if (bufsz >= (UART_WRITE_BUF_SIZE - 1))
127+
break;
128+
118129
buffer[bufsz++] = '\r';
130+
}
119131
buffer[bufsz++] = ch;
120132
}
121133
uart_write_raw(device, buffer, bufsz);
@@ -299,6 +311,9 @@ void uart_init(void)
299311
static uintptr_t ext_flash_addr_calc(uintptr_t address)
300312
{
301313
/* offset external flash addresses by the update partition address */
314+
if (address < WOLFBOOT_PARTITION_UPDATE_ADDRESS) {
315+
return 0;
316+
}
302317
address -= WOLFBOOT_PARTITION_UPDATE_ADDRESS;
303318
return address;
304319
}
@@ -307,7 +322,8 @@ int ext_flash_write(uintptr_t address, const uint8_t *data, int len)
307322
{
308323
#ifdef DEBUG_FLASH
309324
uintptr_t addr = ext_flash_addr_calc(address);
310-
wolfBoot_printf("Ext Write: Len %d, Addr 0x%x (off 0x%x) -> 0x%x\n",
325+
wolfBoot_printf("Ext Write: Len %d, Addr 0x%" PRIxPTR " (off 0x%" PRIxPTR
326+
") -> %p\n",
311327
len, address, addr, data);
312328
#endif
313329
return 0;
@@ -317,7 +333,8 @@ int ext_flash_read(uintptr_t address, uint8_t *data, int len)
317333
{
318334
#ifdef DEBUG_FLASH
319335
uintptr_t addr = ext_flash_addr_calc(address);
320-
wolfBoot_printf("Ext Read: Len %d, Addr 0x%x (off 0x%x) -> %p\n",
336+
wolfBoot_printf("Ext Read: Len %d, Addr 0x%" PRIxPTR " (off 0x%" PRIxPTR
337+
") -> %p\n",
321338
len, address, addr, data);
322339
#endif
323340
memset(data, FLASH_BYTE_ERASED, len);
@@ -328,7 +345,8 @@ int ext_flash_erase(uintptr_t address, int len)
328345
{
329346
#ifdef DEBUG_FLASH
330347
uintptr_t addr = ext_flash_addr_calc(address);
331-
wolfBoot_printf("Ext Erase: Len %d, Addr 0x%x (off 0x%x)\n",
348+
wolfBoot_printf("Ext Erase: Len %d, Addr 0x%" PRIxPTR " (off 0x%" PRIxPTR
349+
")\n",
332350
len, address, addr);
333351
#endif
334352
return 0;

hal/nrf54l.h

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -379,19 +379,38 @@ static inline int hal_uart_pin_num_rx(int device)
379379
#define UART_PSEL_CTS_CONNECT_Disconnected (0x1UL << 31)
380380
#define UART_PSEL_RTS_CONNECT_Disconnected (0x1UL << 31)
381381

382-
#define UART_CONFIG_FRAMESIZE_Pos 9UL
383-
#define UART_CONFIG_FRAMESIZE_8bit 0x8UL
384-
#define UART_CONFIG_8N1 (UART_CONFIG_FRAMESIZE_8bit << UART_CONFIG_FRAMESIZE_Pos)
382+
#define UART_CONFIG_VALUE(framesize, parity, stop) \
383+
((framesize << 9UL) | \
384+
((parity == 'E') ? 0x00E : \
385+
(parity == 'O') ? 0x10E : \
386+
0x000) | \
387+
((stop == 2) ? 0x10 : \
388+
0x00))
385389

386390
#define UART_TASKS_DMA_TX_START_START_Trigger 0x1UL
387391
#define UART_TASKS_DMA_TX_STOP_STOP_Trigger 0x1UL
388392

389393
#define UART_TASKS_DMA_RX_START_START_Trigger 0x1UL
390394
#define UART_TASKS_DMA_RX_STOP_STOP_Trigger 0x1UL
391395

392-
#define UART_BAUDRATE_BAUDRATE_Baud115200 0x01D60000UL
393-
394-
#define BAUD_115200 UART_BAUDRATE_BAUDRATE_Baud115200
396+
#define UART_BAUDRATE_VALUE(rate) (rate == 1200) ? 0x0004F000 : \
397+
(rate == 2400) ? 0x0009D000 : \
398+
(rate == 4800) ? 0x0013B000 : \
399+
(rate == 9600) ? 0x00275000 : \
400+
(rate == 14400) ? 0x003AF000 : \
401+
(rate == 19200) ? 0x004EA000 : \
402+
(rate == 28800) ? 0x0075C000 : \
403+
(rate == 31250) ? 0x00800000 : \
404+
(rate == 38400) ? 0x009D0000 : \
405+
(rate == 56000) ? 0x00E50000 : \
406+
(rate == 57600) ? 0x00EB0000 : \
407+
(rate == 76800) ? 0x013A9000 : \
408+
(rate == 115200) ? 0x01D60000 : \
409+
(rate == 230400) ? 0x03B00000 : \
410+
(rate == 250000) ? 0x04000000 : \
411+
(rate == 460800) ? 0x07400000 : \
412+
(rate == 921600) ? 0x0F000000 : \
413+
(rate == 1000000) ? 0x10000000 : 0
395414

396415
/* Nordic PMIC */
397416
#define PMIC_TWIM_PORT 1

hal/spi/spi_drv_nrf54l.c

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
#ifdef TARGET_nrf54l
2929

30-
#if defined(SPI_FLASH) || defined(WOLFBOOT_TPM)
30+
#if defined(SPI_FLASH)
3131

3232
#include "hal/nrf54l.h"
3333
#include "hal/spi/spi_drv_nrf54l.h"
@@ -85,10 +85,14 @@ void RAMFUNCTION spi_write(const char byte)
8585
SPI_DMA_TX_LIST = 0;
8686

8787
SPI_TASKS_START = SPIM_TASKS_START_TASKS_START_Trigger;
88-
while (SPI_EVENTS_END == 0)
88+
while (SPI_EVENTS_END == 0 &&
89+
SPI_EVENTS_DMA_RX_BUSERROR == 0 &&
90+
SPI_EVENTS_DMA_TX_BUSERROR == 0)
8991
;
9092
SPI_TASKS_STOP = SPIM_TASKS_STOP_TASKS_STOP_Trigger;
91-
while (SPI_EVENTS_STOPPED == 0)
93+
while (SPI_EVENTS_STOPPED == 0 &&
94+
SPI_EVENTS_DMA_RX_BUSERROR == 0 &&
95+
SPI_EVENTS_DMA_TX_BUSERROR == 0)
9296
;
9397
SPI_EVENTS_STOPPED = 0;
9498
spi_rx_ready = 1;
@@ -144,21 +148,5 @@ void spi_release(void)
144148

145149
}
146150

147-
#ifdef WOLFBOOT_TPM
148-
int spi_xfer(int cs, const uint8_t* tx, uint8_t* rx, uint32_t sz, int flags)
149-
{
150-
uint32_t i;
151-
spi_cs_on(SPI_CS_TPM_PIO_BASE, cs);
152-
for (i = 0; i < sz; i++) {
153-
spi_write((const char)tx[i]);
154-
rx[i] = spi_read();
155-
}
156-
if (!(flags & SPI_XFER_FLAG_CONTINUE)) {
157-
spi_cs_off(SPI_CS_TPM_PIO_BASE, cs);
158-
}
159-
return 0;
160-
}
161-
#endif /* WOLFBOOT_TPM */
162-
163-
#endif /* SPI_FLASH || WOLFBOOT_TPM */
151+
#endif /* SPI_FLASH */
164152
#endif /* TARGET_nrf54l */

hal/spi/spi_drv_nrf54l.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@
4848
#define SPI_MISO_PIN 27
4949
#endif
5050

51+
#ifndef SPI_CS_TPM
52+
#define SPI_CS_TPM SPI_CS_PIN
53+
#endif
54+
5155
#define SPI_CS_FLASH SPI_CS_PIN
5256
#define SPI_CS_PIO_BASE SPI_CS_PORT
5357
#define SPI_CS_TPM_PIO_BASE SPI_CS_PORT

0 commit comments

Comments
 (0)