@@ -35,8 +35,6 @@ extern "C" { void HW_IPCC_Rx_Handler(void);}
3535/* Private variables ---------------------------------------------------------*/
3636PLACE_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- */
4038PLACE_IN_SECTION (" MB_MEM2" ) ALIGN(4 ) static uint8_t EvtPool[POOL_SIZE];
4139PLACE_IN_SECTION (" MB_MEM2" ) ALIGN(4 ) static TL_CmdPacket_t SystemCmdBuffer;
4240PLACE_IN_SECTION (" MB_MEM2" ) ALIGN(4 ) static uint8_t
@@ -52,6 +50,11 @@ 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+ static bool phase_bd_addr = false ;
56+ static bool phase_tx_power = false ;
57+
5558HCISharedMemTransportClass::HCISharedMemTransportClass (BLEChip_t ble_chip) :
5659 _ble_chip(ble_chip)
5760{
@@ -71,12 +74,26 @@ static void evt_received(TL_EvtPacket_t *hcievt)
7174 // We need to memcpy the data before passing to higher layers.
7275 switch (hcievt->evtserial .type ) {
7376 case TL_BLEEVT_PKT_TYPE:
77+
78+ /* capture event after HCI_RESET */
79+ if (phase_bd_addr) {
80+ phase_bd_addr = false ;
81+ bt_ipm_set_addr ();
82+ phase_tx_power = true ;
83+ } else if (phase_tx_power) {
84+ phase_tx_power = false ;
85+ bt_ipm_set_power ();
86+ } else {
87+ phase_bd_addr = false ;
88+ phase_tx_power = false ;
89+
7490 len = hcievt->evtserial .evt .plen + TL_EVT_HDR_SIZE;
7591 /* store received data in the _rxbuff buffer */
7692 memcpy ((uint8_t *)&_rxbuff, (uint8_t *)&hcievt->evtserial , len);
7793 /* move index */
7894 _write_index += len;
7995 // TODO: control the _rxbuff cannot overflow
96+ }
8097 break ;
8198 case TL_ACL_DATA_PKT_TYPE: {
8299 TL_AclDataSerial_t *acl = &(((TL_AclDataPacket_t *)hcievt)->AclDataSerial );
@@ -155,30 +172,6 @@ static bool sysevt_check(void)
155172 }
156173}
157174
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-
182175static void acl_data_ack (void )
183176{
184177 /* *
@@ -266,12 +259,20 @@ int HCISharedMemTransportClass::begin()
266259 */
267260
268261
269- /* Now start BLE service on firmware side, using Vendor specific
270- * command on the System Channel
271- */
272- stm32wb_start_ble ();
262+ /* Now start BLE service on firmware side, using Vendor specific
263+ * command on the System Channel
264+ */
265+ stm32wb_start_ble ();
273266
274267 /* "IPM Channel Open Completed" */
268+
269+ /* Once reset complete event is received we willneed
270+ * to send a few more commands:
271+ * set bd addr with bt_ipm_set_addr(); */
272+ phase_bd_addr = false ;
273+ /* and Tx power with bt_ipm_set_power(); */
274+ phase_tx_power = false ;
275+
275276 return 1 ;
276277 } else {
277278 /* "Error opening IPM channel" */
@@ -344,6 +345,13 @@ int HCISharedMemTransportClass::read()
344345
345346size_t HCISharedMemTransportClass::write (const uint8_t * data, size_t length)
346347{
348+ /* check if the reset phase is in progress */
349+ if ((data[1 ] == (HCI_RESET & 0x000000FF )) && (data[2 ] == ((HCI_RESET & 0x0000FF00 ) >> 8 ))) {
350+ /* this is the HCI Reset command being sent */
351+ phase_bd_addr = true ;
352+ /* after receiving, the address will be send */
353+ }
354+
347355 const uint8_t * msg_data;
348356 msg_data = &data[1 ];
349357
@@ -488,11 +496,96 @@ size_t HCISharedMemTransportClass::write(const uint8_t* data, size_t length)
488496
489497 }
490498
499+ #if 1
500+ /* This function fills in a BD address table */
501+ bool get_bd_address (uint8_t *bd_addr)
502+ {
503+ uint8_t *otp_addr;
504+ uint32_t udn;
505+ uint32_t company_id;
506+ uint32_t device_id;
507+ bool bd_found;
508+
509+ udn = LL_FLASH_GetUDN ();
510+
511+ if (udn != 0xFFFFFFFF ) {
512+ /* "Found Unique Device Number: %#06x", udn) */
513+
514+ company_id = LL_FLASH_GetSTCompanyID ();
515+ device_id = LL_FLASH_GetDeviceID ();
516+
517+ bd_addr[0 ] = (uint8_t )(udn & 0x000000FF );
518+ bd_addr[1 ] = (uint8_t )((udn & 0x0000FF00 ) >> 8 );
519+ bd_addr[2 ] = (uint8_t )((udn & 0x00FF0000 ) >> 16 );
520+ bd_addr[3 ] = (uint8_t )device_id;
521+ bd_addr[4 ] = (uint8_t )(company_id & 0x000000FF );
522+ bd_addr[5 ] = (uint8_t )((company_id & 0x0000FF00 ) >> 8 );
523+
524+ bd_found = true ;
525+ } else {
526+ bd_addr =NULL ;
527+ bd_found = false ;
528+ }
529+
530+ return bd_found;
531+ }
532+
533+ static int bt_ipm_set_addr (void )
534+ {
535+ /* the specific table for set addr is 8 bytes:
536+ * one byte for config_offset
537+ * one byte for length
538+ * 6 bytes for payload */
539+ uint8_t data[4 +8 ];
540+
541+ if (get_bd_address (bd_addr_udn)) {
542+ /* create ACI_HAL_WRITE_CONFIG_DATA */
543+
544+ data[0 ] = BT_BUF_CMD;
545+ data[1 ] = uint8_t (ACI_WRITE_CONFIG_DATA_OPCODE & 0x000000FF ); /* OCF */
546+ data[2 ] = uint8_t ((ACI_WRITE_CONFIG_DATA_OPCODE & 0x0000FF00 ) >> 8 ); /* OGF */
547+ data[3 ] = 8 ; /* length of parameters */
548+ /* fill the ACI_HAL_WRITE_CONFIG_DATA with the addr*/
549+ data[4 ] = CONFIG_DATA_PUBADDR_OFFSET; /* the offset */
550+ data[5 ] = 6 ; /* is the length of the bd_addr table */
551+ memcpy (data + 6 , bd_addr_udn, 6 );
552+ /* send the ACI_HAL_WRITE_CONFIG_DATA */
553+ mbox_write (data[0 ], 11 , &data[1 ]);
554+
555+ return 1 ; /* success */
556+ } else {
557+ return 0 ; /* Error */
558+ }
559+ }
560+
561+ static int bt_ipm_set_power (void )
562+ {
563+ /* the specific table for power is 3 bytes:
564+ * one byte for cmd
565+ * two bytes for value */
566+ uint8_t data[4 +3 ];
567+
568+ data[0 ] = BT_BUF_CMD; /* the type */
569+ data[1 ] = (uint8_t )(ACI_HAL_SET_TX_POWER_LEVEL & 0x000000FF ); /* the OPCODE */
570+ data[2 ] = (uint8_t )((ACI_HAL_SET_TX_POWER_LEVEL & 0x0000FF00 ) >> 8 );
571+ data[3 ] = 3 ; /* the length */
572+ /* fill the ACI_HAL_WRITE_CONFIG_DATA */
573+ data[4 ] = 0x0F ; /* the command */
574+ data[5 ] = 0x18 ;
575+ data[6 ] = 0x01 ;
576+
577+ /* send the ACI_HAL_WRITE_CONFIG_DATA */
578+ mbox_write (data[0 ], 3 , &data[1 ]);
579+
580+ return 1 ; /* success */
581+ }
582+
583+ #else
491584 int bt_ipm_ble_init(void)
492585 {
493586 uint16_t opcode;
494587 static uint8_t randCnt;
495- # if 0
588+
496589 /* if event is a command complete event */
497590 if (*pMsg == HCI_CMD_CMPL_EVT) {
498591 /* "Command Complete Event Command" */
@@ -707,9 +800,10 @@ size_t HCISharedMemTransportClass::write(const uint8_t* data, size_t length)
707800#endif /* DEBUG */
708801 }
709802 }
710- # endif
711- return 0 ;
803+
804+ return 1; /* success */
712805 }
806+ #endif
713807
714808 uint16_t mbox_write (uint8_t type, uint16_t len, const uint8_t *pData)
715809 {
@@ -817,40 +911,3 @@ static void init_debug(void)
817911
818912 return ;
819913}
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- }
0 commit comments