STM8S active halt under ChibiOS

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
king2
Posts: 98
Joined: Mon May 28, 2012 1:57 pm
Been thanked: 2 times

STM8S active halt under ChibiOS

Post by king2 »

I developing device that can lost power at any moment, if this happens, I should not turn on some device for at least two minutes.

So, I placed diode and big capacitor to MCU, so MCU can work without main power and can detect this situation via EXTI interrupt.
After this was detected, I want to change detection polarity, run AWU and wait... After I detect that power was restored, I should run again.

How to properly handle this in ChibiOS?

Code is:

Code: Select all

volatile unsigned char wait_timeout = 0;
volatile unsigned char pckenr1_save = 0;
volatile unsigned char pckenr2_save = 0;
void p_init() {
    GPIO_output_pushpull(M_PWM);
    GPIO_output_pushpull(M_LED);
    GPIO_output_pushpull(M_ENA);
    UART1_CR2_bit.TEN = 1;
    UART1_CR2_bit.REN = 1;
    UART3_CR2_bit.TEN = 1;
    UART3_CR2_bit.REN = 1;
    CLK->PCKENR1 = pckenr1_save;
    CLK->PCKENR2 = pckenr2_save;
}

void p_deinit() {
    GPIO_input_floating(M_LED);
    GPIO_input_floating(M_PWM);
    GPIO_input_floating(M_ENA);
    UART1_CR2_bit.TEN = 0;
    UART1_CR2_bit.REN = 0;
    UART3_CR2_bit.TEN = 0;
    UART3_CR2_bit.REN = 0;
    pckenr1_save = CLK->PCKENR1;
    pckenr2_save = CLK->PCKENR2;
    CLK->PCKENR1 = 0;
    CLK->PCKENR2 = 0x04; // enable AWU clock
}

volatile unsigned char wait_mode = 0;
#pragma vector=9 // Ïðåðûâàíèå ïî ïðîïàäàíèþ ïèòàíèÿ
__interrupt void VCC_lost(void) {
    if (wait_mode==0) { // halt
        GPIO_set1(M_TEST);
        p_deinit();
        wait_timeout = 20; // we will wait for 20 seconds
        wait_mode = 1;
        AWU_APR_bit.APR = 0x3C;
        AWU_TBR_bit.AWUTB = 0x0C;
        AWU_CSR1_bit.AWUEN = 1;
        EXTI_CR2_bit.PEIS = 0x01; // raising edge interrupt
        __halt();
    } else { // wakeup, Neo!
        if (wait_mode == 1) {
            wait_mode = 0;
            AWU_CSR1_bit.AWUEN = 0;
            GPIO_set0(M_TEST);
            p_init();
            EXTI_CR2_bit.PEIS = 0x02; // raising edge interrupt
        }
    }
}

#pragma vector=3
__interrupt void AWU_IF(void) {
    AWU_CSR1_bit.AWUF;
    if (wait_mode != 1) return;
    if (wait_timeout) wait_timeout--;
    if (wait_timeout) {
        GPIO_set0(M_TEST);
        GPIO_set1(M_TEST);
    } else {
        GPIO_set1(M_TEST);
        GPIO_set0(M_TEST);
    }
    __halt();
}


Sometimes THIS code work as expected, when MCU lost power, it brings M_TEST pin high, then do pulses every second, after 20 seconds it inverts pulses polarity. But after restoring power it sometimes continues working wirh ChibiOS threads, sometimes not.

I want to check at start wait_timeout value, if it zero, all is OK, and I can start device, but if it not 0, I should wait more (but communicate with other devices).

I know that maybe I should add some ChibiOS related calls inside interrupts, but I do not know which and how.

Can you help me with this?

Thanks!
User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

Re: STM8S active halt under ChibiOS

Post by Tectu »

Probably this helps somehow: http://chibios.org/dokuwiki/doku.php?id=chibios:howtos:stop_os (sorry, this is all I can help)


~ Tectu
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: STM8S active halt under ChibiOS

Post by Giovanni »

Correct, you could shut down the OS and restart it when power is back.

Giovanni
king2
Posts: 98
Joined: Mon May 28, 2012 1:57 pm
Been thanked: 2 times

Re: STM8S active halt under ChibiOS

Post by king2 »

For example, ChibiOS in main timer routine and we got EXTI interrupt (no power). So, I should make some flag to shutdown, disable main timer, return from interrupt, than wait main timer function to complete (how I will know that it is finished?), and shutdown the OS in main()?

I'm afraid that this will take too much time and will eat too much electricity :)

May be there is some way to make halt() and following interrupts transparent to OS?

Why it breaks? Stack? Peripherals? I have restored all that I have changed..
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: STM8S active halt under ChibiOS

Post by Giovanni »

You could just wait in the ISR after stopping peripheral clocks, the OS is dead in that situation, on return it would continue. This is dirty and a lot of things could do wrong.

I would set a global "shutdown" flag from the ISR, the main loop would detect the flag and do a proper shutdown. This can be slow or fast depending on how the main loop is done (you may wakeup the main thread from the ISR and it would be a single context switch away).

In general I would prefer handling startup and shutdown in a single place: main().

Giovanni
king2
Posts: 98
Joined: Mon May 28, 2012 1:57 pm
Been thanked: 2 times

Re: STM8S active halt under ChibiOS

Post by king2 »

OK, I have decided not to play with flags, OS statuses and context, and just made RESET at end of EXTI interrupt, so all (including hardware defaults) can be initialized again from scratch (WWDG_CR_bit.WDGA = 1).

Thanks for help!
Post Reply