Microelectronics is a fashion hobby of recent years thanks to the magical Arduino. But the trouble is: with the proper interest, it is possible to outgrow DigitalWrite () quickly, but what to do next is not entirely clear. The Arduino developers have made a lot of effort to reduce the threshold for entry into their ecosystem, but outside it, a dark forest of severe circuitry is still waving, inaccessible to the amateur.
For example, datasheets. It seems that they have everything, take it and use it. But only their authors clearly do not set themselves the task of popularizing microcontrollers; sometimes it seems that they deliberately abuse obscure terms and abbreviations when describing simple things in order to confuse the uninitiated as much as possible. But not everything is so bad, if desired, the casket opens.
In this article I will share the experience of communicating with datasheets of humanities for hobby purposes. The text is intended for lovers who have grown up from Arduino panties, it assumes some idea of ​​how microcontrollers work.
I'll start with the traditional
And immediately the code:
void setup() { DDRB |= (1<<5); } void loop() { PINB = (1<<5); for (volatile uint32_t k=0; k<100000; k++); }
"What is it? - A sophisticated reader will ask. - Why do you write something in the PINB input register? It's only read! ”Indeed, the Arduino documentation , like most educational articles on the Internet, claims that this register is read-only. I thought so myself, until I re-read the datasheet for Atmega328p, preparing this article. And there:
This is a relatively new functionality, it was not on Atmega8, not everyone knows about it or does not mention it for backward compatibility reasons. But it is quite suitable for demonstrating the idea that datasheets are worth reading in order to use all the features of the chip, including the little-known ones. And this is not the only reason.
Usually arduinschiki, playing enough with LEDs and AnalogWrite'ami, begin to connect to the board all sorts of modules and chips, for which there are already written libraries. Sooner or later a library appears that does not work as it should. Then the lover starts picking her up to fix it, and there ...
And there something categorically incomprehensible happens, so you have to go to Google, read numerous tutorials, pull some suitable code in parts, and finally get your way. This gives a powerful sense of accomplishment, but in reality the process resembles the invention of a bicycle by reverse engineering a motorcycle. And understanding how this bike works is not added. I know, since I myself have been doing this for quite some time.
If I had spent a couple of days instead of this fascinating lesson studying the documentation for the Atmega328, I would have saved a lot of time. In the end, it is a fairly simple microcontroller.
Thus, it is necessary to read datasheets at least in order to imagine how the microcontroller is generally arranged and what it can do. And further:
Dictionary
HAL, Hardware Abstraction Layer is a library for controlling a microcontroller with a high level of abstraction. If you need to use the SPI1 interface, simply configure and enable SPI1, without thinking about which registers are responsible for what.
LL, Low Level API - a library containing macros or structures with addresses of registers, allowing to refer to them by name. DDRx, PORTx, PINx on Atmega is LL.
Disputes on the topic "HAL, LL or registers" regularly occur in the comments on Habré. Without pretending to access astral knowledge, I’ll just share my amateur experiences and considerations.
Having more or less dealt with Atmega and having read articles about the beauty of the STM32, I bought half a dozen different boards - both Discovery and Blue Tablets, and even just chips for my homemade products. All of them two years gathering dust in the box. Sometimes I told myself: “everything, I’m learning STM from this weekend”, started CubeMX, generated a setup for SPI, looked at the resulting wall of text richly decorated with STM copyrights, and decided that this was somehow too much.
Understand what he has written here CubeMX, of course, you can. But at the same time it is clear that remembering all the wording, so that later you can write them with your hands is unreal. And debug this, if I accidentally forget to put a tick in Cuba - absolutely hello.
Two years have passed, I still licked at ST MCU Finder on all sorts of tasty, but inaccessible to my understanding chips, and accidentally stumbled upon a wonderful article , albeit about STM8. And suddenly I realized that all this time I was knocking on the open door: the STM's registers are arranged just like any other MC, and the Cube is not necessary for working with them. And what, so it was possible? ..
HAL and specifically STM32CubeMX is a tool for professional engineers working closely with STM32 chips. The main feature is a high level of abstraction, the ability to quickly migrate from one MK to another, and even from one core to another, while remaining within the STM32 line. Lovers rarely encounter such tasks - our choice of the MK is usually limited to the range of AliExpress, and we often migrate between radically different chips - we move from Atmega to STM, from STM to ESP, or Chinese friends will throw a new one at us. HAL does not help here, and time to study it will eat a lot.
It remains LL - but from it to the registers half a step. Personally, I find writing my macros with register addresses useful: I carefully study the datasheet, I think I will need it in the future, and what’s definitely not, better structure my programs, and overcoming it contributes to memorization.
In addition, there is a nuance with the popular STM32F103 - for it there are two incompatible versions of LL, one official from STM, the second from Leaf Labs, used in the project STM32duino. If you write an open-source library (and I had just such a task ), you must either make two versions, or access registers directly.
Finally, the rejection of LL, in my opinion, simplifies migration, especially if it is laid out from the very beginning of the work on the project. Exaggerated example: let's write arduinovsky blink in Atmel Studio without LL:
#include <stdint.h> #define _REG(addr) (*(volatile uint8_t*)(addr)) #define DDR_B 0x24 #define OUT_B 0x25 int main(void) { volatile uint32_t k; _REG(DDR_B) |= (1<<5); while(1) { _REG(OUT_B) |= (1<<5); for (k=0; k<50000; k++); _REG(OUT_B) &= ~(1<<5); for (k=0; k<50000; k++); } }
In order for this code to flash with a LED on a Chinese scarf with STM8 (from ST Visual Desktop), it is enough to change two addresses in it:
#define DDR_B 0x5007 #define OUT_B 0x5005
Yes, I use the LED connection feature on a specific board, it will blink very slowly, but it will be the same!
In articles and on forums, both Russian and English, “datasheets” mean any technical documentation for chips, and I do the same in this text. Formally, they are only one type of such documentation:
Datasheet - TTX, tactical specifications. Be sure to have any electronic component. Background information is useful to keep on hand, but thoughtfully there is nothing special to read in it. However, the simpler chips are often limited to datasheets, so as not to produce unnecessary documents; in this case, the Reference Manual is included here.
Reference Manual - the actual instruction, a healthy book on 1000+ pages. Details signs work all that ponapihano in the chip. The main document for the development of the microcontroller. Unlike the datasheet , the instructions write for the wide MK line, they contain a lot of information about the periphery that is missing in your particular model.
Programming Manual or Instruction Set Manual - instructions for the unique commands of the microcontroller. Designed for those who program in Assembler. Compiler authors actively use it to optimize the code, so in general we don’t need it. But looking here is useful for a general understanding, for some specific commands such as exiting an interrupt, as well as with the active use of a debugger.
Application Note - useful tips on solving specific problems, often with code examples.
Errata Sheet - a description of cases of non-standard behavior of the chip with bypass options, if any.
Directly in the Datasheet we may need the following sections:
Device Summary - the first page of the datasheet briefly describes the device. Very useful in situations where you have found a chip somewhere (you saw it in a store, dropped out, met a mention) and want to understand what it is.
General Description - a more detailed description of the capabilities of chips from the line.
Pinouts - pinout schemes for all possible chip cases (on which leg there is a pin).
Pin Description - a description of the purpose and capabilities of each pin.
Memory Map - we hardly need an address map in memory, but sometimes it also includes a table of addresses of blocks of registers.
Register Map - the table of addresses of register blocks, as a rule, is located in the datasheet, and in the Ref Manual - only offsets.
Electrical Characteristics - in this section, we are primarily interested in absolute maximum ratings , listing the maximum load on the chip. Unlike the unkillable Atmega328p, most of the MCs do not allow serious loads to be connected to the pins, which becomes an unpleasant surprise for the arduinschikov.
Package Information - drawings of available enclosures, useful in the design of their boards.
The Reference Manual is structurally composed of sections dedicated to the specific periphery indicated in their heading. Each chapter can be divided into three parts:
Overview , Introduction , Features - overview of the capabilities of the periphery;
Functional Description , Usage Guide or simply the main unit of the section - a detailed text description of the principles of the device peripherals and how to use it;
Registers - a description of the control registers. In simple cases such as GPIO or SPI, this may be quite enough to start using peripherals, but often you still have to read the previous parts.
Datashits with unaccustomed frighten off by their volume and abundance of incomprehensible words. In fact, everything is not so bad if you know a few life hacks.
Install a good PDF reader . Datasheets are written in the glorious traditions of paper instructions, it’s great to print them out, lay them out with plastic bookmarks and stitch them. Hypertext in them is observed in trace amounts. Fortunately, at least the structure of the document is bookmarked, so a good reader with easy navigation is very necessary.
Datashit is not a Stroustrup textbook, it does not need to read everything in a row . If you used the previous advice - just find the desired section in the bookmarks bar.
Datasheets, especially Reference Manuals , can describe the capabilities of not a specific chip, but the entire line . This means that half, or even two thirds of the information is irrelevant to your chip. Before studying the TIM7 registers, check in the General Description whether you have it.
Knowing English is enough at a basic level . Datasheets are made up of half of the terms unfamiliar to the average native speaker, and half of the simple binding structures. There are also beautiful Chinese datasheets in Chinese English, where half is also terms, and the second half is a random set of words.
If you meet an unfamiliar word , do not try to translate it using the English-Russian dictionary. If hysteresis baffles you , then the hysteresis translation will not make you any warmer. Use Google, Stack Overflow, Wikipedia, forums where the necessary concept will be explained with simple words with examples .
The best way to understand what you read is to check it out . Therefore, keep on hand a debugging board that you are familiar with, and preferably two in case you still misunderstood something and saw the magic smoke.
The habit of keeping a datasheet handy is useful when you are reading someone’s tutorial or studying someone else’s library. Quite possibly, in it you will find a more optimal solution to your problem. And vice versa - if the datasheet fails to understand how the register works, google it: most likely, someone has already described everything in simple words or left a clear code on the GitHub.
A few useful words and symbols to help you quickly get used to datasheets. What was remembered in the last couple of days, additions and corrections are welcome.
Electricity
Vcc , Vdd - plus, power
Vss , Vee - “minus”, earth
current - current
voltage - voltage
to sink current - work as "ground" for external load
to source current - feed external load
high sink / source pin - pin with increased "tolerance" to the load
Io
H, High - on pin Vcc
L, Low - on Vss pin
High Impedance , Hi-Z , floating - there is nothing on the pin, “high resistance”, it is virtually invisible to the outside world.
weak pull up , weak pull down - built-in pull-up / tightening resistor, approximate analogue 50 kOhm (see datasheet). It is used, for example, so that the input pin does not dangle in the air, causing false positives. Weak - because it is easy to "kill".
push pull - pin output mode in which it switches between High and Low - the usual OUTPUT with Arduino.
open drain - the designation of the output mode, in which the pin can be either Low or High Impedance / Floating . Moreover, it is almost always not a “real” open drain, there are protective diodes, resistors, and something else. This is just the designation of the land / nothing mode.
true open drain - but this is already a real open drain: the pin directly leads to the ground if it is open, or stays suspended, if it is closed. This means that through it, if necessary, you can release voltage more than Vcc, but the maximum is still specified in the datasheet in the Absolute Maximum Ratings / Voltage section.
Interfaces
in series - connected in series
to chain - to collect chips in a chain by serial connection, increasing the number of outputs.
shift - shift, usually denotes the bit shift. Accordingly, to shift in and to shift out - to receive and transmit data bit by bit.
latch - the latch covering the buffer while the bits are shifted through it. When the transmission is completed, the valve opens, the bits start to work.
to clock in - perform a bit-by-bit transfer, shift all bits to the right places.
double buffer , shadow register , preload register - history notation, when a register should be able to accept new data, but hold it until a certain moment. For example, for PWM to work correctly, its parameters (duty cycle, frequency) should not change until the current cycle ends, but new parameters can already be transferred. Accordingly, the current ones are kept in shadow register , and the new ones fall into the preload register , being recorded in the corresponding register of the chip.
Any
prescaler - frequency prescaler
to set a bit - set the bit to 1
to clear / reset a bit - reset the bit to 0 ( reset is the STM datasheet chip)
to toggle a bit - change the value of a bit to the opposite (see the example at the beginning of the article)
In general, the practical part was planned here with a demonstration of three projects on STM32 and STM8, made specifically for this article using datasheets, with light bulbs, SPI, timers, PWM and interrupts:
But the text is a bit much, so the projects are sent to the second part.
The skill of reading datasheets will help you with your hobby, but it hardly replaces live chat with your hobby colleagues on forums and in chat rooms. For him, one must still first of all pull up the English language. So, after reading it, there’s a special prize: two free lessons in Skyeng for the first time using the HABR2
code.
Source: https://habr.com/ru/post/449624/
All Articles