Skip to content

Commit 2dd31b4

Browse files
microbit-carlosmbrossard
authored andcommitted
microbitv2: Replace MAIN_USER_EVENT state with a on-demand function.
So rather than go through a specific main_shutdown_state state to set the "user event" (raising an interrupt and sending I2C data), it just does that action directly when the button is long-pressed or when the wake_from_usb flag is checked. Also sets the interface_power_mode default state to 'Running'. Not MB_POWER_DOWN, as until a power mode is requested the device is awake.
1 parent abc2cfa commit 2dd31b4

File tree

4 files changed

+25
-33
lines changed

4 files changed

+25
-33
lines changed

source/board/microbitv2/i2c_commands.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,3 +382,20 @@ void i2c_cmds_init() {
382382
i2c_registerWriteCallback(i2c_write_flash_callback, I2C_SLAVE_FLASH);
383383
i2c_registerReadCallback(i2c_read_flash_callback, I2C_SLAVE_FLASH);
384384
}
385+
386+
void i2c_cmds_user_event(userEventType_t user_event) {
387+
// Release COMBINED_SENSOR_INT in case it was previously asserted
388+
gpio_disable_combined_int();
389+
390+
// Prepare I2C response
391+
i2cCommand_t i2cResponse = {0};
392+
i2cResponse.cmdId = gReadResponse_c;
393+
i2cResponse.cmdData.readRspCmd.propertyId = (uint8_t) gUserEvent_c;
394+
i2cResponse.cmdData.readRspCmd.dataSize = 1;
395+
i2cResponse.cmdData.readRspCmd.data[0] = user_event;
396+
397+
i2c_fillBuffer((uint8_t*) &i2cResponse, 0, sizeof(i2cResponse));
398+
399+
// Response ready, assert COMBINED_SENSOR_INT
400+
gpio_assert_combined_int();
401+
}

source/board/microbitv2/i2c_commands.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ typedef __PACKED_STRUCT i2cFlashCmd_tag {
167167
} i2cFlashCmd_t;
168168

169169
void i2c_cmds_init(void);
170+
void i2c_cmds_user_event(userEventType_t user_event);
170171

171172
#ifdef __cplusplus
172173
}

source/board/microbitv2/microbitv2.c

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ volatile uint8_t wake_from_reset = 0;
5858
volatile uint8_t wake_from_usb = 0;
5959
volatile bool usb_pc_connected = false;
6060
power_source_t power_source;
61-
microbit_if_power_mode_t interface_power_mode = MB_POWER_DOWN;
61+
microbit_if_power_mode_t interface_power_mode = MB_POWER_RUNNING;
6262
bool power_led_sleep_state_on = PWR_LED_SLEEP_STATE_DEFAULT;
6363
bool automatic_sleep_on = AUTOMATIC_SLEEP_DEFAULT;
6464
main_shutdown_state_t main_shutdown_state = MAIN_SHUTDOWN_WAITING;
@@ -182,7 +182,8 @@ void handle_reset_button()
182182
}
183183
else if (gpio_reset_count >= RESET_SHORT_PRESS) {
184184
// Indicate the button has been released when shutdown is requested
185-
main_shutdown_state = MAIN_USER_EVENT;
185+
i2c_cmds_user_event(gResetButtonLongPress_c);
186+
main_shutdown_state = MAIN_SHUTDOWN_WAITING;
186187
}
187188
} else if (reset_pressed && gpio_get_reset_btn_fwrd()) {
188189
// Reset button is still pressed
@@ -223,11 +224,12 @@ void board_30ms_hook()
223224
}
224225

225226
if (wake_from_usb) {
226-
main_shutdown_state = MAIN_USER_EVENT;
227-
227+
wake_from_usb = 0;
228228
if (usb_state == USB_DISCONNECTED) {
229229
usb_state = USB_CONNECTING;
230230
}
231+
power_led_sleep_state_on = PWR_LED_SLEEP_STATE_DEFAULT;
232+
i2c_cmds_user_event(gWakeFromWakeOnEdge_c);
231233
}
232234

233235
i2c_30ms_tick();
@@ -259,34 +261,6 @@ void board_30ms_hook()
259261
final_fade_led_dc--;
260262
}
261263
break;
262-
case MAIN_USER_EVENT:
263-
{
264-
// Release COMBINED_SENSOR_INT in case it was previously asserted
265-
gpio_disable_combined_int();
266-
267-
// Prepare I2C response
268-
i2cCommand_t i2cResponse = {0};
269-
i2cResponse.cmdId = gReadResponse_c;
270-
i2cResponse.cmdData.readRspCmd.propertyId = (uint8_t) gUserEvent_c;
271-
i2cResponse.cmdData.readRspCmd.dataSize = 1;
272-
273-
if (wake_from_usb) {
274-
i2cResponse.cmdData.readRspCmd.data[0] = gWakeFromWakeOnEdge_c;
275-
wake_from_usb = 0;
276-
power_led_sleep_state_on = PWR_LED_SLEEP_STATE_DEFAULT;
277-
} else {
278-
i2cResponse.cmdData.readRspCmd.data[0] = gResetButtonLongPress_c;
279-
}
280-
281-
i2c_fillBuffer((uint8_t*) &i2cResponse, 0, sizeof(i2cResponse));
282-
283-
// Response ready, assert COMBINED_SENSOR_INT
284-
gpio_assert_combined_int();
285-
286-
// Return LED to ON after release of long press
287-
main_shutdown_state = MAIN_SHUTDOWN_WAITING;
288-
}
289-
break;
290264
case MAIN_SHUTDOWN_REQUESTED:
291265
if (power_source == PWR_BATT_ONLY || (usb_state == USB_DISCONNECTED && !usb_pc_connected)) {
292266
main_powerdown_event();
@@ -369,6 +343,7 @@ void board_handle_powerdown()
369343
switch(interface_power_mode){
370344
case MB_POWER_SLEEP:
371345
power_sleep();
346+
interface_power_mode = MB_POWER_RUNNING;
372347
break;
373348
case MB_POWER_DOWN:
374349
power_down();

source/board/microbitv2/microbitv2.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ typedef enum main_shutdown_state {
4343
MAIN_SHUTDOWN_REACHED,
4444
MAIN_SHUTDOWN_REACHED_FADE,
4545
MAIN_SHUTDOWN_REQUESTED,
46-
MAIN_USER_EVENT,
4746
MAIN_LED_BLINK_ONCE,
4847
MAIN_LED_BLINKING,
4948
MAIN_LED_FULL_BRIGHTNESS,

0 commit comments

Comments
 (0)