📜 ⬆️ ⬇️

Just add meat or embedding on mode


the very essence

With the advent of affordable ARM and MIPS solutions, on which Linux or WinCE can be installed, amateur embedding has reached a qualitatively new level (in general, it has been there for a long time, but not on such a wide scale as it is today). The emergence of such massive software products as Android, very much popularized processors with non-x86 architecture, opened up new opportunities for the general public in the form of lowering the price of high-speed hardware and provided access to information that was previously distributed only after the signing of the NDA.
And everyone seems to be good .nix hardware: routers, Raspberry Pi and various devices a la MK802. For many, they are driving production and domestic processes, robots and coffee makers. However, the low reaction rate to external influence somewhat limits the use of such systems in embedding. Such functions, which are completely absent in such devices (meaning consumer goods, not specialized solutions once , twice ), can be PWM with emergency shutdown, high-speed PID controller, quadrature encoder processing and many, many others. All these things require a certain degree of realtime.

A brief description of some ways to add muscle, a little theory, personal reflection and of course, the solution from me under the cut.

Part one. Theory and Reflections


Let's look at the existing iron ways of solving the problem of “smart but weak”. There are several variants of such iron:
')
1. 74xx, 40xx and other multi-legged

Logics. Just logic.
A time-tested option for expanding logical functions.
Simple logic will allow you to collect several signals into one, disassemble one into several and form a digestible signal from the garbage. And do it all very quickly.
The disadvantage of this method is the large size of the scheme, which tend to grow to incredible sizes with increasing complexity.

Point two follows from the first, but I set it apart.
This is also logic, but logic is programmable.

2. CPLD and FPGA

The disadvantages of this method of iron expansion functions virtually none. It is widely used in practice, but a rather high entry threshold, a rather high price for pieces of iron and some (and in the case of FPGA even quite specific) monstrosity, do not allow to use this approach where it is not necessary to count flops and steer by tens and hundreds of logical levels .

Considering that our smart hardware is trained to communicate normally via RS-232 or SPI, and much less willingly through GPIO, we do not have a comforting conclusion: in order for our stupid fast logic to communicate with a smart slow host, you need a miracle device .


miracle device in the habitat

The miracle device must be able to accept a command from the host that is understandable to the host and issue it in a form that is understandable for logic, as well as receive data in a form that is convenient for logic, and send it to the host in a convenient form for it.
The technology is somewhat confused, but it describes the process of communicating stupid iron with smart intelligently.
In the role of a miracle device, it is customary to use a microcontroller (in the case of FPGA, you can often do without it).

3. Microcontroller

Of course the microcontroller! This resource has already seen successful precedents of crossing the router as the “smartest” and microcontroller as the exoskeleton. The trend of crossing an Android with an Arduino or STM32 has also become very noticeable in recent weeks. Actually, this trend prompted the writing of the article.

With the arrival of the MK in this scheme, the use of logic as a labor force becomes unclear, but a close examination of the electrical processes taking place in the system with buttons, relays, power converters and other control and actuation devices does not make it possible to completely abandon logic in some cases.
One of such cases may be an automaton with a certain number of parallel inputs. Suppose that the desired state of the inputs is deterministic and looks not just like “all to zero”, but has some sort of complex form. In this case there is nothing left for the microcontroller, except for how to monitor this most certain number of inputs in the operating cycle and ... do nothing else. And even in this particular case, the microcontroller does not provide comparable performance, because each individual state change of the port will be approached separately. He will need to first process the interrupt, read the contents of the port (or several), compare the state with the desired one and make his resume entry in the port. Even single-ended devices will spend a lot of time on this process. A simple logic scheme will solve this problem in the forehead, without any analysis. She will always work quickly and clearly.

Part two. Or how I came to such a life


Working at the production site as a “production jammer solver”, quite often I had to deal with the need to solder, program and flash something. Whether it was the control of the valve of the water set in the reserve tank, or the automatic switch of the heating pumps according to a given program, these were autonomous devices built on the MC. And in those prehistoric times (5–6 years ago) there were almost no available automation possibilities besides an industrial PLC and an improvised device on the MC.
Agree, because the program that serves the pump (download if the water level is lower - do not download if higher) is not worthy of a whole PLC for 100+ dollars. Anticipating comments from core circuit engineers, I hasten to declare that the MC is also a candidate for this program redundant, but it has indisputable advantages over the analog circuit - time and thresholds programmed in human numbers (instead of applying the magic of mathematics to capacitors and resistors).
And since, to solve the problem, a bare MK was chosen, apart from the obvious savings, I also get an obvious smut in the form of soldering. The balance of costs and problems was decisive in choosing a solution. Particularly critical processes and work in bad electromagnetic conditions, as it should, were trusted by the PLC without any curtsy towards the moder.

The emergence of the notorious routers for $ 20, allowed a new look at the automation of non-critical processes. The experience was twofold. On the one hand, for a penny, I got a system that speaks to me in almost human language. On the other hand, the device is fundamentally incapable of working with discrete signals at an acceptable level of speed. This imposed its limitations in the application of the new technology. Further popularization of arduin-like and boards like STM32 Discovery, predetermined a bunch of Linux devices with a low-level exoskeleton in the form of 8, 16 or 32-bit MC (plus logic, OS and other bare metal).

Simple devices were made according to the old-new scheme:
1) Take the Arduino Nano
2) we tie with infrastructure
3) ???
4) PROFIT!

As before, all this was good only for office use and in the presence of a huge amount of empty space. As for small-sized devices, everything was bad here. And if a simple device that did not need a firmware update could be done on a bare MK, then in a device for which I wasn’t particularly sure (and should be yesterday), it was dumb to install a bare MK. First, you need a programmer to upgrade the firmware. Secondly ... however, the first item was enough for me. I don’t like programmers in any way. For me, the best programmer is a USB cable.
Constant discontent with these nuances has generated a burning desire to unify, unify, unify ...

Part Three Three weeks ago


Three weeks ago, I put the next project into production (my main activity is the mass production of electronics), and the next one only appeared after about two weeks. Two weeks of browsing the Internet? Hmm ... tempting ... but something knocked in my head, and the word "unification" came to mind. And with him the old dissatisfaction with the Embedder life. On such a depressive note, I set myself the task: to create at least something to get a positive for the new project, to meet the ChSV and improve the quality of life of earthlings in two weeks.

To implement such ambitious plans, it was necessary to not less ambitious iron. And it was found among the products widely known in narrow circles of the company Microchip. This is a relatively new MK PIC16F1509. Here, I have to make a theoretical digression and tell about the nature of this irresistible force, which made modern man not use 32bit MK.

So, the periphery.
In addition to the standard peripherals (USART | SPI | I2C, 4xPWM, 10bit ADC, 5 bit DAC, 3 Timers, a crystal temperature sensor, and of course GPIO), there were some interesting innovations among the entry level MCs. These innovations, unconditionally, tipped the scales from the fashionable 32bit STM to the non-fashionable 8bit Microchip.

CLC (Configurable Logic Cell)

Programmable logic on the crystal MK.

Each of the four modules has the form:


On the left there are four inputs to which you can connect the physical outputs of the MK, interrupts from timers, comparators, PWM, a generator, and in general much of what is inside and outside the MK. The source set for one module includes 16 signals, but there are differences between the sets for each of the four modules. In particular, different physical findings MK. Next on the diagram is the switch. Signals can be connected both directly and with inversion. The outputs of the elements going after the switch OR are also controlled (direct / inverse). At the very end on the right, there is the output of the module, which can be inverted, output to the physical output of the MC, or input to the CLC. The output generates an interrupt on the front or on the decline (as configured).

The red box indicates a variable logic function, which, in addition to the one already represented by AND-OR, has 7 more options:





Unfortunately, the purpose of this article is not an educational program on digital circuitry, so there’s nothing to comment on - the illustrations speak for themselves. I will only note that all this farm is configured directly in the code, by banal assignment of values ​​to registers as the most common periphery like a timer or GPIO. To facilitate the configuration of this not the easiest figovin, there is a GUI:



With a GUI, everything becomes extremely simple. In the upper right corner you can see the values ​​of the registers, but you can simply generate a ready assembler or C code. I promise to return to this curious periphery and examples of use next time. And for those interested, I recommend to visit this page .

NCO (Numerically Controlled Oscillator)

In fact, it is a frequency synthesizer up to 500 kHz (when clocked from an internal 16 MHz oscillator) with a tuning step from 0.03 Hz (when clocked from an internal 31 kHz generator) to 15 Hz (clock 16 MHz)
Able to generate a meander and PWM.

One of the options for the use of NCO and CLC ( appnout ) gives 16bit PWM in the range of the fill factor from 0% to 100% at a frequency of PWM 500kHz.

CWG (Complementary Waveform Generator)

Programmable half bridge driver.
There are several signal sources to choose from (CLC1, NCO, PWM and Comparators).
This is a very useful thing, and in many cases it can save the developer from the fire, smoke and bitterness of loss. With the warning of these troubles, the hardware control of the dead time is intended to fight.


Each of the transistors of the pair, opens strictly when the second is probably already closed. This is done to eliminate through-current, leading to beautiful, but sometimes expensive special effects. The time given to the previous transistor to close (deadtime) is regulated separately for the leading and trailing edges. Also, the forced stop signals, which are here four, are designed to fight with special effects: a special output of GPIO, CLC2 and both comparators. Considering that we can send “what else” to the CLC2 input, we get a rather extensive list of possibilities for a speedy emergency stop of the driver without any involvement of the kernel. From timeouts to analog and logic levels.

On this note, perhaps, it is time to tie with the theory and go directly to the practice.

Part Four Development and production


Having decided on MK, I begin to think over the rest of the construct. I wanted the minimum possible footprint. After losing several options in my head, I settled on a solution that covers the area of ​​the chip in the DIP 20 package (!). Further, already starting from dimensional requirements, I chose the element base. The well-known chip CP2102 was used as a USB-TTL converter (QFN case and quartz is not needed), the USB connector was chosen vertical, and the microcontroller in the QFN case. All this stuff was immediately purchased on the maser.

With the board, too, did not particularly stand on ceremony. She, in view of her extreme simplicity, was divorced in about an hour. The power outputs are pointedly placed in the same place where they are located at the 74xx logic chips. Next, I ordered the manufacture of several pieces at the factory and began to wait. While waiting, the work on the module was carried out exclusively mental ... dreamy, I would say.

By the time all the iron was received, the initial two weeks were almost at the end. I ventured to overwhelm my own terms and get a large portion of the depression that is so harmful to a 30-year-old male body.
As a matter of urgency, as a bootloader, without which no microcontroller device can do without, the selected option was chosen - ds30 Loader . In its free version, it only allows you to load the compiled hex into the MK via UART and has a convenient GUI (well, there is a console utility for connoisseurs).

Result on the photo:

in profile next to DIP20 (ATtiny2313)


view from above


perspective ...

MK works from the built-in 16 MHz generator. Quartz, which is visible in the photo, for software RTC, which is implemented on Timer1. 18 of the 20 MK pins are directly output to the connector (two are occupied by quartz), including the RX and TX for direct RS-232 TTL connection. If you use only this very RS-232 TTL, then the board takes on a degenerate appearance and loses half the cost :

so looks bad laundered fee

Instead of conclusion


It turned out pretty nice trinket, which, however, can greatly facilitate communication with the cruel outside world for Linux-powered devices. Since the competition with Arduino in this form factor is huge, I focused on the size and unique periphery.
At the moment, in the firmware, the inclusion and configuration of parameters of several peripheral modules using RS-232 commands is implemented. These are PWM, DAC, GPIO and partly CLC. You can also find out what time it is and read the ADC (both external inputs and the crystal temperature sensor). That is, it already copes with some functions of a fast exoskeleton for a slow OS.

But even if absolutely all the periphery works under the RS-232 tune (there’s no need for a special mind), this will not be the limit. It is already clear that this functionality will take far from all the space in the MK memory, and I think the most suitable way to dispose of the remaining several kilobytes will be the limited interpreter of the SFC graphic language for the simplest automation within 30-50 steps, as well as software modules, such as PID, expansion of the number of I / Os through I2C expanders , which will enable the possibility of unified connecting of displays and keyboards.

The main sources of inspiration were Habrahabr, some Chinese website with androidobalaykami for 50 bucks and the site www.microchip.com

Peace for everyone.

Source: https://habr.com/ru/post/177803/


All Articles