Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

High Res Timer added to Nanoframework.Hardware.Esp32 #866

Merged
merged 2 commits into from
Sep 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMake/Modules/FindnanoFramework.Hardware.Esp32.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ set(nanoFramework.Hardware.Esp32_SRCS
nanoFramework_hardware_esp32_native.cpp
nanoFramework_hardware_esp32_native_Hardware_Esp32_sleep.cpp
nanoFramework_hardware_esp32_native_Hardware_Esp32_Logging.cpp

nanoFramework_hardware_esp32_native_Hardware_Esp32_HighResTimer.cpp
)

foreach(SRC_FILE ${nanoFramework.Hardware.Esp32_SRCS})
Expand Down
3 changes: 2 additions & 1 deletion targets/FreeRTOS/ESP32_DevKitC/Include/esp32_os.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@
#include "esp_wpa2.h"
#include "esp_eth.h"
#include "esp_event_loop.h"
#include "esp_timer.h"

#include "spi_master.h"
#include "gpio.h"
#include "i2c.h"
#include "uart.h"
#include "ledc.h"
#include "adc.h"

#include "timer.h"

// Uncomment to support Ethernet
//#define ESP32_ETHERNET_SUPPORT 1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (c) 2017 The nanoFramework project contributors
// Copyright (c) 2018 The nanoFramework project contributors
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
// See LICENSE file in the project root for full license information.
//
Expand Down Expand Up @@ -33,16 +33,44 @@ static const CLR_RT_MethodHandler method_lookup[] =
Library_nanoFramework_hardware_esp32_native_nanoFramework_Hardware_Esp32_Sleep::NativeGetWakeupTouchpad___STATIC__nanoFrameworkHardwareEsp32SleepTouchPad,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
Library_nanoFramework_hardware_esp32_native_nanoFramework_Hardware_Esp32_HighResTimer::NativeEspTimerCreate___I4,
Library_nanoFramework_hardware_esp32_native_nanoFramework_Hardware_Esp32_HighResTimer::NativeEspTimerDispose___VOID,
Library_nanoFramework_hardware_esp32_native_nanoFramework_Hardware_Esp32_HighResTimer::NativeStop___VOID,
Library_nanoFramework_hardware_esp32_native_nanoFramework_Hardware_Esp32_HighResTimer::NativeStartOneShot___VOID__U8,
Library_nanoFramework_hardware_esp32_native_nanoFramework_Hardware_Esp32_HighResTimer::NativeStartPeriodic___VOID__U8,
NULL,
Library_nanoFramework_hardware_esp32_native_nanoFramework_Hardware_Esp32_HighResTimer::NativeGetCurrent___STATIC__U8,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
};

const CLR_RT_NativeAssemblyData g_CLR_AssemblyNative_nanoFramework_Hardware_Esp32 =
{
"nanoFramework.Hardware.Esp32",
0x3903FAA7,
0xFF4537C1,
method_lookup,
{ 1, 0, 0, 0}
{ 1, 0, 0, 0 }
};




Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,45 @@ struct Library_nanoFramework_hardware_esp32_native_nanoFramework_Hardware_Esp32_

};

struct Library_nanoFramework_hardware_esp32_native_nanoFramework_Hardware_Esp32_HighResEventListener
{
static const int FIELD__HighResTimers = 1;


//--//

};

struct Library_nanoFramework_hardware_esp32_native_nanoFramework_Hardware_Esp32_HighResTimer
{
static const int FIELD_STATIC__s_eventListener = 0;

static const int FIELD___timerHandle = 1;
static const int FIELD___disposedValue = 2;
static const int FIELD___syncLock = 3;
static const int FIELD__OnHighResTimerExpired = 4;

NANOCLR_NATIVE_DECLARE(NativeEspTimerCreate___I4);
NANOCLR_NATIVE_DECLARE(NativeEspTimerDispose___VOID);
NANOCLR_NATIVE_DECLARE(NativeStop___VOID);
NANOCLR_NATIVE_DECLARE(NativeStartOneShot___VOID__U8);
NANOCLR_NATIVE_DECLARE(NativeStartPeriodic___VOID__U8);
NANOCLR_NATIVE_DECLARE(NativeGetCurrent___STATIC__U8);

//--//

};

struct Library_nanoFramework_hardware_esp32_native_nanoFramework_Hardware_Esp32_HighResTimerEvent
{
static const int FIELD__EventType = 3;
static const int FIELD__TimerHandle = 4;


//--//

};

extern const CLR_RT_NativeAssemblyData g_CLR_AssemblyNative_nanoFramework_Hardware_Esp32;

#endif //_HARDWARE_ESP32_NATIVE_H_
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
//
// Copyright (c) 2018 The nanoFramework project contributors
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
// See LICENSE file in the project root for full license information.
//

#include "nanoFramework_hardware_esp32_native.h"

#define MAX_HRTIMERS 10

esp_timer_handle_t hrtimers[MAX_HRTIMERS] = {};

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// !!! KEEP IN SYNC WITH nanoFramework.Hardware.Esp32.HighResTimerEventType (in managed code) !!! //
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

enum HighResTimerEventType
{
TimerExpired = 101
};

static int FindNextTimerIndex()
{
for( int index=0; index<MAX_HRTIMERS; index++)
{
if ( hrtimers[index] == 0 ) return index;
}

return -1;
}

static void HRtimer_callback(void* arg)
{
esp_timer_handle_t timer_handle = hrtimers[(int) arg];
PostManagedEvent( EVENT_CUSTOM, HighResTimerEventType::TimerExpired, 0, (uint32_t)timer_handle );
}

HRESULT Library_nanoFramework_hardware_esp32_native_nanoFramework_Hardware_Esp32_HighResTimer::NativeEspTimerCreate___I4( CLR_RT_StackFrame& stack )
{
NANOCLR_HEADER();
{
int index = FindNextTimerIndex();
if ( index < 0) NANOCLR_SET_AND_LEAVE(CLR_E_TOO_MANY_OPEN_HANDLES);

esp_timer_handle_t out_handle;
esp_timer_create_args_t create_args = {};
create_args.callback = &HRtimer_callback;
create_args.arg = (void*)index;

esp_err_t err = esp_timer_create(&create_args, &out_handle);
if ( err != ESP_OK)
{
NANOCLR_SET_AND_LEAVE(CLR_E_INVALID_PARAMETER);
}
hrtimers[index] = out_handle;
stack.SetResult_I4((int32_t)out_handle);
}
NANOCLR_NOCLEANUP();
}

HRESULT Library_nanoFramework_hardware_esp32_native_nanoFramework_Hardware_Esp32_HighResTimer::NativeEspTimerDispose___VOID( CLR_RT_StackFrame& stack )
{
NANOCLR_HEADER();
{
CLR_RT_HeapBlock* pThis = stack.This(); FAULT_ON_NULL(pThis);
esp_timer_handle_t timer = (esp_timer_handle_t)pThis[ FIELD___timerHandle ].NumericByRefConst().s4;
esp_timer_delete(timer);

for( int index=0; index<MAX_HRTIMERS; index++)
{
if ( hrtimers[index] == timer )
{
hrtimers[index] = 0;
break;
}
}
}
NANOCLR_NOCLEANUP();
}

HRESULT Library_nanoFramework_hardware_esp32_native_nanoFramework_Hardware_Esp32_HighResTimer::NativeStop___VOID( CLR_RT_StackFrame& stack )
{
NANOCLR_HEADER();
{
CLR_RT_HeapBlock* pThis = stack.This(); FAULT_ON_NULL(pThis);
if(pThis[FIELD___disposedValue ].NumericByRef().u1 != 0)
{
NANOCLR_SET_AND_LEAVE(CLR_E_OBJECT_DISPOSED);
}

esp_timer_handle_t timer = (esp_timer_handle_t)pThis[ FIELD___timerHandle ].NumericByRefConst().s4;
esp_timer_stop(timer);
}
NANOCLR_NOCLEANUP();
}

HRESULT Library_nanoFramework_hardware_esp32_native_nanoFramework_Hardware_Esp32_HighResTimer::NativeStartOneShot___VOID__U8( CLR_RT_StackFrame& stack )
{
NANOCLR_HEADER();
{
CLR_RT_HeapBlock* pThis = stack.This(); FAULT_ON_NULL(pThis);

if(pThis[FIELD___disposedValue ].NumericByRef().u1 != 0)
{
NANOCLR_SET_AND_LEAVE(CLR_E_OBJECT_DISPOSED);
}

esp_timer_handle_t timer = (esp_timer_handle_t)pThis[ FIELD___timerHandle ].NumericByRefConst().s4;
uint64_t timeout_us = (uint64_t)stack.Arg1().NumericByRef().u8;

esp_timer_stop(timer);
esp_timer_start_once(timer, timeout_us);
}
NANOCLR_NOCLEANUP();
}

HRESULT Library_nanoFramework_hardware_esp32_native_nanoFramework_Hardware_Esp32_HighResTimer::NativeStartPeriodic___VOID__U8( CLR_RT_StackFrame& stack )
{
NANOCLR_HEADER();
{
CLR_RT_HeapBlock* pThis = stack.This(); FAULT_ON_NULL(pThis);

if(pThis[FIELD___disposedValue ].NumericByRef().u1 != 0)
{
NANOCLR_SET_AND_LEAVE(CLR_E_OBJECT_DISPOSED);
}

esp_timer_handle_t timer = (esp_timer_handle_t)pThis[ FIELD___timerHandle ].NumericByRefConst().s4;
uint64_t period_us = (uint64_t)stack.Arg1().NumericByRef().u8;

esp_timer_stop(timer);
esp_timer_start_periodic(timer, period_us);
}
NANOCLR_NOCLEANUP();
}

HRESULT Library_nanoFramework_hardware_esp32_native_nanoFramework_Hardware_Esp32_HighResTimer::NativeGetCurrent___STATIC__U8( CLR_RT_StackFrame& stack )
{
NANOCLR_HEADER();
{
uint64_t timer_val = esp_timer_get_time();
stack.SetResult_U8(timer_val);
}
NANOCLR_NOCLEANUP_NOLABEL();
}