Skip to content

Commit d229131

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

File tree

2 files changed

+109
-44
lines changed

2 files changed

+109
-44
lines changed

Diff for: src/utility/HCISharedMemTransport.cpp

+104-44
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static uint8_t
5252
uint16_t _write_index;
5353
uint16_t _write_index_initial;
5454

55+
/** Bluetooth Device Address */
56+
static uint8_t bd_addr_udn[6];
57+
5558
HCISharedMemTransportClass::HCISharedMemTransportClass(BLEChip_t ble_chip) :
5659
_ble_chip(ble_chip)
5760
{
@@ -266,12 +269,20 @@ int HCISharedMemTransportClass::begin()
266269
*/
267270

268271

269-
/* Now start BLE service on firmware side, using Vendor specific
270-
* command on the System Channel
271-
*/
272-
stm32wb_start_ble();
272+
/* Now start BLE service on firmware side, using Vendor specific
273+
* command on the System Channel
274+
*/
275+
stm32wb_start_ble();
273276

274277
/* "IPM Channel Open Completed" */
278+
279+
/* Once reset complete evet is received we need
280+
* to send a few more commands:
281+
* set bd addr */
282+
bt_ipm_set_addr();
283+
/* and Tx power */
284+
bt_ipm_set_power();
285+
275286
return 1;
276287
} else {
277288
/* "Error opening IPM channel" */
@@ -488,11 +499,96 @@ size_t HCISharedMemTransportClass::write(const uint8_t* data, size_t length)
488499

489500
}
490501

502+
#if 1
503+
/* This function fills in a BD address table */
504+
bool get_bd_address(uint8_t *bd_addr)
505+
{
506+
uint8_t *otp_addr;
507+
uint32_t udn;
508+
uint32_t company_id;
509+
uint32_t device_id;
510+
bool bd_found;
511+
512+
udn = LL_FLASH_GetUDN();
513+
514+
if (udn != 0xFFFFFFFF) {
515+
/* "Found Unique Device Number: %#06x", udn) */
516+
517+
company_id = LL_FLASH_GetSTCompanyID();
518+
device_id = LL_FLASH_GetDeviceID();
519+
520+
bd_addr[0] = (uint8_t)(udn & 0x000000FF);
521+
bd_addr[1] = (uint8_t)((udn & 0x0000FF00) >> 8);
522+
bd_addr[2] = (uint8_t)((udn & 0x00FF0000) >> 16);
523+
bd_addr[3] = (uint8_t)device_id;
524+
bd_addr[4] = (uint8_t)(company_id & 0x000000FF);
525+
bd_addr[5] = (uint8_t)((company_id & 0x0000FF00) >> 8);
526+
527+
bd_found = true;
528+
} else {
529+
bd_addr =NULL;
530+
bd_found = false;
531+
}
532+
533+
return bd_found;
534+
}
535+
536+
static int bt_ipm_set_addr(void)
537+
{
538+
/* the specific table for set addr is 8 bytes:
539+
* one byte for config_offset
540+
* one byte for length
541+
* 6 bytes for payload */
542+
uint8_t data[4+8];
543+
544+
if (get_bd_address(bd_addr_udn)) {
545+
/* create ACI_HAL_WRITE_CONFIG_DATA */
546+
547+
data[0] = BT_BUF_CMD;
548+
data[1] = uint8_t(ACI_WRITE_CONFIG_DATA_OPCODE & 0x000000FF); /* OCF */
549+
data[2] = uint8_t((ACI_WRITE_CONFIG_DATA_OPCODE & 0x0000FF00) >> 8); /* OGF */
550+
data[3] = 8; /* length of parameters */
551+
/* fill the ACI_HAL_WRITE_CONFIG_DATA with the addr*/
552+
data[4] = CONFIG_DATA_PUBADDR_OFFSET; /* the offset */
553+
data[5] = 6; /* is the length of the bd_addr table */
554+
memcpy(data + 6, bd_addr_udn, 6);
555+
/* send the ACI_HAL_WRITE_CONFIG_DATA */
556+
mbox_write(data[0], 11, &data[1]);
557+
558+
return 1; /* success */
559+
} else {
560+
return 0; /* Error */
561+
}
562+
}
563+
564+
static int bt_ipm_set_power(void)
565+
{
566+
/* the specific table for power is 3 bytes:
567+
* one byte for cmd
568+
* two bytes for value */
569+
uint8_t data[4+3];
570+
571+
data[0] = BT_BUF_CMD; /* the type */
572+
data[1] = (uint8_t)(ACI_HAL_SET_TX_POWER_LEVEL & 0x000000FF); /* the OPCODE */
573+
data[2] = (uint8_t)((ACI_HAL_SET_TX_POWER_LEVEL & 0x0000FF00) >> 8);
574+
data[3] = 3; /* the length */
575+
/* fill the ACI_HAL_WRITE_CONFIG_DATA */
576+
data[4] = 0x0F; /* the command */
577+
data[5] = 0x18;
578+
data[6] = 0x01;
579+
580+
/* send the ACI_HAL_WRITE_CONFIG_DATA */
581+
mbox_write(data[0], 3, &data[1]);
582+
583+
return 1; /* success */
584+
}
585+
586+
#else
491587
int bt_ipm_ble_init(void)
492588
{
493589
uint16_t opcode;
494590
static uint8_t randCnt;
495-
#if 0
591+
496592
/* if event is a command complete event */
497593
if (*pMsg == HCI_CMD_CMPL_EVT) {
498594
/* "Command Complete Event Command" */
@@ -707,9 +803,10 @@ size_t HCISharedMemTransportClass::write(const uint8_t* data, size_t length)
707803
#endif /* DEBUG */
708804
}
709805
}
710-
#endif
711-
return 0;
806+
807+
return 1; /* success */
712808
}
809+
#endif
713810

714811
uint16_t mbox_write(uint8_t type, uint16_t len, const uint8_t *pData)
715812
{
@@ -817,40 +914,3 @@ static void init_debug(void)
817914

818915
return;
819916
}
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-
}

Diff for: 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)