Repository: enjoy-digital/litex (language: python)

Issue:
I2c scan fixes and cleanups



Discussion / hints:
Thanks @geertu, that's merged.

Fix patch:
diff --git a/litex/soc/software/bios/boot.c b/litex/soc/software/bios/boot.c
index 7516c0dd60..bb565fd65e 100644
--- a/litex/soc/software/bios/boot.c
+++ b/litex/soc/software/bios/boot.c
@@ -50,7 +50,7 @@ extern void boot_helper(unsigned long r1, unsigned long r2, unsigned long r3, un
 
 static void __attribute__((noreturn)) boot(unsigned long r1, unsigned long r2, unsigned long r3, unsigned long addr)
 {
-	printf("Executing booted program at 0x%08x\n\n", addr);
+	printf("Executing booted program at 0x%08lx\n\n", addr);
 	printf("--============= \e[1mLiftoff!\e[0m ===============--\n");
 	uart_sync();
 #ifdef CONFIG_CPU_HAS_INTERRUPT
@@ -212,7 +212,7 @@ int serialboot(void)
 				char *writepointer;
 
 				failed = 0;
-				writepointer = (char *) get_uint32(&frame.payload[0]);
+				writepointer = (char *)(uintptr_t) get_uint32(&frame.payload[0]);
 				for(i=4;i<frame.payload_length;i++)
 					*(writepointer++) = frame.payload[i];
 				if (frame.cmd == SFL_CMD_LOAD)
@@ -273,7 +273,7 @@ static int copy_file_from_tftp_to_ram(unsigned int ip, unsigned short server_por
 const char *filename, char *buffer)
 {
 	int size;
-	printf("Copying %s to 0x%08x... ", filename, buffer);
+	printf("Copying %s to %p... ", filename, buffer);
 	size = tftp_get(ip, server_port, filename, buffer);
 	if(size > 0)
 		printf("(%d bytes)", size);
@@ -428,7 +428,7 @@ static int copy_image_from_flash_to_ram(unsigned int flash_address, unsigned lon
 
 	length = check_image_in_flash(flash_address);
 	if(length > 0) {
-		printf("Copying 0x%08x to 0x%08x (%d bytes)...\n", flash_address, ram_address, length);
+		printf("Copying 0x%08x to 0x%08lx (%d bytes)...\n", flash_address, ram_address, length);
 		offset = 0;
 		init_progression_bar(length);
 		while (length > 0) {
@@ -500,7 +500,7 @@ static int copy_file_from_sdcard_to_ram(const char * filename, unsigned long ram
 	}
 
 	length = f_size(&file);
-	printf("Copying %s to 0x%08x (%d bytes)...\n", filename, ram_address, length);
+	printf("Copying %s to 0x%08lx (%d bytes)...\n", filename, ram_address, length);
 	init_progression_bar(length);
 	offset = 0;
 	for (;;) {
@@ -675,7 +675,7 @@ static int copy_file_from_sata_to_ram(const char * filename, unsigned long ram_a
 	}
 
 	length = f_size(&file);
-	printf("Copying %s to 0x%08x (%d bytes)...\n", filename, ram_address, length);
+	printf("Copying %s to 0x%08lx (%d bytes)...\n", filename, ram_address, length);
 	init_progression_bar(length);
 	offset = 0;
 	for (;;) {
diff --git a/litex/soc/software/bios/cmds/cmd_bios.c b/litex/soc/software/bios/cmds/cmd_bios.c
index 2163a7e2a5..873468db24 100644
--- a/litex/soc/software/bios/cmds/cmd_bios.c
+++ b/litex/soc/software/bios/cmds/cmd_bios.c
@@ -87,8 +87,8 @@ define_command(uptime, uptime_handler, "Uptime of the system since power-up", SY
 static void crc_handler(int nb_params, char **params)
 {
 	char *c;
-	unsigned int addr;
-	unsigned int length;
+	uintptr_t addr;
+	size_t length;
 
 	if (nb_params < 2) {
 		printf("crc <address> <length>");
diff --git a/litex/soc/software/bios/cmds/cmd_i2c.c b/litex/soc/software/bios/cmds/cmd_i2c.c
index 2319f37620..0e294397dd 100644
--- a/litex/soc/software/bios/cmds/cmd_i2c.c
+++ b/litex/soc/software/bios/cmds/cmd_i2c.c
@@ -40,7 +40,7 @@ static void i2c_write_handler(int nb_params, char **params)
 	}
 
 	if (nb_params - 1 > sizeof(write_params)) {
-		printf("Max data length is %d", sizeof(write_params));
+		printf("Max data length is %zu", sizeof(write_params));
 		return;
 	}
 
@@ -98,7 +98,7 @@ static void i2c_read_handler(int nb_params, char **params)
 		return;
 	}
 	if (len > sizeof(buf)) {
-		printf("Max data count is %d", sizeof(buf));
+		printf("Max data count is %zu", sizeof(buf));
 		return;
 	}
 
diff --git a/litex/soc/software/bios/helpers.c b/litex/soc/software/bios/helpers.c
index fad4641929..746a792c35 100644
--- a/litex/soc/software/bios/helpers.c
+++ b/litex/soc/software/bios/helpers.c
@@ -25,7 +25,7 @@ void dump_bytes(unsigned int *ptr, int count, unsigned long addr)
 			(count > NUMBER_OF_BYTES_ON_A_LINE)?
 				NUMBER_OF_BYTES_ON_A_LINE : count;
 
-		printf("\n0x%08x  ", addr);
+		printf("\n0x%08lx  ", addr);
 		for (i = 0; i < line_bytes; i++)
 			printf("%02x ", *(unsigned char *)(data+i));
 
diff --git a/litex/soc/software/bios/readline.c b/litex/soc/software/bios/readline.c
index 48d8f1a6c3..f507f1a651 100644
--- a/litex/soc/software/bios/readline.c
+++ b/litex/soc/software/bios/readline.c
@@ -150,10 +150,10 @@ void hist_init(void)
 }
 #endif
 
-static void cread_add_char(char ichar, int insert, unsigned long *num,
-			   unsigned long *eol_num, char *buf, unsigned long len)
+static void cread_add_char(char ichar, int insert, unsigned int *num,
+			   unsigned int *eol_num, char *buf, unsigned int len)
 {
-	unsigned long wlen;
+	unsigned int wlen;
 
 	if (insert || *num == *eol_num) {
 		if (*eol_num > len - 1) {
@@ -186,9 +186,9 @@ static void cread_add_char(char ichar, int insert, unsigned long *num,
 
 int readline(char *buf, int len)
 {
-	unsigned long num = 0;
-	unsigned long eol_num = 0;
-	unsigned long wlen;
+	unsigned int num = 0;
+	unsigned int eol_num = 0;
+	unsigned int wlen;
 	int insert = 1;
 	char ichar;
 
@@ -254,7 +254,7 @@ int readline(char *buf, int len)
 				wlen = eol_num - num - 1;
 				if (wlen) {
 					memmove(&buf[num], &buf[num+1], wlen);
-					putnstr(buf + num, (int)wlen);
+					putnstr(buf + num, wlen);
 				}
 
 				getcmd_putch(' ');
@@ -286,7 +286,7 @@ int readline(char *buf, int len)
 				num--;
 				memmove(buf + num, buf + num + 1, wlen);
 				getcmd_putch(CTL_BACKSPACE);
-				putnstr(buf + num, (int)wlen);
+				putnstr(buf + num, wlen);
 				getcmd_putch(' ');
 				do {
 					getcmd_putch(CTL_BACKSPACE);
@@ -298,7 +298,7 @@ int readline(char *buf, int len)
 			if (num < eol_num) {
 				wlen = eol_num - num;
 				memmove(buf + num, buf + num + 1, wlen);
-				putnstr(buf + num, (int)(wlen - 1));
+				putnstr(buf + num, wlen - 1);
 				getcmd_putch(' ');
 				do {
 					getcmd_putch(CTL_BACKSPACE);
diff --git a/litex/soc/software/bios/readline.h b/litex/soc/software/bios/readline.h
index f1284c5d1e..290b2e2c02 100644
--- a/litex/soc/software/bios/readline.h
+++ b/litex/soc/software/bios/readline.h
@@ -72,7 +72,7 @@ struct esc_cmds {
 #define REFRESH_TO_EOL() {			\
 	if (num < eol_num) {			\
 		wlen = eol_num - num;		\
-		putnstr(buf + num, (int)wlen);	\
+		putnstr(buf + num, wlen);	\
 		num = eol_num;			\
 	}					\
 }
diff --git a/litex/soc/software/include/base/inttypes.h b/litex/soc/software/include/base/inttypes.h
index 4afd04b62f..bc22376a6d 100644
--- a/litex/soc/software/include/base/inttypes.h
+++ b/litex/soc/software/include/base/inttypes.h
@@ -24,7 +24,7 @@
 
 #include <stdint.h>
 
-# if __WORDSIZE == 64
+# ifdef __LP64__
 #  define __PRI64_PREFIX        "l"
 #  define __PRIPTR_PREFIX       "l"
 # else
diff --git a/litex/soc/software/include/base/lfsr.h b/litex/soc/software/include/base/lfsr.h
index 50dfccfc79..8326b811a0 100644
--- a/litex/soc/software/include/base/lfsr.h
+++ b/litex/soc/software/include/base/lfsr.h
@@ -65,7 +65,7 @@ static inline unsigned long lfsr(unsigned long bits, unsigned long prev)
                0x20000029,
                0x48000000,
                0x80200003,
-#if __WORDSIZE == 64
+#ifdef __LP64__
                0x100080000,
                0x204000003,
                0x500000000,
diff --git a/litex/soc/software/include/base/limits.h b/litex/soc/software/include/base/limits.h
index 49ac6ffe6d..fd5888c3bb 100644
--- a/litex/soc/software/include/base/limits.h
+++ b/litex/soc/software/include/base/limits.h
@@ -5,17 +5,21 @@
 extern "C" {
 #endif
 
-#define ULONG_MAX 0xffffffff
+#ifdef __LP64__
+#define ULONG_MAX	18446744073709551615UL
+#else
+#define ULONG_MAX	4294967295UL
+#endif
 
-#define UINT_MAX 0xffffffff
-#define INT_MIN 0x80000000
-#define INT_MAX 0x7fffffff
+#define UINT_MAX	4294967295U
+#define INT_MIN		(-INT_MAX - 1)
+#define INT_MAX		2147483647
 
-#define USHRT_MAX 0xffff
-#define SHRT_MIN 0x8000
-#define SHRT_MAX 0x7fff
+#define USHRT_MAX	65535
+#define SHRT_MIN	(-32768)
+#define SHRT_MAX	32767
 
-#define UCHAR_MAX 0xff
+#define UCHAR_MAX	255
 
 #define CHAR_BIT 8
 
diff --git a/litex/soc/software/include/base/stddef.h b/litex/soc/software/include/base/stddef.h
index 4f9a211f79..858d70b3ec 100644
--- a/litex/soc/software/include/base/stddef.h
+++ b/litex/soc/software/include/base/stddef.h
@@ -11,8 +11,13 @@ extern "C" {
 #define NULL ((void *)0)
 #endif
 
+#ifdef __LP64__
 typedef unsigned long size_t;
 typedef long ptrdiff_t;
+#else
+typedef unsigned int size_t;
+typedef int ptrdiff_t;
+#endif
 
 #define offsetof(type, member) __builtin_offsetof(type, member)
 
diff --git a/litex/soc/software/include/base/stdint.h b/litex/soc/software/include/base/stdint.h
index bff5d09834..a17e1739aa 100644
--- a/litex/soc/software/include/base/stdint.h
+++ b/litex/soc/software/include/base/stdint.h
@@ -5,8 +5,13 @@
 extern "C" {
 #endif
 
+#ifdef __LP64__
+typedef long intptr_t;
+typedef unsigned long uintptr_t;
+#else
 typedef int intptr_t;
 typedef unsigned int uintptr_t;
+#endif
 
 typedef unsigned long long uint64_t;
 typedef unsigned int uint32_t;
diff --git a/litex/soc/software/include/base/stdio.h b/litex/soc/software/include/base/stdio.h
index 5e872d64e4..f070cf1237 100644
--- a/litex/soc/software/include/base/stdio.h
+++ b/litex/soc/software/include/base/stdio.h
@@ -10,11 +10,11 @@ extern "C" {
 int putchar(int c);
 int puts(const char *s);
 
-int snprintf(char *buf, size_t size, const char *fmt, ...);
-int scnprintf(char *buf, size_t size, const char *fmt, ...);
-int sprintf(char *buf, const char *fmt, ...);
+int snprintf(char *buf, size_t size, const char *fmt, ...) __attribute__((format(printf, 3, 4)));
+int scnprintf(char *buf, size_t size, const char *fmt, ...) __attribute__((format(printf, 3, 4)));
+int sprintf(char *buf, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
 
-int printf(const char *fmt, ...);
+int printf(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
 
 /* Not sure this belongs here... */
 typedef long long loff_t;
@@ -53,7 +53,7 @@ extern FILE *stdin;
 extern FILE *stdout;
 extern FILE *stderr;
 
-int fprintf(FILE *stream, const char *format, ...);
+int fprintf(FILE *stream, const char *format, ...) __attribute__((format(printf, 2, 3)));
 int fflush(FILE *stream);
 
 FILE *fopen(const char *path, const char *mode);
diff --git a/litex/soc/software/libbase/exception.c b/litex/soc/software/libbase/exception.c
index 74a0ed14fb..a0c7756824 100644
--- a/litex/soc/software/libbase/exception.c
+++ b/litex/soc/software/libbase/exception.c
@@ -76,9 +76,9 @@ static void gdb_stub(unsigned long pc, unsigned long sr,
 
 			case 'g': {
 				snprintf(buf, sizeof(buf),
-				         "%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x"
-				         "%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x"
-				         "%08x%08x%08x",
+				         "%08x%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx"
+				         "%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx"
+				         "%08lx%08lx%08lx",
 				         0,        r1,       regs[2],  regs[3],  regs[4],  regs[5],  regs[6],  regs[7],
 				         regs[8],  regs[9],  regs[10], regs[11], regs[12], regs[13], regs[14], regs[15],
 				         regs[16], regs[17], regs[18], regs[19], regs[20], regs[21], regs[22], regs[23],
@@ -120,7 +120,7 @@ static void gdb_stub(unsigned long pc, unsigned long sr,
 					snprintf(buf, sizeof(buf), "E01");
 					break;
 				}
-				snprintf(buf, sizeof(buf), "%08x", value);
+				snprintf(buf, sizeof(buf), "%08lx", value);
 				break;
 			}
 
@@ -156,7 +156,6 @@ static void gdb_stub(unsigned long pc, unsigned long sr,
 			}
 
 			default:
-				snprintf(buf, sizeof(buf), "");
 				break;
 		}
 
diff --git a/litex/soc/software/libbase/progress.c b/litex/soc/software/libbase/progress.c
index 829f07f0e1..2c5bac3b48 100644
--- a/litex/soc/software/libbase/progress.c
+++ b/litex/soc/software/libbase/progress.c
@@ -61,6 +61,4 @@ void init_progression_bar(int max)
 	spin = 0;
 	if (progress_max && progress_max != FILESIZE_MAX)
 		printf("[%*s]\r[", HASHES_PER_LINE, "");
-	else
-		printf("");
 }
diff --git a/litex/soc/software/libbase/vsnprintf.c b/litex/soc/software/libbase/vsnprintf.c
index 1785676f3f..1ac2e18cdb 100644
--- a/litex/soc/software/libbase/vsnprintf.c
+++ b/litex/soc/software/libbase/vsnprintf.c
@@ -186,7 +186,7 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
 			case 'p':
 				if (field_width == -1) {
 					field_width = 2*sizeof(void *);
-					flags |= PRINTF_ZEROPAD;
+					flags |= PRINTF_ZEROPAD | PRINTF_SPECIAL;
 				}
 				str = number(str, end,
 						(unsigned long) va_arg(args, void *),
diff --git a/litex/soc/software/liblitedram/bist.c b/litex/soc/software/liblitedram/bist.c
index 65e2dcf4cb..63446d11a4 100644
--- a/litex/soc/software/liblitedram/bist.c
+++ b/litex/soc/software/liblitedram/bist.c
@@ -130,7 +130,7 @@ void sdram_bist_loop(uint32_t loop, uint32_t burst_length, uint32_t random) {
 
 static uint32_t compute_speed_mibs(uint32_t length, uint32_t ticks) {
 	uint32_t speed;
-	//printf("(%lu, %lu)", length, ticks);
+	//printf("(%u, %u)", length, ticks);
 	speed = length*(CONFIG_CLOCK_FREQUENCY/(1024*1024))/ticks;
 	return speed;
 }
diff --git a/litex/soc/software/liblitedram/sdram.c b/litex/soc/software/liblitedram/sdram.c
index e42462173b..e9a3320cb4 100644
--- a/litex/soc/software/liblitedram/sdram.c
+++ b/litex/soc/software/liblitedram/sdram.c
@@ -1008,7 +1008,7 @@ int sdram_init(void)
 	_sdram_write_leveling_cmd_scan  = 0;
 	_sdram_write_leveling_cmd_delay = SDRAM_PHY_CMD_DELAY;
 #endif
-	printf("Initializing SDRAM @0x%08x...\n", MAIN_RAM_BASE);
+	printf("Initializing SDRAM @0x%08lx...\n", MAIN_RAM_BASE);
 	sdram_software_control_on();
 #if CSR_DDRPHY_RST_ADDR
 	ddrphy_rst_write(1);
diff --git a/litex/soc/software/liblitesata/sata.c b/litex/soc/software/liblitesata/sata.c
index 3e8250373b..90477e3b58 100644
--- a/litex/soc/software/liblitesata/sata.c
+++ b/litex/soc/software/liblitesata/sata.c
@@ -51,7 +51,7 @@ void sata_read(uint32_t sector, uint32_t count, uint8_t* buf)
 	for (i=0; i<count; i++) {
 		uint8_t done = 0;
 		while (done == 0) {
-			sata_sector2mem_base_write((uint64_t) buf);
+			sata_sector2mem_base_write((uint64_t)(uintptr_t) buf);
 			sata_sector2mem_sector_write(sector + i);
 			sata_sector2mem_start_write(1);
 			while ((sata_sector2mem_done_read() & 0x1) == 0);
@@ -80,7 +80,7 @@ void sata_write(uint32_t sector, uint32_t count, uint8_t* buf)
 	for (i=0; i<count; i++) {
 		uint8_t done = 0;
 		while (done == 0) {
-			sata_mem2sector_base_write((uint64_t) buf);
+			sata_mem2sector_base_write((uint64_t)(uintptr_t) buf);
 			sata_mem2sector_sector_write(sector + i);
 			sata_mem2sector_start_write(1);
 			while ((sata_sector2mem_done_read() & 0x1) == 0);
diff --git a/litex/soc/software/liblitesdcard/sdcard.c b/litex/soc/software/liblitesdcard/sdcard.c
index eec1e39573..8140fb9123 100644
--- a/litex/soc/software/liblitesdcard/sdcard.c
+++ b/litex/soc/software/liblitesdcard/sdcard.c
@@ -473,7 +473,7 @@ void sdcard_read(uint32_t block, uint32_t count, uint8_t* buf)
 {
 	/* Initialize DMA Writer */
 	sdblock2mem_dma_enable_write(0);
-	sdblock2mem_dma_base_write((uint64_t) buf);
+	sdblock2mem_dma_base_write((uint64_t)(uintptr_t) buf);
 	sdblock2mem_dma_length_write(512*count);
 	sdblock2mem_dma_enable_write(1);
 
@@ -504,7 +504,7 @@ void sdcard_write(uint32_t block, uint32_t count, uint8_t* buf)
 	while (count--) {
 		/* Initialize DMA Reader */
 		sdmem2block_dma_enable_write(0);
-		sdmem2block_dma_base_write((uint64_t) buf);
+		sdmem2block_dma_base_write((uint64_t)(uintptr_t) buf);
 		sdmem2block_dma_length_write(512);
 		sdmem2block_dma_enable_write(1);
 


Test patch:
diff --git a/litex/soc/software/libbase/memtest.c b/litex/soc/software/libbase/memtest.c
index d6f1126141..ae604fe9e9 100644
--- a/litex/soc/software/libbase/memtest.c
+++ b/litex/soc/software/libbase/memtest.c
@@ -53,7 +53,7 @@ int memtest_access(unsigned int *addr)
 	array[0] = ZEROONE;
 	array[1] = array[0];
 	if (ctrl_bus_errors_read() - bus_errors) {
-		printf("memtest_access error @ 0x%0x, exiting memtest.\n", addr);
+		printf("memtest_access error @ %p, exiting memtest.\n", addr);
 		return 1;
 	}
 
@@ -83,7 +83,7 @@ int memtest_bus(unsigned int *addr, unsigned long size)
 		if(rdata != ONEZERO) {
 			errors++;
 #ifdef MEMTEST_BUS_DEBUG
-			printf("memtest_bus error @ 0x%0x: 0x%08x vs 0x%08x\n", addr + i, rdata, ONEZERO);
+			printf("memtest_bus error @ %p: 0x%08x vs 0x%08x\n", addr + i, rdata, ONEZERO);
 #endif
 		}
 	}
@@ -103,7 +103,7 @@ int memtest_bus(unsigned int *addr, unsigned long size)
 		if(rdata != ZEROONE) {
 			errors++;
 #ifdef MEMTEST_BUS_DEBUG
-			printf("memtest_bus error @ 0x%0x:: 0x%08x vs 0x%08x\n", addr + i, rdata, ZEROONE);
+			printf("memtest_bus error @ %p:: 0x%08x vs 0x%08x\n", addr + i, rdata, ZEROONE);
 #endif
 		}
 	}
@@ -139,7 +139,7 @@ int memtest_addr(unsigned int *addr, unsigned long size, int random)
 		if(rdata != i) {
 			errors++;
 #ifdef MEMTEST_ADDR_DEBUG
-			printf("memtest_addr error @ 0x%0x: 0x%08x vs 0x%08x\n", addr + i, rdata, i);
+			printf("memtest_addr error @ %p: 0x%08x vs 0x%08x\n", addr + i, rdata, i);
 #endif
 		}
 	}
@@ -202,7 +202,7 @@ int memtest_data(unsigned int *addr, unsigned long size, int random)
 		if(rdata != seed_32) {
 			errors++;
 #ifdef MEMTEST_DATA_DEBUG
-			printf("memtest_data error @%0x: 0x%08x vs 0x%08x\n", addr + i, rdata, seed_32);
+			printf("memtest_data error @%p: 0x%08x vs 0x%08x\n", addr + i, rdata, seed_32);
 #endif
 		}
 		if (i%0x8000 == 0)