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

Dependencies:   mbed nRF24L01P SDFileSystem

Committer:
rgoto
Date:
Fri Dec 25 01:05:42 2020 +0000
Revision:
2:4a917b67a400
Parent:
1:fd3967c16fcf
L432KC wo tukatta kiban sakusei sita kekka   buji ugokuyouninatta

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