/* * data Received Callback * It toggles an LED based on the first received character. */ void dataReceived(USBDriver *usbp, usbep_t ep){ USBOutEndpointState *osp = usbp->epc[ep]->out_state; (void) usbp; (void) ep; if(osp->rxcnt){ switch(receiveBuf[0]){ case '1': palTogglePad(GPIOD, GPIOD_LED3); break; case '2': palTogglePad(GPIOD, GPIOD_LED4); break; case '3': palTogglePad(GPIOD, GPIOD_LED5); break; case '4': palTogglePad(GPIOD, GPIOD_LED6); break; } } /* * Initiate next receive */ usbPrepareReceive(usbp, EP_OUT, receiveBuf, OUT_PACKETSIZE); chSysLockFromIsr(); usbStartReceiveI(usbp, EP_OUT); chSysUnlockFromIsr(); }
/** * @brief Default EP0 IN callback. * @details This function is used by the low level driver as default handler * for EP0 IN events. * * @param[in] usbp pointer to the @p USBDriver object * @param[in] ep endpoint number, always zero * * @notapi */ void _usb_ep0in(USBDriver *usbp, usbep_t ep) { size_t max; (void)ep; switch (usbp->ep0state) { case USB_EP0_TX: max = (size_t)get_hword(&usbp->setup[6]); /* If the transmitted size is less than the requested size and it is a multiple of the maximum packet size then a zero size packet must be transmitted.*/ if ((usbp->ep0n < max) && ((usbp->ep0n % usbp->epc[0]->in_maxsize) == 0U)) { usbPrepareTransmit(usbp, 0, NULL, 0); osalSysLockFromISR(); (void) usbStartTransmitI(usbp, 0); osalSysUnlockFromISR(); usbp->ep0state = USB_EP0_WAITING_TX0; return; } /* Falls into, it is intentional.*/ case USB_EP0_WAITING_TX0: /* Transmit phase over, receiving the zero sized status packet.*/ usbp->ep0state = USB_EP0_WAITING_STS; #if (USB_EP0_STATUS_STAGE == USB_EP0_STATUS_STAGE_SW) usbPrepareReceive(usbp, 0, NULL, 0); osalSysLockFromISR(); (void) usbStartReceiveI(usbp, 0); osalSysUnlockFromISR(); #else usb_lld_end_setup(usbp, ep); #endif return; case USB_EP0_SENDING_STS: /* Status packet sent, invoking the callback if defined.*/ if (usbp->ep0endcb != NULL) { usbp->ep0endcb(usbp); } usbp->ep0state = USB_EP0_WAITING_SETUP; return; case USB_EP0_WAITING_SETUP: case USB_EP0_WAITING_STS: case USB_EP0_RX: /* All the above are invalid states in the IN phase.*/ osalDbgAssert(false, "EP0 state machine error"); /* Falling through is intentional.*/ case USB_EP0_ERROR: /* Error response, the state machine goes into an error state, the low level layer will have to reset it to USB_EP0_WAITING_SETUP after receiving a SETUP packet.*/ usb_lld_stall_in(usbp, 0); usb_lld_stall_out(usbp, 0); _usb_isr_invoke_event_cb(usbp, USB_EVENT_STALLED); usbp->ep0state = USB_EP0_ERROR; return; default: osalDbgAssert(false, "EP0 state machine invalid state"); } }
static msg_t tUsbRx(void *arg) { (void)arg; chRegSetThreadName("usbRx"); enum {UsbRxComleteID = 0, UsbResetID = 1, UsbConfiguredID = 2}; usbPacket *usbBufp; EventListener elUsbRxComplete; EventListener elUsbReset; EventListener elUsbConfigured; eventmask_t activeEvents; chEvtRegister(&esUsbRxComplete, &elUsbRxComplete, UsbRxComleteID); chEvtRegister(&esUsbReset, &elUsbReset, UsbResetID); chEvtRegister(&esUsbConfigured, &elUsbConfigured, UsbConfiguredID); // Wait for the USB system to be configured. chEvtWaitOne(EVENT_MASK(UsbConfiguredID)); chEvtGetAndClearEvents(EVENT_MASK(UsbRxComleteID) | EVENT_MASK(UsbResetID)); while (TRUE) { // Allocate buffer space for reception of package in the sysctrl mempool usbBufp = usbAllocMailboxBuffer(); // Prepare receive operation and initiate the usb system to listen usbPrepareReceive(usbp, EP_OUT, usbBufp->packet, 64); chSysLock(); usbStartReceiveI(usbp, EP_OUT); chSysUnlock(); // Wait for events from the USB system activeEvents = chEvtWaitAny(EVENT_MASK(UsbRxComleteID) | EVENT_MASK(UsbResetID)); if (activeEvents == EVENT_MASK(UsbResetID)) { // If the system was reset, clean up and wait for new configure. usbFreeMailboxBuffer (usbBufp); chEvtWaitOne(EVENT_MASK(UsbConfiguredID)); chEvtGetAndClearEvents(EVENT_MASK(UsbRxComleteID) | EVENT_MASK(UsbResetID)); } else { // Post pagckage to sysctrl if receive was successful usbBufp->size = ep2outstate.rxcnt; chMBPost (&usbRXMailbox, (msg_t)usbBufp, TIME_INFINITE); } } return 0; }
uint8_t receive_data(){ // while EP_BUSY usbStatus=1; if(initUSB){ usbPrepareReceive(usbp, EP_OUT, receiveBuf, 64); chSysLock(); usbStartReceiveI(usbp, EP_OUT); chSysUnlock(); } initUSB=0; return 0; }
/** * @brief Default EP0 IN callback. * @details This function is used by the low level driver as default handler * for EP0 IN events. * * @param[in] usbp pointer to the @p USBDriver object * @param[in] ep endpoint number, always zero * * @notapi */ void _usb_ep0in(USBDriver *usbp, usbep_t ep) { size_t max; (void)ep; switch (usbp->ep0state) { case USB_EP0_TX: max = usbFetchWord(&usbp->setup[6]); /* If the transmitted size is less than the requested size and it is a multiple of the maximum packet size then a zero size packet must be transmitted.*/ if ((usbp->ep0n < max) && ((usbp->ep0n % usbp->epc[0]->in_maxsize) == 0)) { usbPrepareTransmit(usbp, 0, NULL, 0); chSysLockFromIsr(); usbStartTransmitI(usbp, 0); chSysUnlockFromIsr(); usbp->ep0state = USB_EP0_WAITING_TX0; return; } /* Falls into, it is intentional.*/ case USB_EP0_WAITING_TX0: /* Transmit phase over, receiving the zero sized status packet.*/ usbp->ep0state = USB_EP0_WAITING_STS; usbPrepareReceive(usbp, 0, NULL, 0); chSysLockFromIsr(); usbStartReceiveI(usbp, 0); chSysUnlockFromIsr(); return; case USB_EP0_SENDING_STS: /* Status packet sent, invoking the callback if defined.*/ if (usbp->ep0endcb != NULL) usbp->ep0endcb(usbp); usbp->ep0state = USB_EP0_WAITING_SETUP; return; default: ; } /* Error response, the state machine goes into an error state, the low level layer will have to reset it to USB_EP0_WAITING_SETUP after receiving a SETUP packet.*/ usb_lld_stall_in(usbp, 0); usb_lld_stall_out(usbp, 0); _usb_isr_invoke_event_cb(usbp, USB_EVENT_STALLED); usbp->ep0state = USB_EP0_ERROR; }
/* * data Received Callback * It prints to shell a string */ void dataReceived(USBDriver *usbp, usbep_t ep){ USBOutEndpointState *osp = usbp->epc[ep]->out_state; (void) usbp; (void) ep; // exit on USB reset if(!usbStatus) return; if(osp->rxcnt){ // chprintf(chp,"Data received"); } /* * Initiate next receive */ usbPrepareReceive(usbp, EP_OUT, receiveBuf, 64); chSysLockFromIsr(); usbStartReceiveI(usbp, EP_OUT); chSysUnlockFromIsr(); }
int main(void) { //start system halInit(); chSysInit(); //start and connect USB usbStart(usbp, &config); usbConnectBus(usbp); //init the ADC myADCinit(); //main loop, inits the USB transfers when it is told to do so while (TRUE) { while(!initUSB){ chThdSleepMilliseconds(100); } palTogglePad(GPIOD, GPIOD_LED6); /* * starts first receiving transaction * all further transactions are initiated by the dataReceived callback */ usbPrepareReceive(usbp, EP_OUT, receiveBuf, 64); chSysLock(); usbStartReceiveI(usbp, EP_OUT); chSysUnlock(); /* * starts the transfer thread */ tp = chThdCreateStatic(waInitUsbTransfer, sizeof(waInitUsbTransfer), NORMALPRIO, initUsbTransfer, NULL); initUSB=0; } }
/** * @brief Default EP0 SETUP callback. * @details This function is used by the low level driver as default handler * for EP0 SETUP events. * * @param[in] usbp pointer to the @p USBDriver object * @param[in] ep endpoint number, always zero * * @notapi */ void _usb_ep0setup(USBDriver *usbp, usbep_t ep) { size_t max; usbp->ep0state = USB_EP0_WAITING_SETUP; usbReadSetup(usbp, ep, usbp->setup); /* First verify if the application has an handler installed for this request.*/ if (!(usbp->config->requests_hook_cb) || !(usbp->config->requests_hook_cb(usbp))) { /* Invoking the default handler, if this fails then stalls the endpoint zero as error.*/ if (((usbp->setup[0] & USB_RTYPE_TYPE_MASK) != USB_RTYPE_TYPE_STD) || !default_handler(usbp)) { /* Error response, the state machine goes into an error state, the low level layer will have to reset it to USB_EP0_WAITING_SETUP after receiving a SETUP packet.*/ usb_lld_stall_in(usbp, 0); usb_lld_stall_out(usbp, 0); _usb_isr_invoke_event_cb(usbp, USB_EVENT_STALLED); usbp->ep0state = USB_EP0_ERROR; return; } } #if (USB_SET_ADDRESS_ACK_HANDLING == USB_SET_ADDRESS_ACK_HW) if (usbp->setup[1] == USB_REQ_SET_ADDRESS) { /* Zero-length packet sent by hardware */ return; } #endif /* Transfer preparation. The request handler must have populated correctly the fields ep0next, ep0n and ep0endcb using the macro usbSetupTransfer().*/ max = usbFetchWord(&usbp->setup[6]); /* The transfer size cannot exceed the specified amount.*/ if (usbp->ep0n > max) usbp->ep0n = max; if ((usbp->setup[0] & USB_RTYPE_DIR_MASK) == USB_RTYPE_DIR_DEV2HOST) { /* IN phase.*/ if (usbp->ep0n > 0) { /* Starts the transmit phase.*/ usbp->ep0state = USB_EP0_TX; usbPrepareTransmit(usbp, 0, usbp->ep0next, usbp->ep0n); osalSysLockFromISR(); usbStartTransmitI(usbp, 0); osalSysUnlockFromISR(); } else { /* No transmission phase, directly receiving the zero sized status packet.*/ usbp->ep0state = USB_EP0_WAITING_STS; #if (USB_EP0_STATUS_STAGE == USB_EP0_STATUS_STAGE_SW) usbPrepareReceive(usbp, 0, NULL, 0); osalSysLockFromISR(); usbStartReceiveI(usbp, 0); osalSysUnlockFromISR(); #else usb_lld_end_setup(usbp, ep); #endif } } else { /* OUT phase.*/ if (usbp->ep0n > 0) { /* Starts the receive phase.*/ usbp->ep0state = USB_EP0_RX; usbPrepareReceive(usbp, 0, usbp->ep0next, usbp->ep0n); osalSysLockFromISR(); usbStartReceiveI(usbp, 0); osalSysUnlockFromISR(); } else { /* No receive phase, directly sending the zero sized status packet.*/ usbp->ep0state = USB_EP0_SENDING_STS; #if (USB_EP0_STATUS_STAGE == USB_EP0_STATUS_STAGE_SW) usbPrepareTransmit(usbp, 0, NULL, 0); osalSysLockFromISR(); usbStartTransmitI(usbp, 0); osalSysUnlockFromISR(); #else usb_lld_end_setup(usbp, ep); #endif } } }
void hid_recive(USBDriver *usbp) { usbPrepareReceive(usbp, HID_OUT_EP_ADDRESS, (uint8_t *)&hid_out_data, sizeof (hid_out_data)); chSysLockFromISR(); usbStartReceiveI(usbp, HID_OUT_EP_ADDRESS); chSysUnlockFromISR(); }