Skip to content

Instantly share code, notes, and snippets.

@shima-529
Created March 16, 2021 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shima-529/6af8740fea58132a10981ca51ac97b2c to your computer and use it in GitHub Desktop.
Save shima-529/6af8740fea58132a10981ca51ac97b2c to your computer and use it in GitHub Desktop.
#include <stdint.h>
#include <nrf.h>
#include "rtc.h"
/* 参考:
- http://blog.chinaunix.net/uid-28852942-id-5745469.html
- サンプルプログラム。中国の方。
- https://pe-bank.jp/guide/column/theme/iot_things/20170919
- https://pe-bank.jp/guide/column/theme/iot_things/20171120
- この2つはパケットの基本構造についてざっくり理解するのに必要。
- https://sites.google.com/a/gclue.jp/ble-docs/advertising-1/advertising
- iPhoneから送出されるパケットをビット単位で説明してくれている。
- https://fabo.gitbooks.io/bledocs/content/nordic/beaconadvparam.html
- https://fabo.gitbooks.io/bledocs/content/nordic/beaconadvdata.html
- 上2つはアドバタイズのパラメータについて。
- https://devzone.nordicsemi.com/f/nordic-q-a/11640/nrf51822-raw-transmit-receive-not-working
- 「RADIO実行中はずっとHFCLK(外部クリスタルとして)を起動しておかないと」と示唆。
- https://qiita.com/hktechno/items/f594f87493fbe11de3fb
- UICRを書き換えなければならないという事をここで知る。
*/
static uint8_t packetBuffer[64];
#define BLE_ADV_ADDRESS 0x8E89BED6
void radio_ble_init(void) {
NRF_RADIO->MODE = 3; // BLE mode
NRF_RADIO->TXPOWER = 4; // +4dBm
NRF_RADIO->FREQUENCY = 2; // Ch 37 for Advertising
// Packet Format Decision
NRF_RADIO->PCNF0 = (1 << 8) | (8 << 0); // 1byte S0, 8bit Length
NRF_RADIO->PCNF1 = (37 << 0) | ((4-1) << 16); // max 31bytes payload, 4bytes address
NRF_RADIO->CRCCNF = 3 | (1 << 8);
NRF_RADIO->CRCPOLY = (1 << 24) | (1 << 10) | (1 << 9) | (1 << 6) | (1 << 4) | (1 << 3) | (1 << 1) | (1 << 0);
NRF_RADIO->CRCINIT = 0x555555;
// Address
NRF_RADIO->BASE0 = BLE_ADV_ADDRESS << 8;
NRF_RADIO->PREFIX0 = BLE_ADV_ADDRESS >> 24;
NRF_RADIO->PCNF1 |= (1 << 25);
NRF_RADIO->DATAWHITEIV = 0x25;
NRF_RADIO->OVERRIDE4 = 1 << 31;
NRF_RADIO->OVERRIDE0 = NRF_FICR->BLE_1MBIT[0];
NRF_RADIO->OVERRIDE1 = NRF_FICR->BLE_1MBIT[1];
NRF_RADIO->OVERRIDE2 = NRF_FICR->BLE_1MBIT[2];
NRF_RADIO->OVERRIDE3 = NRF_FICR->BLE_1MBIT[3];
NRF_RADIO->OVERRIDE4 = NRF_FICR->BLE_1MBIT[4];
}
void radio_memwrite_adv_data(void) {
// == PDU Header ==
packetBuffer[0] = 0x42; // ADV_NONCONN_IND
packetBuffer[1] = 6 + 3 + 5; // Length。アドレス分も長さが要るっぽい?
// ===== AdvA =====
packetBuffer[2] = 0x93;
packetBuffer[3] = 0x08;
packetBuffer[4] = 0x81;
packetBuffer[5] = 0x14;
packetBuffer[6] = 0x45;
packetBuffer[7] = 0x11;
// ===== AdvData =====
// -- Data0 ---
packetBuffer[8] = 2; // Length
packetBuffer[9] = 0x01; // flag
packetBuffer[10] = 0x06; // General & Discoverable, Only for BLE
// --- Data1 ---
packetBuffer[11] = 4; // Length
packetBuffer[12] = 0x09; // Complete Local Name
packetBuffer[13] = 'n';
packetBuffer[14] = 'R';
packetBuffer[15] = 'F';
}
void radio_tx(void) {
NRF_RADIO->PACKETPTR = (uintptr_t)packetBuffer;
radio_memwrite_adv_data();
NRF_RADIO->TXADDRESS = 0;
NRF_RADIO->TASKS_TXEN = 1;
while( !NRF_RADIO->EVENTS_READY );
NRF_RADIO->EVENTS_READY = 0;
NRF_RADIO->TASKS_START = 1;
while( !NRF_RADIO->EVENTS_END );
NRF_RADIO->EVENTS_END = 0;
NRF_RADIO->TASKS_DISABLE = 1;
while( !NRF_RADIO->EVENTS_DISABLED );
NRF_RADIO->EVENTS_DISABLED = 0;
}
static uint8_t rxBuffer[32];
void radio_rx(void) {
NRF_RADIO->PACKETPTR = (uintptr_t)rxBuffer;
NRF_RADIO->RXADDRESSES = 1; // Enable RX for Address 0
NRF_RADIO->TASKS_RXEN = 1;
while( !NRF_RADIO->EVENTS_READY );
NRF_RADIO->EVENTS_READY = 0;
NRF_RADIO->TASKS_START = 1;
while( !NRF_RADIO->EVENTS_END );
NRF_RADIO->EVENTS_END = 0;
NRF_RADIO->TASKS_DISABLE = 1;
while( !NRF_RADIO->EVENTS_DISABLED );
NRF_RADIO->EVENTS_DISABLED = 0;
}
int main(void) {
rtc_init();
radio_ble_init();
while(1) {
radio_tx();
radio_rx();
ms_wait(100);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment