USART1 to USART3 Bridge


USART1 to USART3 Bridge. Serial API test to check SPI1 CS10 CH A interface.

Toolkit:STM Development System

Location:/bipom/devtools/STM32/examples/usart1

Code Example


UCB uart1;
UCB uart3;

UBYTE	uart3_rxBuffer[SC16IS7XX_RX_SOFT_FIFO_LENGTH];
UBYTE	uart3_txBuffer[SC16IS7XX_TX_SOFT_FIFO_LENGTH];

DEVICE_INTERFACE spi1_int_arm;
int TComplete = 0;

//********************************************************************************

int main (void)
{
	delayMs(250);
	
	ERRCODE ec=SUCCESS;
	/* Send messages to TERMINAL window */
	tprintf("\n\rMini-Max/STM32F1");
	tprintf("\n\rUSART1 TEST REV 1.01");
	/* Initialize Device Manager */
	DM_Init();
	/* 	Initialize SPI1 and register to Device Manager	*/
	SPI1_Init(SPI1_INTERFACE_TYPE,DEVICE_INTERFACE_IN_PROGRESS);
	//

	/* Initialize SPI/SC16IS76X/UART interface ( SPI1, CS10, CH A )*/
	uart3.baudRate		= 115200;									// uart baudrate

	uart3.parity		= SERIALAPI_PARITY_NO;						// uart parity

	uart3.stop			= SERIALAPI_STOP_1;							// uart stop bit

	uart3.word_length	= SERIALAPI_WORLD_LENGTH_8;					// uart word length

	uart3.rxFifo		= SERIALAPI_FIFO_8;							// uart RX FIFO trigger level

	uart3.txFifo		= SERIALAPI_FIFO_56;						// uart TX FIFO trigger level

	//SPI1/SC16IS760

	uart3.clock			= SC16IS7XX_SPI_CLOCK;						// SPI clock = 1MHz

	uart3.iface			= SERIALAPI_SPI1_INTERFACE;					// SPI1 interface

	uart3.channel 		= CHANNEL_A;								// Channel A of sc16is76x chip 

	// GPIO

	uart3.gpioDir		= 0xE3;										// All outputs

	uart3.gpioState		= 0;
	// RTS

	uart3.rts			= SC16IS7XX_LOW_RTS;
	//

	// Define Rx/Tx buffers

	uart3.rxBuffer		= uart3_rxBuffer;
	uart3.rxLen			= sizeof(uart3_rxBuffer);
	uart3.txBuffer		= uart3_txBuffer;
	uart3.txLen			= sizeof(uart3_txBuffer);
	uart3.transferComplete = NULL;
	//

	ec = serialspi1cs10chA_init(&uart3);
	if(ec) 
	{
		tprintf("\r\nERROR: can't initialize uart3, ec = %d",ec);
		for(;;);	
	}
	tprintf("\r\nuart3 init is OK");
	delayMs(200);
	/* Initialize USART1 interface (native USART1 )*/
	uart1.iface			= SERIALAPI_UART1_INTERFACE;
	uart1.baudRate		= 115200;									// uart baudrate

	uart1.parity		= SERIALAPI_PARITY_NO;						// uart parity

	uart1.stop			= SERIALAPI_STOP_1;							// uart stop bit

	uart1.word_length	= SERIALAPI_WORLD_LENGTH_8;					// uart word length

	uart1.transferComplete = TransferComplete;
	//

	ec = serialAPI_init(&uart1);
	if(ec) 
	{
		tprintf("\r\nERROR: can't initialize USART1, ec = %d",ec);
		for(;;);	
	}
	serialAPI_tiprintf(&uart1,"\r\nuart1 init is OK");
	//

	/* Initialize INT_ARM interrupts */
	INT_ARM_Init();
	/* Register INT ARM interface  to Device Manager as a part of SPI2 interfcace */
	INT_ARM_RegisterInterface(&spi1_int_arm);
	spi1_int_arm.iFaceType 	= SPI1_INTERFACE_TYPE;
	spi1_int_arm.iFaceState	= DEVICE_INTERFACE_PROCESS_REQUEST;
	spi1_int_arm.process	= DM_Process;
	/* Run the bridge  */
	serialAPI_tiprintf(&uart1,"\r\n>");
	ec = SC16IS76X_Bridge();
	//

	serialAPI_tiprintf(&uart1,"\r\nEND, ec =%d",ec);
	return 0;
}

//********************************************************************************

ERRCODE SC16IS76X_Bridge(void)
{
	ERRCODE ec=SUCCESS;
	int dataInt=-1;
	int restart_cnt=0;
	UINT len=1;
	UBYTE dataByte;
	UINT status;
	
	for (;;)
	{
		// try to get data byte from uart1

		if (uart1.getRxCounter())
		{
			dataInt = uart1.getData();
			if(dataInt != -1 ) 
			{
				dataByte = dataInt;
				// Check if we have available space to send data

				while(uart3.getTxCounter());
				uart3.setFiFoData(&dataByte,len);
				
				if(dataInt == '?')
				{
					// Print statistics

					serialAPI_tiprintf(&uart1,"\n\rUART1: rx=%08lu tx=%08lu errors=%08lu overflows=%08lu tc=%08lu", 
						uart1.getRxCnt(),uart1.getTxCnt(),uart1.getErrors(),uart1.getOverflows(),TComplete);
					serialAPI_tiprintf(&uart1,"\n\rUART3: rx=%08lu tx=%08lu errors=%08lu overflows=%08lu", 
						uart3.getRxCnt(),uart3.getTxCnt(),uart3.getErrors(),uart3.getOverflows());
					continue; 
				}
				// Check 

				if(dataInt == '-')
				{ 
					//delayMs(500);

					if (++restart_cnt == 3) 
					{
						restart_cnt =0;
						serialAPI_tiprintf(&uart1,"\r\nRestart...");
						uart3.write(SC16IS762_IOSTATE,0x00);
						do
						{
							status = uart3.getStatus();
							serialAPI_tiprintf(&uart1,"\r\nW0status=%d",status);
						}while(status);
						//

						// Read GPIO register

						uart3.read(SC16IS762_IOSTATE);
						do
						{
							status = uart3.getStatus();
							serialAPI_tiprintf(&uart1,"\r\nR0status=%d",status);
						}while(status);
						serialAPI_tiprintf(&uart1,"\n\rUART3 IOSTATE =0x%02x", uart3.getValue());
						//

						delayMs(100);
						uart3.write(SC16IS762_IOSTATE,0x02);
						do
						{
							status = uart3.getStatus();
							serialAPI_tiprintf(&uart1,"\r\nW1status=%d",status);
						}while(status);
						//

						serialAPI_tiprintf(&uart1,"\r\nRESTART OK");
					}
				}
				else
				{
					restart_cnt =0;
				}		
			}
		}
		// try to get data byte from uart3

		if (uart3.getRxCounter())
		{
			dataInt = uart3.getData();
			if (dataInt != -1) 
			{
				dataByte = dataInt;
				len = 1;
				while(uart1.getTxCounter());
				uart1.setFiFoData(&dataByte,len);
			}		
		}
	}
	return ec;
}

//********************************************************************************

void TransferComplete(void)
{
	TComplete++;
}