/***************************************************************************** * USB callback function. Is called by the USB driver if a non standard * request is received from the HOST. This callback extends the known * requests by masstorage related ones. ****************************************************************************/ callback_state_t usb_ep0_callback(void) { hcc_u8 *pdata=usb_get_rx_pptr(0); callback_state_t r=clbst_error; /* A request to the command interface. */ if (STP_INDEX(pdata) == CMD_IFC_INDEX) { switch(STP_REQU_TYPE(pdata)) { /* Class specific in request. */ case ((1<<7) | (1<<5) | 1): /* Host wants to get a descriptor */ switch (STP_REQUEST(pdata)) { case CDCRQ_GET_SERIAL_STATE: usb_send(0, 0, (void*)&uart_state, 2, STP_LENGTH(pdata)); r=clbst_in; break; case CDCRQ_GET_LINE_CODING: usb_send(0, (void *) 0, (void *)&line_coding, 7, STP_LENGTH(pdata)); r=clbst_in; break; case CDCRQ_GET_ENCAPSULATED_RESPONSE: default: break; } break; /* Class specific out request. */ case ((0<<7) | (1<<5) | 1): switch (STP_REQUEST(pdata)) { case CDCRQ_SET_LINE_CODING: usb_receive(0, got_line_coding, (void *)&line_coding, 7); r=clbst_out; break; case CDCRQ_SET_CONTROL_LINE_STATE: ctrl_line_state=(hcc_u8)STP_VALUE_LO(pdata); r=clbst_out; break; case CDCRQ_SEND_ENCAPSULATED_COMMAND: default: break; } break; default: break; } } return(r); }
/** * Print buff as hex values * @param buf - buffer to print * @param l - buf length * @param s - function to send a byte */ void print_hex(uint8_t *buff, uint8_t l){ void putc(uint8_t c){ if(c < 10) usb_send(c + '0'); else usb_send(c + 'a' - 10); } usb_send('0'); usb_send('x'); // prefix 0x while(l--){ putc(buff[l] >> 4); putc(buff[l] & 0x0f); } }
/** * Print decimal integer value * @param N - value to print * @param s - function to send a byte */ void print_int(int32_t N){ uint8_t buf[10], L = 0; if(N < 0){ usb_send('-'); N = -N; } if(N){ while(N){ buf[L++] = N % 10 + '0'; N /= 10; } while(L--) usb_send(buf[L]); }else usb_send('0'); }
/** * Check checksum */ int checksum_true(uint8_t *buf){ uint8_t *eol; uint8_t checksum = 0, cs[3]; if(*buf != '$' || !(eol = ustrchr(buf, '*'))){ DBG("Wrong data: "); DBG(buf); DBG("\n"); return 0; } while(++buf != eol) checksum ^= *buf; ++buf; cs[0] = hex(checksum >> 4); cs[1] = hex(checksum & 0x0f); if(buf[0] == cs[0] && buf[1] == cs[1]) return 1; #ifdef EBUG cs[2] = 0; P("CHS, get "); P(buf); P(" need "); P(cs); usb_send('\n'); #endif return 0; }
void print_usb_ep2(const char *str) { int i; for(i=0; str[i]!=0; i++); usb_send(USB_EP_ADDR_EP2IN/*_epdesc2.b_endpoint_addr*/, str, i); }
void cmd_exec(void) { switch(usb_in_buffer[0]) { case CMD_CAL: cmd_cali(); break; case CMD_MOV: cmd_move(); break; case CMD_TST: cmd_test(); break; case CMD_HLT: cmd_halt(); break; case CMD_QRY: cmd_query(); usb_send(); break; default: break; } }
static void usb_code (unsigned int cmd, u32 code) { unsigned int *p_int = (unsigned int *) usb_outbuffer; *p_int++ = cmd; *p_int++ = code; usb_send (usb_outbuffer, ((unsigned char *) p_int) - usb_outbuffer); }
msg256 read_data (std::shared_ptr<libusb_device_handle> dh, unsigned char cmd) { send_cmd (dh, cmd); // hey, give me the data! msg32 b = {{10, 11, 12, 13, 0, 0, 1, 0}}; usb_send (dh, b); return usb_recv (dh); }
static int usb_ymodem_rcv_cancel(struct ymodem_rcv * rx) { unsigned char * pkt = rx->pkt.hdr; DCC_LOG(LOG_WARNING, "CANCEL!"); pkt[0] = CAN; pkt[1] = CAN; return usb_send(CDC_TX_EP, pkt, 2); }
static void usb_send_block(unsigned char *buf, int len, void (*done)(int, unsigned char *, int), int ep) { endpoints[ep].out_done = done; endpoints[ep].out_buf = buf; endpoints[ep].out_len = len; endpoints[ep].out_ptr = 0; endpoints[ep].out_in_progress = 1; usb_send(ep); }
void send_cmd (std::shared_ptr<libusb_device_handle> dh, unsigned char cmd) { // hey, here comes a command! { msg32 b = {{0x0a, 0x0b, 0x0c, 0x0d, 0x00, 0x00, 0x02, 0x00}}; usb_send (dh, b); } // issue the command { msg32 b = {{0}}; b[0] = cmd; usb_send (dh, b); } // i2c bus padding { msg32 b = {{0}}; for (int i = 0; i < 7; ++i) usb_send (dh, b); } }
void report_status(void) { outbox.response_type = C2RESPONSE_STATUS; outbox.payload[0] = 0; outbox.payload[0] |= (status_register.emergency_stop << C2DEVSTASTUS_EMERGENCY); outbox.payload[0] |= (status_register.matrix_output << C2DEVSTASTUS_MATRIXOUTPUT); outbox.payload[1] = DEVICE_VER_MAJOR; outbox.payload[2] = DEVICE_VER_MINOR; EEPROM_UpdateTemperature(); outbox.payload[3] = dieTemperature[0]; outbox.payload[4] = dieTemperature[1]; usb_send(); }
static void usb_msg (unsigned int cmd, const char *msg) { unsigned char *p_char = usb_outbuffer; * (int *) p_char = cmd; p_char += sizeof (cmd); if (msg) { while (*msg) *p_char++= *msg++; *p_char++= 0; } usb_send (usb_outbuffer, p_char - usb_outbuffer); }
void ibuddy_cmd( struct ibuddy_dev* dev, uint8_t cmd ) { unsigned char* data = kmemdup( cmd_data, 8, GFP_KERNEL ); if( !data ) { ERROR( "ibuddy_cmd out of memory\n" ); return; } data[7] = cmd; ibuddy_setup( dev ); if (usb_send( dev->udev, data, 8 )) ERROR("usb_send in ibuddy_cmd failed\n"); kfree( data ); }
/** * parce command buffer buf with length len * return 0 if buffer processed or len if there's not enough data in buffer */ int parce_incoming_buf(char *buf, int len){ uint8_t command; //uint32_t utmp; int i = 0; if(Uval_ready == UVAL_START){ // we are in process of user's value reading i += read_int(buf, len); } if(Uval_ready == UVAL_ENTERED){ //print_int(User_value); // printout readed integer value for error control Uval_ready = UVAL_BAD; // clear Uval I(User_value); return 0; } for(; i < len; i++){ command = buf[i]; if(!command) continue; // omit zero switch (command){ case '1': gpio_toggle(LEDS_PORT, LED_D1_PIN); break; case '2': gpio_toggle(LEDS_PORT, LED_D2_PIN); break; case 'H': // show help help(); break; case 'I': // enter integer & show its value I = show_int; READINT(); break; case 'T': newline(); print_int(Timer); // be careful for Time >= 2^{31}!!! newline(); break; case '\n': // show newline, space and tab as is case '\r': case ' ': case '\t': break; default: command = '?'; // echo '?' on unknown command in byte mode } usb_send(command); // echo readed byte } return 0; // all data processed - 0 bytes leave in buffer }
void usb_send_hex(int ep_id, unsigned int val) { char buf[16]; char * cp = buf;; int n; *cp++ = ' '; *cp++ = '0'; *cp++ = 'x'; n = uint2hex(cp, val); cp += n; *cp++ = '\r'; *cp++ = '\n'; n += 5; usb_send(ep_id, buf, n); }
void PrintColumn(uint8 col) { uint16 res[8]; for(uint8 i=0; i<8; ++i) { res[i] = ADC_GetResult16(i); } if (status_register.matrix_output > 0) { outbox.response_type = C2RESPONSE_MATRIX_STATUS; outbox.payload[0] = col; outbox.payload[1] = status_register.matrix_output; memcpy(&outbox.payload[2], res, sizeof(res)); usb_send(); } }
void transfer_output(int fd) { // from PK2 -> host char buf[BUFFER_SIZE]; char cmd[] = {UPLOAD_DATA}; int written; usb_send(cmd, 1); usb_receive_buf(buf); LOG(" O %2d\r\n", buf[0]); if (buf[0]) { written = write(fd, buf + 1, buf[0]); if (written != buf[0]) { RAISE_ERROR("output buffer overrun!\r\n"); } } }
static int ep_rx(struct ep_descr *ep) { uint8_t size; size = UEBCLX; if (size > ep->end-ep->buf) return 0; while (size--) *ep->buf++ = UEDATX; if (ep->buf == ep->end) { ep->state = EP_IDLE; if (ep->callback) ep->callback(ep->user); if (ep == &eps[0]) usb_send(ep, NULL, 0, NULL, NULL); } return 1; }
static int ep_setup(void) { struct setup_request setup; BUG_ON(UEBCLX < 8); setup.bmRequestType = UEDATX; setup.bRequest = UEDATX; setup.wValue = usb_read_word(); setup.wIndex = usb_read_word(); setup.wLength = usb_read_word(); if (!handle_setup(&setup)) return 0; if (!(setup.bmRequestType & 0x80) && eps[0].state == EP_IDLE) usb_send(&eps[0], NULL, 0, NULL, NULL); return 1; }
/* Check if procondition is necessary */ short check_Precondition(short *precondition) { signed float voltage = 0; short res; res = V_Eval(ADC_CH0,&voltage); if (res != OK) { return KO; } if (voltage < battery.charge.restore_Lowest_Voltage * battery.number_of_cells) { *precondition = true; } else { *precondition = false; } usb_send("voltage : %f, precondition : %hd",voltage,precondition); return OK; }
static void ibuddy_setup( struct ibuddy_dev* dev ) { int retval = usb_send( dev->udev, reset_data, 8 ); if( retval ) ERROR("usb_send in reset failed (%d)\n", retval); }
int set_addr(uint8_t addr) { UDADDR = addr; usb_send(&eps[0], NULL, 0, enable_addr, NULL); return 1; }
static int usb_ymodem_rcv_pkt(struct ymodem_rcv * rx) { unsigned char * pkt = rx->pkt.hdr; unsigned char * cp; int ret = 0; int cnt = 0; #if XMODEM_SEQUENCE_CHECK int nseq; int seq; #endif int rem; int pos; int i; for (;;) { if ((ret = usb_send(CDC_TX_EP, &rx->sync, 1)) < 0) { return ret; } rem = 0; pos = 0; for (;;) { int c; if (rem == 0) { ret = usb_recv(CDC_RX_EP, pkt, 128, 2000); DCC_LOG1(LOG_TRACE, "usb_recv() ret=%d)", ret); if (ret <= 0) goto timeout; pos = 0; rem = ret; } c = pkt[pos]; pos++; rem--; if (c == STX) { cnt = 1024; break; } if (c == SOH) { cnt = 128; break; } if (c == CAN) { return -1; } if (c == EOT) { /* end of transmission */ #if XMODEM_CHECKSUM rx->sync = rx->crc_mode ? 'C' : NAK; #else rx->sync = 'C'; #endif rx->pktno = 0; pkt[0] = ACK; usb_send(CDC_TX_EP, pkt, 1); return 0; } } cp = pkt + 1; for (i = 0; i < rem; ++i) cp[i] = pkt[pos + i]; cp += rem; rem = cnt + 4 - rem; /* receive the packet */ while (rem) { ret = usb_recv(CDC_RX_EP, cp, rem, 500); DCC_LOG1(LOG_TRACE, "usb_recv() ret=%d)", ret); if (ret < 0) goto timeout; rem -= ret; cp += ret; } #if XMODEM_SEQUENCE_CHECK /* sequence */ seq = pkt[1]; /* inverse sequence */ nseq = pkt[2]; if (seq != ((~nseq) & 0xff)) { goto error; } #endif cp = &pkt[3]; #if XMODEM_CHECKSUM if (rx->crc_mode) #endif #if XMODEM_CRC_CHECK { unsigned short crc = 0; unsigned short cmp; int i; for (i = 0; i < cnt; ++i) crc = CRC16CCITT(crc, cp[i]); cmp = (unsigned short)cp[i] << 8 | cp[i + 1]; if (cmp != crc) { goto error; } } #endif #if XMODEM_CHECKSUM else { unsigned char cks = 0; int i; for (i = 0; i < cnt; ++i) cks += cp[i]; if (cp[i] != cks) { goto error; } } #endif #if XMODEM_SEQUENCE_CHECK if (seq == ((rx->pktno - 1) & 0xff)) { /* retransmission */ continue; } if (seq != (rx->pktno & 0xff)) { #if XMODEM_FALLBACK if ((rx->pktno == 0) && (seq == 1)) { rx->pktno++; /* Fallback to XMODEM */ rx->xmodem = true; } else #endif { goto error; } } #endif /* YModem first packet ... */ if (rx->pktno == 0) { pkt[0] = ACK; usb_send(CDC_TX_EP, pkt, 1); } else { rx->retry = 10; rx->sync = ACK; if ((rx->count + cnt) > rx->fsize) cnt = rx->fsize - rx->count; rx->count += cnt; } rx->pktno++; return cnt; #if XMODEM_SEQUENCE_CHECK || XMODEM_CRC_CHECK error: /* flush */ while (usb_recv(CDC_RX_EP, pkt, 1024, 100) > 0); ret = -1; break; #endif timeout: if ((--rx->retry) == 0) { /* too many errors */ DCC_LOG(LOG_WARNING, "too many errors!"); ret = -1; break; } } return usb_ymodem_rcv_cancel(rx); }
void frbwrite(void) { /* simple noise filter */ if(ptr>0 && (frbcode.word.high.byte.high&PULSE_BIT)) { frbcode.word.high.byte.high = 0; pulse.dword+=frbcode.dword; if(pulse.dword>250) { sendcode.dword=space.dword; usb_send(); pulse.word.high.byte.high|=PULSE_BIT; sendcode.dword=pulse.dword; usb_send(); ptr=0; pulse.dword=0; } return; } if(!(frbcode.word.high.byte.high&PULSE_BIT)) { if(ptr==0) { if(frbcode.dword>20000) { space.dword=frbcode.dword; ptr++; return; } } else { if(frbcode.dword>20000) { space.dword+=pulse.dword; if (space.word.high.byte.high != 0) { space.word.high.word = 0x00ff; space.word.low.word = 0xffff; } space.dword+=frbcode.dword; if (space.word.high.byte.high != 0) { space.word.high.word = 0x00ff; space.word.low.word = 0xffff; } pulse.dword=0; return; } sendcode.dword=space.dword; usb_send(); pulse.word.high.byte.high|=PULSE_BIT; sendcode.dword=pulse.dword; usb_send(); ptr=0; pulse.dword=0; } } sendcode.dword=frbcode.dword; usb_send(); }
void cdc_process(void) { /* See if USB is in usable state. */ if (usb_get_state() == USBST_CONFIGURED) { /* If endpoint is not busy, and receive buffer is empty */ if (!usb_ep_is_busy(CDC_RX_EP_NO) && (rx_length <= rx_ndx)) { /* Read out error status of endpoint. The reception may be aborted due to a status change on the USB (disconnect, sleep, etc..) or due to an error (CRC, bit stuffing, etc...). In both case wee need to restart reception if possible. */ switch(usb_ep_error(CDC_RX_EP_NO)) { case USBEPERR_NONE: /* Reception finished with no error. */ /* If received bytes are not yet handled. */ if (rx_length == 0) { /* Read out number of received bytes. This will make us to return received characters for the next call. Note: the transfer may contain 0 characters, so we return the first character only when the next call is made. */ rx_length=(hcc_u8)usb_get_done(CDC_RX_EP_NO); rx_ndx=0; /* If we did not received any data, then we need to start a new receive. */ if (rx_length) { break; } } case USBEPERR_PROTOCOL_ERROR: /* restart the reception */ rx_length=0; usb_receive(CDC_RX_EP_NO, (void *) 0, (void *) rx_buffer, sizeof(rx_buffer)); break; case USBEPERR_HOST_ABORT: /* This error can only be detected if error happens after execution of thefirst if in this function and before the switch. This is unlikely to happen. On the other hand this error meand usb has been disconnected or put to low power mode and thus we may safely ignore it. */ break; case USBEPERR_USER_ABORT: case USBEPERR_TO_MANY_DATA: default: /* Upps! unexpected error. Stop here. */ CMX_ASSERT(0); } } /* If tx buffer is not empty, start transmission. */ if (!usb_ep_is_busy(CDC_TX_EP_NO) && (tx_ndx != 0)) { /* Check the status of the next transfer. */ switch (usb_ep_error(CDC_TX_EP_NO)) { case USBEPERR_PROTOCOL_ERROR: /* Ignore error, send next chunk. */ case USBEPERR_HOST_ABORT: case USBEPERR_NONE: /* Finished with no error. */ /* Start sending next chunk. */ usb_send(CDC_TX_EP_NO, (void *) 0, (void *) cur_tx_buffer, tx_ndx, tx_ndx); /* Switch buffer. */ tx_ndx=0; cur_tx_buffer = (hcc_u8*)((cur_tx_buffer == (hcc_u8*)tx_buffer1) ? tx_buffer2 : tx_buffer1); break; case USBEPERR_USER_ABORT: case USBEPERR_TO_MANY_DATA: default: /* Upps! unexpected error. Stop here. */ CMX_ASSERT(0); } } } }
/* one command cycle */ void usb_call(void *cmd, int cmd_len, void *reply, int reply_len) { usb_send(cmd, cmd_len); if (reply_len) usb_receive(reply, reply_len); // PK2 doesn't send empty buffers }
/** * Send char array wrd thru USB or UART */ void prnt(uint8_t *wrd){ if(!wrd) return; while(*wrd) usb_send(*wrd++); }