/*
    RExOS - embedded RTOS
    Copyright (c) 2011-2018, Alexey Kramarenko
    All rights reserved.

    author: Alexey E. Kramarenko (alexeyk13@yandex.ru)
*/

#ifndef LPC_UART_H
#define LPC_UART_H

#include <stdint.h>
#include "../../userspace/sys.h"
#include "../../userspace/uart.h"
#include "../../userspace/io.h"
#include "../../userspace/rb.h"
#include "../../../lpc_config.h"
#include "lpc_exo.h"

#if !defined(UART_ENABLE_DMA)
#define UART_ENABLE_DMA            0
#endif
#if (UART_ENABLE_DMA)
#include "lpc_dma.h"
#endif


typedef struct {
    HANDLE tx_stream, tx_handle, rx_stream, rx_handle;
    uint16_t tx_size, tx_total;
    char tx_buf[UART_BUF_SIZE];
} UART_STREAM;

typedef struct {
    IO* tx_io;
    IO* rx_io;
    unsigned int rx_max, tx_processed;
    HANDLE tx_process, rx_process, rx_timer;
    unsigned int rx_char_timeout, rx_interleaved_timeout;
#if (UART_ENABLE_DMA)
    uint32_t dma_mode;
#endif
} UART_IO;

typedef struct {
    uint16_t error;
    bool io_mode;
    union {
        UART_STREAM s;
        UART_IO i;
    };
} UART;

typedef struct {
    UART* uarts[UARTS_COUNT];
#ifdef LPC11U6x
    uint8_t uart13, uart24;
#endif
} UART_DRV;

void lpc_uart_init(EXO* exo);
void lpc_uart_request(EXO* exo, IPC* ipc);

#endif // LPC_UART_H
