Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Is the RTOS part of the compiled code?

Status
Not open for further replies.

shaiko

Advanced Member level 5
Joined
Aug 20, 2011
Messages
2,644
Helped
303
Reputation
608
Reaction score
297
Trophy points
1,363
Activity points
18,302
Hello,

Suppose I have a Cortex M4 microcontroller and I want to run a small RTOS on it.

1. Should the source code for the RTOS Kernel be compiled together with all the C project files?
2. To run the RTOS, do I simply call it as any other C function from my main code?
Code:
int main()
{
run_rtos () ; // there's a whole world behind this function and how this world works is described in the OS source files that I compiled together with this main function.
}
 

Which RTOS are you wanting to use?
You need to look at the documentation that comes with the RTOS to see how you interact with it. Generally you need to set up the RTOS kernel operation and then create threads within that. Each thread will then interact with the kernel via the RTOS API calls using semaphores, flags and the other structures that the RTOS provides.
From what I've just written, it should be obvious that you need to consider the RTOS as you are writing your code. Whether the RTOS is included into the final app as a library or as source files that you compile will depend on the RTOS itself. (I mainly use freeRTOS and that is compiled in as it uses a number of ".h" files to configure how it operates; however it only needs to be recompiled when those options change and normally the 'make' files handle that for you.)
Susan
 
  • Like
Reactions: shaiko

    shaiko

    Points: 2
    Helpful Answer Positive Rating
Which RTOS are you wanting to use?
None.
It's a pure theoretical question.
I know that C allows only one "main" function per project - so I wanted to know how is it possible to run many programs (in an OS environment) without having to compile more than one "main" function.

Can you post a link with the simplest of examples for an embedded OS?
 

Before run task manager, you should create at least one task for it. That's simple.
 

I suggest that you have a look at the freeRTOS web site. That RTOS may not be the one you want to go with in the long term (I've found it OK for my needs) but it does contain sections on creating tasks and downloadable code examples.
Susan
 
  • Like
Reactions: shaiko

    shaiko

    Points: 2
    Helpful Answer Positive Rating
.. but generalizing, there is still only one 'main()' but it starts the RTOS running, the other applications become tasks called from it.

Brian.
 

That RTOS may not be the one you want to go with in the long term
Any particular reason why ?

- - - Updated - - -

but generalizing, there is still only one 'main()' but it starts the RTOS running, the other applications become tasks called from it.
Similar to the way I described it in the code snippet of post #1 ?
 

You may not want to go with the freeRTOS in the longer term because it may not provide you with everything you want to do or it does not support the chip you want to use. For example, I've used it on a Microchip dsPIC33EP series device that is not "officially" supported. I had to add my own task switching assembler but there were some registers that were NOT saved between context switches because the design of the chip meant there was no reliable way to save and restore them. For me, those registers were not required (they were used in situations I was not wanting to use) but your case may be different.
You have said it was a "theoretical" question so it is impossible for us to know what would suit your needs in any particular situation.

As for the "main" program and the general structure of the code, even on a microcontroller, there will be a minimal run-time that runs before calling your "main" function. That part will not change.
In general, an RTOS will provide a way to switch between tasks. Some will do it on a round-robin basis, others with pre-emptive time-slicing or some other basis or a combination of everything. The main this is that you need to break your application into a set of tasks that can operate independently of each other, or interact using the facilities provided by the RTOS (semaphores and the like).
As EasyRider83 has said, you need to create at least one task before starting the task manager. The task manager really only switches between tasks so there need to be at least one task defined before that task manager takes over (as it acts like the "while" loop and never ends).
Tasks can create and delete other tasks but you need 1 task as a minimum to do anything.
Susan
 
Here is my RTOS test code,
i want to run two threads to blink two different LEDs at different rates.
I am using Keil RTX, I feel its better in use than freeRTOS.

Code:
/*----------------------------------------------------------------------------
 * CMSIS-RTOS 'main' function template
 *---------------------------------------------------------------------------*/

#define osObjectsPublic                     // define objects in main module
#include "osObjects.h"                      // RTOS object definitions

#include "stm32f10x.h"                  // Device header
#include "GPIO_STM32F10x.h"             // Keil::Device:GPIO

void led_init(void)
{
	GPIO_PinConfigure( GPIOB, 8, GPIO_OUT_PUSH_PULL, GPIO_MODE_OUT50MHZ);
	GPIO_PinConfigure( GPIOB, 9, GPIO_OUT_PUSH_PULL, GPIO_MODE_OUT50MHZ);
}

void job1 (void const *arg) { 
	while (1) {
			GPIO_PinWrite(GPIOB, 8, 0);
			osDelay(500);
			GPIO_PinWrite(GPIOB, 8, 1);
			osDelay(500);
		} 
}
void job2 (void const *arg) { 
	while (1) {
			GPIO_PinWrite(GPIOB, 9, 0);
			osDelay(100);
			GPIO_PinWrite(GPIOB, 9, 1);
			osDelay(100);
		} 
}	
osThreadDef (job1, osPriorityNormal, 1, 0); // Define thread for job1
osThreadDef (job2, osPriorityNormal, 1, 0); // Define thread for job2
/*
 * main: initialize and start the system
 */
int main (void) {
  osKernelInitialize ();                    // initialize CMSIS-RTOS

  // initialize peripherals here

  // create 'thread' functions that start executing,
  // example: tid_name = osThreadCreate (osThread(name), NULL);
  // initialize peripherals here
	led_init();
  // create 'thread' functions that start executing,
  // example: tid_name = osThreadCreate (osThread(name), NULL);

	osThreadCreate (osThread (job1), NULL); // Create and start job1 
	osThreadCreate (osThread (job2), NULL); // Create and start job2	

  osKernelStart ();                         // start thread execution 
	while (1) { 
		osThreadYield (); 
		// Next thread 
		}		
}
 
  • Like
Reactions: shaiko

    shaiko

    Points: 2
    Helpful Answer Positive Rating
Thanks gamegurus.

In your example you compile the OS as any other code - correct?
 

yes, i created this code in main.c file

you can find its documentation here from which i created the RTOS test code
**broken link removed**
 
  • Like
Reactions: shaiko

    shaiko

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top