SYS_MODULE_OBJ DRV_USART_Initialize ( const SYS_MODULE_INDEX drvIndex, const SYS_MODULE_INIT * const init ) { DRV_USART_OBJ *dObj = (DRV_USART_OBJ*)NULL; DRV_USART_INIT *usartInit = NULL ; /* Check if the specified driver index is in valid range */ if(drvIndex >= DRV_USART_INSTANCES_NUMBER) { SYS_DEBUG(0, "Invalid driver index"); return SYS_MODULE_OBJ_INVALID; } /* Check if this hardware instance was already initialized */ if(gDrvUSARTObj[drvIndex].inUse != false) { SYS_DEBUG(0, "Instance already in use"); return SYS_MODULE_OBJ_INVALID; } /* Assign to the local pointer the init data passed */ usartInit = ( DRV_USART_INIT * ) init ; /* Disable the USART module */ PLIB_USART_Disable (usartInit->usartID) ; /* Allocate the driver object and set the operation flag to be in use */ dObj = &gDrvUSARTObj[drvIndex]; dObj->inUse = true; /* Update the USART PLIB Id and other parameters. */ dObj->nClients = 0; dObj->moduleId = usartInit->usartID; dObj->brgClock = usartInit->brgClock; dObj->isExclusive = false; dObj->queueSizeRead = usartInit->queueSizeReceive; dObj->queueSizeWrite = usartInit->queueSizeTransmit; dObj->dmaChannelRead = usartInit->dmaReceive; dObj->dmaChannelWrite = usartInit->dmaTransmit; dObj->txInterruptSource = usartInit->interruptTransmit; dObj->rxInterruptSource = usartInit->interruptReceive; dObj->errorInterruptSource = usartInit->interruptError; dObj->dmaInterruptTransmit = usartInit->dmaInterruptTransmit; dObj->dmaInterruptReceive = usartInit->dmaInterruptReceive; dObj->interruptNestingCount = 0; dObj->queueSizeCurrentRead = 0; dObj->queueSizeCurrentWrite = 0; /* Setup the Hardware */ _DRV_USART_HardwareSetup(usartInit->usartID, usartInit ) ; /* Clear the interrupts */ SYS_INT_SourceStatusClear(dObj->txInterruptSource); SYS_INT_SourceStatusClear(dObj->rxInterruptSource); SYS_INT_SourceStatusClear(dObj->errorInterruptSource); /* Enable the interrupt source in case of interrupt mode */ _DRV_USART_InterruptSourceEnable(dObj->errorInterruptSource); /* TODO: Enable DMA interrupts if the DMA channel is selected */ /* Create the hardware instance mutex. */ OSAL_ASSERT((OSAL_MUTEX_Create(&(dObj->mutexDriverInstance)) == OSAL_RESULT_TRUE), "Unable to create hardware instance mutex"); /* Check if the global mutexes have been created. If not then create these. */ if(!gDrvUSARTCommonDataObj.membersAreInitialized) { /* This means that mutexes where not created. Create them. */ OSAL_ASSERT((OSAL_MUTEX_Create(&(gDrvUSARTCommonDataObj.mutexClientObjects)) == OSAL_RESULT_TRUE), "Unable to create client instance mutex"); OSAL_ASSERT((OSAL_MUTEX_Create(&(gDrvUSARTCommonDataObj.mutexBufferQueueObjects)) == OSAL_RESULT_TRUE), "Unable to create buffer queue objects mutex"); /* Set this flag so that global mutexes get allocated only once */ gDrvUSARTCommonDataObj.membersAreInitialized = true; } /* Enable the USART module */ PLIB_USART_Enable(usartInit->usartID) ; /* Update the status */ dObj->status = SYS_STATUS_READY; /* Return the object structure */ return ( (SYS_MODULE_OBJ)drvIndex ); }
int hal_uart_dma_set_baud(uint32_t baud){ PLIB_USART_Disable(BT_USART_ID); PLIB_USART_BaudRateSet(BT_USART_ID, SYS_CLK_PeripheralFrequencyGet(CLK_BUS_PERIPHERAL_1), baud); PLIB_USART_Enable(BT_USART_ID); return 0; }
void DRV_USART_Deinitialize(SYS_MODULE_OBJ object) { DRV_USART_OBJ * dObj; DRV_USART_BUFFER_OBJ * iterator; bool status; /* Check that the object is valid */ if(object == SYS_MODULE_OBJ_INVALID) { SYS_DEBUG(0, "Invalid system object handle" ); return; } if(object >= DRV_USART_INSTANCES_NUMBER) { SYS_DEBUG(0, "Invalid system object handle" ); return; } dObj = (DRV_USART_OBJ*) &gDrvUSARTObj[object]; if(!dObj->inUse) { SYS_DEBUG(0, "Invalid system object handle"); return; } /* The driver will not have clients when it is being de-initialized. So the order in which we do the following steps is not that important */ /* Indicate that this object is not is use */ dObj->inUse = false; /* Deinitialize the USART status */ dObj->status = SYS_STATUS_UNINITIALIZED ; /* Disable the interrupt */ status = _DRV_USART_InterruptSourceDisable(dObj->txInterruptSource) ; status = _DRV_USART_InterruptSourceDisable(dObj->rxInterruptSource) ; status = _DRV_USART_InterruptSourceDisable(dObj->errorInterruptSource); /* Disable USART module */ PLIB_USART_Disable (dObj->moduleId); /* Deallocate all mutexes */ OSAL_ASSERT( (OSAL_MUTEX_Delete(&(dObj->mutexDriverInstance)) == OSAL_RESULT_TRUE), "Unable to delete client handle mutex" ); /* TODO: Disable all DMA interrupts */ /* Remove all objects from the read and write queue */ iterator = dObj->queueWrite; while(iterator != NULL) { /* Return the buffer object to the pool */ iterator->inUse = false; iterator = iterator->next; } iterator = dObj->queueRead; while(iterator != NULL) { /* Return the buffer object to the pool */ iterator->inUse = false; iterator = iterator->next; } }