|
28 | 28 | #include "hal.h" |
29 | 29 | #include "printf.h" |
30 | 30 |
|
| 31 | +/* Override RAMFUNCTION for test-app: when RAM_CODE is set but not __WOLFBOOT, |
| 32 | + * we still need flash functions to run from RAM for self-programming. */ |
| 33 | +#if defined(RAM_CODE) && !defined(__WOLFBOOT) |
| 34 | + #undef RAMFUNCTION |
| 35 | + #define RAMFUNCTION __attribute__((used,section(".ramcode"),long_call)) |
| 36 | +#endif |
| 37 | + |
31 | 38 | /* Assembly helpers */ |
32 | 39 | #define DMB() __asm__ volatile ("dmb") |
33 | 40 | #define DSB() __asm__ volatile ("dsb") |
@@ -280,43 +287,47 @@ void uart_init(void) |
280 | 287 | LPUART_CTRL = LPUART_CTRL_TE | LPUART_CTRL_RE; |
281 | 288 | } |
282 | 289 |
|
| 290 | +/* Transmit a single byte (raw, no conversion) - RAMFUNCTION for use during flash ops */ |
| 291 | +void RAMFUNCTION uart_tx(uint8_t byte) |
| 292 | +{ |
| 293 | + while (!(LPUART1_STAT & LPUART_STAT_TDRE)) {} |
| 294 | + LPUART1_DATA = byte; |
| 295 | + while (!(LPUART1_STAT & LPUART_STAT_TC)) {} |
| 296 | +} |
| 297 | + |
| 298 | +/* Used for sending ASCII and CRLF conversions */ |
283 | 299 | void uart_write(const char* buf, unsigned int sz) |
284 | 300 | { |
285 | 301 | unsigned int i; |
286 | | - |
287 | 302 | for (i = 0; i < sz; i++) { |
288 | 303 | /* Handle newline -> CRLF conversion */ |
289 | 304 | if (buf[i] == '\n') { |
290 | | - /* Wait for transmit buffer empty */ |
291 | | - while (!(LPUART_STAT & LPUART_STAT_TDRE)) {} |
292 | | - LPUART_DATA = '\r'; |
| 305 | + uart_tx('\r'); |
293 | 306 | } |
294 | | - |
295 | | - /* Wait for transmit buffer empty */ |
296 | | - while (!(LPUART_STAT & LPUART_STAT_TDRE)) {} |
297 | | - LPUART_DATA = buf[i]; |
| 307 | + uart_tx(buf[i]); |
298 | 308 | } |
299 | 309 |
|
300 | 310 | /* Wait for transmission complete */ |
301 | 311 | while (!(LPUART_STAT & LPUART_STAT_TC)) {} |
302 | 312 | } |
303 | 313 |
|
304 | | -/* Read a single character from UART (non-blocking) |
305 | | - * Returns: 1 if character read, 0 if no data available, -1 on error |
| 314 | + |
| 315 | + |
| 316 | +/* Read a single character from UART (non-blocking) - RAMFUNCTION for use during flash ops |
| 317 | + * Returns: 1 if character read, 0 if no data available |
306 | 318 | */ |
307 | | -int uart_read(char* c) |
| 319 | +int RAMFUNCTION uart_read(char* c) |
308 | 320 | { |
309 | | - uint32_t stat = LPUART_STAT; |
| 321 | + uint32_t stat = LPUART1_STAT; |
310 | 322 |
|
311 | | - /* Clear any error flags */ |
| 323 | + /* Clear any error flags first */ |
312 | 324 | if (stat & (LPUART_STAT_OR | LPUART_STAT_NF | LPUART_STAT_FE | LPUART_STAT_PF)) { |
313 | | - LPUART_STAT = stat; /* Write 1 to clear flags */ |
314 | | - return -1; |
| 325 | + LPUART1_STAT = stat; /* Write 1 to clear flags */ |
315 | 326 | } |
316 | 327 |
|
317 | | - /* Check if data available */ |
| 328 | + /* Check if data available - read even if there was an error */ |
318 | 329 | if (stat & LPUART_STAT_RDRF) { |
319 | | - *c = (char)(LPUART_DATA & 0xFF); |
| 330 | + *c = (char)(LPUART1_DATA & 0xFF); |
320 | 331 | return 1; |
321 | 332 | } |
322 | 333 |
|
|
0 commit comments