add bluetooth driver

This commit is contained in:
bigmagic 2020-11-29 17:31:51 +08:00
parent bda0d303af
commit 466ca3bf9a
7 changed files with 467 additions and 15 deletions

View File

@ -66,7 +66,7 @@ CONFIG_RT_USING_DEVICE=y
# CONFIG_RT_USING_INTERRUPT_INFO is not set
CONFIG_RT_USING_CONSOLE=y
CONFIG_RT_CONSOLEBUF_SIZE=128
CONFIG_RT_CONSOLE_DEVICE_NAME="uart0"
CONFIG_RT_CONSOLE_DEVICE_NAME="uart1"
CONFIG_RT_VER_NUM=0x40003
# CONFIG_RT_USING_CPU_FFS is not set
CONFIG_ARCH_ARMV8=y
@ -147,7 +147,7 @@ CONFIG_RT_SYSTEM_WORKQUEUE_STACKSIZE=2048
CONFIG_RT_SYSTEM_WORKQUEUE_PRIORITY=23
CONFIG_RT_USING_SERIAL=y
CONFIG_RT_SERIAL_USING_DMA=y
CONFIG_RT_SERIAL_RB_BUFSZ=64
CONFIG_RT_SERIAL_RB_BUFSZ=512
# CONFIG_RT_USING_CAN is not set
# CONFIG_RT_USING_HWTIMER is not set
# CONFIG_RT_USING_CPUTIME is not set
@ -431,6 +431,7 @@ CONFIG_RT_LWIP_USING_PING=y
# CONFIG_PKG_USING_ADBD is not set
# CONFIG_PKG_USING_COREMARK is not set
# CONFIG_PKG_USING_DHRYSTONE is not set
# CONFIG_PKG_USING_MEMORYPERF is not set
# CONFIG_PKG_USING_NR_MICRO_SHELL is not set
# CONFIG_PKG_USING_CHINESE_FONT_LIBRARY is not set
# CONFIG_PKG_USING_LUNAR_CALENDAR is not set
@ -438,6 +439,7 @@ CONFIG_RT_LWIP_USING_PING=y
# CONFIG_PKG_USING_GPS_RMC is not set
# CONFIG_PKG_USING_URLENCODE is not set
# CONFIG_PKG_USING_UMCN is not set
# CONFIG_PKG_USING_LWRB2RTT is not set
#
# system packages
@ -469,6 +471,7 @@ CONFIG_RT_LWIP_USING_PING=y
# Micrium: Micrium software products porting for RT-Thread
#
# CONFIG_PKG_USING_UCOSIII_WRAPPER is not set
# CONFIG_PKG_USING_UCOSII_WRAPPER is not set
# CONFIG_PKG_USING_UC_CRC is not set
# CONFIG_PKG_USING_UC_CLK is not set
# CONFIG_PKG_USING_UC_COMMON is not set
@ -505,8 +508,6 @@ CONFIG_RT_LWIP_USING_PING=y
# CONFIG_PKG_USING_AD7746 is not set
# CONFIG_PKG_USING_PCA9685 is not set
# CONFIG_PKG_USING_I2C_TOOLS is not set
# CONFIG_PKG_USING_I2C_TOOLS_V100 is not set
# CONFIG_PKG_USING_I2C_TOOLS_LATEST_VERSION is not set
# CONFIG_PKG_USING_NRF24L01 is not set
# CONFIG_PKG_USING_TOUCH_DRIVERS is not set
# CONFIG_PKG_USING_MAX17048 is not set
@ -531,7 +532,6 @@ CONFIG_RT_LWIP_USING_PING=y
# CONFIG_PKG_USING_LY68L6400 is not set
# CONFIG_PKG_USING_DM9051 is not set
# CONFIG_PKG_USING_SSD1306 is not set
# CONFIG_PKG_USING_SSD1306_LATEST_VERSION is not set
# CONFIG_PKG_USING_QKEY is not set
#
@ -616,6 +616,7 @@ CONFIG_RT_LWIP_USING_PING=y
# CONFIG_PKG_USING_CFGM is not set
# CONFIG_PKG_USING_RT_CMSIS_DAP is not set
# CONFIG_PKG_USING_VIRTUAL_DEVICE is not set
# CONFIG_PKG_USING_SMODULE is not set
CONFIG_BCM2711_SOC=y
# CONFIG_BSP_SUPPORT_FPU is not set
@ -628,7 +629,7 @@ CONFIG_BCM2711_SOC=y
#
CONFIG_BSP_USING_UART=y
CONFIG_RT_USING_UART0=y
# CONFIG_RT_USING_UART1 is not set
CONFIG_RT_USING_UART1=y
CONFIG_RT_USING_UART3=y
# CONFIG_RT_USING_UART4 is not set
CONFIG_RT_USING_UART5=y
@ -651,6 +652,7 @@ CONFIG_BSP_USING_CORETIMER=y
# CONFIG_BSP_USING_SYSTIMER is not set
CONFIG_BSP_USING_WDT=y
CONFIG_BSP_USING_ETH=y
CONFIG_BSP_USING_BULETOOTH=y
# CONFIG_BSP_USING_RTC is not set
CONFIG_BSP_USING_SDIO=y
CONFIG_BSP_USING_SDIO0=y

View File

@ -123,6 +123,10 @@ menu "Hardware Drivers Config"
bool "Enable ETH"
default n
config BSP_USING_BULETOOTH
bool "Enable BULETOOTH"
default n
menuconfig BSP_USING_RTC
bool "Enable RTC"
select RT_USING_RTC

View File

@ -2,7 +2,7 @@
from building import *
cwd = GetCurrentDir()
src = Glob('*.c') + Glob('*.cpp')
src = Glob('*.c') + Glob('*.cpp') + Glob('*.a')
CPPPATH = [cwd, str(Dir('#'))]
if not GetDepend('BSP_USING_ETH'):

View File

@ -0,0 +1,401 @@
/*
* Copyright (c) 2006-2020, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-11-29 bigmagic first version
*/
#include <rthw.h>
#include <rtthread.h>
#include "drv_bluetooth.h"
#include "drv_uart.h"
#include "raspi4.h"
//https://github.com/RPi-Distro/bluez-firmware/tree/master/broadcom
//arm-none-eabi-objcopy.exe -I binary -O elf32-littlearm -B arm driver\BCM4345C0.hcd driver\BCM4345C0.a
#define BT_UART_NAME "uart0"
#define BT_TX_MAX (4096)
#define BT_RX_MAX (1024)
#define BT_HEAD_NUM (4)
#define BT_TRY_NUM_MAX (3)
#define BT_SEND_MIN_PACK (8)
unsigned char lo(unsigned int val) { return (unsigned char)(val & 0xff); }
unsigned char hi(unsigned int val) { return (unsigned char)((val & 0xff00) >> 8); }
#define BT_THREAD_STACK_SIZE (2048)
#define BT_THREAD_PRIORITY (15)
#define BT_THREAD_TICK (10)
static char ch;
static rt_sem_t bt_rx_sem = RT_NULL;
static rt_device_t bt_device;
static rt_uint8_t tx_buff[BT_TX_MAX];
static rt_uint8_t rx_buff[BT_RX_MAX];
static rt_err_t bt_rx_ind(rt_device_t dev, rt_size_t size)
{
rt_sem_release(bt_rx_sem);
return RT_EOK;
}
int bt_uart_send_data(rt_device_t dev, rt_uint32_t *buf, int len)
{
return rt_device_write(dev, 0, buf, len);
}
void bt_uart_receive_flush(rt_device_t dev)
{
rt_uint32_t overflow_len = 0;
if (rt_sem_trytake(bt_rx_sem) == RT_EOK)
{
while (rt_device_read(dev, -1, &ch, 1) == 1)
{
overflow_len++;
}
bt_rx_sem->value = 0;
if(overflow_len > 0)
{
rt_kprintf("bt_uart_receive_flush: discarded data[%d]\n", overflow_len);
}
}
}
int bt_uart_receive_data(rt_device_t dev, rt_uint8_t *buf, rt_uint32_t *len, rt_int32_t time)
{
rt_uint16_t ii = 0;
rt_uint32_t buf_sz = *len;
rt_uint32_t overflow_len = 0;
rt_sem_take(bt_rx_sem, time);
while (rt_device_read(dev, -1, &ch, 1) == 1)
{
if (ii < buf_sz)
{
buf[ii++] = ch;
}
else
{
overflow_len++;
}
}
*len = ii;
if(overflow_len)
{
rt_kprintf("uart0 rx: Overflow![%d][%d]\n", buf_sz, overflow_len);
}
return ii;
}
void bt_data_pack(rt_uint8_t *tx_buff, rt_uint16_t ogf, rt_uint16_t ocf, rt_uint32_t data_len)
{
tx_buff[0] = BT_HCI_COMMAND_PKT;
tx_buff[1] = ogf;//hi(ogf << 10 | ocf);//opcode hi
tx_buff[2] = ocf;//lo(ogf << 10 | ocf);//opcode lo
tx_buff[3] = data_len;
}
rt_uint32_t bt_reply_check(const rt_uint8_t *buff, rt_uint16_t ogf, rt_uint16_t ocf, int pack_len)
{
//step 1
if(buff[0] != BT_HCI_EVENT_PKT)
{
return 1;
}
//step2
if (buff[1] == BT_CONNECT_COMPLETE_CODE)
{
if(buff[2] != 4)
{
return 2;
}
//err code
if(buff[3] != 0)
{
rt_kprintf("Saw HCI COMMAND STATUS error:%d", buff[3]);
return 12;
}
if(buff[4] == 0)
{
return 3;
}
if(buff[5] != ogf)
{
return 4;
}
if(buff[6] != ocf)
{
return 5;
}
}
else if(buff[1] == BT_COMMAND_COMPLETE_CODE)
{
if(buff[2] != 4)
{
return 6;
}
if(buff[3] == 0)
{
return 7;
}
if(buff[4] != ogf)
{
return 8;
}
if(buff[5] != ocf)
{
return 9;
}
if(buff[6] == 0)
{
return 10;
}
}
else
{
return 12;
}
return 0;
}
rt_err_t bt_loadfirmware(void)
{
int ii = 0;
int ret = 0;
int recv_len = BT_RX_MAX;
int step = 0;
rt_uint16_t ogf,ocf;
rt_memset(tx_buff, 0, BT_TX_MAX);
ogf = hi(BT_OGF_VENDOR << 10 | BT_COMMAND_LOAD_FIRMWARE);
ocf = lo(BT_OGF_VENDOR << 10 | BT_COMMAND_LOAD_FIRMWARE);
bt_data_pack(tx_buff, ogf, ocf, 0);
int kk = 0;
for (ii = 0; ii < BT_TRY_NUM_MAX; ii++)
{
recv_len = BT_RX_MAX;
bt_uart_receive_flush(bt_device);
bt_uart_send_data(bt_device, tx_buff, BT_SEND_MIN_PACK);
rt_thread_delay(100);
ret = bt_uart_receive_data(bt_device, rx_buff, &recv_len, 2000);
for(kk = 0; kk < recv_len; kk++)
{
rt_kprintf("rx_buff[%d]=%02x\n", kk, rx_buff[kk]);
}
if(ret > 0)
{
ret = bt_reply_check(rx_buff, ogf, ocf, ret);
if(ret == 0)
{
step = 1;
break;
}
else
{
rt_kprintf("err code is %d\n", ret);
}
}
}
if (step == 1)
{
extern unsigned char _binary_driver_BCM4345C0_hcd_size[];
extern unsigned char _binary_driver_BCM4345C0_hcd_start[];
unsigned int c = 0;
unsigned int size = (long)&_binary_driver_BCM4345C0_hcd_size;
while (c < size)
{
//unsigned char opcodebytes[] = {_binary_BCM4345C0_hcd_start[c], _binary_BCM4345C0_hcd_start[c + 1]};
unsigned char length = _binary_driver_BCM4345C0_hcd_start[c + 2];
unsigned char *data = &(_binary_driver_BCM4345C0_hcd_start[c + 3]);
rt_memset(tx_buff, 0, BT_TX_MAX);
ogf = hi(_binary_driver_BCM4345C0_hcd_start[c] << 10 | _binary_driver_BCM4345C0_hcd_start[c + 1]);
ocf = lo(_binary_driver_BCM4345C0_hcd_start[c] << 10 | _binary_driver_BCM4345C0_hcd_start[c + 1]);
bt_data_pack(tx_buff, ogf, ocf, length);
rt_memcpy(&tx_buff[BT_HEAD_NUM], data, length);
int kk = 0;
for (ii = 0; ii < BT_TRY_NUM_MAX; ii++)
{
recv_len = BT_RX_MAX;
bt_uart_receive_flush(bt_device);
rt_base_t aaa = rt_hw_interrupt_disable();
//bt_uart_send_data(bt_device, tx_buff, RT_ALIGN(length + BT_HEAD_NUM, 8));
bt_uart_receive_flush(bt_device);
//rt_hw_interrupt_enable(aaa);
rt_kprintf("send len is %d\n", RT_ALIGN(length + BT_HEAD_NUM, 8));
// rt_thread_delay(100);
// ret = bt_uart_receive_data(bt_device, rx_buff, &recv_len, 1000);
// rt_kprintf("recv is %d\n", recv_len);
for(kk = 0; kk < 16; kk++)
{
char ch;
if((PL011_REG_FR(UART0_BASE) & PL011_FR_RXFE) == 0)
{
ch = PL011_REG_DR(UART0_BASE) & 0xff;
rt_kprintf("rx_buff[%d]=%02x\n", kk, ch);
}
}
if(ret > 0)
{
ret = bt_reply_check(rx_buff, ogf, ocf, ret);
if(ret == 0)
{
step = 2;
break;
}
else
{
rt_kprintf("err code is %d\n", ret);
}
}
}
if(ii >= 3)
{
rt_kprintf("err!\n");
return RT_ERROR;
}
c += 3 + length;
}
}
else
{
return RT_ERROR;
}
// wait_msec(0x100000);
return RT_ERROR;
}
rt_err_t bt_reset(void)
{
int ii = 0;
int ret = 0;
int recv_len = BT_RX_MAX;
rt_uint16_t ogf,ocf;
rt_memset(tx_buff, 0, BT_TX_MAX);
ogf = hi(BT_OGF_HOST_CONTROL << 10 | BT_COMMAND_RESET_CHIP);
ocf = lo(BT_OGF_HOST_CONTROL << 10 | BT_COMMAND_RESET_CHIP);
bt_data_pack(tx_buff, ogf, ocf, 0);
for (ii = 0; ii < BT_TRY_NUM_MAX; ii++)
{
recv_len = BT_RX_MAX;
bt_uart_receive_flush(bt_device);
bt_uart_send_data(bt_device, tx_buff, 8);
//rt_thread_delay(100);
ret = bt_uart_receive_data(bt_device, rx_buff, &recv_len, 1000);
//rt_kprintf("recv_len is %d\n", recv_len);
if(ret > 0)
{
ret = bt_reply_check(rx_buff, ogf, ocf, ret);
if(ret == 0)
{
return RT_EOK;
}
else
{
rt_kprintf("err code is %d\n", ret);
}
}
}
return RT_ERROR;
}
rt_device_t bt_uart_init(const char *uartname)
{
rt_device_t dev = RT_NULL;
if (strcmp(uartname, BT_UART_NAME) == 0)
{
bt_rx_sem = rt_sem_create("btbuf", 0, RT_IPC_FLAG_FIFO);
dev = rt_device_find(uartname);
if (dev == RT_NULL)
{
rt_kprintf("can no find dev %s\n", uartname);
return dev;
}
if (rt_device_open(dev, RT_DEVICE_OFLAG_RDWR) == RT_EOK)
{
rt_device_set_rx_indicate(dev, bt_rx_ind);
}
return dev;
}
return dev;
}
static void bt_task_entry(void *param)
{
while (1)
{
rt_thread_delay(1000);
}
}
void bt_uart_protocol_init()
{
rt_thread_t bt_tid = RT_NULL;
bt_device = bt_uart_init(BT_UART_NAME);
bt_tid = rt_thread_create("bt_task",bt_task_entry,RT_NULL,BT_THREAD_STACK_SIZE,BT_THREAD_PRIORITY,BT_THREAD_TICK);
if(bt_tid == RT_NULL)
{
rt_kprintf("bt_task create err!\n");
return 0;
}
rt_thread_startup(bt_tid);
}
int rt_hw_bluetooth_init(void)
{
rt_kprintf("Initialising Bluetooth: \n");
bt_uart_protocol_init();
if(bt_reset() == RT_EOK)
{
rt_kprintf("bluetooth reset ok!\n");
}
else
{
rt_kprintf("bluetooth reset err!\n");
}
rt_thread_delay(1000);
//bt_loadfirmware();
}

View File

@ -0,0 +1,28 @@
/*
* Copyright (c) 2006-2020, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-11-29 bigmagic first version
*/
#ifndef __DRV_BT_H__
#define __DRV_BT_H__
#define BT_HCI_COMMAND_PKT (0x01)
#define BT_OGF_HOST_CONTROL (0x03)
#define BT_OGF_LE_CONTROL (0x08)
#define BT_OGF_VENDOR (0x3f)
#define BT_COMMAND_SET_BDADDR (0x01)
#define BT_COMMAND_RESET_CHIP (0x03)
#define BT_COMMAND_SET_BAUD (0x18)
#define BT_COMMAND_LOAD_FIRMWARE (0x2e)
#define BT_HCI_ACL_PKT (0x02)
#define BT_HCI_EVENT_PKT (0x04)
#define BT_COMMAND_COMPLETE_CODE (0x0e)
#define BT_CONNECT_COMPLETE_CODE (0x0f)
#define BT_LL_SCAN_ACTIVE (0x01)
#define BT_LL_ADV_NONCONN_IND (0x03)
#endif

View File

@ -63,15 +63,22 @@ static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_co
AUX_MU_LCR_REG(uart->hw_base) = 3; /* Works in 8-bit mode */
AUX_MU_MCR_REG(uart->hw_base) = 0; /* Disable RTS */
AUX_MU_IIR_REG(uart->hw_base) = 0xC6; /* Enable FIFO, Clear FIFO */
AUX_MU_BAUD_REG(uart->hw_base) = 270; /* 115200 = system clock 250MHz / (8 * (baud + 1)), baud = 270 */
AUX_MU_BAUD_REG(uart->hw_base) = 541; /* 115200 = system clock 500MHz / (8 * (baud + 1)), baud = 541 */
AUX_MU_CNTL_REG(uart->hw_base) = 3; /* Enable Transmitter and Receiver */
return RT_EOK;
}
if(uart->hw_base == UART0_BASE)
{
#ifndef BSP_USING_BULETOOTH
prev_raspi_pin_mode(GPIO_PIN_14, ALT0);
prev_raspi_pin_mode(GPIO_PIN_15, ALT0);
#else
prev_raspi_pin_mode(GPIO_PIN_30, ALT3);
prev_raspi_pin_mode(GPIO_PIN_31, ALT3);
prev_raspi_pin_mode(GPIO_PIN_32, ALT3);
prev_raspi_pin_mode(GPIO_PIN_33, ALT3);
#endif
}
if(uart->hw_base == UART3_BASE)
@ -92,13 +99,21 @@ static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_co
prev_raspi_pin_mode(GPIO_PIN_13, ALT4);
}
PL011_REG_CR(uart->hw_base) = 0;/*Clear UART setting*/
PL011_REG_LCRH(uart->hw_base) = 0;/*disable FIFO*/
PL011_REG_IMSC(uart->hw_base) = 0; /* mask all interrupt */
PL011_REG_ICR(uart->hw_base) = 0x7ff; /* clear all interrupt */
//PL011 clock 480MHz 480x10^6/baudrate/16
PL011_REG_IBRD(uart->hw_base) = ibrd;
PL011_REG_FBRD(uart->hw_base) = (((bauddiv - ibrd * 1000) * 64 + 500) / 1000);
PL011_REG_LCRH(uart->hw_base) = PL011_LCRH_WLEN_8;/*FIFO*/
PL011_REG_CR(uart->hw_base) = PL011_CR_UARTEN | PL011_CR_TXE | PL011_CR_RXE;/*art enable, TX/RX enable*/
#ifdef BSP_USING_BULETOOTH
PL011_REG_IFLS(uart->hw_base) = 0x08;
PL011_REG_LCRH(uart->hw_base) = 0x70;
PL011_REG_CR(uart->hw_base) = PL011_CR_UARTEN | PL011_CR_TXE | PL011_CR_RXE | PL011_CR_RTS;
#else
PL011_REG_IFLS(uart->hw_base) = 0x0;
PL011_REG_LCRH(uart->hw_base) = PL011_LCRH_WLEN_8;
PL011_REG_CR(uart->hw_base) = PL011_CR_UARTEN | PL011_CR_TXE | PL011_CR_RXE;
#endif
PL011_REG_IMSC(uart->hw_base) = 0;
return RT_EOK;
}

View File

@ -40,7 +40,7 @@
#define RT_USING_DEVICE
#define RT_USING_CONSOLE
#define RT_CONSOLEBUF_SIZE 128
#define RT_CONSOLE_DEVICE_NAME "uart0"
#define RT_CONSOLE_DEVICE_NAME "uart1"
#define RT_VER_NUM 0x40003
#define ARCH_ARMV8
@ -99,7 +99,7 @@
#define RT_SYSTEM_WORKQUEUE_PRIORITY 23
#define RT_USING_SERIAL
#define RT_SERIAL_USING_DMA
#define RT_SERIAL_RB_BUFSZ 64
#define RT_SERIAL_RB_BUFSZ 512
#define RT_USING_I2C
#define RT_USING_I2C_BITOPS
#define RT_USING_PIN
@ -251,6 +251,7 @@
#define BSP_USING_UART
#define RT_USING_UART0
#define RT_USING_UART1
#define RT_USING_UART3
#define RT_USING_UART5
#define BSP_USING_GIC
@ -265,6 +266,7 @@
#define BSP_USING_CORETIMER
#define BSP_USING_WDT
#define BSP_USING_ETH
#define BSP_USING_BULETOOTH
#define BSP_USING_SDIO
#define BSP_USING_SDIO0