runtime/nrf: translate nrf sleep function from C to Go

This is the last critical part of the C runtime.

Code size is reduced by 4 bytes for examples/blinky2 (probably due to
inlining) and is unchanged for examples/test.
This commit is contained in:
Ayke van Laethem
2018-09-12 22:16:06 +02:00
parent 67e33ac30e
commit f7f7d4cbbc
3 changed files with 27 additions and 39 deletions
-30
View File
@@ -1,30 +0,0 @@
#include "hal/nrf_gpio.h"
#include "hal/nrf_uart.h"
#include "nrf.h"
#include "runtime.h"
#include "runtime_nrf.h"
#include <string.h>
static volatile bool rtc_wakeup;
void rtc_sleep(uint32_t ticks) {
NRF_RTC0->INTENSET = RTC_INTENSET_COMPARE0_Msk;
rtc_wakeup = false;
if (ticks == 1) {
// Race condition (even in hardware) at ticks == 1.
// TODO: fix this in a better way by detecting it, like the manual
// describes.
ticks = 2;
}
NRF_RTC0->CC[0] = (NRF_RTC0->COUNTER + ticks) & 0x00ffffff;
while (!rtc_wakeup) {
__WFI();
}
}
void RTC0_IRQHandler() {
NRF_RTC0->INTENCLR = RTC_INTENSET_COMPARE0_Msk;
NRF_RTC0->EVENTS_COMPARE[0] = 0;
rtc_wakeup = true;
}
+27 -3
View File
@@ -7,8 +7,6 @@ import (
"device/nrf"
)
func _Cfunc_rtc_sleep(ticks uint32)
const Microsecond = 1
//go:export _start
@@ -55,7 +53,7 @@ func sleep(d Duration) {
for ticks64 != 0 {
monotime() // update timestamp
ticks := uint32(ticks64) & 0x7fffff // 23 bits (to be on the safe side)
_Cfunc_rtc_sleep(ticks) // TODO: not accurate (must be d / 30.5175...)
rtc_sleep(ticks) // TODO: not accurate (must be d / 30.5175...)
ticks64 -= Duration(ticks)
}
}
@@ -88,3 +86,29 @@ func abort() {
func align(ptr uintptr) uintptr {
return (ptr + 3) &^ 3
}
type __volatile bool
var rtc_wakeup __volatile
func rtc_sleep(ticks uint32) {
nrf.RTC0.INTENSET = nrf.RTC0_INTENSET_COMPARE0_Msk
rtc_wakeup = false
if ticks == 1 {
// Race condition (even in hardware) at ticks == 1.
// TODO: fix this in a better way by detecting it, like the manual
// describes.
ticks = 2
}
nrf.RTC0.CC[0] = (nrf.RTC0.COUNTER + nrf.RegValue(ticks)) & 0x00ffffff
for !rtc_wakeup {
arm.Asm("wfi")
}
}
//go:export RTC0_IRQHandler
func RTC0_IRQHandler() {
nrf.RTC0.INTENCLR = nrf.RTC0_INTENSET_COMPARE0_Msk
nrf.RTC0.EVENTS_COMPARE[0] = 0
rtc_wakeup = true
}