[BLE(低功耗蓝牙)] STEVAL-IDB007V1板子和手机通信成功

bkn1860   2018-1-24 18:53 楼主
      今天在版主@littleshrimp的帮助下终于把手机通信搞通了,调试使用的是@littleshrimp的上位机,可以从https://bbs.eeworld.com.cn/thread-607573-1-1.html中下载。MCU代码是在BLE Beacon Demo中修改的。      调试过程中走了一个弯路,使用BlueNRG-1 Navigator烧录的hex路径是C:\Program Files (x86)\STMicroelectronics\BlueNRG-1_2 DK 2.5.0\Firmware\BLE_Examples\BlueNRG-1\,不是工程所在路径下的hex,需要用MDK编译完复制到BLE_Examples\BlueNRG-1\对应的路径下。




  1. /******************** (C) COPYRIGHT 2016 STMicroelectronics ********************
  2. * File Name          : BLE_Beacon_main.c
  3. * Author             : AMS - VMA RF Application Team
  4. * Version            : V1.1.0
  5. * Date               : 20-May-2016
  6. * Description        : BlueNRG-1 main file for beacon device
  7. ********************************************************************************
  8. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  9. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
  10. * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
  11. * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
  12. * CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
  13. * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  14. *******************************************************************************/
  15. /** @addtogroup BlueNRG1_demonstrations_applications
  16. *  BlueNRG-1 Beacon demo \see BLE_Beacon_main.c for documentation.
  17. *
  18. *@{
  19. */

  20. /** @} */
  21. /** \cond DOXYGEN_SHOULD_SKIP_THIS
  22. */

  23. /* Includes ------------------------------------------------------------------*/
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include "BlueNRG1_it.h"
  27. #include "BlueNRG1_conf.h"
  28. #include "ble_const.h"
  29. #include "bluenrg1_stack.h"
  30. #include "sleep.h"
  31. #include "SDK_EVAL_Config.h"
  32. #include "Beacon_config.h"
  33. #include "OTA_btl.h"

  34. #define H16(x)  (((int) (x)) >> 8)
  35. #define L16(x)  (((int) (x)))

  36. #define H32(x)  (((int) (x)) >> 24)
  37. #define MH32(x)  (((int) (x)) >> 16)
  38. #define L32(x)  (((int) (x)) >> 8)
  39. #define XL32(x)  (((int) (x)))
  40.          
  41. float vlotage = 3.3;//V
  42. uint32_t time = 2500;//ms
  43. float pressure = 101.5;//hPa
  44. float temperature = 26.2;//degC
  45. float angleX = 10.2;//deg
  46. float angleY = 0.3;//deg

  47. /* Private typedef -----------------------------------------------------------*/
  48. /* Private define ------------------------------------------------------------*/
  49. #define BLE_BEACON_VERSION_STRING "1.1.0"

  50. /* Set to 1 for enabling Flags AD Type position at the beginning
  51.    of the advertising packet */
  52. #define ENABLE_FLAGS_AD_TYPE_AT_BEGINNING 1

  53. /* Private macro -------------------------------------------------------------*/
  54. /* Private variables ---------------------------------------------------------*/
  55. /* Private function prototypes -----------------------------------------------*/
  56. /* Private functions ---------------------------------------------------------*/

  57. void Device_Init(void)
  58. {
  59.   uint8_t ret;
  60.   uint16_t service_handle;
  61.   uint16_t dev_name_char_handle;
  62.   uint16_t appearance_char_handle;

  63.   /* Set the TX Power to -2 dBm */
  64.   ret = aci_hal_set_tx_power_level(1,4);
  65.   if(ret != 0) {
  66.     printf ("Error in aci_hal_set_tx_power_level() 0x%04xr\n", ret);
  67.     while(1);
  68.   }

  69.   /* Init the GATT */
  70.   ret = aci_gatt_init();
  71.   if (ret != 0)
  72.     printf ("Error in aci_gatt_init() 0x%04xr\n", ret);
  73.   else
  74.     printf ("aci_gatt_init() --> SUCCESS\r\n");

  75.   /* Init the GAP */
  76.   ret = aci_gap_init(0x01, 0x00, 0x08, &service_handle,
  77.                      &dev_name_char_handle, &appearance_char_handle);
  78.   if (ret != 0)
  79.     printf ("Error in aci_gap_init() 0x%04x\r\n", ret);
  80.   else
  81.     printf ("aci_gap_init() --> SUCCESS\r\n");
  82. }


  83. /**
  84. * [url=home.php?mod=space&uid=159083]@brief[/url]  Start beaconing
  85. * @param  None
  86. * @retval None
  87. */
  88. static void Start_Beaconing(void)
  89. {  
  90.   uint8_t ret = BLE_STATUS_SUCCESS;

  91. #if ENABLE_FLAGS_AD_TYPE_AT_BEGINNING
  92.   /* Set AD Type Flags at beginning on Advertising packet  */
  93.   uint8_t adv_data[] = {
  94.       /* Advertising data: Flags AD Type */
  95.       0x02,
  96.       0x01,
  97.       0x06,
  98.       0x17,
  99.       0x11,
  100.       0x22,
  101.       0x33,
  102.       0x44,
  103.       0x55,
  104.       0x66,
  105.       0x77,
  106.       0x88,
  107.       0x99,
  108.       H16(vlotage * 10),
  109.       L16(vlotage * 10),
  110.       H32(time),
  111.       MH32(time),
  112.       L32(time),
  113.       XL32(time),
  114.       H16(pressure * 10),
  115.       L16(pressure * 10),
  116.       H16(temperature * 10),
  117.       L16(temperature * 10),
  118.       H16(angleX * 10),
  119.       L16(angleX * 10),
  120.       H16(angleY * 10),
  121.       L16(angleY * 10),
  122.   };
  123. #else
  124.    uint8_t manuf_data[] = {
  125.       26, //len
  126.       AD_TYPE_MANUFACTURER_SPECIFIC_DATA, //manufacturer type
  127.       0x30, 0x00, //Company identifier code (Default is 0x0030 - STMicroelectronics: To be customized for specific identifier)
  128.       0x02,       // ID
  129.       0x15,       //Length of the remaining payload
  130.       0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4, //Location UUID
  131.       0xA1, 0x2F, 0x17, 0xD1, 0xAD, 0x07, 0xA9, 0x61,
  132.       0x00, 0x00, // Major number
  133.       0x00, 0x00, // Minor number
  134.       0xC8        //2's complement of the Tx power (-56dB)};      
  135.    };
  136. #endif

  137.   /* disable scan response */
  138.   ret = hci_le_set_scan_response_data(0,NULL);
  139.   if (ret != BLE_STATUS_SUCCESS)
  140.   {
  141.     printf ("Error in hci_le_set_scan_resp_data() 0x%04x\r\n", ret);
  142.     return;
  143.   }
  144.   else
  145.     printf ("hci_le_set_scan_resp_data() --> SUCCESS\r\n");

  146.   /* put device in non connectable mode */
  147.   //32 = 20ms
  148.   ret = aci_gap_set_discoverable(ADV_NONCONN_IND, 160, 160, PUBLIC_ADDR, NO_WHITE_LIST_USE,
  149.                                  0, NULL, 0, NULL, 0, 0);
  150.   if (ret != BLE_STATUS_SUCCESS)
  151.   {
  152.     printf ("Error in aci_gap_set_discoverable() 0x%04x\r\n", ret);
  153.     return;
  154.   }
  155.   else
  156.     printf ("aci_gap_set_discoverable() --> SUCCESS\r\n");

  157. #if ENABLE_FLAGS_AD_TYPE_AT_BEGINNING
  158.   /* Set the  ADV data with the Flags AD Type at beginning of the
  159.      advertsing packet,  followed by the beacon manufacturer specific data */
  160.   ret = hci_le_set_advertising_data (sizeof(adv_data), adv_data);
  161.   if (ret != BLE_STATUS_SUCCESS)
  162.   {
  163.     printf ("Error in hci_le_set_advertising_data() 0x%04x\r\n", ret);
  164.     return;
  165.   }
  166.   else
  167.     printf ("hci_le_set_advertising_data() --> SUCCESS\r\n");
  168. #else
  169.   /* Delete the TX power level information */
  170.   ret = aci_gap_delete_ad_type(AD_TYPE_TX_POWER_LEVEL);
  171.   if (ret != BLE_STATUS_SUCCESS)
  172.   {
  173.     printf ("Error in aci_gap_delete_ad_type() 0x%04x\r\n", ret);
  174.     return;
  175.   }
  176.   else
  177.     printf ("aci_gap_delete_ad_type() --> SUCCESS\r\n");

  178.   /* Update the ADV data with the BEACON manufacturing data */
  179.   ret = aci_gap_update_adv_data(27, manuf_data);  
  180.   if (ret != BLE_STATUS_SUCCESS)
  181.   {
  182.     printf ("Error in aci_gap_update_adv_data() 0x%04x\r\n", ret);
  183.     return;
  184.   }
  185.   else
  186.     printf ("aci_gap_update_adv_data() --> SUCCESS\r\n");
  187. #endif
  188. }

  189. static void Update_Beaconing(void)
  190. {
  191.   /* Set AD Type Flags at beginning on Advertising packet  */
  192.   uint8_t adv_data[] = {
  193.       /* Advertising data: Flags AD Type */
  194.       0x02,
  195.       0x01,
  196.       0x06,
  197.       0x17,
  198.       0x11,
  199.       0x22,
  200.       0x33,
  201.       0x44,
  202.       0x55,
  203.       0x66,
  204.       0x77,
  205.       0x88,
  206.       0x99,
  207.       H16(vlotage * 10),
  208.       L16(vlotage * 10),
  209.       H32(time),
  210.       MH32(time),
  211.       L32(time),
  212.       XL32(time),
  213.       H16(pressure * 10),
  214.       L16(pressure * 10),
  215.       H16(temperature * 10),
  216.       L16(temperature * 10),
  217.       H16(angleX * 10),
  218.       L16(angleX * 10),
  219.       H16(angleY * 10),
  220.       L16(angleY * 10),
  221.   };
  222.   /* Update the ADV data with the BEACON manufacturing data */
  223.   hci_le_set_advertising_data (sizeof(adv_data), adv_data);
  224. }

  225. int main(void)
  226. {
  227.   uint8_t ret;
  228.   uint16_t        delay = 0;

  229.   /* System Init */
  230.   SystemInit();

  231.   /* Identify BlueNRG-1 platform */
  232.   SdkEvalIdentification();

  233.   /* Init the UART peripheral */
  234.   SdkEvalComUartInit(UART_BAUDRATE);

  235.   /* BlueNRG-1 stack init */
  236.   ret = BlueNRG_Stack_Initialization(&BlueNRG_Stack_Init_params);
  237.   if (ret != BLE_STATUS_SUCCESS) {
  238.     printf("Error in BlueNRG_Stack_Initialization() 0x%02x\r\n", ret);
  239.     while(1);
  240.   }

  241.   /* Init the BlueNRG-1 device */
  242.   Device_Init();

  243. #if ST_USE_OTA_SERVICE_MANAGER_APPLICATION
  244.   /* Initialize the button */
  245.   SdkEvalPushButtonInit(BUTTON_1);
  246. #endif /* ST_USE_OTA_SERVICE_MANAGER_APPLICATION */

  247.   /* Start Beacon Non Connectable Mode*/
  248.   Start_Beaconing();

  249.   printf("BlueNRG-1 BLE Beacon Application (version: %s)\r\n", BLE_BEACON_VERSION_STRING);

  250.   while(1)
  251.   {
  252.     /* BlueNRG-1 stack tick */
  253.     BTLE_StackTick();

  254.         delay = 10000;  
  255.            
  256.         while(delay--);
  257.            
  258.         Update_Beaconing();  
  259.            
  260.     /* Enable Power Save according the Advertising Interval */
  261.     BlueNRG_Sleep(SLEEPMODE_NOTIMER, 0, 0);

  262. #if ST_USE_OTA_SERVICE_MANAGER_APPLICATION
  263.     if (SdkEvalPushButtonGetState(BUTTON_1) == RESET)
  264.     {
  265.       OTA_Jump_To_Service_Manager_Application();
  266.     }
  267. #endif /* ST_USE_OTA_SERVICE_MANAGER_APPLICATION */
  268.   }
  269. }

  270. /****************** BlueNRG-1 Sleep Management Callback ********************************/

  271. SleepModes App_SleepMode_Check(SleepModes sleepMode)
  272. {
  273.   if(SdkEvalComIOTxFifoNotEmpty())
  274.     return SLEEPMODE_CPU_HALT;

  275.   return SLEEPMODE_NOTIMER;
  276. }

  277. /***************************************************************************************/

  278. #ifdef  USE_FULL_ASSERT

  279. /**
  280. * @brief  Reports the name of the source file and the source line number
  281. *         where the assert_param error has occurred.
  282. * @param  file: pointer to the source file name
  283. * @param  line: assert_param error line source number
  284. */
  285. void assert_failed(uint8_t* file, uint32_t line)
  286. {
  287.   /* User can add his own implementation to report the file name and line number,
  288.   ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  289.   /* Infinite loop */
  290.   while (1)
  291.   {
  292.   }
  293. }

  294. #endif

  295. /******************* (C) COPYRIGHT 2015 STMicroelectronics *****END OF FILE****/
  296. /** \endcond
  297. */

回复评论 (4)

生成的hex文件不是在object 吗?难道要移出来才能用?
点赞  2018-1-24 19:32
MDK直接用BlueNRG-1_Flasher_GUI.exe打开下边的文件,编译完成后直接选下载就可以,不需要修改路径和复制文件
Project\BLE_Examples\BLE_Beacon\MDK-ARM\BlueNRG-1\Release\Objects\BLE_Beacon.hex
使用BlueNRG-1_Flasher_GUI.exe的好处是调试低功耗时SWD通常无法使用
虾扯蛋,蛋扯虾,虾扯蛋扯虾
点赞  2018-1-24 20:05
明白了,楼主使用了BlueNRG-1_Navigator下的程序,这个的路径的确是“Firmware\BLE_Examples\BlueNRG-1”下;如版主所说,应该用BlueNRG-1_Flasher_GUI,随便放哪都可以。

我在第一次安装完程序的时候,使用了BlueNRG-1_Navigato。在在后来都是用rBlueNRG-1_Flasher_GUI,因为仿真调试,烧完低功耗固件后,仿真器就连不上了,必须要在rBlueNRG-1_Flasher_GUI下通过串口擦除一次。
点赞  2018-1-25 00:12
引用: littleshrimp 发表于 2018-1-24 20:05
MDK直接用BlueNRG-1_Flasher_GUI.exe打开下边的文件,编译完成后直接选下载就可以,不需要修改路径和复制文 ...

BlueNRG-1_Flasher_GUI我今天试试  现在复制文件挺麻烦的
点赞  2018-1-25 09:49
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复