# LPC17xx Serial example

**URL:** <https://forums.freertos.org/t/lpc17xx-serial-example/3497>\
**Category:** Kernel\
**Created:** [February 25, 2010, 12:41am UTC](https://forums.freertos.org/t/lpc17xx-serial-example/3497 "2010-02-25T00:41:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [February 25, 2010, 12:41am UTC](https://forums.freertos.org/t/lpc17xx-serial-example/3497/1 "2010-02-25T00:41:14Z")

</div>

**anonymous** wrote on [Thursday, February 25, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/9b6ba662/#2f74):

Hello, I am trying to make a serial driver for the LCP1768, which I tried to derive from the serial driver that was included with STM32 examples.&nbsp; My example is close, but crashes “after some time.”&nbsp; Does anyone have a good example that works with the LPC17xx series?

Thanks

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [February 27, 2010, 12:53am UTC](https://forums.freertos.org/t/lpc17xx-serial-example/3497/2 "2010-02-27T00:53:55Z")

</div>

**anonymous** wrote on [Saturday, February 27, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/9b6ba662/#614c):

Well I haven’t seen a follow up yet, so let me expand my issue.

The main problem that I am having with the LPC1768 is that you cannot have a fully interrupt driven serial driver because the Transmit ISR only triggers on a FIFO transition to empty instead of generating interrupts while empty.&nbsp; So while IDEALLY I would like to do this:

My code would call this function to place characters into the serial buffer:

```
signed portBASE_TYPE serial_putChar(signed portCHAR ch, portTickType xBlockTime )){
	signed portBASE_TYPE xReturn = pdFAIL;
	xReturn = xQueueSend( TxQueue, &ch, xBlockTime );
	UART1->IER |= UART_IER_THREINT_EN;
	return xReturn;
}

```

And this would be the serial interrupt handler:

```
void serialISR(){
	portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
	portCHAR cChar;
	
	if(UART1->IIR & UART_IIR_INTID_THRE){
		if( xQueueReceiveFromISR( TxQueue, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE ){
			UART1->THR = cChar;
		}
		else{
			UART1->IER &= ~UART_IER_THREINT_EN;
		}
	}
	portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
}

```

This part doesn’t work like that.

So I am working to have a task load the THR transmit register for me. This is what I have so far (rough outline):

My main function call would be

```
signed portBASE_TYPE serial_putChar(signed portCHAR cOutChar, portTickType xBlockTime )
{
	signed portBASE_TYPE xReturn = pdFAIL;
	if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) == pdPASS ){
		xReturn = pdPASS;
	}
	return xReturn;
}

```

The interrupt handler:

```
void serialISR(void)
{
	portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
	portCHAR cChar;
	if(UART1->IIR & UART_IIR_INTID_THRE){
	{
		xSemaphoreGiveFromISR(xSendSemaphore, &xHigherPriorityTaskWoken);
	}
	portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
}

```

And my task to load characters into the THR register would be

```
void xUARTsendTask(void *pvParameter)
{
	uint8_t sendChar;
	vSemaphoreCreateBinary(xSendSemaphore);
	xSemaphoreTake(xSendSemaphore, 0);
	for( ;; ){
		xQueueReceive(xCharsForTx, &sendChar, portMAX_DELAY);
		UART1->THR = sendChar;
		xSemaphoreTake(xSendSemaphore, portMAX_DELAY);
	}
}

```

The problem I have with this is that it halts have transmitting 2 characters.&nbsp; So what I am looking for is some ideas on making this work.

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [February 27, 2010, 3:43am UTC](https://forums.freertos.org/t/lpc17xx-serial-example/3497/3 "2010-02-27T03:43:59Z")

</div>

**[richard\_damon](https://sourceforge.net/u/richard_damon/)** wrote on [Saturday, February 27, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/9b6ba662/#e572):

While I am not familiar with this processor, here is what I tend to use a the pseudo code for the transmit character routine:

Place character into software queue  
if that worked and serial transmit buffer is empty, force transmit buffer empty serial interrupt.

The key here is finding a way to force the interrupt. On many processors you can write to the interrupt request flag to set it, or have some other way to trigger the interrupt.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [February 27, 2010, 5:12am UTC](https://forums.freertos.org/t/lpc17xx-serial-example/3497/4 "2010-02-27T05:12:06Z")

</div>

**[tgarner](https://sourceforge.net/u/tgarner/)** wrote on [Saturday, February 27, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/9b6ba662/#aec8):

What I usually do is use the Tx interrupt enable&nbsp; as a busy flag.

Putchar©  
{  
&nbsp;&nbsp;&nbsp; if( Tx IE flag )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; put c in Queue  
&nbsp;&nbsp;&nbsp; else  
&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set Tx IE flag  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Write C directly to hardware  
&nbsp; }  
}

ISR  
&nbsp; if char in Queue  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; transmit it  
&nbsp; else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; turn off&nbsp; Tx IE flag

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [February 27, 2010, 11:48pm UTC](https://forums.freertos.org/t/lpc17xx-serial-example/3497/5 "2010-02-27T23:48:15Z")

</div>

**[richard\_damon](https://sourceforge.net/u/richard_damon/)** wrote on [Saturday, February 27, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/9b6ba662/#4e57):

That has a race condition. If the interrupt for the last character going out occurs between the test of the IE flag and the insertion into the queue, then you end up with a character in the queue that will get output out of order, as the next character sent out will go directly to the transmit buffer, and then the queued character. One fix, if it is available, is to disable interrupts around the code.
