Terminal for printing outputs?

Go To Last Post
86 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hey people,

I would like to see what a program is printing on a terminal. Does AS7 have any inbuilt terminals? If yes where? Otherwise, what are the options I have?

 

 

Azim

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

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

azimmali wrote:
what are the options I have

TeraTerm

PuTTY

Bray

RealTerm

etc, etc, ...

 

EDIT

 

This is a very frequently recurring question; eg, http://www.avrfreaks.net/comment...

Last Edited: Thu. Oct 12, 2017 - 02:44 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have TeraTerm installed. Can I print data without the hardware? I need to see what certain line of code are printing. If yes, how?

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

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

You mean you're running your code in a simulator, and want to see its UART output ?

 

EDIT

 

Is that specifically the Atmel Studio simulator?

 

Last Edited: Thu. Oct 12, 2017 - 02:46 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So, I have this code that uses a circular buffer. It receives incoming bytes in these UART buffers. I want to know what these incoming bytes are. In my previous post I had inquired on how I can assign dummy values to those buffers but in vain. The project does a dummy capture. I am frustrated with this now. I just dont know what to do and whom to approach for such (maybe) trivial questions.

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

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

I do not know much about the ATMEL simulator. I was debugging the project to find out the flow of the program. But as usual its stuck in a while loop.

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

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

So are you running your code in the simulator or not?

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

Why not use the DEBUGGER to do this - stop at any moment and you can actually examine what is in the buffers in actual AVR memory - no need to "print" the contents

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

No..:/

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

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

AVR memory? whats that? I am using the debugger. So this AVR memory is where I can see what's in those buffers?

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

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

azimmali wrote:
AVR memory? whats that?

Are you serious?!

 

The AVR has 3 types of memory:

  1. RAM - where your variables, stack, etc, go;
  2. Flash - where your code goes;
  3. EEPROM.

 

EDIT

 

 

EDIT 2

 

typo

 

#AVRMemories #AVRArchitecture

 

Last Edited: Mon. Oct 16, 2017 - 08:26 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Oh no. I am sorry. Lol. I know that. I thought AVR memory is a tool on AS7 blush. So how do I examine whats in the AVR Memory? ref: clawson

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

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

azimmali wrote:
how do I examine whats in the AVR Memory? 

That's one of the fundamental tools of the AS7 debugger - in fact, of any debugger.

 

I think you need to spend some time reviewing the documentation and available tutorials for Atmel Studio ...

 

There's a whole load of resources - including videos - linked from the Atmel Studio pages on the Atmel & Microchip websites.

 

EDIT

 

To get you started: https://www.google.co.uk/search?q=atmel+studio+inspect+memory

Last Edited: Thu. Oct 12, 2017 - 03:53 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok. Done.

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

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

AS7 has a terminal. That's what the documentation says. Under View. But I dont see any on my AS7 here. Ugh!!!

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

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

Read #8 again.

 

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

There used to be a 'terminal' program in AS, but it was never any good!

You can only use a terminal program(see list above) with real hardware, not with a simulator.

It will require some interface hardware as well, most likely a USB to serial TTL cable like this: http://www.ebay.com/itm/USB-To-R...

Note this has TTL outputs, not RS-232 outputs, you also need one in which the TTL signal levels match your VCC level, either 3.3v or 5.0v, the cables come in both versions, use the correct one for your project.

These will create a COM port when plugged into a PC, set your terminal program to use that COM port, connect the TX,RX and GND pins only to your AVR, like this:

PC - AVR

TX - RX

RX - TX

gnd-gnd

 

Now you should have serial comms between your PC and your AVR.   NOTE: the AVR needs a xtal in order to have an accurate and stable clock for async comms to work well!

 

Jim

 

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

ki0bk wrote:
You can only use a terminal program(see list above) with real hardware, not with a simulator.

No, that's not necessarily true.

 

eg, Keil has a simulator[1] which can send the simulated UART output to a COM port. So you could loop that back to a terminal on another real COM port, or use something like com0com to do it "virtually"

 

http://com0com.sourceforge.net/

 

However, given chips with on-board debug, I found it just as easy to use a real devboard - and that was over ten years ago.

 

Nowadays, given devboards with the debugger and virtual COM port built-in, it would be even more so.

 

 

[1] Of course, not an AVR simulator - but the principle applies.

 

EDIT

 

But, as pointed out in #8, a serial port and terminal is not the way to do what the OP is asking anyhow.

Last Edited: Thu. Oct 12, 2017 - 09:45 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

In case it's not clear what I am suggesting is that in either hardware debugging or simulation you use:

 

http://www.atmel.com/webdoc/GUID...

 

 

If you direct this to the location of "Rx_Buffer" or whatever your UART buffer is called you can immediately see the characters/bytes in the buffer.

 

You do not need to employ a UART to send them "out" of the AVR or a terminal program to take them "in" and display them.

 

The memory view in AS7 will let you "see" the buffer contents directly.

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

You  also have facilities to view your variables by name: http://www.atmel.com/webdoc/GUID...

 

EDIT

 

eg,

 

Last Edited: Fri. Oct 13, 2017 - 09:07 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I found this yesterday. Thanks Neil!  :) I will keep you posted.

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

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

I guess it's horses for courses but for "buffers" I think you are going to find a memory window rather than a variable watch is more useful for this kind of thing.

 

(well I do).

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

I will keep track of both.

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

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

clawson wrote:
for "buffers" I think you are going to find a memory window rather than a variable watch is more useful for this kind of thing.

Probably a combination of both - a memory view to see the content, and watches for the read & write indexes.

 

A watch can also be useful when you're interested in a particular buffer entry - eg, my_buffer[87] - rather than having to manually count through a memory view.

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

I want to use a printf() function for serial printing. I added the stdio.h header. I was trying to define a stream based on a reference online. But its' showing me this error.

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

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

azimmali wrote:
I want to use a printf() function for serial printing

Why??

 

After it's been amply demonstrated that you really do not need to do that!!

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

Does this AVR have two UARTs then?
.
As to the error, do you know what a "function declaration" is in C and why we need/use them?

Last Edited: Sat. Oct 14, 2017 - 01:36 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

or, in fact, what any declaration is?

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

Here's the UART definitions from the header

 

//UART define
//------------------------------------------------------------------- 

//#define UART_DBG  //debug UART0 

#ifdef UART_DBG  

  #define UART_RECV_VECTOR SIG_UART0_RECV
  #define UART_TX_VECTOR   SIG_UART0_DATA
  #define UART_UDR         UDR0
  #define UART_UDRE        (1<<UDRE0)
  #define UART_UDRIE       (1<<UDRIE0)
  #define UART_UCSRA       UCSR0A
  #define UART_UCSRB       UCSR0B
  #define UART_UCSRC       UCSR0C
  #define UART_UBRRL       UBRR0L
  #define UART_TXC         TXC0
  #define UART_RXC         RXC0
  #define UART_TXEN        TXEN0
  #define UART_RXEN        RXEN0
  #define UART_ERRORS      ((1<<FE0)|(1<<DOR0)|(1<<UPE0))
  #define UART_UCSRB_INIT  ((1<<RXCIE0) | (0<<UDRIE0) | (1<<RXEN0) | (1<<TXEN0))
  #define UART_UCSRC_INIT  ((1<<UCSZ01) | (1<<UCSZ00)) 

#else 

  #define UART_RECV_VECTOR USART1_RX_vect
  #define UART_TX_VECTOR   USART1_UDRE_vect
  #define UART_UDR         UDR1
  #define UART_UDRE        (1<<UDRE1)
  #define UART_UCSRA       UCSR1A
  #define UART_UCSRB       UCSR1B
  #define UART_UCSRC       UCSR1C
  #define UART_UBRRL       UBRR1L
  #define UART_SPEED       51
  #define UART_ERRORS      ((1<<FE1)|(1<<DOR1)|(1<<UPE1))
  #define UART_UCSRA_INIT  ((1<<TXC1) | (1<<U2X1))
  #define UART_UCSRB_INIT  ((1<<RXCIE1) | (0<<UDRIE1) | (1<<RXEN1) | (1<<TXEN1))
  #define BL_UCSRB_INIT    ((1<<RXEN1) | (1<<TXEN1))
  #define UART_UCSRC_INIT  ((1<<UCSZ11) | (1<<UCSZ10))
  #define UART_TXC         TXC1
  #define UART_RXC         RXC1  

#endif 

I could only locate UART definitions defined under the else condition in the project.

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

Last Edited: Mon. Oct 16, 2017 - 03:47 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It tells the compiler what the function parameters are, what return type, etc.,

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

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

I wanted to play with code. I am able to see whats going in the memory.

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

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

So the firmware in the embedded board starts functioning based on commands from an application software (AS). So, the AS issues an opcode 02 03 11 12 03. I want to make the board independent of the application software. Forget the board, atleast the firmware. For now the firmware is doing a dummy capture. I would like the firmware to do a real time operation and be able to identify multiple modulation schemes and process that data accordingly. I tried inputting the opcode on a terminal like termite and its displaying some random values; all numbers.

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

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

In your screenshot in your post #25 you try to use usart_putchar_printf() (in the macro call to FDEV_SETUP_STREAM) before you actually define it.

 

Swap those two. First the definition, then do the macro call.

 

Any function must be "seen", either by a definition (i.e. complete implementation) or by its declaration (its definition or just its prototype) before it is being used (referenced). This is absolutely basic C knowledge. IIRC we've advised you in the past to get a C textbook and read. That advice still stands.

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"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

Ok. I changed that.

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(UCSRA, UDRE);
  UDR = c;
  return 0;
 }

I have these two errors. I would shamelessly accept that my exposure to C in real world is less (I would say programming in general). This project has got me hooked on it now. Yes, I also have a text book on C. but Embedded C is completely new to me. 

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

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

 

 

 

Update:

I changed the UCSRA and UDRE to how they are defined to in headers.

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

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

RE: post #34

 

7 of the warnings there say "This header file is obsolete" talking about your use of <avr/signal.h>. The message even says use <avr/interrupt.h> instead. Why on earth are you ignoring things like this?!?

 

As for UCSRA being undeclared. It should be (a) if you use the <avr/io.h> as you should and (b) you are actually building for a model of AVR that has a register called UCSRA

 

This is a complete guess but I think you probably do include the io.h header so presumably the AVR you are using is one that does not have UCSRA. Is it a "modern" AVR like mega48/88/168/328 or perhaps mega164/324/644/1284? Those models have UCSR0A not UCSRA for the first (sometimes only) UART. The same is true if UDR. The register is probably UDR0 in fact.

 

In #29 you posted some code (without any explanation) that mentions things like UCSR0A, UCSR1A, UDR0 and UDR1. That seems to confirm that you may be building for a "modern" (like last 10 years) model of AVR. The code you have found using UCSRA/UDR is probably from a 10+ year old AVR

 

Just start by telling us which model of AVR it is you are trying to use.
 

Last Edited: Mon. Oct 16, 2017 - 05:03 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

clawson wrote:
The message even says use <avr/interrupt.h> instead. Why on earth are you ignoring things like this?!?

+1 .

 

Never ignore warnings. Just because they are warnings (rather than errors) does not mean they are always benign.

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"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: Mon. Oct 16, 2017 - 05:24 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Are you referring to the MCU? If yes, atmega64, 8 bit. The avr/interrupt.h warning. I have replaced the avr/signal.h with the avr/interrupt.h in all the program files. It still shows this error SOMETIMES,  which is kind of weird. I have been noting this for sometime : whenever I add a new piece of code to the program, I get that error.

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

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

I changed UDR to UART_UDR and now I have one warning - mystdout defined but not used <-Wunused-variable>.

 

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

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

Never ignore warnings

 

That's a warning that most programmers always ignore.

When in the dark remember-the future looks brighter than ever.

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

Try actually reading the data sheet. The datasheet for atmega 64 tells you that it has two UART so it has UCSR0A and UCSR1A. Equally it has UDR0 and UDR1. 

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

I was going through the avr library manual and came across #defines in uppercase and lowercase like this one below. Can anyone tell me what's the difference?

 

#define fdev_setup_stream(stream, put, get, rwflag)

 

#define FDEV_SETUP_STREAM(put, get, rwflag)

 

 

 

Thanks

 

 

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

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

two things:

 

1) you do know there is a user manual?

 

http://www.nongnu.org/avr-libc/u...

 

2) so what you are looking at has to be read in context:

#if defined(__DOXYGEN__)
/**
   \brief Setup a user-supplied buffer as an stdio stream

   This macro takes a user-supplied buffer \c stream, and sets it up
   as a stream that is valid for stdio operations, similar to one that
   has been obtained dynamically from fdevopen(). The buffer to setup
   must be of type FILE.

   The arguments \c put and \c get are identical to those that need to
   be passed to fdevopen().

   The \c rwflag argument can take one of the values _FDEV_SETUP_READ,
   _FDEV_SETUP_WRITE, or _FDEV_SETUP_RW, for read, write, or read/write
   intent, respectively.

   \note No assignments to the standard streams will be performed by
   fdev_setup_stream().  If standard streams are to be used, these
   need to be assigned by the user.  See also under
   \ref stdio_without_malloc "Running stdio without malloc()".
 */
#define fdev_setup_stream(stream, put, get, rwflag)
#else  /* !DOXYGEN */
#define fdev_setup_stream(stream, p, g, f) \
	do { \
		(stream)->put = p; \
		(stream)->get = g; \
		(stream)->flags = f; \
		(stream)->udata = 0; \
	} while(0)
#endif /* DOXYGEN */

DOXYGEN is the program that creates the documentation in (1) from this header file. What the above section of code says is that "when parsing this header, if generating the documentation then use the first part, but for building your "normal" AVR programs use the second part". So the #define you referred to is simply a dummy one for documentation purposes. The true definition that is used is:

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

So you create a FILE stream object then use this function to populate the function pointers and flags within it.

 

As to the difference between the upper case and lower case versions. While there are subtle differences the actual intention is that FDEV_SETUP_STREAM is used in C and fdev_setup_stream() in C++. The latter has to exist because the former uses a feature of C (Named initialisers) that is not in C++

 

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

Thanks clawson. :)

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

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

One more question: Should I be making these changes in the main.c or UART source file?

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

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

It's not clear to me what "changes" you are talking about.

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

Changes for a user supplied stdio stream.

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

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

I would be tempted to keep that with the UART stuff. Maybe just make it part of uart_init() that stdin/stdout are connected to the local uart_getchar() and uart_putchar() routines so you main can look like:

#include "uart.h"

int main(void) {
    uart_init(); // maybe pass a baud rate here?
    
    printf("hello");
}

That keeps main() very "clean" so there's no knowledge of the connection to stdout here.

 

OTOH you might want to retain control of whether stdout connects to the UART output channel. Some libraries choose to do it more like:

#include "uart.h"

int main(void) {
    uart_init(); // maybe pass a baud rate here?
    uart_connect_to_stdio();
    
    printf("hello");
}

so that the connection is more obvious and also so it can optionally be removed.

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

So yesterday, I put a main in the main.c file and the obvious happened; the program didn't compile. I am guessing defining the function for serial printing could be done in the uart source file. The init_uart() you are talking about is defined as UARTIni() under level4.c source file with its declarations and macros defined in level4.h. I am guessing I have create that function for serial printing in UART source file.

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

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

You do know about things like extern and function declarations etc?

 

Ultimately the code in a single C file can be split in any way you choose. But often it takes a little effort to get "cross links" between files working. For data you will need "extern" declarations so the non-implementing file can "see" the data in the other. For functions you do it by a split of function declaration and definition with the declarations usually being placed in a shared header file. Were you doing this?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
  • 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.

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"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).

 

 

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"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.

 

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"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.

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"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. 

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"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)