Skip to content

Commit 109a654

Browse files
committed
complete BLE init with device and power settings
Signed-off-by: Francois Ramu <[email protected]>
1 parent 8609b54 commit 109a654

File tree

2 files changed

+109
-70
lines changed

2 files changed

+109
-70
lines changed

src/utility/HCISharedMemTransport.cpp

+104-70
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ extern "C" { void HW_IPCC_Rx_Handler(void);}
3535
/* Private variables ---------------------------------------------------------*/
3636
PLACE_IN_SECTION("MB_MEM1") ALIGN(4) static TL_CmdPacket_t BleCmdBuffer;
3737

38-
/*PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static uint8_t HciAclDataBuffer[MAX_HCI_ACL_PACKET_SIZE];
39-
*/
4038
PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static uint8_t EvtPool[POOL_SIZE];
4139
PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static TL_CmdPacket_t SystemCmdBuffer;
4240
PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static uint8_t
@@ -52,6 +50,9 @@ PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static uint8_t
5250
uint16_t _write_index;
5351
uint16_t _write_index_initial;
5452

53+
/** Bluetooth Device Address */
54+
static uint8_t bd_addr_udn[6];
55+
5556
HCISharedMemTransportClass::HCISharedMemTransportClass(BLEChip_t ble_chip) :
5657
_ble_chip(ble_chip)
5758
{
@@ -155,30 +156,6 @@ static bool sysevt_check(void)
155156
}
156157
}
157158

158-
#if 0
159-
void shci_cmd_resp_release(uint32_t flag)
160-
{
161-
/* the semaphore is released here, so the variable becomes true. */
162-
sys_resp = true;
163-
return;
164-
}
165-
166-
void shci_cmd_resp_wait(uint32_t timeout)
167-
{
168-
/* TO DO: manage timeouts if we can return an error */
169-
// for (unsigned long start = millis(); (millis() - start) < 10000;) {
170-
while(1) {
171-
/* Wait for 10sec max - if not return an error */
172-
if (sys_resp) { break; }
173-
}
174-
175-
if (!sys_resp) {
176-
/* no event received, timeout occurs */
177-
return;
178-
}
179-
}
180-
#endif
181-
182159
static void acl_data_ack(void)
183160
{
184161
/**
@@ -266,12 +243,20 @@ int HCISharedMemTransportClass::begin()
266243
*/
267244

268245

269-
/* Now start BLE service on firmware side, using Vendor specific
270-
* command on the System Channel
271-
*/
272-
stm32wb_start_ble();
246+
/* Now start BLE service on firmware side, using Vendor specific
247+
* command on the System Channel
248+
*/
249+
stm32wb_start_ble();
273250

274251
/* "IPM Channel Open Completed" */
252+
253+
/* Once reset complete evet is received we need
254+
* to send a few more commands:
255+
* set bd addr */
256+
bt_ipm_set_addr();
257+
/* and Tx power */
258+
bt_ipm_set_power();
259+
275260
return 1;
276261
} else {
277262
/* "Error opening IPM channel" */
@@ -488,11 +473,96 @@ size_t HCISharedMemTransportClass::write(const uint8_t* data, size_t length)
488473

489474
}
490475

476+
#if 1
477+
/* This function fills in a BD address table */
478+
bool get_bd_address(uint8_t *bd_addr)
479+
{
480+
uint8_t *otp_addr;
481+
uint32_t udn;
482+
uint32_t company_id;
483+
uint32_t device_id;
484+
bool bd_found;
485+
486+
udn = LL_FLASH_GetUDN();
487+
488+
if (udn != 0xFFFFFFFF) {
489+
/* "Found Unique Device Number: %#06x", udn) */
490+
491+
company_id = LL_FLASH_GetSTCompanyID();
492+
device_id = LL_FLASH_GetDeviceID();
493+
494+
bd_addr[0] = (uint8_t)(udn & 0x000000FF);
495+
bd_addr[1] = (uint8_t)((udn & 0x0000FF00) >> 8);
496+
bd_addr[2] = (uint8_t)((udn & 0x00FF0000) >> 16);
497+
bd_addr[3] = (uint8_t)device_id;
498+
bd_addr[4] = (uint8_t)(company_id & 0x000000FF);
499+
bd_addr[5] = (uint8_t)((company_id & 0x0000FF00) >> 8);
500+
501+
bd_found = true;
502+
} else {
503+
bd_addr =NULL;
504+
bd_found = false;
505+
}
506+
507+
return bd_found;
508+
}
509+
510+
static int bt_ipm_set_addr(void)
511+
{
512+
/* the specific table for set addr is 8 bytes:
513+
* one byte for config_offset
514+
* one byte for length
515+
* 6 bytes for payload */
516+
uint8_t data[4+8];
517+
518+
if (get_bd_address(bd_addr_udn)) {
519+
/* create ACI_HAL_WRITE_CONFIG_DATA */
520+
521+
data[0] = BT_BUF_CMD;
522+
data[1] = uint8_t(ACI_WRITE_CONFIG_DATA_OPCODE & 0x000000FF); /* OCF */
523+
data[2] = uint8_t((ACI_WRITE_CONFIG_DATA_OPCODE & 0x0000FF00) >> 8); /* OGF */
524+
data[3] = 8; /* length of parameters */
525+
/* fill the ACI_HAL_WRITE_CONFIG_DATA with the addr*/
526+
data[4] = CONFIG_DATA_PUBADDR_OFFSET; /* the offset */
527+
data[5] = 6; /* is the length of the bd_addr table */
528+
memcpy(data + 6, bd_addr_udn, 6);
529+
/* send the ACI_HAL_WRITE_CONFIG_DATA */
530+
mbox_write(data[0], 11, &data[1]);
531+
532+
return 1; /* success */
533+
} else {
534+
return 0; /* Error */
535+
}
536+
}
537+
538+
static int bt_ipm_set_power(void)
539+
{
540+
/* the specific table for power is 3 bytes:
541+
* one byte for cmd
542+
* two bytes for value */
543+
uint8_t data[4+3];
544+
545+
data[0] = BT_BUF_CMD; /* the type */
546+
data[1] = (uint8_t)(ACI_HAL_SET_TX_POWER_LEVEL & 0x000000FF); /* the OPCODE */
547+
data[2] = (uint8_t)((ACI_HAL_SET_TX_POWER_LEVEL & 0x0000FF00) >> 8);
548+
data[3] = 3; /* the length */
549+
/* fill the ACI_HAL_WRITE_CONFIG_DATA */
550+
data[4] = 0x0F; /* the command */
551+
data[5] = 0x18;
552+
data[6] = 0x01;
553+
554+
/* send the ACI_HAL_WRITE_CONFIG_DATA */
555+
mbox_write(data[0], 3, &data[1]);
556+
557+
return 1; /* success */
558+
}
559+
560+
#else
491561
int bt_ipm_ble_init(void)
492562
{
493563
uint16_t opcode;
494564
static uint8_t randCnt;
495-
#if 0
565+
496566
/* if event is a command complete event */
497567
if (*pMsg == HCI_CMD_CMPL_EVT) {
498568
/* "Command Complete Event Command" */
@@ -707,9 +777,10 @@ size_t HCISharedMemTransportClass::write(const uint8_t* data, size_t length)
707777
#endif /* DEBUG */
708778
}
709779
}
710-
#endif
711-
return 0;
780+
781+
return 1; /* success */
712782
}
783+
#endif
713784

714785
uint16_t mbox_write(uint8_t type, uint16_t len, const uint8_t *pData)
715786
{
@@ -817,40 +888,3 @@ static void init_debug(void)
817888

818889
return;
819890
}
820-
821-
/* This function fills in a BD address table */
822-
bool get_bd_address(uint8_t *bd_addr)
823-
{
824-
uint8_t *otp_addr;
825-
uint32_t udn;
826-
uint32_t company_id;
827-
uint32_t device_id;
828-
bool bd_found;
829-
830-
udn = LL_FLASH_GetUDN();
831-
832-
if (udn != 0xFFFFFFFF) {
833-
#if defined(DEBUG)
834-
tr_info("Found Unique Device Number: %#06x", udn);
835-
#endif /* DEBUG */
836-
company_id = LL_FLASH_GetSTCompanyID();
837-
device_id = LL_FLASH_GetDeviceID();
838-
839-
bd_addr[0] = (uint8_t)(udn & 0x000000FF);
840-
bd_addr[1] = (uint8_t)((udn & 0x0000FF00) >> 8);
841-
bd_addr[2] = (uint8_t)((udn & 0x00FF0000) >> 16);
842-
bd_addr[3] = (uint8_t)device_id;
843-
bd_addr[4] = (uint8_t)(company_id & 0x000000FF);
844-
bd_addr[5] = (uint8_t)((company_id & 0x0000FF00) >> 8);
845-
846-
bd_found = true;
847-
} else {
848-
#if defined(DEBUG)
849-
tr_info("Cannot find Bluetooth Device ADDRESS to program - will leave hw default");
850-
#endif /* DEBUG */
851-
bd_addr = NULL;
852-
bd_found = false;
853-
}
854-
855-
return bd_found;
856-
}

src/utility/HCISharedMemTransport.h

+5
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@
5959
#endif
6060
#define BLE_MODULE_SHARED_MEM_BUFFER_SIZE 128
6161

62+
#define BT_BUF_CMD 1
63+
#define BT_BUF_ACL_OUT 2
64+
6265
struct aci_set_tx_power {
6366
uint8_t cmd;
6467
uint8_t value[2];
@@ -101,6 +104,8 @@ static void acl_data_ack(void);
101104
static bool acl_data_wait(void);
102105
static void init_debug(void);
103106
static bool get_bd_address(uint8_t *bd_addr);
107+
static int bt_ipm_set_addr(void);
108+
static int bt_ipm_set_power(void);
104109
static bool sysevt_wait(void);
105110
static bool sysevt_check(void);
106111
static void sysevt_received(void *pdata);

0 commit comments

Comments
 (0)