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

Dependencies:   mbed nRF24L01P SDFileSystem

Committer:
rgoto
Date:
Wed Dec 09 04:04:37 2020 +0000
Revision:
4:de94aef84acc
Parent:
3:fe94916d0f12
Child:
5:24ea1bd807e6
ver5: hiaretu wo niko nisite nakamiga tigattatokidake hyoujisuruyouni sitakattakedo umakuikanaiyatu

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 4:de94aef84acc 6 #include <assert.h>
rgoto 4:de94aef84acc 7 #include <stdbool.h>
rgoto 0:53b18a9251ab 8 RawSerial pc(PA_2, PA_3,115200); //ここはpcとケーブルでつないだときにシリアル通信させてたところだから、ここから牛のUARTにつなぐだけで大丈夫なはず。→ここは元々ST基板のPC部分(パキッと折れそうな方)との通信用に使われてるから他のシリアルは無理らしい。
rgoto 0:53b18a9251ab 9 RawSerial UDGS01(PA_0, PA_1, 115200); // 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 3:fe94916d0f12 16 char txData1[TRANSFER_SIZE];
rgoto 3:fe94916d0f12 17 char txData2[TRANSFER_SIZE]; /*char型で一文字ずつ配列に入れていき、表示する。char型は一つ1バイトなので、32個ずつ溜まったら送るようにする。*/
rgoto 0:53b18a9251ab 18 int txDataIdx = 0;
rgoto 0:53b18a9251ab 19 int txDataCnt=0;
rgoto 4:de94aef84acc 20 char rxData1[TRANSFER_SIZE];
rgoto 4:de94aef84acc 21 char rxData2[TRANSFER_SIZE]; /*char型で一文字ずつ配列に入れていき、表示する。char型は一つ1バイトなので、32個ずつ溜まったら送るようにする。*/
rgoto 4:de94aef84acc 22
rgoto 0:53b18a9251ab 23 int rxDataIdx = 0;
rgoto 0:53b18a9251ab 24 int rxDataCnt=0;
rgoto 0:53b18a9251ab 25 int i=0;
rgoto 0:53b18a9251ab 26 int which=0;
rgoto 2:53829a66c0d9 27 int bufferidx=0;
rgoto 0:53b18a9251ab 28
rgoto 0:53b18a9251ab 29 nRF24L01P my_nrf24l01p(D11, D12, D13, D10, D9,D8); // mosi, miso, sck, csn, ce, irq
rgoto 0:53b18a9251ab 30 //nRF24L01P my_nrf24l01p(p5, p6, p7, p8, p9, p10); // mosi, miso, sck, csn, ce, irq
rgoto 0:53b18a9251ab 31
rgoto 0:53b18a9251ab 32 void timer(){ /*タイマー割り込みによるSPEAKER out*/
rgoto 0:53b18a9251ab 33 uint16_t ain;
rgoto 0:53b18a9251ab 34
rgoto 0:53b18a9251ab 35
rgoto 4:de94aef84acc 36 if(rcv_flg == 1) {
rgoto 2:53829a66c0d9 37
rgoto 4:de94aef84acc 38 pc.printf("%s", &rxData2[rxDataIdx]);
rgoto 0:53b18a9251ab 39 rxDataIdx++;
rgoto 4:de94aef84acc 40 if (rxDataIdx >= TRANSFER_SIZE)
rgoto 2:53829a66c0d9 41 rxDataIdx=0;
rgoto 4:de94aef84acc 42 rcv_flg = 0;
rgoto 4:de94aef84acc 43 }
rgoto 4:de94aef84acc 44 if(rcv_flg == 2) {
rgoto 2:53829a66c0d9 45
rgoto 4:de94aef84acc 46 pc.printf("\r\n");
rgoto 4:de94aef84acc 47 rcv_flg = 0;
rgoto 2:53829a66c0d9 48 }
rgoto 2:53829a66c0d9 49 //ここらへんにSDカードの書き込みを入れようかと!
rgoto 0:53b18a9251ab 50 }
rgoto 0:53b18a9251ab 51
rgoto 0:53b18a9251ab 52 void recieve(){
rgoto 0:53b18a9251ab 53 if(UDGS01.readable()){
rgoto 0:53b18a9251ab 54
rgoto 0:53b18a9251ab 55 // ...read the data into the receive buffer
rgoto 3:fe94916d0f12 56 txData1[txDataIdx] = UDGS01.getc(); //getcharだと、PCからのキーボード入力という標準コンソール?のやつだからだめらしい
rgoto 2:53829a66c0d9 57 // pc.printf("tx[%d] = %s", txDataIdx, &txData[txDataIdx]);
rgoto 0:53b18a9251ab 58 txDataIdx++;
rgoto 2:53829a66c0d9 59
rgoto 0:53b18a9251ab 60 if (txDataIdx == TRANSFER_SIZE) {//最初の32回
rgoto 3:fe94916d0f12 61 memcpy(txData2, txData1, TRANSFER_SIZE);
rgoto 3:fe94916d0f12 62
rgoto 3:fe94916d0f12 63 txDataIdx=0;
rgoto 0:53b18a9251ab 64 snd_flg = 1;
rgoto 1:960cd07d2ae7 65
rgoto 3:fe94916d0f12 66 }
rgoto 1:960cd07d2ae7 67
rgoto 0:53b18a9251ab 68 }
rgoto 3:fe94916d0f12 69
rgoto 4:de94aef84acc 70 }
rgoto 4:de94aef84acc 71
rgoto 4:de94aef84acc 72 //for文でrxData1とrxData2を比較して、まったく同じ中身なら0,違えば1を返す関数
rgoto 4:de94aef84acc 73 bool rxData_equal(char* rxData1,/* size_t size1,*/ const char* rxData2/*, size_t size2*/)
rgoto 4:de94aef84acc 74 {
rgoto 4:de94aef84acc 75 assert(rxData1 != NULL);
rgoto 4:de94aef84acc 76 assert(rxData2 != NULL);
rgoto 4:de94aef84acc 77 // assert(size1 != 0);
rgoto 4:de94aef84acc 78 // assert(size2 != 0);
rgoto 4:de94aef84acc 79 /*
rgoto 4:de94aef84acc 80 // 要素数が違うなら、絶対に一致しない
rgoto 4:de94aef84acc 81 if (size1 != size2) {
rgoto 4:de94aef84acc 82 return false;
rgoto 4:de94aef84acc 83 }
rgoto 4:de94aef84acc 84 */
rgoto 4:de94aef84acc 85 for (i = 0; i < TRANSFER_SIZE; ++i) {
rgoto 4:de94aef84acc 86 if (rxData1[i] != rxData2[i]) {
rgoto 4:de94aef84acc 87 return false;
rgoto 4:de94aef84acc 88 }
rgoto 4:de94aef84acc 89 }
rgoto 4:de94aef84acc 90 return true;
rgoto 4:de94aef84acc 91 }
rgoto 0:53b18a9251ab 92
rgoto 0:53b18a9251ab 93 int main() {
rgoto 0:53b18a9251ab 94
rgoto 2:53829a66c0d9 95
rgoto 0:53b18a9251ab 96
rgoto 0:53b18a9251ab 97
rgoto 0:53b18a9251ab 98 my_nrf24l01p.powerUp();
rgoto 0:53b18a9251ab 99 my_nrf24l01p.setRfFrequency(NRF24L01P_MIN_RF_FREQUENCY);//2400-2525
rgoto 0:53b18a9251ab 100 my_nrf24l01p.setRfOutputPower(NRF24L01P_TX_PWR_MINUS_12_DB);//mAX 0 -6 -12 -18
rgoto 0:53b18a9251ab 101 my_nrf24l01p.setAirDataRate(NRF24L01P_DATARATE_1_MBPS);//250k,1000,2000K
rgoto 0:53b18a9251ab 102 // Display the (default) setup of the nRF24L01+ chip
rgoto 0:53b18a9251ab 103 pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() );
rgoto 0:53b18a9251ab 104 pc.printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() );
rgoto 0:53b18a9251ab 105 pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() );
rgoto 0:53b18a9251ab 106 pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() );
rgoto 0:53b18a9251ab 107 // pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() );
rgoto 0:53b18a9251ab 108
rgoto 0:53b18a9251ab 109 pc.printf( "Type keys to test transfers:\r\n (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE );
rgoto 0:53b18a9251ab 110 my_nrf24l01p.setTransferSize( TRANSFER_SIZE );//mAX 32
rgoto 0:53b18a9251ab 111 my_nrf24l01p.setReceiveMode();
rgoto 0:53b18a9251ab 112 my_nrf24l01p.enable();
rgoto 0:53b18a9251ab 113
rgoto 0:53b18a9251ab 114 wait_ms(100);
rgoto 0:53b18a9251ab 115 // my_nrf24l01p.write( NRF24L01P_PIPE_P0, (char*)rxData,1);//dummy
rgoto 0:53b18a9251ab 116
rgoto 4:de94aef84acc 117 interrput.attach(&timer, 1);//100 usec 10Khz
rgoto 0:53b18a9251ab 118 UDGS01.attach(recieve,Serial::RxIrq);//牛からのデータ受信したら割り込み発生してrecieveを呼び出す
rgoto 0:53b18a9251ab 119
rgoto 0:53b18a9251ab 120 // my_nrf24l01p.flush_rx_fifo();
rgoto 0:53b18a9251ab 121 while(1){
rgoto 0:53b18a9251ab 122
rgoto 4:de94aef84acc 123 if ( my_nrf24l01p.readable(NRF24L01P_PIPE_P0) ) { //受信可能であれば
rgoto 4:de94aef84acc 124
rgoto 4:de94aef84acc 125 rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, (char*)(rxData1),TRANSFER_SIZE );
rgoto 4:de94aef84acc 126 if(rxData_equal(rxData1, rxData2)==1){ //rxData1で読んだ値が以前コピーしたのと完全一致なら0, 違えば1を出す。
rgoto 4:de94aef84acc 127
rgoto 4:de94aef84acc 128 memcpy(rxData2, rxData1, TRANSFER_SIZE);
rgoto 4:de94aef84acc 129 rcv_flg = 1;
rgoto 4:de94aef84acc 130
rgoto 4:de94aef84acc 131 }else{
rgoto 4:de94aef84acc 132 rcv_flg = 2;
rgoto 4:de94aef84acc 133 }
rgoto 4:de94aef84acc 134 }
rgoto 2:53829a66c0d9 135
rgoto 0:53b18a9251ab 136 if (snd_flg==1) {//最初のバッファ
rgoto 0:53b18a9251ab 137 snd_flg=0;
rgoto 2:53829a66c0d9 138
rgoto 3:fe94916d0f12 139 my_nrf24l01p.write( NRF24L01P_PIPE_P0, (char *)txData2 , TRANSFER_SIZE );
rgoto 3:fe94916d0f12 140 // memset(txData2, 0, TRANSFER_SIZE/*何バイト書き込むか*/);
rgoto 2:53829a66c0d9 141 // for(i=0; i>32; i++){
rgoto 2:53829a66c0d9 142 // pc.printf("tx[%d] = %s", 30, txData[30]);
rgoto 2:53829a66c0d9 143 // }
rgoto 0:53b18a9251ab 144 pc.putc('1');
rgoto 1:960cd07d2ae7 145 wait(1);
rgoto 0:53b18a9251ab 146 }
rgoto 0:53b18a9251ab 147
rgoto 3:fe94916d0f12 148
rgoto 0:53b18a9251ab 149
rgoto 0:53b18a9251ab 150 } //一番最初のwhileの}
rgoto 0:53b18a9251ab 151 }