本帖最后由 gaoyang9992006 于 2021-8-19 21:39 编辑
#申请原创#@21小跑堂近日参加新唐活动,获得了最新蓝牙单片机M032BT系列的开发板首发体验活动。非常开心,感谢新唐。
以下是厂家提供的手册上的图片,跟M032BT开发板的外观看起来几乎一模一样。
刚收到板子没有开发资料,无从下手,默认的程序也没看到有蓝牙信号,以为板子有问题或者手机有问题呢。
今天收到了官方提供的开发资料,烧录后发现还是不行,竟然报错了。
- In Hard Fault Handler
- r0 = 0x0
- r1 = 0xf5
- r2 = 0x40061000
- r3 = 0x3e
- r12 = 0xffffffff
- lr = 0x4a69
- pc = 0x3548
- psr = 0x21000015
我粗略看了一下程序,觉得没毛病啊。而且DEBUG除错操作发现进入了
- /* Initialize RF PHY */
- RF_Init();
产生的错误,而产生这个错误是在BLE.lib的,我就急不可耐的给新唐技术通过官网的渠道发了咨询。
然后才认真的看芯片的技术手册和BLE操作的相关说明文档,然后对照程序一步一步的看,发现了问题所在。新唐的BLE.lib是绝对没问题的。
是我错以为这是新唐针对M032BT的例子了,原来不是,是针对M031BT的,我被下面三行代码迷惑了
- #define _CHIP_M031BT 0
- #define _CHIP_M032BT 1
- #define _CHIP_SELECTION_ _CHIP_M031BT
以为厂家提供的代码已经在第二行设置为1了,没想到第三行才是坑。
需要修改成
- #define _CHIP_M031BT 0
- #define _CHIP_M032BT 1
- #define _CHIP_SELECTION_ _CHIP_M032BT
根据技术手册,蓝牙模块是挂载在SPI0内部端口的
而该端口的初始化如下所示,如果默认是M031BT,则编译对应的M031BT的管脚,而这两个型号的内部管脚是不同的地址。因此需要做如上修改。
修改后,保存,重新编译烧录
串口打印消息:
- ----------------------------------
- BLE demo: LED control start...
- ----------------------------------
- BLE device address from UID: c0:00:b5:a1:47:09
- Advertising...
这个时候打开手机蓝牙,发现已经可以看到我使用的这个工程的名字了:Nuvoton_LED
安装新唐提供的APK软件,扫描,然后找到上面的蓝牙信号名字,单击连接,然后就可以控制LED的开关了。
在连接和开关断开的时候串口还打印出了相关信息
- Disconnected, ID:0, Reason:0x13
- Advertising...
- Status=0, Connected to 78:77:f6:7d:a1:08
- Connection updated
- Status: 0, Interval: 6, Latency: 0, Supervision Timeout: 500
- Connection updated
- Status: 0, Interval: 39, Latency: 0, Supervision Timeout: 500
结合厂家提供的测试例程,可以看出通过蓝牙进行数据收发非常简单,这大大降低了基于蓝牙的单片机应用开发难度,非常完美的一款具备蓝牙收发功能的单片机。
奉上该例程的main.c以供欣赏。
- /**************************************************************************//**
- * [url=home.php?mod=space&uid=288409]@file[/url] main.c
- * [url=home.php?mod=space&uid=895143]@version[/url] V0.9
- * $Revision: 01 $
- * @brief
- * Demonstrate BLE operation.
- * Includes the basic initialization and the loop for kernel(BLE) operations.
- * @note
- *
- ******************************************************************************/
- #include <stdio.h>
- #include "rf_phy.h"
- #include "porting_flash.h"
- #include "porting_misc.h"
- #include "porting_spi.h"
- #include "mcu_definition.h"
- #pragma push
- //#pragma Otime
- #pragma Ospace
- /*!
- \brief Initial necessary peripheral on MCU.
- */
- void SYS_Init(void)
- {
- int8_t irqno;
- /* Unlock protected registers */
- SYS_UnlockReg();
- /*---------------------------------------------------------------------------------------------------------*/
- /* Init System Clock */
- /*---------------------------------------------------------------------------------------------------------*/
- #if (_USE_MCU_CLK_==MCU_CLK_SOURCE_HXT) //HXT
- GPIO_SetMode(PF, (BIT2 | BIT3), GPIO_MODE_INPUT);
- CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk | CLK_PWRCTL_HIRCEN_Msk);
- CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk | CLK_STATUS_HIRCSTB_Msk);
- #else //HIRC
- CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);
- CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);
- #endif
- /* Set both PCLK0 and PCLK1 as HCLK/PCLK_DIV */
- #if (PCLK_DIV==4)
- CLK->PCLKDIV = CLK_PCLKDIV_APB0DIV_DIV4 | CLK_PCLKDIV_APB1DIV_DIV4;
- #elif (PCLK_DIV==2)
- CLK->PCLKDIV = CLK_PCLKDIV_APB0DIV_DIV2 | CLK_PCLKDIV_APB1DIV_DIV2; //48/2=24MHz
- #elif (PCLK_DIV==1)
- CLK->PCLKDIV = CLK_PCLKDIV_APB0DIV_DIV1 | CLK_PCLKDIV_APB1DIV_DIV1;
- #endif //(PCLK_DIV==4)
- //debug print use UART0
- /* Select HIRC as the clock source of UART0 */
- CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1));
- /* Enable UART peripheral clock */
- CLK_EnableModuleClock(UART0_MODULE);
- /*---------------------------------------------------------------------------------------------------------*/
- /* Init I/O Multi-function */
- /*---------------------------------------------------------------------------------------------------------*/
- /* Set PA multi-function pins for UART0 RXD=PA.0 and TXD=PA.1 */
- SYS->GPA_MFPL = (SYS->GPA_MFPL & ~(SYS_GPA_MFPL_PA0MFP_Msk | SYS_GPA_MFPL_PA1MFP_Msk)) |
- (SYS_GPA_MFPL_PA0MFP_UART0_RXD | SYS_GPA_MFPL_PA1MFP_UART0_TXD);
- /* Set only BLE interrupt with the highest priority to make sure RF can handle event in time */
- for (irqno = BOD_IRQn; irqno <= RTC_IRQn; irqno++)
- {
- NVIC_SetPriority((IRQn_Type)irqno, 1);
- }
- #if (_CHIP_SELECTION_ == _CHIP_M031BT)
- NVIC_SetPriority(GPIO_PCPDPEPF_IRQn, 0);
- #elif (_CHIP_SELECTION_ == _CHIP_M032BT)
- NVIC_SetPriority(EINT135_IRQn, 0);
- #endif
- /* Update System Core Clock */
- /* User can use SystemCoreClockUpdate() to calculate SystemCoreClock and CyclesPerUs automatically. */
- SystemCoreClockUpdate();
- /* Lock protected registers */
- SYS_LockReg();
- }
- void RF_Open(void)
- {
- /* Wait RF PHY stable */
- CLK_SysTickDelay(200000); // MUST wait for stable voltage( >= 1.8V) after power-up
- /* Initialize GPIO reset pin */
- MCU_GpioResetInit();
- /* Do Gpio Reset */
- MCU_GpioReset();
- CLK_SysTickDelay(50000);
- /* SPI IO remapping */
- RF_SpiIoMapping();
- /* initial SPI PDMA */
- SPI_PDMA_Init();
- /* Initialize RF PHY */
- RF_Init();
- }
- /*!
- \brief main loop for initialization and BLE kernel
- */
- int main(void)
- {
- extern void BleApp_Main(void);
- extern void BleApp_Init(void);
- extern BleStackStatus Set_BleAddr(void);
- /* Init System, IP clock and multi-function I/O. */
- SYS_Init();
- /* Open UART0 for debug */
- UART_Open(UART0, 115200);
- /* Enable the BLE RF PHY */
- RF_Open();
- printf("----------------------------------\n");
- printf(" BLE demo: LED control start...\n");
- printf("----------------------------------\n");
- /* Set BLE device address */
- Set_BleAddr();
- /* Initial BLE App */
- BleApp_Init();
- while (1)
- {
- /* Run BLE kernel, the task priority is LL > Host */
- if (Ble_Kernel_Root() == BLESTACK_STATUS_FREE)
- {
- BleApp_Main();
- /* System enter Power Down mode & wait interrupt event. */
- System_PowerDown();
- }
- }
- }
- #pragma pop
|