Adding an RTOS to an existing application can be a very complex process.  While in many cases, this will involve re-architecting the application to get the full benefit, the initial steps can be quickly implemented.  An easy first step is to look over your application and find a functional block that is easy to separate from other logic.

Once that is done, we can start making changes to your code.

First we add a couple of headers.

// chibios specific include files
#include "ch.h"
#include "hal.h"

Next we need to seperate the independent functionallity and turn it into a standalone function that will become a task.  An example of this is the function below that simply flashes an LED.

/*
 * GREEN blinker thread, times are in milliseconds.
 *
 * This function will become a thread that will execute independent
 *  of the other threads.  We will call this thread 2
 *
 */
 
// This macro creates the working space and stack space for thread 2 
static WORKING_AREA(waThread2, 128); 

// This function will become the code for thread 2
static msg_t Thread2(void *arg)
{
    (void)arg;
    chRegSetThreadName("Green Blinker");
    while (TRUE)
    {
        palClearPad(GPIOD, GPIOD_LED4);     // PD12 - Green LED
        chThdSleepMilliseconds( 20 );      // delay 200mS
        palSetPad(GPIOD, GPIOD_LED4);       // PD12 - Green LED
        chThdSleepMilliseconds( 20 );      // delay 200mS
    }
    return 0;
}

Once that is taken care of we need to add some code to the main() function.  First we need to call the functions to initialize the Hardware Abstraction Layer (HAL) and the Chibios kernel.

    /*
     * System initializations.
     * - HAL initialization, this also initializes the configured device drivers
     *	 and performs the board-specific initializations.
     * - Kernel initialization, the main() function becomes a thread and the
     *	 RTOS is active.
     */
    halInit();
    chSysInit();

Finally, we add code to create a thread out of the function we created above

    /*
     * Creates the blinker threads.
     */
    chThdCreateStatic(waThread2, sizeof(waThread2), NORMALPRIO, Thread2, NULL);

If all went well, your application should behave much the same as it did, but the functionality that was selected to turn into a task will operate nearly as a separate program. By moving this file to a separate source module and reducing the number of global variables, program maintenance and debugging will become much easier.