Mini OS
Système minimal démontrant les bases d’un OS.
#include "xc.h"
#include "stdint.h"
#include "stdbool.h"
// =====================================================
// CONFIG
// Adjust these for your PIC18 device
// Example values for many PIC18 parts with internal osc
// =====================================================
#pragma config FOSC = INTIO67
#pragma config WDTEN = OFF
#pragma config LVP = OFF
#pragma config PBADEN = OFF
#define _XTAL_FREQ 8000000UL
// =====================================================
// GLOBAL TICK FLAGS
// =====================================================
volatile uint32_t g_tick_ms = 0;
volatile uint8_t task_1ms_flag = 0;
volatile uint8_t task_10ms_flag = 0;
volatile uint8_t task_100ms_flag = 0;
volatile uint8_t task_500ms_flag = 0;
// =====================================================
// GPIO INIT
// Example: LED on RC0
// =====================================================
static void gpio_init(void)
{
ADCON1 = 0x0F; // All digital on many PIC18s
TRISCbits.TRISC0 = 0;
LATCbits.LATC0 = 0;
}
// =====================================================
// TIMER0 INIT
// Generates ~1 ms tick
//
// Fosc = 8 MHz
// Instruction clock = Fosc/4 = 2 MHz
// Tick period = 0.5 us
//
// Prescaler 1:32 -> timer tick = 16 us
// Need ~1000 us => 62.5 counts
// Preload = 65536 - 63 = 65473 = 0xFFC1
// =====================================================
static void timer0_init(void)
{
T0CON = 0x00;
T0CONbits.T08BIT = 0; // 16-bit timer
T0CONbits.T0CS = 0; // Internal clock
T0CONbits.PSA = 0; // Prescaler assigned
T0CONbits.T0PS = 0b100; // 1:32 prescaler
TMR0H = 0xFF;
TMR0L = 0xC1;
INTCONbits.TMR0IF = 0;
INTCONbits.TMR0IE = 1;
INTCONbits.GIE = 1;
INTCONbits.PEIE = 1;
T0CONbits.TMR0ON = 1;
}
// =====================================================
// TASKS
// =====================================================
static void task_1ms(void)
{
// Fast task
// Example: sample inputs, small state machine, etc.
}
static void task_10ms(void)
{
// Medium-speed task
}
static void task_100ms(void)
{
// Slow task
}
static void task_500ms(void)
{
// Blink LED every 500 ms
LATCbits.LATC0 ^= 1;
}
// =====================================================
// TIMER ISR
// Keep it short: reload timer, update counters, set flags
// =====================================================
void __interrupt() isr(void)
{
if (INTCONbits.TMR0IF)
{
// Reload for ~1 ms
TMR0H = 0xFF;
TMR0L = 0xC1;
INTCONbits.TMR0IF = 0;
g_tick_ms++;
task_1ms_flag = 1;
if ((g_tick_ms % 10U) == 0U)
task_10ms_flag = 1;
if ((g_tick_ms % 100U) == 0U)
task_100ms_flag = 1;
if ((g_tick_ms % 500U) == 0U)
task_500ms_flag = 1;
}
}
// =====================================================
// MAIN
// =====================================================
void main(void)
{
OSCCON = 0b01110010; // 8 MHz internal osc on many PIC18s
gpio_init();
timer0_init();
while (1)
{
if (task_1ms_flag)
{
task_1ms_flag = 0;
task_1ms();
}
if (task_10ms_flag)
{
task_10ms_flag = 0;
task_10ms();
}
if (task_100ms_flag)
{
task_100ms_flag = 0;
task_100ms();
}
if (task_500ms_flag)
{
task_500ms_flag = 0;
task_500ms();
}
// Optional: idle / low-power code here
}
}
Custom Embedded OS (Advanced)
Version avancée d’un OS embarqué avec une architecture temps réel plus structurée, démontrant une gestion fine du scheduling et des interruptions matérielles.
Ce système montre une approche proche d’un RTOS simplifié :
séparation des tâches, timing déterministe et boucle principale non bloquante.
- Timer hardware → génération de ticks précis
- Scheduling multi-fréquence (1ms / 10ms / 100ms)
- Architecture event-driven
- Optimisé pour microcontrôleurs (bare-metal)