L432KCで通信モジュールとSDカード書き込みを動かすプログラムです。

Dependencies:   mbed nRF24L01P SDFileSystem

Committer:
rgoto
Date:
Tue Dec 22 04:19:15 2020 +0000
Revision:
1:fd3967c16fcf
Parent:
0:0a9f4da8e642
Child:
2:4a917b67a400
ver2:  L432KC de ugokaseru code !!

Who changed what in which revision?

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