Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


Loading

avr serial

Posted by kd5uwl on October 4, 2009
I appreciate everyone's help today. I've seen many posts and code exmples regarding the serial port on ATmega128, 644, and would apply to my 1284p. I've made the changes pointed out in this post: https://sourceforge.net/projects/freertos/forums/forum/382005/topic/1325286?message=3268597 but I still don't have it working.

If I comment out the starting of the com task and the check for it, then all the other tasks run fine.

Does anyone have more detail on this? Or, failing the code in this comtest, perhaps an example of some serial code that will work in a freertos project? I've seen where Richard posted that the Flshlite demo is more comprehensive, but I _really_ don't understand that one. And I do not need to use both com ports, just one.

I had to change lines like this:
SIGNAL( SIG_USART0_DATA )
to lines like this:
ISR( USART0_UDRE_vect )
Just to get it to compile. Note I had tried changing the signal name -- see the "USART0" above, but still got the message about it appearing to be a misspelled signal name, so switched to the ISR... above. Don't know if that is right or not.

I know it seems like there are many many posts on and around this topic, but I'm missing something.

Here is my serial.c, after the initial coments:

#include <stdlib.h>
#include <avr/interrupt.h>
#include "FreeRTOS.h"
#include "queue.h"
#include "task.h"
#include "serial.h"
#define serBAUD_DIV_CONSTANT ( ( unsigned portLONG ) 16 )
/* Constants for writing to UCSR0B. */
#define serRX_INT_ENABLE ( ( unsigned portCHAR ) (1<<RXCIE0) )
#define serRX_ENABLE ( ( unsigned portCHAR ) (1<<RXEN0) )
#define serTX_ENABLE ( ( unsigned portCHAR ) (1<<TXEN0) )
#define serTX_INT_ENABLE ( ( unsigned portCHAR ) (1<<UDRIE0) )
/* Constants for writing to UCSR0C. */
#define serUCSRC_SELECT ( ( unsigned portCHAR ) (1<<UCSZ00))
#define serEIGHT_DATA_BITS ( ( unsigned portCHAR ) (1<<UCSZ01) )
static xQueueHandle xRxedChars;
static xQueueHandle xCharsForTx;
#define vInterruptOn() \
{
unsigned portCHAR ucByte; \
\
ucByte = UCSR0B; \
ucByte |= serTX_INT_ENABLE; \
UCSR0B = ucByte; \
}
/*-----------------------------------------------------------*/

#define vInterruptOff() \
{ \
unsigned portCHAR ucInByte; \
\
ucInByte = UCSR0B; \
ucInByte &= ~serTX_INT_ENABLE;\
UCSR0B = ucInByte; \
}
/*-----------------------------------------------------------*/
xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
{
unsigned portLONG ulBaudRateCounter;
unsigned portCHAR ucByte;

portENTER_CRITICAL();
{
/* Create the queues used by the com test task. */
xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );

/* Calculate the baud rate register value from the equation in the
data sheet. */
ulBaudRateCounter = ( configCPU_CLOCK_HZ / ( serBAUD_DIV_CONSTANT * ulWantedBaud ) ) - ( unsigned portLONG ) 1;

/* Set the baud rate. */
ucByte = ( unsigned portCHAR ) ( ulBaudRateCounter & ( unsigned portLONG ) 0xff );
UBRR0L = ucByte;

ulBaudRateCounter >>= ( unsigned portLONG ) 8;
ucByte = ( unsigned portCHAR ) ( ulBaudRateCounter & ( unsigned portLONG ) 0xff );
UBRR0H = ucByte;

/* Enable the Rx interrupt. The Tx interrupt will get enabled
later. Also enable the Rx and Tx. */
UCSR0B = ( serRX_INT_ENABLE | serRX_ENABLE | serTX_ENABLE );

/* Set the data bits to 8. */
UCSR0C = ( serUCSRC_SELECT | serEIGHT_DATA_BITS );
}
portEXIT_CRITICAL();

/* Unlike other ports, this serial code does not allow for more than one
com port. We therefore don't return a pointer to a port structure and can
instead just return NULL. */
return NULL;
}
/*-----------------------------------------------------------*/
signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )
{
/* Only one port is supported. */
( void ) pxPort;

/* Get the next character from the buffer. Return false if no characters
are available, or arrive before xBlockTime expires. */
if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
{
return pdTRUE;
}
else
{
return pdFALSE;
}
}
/*-----------------------------------------------------------*/
signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )
{
/* Only one port is supported. */
( void ) pxPort;

/* Return false if after the block time there is no room on the Tx queue. */
if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
{
return pdFAIL;
}

vInterruptOn();
return pdPASS;
}
/*-----------------------------------------------------------*/
void vSerialClose( xComPortHandle xPort )
{
unsigned portCHAR ucByte;

/* The parameter is not used. */
( void ) xPort;

/* Turn off the interrupts. We may also want to delete the queues and/or
re-install the original ISR. */

portENTER_CRITICAL();
{
vInterruptOff();
ucByte = UCSR0B;
ucByte &= ~serRX_INT_ENABLE;
UCSR0B = ucByte;
}
portEXIT_CRITICAL();
}
/*-----------------------------------------------------------*/
//SIGNAL( SIG_USART0_RECV )
ISR( USART0_RX_vect )
{
signed portCHAR cChar;
signed portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;

/* Get the character and post it on the queue of Rxed characters.
If the post causes a task to wake force a context switch as the woken task
may have a higher priority than the task we have interrupted. */
cChar = UDR0;

xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );

if( xHigherPriorityTaskWoken != pdFALSE )
{
taskYIELD();
}
}
/*-----------------------------------------------------------*/
//SIGNAL( SIG_USART0_DATA )
ISR( USART0_UDRE_vect )
{
signed portCHAR cChar, cTaskWoken;

if( xQueueReceiveFromISR( xCharsForTx, &cChar, &cTaskWoken ) == pdTRUE )
{
/* Send the next character queued for Tx. */
UDR0 = cChar;
}
else
{
/* Queue empty, nothing to send. */
vInterruptOff();
}
}


Thanks to all in advance for your help.


[ Back to the top ]    [ About FreeRTOS ]    [ Privacy ]    [ Sitemap ]    [ ]


Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

NXP tweet showing LPC5500 (ARMv8-M Cortex-M33) running FreeRTOS.

Meet Richard Barry and learn about running FreeRTOS on RISC-V at FOSDEM 2019

Version 10.1.1 of the FreeRTOS kernel is available for immediate download. MIT licensed.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


Careers

FreeRTOS and other embedded software careers at AWS.



FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Espressif ESP32

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Renesas

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS

Xilinx Microblaze and Zynq partner