├── .gitignore ├── LICENSE ├── README.md ├── adc_serial ├── Makefile ├── README.md ├── adc.c ├── adc.lds ├── adc_main.c ├── gpio.c ├── locore.s ├── mkvec ├── nvic.c ├── ocd ├── prf.c ├── rcc.c ├── serial.c └── timer.c ├── blink1 ├── Makefile ├── README.md ├── blink.c ├── blink.lds ├── locore.s └── ocd ├── blink1b ├── Makefile ├── README.md ├── blink.c ├── blink.lds ├── locore.s └── ocd ├── blink2 ├── Makefile ├── README.md ├── blink.c ├── blink.lds ├── gpio.c ├── locore.s ├── ocd └── rcc.c ├── blink_ext ├── Makefile ├── README.md ├── blink.c ├── blink.lds ├── gpio.c ├── locore.s ├── ocd └── rcc.c ├── blink_maple ├── Makefile ├── README.md ├── blink.c ├── blink.lds ├── gpio.c ├── locore.s ├── ocd └── rcc.c ├── i2c_maple ├── Makefile ├── README.md ├── dac.c ├── dac.lds ├── get_files ├── glue.c ├── gpio.c ├── libmaple │ ├── adc.h │ ├── bitband.h │ ├── bkp.h │ ├── dac.h │ ├── delay.h │ ├── dma.h │ ├── dma_common.h │ ├── exti.h │ ├── flash.h │ ├── fsmc.h │ ├── gpio.h │ ├── i2c.h │ ├── i2c_common.h │ ├── iwdg.h │ ├── libmaple.h │ ├── libmaple_types.h │ ├── nvic.h │ ├── pwr.h │ ├── rcc.h │ ├── ring_buffer.h │ ├── scb.h │ ├── series │ │ ├── adc.h │ │ ├── dac.h │ │ ├── dma.h │ │ ├── exti.h │ │ ├── flash.h │ │ ├── gpio.h │ │ ├── i2c.h │ │ ├── nvic.h │ │ ├── pwr.h │ │ ├── rcc.h │ │ ├── spi.h │ │ ├── stm32.h │ │ ├── timer.h │ │ └── usart.h │ ├── spi.h │ ├── stm32.h │ ├── syscfg.h │ ├── systick.h │ ├── timer.h │ ├── usart.h │ ├── usb.h │ ├── usb_cdcacm.h │ └── util.h ├── locore.s ├── main.cpp ├── maple_OLD │ ├── maple_exti.h │ ├── maple_gpio.h │ ├── maple_i2c.h │ ├── maple_i2c_common.h │ ├── maple_nvic.h │ ├── maple_rcc.h │ ├── maple_stm32.h │ ├── maple_stm32f103_exti.h │ ├── maple_stm32f103_gpio.h │ ├── maple_stm32f103_i2c.h │ ├── maple_stm32f103_nvic.h │ ├── maple_stm32f103_rcc.h │ └── maple_types.h ├── maple_i2c.c ├── maple_i2c_private.h ├── maple_stm32f103_i2c.c ├── mkvec ├── nvic.c ├── ocd ├── rcc.c ├── serial.c ├── startup.c ├── timer.c └── tom_i2c.c ├── interrupt ├── .gitignore ├── Makefile ├── README.md ├── gpio.c ├── inter.c ├── inter.lds ├── locore.s ├── mkvec ├── nvic.c ├── ocd ├── rcc.c ├── serial.c └── timer.c ├── lcd ├── .gitignore ├── Makefile ├── README.md ├── afio.c ├── gpio.c ├── i2c.c ├── lcd.c ├── lcd.lds ├── locore.s ├── mkvec ├── nvic.c ├── ocd ├── rcc.c ├── serial.c └── timer.c ├── lithium1 ├── .gitignore ├── Makefile ├── README.md ├── adc.c ├── gpio.c ├── lithium.c ├── lithium.lds ├── lithium.rb ├── locore.s ├── mah.rb ├── mkvec ├── nvic.c ├── ocd ├── plotit ├── prf.c ├── rcc.c ├── serial.c └── timer.c ├── loader ├── Makefile ├── README.md └── loader.c ├── memory ├── Makefile ├── README.md ├── gpio.c ├── locore.s ├── mem.c ├── mem.lds ├── ocd ├── rcc.c ├── serial.c ├── startup.c └── timer.c ├── serial1 ├── .gitignore ├── Makefile ├── README.md ├── gpio.c ├── locore.s ├── ocd ├── rcc.c ├── serial.c ├── talk.c ├── talk.lds └── timer.c ├── serial_boot ├── .gitignore ├── Makefile ├── README.md ├── boot.txt ├── longs ├── ocd ├── odx ├── reloc └── wrap.c ├── usb ├── .gitignore ├── Makefile ├── README.md ├── dragoon.lds ├── gpio.c ├── kyulib.c ├── kyulib.h ├── locore.s ├── main.c ├── mkvec ├── nvic.c ├── ocd ├── prf.c ├── protos.h ├── rcc.c ├── serial.c ├── startup.c ├── timer.c ├── usb.c ├── usb.h ├── usb_enum.c └── usb_watch.c ├── usb1 ├── .gitignore ├── Makefile ├── README.md ├── gpio.c ├── locore.s ├── mkvec ├── nvic.c ├── ocd ├── prf.c ├── rcc.c ├── serial.c ├── timer.c ├── usb.c ├── usb1.lds └── usb1_main.c ├── usb_baboon ├── .gitignore ├── Makefile ├── README.md ├── baboon.lds ├── gpio.c ├── kyulib.c ├── kyulib.h ├── locore.s ├── main.c ├── mkvec ├── nvic.c ├── ocd ├── papoon │ ├── Makefile │ ├── example.cxx │ ├── papoon.h │ ├── regbits.h │ ├── stm32f103xb.h │ ├── stm32f103xb_tim.h │ ├── usb_dev.cxx │ ├── usb_dev.h │ ├── usb_dev_cdc_acm.cxx │ └── usb_dev_cdc_acm.h ├── prf.c ├── protos.h ├── rcc.c ├── serial.c ├── startup.c ├── timer.c ├── usb.c ├── usb.h ├── usb_enum.c └── usb_watch.c ├── usb_papoon ├── .gitignore ├── Makefile ├── README.md ├── gpio.c ├── locore.s ├── main.c ├── mkvec ├── nvic.c ├── ocd ├── papoon.lds ├── papoon │ ├── Makefile │ ├── bin_to_hex.hxx │ ├── core_cm3.hxx │ ├── example.cxx │ ├── papoon.hxx │ ├── regbits.hxx │ ├── stm32f103xb.hxx │ ├── stm32f103xb_tim.hxx │ ├── usb_dev.cxx │ ├── usb_dev.hxx │ ├── usb_dev_cdc_acm.cxx │ ├── usb_dev_cdc_acm.hxx │ ├── usb_mcu_init.cxx │ └── usb_mcu_init.hxx ├── prf.c ├── rcc.c ├── serial.c ├── startup.c └── timer.c └── usb_papoon2 ├── .gitignore ├── Makefile ├── README.md ├── gpio.c ├── locore.s ├── main.c ├── mkvec ├── nvic.c ├── ocd ├── papoon.lds ├── papoon ├── Makefile ├── example.cxx ├── papoon.h ├── regbits.h ├── stm32f103xb.h ├── stm32f103xb_tim.h ├── usb_dev.cxx ├── usb_dev.h ├── usb_dev_cdc_acm.cxx ├── usb_dev_cdc_acm.h ├── usb_mcu_init.cxx └── usb_mcu_init.h ├── prf.c ├── rcc.c ├── serial.c ├── startup.c ├── timer.c └── usb.c /.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.elf 3 | *.bin 4 | *.dump 5 | loader/loader 6 | *.swp 7 | zzz 8 | -------------------------------------------------------------------------------- /adc_serial/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for interrupt demo 2 | 3 | #TOOLS = arm-linux-gnu 4 | TOOLS = arm-none-eabi 5 | 6 | # Assembling with gcc makes it want crt0 at link time. 7 | #AS = $(TOOLS)-gcc 8 | AS = $(TOOLS)-as 9 | 10 | #COPTS = -mcpu=cortex-m3 -mthumb -Wno-implicit-function-declaration 11 | COPTS = -mcpu=cortex-m3 -mthumb -Wno-implicit-function-declaration -fno-builtin-printf 12 | # Use the -g flag if you intend to use gdb 13 | #CC = $(TOOLS)-gcc $(COPTS) -g 14 | #CC = $(TOOLS)-gcc $(COPTS) 15 | CC = $(TOOLS)-gcc $(COPTS) -O2 16 | 17 | #LD = $(TOOLS)-gcc 18 | LD = $(TOOLS)-ld.bfd 19 | OBJCOPY = $(TOOLS)-objcopy 20 | DUMP = $(TOOLS)-objdump -d 21 | GDB = $(TOOLS)-gdb 22 | 23 | OBJS = locore.o adc_main.o nvic.o rcc.o gpio.o prf.o serial.o timer.o adc.o 24 | 25 | all: adc.elf adc.dump 26 | 27 | adc.dump: adc.elf 28 | $(DUMP) adc.elf >adc.dump 29 | 30 | adc.bin: adc.elf 31 | $(OBJCOPY) adc.elf adc.bin -O binary 32 | 33 | adc.elf: $(OBJS) 34 | $(LD) -T adc.lds -o adc.elf $(OBJS) 35 | 36 | locore.o: locore.s 37 | $(AS) locore.s -o locore.o 38 | 39 | .c.o: 40 | $(CC) -o $@ -c $< 41 | 42 | OCDCFG = -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/share/openocd/scripts/target/stm32f1x.cfg 43 | 44 | # This works great once I fixed the base address in the LDS file 45 | flash: adc.elf 46 | openocd $(OCDCFG) -c "program adc.elf verify reset exit" 47 | 48 | gdb: 49 | $(GDB) --eval-command="target remote localhost:3333" adc.elf 50 | 51 | gdbtui: 52 | $(GDB) -tui --eval-command="target remote localhost:3333" adc.elf 53 | 54 | clean: 55 | rm -f *.o adc.elf adc.bin adc.dump 56 | -------------------------------------------------------------------------------- /adc_serial/README.md: -------------------------------------------------------------------------------- 1 | adc_serial - write a simple ADC driver 2 | 3 | The idea here is to do single conversions when asked for by software. 4 | I don't have working USB yet, so I have enhanced serial.c to support 5 | both read and write (up to now it has been a write only console). 6 | -------------------------------------------------------------------------------- /adc_serial/adc.lds: -------------------------------------------------------------------------------- 1 | /* blink.lds 2 | * linker script for blink demo. 3 | * 4 | * Memory layout on my STM32F103 is pretty much like this: 5 | * 6 | * 0x00000000 - 0x07ffffff - aliased to flash or sys memory depending on BOOT jumpers 7 | * 0x08000000 - 0x0800ffff - Flash (64K ... or 128K) 8 | * 0x1ffff000 - 0x1ffff7ff - Boot firmware in system memory 9 | * 0x1ffff800 - 0x1fffffff - option bytes 10 | * 0x20000000 - 0x20004fff - SRAM (20k) 11 | * 0x40000000 - 0x40023400 - peripherals 12 | * 13 | * flash(RX) : ORIGIN = 0x00000000, LENGTH = 128K 14 | */ 15 | MEMORY 16 | { 17 | flash(RX) : ORIGIN = 0x08000000, LENGTH = 128K 18 | sram(WAIL) : ORIGIN = 0x20000000, LENGTH = 20K 19 | } 20 | 21 | SECTIONS 22 | { 23 | .text : { *(.text*) } > flash 24 | .bss : { *(.bss*) } > sram 25 | } 26 | -------------------------------------------------------------------------------- /adc_serial/mkvec: -------------------------------------------------------------------------------- 1 | #!/bin/ruby 2 | 3 | (0..67).each { |v| 4 | print ".word\tbogus\t\t/* IRQ %2d */\n" % v 5 | } 6 | -------------------------------------------------------------------------------- /adc_serial/ocd: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # These are proper file locations for Fedora 24 3 | 4 | openocd -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/share/openocd/scripts/target/stm32f1x.cfg 5 | -------------------------------------------------------------------------------- /blink1/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for blink demo 2 | # 3 | # This is/was the first code I ever wrote to 4 | # run on an STM32F103. 5 | 6 | #TOOLS = arm-linux-gnu 7 | TOOLS = arm-none-eabi 8 | 9 | # Assembling with gcc makes it want crt0 at link time. 10 | #AS = $(TOOLS)-gcc 11 | AS = $(TOOLS)-as 12 | # Use the -g flag if you intend to use gdb 13 | #CC = $(TOOLS)-gcc -mcpu=cortex-m3 -mthumb 14 | CC = $(TOOLS)-gcc -mcpu=cortex-m3 -mthumb -g 15 | 16 | #LD = $(TOOLS)-gcc 17 | LD = $(TOOLS)-ld.bfd 18 | OBJCOPY = $(TOOLS)-objcopy 19 | DUMP = $(TOOLS)-objdump -d 20 | GDB = $(TOOLS)-gdb 21 | 22 | #OBJS = locore.o blink.o 23 | OBJS = blink.o locore.o 24 | 25 | all: blink.bin blink.dump 26 | 27 | blink.dump: blink.elf 28 | $(DUMP) blink.elf >blink.dump 29 | 30 | blink.bin: blink.elf 31 | $(OBJCOPY) blink.elf blink.bin -O binary 32 | 33 | blink.elf: $(OBJS) 34 | $(LD) -T blink.lds -o blink.elf $(OBJS) 35 | 36 | locore.o: locore.s 37 | $(AS) locore.s -o locore.o 38 | 39 | blink.o: blink.c 40 | $(CC) -c blink.c 41 | 42 | # This is what we expect 43 | OCDCFG = -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/share/openocd/scripts/target/stm32f1x.cfg 44 | # Some newer chips are shipping with a idcode of 0x2ba01477, this will make them work. 45 | #OCDCFG = -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/share/openocd/scripts/target/cs32f1x.cfg 46 | 47 | flash: blink.elf 48 | openocd $(OCDCFG) -c "program blink.elf verify reset exit" 49 | 50 | new_flash: 51 | st-flash write blink.bin 0x8000000 52 | 53 | # This is how I first learned to burn the image. 54 | # (I use the above "make flash" these days. 55 | # 56 | # Connect the STLINK gadget to the target. 57 | # Be sure and start openOCD in this directory. 58 | # (or else it won't be able to find the bin file). 59 | # Then use "make flash" to start an openocd console session. 60 | # Type: reset halt 61 | # Type: flash write_image erase blink.bin 0x08000000 62 | old_flash: 63 | @echo "type: flash write_image erase blink.bin 0x08000000" 64 | telnet localhost 4444 65 | gdb: 66 | $(GDB) --eval-command="target remote localhost:3333" blink.elf 67 | 68 | gdbtui: 69 | $(GDB) -tui --eval-command="target remote localhost:3333" blink.elf 70 | 71 | clean: 72 | rm -f *.o blink.elf blink.bin blink.dump 73 | -------------------------------------------------------------------------------- /blink1/README.md: -------------------------------------------------------------------------------- 1 | Here it is! My first code ever to run on the ARM Cortex M3, 2 | in this case the STM32F103C8. This just blinks the LED that 3 | is wired up to GPIO pin PC13. 4 | 5 | It is a "proof of concept" milestone. The smallest bit of 6 | C and assembly that will make the device do something 7 | recognizable. I flashed the code using an STLINK V2 and 8 | OpenOCD under linux. 9 | 10 | It should be instructive to someone wanting to see a truly 11 | minimal bit of code along with a Makefile. 12 | 13 | ----------------------------------- 14 | 15 | And 3 months later I find myself trying to remember how to do 16 | this (namely flash the image). Using Fedora 25 17 | 18 | dnf install arm-none-eabi-gcc-cs 19 | 20 | dnf install openocd 21 | 22 | make clean 23 | 24 | make 25 | 26 | Then connect the ST link to the STM32 unit. 27 | 28 | This supplies power 29 | 30 | I then open up two extra terminal windows. 31 | 32 | In one I run openocd by typing: 33 | 34 | cd /u1/Projects/STM32/Archive/blink1 35 | 36 | ./ocd 37 | 38 | In the other I type "telnet localhost 4444" 39 | 40 | Then in the telnet window I give two commands to openocd 41 | 42 | reset halt 43 | 44 | flash write_image erase blink.bin 0x08000000 45 | 46 | Then I press the reset button and voila, flashing starts. 47 | 48 | Note that openocd must be running in the directory that 49 | holds the bin file or it won't be able to find it. 50 | It doesn't matter what directory telnet runs in. 51 | -------------------------------------------------------------------------------- /blink1/blink.c: -------------------------------------------------------------------------------- 1 | /* blink.c 2 | * (c) Tom Trebisky 9-24-2016 3 | */ 4 | 5 | /* The reset and clock control module */ 6 | struct rcc { 7 | volatile unsigned long rc; /* 0 - clock control */ 8 | volatile unsigned long cfg; /* 4 - clock config */ 9 | volatile unsigned long cir; /* 8 - clock interrupt */ 10 | volatile unsigned long apb2; /* c - peripheral reset */ 11 | volatile unsigned long apb1; /* 10 - peripheral reset */ 12 | volatile unsigned long ape3; /* 14 - peripheral enable */ 13 | volatile unsigned long ape2; /* 18 - peripheral enable */ 14 | volatile unsigned long ape1; /* 1c - peripheral enable */ 15 | volatile unsigned long bdcr; /* 20 - xx */ 16 | volatile unsigned long csr; /* 24 - xx */ 17 | }; 18 | 19 | #define GPIOA_ENABLE 0x04 20 | #define GPIOB_ENABLE 0x08 21 | #define GPIOC_ENABLE 0x10 22 | 23 | #define RCC_BASE (struct rcc *) 0x40021000 24 | 25 | /* One of the 3 gpios */ 26 | struct gpio { 27 | volatile unsigned long cr[2]; 28 | volatile unsigned long idr; 29 | volatile unsigned long odr; 30 | volatile unsigned long bsrr; 31 | volatile unsigned long brr; 32 | volatile unsigned long lock; 33 | }; 34 | 35 | #define GPIOA_BASE (struct gpio *) 0x40010800 36 | #define GPIOB_BASE (struct gpio *) 0x40010C00 37 | #define GPIOC_BASE (struct gpio *) 0x40011000 38 | 39 | #define MODE_OUT_2 0x02 /* Output, 2 Mhz */ 40 | 41 | #define CONF_GP_UD 0x0 /* Pull up/down */ 42 | #define CONF_GP_OD 0x4 /* Open drain */ 43 | 44 | /* This gives a blink rate of about 2.7 Hz */ 45 | /* i.e. the delay time is about 0.2 seconds (200 ms) */ 46 | #define FAST 200 47 | 48 | #define FASTER 50 49 | #define SLOWER 400 50 | 51 | void 52 | delay ( void ) 53 | { 54 | volatile int count = 1000 * FAST; 55 | 56 | while ( count-- ) 57 | ; 58 | } 59 | 60 | struct gpio *gp; 61 | unsigned long on_mask; 62 | unsigned long off_mask; 63 | 64 | void 65 | led_init ( int bit ) 66 | { 67 | int conf; 68 | int shift; 69 | struct rcc *rp = RCC_BASE; 70 | 71 | /* Turn on GPIO C */ 72 | rp->ape2 |= GPIOC_ENABLE; 73 | 74 | gp = GPIOC_BASE; 75 | 76 | shift = (bit - 8) * 4; 77 | conf = gp->cr[1] & ~(0xf<cr[1] = conf; 80 | 81 | off_mask = 1 << bit; 82 | on_mask = 1 << (bit+16); 83 | } 84 | 85 | void 86 | led_on ( void ) 87 | { 88 | gp->bsrr = on_mask; 89 | } 90 | 91 | void 92 | led_off ( void ) 93 | { 94 | gp->bsrr = off_mask; 95 | } 96 | 97 | #define PC13 13 98 | 99 | #define TWICE 100 | 101 | void 102 | startup ( void ) 103 | { 104 | led_init ( PC13 ); 105 | 106 | for ( ;; ) { 107 | led_on (); 108 | delay (); 109 | led_off (); 110 | delay (); 111 | #ifdef TWICE 112 | led_on (); 113 | delay (); 114 | led_off (); 115 | delay (); 116 | delay (); 117 | delay (); 118 | #endif 119 | } 120 | } 121 | 122 | /* THE END */ 123 | -------------------------------------------------------------------------------- /blink1/blink.lds: -------------------------------------------------------------------------------- 1 | /* blink.lds 2 | * linker script for blink demo. 3 | * 4 | * Memory layout on my STM32F103 is pretty much like this: 5 | * 6 | * 0x00000000 - 0x07ffffff - aliased to flash or sys memory depending on BOOT jumpers 7 | * 0x08000000 - 0x0800ffff - Flash (64K ... or 128K) 8 | * 0x1ffff000 - 0x1ffff7ff - Boot firmware in system memory 9 | * 0x1ffff800 - 0x1fffffff - option bytes 10 | * 0x20000000 - 0x20004fff - SRAM (20k) 11 | * 0x40000000 - 0x40023400 - peripherals 12 | */ 13 | MEMORY 14 | { 15 | flash(RX) : ORIGIN = 0x08000000, LENGTH = 128K 16 | sram(WAIL) : ORIGIN = 0x20000000, LENGTH = 20K 17 | } 18 | 19 | SECTIONS 20 | { 21 | .text : { 22 | *(.vectors*) 23 | *(.text*) 24 | } > flash 25 | .bss : { *(.bss*) } > sram 26 | } 27 | -------------------------------------------------------------------------------- /blink1/locore.s: -------------------------------------------------------------------------------- 1 | /* locore.s 2 | * Assembler startup file for the STM32F103 3 | * Tom Trebisky 9-24-2016 4 | */ 5 | 6 | # The Cortex M3 is a thumb only processor 7 | 8 | .section .vectors 9 | .cpu cortex-m3 10 | .thumb 11 | 12 | .word 0x20005000 /* stack top address */ 13 | .word _reset /* 1 Reset */ 14 | .word spin /* 2 NMI */ 15 | .word spin /* 3 Hard Fault */ 16 | .word spin /* 4 MM Fault */ 17 | .word spin /* 5 Bus Fault */ 18 | .word spin /* 6 Usage Fault */ 19 | .word spin /* 7 RESERVED */ 20 | .word spin /* 8 RESERVED */ 21 | .word spin /* 9 RESERVED*/ 22 | .word spin /* 10 RESERVED */ 23 | .word spin /* 11 SV call */ 24 | .word spin /* 12 Debug reserved */ 25 | .word spin /* 13 RESERVED */ 26 | .word spin /* 14 PendSV */ 27 | .word spin /* 15 SysTick */ 28 | .word spin /* 16 IRQ0 */ 29 | .word spin /* 17 IRQ1 */ 30 | .word spin /* 18 IRQ2 */ 31 | .word spin /* 19 ... */ 32 | /* On to IRQ67 */ 33 | .section .text 34 | 35 | .thumb_func 36 | spin: b spin 37 | 38 | .thumb_func 39 | _reset: 40 | bl startup 41 | b . 42 | 43 | /* THE END */ 44 | -------------------------------------------------------------------------------- /blink1/ocd: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # These are proper file locations for Fedora 24 3 | # and for Fedora 25 4 | # On F25, do "dnf install openocd" to get this. 5 | # 2-26-2017 6 | 7 | openocd -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/share/openocd/scripts/target/stm32f1x.cfg 8 | -------------------------------------------------------------------------------- /blink1b/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for blink demo 2 | 3 | #TOOLS = arm-linux-gnu 4 | TOOLS = arm-none-eabi 5 | 6 | # Assembling with gcc makes it want crt0 at link time. 7 | #AS = $(TOOLS)-gcc 8 | AS = $(TOOLS)-as 9 | # Use the -g flag if you intend to use gdb 10 | #CC = $(TOOLS)-gcc -mcpu=cortex-m3 -mthumb -g 11 | #CC = $(TOOLS)-gcc -mcpu=cortex-m3 -mthumb 12 | CC = $(TOOLS)-gcc -mcpu=cortex-m3 -mthumb -O2 13 | 14 | #LD = $(TOOLS)-gcc 15 | LD = $(TOOLS)-ld.bfd 16 | OBJCOPY = $(TOOLS)-objcopy 17 | DUMP = $(TOOLS)-objdump -d 18 | GDB = $(TOOLS)-gdb 19 | 20 | OBJS = locore.o blink.o 21 | 22 | all: blink.elf blink.dump 23 | 24 | blink.dump: blink.elf 25 | $(DUMP) blink.elf >blink.dump 26 | 27 | blink.bin: blink.elf 28 | $(OBJCOPY) blink.elf blink.bin -O binary 29 | 30 | blink.elf: $(OBJS) 31 | $(LD) -T blink.lds -o blink.elf $(OBJS) 32 | 33 | locore.o: locore.s 34 | $(AS) locore.s -o locore.o 35 | 36 | blink.o: blink.c 37 | $(CC) -c blink.c 38 | 39 | # the following is OBSOLETE !!! 40 | # To burn the image: 41 | # Connect the STLINK gadget to the target. 42 | # Be sure and start openOCD in this directory. 43 | # (or else it won't be able to find the bin file). 44 | # Then use "make flash" to start an openocd console session. 45 | # Type: reset halt 46 | # Type: flash write_image erase blink.bin 0x08000000 47 | xflash: 48 | @echo "type: flash write_image erase blink.bin 0x08000000" 49 | telnet localhost 4444 50 | 51 | OCDCFG = -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/share/openocd/scripts/target/stm32f1x.cfg 52 | 53 | # This is way slicker -- run openocd from the command line 54 | bflash: blink.bin 55 | openocd $(OCDCFG) -c "program blink.bin verify reset exit 0x08000000" 56 | # openocd $(OCDCFG) -c "program blink.bin reset exit 0x08000000" 57 | 58 | # This works great once I fixed the base address in the LDS file 59 | 60 | flash: blink.elf 61 | openocd $(OCDCFG) -c "program blink.elf verify reset exit" 62 | # openocd $(OCDCFG) -c "program blink.elf reset exit" 63 | 64 | gdb: 65 | $(GDB) --eval-command="target remote localhost:3333" blink.elf 66 | 67 | gdbtui: 68 | $(GDB) -tui --eval-command="target remote localhost:3333" blink.elf 69 | 70 | clean: 71 | rm -f *.o blink.elf blink.bin blink.dump 72 | -------------------------------------------------------------------------------- /blink1b/README.md: -------------------------------------------------------------------------------- 1 | This is just a small tweak on my first blink demo. 2 | I wanted to find out if it would run OK with compiler 3 | optimization turned on (and it does), and I wanted to 4 | try some coding changes to the same end. 5 | They don't make a significant difference. 6 | 7 | The code size is about 360 bytes. 8 | -------------------------------------------------------------------------------- /blink1b/blink.lds: -------------------------------------------------------------------------------- 1 | /* blink.lds 2 | * linker script for blink demo. 3 | * 4 | * Memory layout on my STM32F103 is pretty much like this: 5 | * 6 | * 0x00000000 - 0x07ffffff - aliased to flash or sys memory depending on BOOT jumpers 7 | * 0x08000000 - 0x0800ffff - Flash (64K ... or 128K) 8 | * 0x1ffff000 - 0x1ffff7ff - Boot firmware in system memory 9 | * 0x1ffff800 - 0x1fffffff - option bytes 10 | * 0x20000000 - 0x20004fff - SRAM (20k) 11 | * 0x40000000 - 0x40023400 - peripherals 12 | * 13 | * flash(RX) : ORIGIN = 0x00000000, LENGTH = 128K 14 | */ 15 | MEMORY 16 | { 17 | flash(RX) : ORIGIN = 0x08000000, LENGTH = 128K 18 | sram(WAIL) : ORIGIN = 0x20000000, LENGTH = 20K 19 | } 20 | 21 | SECTIONS 22 | { 23 | .text : { *(.text*) } > flash 24 | .bss : { *(.bss*) } > sram 25 | } 26 | -------------------------------------------------------------------------------- /blink1b/locore.s: -------------------------------------------------------------------------------- 1 | /* locore.s 2 | * Assembler startup file for the STM32F103 3 | * Tom Trebisky 9-24-2016 4 | */ 5 | 6 | # The Cortex M3 is a thumb only processor 7 | .cpu cortex-m3 8 | .thumb 9 | 10 | .word 0x20005000 /* stack top address */ 11 | .word _reset /* 1 Reset */ 12 | .word spin /* 2 NMI */ 13 | .word spin /* 3 Hard Fault */ 14 | .word spin /* 4 MM Fault */ 15 | .word spin /* 5 Bus Fault */ 16 | .word spin /* 6 Usage Fault */ 17 | .word spin /* 7 RESERVED */ 18 | .word spin /* 8 RESERVED */ 19 | .word spin /* 9 RESERVED*/ 20 | .word spin /* 10 RESERVED */ 21 | .word spin /* 11 SV call */ 22 | .word spin /* 12 Debug reserved */ 23 | .word spin /* 13 RESERVED */ 24 | .word spin /* 14 PendSV */ 25 | .word spin /* 15 SysTick */ 26 | .word spin /* 16 IRQ0 */ 27 | .word spin /* 17 IRQ1 */ 28 | .word spin /* 18 IRQ2 */ 29 | .word spin /* 19 ... */ 30 | /* On to IRQ67 */ 31 | 32 | spin: b spin 33 | 34 | .thumb_func 35 | _reset: 36 | bl startup 37 | b . 38 | 39 | /* THE END */ 40 | -------------------------------------------------------------------------------- /blink1b/ocd: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # These are proper file locations for Fedora 24 3 | 4 | openocd -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/share/openocd/scripts/target/stm32f1x.cfg 5 | -------------------------------------------------------------------------------- /blink2/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for tidy blink demo 2 | 3 | #TOOLS = arm-linux-gnu 4 | TOOLS = arm-none-eabi 5 | 6 | # Assembling with gcc makes it want crt0 at link time. 7 | #AS = $(TOOLS)-gcc 8 | AS = $(TOOLS)-as 9 | # Use the -g flag if you intend to use gdb 10 | #CC = $(TOOLS)-gcc -mcpu=cortex-m3 -mthumb -g 11 | #CC = $(TOOLS)-gcc -mcpu=cortex-m3 -mthumb 12 | CC = $(TOOLS)-gcc -mcpu=cortex-m3 -mthumb -O2 13 | 14 | #LD = $(TOOLS)-gcc 15 | LD = $(TOOLS)-ld.bfd 16 | OBJCOPY = $(TOOLS)-objcopy 17 | DUMP = $(TOOLS)-objdump -d 18 | GDB = $(TOOLS)-gdb 19 | 20 | OBJS = locore.o blink.o rcc.o gpio.o 21 | 22 | all: blink.elf blink.dump 23 | 24 | blink.dump: blink.elf 25 | $(DUMP) blink.elf >blink.dump 26 | 27 | blink.bin: blink.elf 28 | $(OBJCOPY) blink.elf blink.bin -O binary 29 | 30 | blink.elf: $(OBJS) 31 | $(LD) -T blink.lds -o blink.elf $(OBJS) 32 | 33 | locore.o: locore.s 34 | $(AS) locore.s -o locore.o 35 | 36 | .c.o: 37 | $(CC) -o $@ -c $< 38 | 39 | # This is what we expect 40 | OCDCFG = -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/share/openocd/scripts/target/stm32f1x.cfg 41 | # Some newer chips are shipping with a idcode of 0x2ba01477, this will make them work. 42 | #OCDCFG = -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/share/openocd/scripts/target/cs32f1x.cfg 43 | 44 | flash: blink.elf 45 | openocd $(OCDCFG) -c "program blink.elf verify reset exit" 46 | 47 | BOARD_USB_VENDOR_ID := 1EAF 48 | BOARD_USB_PRODUCT_ID := 0003 49 | 50 | # For this to work, the executable must be linked to 0x08005000 not 0x08000000 51 | dfu: blink.bin 52 | dfu-util -a1 -d $(BOARD_USB_VENDOR_ID):$(BOARD_USB_PRODUCT_ID) -D blink.bin -R 53 | 54 | dfux: blink.bin 55 | reset_maple.py && \ 56 | sleep 1 && \ 57 | dfu-util -a1 -d $(BOARD_USB_VENDOR_ID):$(BOARD_USB_PRODUCT_ID) -D blink.bin -R 58 | 59 | 60 | # No need for this now that the above works 61 | bflash: blink.bin 62 | openocd $(OCDCFG) -c "program blink.bin verify reset exit 0x08000000" 63 | 64 | gdb: 65 | $(GDB) --eval-command="target remote localhost:3333" blink.elf 66 | 67 | gdbtui: 68 | $(GDB) -tui --eval-command="target remote localhost:3333" blink.elf 69 | 70 | clean: 71 | rm -f *.o blink.elf blink.bin blink.dump 72 | -------------------------------------------------------------------------------- /blink2/README.md: -------------------------------------------------------------------------------- 1 | The game here is to write a USART driver. 2 | To start, we just want to write characters 3 | and poll for status. Reading and interrupts 4 | will come later. 5 | -------------------------------------------------------------------------------- /blink2/blink.c: -------------------------------------------------------------------------------- 1 | /* blink.c 2 | * (c) Tom Trebisky 7-2-2017 3 | * 4 | */ 5 | 6 | void rcc_init ( void ); 7 | void led_init ( int ); 8 | 9 | void led_on ( void ); 10 | void led_off ( void ); 11 | 12 | /* By itself, this gives a blink rate of about 2.7 Hz 13 | * so the delay is about 185 ms 14 | */ 15 | void 16 | delay ( void ) 17 | { 18 | volatile int count = 1000 * 200; 19 | 20 | while ( count-- ) 21 | ; 22 | } 23 | 24 | /* We scale the above to try to get a 500 ms delay */ 25 | void 26 | big_delay ( void ) 27 | { 28 | volatile int count = 1000 * 540; 29 | 30 | while ( count-- ) 31 | ; 32 | } 33 | 34 | #define PC13 13 35 | 36 | #define NBLINKS 3 37 | 38 | void 39 | startup ( void ) 40 | { 41 | int i; 42 | 43 | rcc_init (); 44 | 45 | led_init ( PC13 ); 46 | 47 | for ( ;; ) { 48 | for ( i=0; i flash 26 | .bss : { *(.bss*) } > sram 27 | } 28 | -------------------------------------------------------------------------------- /blink2/gpio.c: -------------------------------------------------------------------------------- 1 | /* gpio.c 2 | * (c) Tom Trebisky 7-2-2017 3 | */ 4 | 5 | /* One of the 3 gpios */ 6 | struct gpio { 7 | volatile unsigned long cr[2]; 8 | volatile unsigned long idr; 9 | volatile unsigned long odr; 10 | volatile unsigned long bsrr; 11 | volatile unsigned long brr; 12 | volatile unsigned long lock; 13 | }; 14 | 15 | #define GPIOA_BASE (struct gpio *) 0x40010800 16 | #define GPIOB_BASE (struct gpio *) 0x40010C00 17 | #define GPIOC_BASE (struct gpio *) 0x40011000 18 | 19 | #define MODE_OUT_2 0x02 /* Output, 2 Mhz */ 20 | 21 | #define CONF_GP_UD 0x0 /* Pull up/down */ 22 | #define CONF_GP_OD 0x4 /* Open drain */ 23 | 24 | struct gpio *gp; 25 | unsigned long on_mask; 26 | unsigned long off_mask; 27 | 28 | void 29 | led_init ( int bit ) 30 | { 31 | int conf; 32 | int shift; 33 | 34 | gp = GPIOC_BASE; 35 | 36 | shift = (bit - 8) * 4; 37 | conf = gp->cr[1] & ~(0xf<cr[1] = conf; 40 | 41 | off_mask = 1 << bit; 42 | on_mask = 1 << (bit+16); 43 | } 44 | 45 | void 46 | led_on ( void ) 47 | { 48 | gp->bsrr = on_mask; 49 | } 50 | 51 | void 52 | led_off ( void ) 53 | { 54 | gp->bsrr = off_mask; 55 | } 56 | 57 | /* THE END */ 58 | -------------------------------------------------------------------------------- /blink2/locore.s: -------------------------------------------------------------------------------- 1 | /* locore.s 2 | * Assembler startup file for the STM32F103 3 | * Tom Trebisky 9-24-2016 4 | */ 5 | 6 | # The Cortex M3 is a thumb only processor 7 | .cpu cortex-m3 8 | .thumb 9 | 10 | .word 0x20005000 /* stack top address */ 11 | .word _reset /* 1 Reset */ 12 | .word spin /* 2 NMI */ 13 | .word spin /* 3 Hard Fault */ 14 | .word spin /* 4 MM Fault */ 15 | .word spin /* 5 Bus Fault */ 16 | .word spin /* 6 Usage Fault */ 17 | .word spin /* 7 RESERVED */ 18 | .word spin /* 8 RESERVED */ 19 | .word spin /* 9 RESERVED*/ 20 | .word spin /* 10 RESERVED */ 21 | .word spin /* 11 SV call */ 22 | .word spin /* 12 Debug reserved */ 23 | .word spin /* 13 RESERVED */ 24 | .word spin /* 14 PendSV */ 25 | .word spin /* 15 SysTick */ 26 | .word spin /* 16 IRQ0 */ 27 | .word spin /* 17 IRQ1 */ 28 | .word spin /* 18 IRQ2 */ 29 | .word spin /* 19 ... */ 30 | /* On to IRQ67 */ 31 | 32 | spin: b spin 33 | 34 | .thumb_func 35 | _reset: 36 | bl startup 37 | b . 38 | 39 | /* THE END */ 40 | -------------------------------------------------------------------------------- /blink2/ocd: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # These are proper file locations for Fedora 24 3 | 4 | openocd -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/share/openocd/scripts/target/stm32f1x.cfg 5 | -------------------------------------------------------------------------------- /blink2/rcc.c: -------------------------------------------------------------------------------- 1 | /* rcc.c 2 | * (c) Tom Trebisky 7-2-2017 3 | */ 4 | 5 | /* The reset and clock control module */ 6 | struct rcc { 7 | volatile unsigned long rc; /* 0 - clock control */ 8 | volatile unsigned long cfg; /* 4 - clock config */ 9 | volatile unsigned long cir; /* 8 - clock interrupt */ 10 | volatile unsigned long apb2; /* c - peripheral reset */ 11 | volatile unsigned long apb1; /* 10 - peripheral reset */ 12 | volatile unsigned long ape3; /* 14 - peripheral enable */ 13 | volatile unsigned long ape2; /* 18 - peripheral enable */ 14 | volatile unsigned long ape1; /* 1c - peripheral enable */ 15 | volatile unsigned long bdcr; /* 20 - xx */ 16 | volatile unsigned long csr; /* 24 - xx */ 17 | }; 18 | 19 | #define GPIOA_ENABLE 0x04 20 | #define GPIOB_ENABLE 0x08 21 | #define GPIOC_ENABLE 0x10 22 | 23 | #define UART1_ENABLE 0x4000 24 | #define UART2_ENABLE 0x20000 25 | #define UART3_ENABLE 0x40000 26 | 27 | #define RCC_BASE (struct rcc *) 0x40021000 28 | 29 | /* As well as enabling peripherals, each peripheral may have 30 | * clocks to be enabled as well */ 31 | 32 | void 33 | rcc_init ( void ) 34 | { 35 | struct rcc *rp = RCC_BASE; 36 | 37 | /* Turn on GPIO C */ 38 | rp->ape2 |= GPIOC_ENABLE; 39 | 40 | /* Turn on USART 1 */ 41 | rp->apb2 |= UART1_ENABLE; 42 | 43 | // rp->apb1 |= UART2_ENABLE; 44 | // rp->apb1 |= UART3_ENABLE; 45 | } 46 | 47 | /* THE END */ 48 | -------------------------------------------------------------------------------- /blink_ext/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for tidy blink demo 2 | 3 | #TOOLS = arm-linux-gnu 4 | TOOLS = arm-none-eabi 5 | 6 | # Assembling with gcc makes it want crt0 at link time. 7 | #AS = $(TOOLS)-gcc 8 | AS = $(TOOLS)-as 9 | # Use the -g flag if you intend to use gdb 10 | #CC = $(TOOLS)-gcc -mcpu=cortex-m3 -mthumb -g 11 | #CC = $(TOOLS)-gcc -mcpu=cortex-m3 -mthumb 12 | CC = $(TOOLS)-gcc -mcpu=cortex-m3 -mthumb -O2 13 | 14 | #LD = $(TOOLS)-gcc 15 | LD = $(TOOLS)-ld.bfd 16 | OBJCOPY = $(TOOLS)-objcopy 17 | DUMP = $(TOOLS)-objdump -d 18 | GDB = $(TOOLS)-gdb 19 | 20 | OBJS = locore.o blink.o rcc.o gpio.o 21 | 22 | all: blink.elf blink.dump 23 | 24 | blink.dump: blink.elf 25 | $(DUMP) blink.elf >blink.dump 26 | 27 | blink.bin: blink.elf 28 | $(OBJCOPY) blink.elf blink.bin -O binary 29 | 30 | blink.elf: $(OBJS) 31 | $(LD) -T blink.lds -o blink.elf $(OBJS) 32 | 33 | locore.o: locore.s 34 | $(AS) locore.s -o locore.o 35 | 36 | .c.o: 37 | $(CC) -o $@ -c $< 38 | 39 | OCDCFG = -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/share/openocd/scripts/target/stm32f1x.cfg 40 | 41 | # This works great once I fixed the base address in the LDS file 42 | flash: blink.elf 43 | openocd $(OCDCFG) -c "program blink.elf verify reset exit" 44 | 45 | # No need for this now that the above works 46 | bflash: blink.bin 47 | openocd $(OCDCFG) -c "program blink.bin verify reset exit 0x08000000" 48 | 49 | gdb: 50 | $(GDB) --eval-command="target remote localhost:3333" blink.elf 51 | 52 | gdbtui: 53 | $(GDB) -tui --eval-command="target remote localhost:3333" blink.elf 54 | 55 | clean: 56 | rm -f *.o blink.elf blink.bin blink.dump 57 | -------------------------------------------------------------------------------- /blink_ext/README.md: -------------------------------------------------------------------------------- 1 | blink_ext 2 | 3 | This is derived from blink2, the goal here is to 4 | blink an external LED via some arbitrary pin. 5 | 6 | 9-12-2017 7 | -------------------------------------------------------------------------------- /blink_ext/blink.c: -------------------------------------------------------------------------------- 1 | /* blink.c 2 | * (c) Tom Trebisky 9-12-2017 3 | * Blink an external LED on A0 4 | * The external pin is selected in gpio.c 5 | */ 6 | 7 | void rcc_init ( void ); 8 | void led_init ( void ); 9 | void led_test ( void ); 10 | 11 | void led_on ( void ); 12 | void led_off ( void ); 13 | 14 | /* By itself, this gives a blink rate of about 2.7 Hz 15 | * so the delay is about 185 ms 16 | */ 17 | void 18 | delay ( void ) 19 | { 20 | volatile int count = 1000 * 200; 21 | 22 | while ( count-- ) 23 | ; 24 | } 25 | 26 | /* We scale the above to try to get a 500 ms delay */ 27 | void 28 | big_delay ( void ) 29 | { 30 | volatile int count = 1000 * 540; 31 | 32 | while ( count-- ) 33 | ; 34 | } 35 | 36 | #define NBLINKS 2 37 | 38 | void 39 | my_blink ( void ) 40 | { 41 | int i; 42 | 43 | for ( ;; ) { 44 | for ( i=0; i flash 24 | .bss : { *(.bss*) } > sram 25 | } 26 | -------------------------------------------------------------------------------- /blink_ext/locore.s: -------------------------------------------------------------------------------- 1 | /* locore.s 2 | * Assembler startup file for the STM32F103 3 | * Tom Trebisky 9-24-2016 4 | */ 5 | 6 | # The Cortex M3 is a thumb only processor 7 | .cpu cortex-m3 8 | .thumb 9 | 10 | .word 0x20005000 /* stack top address */ 11 | .word _reset /* 1 Reset */ 12 | .word spin /* 2 NMI */ 13 | .word spin /* 3 Hard Fault */ 14 | .word spin /* 4 MM Fault */ 15 | .word spin /* 5 Bus Fault */ 16 | .word spin /* 6 Usage Fault */ 17 | .word spin /* 7 RESERVED */ 18 | .word spin /* 8 RESERVED */ 19 | .word spin /* 9 RESERVED*/ 20 | .word spin /* 10 RESERVED */ 21 | .word spin /* 11 SV call */ 22 | .word spin /* 12 Debug reserved */ 23 | .word spin /* 13 RESERVED */ 24 | .word spin /* 14 PendSV */ 25 | .word spin /* 15 SysTick */ 26 | .word spin /* 16 IRQ0 */ 27 | .word spin /* 17 IRQ1 */ 28 | .word spin /* 18 IRQ2 */ 29 | .word spin /* 19 ... */ 30 | /* On to IRQ67 */ 31 | 32 | spin: b spin 33 | 34 | .thumb_func 35 | _reset: 36 | bl startup 37 | b . 38 | 39 | /* THE END */ 40 | -------------------------------------------------------------------------------- /blink_ext/ocd: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # These are proper file locations for Fedora 24 3 | 4 | openocd -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/share/openocd/scripts/target/stm32f1x.cfg 5 | -------------------------------------------------------------------------------- /blink_ext/rcc.c: -------------------------------------------------------------------------------- 1 | /* rcc.c 2 | * (c) Tom Trebisky 7-2-2017 3 | */ 4 | 5 | /* The reset and clock control module */ 6 | struct rcc { 7 | volatile unsigned long rc; /* 0 - clock control */ 8 | volatile unsigned long cfg; /* 4 - clock config */ 9 | volatile unsigned long cir; /* 8 - clock interrupt */ 10 | volatile unsigned long apb2; /* c - peripheral reset */ 11 | volatile unsigned long apb1; /* 10 - peripheral reset */ 12 | volatile unsigned long ape3; /* 14 - peripheral enable */ 13 | volatile unsigned long ape2; /* 18 - peripheral enable */ 14 | volatile unsigned long ape1; /* 1c - peripheral enable */ 15 | volatile unsigned long bdcr; /* 20 - xx */ 16 | volatile unsigned long csr; /* 24 - xx */ 17 | }; 18 | 19 | #define GPIOA_ENABLE 0x04 20 | #define GPIOB_ENABLE 0x08 21 | #define GPIOC_ENABLE 0x10 22 | 23 | #define UART1_ENABLE 0x4000 24 | #define UART2_ENABLE 0x20000 25 | #define UART3_ENABLE 0x40000 26 | 27 | #define RCC_BASE (struct rcc *) 0x40021000 28 | 29 | /* As well as enabling peripherals, each peripheral may have 30 | * clocks to be enabled as well */ 31 | 32 | void 33 | rcc_init ( void ) 34 | { 35 | struct rcc *rp = RCC_BASE; 36 | 37 | /* 38 | rp->ape2 |= GPIOC_ENABLE; 39 | rp->ape2 |= GPIOB_ENABLE; 40 | rp->ape2 |= GPIOA_ENABLE; 41 | */ 42 | 43 | rp->ape2 = (GPIOA_ENABLE | GPIOB_ENABLE | GPIOC_ENABLE); 44 | // rp->ape2 |= (GPIOA_ENABLE | GPIOB_ENABLE | GPIOC_ENABLE); 45 | 46 | /* Turn on USART 1 */ 47 | rp->apb2 |= UART1_ENABLE; 48 | 49 | // rp->apb1 |= UART2_ENABLE; 50 | // rp->apb1 |= UART3_ENABLE; 51 | } 52 | 53 | /* THE END */ 54 | -------------------------------------------------------------------------------- /blink_maple/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for tidy blink demo 2 | 3 | #TOOLS = arm-linux-gnu 4 | TOOLS = arm-none-eabi 5 | 6 | # Assembling with gcc makes it want crt0 at link time. 7 | #AS = $(TOOLS)-gcc 8 | AS = $(TOOLS)-as 9 | # Use the -g flag if you intend to use gdb 10 | #CC = $(TOOLS)-gcc -mcpu=cortex-m3 -mthumb -g 11 | #CC = $(TOOLS)-gcc -mcpu=cortex-m3 -mthumb 12 | #CC = $(TOOLS)-gcc -mcpu=cortex-m3 -mthumb -O2 13 | CC = $(TOOLS)-gcc -mcpu=cortex-m3 -mthumb 14 | 15 | #LD = $(TOOLS)-gcc 16 | LD = $(TOOLS)-ld.bfd 17 | OBJCOPY = $(TOOLS)-objcopy 18 | DUMP = $(TOOLS)-objdump -d 19 | GDB = $(TOOLS)-gdb 20 | 21 | OBJS = locore.o blink.o rcc.o gpio.o 22 | 23 | all: blink.elf blink.dump 24 | 25 | blink.dump: blink.elf 26 | $(DUMP) blink.elf >blink.dump 27 | 28 | blink.bin: blink.elf 29 | $(OBJCOPY) blink.elf blink.bin -O binary 30 | 31 | blink.elf: $(OBJS) 32 | $(LD) -T blink.lds -o blink.elf $(OBJS) 33 | 34 | locore.o: locore.s 35 | $(AS) locore.s -o locore.o 36 | 37 | .c.o: 38 | $(CC) -o $@ -c $< 39 | 40 | # This is what we expect 41 | OCDCFG = -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/share/openocd/scripts/target/stm32f1x.cfg 42 | # Some newer chips are shipping with a idcode of 0x2ba01477, this will make them work. 43 | #OCDCFG = -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/share/openocd/scripts/target/cs32f1x.cfg 44 | 45 | # This does not work with maple code. 46 | # The code loads to 08005000, but reset runs at 08000000 47 | # (or something more or less equivalent). 48 | flash: blink.elf 49 | openocd $(OCDCFG) -c "program blink.elf verify reset exit" 50 | 51 | BOARD_USB_VENDOR_ID := 1EAF 52 | BOARD_USB_PRODUCT_ID := 0003 53 | 54 | # For this to work, the executable must be linked to 0x08005000 not 0x08000000 55 | dfu: blink.bin 56 | dfu-util -a1 -d $(BOARD_USB_VENDOR_ID):$(BOARD_USB_PRODUCT_ID) -D blink.bin -R 57 | 58 | dfux: blink.bin 59 | reset_maple.py && \ 60 | sleep 1 && \ 61 | dfu-util -a1 -d $(BOARD_USB_VENDOR_ID):$(BOARD_USB_PRODUCT_ID) -D blink.bin -R 62 | 63 | 64 | # No need for this now that the above works 65 | bflash: blink.bin 66 | openocd $(OCDCFG) -c "program blink.bin verify reset exit 0x08000000" 67 | 68 | gdb: 69 | $(GDB) --eval-command="target remote localhost:3333" blink.elf 70 | 71 | gdbtui: 72 | $(GDB) -tui --eval-command="target remote localhost:3333" blink.elf 73 | 74 | clean: 75 | rm -f *.o blink.elf blink.bin blink.dump 76 | -------------------------------------------------------------------------------- /blink_maple/README.md: -------------------------------------------------------------------------------- 1 | This is just the blink2 demo copied here and fiddled with 2 | to run on a Maple r5 board. 3 | 4 | The lds file must be changed so the code starts at 0x08005000 5 | 6 | Also the LED is now PA5 7 | 8 | Note that the GPIO is hard wired in gpio.c 9 | 10 | Also, to load this (as intended) via the Maple DFU bootloader, 11 | you will probably need to put the board into "perpetual boot mode". 12 | Hit reset, then as soon as the fast blinks start, hold down the 13 | other button a few seconds. 14 | 15 | This demo never worked. On the Maple. Until 11-18-2023!! 16 | 17 | I had to enable GPIOA in the rcc 18 | I also had to cofigure the GPIO to do push pull, for the maple. 19 | -------------------------------------------------------------------------------- /blink_maple/blink.c: -------------------------------------------------------------------------------- 1 | /* blink.c 2 | * (c) Tom Trebisky 7-2-2017 3 | * 4 | */ 5 | 6 | void rcc_init ( void ); 7 | void led_init ( void ); 8 | 9 | void led_on ( void ); 10 | void led_off ( void ); 11 | 12 | /* By itself, this gives a blink rate of about 2.7 Hz 13 | * so the delay is about 185 ms 14 | */ 15 | void 16 | delay ( void ) 17 | { 18 | volatile int count = 1000 * 200; 19 | 20 | while ( count-- ) 21 | ; 22 | } 23 | 24 | /* We scale the above to try to get a 500 ms delay */ 25 | void 26 | big_delay ( void ) 27 | { 28 | volatile int count = 1000 * 540; 29 | 30 | while ( count-- ) 31 | ; 32 | } 33 | 34 | /* To change GPIO, you must hack it in gpio.c */ 35 | #define PC13 13 36 | #define PA5 5 37 | 38 | #define NBLINKS 3 39 | 40 | void 41 | startup ( void ) 42 | { 43 | int i; 44 | 45 | rcc_init (); 46 | 47 | // led_init ( PC13 ); 48 | // led_init ( PA5 ); 49 | led_init (); 50 | 51 | for ( ;; ) { 52 | for ( i=0; i flash 29 | .bss : { *(.bss*) } > sram 30 | } 31 | -------------------------------------------------------------------------------- /blink_maple/gpio.c: -------------------------------------------------------------------------------- 1 | /* gpio.c 2 | * (c) Tom Trebisky 7-2-2017 3 | */ 4 | 5 | /* One of the 3 gpios */ 6 | struct gpio { 7 | volatile unsigned long cr[2]; 8 | volatile unsigned long idr; 9 | volatile unsigned long odr; 10 | volatile unsigned long bsrr; 11 | volatile unsigned long brr; 12 | volatile unsigned long lock; 13 | }; 14 | 15 | #define GPIOA_BASE (struct gpio *) 0x40010800 16 | #define GPIOB_BASE (struct gpio *) 0x40010C00 17 | #define GPIOC_BASE (struct gpio *) 0x40011000 18 | 19 | #define MODE_OUT_2 0x02 /* Output, 2 Mhz */ 20 | 21 | #define CONF_GP_PP 0x0 /* Push pull */ 22 | #define CONF_GP_OD 0x4 /* Open drain */ 23 | 24 | struct gpio *gp; 25 | unsigned long on_mask; 26 | unsigned long off_mask; 27 | 28 | /* XXX - GPIO is "wired" in */ 29 | 30 | #define MAPLE 31 | 32 | void 33 | led_init ( void ) 34 | { 35 | int conf; 36 | int shift; 37 | int bit; 38 | 39 | #ifdef MAPLE 40 | /* A5 for Maple */ 41 | bit = 5; 42 | shift = bit * 4; 43 | gp = GPIOA_BASE; 44 | conf = gp->cr[0] & ~(0xf<cr[0] = conf; 47 | #else 48 | /* C13 for Pill */ 49 | bit = 13; 50 | shift = (bit - 8) * 4; 51 | gp = GPIOC_BASE; 52 | conf = gp->cr[1] & ~(0xf<cr[1] = conf; 55 | #endif 56 | 57 | off_mask = 1 << bit; 58 | on_mask = 1 << (bit+16); 59 | } 60 | 61 | void 62 | led_on ( void ) 63 | { 64 | gp->bsrr = on_mask; 65 | } 66 | 67 | void 68 | led_off ( void ) 69 | { 70 | gp->bsrr = off_mask; 71 | } 72 | 73 | /* THE END */ 74 | -------------------------------------------------------------------------------- /blink_maple/locore.s: -------------------------------------------------------------------------------- 1 | /* locore.s 2 | * Assembler startup file for the STM32F103 3 | * Tom Trebisky 9-24-2016 4 | */ 5 | 6 | # The Cortex M3 is a thumb only processor 7 | .cpu cortex-m3 8 | .thumb 9 | 10 | .word 0x20005000 /* stack top address */ 11 | .word _reset /* 1 Reset */ 12 | .word spin /* 2 NMI */ 13 | .word spin /* 3 Hard Fault */ 14 | .word spin /* 4 MM Fault */ 15 | .word spin /* 5 Bus Fault */ 16 | .word spin /* 6 Usage Fault */ 17 | .word spin /* 7 RESERVED */ 18 | .word spin /* 8 RESERVED */ 19 | .word spin /* 9 RESERVED*/ 20 | .word spin /* 10 RESERVED */ 21 | .word spin /* 11 SV call */ 22 | .word spin /* 12 Debug reserved */ 23 | .word spin /* 13 RESERVED */ 24 | .word spin /* 14 PendSV */ 25 | .word spin /* 15 SysTick */ 26 | .word spin /* 16 IRQ0 */ 27 | .word spin /* 17 IRQ1 */ 28 | .word spin /* 18 IRQ2 */ 29 | .word spin /* 19 ... */ 30 | /* On to IRQ67 */ 31 | 32 | spin: b spin 33 | 34 | .thumb_func 35 | _reset: 36 | bl startup 37 | b . 38 | 39 | /* THE END */ 40 | -------------------------------------------------------------------------------- /blink_maple/ocd: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # These are proper file locations for Fedora 24 3 | 4 | openocd -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/share/openocd/scripts/target/stm32f1x.cfg 5 | -------------------------------------------------------------------------------- /blink_maple/rcc.c: -------------------------------------------------------------------------------- 1 | /* rcc.c 2 | * (c) Tom Trebisky 7-2-2017 3 | */ 4 | 5 | /* The reset and clock control module */ 6 | struct rcc { 7 | volatile unsigned long rc; /* 0 - clock control */ 8 | volatile unsigned long cfg; /* 4 - clock config */ 9 | volatile unsigned long cir; /* 8 - clock interrupt */ 10 | volatile unsigned long apb2; /* c - peripheral reset */ 11 | volatile unsigned long apb1; /* 10 - peripheral reset */ 12 | volatile unsigned long ape3; /* 14 - peripheral enable */ 13 | volatile unsigned long ape2; /* 18 - peripheral enable */ 14 | volatile unsigned long ape1; /* 1c - peripheral enable */ 15 | volatile unsigned long bdcr; /* 20 - xx */ 16 | volatile unsigned long csr; /* 24 - xx */ 17 | }; 18 | 19 | #define GPIOA_ENABLE 0x04 20 | #define GPIOB_ENABLE 0x08 21 | #define GPIOC_ENABLE 0x10 22 | 23 | #define UART1_ENABLE 0x4000 24 | #define UART2_ENABLE 0x20000 25 | #define UART3_ENABLE 0x40000 26 | 27 | #define RCC_BASE (struct rcc *) 0x40021000 28 | 29 | /* As well as enabling peripherals, each peripheral may have 30 | * clocks to be enabled as well */ 31 | 32 | void 33 | rcc_init ( void ) 34 | { 35 | struct rcc *rp = RCC_BASE; 36 | 37 | /* Turn on all the GPIO */ 38 | rp->ape2 |= GPIOC_ENABLE; 39 | rp->ape2 |= GPIOB_ENABLE; 40 | rp->ape2 |= GPIOA_ENABLE; 41 | 42 | /* Turn on USART 1 */ 43 | rp->apb2 |= UART1_ENABLE; 44 | 45 | // rp->apb1 |= UART2_ENABLE; 46 | // rp->apb1 |= UART3_ENABLE; 47 | } 48 | 49 | /* THE END */ 50 | -------------------------------------------------------------------------------- /i2c_maple/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for i2c/dac demo using maple i2c driver 2 | 3 | # From: libmaple/support/make/board-includes/maple_mini.mk 4 | #MCU := STM32F103CB 5 | #PRODUCT_ID := 0003 6 | #ERROR_LED_PORT := GPIOB 7 | #ERROR_LED_PIN := 1 8 | #MCU_SERIES := stm32f1 9 | #MCU_F1_LINE := performance 10 | #LD_MEM_DIR := sram_20k_flash_128k 11 | # END 12 | 13 | #TOOLS = arm-linux-gnu 14 | TOOLS = arm-none-eabi 15 | 16 | # Assembling with gcc makes it want crt0 at link time. 17 | #AS = $(TOOLS)-gcc 18 | AS = $(TOOLS)-as 19 | 20 | #COPTS = -mcpu=cortex-m3 -mthumb -Wno-implicit-function-declaration -I. -Ilibmaple -DMCU_STM32F103CB 21 | COPTS = -mcpu=cortex-m3 -mthumb -Wno-implicit-function-declaration -I. -Ilibmaple -DMCU_STM32F103C8 22 | # Use the -g flag if you intend to use gdb 23 | #CC = $(TOOLS)-gcc $(COPTS) -g 24 | #CC = $(TOOLS)-gcc $(COPTS) 25 | CC = $(TOOLS)-gcc $(COPTS) -O2 26 | 27 | #LD = $(TOOLS)-gcc 28 | LD = $(TOOLS)-ld.bfd 29 | OBJCOPY = $(TOOLS)-objcopy 30 | DUMP = $(TOOLS)-objdump -d 31 | OBJDUMP = $(TOOLS)-objdump 32 | GDB = $(TOOLS)-gdb 33 | 34 | #OBJS = locore.o dac.o nvic.o rcc.o gpio.o serial.o timer.o i2c.o 35 | #OBJS = locore.o dac.o nvic.o rcc.o gpio.o serial.o timer.o 36 | OBJS = locore.o startup.o dac.o nvic.o rcc.o gpio.o serial.o timer.o maple_i2c.o maple_stm32f103_i2c.o glue.o 37 | 38 | all: dac.elf dac.dump 39 | 40 | dac.dump: dac.elf 41 | $(DUMP) dac.elf >dac.dump 42 | 43 | dac.bin: dac.elf 44 | $(OBJCOPY) dac.elf dac.bin -O binary 45 | 46 | dac.elf: $(OBJS) 47 | $(LD) -T dac.lds -o dac.elf $(OBJS) 48 | 49 | locore.o: locore.s 50 | $(AS) locore.s -o locore.o 51 | 52 | .c.o: 53 | $(CC) -o $@ -c $< 54 | 55 | # This is what we expect 56 | OCDCFG = -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/share/openocd/scripts/target/stm32f1x.cfg 57 | # Some newer chips are shipping with a idcode of 0x2ba01477, this will make them work. 58 | #OCDCFG = -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/share/openocd/scripts/target/cs32f1x.cfg 59 | 60 | # This works great once I fixed the base address in the LDS file 61 | flash: dac.elf 62 | openocd $(OCDCFG) -c "program dac.elf verify reset exit" 63 | 64 | gdb: 65 | $(GDB) --eval-command="target remote localhost:3333" dac.elf 66 | 67 | gdbtui: 68 | $(GDB) -tui --eval-command="target remote localhost:3333" dac.elf 69 | 70 | hdr: 71 | $(OBJDUMP) -h maple_i2c.o 72 | 73 | clean: 74 | rm -f *.o dac.elf dac.bin dac.dump 75 | -------------------------------------------------------------------------------- /i2c_maple/README.md: -------------------------------------------------------------------------------- 1 | I am experimenting with libmaple and an i2c driver 2 | 3 | This is my so called "maple-ectomy" of the i2c driver. 4 | 5 | At this time, it simply does not work, so I am back 6 | to the drawing board. 7 | 8 | 9-9-2020 9 | 10 | I just finishing running the demo that comes with libmaple 11 | and that uses the MCP4725 i2c DAC chip. 12 | I have built this demo in the standard maple environment 13 | and it works just fine. 14 | 15 | The idea here is to move that demo here and extract just the 16 | i2c driver from libmaple. 17 | 18 | This began by copying my i2c demo (which is a copy of my old interrupt demo) 19 | -------------------------------------------------------------------------------- /i2c_maple/dac.lds: -------------------------------------------------------------------------------- 1 | /* blink.lds 2 | * linker script for blink demo. 3 | * 4 | * Memory layout on my STM32F103 is pretty much like this: 5 | * 6 | * 0x00000000 - 0x07ffffff - aliased to flash or sys memory depending on BOOT jumpers 7 | * 0x08000000 - 0x0800ffff - Flash (64K ... or 128K) 8 | * 0x1ffff000 - 0x1ffff7ff - Boot firmware in system memory 9 | * 0x1ffff800 - 0x1fffffff - option bytes 10 | * 0x20000000 - 0x20004fff - SRAM (20k) 11 | * 0x40000000 - 0x40023400 - peripherals 12 | * 13 | * flash(RX) : ORIGIN = 0x00000000, LENGTH = 128K 14 | */ 15 | MEMORY 16 | { 17 | flash(RX) : ORIGIN = 0x08000000, LENGTH = 128K 18 | sram(RW) : ORIGIN = 0x20000000, LENGTH = 20K 19 | } 20 | 21 | SECTIONS 22 | { 23 | .text : 24 | { 25 | *(.text*) 26 | . = ALIGN(4); 27 | __text_end = .; 28 | } > flash 29 | 30 | .bss : 31 | { 32 | . = ALIGN(4); 33 | __bss_start = .; 34 | *(.bss*) 35 | *(COMMON) 36 | . = ALIGN(4); 37 | __bss_end = .; 38 | } > sram 39 | 40 | .data : 41 | { 42 | . = ALIGN(4); 43 | __data_start = .; 44 | *(.data*) 45 | . = ALIGN(4); 46 | __data_end = .; 47 | } > sram AT> flash 48 | 49 | .rodata : 50 | { 51 | . = ALIGN(4); 52 | *(.rodata*) 53 | . = ALIGN(4); 54 | } > flash 55 | 56 | } 57 | -------------------------------------------------------------------------------- /i2c_maple/get_files: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | cp /u1/Projects/STM32/libmaple/libmaple/include/libmaple/*.h libmaple 4 | cp /u1/Projects/STM32/libmaple/libmaple/stm32f1/include/series/*.h libmaple/series 5 | -------------------------------------------------------------------------------- /i2c_maple/libmaple/delay.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2010 Perry Hung. 5 | * Copyright (c) 2011 LeafLabs, LLC. 6 | * 7 | * Permission is hereby granted, free of charge, to any person 8 | * obtaining a copy of this software and associated documentation 9 | * files (the "Software"), to deal in the Software without 10 | * restriction, including without limitation the rights to use, copy, 11 | * modify, merge, publish, distribute, sublicense, and/or sell copies 12 | * of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 22 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 23 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 24 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | * SOFTWARE. 26 | *****************************************************************************/ 27 | 28 | /** 29 | * @file libmaple/include/libmaple/delay.h 30 | * @brief Delay implementation 31 | */ 32 | 33 | #ifndef _LIBMAPLE_DELAY_H_ 34 | #define _LIBMAPLE_DELAY_H_ 35 | 36 | #ifdef __cplusplus 37 | extern "C" { 38 | #endif 39 | 40 | #include 41 | #include 42 | 43 | /** 44 | * @brief Delay the given number of microseconds. 45 | * 46 | * @param us Number of microseconds to delay. 47 | */ 48 | static inline void delay_us(uint32 us) { 49 | us *= STM32_DELAY_US_MULT; 50 | 51 | /* fudge for function call overhead */ 52 | us--; 53 | asm volatile(" mov r0, %[us] \n\t" 54 | "1: subs r0, #1 \n\t" 55 | " bhi 1b \n\t" 56 | : 57 | : [us] "r" (us) 58 | : "r0"); 59 | } 60 | 61 | #ifdef __cplusplus 62 | } 63 | #endif 64 | 65 | #endif 66 | -------------------------------------------------------------------------------- /i2c_maple/libmaple/libmaple.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2010 Perry Hung. 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | *****************************************************************************/ 26 | 27 | /** 28 | * @file libmaple/include/libmaple/libmaple.h 29 | * @brief General include file for libmaple 30 | */ 31 | 32 | #ifndef _LIBMAPLE_LIBMAPLE_H_ 33 | #define _LIBMAPLE_LIBMAPLE_H_ 34 | 35 | #ifdef __cplusplus 36 | extern "C" { 37 | #endif 38 | 39 | #include 40 | #include 41 | #include 42 | #include 43 | 44 | #ifdef __cplusplus 45 | } 46 | #endif 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /i2c_maple/libmaple/libmaple_types.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2010 Perry Hung. 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | *****************************************************************************/ 26 | 27 | /** 28 | * @file libmaple/include/libmaple/libmaple_types.h 29 | * 30 | * @brief libmaple's types, and operations on types. 31 | */ 32 | 33 | #ifndef _LIBMAPLE_LIBMAPLE_TYPES_H_ 34 | #define _LIBMAPLE_LIBMAPLE_TYPES_H_ 35 | 36 | #ifdef __cplusplus 37 | extern "C" { 38 | #endif 39 | 40 | typedef unsigned char uint8; 41 | typedef unsigned short uint16; 42 | typedef unsigned int uint32; 43 | typedef unsigned long long uint64; 44 | 45 | typedef signed char int8; 46 | typedef short int16; 47 | typedef int int32; 48 | typedef long long int64; 49 | 50 | typedef void (*voidFuncPtr)(void); 51 | typedef void (*voidArgumentFuncPtr)(void *); 52 | 53 | #define __io volatile 54 | #define __attr_flash __attribute__((section (".USER_FLASH"))) 55 | #define __packed __attribute__((__packed__)) 56 | #define __deprecated __attribute__((__deprecated__)) 57 | #define __weak __attribute__((weak)) 58 | #ifndef __always_inline 59 | #define __always_inline inline __attribute__((always_inline)) 60 | #endif 61 | #ifndef __unused 62 | #define __unused __attribute__((unused)) 63 | #endif 64 | 65 | #ifndef NULL 66 | #define NULL 0 67 | #endif 68 | 69 | #ifndef offsetof 70 | #define offsetof(type, member) __builtin_offsetof(type, member) 71 | #endif 72 | 73 | #ifdef __cplusplus 74 | } 75 | #endif 76 | 77 | #endif 78 | -------------------------------------------------------------------------------- /i2c_maple/libmaple/series/dac.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2012 LeafLabs, LLC. 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | *****************************************************************************/ 26 | 27 | /** 28 | * @file libmaple/stm32f1/include/series/dac.h 29 | * @brief STM32F1 DAC support 30 | */ 31 | 32 | #ifndef _LIBMAPLE_STM32F1_DAC_H_ 33 | #define _LIBMAPLE_STM32F1_DAC_H_ 34 | 35 | #ifdef __cplusplus 36 | extern "C"{ 37 | #endif 38 | 39 | #include 40 | 41 | /** STM32F1 DAC register map type. */ 42 | typedef struct dac_reg_map { 43 | __io uint32 CR; /**< Control register */ 44 | __io uint32 SWTRIGR; /**< Software trigger register */ 45 | __io uint32 DHR12R1; /**< Channel 1 12-bit right-aligned data 46 | holding register */ 47 | __io uint32 DHR12L1; /**< Channel 1 12-bit left-aligned data 48 | holding register */ 49 | __io uint32 DHR8R1; /**< Channel 1 8-bit left-aligned data 50 | holding register */ 51 | __io uint32 DHR12R2; /**< Channel 2 12-bit right-aligned data 52 | holding register */ 53 | __io uint32 DHR12L2; /**< Channel 2 12-bit left-aligned data 54 | holding register */ 55 | __io uint32 DHR8R2; /**< Channel 2 8-bit left-aligned data 56 | holding register */ 57 | __io uint32 DHR12RD; /**< Dual DAC 12-bit right-aligned data 58 | holding register */ 59 | __io uint32 DHR12LD; /**< Dual DAC 12-bit left-aligned data 60 | holding register */ 61 | __io uint32 DHR8RD; /**< Dual DAC 8-bit right-aligned data holding 62 | register */ 63 | __io uint32 DOR1; /**< Channel 1 data output register */ 64 | __io uint32 DOR2; /**< Channel 2 data output register */ 65 | } dac_reg_map; 66 | 67 | #ifdef __cplusplus 68 | } 69 | #endif 70 | 71 | #endif 72 | -------------------------------------------------------------------------------- /i2c_maple/libmaple/series/exti.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2012 LeafLabs, LLC. 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | *****************************************************************************/ 26 | 27 | /** 28 | * @file libmaple/stm32f1/include/series/exti.h 29 | * @brief STM32F1 external interrupts 30 | */ 31 | 32 | #ifndef _LIBMAPLE_STM32F1_EXTI_H_ 33 | #define _LIBMAPLE_STM32F1_EXTI_H_ 34 | 35 | #ifdef __cpluspus 36 | extern "C" { 37 | #endif 38 | 39 | struct exti_reg_map; 40 | #define EXTI_BASE ((struct exti_reg_map*)0x40010400) 41 | 42 | #ifdef __cpluspus 43 | } 44 | #endif 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /i2c_maple/libmaple/series/pwr.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2012 LeafLabs, LLC. 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | *****************************************************************************/ 26 | 27 | /** 28 | * @file libmaple/stm32f1/include/series/pwr.h 29 | * @author Marti Bolivar 30 | * @brief STM32F1 Power control (PWR) support. 31 | */ 32 | 33 | #ifndef _LIBMAPLE_STM32F1_PWR_H_ 34 | #define _LIBMAPLE_STM32F1_PWR_H_ 35 | 36 | /* 37 | * Register bit definitions 38 | */ 39 | 40 | /* Control register */ 41 | 42 | /* PVD level selection */ 43 | #define PWR_CR_PLS_2_2V (0x0 << 5) 44 | #define PWR_CR_PLS_2_3V (0x1 << 5) 45 | #define PWR_CR_PLS_2_4V (0x2 << 5) 46 | #define PWR_CR_PLS_2_5V (0x3 << 5) 47 | #define PWR_CR_PLS_2_6V (0x4 << 5) 48 | #define PWR_CR_PLS_2_7V (0x5 << 5) 49 | #define PWR_CR_PLS_2_8V (0x6 << 5) 50 | #define PWR_CR_PLS_2_9V (0x7 << 5) 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /i2c_maple/libmaple/series/usart.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2012 LeafLabs, LLC. 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | *****************************************************************************/ 26 | 27 | /** 28 | * @file libmaple/stm32f1/include/series/usart.h 29 | * @author Marti Bolivar 30 | * @brief STM32F1 USART support. 31 | */ 32 | 33 | #ifndef _LIBMAPLE_STM32F1_USART_H_ 34 | #define _LIBMAPLE_STM32F1_USART_H_ 35 | 36 | #ifdef __cplusplus 37 | extern "C"{ 38 | #endif 39 | 40 | /* 41 | * Register map base pointers 42 | */ 43 | 44 | struct usart_reg_map; 45 | 46 | /** USART1 register map base pointer */ 47 | #define USART1_BASE ((struct usart_reg_map*)0x40013800) 48 | /** USART2 register map base pointer */ 49 | #define USART2_BASE ((struct usart_reg_map*)0x40004400) 50 | /** USART3 register map base pointer */ 51 | #define USART3_BASE ((struct usart_reg_map*)0x40004800) 52 | #ifdef STM32_HIGH_DENSITY 53 | /** UART4 register map base pointer */ 54 | #define UART4_BASE ((struct usart_reg_map*)0x40004C00) 55 | /** UART5 register map base pointer */ 56 | #define UART5_BASE ((struct usart_reg_map*)0x40005000) 57 | #endif 58 | 59 | /* 60 | * Devices 61 | */ 62 | 63 | struct usart_dev; 64 | extern struct usart_dev *USART1; 65 | extern struct usart_dev *USART2; 66 | extern struct usart_dev *USART3; 67 | #ifdef STM32_HIGH_DENSITY 68 | extern struct usart_dev *UART4; 69 | extern struct usart_dev *UART5; 70 | #endif 71 | 72 | #ifdef __cplusplus 73 | } 74 | #endif 75 | 76 | #endif 77 | -------------------------------------------------------------------------------- /i2c_maple/maple_OLD/maple_stm32.h: -------------------------------------------------------------------------------- 1 | /* Tom Trebisky 2 | * 9-9-2020 3 | * I am trying to pull out only what I need to avoid 4 | * dragging in the entire world. 5 | */ 6 | 7 | /* The following from libmaple/libmaple/stm32f1/include/series/stm32.h */ 8 | 9 | # define STM32_PCLK1 36000000U 10 | # define STM32_PCLK2 72000000U 11 | # define STM32_DELAY_US_MULT 12 /* FIXME: value is incorrect. */ 12 | 13 | -------------------------------------------------------------------------------- /i2c_maple/maple_OLD/maple_stm32f103_exti.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2012 LeafLabs, LLC. 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | *****************************************************************************/ 26 | 27 | /** 28 | * @file libmaple/stm32f1/include/series/exti.h 29 | * @brief STM32F1 external interrupts 30 | */ 31 | 32 | #ifndef _LIBMAPLE_STM32F1_EXTI_H_ 33 | #define _LIBMAPLE_STM32F1_EXTI_H_ 34 | 35 | #ifdef __cpluspus 36 | extern "C" { 37 | #endif 38 | 39 | struct exti_reg_map; 40 | #define EXTI_BASE ((struct exti_reg_map*)0x40010400) 41 | 42 | #ifdef __cpluspus 43 | } 44 | #endif 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /i2c_maple/maple_OLD/maple_types.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2010 Perry Hung. 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | *****************************************************************************/ 26 | 27 | /** 28 | * @file libmaple/include/libmaple/libmaple_types.h 29 | * 30 | * @brief libmaple's types, and operations on types. 31 | */ 32 | 33 | #ifndef _LIBMAPLE_LIBMAPLE_TYPES_H_ 34 | #define _LIBMAPLE_LIBMAPLE_TYPES_H_ 35 | 36 | #ifdef __cplusplus 37 | extern "C" { 38 | #endif 39 | 40 | typedef unsigned char uint8; 41 | typedef unsigned short uint16; 42 | typedef unsigned int uint32; 43 | typedef unsigned long long uint64; 44 | 45 | typedef signed char int8; 46 | typedef short int16; 47 | typedef int int32; 48 | typedef long long int64; 49 | 50 | typedef void (*voidFuncPtr)(void); 51 | typedef void (*voidArgumentFuncPtr)(void *); 52 | 53 | #define __io volatile 54 | #define __attr_flash __attribute__((section (".USER_FLASH"))) 55 | #define __packed __attribute__((__packed__)) 56 | #define __deprecated __attribute__((__deprecated__)) 57 | #define __weak __attribute__((weak)) 58 | #ifndef __always_inline 59 | #define __always_inline inline __attribute__((always_inline)) 60 | #endif 61 | #ifndef __unused 62 | #define __unused __attribute__((unused)) 63 | #endif 64 | 65 | #ifndef NULL 66 | #define NULL 0 67 | #endif 68 | 69 | #ifndef offsetof 70 | #define offsetof(type, member) __builtin_offsetof(type, member) 71 | #endif 72 | 73 | #ifdef __cplusplus 74 | } 75 | #endif 76 | 77 | #endif 78 | -------------------------------------------------------------------------------- /i2c_maple/mkvec: -------------------------------------------------------------------------------- 1 | #!/bin/ruby 2 | 3 | (0..67).each { |v| 4 | print ".word\tbogus\t\t/* IRQ %2d */\n" % v 5 | } 6 | -------------------------------------------------------------------------------- /i2c_maple/ocd: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # These are proper file locations for Fedora 24 3 | 4 | openocd -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/share/openocd/scripts/target/stm32f1x.cfg 5 | -------------------------------------------------------------------------------- /i2c_maple/startup.c: -------------------------------------------------------------------------------- 1 | /* startup.c 2 | * (c) Tom Trebisky 9-14-2020 3 | * 4 | * This runs before main. 5 | */ 6 | 7 | extern unsigned int __text_end; 8 | 9 | extern unsigned int __data_start; 10 | extern unsigned int __data_end; 11 | 12 | extern unsigned int __bss_start; 13 | extern unsigned int __bss_end; 14 | 15 | void 16 | init_vars ( void ) 17 | { 18 | unsigned int *src = &__text_end; 19 | unsigned int *p; 20 | 21 | } 22 | 23 | /* This gets called from locore.s */ 24 | void 25 | startup ( void ) 26 | { 27 | unsigned int *src = &__text_end; 28 | unsigned int *p; 29 | 30 | /* Zero BSS */ 31 | for ( p = &__bss_start; p < &__bss_end; p++ ) 32 | *p = 0; 33 | 34 | // init_vars (); 35 | 36 | /* Copy initialized data from flash */ 37 | for ( p = &__data_start; p < &__data_end; p++ ) 38 | *p = *src++; 39 | 40 | main (); 41 | } 42 | 43 | /* THE END */ 44 | -------------------------------------------------------------------------------- /interrupt/.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.elf 3 | *.bin 4 | *.dump 5 | *.swp 6 | zzz 7 | zz 8 | z 9 | -------------------------------------------------------------------------------- /interrupt/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for interrupt demo 2 | 3 | #TOOLS = arm-linux-gnu 4 | TOOLS = arm-none-eabi 5 | 6 | # Assembling with gcc makes it want crt0 at link time. 7 | #AS = $(TOOLS)-gcc 8 | AS = $(TOOLS)-as 9 | 10 | COPTS = -mcpu=cortex-m3 -mthumb -Wno-implicit-function-declaration 11 | # Use the -g flag if you intend to use gdb 12 | #CC = $(TOOLS)-gcc $(COPTS) -g 13 | #CC = $(TOOLS)-gcc $(COPTS) 14 | CC = $(TOOLS)-gcc $(COPTS) -O2 15 | 16 | #LD = $(TOOLS)-gcc 17 | LD = $(TOOLS)-ld.bfd 18 | OBJCOPY = $(TOOLS)-objcopy 19 | DUMP = $(TOOLS)-objdump -d 20 | GDB = $(TOOLS)-gdb 21 | 22 | OBJS = locore.o inter.o nvic.o rcc.o gpio.o serial.o timer.o 23 | 24 | all: inter.elf inter.dump 25 | 26 | inter.dump: inter.elf 27 | $(DUMP) inter.elf >inter.dump 28 | 29 | inter.bin: inter.elf 30 | $(OBJCOPY) inter.elf inter.bin -O binary 31 | 32 | inter.elf: $(OBJS) 33 | $(LD) -T inter.lds -o inter.elf $(OBJS) 34 | 35 | locore.o: locore.s 36 | $(AS) locore.s -o locore.o 37 | 38 | .c.o: 39 | $(CC) -o $@ -c $< 40 | 41 | # This is what we expect 42 | #OCDCFG = -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/share/openocd/scripts/target/stm32f1x.cfg 43 | # Some newer chips are shipping with a idcode of 0x2ba01477, this will make them work. 44 | OCDCFG = -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/share/openocd/scripts/target/cs32f1x.cfg 45 | 46 | # This works great once I fixed the base address in the LDS file 47 | flash: inter.elf 48 | openocd $(OCDCFG) -c "program inter.elf verify reset exit" 49 | 50 | welch: notmain.elf 51 | openocd $(OCDCFG) -c "program notmain.elf verify reset exit" 52 | 53 | # No need for this now that the above works 54 | bflash: inter.bin 55 | openocd $(OCDCFG) -c "program inter.bin verify reset exit 0x08000000" 56 | 57 | gdb: 58 | $(GDB) --eval-command="target remote localhost:3333" inter.elf 59 | 60 | gdbtui: 61 | $(GDB) -tui --eval-command="target remote localhost:3333" inter.elf 62 | 63 | clean: 64 | rm -f *.o inter.elf inter.bin inter.dump 65 | -------------------------------------------------------------------------------- /interrupt/README.md: -------------------------------------------------------------------------------- 1 | The game here is to write a USART driver. 2 | To start, we just want to write characters and poll for status. 3 | Reading and interrupts will come later. 4 | 5 | This example also has a lot of code cleanup compared to my 6 | earlier blink examples. 7 | -------------------------------------------------------------------------------- /interrupt/inter.lds: -------------------------------------------------------------------------------- 1 | /* blink.lds 2 | * linker script for blink demo. 3 | * 4 | * Memory layout on my STM32F103 is pretty much like this: 5 | * 6 | * 0x00000000 - 0x07ffffff - aliased to flash or sys memory depending on BOOT jumpers 7 | * 0x08000000 - 0x0800ffff - Flash (64K ... or 128K) 8 | * 0x1ffff000 - 0x1ffff7ff - Boot firmware in system memory 9 | * 0x1ffff800 - 0x1fffffff - option bytes 10 | * 0x20000000 - 0x20004fff - SRAM (20k) 11 | * 0x40000000 - 0x40023400 - peripherals 12 | * 13 | * flash(RX) : ORIGIN = 0x00000000, LENGTH = 128K 14 | */ 15 | MEMORY 16 | { 17 | flash(RX) : ORIGIN = 0x08000000, LENGTH = 128K 18 | sram(WAIL) : ORIGIN = 0x20000000, LENGTH = 20K 19 | } 20 | 21 | SECTIONS 22 | { 23 | .text : { *(.text*) } > flash 24 | .bss : { *(.bss*) } > sram 25 | } 26 | -------------------------------------------------------------------------------- /interrupt/locore.s: -------------------------------------------------------------------------------- 1 | /* locore.s 2 | * Assembler startup file for the STM32F103 3 | * Tom Trebisky 9-24-2016 4 | */ 5 | 6 | # The Cortex M3 is a thumb only processor 7 | .cpu cortex-m3 8 | .thumb 9 | 10 | @ First the "standard" 16 entries for a cortex-m3 11 | .word 0x20005000 /* stack top address */ 12 | .word _reset /* 1 Reset */ 13 | .word bogus /* 2 NMI */ 14 | .word bogus /* 3 Hard Fault */ 15 | .word bogus /* 4 MM Fault */ 16 | .word bogus /* 5 Bus Fault */ 17 | .word bogus /* 6 Usage Fault */ 18 | .word bogus /* 7 RESERVED */ 19 | .word bogus /* 8 RESERVED */ 20 | .word bogus /* 9 RESERVED*/ 21 | .word bogus /* 10 RESERVED */ 22 | .word bogus /* 11 SV call */ 23 | .word bogus /* 12 Debug reserved */ 24 | .word bogus /* 13 RESERVED */ 25 | .word bogus /* 14 PendSV */ 26 | .word systick_handler /* 15 SysTick */ 27 | 28 | @ and now 68 IRQ vectors 29 | .word bogus /* IRQ 0 */ 30 | .word bogus /* IRQ 1 */ 31 | .word bogus /* IRQ 2 */ 32 | .word bogus /* IRQ 3 -- RTC */ 33 | .word bogus /* IRQ 4 */ 34 | .word bogus /* IRQ 5 */ 35 | .word bogus /* IRQ 6 */ 36 | .word bogus /* IRQ 7 */ 37 | .word bogus /* IRQ 8 */ 38 | .word bogus /* IRQ 9 */ 39 | .word bogus /* IRQ 10 */ 40 | .word bogus /* IRQ 11 */ 41 | .word bogus /* IRQ 12 */ 42 | .word bogus /* IRQ 13 */ 43 | .word bogus /* IRQ 14 */ 44 | .word bogus /* IRQ 15 */ 45 | .word bogus /* IRQ 16 */ 46 | .word bogus /* IRQ 17 */ 47 | .word bogus /* IRQ 18 */ 48 | .word bogus /* IRQ 19 */ 49 | .word bogus /* IRQ 20 */ 50 | .word bogus /* IRQ 21 */ 51 | .word bogus /* IRQ 22 */ 52 | .word bogus /* IRQ 23 */ 53 | .word bogus /* IRQ 24 -- Timer 1 break */ 54 | .word bogus /* IRQ 25 -- Timer 1 update */ 55 | .word bogus /* IRQ 26 -- Timer 1 trig */ 56 | .word bogus /* IRQ 27 -- Timer 1 cc */ 57 | .word tim2_handler /* IRQ 28 -- Timer 2 */ 58 | .word tim3_handler /* IRQ 29 -- Timer 3 */ 59 | .word tim4_handler /* IRQ 30 -- Timer 4 */ 60 | .word bogus /* IRQ 31 */ 61 | .word bogus /* IRQ 32 */ 62 | .word bogus /* IRQ 33 */ 63 | .word bogus /* IRQ 34 */ 64 | .word bogus /* IRQ 35 */ 65 | .word bogus /* IRQ 36 */ 66 | .word bogus /* IRQ 37 -- UART 1 */ 67 | .word bogus /* IRQ 38 -- UART 2 */ 68 | .word bogus /* IRQ 39 -- UART 3 */ 69 | .word bogus /* IRQ 40 */ 70 | .word bogus /* IRQ 41 */ 71 | .word bogus /* IRQ 42 */ 72 | .word bogus /* IRQ 43 */ 73 | .word bogus /* IRQ 44 */ 74 | .word bogus /* IRQ 45 */ 75 | .word bogus /* IRQ 46 */ 76 | .word bogus /* IRQ 47 */ 77 | .word bogus /* IRQ 48 */ 78 | .word bogus /* IRQ 49 */ 79 | .word bogus /* IRQ 50 */ 80 | .word bogus /* IRQ 51 */ 81 | .word bogus /* IRQ 52 */ 82 | .word bogus /* IRQ 53 */ 83 | .word bogus /* IRQ 54 */ 84 | .word bogus /* IRQ 55 */ 85 | .word bogus /* IRQ 56 */ 86 | .word bogus /* IRQ 57 */ 87 | .word bogus /* IRQ 58 */ 88 | .word bogus /* IRQ 59 */ 89 | 90 | #ifdef notdef 91 | /* These last 8 aren't assigned on the STM32F103, so we can save a few 92 | * bytes of flash. 93 | */ 94 | .word bogus /* IRQ 60 */ 95 | .word bogus /* IRQ 61 */ 96 | .word bogus /* IRQ 62 */ 97 | .word bogus /* IRQ 63 */ 98 | .word bogus /* IRQ 64 */ 99 | .word bogus /* IRQ 65 */ 100 | .word bogus /* IRQ 66 */ 101 | .word bogus /* IRQ 67 */ 102 | #endif 103 | 104 | .thumb_func 105 | bogus: b . 106 | 107 | .thumb_func 108 | _reset: 109 | bl startup 110 | b . 111 | 112 | /* THE END */ 113 | -------------------------------------------------------------------------------- /interrupt/mkvec: -------------------------------------------------------------------------------- 1 | #!/bin/ruby 2 | 3 | (0..67).each { |v| 4 | print ".word\tbogus\t\t/* IRQ %2d */\n" % v 5 | } 6 | -------------------------------------------------------------------------------- /interrupt/ocd: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # These are proper file locations for Fedora 24 3 | 4 | openocd -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/share/openocd/scripts/target/stm32f1x.cfg 5 | -------------------------------------------------------------------------------- /interrupt/serial.c: -------------------------------------------------------------------------------- 1 | /* serial.c 2 | * (c) Tom Trebisky 7-2-2017 3 | * 4 | * Driver for the STM32F103 usart 5 | */ 6 | 7 | /* One of the 3 uarts */ 8 | struct uart { 9 | volatile unsigned long status; /* 00 */ 10 | volatile unsigned long data; /* 04 */ 11 | volatile unsigned long baud; /* 08 */ 12 | volatile unsigned long cr1; /* 0c */ 13 | volatile unsigned long cr2; /* 10 */ 14 | volatile unsigned long cr3; /* 14 */ 15 | volatile unsigned long gtp; /* 18 - guard time and prescaler */ 16 | }; 17 | 18 | #define UART1_BASE (struct uart *) 0x40013800 19 | #define UART2_BASE (struct uart *) 0x40004400 20 | #define UART3_BASE (struct uart *) 0x40004800 21 | 22 | /* bits in the status register */ 23 | #define ST_PE 0x0001 24 | #define ST_FE 0x0002 25 | #define ST_NE 0x0004 26 | #define ST_OVER 0x0008 27 | #define ST_IDLE 0x0010 28 | #define ST_RXNE 0x0020 /* Receiver not empty */ 29 | #define ST_TC 0x0040 /* Transmission complete */ 30 | #define ST_TXE 0x0080 /* Transmitter empty */ 31 | #define ST_BREAK 0x0100 32 | #define ST_CTS 0x0200 33 | 34 | static void 35 | uart_init ( struct uart *up, int baud ) 36 | { 37 | /* 1 start bit, even parity */ 38 | up->cr1 = 0x340c; 39 | up->cr2 = 0; 40 | up->cr3 = 0; 41 | up->gtp = 0; 42 | 43 | if ( up == UART1_BASE ) 44 | up->baud = get_pclk2() / baud; 45 | else 46 | up->baud = get_pclk1() / baud; 47 | } 48 | 49 | void 50 | serial_init ( void ) 51 | { 52 | gpio_uart1 (); 53 | // uart_init ( UART1_BASE, 9600 ); 54 | // uart_init ( UART1_BASE, 38400 ); 55 | // uart_init ( UART1_BASE, 57600 ); ?? 56 | uart_init ( UART1_BASE, 115200 ); 57 | 58 | #ifdef notdef 59 | gpio_uart2 (); 60 | uart_init ( UART2_BASE, 9600 ); 61 | 62 | gpio_uart3 (); 63 | uart_init ( UART3_BASE, 9600 ); 64 | #endif 65 | } 66 | 67 | void 68 | serial_putc ( int c ) 69 | { 70 | struct uart *up = UART1_BASE; 71 | 72 | if ( c == '\n' ) 73 | serial_putc ( '\r' ); 74 | 75 | while ( ! (up->status & ST_TXE) ) 76 | ; 77 | up->data = c; 78 | } 79 | 80 | void 81 | serial_puts ( char *s ) 82 | { 83 | while ( *s ) 84 | serial_putc ( *s++ ); 85 | } 86 | 87 | /* -------------------------------------------- */ 88 | /* Some IO stuff from Kyu */ 89 | /* -------------------------------------------- */ 90 | 91 | #define HEX(x) ((x)<10 ? '0'+(x) : 'A'+(x)-10) 92 | 93 | #define PUTCHAR(x) *buf++ = (x) 94 | 95 | static char * 96 | shex2( char *buf, int val ) 97 | { 98 | PUTCHAR( HEX((val>>4)&0xf) ); 99 | PUTCHAR( HEX(val&0xf) ); 100 | return buf; 101 | } 102 | 103 | static char * 104 | shex4( char *buf, int val ) 105 | { 106 | buf = shex2(buf,val>>8); 107 | return shex2(buf,val); 108 | } 109 | 110 | static char * 111 | shex8( char *buf, int val ) 112 | { 113 | buf = shex2(buf,val>>24); 114 | buf = shex2(buf,val>>16); 115 | buf = shex2(buf,val>>8); 116 | return shex2(buf,val); 117 | } 118 | 119 | void 120 | show16 ( char *s, int val ) 121 | { 122 | char buf[5]; 123 | 124 | serial_puts ( s ); 125 | shex4 ( buf, val ); 126 | buf[4] = '\0'; 127 | serial_puts ( buf ); 128 | serial_putc ( '\n' ); 129 | } 130 | 131 | /* THE END */ 132 | -------------------------------------------------------------------------------- /lcd/.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.elf 3 | *.bin 4 | *.dump 5 | loader/loader 6 | *.swp 7 | zzz 8 | -------------------------------------------------------------------------------- /lcd/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for i2c/lcd demo 2 | 3 | #TOOLS = arm-linux-gnu 4 | TOOLS = arm-none-eabi 5 | 6 | # Assembling with gcc makes it want crt0 at link time. 7 | #AS = $(TOOLS)-gcc 8 | AS = $(TOOLS)-as 9 | 10 | COPTS = -mcpu=cortex-m3 -mthumb -Wno-implicit-function-declaration 11 | # Use the -g flag if you intend to use gdb 12 | #CC = $(TOOLS)-gcc $(COPTS) -g 13 | #CC = $(TOOLS)-gcc $(COPTS) 14 | CC = $(TOOLS)-gcc $(COPTS) -O2 15 | 16 | #LD = $(TOOLS)-gcc 17 | LD = $(TOOLS)-ld.bfd 18 | OBJCOPY = $(TOOLS)-objcopy 19 | DUMP = $(TOOLS)-objdump -d 20 | GDB = $(TOOLS)-gdb 21 | 22 | OBJS = locore.o lcd.o nvic.o rcc.o afio.o gpio.o serial.o timer.o i2c.o 23 | 24 | all: lcd.elf lcd.dump 25 | 26 | lcd.dump: lcd.elf 27 | $(DUMP) lcd.elf >lcd.dump 28 | 29 | lcd.bin: lcd.elf 30 | $(OBJCOPY) lcd.elf lcd.bin -O binary 31 | 32 | lcd.elf: $(OBJS) 33 | $(LD) -T lcd.lds -o lcd.elf $(OBJS) 34 | 35 | locore.o: locore.s 36 | $(AS) locore.s -o locore.o 37 | 38 | .c.o: 39 | $(CC) -o $@ -c $< 40 | 41 | # This is what we expect 42 | OCDCFG = -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/share/openocd/scripts/target/stm32f1x.cfg 43 | # Some newer chips are shipping with a idcode of 0x2ba01477, this will make them work. 44 | #OCDCFG = -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/share/openocd/scripts/target/cs32f1x.cfg 45 | 46 | # This works great once I fixed the base address in the LDS file 47 | flash: lcd.elf 48 | openocd $(OCDCFG) -c "program lcd.elf verify reset exit" 49 | 50 | gdb: 51 | $(GDB) --eval-command="target remote localhost:3333" lcd.elf 52 | 53 | gdbtui: 54 | $(GDB) -tui --eval-command="target remote localhost:3333" lcd.elf 55 | 56 | clean: 57 | rm -f *.o lcd.elf lcd.bin lcd.dump 58 | -------------------------------------------------------------------------------- /lcd/README.md: -------------------------------------------------------------------------------- 1 | The main purpose here is to write an i2c drive. 2 | It will probably first be developed with some simple i2c device, 3 | but the ultimate purpose is to drive one of the little 16x2 line LCD modules. 4 | 5 | This began by copying my "interrupt" demo and working from there. 6 | 8-31-2020 7 | -------------------------------------------------------------------------------- /lcd/afio.c: -------------------------------------------------------------------------------- 1 | /* afio.c 2 | * (c) Tom Trebisky 9-10-2020 3 | * 4 | * Alternate function gadget driver 5 | * a close brother to gpio, sort of. 6 | * See section 9.3 in the RM 7 | */ 8 | 9 | struct afio { 10 | volatile unsigned long cr; /* 00 */ 11 | volatile unsigned long mapr; /* 04 */ 12 | volatile unsigned long exticr1; /* 08 */ 13 | volatile unsigned long exticr2; /* 0c */ 14 | volatile unsigned long exticr3; /* 10 */ 15 | volatile unsigned long exticr4; /* 14 */ 16 | int _pad; 17 | volatile unsigned long mapr2; /* 1c */ 18 | }; 19 | 20 | #define AFIO_I2C1_REMAP 0x00000002 21 | #define AFIO_SPI1_REMAP 0x00000001 22 | 23 | #define AFIO_BASE (struct afio *) 0x40010000 24 | 25 | /* The purpose of this thing is not to enable or disable 26 | * alternate functions, but to determine which pins they 27 | * get assigned to if there is a choice. 28 | * 29 | * So in the case of i2c, i2c2 has no choice, the pins are 30 | * fixed on PB10 and PB11, so the AFIO does not get involved. 31 | * In the case of i2c1 we get to choose. 32 | * The default is pins PB6 and PB7, 33 | * but if you want PB8 and PB9, you need to set a bit 34 | * in an AFIO register. 35 | */ 36 | 37 | void 38 | afio_init ( void ) 39 | { 40 | /* The AFIO has its own clock and reset ! */ 41 | /* See rcc.c */ 42 | } 43 | 44 | void 45 | afio_i2c1_remap ( int alternate ) 46 | { 47 | struct afio *ap = AFIO_BASE; 48 | 49 | if ( alternate ) 50 | ap->mapr |= AFIO_I2C1_REMAP; 51 | else 52 | ap->mapr &= ~AFIO_I2C1_REMAP; 53 | } 54 | 55 | /* THE END */ 56 | -------------------------------------------------------------------------------- /lcd/lcd.c: -------------------------------------------------------------------------------- 1 | /* lcd.c 2 | * (c) Tom Trebisky 9-2-2020 3 | * 4 | * i2c demo. 5 | * This began as the interrupt demo and added i2c code. 6 | * 7 | * I use the systick interrupt to blink the onboard LED as 8 | * a sanity check. 9 | */ 10 | 11 | void rcc_init ( void ); 12 | void led_init ( int ); 13 | 14 | void led_on ( void ); 15 | void led_off ( void ); 16 | 17 | /* By itself, with an 8 Mhz clock this gives a blink rate of about 2.7 Hz 18 | * so the delay is about 185 ms 19 | * With a 72 Mhz clock this yields a 27.75 ms delay 20 | */ 21 | void 22 | delay ( void ) 23 | { 24 | volatile int count = 1000 * 200; 25 | 26 | while ( count-- ) 27 | ; 28 | } 29 | 30 | /* We scale the above to try to get a 500 ms delay */ 31 | void 32 | big_delay ( void ) 33 | { 34 | volatile int count = 1000 * 540; 35 | 36 | while ( count-- ) 37 | ; 38 | } 39 | 40 | #define PC13 13 41 | #define NBLINKS 2 42 | 43 | /* Turn the LED on for a pulse */ 44 | static void 45 | led_show ( void ) 46 | { 47 | led_on (); 48 | delay (); 49 | led_off (); 50 | } 51 | 52 | static void 53 | led_demo ( void ) 54 | { 55 | int i; 56 | 57 | 58 | for ( ;; ) { 59 | for ( i=0; i flash 24 | .bss : { *(.bss*) } > sram 25 | } 26 | -------------------------------------------------------------------------------- /lcd/mkvec: -------------------------------------------------------------------------------- 1 | #!/bin/ruby 2 | 3 | (0..67).each { |v| 4 | print ".word\tbogus\t\t/* IRQ %2d */\n" % v 5 | } 6 | -------------------------------------------------------------------------------- /lcd/ocd: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # These are proper file locations for Fedora 24 3 | 4 | openocd -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/share/openocd/scripts/target/stm32f1x.cfg 5 | -------------------------------------------------------------------------------- /lithium1/.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.elf 3 | *.bin 4 | *.dump 5 | *.swp 6 | zzz 7 | Data 8 | zplot 9 | *.png 10 | *.dat 11 | -------------------------------------------------------------------------------- /lithium1/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for interrupt demo 2 | 3 | #TOOLS = arm-linux-gnu 4 | TOOLS = arm-none-eabi 5 | 6 | # Assembling with gcc makes it want crt0 at link time. 7 | #AS = $(TOOLS)-gcc 8 | AS = $(TOOLS)-as 9 | 10 | #COPTS = -mcpu=cortex-m3 -mthumb -Wno-implicit-function-declaration 11 | COPTS = -mcpu=cortex-m3 -mthumb -Wno-implicit-function-declaration -fno-builtin-printf 12 | # Use the -g flag if you intend to use gdb 13 | #CC = $(TOOLS)-gcc $(COPTS) -g 14 | #CC = $(TOOLS)-gcc $(COPTS) 15 | CC = $(TOOLS)-gcc $(COPTS) -O2 16 | 17 | #LD = $(TOOLS)-gcc 18 | LD = $(TOOLS)-ld.bfd 19 | OBJCOPY = $(TOOLS)-objcopy 20 | DUMP = $(TOOLS)-objdump -d 21 | GDB = $(TOOLS)-gdb 22 | 23 | OBJS = locore.o lithium.o nvic.o rcc.o gpio.o prf.o serial.o timer.o adc.o 24 | 25 | all: lithium.elf lithium.dump 26 | 27 | lithium.dump: lithium.elf 28 | $(DUMP) lithium.elf >lithium.dump 29 | 30 | lithium.bin: lithium.elf 31 | $(OBJCOPY) lithium.elf lithium.bin -O binary 32 | 33 | lithium.elf: $(OBJS) 34 | $(LD) -T lithium.lds -o lithium.elf $(OBJS) 35 | 36 | locore.o: locore.s 37 | $(AS) locore.s -o locore.o 38 | 39 | .c.o: 40 | $(CC) -o $@ -c $< 41 | 42 | OCDCFG = -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/share/openocd/scripts/target/stm32f1x.cfg 43 | 44 | # This works great once I fixed the base address in the LDS file 45 | flash: lithium.elf 46 | openocd $(OCDCFG) -c "program lithium.elf verify reset exit" 47 | 48 | gdb: 49 | $(GDB) --eval-command="target remote localhost:3333" lithium.elf 50 | 51 | gdbtui: 52 | $(GDB) -tui --eval-command="target remote localhost:3333" lithium.elf 53 | 54 | clean: 55 | rm -f *.o lithium.elf lithium.bin lithium.dump 56 | -------------------------------------------------------------------------------- /lithium1/README.md: -------------------------------------------------------------------------------- 1 | lithium1 - the first version of my lithium ion battery tester. 2 | 3 | Here is is !! My first actual project with the STM32F103. 4 | First in the sense of doing something "useful" beyond just 5 | playing with the STM32F103 itself. 6 | 7 | Derived from adc_serial 11-18-2017 8 | -------------------------------------------------------------------------------- /lithium1/lithium.lds: -------------------------------------------------------------------------------- 1 | /* blink.lds 2 | * linker script for blink demo. 3 | * 4 | * Memory layout on my STM32F103 is pretty much like this: 5 | * 6 | * 0x00000000 - 0x07ffffff - aliased to flash or sys memory depending on BOOT jumpers 7 | * 0x08000000 - 0x0800ffff - Flash (64K ... or 128K) 8 | * 0x1ffff000 - 0x1ffff7ff - Boot firmware in system memory 9 | * 0x1ffff800 - 0x1fffffff - option bytes 10 | * 0x20000000 - 0x20004fff - SRAM (20k) 11 | * 0x40000000 - 0x40023400 - peripherals 12 | * 13 | * flash(RX) : ORIGIN = 0x00000000, LENGTH = 128K 14 | */ 15 | MEMORY 16 | { 17 | flash(RX) : ORIGIN = 0x08000000, LENGTH = 128K 18 | sram(WAIL) : ORIGIN = 0x20000000, LENGTH = 20K 19 | } 20 | 21 | SECTIONS 22 | { 23 | .text : { *(.text*) } > flash 24 | .bss : { *(.bss*) } > sram 25 | } 26 | -------------------------------------------------------------------------------- /lithium1/mah.rb: -------------------------------------------------------------------------------- 1 | #!/bin/ruby 2 | 3 | # mah.rb 4 | # Calculate mAh rating from calibration data. 5 | # 6 | # Tom Trebisky 11-26-2017 7 | # 8 | # A calibration log may have 25,000 points. 9 | 10 | # Only these first two were done with 16.67 11 | #infile = "battery1.log" 12 | #infile = "battery2.log" 13 | #resist = 16.667 14 | 15 | #infile = "battery3.log" 16 | #infile = "battery4.log" 17 | #infile = "battery5.log" 18 | #infile = "Data/2017_11_29.dat" 19 | infile = "Data/2017_11_29b.dat" 20 | 21 | # Now this script is pretty much obsolete and 22 | # included in the lithium.rb script 23 | 24 | resist = 5.0 25 | 26 | sph = 60.0 * 60.0 27 | 28 | f = File.new infile 29 | time = 0 30 | sum = 0.0 31 | f.each { |line| 32 | next unless line =~ /^[0-9]/ 33 | w = line.split 34 | next unless w[2] == "1" 35 | #puts line 36 | volts = w[1].to_f / 1000.0 37 | cur = volts / resist 38 | sum += cur 39 | time += 2 40 | } 41 | f.close 42 | 43 | factor = 2.0 * 1000.0 / 60.0 / 60.0 44 | mah = sum * factor 45 | print "%.1f mAh\n" % mah 46 | 47 | # THE END 48 | -------------------------------------------------------------------------------- /lithium1/mkvec: -------------------------------------------------------------------------------- 1 | #!/bin/ruby 2 | 3 | (0..67).each { |v| 4 | print ".word\tbogus\t\t/* IRQ %2d */\n" % v 5 | } 6 | -------------------------------------------------------------------------------- /lithium1/ocd: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # These are proper file locations for Fedora 24 3 | 4 | openocd -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/share/openocd/scripts/target/stm32f1x.cfg 5 | -------------------------------------------------------------------------------- /lithium1/plotit: -------------------------------------------------------------------------------- 1 | #!/usr/bin/gnuplot -c 2 | set terminal png 3 | set output "Data/2017_11_29b.png" 4 | #plot 'battery2.log' using 1:2 title 'Samsung ICR18650-26C 11-24-2017' with lines 5 | #plot 'battery2.log' using ($1/3600.0):($2/1000.0) title 'Samsung ICR18650-26C 11-24-2017' with lines 6 | #plot 'battery3.log' using ($1/3600.0):($2/1000.0) title 'Panasonic CGR18650CC 11-26-2017' with lines 7 | #plot 'battery4.log' using ($1/3600.0):($2/1000.0) title 'Sony US18650GR 11-27-2017' with lines 8 | #plot 'Data/battery5.log' using ($1/3600.0):($2/1000.0) title 'Sony US18650GR 11-28-2017' with lines 9 | plot 'Data/2017_11_29b.dat' using ($1/3600.0):($2/1000.0) title 'Sony US18650GR 11-29-2017' with lines 10 | -------------------------------------------------------------------------------- /loader/Makefile: -------------------------------------------------------------------------------- 1 | # Tom Trebisky 9-23-2016 2 | 3 | all: loader 4 | 5 | loader: loader.c 6 | cc -o loader loader.c 7 | 8 | clean: 9 | rm -f loader 10 | -------------------------------------------------------------------------------- /loader/README.md: -------------------------------------------------------------------------------- 1 | This is a C program to talk to the serial bootloader on the STM32F103 chip. 2 | 3 | At this time it does only one thing, namely sends the "read unprotect" command, 4 | which I find essential to unlock the chips I have received. 5 | 6 | I expect to develop it into a more general tool. 7 | -------------------------------------------------------------------------------- /memory/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for memory experiments 2 | 3 | #TOOLS = arm-linux-gnu 4 | TOOLS = arm-none-eabi 5 | 6 | # Assembling with gcc makes it want crt0 at link time. 7 | #AS = $(TOOLS)-gcc 8 | AS = $(TOOLS)-as 9 | 10 | COPTS = -mcpu=cortex-m3 -mthumb -Wno-implicit-function-declaration 11 | # Use the -g flag if you intend to use gdb 12 | #CC = $(TOOLS)-gcc $(COPTS) -g 13 | #CC = $(TOOLS)-gcc $(COPTS) 14 | CC = $(TOOLS)-gcc $(COPTS) -O2 15 | 16 | #LD = $(TOOLS)-gcc 17 | LD = $(TOOLS)-ld.bfd 18 | OBJCOPY = $(TOOLS)-objcopy 19 | DUMP = $(TOOLS)-objdump -d 20 | GDB = $(TOOLS)-gdb 21 | OBJDUMP = $(TOOLS)-objdump 22 | 23 | OBJS = locore.o startup.o mem.o rcc.o gpio.o serial.o timer.o 24 | 25 | all: mem.elf mem.dump 26 | 27 | od: 28 | $(OBJDUMP) -h mem.elf 29 | 30 | mem.dump: mem.elf 31 | $(DUMP) mem.elf >mem.dump 32 | 33 | mem.bin: mem.elf 34 | $(OBJCOPY) mem.elf mem.bin -O binary 35 | 36 | mem.elf: $(OBJS) 37 | $(LD) -T mem.lds -o mem.elf $(OBJS) 38 | 39 | locore.o: locore.s 40 | $(AS) locore.s -o locore.o 41 | 42 | .c.o: 43 | $(CC) -o $@ -c $< 44 | 45 | OCDCFG = -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/share/openocd/scripts/target/stm32f1x.cfg 46 | 47 | # This works great once I fixed the base address in the LDS file 48 | flash: mem.elf 49 | openocd $(OCDCFG) -c "program mem.elf verify reset exit" 50 | 51 | gdb: 52 | $(GDB) --eval-command="target remote localhost:3333" mem.elf 53 | 54 | gdbtui: 55 | $(GDB) -tui --eval-command="target remote localhost:3333" mem.elf 56 | 57 | clean: 58 | rm -f *.o mem.elf mem.bin mem.dump 59 | -------------------------------------------------------------------------------- /memory/README.md: -------------------------------------------------------------------------------- 1 | memory 2 | 3 | This started from serial1. 4 | 5 | What I am aiming to do here is to experiment with bss 6 | and initialized data, improve my linker script, and write 7 | a bit of startup code to initialize bss and data properly. 8 | 9 | I will need a serial driver to provide for messages. 10 | -------------------------------------------------------------------------------- /memory/gpio.c: -------------------------------------------------------------------------------- 1 | /* gpio.c 2 | * (c) Tom Trebisky 7-2-2017 3 | */ 4 | 5 | /* One of the 3 gpios */ 6 | struct gpio { 7 | volatile unsigned long cr[2]; 8 | volatile unsigned long idr; 9 | volatile unsigned long odr; 10 | volatile unsigned long bsrr; 11 | volatile unsigned long brr; 12 | volatile unsigned long lock; 13 | }; 14 | 15 | #define GPIOA_BASE (struct gpio *) 0x40010800 16 | #define GPIOB_BASE (struct gpio *) 0x40010C00 17 | #define GPIOC_BASE (struct gpio *) 0x40011000 18 | 19 | /* Each gpio has 16 bits. 20 | * The CR registers each control 8 of these bits. 21 | * cr[0] == cr_low configures 0-7 22 | * cr[1] == cr_high configures 8-15 23 | * Each bit is controlled by a 4 bit field 24 | * The reset state is 0x4 for all (input, floating) 25 | */ 26 | 27 | /* there are only 3 choices for inputs */ 28 | #define INPUT_ANALOG 0x0 29 | #define INPUT_FLOAT 0x4 30 | #define INPUT_PUPD 0x8 31 | 32 | /* For outputs, combine one from the following list of 3 33 | * with one of the 4 that follow. 34 | */ 35 | #define OUTPUT_10M 1 36 | #define OUTPUT_2M 2 37 | #define OUTPUT_50M 3 38 | 39 | #define OUTPUT_PUSH_PULL 0 40 | #define OUTPUT_ODRAIN 4 41 | 42 | #define ALT_PUSH_PULL 8 43 | #define ALT_ODRAIN 0xc 44 | 45 | struct gpio *led_gp; 46 | unsigned long on_mask; 47 | unsigned long off_mask; 48 | 49 | static void 50 | gpio_mode ( struct gpio *gp, int bit, int mode ) 51 | { 52 | int reg; 53 | int conf; 54 | int shift; 55 | 56 | reg = 0; 57 | if ( bit >= 8 ) { 58 | reg = 1; 59 | bit -= 8; 60 | } 61 | shift = bit * 4; 62 | 63 | conf = gp->cr[reg] & ~(0xf<cr[reg] = conf | (mode << shift); 65 | } 66 | 67 | void 68 | led_init ( int bit ) 69 | { 70 | gpio_mode ( GPIOC_BASE, bit, OUTPUT_2M | OUTPUT_ODRAIN ); 71 | 72 | led_gp = GPIOC_BASE; 73 | off_mask = 1 << bit; 74 | on_mask = 1 << (bit+16); 75 | } 76 | 77 | void 78 | led_on ( void ) 79 | { 80 | led_gp->bsrr = on_mask; 81 | } 82 | 83 | void 84 | led_off ( void ) 85 | { 86 | led_gp->bsrr = off_mask; 87 | } 88 | 89 | void 90 | gpio_uart1 ( void ) 91 | { 92 | gpio_mode ( GPIOA_BASE, 9, OUTPUT_50M | ALT_PUSH_PULL ); 93 | // gpio_mode ( GPIOA_BASE, 10, INPUT_FLOAT ); 94 | } 95 | 96 | void 97 | gpio_uart2 ( void ) 98 | { 99 | gpio_mode ( GPIOA_BASE, 2, OUTPUT_50M | ALT_PUSH_PULL ); 100 | // gpio_mode ( GPIOA_BASE, 3, INPUT_FLOAT ); 101 | } 102 | 103 | void 104 | gpio_uart3 ( void ) 105 | { 106 | gpio_mode ( GPIOB_BASE, 10, OUTPUT_50M | ALT_PUSH_PULL ); 107 | // gpio_mode ( GPIOB_BASE, 11, INPUT_FLOAT ); 108 | } 109 | 110 | /* Timer 2, pin 1 is A15 of 0-15 */ 111 | void 112 | gpio_timer ( void ) 113 | { 114 | gpio_mode ( GPIOA_BASE, 15, OUTPUT_50M | ALT_PUSH_PULL ); 115 | } 116 | 117 | /* THE END */ 118 | -------------------------------------------------------------------------------- /memory/locore.s: -------------------------------------------------------------------------------- 1 | /* locore.s 2 | * Assembler startup file for the STM32F103 3 | * Tom Trebisky 9-24-2016 4 | */ 5 | 6 | # The Cortex M3 is a thumb only processor 7 | .cpu cortex-m3 8 | .thumb 9 | 10 | .word 0x20005000 /* stack top address */ 11 | .word _reset /* 1 Reset */ 12 | .word spin /* 2 NMI */ 13 | .word spin /* 3 Hard Fault */ 14 | .word spin /* 4 MM Fault */ 15 | .word spin /* 5 Bus Fault */ 16 | .word spin /* 6 Usage Fault */ 17 | .word spin /* 7 RESERVED */ 18 | .word spin /* 8 RESERVED */ 19 | .word spin /* 9 RESERVED*/ 20 | .word spin /* 10 RESERVED */ 21 | .word spin /* 11 SV call */ 22 | .word spin /* 12 Debug reserved */ 23 | .word spin /* 13 RESERVED */ 24 | .word spin /* 14 PendSV */ 25 | .word spin /* 15 SysTick */ 26 | .word spin /* 16 IRQ0 */ 27 | .word spin /* 17 IRQ1 */ 28 | .word spin /* 18 IRQ2 */ 29 | .word spin /* 19 ... */ 30 | /* On to IRQ67 */ 31 | 32 | spin: b spin 33 | 34 | .thumb_func 35 | _reset: 36 | bl startup 37 | b . 38 | 39 | /* THE END */ 40 | -------------------------------------------------------------------------------- /memory/mem.c: -------------------------------------------------------------------------------- 1 | /* mem.c 2 | * (c) Tom Trebisky 7-2-2017 3 | * 4 | */ 5 | 6 | void rcc_init ( void ); 7 | void led_init ( int ); 8 | 9 | void led_on ( void ); 10 | void led_off ( void ); 11 | 12 | /* By itself, this gives a blink rate of about 2.7 Hz 13 | * so the delay is about 185 ms 14 | */ 15 | void 16 | delay ( void ) 17 | { 18 | volatile int count = 1000 * 200; 19 | 20 | while ( count-- ) 21 | ; 22 | } 23 | 24 | static void 25 | ndelay ( int n ) 26 | { 27 | while ( n-- ) 28 | delay (); 29 | } 30 | 31 | /* We scale the above to try to get a 500 ms delay */ 32 | void 33 | big_delay ( void ) 34 | { 35 | volatile int count = 1000 * 540; 36 | 37 | while ( count-- ) 38 | ; 39 | } 40 | 41 | #define PC13 13 42 | #define NBLINKS 2 43 | 44 | /* Turn the LED on for a pulse */ 45 | static void 46 | led_show ( void ) 47 | { 48 | led_on (); 49 | delay (); 50 | led_off (); 51 | } 52 | 53 | static void 54 | led_demo ( void ) 55 | { 56 | int i; 57 | 58 | 59 | for ( ;; ) { 60 | for ( i=0; i flash 29 | 30 | .bss : 31 | { 32 | . = ALIGN(4); 33 | __bss_start = .; 34 | *(.bss*) 35 | *(COMMON) 36 | . = ALIGN(4); 37 | __bss_end = .; 38 | } > sram 39 | 40 | .data : 41 | { 42 | . = ALIGN(4); 43 | __data_start = .; 44 | *(.data*) 45 | . = ALIGN(4); 46 | __data_end = .; 47 | } > sram AT> flash 48 | 49 | .rodata : 50 | { 51 | . = ALIGN(4); 52 | *(.rodata*) 53 | . = ALIGN(4); 54 | } > flash 55 | 56 | } 57 | -------------------------------------------------------------------------------- /memory/ocd: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # These are proper file locations for Fedora 24 3 | 4 | openocd -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/share/openocd/scripts/target/stm32f1x.cfg 5 | -------------------------------------------------------------------------------- /memory/startup.c: -------------------------------------------------------------------------------- 1 | /* startup.c 2 | * (c) Tom Trebisky 9-14-2020 3 | * 4 | * This runs before main. 5 | */ 6 | 7 | extern unsigned int __text_end; 8 | 9 | extern unsigned int __data_start; 10 | extern unsigned int __data_end; 11 | 12 | extern unsigned int __bss_start; 13 | extern unsigned int __bss_end; 14 | 15 | void 16 | init_vars ( void ) 17 | { 18 | unsigned int *src = &__text_end; 19 | unsigned int *p; 20 | 21 | } 22 | 23 | /* This gets called from locore.s */ 24 | void 25 | startup ( void ) 26 | { 27 | unsigned int *src = &__text_end; 28 | unsigned int *p; 29 | 30 | /* Zero BSS */ 31 | for ( p = &__bss_start; p < &__bss_end; p++ ) 32 | *p = 0; 33 | 34 | // init_vars (); 35 | 36 | /* Copy initialized data from flash */ 37 | for ( p = &__data_start; p < &__data_end; p++ ) 38 | *p = *src++; 39 | 40 | main (); 41 | } 42 | 43 | /* THE END */ 44 | -------------------------------------------------------------------------------- /memory/timer.c: -------------------------------------------------------------------------------- 1 | /* Timer.c 2 | * (c) Tom Trebisky 7-5-2017 3 | * 4 | * Driver for the STM32F103 timer 5 | */ 6 | 7 | /* One of the 4 timers */ 8 | 9 | /* Note that timer 1 is the "advanced" timer and this 10 | * code is written for the general purpose timers 2,3,4 11 | */ 12 | 13 | /* Timer 1 gets a version of PCLK2 14 | * Timer 2,3,4 get a version of PCLK1 15 | * If the PCLK prescaler is == 1, they get the straight PCLK 16 | * if the PCLK prescaler is != 1, they get PCLK * 2 17 | */ 18 | 19 | struct timer { 20 | volatile unsigned long cr1; /* 00 */ 21 | volatile unsigned long cr2; /* 04 */ 22 | volatile unsigned long smcr; /* 08 */ 23 | volatile unsigned long dier; /* 0c */ 24 | volatile unsigned long sr; /* 10 */ 25 | volatile unsigned long egr; /* 14 */ 26 | volatile unsigned long ccmr1; /* 18 */ 27 | volatile unsigned long ccmr2; /* 1c */ 28 | volatile unsigned long ccer; /* 20 */ 29 | volatile unsigned long cnt; /* 24 */ 30 | volatile unsigned long psc; /* 28 */ 31 | volatile unsigned long arr; /* 2c */ 32 | long _pad0; 33 | volatile unsigned long ccr1; /* 34 */ 34 | volatile unsigned long ccr2; /* 38 */ 35 | volatile unsigned long ccr3; /* 3c */ 36 | volatile unsigned long ccr4; /* 40 */ 37 | long _pad1; 38 | volatile unsigned long dcr; /* 48 */ 39 | volatile unsigned long dmar; /* 4c */ 40 | }; 41 | 42 | #define TIMER1_BASE (struct timer *) 0x40012C00 43 | #define TIMER2_BASE (struct timer *) 0x40000000 44 | #define TIMER3_BASE (struct timer *) 0x40000400 45 | #define TIMER4_BASE (struct timer *) 0x40000800 46 | 47 | /* Timer 2, pin 1 is A15 of 0-15 */ 48 | void 49 | timer_init ( void ) 50 | { 51 | gpio_timer (); 52 | } 53 | 54 | /* THE END */ 55 | -------------------------------------------------------------------------------- /serial1/.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.elf 3 | *.bin 4 | *.dump 5 | *.swp 6 | zzz 7 | -------------------------------------------------------------------------------- /serial1/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for talk serial demo 2 | 3 | #TOOLS = arm-linux-gnu 4 | TOOLS = arm-none-eabi 5 | 6 | # Assembling with gcc makes it want crt0 at link time. 7 | #AS = $(TOOLS)-gcc 8 | AS = $(TOOLS)-as 9 | 10 | COPTS = -mcpu=cortex-m3 -mthumb -Wno-implicit-function-declaration 11 | # Use the -g flag if you intend to use gdb 12 | #CC = $(TOOLS)-gcc $(COPTS) -g 13 | #CC = $(TOOLS)-gcc $(COPTS) 14 | CC = $(TOOLS)-gcc $(COPTS) -O2 15 | 16 | #LD = $(TOOLS)-gcc 17 | LD = $(TOOLS)-ld.bfd 18 | OBJCOPY = $(TOOLS)-objcopy 19 | DUMP = $(TOOLS)-objdump -d 20 | GDB = $(TOOLS)-gdb 21 | 22 | OBJS = locore.o talk.o rcc.o gpio.o serial.o timer.o 23 | 24 | all: talk.elf talk.dump 25 | 26 | talk.dump: talk.elf 27 | $(DUMP) talk.elf >talk.dump 28 | 29 | talk.bin: talk.elf 30 | $(OBJCOPY) talk.elf talk.bin -O binary 31 | 32 | talk.elf: $(OBJS) 33 | $(LD) -T talk.lds -o talk.elf $(OBJS) 34 | 35 | locore.o: locore.s 36 | $(AS) locore.s -o locore.o 37 | 38 | .c.o: 39 | $(CC) -o $@ -c $< 40 | 41 | OCDCFG = -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/share/openocd/scripts/target/stm32f1x.cfg 42 | 43 | # This works great once I fixed the base address in the LDS file 44 | flash: talk.elf 45 | openocd $(OCDCFG) -c "program talk.elf verify reset exit" 46 | 47 | # No need for this now that the above works 48 | bflash: talk.bin 49 | openocd $(OCDCFG) -c "program talk.bin verify reset exit 0x08000000" 50 | 51 | gdb: 52 | $(GDB) --eval-command="target remote localhost:3333" talk.elf 53 | 54 | gdbtui: 55 | $(GDB) -tui --eval-command="target remote localhost:3333" talk.elf 56 | 57 | clean: 58 | rm -f *.o talk.elf talk.bin talk.dump 59 | -------------------------------------------------------------------------------- /serial1/README.md: -------------------------------------------------------------------------------- 1 | The game here is to write a USART driver. 2 | To start, we just want to write characters and poll for status. 3 | Reading and interrupts will come later. 4 | 5 | This requires a USB to serial gadget connected to PB9 and PB10 6 | Connect PA9 to RX 7 | Connect PA10 to TX 8 | also connect ground. 9 | 10 | This example also has a lot of code cleanup compared to my 11 | earlier blink examples. 12 | -------------------------------------------------------------------------------- /serial1/gpio.c: -------------------------------------------------------------------------------- 1 | /* gpio.c 2 | * (c) Tom Trebisky 7-2-2017 3 | */ 4 | 5 | /* One of the 3 gpios */ 6 | struct gpio { 7 | volatile unsigned long cr[2]; 8 | volatile unsigned long idr; 9 | volatile unsigned long odr; 10 | volatile unsigned long bsrr; 11 | volatile unsigned long brr; 12 | volatile unsigned long lock; 13 | }; 14 | 15 | #define GPIOA_BASE (struct gpio *) 0x40010800 16 | #define GPIOB_BASE (struct gpio *) 0x40010C00 17 | #define GPIOC_BASE (struct gpio *) 0x40011000 18 | 19 | /* Each gpio has 16 bits. 20 | * The CR registers each control 8 of these bits. 21 | * cr[0] == cr_low configures 0-7 22 | * cr[1] == cr_high configures 8-15 23 | * Each bit is controlled by a 4 bit field 24 | * The reset state is 0x4 for all (input, floating) 25 | */ 26 | 27 | /* there are only 3 choices for inputs */ 28 | #define INPUT_ANALOG 0x0 29 | #define INPUT_FLOAT 0x4 30 | #define INPUT_PUPD 0x8 31 | 32 | /* For outputs, combine one from the following list of 3 33 | * with one of the 4 that follow. 34 | */ 35 | #define OUTPUT_10M 1 36 | #define OUTPUT_2M 2 37 | #define OUTPUT_50M 3 38 | 39 | #define OUTPUT_PUSH_PULL 0 40 | #define OUTPUT_ODRAIN 4 41 | 42 | #define ALT_PUSH_PULL 8 43 | #define ALT_ODRAIN 0xc 44 | 45 | struct gpio *led_gp; 46 | unsigned long on_mask; 47 | unsigned long off_mask; 48 | 49 | static void 50 | gpio_mode ( struct gpio *gp, int bit, int mode ) 51 | { 52 | int reg; 53 | int conf; 54 | int shift; 55 | 56 | reg = 0; 57 | if ( bit >= 8 ) { 58 | reg = 1; 59 | bit -= 8; 60 | } 61 | shift = bit * 4; 62 | 63 | conf = gp->cr[reg] & ~(0xf<cr[reg] = conf | (mode << shift); 65 | } 66 | 67 | void 68 | led_init ( int bit ) 69 | { 70 | gpio_mode ( GPIOC_BASE, bit, OUTPUT_2M | OUTPUT_ODRAIN ); 71 | 72 | led_gp = GPIOC_BASE; 73 | off_mask = 1 << bit; 74 | on_mask = 1 << (bit+16); 75 | } 76 | 77 | void 78 | led_on ( void ) 79 | { 80 | led_gp->bsrr = on_mask; 81 | } 82 | 83 | void 84 | led_off ( void ) 85 | { 86 | led_gp->bsrr = off_mask; 87 | } 88 | 89 | void 90 | gpio_uart1 ( void ) 91 | { 92 | gpio_mode ( GPIOA_BASE, 9, OUTPUT_50M | ALT_PUSH_PULL ); 93 | // gpio_mode ( GPIOA_BASE, 10, INPUT_FLOAT ); 94 | } 95 | 96 | void 97 | gpio_uart2 ( void ) 98 | { 99 | gpio_mode ( GPIOA_BASE, 2, OUTPUT_50M | ALT_PUSH_PULL ); 100 | // gpio_mode ( GPIOA_BASE, 3, INPUT_FLOAT ); 101 | } 102 | 103 | void 104 | gpio_uart3 ( void ) 105 | { 106 | gpio_mode ( GPIOB_BASE, 10, OUTPUT_50M | ALT_PUSH_PULL ); 107 | // gpio_mode ( GPIOB_BASE, 11, INPUT_FLOAT ); 108 | } 109 | 110 | /* Timer 2, pin 1 is A15 of 0-15 */ 111 | void 112 | gpio_timer ( void ) 113 | { 114 | gpio_mode ( GPIOA_BASE, 15, OUTPUT_50M | ALT_PUSH_PULL ); 115 | } 116 | 117 | /* THE END */ 118 | -------------------------------------------------------------------------------- /serial1/locore.s: -------------------------------------------------------------------------------- 1 | /* locore.s 2 | * Assembler startup file for the STM32F103 3 | * Tom Trebisky 9-24-2016 4 | */ 5 | 6 | # The Cortex M3 is a thumb only processor 7 | .cpu cortex-m3 8 | .thumb 9 | 10 | .word 0x20005000 /* stack top address */ 11 | .word _reset /* 1 Reset */ 12 | .word spin /* 2 NMI */ 13 | .word spin /* 3 Hard Fault */ 14 | .word spin /* 4 MM Fault */ 15 | .word spin /* 5 Bus Fault */ 16 | .word spin /* 6 Usage Fault */ 17 | .word spin /* 7 RESERVED */ 18 | .word spin /* 8 RESERVED */ 19 | .word spin /* 9 RESERVED*/ 20 | .word spin /* 10 RESERVED */ 21 | .word spin /* 11 SV call */ 22 | .word spin /* 12 Debug reserved */ 23 | .word spin /* 13 RESERVED */ 24 | .word spin /* 14 PendSV */ 25 | .word spin /* 15 SysTick */ 26 | .word spin /* 16 IRQ0 */ 27 | .word spin /* 17 IRQ1 */ 28 | .word spin /* 18 IRQ2 */ 29 | .word spin /* 19 ... */ 30 | /* On to IRQ67 */ 31 | 32 | spin: b spin 33 | 34 | .thumb_func 35 | _reset: 36 | bl startup 37 | b . 38 | 39 | /* THE END */ 40 | -------------------------------------------------------------------------------- /serial1/ocd: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # These are proper file locations for Fedora 24 3 | 4 | openocd -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/share/openocd/scripts/target/stm32f1x.cfg 5 | -------------------------------------------------------------------------------- /serial1/serial.c: -------------------------------------------------------------------------------- 1 | /* serial.c 2 | * (c) Tom Trebisky 7-2-2017 3 | * 4 | * Driver for the STM32F103 usart 5 | */ 6 | 7 | /* One of the 3 uarts */ 8 | struct uart { 9 | volatile unsigned long status; /* 00 */ 10 | volatile unsigned long data; /* 04 */ 11 | volatile unsigned long baud; /* 08 */ 12 | volatile unsigned long cr1; /* 0c */ 13 | volatile unsigned long cr2; /* 10 */ 14 | volatile unsigned long cr3; /* 14 */ 15 | volatile unsigned long gtp; /* 18 - guard time and prescaler */ 16 | }; 17 | 18 | #define UART1_BASE (struct uart *) 0x40013800 19 | #define UART2_BASE (struct uart *) 0x40004400 20 | #define UART3_BASE (struct uart *) 0x40004800 21 | 22 | /* bits in the status register */ 23 | #define ST_PE 0x0001 24 | #define ST_FE 0x0002 25 | #define ST_NE 0x0004 26 | #define ST_OVER 0x0008 27 | #define ST_IDLE 0x0010 28 | #define ST_RXNE 0x0020 /* Receiver not empty */ 29 | #define ST_TC 0x0040 /* Transmission complete */ 30 | #define ST_TXE 0x0080 /* Transmitter empty */ 31 | #define ST_BREAK 0x0100 32 | #define ST_CTS 0x0200 33 | 34 | static void 35 | uart_init ( struct uart *up, int baud ) 36 | { 37 | /* 1 start bit, even parity */ 38 | up->cr1 = 0x340c; 39 | up->cr2 = 0; 40 | up->cr3 = 0; 41 | up->gtp = 0; 42 | 43 | if ( up == UART1_BASE ) 44 | up->baud = get_pclk2() / baud; 45 | else 46 | up->baud = get_pclk1() / baud; 47 | } 48 | 49 | void 50 | serial_init ( void ) 51 | { 52 | gpio_uart1 (); 53 | // uart_init ( UART1_BASE, 9600 ); 54 | // uart_init ( UART1_BASE, 38400 ); 55 | // uart_init ( UART1_BASE, 57600 ); ?? 56 | uart_init ( UART1_BASE, 115200 ); 57 | 58 | #ifdef notdef 59 | gpio_uart2 (); 60 | uart_init ( UART2_BASE, 9600 ); 61 | 62 | gpio_uart3 (); 63 | uart_init ( UART3_BASE, 9600 ); 64 | #endif 65 | } 66 | 67 | void 68 | serial_putc ( int c ) 69 | { 70 | struct uart *up = UART1_BASE; 71 | 72 | while ( ! (up->status & ST_TXE) ) 73 | ; 74 | up->data = c; 75 | } 76 | 77 | /* THE END */ 78 | -------------------------------------------------------------------------------- /serial1/talk.c: -------------------------------------------------------------------------------- 1 | /* talk.c 2 | * (c) Tom Trebisky 7-2-2017 3 | * 4 | */ 5 | 6 | void rcc_init ( void ); 7 | void led_init ( int ); 8 | 9 | void led_on ( void ); 10 | void led_off ( void ); 11 | 12 | /* By itself, this gives a blink rate of about 2.7 Hz 13 | * so the delay is about 185 ms 14 | */ 15 | void 16 | delay ( void ) 17 | { 18 | volatile int count = 1000 * 200; 19 | 20 | while ( count-- ) 21 | ; 22 | } 23 | 24 | /* We scale the above to try to get a 500 ms delay */ 25 | void 26 | big_delay ( void ) 27 | { 28 | volatile int count = 1000 * 540; 29 | 30 | while ( count-- ) 31 | ; 32 | } 33 | 34 | #define PC13 13 35 | #define NBLINKS 2 36 | 37 | /* Turn the LED on for a pulse */ 38 | static void 39 | led_show ( void ) 40 | { 41 | led_on (); 42 | delay (); 43 | led_off (); 44 | } 45 | 46 | static void 47 | led_demo ( void ) 48 | { 49 | int i; 50 | 51 | 52 | for ( ;; ) { 53 | for ( i=0; i flash 24 | .bss : { *(.bss*) } > sram 25 | } 26 | -------------------------------------------------------------------------------- /serial1/timer.c: -------------------------------------------------------------------------------- 1 | /* Timer.c 2 | * (c) Tom Trebisky 7-5-2017 3 | * 4 | * Driver for the STM32F103 timer 5 | */ 6 | 7 | /* One of the 4 timers */ 8 | 9 | /* Note that timer 1 is the "advanced" timer and this 10 | * code is written for the general purpose timers 2,3,4 11 | */ 12 | 13 | /* Timer 1 gets a version of PCLK2 14 | * Timer 2,3,4 get a version of PCLK1 15 | * If the PCLK prescaler is == 1, they get the straight PCLK 16 | * if the PCLK prescaler is != 1, they get PCLK * 2 17 | */ 18 | 19 | struct timer { 20 | volatile unsigned long cr1; /* 00 */ 21 | volatile unsigned long cr2; /* 04 */ 22 | volatile unsigned long smcr; /* 08 */ 23 | volatile unsigned long dier; /* 0c */ 24 | volatile unsigned long sr; /* 10 */ 25 | volatile unsigned long egr; /* 14 */ 26 | volatile unsigned long ccmr1; /* 18 */ 27 | volatile unsigned long ccmr2; /* 1c */ 28 | volatile unsigned long ccer; /* 20 */ 29 | volatile unsigned long cnt; /* 24 */ 30 | volatile unsigned long psc; /* 28 */ 31 | volatile unsigned long arr; /* 2c */ 32 | long _pad0; 33 | volatile unsigned long ccr1; /* 34 */ 34 | volatile unsigned long ccr2; /* 38 */ 35 | volatile unsigned long ccr3; /* 3c */ 36 | volatile unsigned long ccr4; /* 40 */ 37 | long _pad1; 38 | volatile unsigned long dcr; /* 48 */ 39 | volatile unsigned long dmar; /* 4c */ 40 | }; 41 | 42 | #define TIMER1_BASE (struct timer *) 0x40012C00 43 | #define TIMER2_BASE (struct timer *) 0x40000000 44 | #define TIMER3_BASE (struct timer *) 0x40000400 45 | #define TIMER4_BASE (struct timer *) 0x40000800 46 | 47 | /* Timer 2, pin 1 is A15 of 0-15 */ 48 | void 49 | timer_init ( void ) 50 | { 51 | gpio_timer (); 52 | } 53 | 54 | /* THE END */ 55 | -------------------------------------------------------------------------------- /serial_boot/.gitignore: -------------------------------------------------------------------------------- 1 | *.dis 2 | *.elf 3 | *.out 4 | -------------------------------------------------------------------------------- /serial_boot/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for serial bootrom disassembly 2 | # Tom Trebisky 9-25-2016 3 | 4 | TOOLS = arm-none-eabi 5 | 6 | # DUMP = $(TOOLS)-objdump -D -b binary -marm 7 | # -mcpu=cortex-m3 -mthumb 8 | #-marm flash_orig_dump.bin -Mforce-thumb 9 | 10 | BDUMP = $(TOOLS)-objdump -D -b binary -marm -Mforce-thumb 11 | 12 | all: boot.odx 13 | 14 | naive.dis: serialboot.bin 15 | $(BDUMP) serialboot.bin >naive.dis 16 | 17 | naive2.dis: serialboot.bin 18 | $(BDUMP) --start-address=0x20 serialboot.bin >naive2.dis 19 | 20 | x.dis: serialboot.bin 21 | $(BDUMP) --start-address=0x10 serialboot.bin >x.dis 22 | 23 | reloc.dis: naive2.dis 24 | ./reloc >reloc.dis 25 | 26 | boot.odx: serialboot.bin 27 | odx serialboot.bin >boot.odx 28 | 29 | # Does not work 30 | #EDUMP = $(TOOLS)-objdump -D -marmv5 -Mforce-thumb 31 | #boot.dis: boot.elf 32 | # $(EDUMP) --start-address=0x1ffff020 boot.elf >boot.dis 33 | 34 | boot.elf: serialboot.bin wrap 35 | ./wrap serialboot.bin boot.elf 36 | 37 | longs.out: 38 | ./longs serialboot.bin >longs.out 39 | 40 | #wrap: wrap.c 41 | # cc -o wrap wrap.c 42 | 43 | GDB = $(TOOLS)-gdb 44 | gdb: 45 | $(GDB) --eval-command="target remote localhost:3333" blink.elf 46 | 47 | gdbtui: 48 | $(GDB) -tui --eval-command="target remote localhost:3333" blink.elf 49 | 50 | clean: 51 | rm -f *.dis wrap longs.out boot.odx 52 | rm -r boot.elf 53 | -------------------------------------------------------------------------------- /serial_boot/README.md: -------------------------------------------------------------------------------- 1 | This is an effort to disassemble the serial bootloader 2 | in the STM32F103. 3 | 4 | The disassembly is done, but anotating it will be a work in 5 | progress. Keep an eye on boot.txt if you want to watch the 6 | fun. The rest of the files are some of the tools I use to 7 | do the raw disassembly. 8 | 9 | It is only about 800 lines of code. 10 | -------------------------------------------------------------------------------- /serial_boot/longs: -------------------------------------------------------------------------------- 1 | #!/usr/bin/ruby 2 | # 3 | # Tom Trebisky 9-25-2016 4 | # 5 | # "longs" 6 | # 7 | # dump a binary image as a bunch of longs 8 | # 9 | 10 | $start_addr = 0x1ffff000 11 | 12 | def dump_line ( addr, r, w ) 13 | print "%08x: " % addr 14 | 15 | print "%08x" % w 16 | print "\t\t" 17 | print ".word 0x%08x " % w 18 | print "\n" 19 | end 20 | 21 | def dofile ( name ) 22 | f = File.new( name ) 23 | 24 | addr = $start_addr 25 | while buf = f.read( 4 ) 26 | # The read yields a string 27 | # we unpack to a single 32 bit item 28 | raw = buf.unpack('N') 29 | val = buf.unpack('V') 30 | 31 | dump_line addr, raw, val 32 | addr += 4 33 | end 34 | 35 | f.close 36 | end 37 | 38 | # ----------------------------------------- 39 | 40 | if ARGV.size < 1 41 | puts "usage: longs file" 42 | exit 43 | end 44 | 45 | ARGV.each { |arg| 46 | dofile arg 47 | } 48 | 49 | # THE END 50 | -------------------------------------------------------------------------------- /serial_boot/ocd: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # These are proper file locations for Fedora 24 3 | 4 | openocd -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/share/openocd/scripts/target/stm32f1x.cfg 5 | -------------------------------------------------------------------------------- /serial_boot/odx: -------------------------------------------------------------------------------- 1 | #!/usr/bin/ruby 2 | # 3 | # Tom Trebisky 4 | # 6-1-2016 5 | # 6 | # A new version of my odx program 7 | # 8 | # options for byte swapping 9 | # and to mask high bit for character display 10 | # 11 | # -s -- swap bytes in 16 bit words 12 | # -h -- mask high bit for ascii display 13 | # -ixxx - start addresses with given hex value 14 | 15 | $mask_high = false 16 | $swap_bytes = false 17 | $start_addr = 0 18 | 19 | # dump a single line 20 | def printc ( val ) 21 | val &= 0x7f if $mask_high 22 | if val >= 0x20 and val <= 0x7e 23 | print val.chr 24 | else 25 | print " " 26 | end 27 | end 28 | 29 | def dump_line ( addr, words ) 30 | print "%08x" % addr, " " 31 | 32 | words.each { |w| 33 | print "%04x " % w 34 | } 35 | 36 | print " " 37 | 38 | words.each { |w| 39 | printc ( (w>>8) & 0xff ) 40 | printc ( w & 0xff ) 41 | } 42 | print "\n" 43 | end 44 | 45 | def dofile ( name ) 46 | f = File.new( name ) 47 | 48 | addr = $start_addr 49 | while buf = f.read( 16 ) 50 | # The read yields a string 51 | # we unpack to an array of 16 bit items 52 | if ( $swap_bytes ) 53 | # little endian unpack 54 | #words = buf.unpack('S*') 55 | words = buf.unpack('v*') 56 | else 57 | # big endian unpack 58 | words = buf.unpack('n*') 59 | end 60 | dump_line(addr,words) 61 | addr += 16 62 | end 63 | 64 | f.close 65 | end 66 | 67 | # ----------------------------------------- 68 | 69 | if ARGV.size < 1 70 | puts "usage: odx file" 71 | exit 72 | end 73 | 74 | ARGV.each { |arg| 75 | if ( arg =~ /-i/ ) 76 | $start_addr = arg.sub(/^-i/,"").hex 77 | elsif ( arg =~ /-s/ ) 78 | $swap_bytes = true 79 | elsif ( arg =~ /^-h/ ) 80 | $mask_high = true 81 | else 82 | dofile arg 83 | end 84 | } 85 | 86 | # THE END 87 | -------------------------------------------------------------------------------- /serial_boot/reloc: -------------------------------------------------------------------------------- 1 | #!/bin/ruby 2 | 3 | # relocate zero based disassembly 4 | # Tom Trebisky 9-25-2016 5 | 6 | infile = "naive2.dis" 7 | 8 | $base = 0x1ffff000 9 | start = true 10 | 11 | def fixref ( l ) 12 | w = l.split "(0x" 13 | val = w[1].chop.hex 14 | return w[0] + "(0x%08x)" % (val + $base) 15 | end 16 | 17 | def fixaddr ( l ) 18 | w = l.split ":" 19 | tail = w[1] 20 | if w.size > 2 21 | tail = w[1..-1].join(":") 22 | end 23 | addr = $base + w[0].hex 24 | return "%08x:"%addr + tail 25 | end 26 | 27 | def fixbranch ( l ) 28 | return l if l =~ /UNDEFINED/ 29 | return l if l =~ /\)$/ 30 | w = l.split 31 | n = w.size 32 | return l if w[n-1] !~ /^0x/ 33 | return l if w[n-2] == ";" 34 | 35 | addr = $base + w[n-1].hex 36 | start = l.chomp.sub /0x.*/,"" 37 | return start + "0x%08x" % addr 38 | end 39 | 40 | File.foreach(infile) { |x| 41 | if start 42 | if x =~ /^ 20:/ 43 | start = false 44 | else 45 | next 46 | end 47 | end 48 | if x =~ /\(0x/ 49 | x = fixref x 50 | end 51 | x = fixaddr x 52 | x = fixbranch x 53 | puts x 54 | } 55 | -------------------------------------------------------------------------------- /usb/.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.elf 3 | *.dump 4 | *.swp 5 | *.map 6 | zzz 7 | zz 8 | z 9 | OLD 10 | OLD/* 11 | tags 12 | -------------------------------------------------------------------------------- /usb/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for interrupt demo 2 | 3 | #TOOLS = arm-linux-gnu 4 | TOOLS = arm-none-eabi 5 | 6 | # Assembling with gcc makes it want crt0 at link time. 7 | #AS = $(TOOLS)-gcc 8 | AS = $(TOOLS)-as 9 | 10 | #COPTS = -mcpu=cortex-m3 -mthumb -Wno-implicit-function-declaration 11 | COPTS = -mcpu=cortex-m3 -mthumb -Wno-implicit-function-declaration -fno-builtin-printf -fno-builtin 12 | # Use the -g flag if you intend to use gdb 13 | #CC = $(TOOLS)-gcc $(COPTS) -g 14 | #CC = $(TOOLS)-gcc $(COPTS) 15 | CC = $(TOOLS)-gcc $(COPTS) -O2 16 | 17 | ##-Wl,-gc-sections,-Map,main.map -Wl,--cref 18 | 19 | #LDOPTS = --no-warn-rwx-segments 20 | LDOPTS = -no-warn-rwx-segments -Map dragoon.map --cref 21 | 22 | #LD = $(TOOLS)-gcc 23 | LD = $(TOOLS)-ld.bfd $(LDOPTS) 24 | OBJCOPY = $(TOOLS)-objcopy 25 | DUMP = $(TOOLS)-objdump -d 26 | GDB = $(TOOLS)-gdb 27 | 28 | OBJS = locore.o main.o startup.o nvic.o rcc.o gpio.o prf.o kyulib.o serial.o timer.o usb.o usb_enum.o usb_watch.o 29 | 30 | all: dragoon.elf dragoon.dump tags 31 | 32 | dragoon.dump: dragoon.elf 33 | $(DUMP) dragoon.elf >dragoon.dump 34 | 35 | dragoon.bin: dragoon.elf 36 | $(OBJCOPY) dragoon.elf dragoon.bin -O binary 37 | 38 | dragoon.elf: $(OBJS) 39 | $(LD) -T dragoon.lds -o dragoon.elf $(OBJS) 40 | size dragoon.elf 41 | 42 | locore.o: locore.s 43 | $(AS) locore.s -o locore.o 44 | 45 | .c.o: 46 | $(CC) -o $@ -c $< 47 | 48 | OCDCFG = -f /usr/share/openocd/scripts/interface/stlink.cfg -f /usr/share/openocd/scripts/target/stm32f1x.cfg 49 | 50 | # Does not work yet for the Maple 51 | # reset takes the CPU to 08000000 and either nothing is there 52 | # or it runs an old blink demo it finds there. 53 | flash: dragoon.elf 54 | openocd $(OCDCFG) -c "program dragoon.elf verify reset exit" 55 | 56 | ocd: 57 | openocd $(OCDCFG) 58 | 59 | gdb: 60 | $(GDB) --eval-command="target remote localhost:3333" dragoon.elf 61 | 62 | gdbtui: 63 | $(GDB) -tui --eval-command="target remote localhost:3333" dragoon.elf 64 | 65 | # This is useful when a vid/pid is selected that is not known by any standard 66 | # driver. I used it with the "daed" code -- and it is persistent until the 67 | # system is rebooted. 68 | usb-serial: 69 | echo 0483 5722 >/sys/bus/usb-serial/drivers/generic/new_id 70 | 71 | tags: 72 | ctags -R . 73 | 74 | clean: 75 | rm -f *.o dragoon.elf dragoon.bin dragoon.dump dragoon.map 76 | rm -f tags 77 | -------------------------------------------------------------------------------- /usb/README.md: -------------------------------------------------------------------------------- 1 | usb 12-6-2023 2 | 3 | This is my "final" USB project. 4 | 5 | Development was done on a Maple board 6 | -------------------------------------------------------------------------------- /usb/dragoon.lds: -------------------------------------------------------------------------------- 1 | /* baboon.lds 2 | * linker script for baboon demo. 3 | * 4 | * Memory layout on my STM32F103 is pretty much like this: 5 | * 6 | * 0x00000000 - 0x07ffffff - aliased to flash or sys memory depending on BOOT jumpers 7 | * 0x08000000 - 0x0800ffff - Flash (64K ... or 128K) 8 | * 0x1ffff000 - 0x1ffff7ff - Boot firmware in system memory 9 | * 0x1ffff800 - 0x1fffffff - option bytes 10 | * 0x20000000 - 0x20004fff - SRAM (20k) 11 | * 0x40000000 - 0x40023400 - peripherals 12 | * 13 | * flash(RX) : ORIGIN = 0x00000000, LENGTH = 128K 14 | * flash(RX) : ORIGIN = 0x08005000, LENGTH = 128K 15 | */ 16 | 17 | /* The important thing for a Maple board is that the flash 18 | * is linked to offset 5000 to avoid overwriting the USB 19 | * loader. 20 | */ 21 | MEMORY 22 | { 23 | flash(RX) : ORIGIN = 0x08000000, LENGTH = 128K 24 | sram(RW) : ORIGIN = 0x20000000, LENGTH = 20K 25 | } 26 | 27 | /* An experiment 12-3-2023 28 | */ 29 | 30 | PERIPH_BASE = 0x40000000; 31 | APB1PERIPH = PERIPH_BASE; 32 | USB = APB1PERIPH + 0x5C00; 33 | USB_PMA = APB1PERIPH + 0x6000; 34 | 35 | PMA_buf = USB_PMA; 36 | PMA_btable = USB_PMA; 37 | 38 | SECTIONS 39 | { 40 | .text : 41 | { 42 | *(.text*) 43 | . = ALIGN(4); 44 | __text_end = .; 45 | _etext = .; 46 | } > flash 47 | 48 | .bss : 49 | { 50 | . = ALIGN(4); 51 | __bss_start = .; 52 | *(.bss*) 53 | *(COMMON) 54 | . = ALIGN(4); 55 | __bss_end = .; 56 | } > sram 57 | 58 | .data : 59 | { 60 | . = ALIGN(4); 61 | __data_start = .; 62 | *(.data*) 63 | . = ALIGN(4); 64 | __data_end = .; 65 | } > sram AT> flash 66 | 67 | .rodata : 68 | { 69 | . = ALIGN(4); 70 | *(.rodata*) 71 | . = ALIGN(4); 72 | } > flash 73 | 74 | } 75 | -------------------------------------------------------------------------------- /usb/kyulib.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 Tom Trebisky 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License version 2 as 6 | * published by the Free Software Foundation. See README and COPYING for 7 | * more details. 8 | */ 9 | 10 | /* kyulib.h 11 | * Tom Trebisky 12/1/2001 12 | * Tom Trebisky 9/23/2015 13 | * Tom Trebisky 12/1/2023 14 | */ 15 | 16 | /* We provide these (at least thus far */ 17 | int printf(const char *, ...); 18 | // int sprintf(char *, const char *, ...); 19 | 20 | void * memcpy ( void *, char *, int ); 21 | 22 | /* XXX - the following fixed size should really 23 | * be an argument to cq_init() and be dynamically 24 | * allocated. 25 | */ 26 | 27 | // #define MAX_CQ_SIZE 2048 28 | #define DEFAULT_CQ_SIZE 512 29 | 30 | struct cqueue { 31 | // char buf[MAX_CQ_SIZE]; 32 | char *buf; 33 | char *bp; 34 | char *ip; 35 | char *op; 36 | char *limit; 37 | int size; 38 | int count; 39 | int toss; 40 | }; 41 | 42 | // struct cqueue * cq_init ( int ); 43 | // struct cqueue * cq_init ( struct cqueue * ); 44 | struct cqueue * cq_init ( struct cqueue *, char *, int ); 45 | void cq_add ( struct cqueue *, int ); 46 | int cq_remove ( struct cqueue * ); 47 | int cq_count ( struct cqueue * ); 48 | 49 | /* THE END */ 50 | -------------------------------------------------------------------------------- /usb/locore.s: -------------------------------------------------------------------------------- 1 | /* locore.s 2 | * Assembler startup file for the STM32F103 3 | * Tom Trebisky 9-24-2016 4 | */ 5 | 6 | # The Cortex M3 is a thumb only processor 7 | .cpu cortex-m3 8 | .thumb 9 | 10 | @ First the "standard" 16 entries for a cortex-m3 11 | .word 0x20005000 /* stack top address */ 12 | .word _reset /* 1 Reset */ 13 | .word bogus /* 2 NMI */ 14 | .word bogus /* 3 Hard Fault */ 15 | .word bogus /* 4 MM Fault */ 16 | .word bogus /* 5 Bus Fault */ 17 | .word bogus /* 6 Usage Fault */ 18 | .word bogus /* 7 RESERVED */ 19 | .word bogus /* 8 RESERVED */ 20 | .word bogus /* 9 RESERVED*/ 21 | .word bogus /* 10 RESERVED */ 22 | .word bogus /* 11 SV call */ 23 | .word bogus /* 12 Debug reserved */ 24 | .word bogus /* 13 RESERVED */ 25 | .word bogus /* 14 PendSV */ 26 | .word systick_handler /* 15 SysTick */ 27 | 28 | @ and now 68 IRQ vectors 29 | .word bogus /* IRQ 0 */ 30 | .word bogus /* IRQ 1 */ 31 | .word bogus /* IRQ 2 */ 32 | .word bogus /* IRQ 3 -- RTC */ 33 | .word bogus /* IRQ 4 */ 34 | .word bogus /* IRQ 5 */ 35 | .word bogus /* IRQ 6 */ 36 | .word bogus /* IRQ 7 */ 37 | .word bogus /* IRQ 8 */ 38 | .word bogus /* IRQ 9 */ 39 | .word bogus /* IRQ 10 */ 40 | .word bogus /* IRQ 11 */ 41 | .word bogus /* IRQ 12 */ 42 | .word bogus /* IRQ 13 */ 43 | .word bogus /* IRQ 14 */ 44 | .word bogus /* IRQ 15 */ 45 | .word bogus /* IRQ 16 */ 46 | .word bogus /* IRQ 17 */ 47 | .word bogus /* IRQ 18 */ 48 | .word usb_hp_handler /* IRQ 19 */ 49 | .word usb_lp_handler /* IRQ 20 */ 50 | .word bogus /* IRQ 21 */ 51 | .word bogus /* IRQ 22 */ 52 | .word bogus /* IRQ 23 */ 53 | .word bogus /* IRQ 24 -- Timer 1 break */ 54 | .word bogus /* IRQ 25 -- Timer 1 update */ 55 | .word bogus /* IRQ 26 -- Timer 1 trig */ 56 | .word bogus /* IRQ 27 -- Timer 1 cc */ 57 | .word tim2_handler /* IRQ 28 -- Timer 2 */ 58 | .word bogus /* IRQ 29 -- Timer 3 */ 59 | .word bogus /* IRQ 30 -- Timer 4 */ 60 | .word bogus /* IRQ 31 */ 61 | .word bogus /* IRQ 32 */ 62 | .word bogus /* IRQ 33 */ 63 | .word bogus /* IRQ 34 */ 64 | .word bogus /* IRQ 35 */ 65 | .word bogus /* IRQ 36 */ 66 | .word uart1_handler /* IRQ 37 -- UART 1 */ 67 | .word bogus /* IRQ 38 -- UART 2 */ 68 | .word bogus /* IRQ 39 -- UART 3 */ 69 | .word bogus /* IRQ 40 */ 70 | .word bogus /* IRQ 41 */ 71 | .word usb_wk_handler /* IRQ 42 */ 72 | .word bogus /* IRQ 43 */ 73 | .word bogus /* IRQ 44 */ 74 | .word bogus /* IRQ 45 */ 75 | .word bogus /* IRQ 46 */ 76 | .word bogus /* IRQ 47 */ 77 | .word bogus /* IRQ 48 */ 78 | .word bogus /* IRQ 49 */ 79 | .word bogus /* IRQ 50 */ 80 | .word bogus /* IRQ 51 */ 81 | .word bogus /* IRQ 52 */ 82 | .word bogus /* IRQ 53 */ 83 | .word bogus /* IRQ 54 */ 84 | .word bogus /* IRQ 55 */ 85 | .word bogus /* IRQ 56 */ 86 | .word bogus /* IRQ 57 */ 87 | .word bogus /* IRQ 58 */ 88 | .word bogus /* IRQ 59 */ 89 | 90 | #ifdef notdef 91 | /* These last 8 aren't assigned on the STM32F103, so we can save a few 92 | * bytes of flash. 93 | */ 94 | .word bogus /* IRQ 60 */ 95 | .word bogus /* IRQ 61 */ 96 | .word bogus /* IRQ 62 */ 97 | .word bogus /* IRQ 63 */ 98 | .word bogus /* IRQ 64 */ 99 | .word bogus /* IRQ 65 */ 100 | .word bogus /* IRQ 66 */ 101 | .word bogus /* IRQ 67 */ 102 | #endif 103 | 104 | .thumb_func 105 | bogus: b . 106 | 107 | .thumb_func 108 | _reset: 109 | bl startup 110 | b . 111 | 112 | /* THE END */ 113 | -------------------------------------------------------------------------------- /usb/mkvec: -------------------------------------------------------------------------------- 1 | #!/bin/ruby 2 | 3 | (0..67).each { |v| 4 | print ".word\tbogus\t\t/* IRQ %2d */\n" % v 5 | } 6 | -------------------------------------------------------------------------------- /usb/ocd: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # These are proper file locations for Fedora 24 3 | # Fedora 38 changed the stlink name 4 | 5 | #openocd -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/share/openocd/scripts/target/stm32f1x.cfg 6 | openocd -f /usr/share/openocd/scripts/interface/stlink.cfg -f /usr/share/openocd/scripts/target/stm32f1x.cfg 7 | -------------------------------------------------------------------------------- /usb/protos.h: -------------------------------------------------------------------------------- 1 | /* protos.h 2 | * 3 | * (c) Tom Trebisky 11-22-2023 4 | * 5 | * prototypes and such 6 | * 7 | */ 8 | 9 | typedef unsigned char u8; 10 | typedef unsigned short u16; 11 | typedef unsigned int u32; 12 | 13 | typedef volatile unsigned int vu32; 14 | 15 | #define BIT(x) (1<usb1.dump 29 | 30 | usb1.bin: usb1.elf 31 | $(OBJCOPY) usb1.elf usb1.bin -O binary 32 | 33 | usb1.elf: $(OBJS) 34 | $(LD) -T usb1.lds -o usb1.elf $(OBJS) 35 | 36 | locore.o: locore.s 37 | $(AS) locore.s -o locore.o 38 | 39 | .c.o: 40 | $(CC) -o $@ -c $< 41 | 42 | OCDCFG = -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/share/openocd/scripts/target/stm32f1x.cfg 43 | 44 | # This works great once I fixed the base address in the LDS file 45 | flash: usb1.elf 46 | openocd $(OCDCFG) -c "program usb1.elf verify reset exit" 47 | 48 | # No need for this now that the above works 49 | bflash: usb1.bin 50 | openocd $(OCDCFG) -c "program usb1.bin verify reset exit 0x08000000" 51 | 52 | gdb: 53 | $(GDB) --eval-command="target remote localhost:3333" usb1.elf 54 | 55 | gdbtui: 56 | $(GDB) -tui --eval-command="target remote localhost:3333" usb1.elf 57 | 58 | clean: 59 | rm -f *.o usb1.elf usb1.bin usb1.dump 60 | -------------------------------------------------------------------------------- /usb1/README.md: -------------------------------------------------------------------------------- 1 | usb1 - first fiddling with USB hardware on the STM32F103C8T6 2 | 3 | This was some preliminary work done in 2017 that never got finished. 4 | 5 | Just ignore this. I should probably delete this. 6 | -------------------------------------------------------------------------------- /usb1/locore.s: -------------------------------------------------------------------------------- 1 | /* locore.s 2 | * Assembler startup file for the STM32F103 3 | * Tom Trebisky 9-24-2016 4 | */ 5 | 6 | # The Cortex M3 is a thumb only processor 7 | .cpu cortex-m3 8 | .thumb 9 | 10 | @ First the "standard" 16 entries for a cortex-m3 11 | .word 0x20005000 /* stack top address */ 12 | .word _reset /* 1 Reset */ 13 | .word bogus /* 2 NMI */ 14 | .word bogus /* 3 Hard Fault */ 15 | .word bogus /* 4 MM Fault */ 16 | .word bogus /* 5 Bus Fault */ 17 | .word bogus /* 6 Usage Fault */ 18 | .word bogus /* 7 RESERVED */ 19 | .word bogus /* 8 RESERVED */ 20 | .word bogus /* 9 RESERVED*/ 21 | .word bogus /* 10 RESERVED */ 22 | .word bogus /* 11 SV call */ 23 | .word bogus /* 12 Debug reserved */ 24 | .word bogus /* 13 RESERVED */ 25 | .word bogus /* 14 PendSV */ 26 | .word systick_handler /* 15 SysTick */ 27 | 28 | @ and now 68 IRQ vectors 29 | .word bogus /* IRQ 0 */ 30 | .word bogus /* IRQ 1 */ 31 | .word bogus /* IRQ 2 */ 32 | .word bogus /* IRQ 3 -- RTC */ 33 | .word bogus /* IRQ 4 */ 34 | .word bogus /* IRQ 5 */ 35 | .word bogus /* IRQ 6 */ 36 | .word bogus /* IRQ 7 */ 37 | .word bogus /* IRQ 8 */ 38 | .word bogus /* IRQ 9 */ 39 | .word bogus /* IRQ 10 */ 40 | .word bogus /* IRQ 11 */ 41 | .word bogus /* IRQ 12 */ 42 | .word bogus /* IRQ 13 */ 43 | .word bogus /* IRQ 14 */ 44 | .word bogus /* IRQ 15 */ 45 | .word bogus /* IRQ 16 */ 46 | .word bogus /* IRQ 17 */ 47 | .word bogus /* IRQ 18 */ 48 | .word usb_hp_handler /* IRQ 19 */ 49 | .word usb_lp_handler /* IRQ 20 */ 50 | .word bogus /* IRQ 21 */ 51 | .word bogus /* IRQ 22 */ 52 | .word bogus /* IRQ 23 */ 53 | .word bogus /* IRQ 24 -- Timer 1 break */ 54 | .word bogus /* IRQ 25 -- Timer 1 update */ 55 | .word bogus /* IRQ 26 -- Timer 1 trig */ 56 | .word bogus /* IRQ 27 -- Timer 1 cc */ 57 | .word tim2_handler /* IRQ 28 -- Timer 2 */ 58 | .word bogus /* IRQ 29 -- Timer 3 */ 59 | .word bogus /* IRQ 30 -- Timer 4 */ 60 | .word bogus /* IRQ 31 */ 61 | .word bogus /* IRQ 32 */ 62 | .word bogus /* IRQ 33 */ 63 | .word bogus /* IRQ 34 */ 64 | .word bogus /* IRQ 35 */ 65 | .word bogus /* IRQ 36 */ 66 | .word bogus /* IRQ 37 -- UART 1 */ 67 | .word bogus /* IRQ 38 -- UART 2 */ 68 | .word bogus /* IRQ 39 -- UART 3 */ 69 | .word bogus /* IRQ 40 */ 70 | .word bogus /* IRQ 41 */ 71 | .word usb_wk_handler /* IRQ 42 */ 72 | .word bogus /* IRQ 43 */ 73 | .word bogus /* IRQ 44 */ 74 | .word bogus /* IRQ 45 */ 75 | .word bogus /* IRQ 46 */ 76 | .word bogus /* IRQ 47 */ 77 | .word bogus /* IRQ 48 */ 78 | .word bogus /* IRQ 49 */ 79 | .word bogus /* IRQ 50 */ 80 | .word bogus /* IRQ 51 */ 81 | .word bogus /* IRQ 52 */ 82 | .word bogus /* IRQ 53 */ 83 | .word bogus /* IRQ 54 */ 84 | .word bogus /* IRQ 55 */ 85 | .word bogus /* IRQ 56 */ 86 | .word bogus /* IRQ 57 */ 87 | .word bogus /* IRQ 58 */ 88 | .word bogus /* IRQ 59 */ 89 | 90 | #ifdef notdef 91 | /* These last 8 aren't assigned on the STM32F103, so we can save a few 92 | * bytes of flash. 93 | */ 94 | .word bogus /* IRQ 60 */ 95 | .word bogus /* IRQ 61 */ 96 | .word bogus /* IRQ 62 */ 97 | .word bogus /* IRQ 63 */ 98 | .word bogus /* IRQ 64 */ 99 | .word bogus /* IRQ 65 */ 100 | .word bogus /* IRQ 66 */ 101 | .word bogus /* IRQ 67 */ 102 | #endif 103 | 104 | .thumb_func 105 | bogus: b . 106 | 107 | .thumb_func 108 | _reset: 109 | bl startup 110 | b . 111 | 112 | /* THE END */ 113 | -------------------------------------------------------------------------------- /usb1/mkvec: -------------------------------------------------------------------------------- 1 | #!/bin/ruby 2 | 3 | (0..67).each { |v| 4 | print ".word\tbogus\t\t/* IRQ %2d */\n" % v 5 | } 6 | -------------------------------------------------------------------------------- /usb1/ocd: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # These are proper file locations for Fedora 24 3 | 4 | openocd -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/share/openocd/scripts/target/stm32f1x.cfg 5 | -------------------------------------------------------------------------------- /usb1/serial.c: -------------------------------------------------------------------------------- 1 | /* serial.c 2 | * (c) Tom Trebisky 7-2-2017 3 | * 4 | * Driver for the STM32F103 usart 5 | */ 6 | 7 | /* One of the 3 uarts */ 8 | struct uart { 9 | volatile unsigned long status; /* 00 */ 10 | volatile unsigned long data; /* 04 */ 11 | volatile unsigned long baud; /* 08 */ 12 | volatile unsigned long cr1; /* 0c */ 13 | volatile unsigned long cr2; /* 10 */ 14 | volatile unsigned long cr3; /* 14 */ 15 | volatile unsigned long gtp; /* 18 - guard time and prescaler */ 16 | }; 17 | 18 | #define UART1_BASE (struct uart *) 0x40013800 19 | #define UART2_BASE (struct uart *) 0x40004400 20 | #define UART3_BASE (struct uart *) 0x40004800 21 | 22 | /* bits in the status register */ 23 | #define ST_PE 0x0001 24 | #define ST_FE 0x0002 25 | #define ST_NE 0x0004 26 | #define ST_OVER 0x0008 27 | #define ST_IDLE 0x0010 28 | #define ST_RXNE 0x0020 /* Receiver not empty */ 29 | #define ST_TC 0x0040 /* Transmission complete */ 30 | #define ST_TXE 0x0080 /* Transmitter empty */ 31 | #define ST_BREAK 0x0100 32 | #define ST_CTS 0x0200 33 | 34 | static void 35 | uart_init ( struct uart *up, int baud ) 36 | { 37 | /* 1 start bit, even parity */ 38 | up->cr1 = 0x340c; 39 | up->cr2 = 0; 40 | up->cr3 = 0; 41 | up->gtp = 0; 42 | 43 | if ( up == UART1_BASE ) 44 | up->baud = get_pclk2() / baud; 45 | else 46 | up->baud = get_pclk1() / baud; 47 | } 48 | 49 | void 50 | serial_init ( void ) 51 | { 52 | gpio_uart1 (); 53 | // uart_init ( UART1_BASE, 9600 ); 54 | // uart_init ( UART1_BASE, 38400 ); 55 | // uart_init ( UART1_BASE, 57600 ); ?? 56 | uart_init ( UART1_BASE, 115200 ); 57 | 58 | #ifdef notdef 59 | gpio_uart2 (); 60 | uart_init ( UART2_BASE, 9600 ); 61 | 62 | gpio_uart3 (); 63 | uart_init ( UART3_BASE, 9600 ); 64 | #endif 65 | } 66 | 67 | void 68 | serial_putc ( int c ) 69 | { 70 | struct uart *up = UART1_BASE; 71 | 72 | if ( c == '\n' ) 73 | serial_putc ( '\r' ); 74 | 75 | while ( ! (up->status & ST_TXE) ) 76 | ; 77 | up->data = c; 78 | } 79 | 80 | void 81 | serial_puts ( char *s ) 82 | { 83 | while ( *s ) 84 | serial_putc ( *s++ ); 85 | } 86 | 87 | /* -------------------------------------------- */ 88 | /* Some IO stuff from Kyu */ 89 | /* -------------------------------------------- */ 90 | 91 | #define HEX(x) ((x)<10 ? '0'+(x) : 'A'+(x)-10) 92 | 93 | #define PUTCHAR(x) *buf++ = (x) 94 | 95 | static char * 96 | shex2( char *buf, int val ) 97 | { 98 | PUTCHAR( HEX((val>>4)&0xf) ); 99 | PUTCHAR( HEX(val&0xf) ); 100 | return buf; 101 | } 102 | 103 | static char * 104 | shex4( char *buf, int val ) 105 | { 106 | buf = shex2(buf,val>>8); 107 | return shex2(buf,val); 108 | } 109 | 110 | static char * 111 | shex8( char *buf, int val ) 112 | { 113 | buf = shex2(buf,val>>24); 114 | buf = shex2(buf,val>>16); 115 | buf = shex2(buf,val>>8); 116 | return shex2(buf,val); 117 | } 118 | 119 | void 120 | show16 ( char *s, int val ) 121 | { 122 | char buf[5]; 123 | 124 | serial_puts ( s ); 125 | shex4 ( buf, val ); 126 | buf[4] = '\0'; 127 | serial_puts ( buf ); 128 | serial_putc ( '\n' ); 129 | } 130 | 131 | /* THE END */ 132 | -------------------------------------------------------------------------------- /usb1/usb1.lds: -------------------------------------------------------------------------------- 1 | /* blink.lds 2 | * linker script for blink demo. 3 | * 4 | * Memory layout on my STM32F103 is pretty much like this: 5 | * 6 | * 0x00000000 - 0x07ffffff - aliased to flash or sys memory depending on BOOT jumpers 7 | * 0x08000000 - 0x0800ffff - Flash (64K ... or 128K) 8 | * 0x1ffff000 - 0x1ffff7ff - Boot firmware in system memory 9 | * 0x1ffff800 - 0x1fffffff - option bytes 10 | * 0x20000000 - 0x20004fff - SRAM (20k) 11 | * 0x40000000 - 0x40023400 - peripherals 12 | * 13 | * flash(RX) : ORIGIN = 0x00000000, LENGTH = 128K 14 | */ 15 | MEMORY 16 | { 17 | flash(RX) : ORIGIN = 0x08000000, LENGTH = 128K 18 | sram(WAIL) : ORIGIN = 0x20000000, LENGTH = 20K 19 | } 20 | 21 | SECTIONS 22 | { 23 | .text : { *(.text*) } > flash 24 | .bss : { *(.bss*) } > sram 25 | } 26 | -------------------------------------------------------------------------------- /usb_baboon/.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.elf 3 | *.dump 4 | *.swp 5 | *.map 6 | zzz 7 | zz 8 | z 9 | OLD 10 | OLD/* 11 | tags 12 | maple_all.bin 13 | -------------------------------------------------------------------------------- /usb_baboon/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for interrupt demo 2 | 3 | #TOOLS = arm-linux-gnu 4 | TOOLS = arm-none-eabi 5 | 6 | # Assembling with gcc makes it want crt0 at link time. 7 | #AS = $(TOOLS)-gcc 8 | AS = $(TOOLS)-as 9 | 10 | #COPTS = -mcpu=cortex-m3 -mthumb -Wno-implicit-function-declaration 11 | COPTS = -mcpu=cortex-m3 -mthumb -Wno-implicit-function-declaration -fno-builtin-printf -fno-builtin 12 | # Use the -g flag if you intend to use gdb 13 | #CC = $(TOOLS)-gcc $(COPTS) -g 14 | #CC = $(TOOLS)-gcc $(COPTS) 15 | CC = $(TOOLS)-gcc $(COPTS) -O2 16 | 17 | ##-Wl,-gc-sections,-Map,main.map -Wl,--cref 18 | 19 | #LDOPTS = --no-warn-rwx-segments 20 | LDOPTS = -no-warn-rwx-segments -Map baboon.map --cref 21 | 22 | #LD = $(TOOLS)-gcc 23 | LD = $(TOOLS)-ld.bfd $(LDOPTS) 24 | OBJCOPY = $(TOOLS)-objcopy 25 | DUMP = $(TOOLS)-objdump -d 26 | GDB = $(TOOLS)-gdb 27 | 28 | #OBJS = locore.o main.o nvic.o rcc.o gpio.o prf.o serial.o timer.o usb.o 29 | #OBJS = locore.o main.o startup.o nvic.o rcc.o gpio.o prf.o serial.o timer.o usb.o papoon.o 30 | 31 | #OBJS = locore.o main.o startup.o nvic.o rcc.o gpio.o prf.o kyulib.o serial.o timer.o usb.o usb_enum.o usb_watch.o papoon.o 32 | OBJS = locore.o main.o startup.o nvic.o rcc.o gpio.o prf.o kyulib.o serial.o timer.o usb.o usb_enum.o usb_watch.o 33 | 34 | all: baboon.elf baboon.dump tags 35 | 36 | papoon.o: bogus 37 | cd papoon; make 38 | 39 | bogus: 40 | 41 | baboon.dump: baboon.elf 42 | $(DUMP) baboon.elf >baboon.dump 43 | 44 | baboon.bin: baboon.elf 45 | $(OBJCOPY) baboon.elf baboon.bin -O binary 46 | 47 | baboon.elf: $(OBJS) 48 | $(LD) -T baboon.lds -o baboon.elf $(OBJS) 49 | size baboon.elf 50 | 51 | locore.o: locore.s 52 | $(AS) locore.s -o locore.o 53 | 54 | .c.o: 55 | $(CC) -o $@ -c $< 56 | 57 | OCDCFG = -f /usr/share/openocd/scripts/interface/stlink.cfg -f /usr/share/openocd/scripts/target/stm32f1x.cfg 58 | 59 | # Does not work yet for the Maple 60 | # reset takes the CPU to 08000000 and either nothing is there 61 | # or it runs an old blink demo it finds there. 62 | flash: baboon.elf 63 | openocd $(OCDCFG) -c "program baboon.elf verify reset exit" 64 | 65 | # No need for this now that the above works 66 | ##bflash: baboon.bin 67 | ## openocd $(OCDCFG) -c "program baboon.bin verify reset exit 0x08000000" 68 | 69 | xyz: 70 | openocd $(OCDCFG) 71 | 72 | ocd: 73 | openocd $(OCDCFG) 74 | 75 | gdb: 76 | $(GDB) --eval-command="target remote localhost:3333" baboon.elf 77 | 78 | gdbtui: 79 | $(GDB) -tui --eval-command="target remote localhost:3333" baboon.elf 80 | 81 | tags: 82 | ctags -R . 83 | 84 | clean: 85 | cd papoon; make clean 86 | rm -f *.o baboon.elf baboon.bin baboon.dump 87 | rm -f tags 88 | -------------------------------------------------------------------------------- /usb_baboon/README.md: -------------------------------------------------------------------------------- 1 | usb_baboon 11-18-2023 2 | 3 | This is a "stepping stone" project that 4 | you should ignore. Move on to "usb" 5 | 6 | This began as a copy of usb_papoon2. 7 | My goals here are -- 8 | 9 | * To run the code on a Maple board 10 | * To run independent of the papoon code. 11 | 12 | The papoon code is C++ sequestered in the papoon directory. 13 | -------------------------------------------------------------------------------- /usb_baboon/baboon.lds: -------------------------------------------------------------------------------- 1 | /* baboon.lds 2 | * linker script for baboon demo. 3 | * 4 | * Memory layout on my STM32F103 is pretty much like this: 5 | * 6 | * 0x00000000 - 0x07ffffff - aliased to flash or sys memory depending on BOOT jumpers 7 | * 0x08000000 - 0x0800ffff - Flash (64K ... or 128K) 8 | * 0x1ffff000 - 0x1ffff7ff - Boot firmware in system memory 9 | * 0x1ffff800 - 0x1fffffff - option bytes 10 | * 0x20000000 - 0x20004fff - SRAM (20k) 11 | * 0x40000000 - 0x40023400 - peripherals 12 | * 13 | * flash(RX) : ORIGIN = 0x00000000, LENGTH = 128K 14 | * flash(RX) : ORIGIN = 0x08005000, LENGTH = 128K 15 | */ 16 | 17 | /* The important thing for a Maple board is that the flash 18 | * is linked to offset 5000 to avoid overwriting the USB 19 | * loader. 20 | */ 21 | MEMORY 22 | { 23 | flash(RX) : ORIGIN = 0x08000000, LENGTH = 128K 24 | sram(RW) : ORIGIN = 0x20000000, LENGTH = 20K 25 | } 26 | 27 | /* An experiment 12-3-2023 28 | */ 29 | 30 | PERIPH_BASE = 0x40000000; 31 | APB1PERIPH = PERIPH_BASE; 32 | USB = APB1PERIPH + 0x5C00; 33 | USB_PMA = APB1PERIPH + 0x6000; 34 | 35 | PMA_buf = USB_PMA; 36 | PMA_btable = USB_PMA; 37 | 38 | SECTIONS 39 | { 40 | .text : 41 | { 42 | *(.text*) 43 | . = ALIGN(4); 44 | __text_end = .; 45 | _etext = .; 46 | } > flash 47 | 48 | .bss : 49 | { 50 | . = ALIGN(4); 51 | __bss_start = .; 52 | *(.bss*) 53 | *(COMMON) 54 | . = ALIGN(4); 55 | __bss_end = .; 56 | } > sram 57 | 58 | .data : 59 | { 60 | . = ALIGN(4); 61 | __data_start = .; 62 | *(.data*) 63 | . = ALIGN(4); 64 | __data_end = .; 65 | } > sram AT> flash 66 | 67 | .rodata : 68 | { 69 | . = ALIGN(4); 70 | *(.rodata*) 71 | . = ALIGN(4); 72 | } > flash 73 | 74 | } 75 | -------------------------------------------------------------------------------- /usb_baboon/kyulib.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 Tom Trebisky 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License version 2 as 6 | * published by the Free Software Foundation. See README and COPYING for 7 | * more details. 8 | */ 9 | 10 | /* kyulib.h 11 | * Tom Trebisky 12/1/2001 12 | * Tom Trebisky 9/23/2015 13 | * Tom Trebisky 12/1/2023 14 | */ 15 | 16 | /* We provide these (at least thus far */ 17 | int printf(const char *, ...); 18 | // int sprintf(char *, const char *, ...); 19 | 20 | void * memcpy ( void *, char *, int ); 21 | 22 | /* XXX - the following fixed size should really 23 | * be an argument to cq_init() and be dynamically 24 | * allocated. 25 | */ 26 | 27 | #define MAX_CQ_SIZE 2048 28 | 29 | struct cqueue { 30 | char buf[MAX_CQ_SIZE]; 31 | char *bp; 32 | char *ip; 33 | char *op; 34 | char *limit; 35 | int size; 36 | int count; 37 | int toss; 38 | }; 39 | 40 | // struct cqueue * cq_init ( int ); 41 | struct cqueue * cq_init ( struct cqueue * ); 42 | void cq_add ( struct cqueue *, int ); 43 | int cq_remove ( struct cqueue * ); 44 | int cq_count ( struct cqueue * ); 45 | 46 | /* THE END */ 47 | -------------------------------------------------------------------------------- /usb_baboon/mkvec: -------------------------------------------------------------------------------- 1 | #!/bin/ruby 2 | 3 | (0..67).each { |v| 4 | print ".word\tbogus\t\t/* IRQ %2d */\n" % v 5 | } 6 | -------------------------------------------------------------------------------- /usb_baboon/ocd: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # These are proper file locations for Fedora 24 3 | # Fedora 38 changed the stlink name 4 | 5 | #openocd -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/share/openocd/scripts/target/stm32f1x.cfg 6 | openocd -f /usr/share/openocd/scripts/interface/stlink.cfg -f /usr/share/openocd/scripts/target/stm32f1x.cfg 7 | -------------------------------------------------------------------------------- /usb_baboon/papoon/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for papoon submodule 2 | 3 | #TOOLS = arm-linux-gnu 4 | TOOLS = arm-none-eabi 5 | 6 | #LD = $(TOOLS)-gcc 7 | LD = $(TOOLS)-ld.bfd --no-warn-rwx-segments 8 | 9 | #OBJS = example.o usb_dev.o usb_dev_cdc_acm.o usb_mcu_init.o 10 | OBJS = example.o usb_dev.o usb_dev_cdc_acm.o 11 | 12 | all: ../papoon.o 13 | 14 | ../papoon.o: $(OBJS) 15 | $(LD) -r -o ../papoon.o $(OBJS) 16 | 17 | # =========================================== 18 | 19 | CXX = $(TOOLS)-g++ 20 | 21 | # Without the -fno-exceptions, we get calls to: 22 | # __aeabi_unwind_cpp_pr0 23 | # __aeabi_unwind_cpp_pr1 24 | 25 | #CXX_FLAGS = -g -O1 -mcpu=cortex-m3 -mthumb -fno-builtin -fno-exceptions 26 | CXX_FLAGS = -O1 -mcpu=cortex-m3 -mthumb -fno-builtin -fno-exceptions 27 | INCLUDES = -I. 28 | 29 | # All this is now in papoon.hxx 30 | #SEND_B4_RECV ?= 64 31 | #SYNC_LEN ?= 4 32 | #REPORT_EVERY ?= 10000 33 | #ASYNC ?= -D 34 | #DEBUG ?= -U 35 | # 36 | #CONFIGURATION = -DUP_MAX_PACKET_SIZE=64 \ 37 | # -DDOWN_MAX_PACKET_SIZE=64 \ 38 | # -DSEND_B4_RECV_MAX=$(SEND_B4_RECV) \ 39 | # -DUSB_RANDOMTEST_UP_SEED=0x5f443bba \ 40 | # -DUSB_RANDOMTEST_DOWN_SEED=0x684053d8 \ 41 | # -DUSB_RANDOMTEST_LENGTH_SEED=0x769bc5e6 \ 42 | # -DSLAVE_LENGTH_SEED=0x4b6420e0 \ 43 | # -DUSB_RANDOMTEST_SYNC_LENGTH=$(SYNC_LEN)\ 44 | # -DHISTOGRAM_LENGTH=8 \ 45 | # -DREPORT_EVERY=$(REPORT_EVERY) \ 46 | # $(ASYNC)RANDOMTEST_LIBUSB_ASYNC \ 47 | # $(DEBUG)DEBUG 48 | 49 | #CXX_OPTIONS = $(CXX_FLAGS) $(INCLUDES) $(CONFIGURATION) 50 | CXX_OPTIONS = $(CXX_FLAGS) $(INCLUDES) 51 | 52 | %.o: %.cxx 53 | $(CXX) -c $(CXX_OPTIONS) $< -o $@ 54 | 55 | # Copy files from the papoon_usb project 56 | # We did this to first copy the files, but now we have 57 | # made changes to a few of them and if we did "make get" 58 | # again, we would overwrite those changes, so we 59 | # comment this out to avoid such a catastrophe. 60 | 61 | PAPOON_PATH = /u1/Projects/STM32/papoon_usb 62 | #get: 63 | # cp $(PAPOON_PATH)/examples/blue_pill/example.cxx . 64 | # cp $(PAPOON_PATH)/arm/core_cm3.hxx . 65 | # cp $(PAPOON_PATH)/regbits/regbits.hxx . 66 | # cp $(PAPOON_PATH)/regbits/stm32f103xb.hxx . 67 | # cp $(PAPOON_PATH)/regbits/stm32f103xb_tim.hxx . 68 | # cp $(PAPOON_PATH)/usb/usb_dev_cdc_acm.hxx . 69 | # cp $(PAPOON_PATH)/usb/usb_dev.hxx . 70 | # cp $(PAPOON_PATH)/examples/blue_pill/usb_mcu_init.hxx . 71 | # cp $(PAPOON_PATH)/usb/usb_dev.cxx . 72 | # cp $(PAPOON_PATH)/util/bin_to_hex.hxx . 73 | # cp $(PAPOON_PATH)/usb/usb_dev_cdc_acm.cxx . 74 | # cp $(PAPOON_PATH)/examples/blue_pill/usb_mcu_init.cxx . 75 | 76 | clean: 77 | rm -f *.o 78 | 79 | # THE END 80 | -------------------------------------------------------------------------------- /usb_baboon/papoon/papoon.h: -------------------------------------------------------------------------------- 1 | /* papoon.hxx 2 | * Introduced by Tom Trebisky 11-4-2023 3 | * 4 | * The original Papoon setup did all this CONFIGURATION stuff 5 | # in the Makefile via the following long list of compiler options. 6 | * 7 | * I decided that I would rather clean up the build messages 8 | * and moved it all into this file. 9 | */ 10 | 11 | extern "C" void printf ( const char *, ... ); 12 | // extern "C" void delay ( void ); 13 | extern "C" void delay_ms ( int ); 14 | extern "C" void pma_show ( void ); 15 | extern "C" void usb_show ( void ); 16 | extern "C" void tjt_enum_logger ( int what ); 17 | 18 | #define USB_DEV_INTERRUPT_DRIVEN 19 | 20 | // SEND_B4_RECV ?= 64 21 | // SYNC_LEN ?= 4 22 | // REPORT_EVERY ?= 10000 23 | // ASYNC ?= -D 24 | // DEBUG ?= -U 25 | 26 | // CONFIGURATION = -DUP_MAX_PACKET_SIZE=64 \ 27 | // -DDOWN_MAX_PACKET_SIZE=64 \ 28 | // -DSEND_B4_RECV_MAX=$(SEND_B4_RECV) \ 29 | // -DUSB_RANDOMTEST_UP_SEED=0x5f443bba \ 30 | // -DUSB_RANDOMTEST_DOWN_SEED=0x684053d8 \ 31 | // -DUSB_RANDOMTEST_LENGTH_SEED=0x769bc5e6 \ 32 | // -DSLAVE_LENGTH_SEED=0x4b6420e0 \ 33 | // -DUSB_RANDOMTEST_SYNC_LENGTH=$(SYNC_LEN)\ 34 | // -DHISTOGRAM_LENGTH=8 \ 35 | // -DREPORT_EVERY=$(REPORT_EVERY) \ 36 | // $(ASYNC)RANDOMTEST_LIBUSB_ASYNC \ 37 | // $(DEBUG)DEBUG 38 | 39 | #define UP_MAX_PACKET_SIZE 64 40 | #define DOWN_MAX_PACKET_SIZE 64 41 | #define SEND_B4_RECV_MAX 64 42 | 43 | #define USB_RANDOMTEST_UP_SEED 0x5f443bba 44 | #define USB_RANDOMTEST_DOWN_SEED 0x684053d8 45 | #define USB_RANDOMTEST_LENGTH_SEED 0x769bc5e6 46 | 47 | #define SLAVE_LENGTH_SEED 0x4b6420e0 48 | #define USB_RANDOMTEST_SYNC_LENGTH 4 49 | 50 | #define HISTOGRAM_LENGTH 8 51 | #define REPORT_EVERY 1000 52 | 53 | #define RANDOMTEST_LIBUSB_ASYNC 54 | // #define DEBUG 55 | 56 | // THE END 57 | -------------------------------------------------------------------------------- /usb_baboon/papoon/usb_dev_cdc_acm.h: -------------------------------------------------------------------------------- 1 | // papoon_usb: "Not Insane" USB library for STM32F103xx MCUs 2 | // Copyright (C) 2019,2020 Mark R. Rubin 3 | // 4 | // This file is part of papoon_usb. 5 | // 6 | // The papoon_usb program is free software: you can redistribute it 7 | // and/or modify it under the terms of the GNU General Public License 8 | // as published by the Free Software Foundation, either version 3 of 9 | // the License, or (at your option) any later version. 10 | // 11 | // The papoon_usb program is distributed in the hope that it will be 12 | // useful, but WITHOUT ANY WARRANTY; without even the implied warranty 13 | // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | // General Public License for more details. 15 | // 16 | // You should have received a copy of the GNU General Public License 17 | // (LICENSE.txt) along with the papoon_usb program. If not, see 18 | // 19 | 20 | 21 | #ifndef USB_DEV_CDC_ACM_HXX 22 | #define USB_DEV_CDC_ACM_HXX 23 | 24 | 25 | #ifndef CDC_OUT_EP_SIZE 26 | #define CDC_OUT_EP_SIZE 64 // must be modulo 4, max 64) 27 | #endif 28 | 29 | 30 | #include 31 | 32 | #if USB_DEV_MAJOR_VERSION == 1 33 | #if USB_DEV_MINOR_VERSION < 0 34 | #warning USB_DEV_MINOR_VERSION < 0 with required USB_DEV_MAJOR_VERSION == 1 35 | #endif 36 | #else 37 | #error USB_DEV_MAJOR_VERSION != 1 38 | #endif 39 | 40 | 41 | namespace stm32f10_12357_xx { 42 | 43 | class UsbDevCdcAcm : public UsbDev 44 | { 45 | public: 46 | static const uint8_t // have to be public for clients, static descriptors 47 | ACM_ENDPOINT = 2, 48 | CDC_ENDPOINT_IN = 1, 49 | CDC_ENDPOINT_OUT = 3, 50 | // have to be public for extern static definition 51 | CDC_IN_DATA_SIZE = 64, 52 | CDC_OUT_DATA_SIZE = CDC_OUT_EP_SIZE, 53 | ACM_DATA_SIZE = 8; 54 | 55 | constexpr UsbDevCdcAcm() 56 | : UsbDev() 57 | {} 58 | 59 | bool init(); 60 | 61 | 62 | // need public accessor for static initialization of _NEW_STRING_DESCS 63 | // 64 | static constexpr const uint8_t* device_string_desc() 65 | { 66 | return _device_string_desc; 67 | } 68 | 69 | 70 | 71 | 72 | protected: 73 | friend class UsbDev; 74 | 75 | struct LineCoding { 76 | uint32_t baud ; 77 | uint8_t stop_bits , 78 | parity_code, 79 | bits ; 80 | }; 81 | 82 | static const uint8_t _NUM_ENDPOINTS = 4 , 83 | _SET_LINE_CODING = 0x20, 84 | _GET_LINE_CODING = 0x21, 85 | _SET_CONTROL_LINE_STATE = 0x22; 86 | 87 | static const uint8_t _device_string_desc[]; 88 | static LineCoding _line_coding ; 89 | 90 | }; // class UsbDevCdcAcm 91 | 92 | } // namespace stm32f10_12357_xx 93 | 94 | #endif // ifndef USB_DEV_CDC_ACM_HXX 95 | -------------------------------------------------------------------------------- /usb_baboon/protos.h: -------------------------------------------------------------------------------- 1 | /* protos.h 2 | * 3 | * (c) Tom Trebisky 11-22-2023 4 | * 5 | * prototypes and such 6 | * 7 | */ 8 | 9 | typedef unsigned char u8; 10 | typedef unsigned short u16; 11 | typedef unsigned int u32; 12 | 13 | typedef volatile unsigned int vu32; 14 | 15 | #define BIT(x) (1<papoon.dump 35 | 36 | papoon.bin: papoon.elf 37 | $(OBJCOPY) papoon.elf papoon.bin -O binary 38 | 39 | papoon.elf: $(OBJS) 40 | $(LD) -T papoon.lds -o papoon.elf $(OBJS) 41 | 42 | locore.o: locore.s 43 | $(AS) locore.s -o locore.o 44 | 45 | .c.o: 46 | $(CC) -o $@ -c $< 47 | 48 | OCDCFG = -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/share/openocd/scripts/target/stm32f1x.cfg 49 | 50 | # This works great once I fixed the base address in the LDS file 51 | flash: papoon.elf 52 | openocd $(OCDCFG) -c "program papoon.elf verify reset exit" 53 | 54 | # No need for this now that the above works 55 | bflash: papoon.bin 56 | openocd $(OCDCFG) -c "program papoon.bin verify reset exit 0x08000000" 57 | 58 | gdb: 59 | $(GDB) --eval-command="target remote localhost:3333" papoon.elf 60 | 61 | gdbtui: 62 | $(GDB) -tui --eval-command="target remote localhost:3333" papoon.elf 63 | 64 | clean: 65 | cd papoon; make clean 66 | rm -f *.o papoon.elf papoon.bin papoon.dump 67 | -------------------------------------------------------------------------------- /usb_papoon/README.md: -------------------------------------------------------------------------------- 1 | usb_papoon 2 | 3 | This works, but it is just a stepping stone to "usb". 4 | 5 | What you really want is "usb" 6 | 7 | I wanted to try to use the C++ code from papoon_usb 8 | 9 | https://github.com/thanks4opensource/papoon_usb 10 | 11 | In particular, the first example. This is the first real work 12 | I have done in 2023. I keep the papoon C++ code in a 13 | directory of its own and set up my makefile to deal with 14 | that and make minimal changes to let me call it from my 15 | C framework. 16 | 17 | This works!! It yields /dev/ttyACM0 that echos whatever 18 | you type at it (I use picocom). 19 | 20 | I'll move on to usb_papoon2 for further work. 21 | -------------------------------------------------------------------------------- /usb_papoon/locore.s: -------------------------------------------------------------------------------- 1 | /* locore.s 2 | * Assembler startup file for the STM32F103 3 | * Tom Trebisky 9-24-2016 4 | */ 5 | 6 | # The Cortex M3 is a thumb only processor 7 | .cpu cortex-m3 8 | .thumb 9 | 10 | @ First the "standard" 16 entries for a cortex-m3 11 | .word 0x20005000 /* stack top address */ 12 | .word _reset /* 1 Reset */ 13 | .word bogus /* 2 NMI */ 14 | .word bogus /* 3 Hard Fault */ 15 | .word bogus /* 4 MM Fault */ 16 | .word bogus /* 5 Bus Fault */ 17 | .word bogus /* 6 Usage Fault */ 18 | .word bogus /* 7 RESERVED */ 19 | .word bogus /* 8 RESERVED */ 20 | .word bogus /* 9 RESERVED*/ 21 | .word bogus /* 10 RESERVED */ 22 | .word bogus /* 11 SV call */ 23 | .word bogus /* 12 Debug reserved */ 24 | .word bogus /* 13 RESERVED */ 25 | .word bogus /* 14 PendSV */ 26 | .word systick_handler /* 15 SysTick */ 27 | 28 | @ and now 68 IRQ vectors 29 | .word bogus /* IRQ 0 */ 30 | .word bogus /* IRQ 1 */ 31 | .word bogus /* IRQ 2 */ 32 | .word bogus /* IRQ 3 -- RTC */ 33 | .word bogus /* IRQ 4 */ 34 | .word bogus /* IRQ 5 */ 35 | .word bogus /* IRQ 6 */ 36 | .word bogus /* IRQ 7 */ 37 | .word bogus /* IRQ 8 */ 38 | .word bogus /* IRQ 9 */ 39 | .word bogus /* IRQ 10 */ 40 | .word bogus /* IRQ 11 */ 41 | .word bogus /* IRQ 12 */ 42 | .word bogus /* IRQ 13 */ 43 | .word bogus /* IRQ 14 */ 44 | .word bogus /* IRQ 15 */ 45 | .word bogus /* IRQ 16 */ 46 | .word bogus /* IRQ 17 */ 47 | .word bogus /* IRQ 18 */ 48 | .word usb_hp_handler /* IRQ 19 */ 49 | .word usb_lp_handler /* IRQ 20 */ 50 | .word bogus /* IRQ 21 */ 51 | .word bogus /* IRQ 22 */ 52 | .word bogus /* IRQ 23 */ 53 | .word bogus /* IRQ 24 -- Timer 1 break */ 54 | .word bogus /* IRQ 25 -- Timer 1 update */ 55 | .word bogus /* IRQ 26 -- Timer 1 trig */ 56 | .word bogus /* IRQ 27 -- Timer 1 cc */ 57 | .word tim2_handler /* IRQ 28 -- Timer 2 */ 58 | .word bogus /* IRQ 29 -- Timer 3 */ 59 | .word bogus /* IRQ 30 -- Timer 4 */ 60 | .word bogus /* IRQ 31 */ 61 | .word bogus /* IRQ 32 */ 62 | .word bogus /* IRQ 33 */ 63 | .word bogus /* IRQ 34 */ 64 | .word bogus /* IRQ 35 */ 65 | .word bogus /* IRQ 36 */ 66 | .word bogus /* IRQ 37 -- UART 1 */ 67 | .word bogus /* IRQ 38 -- UART 2 */ 68 | .word bogus /* IRQ 39 -- UART 3 */ 69 | .word bogus /* IRQ 40 */ 70 | .word bogus /* IRQ 41 */ 71 | .word usb_wk_handler /* IRQ 42 */ 72 | .word bogus /* IRQ 43 */ 73 | .word bogus /* IRQ 44 */ 74 | .word bogus /* IRQ 45 */ 75 | .word bogus /* IRQ 46 */ 76 | .word bogus /* IRQ 47 */ 77 | .word bogus /* IRQ 48 */ 78 | .word bogus /* IRQ 49 */ 79 | .word bogus /* IRQ 50 */ 80 | .word bogus /* IRQ 51 */ 81 | .word bogus /* IRQ 52 */ 82 | .word bogus /* IRQ 53 */ 83 | .word bogus /* IRQ 54 */ 84 | .word bogus /* IRQ 55 */ 85 | .word bogus /* IRQ 56 */ 86 | .word bogus /* IRQ 57 */ 87 | .word bogus /* IRQ 58 */ 88 | .word bogus /* IRQ 59 */ 89 | 90 | #ifdef notdef 91 | /* These last 8 aren't assigned on the STM32F103, so we can save a few 92 | * bytes of flash. 93 | */ 94 | .word bogus /* IRQ 60 */ 95 | .word bogus /* IRQ 61 */ 96 | .word bogus /* IRQ 62 */ 97 | .word bogus /* IRQ 63 */ 98 | .word bogus /* IRQ 64 */ 99 | .word bogus /* IRQ 65 */ 100 | .word bogus /* IRQ 66 */ 101 | .word bogus /* IRQ 67 */ 102 | #endif 103 | 104 | .thumb_func 105 | bogus: b . 106 | 107 | .thumb_func 108 | _reset: 109 | bl startup 110 | b . 111 | 112 | /* THE END */ 113 | -------------------------------------------------------------------------------- /usb_papoon/mkvec: -------------------------------------------------------------------------------- 1 | #!/bin/ruby 2 | 3 | (0..67).each { |v| 4 | print ".word\tbogus\t\t/* IRQ %2d */\n" % v 5 | } 6 | -------------------------------------------------------------------------------- /usb_papoon/ocd: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # These are proper file locations for Fedora 24 3 | 4 | openocd -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/share/openocd/scripts/target/stm32f1x.cfg 5 | -------------------------------------------------------------------------------- /usb_papoon/papoon.lds: -------------------------------------------------------------------------------- 1 | /* blink.lds 2 | * linker script for blink demo. 3 | * 4 | * Memory layout on my STM32F103 is pretty much like this: 5 | * 6 | * 0x00000000 - 0x07ffffff - aliased to flash or sys memory depending on BOOT jumpers 7 | * 0x08000000 - 0x0800ffff - Flash (64K ... or 128K) 8 | * 0x1ffff000 - 0x1ffff7ff - Boot firmware in system memory 9 | * 0x1ffff800 - 0x1fffffff - option bytes 10 | * 0x20000000 - 0x20004fff - SRAM (20k) 11 | * 0x40000000 - 0x40023400 - peripherals 12 | * 13 | * flash(RX) : ORIGIN = 0x00000000, LENGTH = 128K 14 | */ 15 | MEMORY 16 | { 17 | flash(RX) : ORIGIN = 0x08000000, LENGTH = 128K 18 | sram(RW) : ORIGIN = 0x20000000, LENGTH = 20K 19 | } 20 | 21 | SECTIONS 22 | { 23 | .text : 24 | { 25 | *(.text*) 26 | . = ALIGN(4); 27 | __text_end = .; 28 | } > flash 29 | 30 | .bss : 31 | { 32 | . = ALIGN(4); 33 | __bss_start = .; 34 | *(.bss*) 35 | *(COMMON) 36 | . = ALIGN(4); 37 | __bss_end = .; 38 | } > sram 39 | 40 | .data : 41 | { 42 | . = ALIGN(4); 43 | __data_start = .; 44 | *(.data*) 45 | . = ALIGN(4); 46 | __data_end = .; 47 | } > sram AT> flash 48 | 49 | .rodata : 50 | { 51 | . = ALIGN(4); 52 | *(.rodata*) 53 | . = ALIGN(4); 54 | } > flash 55 | 56 | } 57 | -------------------------------------------------------------------------------- /usb_papoon/papoon/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for papoon submodule 2 | 3 | #TOOLS = arm-linux-gnu 4 | TOOLS = arm-none-eabi 5 | 6 | #LD = $(TOOLS)-gcc 7 | LD = $(TOOLS)-ld.bfd --no-warn-rwx-segments 8 | 9 | OBJS = example.o usb_dev.o usb_dev_cdc_acm.o usb_mcu_init.o 10 | 11 | all: ../papoon.o 12 | 13 | ../papoon.o: $(OBJS) 14 | $(LD) -r -o ../papoon.o $(OBJS) 15 | 16 | # =========================================== 17 | 18 | CXX = $(TOOLS)-g++ 19 | 20 | # Without the -fno-exceptions, we get calls to: 21 | # __aeabi_unwind_cpp_pr0 22 | # __aeabi_unwind_cpp_pr1 23 | 24 | #CXX_FLAGS = -g -O1 -mcpu=cortex-m3 -mthumb -fno-builtin -fno-exceptions 25 | CXX_FLAGS = -O1 -mcpu=cortex-m3 -mthumb -fno-builtin -fno-exceptions 26 | INCLUDES = -I. 27 | 28 | # All this is now in papoon.hxx 29 | #SEND_B4_RECV ?= 64 30 | #SYNC_LEN ?= 4 31 | #REPORT_EVERY ?= 10000 32 | #ASYNC ?= -D 33 | #DEBUG ?= -U 34 | # 35 | #CONFIGURATION = -DUP_MAX_PACKET_SIZE=64 \ 36 | # -DDOWN_MAX_PACKET_SIZE=64 \ 37 | # -DSEND_B4_RECV_MAX=$(SEND_B4_RECV) \ 38 | # -DUSB_RANDOMTEST_UP_SEED=0x5f443bba \ 39 | # -DUSB_RANDOMTEST_DOWN_SEED=0x684053d8 \ 40 | # -DUSB_RANDOMTEST_LENGTH_SEED=0x769bc5e6 \ 41 | # -DSLAVE_LENGTH_SEED=0x4b6420e0 \ 42 | # -DUSB_RANDOMTEST_SYNC_LENGTH=$(SYNC_LEN)\ 43 | # -DHISTOGRAM_LENGTH=8 \ 44 | # -DREPORT_EVERY=$(REPORT_EVERY) \ 45 | # $(ASYNC)RANDOMTEST_LIBUSB_ASYNC \ 46 | # $(DEBUG)DEBUG 47 | 48 | #CXX_OPTIONS = $(CXX_FLAGS) $(INCLUDES) $(CONFIGURATION) 49 | CXX_OPTIONS = $(CXX_FLAGS) $(INCLUDES) 50 | 51 | %.o: %.cxx 52 | $(CXX) -c $(CXX_OPTIONS) $< -o $@ 53 | 54 | # Copy files from the papoon_usb project 55 | # We did this to first copy the files, but now we have 56 | # made changes to a few of them and if we did "make get" 57 | # again, we would overwrite those changes, so we 58 | # comment this out to avoid such a catastrophe. 59 | 60 | PAPOON_PATH = /u1/Projects/STM32/papoon_usb 61 | #get: 62 | # cp $(PAPOON_PATH)/examples/blue_pill/example.cxx . 63 | # cp $(PAPOON_PATH)/arm/core_cm3.hxx . 64 | # cp $(PAPOON_PATH)/regbits/regbits.hxx . 65 | # cp $(PAPOON_PATH)/regbits/stm32f103xb.hxx . 66 | # cp $(PAPOON_PATH)/regbits/stm32f103xb_tim.hxx . 67 | # cp $(PAPOON_PATH)/usb/usb_dev_cdc_acm.hxx . 68 | # cp $(PAPOON_PATH)/usb/usb_dev.hxx . 69 | # cp $(PAPOON_PATH)/examples/blue_pill/usb_mcu_init.hxx . 70 | # cp $(PAPOON_PATH)/usb/usb_dev.cxx . 71 | # cp $(PAPOON_PATH)/util/bin_to_hex.hxx . 72 | # cp $(PAPOON_PATH)/usb/usb_dev_cdc_acm.cxx . 73 | # cp $(PAPOON_PATH)/examples/blue_pill/usb_mcu_init.cxx . 74 | 75 | clean: 76 | rm -f *.o 77 | 78 | # THE END 79 | -------------------------------------------------------------------------------- /usb_papoon/papoon/example.cxx: -------------------------------------------------------------------------------- 1 | // papoon_usb: "Not Insane" USB library for STM32F103xx MCUs 2 | // Copyright (C) 2019,2020 Mark R. Rubin 3 | // 4 | // This file is part of papoon_usb. 5 | // 6 | // The papoon_usb program is free software: you can redistribute it 7 | // and/or modify it under the terms of the GNU General Public License 8 | // as published by the Free Software Foundation, either version 3 of 9 | // the License, or (at your option) any later version. 10 | // 11 | // The papoon_usb program is distributed in the hope that it will be 12 | // useful, but WITHOUT ANY WARRANTY; without even the implied warranty 13 | // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | // General Public License for more details. 15 | // 16 | // You should have received a copy of the GNU General Public License 17 | // (LICENSE.txt) along with the papoon_usb program. If not, see 18 | // 19 | 20 | #include 21 | 22 | // #include 23 | // #include 24 | 25 | #include 26 | 27 | #include 28 | 29 | 30 | using namespace stm32f10_12357_xx; 31 | 32 | 33 | UsbDevCdcAcm usb_dev; 34 | 35 | uint8_t recv_buf[UsbDevCdcAcm::CDC_OUT_DATA_SIZE], 36 | send_buf[UsbDevCdcAcm::CDC_IN_DATA_SIZE ]; 37 | 38 | void cxx_main ( void ); 39 | 40 | // extern "C" void printf ( const char *, ... ); 41 | 42 | extern "C" void 43 | papoon_main ( void ) 44 | { 45 | cxx_main(); 46 | } 47 | 48 | // int main() 49 | void cxx_main() 50 | { 51 | usb_dev.serial_number_init(); 52 | 53 | usb_mcu_init(); 54 | 55 | usb_dev.init(); 56 | 57 | while (usb_dev.device_state() != UsbDev::DeviceState::CONFIGURED) 58 | usb_dev.poll(); 59 | 60 | while (true) { 61 | usb_dev.poll(); 62 | 63 | uint16_t recv_len, 64 | send_len; 65 | 66 | int i; 67 | int j; 68 | 69 | if (recv_len = usb_dev.recv(UsbDevCdcAcm::CDC_ENDPOINT_OUT, recv_buf)) { 70 | 71 | // process data received from host -- populate send_buf and set send_len 72 | // printf ( "Papoon recv %d\n", recv_len ); 73 | // we see single characters received as we type on picocom 74 | j = 0; 75 | for ( i=0; i 19 | 20 | 21 | #ifndef USB_DEV_CDC_ACM_HXX 22 | #define USB_DEV_CDC_ACM_HXX 23 | 24 | 25 | #ifndef CDC_OUT_EP_SIZE 26 | #define CDC_OUT_EP_SIZE 64 // must be modulo 4, max 64) 27 | #endif 28 | 29 | 30 | #include 31 | 32 | #if USB_DEV_MAJOR_VERSION == 1 33 | #if USB_DEV_MINOR_VERSION < 0 34 | #warning USB_DEV_MINOR_VERSION < 0 with required USB_DEV_MAJOR_VERSION == 1 35 | #endif 36 | #else 37 | #error USB_DEV_MAJOR_VERSION != 1 38 | #endif 39 | 40 | 41 | namespace stm32f10_12357_xx { 42 | 43 | class UsbDevCdcAcm : public UsbDev 44 | { 45 | public: 46 | static const uint8_t // have to be public for clients, static descriptors 47 | ACM_ENDPOINT = 2, 48 | CDC_ENDPOINT_IN = 1, 49 | CDC_ENDPOINT_OUT = 3, 50 | // have to be public for extern static definition 51 | CDC_IN_DATA_SIZE = 64, 52 | CDC_OUT_DATA_SIZE = CDC_OUT_EP_SIZE, 53 | ACM_DATA_SIZE = 8; 54 | 55 | constexpr UsbDevCdcAcm() 56 | : UsbDev() 57 | {} 58 | 59 | bool init(); 60 | 61 | 62 | // need public accessor for static initialization of _NEW_STRING_DESCS 63 | // 64 | static constexpr const uint8_t* device_string_desc() 65 | { 66 | return _device_string_desc; 67 | } 68 | 69 | 70 | 71 | 72 | protected: 73 | friend class UsbDev; 74 | 75 | struct LineCoding { 76 | uint32_t baud ; 77 | uint8_t stop_bits , 78 | parity_code, 79 | bits ; 80 | }; 81 | 82 | static const uint8_t _NUM_ENDPOINTS = 4 , 83 | _SET_LINE_CODING = 0x20, 84 | _GET_LINE_CODING = 0x21, 85 | _SET_CONTROL_LINE_STATE = 0x22; 86 | 87 | static const uint8_t _device_string_desc[]; 88 | static LineCoding _line_coding ; 89 | 90 | }; // class UsbDevCdcAcm 91 | 92 | } // namespace stm32f10_12357_xx 93 | 94 | #endif // ifndef USB_DEV_CDC_ACM_HXX 95 | -------------------------------------------------------------------------------- /usb_papoon/papoon/usb_mcu_init.cxx: -------------------------------------------------------------------------------- 1 | // papoon_usb: "Not Insane" USB library for STM32F103xx MCUs 2 | // Copyright (C) 2019,2020 Mark R. Rubin 3 | // 4 | // This file is part of papoon_usb. 5 | // 6 | // The papoon_usb program is free software: you can redistribute it 7 | // and/or modify it under the terms of the GNU General Public License 8 | // as published by the Free Software Foundation, either version 3 of 9 | // the License, or (at your option) any later version. 10 | // 11 | // The papoon_usb program is distributed in the hope that it will be 12 | // useful, but WITHOUT ANY WARRANTY; without even the implied warranty 13 | // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | // General Public License for more details. 15 | // 16 | // You should have received a copy of the GNU General Public License 17 | // (LICENSE.txt) along with the papoon_usb program. If not, see 18 | // 19 | 20 | #include 21 | 22 | #include 23 | 24 | 25 | namespace stm32f10_12357_xx { 26 | 27 | using namespace stm32f103xb; 28 | 29 | 30 | void usb_mcu_init() 31 | { 32 | #ifdef USB_DEV_FLASH_WAIT_STATES 33 | #if USB_DEV_FLASH_WAIT_STATES == 1 34 | // enable flash prefetch buffer, one wait state 35 | flash->acr |= Flash::Acr::PRFTBE | Flash::Acr::LATENCY_1_WAIT_STATE; 36 | #elif USB_DEV_FLASH_WAIT_STATES == 2 37 | // enable flash prefetch buffer, two wait states 38 | flash->acr |= Flash::Acr::PRFTBE | Flash::Acr::LATENCY_2_WAIT_STATES; 39 | #endif 40 | #endif 41 | 42 | // TJT is curious 43 | printf ( "TJT PERIPH_BASE = %08x\n", PERIPH_BASE ); 44 | printf ( "TJT (addr) PERIPH_BASE = %08x\n", &PERIPH_BASE ); 45 | 46 | rcc->cr |= Rcc::Cr::HSEON; 47 | while(!rcc->cr.any(Rcc::Cr::HSERDY)); 48 | 49 | rcc->cfgr |= Rcc::Cfgr::HPRE_DIV_1 50 | | Rcc::Cfgr::PPRE2_DIV_1 51 | | Rcc::Cfgr::PPRE1_DIV_1; 52 | 53 | rcc->cfgr.ins( Rcc::Cfgr::PLLSRC // PLL input from HSE (8 MHz) 54 | | Rcc::Cfgr::PLLMULL_9);// Multiply by 9 (8*9=72 MHz) 55 | 56 | rcc->cr |= Rcc::Cr::PLLON; 57 | while(!rcc->cr.any(Rcc::Cr::PLLRDY)); 58 | 59 | rcc->cfgr.ins(Rcc::Cfgr::SW_PLL); // use PLL as system clock 60 | while(!rcc->cfgr.all(Rcc::Cfgr::SWS_PLL)); // wait for confirmation 61 | 62 | rcc->cfgr.clr(Rcc::Cfgr::USBPRE); // 1.5x USB prescaler 63 | 64 | rcc->apb1enr |= Rcc::Apb1enr::USBEN; // enable USB peripheral 65 | 66 | #ifdef USB_DEV_DMA_PMA 67 | rcc->ahbenr |= Rcc::Ahbenr::DMA1EN; 68 | #endif 69 | 70 | // enable GPIOs and alternate functions 71 | rcc->apb2enr |= Rcc::Apb2enr::IOPAEN 72 | | Rcc::Apb2enr::IOPCEN 73 | | Rcc::Apb2enr::AFIOEN; 74 | 75 | } // usb_mcu_init() 76 | 77 | 78 | 79 | void usb_gpio_init() 80 | { 81 | // set user LED to output, 2 MHz 82 | gpioc->crh.ins( Gpio::Crh::CNF13_OUTPUT_OPEN_DRAIN 83 | | Gpio::Crh::MODE13_OUTPUT_2_MHZ ); 84 | 85 | // enable USB alternate function on USB+ and USB- data pins 86 | // alternate function output, open-drain (for paranoia), speed 50MHz 87 | // == 0b1111 88 | // some parameters reset by USB peripheral anyway?? 89 | gpioa->crh.ins( Gpio::Crh::CNF11_ALTFUNC_OPEN_DRAIN 90 | | Gpio::Crh::CNF12_ALTFUNC_OPEN_DRAIN 91 | | Gpio::Crh::MODE11_OUTPUT_50_MHZ 92 | | Gpio::Crh::MODE12_OUTPUT_50_MHZ ); 93 | } 94 | 95 | } // namespace stm32f10_12357_xx 96 | -------------------------------------------------------------------------------- /usb_papoon/papoon/usb_mcu_init.hxx: -------------------------------------------------------------------------------- 1 | // papoon_usb: "Not Insane" USB library for STM32F103xx MCUs 2 | // Copyright (C) 2019,2020 Mark R. Rubin 3 | // 4 | // This file is part of papoon_usb. 5 | // 6 | // The papoon_usb program is free software: you can redistribute it 7 | // and/or modify it under the terms of the GNU General Public License 8 | // as published by the Free Software Foundation, either version 3 of 9 | // the License, or (at your option) any later version. 10 | // 11 | // The papoon_usb program is distributed in the hope that it will be 12 | // useful, but WITHOUT ANY WARRANTY; without even the implied warranty 13 | // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | // General Public License for more details. 15 | // 16 | // You should have received a copy of the GNU General Public License 17 | // (LICENSE.txt) along with the papoon_usb program. If not, see 18 | // 19 | 20 | 21 | #ifndef USB_MCY_INIT_HXX 22 | #define USB_MCY_INIT_HXX 23 | 24 | namespace stm32f10_12357_xx { 25 | 26 | void usb_mcu_init (); 27 | void usb_gpio_init(); 28 | 29 | } // namespace stm32f10_12357_xx 30 | 31 | #endif // ifndef USB_MCY_INIT_HXX 32 | -------------------------------------------------------------------------------- /usb_papoon/serial.c: -------------------------------------------------------------------------------- 1 | /* serial.c 2 | * (c) Tom Trebisky 7-2-2017 3 | * 4 | * Driver for the STM32F103 usart 5 | */ 6 | 7 | /* One of the 3 uarts */ 8 | struct uart { 9 | volatile unsigned long status; /* 00 */ 10 | volatile unsigned long data; /* 04 */ 11 | volatile unsigned long baud; /* 08 */ 12 | volatile unsigned long cr1; /* 0c */ 13 | volatile unsigned long cr2; /* 10 */ 14 | volatile unsigned long cr3; /* 14 */ 15 | volatile unsigned long gtp; /* 18 - guard time and prescaler */ 16 | }; 17 | 18 | #define UART1_BASE (struct uart *) 0x40013800 19 | #define UART2_BASE (struct uart *) 0x40004400 20 | #define UART3_BASE (struct uart *) 0x40004800 21 | 22 | /* bits in the status register */ 23 | #define ST_PE 0x0001 24 | #define ST_FE 0x0002 25 | #define ST_NE 0x0004 26 | #define ST_OVER 0x0008 27 | #define ST_IDLE 0x0010 28 | #define ST_RXNE 0x0020 /* Receiver not empty */ 29 | #define ST_TC 0x0040 /* Transmission complete */ 30 | #define ST_TXE 0x0080 /* Transmitter empty */ 31 | #define ST_BREAK 0x0100 32 | #define ST_CTS 0x0200 33 | 34 | static void 35 | uart_init ( struct uart *up, int baud ) 36 | { 37 | /* 1 start bit, even parity */ 38 | up->cr1 = 0x340c; 39 | up->cr2 = 0; 40 | up->cr3 = 0; 41 | up->gtp = 0; 42 | 43 | if ( up == UART1_BASE ) 44 | up->baud = get_pclk2() / baud; 45 | else 46 | up->baud = get_pclk1() / baud; 47 | } 48 | 49 | void 50 | serial_init ( void ) 51 | { 52 | gpio_uart1 (); 53 | // uart_init ( UART1_BASE, 9600 ); 54 | // uart_init ( UART1_BASE, 38400 ); 55 | // uart_init ( UART1_BASE, 57600 ); ?? 56 | uart_init ( UART1_BASE, 115200 ); 57 | 58 | #ifdef notdef 59 | gpio_uart2 (); 60 | uart_init ( UART2_BASE, 9600 ); 61 | 62 | gpio_uart3 (); 63 | uart_init ( UART3_BASE, 9600 ); 64 | #endif 65 | } 66 | 67 | void 68 | serial_putc ( int c ) 69 | { 70 | struct uart *up = UART1_BASE; 71 | 72 | if ( c == '\n' ) 73 | serial_putc ( '\r' ); 74 | 75 | while ( ! (up->status & ST_TXE) ) 76 | ; 77 | up->data = c; 78 | } 79 | 80 | void 81 | serial_puts ( char *s ) 82 | { 83 | while ( *s ) 84 | serial_putc ( *s++ ); 85 | } 86 | 87 | /* -------------------------------------------- */ 88 | /* Some IO stuff from Kyu */ 89 | /* -------------------------------------------- */ 90 | 91 | #define HEX(x) ((x)<10 ? '0'+(x) : 'A'+(x)-10) 92 | 93 | #define PUTCHAR(x) *buf++ = (x) 94 | 95 | static char * 96 | shex2( char *buf, int val ) 97 | { 98 | PUTCHAR( HEX((val>>4)&0xf) ); 99 | PUTCHAR( HEX(val&0xf) ); 100 | return buf; 101 | } 102 | 103 | static char * 104 | shex4( char *buf, int val ) 105 | { 106 | buf = shex2(buf,val>>8); 107 | return shex2(buf,val); 108 | } 109 | 110 | static char * 111 | shex8( char *buf, int val ) 112 | { 113 | buf = shex2(buf,val>>24); 114 | buf = shex2(buf,val>>16); 115 | buf = shex2(buf,val>>8); 116 | return shex2(buf,val); 117 | } 118 | 119 | void 120 | show16 ( char *s, int val ) 121 | { 122 | char buf[5]; 123 | 124 | serial_puts ( s ); 125 | shex4 ( buf, val ); 126 | buf[4] = '\0'; 127 | serial_puts ( buf ); 128 | serial_putc ( '\n' ); 129 | } 130 | 131 | /* THE END */ 132 | -------------------------------------------------------------------------------- /usb_papoon/startup.c: -------------------------------------------------------------------------------- 1 | /* startup.c 2 | * (c) Tom Trebisky 9-14-2020 3 | * 4 | * This runs before main. 5 | */ 6 | 7 | extern unsigned int __text_end; 8 | 9 | extern unsigned int __data_start; 10 | extern unsigned int __data_end; 11 | 12 | extern unsigned int __bss_start; 13 | extern unsigned int __bss_end; 14 | 15 | /* Now startup() is in main.c and calls this. 16 | * I do this so I can initialize the uart early 17 | * and then do printf from in here. 18 | */ 19 | void 20 | mem_init ( void ) 21 | { 22 | unsigned int *src = &__text_end; 23 | unsigned int *p; 24 | int count; 25 | 26 | /* Zero BSS */ 27 | for ( p = &__bss_start; p < &__bss_end; p++ ) 28 | *p = 0; 29 | 30 | count = &__bss_end - &__bss_start; 31 | count--; 32 | printf ( "%d bytes of BSS cleared\n", count ); 33 | 34 | // init_vars (); 35 | 36 | /* Copy initialized data from flash */ 37 | for ( p = &__data_start; p < &__data_end; p++ ) 38 | *p = *src++; 39 | 40 | count = &__data_end - &__data_start; 41 | count--; 42 | printf ( "%d bytes of Data initialized\n", count ); 43 | } 44 | 45 | #ifdef notdef 46 | /* This gets called from locore.s */ 47 | void 48 | startup ( void ) 49 | { 50 | unsigned int *src = &__text_end; 51 | unsigned int *p; 52 | 53 | /* Zero BSS */ 54 | for ( p = &__bss_start; p < &__bss_end; p++ ) 55 | *p = 0; 56 | 57 | // init_vars (); 58 | 59 | /* Copy initialized data from flash */ 60 | for ( p = &__data_start; p < &__data_end; p++ ) 61 | *p = *src++; 62 | 63 | main (); 64 | } 65 | #endif 66 | 67 | /* THE END */ 68 | -------------------------------------------------------------------------------- /usb_papoon2/.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.elf 3 | *.bin 4 | *.dump 5 | *.swp 6 | zzz 7 | zz 8 | z 9 | OLD 10 | OLD/* 11 | tags 12 | -------------------------------------------------------------------------------- /usb_papoon2/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for interrupt demo 2 | 3 | #TOOLS = arm-linux-gnu 4 | TOOLS = arm-none-eabi 5 | 6 | # Assembling with gcc makes it want crt0 at link time. 7 | #AS = $(TOOLS)-gcc 8 | AS = $(TOOLS)-as 9 | 10 | #COPTS = -mcpu=cortex-m3 -mthumb -Wno-implicit-function-declaration 11 | COPTS = -mcpu=cortex-m3 -mthumb -Wno-implicit-function-declaration -fno-builtin-printf -fno-builtin 12 | # Use the -g flag if you intend to use gdb 13 | #CC = $(TOOLS)-gcc $(COPTS) -g 14 | #CC = $(TOOLS)-gcc $(COPTS) 15 | CC = $(TOOLS)-gcc $(COPTS) -O2 16 | 17 | #LD = $(TOOLS)-gcc 18 | LD = $(TOOLS)-ld.bfd --no-warn-rwx-segments 19 | OBJCOPY = $(TOOLS)-objcopy 20 | DUMP = $(TOOLS)-objdump -d 21 | GDB = $(TOOLS)-gdb 22 | 23 | #OBJS = locore.o main.o nvic.o rcc.o gpio.o prf.o serial.o timer.o usb.o 24 | OBJS = locore.o main.o startup.o nvic.o rcc.o gpio.o prf.o serial.o timer.o usb.o papoon.o 25 | 26 | all: papoon.elf papoon.dump tags 27 | 28 | papoon.o: bogus 29 | cd papoon; make 30 | 31 | bogus: 32 | 33 | papoon.dump: papoon.elf 34 | $(DUMP) papoon.elf >papoon.dump 35 | 36 | papoon.bin: papoon.elf 37 | $(OBJCOPY) papoon.elf papoon.bin -O binary 38 | 39 | papoon.elf: $(OBJS) 40 | $(LD) -T papoon.lds -o papoon.elf $(OBJS) 41 | 42 | locore.o: locore.s 43 | $(AS) locore.s -o locore.o 44 | 45 | .c.o: 46 | $(CC) -o $@ -c $< 47 | 48 | OCDCFG = -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/share/openocd/scripts/target/stm32f1x.cfg 49 | 50 | # This works great once I fixed the base address in the LDS file 51 | flash: papoon.elf 52 | openocd $(OCDCFG) -c "program papoon.elf verify reset exit" 53 | 54 | # No need for this now that the above works 55 | bflash: papoon.bin 56 | openocd $(OCDCFG) -c "program papoon.bin verify reset exit 0x08000000" 57 | 58 | gdb: 59 | $(GDB) --eval-command="target remote localhost:3333" papoon.elf 60 | 61 | gdbtui: 62 | $(GDB) -tui --eval-command="target remote localhost:3333" papoon.elf 63 | 64 | tags: 65 | ctags -R . 66 | 67 | clean: 68 | cd papoon; make clean 69 | rm -f *.o papoon.elf papoon.bin papoon.dump 70 | rm -f tags 71 | -------------------------------------------------------------------------------- /usb_papoon2/README.md: -------------------------------------------------------------------------------- 1 | usb_papoon2 2 | 3 | Ignore this, just a stepping stone to usb_baboon. 4 | -------------------------------------------------------------------------------- /usb_papoon2/mkvec: -------------------------------------------------------------------------------- 1 | #!/bin/ruby 2 | 3 | (0..67).each { |v| 4 | print ".word\tbogus\t\t/* IRQ %2d */\n" % v 5 | } 6 | -------------------------------------------------------------------------------- /usb_papoon2/ocd: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # These are proper file locations for Fedora 24 3 | 4 | openocd -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/share/openocd/scripts/target/stm32f1x.cfg 5 | -------------------------------------------------------------------------------- /usb_papoon2/papoon.lds: -------------------------------------------------------------------------------- 1 | /* blink.lds 2 | * linker script for blink demo. 3 | * 4 | * Memory layout on my STM32F103 is pretty much like this: 5 | * 6 | * 0x00000000 - 0x07ffffff - aliased to flash or sys memory depending on BOOT jumpers 7 | * 0x08000000 - 0x0800ffff - Flash (64K ... or 128K) 8 | * 0x1ffff000 - 0x1ffff7ff - Boot firmware in system memory 9 | * 0x1ffff800 - 0x1fffffff - option bytes 10 | * 0x20000000 - 0x20004fff - SRAM (20k) 11 | * 0x40000000 - 0x40023400 - peripherals 12 | * 13 | * flash(RX) : ORIGIN = 0x00000000, LENGTH = 128K 14 | */ 15 | MEMORY 16 | { 17 | flash(RX) : ORIGIN = 0x08000000, LENGTH = 128K 18 | sram(RW) : ORIGIN = 0x20000000, LENGTH = 20K 19 | } 20 | 21 | SECTIONS 22 | { 23 | .text : 24 | { 25 | *(.text*) 26 | . = ALIGN(4); 27 | __text_end = .; 28 | } > flash 29 | 30 | .bss : 31 | { 32 | . = ALIGN(4); 33 | __bss_start = .; 34 | *(.bss*) 35 | *(COMMON) 36 | . = ALIGN(4); 37 | __bss_end = .; 38 | } > sram 39 | 40 | .data : 41 | { 42 | . = ALIGN(4); 43 | __data_start = .; 44 | *(.data*) 45 | . = ALIGN(4); 46 | __data_end = .; 47 | } > sram AT> flash 48 | 49 | .rodata : 50 | { 51 | . = ALIGN(4); 52 | *(.rodata*) 53 | . = ALIGN(4); 54 | } > flash 55 | 56 | } 57 | -------------------------------------------------------------------------------- /usb_papoon2/papoon/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for papoon submodule 2 | 3 | #TOOLS = arm-linux-gnu 4 | TOOLS = arm-none-eabi 5 | 6 | #LD = $(TOOLS)-gcc 7 | LD = $(TOOLS)-ld.bfd --no-warn-rwx-segments 8 | 9 | OBJS = example.o usb_dev.o usb_dev_cdc_acm.o usb_mcu_init.o 10 | 11 | all: ../papoon.o 12 | 13 | ../papoon.o: $(OBJS) 14 | $(LD) -r -o ../papoon.o $(OBJS) 15 | 16 | # =========================================== 17 | 18 | CXX = $(TOOLS)-g++ 19 | 20 | # Without the -fno-exceptions, we get calls to: 21 | # __aeabi_unwind_cpp_pr0 22 | # __aeabi_unwind_cpp_pr1 23 | 24 | #CXX_FLAGS = -g -O1 -mcpu=cortex-m3 -mthumb -fno-builtin -fno-exceptions 25 | CXX_FLAGS = -O1 -mcpu=cortex-m3 -mthumb -fno-builtin -fno-exceptions 26 | INCLUDES = -I. 27 | 28 | # All this is now in papoon.hxx 29 | #SEND_B4_RECV ?= 64 30 | #SYNC_LEN ?= 4 31 | #REPORT_EVERY ?= 10000 32 | #ASYNC ?= -D 33 | #DEBUG ?= -U 34 | # 35 | #CONFIGURATION = -DUP_MAX_PACKET_SIZE=64 \ 36 | # -DDOWN_MAX_PACKET_SIZE=64 \ 37 | # -DSEND_B4_RECV_MAX=$(SEND_B4_RECV) \ 38 | # -DUSB_RANDOMTEST_UP_SEED=0x5f443bba \ 39 | # -DUSB_RANDOMTEST_DOWN_SEED=0x684053d8 \ 40 | # -DUSB_RANDOMTEST_LENGTH_SEED=0x769bc5e6 \ 41 | # -DSLAVE_LENGTH_SEED=0x4b6420e0 \ 42 | # -DUSB_RANDOMTEST_SYNC_LENGTH=$(SYNC_LEN)\ 43 | # -DHISTOGRAM_LENGTH=8 \ 44 | # -DREPORT_EVERY=$(REPORT_EVERY) \ 45 | # $(ASYNC)RANDOMTEST_LIBUSB_ASYNC \ 46 | # $(DEBUG)DEBUG 47 | 48 | #CXX_OPTIONS = $(CXX_FLAGS) $(INCLUDES) $(CONFIGURATION) 49 | CXX_OPTIONS = $(CXX_FLAGS) $(INCLUDES) 50 | 51 | %.o: %.cxx 52 | $(CXX) -c $(CXX_OPTIONS) $< -o $@ 53 | 54 | # Copy files from the papoon_usb project 55 | # We did this to first copy the files, but now we have 56 | # made changes to a few of them and if we did "make get" 57 | # again, we would overwrite those changes, so we 58 | # comment this out to avoid such a catastrophe. 59 | 60 | PAPOON_PATH = /u1/Projects/STM32/papoon_usb 61 | #get: 62 | # cp $(PAPOON_PATH)/examples/blue_pill/example.cxx . 63 | # cp $(PAPOON_PATH)/arm/core_cm3.hxx . 64 | # cp $(PAPOON_PATH)/regbits/regbits.hxx . 65 | # cp $(PAPOON_PATH)/regbits/stm32f103xb.hxx . 66 | # cp $(PAPOON_PATH)/regbits/stm32f103xb_tim.hxx . 67 | # cp $(PAPOON_PATH)/usb/usb_dev_cdc_acm.hxx . 68 | # cp $(PAPOON_PATH)/usb/usb_dev.hxx . 69 | # cp $(PAPOON_PATH)/examples/blue_pill/usb_mcu_init.hxx . 70 | # cp $(PAPOON_PATH)/usb/usb_dev.cxx . 71 | # cp $(PAPOON_PATH)/util/bin_to_hex.hxx . 72 | # cp $(PAPOON_PATH)/usb/usb_dev_cdc_acm.cxx . 73 | # cp $(PAPOON_PATH)/examples/blue_pill/usb_mcu_init.cxx . 74 | 75 | clean: 76 | rm -f *.o 77 | 78 | # THE END 79 | -------------------------------------------------------------------------------- /usb_papoon2/papoon/papoon.h: -------------------------------------------------------------------------------- 1 | /* papoon.hxx 2 | * Introduced by Tom Trebisky 11-4-2023 3 | * 4 | * The original Papoon setup did all this CONFIGURATION stuff 5 | # in the Makefile via the following long list of compiler options. 6 | * 7 | * I decided that I would rather clean up the build messages 8 | * and moved it all into this file. 9 | */ 10 | 11 | extern "C" void printf ( const char *, ... ); 12 | // extern "C" void delay ( void ); 13 | extern "C" void delay_ms ( int ); 14 | extern "C" void pma_show ( void ); 15 | extern "C" void tjt_enum_logger ( int what ); 16 | 17 | #define USB_DEV_INTERRUPT_DRIVEN 18 | 19 | // SEND_B4_RECV ?= 64 20 | // SYNC_LEN ?= 4 21 | // REPORT_EVERY ?= 10000 22 | // ASYNC ?= -D 23 | // DEBUG ?= -U 24 | 25 | // CONFIGURATION = -DUP_MAX_PACKET_SIZE=64 \ 26 | // -DDOWN_MAX_PACKET_SIZE=64 \ 27 | // -DSEND_B4_RECV_MAX=$(SEND_B4_RECV) \ 28 | // -DUSB_RANDOMTEST_UP_SEED=0x5f443bba \ 29 | // -DUSB_RANDOMTEST_DOWN_SEED=0x684053d8 \ 30 | // -DUSB_RANDOMTEST_LENGTH_SEED=0x769bc5e6 \ 31 | // -DSLAVE_LENGTH_SEED=0x4b6420e0 \ 32 | // -DUSB_RANDOMTEST_SYNC_LENGTH=$(SYNC_LEN)\ 33 | // -DHISTOGRAM_LENGTH=8 \ 34 | // -DREPORT_EVERY=$(REPORT_EVERY) \ 35 | // $(ASYNC)RANDOMTEST_LIBUSB_ASYNC \ 36 | // $(DEBUG)DEBUG 37 | 38 | #define UP_MAX_PACKET_SIZE 64 39 | #define DOWN_MAX_PACKET_SIZE 64 40 | #define SEND_B4_RECV_MAX 64 41 | 42 | #define USB_RANDOMTEST_UP_SEED 0x5f443bba 43 | #define USB_RANDOMTEST_DOWN_SEED 0x684053d8 44 | #define USB_RANDOMTEST_LENGTH_SEED 0x769bc5e6 45 | 46 | #define SLAVE_LENGTH_SEED 0x4b6420e0 47 | #define USB_RANDOMTEST_SYNC_LENGTH 4 48 | 49 | #define HISTOGRAM_LENGTH 8 50 | #define REPORT_EVERY 1000 51 | 52 | #define RANDOMTEST_LIBUSB_ASYNC 53 | // #define DEBUG 54 | 55 | // THE END 56 | -------------------------------------------------------------------------------- /usb_papoon2/papoon/usb_dev_cdc_acm.h: -------------------------------------------------------------------------------- 1 | // papoon_usb: "Not Insane" USB library for STM32F103xx MCUs 2 | // Copyright (C) 2019,2020 Mark R. Rubin 3 | // 4 | // This file is part of papoon_usb. 5 | // 6 | // The papoon_usb program is free software: you can redistribute it 7 | // and/or modify it under the terms of the GNU General Public License 8 | // as published by the Free Software Foundation, either version 3 of 9 | // the License, or (at your option) any later version. 10 | // 11 | // The papoon_usb program is distributed in the hope that it will be 12 | // useful, but WITHOUT ANY WARRANTY; without even the implied warranty 13 | // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | // General Public License for more details. 15 | // 16 | // You should have received a copy of the GNU General Public License 17 | // (LICENSE.txt) along with the papoon_usb program. If not, see 18 | // 19 | 20 | 21 | #ifndef USB_DEV_CDC_ACM_HXX 22 | #define USB_DEV_CDC_ACM_HXX 23 | 24 | 25 | #ifndef CDC_OUT_EP_SIZE 26 | #define CDC_OUT_EP_SIZE 64 // must be modulo 4, max 64) 27 | #endif 28 | 29 | 30 | #include 31 | 32 | #if USB_DEV_MAJOR_VERSION == 1 33 | #if USB_DEV_MINOR_VERSION < 0 34 | #warning USB_DEV_MINOR_VERSION < 0 with required USB_DEV_MAJOR_VERSION == 1 35 | #endif 36 | #else 37 | #error USB_DEV_MAJOR_VERSION != 1 38 | #endif 39 | 40 | 41 | namespace stm32f10_12357_xx { 42 | 43 | class UsbDevCdcAcm : public UsbDev 44 | { 45 | public: 46 | static const uint8_t // have to be public for clients, static descriptors 47 | ACM_ENDPOINT = 2, 48 | CDC_ENDPOINT_IN = 1, 49 | CDC_ENDPOINT_OUT = 3, 50 | // have to be public for extern static definition 51 | CDC_IN_DATA_SIZE = 64, 52 | CDC_OUT_DATA_SIZE = CDC_OUT_EP_SIZE, 53 | ACM_DATA_SIZE = 8; 54 | 55 | constexpr UsbDevCdcAcm() 56 | : UsbDev() 57 | {} 58 | 59 | bool init(); 60 | 61 | 62 | // need public accessor for static initialization of _NEW_STRING_DESCS 63 | // 64 | static constexpr const uint8_t* device_string_desc() 65 | { 66 | return _device_string_desc; 67 | } 68 | 69 | 70 | 71 | 72 | protected: 73 | friend class UsbDev; 74 | 75 | struct LineCoding { 76 | uint32_t baud ; 77 | uint8_t stop_bits , 78 | parity_code, 79 | bits ; 80 | }; 81 | 82 | static const uint8_t _NUM_ENDPOINTS = 4 , 83 | _SET_LINE_CODING = 0x20, 84 | _GET_LINE_CODING = 0x21, 85 | _SET_CONTROL_LINE_STATE = 0x22; 86 | 87 | static const uint8_t _device_string_desc[]; 88 | static LineCoding _line_coding ; 89 | 90 | }; // class UsbDevCdcAcm 91 | 92 | } // namespace stm32f10_12357_xx 93 | 94 | #endif // ifndef USB_DEV_CDC_ACM_HXX 95 | -------------------------------------------------------------------------------- /usb_papoon2/papoon/usb_mcu_init.cxx: -------------------------------------------------------------------------------- 1 | // papoon_usb: "Not Insane" USB library for STM32F103xx MCUs 2 | // Copyright (C) 2019,2020 Mark R. Rubin 3 | // 4 | // This file is part of papoon_usb. 5 | // 6 | // The papoon_usb program is free software: you can redistribute it 7 | // and/or modify it under the terms of the GNU General Public License 8 | // as published by the Free Software Foundation, either version 3 of 9 | // the License, or (at your option) any later version. 10 | // 11 | // The papoon_usb program is distributed in the hope that it will be 12 | // useful, but WITHOUT ANY WARRANTY; without even the implied warranty 13 | // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | // General Public License for more details. 15 | // 16 | // You should have received a copy of the GNU General Public License 17 | // (LICENSE.txt) along with the papoon_usb program. If not, see 18 | // 19 | 20 | #include 21 | 22 | #include 23 | 24 | 25 | namespace stm32f10_12357_xx { 26 | 27 | using namespace stm32f103xb; 28 | 29 | 30 | void usb_mcu_init() 31 | { 32 | #ifdef USB_DEV_FLASH_WAIT_STATES 33 | #if USB_DEV_FLASH_WAIT_STATES == 1 34 | // enable flash prefetch buffer, one wait state 35 | flash->acr |= Flash::Acr::PRFTBE | Flash::Acr::LATENCY_1_WAIT_STATE; 36 | #elif USB_DEV_FLASH_WAIT_STATES == 2 37 | // enable flash prefetch buffer, two wait states 38 | flash->acr |= Flash::Acr::PRFTBE | Flash::Acr::LATENCY_2_WAIT_STATES; 39 | #endif 40 | #endif 41 | 42 | // TJT is curious 43 | // printf ( "TJT PERIPH_BASE = %08x\n", PERIPH_BASE ); 44 | // printf ( "TJT (addr) PERIPH_BASE = %08x\n", &PERIPH_BASE ); 45 | 46 | rcc->cr |= Rcc::Cr::HSEON; 47 | while(!rcc->cr.any(Rcc::Cr::HSERDY)); 48 | 49 | rcc->cfgr |= Rcc::Cfgr::HPRE_DIV_1 50 | | Rcc::Cfgr::PPRE2_DIV_1 51 | | Rcc::Cfgr::PPRE1_DIV_1; 52 | 53 | rcc->cfgr.ins( Rcc::Cfgr::PLLSRC // PLL input from HSE (8 MHz) 54 | | Rcc::Cfgr::PLLMULL_9);// Multiply by 9 (8*9=72 MHz) 55 | 56 | rcc->cr |= Rcc::Cr::PLLON; 57 | while(!rcc->cr.any(Rcc::Cr::PLLRDY)); 58 | 59 | rcc->cfgr.ins(Rcc::Cfgr::SW_PLL); // use PLL as system clock 60 | while(!rcc->cfgr.all(Rcc::Cfgr::SWS_PLL)); // wait for confirmation 61 | 62 | rcc->cfgr.clr(Rcc::Cfgr::USBPRE); // 1.5x USB prescaler 63 | 64 | rcc->apb1enr |= Rcc::Apb1enr::USBEN; // enable USB peripheral 65 | 66 | #ifdef USB_DEV_DMA_PMA 67 | rcc->ahbenr |= Rcc::Ahbenr::DMA1EN; 68 | #endif 69 | 70 | // enable GPIOs and alternate functions 71 | rcc->apb2enr |= Rcc::Apb2enr::IOPAEN 72 | | Rcc::Apb2enr::IOPCEN 73 | | Rcc::Apb2enr::AFIOEN; 74 | 75 | } // usb_mcu_init() 76 | 77 | 78 | 79 | void usb_gpio_init() 80 | { 81 | // set user LED to output, 2 MHz 82 | gpioc->crh.ins( Gpio::Crh::CNF13_OUTPUT_OPEN_DRAIN 83 | | Gpio::Crh::MODE13_OUTPUT_2_MHZ ); 84 | 85 | // enable USB alternate function on USB+ and USB- data pins 86 | // alternate function output, open-drain (for paranoia), speed 50MHz 87 | // == 0b1111 88 | // some parameters reset by USB peripheral anyway?? 89 | gpioa->crh.ins( Gpio::Crh::CNF11_ALTFUNC_OPEN_DRAIN 90 | | Gpio::Crh::CNF12_ALTFUNC_OPEN_DRAIN 91 | | Gpio::Crh::MODE11_OUTPUT_50_MHZ 92 | | Gpio::Crh::MODE12_OUTPUT_50_MHZ ); 93 | } 94 | 95 | } // namespace stm32f10_12357_xx 96 | -------------------------------------------------------------------------------- /usb_papoon2/papoon/usb_mcu_init.h: -------------------------------------------------------------------------------- 1 | // papoon_usb: "Not Insane" USB library for STM32F103xx MCUs 2 | // Copyright (C) 2019,2020 Mark R. Rubin 3 | // 4 | // This file is part of papoon_usb. 5 | // 6 | // The papoon_usb program is free software: you can redistribute it 7 | // and/or modify it under the terms of the GNU General Public License 8 | // as published by the Free Software Foundation, either version 3 of 9 | // the License, or (at your option) any later version. 10 | // 11 | // The papoon_usb program is distributed in the hope that it will be 12 | // useful, but WITHOUT ANY WARRANTY; without even the implied warranty 13 | // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | // General Public License for more details. 15 | // 16 | // You should have received a copy of the GNU General Public License 17 | // (LICENSE.txt) along with the papoon_usb program. If not, see 18 | // 19 | 20 | 21 | #ifndef USB_MCY_INIT_HXX 22 | #define USB_MCY_INIT_HXX 23 | 24 | namespace stm32f10_12357_xx { 25 | 26 | void usb_mcu_init (); 27 | void usb_gpio_init(); 28 | 29 | } // namespace stm32f10_12357_xx 30 | 31 | #endif // ifndef USB_MCY_INIT_HXX 32 | -------------------------------------------------------------------------------- /usb_papoon2/serial.c: -------------------------------------------------------------------------------- 1 | /* serial.c 2 | * (c) Tom Trebisky 7-2-2017 3 | * 4 | * Driver for the STM32F103 usart 5 | */ 6 | 7 | /* One of the 3 uarts */ 8 | struct uart { 9 | volatile unsigned long status; /* 00 */ 10 | volatile unsigned long data; /* 04 */ 11 | volatile unsigned long baud; /* 08 */ 12 | volatile unsigned long cr1; /* 0c */ 13 | volatile unsigned long cr2; /* 10 */ 14 | volatile unsigned long cr3; /* 14 */ 15 | volatile unsigned long gtp; /* 18 - guard time and prescaler */ 16 | }; 17 | 18 | #define UART1_BASE (struct uart *) 0x40013800 19 | #define UART2_BASE (struct uart *) 0x40004400 20 | #define UART3_BASE (struct uart *) 0x40004800 21 | 22 | /* bits in the status register */ 23 | #define ST_PE 0x0001 24 | #define ST_FE 0x0002 25 | #define ST_NE 0x0004 26 | #define ST_OVER 0x0008 27 | #define ST_IDLE 0x0010 28 | #define ST_RXNE 0x0020 /* Receiver not empty */ 29 | #define ST_TC 0x0040 /* Transmission complete */ 30 | #define ST_TXE 0x0080 /* Transmitter empty */ 31 | #define ST_BREAK 0x0100 32 | #define ST_CTS 0x0200 33 | 34 | static void 35 | uart_init ( struct uart *up, int baud ) 36 | { 37 | /* 1 start bit, even parity */ 38 | up->cr1 = 0x340c; 39 | up->cr2 = 0; 40 | up->cr3 = 0; 41 | up->gtp = 0; 42 | 43 | if ( up == UART1_BASE ) 44 | up->baud = get_pclk2() / baud; 45 | else 46 | up->baud = get_pclk1() / baud; 47 | } 48 | 49 | void 50 | serial_init ( void ) 51 | { 52 | gpio_uart1 (); 53 | // uart_init ( UART1_BASE, 9600 ); 54 | // uart_init ( UART1_BASE, 38400 ); 55 | // uart_init ( UART1_BASE, 57600 ); ?? 56 | uart_init ( UART1_BASE, 115200 ); 57 | 58 | #ifdef notdef 59 | gpio_uart2 (); 60 | uart_init ( UART2_BASE, 9600 ); 61 | 62 | gpio_uart3 (); 63 | uart_init ( UART3_BASE, 9600 ); 64 | #endif 65 | } 66 | 67 | void 68 | serial_putc ( int c ) 69 | { 70 | struct uart *up = UART1_BASE; 71 | 72 | if ( c == '\n' ) 73 | serial_putc ( '\r' ); 74 | 75 | while ( ! (up->status & ST_TXE) ) 76 | ; 77 | up->data = c; 78 | } 79 | 80 | void 81 | serial_puts ( char *s ) 82 | { 83 | while ( *s ) 84 | serial_putc ( *s++ ); 85 | } 86 | 87 | /* -------------------------------------------- */ 88 | /* Some IO stuff from Kyu */ 89 | /* -------------------------------------------- */ 90 | 91 | #define HEX(x) ((x)<10 ? '0'+(x) : 'A'+(x)-10) 92 | 93 | #define PUTCHAR(x) *buf++ = (x) 94 | 95 | static char * 96 | shex2( char *buf, int val ) 97 | { 98 | PUTCHAR( HEX((val>>4)&0xf) ); 99 | PUTCHAR( HEX(val&0xf) ); 100 | return buf; 101 | } 102 | 103 | static char * 104 | shex4( char *buf, int val ) 105 | { 106 | buf = shex2(buf,val>>8); 107 | return shex2(buf,val); 108 | } 109 | 110 | static char * 111 | shex8( char *buf, int val ) 112 | { 113 | buf = shex2(buf,val>>24); 114 | buf = shex2(buf,val>>16); 115 | buf = shex2(buf,val>>8); 116 | return shex2(buf,val); 117 | } 118 | 119 | void 120 | show16 ( char *s, int val ) 121 | { 122 | char buf[5]; 123 | 124 | serial_puts ( s ); 125 | shex4 ( buf, val ); 126 | buf[4] = '\0'; 127 | serial_puts ( buf ); 128 | serial_putc ( '\n' ); 129 | } 130 | 131 | /* THE END */ 132 | -------------------------------------------------------------------------------- /usb_papoon2/startup.c: -------------------------------------------------------------------------------- 1 | /* startup.c 2 | * (c) Tom Trebisky 9-14-2020 3 | * 4 | * This runs before main. 5 | */ 6 | 7 | extern unsigned int __text_end; 8 | 9 | extern unsigned int __data_start; 10 | extern unsigned int __data_end; 11 | 12 | extern unsigned int __bss_start; 13 | extern unsigned int __bss_end; 14 | 15 | /* Now startup() is in main.c and calls this. 16 | * I do this so I can initialize the uart early 17 | * and then do printf from in here. 18 | */ 19 | void 20 | mem_init ( void ) 21 | { 22 | unsigned int *src = &__text_end; 23 | unsigned int *p; 24 | int count; 25 | 26 | /* Zero BSS */ 27 | for ( p = &__bss_start; p < &__bss_end; p++ ) 28 | *p = 0; 29 | 30 | count = &__bss_end - &__bss_start; 31 | count--; 32 | printf ( "%d bytes of BSS cleared\n", count ); 33 | 34 | // init_vars (); 35 | 36 | /* Copy initialized data from flash */ 37 | for ( p = &__data_start; p < &__data_end; p++ ) 38 | *p = *src++; 39 | 40 | count = &__data_end - &__data_start; 41 | count--; 42 | printf ( "%d bytes of Data initialized\n", count ); 43 | } 44 | 45 | #ifdef notdef 46 | /* This gets called from locore.s */ 47 | void 48 | startup ( void ) 49 | { 50 | unsigned int *src = &__text_end; 51 | unsigned int *p; 52 | 53 | /* Zero BSS */ 54 | for ( p = &__bss_start; p < &__bss_end; p++ ) 55 | *p = 0; 56 | 57 | // init_vars (); 58 | 59 | /* Copy initialized data from flash */ 60 | for ( p = &__data_start; p < &__data_end; p++ ) 61 | *p = *src++; 62 | 63 | main (); 64 | } 65 | #endif 66 | 67 | /* THE END */ 68 | --------------------------------------------------------------------------------