Skip to content

Commit 88d64cb

Browse files
microbit-carlosmbrossard
authored andcommitted
microbitv2: Don't calculate the power source in the USB insertion interrupt.
As it caused problems in the KL27 port with a timer interrupt being set up to wait for 3 ms inside the USB insertion interrupt. Sets the timer interrupt to level 2 (second lowest, but higher than the wake up pin and USB insertion), as the default is level 0, over other important interrupts like USB and I2C. Also removes the need to check for the power source when the Interface is requested to go to sleep, as it only really needs to know if it's "USB connected" (being on battery or USB power bank should have the same effect, it should not behave differently in battery power vs USB power bank).
1 parent 2dd31b4 commit 88d64cb

File tree

5 files changed

+12
-24
lines changed

5 files changed

+12
-24
lines changed

source/board/microbitv2/i2c_commands.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232

3333

3434
extern uint16_t board_id_hex;
35-
extern power_source_t power_source;
3635
extern main_usb_connect_t usb_state;
3736
extern microbit_if_power_mode_t interface_power_mode;
3837
extern bool power_led_sleep_state_on;
@@ -77,10 +76,11 @@ static void i2c_write_comms_callback(uint8_t* pData, uint8_t size) {
7776
memcpy(&i2cResponse.cmdData.readRspCmd.data, &daplink_version, sizeof(daplink_version));
7877
}
7978
break;
80-
case gPowerState_c:
81-
power_source = pwr_mon_get_power_source();
79+
case gPowerState_c: {
80+
power_source_t power_source = pwr_mon_get_power_source();
8281
i2cResponse.cmdData.readRspCmd.dataSize = sizeof(power_source);
8382
memcpy(&i2cResponse.cmdData.readRspCmd.data, &power_source, sizeof(power_source));
83+
}
8484
break;
8585
case gPowerConsumption_c: {
8686
uint32_t vin_voltage_uv = pwr_mon_get_vin_mv() * 1000;

source/board/microbitv2/kl27z/power.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ extern volatile uint8_t wake_from_reset;
6767
extern volatile uint8_t wake_from_usb;
6868
extern volatile bool usb_pc_connected;
6969
extern main_usb_connect_t usb_state;
70-
extern power_source_t power_source;
7170

7271
/*******************************************************************************
7372
* Code
@@ -99,8 +98,6 @@ void PORTCD_IRQHandler(void)
9998
if ((1U << PIN_WAKE_ON_EDGE_BIT) & PORT_GetPinsInterruptFlags(PIN_WAKE_ON_EDGE_PORT))
10099
{
101100
PORT_ClearPinsInterruptFlags(PIN_WAKE_ON_EDGE_PORT, (1U << PIN_WAKE_ON_EDGE_BIT));
102-
103-
power_source = pwr_mon_get_power_source();
104101

105102
bool usb_on = (((PIN_WAKE_ON_EDGE_GPIO->PDIR) >> PIN_WAKE_ON_EDGE_BIT) & 0x01U) ? false : true;
106103

@@ -138,6 +135,7 @@ void power_init(void)
138135
/* Enable either edge interrupt on WAKE_ON_EDGE pin to detect USB attach/detach */
139136
PORT_SetPinInterruptConfig(PIN_WAKE_ON_EDGE_PORT, PIN_WAKE_ON_EDGE_BIT, PIN_WAKE_ON_EDGE_PORT_WAKEUP_TYPE);
140137

138+
/* Enable IRQ and set priority to the lowest level */
141139
NVIC_SetPriority(LLWU_IRQn, (1UL << __NVIC_PRIO_BITS) - 1UL);
142140
NVIC_EnableIRQ(LLWU_IRQn);
143141
NVIC_SetPriority(PORTC_PORTD_IRQn, (1UL << __NVIC_PRIO_BITS) - 1UL);

source/board/microbitv2/kl27z/timer_wait.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,15 @@ void timer_wait_init(void)
4747
TPM_GetDefaultConfig(&tpmInfo);
4848
/* TPM clock divide by TPM_PRESCALER */
4949
tpmInfo.prescale = kTPM_Prescale_Divide_4;
50+
/* TPM counter starts immediately once it is enabled */
51+
tpmInfo.enableStartOnTrigger = false;
5052
/* Initialize TPM module */
5153
TPM_Init(TPM0, &tpmInfo);
5254

55+
/* Enable interrupt and set it to second lowest priority level */
56+
const uint32_t lowest_priority_level = (1UL << __NVIC_PRIO_BITS) - 1UL;
5357
TPM_EnableInterrupts(TPM0, kTPM_TimeOverflowInterruptEnable);
58+
NVIC_SetPriority(TPM0_IRQn, lowest_priority_level - 1);
5459
EnableIRQ(TPM0_IRQn);
5560

5661
tpm_source_clock = (CLOCK_GetFreq(kCLOCK_McgIrc48MClk) / 4);

source/board/microbitv2/microbitv2.c

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ extern target_cfg_t target_device_nrf52833;
5757
volatile uint8_t wake_from_reset = 0;
5858
volatile uint8_t wake_from_usb = 0;
5959
volatile bool usb_pc_connected = false;
60-
power_source_t power_source;
6160
microbit_if_power_mode_t interface_power_mode = MB_POWER_RUNNING;
6261
bool power_led_sleep_state_on = PWR_LED_SLEEP_STATE_DEFAULT;
6362
bool automatic_sleep_on = AUTOMATIC_SLEEP_DEFAULT;
@@ -125,15 +124,13 @@ static void prerun_board_config(void)
125124
power_init();
126125
pwr_mon_init();
127126

128-
power_source = pwr_mon_get_power_source();
129-
130127
pwm_init();
131128
pwm_init_pins();
132129

130+
power_source_t power_source = pwr_mon_get_power_source();
133131
if (power_source == PWR_BATT_ONLY){
134132
// Turn on the red LED with low duty cycle to conserve power.
135133
power_led_max_duty_cycle = PWR_LED_ON_BATT_BRIGHTNESS;
136-
137134
} else {
138135
// Turn on the red LED with max duty cycle when powered by USB or EC
139136
power_led_max_duty_cycle = PWR_LED_ON_MAX_BRIGHTNESS;
@@ -262,17 +259,10 @@ void board_30ms_hook()
262259
}
263260
break;
264261
case MAIN_SHUTDOWN_REQUESTED:
265-
if (power_source == PWR_BATT_ONLY || (usb_state == USB_DISCONNECTED && !usb_pc_connected)) {
262+
if (usb_state == USB_DISCONNECTED && !usb_pc_connected) {
266263
main_powerdown_event();
267264

268-
// In VLLS0, set the LED either ON or LOW, depending on power_led_sleep_state_on
269-
// When the duty cycle is 0% or 100%, the FlexIO driver will configure the pin as GPIO
270-
if (power_led_sleep_state_on == true && interface_power_mode == MB_POWER_DOWN) {
271-
shutdown_led_dc = PWR_LED_ON_MAX_BRIGHTNESS;
272-
} else if (power_led_sleep_state_on == true && interface_power_mode == MB_POWER_SLEEP) {
273-
shutdown_led_dc = PWR_LED_ON_BATT_BRIGHTNESS;
274-
}
275-
else {
265+
if (!power_led_sleep_state_on) {
276266
shutdown_led_dc = 0;
277267
}
278268

source/board/microbitv2/nrf52820/power.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ extern volatile uint8_t wake_from_reset;
5151
extern volatile uint8_t wake_from_usb;
5252
extern volatile bool usb_pc_connected;
5353
extern main_usb_connect_t usb_state;
54-
extern power_source_t power_source;
5554

5655

5756
uint8_t power_gpiote_enabled;
@@ -242,8 +241,6 @@ void GPIOTE_IRQHandler(void)
242241

243242
power_gpio_set_sense(reg, idx, absent ? NRF_GPIO_PIN_SENSE_LOW : NRF_GPIO_PIN_SENSE_HIGH);
244243

245-
power_source = pwr_mon_get_power_source();
246-
247244
// Read VBUS_ABSENT (WAKE_ON_EDGE) pin for detecting if board is USB powered
248245
if (absent) {
249246
/* Reset USB on cable detach (VBUS falling edge) */
@@ -266,13 +263,11 @@ void POWER_CLOCK_IRQHandler(void)
266263
{
267264
if (NRF_POWER->EVENTS_USBDETECTED) {
268265
NRF_POWER->EVENTS_USBDETECTED = 0;
269-
power_source = pwr_mon_get_power_source();
270266
wake_from_usb = 1;
271267
}
272268

273269
if (NRF_POWER->EVENTS_USBREMOVED) {
274270
NRF_POWER->EVENTS_USBREMOVED = 0;
275-
power_source = pwr_mon_get_power_source();
276271
/* Reset USB on cable detach (VBUS falling edge) */
277272
USBD_Reset();
278273
usbd_reset_core();

0 commit comments

Comments
 (0)