#include "pic_usb.h"
#include "memory.h"
#include "config.h"
#include "pic_usb_buffer_mgt.h"
#include "pic_serial.h"
Go to the source code of this file.
Functions | |
void | turn_usb_ints_on () |
Turn on USB interrupts. | |
void | usb_configure_endpoints () |
void | usb_enable_module () |
Enables the USB hardware and starts USB negotiations. | |
usb_state_type | usb_get_state () |
Query the current state of the USB connection. | |
void | usb_handle_isr () |
Handle USB interrupts. | |
void | usb_handle_reset () |
void | usb_handle_stall () |
void | usb_handle_standard_request (setup_data_packet sdp) |
void | usb_handle_transaction (uns8 stat) |
void | usb_prime_ep0_out_e () |
void | usb_prime_ep0_out_o () |
void | usb_send_data (uns8 ep, uns8 *data, uns8 send_count, bit first) |
Send data over an endpoint pipe. | |
void | usb_send_data_chunk () |
void | usb_send_empty_data_pkt () |
Send an empty data packet. | |
void | usb_send_one_byte (uns8 data) |
void | usb_setup () |
Setup USB hardware ready for use. | |
void | usb_stall_ep0 () |
Send a stall on control transfer endpoint. | |
void | usb_stall_on_in () |
Variables | |
uns8 | buffer_byte |
control_mode_type | control_mode |
buffer_descriptor * | delivery_bd |
uns8 * | delivery_buffer |
uns8 | delivery_buffer_size |
uns16 | delivery_bytes_max_send |
uns16 | delivery_bytes_sent |
uns16 | delivery_bytes_to_send |
uns8 * | delivery_ptr |
uns8 | usb_address |
setup_data_packet | usb_sdp |
usb_state_type | usb_state = st_POWERED |
usb_status_type | usb_status |
void turn_usb_ints_on | ( | ) |
If you are using interrupt-driven code (generally the best way of doing things) you can turn on USB interrupts using turn_usb_ints_on(). Don't forget that you will also need to call turn_global_ints_on() as well. Typically this is called in your system setup routine.
void usb_configure_endpoints | ( | ) |
Definition at line 66 of file pic_usb.c.
References buffer_descriptor::addr, BC8, BC9, bd1in, bd1out, bd2in, bd2out, bd3in, bd3out, BSTALL, buffer_descriptor::count, DTS, DTSEN, INCDIS, KEN, serial_print_str(), buffer_descriptor::stat, and UOWN.
Referenced by usb_handle_standard_request().
void usb_enable_module | ( | ) |
After you've called usb_setup(), you can call usb_enable_module() whenever you're ready for USB negotiations to occur. Normally, this would need to occur relatively quickly after power-up if your PIC is powered by USB and it's purpose is to talk over USB. This is normally called from your main() routine once all other configuration is done.
Once the USB module has successfully negotiated a connection with the host, usb_device_configured_callback() will be called if you have requested this in your config.h file. This will indicate a successful connection. Because of the way USB works, there is no way to tell that it *hasn't* worked, except via a timer - if you haven't had a good connection in several seconds, you can assume it has failed (although this may just mean the user is hunting for a driver disk etc).
Definition at line 1033 of file pic_usb.c.
References st_DEFAULT, and usb_state.
usb_state_type usb_get_state | ( | ) |
void usb_handle_isr | ( | ) |
usb_handle_isr() should be inserted in your interrupt service routine. Alternatively, if you have reason not to want to do interrupt-driven USB, for example, a bootloader, you can poll this routine.
Make sure you call turn_usb_ints() and turn_global_ints_on() to ensure interrupts occur.
It will check for any of the USB interrupt flags and handle: USB transactions, USB reset, USB stall, USB Start Of Frame (including calling usb_SOF_callback() if configured in your config.h and most importantly USB transaction, which is where all the hard work is done.
Definition at line 928 of file pic_usb.c.
References uns8, usb_handle_reset(), usb_handle_stall(), usb_handle_transaction(), and usb_SOF_callback().
void usb_handle_reset | ( | ) |
Definition at line 853 of file pic_usb.c.
References buffer_descriptor::addr, BC8, BC9, bd0in, bd0out_e, bd0out_o, BSTALL, cm_IDLE, control_mode, buffer_descriptor::count, DTS, DTSEN, INCDIS, KEN, serial_print_int_hex(), serial_print_str(), buffer_descriptor::stat, UOWN, us_IDLE, usb_address, USB_EP0_IN_ADDR, USB_EP0_OUT_E_ADDR, USB_EP0_OUT_O_ADDR, and usb_status.
Referenced by usb_handle_isr().
void usb_handle_stall | ( | ) |
Definition at line 918 of file pic_usb.c.
References bd0in, BSTALL, serial_print_str(), buffer_descriptor::stat, and UOWN.
Referenced by usb_handle_isr().
void usb_handle_standard_request | ( | setup_data_packet | sdp | ) |
Definition at line 428 of file pic_usb.c.
References bd0in, setup_data_packet::bRequest, cm_CTRL_READ_DATA_STAGE, cm_CTRL_WRITE_SENDING_STATUS, control_mode, delivery_buffer, delivery_buffer_size, delivery_bytes_max_send, delivery_bytes_sent, delivery_bytes_to_send, delivery_ptr, DTS, req_Get_Descriptor, req_Get_Interface, req_Get_Status, req_Set_Address, req_Set_Configuration, serial_print_int(), serial_print_int_hex(), serial_print_str(), st_CONFIGURED, buffer_descriptor::stat, uns8, us_SET_ADDRESS, usb_address, usb_configure_endpoints(), usb_device_configured_callback(), USB_EP0_IN_ADDR, usb_get_descriptor_callback(), usb_send_data_chunk(), usb_send_one_byte(), usb_send_status_ack, usb_stall_ep0(), usb_state, usb_status, setup_data_packet::wLength, and setup_data_packet::wValue.
Referenced by usb_handle_transaction().
void usb_handle_transaction | ( | uns8 | stat | ) |
!usb_stall_on_in();
Definition at line 524 of file pic_usb.c.
References buffer_descriptor::addr, BC8, BC9, bd0in, bd0out_e, bd0out_o, setup_data_packet::bmRequestType, setup_data_packet::bRequest, BSTALL, buffer_0_in, buffer_0_out_e, buffer_0_out_o, cm_CTRL_READ_AWAITING_STATUS, cm_CTRL_READ_DATA_STAGE, cm_CTRL_READ_DATA_STAGE_CLASS, cm_CTRL_WRITE_DATA_STAGE_CLASS, cm_CTRL_WRITE_SENDING_STATUS, cm_IDLE, control_mode, buffer_descriptor::count, DATA_STAGE_DIR, DTS, DTSEN, ep_in_bd_location, ep_out_bd_location, ep_out_buffer_location, ep_out_buffer_size, INCDIS, KEN, pid_ACK, pid_IN, pid_OUT, pid_SETUP, REQUEST_TYPE0, REQUEST_TYPE1, serial_print_int(), serial_print_int_hex(), serial_print_int_hex_16bit(), serial_print_spc(), serial_print_str(), serial_putc(), st_ADDRESS, buffer_descriptor::stat, uns16, uns8, UOWN, us_IDLE, us_SET_ADDRESS, usb_address, usb_ep_data_in_callback(), usb_ep_data_out_callback(), usb_handle_class_ctrl_read_callback(), usb_handle_class_ctrl_write_callback(), usb_handle_class_request_callback(), usb_handle_standard_request(), usb_prime_ep0_out_e(), usb_prime_ep0_out_o(), usb_send_data_chunk(), usb_state, usb_status, setup_data_packet::wIndex, setup_data_packet::wLength, and setup_data_packet::wValue.
Referenced by usb_handle_isr().
void usb_prime_ep0_out_e | ( | ) |
! clear
Definition at line 384 of file pic_usb.c.
References buffer_descriptor::addr, BC8, BC9, bd0out_e, BSTALL, buffer_descriptor::count, DTS, DTSEN, INCDIS, KEN, serial_putc(), buffer_descriptor::stat, UOWN, and USB_EP0_OUT_E_ADDR.
Referenced by usb_handle_transaction().
void usb_prime_ep0_out_o | ( | ) |
! clear
Definition at line 406 of file pic_usb.c.
References buffer_descriptor::addr, BC8, BC9, bd0out_o, BSTALL, buffer_descriptor::count, DTS, DTSEN, INCDIS, KEN, serial_putc(), buffer_descriptor::stat, UOWN, and USB_EP0_OUT_O_ADDR.
Referenced by usb_handle_transaction().
void usb_send_data | ( | uns8 | ep, | |
uns8 * | data, | |||
uns8 | send_count, | |||
bit | first | |||
) |
Use this routine to send data across the USB pipe.
ep | Endpoint that the data should be sent from | |
data | pointer to the data | |
send_count | the number of bytes to send | |
first | True if this is the first in a series of sends. Generally, this can be set to False, since it will automatically be set to the right value on endpoint creation. However, in the case of control transfers, the data stage needs to have the first parameter set to True to ensure the DTS bit is set correctly. |
Definition at line 211 of file pic_usb.c.
References buffer_descriptor::addr, BC8, BC9, BSTALL, buffer_descriptor::count, DTS, DTSEN, ep_in_bd_location, ep_in_buffer_location, INCDIS, KEN, serial_print_int(), serial_print_str(), serial_putc(), buffer_descriptor::stat, uns16, uns8, and UOWN.
Referenced by usb_handle_class_request_callback().
void usb_send_data_chunk | ( | ) |
Definition at line 260 of file pic_usb.c.
References buffer_descriptor::addr, BC8, BC9, bd0in, BSTALL, buffer_0_in, cm_CTRL_READ_AWAITING_STATUS, cm_CTRL_WRITE_SENDING_STATUS, control_mode, buffer_descriptor::count, delivery_buffer, delivery_buffer_size, delivery_bytes_max_send, delivery_bytes_sent, delivery_bytes_to_send, delivery_ptr, DTS, DTSEN, INCDIS, KEN, serial_print_int(), serial_print_str(), buffer_descriptor::stat, uns16, uns8, and UOWN.
Referenced by usb_handle_standard_request(), usb_handle_transaction(), usb_send_empty_data_pkt(), and usb_send_one_byte().
void usb_send_empty_data_pkt | ( | ) |
Use this routine to send an data across the USB pipe on endpoint 0. This is the equivalent of sending a status acknowledge.
Definition at line 341 of file pic_usb.c.
References bd0in, buffer_0_in, delivery_buffer, delivery_buffer_size, delivery_bytes_max_send, delivery_bytes_sent, delivery_bytes_to_send, delivery_ptr, DTS, buffer_descriptor::stat, uns8, and usb_send_data_chunk().
void usb_send_one_byte | ( | uns8 | data | ) |
Definition at line 370 of file pic_usb.c.
References bd0in, buffer_0_in, buffer_byte, delivery_buffer, delivery_buffer_size, delivery_bytes_max_send, delivery_bytes_sent, delivery_bytes_to_send, delivery_ptr, DTS, buffer_descriptor::stat, uns8, and usb_send_data_chunk().
Referenced by usb_handle_standard_request().
void usb_setup | ( | ) |
usb_setup() configures the PIC USB hardware ready for use and prepares the internal data structures used to keep track of where the endpoint buffers are.
After calling usb_setup(), you are ready to call usb_enable_module() to actually start USB negotiations. Ensure that you have usb_handle_isr() in your interrupt service routine.
Definition at line 973 of file pic_usb.c.
References bd0in, bd0out_e, bd1in, bd1out, bd2in, bd2out, bd3in, bd3out, bd4in, bd4out, ep_in_bd_location, ep_out_bd_location, st_POWERED, and usb_state.
void usb_stall_ep0 | ( | ) |
Use this routine to send a stall on the control transfer endpoint - usually used to indicate that the requested function is not available.
Definition at line 203 of file pic_usb.c.
References bd0in, BSTALL, buffer_descriptor::stat, and UOWN.
Referenced by usb_handle_class_request_callback(), and usb_handle_standard_request().
void usb_stall_on_in | ( | ) |
uns8 buffer_byte |
Definition at line 368 of file pic_usb.c.
Referenced by usb_send_one_byte().
Store the control mode state
Definition at line 55 of file pic_usb.c.
Referenced by usb_handle_class_ctrl_read_callback(), usb_handle_class_ctrl_write_callback(), usb_handle_class_request_callback(), usb_handle_reset(), usb_handle_standard_request(), usb_handle_transaction(), and usb_send_data_chunk().
uns8* delivery_buffer |
Definition at line 61 of file pic_usb.c.
Referenced by usb_handle_standard_request(), usb_send_data_chunk(), usb_send_empty_data_pkt(), and usb_send_one_byte().
uns8 delivery_buffer_size |
Definition at line 60 of file pic_usb.c.
Referenced by usb_handle_standard_request(), usb_send_data_chunk(), usb_send_empty_data_pkt(), and usb_send_one_byte().
uns16 delivery_bytes_max_send |
Definition at line 56 of file pic_usb.c.
Referenced by usb_handle_standard_request(), usb_send_data_chunk(), usb_send_empty_data_pkt(), and usb_send_one_byte().
uns16 delivery_bytes_sent |
Definition at line 56 of file pic_usb.c.
Referenced by usb_handle_standard_request(), usb_send_data_chunk(), usb_send_empty_data_pkt(), and usb_send_one_byte().
uns16 delivery_bytes_to_send |
Definition at line 56 of file pic_usb.c.
Referenced by usb_handle_standard_request(), usb_send_data_chunk(), usb_send_empty_data_pkt(), and usb_send_one_byte().
uns8* delivery_ptr |
Definition at line 59 of file pic_usb.c.
Referenced by usb_handle_standard_request(), usb_send_data_chunk(), usb_send_empty_data_pkt(), and usb_send_one_byte().
uns8 usb_address |
Store the usb address
Definition at line 54 of file pic_usb.c.
Referenced by usb_handle_reset(), usb_handle_standard_request(), and usb_handle_transaction().
Store the last setup data packet
Definition at line 52 of file pic_usb.c.
Referenced by usb_handle_class_ctrl_read_callback(), and usb_handle_class_ctrl_write_callback().
usb_state_type usb_state = st_POWERED |
Store the current USB device state
Definition at line 50 of file pic_usb.c.
Referenced by usb_enable_module(), usb_get_state(), usb_handle_standard_request(), usb_handle_transaction(), and usb_setup().
Definition at line 64 of file pic_usb.c.
Referenced by usb_handle_reset(), usb_handle_standard_request(), and usb_handle_transaction().