Starting ROM uart support, not tested yet.
authorNathael Pajani <nathael.pajani@ed3l.fr>
Wed, 18 Aug 2021 08:15:09 +0000 (10:15 +0200)
committerNathael Pajani <nathael.pajani@ed3l.fr>
Fri, 10 Feb 2023 18:02:59 +0000 (19:02 +0100)
core/rom_helpers.c
drivers/serial.c
include/core/rom_uart.h [new file with mode: 0644]

index 82d9358..d4d08a6 100644 (file)
@@ -29,6 +29,7 @@
 #include "core/system.h"
 #include "core/iap.h"
 #include "core/lpc_core.h"
+#include "core/rom_uart.h"
 
 /*******************************************************************************/
 /*            Integer division using ROM based division routines               */
@@ -82,6 +83,72 @@ void __aeabi_uidivmod(unsigned numerator, unsigned denominator)
        asm volatile("" : "+r"(r0), "+r"(r1) : "r"(r0), "r"(r1));
 }
 
+/*******************************************************************************/
+/*            UART ROM based API                                               */
+/*******************************************************************************/
+
+struct rom_uart_config {
+       uint32_t sys_clk_in_hz;  /* Main clock / UARTCLKDIV in Hz */
+       uint32_t baudrate;
+       uint8_t config; /* This is (((uart config register) >> 2) & 0x1F))  */
+       uint8_t sync_mod;
+#define ROM_UART_ASYNC 0x00
+       uint16_t error_en;
+#define ROM_UART_RX_OVR_EN (0x01 << 0)
+#define ROM_UART_TX_UDR_EN (0x01 << 1)
+#define ROM_UART_FRAME_ERR_EN (0x01 << 2)
+#define ROM_UART_PAR_ERR_EN (0x01 << 3)
+#define ROM_UART_RX_NOISE_EN (0x01 << 4)
+};
+
+struct rom_uart_param {
+       uint8_t* buffer; /* Used for both sending and receiving */
+       uint32_t size; /* Input buffer size or number of bytes to send */
+       uint16_t transfer_mode;
+#define ROM_UART_TRANSFER_NO_TERM    0x00
+#define ROM_UART_TRANSFER_STOP_CRLF  0x01  /* Stop Rx after <CR><LF> got received, Stop Tx after \0 and send (append) <CR><LF> */
+#define ROM_UART_TRANSFER_STOP_LF    0x02  /* Stop Rx after <LF> got received, Stop Tx after \0 and send (append) <LF> */
+#define ROM_UART_TRANSFER_STOP_EOSTR 0x03  /* No Rx end, Stop Tx after \0 got sent */
+       uint16_t driver_mode;
+#define ROM_UART_DRIVER_POLLING  0x00  /* Function is blocked until transfer is finished */
+#define ROM_UART_DRIVER_INTR     0x01  /* interrupt mode, function returns immediately, callback invoked when Tx finished */
+       void* (*callback)(void); /* Callback function */
+};
+
+struct lpc_rom_uart_helpers {
+       uint32_t (*uart_get_mem_size)(void);
+       void* (*uart_setup)(uint32_t base_addr, uint8_t *ram);
+       uint32_t (*uart_init)(void* handle, struct rom_uart_config* set);
+       /* polling functions */
+       uint8_t (*uart_get_char)(void* handle);
+       void (*uart_put_char)(void* handle, uint8_t data);
+       uint32_t (*uart_get_line)(void* handle, struct rom_uart_param* param);
+       uint32_t (*uart_put_line)(void* handle, struct rom_uart_param* param);
+       /* interrupt functions */
+       void (*uart_isr)(void* handle);
+};
+
+struct lpc_rom_uart_helpers* uart_helpers;
+
+uint32_t uart_get_mem_size(void)
+{
+       return uart_helpers->uart_get_mem_size();
+}
+
+int rom_uart_init(uint32_t regs, struct uart_rom_priv* rp, uint8_t config, uint32_t baudrate)
+{
+       struct rom_uart_config conf;
+       conf.sys_clk_in_hz = get_main_clock();
+       conf.baudrate = baudrate;
+       conf.config = config;
+       conf.sync_mod = ROM_UART_ASYNC;
+       conf.error_en = 0;
+
+       rp->rom_uart_handle = uart_helpers->uart_setup(regs, rp->ram);
+       uart_helpers->uart_init(rp->rom_uart_handle, &conf);
+       return 0;
+}
+
 
 /*******************************************************************************/
 /*            In Application Programming ROM based routines                    */
@@ -185,6 +252,7 @@ struct rom_helpers {
 void rom_helpers_init(void)
 {
        rom_div_helpers = (struct lpc_rom_div_helpers*)(ROM_DRIVERS->rom_div);
+       uart_helpers = (struct lpc_rom_uart_helpers*)(ROM_DRIVERS->rom_usart_driver);
        iap_entry = (iap_entry_func)LPC_82x_IAP_ROM_LOC;
 }
 
index fa54a0b..3e1b9f6 100644 (file)
@@ -31,6 +31,7 @@
 
 #include "core/system.h"
 #include "core/pio.h"
+#include "core/rom_uart.h"
 #include "lib/string.h"
 #include "lib/utils.h"
 #include "lib/errno.h"
@@ -46,6 +47,9 @@ struct uart_device
        uint8_t capabilities;
        uint8_t current_mode;
 
+       /* UART ROM Helpers private data */
+       struct uart_rom_priv rom_priv;
+
        /* Output buffer */
        volatile uint8_t out_buff[SERIAL_OUT_BUFF_SIZE];
        volatile uint32_t sending; /* Actual sending position in out buffer */
@@ -428,6 +432,14 @@ int uart_on(uint32_t uart_num, uint32_t baudrate, void (*rx_callback)(uint8_t))
        status = uart_clk_on(uart_num, baudrate);
        /* Setup */
        uart_setup(uart_num);
+
+       /* ROM UART API Setup */
+#if 0
+       status = rom_uart_init( (uint32_t)uarts[uart_num].regs,
+                                  &(uarts[uart_num].rom_priv),
+                                  ((uarts[uart_num].config >> 2) & 0x1F),
+                                  baudrate);
+#endif
        /* Enable interrupts back on */
        NVIC_EnableIRQ( uartdef->irq );
 
diff --git a/include/core/rom_uart.h b/include/core/rom_uart.h
new file mode 100644 (file)
index 0000000..a3352ed
--- /dev/null
@@ -0,0 +1,46 @@
+/****************************************************************************
+ *   core/rom_uart.h
+ *
+ * Copyright 2020 Nathael Pajani <nathael.pajani@ed3l.fr>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ *************************************************************************** */
+
+#ifndef CORE_ROM_UART_H
+#define CORE_ROM_UART_H
+
+#include "lib/stdint.h"
+
+
+#define UART_ROM_PRIV_SIZE  40
+
+struct uart_rom_priv {
+       uint8_t ram[UART_ROM_PRIV_SIZE];
+       void* rom_uart_handle;
+};
+
+/*******************************************************************************/
+/*            UART ROM based API                                               */
+/*******************************************************************************/
+
+/* Provide access to ROM based UART routines. */
+
+uint32_t uart_get_mem_size(void);
+
+int rom_uart_init(uint32_t regs, struct uart_rom_priv* rp, uint8_t config, uint32_t baudrate);
+
+
+#endif /* CORE_ROM_UART_H */
+