/* * ======== Adaptor_getFreePacket ======== * Function called by a service to "allocate" a msg packet. */ UIAPacket_Hdr *Adaptor_getFreePacket(UIAPacket_HdrType type, UInt timeout) { Adaptor_Entry *entry; /* Get the free packet off the correct queue */ if (type == UIAPacket_HdrType_EventPkt) { Semaphore_pend(Adaptor_module->freeEventSem, timeout); entry = Queue_get(Adaptor_module->freeEventQ); if ((Queue_Elem *)entry == (Queue_Elem *)Adaptor_module->freeEventQ) { return (NULL); } } else { Semaphore_pend(Adaptor_module->freeMsgSem, timeout); entry = Queue_get(Adaptor_module->freeMsgQ); if ((Queue_Elem *)entry == (Queue_Elem *)Adaptor_module->freeMsgQ) { return (NULL); } } /* * Make sure the HdrType is set. This allows this module to * place the packet back on the correct free queue when done processing. */ UIAPacket_setHdrType(&(entry->packet), type); return (&(entry->packet)); }
/** * @brief USART1's interrupt handler * @param None * @retval None */ void USART1_IRQHandler() { if (USART_GetITStatus(USART1, USART_IT_RXNE)) { uint8_t data = USART_ReceiveData(USART1); USART_ClearITPendingBit(USART1, USART_IT_RXNE); // Clear interrupt flag Queue_put(&rxbuf, data); if( registered_rx_handler ) registered_rx_handler(data); } if (USART_GetITStatus(USART1, USART_IT_TXE)) { USART_ClearITPendingBit(USART1, USART_IT_TXE); // Clear interrupt flag uint16_t data = Queue_get(&txbuf); if (data) { USART_SendData(USART1, data & 0xFF); } else { USART_ITConfig(USART1, USART_IT_TXE, DISABLE); txrunning = 0; } } }
/********************************************************************* * @fn Util_dequeueMsg * * @brief Dequeues the message from the RTOS queue. * * @param msgQueue - queue handle. * * @return pointer to dequeued message, NULL otherwise. */ uint8_t *Util_dequeueMsg(Queue_Handle msgQueue) { queueRec_t *pRec = Queue_get(msgQueue); if (pRec != (queueRec_t *)msgQueue) { uint8_t *pData = pRec->pData; // Free the queue node // Note: this does not free space allocated by data within the node. #ifdef USE_ICALL ICall_free(pRec); #else free(pRec); #endif return pData; } return NULL; }
/** * @brief Retrieve received char * @note non-blocking call, would return 0 if nothing received * @retval non-zero with 8 lower bits being the char, zero if queue was empty */ uint16_t usart_getc() { return Queue_get(&rxbuf); }
/* * ======== Adaptor_transferAgentTaskFxn ======== */ Void Adaptor_transferAgentTaskFxn(UArg arg, UArg unused) { Adaptor_Entry *entry; Bool status; Bits32 mask; if (ServiceMgr_transportFxns.startFxn != NULL) { Adaptor_module->transportEventHandle = ServiceMgr_transportFxns.startFxn(UIAPacket_HdrType_EventPkt); } /* Run in a loop. */ while (TRUE) { /* Block on * - the clock: request events to be sent. * - incoming message: call Adaptor_sendToService * - outgoing message: call Adaptor_sendToHost * - energy request: call Adaptor_giveEnergy */ mask = Event_pend(Adaptor_module->event, Event_Id_NONE, Event_Id_00 | Event_Id_01 | Event_Id_02 | Event_Id_03, BIOS_WAIT_FOREVER); /* If the Clock expired, gather data */ if (mask & Event_Id_00) { Adaptor_runScheduledServices(); } /* A msg has been sent from the host, handle all of them */ if (mask & Event_Id_01) { entry = (Adaptor_Entry *)Queue_get(Adaptor_module->incomingQ); while ((Queue_Elem *)entry != (Queue_Elem *)Adaptor_module->incomingQ) { Adaptor_sendToService(entry); /* Get next one */ entry = (Adaptor_Entry *)Queue_get(Adaptor_module->incomingQ); } } /* Service requested energy */ if (mask & Event_Id_02) { Adaptor_giveEnergy(); } /* A msg need to be sent to the host, handle all of them */ if (mask & Event_Id_03) { entry = (Adaptor_Entry *)Queue_get(Adaptor_module->outgoingQ); while ((Queue_Elem *)entry != (Queue_Elem *)Adaptor_module->outgoingQ) { /* Send message to host */ status = Adaptor_sendToHost((UIAPacket_Hdr *)&(entry->packet)); if (status == FALSE) { Adaptor_freePacket((UIAPacket_Hdr *)&(entry->packet)); } /* Get next one */ entry = (Adaptor_Entry *)Queue_get(Adaptor_module->outgoingQ); } } } }