Why we must use a mutex to access the GLCD

How to do it…

To access the GLCD using mutual exclusion, follow the steps outlined:

  1. Create a new project and using the manager configure the RTE to provide support for the Graphic LCD.
  2. Create a new file named RTXBlinky.c, add the boilerplate code, and then add this source file to the project.
  3. Add the following code to RTXBlinky.c:
    #include "stm32f4xx_hal.h"        /* STM32F4xx Defs */
    #include "RTXBlinkyUtils.h"
    #include "cmsis_os.h"
    
    osThreadId tid_taskA;       /* id of thread: task_a */
    osThreadId tid_taskB;       /* id of thread: task_b */
    
    osMutexId mut_GLCD; /* Mutex to control GLCD access */
    
    /*--------------------------------------------------
     *      Switch LED on
     *--------------------------------------------------*/
    void switch_On (unsigned char led) {
    
      osMutexWait(mut_GLCD, osWaitForever);
      GLCD_SetBackgroundColor (GLCD_COLOR_WHITE);
      GLCD_SetForegroundColor(GLCD_COLOR_RED);
      GLCD_SetFont (&GLCD_Font_16x24);
      GLCD_DrawChar(led+(7*16), 4*24, 0x80+1); 
      osMutexRelease(mut_GLCD);
    }
    
    /*--------------------------------------------------
     *      Switch LED off
     *--------------------------------------------------*/
    void switch_Off (unsigned char led) {
    
      osMutexWait(mut_GLCD, osWaitForever);
      GLCD_SetBackgroundColor (GLCD_COLOR_WHITE);             
      GLCD_SetForegroundColor(GLCD_COLOR_RED);   
      GLCD_SetFont (&GLCD_Font_16x24);
      GLCD_DrawChar(led+(7*16), 4*24, 0x80+0);     
      osMutexRelease(mut_GLCD);
    }
    
    /*--------------------------------------------------
     *      Thread 1 'taskA': Switch LED ON
     *--------------------------------------------------*/
    void taskA (void const *argument) {
      for (;;) {
        osSignalWait(0x0001, osWaitForever);    
        switch_On(LED_A);
        osDelay(500);
        osSignalSet(tid_taskB, 0x0001); /* signal taskB */
      }
    }
    
    /*--------------------------------------------------
     *      Thread 2 'taskB': Switch LED OFF
     *--------------------------------------------------*/
    void taskB (void const *argument) {
      for (;;) {
        osSignalWait(0x0001, osWaitForever); 
        switch_Off(LED_A);
        osDelay(500);
        osSignalSet(tid_taskA, 0x0001); /* signal taskA */
      }
    }
    
    osMutexDef(mut_GLCD);
    
    osThreadDef(taskA, osPriorityNormal, __FI, 0);
    osThreadDef(taskB, osPriorityNormal, __FI, 0);
    
    /*--------------------------------------------------
     *      Main: Initialize and start RTX Kernel
     *--------------------------------------------------*/
    int main (void) {
    
      HAL_Init ();   /* Init Hardware Abstraction Layer */
      SystemClock_Config ();           /* Config Clocks */        
    
      GLCD_setup();
    
      mut_GLCD = osMutexCreate(osMutex(mut_GLCD));
    
      tid_taskA = osThreadCreate(osThread(taskA), NULL);
      tid_taskB = osThreadCreate(osThread(taskB), NULL);
    
      osSignalSet(tid_taskA, 0x0001);    /* signal taskA */
    
      osDelay(osWaitForever);
      while(1);
    }
  4. Create the RTXBlinkyUtils.c file, enter the following code, and add this to the project:
    #include "RTXBlinkyUtils.h"
    
    void GLCD_setup(void) {
    
      unsigned char led;
    
      GLCD_Initialize();               /* Initialize and */
      GLCD_SetBackgroundColor (GLCD_COLOR_WHITE);
      GLCD_ClearScreen ();             /* clear the GLCD */
      GLCD_SetBackgroundColor(GLCD_COLOR_BLUE);
      GLCD_SetForegroundColor(GLCD_COLOR_WHITE);
      GLCD_SetFont (&GLCD_Font_16x24);
      GLCD_DrawString(0, 0*24, " CORTEX-M4 COOKBOOK ");
      GLCD_DrawString(0, 1*24, "  PACKT Publishing  ");
      GLCD_SetBackgroundColor(GLCD_COLOR_WHITE);
      GLCD_SetForegroundColor(GLCD_COLOR_RED);
      for (led=LED_A; led<LED_G+1; led++)
          GLCD_DrawChar((led+7)*16, 4*24, 0x80+0); 
    }
  5. Modify RTXBlinkyUtils.h (defined in the previous recipe), accordingly.
  6. Build, download, and run the program.

How it works…

Calls to GLCD functions within switch_Off() and switch_On() are protected by mut_GLCD, thus enforcing mutual exclusion. The mut_GLCD variable is declared as follows:

osMutexId mut_GLCD;   /* Mutex to control GLCD access */

We also need to register the semaphore by including the following statement:

osMutexDef(mut_GLCD);

We initialize this statement within main() by including the following:

mut_sharedVar = osMutexCreate(osMutex(mut_GLCD));
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.99.71