2

I am trying to send printf data to my uart device. I have suitablely written write_r() funciton.

The problems that I am having is,

  • When I say printf(" My name is sam \n I am fine ");
  • and in next time when i say printf(" I am back ");

1st problem : I can see only "My name is Sam", then the cursor goes to next line and stops there.

2nd problem : "I am back" is not getting printed at all.

I tried by removing \n , still the 2nd printf doesn't get printed. There is nothing wrong with the uart.

Looks like then \ is reached the pointer is lost.

My Code

 int write(int file, char *ptr, int len) { #if !defined( OUT_8 ) #define OUT_8(p,d) (*(volatile char *)(p) = (char)(d)) #endif #if !defined( IN_8 ) #define IN_8(p) ((*(volatile char *)(p))) #endif OUT_8(DUART1_UMCR1, 0x02); //Informs external modem or peripheral that the UART is ready for sending data OUT_8(DUART1_ULCR1, 0x03); OUT_8(DUART1_UIER1, 0x0f); OUT_8(DUART1_UFCR1, 0x8f); OUT_8(DUART1_UTHR, '\n'); OUT_8(DUART1_UTHR, '\r'); while (ptr!=NULL) { if (*ptr=='\n') // JUST A TRY to avoid \n bug { OUT_8(DUART1_UTHR, '\n'); wait(); *ptr++; *ptr++; OUT_8(DUART1_UTHR, *ptr++); // \n & \r when given through printf isNot working wait(); } OUT_8(DUART1_UTHR, *ptr++); // \n & \r when given through printf is not working wait(); // TODO: if(len==0) break; else len--; } OUT_8(DUART1_UMCR1, 0x00); // say that modem is not not ready. Connection over OUT_8(DUART1_UFCR1, 0x87); OUT_8(DUART1_ULCR1, 0x00); // Clear all the interrupts ! virtually shut the UART port errno = ENOSYS; return -1; } 
5
  • I think, when you give printf("some text \n other text"), *ptr never gets '\n', it gets '\' and 'n' separately, as the input is a string. You need to handle that exclusively by something like: if(*ptr=='\' && *(ptr+1)=='n') Commented Sep 24, 2013 at 13:49
  • The whole `if (*ptr=='\n') { ...} is certainly not needed. Commented Sep 24, 2013 at 14:32
  • @Dipto Yes I tried, its not working. Commented Sep 25, 2013 at 10:43
  • @chux .. No, its not working, whenever I '\' occurs, the ptr is getting lost, the value of ptr is getting from unknown/unexpected values. Its something related to Newlib. Can you please tell where exactly this special character is programmed ? Commented Sep 25, 2013 at 10:48
  • @Sumanth In C, the backslash is written in coded as '\\'. The newline is written as '\n'. Your code has 2 characters, but the string formed has only 1. The code if(*ptr=='\n') { ...} has nothing to do with an occurrence of a backslash in ptr. Commented Sep 25, 2013 at 12:21

2 Answers 2

1

Confident that OP's I/O is buffered somewhere.
Output does not appear until a \r and/or \n is sent.

#if !defined( OUT_8 ) #define OUT_8(p,d) (*(volatile char *)(p) = (char)(d)) #endif // make ptr a `const char *` int write(int file, const char *ptr, int len) { OUT_8(DUART1_UMCR1, 0x02); //Informs UART is ready for sending data OUT_8(DUART1_ULCR1, 0x03); OUT_8(DUART1_UIER1, 0x0f); OUT_8(DUART1_UFCR1, 0x8f); while (len-- > 0) { OUT_8(DUART1_UTHR, *ptr++); wait(); } // Force an EOL at the _end_ of transmission. OUT_8(DUART1_UTHR, '\n'); OUT_8(DUART1_UTHR, '\r'); wait(); OUT_8(DUART1_UMCR1, 0x00); // say that modem is not not ready. Connection over OUT_8(DUART1_UFCR1, 0x87); OUT_8(DUART1_ULCR1, 0x00); // Clear all interrupts! virtually shut UART port errno = ENOSYS; return -1; } 

I suspect that the buffering is occurring in the receiving side, either the UART or more likely in the terminal viewing the data. OP said "the curser goes to next line". There is no "cursor" in a UART.

Sign up to request clarification or add additional context in comments.

20 Comments

Exactly, It is buffered in some code of 'vfprintf.c' of newlib. I am just not able to figure out where ? I am sure that there is no mistake in UART. Boz the UART is pure bare metal, and its working great - tested with all kinds of characters & special characters like \n etc..
@Sumanth Try sending various messages interspersed with serial cable physical disconnections/connections. Then you can know which side is buffering the data.
Well, I guess - I am using memory map heap1, where the memory cannot be freed -memory can only be created and not free'd. Please let me know, if this is the reason why printf is working only once, the 2nd call of print is not working. My question is does printf tries to free the memory after its used or when some special characters are used ?? @chux
@Sumanth printf() may be be doing some memory allocation/freeing though I doubt it. Even if it did, the problem you have is not that issue. I suspect the reason you do not see the 2nd line is due to the receiving end doing some buffering and not printf().
I checked with step input of debugger. What I observed is write() function of newlib_stubs.c (where the driver for UART is written) is never called for 2nd printf. Yes for 1st printf it does send data to write(). I guess, I am using heap1 type of memory management system, where in MEMORY CAN ONLY BE ALLOCATED; BUT NEVER FREED. can this be the problem that after 1st printf its not able to free the memory and 2nd printf is not able to have any memory for buffer ? But newlib code is very confusing to understand. @chux
|
0

Try calling fflush() to force printing without a \n.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.