データシート:
nRF51822 / Bluetooth Low Energy / Products / Home - Ultra Low Power Wireless Solutions from NORDIC SEMICONDUCTOR
例として GPIO の DIR レジスタを読み取ってみる。各 GPIO ラインの入出力の向きを設定するレジスタである。
- ベースアドレスは 0x50000000 である。
- DIR レジスタはベースアドレスから 0x514 ずれたところにある。
- 結局 DIR レジスタのアドレスは両者を足した 0x50000514 である。
- ただしこれはアドレスの数値表現であるので、(unsigned int *)0x50000514 のようにしてポインターにキャストする。
- ポインターの頭に * を付けて *(unsigned int *)0x50000514 にすると、今度はそのアドレスに存在しているデータが表現できる。このデータが DIR レジスタである。
#include "MicroBit.h" MicroBit uBit; int main(void){ // micro:bit を初期化した直後の GPIO ピンの入出力の設定を見てみる。 uBit.init(); printf("%x\n", *(unsigned int *)(0x50000514)); // 今度は micro:bit の P0、P1、P2 (GPIO ピンの 3、2、1) を出力に設定して、 // GPIO ピンの入出力の設定を見てみる。 uBit.io.P0.setDigitalValue(0); uBit.io.P1.setDigitalValue(0); uBit.io.P2.setDigitalValue(0); printf("%x\n", *(unsigned int *)(0x50000514)); // さらに micro:bit の P0、P1、P2 (GPIO ピンの 3、2、1) を入力に戻して、 // GPIO ピンの入出力の設定を見てみる。 uBit.io.P0.getDigitalValue(); uBit.io.P1.getDigitalValue(); uBit.io.P2.getDigitalValue(); printf("%x\n", *(unsigned int *)(0x50000514)); return 0; }
実行結果:
エッジコネクターと GPIO ピンとの対応は MicroBitPin.h に定義してある。