Skip to content

Commit

Permalink
Put the core to rest, do not spin
Browse files Browse the repository at this point in the history
  • Loading branch information
JuantAldea committed Dec 16, 2023
1 parent ca66ea2 commit a882b70
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 41 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ENABLE_CLANG ?= 0
ENABLE_SWD ?= 0
ENABLE_OVERLAY ?= 0
ENABLE_LTO ?= 1

ENABLE_WFI ?= 1
# ---- STOCK QUANSHENG FERATURES ----
ENABLE_UART ?= 1
ENABLE_AIRCOPY ?= 0
Expand Down Expand Up @@ -365,6 +365,9 @@ ifeq ($(ENABLE_FLASHLIGHT),1)
CFLAGS += -DENABLE_FLASHLIGHT
endif

ifeq ($(ENABLE_WFI),1)
CFLAGS += -DENABLE_WFI
endif
LDFLAGS =
LDFLAGS += -z noexecstack -mcpu=cortex-m0 -nostartfiles -Wl,-T,firmware.ld -Wl,--gc-sections

Expand Down
15 changes: 8 additions & 7 deletions driver/bk4819.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@

#include <stdio.h> // NULL

#include "audio.h"
#include "../audio.h"
#include "../bsp/dp32g030/gpio.h"
#include "../bsp/dp32g030/portcon.h"

#include "bk4819.h"
#include "bsp/dp32g030/gpio.h"
#include "bsp/dp32g030/portcon.h"
#include "driver/gpio.h"
#include "driver/system.h"
#include "driver/systick.h"
#include "gpio.h"
#include "system.h"
#include "systick.h"


#ifndef ARRAY_SIZE
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
Expand Down Expand Up @@ -127,7 +129,6 @@ static uint16_t BK4819_ReadU16(void)
PORTCON_PORTC_IE = (PORTCON_PORTC_IE & ~PORTCON_PORTC_IE_C2_MASK) | PORTCON_PORTC_IE_C2_BITS_ENABLE;
GPIOC->DIR = (GPIOC->DIR & ~GPIO_DIR_2_MASK) | GPIO_DIR_2_BITS_INPUT;
SYSTICK_DelayUs(1);

Value = 0;
for (i = 0; i < 16; i++)
{
Expand Down
8 changes: 4 additions & 4 deletions driver/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* limitations under the License.
*/

#include "bsp/dp32g030/pmu.h"
#include "bsp/dp32g030/syscon.h"
#include "driver/system.h"
#include "driver/systick.h"
#include "../bsp/dp32g030/pmu.h"
#include "../bsp/dp32g030/syscon.h"
#include "system.h"
#include "systick.h"

void SYSTEM_DelayMs(uint32_t Delay)
{
Expand Down
35 changes: 23 additions & 12 deletions driver/systick.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/

#include "ARMCM0.h"
#include "driver/systick.h"
#include "misc.h"
#include "systick.h"
#include "../misc.h"

// 0x20000324
static uint32_t gTickMultiplier;
Expand All @@ -29,17 +29,28 @@ void SYSTICK_Init(void)

void SYSTICK_DelayUs(uint32_t Delay)
{
const uint32_t ticks = Delay * gTickMultiplier;
uint32_t i = 0;
uint32_t Start = SysTick->LOAD;
uint32_t Previous = SysTick->VAL;
const uint32_t ticks = Delay * gTickMultiplier;
uint32_t elapsed_ticks = 0;
uint32_t Start = SysTick->LOAD;
uint32_t Previous = SysTick->VAL;
do {
uint32_t Current;
uint32_t Delta;
while ((Current = SysTick->VAL) == Previous) {}
Delta = (Current < Previous) ? -Current : Start - Current;
i += Delta + Previous;

do {
Current = SysTick->VAL;
} while (Current == Previous);

uint32_t Delta = ((Current < Previous) ? - Current : Start - Current);

elapsed_ticks += Delta + Previous;

Previous = Current;
} while (i < ticks);
}

#ifdef ENABLE_WFI
if (((int32_t)(ticks - elapsed_ticks)) >= 48 * 10000) {
__WFI();
}
#endif

} while (elapsed_ticks < ticks);
}
47 changes: 30 additions & 17 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,27 @@
* limitations under the License.
*/

#include <stdint.h>
#include <string.h>
#include <stdio.h> // NULL

#include "ARMCM0.h"

#ifdef ENABLE_AM_FIX
#include "am_fix.h"
#endif
#include "misc.h"
#include "radio.h"
#include "settings.h"
#include "version.h"
#include "board.h"
#include "audio.h"

#include "app/app.h"
#include "app/dtmf.h"
#include "audio.h"
#include "bsp/dp32g030/gpio.h"
#include "bsp/dp32g030/syscon.h"
#include "board.h"

#include "driver/backlight.h"
#include "driver/bk4819.h"
#include "driver/gpio.h"
Expand All @@ -34,16 +43,13 @@
#ifdef ENABLE_UART
#include "driver/uart.h"
#endif

#include "helper/battery.h"
#include "helper/boot.h"
#include "misc.h"
#include "radio.h"
#include "settings.h"

#include "ui/lock.h"
#include "ui/welcome.h"
#include "ui/menu.h"
#include "version.h"

void _putchar(__attribute__((unused)) char c)
{

Expand Down Expand Up @@ -102,9 +108,9 @@ void Main(void)

BATTERY_GetReadings(false);

#ifdef ENABLE_AM_FIX
AM_fix_init();
#endif
#ifdef ENABLE_AM_FIX
AM_fix_init();
#endif

const BOOT_Mode_t BootMode = BOOT_GetMode();

Expand Down Expand Up @@ -215,18 +221,25 @@ void Main(void)
// ******************
}

while (1)
{
while (true) {
APP_Update();

if (gNextTimeslice)
{
#ifdef ENABLE_WFI
/*
* Do not spin mindlessly. Wait until the next interrupt is raised.
* Could be SysTick, then gNextTimeslice would be true, or maybe there's
* something else that is seeking attention that would or should be handled
* by APP_Update(), who knows...
* Longest-case-scenario: we are woken up by SysTick, and this loop runs at 10Mhz.
* Should save quite a bit of battery, I guess.
*/
__WFI();
#endif
if (gNextTimeslice) {
APP_TimeSlice10ms();
gNextTimeslice = false;
}

if (gNextTimeslice_500ms)
{
if (gNextTimeslice_500ms) {
APP_TimeSlice500ms();
gNextTimeslice_500ms = false;
}
Expand Down

0 comments on commit a882b70

Please sign in to comment.