[SOLVED] FREERTOS 9 - How do I write code for this?

Post Reply
acronis
Posts: 122
Joined: Thu Mar 02, 2017 12:32 pm

[SOLVED] FREERTOS 9 - How do I write code for this?

Post by acronis » Wed Aug 02, 2017 7:17 am

STM32F407VET6 BLACK + TFT DISPLAY + RTC

Hello. Doing my very first project on FreeRTOS 9.
Did for example two absolutely identical tasks - displays the date and time on the screen - but the controller starts to work and freezes.
If you leave one problem - everything works fine.
Tell me what I have wrong ?

Code: Select all


#include <MapleFreeRTOS900.h>                                     
#include <Adafruit_TFTLCD_16bit_STM32.h>                           
#include <RTClock.h>                                              
Adafruit_TFTLCD_16bit_STM32 tft;                                  
RTClock rtc;                                                       
time_t tt;                                                        
time_t ttt;                                                        
tm_t tm;                                                         

int hh;                                                            
int mm;                                                            
int ss;                                                           
int sss;                                                          
int dd;                                                            
int mmm;                                                           
int yy;                                                            



//------------------------------------------------------------------------------
void TimeDisplay(void *pvParameters) {                          
  for (;;) {
    tt = rtc.getTime();                                           
    rtc.getTime(tm);                                             
    if (tt != ttt)                                               
    {
      tft.fillScreen(BLACK);
      tft.setCursor(0, 0);
      showTime ();
      tft.print(" ");
      showDate ();
      tft.println("");
      ttt = tt;
    }
  }
}
//------------------------------------------------------------------------------



//------------------------------------------------------------------------------
void TimeDisplay2(void *pvParameters) {                             

  for (;;) {

          
    tt = rtc.getTime();                                           
    rtc.getTime(tm);                                               
    if (tt != ttt)                                                
    {
      tft.fillScreen(BLACK);
      tft.setCursor(0, 0);
      showTime ();
      tft.print(" ");
      showDate ();
      tft.println("");
      ttt = tt;
    }
  }
}
//------------------------------------------------------------------------------



//------------------------------------------------------------------------------
void setup() {

  
  StartsDisplay ();                                           

  xTaskCreate(TimeDisplay,                                   
              "Task1",
              configMINIMAL_STACK_SIZE,
              NULL,
              tskIDLE_PRIORITY + 2,
              NULL);


  xTaskCreate(TimeDisplay2,                                   
              "Task2",
              configMINIMAL_STACK_SIZE,
              NULL,
              tskIDLE_PRIORITY + 2,
              NULL);
          

  vTaskStartScheduler();                                      
}
//------------------------------------------------------------------------------



//------------------------------------------------------------------------------
void loop() {

}
//------------------------------------------------------------------------------



//------------------------------------------------------------------------------
void StartsDisplay ()  
{
  tft.reset();
  tft.begin(0x9341);
  tft.cp437(true);
  tft.setTextSize(1);
  tft.setRotation(1); 
  tft.fillScreen(BLACK);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);

}
//------------------------------------------------------------------------------



//------------------------------------------------------------------------------

void showTime ()
{
  hh = tm.hour;
  mm = tm.minute;
  ss = tm.second;

  if (hh < 10)
    tft.print('0');
  tft.print(hh);
  tft.print(":");

  if (mm < 10)
    tft.print('0');
  tft.print(mm);
  tft.print(":");

  if (ss < 10)
    tft.print('0');
  tft.print(ss);
}
//------------------------------------------------------------------------------



//------------------------------------------------------------------------------
void showDate ()
{
  dd = tm.day;
  mmm = tm.month;
  yy = tm.year + 1970;

  if (dd < 10)
    tft.print('0');
  tft.print(dd);
  tft.print(".");

  if (mmm < 10)
    tft.print('0');
  tft.print(mmm);
  tft.print(".");

  if (yy < 10)
    tft.print('0');
  tft.print(yy);
}
//------------------------------------------------------------------------------





One task example works fine

Code: Select all




#include <MapleFreeRTOS900.h>                                     
#include <Adafruit_TFTLCD_16bit_STM32.h>                           
#include <RTClock.h>                                              
Adafruit_TFTLCD_16bit_STM32 tft;                                  
RTClock rtc;                                                       
time_t tt;                                                        
time_t ttt;                                                        
tm_t tm;                                                         

int hh;                                                            
int mm;                                                            
int ss;                                                           
int sss;                                                          
int dd;                                                            
int mmm;                                                           
int yy;                                                            



//------------------------------------------------------------------------------
void TimeDisplay(void *pvParameters) {                          
  for (;;) {
    tt = rtc.getTime();                                           
    rtc.getTime(tm);                                             
    if (tt != ttt)                                               
    {
      tft.fillScreen(BLACK);
      tft.setCursor(0, 0);
      showTime ();
      tft.print(" ");
      showDate ();
      tft.println("");
      ttt = tt;
    }
  }
}
//------------------------------------------------------------------------------



//------------------------------------------------------------------------------
void setup() {

  
  StartsDisplay ();                                           

  xTaskCreate(TimeDisplay,                                   
              "Task1",
              configMINIMAL_STACK_SIZE,
              NULL,
              tskIDLE_PRIORITY + 2,
              NULL);


          

  vTaskStartScheduler();                                      
}
//------------------------------------------------------------------------------



//------------------------------------------------------------------------------
void loop() {

}
//------------------------------------------------------------------------------



//------------------------------------------------------------------------------
void StartsDisplay ()  
{
  tft.reset();
  tft.begin(0x9341);
  tft.cp437(true);
  tft.setTextSize(1);
  tft.setRotation(1); 
  tft.fillScreen(BLACK);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);

}
//------------------------------------------------------------------------------



//------------------------------------------------------------------------------

void showTime ()
{
  hh = tm.hour;
  mm = tm.minute;
  ss = tm.second;

  if (hh < 10)
    tft.print('0');
  tft.print(hh);
  tft.print(":");

  if (mm < 10)
    tft.print('0');
  tft.print(mm);
  tft.print(":");

  if (ss < 10)
    tft.print('0');
  tft.print(ss);
}
//------------------------------------------------------------------------------



//------------------------------------------------------------------------------
void showDate ()
{
  dd = tm.day;
  mmm = tm.month;
  yy = tm.year + 1970;

  if (dd < 10)
    tft.print('0');
  tft.print(dd);
  tft.print(".");

  if (mmm < 10)
    tft.print('0');
  tft.print(mmm);
  tft.print(".");

  if (yy < 10)
    tft.print('0');
  tft.print(yy);
}
//------------------------------------------------------------------------------




Two absolutely identical tasks made specially - for example, in the project I have different tasks will

User avatar
Pito
Posts: 1531
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: FREERTOS 9 - First draft , does not work

Post by Pito » Wed Aug 02, 2017 7:47 am

In an preemptive RTOS, when you are accessing a single "resource" (ie. the RTC, I2C, SPI, UART, GPIO, etc) from two or several independent tasks, you must use mutexes to allocate the resource for the specific task.
The tasks compete for the resources in an RTOS.

http://www.freertos.org/CreateMutex.html
Last edited by Pito on Wed Aug 02, 2017 7:58 am, edited 2 times in total.
Pukao Hats Cleaning Services Ltd.

acronis
Posts: 122
Joined: Thu Mar 02, 2017 12:32 pm

Re: FREERTOS 9 - First draft , does not work

Post by acronis » Wed Aug 02, 2017 7:51 am

Could You show how to do it in my example ?

User avatar
Pito
Posts: 1531
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: FREERTOS 9 - First draft , does not work

Post by Pito » Wed Aug 02, 2017 8:07 am

Not easy with your example. You have taken 2 complete sketches and you want run them as two independent programs.
Most probably you have to reorganize your tasks. You are using TFT, SERIAL, RTC as the shared resources.
You must use a special semaphore - a mutex, to indicate to the other tasks "the resource is locked by me, I am using it now"..
I would highly recommend you to start with simple examples, step by step, while learning the basic principles.. Do not try to run whole sketches as tasks, it will not work..
Pukao Hats Cleaning Services Ltd.

acronis
Posts: 122
Joined: Thu Mar 02, 2017 12:32 pm

Re: FREERTOS 9 - First draft , does not work

Post by acronis » Wed Aug 02, 2017 8:24 am

I looked on the Internet, I can not understand how it works.
Could you show any example of any two tasks, such as blinking the led ?
myself I'll be a long time to understand , and the example will quickly understand.
Thanks in advance


That would be two tasks using the mutex blinked the led ? Or is it possible ?

User avatar
Pito
Posts: 1531
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: FREERTOS 9 - First draft , does not work

Post by Pito » Wed Aug 02, 2017 8:36 am

Pukao Hats Cleaning Services Ltd.

acronis
Posts: 122
Joined: Thu Mar 02, 2017 12:32 pm

Re: FREERTOS 9 - First draft , does not work

Post by acronis » Wed Aug 02, 2017 8:47 am

Thank you.

User avatar
Pito
Posts: 1531
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: FREERTOS 9 - First draft , does not work

Post by Pito » Wed Aug 02, 2017 9:46 am

Examples: https://github.com/greiman/FreeRTOS-Ard ... ple015.ino

Example015 is about mutex.
Pukao Hats Cleaning Services Ltd.

victor_pv
Posts: 1654
Joined: Mon Apr 27, 2015 12:12 pm

Re: FREERTOS 9 - First draft , does not work

Post by victor_pv » Wed Aug 02, 2017 1:09 pm

There are examples included that rotate cubes in the screen,they use mutexes for sharing access to the spi peripheral, check the examples included in our core, either in the freertos9 or the freertos821 folders.

acronis
Posts: 122
Joined: Thu Mar 02, 2017 12:32 pm

Re: FREERTOS 9 - First draft , does not work

Post by acronis » Thu Aug 03, 2017 2:05 am

Pito, victor_pv, RogerClark !

Thank You very much You help me a lot in learning the beautiful STM32F4 microcontroller. I am very grateful !

stevestrong You special gratitude and many thanks, your help is priceless !

You guys are great and I'm so glad to have such a wonderful team that never throws beginners !

Post Reply