diff options
author | Brett Weiland <brett_weiland@bpcsapce.com> | 2023-01-12 13:41:48 -0600 |
---|---|---|
committer | Brett Weiland <brett_weiland@bpcsapce.com> | 2023-01-12 13:41:48 -0600 |
commit | b666668d0e6b67e4632e65486cae814ab5abbc39 (patch) | |
tree | 0cf0c2d689799c5e98d505e632248fdc5ed5a072 | |
parent | 94304b11e7220f060dbc345de5fa1952d0465016 (diff) |
updating for linkedin
-rw-r--r-- | docs/mega_datasheet.pdf | bin | 0 -> 935895 bytes | |||
-rw-r--r-- | src/backup | 89 | ||||
-rw-r--r-- | src/br24t_eeprom_driver.c | 71 | ||||
-rw-r--r-- | src/compile_commands.json | 2 | ||||
-rwxr-xr-x | src/compile_eeprom.py | 18 | ||||
-rw-r--r-- | src/debug.c | 10 | ||||
-rw-r--r-- | src/debug.h | 1 | ||||
-rw-r--r-- | src/eeprom_address.h | 73 | ||||
-rw-r--r-- | src/i2c.c | 6 | ||||
-rw-r--r-- | src/main.c | 19 | ||||
-rw-r--r-- | src/makefile | 16 | ||||
-rw-r--r-- | src/paint.c | 5 | ||||
-rw-r--r-- | src/paint.h | 10 | ||||
-rw-r--r-- | src/pins.h | 27 | ||||
-rw-r--r-- | src/uart.c | 80 | ||||
-rw-r--r-- | src/uart.h | 10 | ||||
-rwxr-xr-x | src/write_eeprom.py | 54 | ||||
-rwxr-xr-x | tests/xy_to_page.py | 19 |
18 files changed, 399 insertions, 111 deletions
diff --git a/docs/mega_datasheet.pdf b/docs/mega_datasheet.pdf Binary files differnew file mode 100644 index 0000000..4a326ce --- /dev/null +++ b/docs/mega_datasheet.pdf diff --git a/src/backup b/src/backup new file mode 100644 index 0000000..a90b206 --- /dev/null +++ b/src/backup @@ -0,0 +1,89 @@ +#include <avr/io.h> +#include <stdio.h> +#include <string.h> +#include <util/delay.h> +#include "i2c.h" +#include "br24t_eeprom_driver.h" +#include "uart.h" +#include "pins.h" +#include "debug.h" + +#define EEPROM_ADDR 0x57 +#define EEPROM_PAGE_SIZE 64 + +//for burning eeprom +#define MAGIC_SYN 0xdeadbeef +#define MAGIC_ACK 0xf00dd00d + +#define PAGE_WRITE_OK 0x00 +#define PAGE_WRITE_CORRUPTED 0x01 +#define PAGE_WRITE_UNKNOWN_ERROR 0xff + + +#ifdef FLASH_EEPROM +int main() { + LED_DEBUG_DDR |= _BV(LED_DEBUG); //TODO move to debug file or somethin + LED_DEBUG_PORT &= ~(_BV(LED_DEBUG)); + i2c_init(); + + uart_debug_init(); + FILE stdout_replacement = FDEV_SETUP_STREAM((void *)uart_debug_sendbyte, NULL, _FDEV_SETUP_WRITE); + FILE stdin_replacement = FDEV_SETUP_STREAM(NULL, (void *)uart_debug_recvbyte, _FDEV_SETUP_READ); + stdout = &stdout_replacement; + stdin = &stdin_replacement; + + printf("booted\r\n"); + flash_eeprom(); + return 0; + +} + +void flash_eeprom() { + unsigned int page_len; + unsigned int on_byte; + uint8_t eeprom_buffer_out[EEPROM_PAGE_SIZE]; + uint8_t eeprom_buffer_in[EEPROM_PAGE_SIZE]; + + /** + FILE master_in = FDEV_SETUP_STREAM((void *)uart_flash_sendbyte, NULL, _FDEV_SETUP_WRITE); + FILE master_out = FDEV_SETUP_STREAM(NULL, (void *)uart_flash_recvbyte, _FDEV_SETUP_READ); + **/ + FILE master_f = FDEV_SETUP_STREAM((void *)uart_flash_sendbyte, NULL, _FDEV_SETUP_WRITE); + + printf("waiting for master\r\n"); + + fread(&page_len, 1, 1, master_out); + printf("%i pages total\r\n", page_len); + + for(int page = 0; page < page_len; page++) { + printf("writing page %i...\r\n", page); + + + fread(eeprom_buffer_out, EEPROM_PAGE_SIZE, 1, &master_in); + fputc(PAGE_WRITE_CORRUPTED, &master_out); + + + i2c_start(EEPROM_I2C_ADDR, I2C_READ); + i2c_send((uint8_t)(page * EEPROM_PAGE_SIZE) & 0xff); + i2c_send((uint8_t)(page * EEPROM_PAGE_SIZE) >> 8); + for(on_byte = 0; on_byte < EEPROM_PAGE_SIZE; on_byte++) + i2c_send(eeprom_buffer_out[on_byte]); + i2c_stop(); + + //verify + eeprom_buffer_in[0] = EEPROM_READBYTE(page * EEPROM_PAGE_SIZE); + //eeprom_buffer_in[0] = i2c_read_reg_addr16(EEPROM_I2C_ADDR, page * EEPROM_PAGE_SIZE); + i2c_start(EEPROM_I2C_ADDR, I2C_READ); + for(on_byte = 1; on_byte < EEPROM_PAGE_SIZE; on_byte++) + eeprom_buffer_in[on_byte] = i2c_recv(); + i2c_stop(); + + if(memcmp(eeprom_buffer_in, eeprom_buffer_out, EEPROM_PAGE_SIZE)) { + fputc(PAGE_WRITE_CORRUPTED, &master_out); + printf("invalid checksum on page %i!", page); + } + fputc(PAGE_WRITE_OK, &master_out); + } + printf("If you ever read this, you should be happy\r\n"); +} +#endif diff --git a/src/br24t_eeprom_driver.c b/src/br24t_eeprom_driver.c index 2fc0bd0..68e64cb 100644 --- a/src/br24t_eeprom_driver.c +++ b/src/br24t_eeprom_driver.c @@ -21,22 +21,66 @@ #ifdef FLASH_EEPROM +int main() { + LED_DEBUG_DDR |= _BV(LED_DEBUG); //TODO move to debug file or somethin + LED_DEBUG_PORT &= ~(_BV(LED_DEBUG)); + i2c_init(); + + uart_debug_init(); + FILE stdout_replacement = FDEV_SETUP_STREAM((void *)uart_debug_sendbyte, NULL, _FDEV_SETUP_WRITE); + FILE stdin_replacement = FDEV_SETUP_STREAM(NULL, (void *)uart_debug_recvbyte, _FDEV_SETUP_READ); + stdout = &stdout_replacement; + stdin = &stdin_replacement; + printf("booted\r\n\r\n"); + + uart_flash_init(); + flash_eeprom(); + return 0; + +} + void flash_eeprom() { - uint16_t page_len; - uint8_t on_byte; + unsigned int page_len; + unsigned int on_byte; uint8_t eeprom_buffer_out[EEPROM_PAGE_SIZE]; uint8_t eeprom_buffer_in[EEPROM_PAGE_SIZE]; + + /** + FILE master_f = FDEV_SETUP_STREAM((void *)uart_flash_sendbyte, NULL, _FDEV_SETUP_WRITE); + **/ + FILE master_fin = FDEV_SETUP_STREAM(NULL, (void *)uart_flash_recvbyte, _FDEV_SETUP_READ); + FILE master_fout = FDEV_SETUP_STREAM((void *)uart_flash_sendbyte, NULL, _FDEV_SETUP_WRITE); - fread(&page_len, 1, 1, stdin); + FILE master_f; + char temp_buffer[15]; + int blah = 0; + int debug = 0; + unsigned int tots_blah = 0; + for(;;) { + if(blah > 9) blah = 0; + blah++; + tots_blah++; + debug = fread(temp_buffer, 12, 1, &master_fin); + printf("PACKET %i : fread status: %i, error: %i, eof: %i\r\n", tots_blah, debug, ferror(&master_fin), feof(&master_fin)); + printf("recv: %s\r\n\r\n\r\n", temp_buffer); + snprintf(temp_buffer, 12, "debugstr %i\r\n", blah); + printf("sending \"%s\"...\r\n", temp_buffer); + fwrite(temp_buffer, 12, 1, &master_fout); + memset(temp_buffer, 0, 12); + } + + printf("waiting for master\r\n"); + + fread(&page_len, 1, 1, &master_f); + printf("%i pages total\r\n", page_len); - for(int page = 0; page < page_len; page -= page_len) { + for(int page = 0; page < page_len; page++) { + printf("writing page %i...\r\n", page); + - fread(eeprom_buffer_out, EEPROM_PAGE_SIZE, 1, stdin); - //fread(eeprom_buffer_out, 11, 1, stdin); - fputc(PAGE_WRITE_CORRUPTED, stdout); + fread(eeprom_buffer_out, EEPROM_PAGE_SIZE, 1, &master_f); + fputc(PAGE_WRITE_CORRUPTED, &master_f); - DEBUG_LED_ON(); - for(;;); i2c_start(EEPROM_I2C_ADDR, I2C_READ); i2c_send((uint8_t)(page * EEPROM_PAGE_SIZE) & 0xff); @@ -53,11 +97,12 @@ void flash_eeprom() { eeprom_buffer_in[on_byte] = i2c_recv(); i2c_stop(); - if(!memcmp(eeprom_buffer_in, eeprom_buffer_out, EEPROM_PAGE_SIZE)) { - fputc(PAGE_WRITE_CORRUPTED, stdout); - for(;;); //TODO error here + if(memcmp(eeprom_buffer_in, eeprom_buffer_out, EEPROM_PAGE_SIZE)) { + fputc(PAGE_WRITE_CORRUPTED, &master_f); + printf("invalid checksum on page %i!", page); } - fputc(PAGE_WRITE_OK, stdout); + fputc(PAGE_WRITE_OK, &master_f); } + printf("If you ever read this, you should be happy\r\n"); } #endif diff --git a/src/compile_commands.json b/src/compile_commands.json index 7cb5d9c..7646b7a 100644 --- a/src/compile_commands.json +++ b/src/compile_commands.json @@ -115,7 +115,7 @@ "directory": "/home/indigo/projects/watch/src", "arguments": [ "/home/indigo/packs/avr8-gnu-toolchain-linux_x86_64/bin/avr-gcc", - "-mmcu=atmega328p", + "-mmcu=atmega2560", "-I", "/home/indigo/packs/avr8-gnu-toolchain-linux_x86_64/avr/include", "-o", diff --git a/src/compile_eeprom.py b/src/compile_eeprom.py index dee9341..3fbd60d 100755 --- a/src/compile_eeprom.py +++ b/src/compile_eeprom.py @@ -18,7 +18,6 @@ page_bit_height = 8 page_cnt = 4 - #fonts # for some reason BdfFontFile.BdfFontFile.bitmap doesn't have proper spacing # so we'll just manually go through each character ourselves @@ -27,6 +26,7 @@ page_cnt = 4 # get to big for litteraly no reason header.write("/** GENERATED BY EEPROM COMPILER SCRIPT **/\r\n\r\n") +header.write("#include \"paint.h\"\r\n") header.write("// FONTS\r\n") for file in os.listdir("{}/fonts/".format(data_path)): @@ -36,20 +36,28 @@ for file in os.listdir("{}/fonts/".format(data_path)): font_bdf = BdfFontFile.BdfFontFile(font_fd) font_name = file.split('.')[0].capitalize() - header.write("#define FONT_{}_ADDR\t0x{:04x}\r\n".format(font_name, output.tell())) + #header.write("#define FONT_{}_ADDR\t0x{:04x}\r\n".format(font_name, output.tell())) + header.write("font_t FONT_{} = {{ \r\n".format(font_name)) first_char = False for char in enumerate(font_bdf.glyph): if char[1]: if not first_char: first_char = True - header.write(("#define FONT_{}_FIRST_CHAR\t{}\r\n" - "#define FONT_{}_WIDTH\t{}\r\n" - "#define FONT_{}_HEIGHT\t{}\r\n").format( + header.write(( + " .FONT_{}_WIDTH\t{}\r\n" + " .FONT_{}_HEIGHT\t{}\r\n").format( font_name, char[0], font_name, char[1][-1].size[0], font_name, char[1][-1].size[1])) +# header.write(("#define FONT_{}_FIRST_CHAR\t{}\r\n" +# "#define FONT_{}_WIDTH\t{}\r\n" +# "#define FONT_{}_HEIGHT\t{}\r\n").format( +# font_name, char[0], +# font_name, char[1][-1].size[0], +# font_name, char[1][-1].size[1])) output.write(char[1][-1].tobytes('raw')) + header.write("}\r\n") header.write("\r\n\r\n// IMAGES\r\n#define IMG_BEGIN\t0x{:04x}\r\n".format(output.tell())) diff --git a/src/debug.c b/src/debug.c index d7babda..49e1f01 100644 --- a/src/debug.c +++ b/src/debug.c @@ -16,6 +16,13 @@ #define I2C_WRITE 0 #define I2C_READ 1 +void debug_crash() { + for(;;) { + LED_DEBUG_PORT ^= _BV(LED_DEBUG); + _delay_ms(500); + } +} + void scroll_test() { //sets scroll area to whole screen i2c_write_reg_multi(SSD1306_ADDR, SSD1306_CMD_REG, 3, (uint8_t[]){0xa3, 0x00, 0x20}); @@ -92,10 +99,11 @@ void test_timer() { } void eeprom_testbyte() { - uint8_t data_in = 0xde; + uint8_t data_in = 0xdf; uint8_t data_out = 0; printf("writing 0x%x to eeprom...\r\n", data_in); EEPROM_WRITEBYTE(0x0000, data_in); + _delay_ms(10); //max write cycle time data_out = EEPROM_READBYTE(0x0000); printf("byte read from eeprom: 0x%x\r\n", data_out); } diff --git a/src/debug.h b/src/debug.h index fe8e968..f62dd96 100644 --- a/src/debug.h +++ b/src/debug.h @@ -13,6 +13,7 @@ void screen_lowlvl_testdraw(); void uart_echo(); void test_clock(); void eeprom_testbyte(); +void debug_crash(); unsigned int ticks_to_seconds(); #define DEBUG_LED_ON() LED_DEBUG_PORT |= _BV(LED_DEBUG) diff --git a/src/eeprom_address.h b/src/eeprom_address.h index 8dda9cf..ef19efb 100644 --- a/src/eeprom_address.h +++ b/src/eeprom_address.h @@ -1,42 +1,43 @@ /** GENERATED BY EEPROM COMPILER SCRIPT **/
+#include "paint.h"
// FONTS
-#define FONT_5thelement_ADDR 0x0000
-#define FONT_5thelement_FIRST_CHAR 32
-#define FONT_5thelement_WIDTH 4
-#define FONT_5thelement_HEIGHT 5
-#define FONT_Bitbuntu_ADDR 0x01db
-#define FONT_Bitbuntu_FIRST_CHAR 32
-#define FONT_Bitbuntu_WIDTH 0
-#define FONT_Bitbuntu_HEIGHT 0
-#define FONT_Bitocra-13_ADDR 0x067b
-#define FONT_Bitocra-13_FIRST_CHAR 32
-#define FONT_Bitocra-13_WIDTH 7
-#define FONT_Bitocra-13_HEIGHT 13
-#define FONT_Bitocra-13-full_ADDR 0x1014
-#define FONT_Bitocra-13-full_FIRST_CHAR 32
-#define FONT_Bitocra-13-full_WIDTH 7
-#define FONT_Bitocra-13-full_HEIGHT 13
-#define FONT_4thd_ADDR 0x19ad
-#define FONT_4thd_FIRST_CHAR 32
-#define FONT_4thd_WIDTH 4
-#define FONT_4thd_HEIGHT 4
-#define FONT_Bitocra_ADDR 0x1b29
-#define FONT_Bitocra_FIRST_CHAR 32
-#define FONT_Bitocra_WIDTH 0
-#define FONT_Bitocra_HEIGHT 0
-#define FONT_Bitocra7_ADDR 0x1fbd
-#define FONT_Bitocra7_FIRST_CHAR 0
-#define FONT_Bitocra7_WIDTH 4
-#define FONT_Bitocra7_HEIGHT 7
-#define FONT_Bitbuntu-full_ADDR 0x24fd
-#define FONT_Bitbuntu-full_FIRST_CHAR 32
-#define FONT_Bitbuntu-full_WIDTH 6
-#define FONT_Bitbuntu-full_HEIGHT 10
-#define FONT_Bitocra-full_ADDR 0x2c69
-#define FONT_Bitocra-full_FIRST_CHAR 32
-#define FONT_Bitocra-full_WIDTH 6
-#define FONT_Bitocra-full_HEIGHT 11
+font_t FONT_5thelement = {
+ .FONT_5thelement_WIDTH 32
+ .FONT_5thelement_HEIGHT 4
+}
+font_t FONT_4thd = {
+ .FONT_4thd_WIDTH 32
+ .FONT_4thd_HEIGHT 4
+}
+font_t FONT_Bitocra-13-full = {
+ .FONT_Bitocra-13-full_WIDTH 32
+ .FONT_Bitocra-13-full_HEIGHT 7
+}
+font_t FONT_Bitocra-13 = {
+ .FONT_Bitocra-13_WIDTH 32
+ .FONT_Bitocra-13_HEIGHT 7
+}
+font_t FONT_Bitocra = {
+ .FONT_Bitocra_WIDTH 32
+ .FONT_Bitocra_HEIGHT 0
+}
+font_t FONT_Bitbuntu = {
+ .FONT_Bitbuntu_WIDTH 32
+ .FONT_Bitbuntu_HEIGHT 0
+}
+font_t FONT_Bitocra-full = {
+ .FONT_Bitocra-full_WIDTH 32
+ .FONT_Bitocra-full_HEIGHT 6
+}
+font_t FONT_Bitbuntu-full = {
+ .FONT_Bitbuntu-full_WIDTH 32
+ .FONT_Bitbuntu-full_HEIGHT 6
+}
+font_t FONT_Bitocra7 = {
+ .FONT_Bitocra7_WIDTH 0
+ .FONT_Bitocra7_HEIGHT 4
+}
// IMAGES
@@ -37,7 +37,7 @@ * */ -void error() { } +void error() { debug_crash(); } void i2c_init() { TWBR = 12; @@ -61,7 +61,7 @@ void i2c_start(uint8_t addr, bool rw) { loop_until_bit_is_set(TWCR, TWINT); if(((TWSR & 0xf8) != (TW_MT_SLA_ACK)) && (TWSR & 0xf8) != (TW_MR_SLA_ACK)) { - #ifdef DEBUG_BUILD + #if DEBUG_BUILD printf("unhandled NACK in address transmission\ TWCR: 0x%x\ TWSR: 0x%x\r\n", TWCR, (TWSR & ~(0b11))); @@ -71,7 +71,7 @@ void i2c_start(uint8_t addr, bool rw) { } -void i2c_stop() { TWCR = (_BV(TWSTO) | _BV(TWEN) | _BV(TWINT)); _delay_ms(500); } +void i2c_stop() { TWCR = (_BV(TWSTO) | _BV(TWEN) | _BV(TWINT)); } void i2c_send(uint8_t byte) { TWDR = byte; //fist packet is address @@ -26,22 +26,26 @@ +#ifndef FLASH_EEPROM int main() { LED_DEBUG_DDR |= _BV(LED_DEBUG); //TODO move to debug file or somethin LED_DEBUG_PORT &= ~(_BV(LED_DEBUG)); //initlizes i2c, right now only speed //TODO don't delegate a whole function if desired i2c_init(); - //screen_init(); -#if defined(DEBUG_BUILD) || defined(FLASH_EEPROM) + screen_init(); +#ifdef DEBUG_BUILD - uart_init(); - FILE stdout_replacement = FDEV_SETUP_STREAM((void *)uart_sendbyte, NULL, _FDEV_SETUP_WRITE); - FILE stdin_replacement = FDEV_SETUP_STREAM(NULL, (void *)uart_recvbyte, _FDEV_SETUP_READ); + uart_debug_init(); + FILE stdout_replacement = FDEV_SETUP_STREAM((void *)uart_debug_sendbyte, NULL, _FDEV_SETUP_WRITE); + FILE stdin_replacement = FDEV_SETUP_STREAM(NULL, (void *)uart_debug_recvbyte, _FDEV_SETUP_READ); stdout = &stdout_replacement; stdin = &stdin_replacement; - //screen_lowlvl_testdraw(); - //eeprom_testbyte(); + printf("booted\n"); + + screen_lowlvl_testdraw(); + + eeprom_testbyte(); //test_clock(); #endif @@ -60,5 +64,6 @@ int main() { return 0; } +#endif diff --git a/src/makefile b/src/makefile index 8c6b06d..7191432 100644 --- a/src/makefile +++ b/src/makefile @@ -4,16 +4,17 @@ CC=$(TOOLCHAIN_DIR)/bin/avr-gcc LD=$(TOOLCHAIN_DIR)/bin/avr-ld INC=$(TOOLCHAIN_DIR)/avr/include OUT=compiled_payload.elf -#DEVICE=atmega2560 -DEVICE=atmega328p +DEVICE=atmega2560 +#DEVICE=atmega328p F_CPU=16000000 DEBUG=1 #avrdude options -#PARTNO=ATmega2560 -PARTNO=ATmega328p +PARTNO=ATmega2560 +#PARTNO=ATmega328p PORT=/dev/ttyUSB0 +DEBUG_PORT=/dev/ttyACM0 #only works on atmega2560, intented for debugging flashing BAUD=115200 @@ -41,8 +42,8 @@ eeprom: ./compile_eeprom.py eeprom_install: - $(CC) -mmcu=$(DEVICE) -I $(INC) -o $(OUT) -DFLASH_EEPROM -DDEBUG_BUILD=$(DEBUG) -DBAUD=$(RUNTIME_BAUDRATE) -DF_CPU=$(F_CPU) -Wall -O1 main.c debug.c i2c.c ssd1306_display_driver.c uart.c br24t_eeprom_driver.c paint.c -Wall - doas avrdude -B 1 -v -p $(PARTNO) -c usbtiny -U flash:w:$(OUT):e + $(CC) -mmcu=atmega2560 -I $(INC) -o $(OUT) -DFLASH_EEPROM -DDEBUG_BUILD=$(DEBUG) -DBAUD=$(RUNTIME_BAUDRATE) -DF_CPU=$(F_CPU) -Wall -O1 main.c debug.c i2c.c ssd1306_display_driver.c uart.c br24t_eeprom_driver.c paint.c -Wall + doas avrdude -B 1 -v -p ATmega2560 -c usbtiny -U flash:w:$(OUT):e install: $(OUT) @@ -51,7 +52,8 @@ install: $(OUT) screen: doas avrdude -c usbtiny -p $(PARTNO) - doas screen $(PORT) $(RUNTIME_BAUDRATE) + #doas screen $(PORT) $(RUNTIME_BAUDRATE) + doas screen $(DEBUG_PORT) $(RUNTIME_BAUDRATE) clean: rm -f *.o *.elf compiled_eeprom diff --git a/src/paint.c b/src/paint.c index a804d14..6a2b741 100644 --- a/src/paint.c +++ b/src/paint.c @@ -31,5 +31,8 @@ void draw_hline(int pos_y) { screen_buffer[((pos_y / 8) * 8) + on_pix] |= (1 << (pos_y % 8)); } -void draw_text(char *text, int x, int y, EEPROM_ADDR font) { +//also here +void draw_text(char *text, int x, int y, font_t font) { + for(int c = 0; text[c] == 0; c++) { + } } diff --git a/src/paint.h b/src/paint.h index c4efb81..2a82ba0 100644 --- a/src/paint.h +++ b/src/paint.h @@ -4,10 +4,18 @@ #include "ssd1306_display_driver.h" #include "br24t_eeprom_driver.h" +typedef const struct font { + EEPROM_ADDR *font; + uint8_t width; + uint8_t height; + uint16_t first_char; +} font_t; + void screen_clear(); void draw_image(EEPROM_ADDR image); void draw_hline(int pos_y); -void draw_text(char *text, int x, int y, EEPROM_ADDR font); +void draw_text(char *text, int x, int y, font_t font); + #endif @@ -1,4 +1,5 @@ #ifndef __PINS_INC +#define __PINS_INC /** * CTS is connected to CTS, CTS is 1 when we are ready * scope pins: @@ -7,19 +8,39 @@ * 3: desktop rts * 4: desktop cts */ -#define __PINS_INC #include <avr/io.h> +#ifdef __AVR_ATmega328P__ #define UART_DDR DDRD -#define UART_CTS PD2 //cyan -#define UART_RTS PD3 //yellow +#define UART_CTS PD2 +#define UART_RTS PD3 #define UART_PORT PORTD #define LED_DEBUG_DDR DDRB #define LED_DEBUG_PORT PORTB #define LED_DEBUG PB5 +#elif __AVR_ATmega2560__ +#define UART_DDR DDRB +#define UART_CTS PB6 +#define UART_RTS PB5 +#define UART_PORT PORTB +#define UART_PIN PINB + +#define LED_DEBUG_DDR DDRB +#define LED_DEBUG_PORT PORTB +#define LED_DEBUG PB7 +#else +#error "Only have arduino and arduino mega pin mappings!" +#endif + #endif +/** +yelow = slave rts/master cts +cyan = slave cts/master rts +pink = slave tx/master tx +blue = slave rx/slave tx +**/ @@ -1,7 +1,9 @@ #include <avr/io.h> #include <stdio.h> #include <util/setbaud.h> +#include <util/delay.h> #include "pins.h" +#include "debug.h" /** UART notes * UCSR0A: @@ -39,29 +41,18 @@ * **/ -//TODO replace with static void and just use high level functions? -void uart_sendbyte(uint8_t byte) { - UART_PORT &= ~(_BV(UART_RTS)); //us: ready to send - loop_until_bit_is_clear(UART_PORT, UART_CTS); //them: go ahead - //also wait for buffer to be empty, should never be a problem - loop_until_bit_is_set(UCSR0A, UDRE0); //done recieving +void uart_debug_sendbyte(uint8_t byte) { + loop_until_bit_is_set(UCSR0A, UDRE0); UDR0 = byte; - UART_PORT |= _BV(UART_RTS); //keep in mind cts is inverted; low is 1 and high is 0 } -//TODO replace with static void and just use high level functions? -uint8_t uart_recvbyte() { - loop_until_bit_is_clear(UART_PORT, UART_CTS); //wait for their request - UART_PORT &= ~(_BV(UART_RTS)); //hey, you can send now - loop_until_bit_is_set(UCSR0A, RXC0); //wait for them to actually send - UART_PORT |= _BV(UART_RTS); //okay, you can no longer send +uint8_t uart_debug_recvbyte() { + loop_until_bit_is_set(UCSR0A, RXC0); + UART_PORT |= _BV(UART_RTS); return UDR0; } -void uart_init() { - //set baud rate - UART_DDR |= _BV(UART_RTS); - UART_PORT |= _BV(UART_RTS); //keep in mind cts is inverted; low is 1 and high is 0 +void uart_debug_init() { UBRR0H = UBRRH_VALUE; UBRR0L = UBRRL_VALUE; @@ -82,3 +73,58 @@ TODO figure out why USE_2X is enable when it shoudn't be UCSR0B = (_BV(RXEN0) | _BV(TXEN0)); } + +#ifdef FLASH_EEPROM +//this uart port recieves from the computer and writes the eeprom +void uart_flash_init() { + //syncronization + UART_DDR |= _BV(UART_RTS); + UART_DDR &= ~(_BV(UART_CTS)); //keep in mind cts/rts is inverted; low is 1 and high is 0 + UART_PORT |= _BV(UART_RTS); //keep in mind cts/rts is inverted; low is 1 and high is 0 + + //for serial speed, we'll just use the same speed for now + UBRR1H = UBRRH_VALUE; + UBRR1L = UBRRL_VALUE; + + UCSR1A &= ~(_BV(U2X1)); + + UCSR1C = ((_BV(UCSZ11)) | _BV(UCSZ10)); // set 8 bit char size, yes the '=' is intentional + UCSR1B = (_BV(RXEN1) | _BV(TXEN1)); + + //when the master initates serial via pyserial, for some reason it pulls our CTS low + //for a brief moment. This hack avoids that + printf("waiting for unwanted initation pulse...\r\n"); + loop_until_bit_is_clear(UART_PIN, UART_CTS); + loop_until_bit_is_set(UART_PIN, UART_CTS); + printf("complete\r\n"); +} + +void uart_flash_sendbyte(uint8_t byte) { + printf("send start\r\n"); + if(UART_PORT & _BV(UART_CTS)) { + printf("send conflict!\r\n"); + debug_crash(); //make sure they're not trying to send + } + UART_PORT &= ~(_BV(UART_RTS)); //us: ready to send + printf("waiting on master\r\n"); + loop_until_bit_is_clear(UART_PIN, UART_CTS); //them: go ahead + printf("waiting for transmit buffer\r\n"); + loop_until_bit_is_set(UCSR1A, UDRE1); //wait until buffer is empty + UDR1 = byte; + UART_PORT |= _BV(UART_RTS); //okay, we be done sending + printf("done splurting out the data, still need to mark end of packet\r\n"); + loop_until_bit_is_set(UART_PIN, UART_CTS); //wait for them to get the message so we don't double dip + printf("we be done tho\r\n"); +} + +uint8_t uart_flash_recvbyte() { + char byte_in; + loop_until_bit_is_clear(UART_PIN, UART_CTS); //wait for their request + UART_PORT &= ~(_BV(UART_RTS)); //hey, you can send now + loop_until_bit_is_set(UCSR1A, RXC1); //wait for them to actually send + byte_in = UDR1; + UART_PORT |= _BV(UART_RTS); //okay, you can no longer send + loop_until_bit_is_set(UART_PIN, UART_CTS); //wait for them to get the message so we don't double dip + return byte_in; +} +#endif @@ -1,8 +1,12 @@ #ifndef __UART_INC #define __UART_INC -void uart_init(); -void uart_sendbyte(uint8_t byte); -uint8_t uart_recvbyte(); +void uart_debug_init(); +void uart_debug_sendbyte(uint8_t byte); +uint8_t uart_debug_recvbyte(); + +void uart_flash_init(); +void uart_flash_sendbyte(uint8_t byte); +uint8_t uart_flash_recvbyte(); #define HARDWARE_FLOW 1 //TODO #endif diff --git a/src/write_eeprom.py b/src/write_eeprom.py index e0a8604..b0c2594 100755 --- a/src/write_eeprom.py +++ b/src/write_eeprom.py @@ -22,55 +22,83 @@ eeprom_file = open("./compiled_eeprom", "rb") eeprom_filesize = os.fstat(eeprom_file.fileno()).st_size page_len = ceil(eeprom_filesize / EEPROM_PAGESIZE) -s = serial.Serial(port=args.port, baudrate=args.baud, parity=serial.PARITY_NONE, timeout=0, dsrdtr=1) +s = serial.Serial(port=args.port, baudrate=args.baud, parity=serial.PARITY_NONE, timeout=0, rtscts=0, dsrdtr=0, write_timeout = 0) +s.rts = 0 #slow method def write_hwf(data): + #check if the other guy is already trying to send debugval = 0 print("writing") data_parsed = [data[b:b+1] for b in range(len(data))] for c in data_parsed: print(debugval) + if s.cts: + print("Slave is trying to send before we do!") + exit(1) debugval += 1 s.rts = 1 while not s.cts: pass - s.rts = 0 s.write(c) + while s.cts: + pass + s.rts = 0 + print("done writing") def read_hwf(length): print("reading") msg = b'' for c in range(length): - s.rts = 1 + print("waiting for CTS") while not s.cts: pass - s.rts = 0 + s.rts = 1 msg += s.read(size=1) + s.rts = 0 + print("waiting for CTS to go back up, recieved {}".format(msg)) + while s.cts: + pass + print("done reading") return(msg) -def handle_writer_error(write_status): - if write_status == 1: # verification failed - print("Verification failed on page {}!".format(page)) - elif write_status == 0xff: - print("Unknown error writing page {}!".format(page)) +#def handle_writer_error(write_status): #while True: # write_hwf(b"abcdef\r\n") # print(read_hwf(10)) +input() +blah = 0 +tots_blah = 0 +while True: + tots_blah += 1 + print("PACKET {}".format(tots_blah)) + if blah > 8: + blah = 0 + blah += 1 + write_hwf(bytes("Debugstr {}\r\n".format(blah), 'UTF-8')) + print(read_hwf(12)) + +print(page_len) write_hwf(page_len.to_bytes(1, 'little')) + for page in range(0, page_len): - print(page) + print("PAGE: {}".format(page)) if EEPROM_PAGESIZE > (eeprom_filesize - eeprom_file.tell()): + print("writing and padding last page") write_hwf(eeprom_file.read()) [write_hwf(b'\x00') for b in range(0, eeprom_filesize % EEPROM_PAGESIZE)] else: write_hwf(eeprom_file.read(EEPROM_PAGESIZE)) - write_status = read_hwf(1) + write_status = int(read_hwf(1)) print("WRITE STATUS: {}".format(write_status)) - if not write_status == 0: - handle_writer_error(write_status) + if write_status == 0: + pass + elif write_status == 1: # verification failed + print("Verification failed on page {}!".format(page)) + elif write_status == 0xff: + print("Unknown error writing page {}!".format(page)) print("Done writing to EEPROM!\n") diff --git a/tests/xy_to_page.py b/tests/xy_to_page.py new file mode 100755 index 0000000..dc552d9 --- /dev/null +++ b/tests/xy_to_page.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python3 + +# +(int(xy / 16) * 16) + int((((xy - (int(xy / 16) * 16)))) / 8) + ((xy - ((int(xy / 16) * 16) + int(((xy - (int(xy / 16) * 16))) / 8) * 8)) * 2) + +BLEN = 2 +WIDTH = 8 +HEIGHT = 4 + +for xy in range(0, 32): + page_offset = int(xy / (BLEN * WIDTH)) * (BLEN * WIDTH) + collumn_offset = int((xy - page_offset) / WIDTH) + + bit_offset = page_offset + collumn_offset + ((xy - (page_offset + collumn_offset * WIDTH)) * BLEN) + + page_location = bit_offset + print(page_location) + #print("pixel {}: bit {}".format(xy, column_offset)) + |