L432KCで通信モジュールとSDカード書き込みを動かすプログラムです。
Dependencies: mbed nRF24L01P SDFileSystem
dummy-L432KC.cpp@0:0a9f4da8e642, 2020-12-21 (annotated)
- Committer:
- rgoto
- Date:
- Mon Dec 21 01:28:24 2020 +0000
- Revision:
- 0:0a9f4da8e642
- Child:
- 1:fd3967c16fcf
ver2: katahou zutu nara ugoku
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rgoto | 0:0a9f4da8e642 | 1 | #include "mbed.h" |
rgoto | 0:0a9f4da8e642 | 2 | #include "nRF24L01P.h" |
rgoto | 0:0a9f4da8e642 | 3 | #include "SDFileSystem.h" |
rgoto | 0:0a9f4da8e642 | 4 | |
rgoto | 0:0a9f4da8e642 | 5 | //DigitalOut nRF_csn(D3 /*PB_6*/ ); //これは、もしかしてこのコードでも定義しとかないとかな?と思って定義した通信モジュのピン定義だけど、 |
rgoto | 0:0a9f4da8e642 | 6 | //DigitalOut nRF_ce(D6); //ライブラリに定義があり、そことかぶるから不要と言われたので不要! |
rgoto | 0:0a9f4da8e642 | 7 | //DigitalIn IRQ(D9); |
rgoto | 0:0a9f4da8e642 | 8 | //SPI nRF_spi(A6, A5, A1);// mosi, miso, sck |
rgoto | 0:0a9f4da8e642 | 9 | RawSerial pc(PA_2, PA_15); //432KCはpcとつながってるのはUART2らしい。UART使いたいならUART1。 |
rgoto | 0:0a9f4da8e642 | 10 | RawSerial UDGS01(D1, D0); // UD-GS01とのシリアル通信用にUARTピンを新たに定義。 |
rgoto | 0:0a9f4da8e642 | 11 | Ticker interrput; |
rgoto | 0:0a9f4da8e642 | 12 | |
rgoto | 0:0a9f4da8e642 | 13 | SDFileSystem sd(D11, D12, D13, D10, "sd"); // mosi, miso, sclk, cs__このコードの場所はメインの上にしよう_SDFileSystemを新しくしたら、ヘッダ側(SDFileSystem.cppの122行辺り)で周波数を定義してたので,(10MHzぐらいに設定)周波数は削除した書き方に |
rgoto | 0:0a9f4da8e642 | 14 | |
rgoto | 0:0a9f4da8e642 | 15 | #define TRANSFER_SIZE 32 |
rgoto | 0:0a9f4da8e642 | 16 | int out_flg = 0; |
rgoto | 0:0a9f4da8e642 | 17 | int rcv_flg = 0; |
rgoto | 0:0a9f4da8e642 | 18 | int snd_flg = 0; |
rgoto | 0:0a9f4da8e642 | 19 | char txData1[TRANSFER_SIZE]; |
rgoto | 0:0a9f4da8e642 | 20 | char txData2[TRANSFER_SIZE]; /*char型で一文字ずつ配列に入れていき、表示する。char型は一つ1バイトなので、32個ずつ溜まったら送るようにする。*/ |
rgoto | 0:0a9f4da8e642 | 21 | int txDataIdx = 0; |
rgoto | 0:0a9f4da8e642 | 22 | int txDataCnt=0; |
rgoto | 0:0a9f4da8e642 | 23 | char rxData1[TRANSFER_SIZE]; |
rgoto | 0:0a9f4da8e642 | 24 | char rxData2[TRANSFER_SIZE]; /*char型で一文字ずつ配列に入れていき、表示する。char型は一つ1バイトなので、32個ずつ溜まったら送るようにする。*/ |
rgoto | 0:0a9f4da8e642 | 25 | |
rgoto | 0:0a9f4da8e642 | 26 | int rxDataIdx = 0; |
rgoto | 0:0a9f4da8e642 | 27 | int rxDataCnt=0; |
rgoto | 0:0a9f4da8e642 | 28 | int i=0; |
rgoto | 0:0a9f4da8e642 | 29 | int which=0; |
rgoto | 0:0a9f4da8e642 | 30 | int bufferidx=0; |
rgoto | 0:0a9f4da8e642 | 31 | FILE *fp; |
rgoto | 0:0a9f4da8e642 | 32 | int open_flg = 0; |
rgoto | 0:0a9f4da8e642 | 33 | int write_flg = 0; |
rgoto | 0:0a9f4da8e642 | 34 | int close_flg = 0; |
rgoto | 0:0a9f4da8e642 | 35 | |
rgoto | 0:0a9f4da8e642 | 36 | nRF24L01P my_nrf24l01p(A6, A5, A1, D3, D6, D9); // mosi, miso, sck, csn, ce, irq |
rgoto | 0:0a9f4da8e642 | 37 | |
rgoto | 0:0a9f4da8e642 | 38 | |
rgoto | 0:0a9f4da8e642 | 39 | //nRF24L01P my_nrf24l01p(p5, p6, p7, p8, p9, p10); // mosi, miso, sck, csn, ce, irq |
rgoto | 0:0a9f4da8e642 | 40 | |
rgoto | 0:0a9f4da8e642 | 41 | void timer(){ |
rgoto | 0:0a9f4da8e642 | 42 | uint16_t ain; |
rgoto | 0:0a9f4da8e642 | 43 | |
rgoto | 0:0a9f4da8e642 | 44 | |
rgoto | 0:0a9f4da8e642 | 45 | if(rcv_flg == 1) { |
rgoto | 0:0a9f4da8e642 | 46 | |
rgoto | 0:0a9f4da8e642 | 47 | printf("%c", rxData2[rxDataIdx]); |
rgoto | 0:0a9f4da8e642 | 48 | rxDataIdx++; |
rgoto | 0:0a9f4da8e642 | 49 | |
rgoto | 0:0a9f4da8e642 | 50 | |
rgoto | 0:0a9f4da8e642 | 51 | if (rxDataIdx >= TRANSFER_SIZE){ |
rgoto | 0:0a9f4da8e642 | 52 | write_flg = 1; |
rgoto | 0:0a9f4da8e642 | 53 | rxDataIdx=0; |
rgoto | 0:0a9f4da8e642 | 54 | rcv_flg = 0; |
rgoto | 0:0a9f4da8e642 | 55 | i++; |
rgoto | 0:0a9f4da8e642 | 56 | } |
rgoto | 0:0a9f4da8e642 | 57 | |
rgoto | 0:0a9f4da8e642 | 58 | |
rgoto | 0:0a9f4da8e642 | 59 | } |
rgoto | 0:0a9f4da8e642 | 60 | /* |
rgoto | 0:0a9f4da8e642 | 61 | if(i>5){ |
rgoto | 0:0a9f4da8e642 | 62 | i = 0; |
rgoto | 0:0a9f4da8e642 | 63 | close_flg = 1; |
rgoto | 0:0a9f4da8e642 | 64 | } |
rgoto | 0:0a9f4da8e642 | 65 | */ |
rgoto | 0:0a9f4da8e642 | 66 | |
rgoto | 0:0a9f4da8e642 | 67 | } |
rgoto | 0:0a9f4da8e642 | 68 | |
rgoto | 0:0a9f4da8e642 | 69 | void recieve(){ |
rgoto | 0:0a9f4da8e642 | 70 | if(UDGS01.readable()){ |
rgoto | 0:0a9f4da8e642 | 71 | |
rgoto | 0:0a9f4da8e642 | 72 | // ...read the data into the receive buffer |
rgoto | 0:0a9f4da8e642 | 73 | txData1[txDataIdx] = UDGS01.getc(); //getcharだと、PCからのキーボード入力という標準コンソール?のやつだからだめらしい |
rgoto | 0:0a9f4da8e642 | 74 | // pc.printf("tx[%d] = %s", txDataIdx, &txData[txDataIdx]); |
rgoto | 0:0a9f4da8e642 | 75 | txDataIdx++; |
rgoto | 0:0a9f4da8e642 | 76 | |
rgoto | 0:0a9f4da8e642 | 77 | if (txDataIdx == TRANSFER_SIZE) {//最初の32回 |
rgoto | 0:0a9f4da8e642 | 78 | memcpy(txData2, txData1, TRANSFER_SIZE); |
rgoto | 0:0a9f4da8e642 | 79 | |
rgoto | 0:0a9f4da8e642 | 80 | txDataIdx=0; |
rgoto | 0:0a9f4da8e642 | 81 | write_flg = 1; |
rgoto | 0:0a9f4da8e642 | 82 | snd_flg = 1; |
rgoto | 0:0a9f4da8e642 | 83 | |
rgoto | 0:0a9f4da8e642 | 84 | } |
rgoto | 0:0a9f4da8e642 | 85 | |
rgoto | 0:0a9f4da8e642 | 86 | } |
rgoto | 0:0a9f4da8e642 | 87 | |
rgoto | 0:0a9f4da8e642 | 88 | } |
rgoto | 0:0a9f4da8e642 | 89 | |
rgoto | 0:0a9f4da8e642 | 90 | int main() { |
rgoto | 0:0a9f4da8e642 | 91 | |
rgoto | 0:0a9f4da8e642 | 92 | pc.baud(115200); |
rgoto | 0:0a9f4da8e642 | 93 | UDGS01.baud(115200); |
rgoto | 0:0a9f4da8e642 | 94 | |
rgoto | 0:0a9f4da8e642 | 95 | wait(1); |
rgoto | 0:0a9f4da8e642 | 96 | |
rgoto | 0:0a9f4da8e642 | 97 | |
rgoto | 0:0a9f4da8e642 | 98 | my_nrf24l01p.powerUp(); |
rgoto | 0:0a9f4da8e642 | 99 | my_nrf24l01p.setRfFrequency(NRF24L01P_MIN_RF_FREQUENCY);//2400-2525 |
rgoto | 0:0a9f4da8e642 | 100 | my_nrf24l01p.setRfOutputPower(NRF24L01P_TX_PWR_MINUS_12_DB);//mAX 0 -6 -12 -18 |
rgoto | 0:0a9f4da8e642 | 101 | my_nrf24l01p.setAirDataRate(NRF24L01P_DATARATE_1_MBPS);//250k,1000,2000K |
rgoto | 0:0a9f4da8e642 | 102 | // Display the (default) setup of the nRF24L01+ chip |
rgoto | 0:0a9f4da8e642 | 103 | printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() ); |
rgoto | 0:0a9f4da8e642 | 104 | printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() ); |
rgoto | 0:0a9f4da8e642 | 105 | printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() ); |
rgoto | 0:0a9f4da8e642 | 106 | printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() ); |
rgoto | 0:0a9f4da8e642 | 107 | // pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() ); |
rgoto | 0:0a9f4da8e642 | 108 | |
rgoto | 0:0a9f4da8e642 | 109 | printf( "Type keys to test transfers:\r\n (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE ); |
rgoto | 0:0a9f4da8e642 | 110 | my_nrf24l01p.setTransferSize( TRANSFER_SIZE );//mAX 32 |
rgoto | 0:0a9f4da8e642 | 111 | my_nrf24l01p.setReceiveMode(); |
rgoto | 0:0a9f4da8e642 | 112 | my_nrf24l01p.enable(); |
rgoto | 0:0a9f4da8e642 | 113 | |
rgoto | 0:0a9f4da8e642 | 114 | |
rgoto | 0:0a9f4da8e642 | 115 | printf("Hellooooo\r\n"); |
rgoto | 0:0a9f4da8e642 | 116 | wait_ms(10); |
rgoto | 0:0a9f4da8e642 | 117 | |
rgoto | 0:0a9f4da8e642 | 118 | if(open_flg == 0){ |
rgoto | 0:0a9f4da8e642 | 119 | wait(1); |
rgoto | 0:0a9f4da8e642 | 120 | fp = fopen("/sd/testlog.txt", "a"); |
rgoto | 0:0a9f4da8e642 | 121 | |
rgoto | 0:0a9f4da8e642 | 122 | if (fp == NULL) |
rgoto | 0:0a9f4da8e642 | 123 | { |
rgoto | 0:0a9f4da8e642 | 124 | printf("open error!!\r\n"); |
rgoto | 0:0a9f4da8e642 | 125 | // while(1); |
rgoto | 0:0a9f4da8e642 | 126 | } |
rgoto | 0:0a9f4da8e642 | 127 | else{ |
rgoto | 0:0a9f4da8e642 | 128 | // fwrite(rxData3, sizeof(char), TRANSFER_SIZE, fp); |
rgoto | 0:0a9f4da8e642 | 129 | fprintf(fp, "opened!!\r\n"); |
rgoto | 0:0a9f4da8e642 | 130 | fclose(fp); |
rgoto | 0:0a9f4da8e642 | 131 | wait_ms(10); |
rgoto | 0:0a9f4da8e642 | 132 | open_flg = 1; |
rgoto | 0:0a9f4da8e642 | 133 | |
rgoto | 0:0a9f4da8e642 | 134 | } |
rgoto | 0:0a9f4da8e642 | 135 | } |
rgoto | 0:0a9f4da8e642 | 136 | |
rgoto | 0:0a9f4da8e642 | 137 | interrput.attach(&timer, 0.001);//1 msec 10Khz |
rgoto | 0:0a9f4da8e642 | 138 | UDGS01.attach(recieve,Serial::RxIrq);//牛からのデータ受信したら割り込み発生してrecieveを呼び出す |
rgoto | 0:0a9f4da8e642 | 139 | |
rgoto | 0:0a9f4da8e642 | 140 | |
rgoto | 0:0a9f4da8e642 | 141 | // my_nrf24l01p.flush_rx_fifo(); |
rgoto | 0:0a9f4da8e642 | 142 | while(1){ |
rgoto | 0:0a9f4da8e642 | 143 | |
rgoto | 0:0a9f4da8e642 | 144 | if ( my_nrf24l01p.readable(NRF24L01P_PIPE_P0) ) { //受信可能であれば |
rgoto | 0:0a9f4da8e642 | 145 | |
rgoto | 0:0a9f4da8e642 | 146 | rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, (char*)(rxData1),TRANSFER_SIZE ); |
rgoto | 0:0a9f4da8e642 | 147 | |
rgoto | 0:0a9f4da8e642 | 148 | if(rcv_flg == 0){ |
rgoto | 0:0a9f4da8e642 | 149 | memcpy(rxData2, rxData1, TRANSFER_SIZE); |
rgoto | 0:0a9f4da8e642 | 150 | wait_ms(1); |
rgoto | 0:0a9f4da8e642 | 151 | } |
rgoto | 0:0a9f4da8e642 | 152 | |
rgoto | 0:0a9f4da8e642 | 153 | // memcpy(rxData3, rxData1, TRANSFER_SIZE); |
rgoto | 0:0a9f4da8e642 | 154 | |
rgoto | 0:0a9f4da8e642 | 155 | if(open_flg == 1){ |
rgoto | 0:0a9f4da8e642 | 156 | fp = fopen("/sd/recieve_log.txt", "a"); |
rgoto | 0:0a9f4da8e642 | 157 | if (fp == NULL) |
rgoto | 0:0a9f4da8e642 | 158 | { |
rgoto | 0:0a9f4da8e642 | 159 | printf("open error!!\r\n"); |
rgoto | 0:0a9f4da8e642 | 160 | // while(1); |
rgoto | 0:0a9f4da8e642 | 161 | }else{ |
rgoto | 0:0a9f4da8e642 | 162 | open_flg = 2; |
rgoto | 0:0a9f4da8e642 | 163 | } |
rgoto | 0:0a9f4da8e642 | 164 | } |
rgoto | 0:0a9f4da8e642 | 165 | |
rgoto | 0:0a9f4da8e642 | 166 | if(write_flg == 1){ |
rgoto | 0:0a9f4da8e642 | 167 | // fprintf(fp, "check\r\n"); |
rgoto | 0:0a9f4da8e642 | 168 | fwrite(rxData2, sizeof(char), TRANSFER_SIZE, fp); |
rgoto | 0:0a9f4da8e642 | 169 | wait_ms(1); |
rgoto | 0:0a9f4da8e642 | 170 | |
rgoto | 0:0a9f4da8e642 | 171 | fclose(fp); |
rgoto | 0:0a9f4da8e642 | 172 | wait_ms(1); |
rgoto | 0:0a9f4da8e642 | 173 | // close_flg = 0; |
rgoto | 0:0a9f4da8e642 | 174 | write_flg = 0; |
rgoto | 0:0a9f4da8e642 | 175 | open_flg = 1; |
rgoto | 0:0a9f4da8e642 | 176 | } |
rgoto | 0:0a9f4da8e642 | 177 | |
rgoto | 0:0a9f4da8e642 | 178 | rcv_flg = 1; |
rgoto | 0:0a9f4da8e642 | 179 | // wait(1); |
rgoto | 0:0a9f4da8e642 | 180 | } |
rgoto | 0:0a9f4da8e642 | 181 | |
rgoto | 0:0a9f4da8e642 | 182 | if (snd_flg==1) {//送信用 送信側もログをSDカードに書き込むようにする。 |
rgoto | 0:0a9f4da8e642 | 183 | snd_flg=0; |
rgoto | 0:0a9f4da8e642 | 184 | |
rgoto | 0:0a9f4da8e642 | 185 | if(open_flg == 1){ |
rgoto | 0:0a9f4da8e642 | 186 | fp = fopen("/sd/transfer_log.txt", "a"); |
rgoto | 0:0a9f4da8e642 | 187 | if (fp == NULL) |
rgoto | 0:0a9f4da8e642 | 188 | { |
rgoto | 0:0a9f4da8e642 | 189 | printf("open error!!\r\n"); |
rgoto | 0:0a9f4da8e642 | 190 | // while(1); |
rgoto | 0:0a9f4da8e642 | 191 | }else{ |
rgoto | 0:0a9f4da8e642 | 192 | open_flg = 2; |
rgoto | 0:0a9f4da8e642 | 193 | } |
rgoto | 0:0a9f4da8e642 | 194 | } |
rgoto | 0:0a9f4da8e642 | 195 | |
rgoto | 0:0a9f4da8e642 | 196 | if(write_flg == 1){ |
rgoto | 0:0a9f4da8e642 | 197 | // fprintf(fp, "check\r\n"); |
rgoto | 0:0a9f4da8e642 | 198 | fwrite(txData2, sizeof(char), TRANSFER_SIZE, fp); |
rgoto | 0:0a9f4da8e642 | 199 | wait_ms(1); |
rgoto | 0:0a9f4da8e642 | 200 | |
rgoto | 0:0a9f4da8e642 | 201 | fclose(fp); |
rgoto | 0:0a9f4da8e642 | 202 | wait_ms(1); |
rgoto | 0:0a9f4da8e642 | 203 | // close_flg = 0; |
rgoto | 0:0a9f4da8e642 | 204 | write_flg = 0; |
rgoto | 0:0a9f4da8e642 | 205 | open_flg = 1; |
rgoto | 0:0a9f4da8e642 | 206 | } |
rgoto | 0:0a9f4da8e642 | 207 | |
rgoto | 0:0a9f4da8e642 | 208 | my_nrf24l01p.write( NRF24L01P_PIPE_P0, (char *)txData2 , TRANSFER_SIZE ); |
rgoto | 0:0a9f4da8e642 | 209 | printf("1\r\n"); |
rgoto | 0:0a9f4da8e642 | 210 | wait_ms(1); |
rgoto | 0:0a9f4da8e642 | 211 | } |
rgoto | 0:0a9f4da8e642 | 212 | |
rgoto | 0:0a9f4da8e642 | 213 | |
rgoto | 0:0a9f4da8e642 | 214 | |
rgoto | 0:0a9f4da8e642 | 215 | } //一番最初のwhileの} |
rgoto | 0:0a9f4da8e642 | 216 | } |