diff options
Diffstat (limited to '_site/blog')
| -rw-r--r-- | _site/blog/arduino-due/connections.jpeg | bin | 0 -> 29090 bytes | |||
| -rw-r--r-- | _site/blog/arduino-due/index.html | 172 | ||||
| -rw-r--r-- | _site/blog/arduino-due/schematic.png | bin | 0 -> 68688 bytes | |||
| -rw-r--r-- | _site/blog/arduino-due/source.tar.gz | bin | 0 -> 1174 bytes | |||
| -rw-r--r-- | _site/blog/arduino-uno/3v3.Makefile | 46 | ||||
| -rw-r--r-- | _site/blog/arduino-uno/Makefile | 43 | ||||
| -rw-r--r-- | _site/blog/arduino-uno/breadboard.jpeg | bin | 0 -> 54319 bytes | |||
| -rw-r--r-- | _site/blog/arduino-uno/index.html | 139 | ||||
| -rw-r--r-- | _site/blog/arduino-uno/pinout.png | bin | 0 -> 247197 bytes | |||
| -rw-r--r-- | _site/blog/index.html | 129 | ||||
| -rw-r--r-- | _site/blog/mosfet-switches/bjt.png | bin | 0 -> 12838 bytes | |||
| -rw-r--r-- | _site/blog/mosfet-switches/index.html | 173 | ||||
| -rw-r--r-- | _site/blog/mosfet-switches/n_high_side.png | bin | 0 -> 10825 bytes | |||
| -rw-r--r-- | _site/blog/mosfet-switches/p_high_side.png | bin | 0 -> 10724 bytes | |||
| -rw-r--r-- | _site/blog/neo4j-a-star-search/index.html | 371 | ||||
| -rw-r--r-- | _site/blog/suckless-software/index.html | 142 |
16 files changed, 1215 insertions, 0 deletions
diff --git a/_site/blog/arduino-due/connections.jpeg b/_site/blog/arduino-due/connections.jpeg Binary files differnew file mode 100644 index 0000000..081e6d4 --- /dev/null +++ b/_site/blog/arduino-due/connections.jpeg diff --git a/_site/blog/arduino-due/index.html b/_site/blog/arduino-due/index.html new file mode 100644 index 0000000..fee442f --- /dev/null +++ b/_site/blog/arduino-due/index.html @@ -0,0 +1,172 @@ +<!DOCTYPE html> +<html> + <head> + <meta charset="utf-8"> + <title>How to set up ATSAM3X8E microcontrollers for bare-metal programming in C</title> + + <head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <title>How to set up ATSAM3X8E microcontrollers for bare-metal programming in C</title> + <link rel="stylesheet" href="/assets/css/main.css"> + <link rel="stylesheet" href="/assets/css/skeleton.css"> +</head> + + + + </head> + <body> + + <div id="nav-container" class="container"> + <ul id="navlist" class="left"> + + <li > + <a href="/" class="link-decor-none">hme</a> + </li> + <li class="active"> + <a href="/blog/" class="link-decor-none">blg</a> + </li> + <li > + <a href="/projects/" class="link-decor-none">poc</a> + </li> + <li > + <a href="/about/" class="link-decor-none">abt</a> + </li> + <li><a href="/feed.xml" class="link-decor-none">rss</a></li> + </ul> +</div> + + + + <main> + <div class="container"> + <div class="container-2"> + <h2 class="center" id="title">HOW TO SET UP ATSAM3X8E MICROCONTROLLERS FOR BARE-METAL PROGRAMMING IN C</h2> + <h6 class="center">05 OCTOBER 2024</h5> + <br> + <div class="twocol justify"><p>This article is a step-by-step guide for programming bare-metal ATSAM3X8E chips +found on Arduino Due boards. It also includes notes on the chip’s memory layout +relevant for writing linker scripts. The steps described in this article were +tested on an OpenBSD workstation.</p> + +<h2 id="toolchain">Toolchain</h2> + +<p>To interact directly with a bare-metal ATSAM3X8E chips, we must bypass the +embedded bootloader. To do that, we need a hardware programmer capable of +communicating with the chip over the Serial Wire Debug (SWD) protocol. Since +the workstation we upload the program from presumably doesn’t speak SWD, the +hardware programmer acts as a SWD-USB adapter. The <a href="https://www.st.com/en/development-tools/st-link-v2.html" class="external" target="_blank" rel="noopener noreferrer">ST-LINK/V2</a> programmer fits this +bill.</p> + +<p>The <a href="https://openocd.org/" class="external" target="_blank" rel="noopener noreferrer">OpenOCD</a> on-chip debugger software supports +ATSAM3X8E chips. OpenOCD, on startup, runs a telnet server that we can connect to +to issue commands to the ATSAM3X8E chip. OpenOCD translates plain-text commands +into the binary sequences the chip understands, and sends them over the wire.</p> + +<p>Finally, we need the <a href="https://developer.arm.com/Tools%20and%20Software/GNU%20Toolchain" class="external" target="_blank" rel="noopener noreferrer">ARM GNU Compiler +Toolchain</a> to compile C programs for the chip. The ARM GNU compiler +toolchain and OpenOCD, as a consequence of being free software, are available +on every conceivable platform, including OpenBSD.</p> + +<h2 id="electrical-connections">Electrical connections</h2> + +<p>The following photos illustrate the electrical connections between the Arduino +Due, PC, and the ST-LINK/V2 programmer required to transfer a compiled program +from a PC to the MCU.</p> + +<table style="border: none; width: 100%;"> + <tr style="border: none;"> + <td style="border: none; width: 50%; vertical-align: top; background-color: transparent;"> + <img src="schematic.png" alt="Pinout" style="width: 100%" /> + <p style="text-align: center;">Wiring</p> + </td> + <td style="border: none; width: 50%; vertical-align: top; background-color: transparent;"> + <img src="connections.jpeg" alt="Circuit" style="width: 100%" /> + <p style="text-align: center;">Arduino Due</p> + </td> + </tr> +</table> + +<p>Arduino Due exposes the ATSAM3X8E’s SWD interface via its DEBUG port. The +ST-LINK/v2 programmer connects to that to communicate with the chip.</p> + +<h2 id="uploading-the-program">Uploading the program</h2> + +<p>The source.tar.gz tarball at the end of this page contains a sample C program +(the classic LED blink program) with OpenOCD configuration and linker scripts. +First, use the following command to build it:</p> + +<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$ arm-none-eabi-gcc -mcpu=cortex-m3 -mthumb -T script.ld \ + -nostartfiles \ + -nostdlib \ + -o a.elf main.c +</code></pre></div></div> + +<p>Then, open a telnet session with OpenOCD and issue the following sequence of +commands to configure the chip and upload the compiled program to it:</p> + +<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$ openocd -f openocd-due.cfg +$ telnet localhost 4444 + > halt + > at91sam3 gpnvm show + > at91sam3 gpnvm set 1 + > at91sam3 gpnvm show +$ openocd -f openocd-due.cfg -c "program a.elf verify reset exit" +</code></pre></div></div> + +<p>The first of the above commands starts OpenOCD. In the telnet session, the +first command halts the chip in preparation for receiving commands. Next, we +inspect the current GPNVM bit setting (more on this later). If the bit is unset +(the gpnvm show command returns 0), we set it to 1 and verify the update.</p> + +<p>The final command, issued from outside the telnet session, uploads the program +to the chip. Those are the bare minimum set of commands required to program the +chip. The AT91SAM3 flash driver section of the OpenOCD manual lists all +available commands for the ATSAM3X8E chip.</p> + +<h2 id="gpnvm-bits">GPNVM bits</h2> + +<p>By design, ARM chips boot into address 0x00000. ATSAM3X8E’s memory consists of +a ROM and a dual-banked flash (flash0 and flash1), residing in different +locations of the chip’s address space. The GPNVM bits control which of them +maps to 0x00000. When GPNVM1 is cleared (the default), the chip boots from the ROM, +which contains Atmel’s SAM-BA bootloader.</p> + +<p>Conversely, when the GPNVM1 bit is 1 (and the GPNVM2 bit is 0), flash0 at +address 0x80000 maps to 0x00000. When both GPNVM bits are 0, flash1 maps to +0x00000. Since we place our program in flash0 in the linker script, we set the +GPNVM1 bit and leave the GPNVM2 bit unchanged to ensure the chip +executes our program instead of the embedded bootloader at startup.</p> + +<h2 id="linker-script">Linker script</h2> + +<p>At a minimum, the linker script must place the vector table at the first +address of the flash. This is mandatory for ARM chips unless we relocate the +vector table using the VTOR register.</p> + +<p>The first entry of the vector table must be the stack pointer. The stack +pointer must be initializes to the highest memory location available to +accommodate the ATSAM3X8E’s descending stack.</p> + +<p>The second entry of the vector table must be the reset vector. In the reset +vector, we can perform tasks such as zeroing out memory and initializing +registers before passing control to the main program.</p> + +<p>Files: <a href="source.tar.gz">source.tar.gz</a></p> +</div> + <p class="post-author right">by W. D. Sadeep Madurange</p> + </div> + </div> + </main> + + <div class="footer"> + <div class="container"> + <div class="twelve columns right container-2"> + <p id="footer-text">© ASCIIMX - 2025</p> + </div> + </div> +</div> + + + </body> +</html> diff --git a/_site/blog/arduino-due/schematic.png b/_site/blog/arduino-due/schematic.png Binary files differnew file mode 100644 index 0000000..62ddadd --- /dev/null +++ b/_site/blog/arduino-due/schematic.png diff --git a/_site/blog/arduino-due/source.tar.gz b/_site/blog/arduino-due/source.tar.gz Binary files differnew file mode 100644 index 0000000..496567b --- /dev/null +++ b/_site/blog/arduino-due/source.tar.gz diff --git a/_site/blog/arduino-uno/3v3.Makefile b/_site/blog/arduino-uno/3v3.Makefile new file mode 100644 index 0000000..4ca89d4 --- /dev/null +++ b/_site/blog/arduino-uno/3v3.Makefile @@ -0,0 +1,46 @@ +CC = avr-gcc +MCU = atmega328p +PORT = /dev/cuaU0 +TARGET = app + +SRC = main.c +OBJ = $(SRC:.c=.o) + +CFLAGS = -std=gnu99 +CFLAGS += -Os +CFLAGS += -Wall +CFLAGS += -mmcu=$(MCU) +CFLAGS += -DBAUD=57600 +CFLAGS += -DF_CPU=8000000UL +CFLAGS += -ffunction-sections -fdata-sections + +LDFLAGS = -mmcu=$(MCU) +LDFLAGS += -Wl,--gc-sections + +HEX_FLAGS = -O ihex +HEX_FLAGS += -j .text -j .data + +AVRDUDE_FLAGS = -p $(MCU) +AVRDUDE_FLAGS += -c arduino +AVRDUDE_FLAGS += -b 57600 +AVRDUDE_FLAGS += -P $(PORT) +AVRDUDE_FLAGS += -D -U + +%.o: %.c + $(CC) $(CFLAGS) -c -o $@ $< + +elf: $(OBJ) + $(CC) $(LDFLAGS) $(OBJ) -o $(TARGET).elf + +hex: elf + avr-objcopy $(HEX_FLAGS) $(TARGET).elf $(TARGET).hex + +upload: hex + avrdude $(AVRDUDE_FLAGS) flash:w:$(TARGET).hex:i + +.PHONY: clean + +clean: + rm -f *.o *.elf *.hex + + diff --git a/_site/blog/arduino-uno/Makefile b/_site/blog/arduino-uno/Makefile new file mode 100644 index 0000000..9db7b09 --- /dev/null +++ b/_site/blog/arduino-uno/Makefile @@ -0,0 +1,43 @@ +CC = avr-gcc +MCU = atmega328p +PORT = /dev/cuaU0 +TARGET = app + +SRC = main.c +OBJ = $(SRC:.c=.o) + +CFLAGS = -std=gnu99 +CFLAGS += -Os +CFLAGS += -Wall +CFLAGS += -mmcu=$(MCU) +CFLAGS += -DBAUD=115200 +CFLAGS += -DF_CPU=16000000UL +CFLAGS += -ffunction-sections -fdata-sections + +LDFLAGS = -mmcu=$(MCU) +LDFLAGS += -Wl,--gc-sections + +HEX_FLAGS = -O ihex +HEX_FLAGS += -j .text -j .data + +AVRDUDE_FLAGS = -p $(MCU) +AVRDUDE_FLAGS += -c arduino +AVRDUDE_FLAGS += -P $(PORT) +AVRDUDE_FLAGS += -D -U + +%.o: %.c + $(CC) $(CFLAGS) -c -o $@ $< + +elf: $(OBJ) + $(CC) $(LDFLAGS) $(OBJ) -o $(TARGET).elf + +hex: elf + avr-objcopy $(HEX_FLAGS) $(TARGET).elf $(TARGET).hex + +upload: hex + avrdude $(AVRDUDE_FLAGS) flash:w:$(TARGET).hex:i + +.PHONY: clean + +clean: + rm *.o *.elf *.hex diff --git a/_site/blog/arduino-uno/breadboard.jpeg b/_site/blog/arduino-uno/breadboard.jpeg Binary files differnew file mode 100644 index 0000000..bd74907 --- /dev/null +++ b/_site/blog/arduino-uno/breadboard.jpeg diff --git a/_site/blog/arduino-uno/index.html b/_site/blog/arduino-uno/index.html new file mode 100644 index 0000000..7c4a71b --- /dev/null +++ b/_site/blog/arduino-uno/index.html @@ -0,0 +1,139 @@ +<!DOCTYPE html> +<html> + <head> + <meta charset="utf-8"> + <title>How to configure ATmega328P microcontrollers to run at 3.3V and 5V</title> + + <head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <title>How to configure ATmega328P microcontrollers to run at 3.3V and 5V</title> + <link rel="stylesheet" href="/assets/css/main.css"> + <link rel="stylesheet" href="/assets/css/skeleton.css"> +</head> + + + + </head> + <body> + + <div id="nav-container" class="container"> + <ul id="navlist" class="left"> + + <li > + <a href="/" class="link-decor-none">hme</a> + </li> + <li class="active"> + <a href="/blog/" class="link-decor-none">blg</a> + </li> + <li > + <a href="/projects/" class="link-decor-none">poc</a> + </li> + <li > + <a href="/about/" class="link-decor-none">abt</a> + </li> + <li><a href="/feed.xml" class="link-decor-none">rss</a></li> + </ul> +</div> + + + + <main> + <div class="container"> + <div class="container-2"> + <h2 class="center" id="title">HOW TO CONFIGURE ATMEGA328P MICROCONTROLLERS TO RUN AT 3.3V AND 5V</h2> + <h6 class="center">10 APRIL 2025</h5> + <br> + <div class="twocol justify"><p>This is a quick reference for wiring up ATmega328P ICs to run at 5V and 3.3V. +While the 5V configuration is common, the 3.3V configuration can be useful in +low-power applications and when interfacing with parts that themselves run at +3.3V. In this guide, the 5V setup is configured with a 16MHz crystal +oscillator, while the 3.3V configuration makes use of an 8MHz crystal +oscillator.</p> + +<p>The steps that follow refer to the following pinout.</p> + +<table style="border: none; width: 100%;"> + <tr style="border: none;"> + <td style="border: none; width: 50%; vertical-align: top;"> + <img src="pinout.png" alt="Pinout" style="width: 100%" /> + <p style="text-align: center;">Pinout</p> + </td> + <td style="border: none; width: 50%; vertical-align: top;"> + <img src="breadboard.jpeg" alt="Circuit" style="width: 100%" /> + <p style="text-align: center;">Breadboard</p> + </td> + </tr> +</table> + +<h2 id="5v-16mhz-configuration">5V-16MHz configuration</h2> + +<p>Powering ATmega328P microcontrollers with 5V is the most common setup. This is +also how Arduino Uno boards are wired.</p> + +<p>In this configuration, the microcontroller’s pin 1 is connected to 5V via a +10kΩ resistor. Pins 9 and 10 are connected to a 16MHz crystal oscillator via +two 22pF capacitors connected to ground. The microcontroller is powered by +connecting pins 7, 20, and 21 to a 5V DC power supply. Lastly, pins 8 and 22 +are connected to ground. In addition to the these connections, which are +required, it’s a good idea to add 0.1μF decoupling capacitors between pins 7, +20, and 21 and ground.</p> + +<p><a href="Makefile">Here’s</a> a sample Makefile for compiling C programs for ATmega328P +microcontrollers using avr-gcc/avrdude toolchain.</p> + +<h2 id="33v-8mhz-configuration">3.3V-8MHz configuration</h2> + +<p>Electrical connections for running an ATmega328P at 3.3V are identical to that +of the 5V circuit. The only differences are that all the 5V connections are +replaced with a 3.3V power source and a 8MHz crystal oscillator takes the place +of the 16MHz crystal.</p> + +<p>However, standard ATmega328P chips are preconfigured to run at 5V. To run one +at 3.3V, we must first modify its fuses that control characteristics like the +BOD level. If a bootloader that expects a 16MHz clock (e.g., Arduino +bootloader) is pre-installed on the ATmega328P, it must be swapped with one +that accepts an 8MHz clock. To accomplish that, we need an in-system programmer +(ISP).</p> + +<p>Fortunately, we can turn an ordinary Arduino Uno board into an ISP by uploading +the ‘ArduinoISP’ sketch found in the Arduino IDE. The ISP communicates with the +microcontroller using a Serial Peripheral Interface (SPI). So, connect the SPI +port of the ATmega328P to that of the Arduino Uno, and the Uno’s SS pin +to the ATmega328P’s RESET pin.</p> + +<p>Power up the the ATmega328P by connecting its V<sub>CC</sub> to a 5V supply (we +can use Arduino Uno’s 5V pin). From the Arduino IDE, select ‘ATmega328P (3.3V, +8MHz)’ for processor from the tools menu. Also from the tools menu, select +‘Arduino as ISP’ as programmer. Finally, upload the new bootloader by selecting +‘Burn Bootloader’ from the tools menu.</p> + +<p>The ATmega328P is now ready to run at 8MHz with a 3.3V power supply. You can +upload programs to the ATmega328P as you normally would using avrdude. +<a href="3v3.Makefile">Here’s</a> a sample Makefile with adjusted parameters (e.g., baud +rate) for an 8MHz clock.</p> + +<h2 id="remarks">Remarks</h2> + +<p>In both configurations, if you intend to use the ATmega328P’s analog-to-digital +converter with the internal 1.1V or AV<sub>cc</sub> voltage as reference, do +not connect AREF (pin 21) to V<sub>cc</sub>. Refer to section 23.5.2 in the +datasheet for more information.</p> + +</div> + <p class="post-author right">by W. D. Sadeep Madurange</p> + </div> + </div> + </main> + + <div class="footer"> + <div class="container"> + <div class="twelve columns right container-2"> + <p id="footer-text">© ASCIIMX - 2025</p> + </div> + </div> +</div> + + + </body> +</html> diff --git a/_site/blog/arduino-uno/pinout.png b/_site/blog/arduino-uno/pinout.png Binary files differnew file mode 100644 index 0000000..59acfbc --- /dev/null +++ b/_site/blog/arduino-uno/pinout.png diff --git a/_site/blog/index.html b/_site/blog/index.html new file mode 100644 index 0000000..c213ee5 --- /dev/null +++ b/_site/blog/index.html @@ -0,0 +1,129 @@ +<!doctype html> +<html lang="en-us"> + + <head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <title>Blog</title> + <link rel="stylesheet" href="/assets/css/main.css"> + <link rel="stylesheet" href="/assets/css/skeleton.css"> +</head> + + + + <body> + + <div id="nav-container" class="container"> + <ul id="navlist" class="left"> + + <li > + <a href="/" class="link-decor-none">hme</a> + </li> + <li class="active"> + <a href="/blog/" class="link-decor-none">blg</a> + </li> + <li > + <a href="/projects/" class="link-decor-none">poc</a> + </li> + <li > + <a href="/about/" class="link-decor-none">abt</a> + </li> + <li><a href="/feed.xml" class="link-decor-none">rss</a></li> + </ul> +</div> + + + + <main class="container" id="main"><div class="container"> + + <h2>Blog</h2> + + <div class="articles"> + <table class="posts-table"> + + + + + <tr> + <td class="posts-td posts-td-link"> + <a href="/blog/suckless-software/" class="link-decor-none">How I manage Suckless software packages</a> + </td> + <td class="posts-td posts-td-time"> + <span class="post-meta"> + <time datetime="2025-11-30 00:00:00 +0800">2025-11-30</time> + </span> + </td> + </tr> + + + + <tr> + <td class="posts-td posts-td-link"> + <a href="/blog/neo4j-a-star-search/" class="link-decor-none">Neo4J A* search</a> + </td> + <td class="posts-td posts-td-time"> + <span class="post-meta"> + <time datetime="2025-09-14 00:00:00 +0800">2025-09-14</time> + </span> + </td> + </tr> + + + + <tr> + <td class="posts-td posts-td-link"> + <a href="/blog/mosfet-switches/" class="link-decor-none">MOSFETs as electronic switches</a> + </td> + <td class="posts-td posts-td-time"> + <span class="post-meta"> + <time datetime="2025-06-22 00:00:00 +0800">2025-06-22</time> + </span> + </td> + </tr> + + + + <tr> + <td class="posts-td posts-td-link"> + <a href="/blog/arduino-uno/" class="link-decor-none">How to configure ATmega328P microcontrollers to run at 3.3V and 5V</a> + </td> + <td class="posts-td posts-td-time"> + <span class="post-meta"> + <time datetime="2025-04-10 00:00:00 +0800">2025-04-10</time> + </span> + </td> + </tr> + + + + <tr> + <td class="posts-td posts-td-link"> + <a href="/blog/arduino-due/" class="link-decor-none">How to set up ATSAM3X8E microcontrollers for bare-metal programming in C</a> + </td> + <td class="posts-td posts-td-time"> + <span class="post-meta"> + <time datetime="2024-10-05 00:00:00 +0800">2024-10-05</time> + </span> + </td> + </tr> + + + + </table> + </div> + + +</div> +</main> + + <div class="footer"> + <div class="container"> + <div class="twelve columns right container-2"> + <p id="footer-text">© ASCIIMX - 2025</p> + </div> + </div> +</div> + + + </body> +</html> diff --git a/_site/blog/mosfet-switches/bjt.png b/_site/blog/mosfet-switches/bjt.png Binary files differnew file mode 100644 index 0000000..9858fa7 --- /dev/null +++ b/_site/blog/mosfet-switches/bjt.png diff --git a/_site/blog/mosfet-switches/index.html b/_site/blog/mosfet-switches/index.html new file mode 100644 index 0000000..ed63e0a --- /dev/null +++ b/_site/blog/mosfet-switches/index.html @@ -0,0 +1,173 @@ +<!DOCTYPE html> +<html> + <head> + <meta charset="utf-8"> + <title>MOSFETs as electronic switches</title> + + <head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <title>MOSFETs as electronic switches</title> + <link rel="stylesheet" href="/assets/css/main.css"> + <link rel="stylesheet" href="/assets/css/skeleton.css"> +</head> + + + + </head> + <body> + + <div id="nav-container" class="container"> + <ul id="navlist" class="left"> + + <li > + <a href="/" class="link-decor-none">hme</a> + </li> + <li class="active"> + <a href="/blog/" class="link-decor-none">blg</a> + </li> + <li > + <a href="/projects/" class="link-decor-none">poc</a> + </li> + <li > + <a href="/about/" class="link-decor-none">abt</a> + </li> + <li><a href="/feed.xml" class="link-decor-none">rss</a></li> + </ul> +</div> + + + + <main> + <div class="container"> + <div class="container-2"> + <h2 class="center" id="title">MOSFETS AS ELECTRONIC SWITCHES</h2> + <h6 class="center">22 JUNE 2025</h5> + <br> + <div class="twocol justify"><p>Recently, I needed a low-power circuit for one of my battery-operated projects. +Much of the system’s power savings depended on its ability to electronically +switch off components, such as servos, that draw high levels of quiescent +currents. My search for a solution led me to MOSFETs, transistors capable of +controlling circuits operating at voltages far above their own.</p> + +<h2 id="acknowledgments">Acknowledgments</h2> + +<p>This article is a summary of what I learnt about using MOSFETs as switches. +I’m not an electronics engineer, and this is not an authoritative guide. The +circuits in this post must be considered within the narrow context in which +I’ve used them. All credits for the schematics belong to <a href="https://electronics.stackexchange.com/users/292884/simon-fitch" class="external" target="_blank" rel="noopener noreferrer">Simon Fitch</a>.</p> + +<h2 id="preamble">Preamble</h2> + +<p>For a typical MOSFET-based switch, we can connect a GPIO pin of a +microcontroller to the gate of a logic-level N-channel MOSFET placed on the low +side of the load and tie the gate and the drain pins of the MOSFET with a +pull-down resistor. This would work as long as the power supplies of the +microcontroller and the load don’t share a common ground. Things become more +complicated when they do (e.g., controlling power to a component driven by the +same microcontroller).</p> + +<p>In that scenario, the source potential visible to the load is the difference +between the gate and the threshold potentials of the MOSFET. For example, when +the gate and the threshold potentials are 3.3 V and 1.5 V, the potential the +load sees is 1.8 V. So, to use a low-side N-channel MOSFET, we need the gate +potential to be higher than the source potential, which may not always be +practical. The alternative would be a hide-side switch.</p> + +<h2 id="p-channel-high-side-switch">P-channel high-side switch</h2> + +<p>The following schematic shows how a high-side P-channel MOSFET (M1) could +switch power to a 6 V servo driven by a 3.3 V MCU.</p> + +<p><img src="p_high_side.png" alt="P-channel high-side switching circuit" /></p> + +<p>When the microcontroller outputs low, the M2 N-channel MOSFET stops conducting. +The R1 resistor pulls the gate of the M1 P-channel MOSFET up to +6 V, switching +the servo off. When the microcontroller outputs high on the GPIO pin, M2’s +source-drain connection starts conducting, causing M1’s gate potential to drop +to 0 V, which switches on power to the servo.</p> + +<h2 id="n-channel-high-side-switch">N-channel high-side switch</h2> + +<p>The P-channel high-side switch would be the typical architecture for our use +case. However, if we have access to a potential high enough to safely raise the +gate potential above the threshold such that their difference outputs the source +potential required to drive the load, we can switch on the high side using an +N-channel MOSFET:</p> + +<p><img src="n_high_side.png" alt="N-channel high-side switching circuit" /></p> + +<p>In the schematic, both M1 and M2 are N-channel MOSFETs. When the +microcontroller output is low, M2 stops conducting. This causes the M1’s gate +potential to rise above the threshold, turning the servo on. Conversely, a high +output on the GPIO line switches M2 on, which lowers M1’s gate potential. This +switches the servo off. The R2 pull-up resistor prevents the high impedance of +the output pins at power-up from switching the servo on.</p> + +<p>Both topologies require M2 to act as a level converter between circuits +containing the microcontroller and the servo, converting between 0 V and +6 V +or +9 V. M2 is a low-power signal converter carrying less than a milliamp of +current. The gate-source threshold voltage of M2 must be lower than the MCU’s +supply voltage. 2N7000, 2N7002, and BSS138 are popular choices for M2.</p> + +<p>The D1 flyback diodes used in the two topologies safeguard the MOSFET from +voltage spikes caused by inductive loads such as servos.</p> + +<h2 id="a-bjt-alternative">A BJT alternative</h2> + +<p>A Bipolar Junction Transistor (BJT) is a simpler, cheaper, and more widely +available type of transistor that can be used as a switch.</p> + +<p><img src="bjt.png" alt="BJT architecture" /></p> + +<p>In the schematic, when the MCU outputs high, Q2 starts conducting. Q2 amplifies +Q1’s base current. Unlike MOSFETs, which are voltage-driven, BJTs are driven by +base current. Resistors R3 and R4 must be chosen carefully to output the +desired base currents. <a href="https://teachmetomake.wordpress.com/how-to-use-a-transistor-as-a-switch/" class="external" target="_blank" rel="noopener noreferrer">“How to choose a +transistor as a switch”</a> is an excellent guide on using BJTs as electronic +switches.</p> + +<h2 id="which-topology-to-choose">Which topology to choose?</h2> + +<p>The professional community appears to prefer MOSFETs over BJTs. MOSFETs are +more efficient when the switch is on. However, they are more challenging to +drive, especially with a 3.3 V MCU, due to the V<sub>GS</sub> potentials +required to achieve specified R<sub>DS(on)</sub> values (i.e., to turn them on +fully).</p> + +<p>N-channel MOSFETs have lower on-resistance values, making them more efficient +than P-channel ones. They are also cheaper. However, they are harder to drive +on the high side as their gate potential must be higher than the source +potential. This often requires extra circuitry such as MOSFET drivers.</p> + +<h2 id="further-reading">Further reading</h2> + +<ul> + <li><a href="https://www.embeddedrelated.com/showarticle/98.php" class="external" target="_blank" rel="noopener noreferrer">Different MOSFET +topologies</a></li> + <li><a href="https://www.embeddedrelated.com/showarticle/809.php" class="external" target="_blank" rel="noopener noreferrer">How to read +MOSFET datasheets</a></li> + <li><a src="https://teachmetomake.wordpress.com/how-to-use-a-transistor-as-a-switch/" class="external" target="_blank" rel="noopener noreferrer">How to use a +transistor as a switch</a></li> + <li><a src="https://forum.digikey.com/t/guide-to-selecting-and-controlling-a-mosfet-for-3-3-vdc-logic-applications/42606" class="external" target="_blank" rel="noopener noreferrer">Guide to +selecting and controlling a MOSFET for 3.3 VDC logic applications</a></li> + <li><a src="https://forum.digikey.com/t/driving-a-large-relay-from-a-3-3-vdc-microcontroller-using-an-npn-darlington-transistor/41751" class="external" target="_blank" rel="noopener noreferrer">Driving a large +relay from a 3.3 VDC microcontroller using an NPN Darlington transistor</a></li> +</ul> +</div> + <p class="post-author right">by W. D. Sadeep Madurange</p> + </div> + </div> + </main> + + <div class="footer"> + <div class="container"> + <div class="twelve columns right container-2"> + <p id="footer-text">© ASCIIMX - 2025</p> + </div> + </div> +</div> + + + </body> +</html> diff --git a/_site/blog/mosfet-switches/n_high_side.png b/_site/blog/mosfet-switches/n_high_side.png Binary files differnew file mode 100644 index 0000000..c851768 --- /dev/null +++ b/_site/blog/mosfet-switches/n_high_side.png diff --git a/_site/blog/mosfet-switches/p_high_side.png b/_site/blog/mosfet-switches/p_high_side.png Binary files differnew file mode 100644 index 0000000..9f5397a --- /dev/null +++ b/_site/blog/mosfet-switches/p_high_side.png diff --git a/_site/blog/neo4j-a-star-search/index.html b/_site/blog/neo4j-a-star-search/index.html new file mode 100644 index 0000000..0d918c7 --- /dev/null +++ b/_site/blog/neo4j-a-star-search/index.html @@ -0,0 +1,371 @@ +<!DOCTYPE html> +<html> + <head> + <meta charset="utf-8"> + <title>Neo4J A* search</title> + + <head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <title>Neo4J A* search</title> + <link rel="stylesheet" href="/assets/css/main.css"> + <link rel="stylesheet" href="/assets/css/skeleton.css"> +</head> + + + + </head> + <body> + + <div id="nav-container" class="container"> + <ul id="navlist" class="left"> + + <li > + <a href="/" class="link-decor-none">hme</a> + </li> + <li class="active"> + <a href="/blog/" class="link-decor-none">blg</a> + </li> + <li > + <a href="/projects/" class="link-decor-none">poc</a> + </li> + <li > + <a href="/about/" class="link-decor-none">abt</a> + </li> + <li><a href="/feed.xml" class="link-decor-none">rss</a></li> + </ul> +</div> + + + + <main> + <div class="container"> + <div class="container-2"> + <h2 class="center" id="title">NEO4J A* SEARCH</h2> + <h6 class="center">14 SEPTEMBER 2025</h5> + <br> + <div class="twocol justify"><p>Back in 2018, we used <a href="https://neo4j.com/" class="external" target="_blank" rel="noopener noreferrer">Neo4J</a> graph database to track the +movement of marine vessels. We were interested in the shortest path a ship +could take through a network of about 13,000 route points. Algorithms based on +graph theory, such as A* search, provide optimal solutions to such problems. +In other words, the set of route points lends itself well to a model based on +graphs.</p> + +<p>A graph is a finite set of vertices, and a subset of vertex pairs (edges). +Edges can have weights. In the case of vessel tracking, the route points form +the vertices of a graph; the routes between them, the edges; and the distances +between them are the weights. For different reasons, people are interested in +minimizing (or maximizing) the weight of a path through a set of vertices. For +instance, we may want to find the shortest path between two ports.</p> + +<p>Given such a graph, an algorithm like Dijkstra’s search could compute the +shortest path between two vertices. In fact, this was the algorithm Neo4J +shipped with at the time. One drawback of Dijkstra’s algorithm is that it +computes all the shortest paths from the source to all other vertices before +terminating at the destination vertex. The exhaustive nature of this search +limited our search to about 4,000 route points.</p> + +<p>The following enhancement to Dijkstra’s search, also known as the A* search, +employs a heuristic to steer the search in the direction of the destination +more quickly. In the case of our network of vessels, which are on the earth’s +surface, spherical distance is a good candidate for a heuristic:</p> + +<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>package org.neo4j.graphalgo.impl; + +import java.util.stream.Stream; +import java.util.stream.StreamSupport; + +import org.neo4j.graphalgo.api.Graph; +import org.neo4j.graphalgo.core.utils.ProgressLogger; +import org.neo4j.graphalgo.core.utils.queue.IntPriorityQueue; +import org.neo4j.graphalgo.core.utils.queue.SharedIntPriorityQueue; +import org.neo4j.graphalgo.core.utils.traverse.SimpleBitSet; +import org.neo4j.graphdb.Direction; +import org.neo4j.graphdb.Node; +import org.neo4j.kernel.internal.GraphDatabaseAPI; + +import com.carrotsearch.hppc.IntArrayDeque; +import com.carrotsearch.hppc.IntDoubleMap; +import com.carrotsearch.hppc.IntDoubleScatterMap; +import com.carrotsearch.hppc.IntIntMap; +import com.carrotsearch.hppc.IntIntScatterMap; + +public class ShortestPathAStar extends Algorithm<ShortestPathAStar> { + + private final GraphDatabaseAPI dbService; + private static final int PATH_END = -1; + + private Graph graph; + private final int nodeCount; + private IntDoubleMap gCosts; + private IntDoubleMap fCosts; + private double totalCost; + private IntPriorityQueue openNodes; + private IntIntMap path; + private IntArrayDeque shortestPath; + private SimpleBitSet closedNodes; + private final ProgressLogger progressLogger; + + public static final double NO_PATH_FOUND = -1.0; + + public ShortestPathAStar( + final Graph graph, + final GraphDatabaseAPI dbService) { + + this.graph = graph; + this.dbService = dbService; + + nodeCount = Math.toIntExact(graph.nodeCount()); + gCosts = new IntDoubleScatterMap(nodeCount); + fCosts = new IntDoubleScatterMap(nodeCount); + openNodes = SharedIntPriorityQueue.min( + nodeCount, + fCosts, + Double.MAX_VALUE); + path = new IntIntScatterMap(nodeCount); + closedNodes = new SimpleBitSet(nodeCount); + shortestPath = new IntArrayDeque(); + progressLogger = getProgressLogger(); + } + + public ShortestPathAStar compute( + final long startNode, + final long goalNode, + final String propertyKeyLat, + final String propertyKeyLon, + final Direction direction) { + + reset(); + + final int startNodeInternal = + graph.toMappedNodeId(startNode); + final double startNodeLat = + getNodeCoordinate(startNodeInternal, propertyKeyLat); + final double startNodeLon = + getNodeCoordinate(startNodeInternal, propertyKeyLon); + + final int goalNodeInternal = + graph.toMappedNodeId(goalNode); + final double goalNodeLat = + getNodeCoordinate(goalNodeInternal, propertyKeyLat); + final double goalNodeLon = + getNodeCoordinate(goalNodeInternal, propertyKeyLon); + + final double initialHeuristic = + computeHeuristic(startNodeLat, + startNodeLon, + goalNodeLat, + goalNodeLon); + + gCosts.put(startNodeInternal, 0.0); + fCosts.put(startNodeInternal, initialHeuristic); + openNodes.add(startNodeInternal, 0.0); + + run(goalNodeInternal, + propertyKeyLat, + propertyKeyLon, + direction); + + if (path.containsKey(goalNodeInternal)) { + totalCost = gCosts.get(goalNodeInternal); + int node = goalNodeInternal; + while (node != PATH_END) { + shortestPath.addFirst(node); + node = path.getOrDefault(node, PATH_END); + } + } + return this; + } + + private void run( + final int goalNodeId, + final String propertyKeyLat, + final String propertyKeyLon, + final Direction direction) { + + final double goalLat = + getNodeCoordinate(goalNodeId, propertyKeyLat); + final double goalLon = + getNodeCoordinate(goalNodeId, propertyKeyLon); + + while (!openNodes.isEmpty() && running()) { + int currentNodeId = openNodes.pop(); + if (currentNodeId == goalNodeId) { + return; + } + + closedNodes.put(currentNodeId); + + double currentNodeCost = + this.gCosts.getOrDefault( + currentNodeId, + Double.MAX_VALUE); + + graph.forEachRelationship( + currentNodeId, + direction, + (source, target, relationshipId, weight) -> { + double neighbourLat = + getNodeCoordinate(target, propertyKeyLat); + double neighbourLon = + getNodeCoordinate(target, propertyKeyLon); + double heuristic = + computeHeuristic( + neighbourLat, + neighbourLon, + goalLat, + goalLon); + + updateCosts( + source, + target, + weight + currentNodeCost, + heuristic); + + if (!closedNodes.contains(target)) { + openNodes.add(target, 0); + } + return true; + }); + + progressLogger.logProgress( + (double) currentNodeId / (nodeCount - 1)); + } + } + + private double computeHeuristic( + final double lat1, + final double lon1, + final double lat2, + final double lon2) { + + final int earthRadius = 6371; + final double kmToNM = 0.539957; + final double latDistance = Math.toRadians(lat2 - lat1); + final double lonDistance = Math.toRadians(lon2 - lon1); + final double a = Math.sin(latDistance / 2) + * Math.sin(latDistance / 2) + + Math.cos(Math.toRadians(lat1)) + * Math.cos(Math.toRadians(lat2)) + * Math.sin(lonDistance / 2) + * Math.sin(lonDistance / 2); + final double c = 2 + * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); + final double distance = earthRadius * c * kmToNM; + return distance; + } + + private double getNodeCoordinate( + final int nodeId, + final String coordinateType) { + + final long neo4jId = graph.toOriginalNodeId(nodeId); + final Node node = dbService.getNodeById(neo4jId); + return (double) node.getProperty(coordinateType); + } + + private void updateCosts( + final int source, + final int target, + final double newCost, + final double heuristic) { + + final double oldCost = + gCosts.getOrDefault(target, Double.MAX_VALUE); + + if (newCost < oldCost) { + gCosts.put(target, newCost); + fCosts.put(target, newCost + heuristic); + path.put(target, source); + } + } + + private void reset() { + closedNodes.clear(); + openNodes.clear(); + gCosts.clear(); + fCosts.clear(); + path.clear(); + shortestPath.clear(); + totalCost = NO_PATH_FOUND; + } + + public Stream<Result> resultStream() { + return StreamSupport.stream( + shortestPath.spliterator(), false) + .map(cursor -> new Result( + graph.toOriginalNodeId(cursor.value), + gCosts.get(cursor.value))); + } + + public IntArrayDeque getFinalPath() { + return shortestPath; + } + + public double getTotalCost() { + return totalCost; + } + + public int getPathLength() { + return shortestPath.size(); + } + + @Override + public ShortestPathAStar me() { + return this; + } + + @Override + public ShortestPathAStar release() { + graph = null; + gCosts = null; + fCosts = null; + openNodes = null; + path = null; + shortestPath = null; + closedNodes = null; + return this; + } + + public static class Result { + + /** + * the neo4j node id + */ + public final Long nodeId; + + /** + * cost to reach the node from startNode + */ + public final Double cost; + + public Result(Long nodeId, Double cost) { + this.nodeId = nodeId; + this.cost = cost; + } + } +} +</code></pre></div></div> + +<p>The heuristic function is domain-specific. If chosen wisely, it can +significantly speed up the search. In our case, we achieved a 300x speedup, +enabling us to expand our search from 4,000 to 13,000 route points. The <a href="https://github.com/neo4j-contrib/neo4j-graph-algorithms/releases/tag/3.4.0.0" class="external" target="_blank" rel="noopener noreferrer">v3.4.0</a> of the +Neo4J graph algorithms shipped with the A* search algorithm.</p> + +</div> + <p class="post-author right">by W. D. Sadeep Madurange</p> + </div> + </div> + </main> + + <div class="footer"> + <div class="container"> + <div class="twelve columns right container-2"> + <p id="footer-text">© ASCIIMX - 2025</p> + </div> + </div> +</div> + + + </body> +</html> diff --git a/_site/blog/suckless-software/index.html b/_site/blog/suckless-software/index.html new file mode 100644 index 0000000..ea91072 --- /dev/null +++ b/_site/blog/suckless-software/index.html @@ -0,0 +1,142 @@ +<!DOCTYPE html> +<html> + <head> + <meta charset="utf-8"> + <title>How I manage Suckless software packages</title> + + <head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <title>How I manage Suckless software packages</title> + <link rel="stylesheet" href="/assets/css/main.css"> + <link rel="stylesheet" href="/assets/css/skeleton.css"> +</head> + + + + </head> + <body> + + <div id="nav-container" class="container"> + <ul id="navlist" class="left"> + + <li > + <a href="/" class="link-decor-none">hme</a> + </li> + <li class="active"> + <a href="/blog/" class="link-decor-none">blg</a> + </li> + <li > + <a href="/projects/" class="link-decor-none">poc</a> + </li> + <li > + <a href="/about/" class="link-decor-none">abt</a> + </li> + <li><a href="/feed.xml" class="link-decor-none">rss</a></li> + </ul> +</div> + + + + <main> + <div class="container"> + <div class="container-2"> + <h2 class="center" id="title">HOW I MANAGE SUCKLESS SOFTWARE PACKAGES</h2> + <h6 class="center">30 NOVEMBER 2025</h5> + <br> + <div class="twocol justify"><p>Since <a href="https://suckless.org/" class="external" target="_blank" rel="noopener noreferrer">suckless</a> software requires users to modify the +source code and recompile to customize, I need a way to maintain patches over +the long term while retaining the ability to upgrade the software as new +versions are released.</p> + +<h2 id="initial-setup">Initial setup</h2> + +<p>When using a suckless program, I usually begin by cloning the project and +setting the remote URL to push a copy of the source code with my patches to my +own git repository:</p> + +<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>git clone git://git.suckless.org/dwm +git reset --hard <tag> +git remote set-url --push origin git@git.asciimx.com:/repos/dwm +</code></pre></div></div> + +<p>This way, I can pull updates from the upstream project whenever I want, while +committing my changes to my own git repository. The git reset command aligns my +branch head with a stable release before applying patches or installing the +software.</p> + +<p>If all I want to do is reconfigure the software (e.g., change key bindings), +which is what I need most of the time, the recommended approach is to modify +the config.h file. If the config.h isn’t yet in the project, the following +command generates it from the defaults and compiles the software using <code class="language-plaintext highlighter-rouge">make +clean <target></code> here <code class="language-plaintext highlighter-rouge"><target></code> is the name of the application (e.g., dwm) +found in the Makefile. I modify the resulting config.h file and run <code class="language-plaintext highlighter-rouge">make clean +install</code> to install the software before committing and pushing my changes to my +git repo.</p> + +<h2 id="dwm-and-slstatus">dwm and slstatus</h2> + +<p>Since dwm and slstatus are always running, <code class="language-plaintext highlighter-rouge">make install</code> will likely fail for +them. The operating system will prevent the installer from replacing running +executables with new ones. Hence, we must first stop the running instances of +these programs (Mod + Shift + q). Then, switch to a tty (Ctrl + Alt + F1), +log in, and change the directory to where dwm/slstatus is. We can run <code class="language-plaintext highlighter-rouge">make +install</code> to install the software and switch back to the graphical session +(Ctrl + Alt + F5).</p> + +<p>The key combinations for switching to the tty and back may differ across +systems. The ones listed above are for OpenBSD.</p> + +<h2 id="subsequent-upgrades">Subsequent upgrades</h2> + +<p>When suckless releases a new version, I run <code class="language-plaintext highlighter-rouge">git pull --rebase</code> to fetch the +upstream changes and rebase my patches on top of them. Because I tend to use +stable versions, I perform another interactive rebase to drop the commits +between the latest stable version tag and my patch before installing the +software.</p> + +<p>Commit log before upgrading:</p> + +<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>dt236 My patch. +3fkdf Version 6.5. +</code></pre></div></div> + +<p>Commit log after pulling:</p> + +<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>w467d My patch. +gh25g A commit. +g525g Another commit. +3fkdf Version 6.6. +vd425 Old commit. +q12vu Another old commit. +3fkdf Version 6.5. +</code></pre></div></div> + +<p>Commit log after the interactive rebase:</p> + +<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>h57jh My patch. +3fkdf Version 6.6. +vd425 Old commit. +q12vu Another old commit. +3fkdf Version 6.5. +</code></pre></div></div> + +<p>And finally, commit and push all the changes to my own git repository.</p> + +</div> + <p class="post-author right">by W. D. Sadeep Madurange</p> + </div> + </div> + </main> + + <div class="footer"> + <div class="container"> + <div class="twelve columns right container-2"> + <p id="footer-text">© ASCIIMX - 2025</p> + </div> + </div> +</div> + + + </body> +</html> |
