int ubigen_write_leb(ubi_info_t u, ubigen_action_t action) { int rc = 0; size_t read = 0; clear_buf(u); write_ec_hdr(u); rc = fill_data_buffer_from_file(u, &read); if (rc != 0) return rc; if (u->v->vol_type == UBI_VID_STATIC) { add_static_info(u, read, action); } u->v->lnum = cpu_to_ubi32(u->blks_written); if (action & MARK_AS_UPDATE) { u->v->copy_flag = (u->v->copy_flag)++; } write_vid_hdr(u, action); rc = write_to_output_stream(u); if (rc != 0) return rc; /* Update current handle */ u->bytes_read += read; u->blks_written++; return 0; }
int main() { freopen("robot_direct_prob_info.txt", "r", stdin); setbuf(stdout, NULL); int T, N, i; scanf("%d", &T); while (T--) { scanf("%d", &N); // east->west->south->north for (i = 0; i < 4; i++) { int x; scanf("%d", &x); prob[i] = x / 100.0; } double ans = 0.0; ans = get_prob(50, 50, N); printf("%f\n", ans); clear_buf(); } return 0; }
/* * Function change_switch * ---------------------- * get contact status, tos command 'C9' * * return: contact status * */ int get_contact_status(int contact_id) { Serial_Message msg; char input_buf[4]; char contact_buf[5]; char *buf_ptr = contact_buf; *buf_ptr = 'C'; buf_ptr++; /* Give more space if contact_id is bigger than 1 */ if (contact_id > 9) { k_memcpy(buf_ptr, int_to_string[contact_id], 2); buf_ptr += 2; } else { k_memcpy(buf_ptr, int_to_string[contact_id], 1); buf_ptr += 1; } *(buf_ptr) = '\015'; buf_ptr++; *(buf_ptr) = '\0'; msg.output_buffer = contact_buf; msg.input_buffer = input_buf; msg.len_input_buffer = sizeof(input_buf); clear_buf(); send(serial_port, &msg); cmd_sleep(); /* int i = 0; kprintf("Got Message:"); for (i = 0; i < 6; i++) { kprintf("%x ", input_buf[i]); } kprintf("\n");*/ return input_buf[1] - '0'; }
int main() { freopen("double_encodings.txt", "r", stdin); setbuf(stdout, NULL); int T, tc, i, j, k; scanf("%d", &T); for (tc = 1; tc <= T; tc++) { // input: a_cnt = 0, b_cnt = 0, c_cnt = 0; input_target_words(); //print_target_words(a_cnt, b_cnt, c_cnt); // solve: matched_cnt = 0; no_match = false; ed_idx = -1; /* check matched with B and C*/ check_matched(); //print_target_words(a_cnt, b_cnt, c_cnt); // output: print_outputs(); // clear buf: clear_buf(); } return 0; }
/* DoCreateStringOutput (static) * * Private function used to send a C-string through the 1847 creator and * eventually pipe the 1847 output to a file. */ static int DoCreateStringOutput(const char *pStr, int nPart, emsMIMEtypeP mimePtr, createStatePtr pState, FILE *fOut) { BufTypePtr pTmpBuf = make_buf(); int ret = RFC1847_FAIL; if (pTmpBuf) { // Manually setup the buffer structure -- kind of ugly, but // more effiecient than copying the string pTmpBuf->buf = (char *) pStr; pTmpBuf->len = strlen(pStr); pTmpBuf->buf_size = pTmpBuf->len + 1; resetpos_buf(pTmpBuf); // Now output the buffer ret = DoCreatePartsOutput(mimePtr, ((nPart == 1) ? pTmpBuf : NULL), /* Part 1 */ ((nPart == 2) ? pTmpBuf : NULL), /* Part 2 */ pState, fOut); clear_buf(pTmpBuf); // Unassociate buffer with string delete_buf(pTmpBuf); // Delete buffer struct -- AFTER we clear it's contents } else return (FALSE); // Could not create buffer return (ret != RFC1847_FAIL); }
int main() { setbuf(stdout, NULL); int T, tc, i; scanf("%d", &T); for (tc = 1; tc <= T; tc++) { scanf("%s %s", &str1, &str2); size_t len1, len2; for (len1 = 0; str1[len1]; len1++); for (len2 = 0; str2[len2]; len2++); bool ans = one_edit_away(str1, str2, len1, len2); if (ans) printf("Yes, Right!\n"); else printf("No way T.T\n"); clear_buf(); } return 0; }
int main() { setbuf(stdout, NULL); int T, tc, i, j; scanf("%d", &T); for (tc = 1; tc <= T; tc++) { // inputs scanf("%d%d", &N, &M); /* Save the suspects info to suspects array*/ for (i = 0; i < M; i++) scanf("%d", &suspects[i]); // solve /* Play games */ for (i = 0; i < M; i++) { /* suspect get 1 point always */ int curr_suspt = suspects[i]; players[curr_suspt]++; /* get the answers from players*/ /* player : 1, 2, 3 ... */ int losers = 0; for (j = 1; j <= N; j++) { int x; scanf("%d", &x); /* get the points if answer == suspect in this game */ if (x == curr_suspt && j != curr_suspt) players[j]++; /* count players who write wrong answer */ else if (j != curr_suspt) losers++; } /* current suspect gets more points from losers */ players[curr_suspt] += losers; } // output /* print each player's game point */ for (i = 1; i <= N; i++) printf("%d ", players[i]); printf("\n"); // clear buffer for next games clear_buf(); } return 0; }
int get_char(void) { int ch = getchar(); if (ch == '\n') ungetc(ch, stdin); clear_buf(); return ch; }
void fill_header(char *name, char *comment, header_t *header) { if (my_strlen(name) > PROG_NAME_LENGTH) { my_putstr("Error : The Name is too long\n"); exit(EXIT_FAILURE); } if (my_strlen(comment) > COMMENT_LENGTH) { my_putstr("Error : The comment is too long\n"); exit(EXIT_FAILURE); } if (verif_endian() == 1) header->magic = INVERT_ENDIAN(COREWAR_EXEC_MAGIC); clear_buf(header->prog_name, PROG_NAME_LENGTH + 1); my_strcpy(header->prog_name, name); header->prog_size = 0; clear_buf(header->comment, COMMENT_LENGTH + 1); my_strcpy(header->comment, comment); }
void test_crypto (const struct crypto_options *co, struct frame* frame) { int i, j; struct gc_arena gc = gc_new (); struct buffer src = alloc_buf_gc (TUN_MTU_SIZE (frame), &gc); struct buffer work = alloc_buf_gc (BUF_SIZE (frame), &gc); struct buffer encrypt_workspace = alloc_buf_gc (BUF_SIZE (frame), &gc); struct buffer decrypt_workspace = alloc_buf_gc (BUF_SIZE (frame), &gc); struct buffer buf = clear_buf(); /* init work */ ASSERT (buf_init (&work, FRAME_HEADROOM (frame))); msg (M_INFO, "Entering " PACKAGE_NAME " crypto self-test mode."); for (i = 1; i <= TUN_MTU_SIZE (frame); ++i) { update_time (); msg (M_INFO, "TESTING ENCRYPT/DECRYPT of packet length=%d", i); /* * Load src with random data. */ ASSERT (buf_init (&src, 0)); ASSERT (i <= src.capacity); src.len = i; ASSERT (rand_bytes (BPTR (&src), BLEN (&src))); /* copy source to input buf */ buf = work; memcpy (buf_write_alloc (&buf, BLEN (&src)), BPTR (&src), BLEN (&src)); /* encrypt */ openvpn_encrypt (&buf, encrypt_workspace, co, frame); /* decrypt */ openvpn_decrypt (&buf, decrypt_workspace, co, frame); /* compare */ if (buf.len != src.len) msg (M_FATAL, "SELF TEST FAILED, src.len=%d buf.len=%d", src.len, buf.len); for (j = 0; j < i; ++j) { const uint8_t in = *(BPTR (&src) + j); const uint8_t out = *(BPTR (&buf) + j); if (in != out) msg (M_FATAL, "SELF TEST FAILED, pos=%d in=%d out=%d", j, in, out); } } msg (M_INFO, PACKAGE_NAME " crypto self-test mode SUCCEEDED."); gc_free (&gc); }
char set_interval() { interval = current_command[1]; clear_buf(); if (interval >= INTERVAL_1US && interval <= INTERVAL_500MS) return STATE_GET_COMMAND; interval = INTERVAL_1US; return STATE_UNKNOWN_COMMAND; }
/* Read a move string from a stream. Returns the length of the string if successfull (a move string is found). Returns -1 if EOF is reached, or 0 if the word isn't a move string. */ static int read_move(char *word, int lim, FILE *fp) { int i; int c; /* Skip leading spaces. */ do { c = fgetc(fp); } while (isspace(c)); if (c == EOF) return -1; /* Save the first character of the word. */ *word++ = (char)c; /* The word is not a move string. */ if (!isalpha(c)) { /* If the line starts with a '[' we skip the whole line. Skipping until the closing ']' wouldn't be safe because PGN files often have those pairs broken. */ if (c == '[') clear_buf(fp); /* Skip everything inside brackets. */ else if (c == '(' || c == '{') { int open_b = c; /* opening bracket */ int close_b; /* closing bracket */ int nb = 1; /* num. of brackets */ if (c == '(') close_b = ')'; else close_b = '}'; while ((c = fgetc(fp)) != EOF) { if (c == open_b) nb++; else if (c == close_b && --nb <= 0) break; } } return 0; } /* Get the rest of the move string. */ for (i = 1; i < lim - 1; i++) { c = fgetc(fp); if (isspace(c)) break; *word++ = (char)c; } *word = '\0'; return i; }
void reset() { stop(); clear_buf(); mode = MODE_INTERVAL; trigger = TRIGGER_IMMEDIATE; interval = INTERVAL_1MS; trigger_pins = 0; state = STATE_GET_COMMAND; tick = 0; led(0,1,0); }
char set_mode() { mode = current_command[1]; clear_buf(); if (mode == MODE_INTERVAL) return STATE_GET_COMMAND; if (mode == MODE_EVENT) return STATE_GET_COMMAND; mode = MODE_INTERVAL; return STATE_UNKNOWN_COMMAND; }
int main() { setbuf(stdout, NULL); int T, tc; scanf("%d", &T); for (tc = 1; tc <= T; tc++) { scanf("%d%d", &n, &k); printf("\n Minimum number of trials in worst cases with %d eggs %d floors is %d\n", n, k, drop_eggs()); clear_buf(); } return 0; }
void menu_char(int x_adjust, int menu_wid, int odd, register unsigned char *menu_border) { register int i, j, k; int embold = 1; int base; int totwid = font.width; /* Read in the character bit map, with two blank lines on each end. */ for (i = 0; i < 2; i++) clear_buf (totwid, filterbuf[i]); for (i = font.height + 1; i >= 2; i--) fill_buf (font.width, filterbuf[i]); for (i = font.height + 2; i < font.height + 4; i++) clear_buf (totwid, filterbuf[i]); for (k=0; k<embold; k++) for (i=2; i<font.height+2; i++) for (j=totwid+1; j>=2; j--) filterbuf[i][j+1] |= filterbuf[i][j]; /* Initial base line for filtering depends on odd flag. */ base = (odd ? 1 : 2); /* Change bits in menu that correspond to character bitmap. */ for (i = font.height + base, k = 0; i >= base; i--, k++) { register RGBpixel *menu; menu = menu_addr + k * menu_wid + x_adjust; for (j = 0; j < (totwid + 3) - 1; j++, menu++ ) if ( filterbuf[i][j] ) { COPYRGB(*menu, menu_border); } } return; }
int main() { freopen("words.txt", "r", stdin); setbuf(stdout, NULL); int n, i, len; scanf("%d", &n); while (n--) { scanf("%s", str); printf("%s is unique?(0:false, 1:true): ", str); for (len = 0; str[len]; len++); printf("length = %d\n", len); printf("%d\n", is_uniq_chars(str, len)); clear_buf(); } }
void do_replay (CHAR_DATA *ch, char *argument) { if (IS_NPC(ch)) { send_to_char("You can't replay.\n\r",ch); return; } if (buf_string(ch->pcdata->buffer)[0] == '\0') { send_to_char("You have no tells to replay.\n\r",ch); return; } page_to_char(buf_string(ch->pcdata->buffer),ch); clear_buf(ch->pcdata->buffer); }
int main() { freopen("statements.txt", "r", stdin); setbuf(stdout, NULL); int T, x; scanf("%d", &T); while (T--) { scanf(" %[^\n]\n", str); x = 0; x = find_value(); printf("%d\n", x); clear_buf(); } return 0; }
int main() { freopen("statements.txt", "r", stdin); setbuf(stdout, NULL); int T; scanf("%d", &T); while (T--) { scanf(" %[^\n]\n", str); int i = 0, no1 = 0, no2 = 0; while (str[i]) { if (no1 == 0 && str[i] >= 48 && str[i] <= 57) { while (str[i] >= 48 && str[i] <= 57) { no1 = no1 * 10 + (str[i] - '0'); i++; } } else if (no2 == 0 && str[i] >= 48 && str[i] <= 57) { while (str[i] >= 48 && str[i] <= 57) { no2 = no2 * 10 + (str[i] - '0'); i++; } } i++; } printf("%d\n", no2 - no1); clear_buf(); } return 0; }
int main() { freopen("events_info_for_patroling.txt", "r", stdin); setbuf(stdout, NULL); int T, tc, i, a, b; scanf("%d", &T); for (tc = 1; tc <= T; tc++) { scanf("%d%d", &N, &evnts); evnts++; row[0] = col[0] = 1; // 첫번째 사건 row[1] = col[1] = N; // 두번째 사건 // 실제 입력되는 사건 3번째라고 두고, for (i = 2; i <= evnts; i++) scanf("%d%d", &row[i], &col[i]); a = b = 0; a = min_Dist(2, 1) + get_Dist(0, 2); b = min_Dist(2, 0) + get_Dist(1, 2); printf("%d\n", min(a, b)); if (a < b) { puts("1"); construct(2, 1, 1); } else { puts("2"); construct(2, 0, 2); } clear_buf(); } return 0; }
void train_test() { /* Check contact 2 should return 2 */ disable_keyboard(); Serial_Message msg; char input_buf[4]; char contact_buf[5]; char *buf_ptr = contact_buf; int contact_id = 2; *buf_ptr = 'C'; buf_ptr++; /* Give more space if contact_id is bigger than 1 */ if (contact_id > 9) { k_memcpy(buf_ptr, int_to_string[contact_id], 2); buf_ptr += 2; } else { k_memcpy(buf_ptr, int_to_string[contact_id], 1); buf_ptr += 1; } *(buf_ptr) = '\015'; buf_ptr++; *(buf_ptr) = '\0'; msg.output_buffer = contact_buf; msg.input_buffer = input_buf; msg.len_input_buffer = sizeof(input_buf); clear_buf(); kprintf("Send Message:"); send(serial_port, &msg); cmd_sleep(); int i = 0; kprintf("Got Message:"); for (i = 0; i < sizeof(input_buf); i++) { kprintf("%x ", input_buf[i]); } kprintf("\n"); kprintf("Result = %d, shoulde be 1", input_buf[1] - '0'); enable_keyboard(); }
int main() { freopen("making_maze.txt", "r", stdin); setbuf(stdout, NULL); int T; scanf("%d", &T); while (T--) { scanf("%d%d", &H, &W); int i, j; for (i = 0; i < H; i++) scanf("%s", MAP[i]); // start point scanf("%d%d", &st.u, &st.v); // move count scanf("%d", &M); // moving info for (i = 0; i < M; i++) scanf("%d%d", &mrow[i], &mcol[i]); // initialize board with -1 for (i = 0; i < H; i++) for (j = 0; j < W; j++) board[i][j] = -1; board[st.u][st.v] = 0; // solve int max_mv = 0; max_mv = bfs(); // output printf("%d\n", max_mv); // clear buffer clear_buf(); } return 0; }
HIDDEN void do_Char(int c, int xpos, int ypos, int odd) { register int i, j; int base; int totwid = font.width; int down; static float resbuf[FONTBUFSZ]; static RGBpixel fbline[FONTBUFSZ]; #if DEBUG_STRINGS fb_log( "do_Char: c='%c' xpos=%d ypos=%d odd=%d\n", c, xpos, ypos, odd ); #endif /* read in character bit map, with two blank lines on each end */ for (i = 0; i < 2; i++) clear_buf (totwid, filterbuf[i]); for (i = font.height + 1; i >= 2; i--) fill_buf (font.width, filterbuf[i]); for (i = font.height + 2; i < font.height + 4; i++) clear_buf (totwid, filterbuf[i]); (void)SignedChar( font.dir[c].up ); down = SignedChar( font.dir[c].down ); /* Initial base line for filtering depends on odd flag. */ base = (odd ? 1 : 2); /* Produce a RGBpixel buffer from a description of the character and the read back data from the frame buffer for anti-aliasing. */ for (i = font.height + base; i >= base; i--) { squash( filterbuf[i - 1], /* filter info */ filterbuf[i], filterbuf[i + 1], resbuf, totwid + 4 ); fb_read( fbp, xpos, ypos - down + i, (unsigned char *)fbline, totwid+3); for (j = 0; j < (totwid + 3) - 1; j++) { register int tmp; /* EDITOR'S NOTE : do not rearrange this code, the SUN compiler can't handle more complex expressions. */ tmp = fbline[j][RED] & 0377; fbline[j][RED] = (int)(paint[RED]*resbuf[j]+(1-resbuf[j])*tmp); fbline[j][RED] &= 0377; tmp = fbline[j][GRN] & 0377; fbline[j][GRN] = (int)(paint[GRN]*resbuf[j]+(1-resbuf[j])*tmp); fbline[j][GRN] &= 0377; tmp = fbline[j][BLU] & 0377; fbline[j][BLU] = (int)(paint[BLU]*resbuf[j]+(1-resbuf[j])*tmp); fbline[j][BLU] &= 0377; } fb_write( fbp, xpos, ypos - down + i, (unsigned char *)fbline, totwid+3 ); } return; }
/** * owhere <var> <value> */ void do_owhere(struct char_data *ch, const char *argument) { struct buf_type *buffer; OBJ_CMP_FN *cmp_fn; char buf[MAX_INPUT_LENGTH]; char arg[MAX_INPUT_LENGTH]; int iter; if (ch == NULL || IS_NPC(ch)) return; if (argument[0] == '\0') { send_to_char("\n\rFind what?\n\r", ch); return; } cmp_fn = NULL; one_argument(argument, arg); if (argument[0] == '?' || !str_prefix(argument, "help")) { show_help(ch->desc, "owhere", NULL); return; } buffer = new_buf(); if (!str_prefix(argument, "list")) { add_buf(buffer, "owhere: searchable property list\n\r"); for (iter = 0; obj_flags[iter].var[0] != '\0'; iter++) { sprintf(buf, "%-18.17s", obj_flags[iter].var); add_buf(buffer, buf); if ((iter % 2) == 1) add_buf(buffer, "\n\r"); } add_buf(buffer, "\n\r"); page_to_char(buf_string(buffer), ch); return; } sprintf(buf, "`#QUERY``: owhere %s\n\r\n\r", argument); add_buf(buffer, buf); for (iter = 0; obj_flags[iter].var[0] != '\0'; iter++) { if (!str_prefix(arg, obj_flags[iter].var)) { cmp_fn = (OBJ_CMP_FN *)obj_flags[iter].fn; argument = one_argument(argument, arg); break; } } if (cmp_fn == NULL) cmp_fn = obj_cmp_name; if (argument[0] == '?' || argument[0] == '\0') { clear_buf(buffer); sprintf(buf, "`#SYNTAX``:\n\r owhere %s <value>:\n\r\n\r", arg); add_buf(buffer, buf); (*cmp_fn)(object_iterator_start(&object_empty_filter), argument, buffer); page_to_char(buf_string(buffer), ch); } else { struct gameobject *obj, *opending; struct gameobject *in_obj; char *clr1; char *clr2; int number; number = 0; opending = object_iterator_start(&object_empty_filter); while ((obj = opending) != NULL) { opending = object_iterator(obj, &object_empty_filter); if (can_see_obj(ch, obj) && (*cmp_fn)(obj, argument, NULL)) { number++; if (number == 1) { sprintf(buf, "# vnum name where room\n\r"); add_buf(buffer, buf); sprintf(buf, "=== ====== =========================== ========================== =====\n\r"); add_buf(buffer, buf); } for (in_obj = obj; in_obj->in_obj != NULL; in_obj = in_obj->in_obj) continue; if (in_obj->carried_by != NULL && can_see(ch, in_obj->carried_by) && in_obj->carried_by->in_room != NULL) { clr1 = uncolor_str(obj->short_descr); clr2 = uncolor_str(PERS(in_obj->carried_by, ch)); sprintf(buf, "%-3d %-7ld %-26.26s %-25.25s %-7ld\n\r", number, obj->objprototype->vnum, clr1, clr2, in_obj->carried_by->in_room->vnum); free_string(clr1); free_string(clr2); } else if (in_obj->in_room != NULL && can_see_room(ch, in_obj->in_room)) { clr1 = uncolor_str(obj->short_descr); clr2 = uncolor_str(in_obj->in_room->name); sprintf(buf, "%-3d %-7ld %-26.26s %-25.25s %-7ld\n\r", number, obj->objprototype->vnum, clr1, clr2, in_obj->in_room->vnum); free_string(clr1); free_string(clr2); } else { clr1 = uncolor_str(obj->short_descr); sprintf(buf, "%-3d %-7ld %-26.26s\n\r", number, obj->objprototype->vnum, clr1); free_string(clr1); } buf[0] = UPPER(buf[0]); add_buf(buffer, buf); if (number >= MAX_RETURN) break; } } if (number == 0) send_to_char("Nothing like that in heaven or earth.\n\r", ch); else page_to_char(buf_string(buffer), ch); } free_buf(buffer); }
char run() { unsigned long period = (SysCtlClockGet() / multi2hz(interval)) / 2; stop(); led(1,0,0); clear_buf(); if (mode == MODE_INTERVAL) { ROM_IntEnable(INT_TIMER0A); ROM_TimerLoadSet(TIMER0_BASE, TIMER_A, period - 1); ROM_TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT); ROM_TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT); if (trigger == TRIGGER_IMMEDIATE) { ROM_TimerEnable(TIMER0_BASE, TIMER_A); } else if (trigger == TRIGGER_PIN_RISING) { ROM_GPIOIntTypeSet(GPIO_PORTB_BASE, trigger_pins, GPIO_RISING_EDGE); ROM_GPIOPinIntClear(GPIO_PORTB_BASE, 0xff); ROM_GPIOPinIntEnable(GPIO_PORTB_BASE, trigger_pins); ROM_IntEnable(INT_GPIOB); } else if (trigger == TRIGGER_PIN_FALLING) { ROM_GPIOIntTypeSet(GPIO_PORTB_BASE, trigger_pins, GPIO_FALLING_EDGE); ROM_GPIOPinIntClear(GPIO_PORTB_BASE, 0xff); ROM_GPIOPinIntEnable(GPIO_PORTB_BASE, trigger_pins); ROM_IntEnable(INT_GPIOB); } else if (trigger == TRIGGER_PIN_BOTH) { ROM_GPIOIntTypeSet(GPIO_PORTB_BASE, 1, GPIO_BOTH_EDGES); ROM_GPIOPinIntClear(GPIO_PORTB_BASE, 0xff); ROM_GPIOPinIntEnable(GPIO_PORTB_BASE, trigger_pins); ROM_IntEnable(INT_GPIOB); } } else if (mode == MODE_EVENT) { if (trigger == TRIGGER_PIN_FALLING) { ROM_GPIOIntTypeSet(GPIO_PORTB_BASE, trigger_pins, GPIO_FALLING_EDGE); ROM_GPIOPinIntClear(GPIO_PORTB_BASE, 0xff); ROM_GPIOPinIntEnable(GPIO_PORTB_BASE, trigger_pins); ROM_IntEnable(INT_GPIOB); } else if (trigger == TRIGGER_PIN_RISING) { ROM_GPIOIntTypeSet(GPIO_PORTB_BASE, trigger_pins, GPIO_RISING_EDGE); ROM_GPIOPinIntClear(GPIO_PORTB_BASE, 0xff); ROM_GPIOPinIntEnable(GPIO_PORTB_BASE, trigger_pins); ROM_IntEnable(INT_GPIOB); } else if (trigger == TRIGGER_PIN_BOTH) { ROM_GPIOIntTypeSet(GPIO_PORTB_BASE, trigger_pins, GPIO_BOTH_EDGES); ROM_GPIOPinIntClear(GPIO_PORTB_BASE, 0xff); ROM_GPIOPinIntEnable(GPIO_PORTB_BASE, trigger_pins); ROM_IntEnable(INT_GPIOB); } HWREG(NVIC_ST_CURRENT) = 0; } ROM_IntMasterEnable(); sampling = SAMPLING_ON; return STATE_GET_COMMAND; }
int main(int argc, char *argv[]) { /* Avoid unused parameter warning */ (void)argv; // set scpi lib debug level: 0 for no debug info, 10 for lots switch (argc) { case 1: fputs( "\nUsage: stlink-access-test [anything at all] ...\n" "\n*** Notice: The stlink firmware violates the USB standard.\n" "*** Because we just use libusb, we can just tell the kernel's\n" "*** driver to simply ignore the device...\n" "*** Unplug the stlink and execute once as root:\n" "modprobe -r usb-storage && modprobe usb-storage quirks=483:3744:i\n\n", stderr); return EXIT_FAILURE; default: break; } stlink_t *sl = stlink_v1_open(99, 1); if (sl == NULL) return EXIT_FAILURE; // we are in mass mode, go to swd stlink_enter_swd_mode(sl); stlink_current_mode(sl); stlink_core_id(sl); //---------------------------------------------------------------------- stlink_status(sl); //stlink_force_debug(sl); stlink_reset(sl); stlink_status(sl); // core system control block stlink_read_mem32(sl, 0xe000ed00, 4); DLOG("cpu id base register: SCB_CPUID = got 0x%08x expect 0x411fc231\n", read_uint32(sl->q_buf, 0)); // no MPU stlink_read_mem32(sl, 0xe000ed90, 4); DLOG("mpu type register: MPU_TYPER = got 0x%08x expect 0x0\n", read_uint32(sl->q_buf, 0)); #if 0 stlink_read_mem32(sl, 0xe000edf0, 4); DD(sl, "DHCSR = 0x%08x", read_uint32(sl->q_buf, 0)); stlink_read_mem32(sl, 0x4001100c, 4); DD(sl, "GPIOC_ODR = 0x%08x", read_uint32(sl->q_buf, 0)); #endif #if 0 // happy new year 2011: let blink all the leds // see "RM0041 Reference manual - STM32F100xx advanced ARM-based 32-bit MCUs" #define GPIOC 0x40011000 // port C #define GPIOC_CRH (GPIOC + 0x04) // port configuration register high #define GPIOC_ODR (GPIOC + 0x0c) // port output data register #define LED_BLUE (1<<8) // pin 8 #define LED_GREEN (1<<9) // pin 9 stlink_read_mem32(sl, GPIOC_CRH, 4); uint32_t io_conf = read_uint32(sl->q_buf, 0); DLOG("GPIOC_CRH = 0x%08x\n", io_conf); // set: general purpose output push-pull, output mode, max speed 10 MHz. write_uint32(sl->q_buf, 0x44444411); stlink_write_mem32(sl, GPIOC_CRH, 4); memset(sl->q_buf, 0, sizeof(sl->q_buf)); for (int i = 0; i < 100; i++) { write_uint32(sl->q_buf, LED_BLUE | LED_GREEN); stlink_write_mem32(sl, GPIOC_ODR, 4); /* stlink_read_mem32(sl, 0x4001100c, 4); */ /* DD(sl, "GPIOC_ODR = 0x%08x", read_uint32(sl->q_buf, 0)); */ usleep(100 * 1000); memset(sl->q_buf, 0, sizeof(sl->q_buf)); stlink_write_mem32(sl, GPIOC_ODR, 4); // PC lo usleep(100 * 1000); } write_uint32(sl->q_buf, io_conf); // set old state #endif #if 0 // TODO rtfm: stlink doesn't have flash write routines // writing to the flash area confuses the fw for the next read access //stlink_read_mem32(sl, 0, 1024*6); // flash 0x08000000 128kB fputs("++++++++++ read a flash at 0x0800 0000\n", stderr); stlink_read_mem32(sl, 0x08000000, 1024 * 6); //max 6kB clear_buf(sl); stlink_read_mem32(sl, 0x08000c00, 5); stlink_read_mem32(sl, 0x08000c00, 4); mark_buf(sl); stlink_write_mem32(sl, 0x08000c00, 4); stlink_read_mem32(sl, 0x08000c00, 256); stlink_read_mem32(sl, 0x08000c00, 256); #endif #if 0 // sram 0x20000000 8kB fputs("\n++++++++++ read/write 8bit, sram at 0x2000 0000 ++++++++++++++++\n\n", stderr); memset(sl->q_buf, 0, sizeof(sl->q_buf)); mark_buf(sl); //stlink_write_mem8(sl, 0x20000000, 16); //stlink_write_mem8(sl, 0x20000000, 1); //stlink_write_mem8(sl, 0x20000001, 1); stlink_write_mem8(sl, 0x2000000b, 3); stlink_read_mem32(sl, 0x20000000, 16); #endif #if 0 // a not aligned mem32 access doesn't work indeed fputs("\n++++++++++ read/write 32bit, sram at 0x2000 0000 ++++++++++++++++\n\n", stderr); memset(sl->q_buf, 0, sizeof(sl->q_buf)); mark_buf(sl); stlink_write_mem32(sl, 0x20000000, 1); stlink_read_mem32(sl, 0x20000000, 16); mark_buf(sl); stlink_write_mem32(sl, 0x20000001, 1); stlink_read_mem32(sl, 0x20000000, 16); mark_buf(sl); stlink_write_mem32(sl, 0x2000000b, 3); stlink_read_mem32(sl, 0x20000000, 16); mark_buf(sl); stlink_write_mem32(sl, 0x20000000, 17); stlink_read_mem32(sl, 0x20000000, 32); #endif #if 0 // sram 0x20000000 8kB fputs("++++++++++ read/write 32bit, sram at 0x2000 0000 ++++++++++++\n", stderr); memset(sl->q_buf, 0, sizeof(sl->q_buf)); mark_buf(sl); stlink_write_mem8(sl, 0x20000000, 64); stlink_read_mem32(sl, 0x20000000, 64); mark_buf(sl); stlink_write_mem32(sl, 0x20000000, 1024 * 8); //8kB stlink_read_mem32(sl, 0x20000000, 1024 * 6); stlink_read_mem32(sl, 0x20000000 + 1024 * 6, 1024 * 2); #endif #if 1 reg regs; stlink_read_all_regs(sl, ®s); stlink_step(sl); fputs("++++++++++ write r0 = 0x12345678\n", stderr); stlink_write_reg(sl, 0x12345678, 0); stlink_read_reg(sl, 0, ®s); stlink_read_all_regs(sl, ®s); #endif #if 0 stlink_run(sl); stlink_status(sl); stlink_force_debug(sl); stlink_status(sl); #endif #if 0 /* read the system bootloader */ fputs("\n++++++++++ reading bootloader ++++++++++++++++\n\n", stderr); stlink_fread(sl, "/tmp/barfoo", sl->sys_base, sl->sys_size); #endif #if 0 /* read the flash memory */ fputs("\n+++++++ read flash memory\n\n", stderr); /* mark_buf(sl); */ stlink_read_mem32(sl, 0x08000000, 4); #endif #if 0 /* flash programming */ fputs("\n+++++++ program flash memory\n\n", stderr); stlink_fwrite_flash(sl, "/tmp/foobar", 0x08000000); #endif #if 0 /* check file contents */ fputs("\n+++++++ check flash memory\n\n", stderr); { const int res = stlink_fcheck_flash(sl, "/tmp/foobar", 0x08000000); printf("_____ stlink_fcheck_flash() == %d\n", res); } #endif #if 0 fputs("\n+++++++ sram write and execute\n\n", stderr); stlink_fwrite_sram(sl, "/tmp/foobar", sl->sram_base); stlink_run_at(sl, sl->sram_base); #endif #if 0 stlink_run(sl); stlink_status(sl); //---------------------------------------------------------------------- // back to mass mode, just in case ... stlink_exit_debug_mode(sl); stlink_current_mode(sl); stlink_close(sl); #endif //fflush(stderr); fflush(stdout); return EXIT_SUCCESS; }
void sim900_connect(void) { uint8_t i = 0; uint8_t retry = 3; printk("connect...\r\n"); while(1) { switch(status) { case STATUS_POWERON: sim900_poweron(); clear_rx_buf(); printk("wait for at done\r\n"); for(i = 0; i < retry; i++) { sim900_write("AT\r\n", 4); //sim900_wait(5000000); OSTimeDlyHMSM(0, 0, 0, 400); if(!check_at()) { status = STATUS_CONFIG; break; } clear_rx_buf(); } break; //case STATUS_INIT: case STATUS_CONFIG: printk("wait for config done\r\n"); OSTimeDlyHMSM(0, 0, 10, 0); sim900_write("AT+CREG?\r\n", 10); OSTimeDlyHMSM(0, 0, 0, 100); sim900_write("AT+CIPMODE=1\r\n", 14); OSTimeDlyHMSM(0, 0, 0, 100); sim900_write("AT+CGATT?\r\n", 11); OSTimeDlyHMSM(0, 0, 0, 100); sim900_write("AT+CSTT=\"CMNET\"\r\n", 17); OSTimeDlyHMSM(0, 0, 0, 100); sim900_write("AT+CIICR\r\n", 10); OSTimeDlyHMSM(0, 0, 2, 0); printk("try to get ip address\r\n"); sim900_write("AT+CIFSR\r\n", 10); OSTimeDlyHMSM(0, 0, 0, 100); sim900_write("AT+CGATT=1\r\n", 12); OSTimeDlyHMSM(0, 0, 0, 100); sim900_write("AT+CGSN\r\n", 9); OSTimeDlyHMSM(0, 0, 0, 100); status = STATUS_CONNECTING; break; case STATUS_CONNECTING: printk("wait for connecting...\r\n"); sim900_write("AT+CIPSTATUS\r\n", 14); OSTimeDlyHMSM(0, 0, 2, 0); for(i = 0; i < 1; i++) { clear_rx_buf(); sim900_write(connect_cmd, strlen((char *)&connect_cmd[0])); OSTimeDlyHMSM(0, 0, 3, 0); if(!check_at()) { status = STATUS_CONNECTED; break; } clear_rx_buf(); } if(i >= 1) { //retry failed sim900_write("+++\r\n", 5); OSTimeDlyHMSM(0, 0, 1, 0); sim900_write("AT+CIPCLOSE\r\n", 13); OSTimeDlyHMSM(0, 0, 1, 0); sim900_write("AT+CIPSHUT\r\n", 12); OSTimeDlyHMSM(0, 0, 1, 0); status = STATUS_CONFIG; } break; case STATUS_CONNECTED: printk("Connect done!\r\n"); clear_buf(); return; default: break; } } }
int main(int argc, char *argv[]) { // set scpi lib debug level: 0 for no debug info, 10 for lots const int scsi_verbose = 2; char *dev_name; switch (argc) { case 1: fputs( "\nUsage: stlink-access-test /dev/sg0, sg1, ...\n" "\n*** Notice: The stlink firmware violates the USB standard.\n" "*** If you plug-in the discovery's stlink, wait a several\n" "*** minutes to let the kernel driver swallow the broken device.\n" "*** Watch:\ntail -f /var/log/messages\n" "*** This command sequence can shorten the waiting time and fix some issues.\n" "*** Unplug the stlink and execute once as root:\n" "modprobe -r usb-storage && modprobe usb-storage quirks=483:3744:lrwsro\n\n", stderr); return EXIT_FAILURE; case 2: dev_name = argv[1]; break; default: return EXIT_FAILURE; } fputs("*** stlink access test ***\n", stderr); fprintf(stderr, "Using sg_lib %s : scsi_pt %s\n", sg_lib_version(), scsi_pt_version()); stlink_t *sl = stlink_quirk_open(dev_name, scsi_verbose); if (sl == NULL) return EXIT_FAILURE; // we are in mass mode, go to swd stlink_enter_swd_mode(sl); stlink_current_mode(sl); stlink_core_id(sl); //---------------------------------------------------------------------- stlink_status(sl); //stlink_force_debug(sl); stlink_reset(sl); stlink_status(sl); #if 0 // core system control block stlink_read_mem32(sl, 0xe000ed00, 4); DD(sl, "cpu id base register: SCB_CPUID = got 0x%08x expect 0x411fc231", read_uint32(sl->q_buf, 0)); // no MPU stlink_read_mem32(sl, 0xe000ed90, 4); DD(sl, "mpu type register: MPU_TYPER = got 0x%08x expect 0x0", read_uint32(sl->q_buf, 0)); stlink_read_mem32(sl, 0xe000edf0, 4); DD(sl, "DHCSR = 0x%08x", read_uint32(sl->q_buf, 0)); stlink_read_mem32(sl, 0x4001100c, 4); DD(sl, "GPIOC_ODR = 0x%08x", read_uint32(sl->q_buf, 0)); #endif #if 0 // happy new year 2011: let blink all the leds // see "RM0041 Reference manual - STM32F100xx advanced ARM-based 32-bit MCUs" #define GPIOC 0x40011000 // port C #define GPIOC_CRH (GPIOC + 0x04) // port configuration register high #define GPIOC_ODR (GPIOC + 0x0c) // port output data register #define LED_BLUE (1<<8) // pin 8 #define LED_GREEN (1<<9) // pin 9 stlink_read_mem32(sl, GPIOC_CRH, 4); uint32_t io_conf = read_uint32(sl->q_buf, 0); DD(sl, "GPIOC_CRH = 0x%08x", io_conf); // set: general purpose output push-pull, output mode, max speed 10 MHz. write_uint32(sl->q_buf, 0x44444411); stlink_write_mem32(sl, GPIOC_CRH, 4); clear_buf(sl); for (int i = 0; i < 100; i++) { write_uint32(sl->q_buf, LED_BLUE | LED_GREEN); stlink_write_mem32(sl, GPIOC_ODR, 4); /* stlink_read_mem32(sl, 0x4001100c, 4); */ /* DD(sl, "GPIOC_ODR = 0x%08x", read_uint32(sl->q_buf, 0)); */ delay(100); clear_buf(sl); stlink_write_mem32(sl, GPIOC_ODR, 4); // PC lo delay(100); } write_uint32(sl->q_buf, io_conf); // set old state #endif #if 0 // TODO rtfm: stlink doesn't have flash write routines // writing to the flash area confuses the fw for the next read access //stlink_read_mem32(sl, 0, 1024*6); // flash 0x08000000 128kB fputs("++++++++++ read a flash at 0x0800 0000\n", stderr); stlink_read_mem32(sl, 0x08000000, 1024 * 6); //max 6kB clear_buf(sl); stlink_read_mem32(sl, 0x08000c00, 5); stlink_read_mem32(sl, 0x08000c00, 4); mark_buf(sl); stlink_write_mem32(sl, 0x08000c00, 4); stlink_read_mem32(sl, 0x08000c00, 256); stlink_read_mem32(sl, 0x08000c00, 256); #endif #if 0 // sram 0x20000000 8kB fputs("\n++++++++++ read/write 8bit, sram at 0x2000 0000 ++++++++++++++++\n\n", stderr); clear_buf(sl); stlink_write_mem8(sl, 0x20000000, 16); mark_buf(sl); stlink_write_mem8(sl, 0x20000000, 1); stlink_write_mem8(sl, 0x20000001, 1); stlink_write_mem8(sl, 0x2000000b, 3); stlink_read_mem32(sl, 0x20000000, 16); #endif #if 0 // a not aligned mem32 access doesn't work indeed fputs("\n++++++++++ read/write 32bit, sram at 0x2000 0000 ++++++++++++++++\n\n", stderr); clear_buf(sl); stlink_write_mem8(sl, 0x20000000, 32); mark_buf(sl); stlink_write_mem32(sl, 0x20000000, 1); stlink_read_mem32(sl, 0x20000000, 16); mark_buf(sl); stlink_write_mem32(sl, 0x20000001, 1); stlink_read_mem32(sl, 0x20000000, 16); mark_buf(sl); stlink_write_mem32(sl, 0x2000000b, 3); stlink_read_mem32(sl, 0x20000000, 16); mark_buf(sl); stlink_write_mem32(sl, 0x20000000, 17); stlink_read_mem32(sl, 0x20000000, 32); #endif #if 0 // sram 0x20000000 8kB fputs("++++++++++ read/write 32bit, sram at 0x2000 0000 ++++++++++++\n", stderr); mark_buf(sl); stlink_write_mem8(sl, 0x20000000, 64); stlink_read_mem32(sl, 0x20000000, 64); mark_buf(sl); stlink_write_mem32(sl, 0x20000000, 1024 * 8); //8kB stlink_read_mem32(sl, 0x20000000, 1024 * 6); stlink_read_mem32(sl, 0x20000000 + 1024 * 6, 1024 * 2); #endif #if 0 stlink_read_all_regs(sl); stlink_step(sl); fputs("++++++++++ write r0 = 0x12345678\n", stderr); stlink_write_reg(sl, 0x12345678, 0); stlink_read_reg(sl, 0); stlink_read_all_regs(sl); #endif #if 0 stlink_run(sl); stlink_status(sl); stlink_force_debug(sl); stlink_status(sl); #endif #if 1 /* read the system bootloader */ fputs("\n++++++++++ reading bootloader ++++++++++++++++\n\n", stderr); stlink_fread(sl, "/tmp/barfoo", sl->sys_base, sl->sys_size); #endif #if 0 /* read the flash memory */ fputs("\n+++++++ read flash memory\n\n", stderr); /* mark_buf(sl); */ stlink_read_mem32(sl, 0x08000000, 4); #endif #if 0 /* flash programming */ fputs("\n+++++++ program flash memory\n\n", stderr); stlink_fwrite_flash(sl, "/tmp/foobar", 0x08000000); #endif #if 0 /* check file contents */ fputs("\n+++++++ check flash memory\n\n", stderr); { const int res = stlink_fcheck_flash(sl, "/tmp/foobar", 0x08000000); printf("_____ stlink_fcheck_flash() == %d\n", res); } #endif #if 0 fputs("\n+++++++ sram write and execute\n\n", stderr); stlink_fwrite_sram(sl, "/tmp/foobar", sl->sram_base); stlink_run_at(sl, sl->sram_base); #endif stlink_run(sl); stlink_status(sl); //---------------------------------------------------------------------- // back to mass mode, just in case ... stlink_exit_debug_mode(sl); stlink_current_mode(sl); stlink_close(sl); //fflush(stderr); fflush(stdout); return EXIT_SUCCESS; }
void replay(struct char_data *ch) { page_to_char(buf_string(ch->pcdata->buffer), ch); clear_buf(ch->pcdata->buffer); return; }