路徑為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
}