Commit b7d27e31 authored by Damien George's avatar Damien George
Browse files

cc3200: Refactor "ticks" functions to use common extmod implementation.

The port now uses the common mp_utime_ticks_{ms,us,cpu,add,diff} functions
from extmod/utime_mphal.c.

The mp_utime_sleep_XXX functions are still cc3200-specific because they
handle the GIL differently to the ones in extmod.

The files misc/mpsystick.[ch] have been removed because they contain 2
unused functions, and the other remaining function is renamed to
mp_hal_ticks_us and moved to hal/cc3200_hal.c.
parent a4a439ca
......@@ -77,7 +77,6 @@ APP_MISC_SRC_C = $(addprefix misc/,\
mpirq.c \
mperror.c \
mpexception.c \
mpsystick.c \
)
APP_MODS_SRC_C = $(addprefix mods/,\
......
......@@ -108,6 +108,19 @@ mp_uint_t mp_hal_ticks_ms(void) {
return HAL_tickCount;
}
// The SysTick timer counts down at HAL_FCPU_HZ, so we can use that knowledge
// to grab a microsecond counter.
mp_uint_t mp_hal_ticks_us(void) {
mp_uint_t irq_state = disable_irq();
uint32_t counter = SysTickValueGet();
uint32_t milliseconds = mp_hal_ticks_ms();
enable_irq(irq_state);
uint32_t load = SysTickPeriodGet();
counter = load - counter; // Convert from decrementing to incrementing
return (milliseconds * 1000) + ((counter * 1000) / load);
}
void mp_hal_delay_ms(mp_uint_t delay) {
// only if we are not within interrupt context and interrupts are enabled
if ((HAL_NVIC_INT_CTRL_REG & HAL_VECTACTIVE_MASK) == 0 && query_irq() == IRQ_STATE_ENABLED) {
......@@ -211,4 +224,3 @@ static void hal_TickInit (void) {
MAP_SysTickEnable();
}
#endif
......@@ -30,6 +30,9 @@
#include <stdint.h>
#include <stdbool.h>
#include "hal/utils.h"
#include "hal/systick.h"
/******************************************************************************
DEFINE CONSTANTS
******************************************************************************/
......@@ -64,4 +67,7 @@ extern void HAL_SystemDeInit (void);
extern void HAL_IncrementTick(void);
extern void mp_hal_set_interrupt_char (int c);
#define mp_hal_delay_us(usec) UtilsDelay(UTILS_DELAY_US_TO_COUNT(usec))
#define mp_hal_ticks_cpu() (SysTickPeriodGet() - SysTickValueGet())
#endif /* CC3200_LAUNCHXL_HAL_CC3200_HAL_H_ */
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
* Copyright (c) 2015 Daniel Campora
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "py/mpconfig.h"
#include "py/obj.h"
#include "py/mphal.h"
#include "mpsystick.h"
#include "systick.h"
#include "inc/hw_types.h"
#include "inc/hw_nvic.h"
#ifdef USE_FREERTOS
#include "FreeRTOS.h"
#include "task.h"
#endif
bool sys_tick_has_passed(uint32_t start_tick, uint32_t delay_ms) {
return mp_hal_ticks_ms() - start_tick >= delay_ms;
}
// waits until at least delay_ms milliseconds have passed from the sampling of
// startTick. Handles overflow properly. Assumes stc was taken from
// mp_hal_ticks_ms() some time before calling this function.
void sys_tick_wait_at_least(uint32_t start_tick, uint32_t delay_ms) {
#ifdef USE_FREERTOS
vTaskDelay (delay_ms / portTICK_PERIOD_MS);
#else
while (!sys_tick_has_passed(start_tick, delay_ms)) {
__WFI(); // enter sleep mode, waiting for interrupt
}
#endif
}
// The SysTick timer counts down at HAL_FCPU_HZ, so we can use that knowledge
// to grab a microsecond counter.
// We assume that mp_hal_ticks_ms returns milliseconds.
uint32_t sys_tick_get_microseconds(void) {
mp_uint_t irq_state = disable_irq();
uint32_t counter = SysTickValueGet();
uint32_t milliseconds = mp_hal_ticks_ms();
enable_irq(irq_state);
uint32_t load = SysTickPeriodGet();
counter = load - counter; // Convert from decrementing to incrementing
return (milliseconds * 1000) + ((counter * 1000) / load);
}
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
* Copyright (c) 2015 Daniel Campora
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MPSYSTICK_H
#define MPSYSTICK_H
void sys_tick_wait_at_least(uint32_t stc, uint32_t delay_ms);
bool sys_tick_has_passed(uint32_t stc, uint32_t delay_ms);
uint32_t sys_tick_get_microseconds(void);
#endif // MPSYSTICK_H
......@@ -33,6 +33,7 @@
#include "py/obj.h"
#include "py/smallint.h"
#include "py/mphal.h"
#include "extmod/utime_mphal.h"
#include "timeutils.h"
#include "inc/hw_types.h"
#include "inc/hw_ints.h"
......@@ -41,7 +42,6 @@
#include "prcm.h"
#include "systick.h"
#include "pybrtc.h"
#include "mpsystick.h"
#include "mpexception.h"
#include "utils.h"
......@@ -143,38 +143,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_ms_obj, time_sleep_ms);
STATIC mp_obj_t time_sleep_us (mp_obj_t usec_in) {
mp_int_t usec = mp_obj_get_int(usec_in);
if (usec > 0) {
UtilsDelay(UTILS_DELAY_US_TO_COUNT(usec));
mp_hal_delay_us(usec);
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_us_obj, time_sleep_us);
STATIC mp_obj_t time_ticks_ms(void) {
// We want to "cast" the 32 bit unsigned into a 30-bit small-int
return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_ms() & MP_SMALL_INT_POSITIVE_MASK);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(time_ticks_ms_obj, time_ticks_ms);
STATIC mp_obj_t time_ticks_us(void) {
// We want to "cast" the 32 bit unsigned into a 30-bit small-int
return MP_OBJ_NEW_SMALL_INT(sys_tick_get_microseconds() & MP_SMALL_INT_POSITIVE_MASK);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(time_ticks_us_obj, time_ticks_us);
STATIC mp_obj_t time_ticks_cpu(void) {
// We want to "cast" the 32 bit unsigned into a 30-bit small-int
return MP_OBJ_NEW_SMALL_INT((SysTickPeriodGet() - SysTickValueGet()) & MP_SMALL_INT_POSITIVE_MASK);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(time_ticks_cpu_obj, time_ticks_cpu);
STATIC mp_obj_t time_ticks_diff(mp_obj_t t0, mp_obj_t t1) {
// We want to "cast" the 32 bit unsigned into a 30-bit small-int
uint32_t start = mp_obj_get_int(t0);
uint32_t end = mp_obj_get_int(t1);
return MP_OBJ_NEW_SMALL_INT((end - start) & MP_SMALL_INT_POSITIVE_MASK);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(time_ticks_diff_obj, time_ticks_diff);
STATIC const mp_map_elem_t time_module_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_utime) },
......@@ -186,10 +160,11 @@ STATIC const mp_map_elem_t time_module_globals_table[] = {
// MicroPython additions
{ MP_OBJ_NEW_QSTR(MP_QSTR_sleep_ms), (mp_obj_t)&time_sleep_ms_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_sleep_us), (mp_obj_t)&time_sleep_us_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_ms), (mp_obj_t)&time_ticks_ms_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_us), (mp_obj_t)&time_ticks_us_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_cpu), (mp_obj_t)&time_ticks_cpu_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_diff), (mp_obj_t)&time_ticks_diff_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_ms), (mp_obj_t)&mp_utime_ticks_ms_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_us), (mp_obj_t)&mp_utime_ticks_us_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_cpu), (mp_obj_t)&mp_utime_ticks_cpu_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_add), (mp_obj_t)&mp_utime_ticks_add_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_diff), (mp_obj_t)&mp_utime_ticks_diff_obj },
};
STATIC MP_DEFINE_CONST_DICT(time_module_globals, time_module_globals_table);
......
......@@ -116,6 +116,7 @@
#define MICROPY_PY_UHEAPQ (0)
#define MICROPY_PY_UHASHLIB (0)
#define MICROPY_PY_USELECT (1)
#define MICROPY_PY_UTIME_MP_HAL (1)
#define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1)
#define MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE (0)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment