📜 ⬆️ ⬇️

Lecture number 02: MSP430. Cities and Registers

Translator's note: Not so long ago, the LaunchPad I ordered came to me. The only thing I wanted then was to immediately start flashing the LEDs. So I ran into the first problem - I have Linux. After reading about the main IDE for development for this platform, I realized that nothing good shines for me here. And the use of Energia, just simply did not allow religion. I have a habit (I don’t know if it’s good or not) not to work with such tools of “increased abstraction” until I study the background of what I’m working with. Therefore, Energia quickly dropped out of the options. Only one thing remains - to assemble the gcc-msp430 toolchain, which includes everything you need. My laptop is very weak, so I was going to this toolchain for half a day already. That in no way pleased me, since I already wanted to FLASH. When collected, faced with the following difficulty. Very little Russian-language literature and help on this microcontroller. Something asked among the habrovchan, something was somehow able to pull out of the code examples from TI, but the problem remained - I had little understanding of everything I was doing. It lasted exactly until I came across a wonderful English-language blog , which, although in English, explained all the basics fairly well. I began to read and translate it. I admit honestly, I wrote this translation not so much for Habra as for myself, in order to understand the material presented 100%. To say that I understood everything 100% - to lie a little. Frankly speaking, I’m far from electronics, at the time of the launch pad I had zero knowledge in this field. Therefore, some moments of the translation may, probably, make the more serious developers cry in tears of blood. For example - Vcc and Vss or PxREN. I understand the idea, but I doubt that I translated it technically competently. However, I tried to translate so that it was clear to the same zero as me. In general, if that - do not blame me. How could. In addition, there are paragraphs in the translations that are written purely by myself in order to chew the material in a little more detail.
Oh, yes, I began to translate from lecture 02, because the first “Introductory” lecture contains too much water, which is already the sea and in the vast Habra, and indeed the Internet. We want to work right away, not read about how well TI did when releasing cheap LaunchPads.

So.

Most of the articles on MSP430, what I have seen, almost from the first lines told about processor architecture, memory addressing, data buses and other technical things, which, unfortunately, a person without experience in electronics is hard to understand. In this article I will try to explain to you the basics necessary to start working with this family of microcontrollers (hereinafter referred to as MK), without any abstruse things like bipolar connections and the like. Deep technical knowledge would be very helpful in fine-tuning the system being developed or programming in assembly language, but also elementary knowledge of the C language is enough to begin quite comfortable and productive work.

One of the features that distinguishes the MSP430 microcontroller from the main mass is the von Neumann architecture: all the data that the processor can operate with are located in the same address space. The rest of the microcontrollers are different in this respect: their data can be located in different address spaces, which increases the efficiency, but considerably complicates the work with them, but oh, how we don’t want it. To quickly understand what it is about, you can imagine a microcontroller as a city: in the background Neumann cities (like MSP430) all people live in the same city and have their own unique address. Well, for example, there are several cities in Harvard architecture, therefore addresses can be repeated. Thus, if you want to send a letter to a specific address, you will first have to specify the city.
')

Memory


The MSP430's address space includes the area for storing the program itself, the area for storing the data needed by this program, RAM, and other important information that we will look at later on. Today we will satisfy all your insatiable craving for knowledge by studying addresses in memory, which are called “registers”. These addresses are like various municipal and social services (such as post office or housing and communal services) in our small MSP430 town. In our microcontroller, the registers control various functions of the processor and peripherals. Imagine a room with many different switches and buttons on which it depends whether tomorrow in your area will turn on heating or hot water - these will be our registers. Like any other switches, they have only two states - “on” and “off”, in computer language - 1 and 0. In the microcontroller, the state of the registers determines how one or the other periphery will behave, for which one or another leg of your MK, what happens when a certain event occurs, and so on and so forth. The registers in MSP430 are grouped into 3 sections:
1) Special Functions Register (English: Special F Unction R egisters (SFR)) .
(comment of the translator .: similar abbreviations should be remembered, or at least recorded, in the future, they will be used quite often)
2) 8-bit registers for various processor peripherals (for storing the “settings” of the peripherals with which only 8 bits are sufficient)
3) 16-bit registers (the same, only 16 bits are allocated).
As an example, let's take a look at the smallest member of the MSP430 family, namely the MSP430G2001 microcontroller. Open the following documents: x2xx series description and G2x01 specifications .

Note that the above links may be non-existent, this happens if Texas Instruments does update the documentation. The easiest way to find these documents is to go to the official TI website and find the “Search by Part Number” block. In order to find the documentation on the G2x01 series, it is enough to enter any marking that falls under this mask, for example msp430g2001.

Let's take a look at the fourth part of the first chapter (Address Space) of the x2xx series description (English: family guide) , which illustrates the map of our “town” of the x2xx family. As you can see, the SFR registers are located starting at address 0h and ending at Fh (the h character indicates that the address is written in hexadecimal; in decimal, they would look like 0 and 15). Next come the 8-bit registers; their addresses begin with 010h and end with the address 0FFh (in the decimal system: from 16 to 255), then, from the address 0100h to 01FFh there are 16-bit registers. You have probably noticed a huge empty space between the operating and flash memory areas (note: do not confuse flash memory with USB drives or other external storage devices, in this case, flash memory is “reprogrammable memory”) . The description of this series of microcontrollers shows us the areas of memory that belong to the entire x2xx family, in order to see the exact values ​​for any particular microcontroller, you need to refer to the specifications of this chip, called datasheet (Eng .: datasheet - get used to this name, everywhere it is used exactly) .

The eleventh page of datasheet provides us with a memory map, which fits all microcontrollers of the G2x01 and G2x11 series. Looking at the bar from G2001, you can see that the service peripherals registers (SFR, 8-bit, 16-bit) go first, then 128 bits of RAM are used. The address space, starting at address 0201h and ending with 10FEh, is empty; after it we see 256-bit memory, called the service memory area (this memory area stores calibration data and other important values ​​that need to be saved if the device has stopped receiving power Usually this data is not available for rewriting, but this does not mean that it is impossible under real need, but it is a separate story that deserves a separate lecture). As a result, we have 512 bytes of memory for program code and, to top it all, the memory allocated for interrupt vectors (but this is also discussed in another lecture).

Registers


Now we can see how our “G2001-town” looks from the inside, therefore, we should figure out how to work with the very “switch houses” that we described above. The registers in the MSP430 microcontroller are special sections in memory, with the help of which it is configured and which inform us when something important happens. Page 10 of our datasheet shows which special function registers (SFR) are available in our G2001. As we can see, this device has only four registers of specials. functions located at addresses 0h through 3h: interrupt enable register 1 (English: I nterrupt E nable 1 (IE1)) , interrupt enable register 2 (IE2), and two register interrupt flags IFG1 and IFG2 (English: I nterrupt F la G Register) . Each address points to 1 byte in memory, and in one byte, as we know, 8 bits. Remember our “switch houses”? So, each bit in the register performs the role of one switch. Thus, the register itself is a home, and its every bit is a switch that has two states 0 (off) and 1 (on). Each of these bits is very important, the state of each of them in one way or another affects the operation of the microcontroller and its behavior in general. Page 10 also shows us which bits in these registers are available on our G2001.

In this case, in the IE1 register, only 0, 1, 4, and 5 bits are available to us, while in IFG1 all bits from 0 to 5 are available to us. The datasheet contains full information about the name of each of these bits and what function it performs. For example, the zero bit in the IE1 register is called WDTIE — the resolution of the watchdog timer interrupt (English: W atch D og T imer I nterrupt Enable) . By default, this bit is set to 0, but if we change it and set it to 1, we will allow the watchdog timer to “raise” the interrupt flag in the IFG1 register (bit number 0, to be exact), thereby causing an interrupt (as already it was said that we will consider such interruptions later). Simply put, this bit tells our MSP430 whether the watchdog timer can signal the processor to do something or use it normally. It would probably be worth explaining what a watchdog is and what its “normal” mode is. A watchdog timer is needed in order to reset the processor to the state in which it was at the time the power was applied. He drops it regularly, at a certain interval. Why is it necessary? If a program crashes while the program is running, the watchdog timer resets the program so that it can work again. Of course, you can disable it. When we translate the watchdog timer into an interval mode, instead of resetting our program, it simply sends a signal to the processor. How to handle this signal and what to do when the processor receives this signal - this decision falls on the programmer’s shoulders. This is usually used to perform any cyclical actions. For example, in the most elementary case to blink an LED, than we and we will soon be engaged.

Now take a look at page 14 of our datasheet. This table provides us with all the information about existing peripheral “devices”, the addresses of their registers and their names. Incidentally, at the bottom of the table is the information about SFR, which we have repeatedly mentioned in this article. Thus, when you want to use any peripherals of your microcontroller, this table will be your starting point in order to find out exactly which “switch houses” you need to visit in order to be sure that all your registers are configured correctly. Today, we will take a closer look at the registers intended for port 1 (Port P1) and port 2 (Port P2), since they are the main elements that are needed to work with our microcontroller.

Ports are the main input / output devices available on the microcontroller, input / output is done through the legs (English: pin) of your MK. You can see the pinout of your device on the third page of the datasheet. A port usually means a set of 8 pins of a microcontroller. But the pins at the port may be less, it happens if the legs are simply not enough to complete the set. The pinout shown in the datasheet shows us that the G2001 has a full set of 8 legs for port 1 (c P1.0 to P1.7) and two legs for port 2 (p2.6 to P2.7). Each register of each port has a bit that corresponds to the corresponding leg of the microcontroller. For example, P1.4 is controlled by the 4th bit of each register that belongs to port 1 (P1). Let's take a closer look at the registers that the microcontroller offers us.

Pxin


An input register (input mode) whose values ​​are in read-only mode (English: read-only) . If the direction of the foot was selected as “incoming” (English: IN put) , then the value of this register will tell you whether the voltage is currently applied to this foot or not. It should be understood that reading the value of the PxIN register returns you data on all the legs of the corresponding port at a time. You should not forget that this is digital technology, so each bit read can only be in two states: 1 or 0. These values ​​show us what voltage is applied to the leg: Vss (if the value is 0) or Vcc (if a value of 1), a value of 0 is accepted if the applied voltage is below 1.8 volts (Vss <1.8V), and a value of 1 is accepted if the supplied voltage falls in the range between 1.8 and 3.6 volts (1.8V <= Vcc <= 3.6V). To avoid damage to your device, NEVER try to directly apply voltage above the Vcc high threshold. In a nutshell, Vcc is voltage, and Vss is earth.

PxOUT


"Output" register (output mode), available for writing. When a certain pin of your microcontroller is set to “output mode” (English: OUT put) , you can energize it by simply setting the corresponding bit in this register to 1. Just as in the PxIN register, with a value of 0, voltage is applied to the leg with Vss, if 1 - Vcc.

PxDIR


The direction register (English: DIR ection) determines the pin to the input mode, if the corresponding bit of this register is set to 0 and to the output mode, if 1. At the very beginning of your program, you must specify all the pin directions you plan to use. Of course, this does not mean that you cannot change these values ​​somewhere in the middle of the program.

PxIE, PxIES, and PxIFG


The following three registers are available at our ports: the interrupt enable register (English: I nterrupt E nable (IE)) , the control interrupt register (English: I n interrupt E dge S elect (IES)) and the interrupt flag register (eng. : I nterrupt F laG (IFG)) . We consider these three registers together, since working with any of these registers involves working with the two remaining ones, they are inseparable from each other. What is interruption? At the moment, it will be enough for you to simply imagine that this is a kind of message sent to the processor, upon receipt of which it suspends all its affairs and begins to perform the actions that are prescribed for execution upon receipt thereof. As soon as he completes the processing of the interrupt, the processor returns to its previous work as if nothing had happened and continues the execution of the main program. To allow a specific pin of your port to generate interrupts, you simply “raise” (set to 1) the corresponding bit in the interrupt enable register (PxIE where x is the port number). The control interrupt register (PxIES) contains control bits (edge-bits) with which the current state of the PxIN register is compared. Simply put, if you have legs that are assigned to the input-mode in the PxDIR register (the bits corresponding to the legs are assigned the value 0), then the PxIN register is compared in bits to all the values ​​of the PxIES register and if it happens that the values ​​of the corresponding bits in these registers have different meanings, the next thing the processor does is to check if interrupt legs are allowed for these legs — compares with the PxIE register — and if interrupts are enabled — an interrupt is generated, and this, in turn, is nothing more than a “raise” corresponding bits in the PxIFG register (interrupt flag register).
Once again in brief: the interruption for a certain leg of your microcontroller is considered generated if a flag is raised in the PxIFG register (the bit has assumed the value 1) which corresponds to this leg with its sequence number. The PxIE register bits determine which legs of your microcontroller are allowed to raise these flags, if in the PxIE register the bit with the sequence number 0 is 0, then this leg simply does not have access to the PxIFG register, and therefore cannot generate interrupts. The PxIES register stores control values. The processor comparing the PxIN and PxIES registers generates interrupts for the legs, the bits of which in these registers differ.

PxSEL


If you look again at the third page of our datasheet, where the pinout of our microcontroller is shown, you can see that each leg has several functions, which are separated by a slash. This selection register (English: SEL ection Register) determines which function a pin will perform. The function that a particular foot performs by default is listed first - so we can see that all the legs of the first port (P1) are by default just configured for input and output (I / O - Input / O utput) , while as the legs of the P2 are by default connected to a quartz oscillator. Changing the bits in PxSEL will change the basic function of the corresponding foot. We will consider all this later, now our main task is to learn how to use standard functions that are predefined in this register.

PxREN


The resistor enable register (English: R esistor EN able register) is a very convenient port function. Sometimes it is necessary to manually apply the voltage or, on the contrary, to stop the supply thereof. For example, if you connect a button to your microcontroller. The resistor enable register gives you this opportunity. When the bit corresponding to your pin in this register is “raised”, the pitch can be adjusted by setting the same bit in the PxOUT register to 1 or 0.

Well, we looked at some of the “switches” available to us, so in order to consolidate what we have covered, in the next article we will look at how to use them when writing our program and downloading it to our device.

Original article.
A separate “thank you”, I would like to say to Megafon, which for some reason has blocked access to this blog.

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


All Articles