<commit_before>/*--------------------------------------------------------------------------
File   : nrf5x_uart.c

Author : <NAME>          Aug. 30, 2015

Desc   : nRF5x UART implementation

Copyright (c) 2015, I-SYST, all rights reserved

Permission to use, copy, modify, and distribute this software for any purpose
with or without fee is hereby granted, provided that the above copyright
notice and this permission notice appear in all copies, and none of the
names : I-SYST, I-SYST inc. or its contributors may be used to endorse or
promote products derived from this software without specific prior written
permission.

For info or contributing contact : hnhoan at i-syst dot com

THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

----------------------------------------------------------------------------
Modified by          Date              Description

----------------------------------------------------------------------------*/
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include <stdarg.h>

#include "nrf51_bitfields.h"
#include "nrf51.h"
#include "nrf_gpio.h"
//#include "nrf_drv_gpiote.h"

#include "istddef.h"
#include "nrf5x_uart.h"
#include "idelay.h"

extern char s_Buffer[];	// defined in sbuffer.c
extern int s_BufferSize;

#define NRF51UART_FIFO_MAX		6

typedef struct {
	int Baud;
	int nRFBaud;
} NRFRATECVT;

const NRFRATECVT s_BaudnRF[] = {
	{1200, UART_BAUDRATE_BAUDRATE_Baud1200},
	{2400, UART_BAUDRATE_BAUDRATE_Baud2400},
	{4800, UART_BAUDRATE_BAUDRATE_Baud4800},
	{9600, UART_BAUDRATE_BAUDRATE_Baud9600},
	{14400, UART_BAUDRATE_BAUDRATE_Baud14400},
	{19200, UART_BAUDRATE_BAUDRATE_Baud19200},
	{28800, UART_BAUDRATE_BAUDRATE_Baud28800},
	{38400, UART_BAUDRATE_BAUDRATE_Baud38400},
	{57600, UART_BAUDRATE_BAUDRATE_Baud57600},
	{76800, UART_BAUDRATE_BAUDRATE_Baud76800},
	{115200, UART_BAUDRATE_BAUDRATE_Baud115200},
	{230400, UART_BAUDRATE_BAUDRATE_Baud230400},
	{250000, UART_BAUDRATE_BAUDRATE_Baud250000},
	{460800, UART_BAUDRATE_BAUDRATE_Baud460800},
	{921600, UART_BAUDRATE_BAUDRATE_Baud921600},
	{100000, UART_BAUDRATE_BAUDRATE_Baud1M}
};

const int s_NbBaudnRF = sizeof(s_BaudnRF) / sizeof(NRFRATECVT);

NRFUARTDEV s_nRFUartDev = {
	0,
	NRF_UART0,
	NULL,
	true,
};

bool nRFUARTWaitForRxFifo(NRFUARTDEV *pDev, uint32_t Timeout)
{
	do {
		if (pDev->pReg->EVENTS_RXDRDY || pDev->pReg->EVENTS_RXTO)
		{
			pDev->pReg->EVENTS_RXDRDY = 0;
			pDev->pReg->EVENTS_RXTO = 0;
			return true;
		}
	} while (Timeout-- > 0);

	return false;
}

bool nRFUARTWaitForTxFifo(NRFUARTDEV *pDev, uint32_t Timeout)
{
	do {
		if (pDev->pReg->EVENTS_TXDRDY || pDev->bTxReady == true)
		{
			pDev->pReg->EVENTS_TXDRDY = 0;
			pDev->bTxReady = true;
			return true;
		}
	} while (Timeout-- > 0);

	return false;
}

void UART0_IRQHandler()
{
	uint8_t buff[NRF51UART_FIFO_MAX];
	int len = 0;

	if (s_nRFUartDev.pReg->EVENTS_RXDRDY)
	{
		do {
			s_nRFUartDev.pReg->EVENTS_RXDRDY = 0;
			buff[len++] = s_nRFUartDev.pReg->RXD;
		} while (len < NRF51UART_FIFO_MAX && s_nRFUartDev.pReg->EVENTS_RXDRDY);

		if (s_nRFUartDev.pUartDev->EvtCallback)
		{
			s_nRFUartDev.pUartDev->EvtCallback(s_nRFUartDev.pUartDev, UART_EVT_RXDATA, buff, len);
		}
	}
	if (s_nRFUartDev.pReg->EVENTS_RXTO)
	{
		s_nRFUartDev.pReg->EVENTS_RXTO = 0;
		len = 0;
		if (s_nRFUartDev.pUartDev->EvtCallback)
		{
			s_nRFUartDev.pUartDev->EvtCallback(s_nRFUartDev.pUartDev, UART_EVT_RXTIMEOUT, buff, len);
		}
	}

	if (s_nRFUartDev.pReg->EVENTS_TXDRDY)
	{
		len = 1; // nRF51 only 1 byte;
		if (s_nRFUartDev.pUartDev->EvtCallback)
		{
			len = s_nRFUartDev.pUartDev->EvtCallback(s_nRFUartDev.pUartDev, UART_EVT_TXREADY, buff, len);
			uint8_t *p = buff;
			while (len > 0)
			{
				if (nRFUARTWaitForTxFifo(&s_nRFUartDev, 1000))
				{
					s_nRFUartDev.pReg->EVENTS_TXDRDY = 0;
					s_nRFUartDev.pReg->TXD = *p++;
					len--;
				}
			}
		}
		s_nRFUartDev.pReg->EVENTS_TXDRDY = 0;
		s_nRFUartDev.bTxReady = true;
	}

	if (s_nRFUartDev.pReg->EVENTS_ERROR)
	{
		s_nRFUartDev.pReg->EVENTS_ERROR = 0;
		if (s_nRFUartDev.pReg->ERRORSRC & 1)	// Overrrun
		{
			len = 0;
			do {
				s_nRFUartDev.pReg->EVENTS_RXDRDY = 0;
				buff[len] = s_nRFUartDev.pReg->RXD;
				len++;
			} while (len < NRF51UART_FIFO_MAX && s_nRFUartDev.pReg->EVENTS_RXDRDY);
			if (s_nRFUartDev.pUartDev->EvtCallback)
			{
				s_nRFUartDev.pUartDev->EvtCallback(s_nRFUartDev.pUartDev, UART_EVT_RXDATA, buff, len);
			}
		}
		s_nRFUartDev.pReg->ERRORSRC = 0;
		//len = 1;
		if (s_nRFUartDev.pUartDev->EvtCallback)
		{
			s_nRFUartDev.pUartDev->EvtCallback(s_nRFUartDev.pUartDev, UART_EVT_ERROR, buff, len);
		}
		NRF_UART0->TASKS_STARTRX = 1;
	}

	if (s_nRFUartDev.pReg->EVENTS_CTS)
	{
		s_nRFUartDev.pReg->EVENTS_CTS = 0;
		NRF_UART0->TASKS_STARTRX = 1;
	}

	if (s_nRFUartDev.pReg->EVENTS_NCTS)
	{
		s_nRFUartDev.pReg->EVENTS_NCTS = 0;
		NRF_UART0->TASKS_STARTRX = 0;
	}
}

int nRFUARTSetRate(SERINTRFDEV *pDev, int Rate)
{
	int rate = s_BaudnRF[s_NbBaudnRF].Baud;

	for (int i = 0; i < s_NbBaudnRF; i++)
	{
		if (s_BaudnRF[i].Baud >= Rate)
		{
		    NRF_UART0->BAUDRATE = (s_BaudnRF[i].nRFBaud << UART_BAUDRATE_BAUDRATE_Pos);

		    rate = s_BaudnRF[i].Baud;
		    break;
		}
	}

	return rate;
}

int nRFUARTRxData(SERINTRFDEV *pDev, uint8_t *pBuff, int Bufflen)
{
	NRFUARTDEV *dev = (NRFUARTDEV *)pDev->pDevData;
	int cnt = 0;

	while (cnt < Bufflen)
	{
		if (nRFUARTWaitForRxFifo(dev, 800000) == false)
			break;
		pBuff[cnt++] = dev->pReg->RXD;
	}

	return cnt;
}

int nRFUARTTxData(SERINTRFDEV *pDev, uint8_t *pData, int Datalen)
{
	NRFUARTDEV *dev = (NRFUARTDEV *)pDev->pDevData;
	int cnt = 0;

	while (cnt < Datalen)
	{
		if (nRFUARTWaitForTxFifo(dev, 1000) == false)
			break;
		dev->bTxReady = false;
		dev->pReg->TXD = pData[cnt];
		cnt++;
	}

	return cnt;
}

bool UARTInit(UARTDEV *pDev, const UARTCFG *pCfg)
{
//	NRFUARTDEV *dev = (NRFUARTDEV*)pDev->SerIntrf.pDevData;
	// Config I/O pins

	//NRF_GPIO->OUTSET = (1 << pCfg->PinCfg[UARTPIN_TX_IDX].PinNo);
	IOPinCfg(pCfg->PinCfg, UART_NB_PINS);
//    nrf_gpio_pin_set(pCfg->PinCfg[UARTPIN_TX_IDX].PinNo);
//    nrf_gpio_cfg_output(pCfg->PinCfg[UARTPIN_TX_IDX].PinNo);
//    nrf_gpio_cfg_input(pCfg->PinCfg[UARTPIN_RX_IDX].PinNo, NRF_GPIO_PIN_PULLUP);

	pDev->SerIntrf.pDevData = &s_nRFUartDev;
	s_nRFUartDev.pUartDev = pDev;

	//NRF_UART0->POWER = UART_POWER_POWER_Enabled << UART_POWER_POWER_Pos;

    NRF_UART0->PSELRXD = pCfg->PinCfg[UARTPIN_RX_IDX].PinNo;<commit_msg>Remove garbage on first byte transmit<commit_after>/*--------------------------------------------------------------------------
File   : nrf5x_uart.c

Author : <NAME>          Aug. 30, 2015

Desc   : nRF5x UART implementation

Copyright (c) 2015, I-SYST, all rights reserved

Permission to use, copy, modify, and distribute this software for any purpose
with or without fee is hereby granted, provided that the above copyright
notice and this permission notice appear in all copies, and none of the
names : I-SYST, I-SYST inc. or its contributors may be used to endorse or
promote products derived from this software without specific prior written
permission.

For info or contributing contact : hnhoan at i-syst dot com

THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

----------------------------------------------------------------------------
Modified by          Date              Description

----------------------------------------------------------------------------*/
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include <stdarg.h>

#include "nrf51_bitfields.h"
#include "nrf51.h"
#include "nrf_gpio.h"
//#include "nrf_drv_gpiote.h"

#include "istddef.h"
#include "nrf5x_uart.h"
#include "idelay.h"

extern char s_Buffer[];	// defined in sbuffer.c
extern int s_BufferSize;

#define NRF51UART_FIFO_MAX		6

typedef struct {
	int Baud;
	int nRFBaud;
} NRFRATECVT;

const NRFRATECVT s_BaudnRF[] = {
	{1200, UART_BAUDRATE_BAUDRATE_Baud1200},
	{2400, UART_BAUDRATE_BAUDRATE_Baud2400},
	{4800, UART_BAUDRATE_BAUDRATE_Baud4800},
	{9600, UART_BAUDRATE_BAUDRATE_Baud9600},
	{14400, UART_BAUDRATE_BAUDRATE_Baud14400},
	{19200, UART_BAUDRATE_BAUDRATE_Baud19200},
	{28800, UART_BAUDRATE_BAUDRATE_Baud28800},
	{38400, UART_BAUDRATE_BAUDRATE_Baud38400},
	{57600, UART_BAUDRATE_BAUDRATE_Baud57600},
	{76800, UART_BAUDRATE_BAUDRATE_Baud76800},
	{115200, UART_BAUDRATE_BAUDRATE_Baud115200},
	{230400, UART_BAUDRATE_BAUDRATE_Baud230400},
	{250000, UART_BAUDRATE_BAUDRATE_Baud250000},
	{460800, UART_BAUDRATE_BAUDRATE_Baud460800},
	{921600, UART_BAUDRATE_BAUDRATE_Baud921600},
	{100000, UART_BAUDRATE_BAUDRATE_Baud1M}
};

const int s_NbBaudnRF = sizeof(s_BaudnRF) / sizeof(NRFRATECVT);

NRFUARTDEV s_nRFUartDev = {
	0,
	NRF_UART0,
	NULL,
	true,
};

bool nRFUARTWaitForRxFifo(NRFUARTDEV *pDev, uint32_t Timeout)
{
	do {
		if (pDev->pReg->EVENTS_RXDRDY || pDev->pReg->EVENTS_RXTO)
		{
			pDev->pReg->EVENTS_RXDRDY = 0;
			pDev->pReg->EVENTS_RXTO = 0;
			return true;
		}
	} while (Timeout-- > 0);

	return false;
}

bool nRFUARTWaitForTxFifo(NRFUARTDEV *pDev, uint32_t Timeout)
{
	do {
		if (pDev->pReg->EVENTS_TXDRDY || pDev->bTxReady == true)
		{
			pDev->pReg->EVENTS_TXDRDY = 0;
			pDev->bTxReady = true;
			return true;
		}
	} while (Timeout-- > 0);

	return false;
}

void UART0_IRQHandler()
{
	uint8_t buff[NRF51UART_FIFO_MAX];
	int len = 0;

	if (s_nRFUartDev.pReg->EVENTS_RXDRDY)
	{
		do {
			s_nRFUartDev.pReg->EVENTS_RXDRDY = 0;
			buff[len++] = s_nRFUartDev.pReg->RXD;
		} while (len < NRF51UART_FIFO_MAX && s_nRFUartDev.pReg->EVENTS_RXDRDY);

		if (s_nRFUartDev.pUartDev->EvtCallback)
		{
			s_nRFUartDev.pUartDev->EvtCallback(s_nRFUartDev.pUartDev, UART_EVT_RXDATA, buff, len);
		}
	}
	if (s_nRFUartDev.pReg->EVENTS_RXTO)
	{
		s_nRFUartDev.pReg->EVENTS_RXTO = 0;
		len = 0;
		if (s_nRFUartDev.pUartDev->EvtCallback)
		{
			s_nRFUartDev.pUartDev->EvtCallback(s_nRFUartDev.pUartDev, UART_EVT_RXTIMEOUT, buff, len);
		}
	}

	if (s_nRFUartDev.pReg->EVENTS_TXDRDY)
	{
		len = 1; // nRF51 only 1 byte;
		if (s_nRFUartDev.pUartDev->EvtCallback)
		{
			len = s_nRFUartDev.pUartDev->EvtCallback(s_nRFUartDev.pUartDev, UART_EVT_TXREADY, buff, len);
			uint8_t *p = buff;
			while (len > 0)
			{
				if (nRFUARTWaitForTxFifo(&s_nRFUartDev, 1000))
				{
					s_nRFUartDev.pReg->EVENTS_TXDRDY = 0;
					s_nRFUartDev.pReg->TXD = *p++;
					len--;
				}
			}
		}
		s_nRFUartDev.pReg->EVENTS_TXDRDY = 0;
		s_nRFUartDev.bTxReady = true;
	}

	if (s_nRFUartDev.pReg->EVENTS_ERROR)
	{
		s_nRFUartDev.pReg->EVENTS_ERROR = 0;
		if (s_nRFUartDev.pReg->ERRORSRC & 1)	// Overrrun
		{
			len = 0;
			do {
				s_nRFUartDev.pReg->EVENTS_RXDRDY = 0;
				buff[len] = s_nRFUartDev.pReg->RXD;
				len++;
			} while (len < NRF51UART_FIFO_MAX && s_nRFUartDev.pReg->EVENTS_RXDRDY);
			if (s_nRFUartDev.pUartDev->EvtCallback)
			{
				s_nRFUartDev.pUartDev->EvtCallback(s_nRFUartDev.pUartDev, UART_EVT_RXDATA, buff, len);
			}
		}
		s_nRFUartDev.pReg->ERRORSRC = 0;
		//len = 1;
		if (s_nRFUartDev.pUartDev->EvtCallback)
		{
			s_nRFUartDev.pUartDev->EvtCallback(s_nRFUartDev.pUartDev, UART_EVT_ERROR, buff, len);
		}
		NRF_UART0->TASKS_STARTRX = 1;
	}

	if (s_nRFUartDev.pReg->EVENTS_CTS)
	{
		s_nRFUartDev.pReg->EVENTS_CTS = 0;
		NRF_UART0->TASKS_STARTRX = 1;
	}

	if (s_nRFUartDev.pReg->EVENTS_NCTS)
	{
		s_nRFUartDev.pReg->EVENTS_NCTS = 0;
		NRF_UART0->TASKS_STARTRX = 0;
	}
}

int nRFUARTSetRate(SERINTRFDEV *pDev, int Rate)
{
	int rate = s_BaudnRF[s_NbBaudnRF].Baud;

	for (int i = 0; i < s_NbBaudnRF; i++)
	{
		if (s_BaudnRF[i].Baud >= Rate)
		{
		    NRF_UART0->BAUDRATE = (s_BaudnRF[i].nRFBaud << UART_BAUDRATE_BAUDRATE_Pos);

		    rate = s_BaudnRF[i].Baud;
		    break;
		}
	}

	return rate;
}

int nRFUARTRxData(SERINTRFDEV *pDev, uint8_t *pBuff, int Bufflen)
{
	NRFUARTDEV *dev = (NRFUARTDEV *)pDev->pDevData;
	int cnt = 0;

	while (cnt < Bufflen)
	{
		if (nRFUARTWaitForRxFifo(dev, 800000) == false)
			break;
		pBuff[cnt++] = dev->pReg->RXD;
	}

	return cnt;
}

int nRFUARTTxData(SERINTRFDEV *pDev, uint8_t *pData, int Datalen)
{
	NRFUARTDEV *dev = (NRFUARTDEV *)pDev->pDevData;
	int cnt = 0;

	while (cnt < Datalen)
	{
		if (nRFUARTWaitForTxFifo(dev, 1000) == false)
			break;
		dev->bTxReady = false;
		dev->pReg->TXD = pData[cnt];
		cnt++;
	}

	return cnt;
}

bool UARTInit(UARTDEV *pDev, const UARTCFG *pCfg)
{
//	NRFUARTDEV *dev = (NRFUARTDEV*)pDev->SerIntrf.pDevData;
	// Config I/O pins

	NRF_GPIO->OUTSET = (1 << pCfg->PinCfg[UARTPIN_TX_IDX].PinNo);
	IOPinCfg(pCfg->PinCfg, UART_NB_PINS);
//    nrf_gpio_pin_set(pCfg->PinCfg[UARTPIN_TX_IDX].PinNo);
//    nrf_gpio_cfg_output(pCfg->PinCfg[UARTPIN_TX_IDX].PinNo);
//    nrf_gpio_cfg_input(pCfg->PinCfg[UARTPIN_RX_IDX].PinNo, NRF_GPIO_PIN_PULLUP);

	pDev->SerIntrf.pDevData = &s_nRFUartDev;
	s_nRFUartDev.pUartDev = pDev;

	//NRF_UART0->POWER = UART_POWER_POWER_Enabled << UART_POWER_POWER_Pos;

    NRF_UART0->PSELRXD = pCfg->PinCfg[UARTPIN_RX_IDX].PinNo;
{'fraction_non_alphanumeric': 0.1458803611738149, 'fraction_numerical': 0.03484762979683973, 'mean_word_length': 3.7256666666666667, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 155, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5870681', 'n_tokens_mistral': 6987, 'n_tokens_neox': 6165, 'n_words': 1136}