STM32 + RT Thread OS learning Note [II]

Source: Internet
Author: User
Tags message queue reset
Serial communication Routines

Through the above exercises, the STM32 project development has a visual impression, the next attempt to operate the serial port RS232.

1. Target Requirements:

Turn on the serial port 1, listen to the host computer (using the PC serial test software) sent information, and then delivered to the serial port 1.

2. Create a project

A) disabling Finsh and console

b) By default, the project file contains Finsh, which uses COM1 to communicate, and the console output (rt_kprintf) uses COM1. Therefore, before you run the scons command to build the project file, modify the Rtconfig.h to disable both. (The following figure L65, L70)

c) Build the project file

Run SCons--target=mdk4–s

Open the generated project file, and you can see that the filegroup Finsh is no longer included in the file.

d) Create echo.c

Create a new C file echo.c, write the Rt_thread task entry, COM1 listen, and initialize the function. The sample code is as follows:

#include "echo.h" struct rx_msg {rt_device_t dev;
rt_size_t size;
 
};
static struct Rt_messagequeue rx_mq;
static Char uart_rx_buffer[64];
 
static Char msg_pool[2048];
    Serial Listener callback function rt_err_t uart_input (rt_device_t Dev, rt_size_t size) {struct rx_msg msg;
    Msg.dev = Dev;
   
        msg.size = size;
   
    Put the received content into the message queue Rt_mq_send (&RX_MQ, &msg, sizeof (struct rx_msg));
return rt_eok;
   
    }//Task entry function void Usr_echo_thread_entry (void* parameter) {struct rx_msg msg;
    rt_device_t device;
   
        rt_err_t result = Rt_eok;
    Acquisition of serial 1 devices from RT System device = Rt_device_find ("Uart1"); if (device! = Rt_null) {//Specifies the callback function that receives the serial content rt_device_set_rx_indicate (device, uart_in
                           Put);
    Open the device Rt_device_open (device, RT_DEVICE_OFLAG_RDWR) in a read-write manner; } while (1) {//Gets the contents of the callback function into the message queue from the message queue result = RT_MQ_RECV (&RX_MQ, &am P;msg, sizeof (sTruct rx_msg), 50);
        if (result = =-rt_etimeout) {//timeout, do nothing} if (result = = Rt_eok)
           
            {rt_uint32_t rx_length;
                Rx_length = (sizeof (uart_rx_buffer)-1) > Msg.size?
           
            Msg.size:sizeof (Uart_rx_buffer)-1;
            Rx_length = Rt_device_read (Msg.dev, 0, &uart_rx_buffer[0], rx_length);
            Uart_rx_buffer[rx_length] = ' + ';
        Write the contents back to the serial port 1 rt_device_write (device, 0, &uart_rx_buffer[0], rx_length);
   
    }}}///serial port routine initialization function void Usr_echo_init () {rt_thread_t thread; 
      rt_err_t result; Create a message queue, allocate the queue storage space result = Rt_mq_init (&RX_MQ, "&msg_pool[0", 128-sizeof (void*), sizeof (Msg_pool), Rt_
   
    IPC_FLAG_FIFO); 
        if (Result! = Rt_eok) {rt_kprintf ("init message queue failed.\n"); 
    Return }//Create Task thread = rt_thread_create ("Devt", uSr_echo_thread_entry, Rt_null, 1024, 25, 7);
Start the task thread if (thread! = rt_null) rt_thread_startup (thread);
  }

Add initialization code to APPLICATION.C (echo.h)

L189:usr_echo_init ()

;

Before starting the compilation, also modify the BOARD.C, comment out the 183th line, or the error will be. Because we have disabled the console, we do not need to set the console output device.


e) test

Compile, download, test.

3. Program Analysis

A) memory distribution

View the compiled generated Obj/rtthread-stm32.map file, you can see the code and constants, downloaded to the 0x8000000 address segment of the chip, the first is the interrupt vector table, the initial interrupt address is reset, vector table total 0x130 bytes.

Variable definition with initial value, starting from address segment 0x20000000

The corresponding Stm32 memory map table, code and constants are downloaded to flash, initialized variables are positioned to SRAM (may be downloaded to flash, boot initialization is copied to RAM, not directly downloaded to RAM, or the next run, the initial value may have been modified)

This is the address assignment for the chip memory area in the MDK

This is j-link on the chip definition, memory is 512K, type is on-chip Flash, address space from 0x08000000 to 0X0807FFFF

b) program running process

After booting, from the Flash 0x080000000 interrupt vector table, get reset interrupt processing function entry address, jump to the entry function to start performing reset interrupt service, as shown below, reset interrupt service function defined in Startup_stm32f10x_ Hd.s, the Systeminit () in the Stm32 class library is executed first, and then the main () function is transferred.

Systeminit () is primarily about the chip's basic settings, such as the clock frequency.

In Rt-thread, STARTUP.C is provided under the BSP directory, containing the main () function, which invokes Rtthread_startup () in the same file, and then Rtthread_startup () calls Rt_application_ Init (), Rt_application_init () is defined in APPLICATION.C, and the user code starts here.

Another important file is stm32f10x_it.c, which defines the interrupt service routine, which addresses the address in the interrupt vector table to the corresponding service function entry address in the file. For example, our serial port 1 received the host computer's message, will produce USART1_IRQ, then the chip will be in the 0x08000000 interrupt vector table to find the Usart1_irqhandler () of the entry address, jump to start the execution of the interrupt service function usart1_ Irqhandler ().


Of course, to generate interrupts, you need to open interrupts in the initialization code.

<echo.c>

The source code of this file is posted above, except Uart_input (), others are more intuitive. In the RTT system, Uart_input () is only part of the Usart1_irqhandler (), and in ECHO.C's initialization code, is registered as a callback function for Uart1 this device (RTT encapsulated object):

L34:rt_device_set_rx_indicate (device, uart_input);

The process reference is shown below:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.