printf() with simulavr?

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

I'm using simulavr 0.1.2.6. Is there a way to call printf() (or any other write to stdout) in my AVR program, and have the resulting strings show up in my terminal?

I think it's possible to get simulavr to print the output of the UART, but I don't know what options to specify. There seems to be almost no simulavr documentation aside from the very sparse manpage.

Also, what's the difference between simulavr and simulavrxx? Which is newer? Which should I be using?

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

Pick another tool. Word is simulavr is a POS, antiquated, not maintained. See:

https://www.avrfreaks.net/index.p...

C: i = "told you so";

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

The only simulator I've seen that simulates UART output (in a *real* way,
i.e. you get gibberish if your baud rate doesn't match) is VMlab.

simulavrxx is a rewrite that is supposed to replace simulavr. It still
lacks a few features of the ancient simulavr, OTOH offers a number of
things that are implemented better/more complete.

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

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

Quote:

The only simulator I've seen that simulates UART output (in a *real* way,
i.e. you get gibberish if your baud rate doesn't match) is VMlab.

Hapsim (another Windows program) also simulates a UART but it does NOT police the baud rate. You can set it to anything you like and still see sensible output.

Persoally I don't see the point of simulating UART. It's so trivial it's almost impossible to get it wrong. When I start a new AVR app I write the handful of lines to get a UART (tx) channel working first then use that for early debug. If it helps here's one I put together just the other day:

#include 
#include 
#include 

uint8_t uart_rx_buffer[32];
volatile uint8_t rx_wr_ptr, rx_rd_ptr;

int uart_putc(char c, FILE *unused) {
	while (!(UCSR0A & (1<<UDRE)));
	UDR0 = c;
	return 0;
}

ISR(USART0_RX_vect) {
	if (UCSR0A & _BV(FE))
	  return;
	if (UCSR0A & _BV(DOR))
	  return;
	uart_rx_buffer[ rx_wr_ptr ] = UDR0;
	rx_wr_ptr = (++rx_wr_ptr) % 32;
}

int uart_getc(FILE * unused) {
	uint8_t c;
	while (rx_wr_ptr == rx_rd_ptr); // wait until character(s) in buffer
	c = uart_rx_buffer[ rx_rd_ptr ];
	rx_rd_ptr = (++rx_rd_ptr) % 32;
	return c;
}

uint8_t uart_test(void) {
	return (rx_wr_ptr != rx_rd_ptr);
}

FILE uart_str = FDEV_SETUP_STREAM(uart_putc, uart_getc, _FDEV_SETUP_RW);

void uart_init(void) {
	UBRR0L = 51; // 9600 @ 8MHz
	UCSR0B = (1<<TXEN) | (1<<RXEN) | (1<<RXCIE); 
	stdout = stdin = &uart_str;
}

int main(void) {
	uint8_t n = 0, c='*', string[20], str_ptr=0;

	string[0] = 0;
	uart_init();
	sei();

	while(1) {
		if (uart_test()) {
			c = getchar();
			if (c == '\r') {
				str_ptr = 0;
				string[0] = 0;
			}
			else if (str_ptr <= 18) {
				string[str_ptr++] = c;
				string[str_ptr] = 0;
			}
		}
		if (string[0]) {
			printf("Hello, world n=#u, s=#s\r\n", n++, string);
		}
		else {
			printf("Hello, world n=#u\r\n", n++);
		}
	}
}

Cliff

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

simulavr actually seems to be OK for what I'm doing (testing some assembly algorithms) and I'm quite comfortable with gdb, so I don't mind a whole lot. With some creatively placed breakpoints I was able to get around not being able to print from AVR code.

If I need to simulate input/output, then I'd have to use something else. Thankfully I just need an instruction set simulator for now.