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_1, SYS_CLK_PeripheralFrequencyGet(CLK_BUS_PERIPHERAL_1), 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_ReceiverEnable(USART_ID_1); PLIB_USART_ReceiverInterruptModeSelect(USART_ID_1, USART_RECEIVE_FIFO_ONE_CHAR); /* Initialize interrupts */ 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(USART_ID_1); }
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); }
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); }