Skip to content

Commit 6db11cf

Browse files
committed
Fix up Spresense build. It doesn't sleep.
1 parent d9e6815 commit 6db11cf

13 files changed

Lines changed: 72 additions & 186 deletions

File tree

ports/atmel-samd/mphalport.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@
3434
#include "supervisor/shared/tick.h"
3535

3636
// Global millisecond tick count (driven by SysTick interrupt).
37-
static inline mp_uint_t mp_hal_ticks_ms(void) {
38-
return supervisor_ticks_ms32();
39-
}
37+
#define mp_hal_ticks_ms() ((mp_uint_t) supervisor_ticks_ms32())
38+
4039
// Number of bytes in receive buffer
4140
volatile uint8_t usb_rx_count;
4241
volatile bool mp_cdc_enabled;

ports/cxd56/Makefile

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ SRC_SHARED_MODULE_EXPANDED = $(addprefix shared-bindings/, $(SRC_SHARED_MODULE))
152152
SRC_S = supervisor/cpu.s
153153

154154
SRC_C = \
155-
tick.c \
156155
background.c \
157156
fatfs_port.c \
158157
mphalport.c \
@@ -184,7 +183,7 @@ OBJ += $(addprefix $(BUILD)/, $(SRC_MOD:.c=.o))
184183
# List of sources for qstr extraction
185184
SRC_QSTR += $(SRC_C) $(SRC_SUPERVISOR) $(SRC_COMMON_HAL_EXPANDED) $(SRC_SHARED_MODULE_EXPANDED)
186185
# Sources that only hold QSTRs after pre-processing.
187-
SRC_QSTR_PREPROCESSOR +=
186+
SRC_QSTR_PREPROCESSOR +=
188187

189188
all: $(BUILD)/firmware.spk
190189

@@ -197,7 +196,7 @@ $(FIRMWARE):
197196
$(ECHO) "run make flash-bootloader again to flash bootloader."
198197
exit 1
199198

200-
$(BUILD)/libmpy.a: $(SPRESENSE_SDK) $(OBJ)
199+
$(BUILD)/libmpy.a: $(SPRESENSE_SDK) $(OBJ)
201200
$(ECHO) "AR $@"
202201
$(Q)$(AR) rcs $(BUILD)/libmpy.a $(OBJ)
203202

ports/cxd56/common-hal/microcontroller/Processor.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,16 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#include <stdbool.h> // for cxd56_clock.h
28+
#include <cxd56_clock.h>
2729
#include <sys/boardctl.h>
2830

2931
// For NAN: remove when not needed.
3032
#include <math.h>
3133
#include "py/mphal.h"
3234

3335
uint32_t common_hal_mcu_processor_get_frequency(void) {
34-
return mp_hal_ticks_cpu();
36+
return cxd56_get_cpu_baseclk();
3537
}
3638

3739
float common_hal_mcu_processor_get_temperature(void) {

ports/cxd56/common-hal/microcontroller/__init__.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#include <stdbool.h> // for cxd56_clock.h
28+
#include <cxd56_clock.h>
2729
#include <sys/boardctl.h>
2830

2931
#include "py/mphal.h"
@@ -42,8 +44,20 @@ const mcu_processor_obj_t common_hal_mcu_processor_obj = {
4244
},
4345
};
4446

47+
#define DELAY_CORRECTION (700)
48+
4549
void common_hal_mcu_delay_us(uint32_t delay) {
46-
mp_hal_delay_us(delay);
50+
if (delay) {
51+
unsigned long long ticks = cxd56_get_cpu_baseclk() / 1000000L * delay;
52+
if (ticks < DELAY_CORRECTION) return; // delay time already used in calculation
53+
54+
ticks -= DELAY_CORRECTION;
55+
ticks /= 6;
56+
// following loop takes 6 cycles
57+
do {
58+
__asm__ __volatile__("nop");
59+
} while(--ticks);
60+
}
4761
}
4862

4963
void common_hal_mcu_disable_interrupts(void) {

ports/cxd56/common-hal/pulseio/PulseIn.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*/
2626

2727
#include <arch/board/board.h>
28+
#include <sys/time.h>
2829

2930
#include "py/runtime.h"
3031
#include "py/mphal.h"
@@ -51,7 +52,9 @@ static int pulsein_set_config(pulseio_pulsein_obj_t *self, bool first_edge) {
5152

5253
static int pulsein_interrupt_handler(int irq, FAR void *context, FAR void *arg) {
5354
// Grab the current time first.
54-
uint32_t current_us = mp_hal_ticks_us();
55+
struct timeval tv;
56+
gettimeofday(&tv, NULL);
57+
uint32_t current_us = tv.tv_sec * 1000000 + tv.tv_usec;
5558

5659
pulseio_pulsein_obj_t *self = pulsein_objects[irq - CXD56_IRQ_EXDEVICE_0];
5760

ports/cxd56/common-hal/time/__init__.c

Lines changed: 0 additions & 45 deletions
This file was deleted.

ports/cxd56/mphalport.c

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -23,66 +23,3 @@
2323
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2424
* THE SOFTWARE.
2525
*/
26-
27-
#include <stdbool.h>
28-
#include <sys/time.h>
29-
#include <cxd56_clock.h>
30-
#include <sys/boardctl.h>
31-
32-
#include "py/mpstate.h"
33-
34-
#include "supervisor/shared/tick.h"
35-
36-
#define DELAY_CORRECTION (700)
37-
#define DELAY_INTERVAL (50)
38-
39-
void mp_hal_init(void) {
40-
boardctl(BOARDIOC_INIT, 0);
41-
}
42-
43-
mp_uint_t mp_hal_ticks_ms(void) {
44-
struct timeval tv;
45-
gettimeofday(&tv, NULL);
46-
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
47-
}
48-
49-
mp_uint_t mp_hal_ticks_us(void) {
50-
struct timeval tv;
51-
gettimeofday(&tv, NULL);
52-
return tv.tv_sec * 1000000 + tv.tv_usec;
53-
}
54-
55-
mp_uint_t mp_hal_ticks_cpu(void) {
56-
return cxd56_get_cpu_baseclk();
57-
}
58-
59-
void mp_hal_delay_ms(mp_uint_t delay) {
60-
uint64_t start_tick = supervisor_ticks_ms64();
61-
uint64_t duration = 0;
62-
while (duration < delay) {
63-
#ifdef MICROPY_VM_HOOK_LOOP
64-
MICROPY_VM_HOOK_LOOP
65-
#endif
66-
// Check to see if we've been CTRL-Ced by autoreload or the user.
67-
if(MP_STATE_VM(mp_pending_exception) == MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception)) ||
68-
MP_STATE_VM(mp_pending_exception) == MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_reload_exception))) {
69-
break;
70-
}
71-
duration = (supervisor_ticks_ms64() - start_tick);
72-
// TODO(tannewt): Go to sleep for a little while while we wait.
73-
}
74-
}
75-
76-
void mp_hal_delay_us(uint32_t us) {
77-
if (us) {
78-
unsigned long long ticks = mp_hal_ticks_cpu() / 1000000L * us;
79-
if (ticks < DELAY_CORRECTION) return; // delay time already used in calculation
80-
81-
ticks -= DELAY_CORRECTION;
82-
ticks /= 6;
83-
// following loop takes 6 cycles
84-
do {
85-
__asm__ __volatile__("nop");
86-
} while(--ticks);
87-
}
88-
}

ports/cxd56/mphalport.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,8 @@
3030
#include <sys/types.h>
3131

3232
#include "lib/utils/interrupt_char.h"
33+
#include "supervisor/shared/tick.h"
34+
35+
#define mp_hal_ticks_ms() ((mp_uint_t) supervisor_ticks_ms32())
3336

3437
#endif // MICROPY_INCLUDED_CXD56_MPHALPORT_H

ports/cxd56/supervisor/internal_flash.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ uint32_t supervisor_flash_get_block_count(void) {
5959
return CXD56_SPIFLASHSIZE >> PAGE_SHIFT;
6060
}
6161

62-
void supervisor_flash_flush(void) {
62+
void port_internal_flash_flush(void) {
6363
if (flash_sector == NO_SECTOR) {
6464
return;
6565
}

ports/cxd56/supervisor/port.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@
2525
*/
2626

2727
#include <stdint.h>
28+
2829
#include <sys/boardctl.h>
30+
#include <sys/time.h>
2931

3032
#include "sched/sched.h"
3133

3234
#include "boards/board.h"
3335

3436
#include "supervisor/port.h"
37+
#include "supervisor/shared/tick.h"
3538

3639
#include "common-hal/microcontroller/Pin.h"
3740
#include "common-hal/analogio/AnalogIn.h"
@@ -103,3 +106,41 @@ void port_set_saved_word(uint32_t value) {
103106
uint32_t port_get_saved_word(void) {
104107
return _ebss;
105108
}
109+
110+
volatile bool _tick_enabled;
111+
void board_timerhook(void)
112+
{
113+
// Do things common to all ports when the tick occurs
114+
if (_tick_enabled) {
115+
supervisor_tick();
116+
}
117+
}
118+
119+
uint64_t port_get_raw_ticks(uint8_t* subticks) {
120+
struct timeval tv;
121+
gettimeofday(&tv, NULL);
122+
long computed_subticks = tv.tv_usec * 1024 * 32 / 1000000;
123+
if (subticks != NULL) {
124+
*subticks = computed_subticks % 32;
125+
}
126+
127+
return tv.tv_sec * 1024 + computed_subticks / 32;
128+
}
129+
130+
// Enable 1/1024 second tick.
131+
void port_enable_tick(void) {
132+
_tick_enabled = true;
133+
}
134+
135+
// Disable 1/1024 second tick.
136+
void port_disable_tick(void) {
137+
_tick_enabled = false;
138+
}
139+
140+
void port_interrupt_after_ticks(uint32_t ticks) {
141+
}
142+
143+
void port_sleep_until_interrupt(void) {
144+
// TODO: Implement sleep.
145+
}
146+

0 commit comments

Comments
 (0)