4180 lab 1

Dependencies:   mbed MCP23S17 PinDetect USBDevice

Committer:
emilywilson
Date:
Wed Jan 22 13:08:48 2020 +0000
Revision:
12:cc5bda248946
Parent:
11:2cfdab516b21
mouse extra credit and power management extra credit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilywilson 11:2cfdab516b21 1 #include "mbed.h"
emilywilson 11:2cfdab516b21 2 #include "PowerControl/PowerControl.h"
emilywilson 11:2cfdab516b21 3 #include "PowerControl/EthernetPowerControl.h"
emilywilson 11:2cfdab516b21 4 // Need PowerControl *.h files from this URL
emilywilson 11:2cfdab516b21 5 // http://mbed.org/users/no2chem/notebook/mbed-power-controlconsumption/
emilywilson 11:2cfdab516b21 6
emilywilson 11:2cfdab516b21 7 // Function to power down magic USB interface chip with new firmware
emilywilson 11:2cfdab516b21 8 #define USR_POWERDOWN (0x104)
emilywilson 11:2cfdab516b21 9 int semihost_powerdown() {
emilywilson 11:2cfdab516b21 10 uint32_t arg;
emilywilson 11:2cfdab516b21 11 return __semihost(USR_POWERDOWN, &arg);
emilywilson 11:2cfdab516b21 12 }
emilywilson 11:2cfdab516b21 13
emilywilson 11:2cfdab516b21 14 DigitalOut myled(p26);
emilywilson 11:2cfdab516b21 15 DigitalIn pb(p22);
emilywilson 12:cc5bda248946 16
emilywilson 12:cc5bda248946 17 Ticker blinker;
emilywilson 12:cc5bda248946 18
emilywilson 12:cc5bda248946 19 void blink() {
emilywilson 12:cc5bda248946 20 myled = !pb;
emilywilson 12:cc5bda248946 21 }
emilywilson 11:2cfdab516b21 22
emilywilson 11:2cfdab516b21 23 int run_powermanagementEC() {
emilywilson 11:2cfdab516b21 24 int result;
emilywilson 11:2cfdab516b21 25 // Normal mbed power level for this setup is around 690mW
emilywilson 11:2cfdab516b21 26 // assuming 5V used on Vin pin
emilywilson 11:2cfdab516b21 27 // If you don't need networking...
emilywilson 11:2cfdab516b21 28 // Power down Ethernet interface - saves around 175mW
emilywilson 11:2cfdab516b21 29 // Also need to unplug network cable - just a cable sucks power
emilywilson 11:2cfdab516b21 30 PHY_PowerDown();
emilywilson 11:2cfdab516b21 31
emilywilson 11:2cfdab516b21 32 // If you don't need the PC host USB interface....
emilywilson 11:2cfdab516b21 33 // Power down magic USB interface chip - saves around 150mW
emilywilson 11:2cfdab516b21 34 // Needs new firmware (URL below) and USB cable not connected
emilywilson 11:2cfdab516b21 35 // http://mbed.org/users/simon/notebook/interface-powerdown/
emilywilson 11:2cfdab516b21 36 // Supply power to mbed using Vin pin
emilywilson 11:2cfdab516b21 37 result = semihost_powerdown();
emilywilson 11:2cfdab516b21 38 // Power consumption is now around half
emilywilson 11:2cfdab516b21 39
emilywilson 11:2cfdab516b21 40 // Turn off clock enables on unused I/O Peripherals (UARTs, Timers, PWM, SPI, CAN, I2C, A/D...)
emilywilson 11:2cfdab516b21 41 // To save just a tiny bit more power - most are already off by default in this short code example
emilywilson 11:2cfdab516b21 42 // See PowerControl.h for I/O device bit assignments
emilywilson 11:2cfdab516b21 43 // Don't turn off GPIO - it is needed to blink the LEDs
emilywilson 11:2cfdab516b21 44 Peripheral_PowerDown(0xFFFF7FFF);
emilywilson 11:2cfdab516b21 45
emilywilson 11:2cfdab516b21 46 // use Ticker interrupt and Sleep instead of a wait for time delay - saves up to 70mW
emilywilson 11:2cfdab516b21 47 // Sleep halts and waits for an interrupt instead of executing instructions
emilywilson 11:2cfdab516b21 48 // power is saved by not constantly fetching and decoding instructions
emilywilson 11:2cfdab516b21 49 // Exact power level reduction depends on the amount of time spent in Sleep mode
emilywilson 12:cc5bda248946 50 blinker.attach(&blink, 0.0625);
emilywilson 11:2cfdab516b21 51 while (1) {
emilywilson 12:cc5bda248946 52 Sleep();
emilywilson 11:2cfdab516b21 53 }
emilywilson 11:2cfdab516b21 54 }