/** Function to read a string from USB @param buffer Buffer for reading data @param lenght Number of bytes to be read @return number of bytes acutally read **/ byte UARTgets(char *buffer, byte length) { byte i=0; if (deviceState != CONFIGURED) return 0; // Only Process if we own the buffer aka not own by SIE if (!EP_OUT_BD(UART_DATA_EP_NUM).Stat.UOWN) { #ifdef DEBUG_PRINT printf("Rx on EP %d, Size %d\r\n", UART_DATA_EP_NUM, EP_OUT_BD(UART_DATA_EP_NUM).Cnt); #endif // check how much bytes came if (length > EP_OUT_BD(UART_DATA_EP_NUM).Cnt) length = EP_OUT_BD(UART_DATA_EP_NUM).Cnt; for (i=0; i < EP_OUT_BD(UART_DATA_EP_NUM).Cnt; i++) { buffer[i] = UARTRxBuffer[i]; #ifdef DEBUG_PRINT printf("%c",UARTRxBuffer[i]); #endif } #ifdef DEBUG_PRINT printf("->"); #endif // clear BDT Stat bits beside DTS and then togle DTS EP_OUT_BD(UART_DATA_EP_NUM).Stat.uc &= 0x40; EP_OUT_BD(UART_DATA_EP_NUM).Stat.DTS = !EP_OUT_BD(UART_DATA_EP_NUM).Stat.DTS; // reset buffer count and handle controll of buffer to USB EP_OUT_BD(UART_DATA_EP_NUM).Cnt = sizeof(UARTRxBuffer); EP_OUT_BD(UART_DATA_EP_NUM).Stat.uc |= BDS_UOWN | BDS_DTSEN; } // return number of bytes read return i; }
void ep0_init(void) { debug_usb("ep0_init\r\n"); init_dfu(); ep0_state = WAIT_SETUP; EP_OUT_BD(0).Cnt = EP0_BUFFER_SIZE; EP_OUT_BD(0).ADR = (u8 __data *)&SetupBuffer; EP_OUT_BD(0).Stat.uc = BDS_USIE | BDS_DAT0 | BDS_DTSEN; EP_IN_BD(0).Stat.uc = BDS_UCPU; UEP0 = EPINEN_EN | EPOUTEN_EN | EPHSHK_EN; }
void ep0_out(void) { if (ep0_state == WAIT_DFU_OUT) { process_dfu_data((u8 __data *)InBuffer, SetupBuffer.wLength); } ep0_state = WAIT_SETUP; EP_OUT_BD(0).Cnt = EP0_BUFFER_SIZE; EP_OUT_BD(0).ADR = (u8 __data *)&SetupBuffer; if (EP_OUT_BD(0).Stat.DTS == 0) { EP_OUT_BD(0).Stat.uc = BDS_USIE | BDS_DAT1 | BDS_DTSEN; } else { EP_OUT_BD(0).Stat.uc = BDS_USIE | BDS_DAT0 | BDS_DTSEN; } }
/** Initialize **/ void BULKInitEndpoint(void) { // BULK Data EP is IN and OUT EP BULK_DATA_EP_UEP = EP_OUT_IN | HSHK_EN; // now build EP EP_OUT_BD(BULK_DATA_EP_NUM).Cnt = sizeof(BULKRxBuffer); EP_OUT_BD(BULK_DATA_EP_NUM).ADDR = PTR16(&BULKRxBuffer); // USB owns buffer EP_OUT_BD(BULK_DATA_EP_NUM).Stat.uc = BDS_UOWN | BDS_DTSEN; EP_IN_BD(BULK_DATA_EP_NUM).ADDR = PTR16(&BULKTxBuffer); // CPU owns buffer EP_IN_BD(BULK_DATA_EP_NUM).Stat.uc = BDS_DTS ; }
/** Initialize **/ void UARTInitEndpoint(void) { #ifdef DEBUG_UART printf("UARTInitEndpoint\r\n"); #endif // UART Data EP is IN and OUT EP UART_DATA_EP_UEP = EP_OUT_IN | HSHK_EN; // now build EP EP_OUT_BD(UART_DATA_EP_NUM).Cnt = sizeof(UARTRxBuffer); EP_OUT_BD(UART_DATA_EP_NUM).ADDR = PTR16(&UARTRxBuffer); // USB owns buffer EP_OUT_BD(UART_DATA_EP_NUM).Stat.uc = BDS_UOWN | BDS_DTSEN; EP_IN_BD(UART_DATA_EP_NUM).ADDR = PTR16(&UARTTxBuffer); // CPU owns buffer EP_IN_BD(UART_DATA_EP_NUM).Stat.uc = BDS_DTS ; }
/** Initialize **/ void CDCInitEndpoint(void) { #ifdef DEBUG_PRINT_CDC printf("CDCInitEndpoint\r\n"); #endif line_config.dwDTERate = USB_CDC_BAUD_RATE; line_config.bCharFormat = USB_CDC_STOP_BITS; line_config.bParityType = USB_CDC_PARITY; line_config.bDataBits = USB_CDC_DATA_BITS; zlp.wValue0=0; zlp.wValue1=0; zlp.wValue2=0; zlp.wValue3=0; zlp.wValue4=0; zlp.wValue5=0; zlp.wValue6=0; zlp.wValue7=0; // set global state variable // Configure USB_COMM_EP_UEP as IN and Communication PIPE USB_COMM_EP_UEP = EP_IN | HSHK_EN; // CDC Data EP is IN and OUT EP CDC_DATA_EP_UEP = EP_OUT_IN | HSHK_EN; // configure buffer for the Eps // firt communication EP EP_IN_BD(USB_COMM_EP_NUM).ADDR = PTR16(&CDCControlBuffer); EP_IN_BD(USB_COMM_EP_NUM).Stat.uc = BDS_DAT1 | BDS_COWN; // now Buld EP EP_OUT_BD(CDC_DATA_EP_NUM).Cnt = sizeof(CDCRxBuffer); EP_OUT_BD(CDC_DATA_EP_NUM).ADDR = PTR16(&CDCRxBuffer); EP_OUT_BD(CDC_DATA_EP_NUM).Stat.uc = BDS_UOWN | BDS_DAT0 | BDS_DTSEN; EP_IN_BD(CDC_DATA_EP_NUM).ADDR = PTR16(&CDCTxBuffer); // +1 EP_IN_BD(CDC_DATA_EP_NUM).Stat.uc = BDS_DAT1 | BDS_COWN; deviceState=CONFIGURED; #ifdef DEBUG_PRINT_CDC printf("end CDCInitEndpoint\r\n"); #endif }
/** Function to read a string from USB @param buffer Buffer for reading data @param lenght Number of bytes to be read @return number of bytes acutally read **/ byte BULKgets(char *buffer) { byte i=0; byte length=64; if (deviceState != CONFIGURED) return 0; // Only Process if we own the buffer aka not own by SIE if (!EP_OUT_BD(BULK_DATA_EP_NUM).Stat.UOWN) { // check how much bytes came if (length > EP_OUT_BD(BULK_DATA_EP_NUM).Cnt) length = EP_OUT_BD(BULK_DATA_EP_NUM).Cnt; for (i=0; i < EP_OUT_BD(BULK_DATA_EP_NUM).Cnt; i++) { buffer[i] = BULKRxBuffer[i]; } // clear BDT Stat bits beside DTS and then togle DTS EP_OUT_BD(BULK_DATA_EP_NUM).Stat.uc &= 0x40; EP_OUT_BD(BULK_DATA_EP_NUM).Stat.DTS = !EP_OUT_BD(BULK_DATA_EP_NUM).Stat.DTS; // reset buffer count and handle controll of buffer to USB EP_OUT_BD(BULK_DATA_EP_NUM).Cnt = sizeof(BULKRxBuffer); EP_OUT_BD(BULK_DATA_EP_NUM).Stat.uc |= BDS_UOWN | BDS_DTSEN; } // return number of bytes read return i; }
void ep0_setup(void) { debug_usb("ep0_setup\n"); ep0_state = WAIT_SETUP; num_bytes_to_be_send = 0; if (ep0_usb_std_request()) { UCONbits.PKTDIS = 0; if (SetupBuffer.data_transfer_direction == DEVICE_TO_HOST) { ep0_state = WAIT_IN; EP_OUT_BD(0).Cnt = EP0_BUFFER_SIZE; EP_OUT_BD(0).ADR = (u8 __data *)&SetupBuffer; EP_OUT_BD(0).Stat.uc = BDS_USIE; EP_IN_BD(0).ADR = (u8 __data *)InBuffer; if (SetupBuffer.wLength < num_bytes_to_be_send) { num_bytes_to_be_send = SetupBuffer.wLength; } debug2_usb("bytes to send: %d\r\n", num_bytes_to_be_send); fill_in_buffer(0, &sourceData, EP0_BUFFER_SIZE, &num_bytes_to_be_send); EP_IN_BD(0).Stat.uc = BDS_USIE | BDS_DAT1 | BDS_DTSEN; } else // HOST_TO_DEVICE { ep0_state = WAIT_OUT; EP_OUT_BD(0).Cnt = EP0_BUFFER_SIZE; EP_OUT_BD(0).ADR = (u8 __data *)InBuffer; EP_OUT_BD(0).Stat.uc = BDS_USIE | BDS_DAT1 | BDS_DTSEN; EP_IN_BD(0).Cnt = 0; EP_IN_BD(0).Stat.uc = BDS_USIE | BDS_DAT1 | BDS_DTSEN; } } else if (ep0_dfu_request()) { UCONbits.PKTDIS = 0; if (SetupBuffer.data_transfer_direction == DEVICE_TO_HOST) { ep0_state = WAIT_DFU_IN; EP_OUT_BD(0).Cnt = EP0_BUFFER_SIZE; EP_OUT_BD(0).ADR = (u8 __data *)&SetupBuffer; EP_OUT_BD(0).Stat.uc = BDS_USIE; EP_IN_BD(0).ADR = (u8 __data *)InBuffer; if (SetupBuffer.wLength < num_bytes_to_be_send) { num_bytes_to_be_send = SetupBuffer.wLength; } debug2_usb("bytes to send: %d\n", num_bytes_to_be_send); // debug2("2: %x\n", sourceData[0]); fill_in_buffer(0, &sourceData, EP0_BUFFER_SIZE, &num_bytes_to_be_send); EP_IN_BD(0).Stat.uc = BDS_USIE | BDS_DAT1 | BDS_DTSEN; } else // HOST_TO_DEVICE { ep0_state = WAIT_DFU_OUT; EP_OUT_BD(0).Cnt = EP0_BUFFER_SIZE; EP_OUT_BD(0).ADR = (u8 __data *)InBuffer; EP_OUT_BD(0).Stat.uc = BDS_USIE | BDS_DAT1 | BDS_DTSEN; EP_IN_BD(0).Cnt = 0; EP_IN_BD(0).Stat.uc = BDS_USIE | BDS_DAT1 | BDS_DTSEN; } } else { debug_usb("unknown request\n"); UCONbits.PKTDIS = 0; EP_OUT_BD(0).Cnt = EP0_BUFFER_SIZE; EP_OUT_BD(0).ADR = (u8 __data *)&SetupBuffer; EP_OUT_BD(0).Stat.uc = BDS_USIE | BDS_BSTALL; EP_IN_BD(0).Stat.uc = BDS_USIE | BDS_BSTALL; } }
void ep0_in(void) { debug2_usb("ep0_in %d\n", (u16) num_bytes_to_be_send); if (GET_DEVICE_STATE() == ADDRESS_PENDING_STATE) { UADDR = SetupBuffer.bAddress; if (UADDR != 0) { SET_DEVICE_STATE(ADDRESS_STATE); } else { SET_DEVICE_STATE(DEFAULT_STATE); } } if (ep0_state == WAIT_IN) { fill_in_buffer(0, &sourceData, EP0_BUFFER_SIZE, &num_bytes_to_be_send); if (EP_IN_BD(0).Stat.DTS == 0) { EP_IN_BD(0).Stat.uc = BDS_USIE | BDS_DAT1 | BDS_DTSEN; } else { EP_IN_BD(0).Stat.uc = BDS_USIE | BDS_DAT0 | BDS_DTSEN; } } else if (ep0_state == WAIT_DFU_IN) { fill_in_buffer(0, &sourceData, EP0_BUFFER_SIZE, &num_bytes_to_be_send); if (EP_IN_BD(0).Stat.DTS == 0) { EP_IN_BD(0).Stat.uc = BDS_USIE | BDS_DAT1 | BDS_DTSEN; } else { EP_IN_BD(0).Stat.uc = BDS_USIE | BDS_DAT0 | BDS_DTSEN; } } else { ep0_state = WAIT_SETUP; EP_OUT_BD(0).Cnt = EP0_BUFFER_SIZE; EP_OUT_BD(0).ADR = (u8 __data *)&SetupBuffer; EP_OUT_BD(0).Stat.uc = BDS_USIE | BDS_DAT0 | BDS_DTSEN; EP_IN_BD(0).Stat.uc = BDS_UCPU; UEP0 = EPINEN_EN | EPOUTEN_EN | EPHSHK_EN; } if (GET_DEVICE_STATE() == CONFIGURATION_PENDING_STATE) { // First, disable all endpoints. // UEP0 is never disabled UEP1 = 0; UEP2 = 0; UEP3 = 0; UEP4 = 0; UEP5 = 0; UEP6 = 0; UEP7 = 0; UEP8 = 0; UEP9 = 0; UEP10 = 0; UEP11 = 0; UEP12 = 0; UEP13 = 0; UEP14 = 0; UEP15 = 0; // switch the functions vectors /* if(coming_cfg <= FLASH_CONFIGURATION) { // switch back to the bootloader vectors ep_init = boot_ep_init; ep_in = boot_ep_in; ep_out = boot_ep_out; ep_setup = boot_ep_setup; } else { // switch to the application vectors ep_init = application_data.ep_init; ep_in = application_data.ep_in; ep_out = application_data.ep_out; ep_setup = application_data.ep_setup; } */ ep_init = boot_ep_init; ep_in = boot_ep_in; ep_out = boot_ep_out; ep_setup = boot_ep_setup; SET_ACTIVE_CONFIGURATION(coming_cfg); if (coming_cfg == 0) { SET_DEVICE_STATE(ADDRESS_STATE); } else { static u8 i; // Switch to decrement loop because of a sdcc bug for (i = 15; i > 0; i--) // for(i = 1; i < 16; i++) { ep_init[coming_cfg][i](); } SET_DEVICE_STATE(CONFIGURED_STATE); } } }
byte BULKavailable() { u8 received = (!EP_OUT_BD(BULK_DATA_EP_NUM).Stat.UOWN) &&(EP_OUT_BD(BULK_DATA_EP_NUM).Cnt > 0); return(received ); }