PIC32 Bluetooth® Stack Library Demonstrations
Total Page:16
File Type:pdf, Size:1020Kb
PIC32 Bluetooth® Stack Library Demonstrations MPLAB® Harmony Integrated Software Framework Copyright (c) 2013-2017 Microchip Technology Inc. All rights reserved. Bluetooth Demonstrations Bluetooth Demonstrations This section provides descriptions of the PIC32 Bluetooth Stack Library demonstrations. MPLAB Harmony is available for download from the Microchip website by visiting: http://www.microchip.com/mplabharmony. Once you are on the site, click the Downloads tab to access the appropriate download for your operating system. For additional information on this demonstration, refer to the “Applications Help” section in the MPLAB Harmony Help. © 2013-2017 Microchip Technology Inc. MPLAB Harmony v2.05.01 2 Bluetooth Demonstrations Introduction Introduction PIC32 Bluetooth Stack Library Demonstration Applications Help. Description This help file contains instructions and associated information about MPLAB Harmony Bluetooth demonstration applications, which are contained in the MPLAB Harmony Library distribution. There are two types of applications: those built using a Bluetooth Driver (currently there are only two, for the BM64), and those using the MPLAB Harmony PIC32 Bluetooth Stack Library (which is not written as a driver). The Bluetooth Stack Library, which is considered the "basic" Bluetooth Stack , includes basic demonstrations that are referred to as Data Demonstrations. This section describes the hardware requirement and procedures to run these Basic Bluetooth Stack firmware projects on Microchip demonstration and development boards. In addition to the Data Demonstrations provided with the PIC32 Bluetooth Basic Stack Library, additional Premium Demonstrations, which are available for purchase, demonstrate the capabilities of the Bluetooth Audio Stack Library. Information is provided in the Premium Demonstrations section on how to obtain, configure, and run these demonstrations. To learn more about MPLAB Harmony stacks and libraries refer to the related documentation in Volume V: MPLAB Harmony Framework Reference. © 2013-2017 Microchip Technology Inc. MPLAB Harmony v2.05.01 3 Bluetooth Demonstrations Demonstrations BM64 Driver Demonstrations Demonstrations This section provides information on the standard Data Demonstrations and demonstration applications built with the BM64 Driver, as well as the purchased Premium Demonstrations. BM64 Driver Demonstrations This section provides instructions about how to run the demonstration applications built using the BM64 Driver. BM64_a2dp_hfp This topic provides information on the supported demonstration boards, how to configure the hardware, and how to run the demonstration. Description In this demonstration application, the BM64 Module Daughter Board, installed on the PIC32 Bluetooth Audio Development Kit (BTADK), is used to stream A2DP audio from a Bluetooth host, such as a smartphone, to a pair of headphones connected to the Audio DAC Daughter Board which comes with the BTADK. The demonstration can also automatically answer a voice call coming in via Hands-Free Protocol (HFP), interrupting (and pausing) any A2DP streaming in progress. Local audio is input using a microphone connected to the MIC IN input of the BM64 Module Daughter Board. When the call is terminated, streaming resumes. Architecture This application runs on the PIC32 Bluetooth Audio Development Kit, which contains a PIC32MX470F512L microcontroller with 512 KB of Flash memory and 128 KB of RAM running at 96 MHz with the following features: • 220x176 color LCD • Six push buttons (SW1-SW6) • Five LEDs • USB Device and Host interfaces • Two X32 sockets, one for Bluetooth module and a second for a codec or DAC 1. The PIC32 Bluetooth Audio Development Kit comes with a BTM805 Bluetooth Daughter Board and an AK4384 Audio DAC Notes: daughter Board; however, you must replace the BTM805 daughter board with the daughter board for the BM64. 2. The USB Device and Host interfaces are not used in this application. Surrounding the PIC32MX470F512L microcontroller are a set of headers, which accept various plug-in modules (PIM), including one for a PIC32MX270F512L PIM as a second configuration for this project. The following figure illustrates the application architecture. The PIC32 microcontroller (MCU) runs the application code, and communicates with the BM64 over a USART interface operating at 115,200 baud. The BM64 module contains its own Bluetooth stack, so it is unnecessary to include a software Bluetooth stack in the PIC32 MCU. The program takes up about 40% of the PIC32MX470F512 microcontroller’s program space, and 27% of its RAM. The interface between the PIC32 MCU and the LCD is an 8-bit parallel master port (PMP). The program uses the MPLAB Harmony v2.0 Graphics Library to draw on the screen. The buttons are also interfaced using GPIO pins. As with any MPLAB Harmony application, the SYS_Initialize function, which is located in the system_init.c source file, makes calls to initialize various subsystems, such as the clock, ports, board support package (BSP), BM64, AK4384 DAC, PMP, timers, graphics, and interrupts. The BM64 and AK4384 drivers, graphics, timers, and the application state machines are all updated through calls located in the SYS_Tasks function in system_tasks.c file. Interrupt handlers in the system_interrupt.c file are used for the four DMA channels (only channels 0 and 1 are used), the USART, and the two timers. The application code is contained in two source files, app.c and audio.c. The former contains the application state machine (APP_Tasks). It first initializes the application, sets up a periodic timing callback, and then waits for the BM64 driver’s internal state machine to complete initialization (indicated by the function DRV_BT_GetPowerStatus returning RV_BT_STATUS_READY ). At that point the application calls a function called audioStart which initiates audio processing. Meanwhile, the audio state machine in audio.c has received a handle to the BM64 driver by calling DRV_BT_Open, and then setting callbacks for two event handlers (AUDIO_BT_RxBufferEventHandler for buffer event handling, and _audioEventHandler for general event handling). After that it gets a handle to the AK4384 driver by calling its DRV_CODEC_Open function with a mode of DRV_IO_INTENT_WRITE. Then it registers an event handler, AUDIO_CODEC_TxBufferEventHandler as a callback with the AK4384 driver. Then, it waits for the application to call audioStart before it makes its first call to DRV_BT_BufferAddRead to request audio samples from the BM64. The event handler callback is given control whenever a buffer has been processed. By default two sets of buffers are used (AUDIO_QUEUE_SIZE = 2), each with 1250 32-bit samples; however, simply by changing the queue size a circular buffer scheme can be used instead. As each buffer is © 2013-2017 Microchip Technology Inc. MPLAB Harmony v2.05.01 4 Bluetooth Demonstrations Demonstrations BM64 Driver Demonstrations handed off to thebe sent to the codec, the other one becomes available to be filled by the BM64. The BM64 and AK4384 drivers interfaces with their respective I2S interfaces via DMA, but this is largely transparent to the application, as it is all taken care of by MPLAB Harmony. Demonstration Features • Uses the BM64 Bluetooth Driver Library to read audio samples from the BM64 • Use of either a “ping-pong” or circular buffer scheme • At a lower level, the BM64 driver uses the USART Driver Library to talk to the BM64 module • Uses the AK4384 Codec Driver Library to write samples to the AK4384 DAC • At a lower level, using the I2S Driver Library between the BM64 and BM64 driver, and the AK4384 Codec Library and the AK4384 DAC • Use of “ping-pong” audio buffers • Sending and receiving characters between the USART of the PIC32 MCU and the BM64 (see USART Driver Library) • Displays graphics on the 220x176 LCD of the PIC32 Bluetooth Audio Development Kit using the MPLAB Harmony Graphics Library (see MPLAB Harmony Graphics Composer Suite) and the Parallel Master Port (see PMP Driver Library) • Processing of button pushes on the PIC32 Bluetooth Audio Development Kit using a state machine for contact debouncing (see Ports Peripheral Library) • Use of two timers: one as a periodic 1 ms timer for the application, and a second used by the BM64 driver (see Timer Driver Library) The following API calls are made to the BM64 Bluetooth driver: • DRV_BT_Status – get a handle to an opened driver instance • DRV_BT_Open – get a handle to an opened BM64 driver instance • DRV_BT_BufferEventHandlerSet – register a callback for a buffer-related event handler • DRV_BT_EventHandlerSet – register a callback for a general event handler • DRV_BT_BufferAddRead – read audio samples from the BM64 • DRV_BT_GetPowerStatus - • DRV_BT_GetLinkStatus - • DRV_BT_GetBDName – get the Bluetooth device name • DRV_BT_GetBDAddress – get the Bluetooth device address • DRV_BT_GetPlayingStatus – get the playing status (paying, paused, etc.) • DRV_BT_PlayPause – toggle the play/pause status of the host device • DRV_BT_FastForward – put the host device into fast forward • DRV_BT_Rewind – put the host device into rewind • DRV_BT_CancelForwardOrRewind – cancel a previous fast forward or rewind request • DRV_BT_PlayPreviousSong – play the previous song of a playlist • DRV_BT_PlayNextSong – play the next song in a playlist • DRV_BT_volumeUp – bump the volume up one increment • DRV_BT_volumeDown – bump the volume down one increment • DRV_BT_EnterBTPairingMode – enter Bluetooth pairing mode • DRV_BT_DisconnectAllLinks – disconnect all links