简体   繁体   中英

stm8 uart tx interrupt issue

I am programming a STM8S103F3 to TX on UART via interrupt. I understand a write to DR after "Transmit data register empty interrupt" will start another TX, so I have this in my ISR. But it only works if my main loop spins on wait for interrupt. If it spins on nop only the first char is TXed - as though the write to DR within the ISR does not generate a subsequent interrupt.

Using SDCC compiler.

sdcc -mstm8 -o build\uart.hex uart.c

#include <stdint.h>
#include <stdlib.h>
#include "stm8.h"

#define DEBUG_BUF_SIZE 10
char debugBuf[DEBUG_BUF_SIZE];
volatile unsigned char *debugPtr;

// UART Tx interrupt
void TX_complete(void) __interrupt(UART_TX_COMPLETE) {
    if(*debugPtr != 0) {
        UART1_DR = *debugPtr++;
    } 
}

void log(char *msg)
{
    unsigned char i = 0;

    UART1_CR2 &= ~UART_CR2_TIEN;
    for(; msg[i] != 0 && i<DEBUG_BUF_SIZE-1; i++) {
        debugBuf[i] = msg[i];
    }
    debugBuf[i] = 0;
    debugPtr = debugBuf;
    UART1_CR2 |= UART_CR2_TIEN;

    // Write to DR will start tx
    UART1_DR = *debugPtr++;
}


int main(void)
{
    // UART 115K2 baud, interrupt driven tx
    // UART1_CR1, UART_CR3 reset values are 8N1
    UART1_BRR2 = 0x0B;
    UART1_BRR1 = 0x08;
    UART1_CR2 |= UART_CR2_TEN | UART_CR2_TIEN;

    /* Set clock to full speed (16 Mhz) */
    CLK_CKDIVR = 0;

    log("Run\r\n");

    while(1) {
        // Only the first char is txed if nop is used
        nop();
        // But all chars txed if wfi is used
        // wfi();
    }
}

See your reference manual for stm8 (I use CD00218714) at chapter 12.9.1 you will see default value (after reset) of CPU condition code register it's 0x28 - this mean that just after start your mcu will work at interrupt level 3 and all software interrupt are disabled, only RESET and TRAP will workable.

According to program manual (I use CD00161709) instruction WFI change interrupt level to level 0 and your software interrupt of USART become workable.

You need to insert asm("rim"); just after initialization code (after line CLK_CKDIVR = 0; ) - this will make your code workable with asm("nop"); based main loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM