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

Sharing functions between two threads

Posted by dibosco on February 27, 2015

I've got a situation that is - to me at least - rather complicated. I have two interfaces that are bit-bashed lighting interfaces. There is a Tx pin and an Rx pin that are controlled by timer interrupts and pin change interrupts. They are Manchester encoded interfaces that need to act completely independently to each other.

That in itself is not so complicated; I have written code to do one interface that works nicely and I could duplicate this code using different a timer and a different pin change interrupt for the second interface.

However, what is not at all clear to me is how much code I can share in terms of sending messages in and out of the two interfaces. I will attempt to explain the kind of thing I mean.

The send message is like this, presented here in a simplified manner:

int16t SendMessage(uint8t Address, uint8t Data, uint8t ReceiveData) {

int16_t RetVal;
uint8_t Data;

// Here Address and Data variables are turned into a sixteen bit value to send out // This sixteen bit value is a static variable to this module

DisableReception(); // Disable Rx pin interrpt

// The timer bit-bashes the data and enables the Rx pin interrupt if necessary // when the sixteen bits have been sent out StartTimer();
if (ReceiveData == TRUE) { if (xQueueReceive(xRxQueue,Data,TIMEOUTVALUE) == pdTRUE) RetVal = (int16t)Data; // Pin int fills xRxQueue else return NODATARECEIVED; } else RetVal = NODATARETURN_REQUESTED;

return RetVal;

}

The xRxQueue is filled by the pin interrupt if a slave is [supposed to be] returning data.

This works a treat for the one interface. I can easily write a version for the second interface, to end up with two functions:

int16t SendMessageA(uint8t Address, uint8t Data, uint8t ReceiveData); int16t SendMessageB(uint8t Address, uint8t Data, uint8t ReceiveData);

The two functions would have their own RxQueue, use their own timer and own pin interrupt of course.

I have a commands.c/commands.h module which calls this SendMessage() function. Here is an example:

uint8t ProgramShortAddress(uint8t NewShortAddress) {

int16_t Reply;
uint8_t RetVal;

NewShortAddress <<= 1;
NewShortAddress |= 1;

Reply = SendMessage(PROGRAM_SHORT_ADDRESS, NewShortAddress, TRUE);

if (Reply == 0xFF)
    RetVal = TRUE;
else
    RetVal = FALSE;

return RetVal;

}

Now, I would like to mod this and all the other similar commands this so I pass an extra variable in to the function, which specifies which of the two interfaces I am addressing. It would become:

uint8t ProgramShortAddress(uint8t NewShortAddress, uint8_t Interface) {

// All stays the same inside the function except:

if (Interface == INTERFACE_A)
    Reply = SendMessageA(PROGRAM_SHORT_ADDRESS, NewShortAddress, TRUE);
else if (Interface == INTERFACE_B)
    Reply = SendMessageB(PROGRAM_SHORT_ADDRESS, NewShortAddress, TRUE);

}

So, here is the crux of the matter:

If I have two threads, one running for interface A and one for interface B, one calls ProgramShortAddress(), its time slice ends while in this function and the other interface calls ProgramShortAddress(), is this going to be an issue? It's not like having an SPI port and locking it out with a mutex as far as I can see, as it's going to talk to one of two interfaces. There would never be an occasion when two threads would try to access the same interface.

Does this make sense? Have I explained it clearly enough? Am I worrying over nothing or maybe I am tackling this in completely the wrong manner?

Many thanks.


Sharing functions between two threads

Posted by heinbali01 on February 27, 2015

If I have two threads, one running for interface A and one for interface B, one calls ProgramShortAddress(), its time slice ends while in this function and the other interface calls ProgramShortAddress(), is this going to be an issue

I don't see any problem with that. The task-switch may come at any moment and it will be safe.

The essence of a task-switch is saving and restoring a context. A context can be described as a set of registers, most importantly the PC (Program Counter) and the SP (Stack Pointer).

~~~~~ uint8t ProgramShortAddress(uint8t NewShortAddress, uint8_t Interface) { // All stays the same inside the function except:

/* Suppose a task switch comes here, just before testing the
variable "Interface" */
if (Interface == INTERFACE_A)
    Reply = SendMessageA(PROGRAM_SHORT_ADDRESS, NewShortAddress, TRUE);
else if (Interface == INTERFACE_B)
    Reply = SendMessageB(PROGRAM_SHORT_ADDRESS, NewShortAddress, TRUE);

~~~~~

Function parameters and automatic variables (variable on stack, declared within a function) are thread-safe and they're private to a task. Very often the parameters and automatic variables are optimised and stored directly in registers. That is no problem because all relevant registers will be stored and restored.

So: sharing function among tasks does not pose any problem.

I once had a device with 16 Manchester-encoded devices attached to it (wall panels) and took (had to take) a slightly different approach:

The baud-rate was 4800. Every bit is sent as two bits '10' or '01', so I needed at least a 9600 Hz Timer.

I stored live information about each of 16 connections in a structure. All devices were served by a single task.

I installed one Timer/Counter (TC) running at 3 * 2 * 4800 = 28.8 KHz.

The ISR of the TC did two things: it takes samples of the RX values and at every 3rd tick it may send a sample.

Normally the TC interrupt is disabled, unless any of the lines needs attention. Once the last message has been exchanged, the ISR goes off again.

Regards.


Sharing functions between two threads

Posted by dibosco on February 27, 2015

Smashing! That puts my mind at rest and will simplify the code a lot! Thanks again for such a clear explanation. :~)


[ 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