Author Topic: PIC mid-range MCU async event loop  (Read 1807 times)

0 Members and 1 Guest are viewing this topic.

Offline kdmalTopic starter

  • Newbie
  • Posts: 3
  • Country: ru
PIC mid-range MCU async event loop
« on: November 07, 2019, 10:04:13 am »
Hello, please rate my project https://github.com/humkanet/pic-loop/

Features at this moment:
  • Running multiple tasks asynchronously
  • Asynchronous sleep

All task variable must by ** static **, by default event loop can run 5 tasks, but you can change event loop queue by change LOOP_MAX_TASKS in loop.h
« Last Edit: November 09, 2019, 07:27:56 am by kdmal »
 

Online jaromir

  • Supporter
  • ****
  • Posts: 349
  • Country: sk
Re: PIC mid-range MCU async event loop
« Reply #1 on: November 07, 2019, 12:49:54 pm »
Hello, please rate my project https://github.com/humkanet/pic-async/

From what I gathered it's simple task scheduler/RTOS. I'd like to comment, but it's hard for me, because:
 - documentation is somehow lacking
 - I can't find much of an real world examples and testing results
 - comments in code are sparse and in Russian only
 
The following users thanked this post: kdmal

Offline kdmalTopic starter

  • Newbie
  • Posts: 3
  • Country: ru
Re: PIC mid-range MCU async event loop
« Reply #2 on: November 07, 2019, 01:06:48 pm »
Sorry, im rewrite comments to eng

Real world example, blink led in backgroud, or update any information on display, for example:

Code: [Select]
void task_blink()
{
for(;;){
led_on();
loop_sleep(500);
led_off();
loop_sleep(500);
}
}


void task_uptime()
{
static uint16_t  prev;
statuc uint16_t  sec;
prev = clock_sec();
for(;;){
// Sleep 1 second
loop_sleep(1000);
// Seconds changed
sec = clock_sec();
if (sec==prev) continue;
prev = sec;
// Display uptime
... display seconds ...
}
}

...

void main()
{
...
loop_init();
loop_start_task(task_blink);
loop_start_task(task_uptime);
...
// Main loop
for(;;){
// Current system clock
uint16_t  msec = clock_msec();
// Process event loop
loop_tick(msec);
// Other actions
}
}


Both tasks (task_blink and task_uptime) working without block main loop
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 14140
Re: PIC mid-range MCU async event loop
« Reply #3 on: November 07, 2019, 02:23:08 pm »
'Classic' Midrange PICs don't support task switching.  Therefore your code is only suitable for Enhanced Midrange Pics, and in loop.h, you should check that either _PIC14E or _PIC14EX is defined, (see XC8 user guide: TABLE 4-15: PREDEFINED MACROS), and if not, fail with:
Code: [Select]
#error Not an Enhanced Midrange PIC!
 
The following users thanked this post: kdmal

Offline NorthGuy

  • Super Contributor
  • ***
  • Posts: 3593
  • Country: ca
Re: PIC mid-range MCU async event loop
« Reply #4 on: November 08, 2019, 12:43:47 am »
What's the point of saving FSR0 when you must know it before restoring:

Code: [Select]
        // Save registers
asm("movwi     0[FSR0]");
asm("movf      BSR, W");
asm("movwi     1[FSR0]");
asm("movf      FSR0H, W"); // this is redundant
asm("movwi     2[FSR0]"); // this is redundant
asm("movf      FSR0L, W"); // this is redundant
asm("movwi     3[FSR0]"); // this is redundant
asm("movf      FSR1H, W");
asm("movwi     4[FSR0]");
asm("movf      FSR1L, W");
        asm("movwi     5[FSR0]");

When you restore, the FSR0 is already set when you start restoring, so:

Code: [Select]
        asm("moviw     1[FSR0]");
asm("movwf     BSR");
asm("moviw     2[FSR0]"); // this does nothing
asm("movwf     FSR0H"); // this does nothing
asm("moviw     3[FSR0]"); // this does nothing
asm("movwf     FSR0L"); // this does nothing
asm("moviw     4[FSR0]");
asm("movwf     FSR1H");
asm("moviw     5[FSR0]");
asm("movwf     FSR1L");
        asm("moviw     0[FSR0]");
 
The following users thanked this post: kdmal

Offline kdmalTopic starter

  • Newbie
  • Posts: 3
  • Country: ru
Re: PIC mid-range MCU async event loop
« Reply #5 on: November 08, 2019, 02:59:34 am »
NorthGuy, thanks!
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->