/*
 * spi.h
 *
 *  Created on: 29.07.2018
 *      Author: eyck
 */

#ifndef UART_H_
#define UART_H_

#include <sifive/devices/uart.h>
#include "util/bit_field.h"
#include <cstdint>

template<uint32_t BASE_ADDR>
class uart_regs {
public:
    BEGIN_BF_DECL(txdata_t, uint32_t);
        BF_FIELD(data, 0, 8);
        BF_FIELD(full, 31, 1);
    END_BF_DECL() ;

    BEGIN_BF_DECL(rxdata_t, uint32_t);
        BF_FIELD(data, 0, 8);
        BF_FIELD(empty, 31, 1);
    END_BF_DECL();

    BEGIN_BF_DECL(txctrl_t, uint32_t);
        BF_FIELD(txen, 0, 1);
        BF_FIELD(nstop, 1, 1);
        BF_FIELD(txcnt, 16, 3);
    END_BF_DECL();

    BEGIN_BF_DECL(rxctrl_t, uint32_t);
        BF_FIELD(rxen, 0, 1);
        BF_FIELD(rxcnt, 16, 3);
    END_BF_DECL();

    BEGIN_BF_DECL(ie_t, uint32_t);
        BF_FIELD(txwm, 0, 1);
        BF_FIELD(rxwm, 1, 1);
    END_BF_DECL();

    BEGIN_BF_DECL(ip_t, uint32_t);
        BF_FIELD(txwm, 0, 1);
        BF_FIELD(rxwm, 1, 1);
    END_BF_DECL();

    BEGIN_BF_DECL(div_t, uint32_t);
        BF_FIELD(div, 0, 16);
    END_BF_DECL();

    static inline txdata_t& txdata_reg(){
        return *reinterpret_cast<txdata_t*>(BASE_ADDR+UART_REG_TXFIFO);
    }

    static inline rxdata_t& rxdata_reg(){
        return *reinterpret_cast<rxdata_t*>(BASE_ADDR+UART_REG_RXFIFO);
    }

    static inline txctrl_t& txctrl_reg(){
        return *reinterpret_cast<txctrl_t*>(BASE_ADDR+UART_REG_TXCTRL);
    }

    static inline rxctrl_t& rxctrl_reg(){
        return *reinterpret_cast<rxctrl_t*>(BASE_ADDR+UART_REG_RXCTRL);
    }

    static inline ie_t& ie_reg(){
        return *reinterpret_cast<ie_t*>(BASE_ADDR+UART_REG_IE);
    }

    static inline ip_t& ip_reg(){
        return *reinterpret_cast<ip_t*>(BASE_ADDR+UART_REG_IP);
    }

    static inline div_t& div_reg(){
        return *reinterpret_cast<div_t*>(BASE_ADDR+UART_REG_DIV);
    }

};

#endif /* SPI_H_ */
