UD-GS01治具の試作プログラムです
Dependencies: mbed nRF24L01P SDFileSystem
Goto_UD-GS01.cpp@15:481f92a029dd, 2020-12-15 (annotated)
- Committer:
- rgoto
- Date:
- Tue Dec 15 01:13:36 2020 +0000
- Revision:
- 15:481f92a029dd
- Parent:
- 14:894a7ca12e70
ver.2: communication--OK; SDcard Input--OK; speed--chottoosoi
Who changed what in which revision?
User | Revision | Line number | New 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 | 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 | 11:e3549d37183a | 26 | int which=0; |
rgoto | 11:e3549d37183a | 27 | int bufferidx=0; |
rgoto | 11:e3549d37183a | 28 | FILE *fp; |
rgoto | 11:e3549d37183a | 29 | int open_flg = 0; |
rgoto | 13:8357704ad091 | 30 | int write_flg = 0; |
rgoto | 14:894a7ca12e70 | 31 | int close_flg = 0; |
rgoto | 0:53b18a9251ab | 32 | |
rgoto | 13:8357704ad091 | 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 | 14:894a7ca12e70 | 42 | pc.printf("%c", rxData2[rxDataIdx]); |
rgoto | 0:53b18a9251ab | 43 | rxDataIdx++; |
rgoto | 13:8357704ad091 | 44 | |
rgoto | 11:e3549d37183a | 45 | |
rgoto | 13:8357704ad091 | 46 | if (rxDataIdx >= TRANSFER_SIZE){ |
rgoto | 14:894a7ca12e70 | 47 | write_flg = 1; |
rgoto | 2:53829a66c0d9 | 48 | rxDataIdx=0; |
rgoto | 11:e3549d37183a | 49 | rcv_flg = 0; |
rgoto | 13:8357704ad091 | 50 | i++; |
rgoto | 13:8357704ad091 | 51 | } |
rgoto | 11:e3549d37183a | 52 | |
rgoto | 11:e3549d37183a | 53 | |
rgoto | 11:e3549d37183a | 54 | } |
rgoto | 14:894a7ca12e70 | 55 | /* |
rgoto | 14:894a7ca12e70 | 56 | if(i>5){ |
rgoto | 13:8357704ad091 | 57 | i = 0; |
rgoto | 14:894a7ca12e70 | 58 | close_flg = 1; |
rgoto | 14:894a7ca12e70 | 59 | } |
rgoto | 14:894a7ca12e70 | 60 | */ |
rgoto | 11:e3549d37183a | 61 | |
rgoto | 0:53b18a9251ab | 62 | } |
rgoto | 0:53b18a9251ab | 63 | |
rgoto | 0:53b18a9251ab | 64 | void recieve(){ |
rgoto | 0:53b18a9251ab | 65 | if(UDGS01.readable()){ |
rgoto | 0:53b18a9251ab | 66 | |
rgoto | 0:53b18a9251ab | 67 | // ...read the data into the receive buffer |
rgoto | 3:fe94916d0f12 | 68 | txData1[txDataIdx] = UDGS01.getc(); //getcharだと、PCからのキーボード入力という標準コンソール?のやつだからだめらしい |
rgoto | 11:e3549d37183a | 69 | // pc.printf("tx[%d] = %s", txDataIdx, &txData[txDataIdx]); |
rgoto | 0:53b18a9251ab | 70 | txDataIdx++; |
rgoto | 11:e3549d37183a | 71 | |
rgoto | 11:e3549d37183a | 72 | if (txDataIdx == TRANSFER_SIZE) {//最初の32回 |
rgoto | 3:fe94916d0f12 | 73 | memcpy(txData2, txData1, TRANSFER_SIZE); |
rgoto | 3:fe94916d0f12 | 74 | |
rgoto | 3:fe94916d0f12 | 75 | txDataIdx=0; |
rgoto | 0:53b18a9251ab | 76 | snd_flg = 1; |
rgoto | 1:960cd07d2ae7 | 77 | |
rgoto | 3:fe94916d0f12 | 78 | } |
rgoto | 11:e3549d37183a | 79 | |
rgoto | 11:e3549d37183a | 80 | } |
rgoto | 3:fe94916d0f12 | 81 | |
rgoto | 4:de94aef84acc | 82 | } |
rgoto | 11:e3549d37183a | 83 | |
rgoto | 0:53b18a9251ab | 84 | int main() { |
rgoto | 11:e3549d37183a | 85 | |
rgoto | 8:3369eef8f9e1 | 86 | pc.baud(115200); |
rgoto | 11:e3549d37183a | 87 | UDGS01.baud(115200); |
rgoto | 11:e3549d37183a | 88 | |
rgoto | 0:53b18a9251ab | 89 | my_nrf24l01p.powerUp(); |
rgoto | 0:53b18a9251ab | 90 | my_nrf24l01p.setRfFrequency(NRF24L01P_MIN_RF_FREQUENCY);//2400-2525 |
rgoto | 0:53b18a9251ab | 91 | my_nrf24l01p.setRfOutputPower(NRF24L01P_TX_PWR_MINUS_12_DB);//mAX 0 -6 -12 -18 |
rgoto | 0:53b18a9251ab | 92 | my_nrf24l01p.setAirDataRate(NRF24L01P_DATARATE_1_MBPS);//250k,1000,2000K |
rgoto | 0:53b18a9251ab | 93 | // Display the (default) setup of the nRF24L01+ chip |
rgoto | 0:53b18a9251ab | 94 | pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() ); |
rgoto | 0:53b18a9251ab | 95 | pc.printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() ); |
rgoto | 0:53b18a9251ab | 96 | pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() ); |
rgoto | 0:53b18a9251ab | 97 | pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() ); |
rgoto | 0:53b18a9251ab | 98 | // pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() ); |
rgoto | 0:53b18a9251ab | 99 | |
rgoto | 0:53b18a9251ab | 100 | pc.printf( "Type keys to test transfers:\r\n (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE ); |
rgoto | 0:53b18a9251ab | 101 | my_nrf24l01p.setTransferSize( TRANSFER_SIZE );//mAX 32 |
rgoto | 0:53b18a9251ab | 102 | my_nrf24l01p.setReceiveMode(); |
rgoto | 0:53b18a9251ab | 103 | my_nrf24l01p.enable(); |
rgoto | 11:e3549d37183a | 104 | |
rgoto | 11:e3549d37183a | 105 | |
rgoto | 13:8357704ad091 | 106 | SDFileSystem *sd = new SDFileSystem(PB_15, PB_14, PB_13, PC_4, "sd", NC, SDFileSystem::SWITCH_NONE, 20000000); // mosi, miso, sclk, name, cs, card detect, sw type, freq |
rgoto | 0:53b18a9251ab | 107 | wait_ms(100); |
rgoto | 13:8357704ad091 | 108 | |
rgoto | 13:8357704ad091 | 109 | if(open_flg == 0){ |
rgoto | 13:8357704ad091 | 110 | fp = fopen("/sd/testlog.txt", "a"); |
rgoto | 13:8357704ad091 | 111 | |
rgoto | 13:8357704ad091 | 112 | if (fp == NULL) |
rgoto | 13:8357704ad091 | 113 | { |
rgoto | 13:8357704ad091 | 114 | printf("open error!!\r\n"); |
rgoto | 13:8357704ad091 | 115 | // while(1); |
rgoto | 13:8357704ad091 | 116 | } |
rgoto | 13:8357704ad091 | 117 | else{ |
rgoto | 13:8357704ad091 | 118 | // fwrite(rxData3, sizeof(char), TRANSFER_SIZE, fp); |
rgoto | 13:8357704ad091 | 119 | fprintf(fp, "opened!!\r\n"); |
rgoto | 13:8357704ad091 | 120 | fclose(fp); |
rgoto | 13:8357704ad091 | 121 | open_flg = 1; |
rgoto | 13:8357704ad091 | 122 | wait(0.5); |
rgoto | 13:8357704ad091 | 123 | } |
rgoto | 13:8357704ad091 | 124 | } |
rgoto | 11:e3549d37183a | 125 | |
rgoto | 14:894a7ca12e70 | 126 | interrput.attach(&timer, 0.1);//100 usec 10Khz |
rgoto | 0:53b18a9251ab | 127 | UDGS01.attach(recieve,Serial::RxIrq);//牛からのデータ受信したら割り込み発生してrecieveを呼び出す |
rgoto | 0:53b18a9251ab | 128 | |
rgoto | 13:8357704ad091 | 129 | |
rgoto | 0:53b18a9251ab | 130 | // my_nrf24l01p.flush_rx_fifo(); |
rgoto | 0:53b18a9251ab | 131 | while(1){ |
rgoto | 0:53b18a9251ab | 132 | |
rgoto | 4:de94aef84acc | 133 | if ( my_nrf24l01p.readable(NRF24L01P_PIPE_P0) ) { //受信可能であれば |
rgoto | 11:e3549d37183a | 134 | |
rgoto | 11:e3549d37183a | 135 | rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, (char*)(rxData1),TRANSFER_SIZE ); |
rgoto | 14:894a7ca12e70 | 136 | |
rgoto | 14:894a7ca12e70 | 137 | if(rcv_flg == 0){ |
rgoto | 4:de94aef84acc | 138 | memcpy(rxData2, rxData1, TRANSFER_SIZE); |
rgoto | 14:894a7ca12e70 | 139 | wait(0.2); |
rgoto | 14:894a7ca12e70 | 140 | } |
rgoto | 13:8357704ad091 | 141 | |
rgoto | 13:8357704ad091 | 142 | // memcpy(rxData3, rxData1, TRANSFER_SIZE); |
rgoto | 12:d935d1243611 | 143 | |
rgoto | 13:8357704ad091 | 144 | if(open_flg == 1){ |
rgoto | 13:8357704ad091 | 145 | fp = fopen("/sd/testlog.txt", "a"); |
rgoto | 14:894a7ca12e70 | 146 | if (fp == NULL) |
rgoto | 14:894a7ca12e70 | 147 | { |
rgoto | 14:894a7ca12e70 | 148 | printf("open error!!\r\n"); |
rgoto | 14:894a7ca12e70 | 149 | // while(1); |
rgoto | 14:894a7ca12e70 | 150 | }else{ |
rgoto | 13:8357704ad091 | 151 | open_flg = 2; |
rgoto | 14:894a7ca12e70 | 152 | } |
rgoto | 11:e3549d37183a | 153 | } |
rgoto | 11:e3549d37183a | 154 | |
rgoto | 13:8357704ad091 | 155 | if(write_flg == 1){ |
rgoto | 14:894a7ca12e70 | 156 | // fprintf(fp, "check\r\n"); |
rgoto | 14:894a7ca12e70 | 157 | fwrite(rxData2, sizeof(char), TRANSFER_SIZE, fp); |
rgoto | 14:894a7ca12e70 | 158 | wait(1); |
rgoto | 14:894a7ca12e70 | 159 | |
rgoto | 14:894a7ca12e70 | 160 | fclose(fp); |
rgoto | 14:894a7ca12e70 | 161 | wait(0.2); |
rgoto | 14:894a7ca12e70 | 162 | // close_flg = 0; |
rgoto | 14:894a7ca12e70 | 163 | write_flg = 0; |
rgoto | 14:894a7ca12e70 | 164 | open_flg = 1; |
rgoto | 11:e3549d37183a | 165 | } |
rgoto | 14:894a7ca12e70 | 166 | |
rgoto | 13:8357704ad091 | 167 | rcv_flg = 1; |
rgoto | 13:8357704ad091 | 168 | // wait(1); |
rgoto | 13:8357704ad091 | 169 | } |
rgoto | 2:53829a66c0d9 | 170 | |
rgoto | 0:53b18a9251ab | 171 | if (snd_flg==1) {//最初のバッファ |
rgoto | 0:53b18a9251ab | 172 | snd_flg=0; |
rgoto | 2:53829a66c0d9 | 173 | |
rgoto | 11:e3549d37183a | 174 | my_nrf24l01p.write( NRF24L01P_PIPE_P0, (char *)txData2 , TRANSFER_SIZE ); |
rgoto | 8:3369eef8f9e1 | 175 | pc.putc('1'); |
rgoto | 11:e3549d37183a | 176 | wait(2); |
rgoto | 0:53b18a9251ab | 177 | } |
rgoto | 0:53b18a9251ab | 178 | |
rgoto | 11:e3549d37183a | 179 | |
rgoto | 11:e3549d37183a | 180 | |
rgoto | 11:e3549d37183a | 181 | } //一番最初のwhileの} |
rgoto | 11:e3549d37183a | 182 | } |