/* 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));
/*---------------------------------------------------------------------------------------------------------*/
/* 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
/* Do Gpio Reset */
MCU_GpioReset();
CLK_SysTickDelay(50000);
/* SPI IO remapping */
RF_SpiIoMapping();
/* initial SPI PDMA */
SPI_PDMA_Init();
/* Initialize RF PHY */
RF_Init();
}
/*!
rief 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("----------------------------------
");
printf(" BLE demo: LED control start...
");
printf("----------------------------------
");
/* 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