154

How are everyday machines (not so much computers and mobile devices as appliances, digital watches, etc) programmed? What kind of code goes into the programming of a Coca-Cola vending machine? How does my coffee maker accept a pre-programmed time and begin brewing a pot of coffee hours later, when that time arrives?

Do these kinds of machines have operating systems inside of them, or is it something even more basic? Are they written in Assembly, C, or some other language?

And, I would really like to find some resource that lists these operating systems or underlying code systems, possibly even with source code if possible. If anyone knows of such a resource (searching yielded nothing for me), that would be fantastic.

Josh Leitzel
  • 15,089
  • 13
  • 59
  • 76

13 Answers13

50

Most of what you're talking about are embedded systems where C is a luxury that may not be available. The software often isn't separate programs running under an OS like you'd have on a desktop or phone, especially if the chip the designers chose to use is described as a "microcontroller".

Most of the time the software is written in C or assembly. C requires a compiler to be written for that platform (and might produce bloated or inefficient asm if it doesn't optimize well), but a simple assembler only has to turn text into machine code one line at a time and is easy to write. (And if a vendor wants anyone to buy their microcontrollers, they'll make sure at least an assembler exists for it to make development attractive, often also a C compiler although sometimes non-optimizing.)

Your coffee pot and most simple systems like that don't carry an operating system. They simply load from a start address in memory and you put your code there. Often these systems have their "code" burned into EEPROMS that act as the hard drive of the system. Or depending on the type of EEPROM / flash, code may be able to run directly from flash without having to get loaded into RAM first. (The device may not be able to write to its own flash memory; that's done with external tools. The edit/compile/run cycle may include reprogramming the flash of actual hardware, if not testing in a simulator.)

Coca-cola machines, routers, etc. typically use a realtime OS like QNX, EMBOS, or sometimes RTlinux if you're lucky. Most of these are proprietary OS you license for lots of money, but they have C compilers, drivers to work with hardware, etc.

http://www.qnx.com/

http://www.segger.com/cms/embos.html

http://www.microsoft.com/windowsembedded/en-us/campaigns/compact7/default.aspx?WT.srch=1&WT.mc_ID=SEARCH

RTLinux

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
chubbsondubs
  • 37,646
  • 24
  • 106
  • 138
  • Thank you! I've chosen yours as the best answer since you directly addressed everything in my question. A special thank you for pointing me toward many examples with links. – Josh Leitzel Apr 11 '11 at 16:42
  • 37
    People coding in binary? Assemblers are so stupidly simple to write I find that hard to believe. Do you have a source for this? Also, this answer gives the false impression that all systems contain a microprocessor which runs code. Though this is becoming more popular as microcontrollers and EEPROM (which make debugging and upgrading easier) become cheaper, it is still not worth it for very cheap systems, or systems which have not changed much over the last 20 years. For example, some digital watches. – BlueRaja - Danny Pflughoeft Apr 11 '11 at 18:54
  • 6
    This is simply too strongly stated. You don't need to host the compiler on the target. Cross-compilers are *very* common in embedded programming. – dmckee --- ex-moderator kitten Apr 12 '11 at 01:34
  • 14
    A coke machine running QNX? WTF - that's another kind of coke! And yes, there is a C compiler for every single platform currently shipping in volume. Nobody wants to write assembly, lack of C would be a huge handicap. (Some compilers aren't very *good*, granted.) – Potatoswatter Apr 12 '11 at 16:08
  • 9
    Also, EEPROM by definition can be rewritten 100000+ times. Write-once memories are obsolete; all popular microcontrollers today have Flash. FPGA is a completely different market from low-end MCU… you have no idea what you're talking about. – Potatoswatter Apr 12 '11 at 16:10
  • 8
    I have a friend who works on vending machines that are quite complex. They have cellular radios, monitor inventory, track temperature, report financial figures, and send HTTP calls back to servers to aggregate all of this data so you can track across all of your machines. And yes these brands of machines are using an Embedded OS. I think they're using RTLinux using C. So yes even the lowly vending machine is becoming complex enough to warrant embedded OS. – chubbsondubs Apr 13 '11 at 01:28
  • Embedded OS like RTLinux, yes. But QNX has significant license costs and isn't used where it's not needed, and it's not needed for a vending machine no matter what. – Potatoswatter Feb 25 '13 at 09:48
  • I'add that RTLinux is not used in routers, just plain Linux. (or uCLinux) – Prof. Falken Apr 09 '13 at 19:13
  • @BlueRaja-DannyPflughoeft: This answer is community-wiki; someone with actual embedded experience should probably edit to fix the egregious claims like hand-writing machine code because asm needs a "compiler". – Peter Cordes Jan 17 '21 at 10:41
30

They use microcontrollers, the 8051 is the classic one. These are 8-bit or 16-bit cores, they rarely have an operating system. The programmer writes the code to initialize the onboard peripherals and implement the interrupt handlers. Languages used are assembly and C. Tough debugging jobs require an in-circuit emulator.

There's lots of growing room beyond this, with 32-bit embedded cores (ARM is the 100 pound gorilla) that boot an embedded version of Linux and/or the Java JVM.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
25

These are embedded systems, and would be programmed using a very low-level language such as C or assembly. In general such a system will run without an operating system, although some newer "everyday machines" such as blue-ray DVD players and wireless routers do run their code on top of a unix-based operating system.


Update

Along the lines of what others have said, many modern embedded systems also run a flavor of windows. It depends up on the application. Also, there is a trend in many spaces to run on a more powerful platform with an operating system, to handle cases such as Blue-ray players needing to run Java, and other instances where the end user desires more functionality.

Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
  • To clarify, Bluray uses Java. – Andrew Marshall Apr 11 '11 at 03:06
  • 6
    Perhaps, but the actual players run Linux and are written in C/C++ – Justin Ethier Apr 11 '11 at 03:30
  • Cash registers and gas pumps and the like were running dos (some brands), sadly some of those run windows now (grocery store or other self checkout lines). The desire to have tv shows blaring at you while you pump your gas, and dancing baloney on the screen when not, has lead to more horsepower. Likewise the coke and water machines with the two axis grabber. – old_timer Apr 11 '11 at 03:32
  • @Justin, well yes of course, Java needs to sit atop something to function. My point was that it utilizes such a high-level language (comparatively) as Java. – Andrew Marshall Apr 11 '11 at 04:23
  • @Andrew: And on what operating system does the Java interpreter in the blue ray run? And in what language is that operating system written? – Gunther Piez Apr 11 '11 at 07:35
  • @dwelch - Good point, and sadly I have actually see the BSOD on one of the machines at the local grocery store. – Justin Ethier Apr 11 '11 at 13:34
  • @drhirsch …I just stated that it can't run on its own. As I said before I would think that would be implied as Java is incapable of running without sitting atop a low-level interpreter. @dwelch, that's interesting as Justin says they run Linux & BSODs only exist in the world of Windows. – Andrew Marshall Apr 11 '11 at 16:25
  • Thanks! I've given you an upvote since you were the first to answer and your answer definitely helped me out. – Josh Leitzel Apr 11 '11 at 16:43
  • Thanks, I'm glad the answer helped you out! – Justin Ethier Apr 11 '11 at 16:52
18

Let's think about the processor in your desktop. All it does is run machine instructions, and by itself, isn't really concerned about "operating systems" or "programs".

You turn your computer on, the processor points to the first instruction, and it starts executing.

On your desktop, it starts executing the "operating system". But there is no reason that you couldn't have the processor executing any set of instructions you chose. (This may not be very useful, since you'd still want to output results to the screen, and that functionality resides in the OS.) At the same time, if your machine instructions consisted of the right opcodes so that the processor would output the correct sequence of signals to paint a picture on the monitor, all the better. No OS needed.

Desktops do so much stuff that we generally require the abstraction of an OS. But at its core, all the processor does is execute instructions.

Same for the processor in Coke machines and Coffee machines. All it does is execute instructions.

Well, writing machine instructions bit-by-bit is tedious. So, just as with desktops, we typically write code in C, which is then compiled into machine code. That machine code is loaded onto the embedded processor and it runs.

Embedded systems do so little that they don't need full-on OSes. A microcontroller might have 8 or 16 pins on the chip - compared to scores of pins in your regular CPU socket.

So the workflow is write some code (say, in C), compile it on your desktop machine. That compiler generates machine code for the embedded chip. Then that code is loaded onto the microprocessor (and you need special hardware to do this.) Then you power the chip and it starts executing instructions. Simple!

poundifdef
  • 18,726
  • 23
  • 95
  • 134
  • 2
    Thank you so much! If I could have chosen another best answer, it would have been yours. I found it very useful. +1 – Josh Leitzel Apr 11 '11 at 16:49
  • Modern processors are surprisingly much concerned about operating systems. They provide features like [Memory Management Units](https://en.wikipedia.org/wiki/Memory_management_unit), [privileged instructions and modes](https://en.wikipedia.org/wiki/Protection_ring) and [hardware virtualization](https://en.wikipedia.org/wiki/Hardware_virtualization). Modern operating systems depend on some of these features. That's the reason why you can't ([easily](http://dmitry.gr/index.php?r=05.Projects&proj=07.%20Linux%20on%208bit)) run linux on small microcontrollers. – Mira Weller Feb 27 '17 at 02:50
12

Many devices which perform a specific function do not contain any "code". They perform their functions via the properties of their electronic components. More advanced systems, which can perform many different functions or need to be easily upgradable, will contain a microcontroller and some sort of "operating system". Since these still have some limits to their functionality, the operating system will be simple and specially designed. Getting even more advanced, the device will contain something similar to a computer. It will have a more complicated operating system which can communicate with different parts of the system. Finally, you reach devices such as smart phones, which contain a full operating system which can run user level code and have much more user input than simpler devices. However, even modern processors are essentially very large electrical circuits. Every instruction the CPU recognizes will cause a different circuit to be used to perform that function.

Here are some Wikipedia pages you may be interested in:

http://en.wikipedia.org/wiki/Electrical_engineering
http://en.wikipedia.org/wiki/Integrated_circuits
http://en.wikipedia.org/wiki/Electronic_engineering
http://en.wikipedia.org/wiki/Computer_engineering
http://en.wikipedia.org/wiki/Microcontroller

ughoavgfhw
  • 39,734
  • 6
  • 101
  • 123
  • This is wrong. A major factor in the cost of low-cost chips is the package that carries the silicon. For this reason, it is cheaper to replace discrete logic with a microcontroller once you get to a couple dozen gates or so… and a dozen gates don't do much. MCU ubiquity has driven up the cost of discrete gates, changing the balance. Also, most low-end MCUs don't run any kind of operating system. – Potatoswatter Apr 12 '11 at 16:15
7

That's a very broad question and it depends a lot on the machine. I can only guess that the majority of these vending machines are controlled by microcontrollers (8051, PIC, ARM7, to name some of the most used) and rarely have an OS and if has any, it would be some kind of RTOS, such as FreeRTOS.

More complex machines, such as DVD/BluRay players or mobile phones run on top of sofisticated platforms, such as OMAP4. Usually a Unix OS runs on them.

Thiago Cardoso
  • 725
  • 1
  • 5
  • 19
7

Low-end microcontrollers found in everyday appliances typically do not run an operating system. They are chosen for low cost, and the main factors driving that cost are the number of pins on the chip (from a dozen to a couple hundred) and the amount of memory inside (from a couple kilobytes to a megabyte ROM, from a few bytes to a hundred kilobytes RAM).

As feature creep works its magic, it does happen that a microwave may need to multitask. In this case, the programmer recalls their operating systems course and implements message-passing, task scheduling, asynchronous I/O, etc as necessary!

Of course, for the sake of expedience, simplicity, code size, etc, features tend to be done in a rudimentary way. Often it depends how you analyze the program, to find generalized OS functionality within task-specific code. But it's a long way from a hard-coded task scheduler to an operating system, and when you have only a few kilobytes to work with, an off-the-shelf OS is not the solution.

Take a look at DigiKey, a popular electronic parts selection site, to learn about low-end MCUs. Here is their info on a very cheap MCU with an LCD controller, such as might be found in a coffee machine. It's easy enough to get the programming manual and everything.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
5

The job of an operating system is to provided shared access to resources - CPU execution time, RAM, I/O etc. Most simple microcontroller based embedded systems have only one program running at a time, and they access (and manage) these resources themselves, hence they don't need an operating system.

Embedded systems are typically programmed in C, and sometimes in assembly for extreme timing or memory optimisation. Some embedded compilers allow you to intersperse assembly within a higher level language.

Nick Forge
  • 21,344
  • 7
  • 55
  • 78
4

I recently came across a cigarette vending machine that ran Ubuntu (the machine was rebooting, so I could see the logo).

ryyst
  • 9,563
  • 18
  • 70
  • 97
2

Take a look at Embedded Systems Magazine and Circuit Cellar

Doug Currie
  • 40,708
  • 1
  • 95
  • 119
1

Most Kiosks, registers, drive-thru screens, and even high end coffee machines and microwaves actually run Windows XP or Linux, like the "Jura Impressa" series machines; you can SSH into them and brew coffee.

Here's a github script of a coffee machine cron job: https://github.com/NARKOZ/hacker-scripts/blob/master/fucking_coffee.rb

Most Routers, newer ovens, newer refrigerators, cars, DVD players, various electronics, newer home automation products including light bulbs, run a version of ARM linux or embedded Linux.

Most cheaper devices, under $20, if they are newer, run on the ESP8266 or similar device (can run LUA or a reduced Node server for $2 in bulk - insanely cheap)

http://nodemcu.com/index_en.html

Using FPGA and embedded systems like 8051, Z80 or other embedded devices like PICC, AVR and Arduino are going to soon be replaced by all-in-one/SoC (System on Chip) systems like the ESP8266. They are simply too easy to program, and are a complete system on a chip that run their own web server; you just power them up, upload your source code and you have a networked server for $2.

I grew up coding PICC and AVR and 8051, and I'm sad to see them go, but I haven't touched anything other than ESP8266es in years because they are 1/10th the price and are an order of magnitude easier to work with. You can get them on development boards with battery packs and exploded pin layouts for $5 on ebay or $10 on adafruit.

Nick Steele
  • 7,419
  • 4
  • 36
  • 33
1

If you're googling around for general info, look for things like "embedded systems", "soc" (system-on-a-chip). I'd say a large portion of these types of devices run in low-level languages, such as C.

Fun fact: Java was originally conceived as a solution to embedded systems programming: http://en.wikipedia.org/wiki/Oak_(programming_language)

Travis Webb
  • 14,688
  • 7
  • 55
  • 109
-2

I heard from an engineer that the Siemens Light-Rail Commuter Trains run on 386s.

luser droog
  • 18,988
  • 3
  • 53
  • 105