STM8S UART serial port uses interrupt to send and receive data

Publisher:cocolangLatest update time:2019-09-17 Source: eefocusKeywords:STM8S Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

I used to adjust the serial port of STM8L, which had simple logic and clear interrupts. After changing to STM8S105K4, although the STD library is also used, in addition to the differences in language levels such as function names and macro names, there are also some differences in interrupt handling, which I would like to note.

The structure is the same as this article [Using STM8L USART serial port], which is also an interrupt asynchronous mode, but for the convenience of calling, it is changed to synchronous at the calling level.

(For STM8S105K MCU, RX is PD6 and RX is PD5.)


In terms of usage, the main problem is that the interrupt name, usage occasions and timing are not clear, which is not as clear as the definition of STM8L.

For example, use UART2_IT_RXNE_OR to switch interrupts, and use UART2_IT_RXNE to clear interrupts. You cannot use UART2_IT_RXNE to switch, nor can you use UART2_IT_RXNE_OR to clear interrupts, otherwise the STD library will assert that the parameters are legal, and the program will hang in minutes.

For reference.


The following is the sample code. In order to better separate from the application layer and generalize the code, an independent UART read and write buffer is set. If the buffer is large, please use @near to initialize it in the separated data area.

In addition, although it is interrupt driven, considering that most usage scenarios are synchronous, a synchronous state variable is set and detected in the read and write functions. If it is changed to an interrupt, you only need to change the judgment of the state variable to judgment on the application side.


1. Read and write buffer and identification value definition


#define UART_BUF_SIZE 128


/* Read buffer */

uint8_t read_ok = 0; // read completion flag

uint8_t read_idx = 0;

uint8_t read_len = 0;

@near uint8_t read_buffer[UART_BUF_SIZE]; // When the buffer is set to be large, @near can be used


/* Write buffer */

uint8_t writ_ok = 0; // Write completed flag

uint8_t writ_idx = 0;

uint8_t writ_len = 0;

@near uint8_t writ_buffer[UART_BUF_SIZE]; // When the buffer is set to be large, @near can be used


2. Serial port initialization

STM8S105K4 has only one serial port, namely UART2


int8_t uart_init(void)

{

// Please modify the serial port parameters as required

UART2_DeInit();

UART2_Init((uint32_t)38400, 

UART2_WORDLENGTH_8D, 

UART2_STOPBITS_1, 

UART2_PARITY_NO, 

UART2_SYNCMODE_CLOCK_DISABLE, 

UART2_MODE_TXRX_ENABLE);

// Explicitly turn off interrupts (off by default)

// Notice:

// The read interrupt name is UART2_IT_RXNE_OR, not UART2_IT_RXNE

// Write interrupt name as UART2_IT_TXE

UART2_ITConfig(UART2_IT_RXNE_OR, DISABLE);

UART2_ITConfig(UART2_IT_TXE, DISABLE);

//Enable the serial port

UART2_Cmd(ENABLE);

return 0;

}


3. Read and write functions


// Write multiple bytes

void uart_send_n_byte(uint8_t* data, uint8_t len)

{

uint16_t count = 0;

UART2_ITConfig(UART2_IT_TXE, DISABLE);


// Prepare to write data buffer (copy from user data area to serial port write buffer, initialize index value, etc.)

memcpy(writ_buffer, data, len);

writ_idx = 0;

writ_len = len;


// Enable write interrupt

UART2_ITConfig(UART2_IT_TXE, ENABLE);

while(!writ_ok) { // Wait for writing to complete (synchronous processing)

count++;

if( count >= 10000 ) { // Simple timeout processing, can be removed if no timeout is needed

writ_idx = 0;

writ_len = 0;

break;

}

}

writ_ok = 0; // Writing completed, reset writing completed flag

return;

}


// Read multiple bytes

void uart_read_n_byte(uint8_t* data, uint8_t len)

{

// Disable read interrupt

UART2_ITConfig(UART2_IT_RXNE_OR, DISABLE);


// Clear the read buffer (reset the read index value)

read_idx = 0;

read_len = len;


// Enable read interrupt

UART2_ITConfig(UART2_IT_RXNE_OR, ENABLE);

while(!read_ok); // Wait for the read operation to complete (synchronization processing), add timeout processing, refer to the above write operation

read_ok = 0; // Write completed, reset write completed flag

memcpy(data, read_buffer, read_len); // Copy data to user buffer

return;

}


4. Interrupt handling


INTERRUPT_HANDLER(UART2_TX_IRQHandler, 20)

{

// Write operations automatically clear interrupts, so there is no need to explicitly clear interrupts

//UART2_ClearITPendingBit(UART2_IT_TXE); 

// Write 1 byte from the write buffer

UART2_SendData8(writ_buffer[writ_idx++]);


// Complete all writing, turn off writing interrupt, set writing completion flag (synchronization processing)

if( writ_idx == writ_len ) {

UART2_ITConfig(UART2_IT_TXE, DISABLE);

write_ok = 1;

}

}


 INTERRUPT_HANDLER(UART2_RX_IRQHandler, 21)

{

// The read operation automatically clears the interrupt, so there is no need to explicitly clear the interrupt

// Note that the interrupt name here is RXNE, not RXNE_OR

UART2_ClearITPendingBit(UART2_IT_RXNE);


// Read 1 byte

read_buffer[read_idx++] = UART2_ReceiveData8();


// After reading all, turn off interrupt (UART2_IT_RXNE_OR), set the read completion flag (synchronization processing)

if( read_idx == read_len ) {

UART2_ITConfig(UART2_IT_RXNE_OR, DISABLE);

read_ok = 1;

}

}


5. Use code examples


// Write 2 bytes

uint8_t buf[32];

memset(buf, 0x00, sizeof(buf));

buf[0] = 0xCC;

buf[1] = 0xDD;

uart_send_n_byte(buf, 2);


// Simple read (must read 24 bytes before returning)

memset(buf, 0x00, sizeof(buf));

uart_send_n_byte(buf, 24);

Keywords:STM8S Reference address:STM8S UART serial port uses interrupt to send and receive data

Previous article:Usage of UART in stm8s (four types of UART interrupts)
Next article:UART0 serial port programming (III): interrupt mode serial port programming; use interrupt to write and send

Recommended ReadingLatest update time:2024-11-16 12:48

LM3S9B96 UART print string function
When debugging, you can observe the value of a variable, provided that you use JLINK for debugging. In fact, when the program is running at full speed, you can see the value of a variable. This is where the UART print string function comes into play. 1. UARTprintf function: This is similar to the printf function in C
[Microcontroller]
S3C2440 bare metal program [2] serial port uart program
When learning ARM7 chip stm32, bare metal program development can be easily developed on the project template according to the library function, while ARM9 is mainly developed by porting Linux, and there is little bare metal program development. Therefore, when playing with S3C2440, I hope to eventually form a templat
[Microcontroller]
S3C2440 bare metal program [2] serial port uart program
S3C2440 Hardware Part 7: UART
In fact, the UART of each MCU is similar. After setting the register, writing data to the buffer can complete the data transmission, and reading the buffer data can receive external data. The following is excerpted from Brother Wei's "Complete Manual for Embedded Linux Application Development"   1. UART principle and
[Microcontroller]
STM8S---IO multiplexing configuration (STVP mode)
1 Description The IO multiplexing of STM8S is more complicated to configure with program code. Generally, the flash is used to operate the option byte. The configuration register is even more complicated. You can use the STM standard peripheral driver library to set it. This article uses an interface configuration m
[Microcontroller]
AVR's UART serial communication program
// 1. Use ICCAVR's terminal debugging window (Terminal) to debug communication.   // 2. Set ICCAVR's terminal debugging window and set the serial port to com1 or com2    // The communication baud rate is 19200 (Tools- Environment Options...).   // 3. Position the PC screen cursor in the debugging window.   #include   
[Microcontroller]
Understanding and setting method of stm8s interrupt priority program
  STM8 interrupt priority   The interrupt of STM8S is controlled by the interrupt controller (ITC). All IOs of STM8 support interrupts and are divided into 5 groups: PA~PE. Each group of IO corresponds to an interrupt service function (that is, each group of IO has only one vector).   STM8 does not have a dedicated
[Microcontroller]
Understanding and setting method of stm8s interrupt priority program
STM8S and STM32F IO port output rate test
When I checked the STM32 data today, I didn't know much about the output speed of 2M 10M 50M, and there were unexpected situations when porting the ARF2496K program to STM32. 1. The data received by the STM8S end as the receiving end and the transmitting end are normal. 2. The sending end (STM8S) and the receiving end
[Microcontroller]
STM8S and STM32F IO port output rate test
STM8 UART receiver
STM8 UART Receiver The UART can receive 8-bit or 9-bit data words. If the M bit is set to 1, the word length is 9 bits, with the MSB stored in bit R8 of register UART_CR1. Character Reception During UART reception, the least significant bit of data is shifted in first from the RX pin. In this mode, the UART_DR regis
[Microcontroller]
STM8 UART receiver
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号