Issue with threading setup

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderators: barthess, RoccoMarco

Posts: 12
Joined: Tue Oct 01, 2013 8:50 am

Issue with threading setup

Postby dhruvin91 » Tue Oct 29, 2013 8:51 am


I am attempting to create a thread which would execute functions from the gfx library. What is happening is that when I run the functions in the main thread everything executes correctly, as soon as I implement this code in a static thread the system hangs. Initially while in normal priority nothing would display, and then when I gave the thread high priority it would display a frame. However after displaying the frame the system hangs and is not capable of taking in touch input. I am unsure of if this is based on the way I am implementing the thread or is it because of what the thread is responsible for (ugfx functions).

Below is my main file which initializes and calls the thread, "mythread".

Code: Select all

#include "ch.h"
#include "hal.h"
#include "gfx.h"
#include "nfc.h"
#include "test.h"
#include "string.h"
#include <stdio.h>
#include "GUI.h"

static GHandle            ghAccounts,ghExit,ghConsole;
static GListener         gl;
static GEvent               *pe;

static void createWidgets(void) {
   // Apply the console parameters   
   uint8_t width = 192;
   uint8_t height = 200;
   uint8_t y = 100;
   uint8_t x = 20;
   font_t font = gdispOpenFont("Small Narrow");
   // Create the actual console
   ghConsole = gwinCreateConsole(NULL,x,y,width,height,font);
   // Set the console fore- and background colors
   gwinSetColor(ghConsole, Yellow);
   gwinSetBgColor(ghConsole, Black);
   // Clear the console

void ghConsoleprint(const char *fmt){
   gwinPrintf(ghConsole, fmt) ;

void GUI_init(void) {
    //GSourceHandle           gs;
    /* initialize the LCD */

    /* Initialize the mouse -- mouse refers to touch */
    //gs = ginputGetMouse(0);
void testScreen(void){

   //coord_t                 swidth, sheight;
   //GHandle                 ghc;
   font_t                  font;
   GButtonObject           gAccounts;
   GButtonObject           gExit;
   GSourceHandle           gsAccounts, gsExit;

    /* Get the display dimensions */
   //swidth = gdispGetWidth();
   //sheight = gdispGetHeight();
   ghAccounts = ghExit = 0;

   /* choose our font */
   font = gdispOpenFont("Larger Double");
   /**change position of objects based on swidth and sheight**/
   /* create the "exit" button */
   ghExit = gwinCreateButton(&gExit, 10, 50, 100, 50, font, GBTN_NORMAL);
   gwinSetButtonText(ghExit, "Exit", FALSE);
   gsExit = gwinGetButtonSource(ghExit);
   geventAttachSource(&gl, gsExit, 0);
   gwinAttachButtonMouse(ghExit, 0);

   /* create the "Accounts" button */
   ghAccounts = gwinCreateButton(&gAccounts, 130, 50, 100, 50, font, GBTN_NORMAL);
   gwinSetButtonText(ghAccounts, "Accounts", FALSE);
   gsAccounts = gwinGetButtonSource(ghAccounts);
   geventAttachSource(&gl, gsAccounts, 0);
   gwinAttachButtonMouse(ghAccounts, 0);

   /* draw the buttons */

   // Create the widget
   GEventGWinButton                *peb;
       pe = geventEventWait(&gl, TIME_INFINITE);
       if(pe->type == GEVENT_GWIN_BUTTON){

       peb = (GEventGWinButton *)pe;
       if (peb->button == ghAccounts) { gwinPrintf(ghConsole, "Accounts\r\n");}
       else if (peb->button == ghExit) { gwinPrintf(ghConsole, "Exit\r\n");}
       //else {return BTN_NONE;}


static WORKING_AREA(myThreadWorkingArea, 100000);

static msg_t myThread(void *arg) {
  uint8_t buttonpress=0;

  while(TRUE) {
     ghConsoleprint("Welcome to the G");
          ghConsoleprint("WIN c");
          if (buttonpress == BTN_ACCOUNTS) {
              palSetPad(GPIOD, GPIOD_LED3);       /* Orange*/
              ghConsoleprint("Welcome to the GWIN console!\r\n");
          else if (buttonpress == BTN_EXIT)
              palClearPad(GPIOD, GPIOD_LED3);     /* Orange*/


int main(void) {

   /* initialize the hardware and the OS */

                                    HIGHPRIO,    /* Initial priority.    */
                                    myThread,      /* Thread function.     */

   return 0;

User avatar
Site Admin
Posts: 12949
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 733 times
Been thanked: 609 times

Re: Issue with threading setup

Postby Giovanni » Tue Oct 29, 2013 9:14 am


You should not return from the main() function, instead enter a loop with a sleep inside, see the various demos.


User avatar
Posts: 1226
Joined: Thu May 10, 2012 9:50 am
Location: Switzerland

Re: Issue with threading setup

Postby Tectu » Tue Oct 29, 2013 5:10 pm

As an unrelated addition to what Giovanni said: µGFX v1.9 was released yesterday. From your code I guess you're still using an old one (fonts). You probably want to upgrade (no API changes).

~ Tectu

Posts: 89
Joined: Fri Jul 27, 2012 1:37 pm
Location: Brisbane, Australia

Re: Issue with threading setup

Postby inmarket » Sun Nov 17, 2013 8:59 am

I am not sure if this will create problems but if you are still having problems this may be worth a try...

gfxInit() should be performed in your main thread before anything else happens. It internally also calls the ChbiOS init functions halInit() and chSysInit(). Calling it from a separate thread after ChibiOS has been initialised has not been tested. It may be OK as I think all the Init functions check for repeated calls but its probably not best to rely on that.

User avatar
Site Admin
Posts: 12949
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 733 times
Been thanked: 609 times

Re: Issue with threading setup

Postby Giovanni » Sun Nov 17, 2013 9:04 am

Calling OS initialization from a library is a bad idea IMO. A library should be designed to have the least external dependencies.


Return to “STM32 Support”

Who is online

Users browsing this forum: No registered users and 5 guests