bool udd_ep_set_halt(udd_ep_id_t ep) { udd_ep_job_t *ptr_job; uint8_t index = ep & USB_EP_ADDR_MASK; if (USB_DEVICE_MAX_EP < index) { return false; } ptr_job = &udd_ep_job[index - 1]; if (Is_udd_endpoint_stall_requested(index) // Endpoint stalled || ptr_job->stall_requested) { // Endpoint stall is requested return true; // Already STALL } if (ptr_job->busy == true) { return false; // Job on going, stall impossible } if ((ep & USB_EP_DIR_IN) && (0 != udd_nb_busy_bank(index))) { // Delay the stall after the end of IN transfer on USB line ptr_job->stall_requested = true; udd_enable_bank_interrupt(index); udd_enable_endpoint_interrupt(index); return true; } // Stall endpoint immediately udd_disable_endpoint_bank_autoswitch(index); udd_ack_stall(index); udd_enable_stall_handshake(index); return true; }
static bool udd_ep_interrupt(void) { udd_ep_id_t ep; udd_ep_job_t *ptr_job; // For each endpoint different of control endpoint (0) for (ep = 1; ep <= USB_DEVICE_MAX_EP; ep++) { // Get job corresponding at endpoint ptr_job = &udd_ep_job[ep - 1]; // Check DMA event if (Is_udd_endpoint_dma_interrupt_enabled(ep) && Is_udd_endpoint_dma_interrupt(ep)) { uint32_t nb_remaining; if( udd_endpoint_dma_get_status(ep) & AVR32_USBB_UDDMA1_STATUS_CH_EN_MASK) { return true; // Ignore EOT_STA interrupt } udd_disable_endpoint_dma_interrupt(ep); // Save number of data no transfered nb_remaining = (udd_endpoint_dma_get_status(ep) & AVR32_USBB_UDDMA1_STATUS_CH_BYTE_CNT_MASK) >> AVR32_USBB_UDDMA1_STATUS_CH_BYTE_CNT_OFFSET; if (nb_remaining) { // Transfer no complete (short packet or ZLP) then: // Update number of data transfered ptr_job->nb_trans -= nb_remaining; // Set transfer complete to stop the transfer ptr_job->buf_size = ptr_job->nb_trans; } udd_ep_trans_done(ep); return true; } // Check empty bank interrupt event if (Is_udd_endpoint_interrupt_enabled(ep)) { if (Is_udd_in_send_interrupt_enabled(ep) && Is_udd_in_send(ep)) { udd_disable_in_send_interrupt(ep); // One bank is free then send a ZLP udd_ack_in_send(ep); udd_ack_fifocon(ep); udd_ep_finish_job(ptr_job, false, ep); return true; } if (Is_udd_bank_interrupt_enabled(ep) && (0 == udd_nb_busy_bank(ep))) { // End of background transfer on IN endpoint udd_disable_bank_interrupt(ep); udd_disable_endpoint_interrupt(ep); Assert(ptr_job->stall_requested); // A stall has been requested during background transfer ptr_job->stall_requested = false; udd_disable_endpoint_bank_autoswitch(ep); udd_enable_stall_handshake(ep); udd_reset_data_toggle(ep); return true; } } }
static bool udd_ep_interrupt(void) { udd_ep_id_t ep; udd_ep_job_t *ptr_job; // For each endpoint different of control endpoint (0) for (ep = 1; ep <= USB_DEVICE_MAX_EP; ep++) { // Check DMA event if (Is_udd_endpoint_dma_interrupt_enabled(ep) && Is_udd_endpoint_dma_interrupt(ep)) { uint32_t nb_remaining; udd_disable_endpoint_dma_interrupt(ep); // Save number of data no transfered nb_remaining = (udd_endpoint_dma_get_status(ep) & AVR32_USBB_UDDMA1_STATUS_CH_BYTE_CNT_MASK) >> AVR32_USBB_UDDMA1_STATUS_CH_BYTE_CNT_OFFSET; // Get job corresponding at endpoint ptr_job = &udd_ep_job[ep - 1]; // Update number of data transfered ptr_job->buf_size -= nb_remaining; if (!Is_udd_endpoint_in(ep)) { // Disable autoswitch bank on OUT udd_disable_endpoint_bank_autoswitch(ep); } else { // Wait end of background transfer on IN endpoint before disabled autoswitch bank udd_enable_endpoint_interrupt(ep); udd_enable_bank_interrupt(ep); } // Call callback to signal end of transfer udd_ep_finish_job(&udd_ep_job[ep - 1], false); return true; } // Check empty bank interrupt event if (Is_udd_endpoint_interrupt_enabled(ep) && (0 == udd_nb_busy_bank(ep))) { // End of background transfer on IN endpoint udd_disable_bank_interrupt(ep); udd_disable_endpoint_interrupt(ep); // If no new transfer running then disable autoswitch bank if (!udd_ep_job[ep - 1].busy) { udd_disable_endpoint_bank_autoswitch(ep); } // If a stall has been requested during backgound transfer then execute it if (udd_ep_job[ep - 1].stall_requested) { udd_ep_job[ep - 1].stall_requested = false; udd_enable_stall_handshake(ep); udd_reset_data_toggle(ep); } return true; } }
void udd_ep_abort(udd_ep_id_t ep) { uint8_t index = ep & USB_EP_ADDR_MASK; // Stop DMA transfer udd_disable_endpoint_dma_interrupt(index); udd_endpoint_dma_set_control(index, 0); // Kill banks udd_disable_endpoint_interrupt(index); while (udd_nb_busy_bank(index)) { udd_kill_last_in_bank(index); while(Is_udd_killing_last_in_bank(index)); } udd_ep_abort_job(ep); }
static void udd_ep_trans_done(udd_ep_id_t ep) { udd_ep_job_t *ptr_job; uint16_t ep_size, nb_trans; uint16_t next_trans; udd_ep_id_t ep_num; irqflags_t flags; ep_num = ep & USB_EP_ADDR_MASK; ep_size = udd_get_endpoint_size(ep_num); // Get job corresponding at endpoint ptr_job = &udd_ep_job[ep_num - 1]; // Disable interrupt of endpoint flags = cpu_irq_save(); udd_disable_endpoint_interrupt(ep_num); cpu_irq_restore(flags); if (!ptr_job->busy) { return; // No job is running, then ignore it (system error) } if (USB_EP_DIR_IN == (ep & USB_EP_DIR_IN)) { // Transfer complete on IN nb_trans = udd_udesc_get_buf0_size(ep_num); // Lock emission of new IN packet udd_enable_busy_bank0(ep_num); // Ack interrupt udd_ack_in_send(ep_num); if (0 == nb_trans) { if (0 == udd_nb_busy_bank(ep_num)) { // All byte are transfered than take nb byte requested nb_trans = udd_udesc_get_buf0_ctn(ep_num); } } // Update number of data transfered ptr_job->nb_trans += nb_trans; // Need to send other data if ((ptr_job->nb_trans != ptr_job->buf_size) || ptr_job->b_shortpacket) { next_trans = ptr_job->buf_size - ptr_job->nb_trans; if (UDD_ENDPOINT_MAX_TRANS < next_trans) { // The USB hardware support a maximum // transfer size of UDD_ENDPOINT_MAX_TRANS Bytes next_trans = UDD_ENDPOINT_MAX_TRANS - (UDD_ENDPOINT_MAX_TRANS % ep_size); udd_udesc_set_buf0_autozlp(ep_num, false); } else { // Need ZLP, if requested and last packet is not a short packet udd_udesc_set_buf0_autozlp(ep_num, ptr_job->b_shortpacket); ptr_job->b_shortpacket = false; // No need to request another ZLP } udd_udesc_set_buf0_ctn(ep_num, next_trans); udd_udesc_rst_buf0_size(ep_num); // Link the user buffer directly on USB hardware DMA udd_udesc_set_buf0_addr(ep_num, &ptr_job->buf[ptr_job->nb_trans]); // Start transfer udd_ack_fifocon(ep_num); udd_disable_busy_bank0(ep_num); // Enable interrupt flags = cpu_irq_save(); udd_enable_in_send_interrupt(ep_num); udd_enable_endpoint_interrupt(ep_num); cpu_irq_restore(flags); return; } } else { // Transfer complete on OUT nb_trans = udd_udesc_get_buf0_ctn(ep_num); // Lock reception of new OUT packet udd_enable_busy_bank0(ep_num); // Ack interrupt udd_ack_out_received(ep_num); udd_ack_fifocon(ep_num); // Can be necessary to copy data receive from cache buffer to user buffer if (ptr_job->b_use_out_cache_buffer) { memcpy(&ptr_job->buf[ptr_job->nb_trans], udd_ep_out_cache_buffer[ep_num - 1], ptr_job->buf_size % ep_size); } // Update number of data transfered ptr_job->nb_trans += nb_trans; if (ptr_job->nb_trans > ptr_job->buf_size) { ptr_job->nb_trans = ptr_job->buf_size; } // If all previous data requested are received and user buffer not full // then need to receive other data if ((nb_trans == udd_udesc_get_buf0_size(ep_num)) && (ptr_job->nb_trans != ptr_job->buf_size)) { next_trans = ptr_job->buf_size - ptr_job->nb_trans; if (UDD_ENDPOINT_MAX_TRANS < next_trans) { // The USB hardware support a maximum transfer size // of UDD_ENDPOINT_MAX_TRANS Bytes next_trans = UDD_ENDPOINT_MAX_TRANS - (UDD_ENDPOINT_MAX_TRANS % ep_size); } else { next_trans -= next_trans % ep_size; } udd_udesc_rst_buf0_ctn(ep_num); if (next_trans < ep_size) { // Use the cache buffer for Bulk or Interrupt size endpoint ptr_job->b_use_out_cache_buffer = true; udd_udesc_set_buf0_addr(ep_num, udd_ep_out_cache_buffer[ep_num-1]); udd_udesc_set_buf0_size(ep_num, ep_size); } else { // Link the user buffer directly on USB hardware DMA udd_udesc_set_buf0_addr(ep_num, &ptr_job->buf[ptr_job->nb_trans]); udd_udesc_set_buf0_size(ep_num, next_trans); } // Start transfer udd_disable_busy_bank0(ep_num); // Enable interrupt flags = cpu_irq_save(); udd_enable_out_received_interrupt(ep_num); udd_enable_endpoint_interrupt(ep_num); cpu_irq_restore(flags); return; } } // Job complete then call callback ptr_job->busy = false; if (NULL != ptr_job->call_trans) { ptr_job->call_trans(UDD_EP_TRANSFER_OK, ptr_job->nb_trans, ep); } return; }
static void udd_ep_finish_job(udd_ep_id_t ep, bool b_abort) { udd_ep_job_t *ptr_job; uint16_t ep_size; irqflags_t flags; // Get job corresponding at endpoint ptr_job = &udd_ep_job[ep - 1]; // Test if a pending transfer is running. If not, disabled interrupt. if (!ptr_job->busy) { flags = cpu_irq_save(); udd_disable_endpoint_interrupt(ep); cpu_irq_restore(flags); return; } if (Is_udd_endpoint_in(ep)) { // Update number of data transfered ptr_job->nb_trans = udd_udesc_get_buf0_size(ep); if (0 == ptr_job->nb_trans) { if (0 == udd_nb_busy_bank(ep)) { // All byte are transfered than take nb byte requested ptr_job->nb_trans = udd_udesc_get_buf0_ctn(ep); } } } else { // Transfer complete on OUT ep_size = udd_format_endpoint_size(ep); if (ptr_job->b_use_out_cache_buffer) { // Copy data receiv from cache buffer to user buffer memcpy(&ptr_job->buf[ptr_job->nb_trans], udd_ep_out_cache_buffer[ep - 1], ptr_job->buf_size % ep_size); ptr_job->nb_trans += udd_udesc_get_buf0_ctn(ep); } else { ptr_job->nb_trans = udd_udesc_get_buf0_ctn(ep); // If all previous data requested are received // and user buffer not full if ((ptr_job->nb_trans == udd_udesc_get_buf0_size(ep)) && (ptr_job->nb_trans != ptr_job->buf_size)) { // Use the cache buffer to receiv last data // which can be more larger than user buffer remaining ptr_job->b_use_out_cache_buffer = true; udd_udesc_rst_buf0_ctn(ep); udd_udesc_set_buf0_addr(ep, udd_ep_out_cache_buffer[ep - 1]); udd_udesc_set_buf0_size(ep, ep_size); // Free buffer to accept another data to reception udd_ack_out_received(ep); udd_ack_fifocon(ep); return; } } // Free buffer but not accept another data to reception udd_ack_out_received(ep); udd_enable_busy_bank0(ep); udd_ack_fifocon(ep); } // Call callback to signal end of transfer flags = cpu_irq_save(); udd_disable_endpoint_interrupt(ep); cpu_irq_restore(flags); ptr_job->busy = false; if (NULL == ptr_job->call_trans) return; // No callback linked to job ptr_job->call_trans((b_abort) ? UDD_EP_TRANSFER_ABORT : UDD_EP_TRANSFER_OK, ptr_job->nb_trans); }