SYS_MODULE_OBJ STIMULUS_USART_DRV_Initialize(const SYS_MODULE_INDEX index, const SYS_MODULE_INIT * const init ) { /* Get the USART COnfig Data */ configData = (DRV_USART_CONFIG_INIT *)init; /* Initialize USART */ PLIB_USART_BaudRateSet(configData->usartId, SYS_CLK_PeripheralFrequencyGet(CLK_BUS_PERIPHERAL_1), configData->baudRate); PLIB_USART_HandshakeModeSelect(configData->usartId, USART_HANDSHAKE_MODE_FLOW_CONTROL); PLIB_USART_OperationModeSelect(configData->usartId, USART_ENABLE_TX_RX_USED); PLIB_USART_LineControlModeSelect(configData->usartId, configData->lineControl); PLIB_USART_TransmitterEnable(configData->usartId); PLIB_USART_TransmitterInterruptModeSelect(configData->usartId, USART_TRANSMIT_FIFO_NOT_FULL); PLIB_USART_ReceiverEnable(configData->usartId); PLIB_USART_ReceiverInterruptModeSelect(configData->usartId, configData->rxIntMode); /* Initialize interrupts */ /* Note: TX interrupt must be enabled when data is ready to be transmitted */ /* The following code can be used to enable TX Interrupt */ /* PLIB_INT_SourceEnable(INT_ID_0, INT_SOURCE_USART_1_TRANSMIT); */ PLIB_INT_SourceEnable(INT_ID_0, INT_SOURCE_USART_1_RECEIVE); PLIB_INT_SourceEnable(INT_ID_0, INT_SOURCE_USART_1_ERROR); PLIB_INT_VectorPrioritySet(INT_ID_0, INT_VECTOR_UART1, INT_PRIORITY_LEVEL1); PLIB_INT_VectorSubPrioritySet(INT_ID_0, INT_VECTOR_UART1, INT_SUBPRIORITY_LEVEL0); PLIB_USART_Enable(configData->usartId); }
void DRV_USART0_Initialize(void) { /* Initialize USART */ PLIB_USART_BaudRateSet(USART_ID_1, SYS_CLK_PeripheralFrequencyGet(CLK_BUS_PERIPHERAL_2), 9600); PLIB_USART_HandshakeModeSelect(USART_ID_1, USART_HANDSHAKE_MODE_FLOW_CONTROL); PLIB_USART_OperationModeSelect(USART_ID_1, USART_ENABLE_TX_RX_USED); PLIB_USART_LineControlModeSelect(USART_ID_1, USART_8N1); PLIB_USART_TransmitterEnable(USART_ID_1); PLIB_USART_TransmitterInterruptModeSelect(USART_ID_1, USART_TRANSMIT_FIFO_NOT_FULL); PLIB_USART_ReceiverEnable(USART_ID_1); PLIB_USART_ReceiverInterruptModeSelect(USART_ID_1, USART_RECEIVE_FIFO_ONE_CHAR); /* Initialize TX interrupt */ PLIB_INT_SourceEnable(INT_ID_0, INT_SOURCE_USART_1_TRANSMIT); PLIB_INT_VectorPrioritySet(INT_ID_0, INT_VECTOR_UART1_TX, INT_PRIORITY_LEVEL1); PLIB_INT_VectorSubPrioritySet(INT_ID_0, INT_VECTOR_UART1_TX, INT_SUBPRIORITY_LEVEL0); /* Initialize RX interrupt */ PLIB_INT_SourceEnable(INT_ID_0, INT_SOURCE_USART_1_RECEIVE); PLIB_INT_VectorPrioritySet(INT_ID_0, INT_VECTOR_UART1_RX, INT_PRIORITY_LEVEL1); PLIB_INT_VectorSubPrioritySet(INT_ID_0, INT_VECTOR_UART1_RX, INT_SUBPRIORITY_LEVEL0); /* Initialize Fault interrupt */ PLIB_INT_SourceEnable(INT_ID_0, INT_SOURCE_USART_1_ERROR); PLIB_INT_VectorPrioritySet(INT_ID_0, INT_VECTOR_UART1_FAULT, INT_PRIORITY_LEVEL1); PLIB_INT_VectorSubPrioritySet(INT_ID_0, INT_VECTOR_UART1_FAULT, INT_SUBPRIORITY_LEVEL0); PLIB_USART_Enable(USART_ID_1); }
void _DRV_USART_SetupLineControlModes_V1(USART_MODULE_ID index, DRV_USART_LINE_CONTROL_MODES lineControlMode, DRV_USART_HANDSHAKE_MODES handShakeMode) { PLIB_USART_LINECONTROL_MODE flowControl; if (handShakeMode == DRV_USART_HANDSHAKE_MODE_SIMPLEX) { flowControl = PLIB_USART_RTS_MODE_SIMPLEX; } else { flowControl = PLIB_USART_RTS_MODE_FLOWCONTROL; } /* Supporting 8NONE1 */ //if(lineControlMode & DRV_USART_LINE_CONTROL_MODE_8NONE1) { PLIB_USART_LineControlModeSelect (DRV_USART_MODULE_ID( index ), PLIB_USART_ONE_STOPBIT |PLIB_USART_8_DATABITS_NO_PARITY |PLIB_USART_RTS_MODE_FLOWCONTROL ); } /* if(lineControlMode & DRV_USART_LINE_CONTROL_MODE_9NONE1) { PLIB_USART_LineControlModeSelect ( DRV_USART_MODULE_ID( index ), PLIB_USART_ONE_STOPBIT | PLIB_USART_9_DATABITS | flowControl ); } if(lineControlMode & DRV_USART_LINE_CONTROL_MODE_8EVEN1) { PLIB_USART_LineControlModeSelect ( DRV_USART_MODULE_ID( index ), PLIB_USART_ONE_STOPBIT | PLIB_USART_8_DATABITS_EVEN_PARITY | flowControl ); } if(lineControlMode & DRV_USART_LINE_CONTROL_MODE_8EVEN2) { PLIB_USART_LineControlModeSelect ( DRV_USART_MODULE_ID( index ), PLIB_USART_TWO_STOPBIT | PLIB_USART_8_DATABITS_EVEN_PARITY | flowControl ); } if(lineControlMode & DRV_USART_LINE_CONTROL_MODE_8ODD1) { PLIB_USART_LineControlModeSelect ( DRV_USART_MODULE_ID( index ), PLIB_USART_ONE_STOPBIT | PLIB_USART_8_DATABITS_ODD_PARITY | flowControl ); } if(lineControlMode & DRV_USART_LINE_CONTROL_MODE_8ODD2) { PLIB_USART_LineControlModeSelect ( DRV_USART_MODULE_ID( index ), PLIB_USART_TWO_STOPBIT | PLIB_USART_8_DATABITS_ODD_PARITY | flowControl ); } */ }
void DRV_USART0_Initialize(void) { /* Initialize USART */ PLIB_USART_BaudRateSet(USART_ID_1, SYS_CLK_PeripheralFrequencyGet(CLK_BUS_PERIPHERAL_1), 115200); PLIB_USART_HandshakeModeSelect(USART_ID_1, USART_HANDSHAKE_MODE_SIMPLEX); PLIB_USART_OperationModeSelect(USART_ID_1, USART_ENABLE_TX_RX_USED); PLIB_USART_LineControlModeSelect(USART_ID_1, USART_8N1); PLIB_USART_TransmitterEnable(USART_ID_1); PLIB_USART_TransmitterInterruptModeSelect(USART_ID_1, USART_TRANSMIT_FIFO_EMPTY); PLIB_USART_Enable(USART_ID_1); }
void APP_Debug_Initialize(){ // PPS Output Mapping: PLIB_PORTS_RemapOutput(PORTS_ID_0, OUTPUT_FUNC_U1TX, OUTPUT_PIN_RPD3 ); /* Initialize USART */ PLIB_USART_BaudRateSet(APP_DEBUG_USART_ID, SYS_CLK_PeripheralFrequencyGet(CLK_BUS_PERIPHERAL_1), APP_DEBUG_USART_BAUD); PLIB_USART_HandshakeModeSelect(APP_DEBUG_USART_ID, USART_HANDSHAKE_MODE_FLOW_CONTROL); PLIB_USART_OperationModeSelect(APP_DEBUG_USART_ID, USART_ENABLE_TX_RX_USED); PLIB_USART_LineControlModeSelect(APP_DEBUG_USART_ID, USART_8N1); PLIB_USART_TransmitterEnable(APP_DEBUG_USART_ID); PLIB_USART_Enable(APP_DEBUG_USART_ID); }
void hal_uart_dma_init(void){ bytes_to_write = 0; bytes_to_read = 0; /* PPS Input Remapping */ PLIB_PORTS_RemapInput(PORTS_ID_0, INPUT_FUNC_U2RX, INPUT_PIN_RPF4 ); PLIB_PORTS_RemapInput(PORTS_ID_0, INPUT_FUNC_U2CTS, INPUT_PIN_RPB2 ); /* PPS Output Remapping */ PLIB_PORTS_RemapOutput(PORTS_ID_0, OUTPUT_FUNC_U2RTS, OUTPUT_PIN_RPG9 ); PLIB_PORTS_RemapOutput(PORTS_ID_0, OUTPUT_FUNC_U2TX, OUTPUT_PIN_RPF5 ); /* Initialize USART */ PLIB_USART_BaudRateSet(BT_USART_ID, SYS_CLK_PeripheralFrequencyGet(CLK_BUS_PERIPHERAL_1), BT_USART_BAUD); PLIB_USART_HandshakeModeSelect(BT_USART_ID, USART_HANDSHAKE_MODE_FLOW_CONTROL); PLIB_USART_OperationModeSelect(BT_USART_ID, USART_ENABLE_TX_RX_CTS_RTS_USED); PLIB_USART_LineControlModeSelect(BT_USART_ID, USART_8N1); PLIB_USART_TransmitterEnable(BT_USART_ID); // PLIB_USART_TransmitterInterruptModeSelect(bluetooth_uart_id, USART_TRANSMIT_FIFO_IDLE); PLIB_USART_ReceiverEnable(BT_USART_ID); // PLIB_USART_ReceiverInterruptModeSelect(bluetooth_uart_id, USART_RECEIVE_FIFO_ONE_CHAR); PLIB_USART_Enable(BT_USART_ID); // enable _RESET SYS_PORTS_PinDirectionSelect(PORTS_ID_0, SYS_PORTS_DIRECTION_OUTPUT, BT_RESET_PORT, BT_RESET_BIT); bluetooth_power_cycle(); // After reset, CTS is high and we need to wait until CTS is low again // HACK: CTS doesn't seem to work right now msleep(250); // HACK: CSR seems to do an auto-baud on the uart, which makes the first HCI RESET fail // 2 options: a) check for CTS going high within 10 ms, b) just send HCI RESET twice // const uint8_t hci_reset_cmd[] = {0x01, 0x03, 0x0c, 0x00}; // int pos = 0; // while(pos < sizeof(hci_reset_cmd)){ // if (PLIB_USART_TransmitterIsEmpty(BT_USART_ID)){ // PLIB_USART_TransmitterByteSend(BT_USART_ID, hci_reset_cmd[pos]); // pos++; // } // } // msleep(250); }
void DRV_USART0_Initialize(void) { /* Initialize USART */ PLIB_USART_BaudRateSet(USART_ID_2, SYS_CLK_PeripheralFrequencyGet(CLK_BUS_PERIPHERAL_1), 230400); PLIB_USART_HandshakeModeSelect(USART_ID_2, USART_HANDSHAKE_MODE_SIMPLEX); PLIB_USART_OperationModeSelect(USART_ID_2, USART_ENABLE_TX_RX_USED); PLIB_USART_LineControlModeSelect(USART_ID_2, USART_8N1); PLIB_USART_TransmitterEnable(USART_ID_2); PLIB_USART_TransmitterInterruptModeSelect(USART_ID_2, USART_TRANSMIT_FIFO_NOT_FULL); /* Initialize interrupts */ /* Note: TX interrupt must be enabled when data is ready to be transmitted */ /* The following code can be used to enable TX Interrupt */ /* PLIB_INT_SourceEnable(INT_ID_0, INT_SOURCE_USART_2_TRANSMIT); */ PLIB_INT_SourceEnable(INT_ID_0, INT_SOURCE_USART_2_ERROR); PLIB_INT_VectorPrioritySet(INT_ID_0, INT_VECTOR_UART2, INT_PRIORITY_LEVEL1); PLIB_INT_VectorSubPrioritySet(INT_ID_0, INT_VECTOR_UART2, INT_SUBPRIORITY_LEVEL0); PLIB_USART_Enable(USART_ID_2); }
DRV_USART_LINE_CONTROL_SET_RESULT DRV_USART_LineControlSet ( DRV_HANDLE handle, DRV_USART_LINE_CONTROL lineControlMode ) { DRV_USART_OBJ * hDriver; DRV_USART_CLIENT_OBJ * client; USART_MODULE_ID plibID; /* Validate the driver handle */ client = _DRV_USART_DriverHandleValidate(handle); if(client == NULL) { /* Driver handle is not valid. Return error */ SYS_DEBUG(0, "Invalid driver handle"); return DRV_USART_LINE_CONTROL_SET_ERROR; } hDriver = (DRV_USART_OBJ*)client->hDriver; plibID = hDriver->moduleId; if(OSAL_MUTEX_Lock(hDriver->mutexDriverInstance, OSAL_WAIT_FOREVER) == OSAL_RESULT_TRUE) { /* Set the Line Control Mode */ PLIB_USART_LineControlModeSelect(plibID, lineControlMode); } else { SYS_DEBUG(0, "Hardware Instance Mutex time out in DRV_USART_LineControlSet() function"); return DRV_USART_LINE_CONTROL_SET_ERROR; } /* Return success */ return(DRV_USART_LINE_CONTROL_SET_SUCCESS); }
void _DRV_USART_LineControlModeSetDyn ( const USART_MODULE_ID plibId, const DRV_USART_LINE_CONTROL_MODES lineControlMode, const DRV_USART_HANDSHAKE_MODES flowControl ) { if( lineControlMode & DRV_USART_LINE_CONTROL_MODE_8NONE1 ) { if(USART_ExistsLineControlMode_RXandTXCombined(_DRV_USART_PERIPHERAL_ID_GET( plibId ))) { PLIB_USART_LineControlModeSelect(_DRV_USART_PERIPHERAL_ID_GET( plibId ), USART_8N1 | _DRV_USART_FLOW_CONTROL(flowControl)); } } else if ( lineControlMode & DRV_USART_LINE_CONTROL_MODE_9NONE1 ) { _DRV_USART_LineControlModeSet9N1(plibId, _DRV_USART_FLOW_CONTROL(flowControl)); } else if ( lineControlMode & DRV_USART_LINE_CONTROL_MODE_8EVEN1 ) { _DRV_USART_LineControlModeSet8E1(plibId, _DRV_USART_FLOW_CONTROL(flowControl)); } else if ( lineControlMode & DRV_USART_LINE_CONTROL_MODE_8ODD1 ) { _DRV_USART_LineControlModeSet8O1(plibId, _DRV_USART_FLOW_CONTROL(flowControl)); } else if ( lineControlMode & DRV_USART_LINE_CONTROL_MODE_8EVEN2 ) { _DRV_USART_LineControlModeSet8E2(plibId, _DRV_USART_FLOW_CONTROL(flowControl)); } else if ( lineControlMode & DRV_USART_LINE_CONTROL_MODE_8ODD2 ) { _DRV_USART_LineControlModeSet8O2(plibId, _DRV_USART_FLOW_CONTROL(flowControl)); } else { SYS_ASSERT(false, "Invalid Line Control Mode"); } } /* _DRV_USART_LineControlModeSetDyn */
void _DRV_USART_HardwareSetup(USART_MODULE_ID plibID, DRV_USART_INIT * init) { bool addressedMode = false; bool loopbackMode = false; bool irdaMode = false; bool wakeFromSleep = false; bool stopInIdle = false; bool autoBaud = false; /* Default is to use UxTX and UxRX only */ USART_OPERATION_MODE handshakeLines = USART_ENABLE_TX_RX_USED; /* Check the operational mode and udpate the relevant flags */ switch(init->mode) { case DRV_USART_OPERATION_MODE_ADDRESSED: addressedMode = true; break; case DRV_USART_OPERATION_MODE_IRDA: irdaMode = true; handshakeLines = USART_ENABLE_TX_RX_BCLK_USED; break; case DRV_USART_OPERATION_MODE_LOOPBACK: loopbackMode = true; break; default: break; } /* Handshaking */ if(init->handshake != DRV_USART_HANDSHAKE_NONE) { /* Note that this mode is exclusive of DRV_USART_OPERATION_MODE_IRDA. In that if the driver must be set up to use DRV_USART_OPERATION_MODE_IRDA then handshake parameter of the init data structure must be DRV_USART_HANDSHAKE_NONE. Setting up the driver to use Simplex or flow control will over ride the requirements of the IRDA mode.*/ handshakeLines = USART_ENABLE_TX_RX_CTS_RTS_USED; /* Set the handshake mode to either simplex or flow control */ PLIB_USART_HandshakeModeSelect(plibID, handshakeLines); } /* Wake up the part from sleep on any activity on the RX Line */ wakeFromSleep = (init->flags & DRV_USART_INIT_FLAG_WAKE_ON_START); /* When the CPU enters IDLE mode, stop the USART */ stopInIdle = (init->flags & DRV_USART_INIT_FLAG_STOP_IN_IDLE); /* When the CPU enters IDLE mode, stop the USART */ autoBaud = (init->flags & DRV_USART_INIT_FLAG_AUTO_BAUD); /* Initialize the USART based on initialization data structure */ PLIB_USART_InitializeModeGeneral(plibID, autoBaud, loopbackMode, wakeFromSleep, irdaMode, stopInIdle); /* Set the line control mode */ PLIB_USART_LineControlModeSelect(plibID, init->lineControl); /* We set the receive interrupt mode to receive an interrupt whenever FIFO is not empty */ PLIB_USART_InitializeOperation(plibID, USART_RECEIVE_FIFO_ONE_CHAR, USART_TRANSMIT_FIFO_EMPTY, handshakeLines); /* Set the baud rate and enable the USART */ PLIB_USART_BaudSetAndEnable(plibID, init->brgClock, init->baud); }