RTOS work threads

git-svn-id: svn://db.shs.com.ru/pip@687 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
2019-02-05 15:40:22 +00:00
parent b7ef5bfdcb
commit 30973842d1
10 changed files with 128 additions and 20 deletions

View File

@@ -18,7 +18,7 @@ set(COMPONENT_PRIV_REQUIRES pthread)
register_component() register_component()
set(PIP_FREERTOS ON) set(PIP_FREERTOS ON)
set(LIB OFF) set(LIB OFF)
include_directories(${IDF_INCLUDE_DIRECTORIES} ../newlib/platform_include ../libsodium/libsodium/src/libsodium/include ../libsodium/port_include) include_directories(${IDF_INCLUDE_DIRECTORIES} ../newlib/platform_include ../libsodium/libsodium/src/libsodium/include ../libsodium/port_include ../heap/include ../esp32/include ../spi_flash/include ../driver/include ../soc/include ../soc/esp32/include ../freertos/include)
#message("IDF_COMPILE_DEFINITIONS = ${IDF_COMPILE_DEFINITIONS}") #message("IDF_COMPILE_DEFINITIONS = ${IDF_COMPILE_DEFINITIONS}")
#message("IDF_INCLUDE_DIRECTORIES = ${IDF_INCLUDE_DIRECTORIES}") #message("IDF_INCLUDE_DIRECTORIES = ${IDF_INCLUDE_DIRECTORIES}")
add_compile_options(${IDF_COMPILE_OPTIONS}) add_compile_options(${IDF_COMPILE_OPTIONS})
@@ -28,4 +28,4 @@ add_definitions(-DGCC_NOT_5_2_0=0)
add_definitions(-DHAVE_CONFIG_H) add_definitions(-DHAVE_CONFIG_H)
add_subdirectory(pip) add_subdirectory(pip)
target_link_libraries(${COMPONENT_TARGET} pip) target_link_libraries(${COMPONENT_TARGET} pip)

View File

@@ -1,7 +1,33 @@
#include "pip.h" #include "pip.h"
class A: public PIObject {
PIOBJECT(A)
public:
A() {}
EVENT_HANDLER1(void, handlerA1, PIString, s) {piCout << "handlerA1 s" << s;}
EVENT_HANDLER1(void, handlerA1, float, f) {piCout << "handlerA1 f" << f;}
//uchar _[0x10];
};
class B: public PIObject {
PIOBJECT(B)
public:
B() {}
EVENT1(eventB1, float, f)
EVENT1(eventB2, PIString, s)
//uchar _[0x20];
};
int main() { int main() {
A a;
B b;
CONNECTU(&b, eventB1, &a, handlerA1)
CONNECTU(&b, eventB2, &a, handlerA1)
a.dump();
b.dump();
b.eventB1(0.33);
b.eventB2("str");
/*PIByteArray msg = PIByteArray::fromHex("0110f0f00001020001"); /*PIByteArray msg = PIByteArray::fromHex("0110f0f00001020001");
PIByteArray src = PIByteArray::fromHex("836f"); PIByteArray src = PIByteArray::fromHex("836f");
ushort s; memcpy(&s, src.data(), 2); ushort s; memcpy(&s, src.data(), 2);
@@ -9,9 +35,9 @@ int main() {
CRC_16 crc = CRC_16(0x8005, 0xFFFF, 0xFFFF, false); CRC_16 crc = CRC_16(0x8005, 0xFFFF, 0xFFFF, false);
piCout << PICoutManipulators::Hex << s; piCout << PICoutManipulators::Hex << s;
piCout << PICoutManipulators::Hex << crc.calculate(msg);*/ piCout << PICoutManipulators::Hex << crc.calculate(msg);*/
PIIODevice * ser = PIIODevice::createFromFullPath("ser://COM3:9600:7:e:1 (wo,bwr)"); //PIIODevice * ser = PIIODevice::createFromFullPath("ser://COM3:9600:7:e:1 (wo,bwr)");
piCout << ser << ser->constructVariant() << ser->constructFullPath(); //piCout << ser << ser->constructVariant() << ser->constructFullPath();
ser = PIIODevice::createFromVariant(ser->constructVariant()); //ser = PIIODevice::createFromVariant(ser->constructVariant());
piCout << ser << ser->constructVariant() << ser->constructFullPath(); //piCout << ser << ser->constructVariant() << ser->constructFullPath();
return 0; return 0;
} }

View File

@@ -28,6 +28,9 @@
# include "pidir.h" # include "pidir.h"
# include "piprocess.h" # include "piprocess.h"
#endif #endif
#ifdef ESP_PLATFORM
# include "esp_system.h"
#endif
#ifdef WINDOWS #ifdef WINDOWS
# include <winsock2.h> # include <winsock2.h>
extern FILETIME __pi_ftjan1970; extern FILETIME __pi_ftjan1970;
@@ -261,6 +264,14 @@ PIInit::PIInit() {
sinfo->architecture = uns.machine; sinfo->architecture = uns.machine;
} }
# endif # endif
#endif
#ifdef ESP_PLATFORM
esp_chip_info_t chip_info;
esp_chip_info(&chip_info);
sinfo->processorsCount = chip_info.cores;
sinfo->architecture = "Xtensa LX6";
//printf("silicon revision %d, ", chip_info.revision);
sinfo->OS_version = esp_get_idf_version();
#endif #endif
sinfo->OS_name = sinfo->OS_name =
#ifdef WINDOWS #ifdef WINDOWS

View File

@@ -30,7 +30,10 @@
//# include <crt_externs.h> //# include <crt_externs.h>
extern clock_serv_t __pi_mac_clock; extern clock_serv_t __pi_mac_clock;
#endif #endif
#ifdef FREERTOS
# include "freertos/FreeRTOS.h"
# include "freertos/task.h"
#endif
/*! \class PISystemTime /*! \class PISystemTime
* \brief System time * \brief System time
@@ -63,8 +66,12 @@ void piUSleep(int usecs) {
//printf("Sleep %d\n", usecs / 1000); //printf("Sleep %d\n", usecs / 1000);
if (usecs > 0) Sleep(usecs / 1000); if (usecs > 0) Sleep(usecs / 1000);
#else #else
# ifdef FREERTOS
vTaskDelay(usecs / 1000 / portTICK_PERIOD_MS);
# else
usecs -= PISystemTests::usleep_offset_us; usecs -= PISystemTests::usleep_offset_us;
if (usecs > 0) usleep(usecs); if (usecs > 0) usleep(usecs);
# endif
#endif #endif
} }
@@ -243,8 +250,18 @@ PISystemTime PISystemTime::current(bool precise_but_not_system) {
mach_timespec_t t_cur; mach_timespec_t t_cur;
clock_get_time(__pi_mac_clock, &t_cur); clock_get_time(__pi_mac_clock, &t_cur);
# else # else
# ifdef FREERTOS
timespec t_cur;
timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
gettimeofday(&tv, NULL);
t_cur.tv_sec = tv.tv_sec;
t_cur.tv_nsec = tv.tv_usec * 1000;
# else
timespec t_cur; timespec t_cur;
clock_gettime(0, &t_cur); clock_gettime(0, &t_cur);
# endif
# endif # endif
return PISystemTime(t_cur.tv_sec, t_cur.tv_nsec); return PISystemTime(t_cur.tv_sec, t_cur.tv_nsec);
#endif #endif

View File

@@ -25,13 +25,9 @@
#include "pistring.h" #include "pistring.h"
#ifdef FREERTOS #include <ctime>
# include "time.h" #ifdef QNX
#else # include <time.h>
# include <ctime>
# ifdef QNX
# include <time.h>
# endif
#endif #endif
//! \brief Sleep for "msecs" milliseconds //! \brief Sleep for "msecs" milliseconds
PIP_EXPORT void msleep(int msecs); PIP_EXPORT void msleep(int msecs);

View File

@@ -168,7 +168,11 @@ void PIIODevice::_init() {
setOptions(0); setOptions(0);
setReopenEnabled(true); setReopenEnabled(true);
setReopenTimeout(1000); setReopenTimeout(1000);
#ifdef FREERTOS
setThreadedReadBufferSize(512);
#else
setThreadedReadBufferSize(4096); setThreadedReadBufferSize(4096);
#endif
timer.setName("__S__reopen_timer"); timer.setName("__S__reopen_timer");
write_thread.setName("__S__write_thread"); write_thread.setName("__S__write_thread");
CONNECT2(void, void * , int, &timer, tickEvent, this, check_start); CONNECT2(void, void * , int, &timer, tickEvent, this, check_start);
@@ -251,7 +255,7 @@ void PIIODevice::run() {
return; return;
} }
if (!thread_started_) { if (!thread_started_) {
piMSleep(5); piMSleep(10);
//cout << "not started\n"; //cout << "not started\n";
return; return;
} }

View File

@@ -5,6 +5,6 @@
#define PIP_VERSION_MAJOR 1 #define PIP_VERSION_MAJOR 1
#define PIP_VERSION_MINOR 9 #define PIP_VERSION_MINOR 9
#define PIP_VERSION_REVISION 0 #define PIP_VERSION_REVISION 0
#define PIP_VERSION_SUFFIX "_alpha" #define PIP_VERSION_SUFFIX "_alpha_2"
#endif // PIVERSION_H #endif // PIVERSION_H

View File

@@ -19,6 +19,12 @@
#include "pisysteminfo.h" #include "pisysteminfo.h"
#include "piincludes_p.h" #include "piincludes_p.h"
#ifdef ESP_PLATFORM
# include "esp_heap_caps.h"
# include "esp_system.h"
# include "esp_spi_flash.h"
#endif
PISystemInfo::PISystemInfo() { PISystemInfo::PISystemInfo() {
processorsCount = 1; processorsCount = 1;
@@ -31,6 +37,36 @@ PISystemInfo * PISystemInfo::instance() {
} }
ullong PISystemInfo::totalRAM() {
#ifdef ESP_PLATFORM
multi_heap_info_t heap_info;
memset(&heap_info, 0, sizeof(multi_heap_info_t));
heap_caps_get_info(&heap_info, MALLOC_CAP_8BIT);
return heap_info.total_allocated_bytes + heap_info.total_free_bytes;
#endif
}
ullong PISystemInfo::freeRAM() {
#ifdef ESP_PLATFORM
multi_heap_info_t heap_info;
memset(&heap_info, 0, sizeof(multi_heap_info_t));
heap_caps_get_info(&heap_info, MALLOC_CAP_8BIT);
return heap_info.total_free_bytes;
#endif
}
ullong PISystemInfo::usedRAM() {
#ifdef ESP_PLATFORM
multi_heap_info_t heap_info;
memset(&heap_info, 0, sizeof(multi_heap_info_t));
heap_caps_get_info(&heap_info, MALLOC_CAP_8BIT);
return heap_info.total_allocated_bytes;
#endif
}
PIStringList PISystemInfo::mountRoots() { PIStringList PISystemInfo::mountRoots() {
PIStringList ret; PIStringList ret;
#ifdef WINDOWS #ifdef WINDOWS
@@ -178,6 +214,14 @@ PIVector<PISystemInfo::MountInfo> PISystemInfo::mountInfo() {
} }
# else # else
# endif # endif
#endif
#ifdef ESP_PLATFORM
esp_chip_info_t chip_info;
esp_chip_info(&chip_info);
m.device = ((chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded SPI flash" : "external SPI flash");
m.space_all = spi_flash_get_chip_size();
m.mount_point = "/";
ret << m;
#endif #endif
cache = ret; cache = ret;
return ret; return ret;

View File

@@ -38,6 +38,7 @@ public:
}; };
PIString ifconfigPath, execCommand, hostname, user, OS_name, OS_version, architecture; PIString ifconfigPath, execCommand, hostname, user, OS_name, OS_version, architecture;
ullong RAM_total, RAM_free;
PIDateTime execDateTime; PIDateTime execDateTime;
int processorsCount; int processorsCount;
@@ -45,6 +46,9 @@ public:
static PIVector<MountInfo> mountInfo(); static PIVector<MountInfo> mountInfo();
static PISystemInfo * instance(); static PISystemInfo * instance();
static ullong totalRAM();
static ullong freeRAM();
static ullong usedRAM();
}; };

View File

@@ -40,6 +40,12 @@ __THREAD_FUNC_RET__ thread_function_once(void * t) {PIThread::__thread_func_once
#define REGISTER_THREAD(t) __PIThreadCollection::instance()->registerThread(t) #define REGISTER_THREAD(t) __PIThreadCollection::instance()->registerThread(t)
#define UNREGISTER_THREAD(t) __PIThreadCollection::instance()->unregisterThread(t) #define UNREGISTER_THREAD(t) __PIThreadCollection::instance()->unregisterThread(t)
#ifdef FREERTOS
# define PTHREAD_MIN_MSLEEP 10.
#else
# define PTHREAD_MIN_MSLEEP 1.
#endif
/*! \class PIThread /*! \class PIThread
* \brief Thread class * \brief Thread class
* \details This class allow you exec your code in separate thread. * \details This class allow you exec your code in separate thread.
@@ -286,7 +292,7 @@ void PIThread::terminate() {
PIINTROSPECTION_UNREGISTER_THREAD(tid()); PIINTROSPECTION_UNREGISTER_THREAD(tid());
terminating = running_ = false; terminating = running_ = false;
tid_ = -1; tid_ = -1;
//piCout << "terminate" << thread; //piCout << "terminate" << PRIVATE->thread;
#ifndef WINDOWS #ifndef WINDOWS
# ifdef ANDROID # ifdef ANDROID
pthread_kill(PRIVATE->thread, SIGTERM); pthread_kill(PRIVATE->thread, SIGTERM);
@@ -423,7 +429,7 @@ void PIThread::__thread_func__(void * t) {
ct.tmr_.reset(); ct.tmr_.reset();
double sl(0.); double sl(0.);
while (1) { while (1) {
sl = piMind(ct.delay_ - ct.tmr_.elapsed_m(), 2.); sl = piMind(ct.delay_ - ct.tmr_.elapsed_m(), PTHREAD_MIN_MSLEEP);
#ifdef WINDOWS #ifdef WINDOWS
/*if (sl <= 1. && sl >= 0.) { /*if (sl <= 1. && sl >= 0.) {
piMSleep(1.); piMSleep(1.);
@@ -431,10 +437,9 @@ void PIThread::__thread_func__(void * t) {
}*/ }*/
#endif #endif
//printf("%f %f %f\n", double(ct.delay_), ct.tmr_.elapsed_m(), sl); //printf("%f %f %f\n", double(ct.delay_), ct.tmr_.elapsed_m(), sl);
if (ct.terminating) break;
if (sl <= 0.) break; if (sl <= 0.) break;
piMSleep(sl); piMSleep(sl);
if (ct.terminating)
break;
} }
} }
} }
@@ -511,3 +516,4 @@ void PIThread::__thread_func_once__(void * t) {
# endif # endif
#endif #endif
} }