我想用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库时,睡眠()不会使mbed进入睡眠状态。即使我不使用rtos库中的任何函数调用,Sleep()也不能工作。
我的解释是:可能rtos有一个在后台运行的计时器,它会时不时地产生一个中断。(但这没有什么意义,因为我没有使用rtos库中的任何函数或对象)
我的问题是:
有没有人让Sleep()函数与rtos一起工作?如果是,请给我指出正确的方向,或者如果你有解决方案,请分享。
发布于 2016-06-16 19:34:03
我不确定是否为RTOS使用而设计了睡眠()函数,但我对此表示怀疑。对mbed-rtos有更好了解的人可能可以肯定,但我怀疑RTOS中的IRQ处理可能会导致问题。如果睡眠()依赖于WFE,那么如果没有挂起的中断标志,MCU将休眠。在超级循环设计中,你(应该)对此有完全的控制;对于RTOS,你不能。
我建议使用Thread::wait(),它应该充分了解RTOS的作用。不知道它是否会导致睡眠,但我希望不会少。
发布于 2018-02-07 18:28:49
我曾经使用过下面的库,它工作得很完美。我不确定它是否适用于mbed 5,但它值得一试。
https://stackoverflow.com/questions/20424440
复制相似问题