Low Power Wireless and Zigbee Networking Workshop - Osal and Hal 6 - 1
Low Power Wireless and Zigbee Networking Workshop - Osal and Hal 6 - 1
Introduction
The purpose of this module is to introduce you to the concepts and operations of OSAL and HAL.
These basic software tools distance your code form the hardware, making code re-use and
debugging among other things, much easier.
Objectives
Review OSAL components and APIs
Low Power Wireless and ZigBee Networking Workshop - OSAL and HAL 6-1
Module Topics
*** this page left blank by order of the fire marshal ***
6-2 Low Power Wireless and ZigBee Networking Workshop - OSAL and HAL
Module Topics
Module Topics
OSAL and HAL......................................................................................................................................... 6-1
Low Power Wireless and ZigBee Networking Workshop - OSAL and HAL 6-3
Module Topics
6-4 Low Power Wireless and ZigBee Networking Workshop - OSAL and HAL
Why Use an OS?
Stack App
App Hardware Platform
App
OSAL/HAL ...
2
OSAL / HAL
OSAL provides scheduling, memory management and messaging
features.
HAL provides easy programming access to hardware and isolates
the software from the hardware specifics
Z-Stack Application(s)
OSAL APIs
OSAL
HAL ISR
Hardware Platform
Because of its very limited functionality, the OSAL is
not, strictly speaking, considered an Operating System
OSAL = Operating System Abstraction Layer
HAL = Hardware Abstraction Layer
Low Power Wireless and ZigBee Networking Workshop - OSAL and HAL 6-5
Tasks
Tasks
Tasks, Events and Messages
Event …
An occurrence used to
trigger a task to run
Task …
A complete piece of code …
A thread
Message …
Event … Information exchanged from
An occurrence used to one Task to another
trigger a task to run
Task …
A complete piece of code …
A thread
Tasks ...
4
Tasks
A single thread of code that performs a function can be
encompassed in a task
A task is made up of an initialization section and a run-time section
Once started, a task runs to completion
An event causes the run-time section to execute
6-6 Low Power Wireless and ZigBee Networking Workshop - OSAL and HAL
Tasks
Task APIs
osal_set_event()
sets the event flags for a task – runs task
osal_init_system()
creates the tasks defined in the task table
osal_start_system()
starts the OSAL main loop
osal_self()
returns the ID of the current task
Events ...
6
Low Power Wireless and ZigBee Networking Workshop - OSAL and HAL 6-7
Tasks
Messaging ...
7
6-8 Low Power Wireless and ZigBee Networking Workshop - OSAL and HAL
Tasks
Scheduling
Scheduling Example
post TSK1 return post TSK2 return
HWI TSK2
completion
TSK 2
interrupt
TSK 1
TSK1
completion
main()
return interrupt
IDL
Low Power Wireless and ZigBee Networking Workshop - OSAL and HAL 6-9
Callback and Interrupts
// Register for all key events - This app will handle all key events
RegisterForKeys( GenericApp_TaskID );
CODE for handling it is in process event.
case KEY_CHANGE:
GenericApp_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
break;
Interrupts ...
10
Interrupts
osal_int_enable()
enable identified interrupt
osal_int_disable()
disable identified interrupt
6 - 10 Low Power Wireless and ZigBee Networking Workshop - OSAL and HAL
Timers, Clock and Memory
Memory APIs
Standard C malloc() and free() operations have little
knowledge of hardware specifics
OSAL provides the same and extended functions for the
user to manage dynamic memory from the heap
These functions should be used exclusively
osal_mem_alloc()
dynamically allocates a buffer
osal_mem_free()
returns allocated buffer to heap
NV Memory ...
13
Low Power Wireless and ZigBee Networking Workshop - OSAL and HAL 6 - 11
NV Memory and Power
osal_nv_item_init()
init an item in non-volatile memory
osal_nv_read()
read item
osal_nv_write()
write item
osal_offsetof()
calculate memory offset
Power ...
14
Power Management
Notifies OSAL when it’s safe to turn off the receiver, external
hardware and put the MCU to sleep
The device can be set to always_on or battery power
OSAL determines whether to sleep based on both the task and
device state
Task default state is to conserve power
You must use POWER_SAVING complier flag to use this feature
osal_pwrmgr_state()
changes or sets the devices power savings mode
osal_pwrmgr_task_state()
change a task’s power state
6 - 12 Low Power Wireless and ZigBee Networking Workshop - OSAL and HAL
HAL API’s
HAL API’s
HAL APIs
The Hardware Abstraction Layer offers these services:
ADC
LCD
LED
KEY
SLEEP
TIMER
UART
PA/LNA
Supporting files: hal.h, onboard.c and onboard.h
See Z-Stack HAL Porting Guide (SWRA199) and Z-Stack
HAL Driver API Guide (SWRA193) to port the HAL to your
target hardware
ADC ...
17
Low Power Wireless and ZigBee Networking Workshop - OSAL and HAL 6 - 13
HAL API’s
HalAdcInit()
initializes ADC
HalAdcRead()
reads value from specified channel at specified resolution
HAL_ADC_CHANNEL_0
. HAL_ADC_RESOLUTION_8
. HAL_ADC_RESOLUTION_10
HAL_ADC_RESOLUTION_12
. HAL_ADC_RESOLUTION_14
HAL_ADC_CHANNEL_7
LCD ...
18
LED ...
19
6 - 14 Low Power Wireless and ZigBee Networking Workshop - OSAL and HAL
HAL API’s
HAL_LED_1
Note: This is a good example HAL_LED_2
of simple GPIO for your
target board code. HAL_LED_3
HAL_LED_4 KEY ...
20
SLEEP ...
21
Low Power Wireless and ZigBee Networking Workshop - OSAL and HAL 6 - 15
HAL API’s
HalSleep()
sets the low power mode of the MAC
HalSleepWait()
performs a blocking wait
TIMER ...
22
HalTimerInit()
initializes timers with specified parameters
HalTimerConfig()
configures channels in different modes
HalTimerStart()
starts the specified timer
HalTimerStop()
stops the specified timer
HalTimerTick()
used for timer polling
HalTimerInterruptEnable()
enables/disables specified timer interrupt
UART ...
23
6 - 16 Low Power Wireless and ZigBee Networking Workshop - OSAL and HAL
HAL API’s
HAL_PA_LAN_RX_LGM()
sets RX low gain mode
HAL_PA_LNA_RX_HGM()
sets RX high gain mode
25
Low Power Wireless and ZigBee Networking Workshop - OSAL and HAL 6 - 17
HAL API’s
6 - 18 Low Power Wireless and ZigBee Networking Workshop - OSAL and HAL