/******************************************************************************
* [url=home.php?mod=space&uid=288409]@file[/url] main.c
* [url=home.php?mod=space&uid=895143]@version[/url] V1.0
* $Revision: 2 $
* $Date: 8/20/19 11:44a $
* [url=home.php?mod=space&uid=247401]@brief[/url] RF24L01 Sample Code
*
* @note
* Copyright (C) 2019 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/
#include <stdio.h>
#include "M051Series.h"
#include "NRF_24L01.h"
/*---------------------------------------------------------------------------*/
/* Define */
/*---------------------------------------------------------------------------*/
#define TEST_COUNT 64
/*---------------------------------------------------------------------------*/
/* Global variables */
/*---------------------------------------------------------------------------*/
uint32_t g_au32SourceData[TEST_COUNT];
uint32_t g_au32DestinationData[TEST_COUNT];
uint32_t u32TestRevCount = 0;
uint8_t u8RevFlag = 0;
void delay_ms(int32_t ms)
{
int32_t i;
for (i = 0; i < ms; i++)
{
CLK_SysTickDelay(1000);/* SysTick to generate the delay time and the UNIT is in us. */
}
}
void TMR0_IRQHandler(void)
{
/* Clear Timer0 time-out interrupt flag */
TIMER_ClearIntFlag(TIMER0);
if (u8RevFlag == 1)
{
u8RevFlag = 0;
}
else
{
rf_ChangeCH(1);
}
}
void EINT0_IRQHandler(void)
{
uint8_t u8Status = 0;
/* For P3.2, clear the INT flag */
GPIO_CLR_INT_FLAG(P3, BIT2);
u8Status = SPI_Read(RF_STATUS);
if (u8Status & STA_MARK_RX)
{
SPI_Read_Buf(RD_RX_PLOAD, au8rx_buf, TX_PLOAD_WIDTH);
if (au8rx_buf[0] == 0xAA)
{
u32TestRevCount++;
LED = ~LED;
}
au8rx_buf[0] = 0;
u8RevFlag = 1;
}
SPI_RW_Reg(WRITE_REG + RF_STATUS, 0xff);
}
void SYS_Init(void)
{
/* Unlock protected registers */
SYS_UnlockReg();
/*---------------------------------------------------------------------------------------------------------*/
/* Init System Clock */
/*---------------------------------------------------------------------------------------------------------*/
/* Enable external 12MHz XTAL */
CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);
/* Waiting for clock ready */
CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);
/* Switch HCLK clock source to HXT and HCLK source divide 1 */
CLK_SetCoreClock(FREQ_50MHZ);
/* Select HXT as the clock source of UART0 */
CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));
/* Select HCLK as the clock source of SPI0 */
CLK_SetModuleClock(SPI0_MODULE, CLK_CLKSEL1_SPI0_S_HCLK, MODULE_NoMsk);
/* Enable UART peripheral clock */
CLK_EnableModuleClock(UART0_MODULE);
/* Enable SPI0 peripheral clock */
CLK_EnableModuleClock(SPI0_MODULE);
/* Select HXT as the clock source of TIME0 */
CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0_S_HXT, 0);
/* Enable TIME peripheral clock */
CLK_EnableModuleClock(TMR0_MODULE);
/*---------------------------------------------------------------------------------------------------------*/
/* Init I/O Multi-function */
/*---------------------------------------------------------------------------------------------------------*/
/* Set P3 multi-function pins for UART0 RXD and TXD */
SYS->P3_MFP = SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0;
/* Setup SPI0 multi-function pins */
SYS->P1_MFP = SYS_MFP_P14_SPISS0 | SYS_MFP_P15_MOSI_0 | SYS_MFP_P16_MISO_0 | SYS_MFP_P17_SPICLK0;
/* Set P3 multi-function pins for UART0 RXD, TXD, EINT0 and EINT1 */
SYS->P3_MFP &= ~(SYS_MFP_P32_Msk);
SYS->P3_MFP |= (SYS_MFP_P32_INT0);
/* Lock protected registers */
SYS_LockReg();
/* Update System Core Clock */
/* User can use SystemCoreClockUpdate() to calculate SystemCoreClock and CycylesPerUs automatically. */
SystemCoreClockUpdate();
}
void SPI_Init(void)
{
/* Configure as a master, clock idle low, 32-bit transaction,
* drive output on falling clock edge and latch input on rising edge.
*/
/* Set IP clock divider. SPI clock rate = 2MHz */
SPI_Open(SPI0, SPI_MASTER, SPI_MODE_0, 8, 1000000);
/* Enable the automatic hardware slave select function. Select the SS pin and configure as low-active. */
SPI_EnableAutoSS(SPI0, SPI_SS, SPI_SS_ACTIVE_LOW);
SPI_DisableAutoSS(SPI0);
}
int main(void)
{
/* Init System, IP clock and multi-function I/O.
* In the end of SYS_Init() will issue SYS_LockReg()
* to lock protected register. If user want to write
* protected register, please issue SYS_UnlockReg()
* to unlock protected register if necessary.
*/
SYS_Init();
/* Configure UART0: 115200, 8-bit word, no parity bit, 1 stop bit. */
UART_Open(UART0, 115200);
/* Init SPI */
SPI_Init();
GPIO_SetMode(P0, BIT6, GPIO_PMD_OUTPUT);
GPIO_SetMode(P3, BIT6, GPIO_PMD_OUTPUT);
/* Configure P3.2 as EINT0 pin and enable interrupt by falling edge trigger */
GPIO_SetMode(P3, BIT2, GPIO_PMD_INPUT);
/* Enable interrupt de-bounce function and select de-bounce sampling cycle time is 1024 * 10 KHz clock */
GPIO_SET_DEBOUNCE_TIME(GPIO_DBCLKSRC_HCLK, GPIO_DBCLKSEL_32);
GPIO_ENABLE_DEBOUNCE(P3, BIT2);
GPIO_EnableEINT0(P3, 2, GPIO_INT_FALLING);
NVIC_EnableIRQ(EINT0_IRQn);
/* Open Timer0 frequency to 0.5 Hz in periodic mode, and enable interrupt */
TIMER_Open(TIMER0, TIMER_PERIODIC_MODE, 20); //50ms
TIMER_EnableInt(TIMER0);
NVIC_EnableIRQ(TMR0_IRQn);
TIMER_Start(TIMER0);
delay_ms(100);
init_nrf24l01_io();
delay_ms(500);
printf("Receive READY!\r\n");
ifnnrf_CLERN_ALL();
ifnnrf_rx_mode();
u8sta = SPI_Read(RF_SETUP);
u8sta = SPI_Read(RF_CH);
u8sta = SPI_Read(EN_AA);
u8sta = SPI_Read(RF_SETUP);
ifnnrf_rx_mode();
while (1)
{
u8sta = 0;
delay_ms(1000);
printf("u8CurCH=%d,u32TestRevCount=%d\n\r", u8CurCH, u32TestRevCount);
}
}
/*** (C) COPYRIGHT 2019 Nuvoton Technology Corp. ***/