0

我想用 mbed (LPC1768) 创建一个低功耗应用程序,并且一直在学习 Jim Hamblen 的教程:https ://mbed.org/cookbook/Power-Management和http://mbed.org/users/no2chem/笔记本/mbed-power-controlconsumption/

我能够通过 GPIO 中断、UART 中断和 Ticker 从 Sleep() 中唤醒 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 一起工作?如果是,请指出正确的方向,或者如果您有解决方案,请分享。

4

2 回答 2

1

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

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

于 2016-06-16T11:34:03.100 回答
0

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

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

于 2018-02-07T10:28:49.007 回答