void spi_get(SPI_CMD cmd, SPI_MISO *rx, int bytes, uint16_t wparam, uint32_t lparam) { lock_enter(&spi_lock); static SPI_MOSI tx3 __attribute__ ((aligned(16))); tx3.cmd = cmd; tx3.wparam = wparam; tx3.lparam = lparam; spi_scan(&tx3, rx, bytes); lock_leave(&spi_lock); rx->status=BUSY; while(rx->status==BUSY) { NextTaskL("spi_get"); // wait for response } }
int web_to_app(conn_t *c, char *s, int sl) { nbuf_t *nb; if (c->stop_data) return 0; nb = nbuf_dequeue(&c->w2a); if (!nb) return 0; assert(!nb->done && nb->buf && nb->len); memcpy(s, nb->buf, MIN(nb->len, sl)); nb->done = TRUE; NextTaskL("w2a"); return nb->len; }
int TaskSleep(u4_t usec) { TASK *t = cur_task; //printf("sleep T%02d %d usec\n", t->id, usec); t->deadline = usec? (timer_us() + usec) : 0; t->stopped = TRUE; do { NextTaskL("TaskSleep"); } while (!t->wakeup); //printf("woke T%02d %d usec\n", t->id, usec); //if (t->id==4 || t->id==6) printf("wo%d ", t->id); fflush(stdout); t->deadline = 0; t->stopped = FALSE; t->wakeup = FALSE; return t->wake_param; }
void lock_enter(lock_t *lock) { if (!lock->init) return; check_lock(); int token = lock->enter++; //bool dbg = (strcmp(lock->name, "&waterfall_hw_lock") == 0); bool dbg = false; bool waiting = false; while (token > lock->leave) { if (dbg && !waiting) { printf("LOCK t%d %s WAIT %s\n", TaskID(), TaskName(), lock->name); waiting = true; } NextTaskL("lock enter"); check_lock(); } if (dbg) printf("LOCK t%d %s ACQUIRE %s\n", TaskID(), TaskName(), lock->name); }
static int iterate_callback(struct mg_connection *mc, enum mg_event ev) { int ret; nbuf_t *nb; if (ev == MG_POLL && mc->is_websocket) { conn_t *c = (conn_t*) mc->connection_param; if (c == NULL) return MG_FALSE; // fixme: not reliable if (c->magic != CN_MAGIC) { //printf("bad CN_MAGIC 0x%x: mc = %s:%d\n", c->magic, mc->remote_ip, mc->remote_port); mc->connection_param = NULL; return MG_FALSE; } while (TRUE) { if (c->stop_data) break; nb = nbuf_dequeue(&c->a2w); //printf("a2w CHK port %d nb %p\n", mc->remote_port, nb); if (nb) { assert(!nb->done && nb->buf && nb->len); //if (c->type == STREAM_SOUND) printf("s%d ", mc->remote_port); //if (c->type == STREAM_WATERFALL) printf("w%d ", mc->remote_port); //fflush(stdout); //printf("a2w %d WEBSOCKET: %d %p\n", mc->remote_port, nb->len, nb->buf); ret = mg_websocket_write(mc, WS_OPCODE_BINARY, nb->buf, nb->len); if (ret<=0) printf("$$$$$$$$ socket write ret %d\n", ret); nb->done = TRUE; } else { break; } } } else { if (ev != MG_POLL) printf("$$$$$$$$ a2w %d OTHER: %d len %d\n", mc->remote_port, (int) ev, (int) mc->content_len); } NextTaskL("web callback"); return MG_TRUE; }
void web_server(void *param) { user_iface_t *ui = (user_iface_t *) param; struct mg_server *server = ui->server; const char *err; u4_t current_timer = 0, last_timer = timer_ms(); while (1) { mg_poll_server(server, 0); // passing 0 effects a poll current_timer = timer_ms(); if (time_diff(current_timer, last_timer) > WEB_SERVER_POLL_MS) { last_timer = current_timer; mg_iterate_over_connections(server, iterate_callback); } NextTaskL("web server"); } //mg_destroy_server(&server); }
static void spi_scan(SPI_MOSI *mosi, SPI_MISO *miso=&junk, int rbytes=0) { int i; assert(rbytes <= NSPI_RX); int tx_bytes = sizeof(SPI_MOSI); int tx_xfers = SPI_B2X(tx_bytes); int rx_bytes = sizeof(miso->status) + rbytes; int rx_xfers = SPI_B2X(rx_bytes); int arx_xfers = MAX(tx_xfers, prev->len_xfers); int arx_bytes = SPI_X2B(arx_xfers); //jks #if 0 static u4_t last_st; static float acc_st, acc2_st; static int big_rx; if (arx_bytes > 1024) big_rx = 1; u4_t st = timer_us(); float inc_st = (float) (st - last_st) / 1000.0; acc_st += inc_st; if (mosi->cmd == CmdSetWFFreq) acc2_st = 0; printf("SCAN +%.3f %6.3f %6.3f %12s %16s tx(%dX=%dB) rx(%dX=%dB) arx(%dX=%dB)\n", inc_st, acc_st, acc2_st, TaskName(), cmds[mosi->cmd], tx_xfers, tx_bytes, rx_xfers, rx_bytes, arx_xfers, arx_bytes); if (mosi->cmd == CmdDuplex && big_rx) { acc_st = 0; big_rx = 0; } else { } acc2_st += inc_st; last_st = st; #endif //memset(miso, 0xee, sizeof(*miso)); miso->len_xfers = rx_xfers; miso->cmd = mosi->cmd; rx_xfers = arx_xfers; if (mosi->cmd == CmdDuplex) mosi->cmd = CmdDummy; if (mosi->cmd == CmdNoDuplex) mosi->cmd = CmdDummy; for (;;) { peri_spi(SPI_HOST, mosi->msg, tx_xfers, // MOSI: new request prev->msg, rx_xfers); // MISO: response to previous caller's request evSpi(EV_SPILOOP, "spiScan", "peri_spi done"); // fixme: understand why is this needed (hangs w/o it) if (spi_delay) spin_us(spi_delay); else usleep(10); evSpi(EV_SPILOOP, "spiScan", "spin_us done"); if (prev->status != BUSY) break; // new request accepted? evSpi(EV_SPILOOP, "spiScan", "BUSY -> NextTask"); NextTaskL("spi_scan"); // wait and try again } //jks #if 0 printf("RX %d: ", prev->len_xfers); if (prev == &junk) { printf("(junk) "); } else { printf("%s ", cmds[prev->cmd]); } for (i=0; i<(prev->len_xfers+10); i++) { printf("%d:", i); #ifdef SPI_8 printf("%02x ", prev->msg[i]); #endif #ifdef SPI_16 printf("%04x ", prev->msg[i]); #endif #ifdef SPI_32 printf("%08x ", prev->msg[i]); #endif } printf("\n"); #endif u4_t status = prev->status; prev = miso; // next caller collects this for us pcmd=mosi->cmd; //if (status & 0x0fff) //printf("st %04x\n", status); //if (mosi->cmd == CmdGetRXCount) printf("C"); //if (mosi->cmd == CmdGetRX) printf("GRX\n"); static int ff; if (status & (1<<SPI_SFT)) { rx0_wakeup = 1; evSnd(EV_SND, "wakeup", ""); //printf("."); ff = 0; } else { rx0_wakeup = 0; if (!ff) { //printf("."); ff = 1; } } // process rx channel wakeup bits for (i=0; i<RX_CHANS; i++) { u4_t ch = 1<<(i+SPI_SFT); conn_t *c; if (status & ch) { c = &conns[i*2]; assert(c->type == STREAM_SOUND); //if (c->task && !c->stop_data) printf("wakeup %d\n", c->task); //if (c->task && !c->stop_data) { printf("%d:%d ", i, c->task); fflush(stdout); } if (c->task && !c->stop_data) TaskWakeup(c->task, FALSE, 0); } } }
void app_to_web(conn_t *c, char *s, int sl) { if (c->stop_data) return; nbuf_allocq(&c->a2w, s, sl); NextTaskL("a2w"); }