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.
main.cpp
00001 #include "mbed.h" 00002 #include "SakuraIO.h" 00003 00004 Serial pc(USBTX, USBRX); 00005 DigitalOut myled(LED1); 00006 LocalFileSystem local("local"); 00007 00008 void rx_interrupt(); 00009 void err2str(int err); 00010 00011 int main() 00012 { 00013 FILE *fp ; 00014 pc.baud(115200); 00015 fp = fopen("/local/file.bin", "wb"); 00016 if (fp == NULL) { 00017 pc.printf("error: fp is null\r\n"); 00018 } 00019 00020 I2C i2c(p9, p10); 00021 SakuraIO_I2C sakuraio(i2c); 00022 00023 pc.printf("waiting to come online"); 00024 for (;;) { 00025 if ((sakuraio.getConnectionStatus() & 0x80) == 0x80) 00026 break; 00027 pc.printf("."); 00028 wait_ms(1000); 00029 } 00030 pc.printf("ok\r\n"); 00031 00032 uint8_t status = 0; 00033 while (status != 1) { 00034 status = sakuraio.startFileDownload(1); 00035 pc.printf("file req result: %d\r\n", status); 00036 if (status != CMD_ERROR_NONE) { 00037 err2str(status); 00038 pc.printf("\r\n"); 00039 } 00040 wait_ms(100); 00041 pc.printf("cancel\r\n"); 00042 uint8_t cancel_result = sakuraio.cancelFileDownload(); 00043 err2str(cancel_result); 00044 pc.printf("\r\n"); 00045 wait_ms(100); 00046 } 00047 00048 00049 00050 uint32_t received_size = 0; 00051 00052 uint8_t file_status = 0; 00053 uint32_t file_total_size = 0, file_crc32 = 0; 00054 uint64_t file_timestamp = 0; 00055 while(file_status == 0) { 00056 00057 sakuraio.getFileMetaData(&file_status, &file_total_size, &file_timestamp, &file_crc32); 00058 00059 pc.printf("metadata status: %d filesize: %d crc32: %08x\r\n", file_status, file_total_size, file_crc32); 00060 00061 pc.printf("date "); 00062 for (int i = 0; i < 8; i++) { 00063 pc.printf("%02x ", ((uint8_t *)&file_timestamp)[i]); 00064 } 00065 pc.printf("\r\n"); 00066 wait(1); 00067 } 00068 00069 while (1) { 00070 status = 0; 00071 uint32_t s = 0; 00072 uint8_t a = sakuraio.getFileDownloadStatus(&status, &s); 00073 pc.printf("result: "); 00074 err2str(a); 00075 pc.printf("status %d size %d\r\n", status, s); 00076 wait_ms(500); 00077 00078 uint8_t buf[255] = {0}; 00079 uint8_t len = 251; // request size 00080 uint8_t fileget = sakuraio.getFileData(&len, buf); 00081 // if (fileget != cmd_error_none) { 00082 // pc.printf("download waiting...\r\n"); 00083 // wait_ms(1000); 00084 // continue; 00085 // //break; 00086 // } 00087 00088 pc.printf("file get "); 00089 err2str(fileget); 00090 if (fileget != 1) { 00091 pc.printf("error: get_data returned invalid status\r\n"); 00092 while(1) { 00093 wait(1); 00094 } 00095 } 00096 pc.printf("len %d\r\n", len); 00097 received_size += len; 00098 pc.printf("received %08x\r\n", len); 00099 for (int i = 0; i < len; i++) { 00100 pc.printf("%02x ", buf[i]); 00101 } 00102 fwrite(buf, len, 1, fp); 00103 pc.printf("\r\n"); 00104 pc.printf("%d / %d (%f%%)\r\n", received_size, file_total_size, ((double)received_size / file_total_size) * 100); 00105 if (received_size >= file_total_size) { 00106 break; 00107 } 00108 wait_ms(100); 00109 } 00110 fclose(fp); 00111 pc.printf("download complete!\r\n"); 00112 00113 // block 00114 while (1) 00115 ; 00116 } 00117 00118 void err2str(int err) 00119 { 00120 switch (err) { 00121 case CMD_ERROR_NONE: 00122 printf("no error."); 00123 break; 00124 case CMD_ERROR_PARITY: 00125 printf("parity error."); 00126 break; 00127 case CMD_ERROR_MISSING: 00128 printf("command missing."); 00129 break; 00130 case CMD_ERROR_INVALID_SYNTAX: 00131 printf("invalid syntax."); 00132 break; 00133 case CMD_ERROR_RUNTIME: 00134 printf("runtime error."); 00135 break; 00136 case CMD_ERROR_LOCKED: 00137 printf("command locked."); 00138 break; 00139 case CMD_ERROR_BUSY: 00140 printf("busy."); 00141 break; 00142 } 00143 }
Generated on Wed Jul 13 2022 19:36:30 by
1.7.2