路径为UWB\NCJ29D5\NCJ29D5_CAS_Examples_v1.4\onic\BlinkyLed\toolsupport\keil
例程怎么来的可以看看上一篇NXP UWB NCJ29D5开发(一)环境搭建
1、
//系统选择外部晶振,时钟频率为55.2Mhz
phscaAppHal_Init(PHSCA_APPHAL_XO_CLOCK_SOURCE_55P2Mhz);
在文档NCJ29D5_AppHAL.chm里可以看到,路径为:UWB\NCJ29D5\NCJ29D5 SDK v11.0\Documentation
2、
/*--- Init baseband to enable the LDOs to power VDD_GLOB for LEDs ---*//** 55.2M+xoClkDivderSet:0+hwAcceleratorCfg:0(software encoder) -->rfpllRxTime:75 rfpllTxTime:180*/phIscaBaseband_Config_t gphscaRciAppConfig_Static ={.Control_b.xoClockSel = PHSCA_BB_XO_CLOCK_SOURCE_55P2Mhz, //55.2M.Control_b.xoClkDivderSet = 0u,.Control_b.hwAcceleratorCfg = 0u,};uint16_t rfpllRxTxTime[2] = {0u, 0u};/* Update rfpll tx and rx rampup time in RciConfiguration */phscaAppHal_GetRfpllRxTxRampupTime(gphscaRciAppConfig_Static.Control_b.hwAcceleratorCfg,gphscaRciAppConfig_Static.Control_b.xoClkDivderSet,&rfpllRxTxTime[0u], &rfpllRxTxTime[1u]);gphscaRciAppConfig_Static.Control_b.rfpllRxTime = rfpllRxTxTime[0u];gphscaRciAppConfig_Static.Control_b.rfpllTxTime = rfpllRxTxTime[1u];/* Init Baseband */phscaBaseband_Init(gphscaRciAppConfig_Static);
这个是baseband的初始化过程,参数的设置目的可以在文档NCJ29D5_Baseband.chm可以看到,路径为:UWB\NCJ29D5\NCJ29D5 SDK v11.0\Documentation
很显然,例程的配置是第一行,从而让rfpllRxTime=75 rfpllTxTime=180
3、
/* Turn on crystal *//**When set to 1, the crystal oscillator is selected as input of the output buffer driving the XTAL_OUT pin.*If set to 0, the XTAL_OUT configuration is not altered*/phscaAppHal_XoOn(false);/* Init UART */phscaLinFlex_UartInit(PHSCA_LINFLEX_UART_BAUD_115200);
这两个从字面意思就可以看出,禁能XTAL_OUT和LINFLEX_UART的波特率是115200
4、
/*--- Init P13 (CS2_N) as GPIO ---*/u_port->ALTF_b.CS2_N = 0u; //CS2_N as GPIOu_port->DIR_b.CS2_N = 1u; //0:input 1:outputu_port->PUD_b.CS2_N_PU = 1u; //0:disabel 1:enableu_port->PUD_b.CS2_N_PD = 0u; //0:disabel 1:enableu_port->OUT_b.CS2_N = 1u; //ouput value
CS2_N就是P13脚的一个复用功能之一,这样就把P13配置为普通GPIO,输出模式,初始输出高电平
相关配置在文档Ranger-4 user manual里,注释是简写
这里提一下开发需要的文档如下
5、
/*--- Init timer GPT0 ---*/uint32_t periodInUs = 500000u;u_timers->TIMERS_TIMER0_PRESCALER_REG_b.TIMER0_PRESCALING_VALUE = 1u; //定时器时钟预分频系数u_timers->TIMERS_MODE_REG = 0u; //0:single shot 1:free runningu_timers->TIMERS_INT_SET_ENABLE_REG = 1u; //0:no effect 1:enable INTu_timers->TIMERS_MODE_REG = 1u;
这里很奇怪的一点是TIMERS_MODE_REG 配置了两次,把u_timers->TIMERS_MODE_REG = 0u;注释掉也不影响。
同样相关配置在文档Ranger-4 user manual里
6、
uint32_t halfPeriod = periodInUs / 2000u; //250uint8_t preScaler = 1u;uint32_t sysCoreClock = phscaAppHal_GetSystemCoreClock() / 1000u; //55200uint32_t timerRegCount = halfPeriod * (sysCoreClock / preScaler); //13800000, 55200000 / 13800000 = 4,即1秒可以记4次,记一次需要250MS,即250MS进一次中断u_timers->TIMERS_TIMER0_TIMEOUT_REG = timerRegCount; //计数值
如果想要500MS,简单做法可以在让timerRegCount * 2
7、
NVIC_EnableIRQ(IRQ19_Timer0_IRQn);
定时器中断向量在startup_NCJ29D5_user.s里面可以看到
8、
__attribute__ ((interrupt ("IRQ")))
void IRQ19_Timer0(void)
{u_port->OUT_b.CS2_N = !u_port->OUT_b.CS2_N;u_timers->TIMERS_INT_CLR_STATUS_REG = 1u; //0:no effect 1:clear timer0 timeout interrupt
}
到此这个例程结束。
没找到相关文档前,看这些代码还是有点懵的,有文档后就清晰了。
全部代码
/*(c) NXP B.V. 2020. All rights reserved.Disclaimer1. The NXP Software/Source Code is provided to Licensee "AS IS" without anywarranties of any kind. NXP makes no warranties to Licensee and shall notindemnify Licensee or hold it harmless for any reason related to the NXPSoftware/Source Code or otherwise be liable to the NXP customer. The NXPcustomer acknowledges and agrees that the NXP Software/Source Code isprovided AS-IS and accepts all risks of utilizing the NXP Software underthe conditions set forth according to this disclaimer.2. NXP EXPRESSLY DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING,BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESSFOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT OF INTELLECTUAL PROPERTYRIGHTS. NXP SHALL HAVE NO LIABILITY TO THE NXP CUSTOMER, OR ITSSUBSIDIARIES, AFFILIATES, OR ANY OTHER THIRD PARTY FOR ANY DAMAGES,INCLUDING WITHOUT LIMITATION, DAMAGES RESULTING OR ALLEGED TO HAVERESULTED FROM ANY DEFECT, ERROR OR OMISSION IN THE NXP SOFTWARE/SOURCECODE, THIRD PARTY APPLICATION SOFTWARE AND/OR DOCUMENTATION, OR AS ARESULT OF ANY INFRINGEMENT OF ANY INTELLECTUAL PROPERTY RIGHT OF ANYTHIRD PARTY. IN NO EVENT SHALL NXP BE LIABLE FOR ANY INCIDENTAL,INDIRECT, SPECIAL, EXEMPLARY, PUNITIVE, OR CONSEQUENTIAL DAMAGES(INCLUDING LOST PROFITS) SUFFERED BY NXP CUSTOMER OR ITS SUBSIDIARIES,AFFILIATES, OR ANY OTHER THIRD PARTY ARISING OUT OF OR RELATED TO THE NXPSOFTWARE/SOURCE CODE EVEN IF NXP HAS BEEN ADVISED OF THE POSSIBILITY OFSUCH DAMAGES.3. NXP reserves the right to make changes to the NXP Software/Sourcecode anytime, also without informing customer.4. Licensee agrees to indemnify and hold harmless NXP and its affiliatedcompanies from and against any claims, suits, losses, damages,liabilities, costs and expenses (including reasonable attorney's fees)resulting from Licensee's and/or Licensee customer's/licensee's use of theNXP Software/Source Code.
*//*** @file* @brief NCJ29D5 Blinky LED App*/
#include <stdbool.h>
#include <stdint.h>// Include NCJ29D5_user.h (rename SPI controller's "EOF" field to "SPI_EOF" to avoid
// clash with EOF macro from stdio.h)
#define EOF SPI_EOF
#include <NCJ29D5_user.h>
#undef EOF#include <stdio.h>
#include <phIscaBaseband.h>
#include <phIscaUtilities.h>
#include <phIscaAppHal.h>
#include <phscaTools.h>
#include <phscaLinFlex.h>int main(void)
{//系统选择外部晶振,时钟频率为55.2MhzphscaAppHal_Init(PHSCA_APPHAL_XO_CLOCK_SOURCE_55P2Mhz);/* Stop Watchdog timer for debugging purpose */phscaUtilities_WdtResetWithMS(0u);/*--- Init baseband to enable the LDOs to power VDD_GLOB for LEDs ---*//** 55.2M+xoClkDivderSet:0+hwAcceleratorCfg:0(software encoder) -->rfpllRxTime:75 rfpllTxTime:180*/phIscaBaseband_Config_t gphscaRciAppConfig_Static ={.Control_b.xoClockSel = PHSCA_BB_XO_CLOCK_SOURCE_55P2Mhz, //55.2M.Control_b.xoClkDivderSet = 0u,.Control_b.hwAcceleratorCfg = 0u,};uint16_t rfpllRxTxTime[2] = {0u, 0u};/* Update rfpll tx and rx rampup time in RciConfiguration */phscaAppHal_GetRfpllRxTxRampupTime(gphscaRciAppConfig_Static.Control_b.hwAcceleratorCfg,gphscaRciAppConfig_Static.Control_b.xoClkDivderSet,&rfpllRxTxTime[0u], &rfpllRxTxTime[1u]);gphscaRciAppConfig_Static.Control_b.rfpllRxTime = rfpllRxTxTime[0u];gphscaRciAppConfig_Static.Control_b.rfpllTxTime = rfpllRxTxTime[1u];/* Init Baseband */phscaBaseband_Init(gphscaRciAppConfig_Static);/* Turn on crystal *//**When set to 1, the crystal oscillator is selected as input of the output buffer driving the XTAL_OUT pin.*If set to 0, the XTAL_OUT configuration is not altered*/phscaAppHal_XoOn(false);/* Init UART */phscaLinFlex_UartInit(PHSCA_LINFLEX_UART_BAUD_115200);printf("\r\n--- Blinky LED ---\n\r");/*--- Init P13 (CS2_N) as GPIO ---*/u_port->ALTF_b.CS2_N = 0u; //CS2_N as GPIOu_port->DIR_b.CS2_N = 1u; //0:input 1:outputu_port->PUD_b.CS2_N_PU = 1u; //0:disabel 1:enableu_port->PUD_b.CS2_N_PD = 0u; //0:disabel 1:enableu_port->OUT_b.CS2_N = 1u; //ouput value/*--- Init timer GPT0 ---*/uint32_t periodInUs = 500000u;u_timers->TIMERS_TIMER0_PRESCALER_REG_b.TIMER0_PRESCALING_VALUE = 1u; //定时器时钟预分频系数//u_timers->TIMERS_MODE_REG = 0u; //0:single shot 1:free runningu_timers->TIMERS_INT_SET_ENABLE_REG = 1u; //0:no effect 1:enable INTu_timers->TIMERS_MODE_REG = 1u;uint32_t halfPeriod = periodInUs / 2000u; //250uint8_t preScaler = 1u;uint32_t sysCoreClock = phscaAppHal_GetSystemCoreClock() / 1000u; //55200uint32_t timerRegCount = halfPeriod * (sysCoreClock / preScaler); //13800000, 55200000 / 13800000 = 4,即1秒可以记4次,记一次需要250MS,即250MS进一次中断u_timers->TIMERS_TIMER0_TIMEOUT_REG = timerRegCount; //计数值printf("Timer Reg Counter = %i\n\r",timerRegCount);NVIC_EnableIRQ(IRQ19_Timer0_IRQn);while(1){}
}__attribute__ ((interrupt ("IRQ")))
void IRQ19_Timer0(void)
{u_port->OUT_b.CS2_N = !u_port->OUT_b.CS2_N;u_timers->TIMERS_INT_CLR_STATUS_REG = 1u; //0:no effect 1:clear timer0 timeout interrupt
}