Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: FatFileSystemCpp mbed PowerControl USBHostLite
wifi.cpp
00001 #include "wifi.h" 00002 #include "utils.h" 00003 00004 #define BAUDR 460800 00005 //#define BAUDR 1100000 00006 00007 Wifi::Wifi (PinName tx, PinName rx, PinName cts, PinName reset): 00008 wifi_(tx, rx) { 00009 // mbed only has CTS available, communication not working properly 00010 //wifi_.set_flow_control(SerialBase::CTS, cts); 00011 wifi_.baud(BAUDR); 00012 } 00013 00014 /* 00015 * Initializes wifly, serves as constructor, were changing PLL speed dynamically 00016 * 00017 */ 00018 void Wifi::init(void) 00019 { 00020 //wifi_.baud(BAUDR); 00021 } 00022 00023 /* 00024 * Format: (int) -1 (int) -1 (int) swimmer_id 00025 * many times (int) count 3*(short) acc 3*(float)gyro 00026 * (int) -1 (int) -1 up to 1023 zeros 00027 */ 00028 int Wifi::sendFile(const char *fname) 00029 { 00030 char c; 00031 int last = 1; 00032 00033 FILE *file = fopen(fname, "rb"); 00034 if (file == NULL) { 00035 printf("Unable to open %s\r\n", fname); 00036 return -1; 00037 } 00038 00039 fseek(file, 0, SEEK_END); // seek to end of file 00040 int size = ftell(file); // get current file pointer 00041 fseek(file, 0, SEEK_SET); // seek back to beginning of file 00042 00043 wifi_.putc('#'); // send ACK 00044 wifi_.putc('A'); 00045 for(int i = 0; i < FRAME_HEADER_LEN - 2; i++) // send num of bytes in the message 00046 { 00047 wifi_.putc((char)((size >> (i * 8)) & 0xFF)); 00048 } 00049 00050 printf("Ack sended...\r\n"); 00051 printf("Transfer of %d bytes initialized...\r\n", size); 00052 00053 if(size == 0) // when nothing to send, terminated here 00054 { 00055 return 0; 00056 } 00057 00058 in_buf = 0; 00059 while (fread(&c, sizeof(char), 1, file) != 0) { 00060 if ((last = bufferSendVerify(c)) == -1) { 00061 fclose(file); 00062 return -1; 00063 } 00064 } 00065 fclose(file); 00066 00067 if (bufferFlush() == -1) { 00068 return -1; 00069 } 00070 00071 return 0; 00072 } 00073 00074 /* 00075 * Returns length of command data received, load the command into the cmd array and the data into the data array. 00076 */ 00077 int Wifi::getCmd(char* cmd, char* data) 00078 { 00079 short dataLength = 0u; 00080 cmd[0] = '#'; 00081 cmd[1] = 'N'; 00082 00083 /* Read the command */ 00084 if (wifi_.readable() && wifi_.getc() == '#') 00085 { 00086 for(int i = 1; i < FRAME_HEADER_LEN; i++) 00087 { 00088 wait_ms(20); 00089 if (wifi_.readable()) 00090 { 00091 cmd[i] = wifi_.getc(); 00092 } 00093 else 00094 { 00095 cmd[1] = 'N'; 00096 } 00097 } 00098 } 00099 00100 /* Read count of data */ 00101 if(cmd[1] == 'N') 00102 { 00103 return 0; 00104 } 00105 00106 for(int i = 0; i < FRAME_HEADER_LEN - 2; i++) 00107 { 00108 dataLength += (int)cmd[i+2] << (i * 8); 00109 } 00110 00111 /* Read data */ 00112 for(int i = 0; i < dataLength; i++) 00113 { 00114 wait_ms(20); 00115 if (wifi_.readable()) 00116 { 00117 data[i] = wifi_.getc(); 00118 } 00119 } 00120 00121 return dataLength; 00122 } 00123 00124 /* 00125 * Sends given byte to wifly 00126 */ 00127 void Wifi::sendByte(char byte) 00128 { 00129 // Send byte 00130 wifi_.putc(byte); 00131 } 00132 00133 /* 00134 * Reads byte from wifly 00135 */ 00136 char Wifi::readByte(void) 00137 { 00138 // Read byte 00139 return wifi_.getc(); 00140 } 00141 00142 /* 00143 * Returns true if some bytes are in the buffer 00144 */ 00145 bool Wifi::readable(void) 00146 { 00147 if (wifi_.readable()) 00148 { 00149 return true; 00150 } 00151 else 00152 { 00153 return false; 00154 } 00155 } 00156 00157 /* 00158 * Sends ACK to wiflys UART 00159 */ 00160 void Wifi::sendAck(void) 00161 { 00162 wifi_.putc('#'); 00163 wifi_.putc('A'); 00164 for(int i = 0; i < FRAME_HEADER_LEN - 2; i++) 00165 { 00166 wifi_.putc(0x0); 00167 } 00168 } 00169 00170 /* 00171 * Sends NACK to wiflys UART 00172 */ 00173 void Wifi::sendNack(void) 00174 { 00175 wifi_.putc('#'); 00176 wifi_.putc('N'); 00177 for(int i = 0; i < FRAME_HEADER_LEN - 2; i++) 00178 { 00179 wifi_.putc(0x0); 00180 } 00181 } 00182 00183 /* 00184 * Sends NACK to wiflys UART 00185 */ 00186 void Wifi::sendFail(void) 00187 { 00188 wifi_.putc('#'); 00189 wifi_.putc('F'); 00190 for(int i = 0; i < FRAME_HEADER_LEN - 2; i++) 00191 { 00192 wifi_.putc(0x0); 00193 } 00194 } 00195 00196 int Wifi::waitForAck() 00197 { 00198 char cmd[FRAME_HEADER_LEN]; 00199 char data[256]; 00200 00201 for (int i=0; i<64; i++) 00202 { 00203 getCmd(cmd, data); 00204 00205 if ('A' == cmd[1]) 00206 { 00207 return 1; 00208 } 00209 else if ('F' == cmd[1]) 00210 { 00211 return 0; 00212 } 00213 else 00214 { 00215 wait_ms(5); 00216 } 00217 } 00218 return -1; 00219 00220 //if (wifi_.getc() == 'A') 00221 // break; 00222 } 00223 00224 /* ******************************** 00225 * private 00226 * *******************************/ 00227 00228 void Wifi::bufferSend(size_t size) 00229 { 00230 int i; 00231 for (i = 0; i < size; i++) { 00232 wifi_.putc(buffer[i]); 00233 } 00234 } 00235 00236 int Wifi::bufferFlush() 00237 { 00238 LPC_WDT->WDFEED = 0xAA; 00239 LPC_WDT->WDFEED = 0x55; 00240 int counter = 0; 00241 int result = 0; 00242 00243 if (in_buf == 0) 00244 { 00245 return 1; 00246 } 00247 00248 while (counter++ < 10) 00249 { 00250 bufferSend(in_buf); 00251 result = waitForAck(); 00252 00253 if(result == 1) 00254 { 00255 /* Ack - break, no need to send it again */ 00256 break; 00257 } 00258 else if (result == -1) 00259 { 00260 /* Fail - transmition faild, terminate sending */ 00261 return -1; 00262 } 00263 else 00264 { 00265 /* Nack - send it again*/ 00266 } 00267 } 00268 00269 if (counter >= 10) 00270 { 00271 return -1; 00272 } 00273 else 00274 { 00275 return 0; 00276 } 00277 } 00278 00279 int Wifi::bufferSendVerify(char c) 00280 { 00281 LPC_WDT->WDFEED = 0xAA; 00282 LPC_WDT->WDFEED = 0x55; 00283 int counter = 0; 00284 int result = 0; 00285 buffer[in_buf] = c; 00286 in_buf++; 00287 00288 if (in_buf != SEND_SIZE) 00289 { 00290 return 1; 00291 } 00292 00293 in_buf = 0; 00294 while (counter++ < 16) 00295 { 00296 bufferSend(SEND_SIZE); 00297 result = waitForAck(); 00298 00299 if(result == 1) 00300 { 00301 /* Ack - break, no need to send it again */ 00302 break; 00303 } 00304 else if (result == -1) 00305 { 00306 /* Fail - transmition faild, terminate sending */ 00307 return -1; 00308 } 00309 else 00310 { 00311 /* Nack - send it again*/ 00312 } 00313 } 00314 00315 if (counter >= 10) 00316 { 00317 return -1; 00318 } 00319 else 00320 { 00321 return 0; 00322 } 00323 }
Generated on Mon Jul 18 2022 01:02:56 by
