Exemple #1
0
void udd_test_mode_packet(void)
{
	irqflags_t flags;
	const uint8_t test_packet[] = {
		// 00000000 * 9
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		// 01010101 * 8
		0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
		// 01110111 * 8
		0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
		// 0, {111111S * 15}, 111111
		0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
				0xFF, 0xFF,
		// S, 111111S, {0111111S * 7}
		0x7F, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD,
		// 00111111, {S0111111 * 9}, S0
		0xFC, 0x7E, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD, 0x7E
	};

	// Reconfigure control endpoint to bulk IN endpoint
	udd_disable_endpoint(0);
	udd_configure_endpoint(0, USB_EP_TYPE_BULK, 1,
			64, AVR32_USBC_UECFG0_EPBK_SINGLE);

	udd_enable_hs_test_mode();
	udd_enable_hs_test_mode_packet();

	// Send packet on endpoint 0
	udd_udesc_set_buf0_addr(0, (uint8_t *) test_packet);
	flags = cpu_irq_save();
	udd_enable_in_send_interrupt(0);
	cpu_irq_restore(flags);

	udd_ack_in_send(0);
}
Exemple #2
0
static void udd_reset_ep_ctrl(void)
{
	irqflags_t flags;

	// Reset USB address to 0
	udd_configure_address(0);
	udd_enable_address();

	// Alloc and configure control endpoint
	udd_configure_endpoint(0,
			USB_EP_TYPE_CONTROL,
			0,
			USB_DEVICE_EP_CTRL_SIZE, AVR32_USBC_UECFG0_EPBK_SINGLE);

	// Use internal buffer for endpoint control
	udd_udesc_set_buf0_addr(0, udd_ctrl_buffer);

	// don't use multipacket on endpoint control
	udd_udesc_rst_buf0_size(0);
	udd_enable_endpoint(0);
	udd_disable_busy_bank0(0);
	flags = cpu_irq_save();
	udd_enable_setup_received_interrupt(0);
	udd_enable_out_received_interrupt(0);
	udd_enable_endpoint_interrupt(0);
	cpu_irq_restore(flags);
}
Exemple #3
0
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;
}
bool udd_ep_run(udd_ep_id_t ep, bool b_shortpacket,
		uint8_t * buf, iram_size_t buf_size,
		udd_callback_trans_t callback)
{
	uint16_t ep_size, trans_size, short_packet;
	bool b_dir_in;
	udd_ep_job_t *ptr_job;
	irqflags_t flags;

	b_dir_in = (USB_EP_DIR_IN == (ep & USB_EP_DIR_IN));
	ep &= USB_EP_ADDR_MASK;
	if (USB_DEVICE_MAX_EP < ep)
		return false;

	// Get job about endpoint
	ptr_job = &udd_ep_job[ep - 1];

	if ((!Is_udd_endpoint_enabled(ep))
			|| Is_udd_endpoint_stall_requested(ep))
		return false;	// Endpoint is halted

   flags = cpu_irq_save();
	if (ptr_job->busy == true) {
		cpu_irq_restore(flags);
		return false;	// Job already on going
	}
	ptr_job->busy = true;
	cpu_irq_restore(flags);

	// No job running. Let's setup a new one.
	//
	// The USB hardware support a maximum transfer size of 0x7FFF Bytes
	ep_size = udd_get_endpoint_size(ep);
	if (0x7FFF < buf_size) {
		trans_size = 0x7FFF - (0x7FFF % ep_size);
		short_packet = 0;
	} else {
		trans_size = buf_size;
		short_packet = trans_size % ep_size;
	}

	if (b_dir_in) {
		// Need ZLP, if requested and last packet is not a short packet
		udd_udesc_set_buf0_autozlp(ep, b_shortpacket);
		udd_udesc_set_buf0_ctn(ep, trans_size);
		udd_udesc_rst_buf0_size(ep);
		// Link the user buffer directly on USB hardware DMA
		udd_udesc_set_buf0_addr(ep, buf);
	} else {
		udd_udesc_rst_buf0_ctn(ep);
		ptr_job->nb_trans = 0;
		if (trans_size < ep_size) {
			// The user buffer is smaller than endpoint size
			if (AVR32_USBC_PTYPE_ISOCHRONOUS ==
					udd_get_endpoint_type(ep)) {
				ptr_job->busy = false;
				return false;	// The user must use a buffer corresponding at isochrnous endpoint 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,
					udd_ep_out_cache_buffer[ep - 1]);
			udd_udesc_set_buf0_size(ep, ep_size);
		} else {
			// Link the user buffer directly on USB hardware DMA
			ptr_job->b_use_out_cache_buffer = false;
			udd_udesc_set_buf0_addr(ep, buf);
			udd_udesc_set_buf0_size(ep, trans_size - short_packet);
		}
	}

	// Update Job information
	ptr_job->buf = buf;
	ptr_job->buf_size = trans_size;
	ptr_job->call_trans = callback;
	ptr_job->busy = true;


	// Start transfer
	udd_disable_busy_bank0(ep);

	// Enable interrupt
	flags = cpu_irq_save();
	if (b_dir_in) {
		udd_ack_fifocon(ep);
		udd_ack_in_send(ep);
		udd_enable_in_send_interrupt(ep);
	} else {
		udd_enable_out_received_interrupt(ep);
	}
	udd_enable_endpoint_interrupt(ep);
	cpu_irq_restore(flags);

	return true;
}
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);
}