Osal Operating System Experimental learning notes 01__osal operating system learning

Source: Internet
Author: User

From the Ucous-ii contact with the operating system, understand the basic operating system concepts and work flow after the start of contact with the osal system, the first operating system of the workflow as shown below

This system is not mentioned for the time being to analyze the osal system.
Here is a source isight software, it is very convenient to see the code, the development environment is IAR for 8051.
The first section of the experiment do osal operating system startup
Starting from SIMPLEBLETEST_MAIN.C

int main (void)
{
/* Initialize Hardware * *
Hal_board_init (); Initialize clock stability clock and so on

Initialize Board I/O
Cold start, turn off the LED lights with interrupts, and the next various initializations are undisturbed
Initboard (Ob_cold);

* * Initialze the HAL driver * *
Haldriverinit (); Various driving initialization, such as key, LCD, ADC, USB, UART, etc.

/* Initialize NV System * *
SNV a section of flash,4kb space used to hold paired data or your user-defined data
Osal_snv_init ();

/* Initialize LL *

/* Initialize the operating system * *
OASL operating system initialization, including memory allocation, Message Queuing, timer, power management, and tasks
Osal_init_system ();

/* Enable Interrupts * *
Hal_enable_interrupts ()//Open Global Interrupt

Final Board Initialization
Initboard (Ob_ready); Set flag marking system initialization complete

#if defined (power_saving)
If you enable low power consumption, start low power mode,
Osal_pwrmgr_device (Pwrmgr_battery);
#endif
/*
Low power portion
1. How to always be in PM1
Osal_pwrmgr_device (pwrmgr_always_on);
2. How to enter the PM2
Osal_pwrmgr_device (Pwrmgr_battery), in the free time will enter the PM2 mode
3. How to enter the PM3
A connection is disconnected, a broadcast stops broadcasting, and all scheduled tasks that you create are closed, the system should enter PM3 mode, and only external interrupt wakes.
*/

/* Start osal * *
Osal_start_system (); No return
/* Osal Operating system startup, is actually a large cycle, just check the corresponding logo bit, the corresponding task to specify, see here, students should look where? In fact, this is the end of it? So where is our application written?
is actually in the above function Osal_init_system in the initialization, now go back to see osal_init_system this function inside know
*/
return 0;
}

Osal_init_system ();
OASL operating system initialization, including memory allocation, Message Queuing, timer, power management, and tasks, and most crucially within this line of functions, actually performs the initialization of our app and initiates event function execution.
Osal_start_system ();
void Osal_start_system (void)
{ if!defined (zbit) &&!defined (ubit)

for (;;)//Forever Loop endif

{
Osal_run_system ();
}
}
Osal operating system startup, is actually a large cycle, just check the corresponding flag bit, to specify the corresponding task.
Osal_run_system ();
void Osal_run_system (void)
{
Uint8 idx = 0; ifndef hal_board_cc2538

Osaltimeupdate (); endif

Hal_processpoll (); Query data, such as serial data, USB data, etc.

do {///training ring to find out whether there is an event, there are events, immediately quit, app application priority lowest
if (Tasksevents[idx])//Task is highest priority this is ready.
{
Break
}
while (++idx < taskscnt);

if (IDX < taskscnt)//found event
{
UInt16 events;
halintstate_t intstate;

Hal_enter_critical_section (intstate); Shutdown interrupt
events = Tasksevents[idx]; Read events for this task (events may be more than 1)
TASKSEVENTS[IDX] = 0; Clears the time record and there are new events to be placed during the task handling function
Hal_exit_critical_section (intstate); Open Interrupt

Activetaskid = idx;
Events = (Tasksarr[idx]) (IDX, events);//Tasksarr is the task handler function pointer
Activetaskid = Task_no_task;

Hal_enter_critical_section (intstate);/close Interrupt
TASKSEVENTS[IDX] |= events; Add back unprocessed events to the current task.
Hal_exit_critical_section (intstate)/Open Interrupt
} if defined (power_saving)

else//Complete pass through the all task events and no activity?
{//System sleep in order to achieve low power consumption purposes
Osal_pwrmgr_powerconserve (); Put the Processor/system in sleep
} endif

/* Yield in case cooperative scheduling is being used. * /If defined (configuse_preemption) && (configuse_preemption = 0)

{
Osal_task_yield ();
} endif

}

typedef unsigned short (*PTASKEVENTHANDLERFN) (unsigned char task_id, unsigned short event);
The first formal parameter task_id is the task ID, which is a meaning with the IDX defined above
The second parameter events are the meaning of the event, which is defined as 16 bits, so that we can only have 16 events,
For SCM, that's enough, if we ask for more of the event in the task, can be used in the app
Customize an internal variable to achieve bifurcation

The following two functions are the functions that provide the settings and cleanup events that we invoke:
Uint8 osal_set_event (uint8 task_id, uint16 event_flag);
Uint8 osal_clear_event (uint8 task_id, uint16 event_flag);

Tasksarr is an array that holds the address of the task handler function
This table is very important and is defined as the event execution function for each task, which can be understood as a callback function, or an event execution function
Const PTASKEVENTHANDLERFN tasksarr[] =
{
Ll_processevent,//Task 0
Hal_processevent,//Task 1
Hci_processevent,//Task 2 if defined (osal_cbtimer_num_tasks)

Osal_cbtimer_process_event (osal_cbtimerprocessevent),//Task 3 endif

L2cap_processevent,//Task 4
Gap_processevent,//Task 5
Gatt_processevent,//Task 6
Sm_processevent,//Task 7
Gaprole_processevent,//Task 8
Gapbondmgr_processevent,//Task 9
Gattservapp_processevent,//Task 10
Simplebletest_processevent//Task 11
This is the event handler function for our application
};

Const UINT8 taskscnt = sizeof (Tasksarr)/sizeof (tasksarr[0));
UInt16 *tasksevents;

We then see the Osal_system_init () function in the osal.c file, which initializes the system task and performs the task initialization of each layer inside the function.
and see it in the osal_simplebletest.c.
void Osalinittasks (void)
{
Uint8 TaskID = 0;
Assign task event space, here adopt dynamic method to do, more convenient in Tasksarr and code modification less
Tasksevents = (uint16) osal_mem_alloc (sizeof (uint16) taskscnt);
Osal_memset (tasksevents, 0, (sizeof (uint16) * taskscnt));
..............
* Application * *
Simplebletest_init (TaskID); This is our application initialization.
}
Is the initialization of our application, and imagine that this has achieved multitasking and that a
The framework of the maximum 16 events has been implemented in the service

Next look at our app

void Simplebletest_init (Uint8 task_id)
{//Save task ID to global variable
Simplebletest_taskid = task_id;
Hallcdwritestring ("Simplebletest", hal_lcd_line_1);
Setup a delayed profile startup
/*
Set up a task to do it in a multitasking manner
Simplebletest_processevent is dealing with sbp_start_device_evt.
*/
Osal_set_event (Simplebletest_taskid, sbp_start_device_evt);
}
This is the event handler function for our application
UInt16 simplebletest_processevent (uint8 task_id, uint16 events)
{
VOID task_id; Osal required parameter that isn ' t used in the This function
Sys_event_msg This is a system event, such as a key event Bluetooth read and write event processing, which will set this event
if (Events & Sys_event_msg)
{
Return unprocessed events
Return (events ^ sys_event_msg);
}
This is the custom event of our application, and the value of SBP_START_DEVICE_EVT is defined as 0x0001,
We can actually define 16 events, the first of which is defined in bits.
//
if (Events & SBP_START_DEVICE_EVT)
{
Halledset (Hal_led_1, hal_led_mode_on); Light led1.
Return to this and tell Osal that this practice you have dealt with
Return (events ^ sbp_start_device_evt);
}
Discard Unknown Events
return 0;
}
Sys_event_msg is the meaning of the system event

To this basic from the hardware layer and system layer are configured, and of course, our app and our own set up the task of processing, we download to SMARTRF and then compile, the board on the LED1 light indicates that the system started.

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.