
Call for papers | You can't write, but you can still get rewards!

What is RT-Thread Nano? As we know, Keil5 now uses the pack format to manage chips and related components. RT-Thread Nano is also released through the Keil pack method, achieving minimal Flash and RAM usage while maintaining basic functions. By default, Flash can be as small as 2.5K and RAM as small as 1K.
The current pack includes three main functions: kernel, shell (msh), and device drivers. These can be loaded on demand depending on actual needs. The main chip used in this example is GD32F150C8T6, which has 64K Flash and 8K RAM.
First, download and install RT-Thread Nano Pack
1. Click the “Pack Install†button on the main interface of Keil5 to enter the Pack Install interface.
Figure 1: Keil5 main interface
2. In the Pack Install interface, the RT-Thread Pack is listed on the right side. If it hasn’t been downloaded, click “Installâ€; if already installed, you may see an “Update†prompt.
Figure 2: RT-Thread Pack download
3. If “RT-Thread†is not found in the “Packs†column, first click “Check for Updates†under the “Packs†menu. After updating, you will see the RT-Thread Pack.
Figure 3: Pack Update
4. After the package is downloaded, Keil will automatically open the installation window. Follow the steps to complete the installation.
Second, create a bare-metal minimum system project
1. This project uses the GD32F150C8T6 chip with 64K Flash and 8K RAM. First, download and install the Keil Pack (GigaDevice.GD32F1x0_DFP.pack) from the official website.
2. Build the project according to the standard bare-metal process in Keil. To test Flash and RAM size, the minimal project only contains necessary library files and no extra processing in the main function.
Figure 4: GD32F150C8T6 minimal project
3. After compilation, the default Flash size is 1112 bytes, and RAM is 2144 bytes.
4. Modify the startup file startup_gd32f1x0.s to define heap and stack sizes. The default heap is 0x400, and the stack is 0x400. We will use RT-Thread to manage memory, so set heap to 0. Adjust the stack to 0x100 or more based on application needs.
Figure 5: Stack and Heap modification
After modifying the startup file, RAM size is reduced to 352 bytes.
Figure 6: Flash and RAM footprint after changes
Third, load the kernel and apply it
1. Load RT-Thread Kernel: Click the “Manage Run-Time Environment†button on the main interface to enter the loading page.
Figure 7: Manage Run-Time Environment
Select “RT-Thread†under the “RTOS†column and choose “kernel†from the list. The current version is 2.1.2.
Figure 8: RT-Thread kernel selection
2. After confirmation, the RT-Thread kernel files are added to the Keil interface, along with the ported M3 code for the selected chip.
Figure 9: RT-Thread kernel files
Among them:
Kernel files include:
Clock.c
Components.c
Device.c
Idle.c
Ipc.c
Irq.c
Kservice.c
Mem.c
Object.c
Scheduler.c
Thread.c
Timer.c
Cortex-M3 chip kernel porting code:
Cpuport.c
Context_rvds.s
Application code and configuration file:
Board.c
Rtconfig.h
3. At this point, recompile the project. The compiler may show repeated definitions. Make the following adjustments:
a) Modify the gd32f10x_it.c file and delete the following functions:
Void HardFault_Handler(void)
Void PendSV_Handler(void)
Void SysTick_Handler(void)
b) Follow the instructions in board.c to make the following changes:
Figure 10: Board.c modification process
Modify line 24: #include "gd32f1x0.h"
Modify line 48: Enable SysTick_Config (SystemCoreClock / RT_TICK_PER_SECOND) in rt_hw_board_init() function;
Modify line 66: void SysTick_Handler(void)
4. Modify main.c:
Add #include
Add rt_thread_delay(100) to the while loop;
5. Recompile successfully, download to the chip, and you’ll see the main function is interrupted every 1 second. The RT-Thread task scheduler is running.
Figure 11: RT-Thread is running normally
You can view the .map file to check resource usage. Without optimization, you can see the resource usage of each file in the RT-Thread kernel.
Figure 12: Resource usage table
6. Add functions such as tasks, timers, and semaphores to the main function. By default, rtconfig.h only supports static tasks and semaphores. For dynamic creation, enable RT_USING_HEAP. See the fifth part for details.
Fourth, analyze the RT-Thread startup process
Although this Keil project uses RT-Thread, the development process is almost the same as traditional OS development. Both use main as the entry point, complete hardware initialization, add application code, and directly use RT-Thread’s features. But we didn’t add any RT-Thread-related initialization or startup code. How did the scheduler start?
01
RT-Thread entryLooking at the 140th line of components.c, we can see the #ifdef RT_USING_USER_MAIN macro definition. It is defined in rtconfig.h. Also, we can see #if defined (__CC_ARM) on line 146, which refers to Keil’s cross-compiler.
Here, two functions are defined: $$Sub$$main() and $$Super$$main(). The $$Sub$$main() function inserts code before the main program, extending functionality without changing the source code. The linker replaces main() by calling $SubSub$$Main(), then returns to main() via $$Super$$main.
#if defined (__CC_ARM)
Extern int $Super$$main(void);
/* re-define main function */
Int $Sub$$main(void)
{
Rt_hw_interrupt_disable();
Rtthread_startup();
Return 0;
}
The rt_hw_interrupt_disable() and rtthread_startup() functions are called within the $Sub$$main function. Familiar with RT-Thread development, you’ll recognize this as the standard boot entry.
Among them:
rt_hw_interrupt_disable(): disables interrupts
rtthread_startup(): configures systick, initializes timer, creates idle task, initializes application thread, starts scheduler, etc.
Int rtthread_startup(void)
{
rt_hw_interrupt_disable();
/* board level initialization */
* NOTE: Please initialize heap inside board initialization.
*/
rt_hw_board_init();
/* show RT-Thread version */
rt_show_version();
/* timer system initialization */
rt_system_timer_init();
/* scheduler system initialization */
rt_system_scheduler_init();
/* create init_thread */
rt_application_init();
/* timer thread initialization */
rt_system_timer_thread_init();
/* idle thread initialization */
rt_thread_idle_init();
/* start scheduler */
rt_system_scheduler_start();
/* never reach here */
return 0;
}
rt_hw_board_init(): defined in board.c and needs modification.
rt_system_timer_init()/rt_system_timer_thread_init(): timer initialization/startup
rt_thread_idle_init(): idle task creation
rt_application_init(): application thread initialization
rt_system_scheduler_start(): scheduler starts
02
Application thread entry rt_application_init()Void rt_application_init(void)
{
rt_thread_t tid;
#ifdef RT_USING_HEAP
tid = rt_thread_create("main", main_thread_entry, RT_NULL,
RT_MAIN_THREAD_STACK_SIZE, RT_THREAD_PRIORITY_MAX / 3, 20);
RT_ASSERT(tid != RT_NULL);
#else
rt_err_t result;
tid = &main_thread;
result = rt_thread_init(tid, "main", main_thread_entry, RT_NULL, main_stack, sizeof(main_stack), RT_THREAD_PRIORITY_MAX / 3, 20);
RT_ASSERT(result == RT_EOK);
#endif
rt_thread_startup(tid);
}
Here, we can see that the application thread has created a task called main_thread_entry and started it. Let's look at the main_thread_entry task again.
/* the system main thread */
Void main_thread_entry(void* parameter)
{
extern int main(void);
extern int $Super$$main(void);
/* RT-Thread components initialization */
rt_components_init();
/* invoke system main function */
#if defined (__CC_ARM)
$Super$$main(); /* for ARMCC. */
#elif defined(__ICCARM__) || defined(__GNUC__)
main();
#endif
}
The main_thread_entry task has completed two jobs: call rt_components_init() and enter the real main function of the application code.
Here we see the call to $$Super$$main(), which we mentioned earlier can be used to return to main().
Figure 13: RT-Thread initialization and startup process
From the above analysis, it is because the RT_USING_USER_MAIN option is enabled in rtconfig.h, the compiler inserts $$Sub$$main() before main to complete the RT-Thread initialization and scheduler startup. And by creating the main_thread_entry task and returning to the main() function via $$Super$$main(). It seems that the main() function is actually a task of RT-Thread. The priority of this task is RT_THREAD_PRIORITY_MAX / 3, and the task stack is RT_MAIN_THREAD_STACK_SIZE.
Figure 14: RT_USING_USER_MAIN option
Fifth, RT-Thread configuration (rtconfig.h)
RT-Thread is a highly configurable embedded real-time operating system configured via the rtconfig.h file. Nano is implemented with a 2.5K Flash, 1K RAM kernel. However, since Nano does not enable RT_USING_HEAP, it only supports static task and semaphore creation. The following steps show how to configure common options in rtconfig.h.
0 1
RT_USING_HEAP: Enable heapAccording to the chip model on line 37 of board.c, modify the SRAM_SIZE value. The default is 8, and for GD32F150C8T6, it is also 8K.
Figure 15: SRAM_SIZE Configuration
After enabling RT_USING_HEAP, rt_system_heap_init() is called in rt_hw_board_init() of board.c
#if defined(RT_USING_USER_MAIN)&& defined(RT_USING_HEAP)
rt_system_heap_init((void*)HEAP_BEGIN, (void*)SRAM_END);
#endif
Among them:
SRAM_END: defined as 0x20000000 + SRAM_SIZE * 1024 based on the macro
HEAP_BEGIN:
Figure 16: HEAP_BEGIN definition
Where Image$$RW_IRAM1$$ZI$$Limit is the linker export symbol indicating the end address of the ZI segment.
Once the configuration is complete, the software can be developed by dynamically creating tasks, semaphores, and more.
0 2
RT_USING_TIMER_SOFT: Enable software timer
The default configuration of Nano does not enable the software timer function. After enabling it, multiple software timers can be created with the accuracy of the Systick trigger.
Figure 17: Software Timer On
- End -
Three Phase Voltage Stabilizer
PC PC-SVC Three Phase series Voltage Stabilizer has the low energy consumption,the over voltage protection,the low voltage protection,the over-current protection,the over-loading protection,the over-temperature protection and so on.It boasts for many kinds of protections,the collection energy conservation and the environmental protection ect.This is a brand-new concept product which possess many new technologies!This series products simultaneously ha applied for many technical monopolies
We already applied many kinds of this products patent, and the technical patent NO: 200720036394.1 and Appearance paten NO: 200730025909.3
2. Use for equipment:
Computer
Test equipment
Light system
Safe alarm system
Ray equipment
Medical equipment
Copy machine
Stereo equipment
Numerical control machine tools
Industrial automation equipment
Color and drying equipment
Test equipment
Three Phase Voltage Stabilizer,3 Phase Stabilizer,3 Phase Voltage Stabilizer,Three Phase Stabilizer
zhejiang ttn electric co.,ltd , https://www.ttnpower.com