Virtual multithreading based on STM32 (TI_BLE protocol stack_ZStack ...

STM32-based virtual multi-threading can be well used in bare-metal programs to simulate the multi-threading concept of small operating systems. This example refers to the TI_BLE protocol stack_ZStack protocol stack.

Virtual multithreading based on STM32 (TI_BLE protocol stack_ZStack protocol stack)

#include "Hal_Led / Hal_Led.h"

#include "Hal_delay / delay.h"

#include "Hal_Key / Hal_Key.h"

#include "ringbuffer.h"

#define APP_LED2_BLINK_EVENT 0x0001

#define HAL_LED1_BLINK_EVENT 0x0001

#define TASK_NO_TASK_RUNNING 0xFF

unsigned short Hal_ProcessEvent (unsigned char task_id, unsigned short events);

unsigned short App_ProcessEvent (unsigned char task_id, unsigned short events);

typedef unsigned short uint16;

typedef unsigned char uint8;

#define TASK_CNT 2 // Define the number of threads

// Define function pointer

typedef unsigned short (* pTaskEventHandlerFn) (unsigned char task_id, unsigned short events);

// Thread function table

const pTaskEventHandlerFn tasksArr [] =

{

Hal_ProcessEvent,

App_ProcessEvent

};

const unsigned char tasksCnt = sizeof (tasksArr) / sizeof (tasksArr [0]);

// uint16 * tasksEvents;

uint16 tasksEvents [TASK_CNT] = {0}; // Each thread has 16 bit field space for setting events

staTIc uint8 acTIveTaskID = 0xFF; // Current task ID, indicating role

#define SUCCESS 0x00

#define FAILURE 0x01

#define INVALID_TASK 0x02

uint8 osal_set_event (uint8 task_id, uint16 event_flag)

{

if (task_id <tasksCnt)

{

tasksEvents [task_id] | = event_flag; // Stuff the event bit (s)

return (SUCCESS);

}

else

{

return (INVALID_TASK);

}

}

/ **

* @brief program entry

* @param none

* @return none

* /

int main (void)

{

unsigned short taskID = 0;

uint8 idx = 0;

SystemInit (); // System clock initialization

delayInit (72); // Initialize tick timer

Led_Init (); // LED initialization

NVIC_PriorityGroupConfig (NVIC_PriorityGroup_2);

osal_set_event (0, HAL_LED1_BLINK_EVENT);

osal_set_event (1, APP_LED2_BLINK_EVENT);

while (1)

{

do

{

if (tasksEvents [idx]) // Pulse training knows which thread needs to be processed

{

break;

}

}

while (++ idx <tasksCnt);

if (idx <tasksCnt)

{

uint16 events;

events = tasksEvents [idx];

tasksEvents [idx] = 0; // Clear events in the event array

acTIveTaskID = idx;

events = (tasksArr [idx]) (idx, events); // Call thread function

activeTaskID = TASK_NO_TASK_RUNNING;

tasksEvents [idx] | = events; // Add unprocessed events to the event group of this thread

}

delayMs (1000);

}

}

/ **

* @brief application layer processing

* @param none

* @r

* /

unsigned short Hal_ProcessEvent (unsigned char task_id, unsigned short events)

{

if (events & HAL_LED1_BLINK_EVENT)

{

Led_Reverse (1);

return events ^ HAL_LED1_BLINK_EVENT; // Clear events

}

}

/ **

* @brief hardware control thread

* @param none

* @r

* /

unsigned short App_ProcessEvent (unsigned char task_id, unsigned short events)

{

if (events & APP_LED2_BLINK_EVENT)

{

Led_Reverse (2);

return events ^ APP_LED2_BLINK_EVENT; // Clear events

Back Seat Headrest Pillow Phone Holder

Back Seat Headrest Pillow Phone Holder,Samsung Tablet Car Holder Back Seat,Car Back Seat Organiser With Tablets,Car Back Seat Tablet Holder

Ningbo Luke Automotive Supplies Ltd. , https://www.car-phone-holder.com