UD-GS01治具の試作プログラムです

Dependencies:   mbed nRF24L01P SDFileSystem

Committer:
rgoto
Date:
Thu Dec 10 08:31:02 2020 +0000
Revision:
11:e3549d37183a
Parent:
10:6e3bdd6f11c3
Child:
12:d935d1243611
ver.8 SDcard wo ugokasu code iretemo log jitaiga denakunaru--!  tada, daibu masina program ni natteiru kigasuru

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rgoto 0:53b18a9251ab 1
rgoto 0:53b18a9251ab 2 //受信用と送信用を一つにする!ボタンで切り替え(元のプログラムは44で、44-2を貼り付ける)
rgoto 0:53b18a9251ab 3
rgoto 0:53b18a9251ab 4 #include "mbed.h"
rgoto 0:53b18a9251ab 5 #include "nRF24L01P.h"
rgoto 11:e3549d37183a 6 #include "SDFileSystem.h"
rgoto 9:086fae0bd5f8 7
rgoto 10:6e3bdd6f11c3 8 RawSerial pc(PA_2, PA_3); //ここはpcとケーブルでつないだときにシリアル通信させてたところだから、ここから牛のUARTにつなぐだけで大丈夫なはず。→ここは元々ST基板のPC部分(パキッと折れそうな方)との通信用に使われてるから他のシリアルは無理らしい。
rgoto 11:e3549d37183a 9 RawSerial UDGS01(PA_0, PA_1); // UD-GS01とのシリアル通信用にUARTピンを新たに定義。
rgoto 0:53b18a9251ab 10 Ticker interrput;
rgoto 0:53b18a9251ab 11
rgoto 0:53b18a9251ab 12 #define TRANSFER_SIZE 32
rgoto 0:53b18a9251ab 13 int out_flg = 0;
rgoto 0:53b18a9251ab 14 int rcv_flg = 0;
rgoto 0:53b18a9251ab 15 int snd_flg = 0;
rgoto 11:e3549d37183a 16 char txData1[TRANSFER_SIZE];
rgoto 11:e3549d37183a 17 char txData2[TRANSFER_SIZE]; /*char型で一文字ずつ配列に入れていき、表示する。char型は一つ1バイトなので、32個ずつ溜まったら送るようにする。*/
rgoto 0:53b18a9251ab 18 int txDataIdx = 0;
rgoto 0:53b18a9251ab 19 int txDataCnt=0;
rgoto 11:e3549d37183a 20 char rxData1[TRANSFER_SIZE];
rgoto 11:e3549d37183a 21 char rxData2[TRANSFER_SIZE]; /*char型で一文字ずつ配列に入れていき、表示する。char型は一つ1バイトなので、32個ずつ溜まったら送るようにする。*/
rgoto 11:e3549d37183a 22 char rxData3[TRANSFER_SIZE];
rgoto 4:de94aef84acc 23
rgoto 0:53b18a9251ab 24 int rxDataIdx = 0;
rgoto 0:53b18a9251ab 25 int rxDataCnt=0;
rgoto 0:53b18a9251ab 26 int i=0;
rgoto 11:e3549d37183a 27 int which=0;
rgoto 11:e3549d37183a 28 int bufferidx=0;
rgoto 11:e3549d37183a 29 FILE *fp;
rgoto 11:e3549d37183a 30 int open_flg = 0;
rgoto 11:e3549d37183a 31
rgoto 0:53b18a9251ab 32
rgoto 0:53b18a9251ab 33 nRF24L01P my_nrf24l01p(D11, D12, D13, D10, D9,D8); // mosi, miso, sck, csn, ce, irq
rgoto 11:e3549d37183a 34 //nRF24L01P my_nrf24l01p(p5, p6, p7, p8, p9, p10); // mosi, miso, sck, csn, ce, irq
rgoto 0:53b18a9251ab 35
rgoto 11:e3549d37183a 36 void timer(){
rgoto 0:53b18a9251ab 37 uint16_t ain;
rgoto 0:53b18a9251ab 38
rgoto 0:53b18a9251ab 39
rgoto 4:de94aef84acc 40 if(rcv_flg == 1) {
rgoto 2:53829a66c0d9 41
rgoto 4:de94aef84acc 42 pc.printf("%s", &rxData2[rxDataIdx]);
rgoto 0:53b18a9251ab 43 rxDataIdx++;
rgoto 11:e3549d37183a 44 i++;
rgoto 11:e3549d37183a 45
rgoto 4:de94aef84acc 46 if (rxDataIdx >= TRANSFER_SIZE)
rgoto 2:53829a66c0d9 47 rxDataIdx=0;
rgoto 11:e3549d37183a 48 rcv_flg = 0;
rgoto 11:e3549d37183a 49
rgoto 11:e3549d37183a 50
rgoto 11:e3549d37183a 51 }
rgoto 11:e3549d37183a 52
rgoto 11:e3549d37183a 53 //ここらへんにSDカードの書き込みを入れようかと!
rgoto 0:53b18a9251ab 54 }
rgoto 0:53b18a9251ab 55
rgoto 0:53b18a9251ab 56 void recieve(){
rgoto 0:53b18a9251ab 57 if(UDGS01.readable()){
rgoto 0:53b18a9251ab 58
rgoto 0:53b18a9251ab 59 // ...read the data into the receive buffer
rgoto 3:fe94916d0f12 60 txData1[txDataIdx] = UDGS01.getc(); //getcharだと、PCからのキーボード入力という標準コンソール?のやつだからだめらしい
rgoto 11:e3549d37183a 61 // pc.printf("tx[%d] = %s", txDataIdx, &txData[txDataIdx]);
rgoto 0:53b18a9251ab 62 txDataIdx++;
rgoto 11:e3549d37183a 63
rgoto 11:e3549d37183a 64 if (txDataIdx == TRANSFER_SIZE) {//最初の32回
rgoto 3:fe94916d0f12 65 memcpy(txData2, txData1, TRANSFER_SIZE);
rgoto 3:fe94916d0f12 66
rgoto 3:fe94916d0f12 67 txDataIdx=0;
rgoto 0:53b18a9251ab 68 snd_flg = 1;
rgoto 1:960cd07d2ae7 69
rgoto 3:fe94916d0f12 70 }
rgoto 11:e3549d37183a 71
rgoto 11:e3549d37183a 72 }
rgoto 3:fe94916d0f12 73
rgoto 4:de94aef84acc 74 }
rgoto 4:de94aef84acc 75
rgoto 11:e3549d37183a 76
rgoto 0:53b18a9251ab 77 int main() {
rgoto 11:e3549d37183a 78
rgoto 8:3369eef8f9e1 79 pc.baud(115200);
rgoto 11:e3549d37183a 80 UDGS01.baud(115200);
rgoto 11:e3549d37183a 81
rgoto 0:53b18a9251ab 82 my_nrf24l01p.powerUp();
rgoto 0:53b18a9251ab 83 my_nrf24l01p.setRfFrequency(NRF24L01P_MIN_RF_FREQUENCY);//2400-2525
rgoto 0:53b18a9251ab 84 my_nrf24l01p.setRfOutputPower(NRF24L01P_TX_PWR_MINUS_12_DB);//mAX 0 -6 -12 -18
rgoto 0:53b18a9251ab 85 my_nrf24l01p.setAirDataRate(NRF24L01P_DATARATE_1_MBPS);//250k,1000,2000K
rgoto 0:53b18a9251ab 86 // Display the (default) setup of the nRF24L01+ chip
rgoto 0:53b18a9251ab 87 pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() );
rgoto 0:53b18a9251ab 88 pc.printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() );
rgoto 0:53b18a9251ab 89 pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() );
rgoto 0:53b18a9251ab 90 pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() );
rgoto 0:53b18a9251ab 91 // pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() );
rgoto 0:53b18a9251ab 92
rgoto 0:53b18a9251ab 93 pc.printf( "Type keys to test transfers:\r\n (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE );
rgoto 0:53b18a9251ab 94 my_nrf24l01p.setTransferSize( TRANSFER_SIZE );//mAX 32
rgoto 0:53b18a9251ab 95 my_nrf24l01p.setReceiveMode();
rgoto 0:53b18a9251ab 96 my_nrf24l01p.enable();
rgoto 11:e3549d37183a 97
rgoto 11:e3549d37183a 98
rgoto 11:e3549d37183a 99 SDFileSystem *sd = new SDFileSystem(PB_15, PB_14, PB_13, PB_11, "sd", NC, SDFileSystem::SWITCH_NONE, 20000000); // mosi, miso, sclk, name, cs, card detect, sw type, freq
rgoto 0:53b18a9251ab 100 wait_ms(100);
rgoto 11:e3549d37183a 101 // my_nrf24l01p.write( NRF24L01P_PIPE_P0, (char*)rxData,1);//dummy
rgoto 11:e3549d37183a 102
rgoto 9:086fae0bd5f8 103 interrput.attach(&timer, 1);//100 usec 10Khz
rgoto 0:53b18a9251ab 104 UDGS01.attach(recieve,Serial::RxIrq);//牛からのデータ受信したら割り込み発生してrecieveを呼び出す
rgoto 0:53b18a9251ab 105
rgoto 0:53b18a9251ab 106 // my_nrf24l01p.flush_rx_fifo();
rgoto 0:53b18a9251ab 107 while(1){
rgoto 0:53b18a9251ab 108
rgoto 4:de94aef84acc 109 if ( my_nrf24l01p.readable(NRF24L01P_PIPE_P0) ) { //受信可能であれば
rgoto 11:e3549d37183a 110
rgoto 11:e3549d37183a 111 rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, (char*)(rxData1),TRANSFER_SIZE );
rgoto 4:de94aef84acc 112
rgoto 4:de94aef84acc 113 memcpy(rxData2, rxData1, TRANSFER_SIZE);
rgoto 11:e3549d37183a 114 wait(0.5);
rgoto 11:e3549d37183a 115 memcpy(rxData3, rxData1, TRANSFER_SIZE);
rgoto 8:3369eef8f9e1 116
rgoto 11:e3549d37183a 117 if(open_flg == 0){
rgoto 11:e3549d37183a 118 fp = fopen("/sd/testlog.txt", "a");
rgoto 11:e3549d37183a 119 open_flg = 1;
rgoto 11:e3549d37183a 120 }
rgoto 11:e3549d37183a 121
rgoto 11:e3549d37183a 122 if(open_flg == 1){
rgoto 11:e3549d37183a 123 fwrite(rxData3, sizeof(char), TRANSFER_SIZE, fp);
rgoto 11:e3549d37183a 124 wait(0.5);
rgoto 11:e3549d37183a 125 }
rgoto 11:e3549d37183a 126
rgoto 4:de94aef84acc 127 rcv_flg = 1;
rgoto 11:e3549d37183a 128
rgoto 11:e3549d37183a 129 if(i>100){
rgoto 11:e3549d37183a 130 fclose(fp);
rgoto 11:e3549d37183a 131 i = 0;
rgoto 11:e3549d37183a 132 open_flg = 0;
rgoto 11:e3549d37183a 133 wait(1);
rgoto 11:e3549d37183a 134 }
rgoto 11:e3549d37183a 135 wait(1);
rgoto 11:e3549d37183a 136 }
rgoto 2:53829a66c0d9 137
rgoto 0:53b18a9251ab 138 if (snd_flg==1) {//最初のバッファ
rgoto 0:53b18a9251ab 139 snd_flg=0;
rgoto 2:53829a66c0d9 140
rgoto 11:e3549d37183a 141 my_nrf24l01p.write( NRF24L01P_PIPE_P0, (char *)txData2 , TRANSFER_SIZE );
rgoto 8:3369eef8f9e1 142 pc.putc('1');
rgoto 11:e3549d37183a 143 wait(2);
rgoto 0:53b18a9251ab 144 }
rgoto 0:53b18a9251ab 145
rgoto 11:e3549d37183a 146
rgoto 11:e3549d37183a 147
rgoto 11:e3549d37183a 148 } //一番最初のwhileの}
rgoto 11:e3549d37183a 149 }