Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Real time embedded FreeRTOS mailing list 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT Training




Loading

Example12 in Freertos LPC17xx Practical Guide

Posted by jinx on April 13, 2010
Hi
I was looking at Example 12 that shows the implementation of using a binary semaphore to synchronize a Task with an Interrupt.

In the vHandlerTask, it is not clear to me as to why we need to take the semaphore before the infinite loop. I read the explanation and it is not clear...I would have let it suffice with the 'forever block' for xSemaphoreTake inside the infinite loop.

RE: Example12 in Freertos LPC17xx Practical Guide

Posted by Richard Damon on April 13, 2010
Semaphores start in the "Ready" state, so the first take will automatically succeed. If you didn't take once outside the loop, then the first time through the loop, the take would succeed, even if the interrupt hasn't happened yet.

RE: Example12 in Freertos LPC17xx Practical Guide

Posted by Richard on April 13, 2010
The binary semaphore can either be 'full' or 'empty' (hence binary). When it is full a call to xSemaphoreTake() will pass. When it is empty a call to xSemaphoreTake() will fail because there is no semaphore to take.

In this example the semaphore is used to signal events, in effect to synchronise a task with an interrupt. The interrupt gives the semaphore, to make the semaphore full, and in so doing unblocks the task. The task, once unblocked, takes the semaphore to make the semaphore empty once again. It then goes around the loop to call xSemaphoreTake() again, at which point it will once again enter the Blocked state to wait for the next interrupt.

However, the semaphore is created to already be in the full state. If the task did not take the semaphore before entering the infinite loop the first call to xSemaphoreTake() would therefore pass - even though no interrupts had yet occurred. The first call to xSemaphoreTake() (outside the loop) ensures the semaphore is empty before the infinite loop is entered - the semaphore should be empty because no interrupts have yet occurred. Now the first call to xSemaphoreTake() will cause the task to enter the Blocked state, because the semaphore is already empty.

Hope this helps your understanding and does not cloud it even more :o)

Regards.

RE: Example12 in Freertos LPC17xx Practical Guide

Posted by Richard on April 13, 2010
Now why did I feel the need to write four paragraphs, when richard_damon managed to say the same thing in two lines?


RE: Example12 in Freertos LPC17xx Practical Guide

Posted by jinx on April 13, 2010
It is crystal clear now. Thanks for taking the time to explain!


[ Back to the top ]    [ About FreeRTOS ]    [ Sitemap ]    [ ]




Copyright (C) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd.. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.

Latest News:

FreeRTOS V9.0.0 is now available for download.


Free TCP/IP and file system demos for the RTOS


Sponsored Links

⇓ Now With No Code Size Limit! ⇓
⇑ Free Download Without Registering ⇑


FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Renesas Electronics Gold Alliance RTOS Partner.jpg

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Atmel RTOS partner supporting ARM Cortex-M3 and AVR32 microcontrollers

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Xilinx Microblaze and Zynq partner

Silicon Labs low power RTOS partner

Altera RTOS partner for Nios II and Cortex-A9 SoC

Freescale Alliance RTOS Member supporting ARM and ColdFire microcontrollers

Infineon ARM Cortex-M microcontrollers

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

Cypress RTOS partner supporting ARM Cortex-M3

Fujitsu RTOS partner supporting ARM Cortex-M3 and FM3

Microsemi (previously Actel) RTOS partner supporting ARM Cortex-M3

Atollic Partner

IAR Partner

Keil ARM Partner

Embedded Artists