Featured

Cory Koch

Home

Hello and welcome to my corner of the digital world! I’m Cory Koch, and I’m thrilled to have you here at my site. Currently, you’ll find an array of work-in-progress drafts within each blog post. These drafts primarily serve as my personal reference, a canvas for thoughts and ideas that I’m cultivating. However, I envision this space evolving into something more, a platform where I can seamlessly share insights and knowledge with others.

What I choose to write in my free time is a reflection of my personal journey and passion. It’s a collection of thoughts and ideas that I believe should be owned and managed in a way that aligns with my vision. This space is not just a repository; rather, I see it as an open Confluence—a collaborative environment where shared knowledge thrives.

My intention is to transition from personal musings to a more communal experience. I invite those who explore this space to contribute, share, and engage. Consider this an open invitation to participate in the ongoing narrative. Whether you want to add your perspective, contribute your insights, or update articles with valuable information, your input is not only welcome but encouraged.

Thank you for joining me on this journey, and I look forward to building a community of like-minded individuals passionate about sharing knowledge and experiences. Feel free to explore, connect, and contribute as we embark on this collective venture together.

STM32 Memory Sections Layout

arm-none-eabi-objdump -h blinky.elf

blinky.elf: file format elf32-littlearm

Sections:
Idx Name Size VMA LMA File off Algn
0 .isr_vector 00000188 08000000 08000000 00010000 20 CONTENTS, ALLOC, LOAD, READONLY, DATA 1 .text 00000088 08000188 08000188 00010188 22
CONTENTS, ALLOC, LOAD, READONLY, CODE
2 .comment 00000059 00000000 00000000 00010210 20 CONTENTS, READONLY 3 .ARM.attributes 0000002c 00000000 00000000 00010269 20
CONTENTS, READONLY

Qemu Uart emulation with AVR Atmega 328P

Building

avr-gcc -gdwarf-2 -g3 -DMOCK -DF_CPU=8000000UL -Os -mmcu=atmega328p -o uart.elf uart.c

Emulating

qemu-system-avr -S -s -nographic -serial tcp::4444,server,nowait -machine uno -bios uart.elf

After connecting to tcp exposed uart enter the c command to continue then you will see Hello in the telnet terminal.

Connecting to Uart

telnet localhost 4444

Control + ‘]’ to stop listening to localhost 4444

Connecting to debugger

avr-gdb -ex ‘target remote :1234’ uart.elf

UART Test Code

#define F_CPU 16000000UL
#endif

#include <avr/io.h>
#include <util/delay.h>

#define BAUD 9600
#define MYUBRR F_CPU/16/BAUD-1

void USART_INIT(void) {
    UBRR0H = (MYUBRR >> 8);
    UBRR0L = MYUBRR;
    UCSR0B = (1 << TXEN0) | (1 << RXEN0);
    UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
}

void USART_Transmit(unsigned char data) {
    while(!(UCSR0A & (1 << UDRE0)));
    UDR0 = data;
}

int main(void) {
    USART_INIT();

    while (1) {
        USART_Transmit('H');
        USART_Transmit('e');
        USART_Transmit('l');
        USART_Transmit('l');
        USART_Transmit('o');
        USART_Transmit('\n');
        _delay_ms(1000);
    }

    return 0;
}

AVR Programming Notes

  1. Pinouts
  2. Words of Warning
  3. Adding compiler and flasher to PATH
  4. Compiling and Flashing
    1. Flashing usbasp with avrdude for an Arduino generated hex file with Ctrl+Shift+S to export the build.
    2. Check the fuses original from factory
    3. Setting to 8MHz
      1. After setting
    4. Set fuse bits to enable 4.3V brown-out detection level, to make it safe to work with internal EEPROM,
      1. Settings of set of three with 16mHz oscillators
      2. Set clock to 8mHz with 4.3V Brown out Detection
  5. Quickly testing hardware with Arduino with libraries.
    1. Saving money on Arduino boards

Pinouts

Words of Warning

  • Arduino is 5V, using Atmega328P directly can be ran at 3.3V but not all peripheral may be supported at that level such as the built in EEPROM which requires at least as much as 4.3V.
  • A word of caution on running at 5V beware that most other modern mcu are only 3.3V so you will need to use a 3.3V to 5V logic level shifter to interface between the two. This may apply to some sensors as well.

Adding compiler and flasher to PATH

The most pain free way to install the compiler avr-gcc and the flasher avr-dude, is to just install Arduino IDE.

For avr-gcc add the following to you PATH

C:\Users\ckoch\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7\bin

For avrdude add the following to your PATH

C:\Users\ckoch\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17\bin

Compiling and Flashing

Flashing usbasp with avrdude for an Arduino generated hex file with Ctrl+Shift+S to export the build.

❯ .\avrdude -c usbasp -p m328p -B 125 -U flash:w:C:\Users\ckoch\OneDrive\Documents\Arduino\bossButton\build\arduino.avr.uno\bossButton.ino.with_bootloader.hex

TODO move this
❯ .\avrdude -c usbasp -p m328p -B 125 -U flash:w:C:\Users\ckoch\OneDrive\Documents\Arduino\Blink\build\arduino.avr.lilypad\Blink.ino.hex

Check the fuses original from factory

❯ .\avrdude -c usbasp -p m328p -U lfuse:r:-:h -U hfuse:r:-:h -U efuse:r:-:h

Error: cannot set sck period; please check for usbasp firmware update

Processing -U lfuse:r:-:h
Reading lfuse memory …
Writing 1 byte to output file
0x62

Processing -U hfuse:r:-:h
Reading hfuse memory …
Writing 1 byte to output file
0xd9

Processing -U efuse:r:-:h
Reading efuse memory …
Writing 1 byte to output file
0xff

Avrdude done. Thank you.

Setting to 8MHz

❯ .\avrdude -c usbasp -p m328p -U lfuse:w:0xE2:m
Error: cannot set sck period; please check for usbasp firmware update
Reading 1 byte for lfuse from input file 0xE2
Writing 1 byte (0xE2) to lfuse, 1 byte written, 1 verified

Avrdude done. Thank you.

After setting

❯ .\avrdude -c usbasp -p m328p -U lfuse:r:-:h
Error: cannot set sck period; please check for usbasp firmware update
Reading lfuse memory …
Writing 1 byte to output file
0xe2

Avrdude done. Thank you.

Set fuse bits to enable 4.3V brown-out detection level, to make it safe to work with internal EEPROM,

to prevent corrupting it from too low of a voltage.

❯ .\avrdude -c usbasp -p m328p -U efuse:w:0xFC:m -B 100kHz
Set SCK frequency to 93750 Hz
Error: cannot set sck period; please check for usbasp firmware update
Reading 1 byte for efuse from input file 0xFC
Writing 1 byte (0xFC) to efuse, 1 byte written, 1 verified

Avrdude done. Thank you.
 ckoch@LAPTOP-G8E9F3LR  ~  Downloads  avrdude-v8.1-windows-x64 
❯ .\avrdude -c usbasp -p m328p -U lfuse:r:-:h -U hfuse:r:-:h -U efuse:r:-:h
Error: cannot set sck period; please check for usbasp firmware update

Processing -U lfuse:r:-:h
Reading lfuse memory …
Writing 1 byte to output file
0xe2

Processing -U hfuse:r:-:h
Reading hfuse memory …
Writing 1 byte to output file
0xd9

Processing -U efuse:r:-:h
Reading efuse memory …
Writing 1 byte to output file
0xfc

________

Settings of set of three with 16mHz oscillators

After hooking up the chip to the oscillator with the 22p capacitors I was able to get the chip working.

.\avrdude -c usbasp -p m328p -U lfuse:r:-:h -U hfuse:r:-:h -U efuse:r:-:h -v
Avrdude version 8.1
Copyright see https://github.com/avrdudes/avrdude/blob/main/AUTHORS

System wide configuration file is C:\Users\ckoch\Downloads\avrdude-v8.1-windows-x64\avrdude.conf

Using port : usb
Using programmer : usbasp
AVR part : ATmega328P
Programming modes : SPM, ISP, HVPP, debugWIRE
Programmer type : usbasp
Description : USBasp ISP and TPI programmer
Error: cannot set sck period; please check for usbasp firmware update

AVR device initialized and ready to accept instructions
Device signature = 1E 95 0F (ATmega328P, ATA6614Q, LGT8F328P)

Processing -U lfuse:r:-:h
Reading lfuse memory …
Writing 1 byte to output file
0xff

Processing -U hfuse:r:-:h
Reading hfuse memory …
Writing 1 byte to output file
0xda

Processing -U efuse:r:-:h
Reading efuse memory …
Writing 1 byte to output file
0xfd

Avrdude done. Thank you.

Set clock to 8mHz with 4.3V Brown out Detection

.\avrdude -c usbasp -p m328p -U lfuse:w:0xe2:m -U hfuse:w:0xd9:m -U efuse:w:0xfc:m
Error: cannot set sck period; please check for usbasp firmware update

Processing -U lfuse:w:0xe2:m
Reading 1 byte for lfuse from input file 0xe2
Writing 1 byte (0xE2) to lfuse, 1 byte written, 1 verified

Processing -U hfuse:w:0xd9:m
Reading 1 byte for hfuse from input file 0xd9
Writing 1 byte (0xD9) to hfuse, 1 byte written, 1 verified

Processing -U efuse:w:0xfc:m
Reading 1 byte for efuse from input file 0xfc
Writing 1 byte (0xFC) to efuse, 1 byte written, 1 verified

Avrdude done. Thank you.

Quickly testing hardware with Arduino with libraries.

If you are having issues with hardware and are limited by time while return is accepted such as Amazon then buy it, start the return asap when running into problems. Install Arduino Libraries for your hardware. Test hardware with library examples to do a quick smoke test of the hardware.

Saving money on Arduino boards

Get third party boards for as low as $7 that get the job done.

Angular CLI workflow

Often when working on using new feature Angular it is best to start out with a small prototype that isolates the feature from the rest of your site.

The advantages to this are:

  1. Able to quickly test the feature independent of your other app code
  2. By isolating the feature we will know that if/when things do not work that it is the feature code and not your existing app code that is the problem.

Workflow:

1.) create a new Angular project with routing

ng new MyProject

2.) Install dependencies

npm install

3.) run project

ng serve

4.) add component(s)

ng generate component my-component

5.) Add component to routes

const routes: Routes =
[
{ path: 'mycomponent', component: MyComponent }
];

RxJS Observables a History

~ infinite async array with methods like; filter, map …

Async Past

  1. Callbacks – function passed as argument to other function. Executed later, when something is done.
  2. Promises – always guaranteed to return single value, result or error. Once resolved handlers get executed.
    • Promises -> Value or Error -> Resolved -> Handlers
  3. Observables – zero to many values over time.
    • Subscribe to observable stream, so that our handler gets called every time a new value is emitted.
    • Need to manage un-subscription manually; if not done properly -> memory leaks.

SingleMultiple
pullFunctionIterator
pushPromisesObservable
Create your own website, free on WordPress.com.Create your own website.
Start free