void InitializeNRF24L01() { // NRF24L01 goes to RX mode by default; NRF24L01_Init(DEFAULT_CHANNEL, DEFAULT_PAYLOAD_SIZE); NRF24L01_SetRF(NRF24L01_DataRate_2M, NRF24L01_OutputPower_M18dBm); NRF24L01_SetMyAddress(MyAddress); NRF24L01_SetTxAddress(TxAddress); }
/** * @brief Initializes the nRF24L01 * @param Device: The device to use * @retval ERROR: If something went wrong * @retval SUCCESS: If everything went OK */ ErrorStatus NRF24L01_Init(NRF24L01_Device* Device) { /* * Create the binary semaphores: * The semaphore is created in the 'empty' state, meaning * the semaphore must first be given before it can be taken (obtained) * using the xSemaphoreTake() function. */ Device->xTxSemaphore = xSemaphoreCreateBinary(); Device->xDataAvailableSemaphore = xSemaphoreCreateBinary(); xSemaphoreGive(Device->xTxSemaphore); xSemaphoreGive(Device->xDataAvailableSemaphore); /* Take the semaphore because no data is available yet */ xSemaphoreTake(Device->xDataAvailableSemaphore, portMAX_DELAY); /* Initialize RX Buffers */ for (uint32_t i = 0; i < 6; i++) { CIRC_BUFFER_Init(&Device->RxPipeBuffer[i]); } /* Initialize GPIOs */ GPIO_InitTypeDef GPIO_InitStructure; /* Initialize CE (Chip Enable) pin */ if (Device->CE_GPIO == GPIOA) RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); else if (Device->CE_GPIO == GPIOB) RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Pin = Device->CE_Pin; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(Device->CE_GPIO, &GPIO_InitStructure); DISABLE_DEVICE(Device); /* Initialize CSN (Chip select) pin */ if (Device->CSN_GPIO == GPIOA) RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); else if (Device->CSN_GPIO == GPIOB) RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Pin = Device->CSN_Pin; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(Device->CSN_GPIO, &GPIO_InitStructure); DESELECT_DEVICE(Device); /* Initialize IRQ (Interrupt) pin */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); if (Device->IRQ_GPIO == GPIOA) RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); else if (Device->IRQ_GPIO == GPIOB) RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Pin = Device->IRQ_Pin; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(Device->IRQ_GPIO, &GPIO_InitStructure); /* Initialize interrupt */ /* Connect EXTIx Line to IRQ pin */ GPIO_EXTILineConfig(Device->IRQ_GPIO_PortSource, Device->IRQ_GPIO_PinSource); EXTI_InitTypeDef EXTI_InitStructure; EXTI_InitStructure.EXTI_Line = Device->IRQ_EXTI_Line; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; EXTI_Init(&EXTI_InitStructure); /* Enable EXTIx Interrupt. The SPIx interrupt has to be higher than this so set EXTIx to the lowest priority*/ NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = Device->IRQ_NVIC_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = configLIBRARY_LOWEST_INTERRUPT_PRIORITY; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Initialize spi peripheral */ SPI_InitTypeDef SPI_InitStructure; SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; /* 72 MHz/8 = 9 MHz, Datasheet page 53 says max 10 MHz (with Cload = 5 pF) */ SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; /* See datasheet page 50 */ SPI_InitWithStructure(Device->SPIDevice, &SPI_InitStructure); /* Wait 100ms for Power-On reset, see page 20 in datasheet */ vTaskDelay(100 / portTICK_PERIOD_MS); /* Set RF Channel */ NRF24L01_SetRFChannel(Device, Device->RfChannel); /* Check that the RF channel was set */ if (NRF24L01_GetRFChannel(Device) != Device->RfChannel) goto error; /* Set payload size for the pipes, use same for all, and enable the pipes */ for (uint32_t pipe = 0; pipe < 6; pipe++) { NRF24L01_SetPayloadSizeForPipe(Device, 32, pipe); NRF24L01_EnablePipe(Device, pipe); } /* Set the TX address */ NRF24L01_SetTxAddress(Device, Device->TxAddress); /* Set RX address for all pipes */ NRF24L01_SetRxAddressForPipe(Device, Device->RxAddress0, 0); NRF24L01_SetRxAddressForPipe(Device, Device->RxAddress1, 1); NRF24L01_SetRxAddressForPipe(Device, Device->RxAddress2, 2); NRF24L01_SetRxAddressForPipe(Device, Device->RxAddress3, 3); NRF24L01_SetRxAddressForPipe(Device, Device->RxAddress4, 4); NRF24L01_SetRxAddressForPipe(Device, Device->RxAddress5, 5); /* Flush the buffers to get rid of old data and reset the flags in the STATUS register */ NRF24L01_FlushTxBuffer(Device); NRF24L01_FlushRxBuffer(Device); NRF24L01_ResetAllFlags(Device); /* Set the address width */ NRF24L01_SetAddressWidth(Device); /* Power up the device i RX mode to start listening for packets */ NRF24L01_PowerUpInRxMode(Device); return SUCCESS; error: return ERROR; }
void retranslate(void) { /* Receiver address */ uint8_t TxAddress[] = { 0xE7, 0xE7, 0xE7, 0xE7, 0xE7 }; /* My address */ uint8_t MyAddress[] = { 0x7E, 0x7E, 0x7E, 0x7E, 0x7E }; uint8_t dataIn[32]; NRF24L01_Transmit_Status_t transmissionStatus; /* Initialize NRF24L01+ on channel 15 and 32bytes of payload */ /* By default 2Mbps data rate and 0dBm output power */ /* NRF24L01 goes to RX mode by default */ NRF24L01_Init(15, 32); /* Set RF settings, Data rate to 2Mbps, Output power to -18dBm */ NRF24L01_SetRF(NRF24L01_DataRate_2M, NRF24L01_OutputPower_M18dBm); /* Set my address, 5 bytes */ NRF24L01_SetMyAddress(MyAddress); /* Set TX address, 5 bytes */ NRF24L01_SetTxAddress(TxAddress); LED1_ON; delay_(3600000); LED1_OFF; while (1) { /* If data is ready on NRF24L01+ */ if (NRF24L01_DataReady()) { /* Get data from NRF24L01+ */ NRF24L01_GetData(dataIn); delay_(36000); /* Send it back, automatically goes to TX mode */ NRF24L01_Transmit(dataIn); /* Start send */ LED1_ON;; delay_(360000); /* Wait for data to be sent */ do { transmissionStatus = NRF24L01_GetTransmissionStatus(); } while (transmissionStatus == NRF24L01_Transmit_Status_Sending); /* Send done */ LED1_OFF; /* Go back to RX Mode */ NRF24L01_PowerUpRx(); } } }