int eeprom_set(struct my_usb_device *mydev, const struct myeeprom *eeprom) { int ret; int len; char buf[PACKET_SIZE]; struct fpga_packet_header *phead = (struct fpga_packet_header *)buf; DBG("%s Start...\n", __FUNCTION__); assert(mydev != NULL); phead->header.op = PT_EEPROM_SET; memcpy(&phead->d.eeprom_set.data, eeprom, EEPROM_SIZE); len = sizeof(phead->d.eeprom_set) + sizeof(phead->header.op); ret = send_usb("eeprom_set[W]", mydev, phead, len, TIMEOUT); if(ret < 0) return ret; ret = recv_usb("eeprom_set[R]", mydev, buf, sizeof(buf), TIMEOUT); if(ret <= 0) return ret; phead = (struct fpga_packet_header *)buf; if(phead->header.op == PT_BAD_COMMAND) { ERR("Firmware rejected PT_EEPROM_SET command\n"); return -EINVAL; } else if(phead->header.op != PT_EEPROM_SET) { ERR("Got unexpected reply op=%d\n", phead->header.op); return -EINVAL; } return 0; }
int eeprom_get(struct my_usb_device *mydev) { int ret; int len; char buf[PACKET_SIZE]; struct fpga_packet_header *phead = (struct fpga_packet_header *)buf; struct myeeprom *eeprom; assert(mydev != NULL); eeprom = &mydev->eeprom; DBG("%s Start...\n", __FUNCTION__); phead->header.op = PT_EEPROM_GET; len = sizeof(phead->header.op); /* warning: sending small packet */ ret = send_usb("eeprom_get[W]", mydev, phead, len, TIMEOUT); if(ret < 0) return ret; ret = recv_usb("eeprom_get[R]", mydev, buf, sizeof(buf), TIMEOUT); if(ret <= 0) return ret; phead = (struct fpga_packet_header *)buf; if(phead->header.op == PT_BAD_COMMAND) { ERR("PT_BAD_COMMAND\n"); return -EINVAL; } else if(phead->header.op != PT_EEPROM_GET) { ERR("Got unexpected reply op=%d\n", phead->header.op); return -EINVAL; } memcpy(eeprom, &phead->d.eeprom_get.data, EEPROM_SIZE); return 0; }
int buzz (unsigned char duration, struct usb_dev_handle * USB_handle) { int RecvLength; int SendLength; //DWORD Comm_OK=0; byte send_buf[64],receive_buf[64]; int retval=0; int errchk; send_buf[0]=BUZZER; //Set main command. send_buf[1]= duration; SendLength=2; //5 bytes in the outbound packet. RecvLength=2; send_usb(USB_handle, SendLength, send_buf); recv_usb(USB_handle, RecvLength, receive_buf); errchk=receive_buf[1]; if (duration != errchk){ printf("BUZZER: Wrong duration value returned: Requested:%d Returned:%d\n", duration, errchk); retval=1; } return (retval); } //END: buzz
int writeLCD(char * buffer, byte length, struct usb_dev_handle * USB_handle) { int RecvLength; int SendLength; //int Comm_OK=0; byte send_buf[64],receive_buf[64]; int retval=0; int errchk; int i; send_buf[0]=PUT_LCD; //Set main command. send_buf[1]= (byte)length; for (i=0;i<length; ++i){ //Copy print buffer into send buffer send_buf[i+2]=(buffer[i]); } SendLength=2+length; // Outbound packet length RecvLength=2; // [1]token, [2]length send_usb(USB_handle, SendLength, send_buf); recv_usb(USB_handle, RecvLength, receive_buf); errchk=receive_buf[1]; if (length != errchk){ printf("writeLCD: Wrong string length returned: Requested:%d Returned:%d\n", length, errchk); retval=1; } return (retval); } //END: writeLCD
int readADC (unsigned char channel, struct usb_dev_handle * USB_handle) { int RecvLength; int SendLength; //int Comm_OK=0; byte send_buf[64],receive_buf[64]; int retval=0; int errchk; send_buf[0]=READ_ADC; //Set main command. send_buf[1]= channel; SendLength=2; //5 bytes in the outbound packet. RecvLength=4; send_usb(USB_handle, SendLength, send_buf); recv_usb(USB_handle, RecvLength, receive_buf); errchk=receive_buf[3]; if (channel != errchk){ printf("readADC: Wrong ADC channel returned: Requested:%d Returned:%d\n", channel, errchk); //retval=1; } retval=(unsigned char)receive_buf[1]*256+(unsigned char)receive_buf[2]; return (retval); } //END: readADC
int send_picline(struct astribank_device *astribank, uint8_t card_type, enum pic_command pcmd, int offs, uint8_t *data, int data_len) { int recv_answer = 0; char buf[PACKET_SIZE]; struct xpp_packet_header *phead = (struct xpp_packet_header *)buf; int pack_len; int ret; assert(astribank != NULL); pack_len = data_len + sizeof(phead->header) + sizeof(phead->d.pic_packet.pic_header); phead->header.len = pack_len; phead->header.op = PIC_REQ_XOP; phead->header.unit = 0x00; phead->d.pic_packet.pic_header.flags = pcmd; phead->d.pic_packet.pic_header.card_type = card_type; phead->d.pic_packet.pic_header.offs = offs; if(data) memcpy(phead->d.pic_packet.data, data, data_len); switch (pcmd) { case PIC_START_FLAG: break; case PIC_DATA_FLAG: break; case PIC_END_FLAG: recv_answer = 1; break; case PIC_ENDS_FLAG: break; } DBG("PICLINE: pack_len=%d pcmd=%d\n", pack_len, pcmd); dump_packet(LOG_DEBUG, "dump:picline[W]", (char *)phead, pack_len); ret = send_usb(astribank, buf, pack_len, TIMEOUT); if(ret < 0) { ERR("send_usb failed: %d\n", ret); return ret; } DBG("send_usb: Written %d bytes\n", ret); if (recv_answer) { ret = recv_usb(astribank, buf, sizeof(buf), TIMEOUT); if(ret <= 0) { ERR("No USB packs to read\n"); return ret; } else { phead = (struct xpp_packet_header *)buf; if(phead->header.op != PIC_REP_XOP) { ERR("Got unexpected reply OP=0x%02X\n", phead->header.op); dump_packet(LOG_ERR, "hexline[ERR]", buf, ret); return -EINVAL; } DBG("received OP=0x%02X, checksum=%02X\n", phead->header.op, phead->d.pic_packet.data[0]); if(phead->d.pic_packet.data[0] != 0) { ERR("PIC burning, bad checksum\n"); return -EINVAL; } } } return 0; }
void picdem_fs_usb_read_version(struct usb_dev_handle * d) { byte resp[reqLen]; byte query[reqLen]; query[0] = READ_VERSION; query[1] = READ_VERSION_LEN; send_usb(d, 2, query); recv_usb(d, 2+READ_VERSION_LEN, resp); if( (int)resp[0] == READ_VERSION && (int)resp[1] == READ_VERSION_LEN ) printf("USB firmware stack version is %d.%d\n",(int)resp[3],(int)resp[2]); } //END: Read version.
int send_hexline(struct my_usb_device *mydev, struct hexline *hexline, int seq) { int ret; int len; uint8_t *data; char buf[PACKET_SIZE]; struct fpga_packet_header *phead = (struct fpga_packet_header *)buf; enum fpga_load_status status; assert(mydev != NULL); assert(hexline != NULL); if(hexline->d.content.header.tt != TT_DATA) { DBG("Non data record %d type = %d\n", seq, hexline->d.content.header.tt); return 0; } len = hexline->d.content.header.ll; /* don't send checksum */ data = hexline->d.content.tt_data.data; phead->header.op = PT_DATA_PACKET; phead->d.data_packet.seq = seq; phead->d.data_packet.reserved = 0x00; memcpy(phead->d.data_packet.data, data, len); len += sizeof(hexline->d.content.header); DBG("%04d+\r", seq); ret = send_usb("hexline[W]", mydev, phead, len, TIMEOUT); if(ret < 0) return ret; ret = recv_usb("hexline[R]", mydev, buf, sizeof(buf), TIMEOUT); if(ret <= 0) return ret; DBG("%04d-\r", seq); phead = (struct fpga_packet_header *)buf; if(phead->header.op != PT_STATUS_REPLY) { ERR("Got unexpected reply op=%d\n", phead->header.op); dump_packet("hexline[ERR]", buf, ret); return -EINVAL; } status = (enum fpga_load_status)phead->d.status_reply.status; switch(status) { case FW_TRANS_OK: case FW_CONFIG_DONE: break; case FW_FAIL_RESET: case FW_FAIL_TRANS: ERR("status reply %s (%d)\n", load_status2str(status), status); dump_packet("hexline[ERR]", buf, ret); return -EPROTO; default: ERR("Unknown status reply %d\n", status); dump_packet("hexline[ERR]", buf, ret); return -EPROTO; } return 0; }
void picdem_fs_usb_led(struct usb_dev_handle * d, int lednum, int onoff) { if( lednum < 3 || lednum > 4 ) return; byte answer[reqLen]; byte question[reqLen]; question[0] = UPDATE_LED; question[1] = lednum; question[2] = (onoff==0) ? 0 : 1; send_usb(d, 3, question); recv_usb(d, 1, answer); printf("LED #%i is now %s\n", lednum, (onoff==0) ? "off" : "on"); } //END: picdem_fs_usb_led
int flush_read(struct astribank_device *astribank) { char tmpbuf[BUFSIZ]; int ret; DBG("starting...\n"); memset(tmpbuf, 0, BUFSIZ); ret = recv_usb(astribank, tmpbuf, BUFSIZ, 1); if(ret < 0 && ret != -ETIMEDOUT) { ERR("ret=%d\n", ret); return ret; } else if(ret > 0) { DBG("Got %d bytes:\n", ret); dump_packet(LOG_DEBUG, DBG_MASK, __FUNCTION__, tmpbuf, ret); } return 0; }
int setPWM (unsigned char motor_no, int PWM_value, struct usb_dev_handle * USB_handle) { int RecvLength; int SendLength; //DWORD Comm_OK=0; byte send_buf[64],receive_buf[64]; int retval=0; int errchk; send_buf[0]=SET_PWM; //Set main command. if (motor_no==1) //Insert motor number in the packet. send_buf[1]=1; else if (motor_no==2) send_buf[1]=2; else { printf("setPWM: Motor number is out of range: %d\n", motor_no); send_buf[1]=0; retval=1; } if (PWM_value>1023){ //Check for overflow in PWM value. printf("setPWM: PWM value is out of range: %d\n", PWM_value); PWM_value =1023; retval=1; } if (PWM_value < -1023){ printf("setPWM: PWM value is out of range: %d\n", PWM_value); PWM_value =-1023; retval=1; } if (PWM_value <0){ //Insert direction in the packet. send_buf[2]=1; //Negative PWM_value = PWM_value*-1; //Set PWM_value as a positive number. } else send_buf[2]=0; send_buf[3]=(byte)(PWM_value%256); //index 3 is low value. send_buf[4]=(byte)(PWM_value/256); //index 4 is high value. /* printf ("Send buffer value 0= %d\n",(unsigned char)send_buf[0]); printf ("Send buffer value 1= %d\n",(unsigned char)send_buf[1]); printf ("Send buffer value 2= %d\n",(unsigned char)send_buf[2]); printf ("Send buffer value 3= %d\n",(unsigned char)send_buf[3]); printf ("Send buffer value 4= %d\n",(unsigned char)send_buf[4]); */ SendLength=5; //5 bytes in the outbound packet. RecvLength=3; send_usb(USB_handle, SendLength, send_buf); recv_usb(USB_handle, RecvLength, receive_buf); /* printf ("Buffer value 0= %d\n",(unsigned char)receive_buf[0]); printf ("Buffer value 1= %d\n",(unsigned char)receive_buf[1]); printf ("Buffer value 2= %d\n",(unsigned char)receive_buf[2]); */ errchk=(unsigned char)receive_buf[1]+256*(unsigned char)receive_buf[2]; if (PWM_value != errchk){ printf("setPWM: Wrong PWM value returned: Requested:%d Returned:%d\n", PWM_value,errchk); retval=1; } return (retval); } //END: set_PWM