Skip to content

Commit

Permalink
Implement asm wait
Browse files Browse the repository at this point in the history
  • Loading branch information
Lorenzooone committed Nov 20, 2023
1 parent 1d3460c commit 2b06a82
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions source/rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@

// Amount of iterations to wait after writing the data,
// before touching other RTC-related registers. Dependant on optimization level
#define TIMEOUT_WAIT_AFTER_WRITE 448
#define TIMEOUT_CYCLES_WAIT_AFTER_WRITE_PER_BYTE 1858

/* Compiler hacks */
#define assume(cond) do { if (!(cond)) __builtin_unreachable(); } while (0)
Expand Down Expand Up @@ -143,7 +143,11 @@ static void rtc_cmd_arg(const unsigned int cmd, unsigned int data, unsigned int
assume(len > 0 && len <= 32);
rtc_write(data, len);

for(volatile int a = 0; a < TIMEOUT_WAIT_AFTER_WRITE; a++);
__asm__ volatile (
"L1%=: subs %[wait], #1" "\n\t"
"bne L1%="
:: [wait]"l"((TIMEOUT_CYCLES_WAIT_AFTER_WRITE_PER_BYTE * ((len + 7) / 8)) / 12)
);
}

static void rtc_cmd_arg_datetime(unsigned int cmd, __agbabi_datetime_t datetime, int is_12hr) {
Expand All @@ -155,7 +159,11 @@ static void rtc_cmd_arg_datetime(unsigned int cmd, __agbabi_datetime_t datetime,
rtc_write(date, 32);
rtc_write(time, 24);

for(volatile int a = 0; a < TIMEOUT_WAIT_AFTER_WRITE; a++);
__asm__ volatile (
"L1%=: subs %[wait], #1" "\n\t"
"bne L1%="
:: [wait]"l"((TIMEOUT_CYCLES_WAIT_AFTER_WRITE_PER_BYTE * 7) / 12)
);
}

static void rtc_reset(void) {
Expand Down

0 comments on commit 2b06a82

Please sign in to comment.