Inter-Task Communication in the SparkFun ESP32 Thing with FreeRTOS

Advanced ESP32 FreeRTOS on the ESP32
8 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€65.14
List Price:  €93.07
You save:  €27.92
£55.73
List Price:  £79.62
You save:  £23.88
CA$95.61
List Price:  CA$136.60
You save:  CA$40.98
A$106.30
List Price:  A$151.87
You save:  A$45.56
S$94.64
List Price:  S$135.20
You save:  S$40.56
HK$546.91
List Price:  HK$781.33
You save:  HK$234.42
CHF 63.50
List Price:  CHF 90.72
You save:  CHF 27.21
NOK kr764.69
List Price:  NOK kr1,092.46
You save:  NOK kr327.77
DKK kr485.92
List Price:  DKK kr694.20
You save:  DKK kr208.28
NZ$117
List Price:  NZ$167.15
You save:  NZ$50.15
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,661.98
List Price:  ৳10,946.16
You save:  ৳3,284.17
₹5,839.65
List Price:  ₹8,342.71
You save:  ₹2,503.06
RM331.75
List Price:  RM473.95
You save:  RM142.20
₦86,437.65
List Price:  ₦123,487.65
You save:  ₦37,050
₨19,492.21
List Price:  ₨27,847.21
You save:  ₨8,355
฿2,575.56
List Price:  ฿3,679.53
You save:  ฿1,103.97
₺2,262.43
List Price:  ₺3,232.18
You save:  ₺969.75
B$357.76
List Price:  B$511.10
You save:  B$153.34
R1,296.01
List Price:  R1,851.52
You save:  R555.51
Лв127.38
List Price:  Лв181.98
You save:  Лв54.60
₩95,113.23
List Price:  ₩135,881.87
You save:  ₩40,768.63
₪260.11
List Price:  ₪371.60
You save:  ₪111.49
₱3,999.61
List Price:  ₱5,713.97
You save:  ₱1,714.36
¥10,715.43
List Price:  ¥15,308.41
You save:  ¥4,592.98
MX$1,185.45
List Price:  MX$1,693.57
You save:  MX$508.12
QR254.79
List Price:  QR364.01
You save:  QR109.21
P955.69
List Price:  P1,365.33
You save:  P409.64
KSh9,427.65
List Price:  KSh13,468.65
You save:  KSh4,041
E£3,355.67
List Price:  E£4,794.02
You save:  E£1,438.35
ብር3,989.43
List Price:  ብር5,699.43
You save:  ብር1,710
Kz58,616.62
List Price:  Kz83,741.62
You save:  Kz25,125
CLP$66,326.02
List Price:  CLP$94,755.52
You save:  CLP$28,429.50
CN¥506.51
List Price:  CN¥723.62
You save:  CN¥217.11
RD$4,049.59
List Price:  RD$5,785.38
You save:  RD$1,735.78
DA9,420.19
List Price:  DA13,457.99
You save:  DA4,037.80
FJ$157.70
List Price:  FJ$225.30
You save:  FJ$67.59
Q542.62
List Price:  Q775.21
You save:  Q232.58
GY$14,613.08
List Price:  GY$20,876.73
You save:  GY$6,263.64
ISK kr9,792.30
List Price:  ISK kr13,989.60
You save:  ISK kr4,197.30
DH706.05
List Price:  DH1,008.69
You save:  DH302.63
L1,239.86
List Price:  L1,771.31
You save:  L531.44
ден4,010.92
List Price:  ден5,730.13
You save:  ден1,719.21
MOP$562.15
List Price:  MOP$803.11
You save:  MOP$240.95
N$1,302.54
List Price:  N$1,860.85
You save:  N$558.31
C$2,571.43
List Price:  C$3,673.63
You save:  C$1,102.20
रु9,317.58
List Price:  रु13,311.40
You save:  रु3,993.82
S/262.81
List Price:  S/375.46
You save:  S/112.65
K268.53
List Price:  K383.63
You save:  K115.10
SAR262.51
List Price:  SAR375.03
You save:  SAR112.52
ZK1,879.71
List Price:  ZK2,685.42
You save:  ZK805.70
L324.19
List Price:  L463.14
You save:  L138.95
Kč1,629.65
List Price:  Kč2,328.17
You save:  Kč698.52
Ft25,373.17
List Price:  Ft36,248.95
You save:  Ft10,875.77
SEK kr758.75
List Price:  SEK kr1,083.98
You save:  SEK kr325.22
ARS$61,468.94
List Price:  ARS$87,816.53
You save:  ARS$26,347.59
Bs482.36
List Price:  Bs689.12
You save:  Bs206.75
COP$272,946.91
List Price:  COP$389,940.87
You save:  COP$116,993.96
₡35,623.88
List Price:  ₡50,893.45
You save:  ₡15,269.56
L1,732.95
List Price:  L2,475.75
You save:  L742.80
₲523,151.84
List Price:  ₲747,391.81
You save:  ₲224,239.96
$U2,683.09
List Price:  $U3,833.15
You save:  $U1,150.06
zł281.85
List Price:  zł402.67
You save:  zł120.81
Already have an account? Log In

Transcript

Into task communication in the sparkfun ESP 32 thing with free articles. In this video we will learn about queues in the ESP 32. Later, we will implement inter task communication with an example project. And finally, we will cover the need in application of queues when handling interrupts. In the last video, we learned about creating tasks and synchronizing them. In many real time systems.

Like the nuclear reactor example, we explained earlier, tasks need to communicate with each other into task communication is necessary for applications where we collect a large amount of data from sensors. In many cases, there will be multiple tasks handling multiple sensors, trying to communicate with the receiver task. The task that receives the data might not be available all the time. It might be busy receiving data from other sensors Thus the sensor reading will get lost. This is why we need a buffer in between the tasks to store the incoming data, so that it can be accessed later. Now, we will implement a small project with our sparkfun ESP 32 thing to teach you how to implement queues in your own project.

In this project, we will create two tasks and a queue. One is the producer task and the other is the consumer task. The producer will collect the sensor readings from the internal Hall Effect sensor and add these values to the end of the queue also known as the tail. The consumer task will read the value from the head of the queue and if the effective magnetic field is too high, the red LED will be on the project circular diagram looks like this. Basically, you have to remove the green LED and its accompanying current limiting resistor from the last project In this project, the Hall Effect sensor task will run on core zero and the LED blinking task will run on core one. A message queue will be created in the ram with a specified size.

There are basically three free Atos functions, you should be familiar to use queues for inter task communication in your projects. They are excu create, excuse send and excu receive the excu create function creates a new queue by using dynamic memory allocation to automatically allocate Ram. This function accepts two parameters. The first parameter is the length of the queue. It determines the maximum number of items the queue can hold. The second parameter is the size in bytes each queue item can hold.

Each item in the queue will be of the same size. They excuse and function We'll post an item at the end of the queue. This function accepts three parameters. The first parameter is the handle to the queue on which the item is to be posted. The second parameter is a pointer to the item that has to be placed on the queue. The final parameter is the maximum amount of time, the task should wait.

If the queue is already full. The execute receive function receives an item from the head of the queue. This function also accepts three parameters. The first parameter is the handle to the queue from which the item has to be received. Next is a pointer to the variable or buffer to which the received item or items will be copied. The final parameter is the maximum amount of time the task should wait for an item to receive if the queue is empty at the time of the call over the Arduino ID and last sensor underscore led underscore q.io no code file from the repository.

First we create two handles for the two tasks. One handle is also created for the queue, we created a variable to set the size of the queue, and also the pin number for the red LED. Inside the setup loop, the only new line of code that got added is this instruction, it creates the queue and assigns it to the queue handle. Here, the size of each item in the queue is set to the size of an integer as the whole sense outputs integer values. Also, don't forget to pin the tasks to separate course. Next, we have the function definitions of the producer task and the consumer task.

Inside the producer task, there is an infinite for loop that reads the whole sensor data and since then, one by one to the queue here, the Val is the pointer to the integer variable named Val. By putting port max underscore delay as a third parameter, the function will wait indefinitely if the queue is full. Inside the consumer task, there is yet another infinite for loop, in which we use the execute receive function. To access the elements stored in the queue, one by one. The sensor values pointer is assigned to the variable element by the end element parameter. Now the sensor value is stored in the variable element.

Later, we use conditional logic to detect the magnetic field the value will go below zero or about 25. If the magnetic field around it is too high, the direction it goes depends on the polarity of the magnetic field. Thus, we use a conditional OR operator to enable or disable the red LED Finally, the word blue is left empty. Now upload the code to the bold and bring a magnet near the ESP 32. You can see that the red LED is on. If you move it away, the LED will become off.

Through this simple example, you were able to implement a very important concept in free Otto's called kills. The concept of queues is very important, especially in applications where we use interrupts. an interrupt is an event that temporarily suspends the main program and passes the control to a special code called the interrupt service routine, also known as Asr, and after that presumes the main program flow where it had left off. Since the ASR doesn't accept any parameters or return values, conventionally, there is no way for our program to share data with the ISF program. For example, when using a keyboard, each keystroke generates an interrupt signal. A keyboard ASR code simply plays the character it reads into a global variable from which the program could read it.

However, this would provide no advantage over software polling. Since the ASR is only moving the data from the receiver data register to another location. It is still necessary for the program to read the character before it is overwritten. But say that the main program is busy with some other task, then this situation will lead to data loss, and some keystrokes entered won't be registered. Using free auto skills softens the requirements on the program to keep up with input. If the ions are places characters in a queue that can hold 25 characters, then 25 characters can be typed.

While the program is busy with other work and no data loss will occur. The program must To keep up with input, but it can check for input much less frequently, as long as it processes all the data in the queue before it overflows. If you want to know more about implementing kills for inter task communication between an ASR and your task, please check out the resources section. Summary. In this video we have covered the following topics queues for inter task communication, implementing queues in a project and queues for interrupt handling. In the next video, we will look at the performance comparison with free Otto's implementation in the ESP 32.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.