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

Best way to pass data from interrupt

Posted by Willem on March 19, 2009
I use the pic32.

I've setup the ADC to capture automatically and store the ADC data during the ADC interrupt in a structure.
This works, no problem.

Now I need the ADC data (structure) available to some variety of tasks. What is the best way to do this?
The tasks do not need the data periodically, more randomly.
Was thinking to use sendQueueFromIsr to send the structure to a queue.

Then i thought to create a function that only peeks and returns a variable from the structure.
So a task that need an information just calls the right function.

But I don't know if this solution is safe, anyone? Or are there better ways?

This is the data that gets filled during the interrupt and that i need to have available to the other tasks:

xAdcData.usBATT_24 = ADC1BUF0;
xAdcData.usTEMP= ADC1BUF1;
xAdcData.usCURR= ADC1BUF2;




RE: Best way to pass data from interrupt

Posted by Willem on March 19, 2009
Or is it safe to do just this: (it could be called by multiple tasks)

unsigned portSHORT getPowerVoltage(void)
{
return xAdcData.usBATT_24;
}

RE: Best way to pass data from interrupt

Posted by MEdwards on March 19, 2009
Your data is 24bits (although your function only returns 16?) and the word size of your microcontroller is 32 bits. As long as you only ever write to the 32bit value in one go then you are safe. So in the interrupt fill the 24bit value byte by byte as you are, then copy this as a single 32bit value into a 32bit variable. Your function can then just return the 32bit value.

The other but less desirable alternative is to use a critical section something like:

unsigned long getPowerVoltage(void)
{
unsigned long returnedValue;

taskENTER_CRITICAL();
returnedValue=xAdcData.usBATT_24;
taskEXIT_CRITICAL();

return returnedValue;
}

This second solution is ok if your tasks only read the value occasionally. Don't use it if they continuously read the value.

RE: Best way to pass data from interrupt

Posted by Willem on March 19, 2009
Thanks for the reply.

I did not show that the structure variables are also defined as portSHORT. I did this
because the resolution of the ADC is 10bits.

But if i understand correctly, i can better make it 32bit variables (unsigned int for example),
and then i will have to do it like this:

for the interrupt, where the variables gets filled:
xAdcData.usCURR = ADC1BUF0;

for the return, where the data gets read out by some tasks:
return xAdcData.usCURR;

where xAdcData.usCURR is a 32 bit variable. Can i then be sure that there will not be any
problems?

Just to make sure, a portSHORT will that too be an atomic read instruction? or only 32bit lengths?

RE: Best way to pass data from interrupt

Posted by Willem on March 19, 2009
When i go into disassembly listing, then i see 4 instructions that gets executed
when using portLONG (32bit) (when writing in the interrupt) :

lui v1,0xa000
lui v0,0xbf81
lw v0,-28560(v0)
sw v0,31072(v1)

and 5 when using portSHORT(16bit)

lui v0,0xa000
addiu v1,v0,31072
lui v0,0xbf81
lw v0,-28544(v0)
sh v0,4(v1)

And when reading, the return uses 3 instructions with portLONG:

lui v0,0xa000
lw v0,31072(v0)
andi v0,v0,0xffff

And 4 instructions when portSHORT:

lui v0,0xa000
addiu v0,v0,31072
lhu v0,6(v0)
andi v0,v0,0xffff

But in all cases, i`m not that good with asm, I see the address from the variable only once.
So can I than say it is an atomic instruction?





[ 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