|
| 1 | +#include <stdio.h> |
| 2 | +#include <pico/stdlib.h> |
| 3 | +#include <hardware/spi.h> |
| 4 | +#include "avrprog.h" |
| 5 | + |
| 6 | +uint8_t program_code[2000] = {}; |
| 7 | +char input[10] = {0}; |
| 8 | + |
| 9 | +int main() { |
| 10 | + stdio_init_all(); |
| 11 | + sleep_ms(1000); |
| 12 | + |
| 13 | + while (true) { |
| 14 | + int code_c = 0; |
| 15 | + int input_c = 0; |
| 16 | + |
| 17 | + while (true) { |
| 18 | + char c = getchar(); |
| 19 | + |
| 20 | + // Tells the automatic programmer the programmer is ready. |
| 21 | + if (c == '?') { |
| 22 | + printf("READY"); |
| 23 | + } |
| 24 | + |
| 25 | + if (!(c == 13 || (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || c == ' ')) { |
| 26 | + continue; |
| 27 | + } |
| 28 | + |
| 29 | + if (c == 13) { |
| 30 | + input[input_c] = 0; |
| 31 | + break; |
| 32 | + } |
| 33 | + |
| 34 | + input[input_c] = c; |
| 35 | + |
| 36 | + if (input[input_c] == ' ') { |
| 37 | + input[input_c] = 0; |
| 38 | + |
| 39 | + // Readback the byte. |
| 40 | + printf(input); |
| 41 | + |
| 42 | + // Convert from hex to byte. |
| 43 | + program_code[code_c] = strtol(input, NULL, 16); |
| 44 | + |
| 45 | + input_c = 0; |
| 46 | + code_c++; |
| 47 | + } |
| 48 | + |
| 49 | + else input_c++; |
| 50 | + } |
| 51 | + |
| 52 | + printf("Program length: %d / first & last byte: %hhu %hhu\n", code_c, program_code[0], program_code[code_c - 1]); |
| 53 | + |
| 54 | + if ((code_c % 2) != 0) { |
| 55 | + printf("program bytes are not a multiple of 2!\n"); |
| 56 | + return 0; |
| 57 | + } |
| 58 | + |
| 59 | + avr_spi_init(); |
| 60 | + avr_reset(); |
| 61 | + |
| 62 | + if (!avr_enter_programming_mode()) { |
| 63 | + printf("failed to enter programming mode\n"); |
| 64 | + return 0; |
| 65 | + } |
| 66 | + |
| 67 | + printf("Entered programming mode\n"); |
| 68 | + |
| 69 | + avr_erase_memory(); |
| 70 | + |
| 71 | + printf("Erased program memory successfully for programming\n"); |
| 72 | + |
| 73 | + // full pages |
| 74 | + for (int i = 0; i < code_c / 64; i++) { |
| 75 | + printf("Flashing page %d..\n", i); |
| 76 | + |
| 77 | + int page_offset = i * 64; |
| 78 | + |
| 79 | + for (int j = 0; j < 32; j++) { |
| 80 | + int word_index = (j * 2) + page_offset; |
| 81 | + |
| 82 | + uint8_t high_byte = program_code[word_index + 1]; |
| 83 | + uint8_t low_byte = program_code[word_index]; |
| 84 | + |
| 85 | + avr_write_temporary_buffer(j, low_byte, high_byte); |
| 86 | + } |
| 87 | + |
| 88 | + avr_flash_program_memory(i * 32); |
| 89 | + |
| 90 | + printf("Verifying flash..\n"); |
| 91 | + |
| 92 | + if (!avr_verify_program_memory_page(i * 32, program_code + page_offset, 32)) { |
| 93 | + printf("Verification failed! Page has not been flashed correctly..\n"); |
| 94 | + sleep_ms(100); |
| 95 | + return 0; |
| 96 | + } else printf("Page flash successfully verified!\n"); |
| 97 | + } |
| 98 | + |
| 99 | + int remaining_bytes = code_c % 64; |
| 100 | + int bytes_offset = code_c - remaining_bytes; |
| 101 | + |
| 102 | + // partial page |
| 103 | + printf("Flashing partial page..\n"); |
| 104 | + |
| 105 | + for (int j = 0; j < remaining_bytes / 2; j++) { |
| 106 | + int word_index = (j * 2) + bytes_offset; |
| 107 | + |
| 108 | + uint8_t high_byte = program_code[word_index + 1]; |
| 109 | + uint8_t low_byte = program_code[word_index]; |
| 110 | + |
| 111 | + avr_write_temporary_buffer(j, low_byte, high_byte); |
| 112 | + } |
| 113 | + |
| 114 | + avr_flash_program_memory(bytes_offset / 2); |
| 115 | + |
| 116 | + printf("Verifying flash..\n"); |
| 117 | + |
| 118 | + if (!avr_verify_program_memory_page(bytes_offset / 2, program_code + bytes_offset, remaining_bytes / 2)) { |
| 119 | + printf("Verification failed! Page has not been flashed correctly..\n"); |
| 120 | + sleep_ms(100); |
| 121 | + return 0; |
| 122 | + } else printf("Page flash successfully verified!\n"); |
| 123 | + |
| 124 | + printf("Verifying overall page flashes..\n"); |
| 125 | + |
| 126 | + if (!avr_verify_program_memory_page(0, program_code, code_c / 2)) { |
| 127 | + printf("Overall page verification failed! Pages have not been flashed correctly..\n"); |
| 128 | + sleep_ms(100); |
| 129 | + return 0; |
| 130 | + } else printf("Overall page flash verification successful! Firmware has been flashed correctly\n"); |
| 131 | + |
| 132 | + printf("Going back to standby mode for future flashes..\n"); |
| 133 | + printf("FINISH"); |
| 134 | + } |
| 135 | + |
| 136 | + return 0; |
| 137 | +} |
0 commit comments