본문 바로가기
임베디드.일렉트로닉스/nRF52

nRF52. Radio Rx 구현.

by i.got.it 2019. 1. 16.



개요

- 타겟보드 nRF52840 동글 에서  Radio Rx  구현 . 


- nRF5 SDK 에서 예제로 제공되는 /example/peripheral/radio/receiver (프로젝트 설명문) 의 소스코드를 참조하여 구현하면 쉽다. 1개의 송신기에서 전송한것을 8개의 수신기에서 동시 수신상황 확인한다.  





타겟보드 : nRF52840 Dongle






프로젝트 IDE 환경 

SES(SEGGER Embedded Studio) 에서 프로젝트 템플릿 기반. 상세보기 -> https://igotit.tistory.com/2042



코드 

수신측

#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
#include <stdio.h>
#include "nrf_drv_systick.h"
#include "nrf_delay.h"
#include "boards.h"
#include "nrf_drv_clock.h"
#include "radio_config.h"
static uint32_t packet; /**< Packet to transmit. */
uint32_t read_packet()
{
uint32_t result = 0;
NRF_RADIO->EVENTS_READY = 0U;
NRF_RADIO->TASKS_RXEN = 1U; // Enable radio and wait for ready
while (NRF_RADIO->EVENTS_READY == 0U) ;
NRF_RADIO->EVENTS_END = 0U;
NRF_RADIO->TASKS_START = 1U; // Start listening and wait for address received event
while (NRF_RADIO->EVENTS_END == 0U) ; // Wait for end of packet or buttons state changed
if (NRF_RADIO->CRCSTATUS == 1U) { result = packet; }
NRF_RADIO->EVENTS_DISABLED = 0U;
NRF_RADIO->TASKS_DISABLE = 1U; // Disable radio
while (NRF_RADIO->EVENTS_DISABLED == 0U) ;
return result;
}
void LED_Control(uint8_t led_idx)
{
switch(led_idx)
{
case 0:
bsp_board_led_on(0);
bsp_board_led_off(1);
bsp_board_led_off(2);
bsp_board_led_off(3);
break;
case 1:
bsp_board_led_off(0);
bsp_board_led_on(1);
bsp_board_led_off(2);
bsp_board_led_off(3);
break;
case 2:
bsp_board_led_off(0);
bsp_board_led_off(1);
bsp_board_led_on(2);
bsp_board_led_off(3);
break;
case 3:
bsp_board_led_off(0);
bsp_board_led_off(1);
bsp_board_led_off(2);
bsp_board_led_on(3);
break;
}
}
int main(void)
{
bsp_board_init(BSP_INIT_LEDS);
///start clock set mandatory for USB CDC
nrf_drv_clock_init();
nrf_drv_clock_hfclk_request(NULL); // for HF 32MHz external X-tal
while(!nrf_drv_clock_hfclk_is_running()) ; // Just waiting
nrf_drv_clock_lfclk_request(NULL); // for LF 32.768kHz external X-tal
while(!nrf_drv_clock_lfclk_is_running()) ;
/// end clock set
/// for radio receiver
radio_configure();
NRF_RADIO->PACKETPTR = (uint32_t)&packet;
while(true)
{
uint32_t received = read_packet(); // received data from RADIO
LED_Control(received-1);
}
}




송신측

#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
#include <stdio.h>
#include "boards.h"
#include "radio_config.h" // for radio
#include "nrf_drv_clock.h"
#include "nrf_delay.h"
static uint32_t packet = 0;
void send_packet()
{
// send the packet:
NRF_RADIO->EVENTS_READY = 0U;
NRF_RADIO->TASKS_TXEN = 1;
while (NRF_RADIO->EVENTS_READY == 0U) ;
NRF_RADIO->EVENTS_END = 0U;
NRF_RADIO->TASKS_START = 1U;
while (NRF_RADIO->EVENTS_END == 0U) ;
NRF_RADIO->EVENTS_DISABLED = 0U;
NRF_RADIO->TASKS_DISABLE = 1U; // Disable radio
while (NRF_RADIO->EVENTS_DISABLED == 0U) ;
}
void LED_Control(uint8_t led_idx)
{
switch(led_idx)
{
case 0:
bsp_board_led_on(0);
bsp_board_led_off(1);
bsp_board_led_off(2);
bsp_board_led_off(3);
break;
case 1:
bsp_board_led_off(0);
bsp_board_led_on(1);
bsp_board_led_off(2);
bsp_board_led_off(3);
break;
case 2:
bsp_board_led_off(0);
bsp_board_led_off(1);
bsp_board_led_on(2);
bsp_board_led_off(3);
break;
case 3:
bsp_board_led_off(0);
bsp_board_led_off(1);
bsp_board_led_off(2);
bsp_board_led_on(3);
break;
}
}
int main(void)
{
bsp_board_init(BSP_INIT_LEDS);
nrf_drv_clock_init();
nrf_drv_clock_hfclk_request(NULL); // for HF 32MHz external X-tal
while(!nrf_drv_clock_hfclk_is_running()) ; // Just waiting
nrf_drv_clock_lfclk_request(NULL); // for LF 32.768kHz external X-tal
while(!nrf_drv_clock_lfclk_is_running()) ; // Just waiting
radio_configure();
NRF_RADIO->PACKETPTR = (uint32_t)&packet; // Set payload pointer
while(true)
{
LED_Control(packet-1);
send_packet();
packet++; if(packet > 4) packet = 1;
nrf_delay_ms(500);
}
}



동작확인 

- 송신측에서 전송하는 데이터(송신측 LED 를 보면 알 수 있음)를 수신측에서 정상수신중임을 알 수 있다. 




 본 글 포함된 상위 정리글


 https://igotit.tistory.com/244 의 nRF52





첫등록 : 2019년 1월 16일 

최종수정 : 


본 글 단축주소 : https://igotit.tistory.com/2055







비트코인




암호화폐       외환/나스닥/골드         암호화폐/외환/나스닥/골드
     
현물 |선물 인버스 |선물 USDT       전략매니저(카피트레이딩)         프랍 트레이더 온라인 지원가능. MT4,MT5