AVR ATmega saves reset when using printf before main loop

I am developing a C application using avr-libc on an AVR ATmega328P microcontroller. Since I don't have an ICE debugger, I followed these instructions and this tutorial to create features stdio.hsuch as those printfcapable of using hardware UART like stdout.

This works, and I can see the output on the PC terminal connected to my target board, but the strange thing is: when I have only one printfon the main one, but before the main loop something causes the processor to reset, whereas if I have printfonly inside main loop or before the main loop And inside the loop it works great. Something like that:

#include <stdio.h>

/* stream definitions for UART input/output */
FILE uart_output = FDEV_SETUP_STREAM(uart_drv_send_byte, NULL, _FDEV_SETUP_WRITE);
FILE uart_input = FDEV_SETUP_STREAM(NULL, uart_drv_read_byte, _FDEV_SETUP_READ);

int main() {
    /* Definition of stdout and stdin */
    stdout = &uart_output;
    stdin = &uart_input;

    /* Configures Timer1 for generating a compare interrupt each 1ms (1kHz) */
    timer_init()

    /* UART initialization */
    uart_drv_start(UBRRH_VALUE, UBRRL_VALUE, USE_2X, &PORTB, 2);

    /* Sets the sleep mode to idle */
    set_sleep_mode(SLEEP_MODE_IDLE);

    printf("START ");

    /* main loop */
    while(1) {
        printf("LOOP ");

        /* Sleeps so the main loop iterates only on interrupts (avoids busy loop) */
        sleep_mode();
    }
}

The above code produces the following output:

START LOOP LOOP LOOP LOOP LOOP LOOP ... LOOP

which is expected. If we comment on the line printf("START "), it will produce this:

LOOP LOOP LOOP LOOP LOOP LOOP LOOP ... LOOP

which is also wonderful. The problem is that if whileI don’t have in the loop printf, it will look like this:

START START START START START START ... START

, , START, , 1 & times; kHz. ? , ( , , LOOP, START).

GPIO

, GPIO print("START ") sleep_mode :

int main() {

    /* Irrelevant parts suppressed... */

    GPIO1_ON;
    printf("START ");
    GPIO1_OFF;

    /* Main loop */
    while(1) {

        /* Sleeps so the main loop iterates only on interrupts (avoids busy loop) */
        GPIO2_ON;
        sleep_mode();
        GPIO2_OFF;
    }
}

, GPIO1 132 μs (printf("START ") ), 6.6 ms - , 9600 / - GPIO2 12 ( : UART-ready-to-transfer UART-empty-data-register), 1,4 , GPIO1 , printf("START ") - , reset. , , UART, , UART , , printf , reset ( , reset , UART ).


(SOLVED!): , UART init TX **

UART AVR, , RS-232, RS-485, TX_ENABLE . , , ATmega328P ATmega644, , #define TX_VECTOR, . "TX_VECTOR" UDRE , USART0_TX_vect ( , ...)

(ISR) USART0_TX_vect, reset , @PeterGibson . !

// Interrupt vectors for Atmega328P
#if defined(__AVR_ATmega328P__)
    #define RX_VECTOR USART_RX_vect
    #define TX_VECTOR USART_UDRE_vect
    // Interrupt vectors for Atmega644
#elif defined(__AVR_ATmega644P__)
    #define RX_VECTOR USART0_RX_vect
    #define TX_VECTOR USART0_UDRE_vect
#endif

ISR(TX_VECTOR)
{
    uint8_t byte;

    if (!ringbuffer_read_byte(&txrb, &byte)) {

        /* If RS-485 is enabled, sets TX_ENABLE high */
        if (TX_ENABLE_PORT)
            *TX_ENABLE_PORT |= _BV(TX_ENABLE_PIN);
        UDR0 = byte;
    }
    else {
        /* No more chars to be read from ringbuffer, disables empty
         * data register interrupt */
        UCSR0B &= ~_BV(UDRIE0);
    }

    /* If RS-485 mode is on and the interrupt was called with TXC0 set it
     * means transmission is over. TX_ENABLED should be cleared. */
    if ((TX_ENABLE_PORT) && (UCSR0A & _BV(TXC0) & _BV(UDR0))) {
        *TX_ENABLE_PORT &= ~_BV(TX_ENABLE_PIN);
        UCSR0B &= ~_BV(UDRIE0);
    }
}

void uart_drv_start(uint8_t ubrrh, uint8_t ubrrl, uint8_t use2x,
                    volatile uint8_t* rs485_tx_enable_io_port,
                    uint8_t rs485_tx_enable_io_pin)
{
    /* Initializes TX and RX ring buffers */
    ringbuffer_init(&txrb, &tx_buffer[0], UART_TX_BUFSIZE);
    ringbuffer_init(&rxrb, &rx_buffer[0], UART_RX_BUFSIZE);

    /* Disables UART */
    UCSR0B = 0x00;

    /* Initializes baud rate */
    UBRR0H = ubrrh;
    UBRR0L = ubrrl;
    if (use2x)
        UCSR0A |= _BV(U2X0);
    else
        UCSR0A &= ~_BV(U2X0);

    /* Configures async 8N1 operation */
    UCSR0C = _BV(UCSZ00) | _BV(UCSZ01);

    /* If a port was specified for a pin to be used as a RS-485 driver TX_ENABLE,
     * configures the pin as output and enables the TX data register empty
     * interrupt so it gets disabled in the end of transmission */
    if (rs485_tx_enable_io_port) {
        TX_ENABLE_PORT = rs485_tx_enable_io_port;
        TX_ENABLE_PIN = rs485_tx_enable_io_pin;
        /* Configures the RS-485 driver as an output (on the datasheet the data
         * direction register is always on the byte preceding the I/O port addr) */
        *(TX_ENABLE_PORT-1) |= _BV(TX_ENABLE_PIN);
        /* Clears TX_ENABLE pin (active high) */
        *TX_ENABLE_PORT &= ~_BV(TX_ENABLE_PIN);
        /* Enables end of transmission interrupt */
        UCSR0B = _BV(TXCIE0);
    }
    /* Enables receptor, transmitter and RX complete interrupts */
    UCSR0B |= _BV(RXEN0) | _BV(TXEN0) | _BV(RXCIE0);
}

UART ( 100%!)

, UART, AVR ATmega, . , ISR!

// Interrupt vectors for Atmega328P
#if defined(__AVR_ATmega328P__)
    #define RX_BYTE_AVAILABLE USART_RX_vect
    #define TX_FRAME_ENDED USART_TX_vect
    #define TX_DATA_REGISTER_EMPTY USART_UDRE_vect
    // Interrupt vectors for Atmega644
#elif defined(__AVR_ATmega644P__)
    #define RX_BYTE_AVAILABLE USART0_RX_vect
    #define TX_FRAME_ENDED USART0_TX_vect
    #define TX_DATA_REGISTER_EMPTY USART0_UDRE_vect
#endif

/* I/O port containing the pin to be used as TX_ENABLE for the RS-485 driver */
static volatile uint8_t* TX_ENABLE_PORT = NULL;

/** Pin from the I/O port to be used as TX_ENABLE for the RS-485 driver */
static volatile uint8_t TX_ENABLE_PIN = 0;

ISR(RX_BYTE_AVAILABLE)
{
    // Read the status and RX registers.
    uint8_t status = UCSR0A;
    // Framing error - treat as EOF.
    if (status & _BV(FE0)) {
        /* TODO: increment statistics */
    }
    // Overrun or parity error.
    if (status & (_BV(DOR0) | _BV(UPE0))) {
        /* TODO: increment statistics */
    }
    ringbuffer_write_byte(&rxrb, UDR0);
}

ISR(TX_FRAME_ENDED)
{
    /* The end of frame interrupt will be enabled only when in RS-485 mode, so
     * there is no need to test, just turn off the TX_ENABLE pin */
    *TX_ENABLE_PORT &= ~_BV(TX_ENABLE_PIN);
}

ISR(TX_DATA_REGISTER_EMPTY)
{
    uint8_t byte;

    if (!ringbuffer_read_byte(&txrb, &byte)) {
        /* If RS-485 is enabled, sets TX_ENABLE high */
        if (TX_ENABLE_PORT)
            *TX_ENABLE_PORT |= _BV(TX_ENABLE_PIN);
        UDR0 = byte;
    }
    else {
        /* No more chars to be read from ringbuffer, disables empty
         * data register interrupt */
        UCSR0B &= ~_BV(UDRIE0);
    }
}

void uart_drv_start(uint8_t ubrrh, uint8_t ubrrl, uint8_t use2x,
                    volatile uint8_t* rs485_tx_enable_io_port,
                    uint8_t rs485_tx_enable_io_pin)
{
    /* Initializes TX and RX ring buffers */
    ringbuffer_init(&txrb, &tx_buffer[0], UART_TX_BUFSIZE);
    ringbuffer_init(&rxrb, &rx_buffer[0], UART_RX_BUFSIZE);

    cli();

    /* Disables UART */
    UCSR0B = 0x00;

    /* Initializes baud rate */
    UBRR0H = ubrrh;
    UBRR0L = ubrrl;
    if (use2x)
        UCSR0A |= _BV(U2X0);
    else
        UCSR0A &= ~_BV(U2X0);

    /* Configures async 8N1 operation */
    UCSR0C = _BV(UCSZ00) | _BV(UCSZ01);

    /* If a port was specified for a pin to be used as a RS-485 driver TX_ENABLE,
     * configures the pin as output and enables the TX data register empty
     * interrupt so it gets disabled in the end of transmission */
    if (rs485_tx_enable_io_port) {
        TX_ENABLE_PORT = rs485_tx_enable_io_port;
        TX_ENABLE_PIN = rs485_tx_enable_io_pin;

        /* Configures the RS-485 driver as an output (on the datasheet the data
         * direction register is always on the byte preceding the I/O port addr) */
         *(TX_ENABLE_PORT-1) |= _BV(TX_ENABLE_PIN);

        /* Clears TX_ENABLE pin (active high) */
        *TX_ENABLE_PORT &= ~_BV(TX_ENABLE_PIN);

        /* Enables end of transmission interrupt */
        UCSR0B = _BV(TXCIE0);
    }
    /*  Enables receptor, transmitter and RX complete interrupts */
    UCSR0B |= _BV(RXEN0) | _BV(TXEN0) | _BV(RXCIE0);

    sei();
}

void uart_drv_send_byte(uint8_t byte, FILE *stream)
{
    if (byte == '\n') {
        uart_drv_send_byte('\r', stream);
    }
    uint8_t sreg = SREG;
    cli();

    /* Write byte to the ring buffer, blocking while it is full */
    while(ringbuffer_write_byte(&txrb, byte)) {
        /* Enable interrupts to allow emptying a full buffer */
        SREG = sreg;
        _NOP();
        sreg = SREG;
        cli();
    }

    /* Enables empty data register interrupt */
    UCSR0B |= _BV(UDRIE0);
    SREG = sreg;
}

uint8_t uart_drv_read_byte(FILE *stream)
{
    uint8_t byte;
    uint8_t sreg = SREG;
    cli();
    ringbuffer_read_byte(&rxrb, &byte);
    SREG = sreg;
    return byte;
}
+3
2

, UDRE (UART Data Register Empty) , , ( ). printf , .

( , ), reset , reset. , BADISR_vect, ISR() . ( BADISR_vect __vector_default. .)

+6

, stackoverflow, .

, :

void USART_Init()
{
    cli();
    /* Set baud rate */
    UBRR0H = (uint8_t)(BAUD_PRESCALE>>8);
    UBRR0L = (uint8_t)BAUD_PRESCALE;
    /* Enable receiver and transmitter */
    UCSR0B |= (1<<RXEN0)|(1<<TXEN0);
    /* Set frame format: 8data, 1stop bit 8N1 => 86uS for a byte*/
    UCSR0C |= (1<<UCSZ01)|(1<<UCSZ00);
    /*enable Rx and Tx Interrupts*/
    UCSR0B |= (1 << RXCIE0) | (1 << TXCIE0); //<- this was the problem
    /*initialize the RingBuffer*/
    RingBuffer_Init(&RxBuffer);
    sei();
}

, , 10 Tx init.

.

0

All Articles