void sam_ba_monitor_run()

in src/sam_ba_monitor.c [129:300]


void sam_ba_monitor_run(void) {
    ptr_data = NULL;
    command = 'z';

    // Start waiting some cmd
    while (1) {
        process_msc();
        length = cdc_read_buf(data, SIZEBUFMAX);
        data[length] = 0;
        if (length) {
            logwrite("SERIAL:");
            logmsg(data);
            led_signal();
        }
        ptr = data;
        for (i = 0; i < length; i++) {
            if (*ptr != 0xff) {
                if (*ptr == '#') {
#if USE_CDC_TERMINAL
                    if (b_terminal_mode) {
                        cdc_write_buf("\n\r", 2);
                    }
#endif
                    if (command == 'S') {
                        // Check if some data are remaining in the "data" buffer
                        if (length > i) {
                            // Move current indexes to next avail data
                            // (currently ptr points to "#")
                            ptr++;
                            i++;
                            // We need to add first the remaining data of the
                            // current buffer already read from usb
                            // read a maximum of "current_number" bytes
                            u32tmp = (length - i) < current_number ? (length - i) : current_number;
                            memcpy(ptr_data, ptr, u32tmp);
                            i += u32tmp;
                            ptr += u32tmp;
                            j = u32tmp;
                        }
                        // update i with the data read from the buffer
                        i--;
                        ptr--;
                        // Do we expect more data ?
                        if (j < current_number)
                            cdc_read_buf_xmd(ptr_data, current_number - j);

                        __asm("nop");
                    } else if (command == 'R') {
                        cdc_write_buf_xmd(ptr_data, current_number);
                    } else if (command == 'O') {
                        *ptr_data = (char)current_number;
                    } else if (command == 'H') {
                        *((uint16_t *)(void *)ptr_data) = (uint16_t)current_number;
                    } else if (command == 'W') {
                        // detect BOSSA resetting us
                        if ((uint32_t)ptr_data == 0xE000ED0C)
                            RGBLED_set_color(COLOR_LEAVE);
                        *((int *)(void *)ptr_data) = current_number;
                    } else if (command == 'o') {
                        sam_ba_putdata_term(ptr_data, 1);
                    } else if (command == 'h') {
                        current_number = *((uint16_t *)(void *)ptr_data);
                        sam_ba_putdata_term((uint8_t *)&current_number, 2);
                    } else if (command == 'w') {
                        current_number = *((uint32_t *)(void *)ptr_data);
                        sam_ba_putdata_term((uint8_t *)&current_number, 4);
                    } else if (command == 'G') {
                        call_applet(current_number);
                        if (b_sam_ba_interface_usart) {
                            cdc_write_buf("\x06", 1);
                        }
                    } else if (command == 'T') {
#if USE_CDC_TERMINAL
                        b_terminal_mode = 1;
                        cdc_write_buf("\n\r", 2);
#endif
                    } else if (command == 'N') {
#if USE_CDC_TERMINAL
                        if (b_terminal_mode == 0) {
                            cdc_write_buf("\n\r", 2);
                        }
                        b_terminal_mode = 0;
#endif
                    } else if (command == 'V') {
                        cdc_write_buf(fullVersion, sizeof(fullVersion));
                    } else if (command == 'X') {
                        // Syntax: X[ADDR]#
                        // Erase the flash memory starting from ADDR to the end
                        // of flash.

                        flash_erase_to_end((uint32_t *) current_number);

                        // Notify command completed
                        cdc_write_buf("X\n\r", 3);
                    } else if (command == 'Y') {
                        // This command writes the content of a buffer in SRAM
                        // into flash memory.

                        // Syntax: Y[ADDR],0#
                        // Set the starting address of the SRAM buffer.

                        // Syntax: Y[ROM_ADDR],[SIZE]#
                        // Write the first SIZE bytes from the SRAM buffer
                        // (previously set) into
                        // flash memory starting from address ROM_ADDR

                        static uint32_t *src_buff_addr = NULL;

                        if (current_number == 0) {
                            // Set buffer address
                            src_buff_addr = (void *)ptr_data;

                        } else {
                            flash_write_words((void *)ptr_data, src_buff_addr, current_number / 4);
                        }

                        // Notify command completed
                        cdc_write_buf("Y\n\r", 3);
                    } else if (command == 'Z') {
                        // This command calculate CRC for a given area of
                        // memory.
                        // It's useful to quickly check if a transfer has been
                        // done
                        // successfully.

                        // Syntax: Z[START_ADDR],[SIZE]#
                        // Returns: Z[CRC]#

                        uint8_t *data = (uint8_t *)ptr_data;
                        uint32_t size = current_number;
                        uint16_t crc = 0;
                        uint32_t i = 0;
                        for (i = 0; i < size; i++)
                            crc = add_crc(*data++, crc);

                        // Send response
                        cdc_write_buf("Z", 1);
                        put_uint32(crc);
                        cdc_write_buf("#\n\r", 3);
                    }

                    command = 'z';
                    current_number = 0;
#if USE_CDC_TERMINAL
                    if (b_terminal_mode) {
                        cdc_write_buf(">", 1);
                    }
#endif
                } else {
                    if (('0' <= *ptr) && (*ptr <= '9')) {
                        current_number = (current_number << 4) | (*ptr - '0');

                    } else if (('A' <= *ptr) && (*ptr <= 'F')) {
                        current_number = (current_number << 4) | (*ptr - 'A' + 0xa);

                    } else if (('a' <= *ptr) && (*ptr <= 'f')) {
                        current_number = (current_number << 4) | (*ptr - 'a' + 0xa);

                    } else if (*ptr == ',') {
                        ptr_data = (uint8_t *)current_number;
                        current_number = 0;

                    } else {
                        command = *ptr;
                        current_number = 0;
                    }
                }
                ptr++;
            }
        }
    }
}