本帖最后由 北方西门吹雪 于 2021-9-30 16:42 编辑
1. 概述
蓝牙控制波形发生器实现通过蓝牙控制波形选择,使用低功耗蓝牙 RISC-V MCU CH573 评估板实现方波发生器的项目。
2. 使用的硬件
低功耗蓝牙 RISC-V MCU CH573 评估板
智能手机
MounRiver Studio(MRS) IDE
WCHISTOOL进行固件下载
3. 开发过程
3.1 创建项目,基于BLE peripheral项目创建,启动MounRiver IDE
3.2 主文件是一个loop(), 具体的实现使用WCH的私有TMS实时系统实现,代码如下
- #include "CONFIG.h"
- #include "CH57x_common.h"
- #include "HAL.h"
- #include "gattprofile.h"
- #include "peripheral.h"
- /*********************************************************************
- * GLOBAL TYPEDEFS
- */
- __attribute__((aligned(4))) u32 MEM_BUF[BLE_MEMHEAP_SIZE/4];
- #if (defined (BLE_MAC)) && (BLE_MAC == TRUE)
- u8C MacAddr[6] = {0x84,0xC2,0xE4,0x03,0x02,0x02};
- #endif
- /*******************************************************************************
- * Function Name : Main_Circulation
- * Description : 主循环
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- __attribute__((section(".highcode")))
- void Main_Circulation()
- {
- while(1){
- TMOS_SystemProcess( );
- }
- }
- /*******************************************************************************
- * Function Name : main
- * Description : 主函数
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- int main( void )
- {
- #if (defined (DCDC_ENABLE)) && (DCDC_ENABLE == TRUE)
- PWR_DCDCCfg( ENABLE );
- #endif
- SetSysClock( CLK_SOURCE_PLL_60MHz );
- #if (defined (HAL_SLEEP)) && (HAL_SLEEP == TRUE)
- GPIOA_ModeCfg( GPIO_Pin_All, GPIO_ModeIN_PU );
- GPIOB_ModeCfg( GPIO_Pin_All, GPIO_ModeIN_PU );
- #endif
- #ifdef DEBUG
- GPIOA_SetBits(bTXD1);
- GPIOA_ModeCfg(bTXD1, GPIO_ModeOut_PP_5mA);
- UART1_DefInit( );
- #endif
- PRINT("%s\n",VER_LIB);
- CH57X_BLEInit( );
- HAL_Init( );
- GAPRole_PeripheralInit( );
- Peripheral_Init( );
- Main_Circulation();
- }
3.3 主要代码在peripheral.c中实现。
示例代码是主循环如下,更改实现PWM功能的入口就在这个代码中,随后修改
- uint16 Peripheral_ProcessEvent( uint8 task_id, uint16 events )
- {
- // VOID task_id; // TMOS required parameter that isn't used in this function
- if ( events & SYS_EVENT_MSG ){
- uint8 *pMsg;
- if ( (pMsg = tmos_msg_receive( Peripheral_TaskID )) != NULL ){
- Peripheral_ProcessTMOSMsg( (tmos_event_hdr_t *)pMsg );
- // Release the TMOS message
- tmos_msg_deallocate( pMsg );
- }
- // return unprocessed events
- return (events ^ SYS_EVENT_MSG);
- }
- if ( events & SBP_START_DEVICE_EVT ){
- // Start the Device
- GAPRole_PeripheralStartDevice( Peripheral_TaskID, &Peripheral_BondMgrCBs, &Peripheral_PeripheralCBs );
- return ( events ^ SBP_START_DEVICE_EVT );
- }
- if ( events & SBP_PERIODIC_EVT )
- {
- // Restart timer
- if ( SBP_PERIODIC_EVT_PERIOD ){
- tmos_start_task( Peripheral_TaskID, SBP_PERIODIC_EVT, SBP_PERIODIC_EVT_PERIOD );
- }
- // Perform periodic application task
- performPeriodicTask();
- return (events ^ SBP_PERIODIC_EVT);
- }
-
- if ( events & SBP_PARAM_UPDATE_EVT )
- {
- // Send connect param update request
- GAPRole_PeripheralConnParamUpdateReq( peripheralConnList.connHandle,
- DEFAULT_DESIRED_MIN_CONN_INTERVAL,
- DEFAULT_DESIRED_MAX_CONN_INTERVAL,
- DEFAULT_DESIRED_SLAVE_LATENCY,
- DEFAULT_DESIRED_CONN_TIMEOUT,
- Peripheral_TaskID);
-
- return (events ^ SBP_PARAM_UPDATE_EVT);
- }
-
- if ( events & SBP_READ_RSSI_EVT )
- {
- GAPRole_ReadRssiCmd(peripheralConnList.connHandle);
- tmos_start_task( Peripheral_TaskID, SBP_READ_RSSI_EVT, SBP_READ_RSSI_EVT_PERIOD );
- return (events ^ SBP_READ_RSSI_EVT);
- }
- // Discard unknown events
- return 0;
- }
编译并下载
编译成功
按着download上电,才能显示USB下载连接成功
点击下载就成功
4. 手机app连接
成功连接到手机,并可以进行数据交换和控制。其中手机是center,CH573为外围peripheral。
|