# Bluetooth audio on embedded hardware

I would really like the bluetooth-alsa project to get far enough along to really shine on embedded platforms. gumstix is the perfect little device for working this out. This device currently ships with an Infineon Singlestone PBA31307.

### Status

- Gumstix uses a bluetooth 1.2 adapter now, the infineon pba 31307 (bt1.2 means better cooperation with wifi)
- Gumstix hardware now routes infineon PCM to xscale's NSSP i/o (PCM-clock goes to NSSP-clock, PCM-frame goes to NSSP-frame, and PCM-in/out map to the NSSPTXD/TXD)
- Unfortunately, Gumstix is talking to bluetooth adapter at a fixed 115k (switching to 921k according to infineon docs does not work properly)

### To-do

- Complete alsa kernel driver code to deal with audio in/out through NSSP as a "regular" audio device (Craig)

### Wishlist

- Negotiate 921kbps connection to bluetooth adapter, needed for a2dp and sco over hci
- Try sco over hci by forcing mtu values and investigating the adapter's ogf and ocf parameters (Brad)
- Modify a2play to build again and so it uses usleep() instead of the rtc (Brad)

### Bluegiga Class 1 bluecore4 module

Adding your own bluetooth module will allow things like full 921k data connection, SCO over HCI, and class 1 ranges. This adapter can do EDR, but the pxa255 UART cannot go beyond 921k unfortunately.

The model for this adapter is wt11-a-hci from semiconductorstore.com. Note that the data sheet referenced from semiconductorstore is old. Go directly to bluegiga for updated docs.

#### Wiring it up

Using GPIO48-51 as I'm suggesting here conflicts with the pcmcia pins, so plan to leave the GPIO settings alone and use HWUART through GPIO42-45 if you need pcmcia. You do this either on a module without bluetooth or by clearing GPIO7 and hoping the pins can all be used while the built-in module is held in reset.

The first thing to do after getting this module is to wire it up and try to reconfigure it from a PC so the module will use the uart/h4 interface.

Bluegiga | pxa | Description
--- | --- | ---
1 |  | gnd
2 |  | 3.3v regulated typical 200mA max 220mA
21 |  | 3.3v to disable spi? (I left this unconnected)
8 |  | usb d+ for programming
9 |  | usb d- for programming
5 rts | gpio50 hwcts | module uart rts->hwcts (asserted by module)
10 cts | gpio51 hwrts | module uart cts<-hwrts (asserted by gumstix)
26 tx | gpio49 hwrxd | module uart tx->hwrxd (asserted by module)
6 rx | gpio48 hwtxd | module uart rx<-hwtxd (asserted by gumstix)
17 reset | gpio16 "PW[M]0" | reset (active high)

#### Firmware settings

Making the adapter use its uart connection is done using the bccmd app from bluez utils cvs (configure with `--enable-bccmd` to get bccmd)

bccmd can "brick" your adapter if you make a mistake! To test things without risking this, connect both usb to a pc and the uart connected to the gumstix. (I power the gumstix from usb when doing this.) Then run

     bccmd psset -r 0x3cd 4

To set the adapter to use the uart and do a soft reset. I had to do this twice before it would actually disconnect from usb and reconnect on the uart.

Then attach to the adapter on the gumstix:

    # shut down internal bluetooth
    /etc/init.d/S30bluetooth stop

    # switch the built-in bluetooth to BTUART for now
    echo "AF1 in" >/proc/gpio/GPIO42
    echo "AF2 out" >/proc/gpio/GPIO43
    echo "AF1 in" >/proc/gpio/GPIO44
    echo "AF2 out" >/proc/gpio/GPIO45

    # make the "HWUART" labeled pads on the audiostix actually carry HWUART
    echo "AF1 out" >/proc/gpio/GPIO48
    echo "AF1 in" >/proc/gpio/GPIO49
    echo "AF1 in" >/proc/gpio/GPIO50
    echo "AF1 out" >/proc/gpio/GPIO51

    # bring it up
    echo "GPIO out set" >/proc/gpio/GPIO16
    sleep 1
    echo clear >/proc/gpio/GPIO16
    sleep 1

    # attach the adapter
    /usr/sbin/hciattach -s 115200 ttyS3 csr 921600

    # check for the adapter
    hciconfig

Once you are satisfied that works, cold boot everything and this time make it permanent from the pc:

     bccmd psset -s default 0x3cd 4

#### Bringing up CSR automatically

- Modify /etc/default/bluetooth

      	HCIATTACH_TYPE=csr
      	HCIATTACH_START_SPEED=115200

- modify S30bluetooth to set GPIO status before attempting an attach--only if NOT using pcmcia
  - We either need to move the bt module to BTUART (we could have two bt interfaces--hmm!)

        	echo "AF1 in" >/proc/gpio/GPIO42
        	echo "AF2 out" >/proc/gpio/GPIO43
        	echo "AF1 in" >/proc/gpio/GPIO44
        	echo "AF2 out" >/proc/gpio/GPIO45

  - Or... disable the bt module's GPIOs entirely (for better power efficiency, set 42 and 44 for output as long as we know they are left in hi-z by the module)

        	echo clear >/proc/gpio/GPIO7

        	echo "GPIO in" >/proc/gpio/GPIO42
        	echo "GPIO out" >/proc/gpio/GPIO43
        	echo "GPIO in" >/proc/gpio/GPIO44
        	echo "GPIO out" >/proc/gpio/GPIO45

        	echo "GPIO out" >/proc/gpio/GPIO12

  - set the appropriate GPIO pins to use the HWUART on GPIOs 48-51

                echo "AF1 out" >/proc/gpio/GPIO48
                echo "AF1 in" >/proc/gpio/GPIO49
        	echo "AF1 in" >/proc/gpio/GPIO50
        	echo "AF1 out" >/proc/gpio/GPIO51

- and finally, be sure the startup script resets the adapter before the hciattach

      	echo "GPIO out set" >/proc/gpio/GPIO16
      	sleep 1
      	echo clear >/proc/gpio/GPIO16
      	sleep 1

It's working (woohoo)

    # hciconfig hci0 revision
    hci0:   Type: UART
            BD Address: 00:07:80:81:BC:3E ACL MTU: 384:8 SCO MTU: 64:8
            Build 2810
            Chip version: BlueCore4-External
            Max key size: 56 bit
            SCO mapping:  HCI
    # hciconfig -a hci0
    hci0:   Type: UART
            BD Address: 00:07:80:81:BC:3E ACL MTU: 384:8 SCO MTU: 64:8
            UP RUNNING PSCAN ISCAN
            RX bytes:4577 acl:95 sco:0 events:289 errors:0
            TX bytes:2735 acl:95 sco:0 commands:80 errors:0
            Features: 0xff 0xff 0x8f 0xfe 0x9b 0xf9 0x00 0x80
            Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3
            Link policy: RSWITCH HOLD SNIFF PARK
            Link mode: SLAVE ACCEPT
            Name: 'Gumstix (0)'
            Class: 0x820116
            Service Classes: Networking
            Device Class: Computer, Palm
            HCI Ver: 2.0 (0x3) HCI Rev: 0xafa LMP Ver: 2.0 (0x3) LMP Subver: 0xafa
            Manufacturer: Cambridge Silicon Radio (10)

fwiw... S30bluetooth runs `echo AF1 out > /proc/gpio/GPIO12` on start but has no corresponding action in stop. This sends out a clock signal that the infineon needs apparently.

### Links

- Old porting effort
- bluetooth-alsa project