UART1 to UART4 bridge on Wipom board


Establish bridge between UART1 and UART4
May initialize devices on different SPI UARTs (UART2, UART3, UART4)
If any device connected to that UARTs then they should be initialized, otherwise bridge may work not properly.

Toolkit:STM Development System

Location:/bipom/devtools/STM32/examples/Wipom/uart4_bridge

Code Example


UCB uart0;
#ifdef USE_UART2
UCB uart2;
UBYTE	uart2_rxBuffer[SC16IS7XX_RX_SOFT_FIFO_LENGTH];
UBYTE	uart2_txBuffer[SC16IS7XX_TX_SOFT_FIFO_LENGTH];
#endif
#ifdef USE_UART3
UCB uart3;
UBYTE	uart3_rxBuffer[SC16IS7XX_RX_SOFT_FIFO_LENGTH];
UBYTE	uart3_txBuffer[SC16IS7XX_TX_SOFT_FIFO_LENGTH];
#endif
#ifdef USE_UART4
UCB uart4;
UBYTE	uart4_rxBuffer[SC16IS7XX_RX_SOFT_FIFO_LENGTH];
UBYTE	uart4_txBuffer[SC16IS7XX_TX_SOFT_FIFO_LENGTH];
#endif

DEVICE_INTERFACE spi1_eint2;
DEVICE_INTERFACE spi2_eint2;


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

int main (void)
{
	ERRCODE ec;
	delayMs(250);


	/* Initialize Device Manager */
	DM_Init();
	/* 	Initialize SPI1 and register to Device Manager	*/
	SPI1_Init(SPI1_INTERFACE_TYPE,DEVICE_INTERFACE_IN_PROGRESS);
	SPI2_Init(SPI2_INTERFACE_TYPE,DEVICE_INTERFACE_IN_PROGRESS);
	/* Initialize EINT2 */
	INT_ARM_Init();
	/* Register EINT2 interface  to Device Manager as a part of SPI1 interfcace */
	INT_ARM_RegisterInterface(&spi1_eint2);
	spi1_eint2.iFaceType 	= SPI1_INTERFACE_TYPE;
	spi1_eint2.iFaceState	= DEVICE_INTERFACE_PROCESS_REQUEST;
	spi1_eint2.process		= DM_Process;

	INT_ARM_RegisterInterface(&spi2_eint2);
	spi2_eint2.iFaceType 	= SPI2_INTERFACE_TYPE;
	spi2_eint2.iFaceState	= DEVICE_INTERFACE_PROCESS_REQUEST;
	spi2_eint2.process		= DM_Process;

	/* Initialize UART0 */
	uart0.baudRate		= 19200;									// uart baudrate

	uart0.parity		= SERIALAPI_PARITY_NO;						// uart parity

	uart0.stop			= SERIALAPI_STOP_1;							// uart stop bit

	uart0.word_length	= SERIALAPI_WORLD_LENGTH_8;					// uart word length

	uart0.rxFifo		= SERIALAPI_FIFO_OFF;						// uart RX FIFO trigger level

	uart0.txFifo		= SERIALAPI_FIFO_OFF;						// uart TX FIFO trigger level

	// NATIVE UART0

	uart0.iface			= SERIALAPI_UART1_INTERFACE;				// interface

	//

	ec = serialAPI_init(&uart0);
	if(ec) 
	{
		// Print the error using the standard 19200 serial channel

		tprintf("\r\nUART0 ERROR: ec =%d",ec);
		for (;;);
	}
	/* Send messages to TERMINAL window */
	serialAPI_tiprintf(&uart0,"\r\n************************************");
	serialAPI_tiprintf(&uart0,"\r\nBT Test v.1");
	serialAPI_tiprintf(&uart0,"\r\nuart0 init is OK");
	//

	
	#ifdef USE_UART2
	uart2.baudRate	= 4800;									
	uart2.parity	= SERIALAPI_PARITY_NO;
	uart2.stop		= SERIALAPI_STOP_1;
	uart2.word_length	= SERIALAPI_WORLD_LENGTH_8;
	uart2.rxFifo	= SERIALAPI_FIFO_8;
	uart2.txFifo	= SERIALAPI_FIFO_56;
	uart2.clock		= SC16IS7XX_SPI_CLOCK;
	uart2.channel 	= CHANNEL_A;
	// Channel A: GPS

	// GPIO6 - OUTPUT , GPS ON

	// GPIO7 - OUTPUT , GPS RESET

	uart2.gpioDir	= 0xFA;										
	uart2.gpioState	= 0x38;
	uart2.rts		= SC16IS7XX_LOW_RTS;
	uart2.rxBuffer	= uart2_rxBuffer;
	uart2.rxLen		= sizeof(uart2_rxBuffer);
	uart2.txBuffer	= uart2_txBuffer;
	uart2.txLen		= sizeof(uart2_txBuffer);
	uart2.iface 	= SERIALAPI_SPI2_INTERFACE;

	serialAPI_tiprintf(&uart0,"\r\nInit uart2...");
	ec = serialspi2cs2chA_init(&uart2);
	if(ec) 
	{
		serialAPI_tiprintf(&uart0,"  FAILED");
		goto func_end;
	}
	serialAPI_tiprintf(&uart0,"\r\nuart2 init is OK");
	#endif
	
	#ifdef USE_UART3
	uart3.baudRate		= 115200;
	uart3.parity		= SERIALAPI_PARITY_NO;
	uart3.stop			= SERIALAPI_STOP_1;
	uart3.word_length	= SERIALAPI_WORLD_LENGTH_8;
	uart3.rxFifo		= SERIALAPI_FIFO_8;
	uart3.txFifo		= SERIALAPI_FIFO_56;
	uart3.clock			= SC16IS7XX_SPI_CLOCK;
	uart3.iface			= SERIALAPI_SPI1_INTERFACE;
	uart3.channel 		= CHANNEL_A;
	// GPIO

	// GPIO0 - OUTPUT , "1" - modem reset, "0" - run modem

	// GPIO1 - OUTPUT,  ON/OFF,  always "0"

	// GPIO2 - INPUT, 	RI  modem

	// GPIO3 - INPUT,  	DCD modem

	// GPIO4 - INPUT, 	DSR modem

	// GPIO5-  OUTPUT, 	DTR modem, always "0"

	// GPIO6 - OUTPUT,  "0" - Digimesh reset, "1" - run Digimesh

	// GPIO7 - OUTPUT,  "0" - Xbee reset, "1" - run Xbee

	uart3.gpioDir		= 0xE3;										
	uart3.gpioState		= 0x00;
	uart3.rts			= SC16IS7XX_LOW_RTS;

	uart3.rxBuffer	= (UBYTE *)uart3_rxBuffer;
	uart3.rxLen		= sizeof(uart3_rxBuffer);
	uart3.txBuffer	= (UBYTE *)uart3_txBuffer;
	uart3.txLen		= sizeof(uart3_txBuffer);

	serialAPI_tiprintf(&uart0,"\r\nInit uart3...");
	ec = serialspi1cs10chA_init(&uart3);
	if(ec) 
	{
		serialAPI_tiprintf(&uart0,"  FAILED");
		goto func_end;
	}

	serialAPI_tiprintf(&uart0,"\r\nuart3 init is OK");
	#endif
	
	#ifdef USE_UART4
	/* Initialize SPI/SC16IS76X/UART interface ( SPI1, CS10, CH A )*/
	uart4.baudRate		= 115200;									// uart baudrate

	uart4.parity		= SERIALAPI_PARITY_NO;						// uart parity

	uart4.stop			= SERIALAPI_STOP_1;							// uart stop bit

	uart4.word_length	= SERIALAPI_WORLD_LENGTH_8;					// uart word length

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

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

	//SPI1/SC16IS760

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

	uart4.iface			= SERIALAPI_SPI2_INTERFACE;					// SPI0 interface

	uart4.channel 		= CHANNEL_B;								// Channel A of sc16is76x chip 

	// GPIO

	uart4.gpioDir		= 0xFA;										// All outputs

	uart4.gpioState		= 0x38;
	// RTS

	uart4.rts			= SC16IS7XX_LOW_RTS;
	//

	// Define Rx/Tx buffers

	uart4.rxBuffer		= uart4_rxBuffer;
	uart4.rxLen			= sizeof(uart4_rxBuffer);
	uart4.txBuffer		= uart4_txBuffer;
	uart4.txLen			= sizeof(uart4_txBuffer);
	//

	serialAPI_tiprintf(&uart0,"\r\nInit uart4...");
	ec = serialspi2cs2chB_init(&uart4);
	if(ec) 
	{
		serialAPI_tiprintf(&uart0,"  FAILED");
		goto func_end;
	}
	serialAPI_tiprintf(&uart0,"\r\nuart4 init is OK");
	#endif
	
	// Run GPS

	//GPS_Run();

	
	/* Run the bridge  */
	serialAPI_tiprintf(&uart0,"\r\n>");
	ec = SC16IS76X_Bridge();
	//

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

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

ERRCODE GPS_Run()
{
	// Enable GPS module

	UINT gpsIoState = 0;
	gpsIoState &= ~(GPS_ON | GPS_RESET);
	uart2.write(SC16IS762_IOSTATE,gpsIoState);
	
	// Reset the device 

	gpsIoState |= GPS_RESET;
	uart2.write(SC16IS762_IOSTATE,gpsIoState);
	delayMs(150);
	gpsIoState &= ~GPS_RESET;
	uart2.write(SC16IS762_IOSTATE,gpsIoState);
	delayMs(150);

	// start GPS with LOW -> HIGH transmission

	gpsIoState &= ~GPS_ON;
	uart2.write(SC16IS762_IOSTATE,gpsIoState);
	delayMs(150);
	gpsIoState |= GPS_ON;
	uart2.write(SC16IS762_IOSTATE,gpsIoState);
	delayMs(150);
	
	return SUCCESS;
}

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

ERRCODE SetBaudrateSpi2Uart(UCB *pUcb, int Baudrate)
{
	UBYTE	regValue;
	UBYTE 	counter;
	
	// Enable divisor registers

	pUcb->read(SC16IS762_LCR);
	counter = 0;
	while(pUcb->getStatus() && (counter++) < 100) delayMs(1);
	if(pUcb->getStatus()) return 1;
	regValue = pUcb->getValue();
	
	pUcb->write(SC16IS762_LCR,regValue|0x80);
	counter = 0;
	while(pUcb->getStatus() && (counter++) < 100) delayMs(1);
	if(pUcb->getStatus()) return 2;
	
	
	// Set baud rate

	UINT divisor = (FREQUENCY_SC16IS762PRESCALER_SC16IS762)(16*(Baudrate));  
	pUcb->write(SC16IS762_DLL,divisor );
	counter = 0;
	while(pUcb->getStatus() && (counter++) < 100) delayMs(1);
	if(pUcb->getStatus()) return 3;
	

	pUcb->write(SC16IS762_DLH,divisor>>8);
	counter = 0;
	while(pUcb->getStatus() && (counter++) < 100) delayMs(1);
	if(pUcb->getStatus()) return 4;
	
	
	// Disable divisor registers, 8 data bits, 1 stop bit, no parity

	pUcb->write(SC16IS762_LCR,regValue);
	counter = 0;
	while(pUcb->getStatus() && (counter++) < 100) delayMs(1);
	if(pUcb->getStatus()) return 5;
	
	return SUCCESS;
}

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

ERRCODE SC16IS76X_Bridge(void)
{
	ERRCODE ec=SUCCESS;
	int dataInt=-1;
	UINT len;
	UBYTE dataByte;

	for (;;)
	{
		// try to get data byte from uart0

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

				int counter = 200;
				while(uart4.getTxCounter() && counter > 0)
				{
					delayMs(1);
					counter--;
				}
				
				if(counter <= 0 && uart4.getTxCounter() > 0)
				{
					dataByte = '?';
					uart0.setFiFoData(&dataByte,1);				
					
				    DM_Process(SPI0_INTERFACE_TYPE,DEVICE_INTERFACE_IN_PROGRESS);
				}
				else
				{				
					uart4.setFiFoData(&dataByte,1);
				}
				#endif
			}
			
			if(dataInt == '*' ) 
			{
				ec = SetBaudrateSpi2Uart(&uart4, 9600);
				serialAPI_tiprintf(&uart0,"\r\nBAUDRATE: 9600, ec=%d", ec);
				
			}

			if(dataInt == '@' ) 
			{
				ec = SetBaudrateSpi2Uart(&uart4, 115200);
				serialAPI_tiprintf(&uart0,"\r\nBAUDRATE: 115200, ec=%d", ec);
				
			}
		}

		
		#ifdef USE_UART4
		// try to get data byte from uart3

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

				while(uart0.getTxCounter());
				uart0.setFiFoData(&dataByte,len);
			}		
		}
		#endif
	}


	return ec;
}