Terminal for printing outputs?

Go To Last Post
86 posts / 0 new

Pages

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am getting a feel of it now. There are a lot of such declarations especially extern in the project. I am doing this on AS7 (is that what you meant?) and all the files are in C(with reference to the io stream declaration - does it matter if I go for the uppercase or lowercase declarations - I read that the upper case is used as an initializer for a variable type 'FILE'

Amateur programmer.
Believe when I tell you that my struggle on here is real.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Edited: I am getting a feel of it now. There are a lot of such declarations especially extern in the project. I haven't made much changes to the code yet - I will try doing it and keep it posted. and all the files are in C(with reference to the io stream declaration - does it matter if I go for the uppercase or lowercase declarations - I read that the upper case is used as an initializer for a variable type 'FILE'

 

How do you delete a comment on here?

Amateur programmer.
Believe when I tell you that my struggle on here is real.

Last Edited: Tue. Oct 17, 2017 - 04:48 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Any views on this one?

 

http://www.avrfreaks.net/forum/o...

Amateur programmer.
Believe when I tell you that my struggle on here is real.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

azimmali wrote:

Any views on this one?

 

http://www.avrfreaks.net/forum/o...

Do you have specific questions about it?

 

As Cliff says in that thread, the first post in it is more or less a repetition of what is written in the official avrlibc documentation.

"He used to carry his guitar in a gunny sack, or sit beneath the tree by the railroad track. Oh the engineers would see him sitting in the shade, Strumming with the rhythm that the drivers made. People passing by, they would stop and say, "Oh, my, what that little country boy could play!" [Chuck Berry]

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In my previous attempt, I had done this:

 

static int uart_putchar(char c, FILE *stream);
 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
 static int 
 uart_putchar(char c, FILE *stream)
 {
  if (c == '\n')
  uart_putchar('\r', stream);
  loop_until_bit_is_set(UART_UCSRA, UART_UDRE);
  UART_UDR = c;
  return 0;
 }
int main(void)
{
 //some other stuff
 UARTIni();
 
 stdout = &mystdout;
 
 printf("Hello World!\n");
 return 0;
 //more other stuff
}*/

 

Errors:

 

 

After adding the code  in the link with some changes, instead of the previous one:

int	uart_putchar(char c, FILE *stream)
{

 if (c == '\a')			//'\a'
 {
  fputs("*ring*\n", stderr);
  return 0;
 }

 if (c == '\n')			//
 uart_putchar('\r', stream);
 loop_until_bit_is_set(UART_UCSRA, UDRE);
 UART_UDR = c;

 return 0;
}

int	uart_getchar(FILE *stream)
{
 uint8_t c;
 char *cp, *cp2;
 static char b[UART_IN_BUFFER_SIZE];
 static char *rxp;

 if (rxp == 0)
 for (cp = b;;)
 {
  loop_until_bit_is_set(UART_UCSRA, RXC);
  if (UART_UCSRA & _BV(FE))
  return _FDEV_EOF;
  if (UART_UCSRA & _BV(DOR))
  return _FDEV_ERR;
  c = UART_UDR;
  //behaviour similar to Unix stty ICRNL
  if (c == '\r')
  c = '\n';
  if (c == '\n')
  {
   *cp = c;
   uart_putchar(c, stream);
   rxp = b;
   break;
  }
  else if (c == '\t')
  c = ' ';

  if ((c >= (uint8_t)' ' && c <= (uint8_t)'\x7e') ||
  c >= (uint8_t)'\xa0')
  {
   if (cp == b + UART_IN_BUFFER_SIZE - 1)
   uart_putchar('\a', stream);
   else
   {
    *cp++ = c;
    uart_putchar(c, stream);
   }
   continue;
  }

  switch (c)
  {
   case 'c' & 0x1f:
   return -1;

   case '\b':
   case '\x7f':
   if (cp > b)
   {
    uart_putchar('\b', stream);
    uart_putchar(' ', stream);
    uart_putchar('\b', stream);
    cp--;
   }
   break;

   case 'r' & 0x1f:
   uart_putchar('\r', stream);
   for (cp2 = b; cp2 < cp; cp2++)
   uart_putchar(*cp2, stream);
   break;

   case 'u' & 0x1f:
   while (cp > b)
   {
    uart_putchar('\b', stream);
    uart_putchar(' ', stream);
    uart_putchar('\b', stream);
    cp--;
   }
   break;

   case 'w' & 0x1f:
   while (cp > b && cp[-1] != ' ')
   {
    uart_putchar('\b', stream);
    uart_putchar(' ', stream);
    uart_putchar('\b', stream);
    cp--;
   }
   break;
  }
 }

 c = *rxp++;
 if (c == '\n')
 rxp = 0;

 return c;
}

FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
stdout = stdin = &uart_str;

I still get errors:

 

 

Question: What I am doing wrong here? When does one use vfprintf and vfscanf?

Amateur programmer.
Believe when I tell you that my struggle on here is real.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

azimmali wrote:
When does one use vfprintf and vfscanf?

Possibly never (explicitly). the vfprintf(...) function is primarily the "worker" behind both printf(...) and sprintf(...). When documentation mentions vsprintf(...) it most likely does so to document that behavior that printf(...) and sprintf(...) shares, rather than documenting it for both of those two functions.

 

azimmali wrote:
What I am doing wrong here?

We can't tell.

 

You've shown a snippet of code, of about 100 lines. Yet all error messages in your screen dump are at line number 410, except for the venerable "recipe failed" - which is a stupid error message, better explained by the complete build output.

 

Speaking of that, haven't we told you before to switch to the output tab and post complete build output rather than those meaningless screen shots? If we've missed that, here it comes: Don't post screen shots. For build errors, post complete build output (switch to output tab, mark everything, copy and paste in a post here.

 

When posting such errors, also post the complete source file(s) (perhaps as attachments).

 

 

"He used to carry his guitar in a gunny sack, or sit beneath the tree by the railroad track. Oh the engineers would see him sitting in the shade, Strumming with the rhythm that the drivers made. People passing by, they would stop and say, "Oh, my, what that little country boy could play!" [Chuck Berry]

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

Last Edited: Tue. Oct 17, 2017 - 08:08 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I wanted a green signal to do that. Thanks Johan.

------ Build started: Project: EMTestRun, Configuration: Debug AVR ------
Build started.
Project "EMTestRun.cproj" (default targets):
Target "PreBuildEvent" skipped, due to false condition; ('$(PreBuildEvent)'!='') was evaluated as (''!='').
Target "CoreBuild" in file "C:\Program Files (x86)\Atmel\Studio\7.0\Vs\Compiler.targets" from project "E:\EMTestRunDev\EMTestRun\EMTestRun\EMTestRun.cproj" (target "Build" depends on it):
	Task "RunCompilerTask"
		Shell Utils Path C:\Program Files (x86)\Atmel\Studio\7.0\shellUtils
		C:\Program Files (x86)\Atmel\Studio\7.0\shellUtils\make.exe all --jobs 4 --output-sync
		Building file: .././level1_4026.c
		Invoking: AVR/GNU C Compiler : 5.4.0
		In file included from .././level1_4026.c:61:0:
c:\program files (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\avr\signal.h(36,2): warning: #warning "This header file is obsolete.  Use <avr/interrupt.h>." [-Wcpp]
		 #warning "This header file is obsolete.  Use <avr/interrupt.h>."
		  ^
		"C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-gcc.exe"  -x c -funsigned-char -funsigned-bitfields -DDEBUG  -I"C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.2.132\include"  -O1 -ffunction-sections -fdata-sections -fpack-struct -fshort-enums -g2 -Wall -mmcu=atmega64 -B "C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.2.132\gcc\dev\atmega64" -c -std=gnu99 -MD -MP -MF "level1_4026.d" -MT"level1_4026.d" -MT"level1_4026.o"   -o "level1_4026.o" ".././level1_4026.c"
		Finished building: .././level1_4026.c
		Building file: .././level1_41xx.c
		Invoking: AVR/GNU C Compiler : 5.4.0
		"C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-gcc.exe"  -x c -funsigned-char -funsigned-bitfields -DDEBUG  -I"C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.2.132\include"  -O1 -ffunction-sections -fdata-sections -fpack-struct -fshort-enums -g2 -Wall -mmcu=atmega64 -B "C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.2.132\gcc\dev\atmega64" -c -std=gnu99 -MD -MP -MF "level1_41xx.d" -MT"level1_41xx.d" -MT"level1_41xx.o"   -o "level1_41xx.o" ".././level1_41xx.c"
		Finished building: .././level1_41xx.c
		In file included from .././level1_41xx.c:63:0:
c:\program files (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\avr\signal.h(36,2): warning: #warning "This header file is obsolete.  Use <avr/interrupt.h>." [-Wcpp]
		 #warning "This header file is obsolete.  Use <avr/interrupt.h>."
		  ^
		Building file: .././boot_ld.c
		Invoking: AVR/GNU C Compiler : 5.4.0
		"C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-gcc.exe"  -x c -funsigned-char -funsigned-bitfields -DDEBUG  -I"C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.2.132\include"  -O1 -ffunction-sections -fdata-sections -fpack-struct -fshort-enums -g2 -Wall -mmcu=atmega64 -B "C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.2.132\gcc\dev\atmega64" -c -std=gnu99 -MD -MP -MF "boot_ld.d" -MT"boot_ld.d" -MT"boot_ld.o"   -o "boot_ld.o" ".././boot_ld.c"
		Finished building: .././boot_ld.c
		Building file: .././level2.c
		Invoking: AVR/GNU C Compiler : 5.4.0
		"C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-gcc.exe"  -x c -funsigned-char -funsigned-bitfields -DDEBUG  -I"C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.2.132\include"  -O1 -ffunction-sections -fdata-sections -fpack-struct -fshort-enums -g2 -Wall -mmcu=atmega64 -B "C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.2.132\gcc\dev\atmega64" -c -std=gnu99 -MD -MP -MF "level2.d" -MT"level2.d" -MT"level2.o"   -o "level2.o" ".././level2.c"
		Finished building: .././level2.c
		.././level2.c: In function 'ExtractData':
E:\EMTestRunDev\EMTestRun\EMTestRun\level2.c(581,20): warning: variable 'column_parity' set but not used [-Wunused-but-set-variable]
		   register uint8_t column_parity;
		                    ^
E:\EMTestRunDev\EMTestRun\EMTestRun\level2.c(580,20): warning: variable 'line_parity' set but not used [-Wunused-but-set-variable]
		   register uint8_t line_parity;
		                    ^
		Building file: .././level2_4026.c
		Invoking: AVR/GNU C Compiler : 5.4.0
		"C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-gcc.exe"  -x c -funsigned-char -funsigned-bitfields -DDEBUG  -I"C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.2.132\include"  -O1 -ffunction-sections -fdata-sections -fpack-struct -fshort-enums -g2 -Wall -mmcu=atmega64 -B "C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.2.132\gcc\dev\atmega64" -c -std=gnu99 -MD -MP -MF "level2_4026.d" -MT"level2_4026.d" -MT"level2_4026.o"   -o "level2_4026.o" ".././level2_4026.c"
		Finished building: .././level2_4026.c
		In file included from .././level2_4026.c:61:0:
c:\program files (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\avr\signal.h(36,2): warning: #warning "This header file is obsolete.  Use <avr/interrupt.h>." [-Wcpp]
		 #warning "This header file is obsolete.  Use <avr/interrupt.h>."
		  ^
		Building file: .././level2_41xx.c
		Invoking: AVR/GNU C Compiler : 5.4.0
		"C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-gcc.exe"  -x c -funsigned-char -funsigned-bitfields -DDEBUG  -I"C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.2.132\include"  -O1 -ffunction-sections -fdata-sections -fpack-struct -fshort-enums -g2 -Wall -mmcu=atmega64 -B "C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.2.132\gcc\dev\atmega64" -c -std=gnu99 -MD -MP -MF "level2_41xx.d" -MT"level2_41xx.d" -MT"level2_41xx.o"   -o "level2_41xx.o" ".././level2_41xx.c"
		Finished building: .././level2_41xx.c
		In file included from .././level2_41xx.c:61:0:
c:\program files (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\avr\signal.h(36,2): warning: #warning "This header file is obsolete.  Use <avr/interrupt.h>." [-Wcpp]
		 #warning "This header file is obsolete.  Use <avr/interrupt.h>."
		  ^
		Building file: .././level3_4026.c
		Invoking: AVR/GNU C Compiler : 5.4.0
		"C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-gcc.exe"  -x c -funsigned-char -funsigned-bitfields -DDEBUG  -I"C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.2.132\include"  -O1 -ffunction-sections -fdata-sections -fpack-struct -fshort-enums -g2 -Wall -mmcu=atmega64 -B "C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.2.132\gcc\dev\atmega64" -c -std=gnu99 -MD -MP -MF "level3_4026.d" -MT"level3_4026.d" -MT"level3_4026.o"   -o "level3_4026.o" ".././level3_4026.c"
		Finished building: .././level3_4026.c
		In file included from .././level3_4026.c:64:0:
c:\program files (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\avr\signal.h(36,2): warning: #warning "This header file is obsolete.  Use <avr/interrupt.h>." [-Wcpp]
		 #warning "This header file is obsolete.  Use <avr/interrupt.h>."
		  ^
		Building file: .././level4.c
		Invoking: AVR/GNU C Compiler : 5.4.0
		In file included from .././level4.c:73:0:
E:\EMTestRunDev\EMTestRun\EMTestRun\level4.c(413,1): error: conflicting types for '__iob'
		 stdout = stdin = &uart_str;
		 ^
c:\program files (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\stdio.h(407,23): info: previous declaration of '__iob' was here
		 extern struct __file *__iob[];
		                       ^
E:\EMTestRunDev\EMTestRun\EMTestRun\level4.c(413,16): warning: assignment makes integer from pointer without a cast [-Wint-conversion]
		 stdout = stdin = &uart_str;
		                ^
		In file included from .././level4.c:73:0:
E:\EMTestRunDev\EMTestRun\EMTestRun\level4.c(413,10): error: invalid initializer
		 stdout = stdin = &uart_str;
		          ^
		make: *** [level4.o] Error 1
		"C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-gcc.exe"  -x c -funsigned-char -funsigned-bitfields -DDEBUG  -I"C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.2.132\include"  -O1 -ffunction-sections -fdata-sections -fpack-struct -fshort-enums -g2 -Wall -mmcu=atmega64 -B "C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.2.132\gcc\dev\atmega64" -c -std=gnu99 -MD -MP -MF "level4.d" -MT"level4.d" -MT"level4.o"   -o "level4.o" ".././level4.c"
E:\EMTestRunDev\EMTestRun\EMTestRun\Debug\Makefile(149,1): error: recipe for target 'level4.o' failed
		make: *** Waiting for unfinished jobs....
		Building file: .././level3.c
		Invoking: AVR/GNU C Compiler : 5.4.0
		"C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-gcc.exe"  -x c -funsigned-char -funsigned-bitfields -DDEBUG  -I"C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.2.132\include"  -O1 -ffunction-sections -fdata-sections -fpack-struct -fshort-enums -g2 -Wall -mmcu=atmega64 -B "C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.2.132\gcc\dev\atmega64" -c -std=gnu99 -MD -MP -MF "level3.d" -MT"level3.d" -MT"level3.o"   -o "level3.o" ".././level3.c"
		Finished building: .././level3.c
		Building file: .././level3_41xx.c
		Invoking: AVR/GNU C Compiler : 5.4.0
		In file included from .././level3_41xx.c:64:0:
c:\program files (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\avr\signal.h(36,2): warning: #warning "This header file is obsolete.  Use <avr/interrupt.h>." [-Wcpp]
		 #warning "This header file is obsolete.  Use <avr/interrupt.h>."
		  ^
		"C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-gcc.exe"  -x c -funsigned-char -funsigned-bitfields -DDEBUG  -I"C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.2.132\include"  -O1 -ffunction-sections -fdata-sections -fpack-struct -fshort-enums -g2 -Wall -mmcu=atmega64 -B "C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.2.132\gcc\dev\atmega64" -c -std=gnu99 -MD -MP -MF "level3_41xx.d" -MT"level3_41xx.d" -MT"level3_41xx.o"   -o "level3_41xx.o" ".././level3_41xx.c"
		Finished building: .././level3_41xx.c
		Building file: .././main.c
		Invoking: AVR/GNU C Compiler : 5.4.0
		"C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-gcc.exe"  -x c -funsigned-char -funsigned-bitfields -DDEBUG  -I"C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.2.132\include"  -O1 -ffunction-sections -fdata-sections -fpack-struct -fshort-enums -g2 -Wall -mmcu=atmega64 -B "C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.2.132\gcc\dev\atmega64" -c -std=gnu99 -MD -MP -MF "main.d" -MT"main.d" -MT"main.o"   -o "main.o" ".././main.c"
		Finished building: .././main.c
	Done executing task "RunCompilerTask" -- FAILED.
Done building target "CoreBuild" in project "EMTestRun.cproj" -- FAILED.
Done building project "EMTestRun.cproj" -- FAILED.

Build FAILED.
========== Build: 0 succeeded or up-to-date, 1 failed, 0 skipped ==========

I am still getting the signal.h warning. I am using the interrupt.h

 

Warning        #warning "This header file is obsolete.  Use <avr/interrupt.h>." [-Wcpp]    EMTestRun    c:\program files (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\avr\signal.h    36

 

Attachment(s): 

Amateur programmer.
Believe when I tell you that my struggle on here is real.

Last Edited: Tue. Oct 17, 2017 - 08:31 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

azimmali wrote:
What I am doing wrong here?

stdout = stdin = &uart_str;

This is line 410, right?

You can't do that outside of a function.

 

Edit: in your latest post it is line 413

Stefan Ernst

Last Edited: Tue. Oct 17, 2017 - 08:40 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok. So in what function do I put that in? 

Amateur programmer.
Believe when I tell you that my struggle on here is real.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

azimmali wrote:
I am still getting the signal.h warning.

Look at the lines just before it in the build output. Here's one example from your build output:

 

		Building file: .././level3_4026.c
		Invoking: AVR/GNU C Compiler : 5.4.0
		"C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-gcc.exe"  -x c -funsigned-char -funsigned-bitfields -DDEBUG  -I"C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.2.132\include"  -O1 -ffunction-sections -fdata-sections -fpack-struct -fshort-enums -g2 -Wall -mmcu=atmega64 -B "C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.2.132\gcc\dev\atmega64" -c -std=gnu99 -MD -MP -MF "level3_4026.d" -MT"level3_4026.d" -MT"level3_4026.o"   -o "level3_4026.o" ".././level3_4026.c"
		Finished building: .././level3_4026.c
		In file included from .././level3_4026.c:64:0:
c:\program files (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\avr\signal.h(36,2): warning: #warning "This header file is obsolete.  Use <avr/interrupt.h>." [-Wcpp]
		 #warning "This header file is obsolete.  Use <avr/interrupt.h>."
		  ^

All those line together tell the true story:

It starts with the build system saying it compiles .././level3_4026.c , and ends with the warning about avr/signal.h . So, there is no question about avr/signal.h being included in .././level3_4026.c . You have several such errors.

 

"He used to carry his guitar in a gunny sack, or sit beneath the tree by the railroad track. Oh the engineers would see him sitting in the shade, Strumming with the rhythm that the drivers made. People passing by, they would stop and say, "Oh, my, what that little country boy could play!" [Chuck Berry]

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

azimmali wrote:
Ok. So in what function do I put that in?
UARTIni

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Solved that error. Thanks Johan, I hardly take a look at the build output. That was an eye opener.

Amateur programmer.
Believe when I tell you that my struggle on here is real.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I did that.

------ Build started: Project: EMTestRun, Configuration: Debug AVR ------
Build started.
Project "EMTestRun.cproj" (default targets):
Target "PreBuildEvent" skipped, due to false condition; ('$(PreBuildEvent)'!='') was evaluated as (''!='').
Target "CoreBuild" in file "C:\Program Files (x86)\Atmel\Studio\7.0\Vs\Compiler.targets" from project "E:\EMTestRunDev\EMTestRun\EMTestRun\EMTestRun.cproj" (target "Build" depends on it):
	Task "RunCompilerTask"
		Shell Utils Path C:\Program Files (x86)\Atmel\Studio\7.0\shellUtils
		C:\Program Files (x86)\Atmel\Studio\7.0\shellUtils\make.exe all --jobs 4 --output-sync 
		Building file: .././level4.c
		Invoking: AVR/GNU C Compiler : 5.4.0
		.././level4.c: In function 'UARTIni':
E:\EMTestRunDev\EMTestRun\EMTestRun\level4.c(294,21): error: 'uart_str' undeclared (first use in this function)
		   stdout = stdin = &uart_str;
		                     ^
E:\EMTestRunDev\EMTestRun\EMTestRun\level4.c(294,21): info: each undeclared identifier is reported only once for each function it appears in
		.././level4.c: At top level:
E:\EMTestRunDev\EMTestRun\EMTestRun\level4.c(409,13): warning: 'uart_str' defined but not used [-Wunused-variable]
		 static FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
		             ^
		make: *** [level4.o] Error 1
		"C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-gcc.exe"  -x c -funsigned-char -funsigned-bitfields -DDEBUG  -I"C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.2.132\include"  -O1 -ffunction-sections -fdata-sections -fpack-struct -fshort-enums -g2 -Wall -mmcu=atmega64 -B "C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.2.132\gcc\dev\atmega64" -c -std=gnu99 -MD -MP -MF "level4.d" -MT"level4.d" -MT"level4.o"   -o "level4.o" ".././level4.c" 
E:\EMTestRunDev\EMTestRun\EMTestRun\Debug\Makefile(149,1): error: recipe for target 'level4.o' failed
	Done executing task "RunCompilerTask" -- FAILED.
Done building target "CoreBuild" in project "EMTestRun.cproj" -- FAILED.
Done building project "EMTestRun.cproj" -- FAILED.

Build FAILED.
========== Build: 0 succeeded or up-to-date, 1 failed, 0 skipped ==========

uart_str  - undeclared error

Amateur programmer.
Believe when I tell you that my struggle on here is real.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You also need the

 static FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);

prior to the

stdout = stdin = &uart_str;

 

David (aka frog_jr)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

A lot of your questions are really just about basic C programming rather than AVR in particular. You may find it easier to learn C first using a PC development system writing programs for Windows/Linux (faster results and easier to debug) then switch to AVR development when you have a stronger understanding of C. Things like:

error: 'uart_str' undeclared (first use in this function)

are the kind of error you will get from a C compiler whatever you are building for:

#include <stdio.h>

int main(void) {
    uart_str = 1;
}

That program will cause the same error whether you build for AVR, x86, ARM, AMD64 or whatever.

 

Once you understand what "undeclared" means you can bring that knowledge to your AVR programs

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I know its a declaration error. I am not aware what value should I assign to it. stdout and stding are storing the address of uart_str. Doesn't that mean the value of uart_str can be anything?

Amateur programmer.
Believe when I tell you that my struggle on here is real.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I did that. I am still getting errors. 

Amateur programmer.
Believe when I tell you that my struggle on here is real.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

azimmali wrote:
Doesn't that mean the value of uart_str can be anything?
Yeeesss...But it has to be assigned some value before it can be used as an expression in a statement.

 

Edit:

azimmali wrote:
I did that. I am still getting errors. 
  What are the errors?

David (aka frog_jr)

Last Edited: Wed. Oct 18, 2017 - 03:03 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

azimmali wrote:
I did that. I am still getting errors. 

This is getting ridiculous!

 

Are you sure you're not trolling us? )-:

 

What did you do? We can't tell exactly what you did only from the word "that" and an implication that tou've taken some earlier advice and applied it. SHOW CODE!

 

Which errors did you get? We can't guess (especially since we don't know what you did). SHOW ERROR MESSAGES! I.e. build output.

"He used to carry his guitar in a gunny sack, or sit beneath the tree by the railroad track. Oh the engineers would see him sitting in the shade, Strumming with the rhythm that the drivers made. People passing by, they would stop and say, "Oh, my, what that little country boy could play!" [Chuck Berry]

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

Last Edited: Wed. Oct 18, 2017 - 03:16 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
------ Build started: Project: EMTestRun, Configuration: Debug AVR ------
Build started.
Project "EMTestRun.cproj" (default targets):
Target "PreBuildEvent" skipped, due to false condition; ('$(PreBuildEvent)'!='') was evaluated as (''!='').
Target "CoreBuild" in file "C:\Program Files (x86)\Atmel\Studio\7.0\Vs\Compiler.targets" from project "E:\EMTestRunDev\EMTestRun\EMTestRun\EMTestRun.cproj" (target "Build" depends on it):
 Task "RunCompilerTask"
  Shell Utils Path C:\Program Files (x86)\Atmel\Studio\7.0\shellUtils
  C:\Program Files (x86)\Atmel\Studio\7.0\shellUtils\make.exe all --jobs 4 --output-sync
  Building file: .././level4.c
  Invoking: AVR/GNU C Compiler : 5.4.0
E:\EMTestRunDev\EMTestRun\EMTestRun\level4.c(201,20): error: "[" may not appear in macro parameter list

   #define stdin(__iob[0])
       ^
E:\EMTestRunDev\EMTestRun\EMTestRun\level4.c(202,21): error: "[" may not appear in macro parameter list
   #define stdout(__iob[1])
        ^
E:\EMTestRunDev\EMTestRun\EMTestRun\level4.c(203,21): error: "[" may not appear in macro parameter list
   #define stderr(__iob[2])
        ^
E:\EMTestRunDev\EMTestRun\EMTestRun\level4.c(210,0): warning: "FDEV_SETUP_STREAM" redefined
   #define FDEV_SETUP_STREAM(put, get, rwflag)
   ^
  In file included from .././level4.c:73:0:
c:\program files (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\stdio.h(389,0): info: this is the location of the previous definition
   #define FDEV_SETUP_STREAM(p, g, f) \
   ^
E:\EMTestRunDev\EMTestRun\EMTestRun\level4.c(211,78): error: expected expression before ';' token
   FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
                       ^
E:\EMTestRunDev\EMTestRun\EMTestRun\level4.c(224,9): error: conflicting types for 'uart_str'
  "C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-gcc.exe"  -x c -funsigned-char -funsigned-bitfields -DDEBUG  -I"C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.2.132\include"  -O1 -ffunction-sections -fdata-sections -fpack-struct -fshort-enums -g2 -Wall -mmcu=atmega64 -B "C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.2.132\gcc\dev\atmega64" -c -std=gnu99 -MD -MP -MF "level4.d" -MT"level4.d" -MT"level4.o"   -o "level4.o" ".././level4.c"
E:\EMTestRunDev\EMTestRun\EMTestRun\Debug\Makefile(149,1): error: recipe for target 'level4.o' failed
   uint8_t uart_str = 1;
     ^
E:\EMTestRunDev\EMTestRun\EMTestRun\level4.c(211,6): info: previous definition of 'uart_str' was here
   FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
     ^
  .././level4.c: In function 'UARTIni':
E:\EMTestRunDev\EMTestRun\EMTestRun\level4.c(300,3): error: 'uart_state' undeclared (first use in this function)
     uart_state = UART_EMPTY;
     ^
E:\EMTestRunDev\EMTestRun\EMTestRun\level4.c(300,3): info: each undeclared identifier is reported only once for each function it appears in
E:\EMTestRunDev\EMTestRun\EMTestRun\level4.c(302,18): warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
     stdout = stdin = &uart_str;
        ^
  .././level4.c: At top level:
E:\EMTestRunDev\EMTestRun\EMTestRun\level4.c(417,13): error: conflicting types for 'uart_str'
   static FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
      ^
E:\EMTestRunDev\EMTestRun\EMTestRun\level4.c(224,9): info: previous definition of 'uart_str' was here
   uint8_t uart_str = 1;
     ^
E:\EMTestRunDev\EMTestRun\EMTestRun\level4.c(417,85): error: expected expression before ';' token
   static FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
                        ^
E:\EMTestRunDev\EMTestRun\EMTestRun\level4.c(417,13): warning: 'uart_str' defined but not used [-Wunused-variable]
   static FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
      ^
  make: *** [level4.o] Error 1
 Done executing task "RunCompilerTask" -- FAILED.
Done building target "CoreBuild" in project "EMTestRun.cproj" -- FAILED.
Done building project "EMTestRun.cproj" -- FAILED.

Build FAILED.
========== Build: 0 succeeded or up-to-date, 1 failed, 0 skipped ==========

I am attaching the .c and .h files of the UART with this message.

Attachment(s): 

Amateur programmer.
Believe when I tell you that my struggle on here is real.

Last Edited: Wed. Oct 18, 2017 - 03:17 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am sorry you felt that way. I don't have a lot of programming experience. I am doing all of this out of pressure. I dont have an option right now. I noticed that the authors of the AVR platform are members on here too and I respect that. I would never do something like trolling you all.  I am close to that tipping point of giving up but I just dont want to run away from it.

Amateur programmer.
Believe when I tell you that my struggle on here is real.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
/*  
*-------------------------------------------------------------------------------
*--  RCSId: $Id: level4.c,v 1.13 2009-12-08 09:49:19+01 mjg Exp $
*--         $Name:  $
*-------------------------------------------------------------------------------
*--
*--  Description   : Level4 source file
*--
*------------------------------------------------------------------------------- 
*--                          History
*-------------------------------------------------------------------------------
*-- $Log: level4.c,v $
*-- Revision 1.13  2009-12-08 09:49:19+01  mjg
*-- EM4450 decode number corrected
*--
*-- Revision 1.12  2009-10-30 09:37:21+01  mjg
*-- release 0.15, EM6869 SendAccess support
*--
*-- Revision 1.11  2009-10-01 14:03:40+02  mjg
*-- basic EM6869 support ported
*--
*-- Revision 1.10  2008-01-25 16:11:07+01  mjg
*-- 4x69 1st reception failure fixed, dummy read removed
*--
*-- Revision 1.9  2007-06-15 13:28:06+02  mjg
*-- 0.11, EM4205/EM4305 support
*--
*-- Revision 1.8  2007-05-11 08:21:00+02  mjg
*-- 0.10, bootloader switch response corrected
*--
*-- Revision 1.7  2007-04-10 13:16:13+02  mjg
*-- 0.10
*--
*-- Revision 1.6  2006-12-05 15:47:54+01  mjg
*-- 0.9, bootloader update
*--
*-- Revision 1.5  2006-09-14 11:25:39+02  mjg
*-- 0.8, EMDB408 bootloader reused
*--
*-- Revision 1.4  2006-07-27 15:40:42+02  mjg
*-- 0.7, another bootloader fixup, uisp programming, gcc 20050214 compiler
*--
*-- Revision 1.3  2006-06-15 11:03:50+02  mjg
*-- automatic EM4026 slow and free scan added
*--
*-- Revision 1.2  2006-01-17 11:06:26+01  mjg
*-- release 0.5
*--
*-- Revision 1.1  2005-12-12 17:05:24+01  mjg
*-- Initial revision
*--
*-- Revision 1.2  2005-11-01 14:50:11+01  hcu
*-- version 0.4
*--
*-- Revision 1.1  2005-09-26 08:38:15+02  hcu
*-- Initial revision
*--
*-- Revision 1.3  2005-08-08 07:58:16+02  hcu
*-- firmware_0.1
*--
*-- Revision 1.2  2005-07-19 16:12:54+02  hcu
*-- check in
*--
*-------------------------------------------------------------------------------
*/


#include <avr/io.h> 
#include <inttypes.h> 
#include <avr/interrupt.h> 
//#include <avr/signal.h> 
#include <avr/wdt.h>
#include <stdio.h> 
#include "level4.h" 
#include "level3.h" 
#include "level2.h" 
#include "level1.h" 
 
//------------------------------------------------------------------- 
// 41xx 
//------------------------------------------------------------------- 
 
uint16_t write_tag_memory_old_login_low; // low word of Login cmd 
uint16_t write_tag_memory_old_login_hi;  // hi word of Login cmd 
uint8_t checksum; 

//------------------------------------------------------------------- 
// 4026 
//------------------------------------------------------------------- 
uint8_t tag_drate;                      // expected tag data rate 
 
uint8_t scan_method;                    // scan method; 0=free, 1= , 2= 
uint8_t crc_check;                      // 0=6-bit, 1=16-bit
uint8_t switch_coil_byte; 

//------------------------------------------------------------------- 
// 6869 
//------------------------------------------------------------------- 
uint16_t read_txp_memory_address;       // Read  6869
uint8_t  read_txp_memory_lwr;           // last read word within the page
uint16_t read_txp_memory_word; 

uint16_t write_txp_memory_address;
uint8_t  write_txp_memory_lwr;          // last read word within the page
uint16_t write_txp_memory_data; 
 
//------------------------------------------------------------------- 
//global declarations 
//------------------------------------------------------------------- 
 
uint16_t write_tag_memory_delay;

uint16_t write_4095_low;                // low word of SPI configuration word
uint16_t write_4095_hi;                 // hi word of SPI configuration word



uint8_t volatile uart_command;          // command handshake value 
                                        // set by UART RECV 
                                        // reset by main() when done 
 
uint8_t read_tag_memory_word_address;   // address of Read cmd 
uint16_t read_tag_memory_word_low;      // Read Cmd content low 
uint16_t read_tag_memory_word_hi;       // Read Cmd content hi 
 
uint8_t read_tag_memory_word_fwr;       // address of Read cmd 
uint8_t read_tag_memory_word_lwr;       // address of Read cmd 
 
uint16_t write_tag_memory_word_address; // address of Write Tag cmd 
uint16_t write_tag_memory_word_low;     // low word of Write Tag cmd 
uint16_t write_tag_memory_word_hi;      // hi word of Write Tag cmd 
 
uint16_t write_tag_memory_login_low;    // low word of Login cmd 
uint16_t write_tag_memory_login_hi;     // hi word of Login cmd 
 
uint8_t write_tag_memory_forward_link;  // forward item of Forward link cmd 
 
uint16_t read_only_word_low;            // Read Only Cmd content low 
uint16_t read_only_word_hi;             // Read Only Cmd content hi 
 
uint8_t config_data_rate;               // see EM4469 spec 
uint8_t config_encoder;                 // see EM4469 spec 
uint8_t config_psk_carrier;             // see EM4469 spec 
uint8_t config_delayed;                 // see EM4469 spec 
uint8_t config_lwr;                     // see EM4469 spec 
uint8_t config_raw;                     // see EM4469 spec 
uint8_t config_forward_link;            // 0=4050 type, 1=other 
uint8_t config_write_conf;              // 1=write this configuration into the EEPROM 
uint8_t config_inv_sync;                // 1=EM4469 uplink sync inversion
 
uint8_t debug_mode=0; //what does this do and why is it initialized to zero?

uint8_t debug_cnt __attribute__ ((section(".noinit")));
uint8_t debug_buffer[DBG_BUFFER_SIZE] __attribute__ ((section(".noinit")));
 
//------------------------------------------------------------------- 
//local declarations 
//------------------------------------------------------------------- 
 
#define UART_IN_BUFFER_SIZE 48             // incoming data uart buffer reserved size /bytes?

//Assign values to the commented lines of code
 
uint8_t volatile uart_in_buffer[UART_IN_BUFFER_SIZE]; // input cyclic buffer
//uint8_t volatile uart_in_buffer[UART_IN_BUFFER_SIZE] =  "peeva999usa" ;
uint8_t volatile uart_in_read;                        // input read index 
//uint8_t volatile uart_in_read = 
uint8_t volatile uart_in_end;                         // input message read index 
//uint8_t volatile uart_in_end = 
uint8_t volatile uart_in_write;                       // input write index 
//uint8_t volatile uart_in_write = 11;
uint8_t volatile uart_in_overflow;                    // input buffer overflow 
//uint8_t volatile uart_in_overflow =
uint8_t volatile uart_in_error;                       // input buffer parity error, ... 
//uint8_t volatile uart_in_error = 
 
#define UART_OUT_BUFFER_SIZE 250           // outgoing data uart buffer reserved size //bytes?

uint8_t volatile uart_out_buffer[UART_OUT_BUFFER_SIZE];   // output buffer
//uint8_t volatile uart_out_buffer[UART_OUT_BUFFER_SIZE] = "965 000 000 396 372"
uint8_t volatile uart_out_read;                       // output read index
//uint8_t volatile uart_out_read = 
uint8_t volatile uart_out_write;                      // output write index
//uint8_t volatile uart_out_write = 19
uint8_t volatile uart_out_end;                        // last output write index 
//uint8_t volatile uart_out_end =  
uint8_t volatile uart_read_bytes;       // number of bytes to read before parsing 
//uint8_t volatile uart_read_bytes = 
uint8_t volatile uart_read_msg_bytes;   // actual number of bytes (msg position no.2) 
//uint8_t volatile uart_read_msg_bytes =  


//UART_STATE - enum is errorneous in gcc+sim/debug 
#define UART_EMPTY           1          // no bytes are pending 
#define UART_READ_SIZE       2          // try to analyze incoming message size from pending bytes 
#define UART_READ_BYTES      3          // try to analyze body and ETX of message from pending bytes 
#define UART_WAIT_ERROR_SENT 4          // something is wrong within analysed pending bytes 
#define UART_VALID           5          // known message format was detected, rest is still pending 
 
//#define DEBUG_UART_BUFFER
#define stdin(__iob[0])
#define stdout(__iob[1])
#define stderr(__iob[2])
#define 	_FDEV_SETUP_READ    __SRD
#define 	_FDEV_SETUP_WRITE   __SWR
#define 	_FDEV_SETUP_RW   (__SRD|__SWR)

//printf

#define FDEV_SETUP_STREAM(put, get, rwflag)
FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);

/*#define fdev_setup_stream(stream, p, g, f)
do { 
	(stream)->put = p; 
	(stream)->get = g; 
	(stream)->flags = f; 
	(stream)->udata = 0;
} while(0)*/


uint8_t uart_state;                     // current state of incoming message analysis  
//------------------------------------------------------------------- 
uint8_t uart_str = 1;
uint8_t ParseMessage(void);

//------------------------------------------------------------------- 
// uart byte receive interrupt 
 
SIGNAL (UART_RECV_VECTOR) 
{ 
  uart_in_buffer[uart_in_write++] = UART_UDR;              //store byte into cyclic buffer 
 
  if (uart_in_write == UART_IN_BUFFER_SIZE) 
    uart_in_write = 0; 
 
  if (uart_in_write == uart_in_read) {   
    uart_in_overflow = 1;                              //set flag in overflow 
    //block reception until some bytes are parsed out 
  } 
 
  if ((UART_UCSRA & ((1<<FE0)|(1<<DOR0)|(1<<UPE0))) != 0) {     //frame error, data overrun or parity error 
    uart_in_error = 1;                                 //set flag in error 
  }; 
 
} 


// ==================================================================
// uart byte transmission

SIGNAL (UART_TX_VECTOR)
{
  if (uart_out_end != uart_out_read) {                 //if current message is being sent
    UART_UDR = uart_out_buffer[uart_out_read++];            //send next byte
    if (uart_out_read == UART_OUT_BUFFER_SIZE)
      uart_out_read = 0;
  } else {
    cbi(UART_UCSRB, UDRIE);                                 //else stop
  }
}

// ==================================================================
// increase uart_out_write pointer by 1 
// (to consider which one has better space/speed trade-off)

void IncWriteOut(uint8_t x) {
  uart_out_buffer[uart_out_write++] = x;
  if (uart_out_write == UART_OUT_BUFFER_SIZE)
    uart_out_write = 0;
}

//------------------------------------------------------------------- 
// uart init routine 
//------------------------------------------------------------------- 
//static FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW); 
void UARTIni(void) { 
 
   // UCSRA is not necessary to set up, using initial values         
   // no double transmission speed, no multi-processor mode           
//FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
   UART_UCSRA = UART_UCSRA_INIT;
   UART_UCSRB  = UART_UCSRB_INIT; 
                       // set up : RXCIE, UDRIE, RXEN, TXEN           
                       // (interrupt enable from receiver,            
                       // UART receiver enable, UART transmit enable) 

   UART_UCSRC = UART_UCSRC_INIT; 
                       // asynchronous operation, 8 data bits,        
                       // no parity, 1 stop bit                       

   UART_UBRRL = UART_SPEED; // baud rate - see UBRR register setting table 
                       //             in AVR ATmega8 specification    
                       // UBRRH - using initial values                
                       // UBRRL - 25 for 38k4Bd and frequency f=16 MHz 
                       // (using SINGLE transmission speed)           

  uart_in_read = uart_in_write = uart_in_end = 0;
  uart_out_read = uart_out_write = uart_out_end = 0; 
  uart_state = UART_EMPTY;
  //int uart_str = 1;
  stdout = stdin = &uart_str;
} 

//printf/scanf

int	uart_putchar(char c, FILE *stream)
{

	if (c == '\a')			//'\a'
	{
		fputs("*ring*\n", stderr);
		return 0;
	}

	if (c == '\n')			//
	uart_putchar('\r', stream);
	loop_until_bit_is_set(UART_UCSRA, UART_UDRE);
	UART_UDR = c;

	return 0;
}

int	uart_getchar(FILE *stream)
{
	uint8_t c;
	char *cp, *cp2;
	static char b[UART_IN_BUFFER_SIZE];
	static char *rxp;

	if (rxp == 0)
	for (cp = b;;)
	{
		loop_until_bit_is_set(UART_UCSRA, RXC);
		if (UART_UCSRA & _BV(FE))
		return _FDEV_EOF;
		if (UART_UCSRA & _BV(DOR))
		return _FDEV_ERR;
		c = UART_UDR;
		//behaviour similar to Unix stty ICRNL
		if (c == '\r')
		c = '\n';
		if (c == '\n')
		{
			*cp = c;
			uart_putchar(c, stream);
			rxp = b;
			break;
		}
		else if (c == '\t')
		c = ' ';

		if ((c >= (uint8_t)' ' && c <= (uint8_t)'\x7e') ||
		c >= (uint8_t)'\xa0')
		{
			if (cp == b + UART_IN_BUFFER_SIZE - 1)
			uart_putchar('\a', stream);
			else
			{
				*cp++ = c;
				uart_putchar(c, stream);
			}
			continue;
		}

		switch (c)
		{
			case 'c' & 0x1f:
			return -1;

			case '\b':
			case '\x7f':
			if (cp > b)
			{
				uart_putchar('\b', stream);
				uart_putchar(' ', stream);
				uart_putchar('\b', stream);
				cp--;
			}
			break;

			case 'r' & 0x1f:
			uart_putchar('\r', stream);
			for (cp2 = b; cp2 < cp; cp2++)
			uart_putchar(*cp2, stream);
			break;

			case 'u' & 0x1f:
			while (cp > b)
			{
				uart_putchar('\b', stream);
				uart_putchar(' ', stream);
				uart_putchar('\b', stream);
				cp--;
			}
			break;

			case 'w' & 0x1f:
			while (cp > b && cp[-1] != ' ')
			{
				uart_putchar('\b', stream);
				uart_putchar(' ', stream);
				uart_putchar('\b', stream);
				cp--;
			}
			break;
		}
	}

	c = *rxp++;
	if (c == '\n')
	rxp = 0;

	return c;
}

static FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
//int uart_str = 1;
//static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
//static int uart_putchar(char c, FILE *stream);
//stdout = &uart_str;
//stdout = stdin = &uart_str;
//stdin = &uart_str;
	
 
 
// ================================================================== 
// parsing procedures 
// ================================================================== 
 
// ================================================================== 
// checks pending bytes in uart buffer to find known message format 
// if already received bytes form incomplete known message, UART_MESSAGE_OK is returned 
// if any chaos bytes are detected, appropriate error is returned 
// if complete known message is detected and parsed ok, command type is returned, 
//    uart_read_msg_bytes size is set and command respective values are parsed 
 
uint8_t CheckIncommingMessage(void) { 
 
uint8_t ptr = uart_in_read; 
uint8_t result = UART_MESSAGE_OK; 
uint8_t c; 
 
 
  if (uart_in_overflow != 0) { 
  //if(uart_in_overflow = 0) {  
	uart_in_overflow = 0;              //buffer is dropped 
    uart_in_read = uart_in_write;      //=async 
    return ERR_UART_OVERFLOW; 
  } 
 
  if (uart_in_error != 0) {
  //if(uart_in_error = 0)	{   
    uart_in_error = 0;                 //buffer is dropped 
    uart_in_read = uart_in_write;      //=async 
    return ERR_UART_ERROR_FLAG; 
  } 
 
  if (uart_in_read == uart_in_write) { 
    return UART_MESSAGE_OK; 
  } 
 
  uart_state = UART_EMPTY; 
  result = UART_MESSAGE_OK; 
 
 
 while ((ptr != uart_in_write) && (uart_state != UART_WAIT_ERROR_SENT) && (uart_state != UART_VALID)) { 
 
    c = uart_in_buffer[ptr]; 
   
    //positive branches inside each case are usually written first  
 
    switch (uart_state) { 
    case UART_EMPTY :                //detect STX 
      if (c == 0x00) {               //zero byte is ignored 
      } else  
      if (c == 0x02) {              
        uart_state = UART_READ_SIZE; 
      } else { 
 
        uart_state = UART_WAIT_ERROR_SENT; 
        result = ERR_UART_ERROR_FLAG; 
      } 
      break; 
 
    case UART_READ_SIZE :            //capture size byte 
 
      if ((c >= 3) || (c <= 9)) { 
        uart_read_bytes = c; 
        uart_read_msg_bytes = c; 
        uart_state = UART_READ_BYTES; 
      } else { 
        result = ERR_UART_INTERBYTE_ERR; 
        uart_state = UART_WAIT_ERROR_SENT; 
      } 
      break; 
 
    case UART_READ_BYTES :           //read specified size bytes to allow  
 
      if (--uart_read_bytes == 0) {  //last byte - ETX expected 
        //check ETX 
        if (c == 0x03) { 
          result = UART_MESSAGE_OK; 
          uart_state = UART_VALID; 
          uart_in_end = ptr; 
        } else { 
          result = ERR_UART_NO_ETX; 
          uart_state = UART_WAIT_ERROR_SENT; 
        } 
      } 
      break; 
    }; 
 
    if(++ptr == UART_IN_BUFFER_SIZE) 
      ptr = 0; 
  } 
 
 
  if (uart_state == UART_WAIT_ERROR_SENT) { 
    uart_in_read = uart_in_write;      //dropped =async 
    return result; 
  } 
 
  if (uart_state == UART_VALID) { 
    result = ParseMessage();            //parse message and commit the command 
    uart_in_read = ptr; 
    return result; 
  } 
  return result;
  //return (0); 
} 
 
 
 
 
//------------------------------------------------------------------- 
// parses valid message format and checks size according to the command type 
//------------------------------------------------------------------- 
 
uint8_t ParseMessage(void) { 
 
  uint8_t byte = 0; 
  uint8_t ptr = uart_in_read; 
  uint8_t ptr2 = 0, c; 
  uint8_t temp_buffer[UART_IN_BUFFER_SIZE]; 
   
  //compute checksum 
  byte = uart_in_buffer[ptr]; 
 
  while (ptr != uart_in_end) { 
    c = uart_in_buffer[ptr++]; 
    if(ptr == UART_IN_BUFFER_SIZE)  
      ptr = 0; 
 
    byte ^= c; 
    temp_buffer[ptr2++] = c;     
  } 
 
  if (byte == 0) {  //checksum ok 
 
    SetLEDOff;

    byte = temp_buffer[2]; 

//------------------------------------------------------------------------------------- 
//4102 
 
    if(byte == 0x10) {                          //Auto Read Only Continuous mode (EM4102) 
        
      if (uart_read_msg_bytes != 3) return ERR_UART_INTERBYTE_ERR;
      
    }
 
//------------------------------------------------------------------------------------- 
//4105 
    else if(byte == 0x11) {                          //Read Only Continuous mode (EM4105) 
      if (uart_read_msg_bytes != 3) return ERR_UART_INTERBYTE_ERR; 
    }
 
 
//------------------------------------------------------------------------------------- 
//4150 
 
    else if(byte == 0x30) {                          //LOGIN 

      if (uart_read_msg_bytes != 8) return ERR_UART_INTERBYTE_ERR; 
      write_tag_memory_login_low = ((uint16_t)temp_buffer[6] << 8) + temp_buffer[7]; 
      write_tag_memory_login_hi  = ((uint16_t)temp_buffer[4] << 8) + temp_buffer[5]; 

      forward_link_type = EM4150; 

    }  
 
    else if(byte == 0x31) { //WRITE PASSWORD 
 
      if (uart_read_msg_bytes != 12) return ERR_UART_INTERBYTE_ERR; 
      write_tag_memory_old_login_low = ((uint16_t)temp_buffer[6] << 8) + temp_buffer[7]; 
      write_tag_memory_old_login_hi  = ((uint16_t)temp_buffer[4] << 8) + temp_buffer[5]; 
      write_tag_memory_login_low = ((uint16_t)temp_buffer[10] << 8) + temp_buffer[11]; 
      write_tag_memory_login_hi  = ((uint16_t)temp_buffer[8] << 8) + temp_buffer[9]; 

      forward_link_type = EM4150; 

    }
 
    else if(byte == 0x32) { //WRITE 

      if (uart_read_msg_bytes != 8)  return ERR_UART_INTERBYTE_ERR; 
      write_tag_memory_word_address = temp_buffer[3]; 
      write_tag_memory_word_low = ((uint16_t)temp_buffer[6] << 8) + temp_buffer[7]; 
      write_tag_memory_word_hi  = ((uint16_t)temp_buffer[4] << 8) + temp_buffer[5]; 

      forward_link_type = EM4150; 

    }

    else if(byte == 0x33) { //SELECTIVE READ 
       
      if (uart_read_msg_bytes != 8)  return ERR_UART_INTERBYTE_ERR; 
      read_tag_memory_word_lwr = temp_buffer[6]; 
      read_tag_memory_word_fwr = temp_buffer[7]; 

      forward_link_type = EM4150; 

    }

    else if(byte == 0x34) { //RESET 
      
      if (uart_read_msg_bytes != 3)  return ERR_UART_INTERBYTE_ERR; 

      forward_link_type = EM4150; 

    }

    else if(byte == 0x35) { //READ TAG DEFINITION 
      
      if (uart_read_msg_bytes != 3)  return ERR_UART_INTERBYTE_ERR; 

      forward_link_type = EM4150; 

    }
 
//------------------------------------------------------------------------------------- 
//EM4026 
 
    else if(byte == 0x50) {                                 //Send Code ID 
 
      if (uart_read_msg_bytes != 6) return ERR_UART_INTERBYTE_ERR; 

      tag_drate = temp_buffer[3]; 
      scan_method = temp_buffer[4]; 
      crc_check = temp_buffer[5]; 
      automatic = NO_SCAN;                      //disable automatic function 
 
      forward_link_type = EM4026; 

    }

 
    else if(byte == 0x51) {                                 //Scan or Single scan 
      if (uart_read_msg_bytes != 6) return ERR_UART_INTERBYTE_ERR; 

      tag_drate = temp_buffer[3]; 
      scan_method = temp_buffer[4]; 
      crc_check = temp_buffer[5]; 
      automatic = NO_SCAN;                     //disable automatic function 
 
      forward_link_type = EM4026; 
  
    }
 
 

    else if(byte == 0x52) {                                //Scan or Single scan 

        if (uart_read_msg_bytes != 6) 
          return ERR_UART_INTERBYTE_ERR; 

       //  sbi( PORTB, SHD_PIN );
       //  for(countr=0;countr<2500;countr++); // 1ms stop
       //  cbi( PORTB, SHD_PIN );

        tag_drate = temp_buffer[3]; 
        scan_method = temp_buffer[4]; 
        crc_check = temp_buffer[5]; 
//        automatic = NO_SCAN;                     //disable automatic function 

        forward_link_type = EM4026; 
    }
 

    else if(byte == 0x53) {                                 //Default Read 
      if (uart_read_msg_bytes != 3) return ERR_UART_INTERBYTE_ERR; 

      tag_drate = temp_buffer[3]; 
      scan_method = temp_buffer[4]; 
      crc_check = temp_buffer[5]; 
      automatic = NO_SCAN;                      //disable automatic function 

      forward_link_type = EM4026; 
    }
 

    else if(byte == 0x54) {                                  //Default Read 
      if (uart_read_msg_bytes != 4) return ERR_UART_INTERBYTE_ERR; 

      tag_drate = temp_buffer[3]; 
      scan_method = temp_buffer[4]; 
      crc_check = temp_buffer[5]; 
      automatic = NO_SCAN;                       //disable automatic function 

      forward_link_type = EM4026; 
    }
 
//------------------------------------------------------------------------------------- 
//EM4469 
 
    else if(byte == 0x80) {                               //Read Tag Memory 
      if (uart_read_msg_bytes != 4) return ERR_UART_INTERBYTE_ERR; 

      forward_link_type = EM4469;          
          
      read_tag_memory_word_address = temp_buffer[3];  
    }
 
    else if(byte == 0x81) {                               //Write Tag  
      if (uart_read_msg_bytes != 8) return ERR_UART_INTERBYTE_ERR; 
          
      forward_link_type = EM4469;
          
      write_tag_memory_word_address = temp_buffer[3]; 
      write_tag_memory_word_low = ((uint16_t)temp_buffer[5] << 8) + temp_buffer[4]; 
      write_tag_memory_word_hi  = ((uint16_t)temp_buffer[7] << 8) + temp_buffer[6];  

    }
 

    else if(byte == 0x82) {           //Login
      if (uart_read_msg_bytes != 7) return ERR_UART_INTERBYTE_ERR; 
        
      forward_link_type = EM4469;        
        
      write_tag_memory_login_low = temp_buffer[4]; 
      write_tag_memory_login_low = (write_tag_memory_login_low << 8) + temp_buffer[3]; 
      write_tag_memory_login_hi  = temp_buffer[6]; 
      write_tag_memory_login_hi  = (write_tag_memory_login_hi << 8) + temp_buffer[5]; 
    }
 
    else if(byte == 0x83) {                               //Disable 
      if (uart_read_msg_bytes != 3) return ERR_UART_INTERBYTE_ERR; 

      forward_link_type = EM4469; 

    }
 
    else if(byte == 0x85) {                               //Read Only Continuous mode 
      if (uart_read_msg_bytes != 3) return ERR_UART_INTERBYTE_ERR; 

      forward_link_type = EM4469; 

    }
 
    else if(byte == 0x86) {                               //Read Only Continuous mode + 
      if (uart_read_msg_bytes != 3) return ERR_UART_INTERBYTE_ERR; 
      forward_link_type = EM4469; 
    }
 
//------------------------------------------------------------------------------------- 
//EM4205/EM4305 
 
    else if(byte == 0x90) {                               //Read Tag Memory 
      if (uart_read_msg_bytes != 4) return ERR_UART_INTERBYTE_ERR; 
          
      forward_link_type = EM4305;
          
      read_tag_memory_word_address = temp_buffer[3];  

    }
 
    else if(byte == 0x91) {                               //Write Tag  
      if (uart_read_msg_bytes != 8) return ERR_UART_INTERBYTE_ERR; 

      forward_link_type = EM4305;
          
      write_tag_memory_word_address = temp_buffer[3]; 
      write_tag_memory_word_low = ((uint16_t)temp_buffer[5] << 8) + temp_buffer[4]; 
      write_tag_memory_word_hi  = ((uint16_t)temp_buffer[7] << 8) + temp_buffer[6]; 

    }
 

    else if(byte == 0x92) {                               //Login
      if (uart_read_msg_bytes != 7) return ERR_UART_INTERBYTE_ERR; 
        
      forward_link_type = EM4305;
        
      write_tag_memory_login_low = temp_buffer[4]; 
      write_tag_memory_login_low = (write_tag_memory_login_low << 8) + temp_buffer[3]; 
      write_tag_memory_login_hi  = temp_buffer[6]; 
      write_tag_memory_login_hi  = (write_tag_memory_login_hi << 8) + temp_buffer[5]; 
      read_tag_memory_word_fwr = 0;                        //dbg, see 0x95
    }
 
    else if(byte == 0x93) {                               //Disable 
      if (uart_read_msg_bytes != 3) return ERR_UART_INTERBYTE_ERR; 

      forward_link_type = EM4305; 
    }
    
    else if(byte == 0x97) {                              //EM4305 Protect
      if (uart_read_msg_bytes != 7) return ERR_UART_INTERBYTE_ERR; 

      forward_link_type = EM4305;
        
      write_tag_memory_login_low = temp_buffer[4]; 
      write_tag_memory_login_low = (write_tag_memory_login_low << 8) + temp_buffer[3]; 
      write_tag_memory_login_hi  = temp_buffer[6]; 
      write_tag_memory_login_hi  = (write_tag_memory_login_hi << 8) + temp_buffer[5]; 

    } 
    
//------------------------------------------------------------------------------------- 
/*
//EM6869 
 
    else if(byte == 0xA0) {                               // LF Wake-up

      if (uart_read_msg_bytes != 3) return ERR_UART_INTERBYTE_ERR; 

    }   
 
    else if(byte == 0xA1) {                          // Read Word

      if (uart_read_msg_bytes != 5) return ERR_UART_INTERBYTE_ERR; 

      read_txp_memory_address = ((uint16_t)temp_buffer[4] << 8) + temp_buffer[3];

    }  
 
    else if(byte == 0xA2) {                         // Write Word 
 
      if (uart_read_msg_bytes != 7) return ERR_UART_INTERBYTE_ERR; 

      write_txp_memory_address = ((uint16_t)temp_buffer[4] << 8) + temp_buffer[3];
      write_txp_memory_data = ((uint16_t)temp_buffer[6] << 8) + temp_buffer[5];
	 
    }
   
    else if(byte == 0xA3) {                         // Read Page
 
      if (uart_read_msg_bytes != 6) return ERR_UART_INTERBYTE_ERR; 

      read_txp_memory_address = ((uint16_t)temp_buffer[4] << 8) + temp_buffer[3];
      read_txp_memory_lwr = temp_buffer[5];
	 
    }
  
    else if(byte == 0xA4) {                         // Write Page (16 words max only)
 
      if (uart_read_msg_bytes <= 6) return ERR_UART_INTERBYTE_ERR; 

      write_txp_memory_address = ((uint16_t)temp_buffer[4] << 8) + temp_buffer[3];
      write_txp_memory_lwr = temp_buffer[5];
    
      // Copy the words
      ptr2 = 6;    
      for(ptr = write_txp_memory_address & 0x1F;ptr<=write_txp_memory_lwr;ptr++) {   
        EM6869Page.words[ptr] = ((uint16_t)temp_buffer[ptr2+1] << 8) + temp_buffer[ptr2];
        ptr2 = ptr2 + 2;
      }
    
    }
  
    else if(byte == 0xA5) {                         // Single Authentication, Mutual Authentication,
                                                    // Mutual Authentication ISO, Unlock UM
 
      if (uart_read_msg_bytes <= 6) return ERR_UART_INTERBYTE_ERR;

      EM6869Auth.mode = temp_buffer[3];
    
      // Copy the words
      ptr2 = 4;
      //extract RN parameter
      if( (EM6869Auth.mode & 0x80) == 0 ) {    //only Single and Mutual have RN parameter
        for(ptr = 0; ptr<((EM6869Auth.mode & 3)+1)*4; ptr++) {
          EM6869Auth.rn[ptr] = temp_buffer[ptr2++];
        }
      }
      //extract f parameter
      for(ptr = 0; ptr<(((EM6869Auth.mode >> 2) & 3)+1)*4; ptr++) {
        EM6869Auth.f[ptr] = temp_buffer[ptr2++];
      } 
    
      if( uart_read_msg_bytes != ptr2 ) return ERR_UART_INTERBYTE_ERR;
    
    }  

    else if( byte == 0xA6 || byte == 0xA8 ) {       // Unlock Key, Send Access
 
      if (uart_read_msg_bytes != 11) return ERR_UART_INTERBYTE_ERR;

      // Copy the ID and SK1/Password words
      ptr2 = 3;
      for(ptr = 0; ptr<8; ptr++) {
        EM6869Auth.rn[ptr] = temp_buffer[ptr2++];
      }
    
    }  

    else if(byte == 0xA7) {                         // GetRN1
 
      if (uart_read_msg_bytes != 4) return ERR_UART_INTERBYTE_ERR;

      EM6869Auth.mode = temp_buffer[3];
    
    }
  
    else if(byte == 0xB7) {                         // GetRN1 with pre RF-reset
 
      if (uart_read_msg_bytes != 4) return ERR_UART_INTERBYTE_ERR;

      EM6869Auth.mode = temp_buffer[3];
    
    }
 */
//------------------------------------------------------------------------------------- 
//READER CONFIG AND DEBUG FUNCTIONS AND BOOTLOADER

    else if(byte == 0x84) {                               //Forward link 
      if (uart_read_msg_bytes != 4) return ERR_UART_INTERBYTE_ERR; 

      write_tag_memory_forward_link = temp_buffer[3]; 
    }

    else if(byte == 0xF0) {                               //RF Reset

      if (uart_read_msg_bytes != 4) return ERR_UART_INTERBYTE_ERR;
      write_tag_memory_delay = temp_buffer[3];
    }

    else if(byte == 0xF1) {
      if (uart_read_msg_bytes != 7) return ERR_UART_INTERBYTE_ERR;

      write_4095_low = *(uint16_t*)&temp_buffer[3];
      write_4095_hi  = *(uint16_t*)&temp_buffer[5];
        
      if (temp_buffer[3] & 1) cbi( SHD_PORT, SHD );            //set shutdown == antenna on 
      else sbi( SHD_PORT, SHD );                               //reset shutdown == antenna off 
         
      FormatResponse_Short( byte, UART_MESSAGE_OK );
    }
    
    else if (byte == 0xF6) {                               //Send Debug Data
      if (uart_read_msg_bytes != 3)
        return ERR_UART_INTERBYTE_ERR;
      SendData( 0xF6, UART_MESSAGE_OK, debug_buffer, debug_cnt );
      
      byte = 0;
    }

    else if(byte == 0xF8) {                               //Send Capture Data
      if (uart_read_msg_bytes != 3) return ERR_UART_INTERBYTE_ERR; 
      SendCaptureData( 0xF8, UART_MESSAGE_OK ); 
      byte = 0; 
    } 
 
    else if(byte == 0xF9) {                               //Toggle debug mode
      if (uart_read_msg_bytes != 4) return ERR_UART_INTERBYTE_ERR; 
      debug_mode = temp_buffer[3]; 
      FormatResponse_Short( 0xF9, UART_MESSAGE_OK ); 
      byte = 0; 
    }
 
    else if(byte == 0xFA) {                               //Forward link pulses tuning
      if (uart_read_msg_bytes != 0xA) return ERR_UART_INTERBYTE_ERR; 
 
      fwd_tuning[0] = temp_buffer[3]; 
      fwd_tuning[1] = temp_buffer[4]; 
      fwd_tuning[2] = temp_buffer[5]; 
      fwd_tuning[3] = temp_buffer[6]; 
      fwd_tuning[4] = temp_buffer[7];                   //this parameter is used for the additional delay after synchronization
//        fwd_set_D = temp_buffer[7];                      //bytes 7-9 unused, set to 0
//        fwd_set_E = temp_buffer[8];  
//        fwd_set_F = temp_buffer[9]; 
 
      FormatResponse_Short( 0xFA, UART_MESSAGE_OK ); 
      byte = 0; 
    }
 
 
    else if(byte == 0xFB) {                               //Reader Read Configuration

      if (uart_read_msg_bytes != 3) return ERR_UART_INTERBYTE_ERR; 
 
    }
 

    else if(byte == 0xFC) {                               //Reader Write Configuration
      if (uart_read_msg_bytes != 7) return ERR_UART_INTERBYTE_ERR; 
 
      config_data_rate = temp_buffer[3] & 0x3F; 
      config_encoder = ((temp_buffer[4] & 0x03) << 2) | (temp_buffer[3] >> 6); 
      config_psk_carrier = (temp_buffer[4] >> 2) & 0x03; 
      config_delayed = (temp_buffer[4] >> 4) & 0x03; 
      config_lwr = ((temp_buffer[5] & 0x03) << 2) | (temp_buffer[4] >> 6); 
      config_raw = (temp_buffer[5] >> 6) & 1; 
      config_forward_link = (temp_buffer[5] >> 3) & 1; 
      config_write_conf = (temp_buffer[5] >> 2) & 1; 
      config_inv_sync = (temp_buffer[6] >> 6) & 1;
    }


    else if(byte == 0xFD) {                               //Reader Status 
      if (uart_read_msg_bytes != 3) return ERR_UART_INTERBYTE_ERR; 
      FormatResponse_Word( UART_MESSAGE_OK, 0xFD,  
        READER_RELEASE | ((uint16_t)(READER_DATE | ((READER_MONTH & 0x07) << 5)) << 8), 
        ((READER_MONTH >> 3) | (READER_YEAR << 1)) | ((uint16_t)(READER_FAMILY) << 8) ); 
      byte = 0; 
    }
 

    else if(byte == 0xFE) {                               //Switch Coil 
 
      if (uart_read_msg_bytes != 4) return ERR_UART_INTERBYTE_ERR; 
 
      if (temp_buffer[3] & 1) {
        cbi( SHD_PORT, SHD );                            //set shutdown == antenna on 
      } else {
        sbi( SHD_PORT, SHD );                            //reset shutdown == antenna off 
      }
        
      FormatResponse_Short( 0xFE, UART_MESSAGE_OK ); 
      byte = 0; 
    }

 
#if END_APP_SECTION != 0 
       
    else if(byte == 0xF3) {                               //Switch to bootloader 
 
      if (uart_read_msg_bytes != 3) return ERR_UART_INTERBYTE_ERR;

      sbi( SHD_PORT, SHD );                          //SHD pin = '1' (antenna off)

      uart_command = byte; 
      FormatResponse_Error( UART_MESSAGE_OK );       //do not use FormatResponse_Short since it is buffered

      //wait until UART is finished
      while(!(UART_UCSRA & (1 << UART_TXC)));
      sbi( UART_UCSRA, UART_TXC );

      uint8_t lo = (END_APP_SECTION >> 0) & 0xFF;
      uint8_t hi = (END_APP_SECTION) >> 8;

      //emulate reset
      UART_UCSRB = 0; 
      UART_UCSRA = (1<<UART_TXC);                   //clear TXC, U2X and MPCM flags

      wdt_disable();
      TIMSK = 0;
      ETIMSK = 0;
      EIMSK = 0;
      cli();                                        // General Interrupt Disable

      SetLEDOff;

      asm(
        " mov r30, %0\n"
        " mov r31, %1\n"
        " ijmp"
        : : "r" (lo), "r" (hi)
      ); 
    }
#endif 
 
//------------------------------------------------------------------------------- 
//DEFAULT
 
    else {                                           //Unknown command 
      return ERR_UART_UNKNOWN_CMD; 
    } 
 
    //command was parsed successfully 
 
    uart_command = byte; 
    return UART_MESSAGE_OK; 
  } else {   //bad checksum 
    return ERR_UART_BAD_CRC; 
  } 
} 
 
 
// ================================================================== 
// Response and error emmision 41xx 
// ================================================================== 



// ==================================================================
// send response 

void SendResponse(uint8_t ack) {

  while ( (UART_UCSRB & (1<<UDRIE)) != 0 )            //stop message emmision until pending UART tranfer finishes
    {}

  uart_out_end = uart_out_write;
  sbi(UART_UCSRB, UDRIE);
}

 
//------------------------------------------------------------------ 
// Send blok of bytes 
//------------------------------------------------------------------ 
 
void Block_Output (uint8_t cnt, uint8_t *data) { 
 
  while (cnt > 0) { 
    UART_UDR = *data; 
    checksum ^= *data++; 
    cnt--; 
    while (!(UART_UCSRA & UART_UDRE)) 
      {}; 
  } 
} 
 
//------------------------------------------------------------------ 
// Send one raw byte 
//------------------------------------------------------------------ 
 
void SendByte(uint8_t byte) { 
  UART_UDR = byte; 
  checksum = checksum ^ byte; 
  while (!(UART_UCSRA & UART_UDRE)) 
    {} 
} 
 
//------------------------------------------------------------------ 
// Send: 
//    
//  | STX(0x02) | check position | uart command | error value | 
// 
//  + calculation checksum 
//------------------------------------------------------------------ 
 
void SendHeader(uint8_t err, uint8_t chsum_pos) { 

  if(err!=0x00) SetLEDOn;

  SendByte(0x02); 
  SendByte(chsum_pos); 
  SendByte(uart_command);            //general error message 
  SendByte(err); 
  checksum = uart_command ^ err ^ chsum_pos; 
} 
 
 
//------------------------------------------------------------------ 
// Send: 
// 
//  .... | checksum | ETX(0x03) | 
//------------------------------------------------------------------ 
 
void SendEnd(void) { 
  SendByte(checksum); 
  SendByte(0x03); 
} 
 
 
//------------------------------------------------------------------ 
// Send error  
//------------------------------------------------------------------ 
 
void FormatResponse_Error(uint8_t err) { 

  if(err!=0x00) SetLEDOn;

  SendHeader(err, 4); 
 
  SendByte(checksum); 
  SendByte(0x03); 
} 
 
 
void FormatResponse_Short_41xx(void) { 
  SendHeader(0, 4); 
 
  SendByte(checksum); 
  SendByte(0x03); 
} 
 
 
//------------------------------------------------------------------ 
//------------------------------------------------------------------ 
 
void FormatResponse_Word_4150(uint8_t read_memory, uint16_t word_low, uint16_t word_hi) { 
 
  SendByte(0x02); 
  checksum=0; 
  SendByte(0x09); 
 
  if(uart_command==0x33) 
   SendByte(0x33); 
  else if(uart_command==0x35) 
   SendByte(0x35); 
 
  SendByte(0x00); 
  SendByte(read_memory); 
  SendByte((uint8_t)word_low); 
  SendByte(word_low>>8); 
  SendByte((uint8_t)word_hi); 
  SendByte(word_hi>>8); 
 
  SendByte(checksum); 
  SendByte(0x03); 
 
} 
 
 
// ================================================================== 
// Response and error emmision 4x69 
// ================================================================== 
 
//------------------------------------------------------------------ 
//------------------------------------------------------------------ 
 
void EmmitError(uint8_t err) { 
 
  if(err!=0x00) SetLEDOn;

  SendByte(0x02); 
  SendByte(0x04); 
  SendByte(0);             //general error message 
  SendByte(err);  
  checksum = err ^ 0x04; 
  SendByte(checksum); 
  SendByte(0x03); 
 
} 
 
//------------------------------------------------------------------ 
//------------------------------------------------------------------ 
 
void FormatResponse_AddrWord(uint8_t ack, uint8_t cmd, uint8_t address, uint16_t word_low, uint16_t word_hi) { 
 
  if(ack!=0x00) SetLEDOn;

  SendByte(0x02); 
  SendByte(0x09); 
  SendByte(cmd); 
  SendByte(ack); 
  SendByte(address); 
  SendByte((uint8_t)word_low); 
  SendByte(word_low>>8); 
  SendByte((uint8_t)word_hi); 
  SendByte(word_hi>>8); 
 
  checksum = 0x09 ^ cmd ^ ack ^ address ^ (word_low & 0xFF) ^ 
                         (word_low >> 8) ^ (word_hi & 0xFF) ^ 
                         (word_hi >> 8) ; 
  SendByte(checksum); 
  SendByte(0x03); 
 
 } 
 
//------------------------------------------------------------------ 
//------------------------------------------------------------------ 
 
void FormatResponse_Short(uint8_t cmd, uint8_t ack) { 

  if(ack!=0x00) SetLEDOn;

  IncWriteOut(0x02);
  IncWriteOut(0x04);
  IncWriteOut(cmd);
  IncWriteOut(ack);
  IncWriteOut(0x04 ^ cmd ^ ack);  
  IncWriteOut(0x03);
  SendResponse( 0 );

} 
 
//------------------------------------------------------------------ 
//------------------------------------------------------------------ 
 

void FormatResponse_Word_4102(uint8_t cmd, uint8_t addr, uint16_t word_low, uint16_t word_hi) {

  SendByte(0x02); 
  SendByte(0x09); 
  SendByte(cmd); 
  SendByte(UART_MESSAGE_OK);
  SendByte(addr);
  SendByte(word_hi>>8);
  SendByte((uint8_t)word_hi);
  SendByte(word_low>>8);
  SendByte((uint8_t)word_low);

  checksum = 0x09 ^ cmd ^ UART_MESSAGE_OK ^ addr ^
                         (word_low & 0xFF) ^ (word_low >> 8) ^
                         (word_hi & 0xFF) ^ (word_hi >> 8);


  SendByte(checksum); 
  SendByte(0x03); 
}


void FormatResponse_Word(uint8_t ack, uint8_t cmd, uint16_t word_low, uint16_t word_hi) { 
 
  if(ack!=0x00) SetLEDOn;

  SendByte(0x02); 
  SendByte(0x08); 
  SendByte(cmd); 
  SendByte(ack); 
  SendByte((uint8_t)word_low); 
  SendByte(word_low>>8); 
  SendByte((uint8_t)word_hi); 
  SendByte(word_hi>>8); 
  checksum = 0x08 ^ cmd ^ ack ^ 
                         (word_low & 0xFF) ^ (word_low >> 8) ^ 
                         (word_hi & 0xFF) ^ (word_hi >> 8) ; 
  SendByte(checksum); 
  SendByte(0x03); 
 
 } 
 
//------------------------------------------------------------------ 
//------------------------------------------------------------------ 
 
void FormatResponse_Status(uint8_t ack) { 
 
  if(ack!=0x00) SetLEDOn;

  SendByte(0x02); 
  SendByte(0x08); 
  SendByte(0xFD); 
  SendByte(ack); 
  SendByte(READER_RELEASE); 
  SendByte(READER_DATE | ((READER_MONTH & 0x07) << 5)); 
  SendByte((READER_MONTH >> 3) | (READER_YEAR << 1)); 
  SendByte(READER_FAMILY);   //Read Only firmware 
  checksum = 0x08 ^ 0xFD ^ ack ^ READER_RELEASE ^ (READER_DATE | ((READER_MONTH & 0x07) << 5)) ^ ((READER_MONTH >> 3) | (READER_YEAR << 1)) ^ READER_FAMILY; 
  SendByte(checksum); 
  SendByte(0x03); 
} 
 
//------------------------------------------------------------------ 
//------------------------------------------------------------------ 
 
void FormatResponse_AutoDefaultRead(uint8_t ack, uint8_t cmd, uint8_t type, uint32_t byte, uint32_t word) { 

  uint8_t i; 

  if(ack!=0x00) SetLEDOn;

  SendByte(0x02); 
  checksum=0;
  SendByte(0x0A); 
  SendByte(cmd); 
  SendByte(ack); 
  SendByte(type); 
  SendByte(byte); 
  
  for(i=0;i<4;i++)  
   { 
     SendByte(word >> 24); 
     //checksum ^= word >> 24; 
     word <<= 8; 
   } 

  SendByte(checksum); 
  SendByte(0x03); 
} 
 
void SendCaptureData_4026(uint8_t cmd, uint8_t ack) { 
 
  if(ack!=0x00) SetLEDOn;

  SendByte(0x02); 
  SendByte(4 + 2 * capture_cnt); 
  SendByte(cmd); 
  SendByte(ack); 
  checksum = (4 + 2 * capture_cnt) ^ cmd ^ ack; 
  Block_Output (capture_cnt, (uint8_t*)capture_data); 
  Block_Output (capture_cnt, (uint8_t*)capture_valid); 
  SendByte(checksum); 
  SendByte(0x03); 
} 

void SendData(uint8_t cmd, uint8_t ack, uint8_t *data, uint8_t cnt) { 
 
  if(ack!=0x00) SetLEDOn;

  SendByte(0x02); 
  SendByte(4 + cnt); 
  SendByte(cmd); 
  SendByte(ack); 
  checksum = (4 + cnt) ^ cmd ^ ack; 
  Block_Output (cnt, data); 
  SendByte(checksum); 
  SendByte(0x03); 
} 
 
void SendDataBuffered(uint8_t cmd, uint8_t ack, uint8_t *data, uint8_t cnt) { 
 
  if(ack!=0x00) SetLEDOn;

  IncWriteOut(0x02);
  IncWriteOut(0x04 + cnt);
  IncWriteOut(cmd);
  IncWriteOut(ack);
  checksum = (4 + cnt) ^ cmd ^ ack;   
  
  while (cnt > 0) { 
    IncWriteOut( *data ); 
    checksum ^= *data++; 
    cnt--; 
  } 
  
  IncWriteOut(checksum);  
  IncWriteOut(0x03);
  SendResponse( 0 );
} 
 
void FormatResponse_Animal(void) { 
 
  SendByte(0x02); 
  SendByte(0x14); 
  SendByte(0x11); 
  SendByte(UART_MESSAGE_OK); 
  checksum = 0x14 ^ 0x11 ^ UART_MESSAGE_OK; 
  Block_Output(16,(uint8_t*)AnimalData);
  SendByte(checksum); 
  SendByte(0x03); 

} 
 
//-------------------------------------------------------------------- 
void SendCaptureData( uint8_t cmd, uint8_t ack ) {
 
  SendByte( 0x02 ); 
  SendByte( 4 + 2 * capture_cnt ); 
  SendByte( cmd ); 
  SendByte( ack ); 
 
  checksum = (4 + 2 * capture_cnt) ^ cmd ^ ack; 
  Block_Output( capture_cnt, capture_data );
  Block_Output( capture_cnt, capture_valid );
  SendByte( checksum ); 
  SendByte( 0x03 ); 
}

// ------------------------------------------------------------------------------
// ------------------------------------------------------------------------------
// ------------------------------------------------------------------------------

void StoreDbg( uint8_t data ) {

  if (debug_cnt < DBG_BUFFER_SIZE ) {
    debug_buffer[debug_cnt++] = data;
  }
}

Here's the code. I haven't attached the solution.

Amateur programmer.
Believe when I tell you that my struggle on here is real.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I've lost track of what "current code" is in this but I took the .rar in #58 and tried to build it and the fatal errors were:

Building file: .././level4.c
Invoking: AVR/GNU C Compiler : 5.4.0
"C:\Sysgcc\avr\bin\avr-gcc.exe"  -x c -funsigned-char -funsigned-bitfields -DDEBUG   -O1 -ffunction-sections -fdata-sections -fpack-struct -fshort-enu
ms -g2 -Wall -mmcu=atmega64  -c -std=gnu99 -MD -MP -MF "level4.d" -MT"level4.d" -MT"level4.o"   -o "level4.o" ".././level4.c"
In file included from .././level4.c:73:0:
.././level4.c:413:1: error: conflicting types for '__iob'
 stdout = stdin = &uart_str;
 ^
c:\sysgcc\avr\avr\include\stdio.h:407:23: note: previous declaration of '__iob' was here
 extern struct __file *__iob[];
                       ^
.././level4.c:413:16: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
 stdout = stdin = &uart_str;
                ^
In file included from .././level4.c:73:0:
.././level4.c:413:10: error: invalid initializer
 stdout = stdin = &uart_str;
          ^
make: *** [level4.o] Error 1

The first error here is:

.././level4.c:413:1: error: conflicting types for '__iob'
 stdout = stdin = &uart_str;
 ^

The :413: in that says it is line 413. As Stefan noted this is because you have:

	return c;
}

static FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
//static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
//static int uart_putchar(char c, FILE *stream);
//stdout = &uart_str;
stdout = stdin = &uart_str;
//stdin = &uart_str;

You just can't put lines to assign values to variables outside of function bodies. It would be like:

#include <stdio.h>

somevar = PINB;

int main(void) {

}

You can't have "stray lines" like this. Sure there is the "special case" of assigning an initial value at creation:

#include <stdio.h>

int somevar = 37;

int main(void) {

}

but in this case the assigned value must be known at compile time and it can only be as part of a variable declaration - so should include a type specifier.

 

As Stefan said a candidate position to do it would be:

void UARTIni(void) { 

   // UCSRA is not necessary to set up, using initial values
   // no double transmission speed, no multi-processor mode           

   UART_UCSRA = UART_UCSRA_INIT;
   UART_UCSRB  = UART_UCSRB_INIT;
                       // set up : RXCIE, UDRIE, RXEN, TXEN
                       // (interrupt enable from receiver,
                       // UART receiver enable, UART transmit enable) 

   UART_UCSRC = UART_UCSRC_INIT;
                       // asynchronous operation, 8 data bits,
                       // no parity, 1 stop bit                       

   UART_UBRRL = UART_SPEED; // baud rate - see UBRR register setting table
                       //             in AVR ATmega8 specification
                       // UBRRH - using initial values
                       // UBRRL - 25 for 38k4Bd and frequency f=16 MHz
                       // (using SINGLE transmission speed)           

  uart_in_read = uart_in_write = uart_in_end = 0;
  uart_out_read = uart_out_write = uart_out_end = 0;
  uart_state = UART_EMPTY;

  // ADD IT HERE...
  stdout = stdin = &uart_str;
}

but for that to work "uart_str" must be "known" by the time the compiler gets to this line. But as it stands you have this function at line 271 and the uart_str you want to use being defined at line 409. So that definition of uart_str:

static FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);

needs to move. A common practice is to put such variable declaration/definitions up near the top of a C file. So maybe slot it in around line 216:

/*#define fdev_setup_stream(stream, p, g, f)
do {
    (stream)->put = p;
    (stream)->get = g;
    (stream)->flags = f;
    (stream)->udata = 0;
} while(0)*/

uint8_t uart_state;                     // current state of incoming message analysis 

// ADD IT HERE ...
static FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);

However this then raises another issue. This is referring to uart_putchar() and uart_getchar() but at line 216 the compiler has not encountered those functions yet (they are defined at lines 299 and 316). So initially this would appear to be a problem. You need uart_str "early" in the file so it is before the UARTIni() function where it is now used but it is created by making reference to uart_putchar() and uart_getchar() that are now after it.

 

This is the whole point of C having declarations and definitions. For a function they look like:

int add(int a, int b); // declaration

int add(int a, int b) { // definition
    return a + b;
}

You can tell a declaration because it ends with ';' and you can tell a definition because it ends with "{ some_code }". You can put a declaration very early in a C file (or even in a .h header file that it #include's) to say "expect to see a function with this name, these parameters and this return type coming up later". In my example:

int add(int a, int b); // declaration

could be on line 5 and

int add(int a, int b) { // definition
    return a + b;
}

on line 417. In between the two I could have at line 207:

void some_function() {
    PORTB = add(37, 52);
}

and the compiler would already understand how to use add() because although it hasn't seen the full function definition yet it has seen the declaration:

int add(int a, int b); // declaration

which told it enough about the function (takes two integers, returns one) so it can be used in some_function().

 

This is exactly what you need to be able to refer to uart_putchar() and uart_getchar() "early" in the FDEV_SETUP_STREAM before the real functions have been encountered. So somewhere up near the top of the file add:

} while(0)*/

uint8_t uart_state;                     // current state of incoming message analysis 

// ADD DECLARATIONS HERE..
int	uart_putchar(char c, FILE *stream);
int	uart_getchar(FILE *stream);

// ADD IT HERE ...
static FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);

Now the FDEV_SETUP_STREAM can work because it "knows" about the two functions before they have been defined.

 

To create declarations I simply went to the point of definition of each function:

int	uart_putchar(char c, FILE *stream)
{
int	uart_getchar(FILE *stream)
{
	uint8_t c;

and simply picked up the first line (everything up to but not including '{') then pasted that in as the declaration with a ';' on the end.

Last Edited: Wed. Oct 18, 2017 - 03:44 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#define stdin(__iob[0])
#define stdout(__iob[1])
#define stderr(__iob[2])
#define 	_FDEV_SETUP_READ    __SRD
#define 	_FDEV_SETUP_WRITE   __SWR
#define 	_FDEV_SETUP_RW   (__SRD|__SWR)

//printf

#define FDEV_SETUP_STREAM(put, get, rwflag)

Where do this come from all of a sudden? It was not in your earlier code. Looks like incomplete and wrongly copied stuff from stdio.h.

Remove that!

 

uint8_t uart_str = 1;

What was your idea behind adding that line?

Remove that too!

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you for that detailed explanation. I am now getting this error:

#include <inttypes.h> 
#include <avr/interrupt.h> 
//#include <avr/signal.h> 
#include <avr/wdt.h>
#include <stdio.h> 
#include "level4.h" 
#include "level3.h" 
#include "level2.h" 
#include "level1.h" 
 
//------------------------------------------------------------------- 
// 41xx 
//------------------------------------------------------------------- 
 
uint16_t write_tag_memory_old_login_low; // low word of Login cmd 
uint16_t write_tag_memory_old_login_hi;  // hi word of Login cmd 
uint8_t checksum; 

//------------------------------------------------------------------- 
// 4026 
//------------------------------------------------------------------- 
uint8_t tag_drate;                      // expected tag data rate 
 
uint8_t scan_method;                    // scan method; 0=free, 1= , 2= 
uint8_t crc_check;                      // 0=6-bit, 1=16-bit
uint8_t switch_coil_byte; 

//------------------------------------------------------------------- 
// 6869 
//------------------------------------------------------------------- 
uint16_t read_txp_memory_address;       // Read  6869
uint8_t  read_txp_memory_lwr;           // last read word within the page
uint16_t read_txp_memory_word; 

uint16_t write_txp_memory_address;
uint8_t  write_txp_memory_lwr;          // last read word within the page
uint16_t write_txp_memory_data; 
 
//------------------------------------------------------------------- 
//global declarations 
//------------------------------------------------------------------- 
 
uint16_t write_tag_memory_delay;

uint16_t write_4095_low;                // low word of SPI configuration word
uint16_t write_4095_hi;                 // hi word of SPI configuration word



uint8_t volatile uart_command;          // command handshake value 
          // set by UART RECV 
          // reset by main() when done 
 
uint8_t read_tag_memory_word_address;   // address of Read cmd 
uint16_t read_tag_memory_word_low;      // Read Cmd content low 
uint16_t read_tag_memory_word_hi;       // Read Cmd content hi 
 
uint8_t read_tag_memory_word_fwr;       // address of Read cmd 
uint8_t read_tag_memory_word_lwr;       // address of Read cmd 
 
uint16_t write_tag_memory_word_address; // address of Write Tag cmd 
uint16_t write_tag_memory_word_low;     // low word of Write Tag cmd 
uint16_t write_tag_memory_word_hi;      // hi word of Write Tag cmd 
 
uint16_t write_tag_memory_login_low;    // low word of Login cmd 
uint16_t write_tag_memory_login_hi;     // hi word of Login cmd 
 
uint8_t write_tag_memory_forward_link;  // forward item of Forward link cmd 
 
uint16_t read_only_word_low;            // Read Only Cmd content low 
uint16_t read_only_word_hi;             // Read Only Cmd content hi 
 
uint8_t config_data_rate;               // see EM4469 spec 
uint8_t config_encoder;                 // see EM4469 spec 
uint8_t config_psk_carrier;             // see EM4469 spec 
uint8_t config_delayed;                 // see EM4469 spec 
uint8_t config_lwr;                     // see EM4469 spec 
uint8_t config_raw;                     // see EM4469 spec 
uint8_t config_forward_link;            // 0=4050 type, 1=other 
uint8_t config_write_conf;              // 1=write this configuration into the EEPROM 
uint8_t config_inv_sync;                // 1=EM4469 uplink sync inversion
 
uint8_t debug_mode=0; //what does this do and why is it initialized to zero?

uint8_t debug_cnt __attribute__ ((section(".noinit")));
uint8_t debug_buffer[DBG_BUFFER_SIZE] __attribute__ ((section(".noinit")));
 
//------------------------------------------------------------------- 
//local declarations 
//------------------------------------------------------------------- 
 
#define UART_IN_BUFFER_SIZE 48             // incoming data uart buffer reserved size /bytes?

//Assign values to the commented lines of code
 
uint8_t volatile uart_in_buffer[UART_IN_BUFFER_SIZE]; // input cyclic buffer
//uint8_t volatile uart_in_buffer[UART_IN_BUFFER_SIZE] =  "peeva999usa" ;
uint8_t volatile uart_in_read;                        // input read index 
//uint8_t volatile uart_in_read = 
uint8_t volatile uart_in_end;                         // input message read index 
//uint8_t volatile uart_in_end = 
uint8_t volatile uart_in_write;                       // input write index 
//uint8_t volatile uart_in_write = 11;
uint8_t volatile uart_in_overflow;                    // input buffer overflow 
//uint8_t volatile uart_in_overflow =
uint8_t volatile uart_in_error;                       // input buffer parity error, ... 
//uint8_t volatile uart_in_error = 
 
#define UART_OUT_BUFFER_SIZE 250           // outgoing data uart buffer reserved size //bytes?

uint8_t volatile uart_out_buffer[UART_OUT_BUFFER_SIZE];   // output buffer
//uint8_t volatile uart_out_buffer[UART_OUT_BUFFER_SIZE] = "965 000 000 396 372"
uint8_t volatile uart_out_read;                       // output read index
//uint8_t volatile uart_out_read = 
uint8_t volatile uart_out_write;                      // output write index
//uint8_t volatile uart_out_write = 19
uint8_t volatile uart_out_end;                        // last output write index 
//uint8_t volatile uart_out_end =  
uint8_t volatile uart_read_bytes;       // number of bytes to read before parsing 
//uint8_t volatile uart_read_bytes = 
uint8_t volatile uart_read_msg_bytes;   // actual number of bytes (msg position no.2) 
//uint8_t volatile uart_read_msg_bytes =  
//uint8_t uart_str = 1;

//UART_STATE - enum is errorneous in gcc+sim/debug 
#define UART_EMPTY           1          // no bytes are pending 
#define UART_READ_SIZE       2          // try to analyze incoming message size from pending bytes 
#define UART_READ_BYTES      3          // try to analyze body and ETX of message from pending bytes 
#define UART_WAIT_ERROR_SENT 4          // something is wrong within analysed pending bytes 
#define UART_VALID           5          // known message format was detected, rest is still pending 
 
//#define DEBUG_UART_BUFFER
//#define stdin(__iob[0])                                |
//#define stdout(__iob[1]                                } ARE THESE MACROS REQUIRED? 
//#define stderr(__iob[2])                               |
#define 	_FDEV_SETUP_READ    __SRD
#define 	_FDEV_SETUP_WRITE   __SWR
#define 	_FDEV_SETUP_RW   (__SRD|__SWR)

//printf

//#define FDEV_SETUP_STREAM(put, get, rwflag)
int	uart_putchar(char c, FILE *stream);
int	uart_getchar(FILE *stream); 
int uart_str = 1;                                           } ERROR IS HERE ----> FILE type??
FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);

/*#define fdev_setup_stream(stream, p, g, f)              } ----------------> DO I NEED TO RETAIN THIS?
do { 
 (stream)->put = p; 
 (stream)->get = g; 
 (stream)->flags = f; 
 (stream)->udata = 0;
} while(0)*/


uint8_t uart_state;                     // current state of incoming message analysis  
//------------------------------------------------------------------- 
//uint8_t uart_str = 1;
uint8_t ParseMessage(void);

//------------------------------------------------------------------- 
// uart byte receive interrupt 
 
SIGNAL (UART_RECV_VECTOR) 
{ 
  uart_in_buffer[uart_in_write++] = UART_UDR;              //store byte into cyclic buffer 
 
  if (uart_in_write == UART_IN_BUFFER_SIZE) 
 uart_in_write = 0; 
 
  if (uart_in_write == uart_in_read) {   
 uart_in_overflow = 1;                              //set flag in overflow 
 //block reception until some bytes are parsed out 
  } 
 
  if ((UART_UCSRA & ((1<<FE0)|(1<<DOR0)|(1<<UPE0))) != 0) {     //frame error, data overrun or parity error 
 uart_in_error = 1;                                 //set flag in error 
  }; 
 
} 


// ==================================================================
// uart byte transmission

SIGNAL (UART_TX_VECTOR)
{
  if (uart_out_end != uart_out_read) {                 //if current message is being sent
 UART_UDR = uart_out_buffer[uart_out_read++];            //send next byte
 if (uart_out_read == UART_OUT_BUFFER_SIZE)
   uart_out_read = 0;
  } else {
 cbi(UART_UCSRB, UDRIE);                                 //else stop
  }
}

// ==================================================================
// increase uart_out_write pointer by 1 
// (to consider which one has better space/speed trade-off)

void IncWriteOut(uint8_t x) {
  uart_out_buffer[uart_out_write++] = x;
  if (uart_out_write == UART_OUT_BUFFER_SIZE)
 uart_out_write = 0;
}

//------------------------------------------------------------------- 
// uart init routine 
//------------------------------------------------------------------- 
//static FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW); 
void UARTIni(void) { 
 
   // UCSRA is not necessary to set up, using initial values         
   // no double transmission speed, no multi-processor mode           
//FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
   UART_UCSRA = UART_UCSRA_INIT;
   UART_UCSRB  = UART_UCSRB_INIT; 
        // set up : RXCIE, UDRIE, RXEN, TXEN           
        // (interrupt enable from receiver,            
        // UART receiver enable, UART transmit enable) 

   UART_UCSRC = UART_UCSRC_INIT; 
        // asynchronous operation, 8 data bits,        
        // no parity, 1 stop bit                       

   UART_UBRRL = UART_SPEED; // baud rate - see UBRR register setting table 
        //             in AVR ATmega8 specification    
        // UBRRH - using initial values                
        // UBRRL - 25 for 38k4Bd and frequency f=16 MHz 
        // (using SINGLE transmission speed)           

  uart_in_read = uart_in_write = uart_in_end = 0;
  uart_out_read = uart_out_write = uart_out_end = 0; 
  uart_state = UART_EMPTY;
  //int uart_str = 1;
  stdout = stdin = &uart_str;
} 

//printf/scanf

int	uart_putchar(char c, FILE *stream)
{

 if (c == '\a')			//'\a'
 {
  fputs("*ring*\n", stderr);
  return 0;
 }

 if (c == '\n')			//
 uart_putchar('\r', stream);
 loop_until_bit_is_set(UART_UCSRA, UART_UDRE);
 UART_UDR = c;

 return 0;
}

int	uart_getchar(FILE *stream)
{
 uint8_t c;
 char *cp, *cp2;
 static char b[UART_IN_BUFFER_SIZE];
 static char *rxp;

 if (rxp == 0)
 for (cp = b;;)
 {
  loop_until_bit_is_set(UART_UCSRA, RXC);
  if (UART_UCSRA & _BV(FE))
  return _FDEV_EOF;
  if (UART_UCSRA & _BV(DOR))
  return _FDEV_ERR;
  c = UART_UDR;
  //behaviour similar to Unix stty ICRNL
  if (c == '\r')
  c = '\n';
  if (c == '\n')
  {
   *cp = c;
   uart_putchar(c, stream);
   rxp = b;
   break;
  }
  else if (c == '\t')
  c = ' ';

  if ((c >= (uint8_t)' ' && c <= (uint8_t)'\x7e') ||
  c >= (uint8_t)'\xa0')
  {
   if (cp == b + UART_IN_BUFFER_SIZE - 1)
   uart_putchar('\a', stream);
   else
   {
    *cp++ = c;
    uart_putchar(c, stream);
   }
   continue;
  }

  switch (c)
  {
   case 'c' & 0x1f:
   return -1;

   case '\b':
   case '\x7f':
   if (cp > b)
   {
    uart_putchar('\b', stream);
    uart_putchar(' ', stream);
    uart_putchar('\b', stream);
    cp--;
   }
   break;

   case 'r' & 0x1f:
   uart_putchar('\r', stream);
   for (cp2 = b; cp2 < cp; cp2++)
   uart_putchar(*cp2, stream);
   break;

   case 'u' & 0x1f:
   while (cp > b)
   {
    uart_putchar('\b', stream);
    uart_putchar(' ', stream);
    uart_putchar('\b', stream);
    cp--;
   }
   break;

   case 'w' & 0x1f:
   while (cp > b && cp[-1] != ' ')
   {
    uart_putchar('\b', stream);
    uart_putchar(' ', stream);
    uart_putchar('\b', stream);
    cp--;
   }
   break;
  }
 }

 c = *rxp++;
 if (c == '\n')
 rxp = 0;

 return c;
}

//static FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
//int uart_str = 1;
//static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
//static int uart_putchar(char c, FILE *stream);
//stdout = &uart_str;
//stdout = stdin = &uart_str;
//stdin = &uart_str;

Build output:

 

------ Build started: Project: EMTestRun, Configuration: Debug AVR ------
Build started.
Project "EMTestRun.cproj" (default targets):
Target "PreBuildEvent" skipped, due to false condition; ('$(PreBuildEvent)'!='') was evaluated as (''!='').
Target "CoreBuild" in file "C:\Program Files (x86)\Atmel\Studio\7.0\Vs\Compiler.targets" from project "E:\EMTestRunDev\EMTestRun\EMTestRun\EMTestRun.cproj" (target "Build" depends on it):
    Task "RunCompilerTask"
        Shell Utils Path C:\Program Files (x86)\Atmel\Studio\7.0\shellUtils
        C:\Program Files (x86)\Atmel\Studio\7.0\shellUtils\make.exe all --jobs 4 --output-sync 
        Building file: .././level4.c
        Invoking: AVR/GNU C Compiler : 5.4.0
E:\EMTestRunDev\EMTestRun\EMTestRun\level4.c(214,6): error: conflicting types for 'uart_str'
         FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
              ^
E:\EMTestRunDev\EMTestRun\EMTestRun\level4.c(213,5): info: previous definition of 'uart_str' was here
         int uart_str = 1;
             ^
        make: *** [level4.o] Error 1
        "C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-gcc.exe"  -x c -funsigned-char -funsigned-bitfields -DDEBUG  -I"C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.2.132\include"  -O1 -ffunction-sections -fdata-sections -fpack-struct -fshort-enums -g2 -Wall -mmcu=atmega64 -B "C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.2.132\gcc\dev\atmega64" -c -std=gnu99 -MD -MP -MF "level4.d" -MT"level4.d" -MT"level4.o"   -o "level4.o" ".././level4.c" 
E:\EMTestRunDev\EMTestRun\EMTestRun\Debug\Makefile(149,1): error: recipe for target 'level4.o' failed
    Done executing task "RunCompilerTask" -- FAILED.
Done building target "CoreBuild" in project "EMTestRun.cproj" -- FAILED.
Done building project "EMTestRun.cproj" -- FAILED.

Build FAILED.
========== Build: 0 succeeded or up-to-date, 1 failed, 0 skipped ==========

Amateur programmer.
Believe when I tell you that my struggle on here is real.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
int uart_str = 1;                                           } ERROR IS HERE ----> FILE type??
FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);

That is a C program with two different variables called "uart_str". What makes you think you can do something like this in C? You wouldn't write:

int someVar;
char someVar;

would you?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No. But I was asked to assign some value to uart_str before using it in the function.

Update: I removed it and the build was successful. Now does that mean I can use printf and scanf statements in the code?

 

THANKS A LOT!!!! 

Amateur programmer.
Believe when I tell you that my struggle on here is real.

Last Edited: Wed. Oct 18, 2017 - 04:13 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What are you talking about?

 

The FDEV_SETUP macro is used in the creation of a variable (a complex struct) to assign a new value to it. No other assignment is required.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I probably misinterpreted your post #66.

Amateur programmer.
Believe when I tell you that my struggle on here is real.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Why are you defining uart_str twice, first as an int and then as a FILE?!?

int uart_str = 1;                                           } ERROR IS HERE ----> FILE type??
FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);

Don't do that.

 

The error  message is quite clear, and you should be able to understand it yourself:

E:\EMTestRunDev\EMTestRun\EMTestRun\level4.c(214,6): error: conflicting types for 'uart_str'
         FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
              ^
E:\EMTestRunDev\EMTestRun\EMTestRun\level4.c(213,5): info: previous definition of 'uart_str' was here
         int uart_str = 1;
             ^

First "conflicting types for 'uart_str'" should be quite clear, and unambiguous, right? It is set at the line

FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);

This, the second definition of uart_str, is where the conflict arises. Next, the compiler is nice enough to tell you where the first definition was:

int uart_str = 1;

Isn't the situation quite clear? In fact, crystal-clear?

 


 

1. It is totally obvious that you are not good friends with C programming yet. Maybe take a step back and learn the basics first? Preferably using and targeting a PC.

 

2. I know you won't follow the advice under 1. above so here's another idea: You have stated earlier that you have software engineers in the project. Even if they are not specifically C programmers they will surely be skilled enough to be able to help you sort out things like the above. 

"He used to carry his guitar in a gunny sack, or sit beneath the tree by the railroad track. Oh the engineers would see him sitting in the shade, Strumming with the rhythm that the drivers made. People passing by, they would stop and say, "Oh, my, what that little country boy could play!" [Chuck Berry]

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I will do both as advised. Thanks Johan.

Amateur programmer.
Believe when I tell you that my struggle on here is real.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have a doubt here. INT stores only a fraction of the range of numbers on an 8 bit system. I would like to use either a uint8_t or uint16_t in these lines:

uint16_t uart_putchar(char c, FILE *stream);
uint16_t uart_getchar(FILE *stream); 

But I am getting a warning at this line:

 

FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);

Build Output:

 

------ Build started: Project: EMTestRun, Configuration: Debug AVR ------
Build started.
Project "EMTestRun.cproj" (default targets):
Target "PreBuildEvent" skipped, due to false condition; ('$(PreBuildEvent)'!='') was evaluated as (''!='').
Target "CoreBuild" in file "C:\Program Files (x86)\Atmel\Studio\7.0\Vs\Compiler.targets" from project "E:\EMTestRunDev\EMTestRun\EMTestRun\EMTestRun.cproj" (target "Build" depends on it):
 Task "RunCompilerTask"
  Shell Utils Path C:\Program Files (x86)\Atmel\Studio\7.0\shellUtils
  C:\Program Files (x86)\Atmel\Studio\7.0\shellUtils\make.exe all --jobs 4 --output-sync
  Building file: .././level4.c
  Invoking: AVR/GNU C Compiler : 5.4.0
  In file included from .././level4.c:73:0:
E:\EMTestRunDev\EMTestRun\EMTestRun\level4.c(208,35): warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]
   FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
                                     ^
E:\EMTestRunDev\EMTestRun\EMTestRun\level4.c(208,35): info: (near initialization for 'uart_str.put')
E:\EMTestRunDev\EMTestRun\EMTestRun\level4.c(208,49): warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]
   FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
                                                   ^
E:\EMTestRunDev\EMTestRun\EMTestRun\level4.c(208,49): info: (near initialization for 'uart_str.get')
  "C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-gcc.exe"  -x c -funsigned-char -funsigned-bitfields -DDEBUG  -I"C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.2.132\include"  -O1 -ffunction-sections -fdata-sections -fpack-struct -fshort-enums -g2 -Wall -mmcu=atmega64 -B "C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.2.132\gcc\dev\atmega64" -c -std=gnu99 -MD -MP -MF "level4.d" -MT"level4.d" -MT"level4.o"   -o "level4.o" ".././level4.c"
  Finished building: .././level4.c
  Building target: EMTestRun.elf
  Invoking: AVR/GNU Linker : 5.4.0
  "C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-gcc.exe" -o EMTestRun.elf  boot_ld.o level1_4026.o level1_41xx.o level2.o level2_4026.o level2_41xx.o level3.o level3_4026.o level3_41xx.o level4.o main.o   -Wl,-Map="EMTestRun.map" -Wl,--start-group -Wl,-lm  -Wl,--end-group -Wl,--gc-sections -mmcu=atmega64 -B "C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.2.132\gcc\dev\atmega64"
  Finished building target: EMTestRun.elf
  "C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-objcopy.exe" -O ihex -R .eeprom -R .fuse -R .lock -R .signature -R .user_signatures  "EMTestRun.elf" "EMTestRun.hex"
  "C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-objcopy.exe" -j .eeprom  --set-section-flags=.eeprom=alloc,load --change-section-lma .eeprom=0  --no-change-warnings -O ihex "EMTestRun.elf" "EMTestRun.eep" || exit 0
  "C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-objdump.exe" -h -S "EMTestRun.elf" > "EMTestRun.lss"
  "C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-objcopy.exe" -O srec -R .eeprom -R .fuse -R .lock -R .signature -R .user_signatures "EMTestRun.elf" "EMTestRun.srec"
  "C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-size.exe" "EMTestRun.elf"
     text	   data	    bss	    dec	    hex	filename
    19358	     35	   1697	  21090	   5262	EMTestRun.elf
 Done executing task "RunCompilerTask".
 Task "RunOutputFileVerifyTask"
    Program Memory Usage 	:	19386 bytes   29.6 % Full
    Data Memory Usage 		:	1725 bytes   42.1 % Full
    EEPROM Memory Usage 	:	7 bytes   0.3 % Full
 Done executing task "RunOutputFileVerifyTask".
Done building target "CoreBuild" in project "EMTestRun.cproj".
Target "PostBuildEvent" skipped, due to false condition; ('$(PostBuildEvent)' != '') was evaluated as ('' != '').
Target "Build" in file "C:\Program Files (x86)\Atmel\Studio\7.0\Vs\Avr.common.targets" from project "E:\EMTestRunDev\EMTestRun\EMTestRun\EMTestRun.cproj" (entry point):
Done building target "Build" in project "EMTestRun.cproj".
Done building project "EMTestRun.cproj".

Build succeeded.
========== Build: 1 succeeded or up-to-date, 0 failed, 0 skipped ==========

Warning:

 

initialization from incompatible pointer type [-Wincompatible-pointer-types]    

 

Amateur programmer.
Believe when I tell you that my struggle on here is real.

Last Edited: Wed. Oct 18, 2017 - 05:27 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

azimmali wrote:
INT stores only a fraction of the range of numbers on an 8 bit system.
???

int (on AVRs) is 16-bit (signed)

 

David (aka frog_jr)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Amateur programmer.
Believe when I tell you that my struggle on here is real.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Have you looked at the definitions of putchar/getchar in stdio.h as an example?

The return value on error is EOF (-1), so using an unsigned type cannot return the correct value...
 

David (aka frog_jr)

Pages