/*
 * frm_uart.c
 *
 *  Created on: 21 авг. 2023 г.
 *      Author: seklyuts
 */
#include "f28x_project.h"
//#include "f2838x_pinmux.h"
#include "frm_uart.h"

uint16_t frmEn = 0;


uint16_t FMSTR_is_enable(void)
{
    return frmEn;
}

void FMSTR_enable_clr(void)
{
    frmEn = 0;
}

void FMSTR_enable_set(void)
{
    frmEn = 1;
}

void FRMUartInit(void)
{
    FMSTR_Init();
    CpuSysRegs.PCLKCR7.bit.SCI_A = 1;

    // For this example, only init the pins for the SCI-A port.
    //  GPIO_SetupPinMux() - Sets the GPxMUX1/2 and GPyMUX1/2 register bits
    //  GPIO_SetupPinOptions() - Sets the direction and configuration of the GPIOS
    // These functions are found in the f2838x_gpio.c file.
//       EALLOW;
//       GpioCtrlRegs.GPCPUD.bit.GPIO85 = 1;   // Disable pull-up
//       GpioCtrlRegs.GPCPUD.bit.GPIO84 = 0;

//       GpioCtrlRegs.GPCMUX2.bit.GPIO85 = 11;   //
//       GpioCtrlRegs.GPCMUX2.bit.GPIO84 = 11;   //
//       EDIS;
       GPIO_SetupPinMux(85, GPIO_MUX_CPU1, 11);
       GPIO_SetupPinOptions(85, GPIO_INPUT, GPIO_PUSHPULL);
       GPIO_SetupPinMux(84, GPIO_MUX_CPU1, 11);
       GPIO_SetupPinOptions(84, GPIO_OUTPUT, GPIO_ASYNC);
    //
    // Note: Clocks were turned on to the SCIA peripheral
    // in the InitSysCtrl() function
    //

    SciaRegs.SCICCR.all = 0x0007;   // 1 stop bit,  No loopback
                                    // No parity,8 char bits,
                                    // async mode, idle-line protocol
    SciaRegs.SCICTL1.all = 0x0003;  // enable TX, RX, internal SCICLK,
                                    // Disable RX ERR, SLEEP, TXWAKE
    SciaRegs.SCICTL2.all = 0x0003;
    SciaRegs.SCICTL2.bit.TXINTENA = 0;
    SciaRegs.SCICTL2.bit.RXBKINTENA = 0;

    //
    // SCIA at 9600 baud
    // @LSPCLK = 50 MHz (200 MHz SYSCLK) HBAUD = 0x02 and LBAUD = 0x8B //8A
    // @LSPCLK = 30 MHz (120 MHz SYSCLK) HBAUD = 0x01 and LBAUD = 0x86.
    //
    //
    // SCIA at 19200 baud
    // @LSPCLK = 50 MHz (200 MHz SYSCLK) HBAUD = 0x01 and LBAUD = 0x45.
    //
    //
    // SCIA at 38400 baud
    // @LSPCLK = 50 MHz (200 MHz SYSCLK) HBAUD = 0x00 and LBAUD = 0xA2.
    //
    //
    // SCIA at 56000 baud
    // @LSPCLK = 50 MHz (200 MHz SYSCLK) HBAUD = 0x00 and LBAUD = 0x6F.
    //
    //
    // SCIA at 57600 baud
    // @LSPCLK = 50 MHz (200 MHz SYSCLK) HBAUD = 0x00 and LBAUD = 0x6C (6B).
    //
    //
    // SCIA at 115200 baud
    // @LSPCLK = 50 MHz (200 MHz SYSCLK) HBAUD = 0x00 and LBAUD = 0x35.
    //
    SciaRegs.SCIHBAUD.all = 0x0001;
    SciaRegs.SCILBAUD.all = 0x0045;

    SciaRegs.SCICTL1.all = 0x0023;  // Relinquish SCI from Reset

    FMSTR_enable_set();
}




void FMSTR_SCI_PUTCHAR(char _data)
{
    SciaRegs.SCITXBUF.all =  _data;
}

char FMSTR_SCI_GETCHAR()
{
    return SciaRegs.SCIRXBUF.all; ;
}

void FMSTR_SCI_RE(void)
{
    SciaRegs.SCICTL1.bit.RXENA = 1;
}

void FMSTR_SCI_RD(void)
{
    SciaRegs.SCICTL1.bit.RXENA = 0;
}

void FMSTR_SCI_TE(void)
{
    SciaRegs.SCICTL1.bit.TXENA = 1;
}

void FMSTR_SCI_TD(void)
{
    SciaRegs.SCICTL1.bit.TXENA = 0;
}


FMSTR_SCISR FMSTR_SCI_RDCLRSR(void)
{
    FMSTR_SCISR SciSR = 0;

    if (SciaRegs.SCIRXST.bit.RXRDY)         // UART receive buffer full
    {
        SciSR = FMSTR_SCISR_RDRF;
    }

    if(SciaRegs.SCICTL2.bit.TXEMPTY)       // UART is transmitting data or transmit register full (UART busy)
    {
        SciSR |= FMSTR_SCISR_TDRE;
    }

    return SciSR;
}
