Jump to content
Sign in to follow this  
aravind90

buffer initialization

Recommended Posts

hi ..

am using the msp430 launchpad for my project..i need to do the communication part in my project by sending the data to a remote computer and then retrieve using the rx tool..for this i have got EZ430-RF2500 and the target board as well.. so as of now i have just transmitted the data to the TXD pin of the launchpad , without storing it in a buffer..

But now i have to store in a buffer and the transmit it so that ther wont be any intervention , in transmitting...

can u pls tell me how to initialise a buffer in the for storing and sending he data???..

Share this post


Link to post
Share on other sites

As OCY replied to your post here.

 

You will need to declare a buffer in the form of an array and initialize it to zero. Once done, populate it with what you want and chug it out of the UART.

 

Before this is answered, are you familiar with C or are you a beginner. This might help you.

Share this post


Link to post
Share on other sites

hi,,

thanks for the reply...

well !! am a neither an expert nor a beginner in C...infact i have some gud knowledge about C...

For declaring the buffer, do i need to use the #definr statement?

 

can u please give me an example of declaring a buffer??

pls it would help me a lot!!!!

Share this post


Link to post
Share on other sites

Google "ring buffer" or search for other uart c libraries. Most buffered uart libraries will have implemented a ring buffer. Check for examples from AVR, PIC, ARM, etc... uart function libraries.

Share this post


Link to post
Share on other sites

Buffers are a key part of C programing. I would recommend you buy the book recommend on the front page of the blog.

http://www.amazon.com/gp/product/0131103628?ie=UTF8&tag=machinegridco-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=0131103628. I have the same book at home.

 

That said, a buffer is usually a unsigned char array that the size is a multiple of a pointer size. in this case 16 bit so a multiple of 2 (bytes). or an array of a type that is pointer size. this helps the compiler ling up the code for faster performance.

 

if you are initializing the buffer on the stack, you can make it = {0}; which will fill it with 0. if not, you will need to use a function like memeset

Share this post


Link to post
Share on other sites

I do not think buffers need to be initialized, to zero (0) or any particular value.

When you fill a buffer, the previous value is replaced. It does not matter what the value was.

If you do not fill a buffer and try to read it, the value is meaningless and does not matter what the value is.

Share this post


Link to post
Share on other sites

UART and other stream buffers are always FIFO (first in first out) buffers and always implemented as ring buffers. I've never seen any other implementation. There's no need to initialize a ring buffer. Sadly, I have buffered UART code for 4 different microcontrollers, but the MSP430 isn't one of them. Usually I would show some code, but for some reason I've just not needed to use the UART so far on the MSP430.

Share this post


Link to post
Share on other sites

I have used a linked list or arrays and grow my buffer and as i read i shrink it. Is this bad practice in the embed world?.

 

I agree that initializing the buffer is not need. I usually only initialize my buffers if I am dealing with strings.

Share this post


Link to post
Share on other sites

For a small RAM microcontroller you would need a really special case in order to make a more complicated array like that. Dynamic variables are also difficult to implement as there is no garbage collection everything like that would need to be done on the stack.

 

Actually I do have an untested buffered read routine using USCI. I'll post a code snips of it. Remember that this is untested, though. I wrote it for reading from another device and since I don't have that device yet, I haven't even bothered to try and test it, yet. It is based on working code from my other libraries, though.

 

--  READ ONLY UART CODE USING USCI - untested lazy code, just an example, don't base your pacemaker on this code.
----------------------------------------
// vars for UART ring buffer
#define UART_RX_BUFFER_SIZE			32			// size of UART RX ring buffer

/* size of RX/TX buffers */
#define UART_RX_BUFFER_MASK ( UART_RX_BUFFER_SIZE - 1)

#if ( UART_RX_BUFFER_SIZE & UART_RX_BUFFER_MASK )
#error RX buffer size is not a power of 2
#endif

// global variables
char 	UART0_RxBuffer[uART_RX_BUFFER_SIZE];	// UART-0 RX ring buffer
char	UART0_RxBufferWr = 0;
char	UART0_RxBufferRd = 0;
char 	UART0_RxBufferOverFlow = 0;

-------------------------------------------------
SIMPLE GETC function.  It has a hard wait in there, just because everything else in my code is done in interrupt, I would not use this in a reusable library.

uint8_t uart_getc()
{
char c;

while (UART0_RxBufferRd == UART0_RxBufferWr)					// wait until there are chars received
{
         asm(" NOP");
}
c = UART0_RxBuffer[uART0_RxBufferRd++];					        // get byte
if (UART0_RxBufferRd >= sizeof(UART0_RxBuffer)) UART0_RxBufferRd = 0;	// wrap around

return c;
}

----------------------------------
THE UART RECEIVE ISR - This is USCI code, so it's a hardware UART.

#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR(void)
{
   /* Receive Data Available */
   if( UART0_RxBufferWr + 1 == UART0_RxBufferRd )					// check overflow
     UART0_RxBufferOverFlow = 1;								// set overflow flag
   else
   {
     UART0_RxBuffer[uART0_RxBufferWr++] = UCA0RXBUF;                                  // save new byte into the buffer
     if (UART0_RxBufferWr >= sizeof(UART0_RxBuffer)) UART0_RxBufferWr = 0;    // wrap around
   }
}

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...