rp2040

RP2040 Programming without SDK
Log | Files | Refs

commit 1c2c5c5c02d837df1e502528210af737c52d8500
parent 8037f89e7810aaaeb2abbf806f517a12ace759d4
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Sat,  8 Apr 2023 11:37:02 +0900

rearange

Diffstat:
MMakefile | 9++++++---
Dbin2uf2.c | 110-------------------------------------------------------------------------------
Dbincrc.c | 90-------------------------------------------------------------------------------
Mmemmap.ld | 2+-
4 files changed, 7 insertions(+), 204 deletions(-)

diff --git a/Makefile b/Makefile @@ -16,7 +16,7 @@ clean: rm -f *.o rm -f *.elf rm -f *.uf2 - rm -f bincrc + rm -f bincrc bin2uf2 rm -f boot2/*.o rm -f boot2/*.bin rm -f boot2/boot2_crc.S @@ -48,5 +48,8 @@ flash: led.uf2 mount /dev/disk/by-label/RPI-RP2 /mnt cp led.uf2 /mnt -bincrc: bincrc.c - tcc -o bincrc bincrc.c +bincrc: tools/bincrc.c + tcc -o bincrc tools/bincrc.c + +bin2uf2: tools/bin2uf2 + tcc -o bin2uf2 tools/bin2uf2.c diff --git a/bin2uf2.c b/bin2uf2.c @@ -1,110 +0,0 @@ -#include <stdio.h> -#include <stdint.h> -#include <stdlib.h> -#include <string.h> - - -size_t -fwrite32l(uint32_t d, FILE *f) -{ - int i; - uint8_t b; - for (i = 0; i < 32; i += 8) { - b = (uint8_t) (d >> i & 0xff); - fwrite(&b, 1, 1, f); - if (ferror(f)) { - fprintf(stderr, "Fwrite32l: write error.\n"); - return 0; - } - } - return 4; -} - -int -main(int argc, char *argv[]) -{ - FILE *src = NULL, *dst = NULL; - size_t sdata = 476; - int retnum = 0; - - uint32_t mag1 = 0x0A324655; - uint32_t mag2 = 0x9E5D5157; - uint32_t flags = 0x00002000; // familyID present - uint32_t saddr = 0x10000000; - uint32_t nbyte = 256; - uint32_t blk = 0; - uint32_t nblk = 0; - uint32_t famid = 0xe48bff56; - uint8_t data[sdata]; - uint32_t mag3 = 0x0AB16F30; - - if (argc != 3) { - fprintf(stderr, "Usage: %s src dst\n", argv[0]); - exit(1); - } - - if ((src = fopen(argv[1], "rb")) == NULL) { - fprintf(stderr, "Could not open %s.\n", argv[1]); - retnum = 1; - goto defer; - } - if ((dst = fopen(argv[2], "wb")) == NULL) { - fprintf(stderr, "Could not open %s.\n", argv[2]); - retnum = 1; - goto defer; - } - - while (!feof(src)) { - fwrite32l(mag1, dst); - fwrite32l(mag2, dst); - fwrite32l(flags, dst); - fwrite32l(saddr, dst); - fwrite32l(nbyte, dst); - fwrite32l(blk, dst); - fwrite32l(nblk, dst); // dammy - fwrite32l(famid, dst); - - memset(data, 0, sdata); - fread(data, 1, nbyte, src); - if (ferror(src)) { - fprintf(stderr, "Read error: %s.\n", argv[1]); - retnum = 1; - goto defer; - } - fwrite(data, 1, sdata, dst); - if (ferror(src)) { - fprintf(stderr, "Write error: %s.\n", argv[2]); - retnum = 1; - goto defer; - } - - fwrite32l(mag3, dst); - - saddr += nbyte; - blk++; - nblk++; - } - - for (int i = 0; i < nblk; i++) { - if (i == 0) - if (fseek(dst, 24, SEEK_SET) < 0) { - fprintf(stderr, "Seek error: %s.\n argv[2]"); - retnum = 1; - goto defer; - } - fwrite32l(nblk, dst); - if (i < nblk - 1) - if(fseek(dst, 512 - 4, SEEK_CUR) < 0){ - fprintf(stderr, "Seek error: %s.\n argv[2]"); - retnum = 1; - goto defer; - } - } - -defer: - if (src) - fclose(src); - if (dst) - fclose(dst); - return retnum; -} diff --git a/bincrc.c b/bincrc.c @@ -1,90 +0,0 @@ -#include <stdio.h> -#include <stdint.h> -#include <stdlib.h> -#include <fcntl.h> -#include <unistd.h> - - -void -printb(uint32_t i) -{ - for (int j = 31; j >= 0; j--) { - if (i >> j & 1) { - printf("1"); - } else { - printf("0"); - } - } - printf("\n"); -} - -uint32_t -crc32(uint8_t *idata, size_t len) -{ - uint32_t pol = 0x04C11DB7; - uint32_t c = 0xFFFFFFFF; - uint32_t b; - - for (int i = 0; i < len; i++) { - b = idata[i] << 24; - c ^= b; - for (int j = 0; j < 8; j++) { - c = c >> 31 & 1 ? c << 1 ^ pol : c << 1; - } - } - - return c; -} - -int -main(int argc, char *argv[]) -{ - int src, dst; - size_t isize, osize = 256; - uint8_t idata[osize]; - uint32_t cs; - - if(argc != 3) { - fprintf(stderr, "Usage: %s src dst\n", argv[0]); - exit(1); - } - - if((src = open(argv[1], O_RDONLY)) < 0) { - fprintf(stderr, "Could not open %s.\n", argv[1]); - exit(1); - } - - isize = read(src, idata, osize); - if (isize > osize - 4) { - fprintf(stderr, "Input file too big.\n"); - exit(1); - } - - for (int i = isize; i < osize - 4; i++) - idata[i] = 0; - - close(src); - - if((dst = open(argv[2], O_CREAT | O_WRONLY, 0777)) < 0) { - fprintf(stderr, "Could not open %s.\n", argv[2]); - exit(1); - } - - cs = crc32(idata, osize - 4); - for (int i = 0; i < 4; i++) - idata[osize - 4 + i] = cs >> i * 8; - - dprintf(dst, ".cpu cortex-m0plus\n"); - dprintf(dst, ".thumb\n\n"); - dprintf(dst, ".section .boot2, \"ax\"\n\n"); - for (int i = 0; i < osize / 16; i++) { - dprintf(dst, ".byte "); - for (int j = 0; j < 16; j++) { - dprintf(dst, "0x%02x%s", idata[i * 16 + j], - j == 15 ? "\n" : ", "); - } - } - - close(dst); - return 0; -} diff --git a/memmap.ld b/memmap.ld @@ -7,7 +7,7 @@ MEMORY SECTIONS { .boot2 : { - boot2/*(.boot2) + *(.boot2) } > FLASH .text 0x20000000 : AT (0x10000100) {