I'm currently going through FreeRTOS documentation, and I'm a bit puzzled about how two tasks can exchange data. It appears that mutexes and semaphores are primarily used to protect resources. They will protect resources but doesn't share data I've noticed that message queues are used for data exchange between tasks, but I'm wondering if this is the only way tasks can share data? if no than what are other methods?
See This Thread.
Have you seen:
In particular:
In there, you have:
- Queues (Chapter 4)
‘Queues’ provide a task-to-task, task-to-interrupt, and interrupt-to-task communication
mechanism.
- Task Notifications (Chapter 9)
For specific FreeRTOS discussions, there is a forum:
I am trying to understand how many tasks needs to create with priority, inter-task communication, synchronization and resources management with below example.
A "Two-Way Roadway with a Signalized Pedestrian Crossing" is like a typical road where traffic moves in both directions, and it has designated pedestrian crossing signals for safety.
When the traffic light for vehicles is green, it's a signal for vehicles to go. During this time, vehicles can proceed through the crossing, but pedestrians should not be on the road.
When the traffic light turns red, it means vehicles must come to a complete stop. This stop is necessary to allow pedestrians to safely cross the road.
For pedestrians, they should wait on the sidewalk until the pedestrian signal shows a "walk" or green figure. This is their cue to cross safely.
However, when the pedestrian signal shows a "don't walk" or a red hand symbol, pedestrians should not attempt to cross. Waiting for the green signal ensures their safety.
In terms of vehicle movement, it typically takes around 20 milliseconds to start moving, 3 milliseconds to slow down, and 20 milliseconds to come to a full stop when the traffic light turns red. This sequence helps maintain safety and order on the road.
We can create two tasks
- Traffic Light Control Task (High Priority): Operates on a 20-second cycle for "Go" and "Stop."
- Pedestrian Signal Control Task (Lower Priority): Cycles every 20 seconds to manage pedestrian signals effectively.
How does two tasks communicate and what synchronization mechanism need to protect resources?
I know there is no need to use RTOS it can done without RTOS
When is our homework due?
Not homework; I've already completed it with a PIC microcontroller. Now, I'm interested in redesigning it using an RTOS
#include <xc.h>
#include <stdio.h>
// Define hardware pins for vehicle and pedestrian lights
#define RED_PIN_1 LATB0
#define GREEN_PIN_1 LATB1
#define YELLOW_PIN_1 LATB2
#define RED_PIN_2 LATB3
#define GREEN_PIN_2 LATB4
#define YELLOW_PIN_2 LATB5
#define PEDESTRIAN_CROSS_BLUE LATB6
#define PEDESTRIAN_DONT_CROSS_RED LATB7
// Define states for the traffic lights (common for both directions)
enum VehicleLightState {
VEHICLE_STOP_STATE,
VEHICLE_GO_STATE,
VEHICLE_SLOW_DOWN_STATE
};
enum VehicleLightState currentVehicleLightState = VEHICLE_STOP_STATE;
enum PedestrianLightState {
PED_STOP_STATE,
PED_GO_STATE
};
enum PedestrianLightState currentPedestrianLightState = PED_STOP_STATE;
volatile unsigned int elapsedTime = 0;
// Function to initialize hardware pins and interrupt
void initializeHardware() {
// Set pins as outputs
TRISB = 0x00;
LATB = 0x00; // Clear all pins
// Set initial states for the lights (common for both directions)
RED_PIN_1 = 1;
YELLOW_PIN_1 = 0;
GREEN_PIN_1 = 0;
RED_PIN_2 = 1;
YELLOW_PIN_2 = 0;
GREEN_PIN_2 = 0;
PEDESTRIAN_CROSS_BLUE = 1;
PEDESTRIAN_DONT_CROSS_RED = 0;
ANCON0 = 0x00;
ANCON1 = 0x00;
INTCON = 0;
INTCONbits.GIE = 1; // Enable global interrupts
INTCONbits.PEIE = 1; // Enable peripheral interrupts
}
// Function to initialize Timer2
void initializeTimer2(void) {
T2CONbits.T2OUTPS = 0b0000;
T2CONbits.TMR2ON = 1;
T2CONbits.T2CKPS = 0b10;
PR2 = 124;
PIE1bits.TMR2IE = 1; // Enable Timer2 interrupt
}
void main(void) {
initializeHardware();
initializeTimer2();
while (1) {
// Temporarily disable global interrupts
INTCONbits.GIE = 0;
unsigned int tempElapsedTime = elapsedTime;
// Re-enable global interrupts
INTCONbits.GIE = 1;
// Update vehicle and pedestrian light states together
switch (currentVehicleLightState) {
case VEHICLE_STOP_STATE:
if (tempElapsedTime >= 20000) {
// Transition to VEHICLE_GO_STATE
currentVehicleLightState = VEHICLE_GO_STATE;
currentPedestrianLightState = PED_GO_STATE;
INTCONbits.GIE = 0;
elapsedTime = 0;
INTCONbits.GIE = 1;
// Update the lights
RED_PIN_1 = 0;
GREEN_PIN_1 = 1;
YELLOW_PIN_1 = 0;
RED_PIN_2 = 0;
GREEN_PIN_2 = 1;
YELLOW_PIN_2 = 0;
// Update the pedestrian lights
PEDESTRIAN_CROSS_BLUE = 0;
PEDESTRIAN_DONT_CROSS_RED = 1; // Pedestrians should not cross during the vehicle "Go" phase
}
break;
case VEHICLE_GO_STATE:
if (tempElapsedTime >= 20000) {
// Transition to VEHICLE_SLOW_DOWN_STATE
currentVehicleLightState = VEHICLE_SLOW_DOWN_STATE;
currentPedestrianLightState = PED_STOP_STATE;
INTCONbits.GIE = 0;
elapsedTime = 0;
INTCONbits.GIE = 1;
// Update the lights
RED_PIN_1 = 0;
YELLOW_PIN_1 = 1;
GREEN_PIN_1 = 0;
RED_PIN_2 = 0;
YELLOW_PIN_2 = 1;
GREEN_PIN_2 = 0;
// Update the pedestrian lights
PEDESTRIAN_CROSS_BLUE = 0;
PEDESTRIAN_DONT_CROSS_RED = 1; // Pedestrians can't cross as it's not safe
}
break;
case VEHICLE_SLOW_DOWN_STATE:
if (tempElapsedTime >= 3000) {
// Transition to VEHICLE_STOP_STATE
currentVehicleLightState = VEHICLE_STOP_STATE;
INTCONbits.GIE = 0;
elapsedTime = 0;
INTCONbits.GIE = 1;
// Update the lights
RED_PIN_1 = 1;
YELLOW_PIN_1 = 0;
GREEN_PIN_1 = 0;
RED_PIN_2 = 1;
YELLOW_PIN_2 = 0;
GREEN_PIN_2 = 0;
// Update the pedestrian lights
PEDESTRIAN_CROSS_BLUE = 1;
PEDESTRIAN_DONT_CROSS_RED = 0; // Pedestrians can now cross as it's safe
}
break;
}
}
}
// Interrupt service routine for Timer2
void __interrupt() ISR(void) {
PIR1bits.TMR2IF = 0; // Clear Timer2 interrupt flag
elapsedTime++;
}
I specially choose the topic 'traffic lights system' to apply an RTOS concepts in project because I saw in many places it's a good use case many recommend. I did it first with bare metal code and trying to do it with RTOS based
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.