Stephen Smith's Blog

Musings on Machine Learning…

Meet the Sipeed Tang Primer 25K FPGA

leave a comment »

Introduction

Last year, I got interested in FPGAs and purchased a Digilent Basys3, which was lots of fun to play with, I even got a minimal RISC-V processor programmed. The Basys3 is their lower end board and doesn’t have any RAM, limiting what you can do, since it has to use logic elements such as flip-flops to fulfill this purpose. To go further, I saw I could get the Sipeed Tang Primer 25K, inexpensively, containing many more logic elements along with RAM. This board is based on the Gowin 25K Arora FPGA and as such means learning the Gowin development tools rather than the familiar AMD Xilinx Vivado tools. This blog post is a review and some of my experiences using this board, including going down the rabbit hole of FPGA retro gaming.

Initial Setup

I received the Tang Primer 25K FPGA gamer bundle, which included a pair of Playstation compatible controllers that attach to a PMOD to connect to the board. However, this isn’t complete, you still require TFcard, HDMI and RAM PMODs which I needed to order separately. Would have been nice if the gaming bundle included everything required. Note that you need to use the Sipeed PMODs as others such as those from Digilent often are wired up differently. Ordering the collection, gives you a few other things to play with such as LED displays and pushbuttons.

The AMD Vivado development tools are really well documented with lots of tutorials and examples. Sipeed provides a wiki for their products and Gowin provides quite a bit of documentation, but the quality isn’t there and many of the instructions are incorrect. For instance, Sipeed says to use their special copy of the programmer to download programs to the FPGA rather than the Gowin one. However the special one didn’t work for me and I found the latest Gowin one was the correct one to use. It was nice that I could just download what I needed without requiring any interactions with Gowin, no export restrictions, since these are all Chinese companies.

FPGA Retro-Gaming

I previously looked at retro-gaming on the Raspberry Pi where people have written emulators for all sorts of processors and gaming systems. Now there is a new project MiSTer FPGA which emulates the hardware of various gaming consoles for FPGAs. I used the snestang fork which emulates the super-nintendo on various Sipeed Gowin based boards.

I didn’t have much luck initially, since I couldn’t load any games from the SDCard, but with some help from the developer, they made some changes to allow a bit more tolerance in accepting different cards. With that, I could load games, but had trouble starting them. Turns out you need to hit the start button first, hitting random buttons seems to disable the controller to the game.

Anyway, with that I could play games. The Verilog code that implements the Super Nintendo system implements a 65C816 cpu which is a 16-bit version of the 6502. This CPU was also used in Apple’s short-lived IIgs. Besides this CPU, it implements a RISC-V CPU that is used to provide the menu for system options and to choose the game to load, which it then loads from the SDCard. This gives you a basic RISC-V CPU with minimal firmware, HDMI support, SDCard support, controller support along with a menuing system.

This isn’t a bad starting point for playing with RISC-V hardware definitions. It gives you plenty of devices to play with, plus a debugging framework where you can print back to the host computer through the usb port, which can be read by putty.

Summary

The Tang Primer 25K is quite a powerful and capable FPGA at a reasonable price. You really need to get the pack of all the Sipeed PMODs so you have some devices to play with. I found it takes some patience to work with the Gowin tools and for me, with the 25K being fairly new, a lot of the documentation hasn’t kept up. You really rely on the samples to see how devices are connected, so you know how to map them in your program.

I think the FPGA approach to recreating vintage video game hardware is really cool. I just hope this doesn’t get stomped on by companies like Nintendo, who have a history of stepping on people’s fun.

Written by smist08

April 1, 2024 at 6:45 pm

Posted in fpga, RiscV

Tagged with , , , , ,

Which Assembly Language Book is Right for You

with 6 comments

Introduction

I’ve now written four books on Assembly Language programming, three for ARM processors and now one for RISC-V processors. This article explains why there are three books for ARM CPUs and then where people will find RISC-V CPUs.

The Three Instruction Sets for ARM CPUs

The ARM processor started development in 1983 as a 16-bit replacement for the popular 8-bit 6502 used in many popular personal computers such as the Apple II and Commodore PET. This CPU was used in the Acorn computer, mostly sold in the UK. I haven’t written a book on 16-bit ARM Assembly Language, as it is pretty much non-existent in today’s world.

32-Bit ARM

The ARM processor became popular when the 32-bit version was released and Apple chose it as the CPU for the iPod music player. From this start, the ARM processor took over the mobile world being used in most mobile devices. When I wrote “Raspberry Pi Assembly Language Programming”, the Raspberry Pi OS was still a 32-bit operating system, although the CPU in the Raspberry Pi 3 and above was capable of 64-bits. Hence this book is really about 32-bit ARM Assembly Language Programming. Although it does have a chapter on 64-bit Assembly Language, since you could run 64-bit operating systems such as Kali Linux at the time.

64-Bit ARM

After finishing the 32-bit book, I felt a need to write a full 64-bit book. After all, iOS and Android had gone full ARM 64-bit for phones and tablets. Although the Raspberry Pi OS was still 32-bit, the popularity of 64-bit flavors of Linux was growing. The Raspberry Pi 4 finally had enough memory to run 64-bits well when it came out with 4Gig of RAM. So that led to my second book “Programming with 64-Bit ARM Assembly Language” which covers iOS, Android and Raspberry Pi 64-bit ARM Assembly Language Programming.

ARM learned a lot from its experience in the 32-bit world and as a consequence made the 64-bit instruction set quite different than the 32-bit one, addressing a lot of deficiencies and incorporating things that made compilers and performance much better.

Today even the Raspberry Pi OS has finally gone to a full 64-bits, though Raspberry still releases a 32-bit version as well.

32-Bit Thumbs its Nose

ARM holdings tries to make the ARM processor available in all CPU markets. The CPUs used in phones, tablets and Raspberry Pis are fairly high end. ARM also competes in the lower end microcontroller market. These devices are all 32-bit. However, these typically do not run the full 32-bit instruction set, but instead run a reduced version called the Thumb instruction set.

In 2021, Raspberry released their first microcontroller product, the Raspberry Pi Pico. With this they developed their own ARM based microcontroller, the RP2040. This led to my third book “RP2040 Assembly Language Programming”. This covers how to program ARM based microcontrollers in Assembly Language. Raspberry also sells its RP2040 chip to other companies and as a result there are a plethora of RP2040 base microcontrollers on the market today.

RISC-V

Intel/AMD processors dominate the desktop/laptop market and ARM dominates the mobile market. However, both these processors are proprietary and to use their instruction sets requires expensive licensing fees. Enter the RISC-V instruction set, created by professors at Berkeley University, as an open source instruction set that anyone can use free of any licensing fees. Note, that only the ISA is open source, the various hardware implementations of the ISA could be either proprietary or open source.

RISC-V is doing well in the microcontroller market where chips usually cost less than a dollar and built-in licensing fees can make chips uncompetitive. We’re beginning to see RISC-V based SBCs and laptops, although these are all quite low end at this point. I think RISC-V has a good future and with several RISC-V devices on the market to play with, I wrote “RISC-V Assembly Language Programming”.

In the case of ARM, the 32-bit and 64-bit instruction sets are quite different and two books were required. In the case of RISC-V both instruction sets are nearly identical with the main difference being the size of the registers. As a result I cover both 32-bit and 64-bit RISC-V Assembly Language in this one book.

Summary

This was a quick summary of why I’ve written four books on Assembly Language programming. Three for the various flavors of the ARM processor and my new one covering the new RISC-V CPUs.

Written by smist08

March 15, 2024 at 11:24 am

Speaking at the Upcoming LinuxFest NorthWest

leave a comment »

Introduction

LinuxFest Northwest is finally getting up and running again at the Bellingham Technical College on April 26-28, 2024. The last in-person event was held in 2019 which I attended and greatly enjoyed. Then the pandemic hit. The 2020 LinuxFest was held virtually and I participated by submitting a video of my intended presentation. Last year they were going to restart the in-person conference, but due to emergency renovations at the Bellingham Technical College, they had to cancel the event. Hopefully the Technical College buildings are all good and the conference is able to run successfully,

Bellingham is a great location for this show as it is halfway between Vancouver, BC and Seattle, WA, providing quite a large metropolitan area to draw attendees and presenters from.

I’ll be giving a presentation: An Introduction to the RISC-V Architecture at 11:00am on Saturday April 27 in room HC111.

Presentations

The description for my presentation is:

An introduction to the RISC-V Architecture and assembly language programming. Advantages and disadvantages of RISC-V, progress to date and support in the Linux kernel.

Last time my presentation was on ARM architecture and this time RISC-V. I think there is a lot of interest in RISC-V, especially as open source hardware is trying to become as established as open source software.

Besides my presentation, there are a large number of other great presentations. I’m looking forward to “Retro hardware preservation with the MiSTer FPGA Project” on Sunday morning. The quality of the talks is really high, and usually given by a main contributor to whichever project is being discussed.

Summary

I’m looking forward to LinuxFest NorthWest this April, and I hope to see a few of my readers at the show. It should be a fun event and nicely fits in with the release of my new book on RISC-V Assembly Language Programming.

Written by smist08

March 1, 2024 at 11:46 am

RISC-V Assembly Language Programming

leave a comment »

Introduction

RISC-V Assembly Language Programming: Unlock the Power of the RISC-V Instruction Set” is my fourth Assembly Language programming book published by Apress, a division of Springer Nature. This book is part of their Maker Innovations Series. My first three books were on the three types of ARM instruction sets, namely 32-bit, 64-bit and thumb (used in microcontrollers). The RISC-V instruction set architecture is more unified, so with this new book, I could cover both 32-bit and 64-bit RISC-V Assembly Language.

Update 2024/04/04: The source code is now in Apress’s github at: https://github.com/Apress/RISC-V-Assembly-Language-Programming

Update 2024/04/03: While waiting for Apress to upload the source code for the book, I placed it here: https://github.com/smist09/riscv-assembly-language/tree/main.

What is RISC-V?

Today’s CPU market is dominated by ARM and Intel. If you want to make your own CPU based on ARM then the royalties can be quite expensive and the terms restrictive on what you are allowed to do. Intel doesn’t like to license their CPU instruction set. There are a number of CPU also-rans such as MIPS that you might be able to license cheaper, but these are getting old and not being updated as much.

RISC-V was developed at Berkeley University. It is an instruction set architecture (ISA) that specifies the RISC-V Assembly Language instruction set and how the instructions are encoded to be executed. There are no royalties on using this architecture, so anyone can implement a RISC-V CPU and manufacture it. Designing and manufacturing a CPU can be expensive and there are a number of companies that have developed CPU designs that you can license and customize for your specific needs. One cool thing is that many people have implemented RISC-V CPUs in Verilog to run on various FPGA boards, something you don’t see in the ARM or Intel world.

Currently RISC-V is doing quite well in the microcontroller market, where CPUs often cost less than a dollar and licensing costs can sink a product. My RISC-V book includes how to program these microcontrollers such as the ESP32-C3 in 32-bit RISC-V Assembly Language.

RISC-V CPUs are starting to enter the desktop/laptop market with products like the StarFive VisionFive II which run 64-bit Debian Linux. My book covers programming these in 64-bit Assembly Language.

The RISC-V specification was released in 2010 and as such is the most modern RISC architecture available. It is an extensible architecture and many useful extensions have been released to support things like GPUs, AI coprocessors and other advanced functionality. When writing RISC-V Assembly Language, it feels much cleaner than either ARM or Intel. It is built on the legacies of all the previous RISC architectures including ARM, Mips, Sparc and Power and developed something quite remarkable.

The RISC-V architecture is still in the early stages and there are many opportunities to provide Assembly Language code to projects like the RISC-V Linux, GCC RISC-V target, bootloaders and firmware for all the new computing boards.

Where to Buy

As the book pushes its way out into the channel, it takes time to appear in all markets. As of today, it appears for sale on Springer Link, but is still pre-order at all the other retailers. Over the coming weeks they will start to change from pre-order to regular order as they receive their shipments. So apologies for anyone keen to buy, whose favorite retailer is still pre-order, but this should sort itself out over the next month. Here are a few links to some bigger retailers:

Summary

Writing this RISC-V Assembly Language Programming book was a lot of fun. I greatly enjoyed writing RISC-V Assembly Language programs and I hope you do as well. I’m sure that as time goes by, we’ll be seeing many more RISC-V CPUs filling all the available niches in the computing world. Before long there will be RISC-V mobile phones, laptops, desktops and even supercomputers.

Written by smist08

February 16, 2024 at 1:17 pm

Programming the SunFounder GalaxyRVR Mars Rover

with one comment

Introduction

Last time, we gave our initial review of the SunFounder GalaxyRVR Mars Rover, this time we’ll look at how to program it. Unlike SunFounder’s PiCar or PiDog which are powered by Raspberry Pis, the GalaxyRVR has an Arduino Uno as its brain. This means writing the program in Arduino C rather than Python. It also means writing to a lower level, since there isn’t any higher level library. The good news is that there are a number of good samples as well as Brett Hagman’s SoftPWM library to make controlling the motors easier.

Before you Begin

The Mars Rover course has good instructions on installing the Arduino IDE and writing your first programs. A couple of other things to remember:

  1. When you download your first program, it will replace the program that talks to the SunFounder mobile app. Note that if you want to use the mobile app again you will need to upload the program from near the end of this course module.
  2. Remember to set the switch on the other side of the Arduino Uno from the power switch to the upload program position, or the Arduino IDE will hang when you go to upload a program.
  3. Remember to set the port to the COM port that the USB cable is associated with, in my case COM3. Better to do this before trying to upload.

Programming Model

The programming module is fairly low level since you are controlling the power levels on the RobotHat fairly directly. Fortunately there is lots of sample code on how to control the various devices. In the sample program at the end, the routines: moveForward, moveBackward, turnLeft, turnRight, stopMove, and readSensorData are taken from the sample code. If you are doing a lot of GalaxyRVR programming, you could develop a nice library of these to make life easier.

Consider the moveFoward routine:

void moveForward(int speed) {
  // Set the left motors rotate counterclockwise
  SoftPWMSet(in1, speed);
  SoftPWMSet(in2, 0);
  // Set the right motors rotate clockwise
  SoftPWMSet(in3, 0);
  SoftPWMSet(in4, speed);
}

Basically you are setting the speed in each direction for each side’s set of motors. 

The Mars Rover doesn’t have a servo to control steering, instead you steer by making the speed of the motors on each side uneven. It can turn quite sharply by having one side move forward and the other backwards. This is why a gentleTurn routine was added so the Mars Rover can proceed in an outward spiral. Also note that you need some delay functions to let the rover move before you go onto the next thing.

For the ultrasonic sensor, it’s connected to pin 10 and you trigger it by setting the pin high for 10 microseconds. It will then return a pulse where the length of the pulse is proportional to the distance that the sound wave is reflected from. This pulse is read by the Arduino’s pulseIn function. This is all handled in the readSensorData function.

Camera

The Mars Rover has a front mounted camera that you can move up and down. I didn’t include use of this in the sample program, like we did for the Raspberry Pi based robots. The Arduino Uno only has 32K of program memory and 2k of variable memory, so it can’t really handle image processing applications. As a result you use the camera by programming it with Wifi sign on information and it sends the wifi to a receiving program on a more powerful computer via Wifi. Since I don’t think I can easily do the sort of processing I did for the other two robots, I left this out. I think you will need to be quite clever to do local image processing on an Arduino Uno.

Power Management

There is a power management API, so you can see the battery charge level. This way you can monitor if the solar cell is doing its job and report the battery level to the mobile app. My sample program doesn’t use this, but it is an interesting feature to see how the solar cell is doing.

Summary

This robot is fun to play with and program. If you are interested in learning Arduino programming and robot control at a lower level, then this will be a great kit for you. Due to the limited memory and processing power of the Arduino Uno’s ATmega328P 8-bit CPU, we can’t do image processing and the size of our programs are limited, but the sample program in this article only uses 13% of memory. As a way to learn C programming, Arduino programming and robotics programming, this is a great kit to experiment with.

#include <SoftPWM.h>
// Define the motor pins
const int in1 = 2;
const int in2 = 3;
const int in3 = 4;
const int in4 = 5;
// Define the pin for the ultrasonic module
#define ULTRASONIC_PIN 10
#define SPIRAL  1
#define BACKANDFORTH 2
#define STUCK 3
int state = SPIRAL;
int StartTurn = 15;
int foundObstacle = 20;
int StuckDist = 5;
#define POWER 100
void moveForward(int speed) {
  // Set the left motors rotate counterclockwise
  SoftPWMSet(in1, speed);
  SoftPWMSet(in2, 0);
  // Set the right motors rotate clockwise
  SoftPWMSet(in3, 0);
  SoftPWMSet(in4, speed);
}
void moveBackward(int speed) {
  // Set the left motors rotate clockwise
  SoftPWMSet(in1, 0);
  SoftPWMSet(in2, speed);
  // Set the right motors rotate counterclockwise
  SoftPWMSet(in3, speed);
  SoftPWMSet(in4, 0);
}
void turnLeft(int speed) {
  // Set al motors to rotate clockwise
  SoftPWMSet(in1, 0);
  SoftPWMSet(in2, speed);
  SoftPWMSet(in3, 0);
  SoftPWMSet(in4, speed);
}
void turnRight(int speed) {
  // Set all motors to rotate counterclockwise
  SoftPWMSet(in1, speed);
  SoftPWMSet(in2, 0);
  SoftPWMSet(in3, speed);
  SoftPWMSet(in4, 0);
}

void gentleTurn(int speed) {
// Set the left motors rotate counterclockwise
SoftPWMSet(in1, speed * 3 / 4);
SoftPWMSet(in2, 0);

// Set the right motors rotate clockwise
SoftPWMSet(in3, 0);
SoftPWMSet(in4, speed);
}
void stopMove() {
  // Stop all the motors
  SoftPWMSet(in1, 0);
  SoftPWMSet(in2, 0);
  SoftPWMSet(in3, 0);
  SoftPWMSet(in4, 0);
}
float readSensorData()
{
  // A 4ms delay is required, otherwise the reading may be 0
  delay(4);
  //Set to OUTPUT to send signal
  pinMode(ULTRASONIC_PIN, OUTPUT);
  // Clear the trigger pin
  digitalWrite(ULTRASONIC_PIN, LOW);
  delayMicroseconds(2);
  // Trigger the sensor by sending a high pulse for 10us
  digitalWrite(ULTRASONIC_PIN, HIGH);
  delayMicroseconds(10);
  // Set the trigger pin back to low
  digitalWrite(ULTRASONIC_PIN, LOW);
  //Set to INPUT to read
  pinMode(ULTRASONIC_PIN, INPUT);
  // pulseIn returns the duration of the pulse on the pin
  float duration = pulseIn(ULTRASONIC_PIN, HIGH);
  // Calculate the distance (in cm) based on the speed of sound (340 m/s or 0.034 cm/us)
  float distance = duration * 0.034 / 2;
  return distance;
}
void executeSpiral()
{
    float distance;
    gentleTurn(POWER);    
    distance = readSensorData();
    printf("spiral distance: %d\n",distance);
    if (distance <= foundObstacle)
    {
        state = BACKANDFORTH;
    }
  delay(2000);
}
void executeUnskick()
{  
    printf("unskick backing up\n");
    moveBackward(POWER);    
    state = SPIRAL;  
delay(2000);                 
}
void executeBackandForth()
{
    float distance;
    distance = readSensorData();
    printf("back and forth distance: %d\n",distance);
    if (distance >= StartTurn)
    {  
        moveForward(POWER);
  delay(2000);
    }  
    else if (distance < StuckDist)
    {
        state = STUCK;
    }
    else
    {
        turnRight(POWER);
  delay(1000);
    }
}
void setup() {
  // Initialize SoftPWM
  SoftPWMBegin();
}
void loop() {
  while (true)
  {
    if (state == SPIRAL)
    {              
      executeSpiral();
    }
    else if (state == BACKANDFORTH)
    {
      executeBackandForth();
    }                
    else if (state == STUCK)
    {
      executeUnskick();
    }                    
  }
}

Written by smist08

February 14, 2024 at 3:57 pm

A Mars Rover Explores my Backyard

with one comment

Introduction

Here is another review of a SunFounder robot, this time the SunFounder GalaxyRVR Mars Rover. I previously reviewed their PiCar and PiDog which both used a Raspberry Pi as their brains. In contrast the Mars Rover uses an Arduino Uno for its smarts. The Arduino is included with the kit and has been pre-programmed so you can immediately use the Mars Rover with the SunFounder mobile app after assembly, No software installation required. The GalaxyRVR retails for around $175CAD. In this blog article, we’ll review the assembly and usage of the Mars Rover with the mobile app and then in a future blog article cover how to custom program the rover.

Assembly

Assembling the kit is fairly straightforward, the provided instruction sheet has quite good drawings and the parts are easy to identify. There are 37 steps, assuming you install the optional solar panel. There are six motors to install, but installing motors is easy and there isn’t any zeroing or calibration step as there is with servo motors. There is one servo motor that controls whether the front camera looks up or down. 

Of the three SunFounder kits I’ve built, this was the easiest and quickest to assemble. I might be getting practiced at assembling these, but I think this kit is a bit simpler to build.

When I installed the front servo, the instructions to zero it didn’t work. As a result when I first ran the robot it always looked at the ceiling. Fixing this was easy as the screw that sets the servo position is still accessible once the robot is assembled.

Like all SunFounder kits, there are lots of spare screws, bolts, nuts and washers. As a result it isn’t any big deal if you drop one and can’t find it.

One thing the assembly instructions are missing is how to clean up the wiring. The kit includes zip ties and cable wrap, but the instructions don’t show what to do with them. The picture on the front of the box shows an assembled Mars rover and you can see where to put the cable wrap and zip ties, which I then applied. It would probably be easier to apply these before the rover is fully assembled.

Operation

The included Arduino Uno is preprogrammed to work with the SunFounder mobile app, but using it with the app isn’t quite as nice as the Raspberry Pi based robots. The Raspberry Pi connects easily to your home Wifi, so as long as your mobile device is also on the home wifi, everything works.

The Arduino on the GalaxyRVR creates a GalaxyRVR Wifi network that you need to connect to. This is just a matter of going into settings, selecting the network and entering the password of 12345678. The problem is that my iPhone doesn’t really like this network because it doesn’t provide internet access and the iPhone tends to disconnect it and switch back to the home Wifi. Plus my iPhone is off the internet while controlling the robot. Bluetooth might have been a better choice. Interestingly the Wifi is part of the ESP32-CAM camera module and not the Arduino Uno board.

Anyway there is a predefined GalaxyRVR dashboard in the SunFounder app, and from this it is easy to control the Mars rover.

Mars Rover control panel. Background is the view from the camera.

As you can see from the screenshot, the background is the view from the Rover’s front camera. Following is a video of the Rover in my backyard. Note that it rained quite hard the day before, so the grass is wet which causes the rover to lose traction at a couple of points.

Video of the Mars Rover in action.

Education

Besides the educational value in assembling the kit, SunFounder provides quite a good course on the real Mars rover and how the GalaxyRVR follows its design. It has the history and details of all three real Mars rovers and covers how the various components of the GalaxyRVR work and can be programmed. The course introduces the Arduino IDE and the basic elements of Arduino programming that we will look at in a future blog post.

Summary

I enjoyed assembling the GalaxyRVR kit and the assembled robot is fun to play with. The wifi communications could be better, but is still workable. I think high school level kids will enjoy going through SunFounder’s Mars Rover course and playing with the robot along the way.

Written by smist08

February 12, 2024 at 3:20 pm

Review of Chip War by Chris Miller

leave a comment »

Introduction

Last time, Jeff Hortobagyi presented his review of Chip War: The Fight For The World’s Most Critical Technology, Simon and Schuster, 2022 by Chris Miller. I agree with Jeff’s review, but thought I’d add a few observations from my personal experience. I first learned to program in 1976 in grade 10 using a 300 baud dialup modem to the local community college’s PDP-11, which we programmed in BASIC. After my first University Coop work term, I purchased an Apple II+ computer. After graduating from University, I’ve worked in the technology field and worked closely with quite a few of the companies whose history appears in Miller’s book.

For instance, my first real job after University was with Epic Data, which no longer exists. We used Intel chips for embedded industrial control applications. When I started, I was sent down to Intel for a two week course on the 80186 chip for embedded systems. To start the course, Intel provided some company propaganda about how the heart of everything at Intel was the semiconductor fabrication process and that everything else flows outward from that. Certainly at this time, Intel was king and this was just ahead of Intel releasing the revolutionary 80386 chip which started the 32-bit era of personal computing. Intel is certainly still the king of desktop, server and laptop CPU chips but as Chip War notes missed out on the phone market, largely by not keeping up with fabrication technologies and not being able to offer a competitive low power CPU.

ASML and Modern Chip Fabrication

For me the most fascinating part of Chip War was the history of ASML and the current EUV Lithography process. How complicated it is and how much technology and material science is required to produce modern chips. Even when Russia or China steal the plans for an ASML fabrication machine, they have no way of building such a machine as all the components are out of their reach. The costs, dedication and work that went into the current process is staggering. It is truly amazing that the current level of semiconductor manufacturing actually happened.

ASML Chip Fabrication Machine

Competition is Actually Good

Chris Miller tells the story from the American point of view, regarding this as a story of protecting American leadership and lamenting all the investments made by various foreign governments such as Japan, South Korea, China and Taiwan to build competitive chip industries. I think this is partly the American companies interviewed by Miller lamenting that they can’t just sit on their laurels and milk the profits from their customers. That is if they stop massive investments in R&D then they quickly fall behind and their profits dry up.

This makes me think of the Apple TV series “For All Mankind” where the premise is that the Russians land on the moon first, and that the space race doesn’t end. That massive investments in space continue and we quickly achieve colonies on first the moon and then Mars. In reality when the Americans landed on the moon, the race was over and the Russians gave up. Then there was no competition for space and as a consequence all government investment was reduced year after year turning Nasa into a shadow of its heyday.

If American companies had got their way, there would be no EUV lithography process from ASML, there would be no TSMC manufacturing the best chips in Taiwan. As it is, all these governments, including the USA are investing massively in the next generation of High NA EUV lithography. The investment to bring the next generation, which should be online in a couple of years, is around 100 billion dollars. The generation after that is anticipated to require trillions in investment. The only way these quantities of capital can be raised is via massive government investments driven by a fear of national security. The Gulf War along with the Ukraine war have shown the importance of high tech in modern warfare.

Summary

Chris Miller’s book is well researched and gives a good history of the chip industry. Ignore the American centered storyline, after all what’s wrong with competition and countries like Japan, South Korea and Taiwan competing? None of those countries have the budget of the US defense department to shore up industry. I would argue that if this competition wasn’t present, then Apple, Intel, AMD, Micron and the other US tech companies wouldn’t be producing the amazing products that they do.

I think the next generation of technology is going to require investment from a large coalition of countries from the US, Europe and Asia. It will be fascinating to see how this all unfolds.

Written by smist08

February 10, 2024 at 6:23 pm

All the Chips: A history of the Semiconductor

with one comment

This is a guest post by Jeff Hortobagyi with his review of Chris Miller’s book: “Chip War: The Fight For The World’s Most Critical Technology”. I’ve read the book as well and will be posting a few comments next.

Chris Miller, Chip War: The Fight For The World’s Most Critical Technology, Simon and Schuster, 2022

“If China takes Taiwan, they will turn the world off, potentially,” Donald Trump told Fox News recently, apparently referring to a potential seizure of one company that is central to, well, pretty much everything. Indeed, it’s arguably the most important company in the world.1

Nicholas Kristof, New York Times, January 24, 2024

The “one company” refers to Taiwan Semiconductor Manufacturing Company (TSMC), maker of 90% of the world’s most advanced computer chips. In Chip War: The Fight For The World’s Most Critical Technology,2 Chris Miller tells a story of the semiconductor, an innovation that changed the world to a degree I didn’t fully understand before reading this book. It’s also a story about globalism and its accomplishments and discontents, about geopolitics, and maybe the story of the world since the second world war. Chip Wars is never dull, reading at times like a spy thriller and other times oddly fascinating when it describes the technology of semiconductors. I was surprised to see myself highlight a paragraph on the lithographic process for computer chips.

Chip Wars is a comprehensive, and it is certainly a well-researched history, if sixty pages of notes are any indication. It is like he has read everything and spoken to anyone with anything to do with or say about semiconductors. Miller has a storyteller’s ability to pull the reader along with him, and a way of explaining the most advanced technology, without dumbing it down, that a lay person can grasp.

The story starts just after the second world war when cumbersome, unreliable vacuum tubes in computers were replaced with the invention of the transistor (in Bell Labs, of course) and then the semiconductor. (A semiconductor –or integrated circuit or chip—is a small piece of semiconducting material, usually silicon, with millions or billions of microscopic transistors carved into it.)

As with so much advanced technology, the US military played a large role in its development. They created a market for semiconductors, namely, for rockets in the space race instigated when the Soviet Union launched Yuri Gagarin into space. The new technology played a large role in reaching the moon and also in revolutionizing warfare and transforming American military power. Semiconductors allowed the US to offset the Soviet advantage in nuclear weapons with precision weapons, better surveillance, and advanced communications. Its overwhelming capability was demonstrated in the 1991 Gulf War. A “triumph of silicon over steel,” is how the New York Times put it. The next battle over military supremacy, undoubtedly with China, will largely be determined by Artificial Intelligence and the triad of data, algorithms, and computing power of chips needed to harness it.

Early on, visionaries in the semiconductor industry realized early that “the integrated circuit would revolutionize society far beyond rockets and radar” and began to focus on mass market products, the transistor radio being one of the first, though its success was driven by Japan’s, specifically, Sony’s, entry into the market—and, since then, the calculator, the Sony Walkman, the PC, the iPhone—to name just a very few of the most prominent products.

The change in the market can be seen by the US military’s share of computer chip purchasing, down from almost all the chips in the early 1960s to about 2% of the total now. As Chris Miller puts it, Apple’s Tim Cook has more influence on the industry than anyone in the Pentagon.

Chip fabrication also expanded, first to Japan, and then to South Korea, Singapore, Malaysia, and Taiwan, as Silicon Valley chased cheap labour. (The cheapest, South Korea, at about 5% of US wages.)  The US government was behind it too in its concern about the influence of communist China and the Soviet Union. Developing the chip industry in Asia undoubtedly did more to extend American influence in the region, and draw the region into US sphere, than military bases and wars. It also eventually led to trade disputes between the US and Japan in the 1980s over protectionism and the US fears that Japan might eclipse the US economy. Semiconductors are also a sticky point in the US’s relationship with China, adding to geopolitical tension between these countries, with the US recently restricting the export of some advanced chips and technology to China. It is about markets, military superiority, and economic supremacy.

Chip Wars takes a balanced perspective, but it also leans into the pro-American. While I understand the importance and achievement, I wasn’t keen to read enthusiastic descriptions of how semiconductors improved the precision and effectiveness of bombing in the Vietnam and Gulf Wars. It can’t be denied; however, that the US deserved credit for the oversized role it played in the history of the semiconductor.

The story of technological development is fascinating, even for those not always excited by technology, like me. It was interesting to read that the cutting-edge computer chip of 1961 had four transistors; the number now is closer to 12 billion. Moore’s Law barely explains it. Or this description of the mind-bending lithographic process used to carve the patterns on silicon wafers that make them into computer chips.

The company’s engineers realized the best approach was to shoot a tiny ball of tin measuring thirty-millionths of a meter wide moving through a vacuum at a speed of around two hundred miles per hour. The tin is then struck twice with a laser, the first pulse to warm it up, the second to blast it into a plasma with a temperature of around half a million degrees, many times hotter than the surface of the sun. This process of blasting tin is then repeated fifty thousand times per second to produce EUV light in the quantities necessary to fabricate chips.

If that description excites you, there’s plenty more of that in the book. Incidentally, the initial lithographic process for computer chips relied on a simple light bulb.

But Chip Wars isn’t just a story of technology. The author makes the important point that the development of the semiconductor “is as much the story of manufacturing experts, supply chain specialists, and marketing managers as it is about physicists or electrical engineers.” And there is a great cast of characters. A Hungarian refugee from the war whose motto is, “Only the paranoid survive”. A refugee from the Japanese in WW2 and the communists in China who switched from studying Shakespeare to engineering, and created the worlds largest chip fabricator. Not to mention an Idaho potato farmer who may have saved the chip fabrication industry in the US.

The Soviet Union’s failure to keep up with the American chip industry is an interesting story. They had a large spying operation—and every country and probably company involved in semiconductors spied—to steal western industrial secrets and copy them in the USSR, ultimately failing by setting themselves up to always be a step behind the US. Moreover, despite having some of the world’s top physicists, they couldn’t match the innovation ecosystem of the US. As a Soviet joke went at the time, “Comrade, we have built the world’s biggest microprocessor.”

In Chip Wars, Miller has a more considered take on globalism than we’re used to in our current polarized environment. He knows the benefits: greater efficiency, lower costs, and increased innovation. He also knows the risks. Most chips are made by a small number of producers in Asia, a region of geopolitical risk—and the most important producer, TMSC, is in Taiwan, a disputed island caught in a geopolitical struggle between the US and China. Moreover, Miller is critical of reliance on shibboleths of globalism, namely, its assumed benefits and the naïve belief in America’s ability to “run faster” than its competitors due to its innovative, open culture.

That brings us back to the quote that leads off this review. If the quote scares you, and it probably isn’t the only quote from the former president to do that, then take some comfort that the story is more nuanced. It is not in China’s best interests to disrupt chip manufacturing in Taiwan. Any disruption would affect them as much as it would affect any other country. Additionally, “the chips are designed in other nations and require international networks to keep production going.”3 Ultimately, a war would be in no one’s interests, but we’ve heard that story before.

Notes

  1. Nicholas Kristof, New York Times, January 24, 2024
  2. All quotes are from Chip Wars unless noted otherwise.
  3. See Kristof above

The Author

Jeff Hortobagyi writes occasional articles and reviews, usually on disaster recovery. He is retired and lives in Vancouver, BC.

Written by smist08

February 10, 2024 at 11:14 am

How to Program a SunFounder PiDog

with one comment

Introduction

Last time, the SunFounder PiDog was introduced, this time we’ll introduce how to program the PiDog to do our bidding. Previously, we looked at the SunFounder PiCar and how to program it. Both robots share the same RobotHat to interface the Raspberry Pi with the various servos, motors and devices attached to the robot. For the PiCar this is fairly simple as you just need to turn on the motors to go and set the steering servo to the angle you want to turn. The PiDog is much more complicated. There are eight servo motors that control the legs. On each leg, one servo sets the angle of the shoulder joint and the other sets the angle of the elbow joint. To get the PiDog to walk takes coordinating the setting of each of these servos in a coordinated fashion. SunFounder provides a PiDog Python class that hides most of the complexity, though it does give you direct access if you want to control the legs directly.

Introduction to the PiDog Class

To use the PiDog class, you need to instantiate a PiDog object and then start issuing commands. A common initialization sequence is:

       dog = Pidog()
       dog.do_action('stand', speed=80)
       dog.wait_all_done()

The first line creates a PiDog object to use, then the next line calls the do_action method which has a long list of standard actions. In the case of ‘stand’, it will set the eight leg servo motors to put the PiDog in the standing position. This is much easier than having to set all eight servo positions ourselves. Then you need to wait for this operation to complete before issuing another command, which is accomplished by calling the wait_all_done method.

The PiDog class uses multithreading so you can issue head and leg related commands at the same time and they are performed on separate threads. The head is reasonably complex as the neck consists of three servo motors. Similarly for the tail which is controlled by one servo motor. So you could start leg, head and tail commands before calling wait_all_done. Then you are free to issue another set of commands. If you issue a new command while a previous one is executing then the library will throw an exception.

To get the PiDog to move we use commands like the following actions:

       dog.do_action('trot', step_count=5, speed=98)
       dog.do_action('turn_right', step_count=5, speed=98)
       dog.do_action('backward', step_count=5, speed=98) 

There are also apis to use the speaker, read the ultrasonic distance sensor and set the front LED strip such as:

   dog.speak("single_bark_1")
   dog.ultrasonic.read_distance()
   dog.rgb_strip.set_mode('breath', 'white', bps=0.5)

To use the camera, it is exactly the same as for the PiCar using the vilib library, as this is the same camera as the PiCar, connected in the same manner. The sample program below uses the vilib program to compare successive images to see if the PiDog is stuck, though this doesn’t work very well as the legs cause so much movement that the images are rarely the same.

Sample Program

I took the PiCar Roomba program and modified it to work with the PiDog. It doesn’t work as well as a Roomba, as the PiDog is quite a bit slower than the PiCar and the standard actions to turn the PiDog have quite a large turning radius. It might be nice if there were some standard actions to perhaps turn 180 degrees in a more efficient manner. The listing is at the end of the article.

PiDog running the sample program.

Documentation

At the lowest level, all the source code for the Python classes like PiDog.py are included which are quite interesting to browse. Then there is a set of sample programs. The standard set is documented on the website, then there is a further set in a test folder that is installed, but not mentioned on the website. The website then has an easy coding section that explains the various parts of the PiDog class. Although there isn’t a reference document, I found the provided docs quite good and the source code easy enough to read for the complete picture.

Summary

Although coordinating eight servo motors to control walking is quite complicated, the PiDog class allows you to still control basic operations without having to understand that complexity. If you did want to program more complicated motion, like perhaps getting the PiDog to gallop then you are free to do so. I found programming the PiDog fairly straightforward and enjoyable. I find the way the PiDog moves quite interesting and this might give you a first look at more complicated robotic programming like you might encounter with a robot arm with a fully articulated hand.

from pidog import Pidog
import time
import random
from vilib import Vilib
import os
import cv2
import numpy as np
SPIRAL = 1
BACKANDFORTH = 2
STUCK = 3
state = SPIRAL
StartTurn = 80
foundObstacle = 40
StuckDist = 10
lastPhoto = ""
currentPhoto = ""
MSE_THRESHOLD = 20
def compareImages():
    if lastPhoto == "":
        return 0
    img1 = cv2.imread(lastPhoto)
    img2 = cv2.imread(currentPhoto)
    if img1 is None or img2 is None:
        return(MSE_THRESHOLD + 1) 
    img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
    img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
    h, w = img1.shape
    try:
        diff = cv2.subtract(img1, img2)
    except:
        return(0)
    err = np.sum(diff**2)
    mse = err/(float(h*w))
    print("comp mse = ", mse)
    return mse    
def take_photo():
    global lastPhoto, currentPhoto
    _time = time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))
    name = 'photo_%s'%_time
    username = os.getlogin()
    path = f"/home/{username}/Pictures/"
    Vilib.take_photo(name, path)
    print('photo save as %s%s.jpg'%(path,name))
    if lastPhoto != "":
        os.remove(lastPhoto)
    lastPhoto = currentPhoto
    currentPhoto = path + name + ".jpg"
def executeSpiral(dog):
    global state
    dog.do_action('turn_right', step_count=5, speed=98)      
    dog.wait_all_done()
    distance = round(dog.ultrasonic.read_distance(), 2)
    print("spiral distance: ",distance)
    if distance <= foundObstacle and distance != -1:
        state = BACKANDFORTH
def executeUnskick(dog):
    global state    
    print("unskick backing up")
    dog.speak("single_bark_1")
    dog.do_action('backward', step_count=5, speed=98)    
    dog.wait_all_done()
    time.sleep(1.2)
    state = SPIRAL                    
def executeBackandForth(dog):
    global state    
    distance = round(dog.ultrasonic.read_distance(), 2)
    print("back and forth distance: ",distance)
    if distance >= StartTurn or distance == -1:       
        dog.do_action('trot', step_count=5, speed=98)
        dog.wait_all_done()
    elif distance < StuckDist:
        state = STUCK
    else:
        dog.do_action('turn_right', step_count=5, speed=98)
        dog.wait_all_done()
    time.sleep(0.5)                
def main():
    global state
    try:
        dog = Pidog()
        dog.do_action('stand', speed=80)
        dog.wait_all_done()
        time.sleep(.5)        
        dog.rgb_strip.set_mode('breath', 'white', bps=0.5)       
        Vilib.camera_start(vflip=False,hflip=False)
        while True:
            take_photo()
            if state == SPIRAL:               
                executeSpiral(dog)
            elif state == BACKANDFORTH:
                executeBackandForth(dog)
            elif state == STUCK:
                    executeUnskick(dog)
            if compareImages() < MSE_THRESHOLD:
                state = STUCK                    
    finally:
        dog.close()
if __name__ == "__main__":
    main()

Written by smist08

February 9, 2024 at 11:58 am

PiDog a New Member of the Pack

with 2 comments

Introduction

I recently received a SunFounder PiDog robot dog kit. I reviewed the SunFounder PiCar here along with software projects here and here. There are a lot of similarities between the PiCar and the PiDog as they are both controlled by a Raspberry Pi and both share the same robot hat to control the mechanisms, The PiDog is a more advanced kit, as there are more parts and more steps to the assembly than the PiCar. Having already assembled the PiCar was a big help as I was familiar with how SunFounder kits go together which sped up the process quite a bit..

The PiDog has eight servo motors controlling the legs for walking, three servo motors as part of the neck to control the head and a twelfth servo motor to wag the tail. There is a speaker so the PiDog can bark or howl, there is an ultrasonic sensor to detect obstacles, a camera so it can see, as well as a directional microphone so it can hear and tell the direction of the sound. There is also a touch sensor. There is a strip of lights along the front that can add to the PiDog’s expressiveness.

There are a number of sample Python programs included to show all the normal doggy type movements and operations. These then provide a great foundation for you to use to develop your own programs.

Although you can have fun just using the sample programs, writing your own programs to control the PiDog is where the real fun begins. Writing programs to control the PiDog will be the topic of future blog posts.

The PiDog is available for sale on Amazon along with other retailers for around $230CAD.

Assembly

The assembly instructions are fairly clear with good diagrams. But be warned there are 74 steps and a lot of parts.

The PiDog parts dumped on the table ready for sorting.

Some of the steps are quick, like peeling the protective cover from an acrylic part to quite long, usually involving doing something to each of the four legs. Mostly the parts are large enough to install easily. The supplied screw drivers are magnetic which helps in manipulating the smaller screws. I think the most annoying part was installing the screws and nuts that hold the servo motors in place. A younger person with smaller fingers will have no trouble at all. The kit includes screwdrivers and a wrench that fits the various nuts and screws. I found a pair of needle nose pliers to hold some nuts while tightening and a knife to peel off the protective layer on the acrylic parts was helpful.

One nice thing about the SunFounder kits is that they come with lots of extra screws, bolts and washers, so if you drop one, you don’t have to panic about finding it.

The partially completed PiDog with the legs assembled, next the neck assembly.

When assembling the robot, make sure you zero each server after installing it, this will make the robot function better and make the calibration step at the end much easier. There are more details when installing the first servo, which is the motor to wag the tail. The following ones hint at the zeroing process, but make sure you do it for all of them.

It’s Alive

The completed PiDog in its powered off pose.

After assembling the PiDog there is a calibration step, which if you zeroed the servo motors correctly should be quick and painless. The software installation part is done early on in the assembly process, this is needed to zero the servo motors as they are installed. When finished there are a couple more pieces of software if you want to control the PiDog from the SunFounder mobile app running on your phone.

Here is a video of the PiDog interacting with our Chihuahua Pilot.

The dogs are rather nervous of the PiDog. I think dogs view their world through scent and although PiDog acts like a dog, it doesn’t smell like a dog which either confuses or makes them nervous.

The Mobile App

I found the example software and mobile app for the PiDog to be quite good and a step up from the PiCar. The mobile app contains a complete screen to control the PiDog shown below.

Screenshot of the SunFounder mobile app with the PiDog default controls. The view from the PiDog’s camera is in the background.

Using the mobile app is a good way to play with the PiDog and make it perform most of its standard functions.

Sample Software

The other examples, let you control the PiDog via the keyboard on your laptop as well as run a number of behaviors like patrolling and barking when an obstacle is encountered.

Here is the PiDog running the patrol example program:

The various examples show how to use the PiDog’s API and exercise the various functions exposed via the PiDog Python Class. All the Python source code for the PiDog is included and is interesting to browse through. The PiDog class talks to the RobotHat class which turns the various servos and electronics off and on. For the PiDog class to make the dog walk, requires controlling all eight servo motors independently using multiple threads. Inside the class there are quite a few trigonometric calculations and a lot of delicate timing. Walking with four legs is a complicated process.

Summary

All in all the PiDog was fun to assemble and worked right away. There are a lot of great educational uses for this robot including:

  1. Assembling a robot
  2. How a robot works
  3. Basic programming in Python using the PiDog class
  4. More sophisticated programming involving trigonometry and how to program walking, or even galloping

Of course if you just assemble the robot and then use it from the mobile app, it is still lots of fun.

One thing I felt was missing was a working jaw. If there was a jaw then the barking would be more realistic and the PiDog could carry things around in its mouth. I think that would be cool.

It would also be cool if there were some additional parts you could 3D print like a body cover, so the electronics and wires aren’t so exposed. Plus this is another avenue for customization as you could have different looks and colors for the body. The same for the head. Something I might look into down the road.

Written by smist08

February 2, 2024 at 10:24 am