int main ( int argc, char** argv ) { char* hexfile = NULL; char* devname = NULL; ISP_EID eid = EID_NOK; IspMessage ispmsg; /* * TODO: enhance the argument parsing */ if (argc < 5) { printf("[Error Usage]:\n" "at892prog -f <hex file name>\n" " -d <serial device>\n"); return -1; } else { hexfile = argv[2]; devname = argv[4]; } // init memset(&serial_dev, 0, sizeof(SerialDevice)); // prepare message header ispmsg.hdr.typ = MSGT_MEM_WRITE; ispmsg.hdr.len = DATA_SIZE; ispmsg.hdr.crc = 0x0000; // message data content eid = read_hexfile(hexfile, ispmsg.msg.data, &ispmsg.hdr.len); if (eid != EID_OK) { printf("Could not read hex file: eid=%d errno=%s\n", eid, strerror(errno)); goto EXIT_PROGRAM; } printf("Read hex file successfully: %d bytes\n", ispmsg.hdr.len); // data crc ispmsg.hdr.crc = gen_crc16(ispmsg.msg.data, ispmsg.hdr.len); printf("Data CRC: %04x\n", ispmsg.hdr.crc); // encode message eid = encode_message(&ispmsg, send_buffer); if (eid != EID_OK) { printf("Encode message failed: eid=%d errno=%s\n", eid, strerror(errno)); goto EXIT_PROGRAM; } printf("Message was encoded successfully\n"); // open connection to TTY device eid = serial_open(devname, &serial_dev, &serial_cfg); if (eid != EID_OK) { printf("Open TTY device failed: eid=%d errno=%s\n", eid, strerror(errno)); goto EXIT_PROGRAM; } printf("Connect to %s successfully\n", devname); // create TTY receiving thread if (pthread_create(&tid_receiving, NULL, get_data_routine, NULL) < 0) { printf("Cannot create thread: %s\n", strerror(errno)); goto EXIT_PROGRAM; } // send message to device eid = serial_send(&serial_dev, send_buffer, MESSAGE_SIZE); if (eid != EID_OK) { printf("Message sent failed: eid=%d errno=%s\n", eid, strerror(errno)); goto EXIT_PROGRAM; } else printf("Sent %d bytes\n", MESSAGE_SIZE); EXIT_PROGRAM: // waiting for thread done pthread_join(tid_receiving, NULL); if (eid != EID_OK) { printf("Received serial data failed: eid=%d errno=%s\n", eid, strerror(errno)); } eid = serial_close(&serial_dev); if (eid != EID_OK) { printf("Device closed fail: eid=%d errno=%s\n", eid, strerror(errno)); } printf("Device closed\n"); return 0; }
int main(int argc, char *argv[]) { uint8_t *buf; int i; if (NULL == (buf=malloc(FLASH_SIZE))) { fprintf(stderr, "out of ram\n"); return 1; } if (0 != parse_options(argc, argv)) { usage(); return 1; } if (!opt_flash) { usage(); return 1; } if (opt_flash) { if (0 != dbg_init()) { fprintf(stderr, "Failed to initialise (run as root for /dev/mem access)\n"); return 1; } memset(buf, 0xFF, FLASH_SIZE); if (0 != read_hexfile(buf, FLASH_SIZE, flash_filename)) { fprintf(stderr, "Failed to read %s\n", flash_filename); return 1; } if (0 != dbg_mass_erase()) { fprintf(stderr, "CC1110 mass erase failed\n"); return 1; } for (i=0;i<FLASH_SIZE;i+=1024) { bool skip = true; int j; for (j=i;j<i+1024;j++) { if (buf[j] != 0xFF) { skip = false; break; } } if (skip) { printf("Skipping blank page %d\n", i/1024); continue; } printf("Programming and verifying page %d\n", i/1024); if (0 != program_verify_page(buf + i, i/1024)) { fprintf(stderr, "FAILED\n"); return 1; } } printf("Programming complete\n"); dbg_reset(); } return 0; }
int main(int argc, char *argv[]) { int fd; int i; struct context ctx; if (0 != parse_options(argc, argv)) { usage(); return 1; } if ((fd = serialOpen(device_name)) < 0) { fprintf(stderr, "Failed to open %s\n", device_name); return 1; } ctx.fd = fd; if (opt_flash) { if (0 != check_for_bootloader(fd, opt_timeout)) { fprintf(stderr, "No bootloader detected\n"); return 1; } else { printf("Bootloader detected\n"); } for (i=0;i<FLASH_SIZE_KB/FLASH_PAGE_SIZE;i++) { printf("Erasing page %d\n", i); if (0 != erase_page(fd, i)) { fprintf(stderr, "Erasing page failed\n"); return 1; } } if (0 != read_hexfile(flash_filename, handle_record_00, handle_record_04, &ctx)) { fprintf(stderr, "Failed to read %s\n", flash_filename); return 1; } if (0 != reset(fd)) { fprintf(stderr, "Reset failed\n"); return 1; } } if (opt_console) { atexit(do_exit); printf("Connected to %s, ctrl-c to exit\n", device_name); do_console(fd); } serialClose(fd); return 0; }