#include "core/lpc_regs_12xx.h"
#include "core/lpc_core_cm0.h"
#include "core/system.h"
+#include "lib/string.h"
+#define SERIAL_OUT_BUFF_SIZE 64
struct uart_device
{
uint32_t num;
uint32_t baudrate;
/* Output buffer */
- volatile const char* out_buff;
+ volatile char out_buff[SERIAL_OUT_BUFF_SIZE];
volatile uint32_t sending; /* Actual sending position in out buffer */
volatile uint32_t out_lock;
volatile uint32_t out_length; /* actual position to add in out buffer */
.num = 0,
.regs = (struct lpc_uart*)LPC_UART_0,
.baudrate = 0,
- .out_buff = NULL,
+ .out_buff = {0},
.sending = 0,
.out_lock = 0,
},
.num = 1,
.regs = (struct lpc_uart*)LPC_UART_1,
.baudrate = 0,
- .out_buff = NULL,
+ .out_buff = {0},
.sending = 0,
.out_lock = 0,
},
/***************************************************************************** */
-/* Serial Write */
-
+/* Serial Write
+ *
+ * Try to send at most "length" characters from "buf" on the requested uart.
+ * Returns -1 on error, or number of characters copied into output buffer, witch
+ * may be less than requested "length"
+ * Possible errors: requested uart does not exists or unable to acquire uart lock.
+ *
+ * Warning for Real Time : This implementation will block if there's already a
+ * transmission ongoing.
+ */
int serial_write(uint32_t uart_num, const char *buf, uint32_t length)
{
struct uart_device* uart = NULL;
/* If UART is sending wait for buffer empty */
do {} while (uart->sending != 0);
- uart->out_buff = buf;
+ if (length > SERIAL_OUT_BUFF_SIZE) {
+ length = SERIAL_OUT_BUFF_SIZE;
+ }
+ memcpy((char*)uart->out_buff, buf, length);
uart->out_length = length;
/* Turn output on */
/***************************************************************************** */
-/* Serial Write */
-
+/* Serial Write
+ *
+ * Try to send at most "length" characters from "buf" on the requested uart.
+ * Returns -1 on error, or number of characters copied into output buffer, witch
+ * may be less than requested "length"
+ * Possible errors: requested uart does not exists or unable to acquire uart lock.
+ *
+ * Warning for Real Time : This implementation will block if there's already a
+ * transmission ongoing.
+ */
int serial_write(uint32_t uart_num, const char *buf, uint32_t length);
/***************************************************************************** */
/* Public access to UART setup */
-
void set_uarts_pins(void);
-
/* Allow any change to the main clock to be forwarded to us */
void uart_clk_update(void);
-
/* Do we need to allow setting of other parameters ? (Other than 8n1) */
/* Do we need to allow specifying an interrupt handler ? */
void uart_on(uint32_t uart_num, uint32_t baudrate);