/* to this function. */ int BTPSAPI HCITR_COMWrite(unsigned int HCITransportID, unsigned int Length, unsigned char *Buffer) { int ret_val; int Count; /* Check to make sure that the specified Transport ID is valid and */ /* the output buffer appears to be valid as well. */ if((HCITransportID == TRANSPORT_ID) && (HCITransportOpen) && (Length) && (Buffer)) { /* Delay and poll until there is enough room in the Tx Buffer (in */ /* the UartContext structure) to hold the data we are trying to */ /* transmit. */ while(UartContext.TxBytesFree < Length) BTPS_Delay(10); /* Process all of the data. */ while(Length) { /* The data may have to be copied in 2 phases. Calculate the */ /* number of character that can be placed in the buffer before */ /* the buffer must be wrapped. */ Count = (UartContext.TxBufferSize-UartContext.TxInIndex); Count = (Count > Length)?Length:Count; BTPS_MemCopy(&(UartContext.TxBuffer[UartContext.TxInIndex]), Buffer, Count); /* Update the number of free bytes in the buffer. Since this */ /* count can also be updated in the interrupt routine, we will */ /* have have to update this with interrupts disabled. */ MAP_IntDisable(UartContext.IntBase); UartContext.TxBytesFree -= Count; MAP_IntEnable(UartContext.IntBase); /* Adjust the count and index values. */ Buffer += Count; Length -= Count; UartContext.TxInIndex += Count; if(UartContext.TxInIndex >= UartContext.TxBufferSize) UartContext.TxInIndex = 0; } /* Check to see if we need to prime the transmitter. */ if(!(HWREG(UartContext.Base + UART_O_IM) & UART_IM_TXIM)) { /* Now that the data is in the input buffer, check to see if we*/ /* need to enable the interrupt to start the TX Transfer. */ HWREG(UartContext.Base + UART_O_IM) |= UART_IM_TXIM; /* Start sending data to the Uart Transmit. */ MAP_IntDisable(UartContext.IntBase); TxInterrupt(); MAP_IntEnable(UartContext.IntBase); } ret_val = 0; } else ret_val = HCITR_ERROR_WRITING_TO_PORT; return(ret_val); }
/* for the data length and data buffer (respectively). */ void BTPSAPI HCITR_COMClose(unsigned int HCITransportID) { HCITR_COMDataCallback_t COMDataCallback; unsigned long CallbackParameter; /* Check to make sure that the specified Transport ID is valid. */ if((HCITransportID == TRANSPORT_ID) && (HCITransportOpen)) { /* Disable the UART Interrupts. */ MAP_UARTIntDisable(UartContext.Base, UART_INT_RX | UART_INT_RT); MAP_IntDisable(UartContext.IntBase); /* Place the Bluetooth Device in Reset. */ MAP_GPIOPinWrite(HCI_RESET_BASE, HCI_RESET_PIN, 0); /* Note the Callback information. */ COMDataCallback = _COMDataCallback; CallbackParameter = _COMCallbackParameter; /* Flag that the HCI Transport is no longer open. */ HCITransportOpen = 0; /* Flag that there is no callback information present. */ _COMDataCallback = NULL; _COMCallbackParameter = 0; /* Flag that the RxThread is deleted. */ Handle = NULL; /* Flag that the Rx Thread should delete itself. */ RxThreadDeleted = TRUE; BTPS_SetEvent(RxDataEvent); /* Flag that the RxThread is deleted. */ Handle = NULL; /* Delay while the RxThread exits. */ BTPS_Delay(5); /* All finished, perform the callback to let the upper layer know */ /* that this module will no longer issue data callbacks and is */ /* completely cleaned up. */ if(COMDataCallback) (*COMDataCallback)(HCITransportID, 0, NULL, CallbackParameter); /* Close the RxData event. */ BTPS_CloseEvent(RxDataEvent); } }
int main(void) { /* Turn off the watchdog timer */ WDTCTL = WDTPW | WDTHOLD; /* Configure the hardware for its intended use. */ HAL_ConfigureHardware(); /* Enable interrupts and call the main application thread. */ __enable_interrupt(); MainThread(); /* MainThread should run continously, if it exits an error occured. */ while (1) { HAL_LedToggle(0); BTPS_Delay(100); } }
/* via the HCI_COMClose() function. */ void BTPSAPI HCITR_COMReconfigure(unsigned int HCITransportID, HCI_Driver_Reconfigure_Data_t *DriverReconfigureData) { unsigned long BaudRate; HCI_Driver_Reconfigure_Data_t DisableRxTxData; /* Check to make sure that the specified Transport ID is valid. */ if((HCITransportID == TRANSPORT_ID) && (HCITransportOpen) && (DriverReconfigureData) && (DriverReconfigureData->ReconfigureCommand == HCI_COMM_DRIVER_RECONFIGURE_DATA_COMMAND_CHANGE_PARAMETERS)) { /* Disable Transmit and Receive while we change the baud rate. */ DisableRxTxData.ReconfigureCommand = HCI_COMM_DRIVER_DISABLE_UART_TX_RX; DisableRxTxData.ReconfigureData = NULL; HCITR_COMReconfigure(0, &DisableRxTxData); /* Configure the requested baud rate. */ BaudRate = *((unsigned long *)DriverReconfigureData->ReconfigureData); UARTIntDisableReceive(UartContext.UartBase); HAL_CommConfigure(UartContext.UartBase, BaudRate, (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE)); UARTIntEnableReceive(UartContext.UartBase); /* Small delay to let the COM Port reconfigure itself. */ BTPS_Delay(1); /* Re-enable Transmit and Receive operation. */ DisableRxTxData.ReconfigureCommand = HCI_COMM_DRIVER_DISABLE_UART_TX_RX; DisableRxTxData.ReconfigureData = (void *)DWORD_SIZE; HCITR_COMReconfigure(0, &DisableRxTxData); } /* Check to see if there is a global reconfigure parameter. */ if((DriverReconfigureData) && (!HCITransportID) && (HCITransportOpen)) { /* Check to see if we are being instructed to disable RX/TX. */ if(DriverReconfigureData->ReconfigureCommand == HCI_COMM_DRIVER_DISABLE_UART_TX_RX) { if(DriverReconfigureData->ReconfigureData) { /* Flow on. */ DISABLE_INTERRUPTS(); /* Clear the RTS High Flag. */ UartContext.Flags &= ~UART_CONTEXT_FLAG_RTS_HIGH; /* Re-enable the Transmitter. */ EnableTransmitter(); ENABLE_INTERRUPTS(); /* Flow the Bluetooth chip on if necessary. */ FLOW_ON(); } else { /* We are being asked to flow off all Bluetooth UART */ /* Transactions. */ DISABLE_INTERRUPTS(); /* Disable the Receiver. */ FLOW_OFF(); /* Flag that the RTS Line should stay high. */ UartContext.Flags |= UART_CONTEXT_FLAG_RTS_HIGH; /* Disable the Transmit Operation. */ DisableTransmitter(); ENABLE_INTERRUPTS(); /* Wait until we have finished transmitting any bytes in the*/ /* UART Transmit Buffer. */ while(UART_TRANSMIT_ACTIVE()) ; } } } }
/* negative return value to signify an error. */ int BTPSAPI HCITR_COMOpen(HCI_COMMDriverInformation_t *COMMDriverInformation, HCITR_COMDataCallback_t COMDataCallback, unsigned long CallbackParameter) { int ret_val; volatile char dummy = 0; /* First, make sure that the port is not already open and make sure */ /* that valid COMM Driver Information was specified. */ if((!HCITransportOpen) && (COMMDriverInformation) && (COMDataCallback)) { /* Initialize the return value for success. */ ret_val = TRANSPORT_ID; /* Note the COM Callback information. */ _COMDataCallback = COMDataCallback; _COMCallbackParameter = CallbackParameter; /* Try to Open the port for Reading/Writing. */ BTPS_MemInitialize(&UartContext, 0, sizeof(UartContext_t)); UartContext.UartBase = BT_UART_MODULE_BASE; UartContext.ID = 1; UartContext.RxBufferSize = DEFAULT_INPUT_BUFFER_SIZE; UartContext.RxBytesFree = DEFAULT_INPUT_BUFFER_SIZE; UartContext.XOffLimit = XOFF_LIMIT; UartContext.XOnLimit = XON_LIMIT; UartContext.TxBufferSize = DEFAULT_OUTPUT_BUFFER_SIZE; UartContext.TxBytesFree = DEFAULT_OUTPUT_BUFFER_SIZE; UartContext.SuspendState = hssNormal; /* Check to see if this is the first time that the port has been */ /* opened. */ if(!HCITransportOpen) { /* Configure the Bluetooth Slow Clock. */ BT_CONFIG_SLOW_CLOCK(); /* Configure the TXD and RXD pins as UART peripheral pins. */ BT_CONFIG_UART_PINS(); /* configures the RTS and CTS lines. */ BT_CONFIG_RTS_PIN(); BT_CONFIG_CTS_PIN(); /* disable Flow through Local RTS line. */ FLOW_OFF(); /* configure the Device Reset line */ BT_CONFIG_RESET(); /* Set the Baud rate up. */ HAL_CommConfigure(UartContext.UartBase, BLUETOOTH_STARTUP_BAUD_RATE, (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE)); /* Disable Tx Flow, later we will check RTS and see if we */ /* should enable it, but enable our receive flow. */ DISABLE_INTERRUPTS(); UartContext.Flags &= (~UART_CONTEXT_FLAG_TX_FLOW_ENABLED); UartContext.Flags |= UART_CONTEXT_FLAG_FLOW_ENABLED; ENABLE_INTERRUPTS(); /* Bring the Bluetooth Device out of Reset. */ BT_DEVICE_RESET(); BTPS_Delay(10); BT_DEVICE_UNRESET(); /* Bring CTS Line Low to Indicate that we are ready to receive.*/ FLOW_ON(); /* Check to see if we need to enable Tx Flow. */ if(BT_CTS_READ()) { /* CTS is High so we cannot send data at this time. We will */ /* configure the CTS Interrupt to be Negative Edge Active. */ DISABLE_INTERRUPTS(); UartContext.Flags &= (~UART_CONTEXT_FLAG_TX_FLOW_ENABLED); BT_CTS_INT_NEG_EDGE(); ENABLE_INTERRUPTS(); } else { /* CTS is low and ergo we may send data to the controller. */ /* The CTS interrupt will be set to fire on the Positive */ /* Edge. */ DISABLE_INTERRUPTS(); UartContext.Flags |= (UART_CONTEXT_FLAG_TX_FLOW_ENABLED); BT_CTS_INT_POS_EDGE(); ENABLE_INTERRUPTS(); } /* Clear any data that is in the Buffer. */ FlushRxFIFO(UartContext.UartBase); /* Enable Receive interrupt. */ UARTIntEnableReceive(UartContext.UartBase); /* Disable Transmit Interrupt. */ UARTIntDisableTransmit(UartContext.UartBase); DISABLE_INTERRUPTS(); /* Flag that the UART Tx Buffer will need to be primed. */ UartContext.Flags &= (~UART_CONTEXT_FLAG_TX_PRIMED); /* Enable the transmit functionality. */ UartContext.Flags |= UART_CONTEXT_FLAG_TRANSMIT_ENABLED; ENABLE_INTERRUPTS(); /* Check to see if we need to delay after opening the COM Port.*/ if(COMMDriverInformation->InitializationDelay) BTPS_Delay(COMMDriverInformation->InitializationDelay); /* Flag that the HCI Transport is open. */ HCITransportOpen = 1; } } else ret_val = HCITR_ERROR_UNABLE_TO_OPEN_TRANSPORT; return(ret_val); }
/* success. */ int BTPSAPI HCITR_COMOpen(HCI_COMMDriverInformation_t *COMMDriverInformation, HCITR_COMDataCallback_t COMDataCallback, unsigned long CallbackParameter) { int ret_val; /* First, make sure that the port is not already open and make sure */ /* that valid COMM Driver Information was specified. */ if((!HCITransportOpen) && (COMMDriverInformation) && (COMDataCallback)) { /* Initialize the return value for success. */ ret_val = TRANSPORT_ID; /* Note the COM Callback information. */ _COMDataCallback = COMDataCallback; _COMCallbackParameter = CallbackParameter; /* Initialize the UART Context Structure. */ BTPS_MemInitialize(&UartContext, 0, sizeof(UartContext_t)); UartContext.Base = HCI_UART_BASE; UartContext.IntBase = HCI_UART_INT; UartContext.ID = 1; UartContext.FlowInfo = UART_CONTEXT_FLAG_FLOW_CONTROL_ENABLED; UartContext.XOnLimit = DEFAULT_XON_LIMIT; UartContext.XOffLimit = DEFAULT_XOFF_LIMIT; UartContext.RxBufferSize = DEFAULT_INPUT_BUFFER_SIZE; UartContext.RxBytesFree = DEFAULT_INPUT_BUFFER_SIZE; UartContext.TxBufferSize = DEFAULT_OUTPUT_BUFFER_SIZE; UartContext.TxBytesFree = DEFAULT_OUTPUT_BUFFER_SIZE; /* Flag that the Rx Thread should not delete itself. */ RxThreadDeleted = FALSE; /* Check to see if this is the first time that the port has been */ /* opened. */ if(!Handle) { /* Configure the UART module and the GPIO pins used by the */ /* UART. */ MAP_SysCtlPeripheralEnable(HCI_UART_GPIO_PERIPH); MAP_SysCtlPeripheralEnable(HCI_UART_RTS_GPIO_PERIPH); MAP_SysCtlPeripheralEnable(HCI_UART_CTS_GPIO_PERIPH); MAP_SysCtlPeripheralEnable(HCI_UART_PERIPH); MAP_GPIOPinConfigure(HCI_PIN_CONFIGURE_UART_RX); MAP_GPIOPinConfigure(HCI_PIN_CONFIGURE_UART_TX); MAP_GPIOPinConfigure(HCI_PIN_CONFIGURE_UART_RTS); MAP_GPIOPinConfigure(HCI_PIN_CONFIGURE_UART_CTS); MAP_GPIOPinTypeUART(HCI_UART_GPIO_BASE, HCI_UART_PIN_RX | HCI_UART_PIN_TX); MAP_GPIOPinTypeUART(HCI_UART_RTS_GPIO_BASE, HCI_UART_PIN_RTS); MAP_GPIOPinTypeUART(HCI_UART_CTS_GPIO_BASE, HCI_UART_PIN_CTS); UARTFlowControlSet(UartContext.Base, UART_FLOWCONTROL_RX | UART_FLOWCONTROL_TX); /* Create an Event that will be used to signal that data has */ /* arrived. */ RxDataEvent = BTPS_CreateEvent(FALSE); if(RxDataEvent) { /* Create a thread that will process the received data. */ Handle = BTPS_CreateThread(RxThread, 1600, NULL); if(!Handle) { BTPS_CloseEvent(RxDataEvent); ret_val = HCITR_ERROR_UNABLE_TO_OPEN_TRANSPORT; } } else ret_val = HCITR_ERROR_UNABLE_TO_OPEN_TRANSPORT; } /* If there was no error, then continue to setup the port. */ if(ret_val != HCITR_ERROR_UNABLE_TO_OPEN_TRANSPORT) { /* Configure UART Baud Rate and Interrupts. */ MAP_UARTConfigSetExpClk(UartContext.Base, MAP_SysCtlClockGet(), COMMDriverInformation->BaudRate, (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE)); /* SafeRTOS requires RTOS-aware int handlers to be priority */ /* value 5 or greater */ MAP_IntPrioritySet(UartContext.IntBase, 6 << 5); MAP_IntEnable(UartContext.IntBase); MAP_UARTIntEnable(UartContext.Base, UART_INT_RX | UART_INT_RT); UartContext.Flags |= UART_CONTEXT_FLAG_RX_FLOW_ENABLED; /* Clear any data that is in the Buffer. */ FlushRxFIFO(UartContext.Base); /* Bring the Bluetooth Device out of Reset. */ MAP_GPIOPinWrite(HCI_RESET_BASE, HCI_RESET_PIN, HCI_RESET_PIN); /* Check to see if we need to delay after opening the COM Port.*/ if(COMMDriverInformation->InitializationDelay) BTPS_Delay(COMMDriverInformation->InitializationDelay); /* Flag that the HCI Transport is open. */ HCITransportOpen = 1; } } else ret_val = HCITR_ERROR_UNABLE_TO_OPEN_TRANSPORT; return(ret_val); }
//***************************************************************************** // // The following is the Main Application Thread. It will Initialize the // Bluetooth Stack and all used profiles. // //***************************************************************************** static void MainApp(void *pThreadParameter) { int iPressCount; int iTick; int iVolume; int iRetVal; tDeviceInfo sDeviceInfo; BTPS_Initialization_t sBTPSInitialization; // // Set the callback function the stack can use for printing to the // console. // #ifdef DEBUG_ENABLED sBTPSInitialization.MessageOutputCallback = MessageOutputCallback; #else sBTPSInitialization.MessageOutputCallback = NULL; #endif // // Initialize the Bluetooth stack, using no callback parameters (NULL). // iRetVal = InitializeBluetooth(BluetoothCallbackFunction, NULL, &sBTPSInitialization); // // Initialize the Graphics Module. // InitializeGraphics(ButtonPressCallback); // // Proceed with application if there was no Bluetooth init error. // if(!iRetVal) { // // Make the device Connectable and Discoverable and enabled Secured // Simple Pairing. // SetLocalDeviceMode(CONNECTABLE_MODE | DISCOVERABLE_MODE | PAIRABLE_SSP_MODE); // // Get information about our local device. // iRetVal = GetLocalDeviceInformation(&sDeviceInfo); if(!iRetVal) { // // Format the board address into a string, and display it on // the console. // BD_ADDRToStr(sDeviceInfo.ucBDAddr, g_cBoardAddress); Display(("Local BD_ADDR: %s\r\n", g_cBoardAddress)); // // Display additional info about the device to the console // Display(("HCI Version : %s\r\n", g_pcHCIVersionStrings[sDeviceInfo.ucHCIVersion])); Display(("Connectable : %s\r\n", ((sDeviceInfo.sMode & CONNECTABLE_MODE) ? "Yes" : "No"))); Display(("Discoverable : %s\r\n", ((sDeviceInfo.sMode & DISCOVERABLE_MODE) ? "Yes" : "No"))); if(sDeviceInfo.sMode & (PAIRABLE_NON_SSP_MODE | PAIRABLE_SSP_MODE)) { Display(("Pairable : Yes\r\n")); Display(("SSP Enabled : %s\r\n", ((sDeviceInfo.sMode & PAIRABLE_SSP_MODE) ? "Yes" : "No"))); } else { Display(("Pairable : No\r\n")); } // // Show message to user on the screen // UpdateStatusBox("Waiting for Connection..."); // // Bluetooth should be running now. Enter a forever loop to run // the user interface on the board screen display and process // button presses. // iTick = ONE_SEC_COUNT; iVolume = DEFAULT_POWERUP_VOLUME; iPressCount = 0; while(1) { // // Update the screen. // ProcessGraphics(); // // Wait 1/10 second. // BTPS_Delay(TENTH_SEC_COUNT); // // If one second has elapsed, toggle the LED // if(!(--iTick)) { iTick = ONE_SEC_COUNT; ToggleLED(LED_PIN); } // // Check to see if the User Switch was pressed. // if(UserSwitchPressed()) { // // Count the amount of time that the button has been // pressed. // iPressCount++; } // // Else the user switch is not pressed // else { // // If the button was just released, then adjust the volume. // Decrease the volume by 10% for each button press. At // zero, then reset it to 100%. // if(iPressCount) { iVolume = (iVolume == 0) ? 100 : iVolume - 10; // // Set the new volume, and display a message on the // console // SoundVolumeSet(iVolume); Display(("Press Count %d Volume %d\r\n", iPressCount, iVolume)); iPressCount = 0; } } } } } // // There was an error initializing Bluetooth // else { // // Print an error message to the console and show a message on // the screen // Display(("Bluetooth Failed to initialize: Error %d\r\n", iRetVal)); UpdateStatusBox("Failed to Initialize Bluetooth."); // // Enter a forever loop. Continue to update the screen, and rapidly // blink the LED as an indication of the error state. // while(1) { ProcessGraphics(); BTPS_Delay(500); ToggleLED(LED_PIN); } } }