用RTOS睡不着

时间:2013-12-06 12:53:43

标签: rtos mbed

我想创建一个带有mbed(LPC1768)的低功耗应用程序,并且一直在关注Jim Hamblen的教程:https://mbed.org/cookbook/Power-Management以及http://mbed.org/users/no2chem/notebook/mbed-power-controlconsumption/

我能够通过GPIO中断,UART中断和Ticker从睡眠()唤醒mbed。我使用PowerControl库。

这是我的代码:

#include "mbed.h"
#include "PowerControl/PowerControl.h"
#include "PowerControl/EthernetPowerControl.h"
// Need PowerControl *.h files from this URL
// http://mbed.org/users/no2chem/notebook/mbed-power-controlconsumption/

// Function to power down magic USB interface chip with new firmware
#define USR_POWERDOWN    (0x104)
int semihost_powerdown() {
    uint32_t arg;
    return __semihost(USR_POWERDOWN, &arg);
}

DigitalOut myled1(LED1);
DigitalOut myled2(LED2);
DigitalOut myled3(LED3);
DigitalOut myled4(LED4);

bool rx_uart_irq = false;

Serial device(p28, p27);  // tx, rx
InterruptIn button(p5);

// Circular buffers for serial TX and RX data - used by interrupt routines
const int buffer_size = 255;
// might need to increase buffer size for high baud rates
char tx_buffer[buffer_size];
char rx_buffer[buffer_size];
// Circular buffer pointers
// volatile makes read-modify-write atomic 
volatile int tx_in=0;
volatile int tx_out=0;
volatile int rx_in=0;
volatile int rx_out=0;
// Line buffers for sprintf and sscanf
char tx_line[80];
char rx_line[80];

void Rx_interrupt();

void blink() {
    myled2 = !myled2;
}


int main() {
    //int result;

    device.baud(9600);
    device.attach(&Rx_interrupt, Serial::RxIrq);
// Normal mbed power level for this setup is around 690mW
// assuming 5V used on Vin pin
// If you don't need networking...
// Power down Ethernet interface - saves around 175mW
// Also need to unplug network cable - just a cable sucks power
    PHY_PowerDown();
    myled2 = 0;
// If you don't need the PC host USB interface....
// Power down magic USB interface chip - saves around 150mW
// Needs new firmware (URL below) and USB cable not connected
// http://mbed.org/users/simon/notebook/interface-powerdown/
// Supply power to mbed using Vin pin
    //result = semihost_powerdown();
// Power consumption is now around half

// Turn off clock enables on unused I/O Peripherals (UARTs, Timers, PWM, SPI, CAN, I2C, A/D...)
// To save just a tiny bit more power - most are already off by default in this short code example
// See PowerControl.h for I/O device bit assignments
// Don't turn off GPIO - it is needed to blink the LEDs
    Peripheral_PowerDown( ~( LPC1768_PCONP_PCUART0 | 
                             LPC1768_PCONP_PCUART2 |
                             0));

// use Ticker interrupt and Sleep instead of a wait for time delay - saves up to 70mW
// Sleep halts and waits for an interrupt instead of executing instructions
// power is saved by not constantly fetching and decoding instructions
// Exact power level reduction depends on the amount of time spent in Sleep mode
    //blinker.attach(&blink, 0.05);
    //button.rise(&blink);
    while (1) {
        myled1 = 0;
        printf("bye\n");
        Sleep();
        if(rx_uart_irq == true) {
            printf("wake from uart irq\n");
        }
        myled1 = 1;
    }
}

// Interupt Routine to read in data from serial port
void Rx_interrupt() {
    myled2 = !myled2;
    rx_uart_irq = true;
        uint32_t IRR0= LPC_UART2->IIR;
    while ((device.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) {
        rx_buffer[rx_in] = LPC_UART2->RBR;
        rx_in = (rx_in + 1) % buffer_size;
    }
}

问题在于:当添加mbed-rtos库时,Sleep()不会让mbed进入睡眠状态。即使我不使用rtos库中的任何函数调用,Sleep()也不起作用。

我的解释:可能rtos有一个在后台运行的计时器,它不时产生一个中断。 (但它有点没有意义,因为我没有使用rtos库中的任何函数或对象)

我的问题:

有没有人让Sleep()函数与rtos一起使用?如果是,请指出正确的方向或如果您有解决方案,请分享。

3 个答案:

答案 0 :(得分:1)

每个rtos都有一个定时器,可以打断中断。这用于任务切换。 只要确保它将在您自己的中断上重新启动,您可以在调用sleep方法时停止此计时器。

答案 1 :(得分:1)

我不确定Sleep()函数是否专为RTOS使用而设计,但我对此表示怀疑。对mbed-rtos有更好了解的人可能肯定会说出来,但我怀疑RTOS中的IRQ处理可能会导致问题。如果Sleep()依赖于WFE,那么如果没有挂起的中断标志,MCU将会休眠。在超级循环设计中,你(应该)完全控制它;使用RTOS,你没有。

我建议使用Thread :: wait(),它应该完全了解RTOS的功能。不知道它是否会导致睡眠,但我希望不会少。

答案 2 :(得分:0)

我曾使用过以下的库,它运行完美。我不确定它是否适用于mbed 5,但值得一试。

https://os.mbed.com/users/no2chem/code/PowerControl/