1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

A silly really small co-operative kernel for atmel avr arduino :-)

Discussion in 'Code Repository' started by voidptr, May 16, 2013.

  1. voidptr

    voidptr Member

    Joined:
    Mar 6, 2012
    Messages:
    50
    Likes:
    2
    Location:
    Montreal :o)
    so ... one of my friend needed a little help with concurrency so last weekend I chopped out one silly little co-operative kernel for his arduino applications (works fine on 328p and atmega2560 )...

    I built many of those little kernels for different cpu over the years (there is also many os outthere too).

    they are usually simple ( not time sliced ) and they work fine with ucontroller and few cooperative tasks.

    this silly one is as much thin you can go, only simple tasks( with their own stack) and semaphore (with P and V) , all in C, and using for context switching setjump() and longjump().

    my current bot used a more extended version of this one, but this code can give to some people a good starting point...

    so there is 3 files attached:

    osp.h - prototype file
    osp.c - c code
    zosavr.ino a silly test for testing the kernel.

    the test is basically 3 tasks

    taskIdle will emulate the arduino main function.

    task0 ans task1 will synchronize themself with semaphores and blink the pin13 led ...

    of course in the real word, the led should be a ressource and it should be gated be semaphore... but this is just a demo so ...

    enjoy :)
     

    Attached Files:

    • zosavr.ino
      File size:
      1.4 KB
      Views:
      234
    • osp.h
      File size:
      225 bytes
      Views:
      248
    • osp.c
      File size:
      2.1 KB
      Views:
      303
  2. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    You should comment your code if you are sharing it for others to use. Only comment I found was "Magic stuff here!" :)

    I have also written a multitasking kernel.. properly commented. Attached.
     

    Attached Files:

    • E2.c
      File size:
      3.1 KB
      Views:
      297
    • avrrtos.h
      File size:
      6.7 KB
      Views:
      339
    • avrrtos.c
      File size:
      8.7 KB
      Views:
      318
    Last edited: May 16, 2013
  3. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    I can't figure out how your silly co-operative kernel differs from finite state machine? There is no system clock.. there is no scheduling.. what is there? I know that you wrote that for arduino, so it makes sense that it is "silly".

    [​IMG]
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. voidptr

    voidptr Member

    Joined:
    Mar 6, 2012
    Messages:
    50
    Likes:
    2
    Location:
    Montreal :o)

    well...
    all task has their own stack...
    no need for a system clock , it is a co-operative scheduling approach, a task yield the cpu when it want, and because it has it own stack, yield or a semaphore P can happen wherever you want ...
    there IS scheduling, simple silly round robin, context switching is done with setjump and longjump ...

    remember the first MAC OS aka The Finder ?

    sometime we dont need to bother our self with time slice kernel and interrupt protection etc etc ...
    (I also wrote few of them :eek:) )

    try it you will see :p
     
    Last edited: Jun 6, 2013
  6. voidptr

    voidptr Member

    Joined:
    Mar 6, 2012
    Messages:
    50
    Likes:
    2
    Location:
    Montreal :o)
    I prefer to be a simple input follower sometime with high input impedance ! [​IMG]


     
    Last edited: Jun 7, 2013
  7. voidptr

    voidptr Member

    Joined:
    Mar 6, 2012
    Messages:
    50
    Likes:
    2
    Location:
    Montreal :o)
    I will add at least 1 kernel function
    taskYield() or taskPreempt()

    in this way, a task can yield itself when it has no more job to do, and not spinning like hell consuming it timeslice doing nothing :eek:)

    semaphore object and P and V operation can be also very handy :)
     

Share This Page