/* Encode a device model */ int db_model_enc(struct microjs_json_parser * jsn, struct microjs_val * val, unsigned int opt, void * ptr) { struct db_dev_model ** rootp = (struct db_dev_model **)ptr; struct db_dev_model model; int ret; memset(&model, 0, sizeof(struct db_dev_model)); model.module = opt; model.next = *rootp; if (opt) model.sim = module_sim_default(); else model.sim = sensor_sim_default(); if ((ret = microjs_json_parse_obj(jsn, model_desc, &model)) < 0) { DCC_LOG(LOG_ERROR, "microjs_json_parse_obj() failed!"); return ret; } DCC_LOG6(LOG_TRACE, "tbl[%d, %d] alm[%d, %d] tst[%d, %d]", model.pw4lut.tbl.idx, model.pw4lut.tbl.cnt, model.pw4lut.alm.idx, model.pw4lut.alm.cnt, model.pw4lut.tst.idx, model.pw4lut.tst.cnt); ret = db_stack_push(&model, sizeof(struct db_dev_model), (void **)rootp); return ret; }
void __attribute__((noreturn)) serial_recv_task(struct vcom * vcom) { struct serial_dev * serial = vcom->serial; struct usb_cdc_class * cdc = vcom->cdc; uint8_t buf[VCOM_BUF_SIZE]; int len; DCC_LOG1(LOG_TRACE, "[%d] started.", thinkos_thread_self()); /* wait for line configuration */ usb_cdc_acm_lc_wait(cdc); /* enable serial */ serial_enable(serial); for (;;) { len = serial_read(serial, buf, VCOM_BUF_SIZE, 1000); if (len > 0) { // dbg_write(buf, len); if (vcom->mode == VCOM_MODE_CONVERTER) { led_flash(LED_AMBER, 50); usb_cdc_write(cdc, buf, len); } if (vcom->mode == VCOM_MODE_SDU_TRACE) { led_flash(LED_AMBER, 50); sdu_decode(buf, len); } #if RAW_TRACE if (len == 1) DCC_LOG1(LOG_TRACE, "RX: %02x", buf[0]); else if (len == 2) DCC_LOG2(LOG_TRACE, "RX: %02x %02x", buf[0], buf[1]); else if (len == 3) DCC_LOG3(LOG_TRACE, "RX: %02x %02x %02x", buf[0], buf[1], buf[2]); else if (len == 4) DCC_LOG4(LOG_TRACE, "RX: %02x %02x %02x %02x", buf[0], buf[1], buf[2], buf[3]); else if (len == 5) DCC_LOG5(LOG_TRACE, "RX: %02x %02x %02x %02x %02x", buf[0], buf[1], buf[2], buf[3], buf[4]); else if (len == 6) DCC_LOG6(LOG_TRACE, "RX: %02x %02x %02x %02x %02x %02x", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]); else if (len == 7) DCC_LOG7(LOG_TRACE, "RX: %02x %02x %02x %02x %02x %02x %02x ", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]); else DCC_LOG8(LOG_TRACE, "RX: %02x %02x %02x %02x %02x %02x " "%02x %02x ...", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]); #endif #if SDU_TRACE RX(buf, len); #endif } } }
int stm32l1xx_flash_bsy_wait(cm3ice_ctrl_t * ctrl) { int ret = -1; uint32_t sr; int again; for (again = 4096; again > 0; again--) { cm3ice_rd32(ctrl, STM32F_BASE_FLASH + FLASH_SR, &sr); if (sr & ERR) { DCC_LOG6(LOG_WARNING, "%s%s%s%s%s%s", sr & RDERR ? "RDERR" : "", sr & OPTVERRUSR ? "OPTVERRUSR" : "", sr & OPTVERR ? "OPTVERR " : "", sr & SIZERR ? "SIZERR " : "", sr & PGAERR ? "PGAERR" : "", sr & WRPERR ? "WRPERR" : ""); break; } if ((sr & BSY) == 0) { ret = 0; break; } } return ret; }
int stm32_flash_erase(unsigned int offs, unsigned int len) { struct stm32_flash * flash = STM32_FLASH; uint32_t addr; uint32_t pecr; int rem = len; int cnt; offs &= ~(FLASH_PAGE_SIZE - 1); addr = (uint32_t)STM32_MEM_FLASH + offs; DCC_LOG2(LOG_INFO, "addr=0x%08x len=%d", addr, len); pecr = flash->pecr; DCC_LOG1(LOG_INFO, "PECR=0x%08x", pecr); if (pecr & FLASH_PRGLOCK) { DCC_LOG(LOG_INFO, "unlocking flash..."); if (pecr & FLASH_PELOCK) { flash->pekeyr = FLASH_PEKEY1; flash->pekeyr = FLASH_PEKEY2; } flash->prgkeyr= FLASH_PRGKEYR1; flash->prgkeyr= FLASH_PRGKEYR2; } cnt = 0; rem = len; while (rem) { uint32_t pri; uint32_t sr; DCC_LOG1(LOG_INFO, "addr=0x%08x", addr); pri = cm3_primask_get(); cm3_primask_set(1); sr = stm32l_flash_blk_erase(flash, (uint32_t *)addr); cm3_primask_set(pri); if (sr & FLASH_ERR) { #if DEBUG DCC_LOG6(LOG_WARNING, "erase failed: %s%s%s%s%s%s", sr & FLASH_RDERR ? "RDERR" : "", sr & FLASH_OPTVERRUSR ? "OPTVERRUSR" : "", sr & FLASH_OPTVERR ? "OPTVERR " : "", sr & FLASH_SIZERR ? "SIZERR " : "", sr & FLASH_PGAERR ? "PGAERR" : "", sr & FLASH_WRPERR ? "WRPERR" : ""); #endif cnt = -1; break; } addr += FLASH_PAGE_SIZE; rem -= FLASH_PAGE_SIZE; cnt += FLASH_PAGE_SIZE; } return cnt; }
void __attribute__((noreturn)) usb_recv_task(struct vcom * vcom) { struct serial_dev * serial = vcom->serial; usb_cdc_class_t * cdc = vcom->cdc; uint8_t buf[VCOM_BUF_SIZE]; int len; DCC_LOG1(LOG_TRACE, "[%d] started.", thinkos_thread_self()); DCC_LOG2(LOG_TRACE, "vcom->%p, cdc->%p", vcom, cdc); for (;;) { len = usb_cdc_read(cdc, buf, VCOM_BUF_SIZE, 1000); if (vcom->mode == VCOM_MODE_CONVERTER) { if (len > 0) { led_flash(LED_RED, 50); serial_write(serial, buf, len); #if RAW_TRACE if (len == 1) DCC_LOG1(LOG_TRACE, "TX: %02x", buf[0]); else if (len == 2) DCC_LOG2(LOG_TRACE, "TX: %02x %02x", buf[0], buf[1]); else if (len == 3) DCC_LOG3(LOG_TRACE, "TX: %02x %02x %02x", buf[0], buf[1], buf[2]); else if (len == 4) DCC_LOG4(LOG_TRACE, "TX: %02x %02x %02x %02x", buf[0], buf[1], buf[2], buf[3]); else if (len == 5) DCC_LOG5(LOG_TRACE, "TX: %02x %02x %02x %02x %02x", buf[0], buf[1], buf[2], buf[3], buf[4]); else if (len == 6) DCC_LOG6(LOG_TRACE, "TX: %02x %02x %02x %02x %02x %02x", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]); else if (len == 7) DCC_LOG7(LOG_TRACE, "TX: %02x %02x %02x %02x %02x %02x %02x ", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]); else DCC_LOG8(LOG_TRACE, "TX: %02x %02x %02x %02x %02x %02x " "%02x %02x ...", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]); #endif #if SDU_TRACE TX(buf, len); #endif // dbg_write(buf, len); } } else { // forward to service input vcom_service_input(vcom, buf, len); } } }
int usb_cdc_read(usb_cdc_class_t * cl, void * buf, unsigned int len, unsigned int msec) { struct usb_cdc_acm_dev * dev = (struct usb_cdc_acm_dev *)cl; int ret; int n; DCC_LOG2(LOG_INFO, "len=%d msec=%d", len, msec); if ((n = dev->rx_cnt - dev->rx_pos) > 0) { DCC_LOG(LOG_INFO, "read from intern buffer"); goto read_from_buffer; }; usb_dev_ep_nak(dev->usb, dev->out_ep, false); if ((ret = thinkos_sem_timedwait(RX_SEM, msec)) < 0) { if (ret == THINKOS_ETIMEDOUT) { DCC_LOG(LOG_INFO, "timeout!!"); } return ret; } if (len >= CDC_EP_IN_MAX_PKT_SIZE) { n = usb_dev_ep_pkt_recv(dev->usb, dev->out_ep, buf, len); DCC_LOG1(LOG_INFO, "wakeup, pkt rcv extern buffer: %d bytes", n); return n; } n = usb_dev_ep_pkt_recv(dev->usb, dev->out_ep, dev->rx_buf, CDC_EP_IN_MAX_PKT_SIZE); DCC_LOG1(LOG_INFO, "wakeup, pkt rcv intern buffer: %d bytes", n); { char * s = (char *)dev->rx_buf; (void)s; if (n == 1) DCC_LOG1(LOG_INFO, "%02x", s[0]); else if (n == 2) DCC_LOG2(LOG_INFO, "%02x %02x", s[0], s[1]); else if (n == 3) DCC_LOG3(LOG_INFO, "%02x %02x %02x", s[0], s[1], s[2]); else if (n == 4) DCC_LOG4(LOG_INFO, "%02x %02x %02x %02x", s[0], s[1], s[2], s[3]); else if (n == 5) DCC_LOG5(LOG_INFO, "%02x %02x %02x %02x %02x", s[0], s[1], s[2], s[3], s[4]); else if (n == 6) DCC_LOG6(LOG_INFO, "%02x %02x %02x %02x %02x %02x", s[0], s[1], s[2], s[3], s[4], s[5]); else if (n == 7) DCC_LOG7(LOG_INFO, "%02x %02x %02x %02x %02x %02x %02x", s[0], s[1], s[2], s[3], s[4], s[5], s[6]); else if (n == 8) DCC_LOG8(LOG_INFO, "%02x %02x %02x %02x %02x %02x %02x %02x", s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7]); else DCC_LOG8(LOG_INFO, "%02x %02x %02x %02x %02x %02x %02x %02x ...", s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7]); } dev->rx_pos = 0; dev->rx_cnt = n; read_from_buffer: DCC_LOG(LOG_INFO, "reading from buffer"); /* get data from the rx buffer if not empty */ n = MIN(n, len); memcpy(buf, &dev->rx_buf[dev->rx_pos], n); dev->rx_pos += n; return n; #if 0 { int rem; uint8_t * cp = (uint8_t *)buf; rem = n; while (rem > 4) { DCC_LOG4(LOG_INFO, "%02x %02x %02x %02x", cp[0], cp[1], cp[2], cp[3]); rem -= 4; cp += 4; } switch (rem) { case 3: DCC_LOG3(LOG_INFO, "%02x %02x %02x", cp[0], cp[1], cp[2]); break; case 2: DCC_LOG2(LOG_INFO, "%02x %02x", cp[0], cp[1]); break; case 1: if ((*cp) >= ' ') { DCC_LOG1(LOG_INFO, "'%c'", cp[0]); } else { DCC_LOG1(LOG_INFO, "%02x", cp[0]); } break; } } #endif }