micro:bit / マイコンの内部レジスタを操作する / 何かを読み取る

データシート:
nRF51822 / Bluetooth Low Energy / Products / Home - Ultra Low Power Wireless Solutions from NORDIC SEMICONDUCTOR
 
例として GPIO の DIR レジスタを読み取ってみる。各 GPIO ラインの入出力の向きを設定するレジスタである。
f:id:ti-nspire:20180522075234p:plain:w400

  1. ベースアドレスは 0x50000000 である。
  2. DIR レジスタはベースアドレスから 0x514 ずれたところにある。
  3. 結局 DIR レジスタのアドレスは両者を足した 0x50000514 である。
  4. ただしこれはアドレスの数値表現であるので、(unsigned int *)0x50000514 のようにしてポインターにキャストする。
  5. ポインターの頭に * を付けて *(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;
}

実行結果:
f:id:ti-nspire:20180522095815p:plain:w400
f:id:ti-nspire:20180522093033p:plain:w700
 
エッジコネクターと GPIO ピンとの対応は MicroBitPin.h に定義してある。
f:id:ti-nspire:20180522093611p:plain:w400