AVR os-less OSAL Topic is solved

ChibiOS public support forum for topics related to the Atmel AVR family of micro-controllers.

Moderators: utzig, tfAteba

Marco
Posts: 128
Joined: Tue Apr 16, 2013 8:22 pm
Has thanked: 4 times
Been thanked: 11 times

AVR os-less OSAL

Postby Marco » Wed Oct 12, 2016 10:51 am

Hi,

I want to try ChibiOS/HAL on an AVR without OS.
Did someone try this on AVR already? There is an os-less OSAL for ARMCMx but not yet for AVR. I can try writing an os-less OSAL for AVR unless there is some reason why it would not be possible to run on an AVR.

Marco

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: AVR os-less OSAL

Postby Giovanni » Wed Oct 12, 2016 11:00 am

Hi,

Currently there is no OSAL for AVR, it should be "easily" derivable from the ARM Cortex-M one, the HW-dependent parts are limited.

Giovanni

Marco
Posts: 128
Joined: Tue Apr 16, 2013 8:22 pm
Has thanked: 4 times
Been thanked: 11 times

Re: AVR os-less OSAL

Postby Marco » Mon Oct 17, 2016 11:01 am

Hi,

I succeed in creating an os-less OSAL for AVR, it was easily derived from the ARMCMx OSAL. Attached are the zipped AVR OSAL files.

While creating the OSAL for AVR i noticed there is quite some duplicated code. Maybe it is a good idea to have a common set of functions for os-less OSAL implementations. What do you think?

Marco
Attachments
osal.zip
AVR OSAL implementation
(8.91 KiB) Downloaded 238 times

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: AVR os-less OSAL

Postby Giovanni » Mon Oct 17, 2016 12:17 pm

Interesting thought, probably it is mostly portable code except for the few asm bits.

Giovanni

utzig
Posts: 359
Joined: Sat Jan 07, 2012 6:22 pm
Location: Brazil
Has thanked: 1 time
Been thanked: 20 times
Contact:

Re: AVR os-less OSAL

Postby utzig » Mon Oct 17, 2016 1:09 pm

osal.c is basically the same, so maybe creating a common/osal.c. Apart from that the code LGTM, I could add this to SVN now and patch to create a common infrastructure later. Will wait for your inputs...

Cheers,
Fabio Utzig

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: AVR os-less OSAL

Postby Giovanni » Mon Oct 17, 2016 1:39 pm

Apply it as-is, I will look at the shared code when the OSAL will get another dev pass.

Giovanni

utzig
Posts: 359
Joined: Sat Jan 07, 2012 6:22 pm
Location: Brazil
Has thanked: 1 time
Been thanked: 20 times
Contact:

Re: AVR os-less OSAL

Postby utzig » Mon Oct 17, 2016 1:49 pm

Done.

Marco
Posts: 128
Joined: Tue Apr 16, 2013 8:22 pm
Has thanked: 4 times
Been thanked: 11 times

Re: AVR os-less OSAL

Postby Marco » Thu Oct 20, 2016 12:41 pm

Hi,

I found out that the os-less AVR OSAL is not working with optimization other than -O0. The application was stuck in the loop in osalThreadSuspendS after calling spiStartSendI:

Code: Select all

  while (self.message == MSG_WAIT) {
    osalSysUnlock();
    /* A state-changing interrupt could occur here and cause the loop to
       terminate, an hook macro is executed while waiting.*/
    OSAL_IDLE_HOOK();
    osalSysLock();
  }


Stepping trough this loop showed the Global Interrupt Enable flag in the SREG register was toggling as expected since osalSysUnlock and osalSysLock are continuously called. At this point the SPI Interrupt Flag in the SPSR was also set so the interrupt vector should be called, but this was not the case.

I checked the sei instruction in the ATmega162 datasheet and found the following:
When using the SEI instruction to enable interrupts, the instruction following SEI will be executed
before any pending interrupts, as shown in this example.


The instruction following SEI is CLI. This means the interrupts are immediately disabled and the SPI interrupt has no chance of being called.

Adding

Code: Select all

asm volatile ("nop");
after

Code: Select all

asm volatile ("sei" : : : "memory");
in osalSysUnlock fixes the problem.

I'm wondering if RT and NIL could have issues with this as well since there is no instruction following sei in chcore.h

Marco

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: AVR os-less OSAL

Postby Giovanni » Thu Oct 20, 2016 2:47 pm

Good point, it should be added in my opinion.

Giovanni

utzig
Posts: 359
Joined: Sat Jan 07, 2012 6:22 pm
Location: Brazil
Has thanked: 1 time
Been thanked: 20 times
Contact:

Re: AVR os-less OSAL

Postby utzig » Thu Oct 20, 2016 5:46 pm

I never had any problem running ChibiOS/RT on AVR using -O2 but this is a really interesting find and I would like to investigate more the possible effects of that behavior because it seems to affect every AVR MCU out there.

For now I would suggest that you submit a patch with the extra nop. Btw, feel free to also submit patches for every change we have been discussing, that has been quite useful.

Cheers,
Fabio Utzig


Return to “AVR Support”

Who is online

Users browsing this forum: No registered users and 23 guests