/*********************************************************************************** * @fn halUartPollRx * * @brief Poll for data from USB. * * @param none * * @return none */ void halUartPollRx(void) { uint8 cnt; uint8 ep = USBFW_GET_SELECTED_ENDPOINT(); USBFW_SELECT_ENDPOINT(4); /* If the OUT endpoint has received a complete packet. */ if (USBFW_OUT_ENDPOINT_DISARMED()) { halIntState_t intState; HAL_ENTER_CRITICAL_SECTION(intState); /* Get length of USB packet, this operation must not be interrupted. */ cnt = USBFW_GET_OUT_ENDPOINT_COUNT_LOW(); cnt += USBFW_GET_OUT_ENDPOINT_COUNT_HIGH() >> 8; HAL_EXIT_CRITICAL_SECTION(intState); while (cnt--) { halUartRxQ[halUartRxT++] = HWREG(USB_F4); } USBFW_ARM_OUT_ENDPOINT(); /* If the USB has transferred in more Rx bytes, reset the Rx idle timer. */ /* Re-sync the shadow on any 1st byte(s) received. */ if (rxTick == 0) { rxShdw = ST0; } rxTick = HAL_UART_USB_IDLE; } else if (rxTick)
/*********************************************************************************** * @fn usbOutProcess * * @brief Handle traffic flow from USB to RF. * * @param none * * @return none */ static void usbOutProcess(void) { uint8 length, nToSend; // If new packet is ready in USB FIFO halIntOff(); oldEndpoint = USBFW_GET_SELECTED_ENDPOINT(); USBFW_SELECT_ENDPOINT(4); if (USBFW_OUT_ENDPOINT_DISARMED() ) { // Get length of USB packet, this operation must not be interrupted. length = USBFW_GET_OUT_ENDPOINT_COUNT_LOW(); length+= (int)(USBFW_GET_OUT_ENDPOINT_COUNT_HIGH()) >> 8; // Calculate number of bytes available in RF buffer; and the number // of bytes we may transfer in this operation. nToSend= MIN(BUF_SIZE - bufNumBytes(&rbRxBuf), length); // Space available in UART RX buffer ? if (nToSend>0) { // Read from USB FIFO usbfwReadFifo(&USBF4, nToSend, buffer); // Write to radio TX buffer bufPut(&rbRxBuf,buffer,nToSend); // If entire USB packet is read from buffer if (length == nToSend) { USBFW_SELECT_ENDPOINT(4); USBFW_ARM_OUT_ENDPOINT(); } } } USBFW_SELECT_ENDPOINT(oldEndpoint); halIntOn(); }
// ************************ USB interrupt event processing ************************* void usbirqHookProcessEvents(void) { T1CNTL=0; if (usbirqData.eventMask & USBIRQ_EVENT_EP5IN){ if (isFifoEmpty()==0){ struct UsbISR * msg = (struct UsbISR *)osal_msg_allocate(sizeof(struct UsbISR) ); msg->msg.event = EVENT_USB_ISR; msg->isr = eventSendFifo; osal_msg_send(zusbTaskId, (uint8 *)msg); } } if (usbirqData.eventMask & USBIRQ_EVENT_EP2OUT){ uint8 oldEndpoint = USBFW_GET_SELECTED_ENDPOINT(); USBFW_SELECT_ENDPOINT(2); uint8 length = USBFW_GET_OUT_ENDPOINT_COUNT_LOW(); if (length > MAX_DATE_SIZE_2){ length = MAX_DATE_SIZE_2; } if (length) { struct UsbISR * msg =NULL; uint8 code = USBF2; switch( code){ case ENABLE_INFO_MESSAGE: usbOn=1; break; case REQ_RESET: msg = (struct UsbISR *)osal_msg_allocate(sizeof(struct UsbISR) ); msg->msg.event = EVENT_USB_ISR; msg->isr = eventReset; break; case REQ_BIND_TABLE: { uint8 addr[2]; addr[0] = USBF2; addr[1] = USBF2; struct BindTableRequestMsg * msgReq = (struct BindTableRequestMsg *)osal_msg_allocate(sizeof(struct BindTableRequestMsg) ); msg = &(msgReq->isr); msg->isr = eventBindReq; msg->msg.event = EVENT_USB_ISR; msgReq->afAddrType.addrMode = Addr16Bit; msgReq->afAddrType.addr.shortAddr = *(uint16 *)(addr); } break; case REQ_ACTIVE_EP:{ struct ReqActiveEndpointsEvent * msgEP = (struct ReqActiveEndpointsEvent *)osal_msg_allocate(sizeof(struct ReqActiveEndpointsEvent) ); msg = &(msgEP->isr); msg->isr = eventActiveEP; msg->msg.event = EVENT_USB_ISR; msgEP->data[0] = USBF2; msgEP->data[1] = USBF2; } break; case REQ_ADD_BIND_TABLE_ENTRY: msg = createMsgForBind(); msg->isr = eventBindRequest; break; case REQ_REMOVE_BIND_TABLE_ENTRY: msg = createMsgForBind(); msg->isr = eventUnbindRequest; break; case NODE_POWER_REQUEST:{ struct ReqPowerNodeMsg * msgReq =(struct ReqPowerNodeMsg *)osal_msg_allocate(sizeof(struct ReqPowerNodeMsg) ); msg = &(msgReq->isr); msg->isr = eventReqPowerNode; msg->msg.event = EVENT_USB_ISR; msgReq->data[0] = USBF2; msgReq->data[1] = USBF2; } break; case REQ_IEEE_ADDRESS:{ struct ReqIeeeAddrMsg * msgReq = (struct ReqIeeeAddrMsg *)osal_msg_allocate(sizeof(struct ReqIeeeAddrMsg) ); msg = &(msgReq->isr); msg->isr = eventReqIeeeAddr; msg->msg.event = EVENT_USB_ISR; msgReq->data[0] = USBF2; msgReq->data[1] = USBF2; msgReq->requestType = USBF2; msgReq->startIndex = USBF2; break; } case WRITE_ATTRIBUTE_VALUE:{ struct WriteAttributeValueUsbMsg usbMsg; uint8 * data = (uint8 *)(&usbMsg); uint8 i; for(i=0; i < sizeof(struct WriteAttributeValueUsbMsg); i++){ *data = USBF2; data++; } struct WriteAttributeValueMsg * msgCmd = (struct WriteAttributeValueMsg *)osal_msg_allocate(sizeof(struct WriteAttributeValueMsg) +sizeof(zclWriteRec_t) + usbMsg.dataValueLen ); msg = &(msgCmd->isr); msg->isr = eventWriteValue; msg->msg.event = EVENT_USB_ISR; msgCmd->afAddrType.addrMode=afAddr16Bit; msgCmd->afAddrType.addr.shortAddr=usbMsg.nwkAddr; msgCmd->afAddrType.endPoint=usbMsg.endpoint; msgCmd->cluster = usbMsg.cluster; msgCmd->writeCmd.numAttr=1; msgCmd->writeCmd.attrList->attrID = usbMsg.attributeId; msgCmd->writeCmd.attrList->dataType=usbMsg.dataType; data = ((uint8 *)msgCmd) + sizeof(struct WriteAttributeValueMsg) +sizeof(zclWriteRec_t); msgCmd->writeCmd.attrList->attrData = data; for(i=0; i < usbMsg.dataValueLen; i++){ *data = USBF2; data++; } } break; case SEND_CMD:{ struct SendCmdUsbMsg usbMsg; uint8 * data = (uint8 *)(&usbMsg); uint8 i; for(i=0; i < sizeof(struct SendCmdUsbMsg); i++){ *data = USBF2; data++; } struct SendCmdMsg * msgCmd = (struct SendCmdMsg *)osal_msg_allocate(sizeof(struct SendCmdMsg) +usbMsg.dataLen ); msg = &(msgCmd->isr); msg->isr = eventSendCmd; msg->msg.event = EVENT_USB_ISR; msgCmd->cluster =usbMsg.cluster; msgCmd->cmdClusterId = usbMsg.cmdClusterId; msgCmd->afAddrType.addr.shortAddr= usbMsg.nwkAddr; msgCmd->afAddrType.addrMode = afAddr16Bit; msgCmd->afAddrType.endPoint = usbMsg.endpoint; msgCmd->dataLen = usbMsg.dataLen; data = (uint8 *)(msgCmd->data); for(i=0; i < usbMsg.dataLen; i++){ *data = USBF2; data++; } } break; case REQ_ATTRIBUTE_VALUES: { struct ReqAttributeValueMsg attr; uint8 * data = (uint8 *)(&attr); uint8 i; for(i=0; i < sizeof(struct ReqAttributeValueMsg); i++){ *data = USBF2; data++; } struct ReqAttributeMsg * msgAttr = (struct ReqAttributeMsg *)osal_msg_allocate(sizeof(struct ReqAttributeMsg) +attr.numAttributes* sizeof(uint16) ); msg = &(msgAttr->isr); msg->isr = attributeValue; msg->msg.event = EVENT_USB_ISR; msgAttr->afAddrType.addr.shortAddr = attr.nwkAddr; msgAttr->afAddrType.addrMode = afAddr16Bit; msgAttr->afAddrType.endPoint = attr.endpoint; msgAttr->numAttr = attr.numAttributes; data = (uint8 *)&msgAttr->attrID; for (uint8 i=0; i < attr.numAttributes; i++){ *data = USBF2; data++; *data = USBF2; data++; } msgAttr->cluster = attr.cluster; osal_msg_send(zusbTaskId, (uint8 *)msg); break; } case REQ_DEVICE_INFO:{ struct ReqDeviceInformationEvent * msgEP = (struct ReqDeviceInformationEvent *)osal_msg_allocate(sizeof(struct ReqDeviceInformationEvent) ); msg = &(msgEP->isr); msg->isr = eventDeviceInfo; msg->msg.event = EVENT_USB_ISR; msgEP->data[0] = USBF2; msgEP->data[1] = USBF2; } break; } if (msg != NULL) { uint8 low = T1CNTL; uint8 hi = T1CNTH; msg->time=BUILD_UINT16(low,hi); osal_msg_send(zusbTaskId, (uint8 *)msg); } /*uint8 __generic *pTemp = rxData; do { *(pTemp++) = USBF2; } while (--length);*/ } USBFW_ARM_OUT_ENDPOINT(); USBFW_SELECT_ENDPOINT(oldEndpoint); } }
/*********************************************************************************** * @fn usbOutProcess * * @brief Handle traffic flow from USB to RF. * * @param none * * @return none */ static void usbOutProcess(void) { uint8 length, /*nToSend,*/ packetlength=0; // If new packet is ready in USB FIFO halIntOff(); oldEndpoint = USBFW_GET_SELECTED_ENDPOINT(); USBFW_SELECT_ENDPOINT(4); if (USBFW_OUT_ENDPOINT_DISARMED() ) { // Get length of USB packet, this operation must not be interrupted. length = USBFW_GET_OUT_ENDPOINT_COUNT_LOW(); length+= USBFW_GET_OUT_ENDPOINT_COUNT_HIGH() >> 8; // Avoid overflow message_length = USB_MAX_MESSAGE_LENGTH; if (usb_bufferIndex + length > USB_MAX_MESSAGE_LENGTH) usb_bufferIndex=0; // Copy received bytes from FIFO to buffer usbfwReadFifo(&USBF4, length, &usb_buffer[usb_bufferIndex]); // Increase buffer index usb_bufferIndex += length; // If entire USB packet is read from buffer USBFW_SELECT_ENDPOINT(4); USBFW_ARM_OUT_ENDPOINT(); // get packet lenght from byte #2 of received packet if ((usb_bufferIndex >= 2) && (packetlength == 0)) packetlength = usb_buffer[2]; if ((usb_bufferIndex > packetlength-1) && (packetlength >= USB_MIN_MESSAGE_LENGTH)) { //extract data from packet usb_decode(); usb_bufferIndex = 0; } /* // Calculate number of bytes available in RF buffer; and the number // of bytes we may transfer in this operation. nToSend= MIN(BUF_SIZE - bufNumBytes(&rbRxBuf), length); // Space available in UART RX buffer ? if (nToSend>0) { // Read from USB FIFO usbfwReadFifo(&USBF4, nToSend, buffer); // Write to radio TX buffer bufPut(&rbRxBuf,buffer,nToSend); // If entire USB packet is read from buffer if (length == nToSend) { USBFW_SELECT_ENDPOINT(4); USBFW_ARM_OUT_ENDPOINT(); } }*/ } USBFW_SELECT_ENDPOINT(oldEndpoint); halIntOn(); }