static u32t _std threadfunc2(void *arg) { qserr res; u64t now = sys_clock(); int i1; double f1; res = mt_tlsset(tlsvar, now); if (res) log_printf("I'm tid %2u, mt_tlsset() = %05X\n", mt_getthread(), res); log_printf("I'm tid %2u and clock now is %LX\n", mt_getthread(), now); i1 = random(2000); f1 = i1/2.5; log_printf("I'm tid %2u, float test: x=%i==%g 0.4x=%g sqrt=%g\n", mt_getthread(), i1, f1*2.5, f1, sqrt(i1)); res = mt_muxcapture(mutex); if (res) log_printf("I'm tid %2u, mt_muxcapture() = %05X\n", mt_getthread(), res); threads++; log_printf("I'm tid %2u in mutex and clock is %LX\n", mt_getthread(), sys_clock()); // if (threads==TEST_THREADS-2) mt_dumptree(); res = mt_muxrelease(mutex); if (res) { log_printf("I'm tid %2u, mt_muxrelease() = %05X\n", mt_getthread(), res); mt_dumptree(); } return 0; }
void comp_test(void) { // Called from Main Loop more often than once per 10 ms if ((sys_clock() & 0x3F) == 0) { // Once per 0.64 seccond test SPI // if (!IC_IS_INIT(IC_USED)) { // IC_INIT(IC_USED, // ICT_FCY2 | ICM_RAISE, 0, SYSCLK_IPL+1); // stage = 0; // Start test // } } // if (!IC_IS_INIT(IC_USED)) return; switch(stage) { case 0: // START_PWM(5000); ++stage; break; // Next test case 1: __asm__ volatile ("nop\nnop"); ++stage; break; // Next test default: stage = 0; // OC_PWOFF(OC_USED); break; } // switch(stage) }
void logic_pinger_restart(void) { struct logic_pinger_state_s *state = logic_pinger_state; struct ping_state_s *ping = &ping_state[LOGIC_PING_CH]; for(int i=0; i<LOGIC_MAX_PINGER; ++i, ++state, ++ping) { ping->state = PING_RESET; state->result = 0xff; state->next_time = sys_clock() + 6000; // changed to 6s to resolve hostname } }
void logic_pinger_exec(void) { int i; struct logic_pinger_setup_s *setup = logic_pinger_setup; struct logic_pinger_state_s *state = logic_pinger_state; struct ping_state_s *ping = &ping_state[LOGIC_PING_CH]; systime_t time = sys_clock(); for(i=0; i<LOGIC_MAX_PINGER; ++i, ++setup, ++state, ++ping) { if(ping->state == PING_RESET && time >= state->next_time) { state->next_time = time + setup->period * 1000; if(valid_ip(setup->ip) == 0) { if(setup->hostname[0] == 0) { // no ip or hostname, skip state->result = 0xfe; continue; } else { // dns unresolved, treat as ping fail state->result = 0; continue; } } util_cpy(setup->ip, ping->ip, 4); ping->timeout = setup->timeout; ping->max_retry = 4; // 26.09.2013 fixed value state->attempt_counter = 0; ping->state = PING_START; } else if(ping->state == PING_COMPLETED) { state->result = ping->result; ping->state = PING_RESET; } } // for channels }
void tstat_exec(void) { static systime_t next_time = 1000; systime_t time = sys_clock(); if(time < next_time) return; next_time = time + 1000; struct tstat_setup_s *setup = tstat_setup; unsigned char *state = tstat_state; int threshold, value, status; for(int i=0; i<TSTAT_MAX_CHANNEL; ++i, ++setup, ++state) { #ifdef RELHUM_MODULE if(setup->sensor_no > TERMO_N_CH) { *state = 0xfe; continue; } // wrong sensor (termo + RH) if(setup->sensor_no == TERMO_N_CH) { status = rh_status_h; value = rh_real_h; } else { status = termo_state[setup->sensor_no].status; value = termo_state[setup->sensor_no].value; } #else if(setup->sensor_no >= TERMO_N_CH) { *state = 0xfe; continue; } // wrong sensor status = termo_state[setup->sensor_no].status; value = termo_state[setup->sensor_no].value; #endif // RELHUM_MODULE if(status == 0) { *state = 0xfe; continue; } // sensor fault // +1 -1 to use exact > and < on comparison, i.e. exact switch threshold (setpoint +/- hyst) // min hyst is 1 deg.C => hyst zone is 2 deg.c (exact > and < on discrete integer T measurements) threshold = setup->setpoint; if(*state == 1) threshold = threshold - setup->hyst + 1; if(*state == 0) threshold = threshold + setup->hyst - 1; if(value > threshold) *state = 1; if(value < threshold) *state = 0; } }
void main(int argc, char *argv[]) { mt_tid tid; mt_waitentry we[TEST_THREADS+1]; u32t ii, sig; clock_t stm; qserr res; qshandle que; mt_ctdata tsd; char *cp; if (!mt_active()) { res = mt_initialize(); if (res) { cmd_shellerr(EMSG_QS, res, "MTLIB initialization error: "); return; } } tlsvar = mt_tlsalloc(); cp = argv[1] ? strdup(argv[1]) : 0; memset(&tsd, 0, sizeof(tsd)); tsd.size = sizeof(tsd); tsd.stacksize = 8192; tsd.onenter = start_hook; tid = mt_createthread(threadfunc1, 0, &tsd, cp); log_printf("mt_createthread() = %X\n", tid); mt_waitthread(tid); res = mt_muxcreate(0, "test mutex", &mutex); if (res) { cmd_shellerr(EMSG_QS, res, "Mutex creation error: "); return; } for (ii=0; ii<TEST_THREADS; ii++) { ta[ii] = mt_createthread(threadfunc2, MTCT_SUSPENDED, 0, 0); we[ii].htype = QWHT_TID; we[ii].tid = ta[ii]; we[ii].group = ii&1 ? 2 : 1; we[ii].resaddr = 0; } we[TEST_THREADS].htype = QWHT_CLOCK; we[TEST_THREADS].group = 0; we[TEST_THREADS].tme = (stm = sys_clock()) + CLOCKS_PER_SEC; /* launch a thread to resume all 20, else some of them can exit before we enter mt_waitobject() */ mt_createthread(threadfunc3, 0, 0, cp); /* AND logic for both groups - i.e. all odd or all even finished threads will signal here (or 1 sec timeout). */ res = mt_waitobject(we, TEST_THREADS+1, 3, &sig); stm = sys_clock() - stm; if (res) { cmd_shellerr(EMSG_QS, res, "mt_waitobject() error: "); } else { printf("Time spent: %Lu mks, ", stm); switch (sig) { case 0: printf("timer win!\n"); break; case 1: printf("group 1 win!\n"); break; case 2: printf("group 2 win!\n"); break; default: printf("???\n"); } } // wait for remaining threads for (sig=0;!sig;) { mt_muxcapture(mutex); if (threads==TEST_THREADS) sig = 1; mt_muxrelease(mutex); } res = mt_closehandle(mutex); if (res) { cmd_shellerr(EMSG_QS, res, "Mutex free error: "); return; } res = mt_eventcreate(0, "test_event", &event); if (res) { cmd_shellerr(EMSG_QS, res, "Event creation error: "); return; } /* just a test of QEVA_PULSEONE event handling: create 20 threads and wait for an event in all of them. Then, in loop - call pulse one and wait for any thread 20 times */ for (ii=1; ii<=TEST_THREADS; ii++) mt_createthread(threadfunc5, 0, 0, (void*)ii); /* let threads above to reach mt_waithandle() string - else first pulse will be lost and code stopped on mt_waitthread() forever */ usleep(32000); for (ii=0; ii<TEST_THREADS; ii++) { mt_eventact(event, QEVA_PULSEONE); mt_waitthread(0); } // this test will fail on PXE (no hlp_fopen()) log_printf("boot file i/o sync test\n", tid); mt_createthread(threadfunc4, 0, 0, 0); mt_createthread(threadfunc4, 0, 0, 0); /* event scheduling test. Post 4 events to the future and then check delivery time */ res = qe_create(0, &que); if (res) { cmd_shellerr(EMSG_QS, res, "Queue creation error: "); return; } else { static u32t timediff[TEST_SCHEDULES] = { 32, 38, 64, 126 }; qe_eid eids[TEST_SCHEDULES]; stm = sys_clock(); for (ii=0; ii<TEST_SCHEDULES; ii++) { eids[ii] = qe_schedule(que, stm+timediff[ii]*1000, ii+1, 0, 0, 0); if (!eids[ii]) printf("Schedule # %u error!\n", ii+1); else if (eids[ii]==QEID_POSTED) printf("Schedule # %u is too late!\n", ii+1); } for (ii=0; ii<TEST_SCHEDULES; ii++) { qe_event *ev = qe_waitevent(que, 5000); if (!ev) { printf("Event is lost? (%Lu)\n", sys_clock() - stm); break; } else { clock_t now = sys_clock(), wanted = stm+timediff[ev->code-1]*1000; printf("Event %u, time now %LX, should be %LX, diff = %Li mks\n", ev->code, now, wanted, now-wanted); free(ev); } } } }