Why / Why Not C++? • Contains all of the core features of C plus some extras • More secure than C o Type checking o String Literals, enumerated constants, etc • Possibly more scaleable than C o Object Oriented nature o Overloaded functions and constructors o Data Abstraction • Not ideal for highly resource constrained systems o The added features have some overhead and reduce efficiency o With modern technology this may not be as much of a concern anymore
Optimized Data Types • The compilers job is to generate the most efficient code possible o For this reason, declaring something as simple as an int can be 8, 16, 32, or 64 bits depending on the most efficient size for the processor o As a result, multiple compilers could use different sizes for the same code • In embedded programming, the compiler “helping” can actually do the opposite o In many circumstances the data size actually matters o Bit manipulations, copying memory, etc…
Fixed Data Types • To solve the Size Signed Unsigned optimized data type 8 Bits int8_t uint8_t problem it is 16 Bits int16_t uint16_t recommended to 32 Bits int32_t uint32_t define specific data 64 Bits int64_t uint64_t types for specific sizes on a processor o C99 actually requires it to be identified in the compiler o Otherwise the user should define it using typedefs of char, short, long, long long (if applicable)
Registers • A register is used by the CPU to store information on a temporary basis o Store data to be processed o Store address pointing to the data which is to be fetched o Usually located within processors memory space or I/O space o Because of the location registers are generally very fast • There are two main types of registers o General Purpose Registers o Special Purpose Registers
General Purpose Registers • Used to hold data values before and after calculations • These are used by the compiler for loading and storing data and/or addresses as well as performing calculations
Special Purpose Registers • Special Purpose Registers are designed for a specific task. • Contain the state of the program • Includes the following: o Instruction Register – holds instruction being executed o Memory Data Register – holds data fetched from memory o Memory Address Register – next address of memory to fetch o Program Counter – location of next instruction to execute o Control/Status Register(s) – used to control various CPU functionalities and check the status
Special Purpose Registers: Memory Mapped I/O • Device drivers communicate with peripheral devices using device registers o Sends commands / data to the device o Retrieves status / data from the device • Memory Mapped I/O maps device registers to fixed addresses in memory o This allows registers to be accessed in the same manner on would access any location in memory o Programs can use ordinary assignment operators to write / read from the registers
Bit Manipulation • Bit manipulation is used to manipulate data and the contents of registers o Particularly used for configuring and reading the status of hardware o Keep in mind that bit manipulation works on just that, bits • So a uint8_t would execute on all 8 bits • Supported bitwise operators o AND (&) o OR (|) o NOT (~) o XOR (^) o Left Shift (<<) o Right Shift (>>)
Bit Manipulation Examples • Testing Bits A 0110 1001 o The AND operator is used to test if a bit is set B 1100 0011 • Setting Bits o The OR operator is used to set a bit A&B 0100 0001 • Clearing Bits o The AND and NOT operators are used to clear A|B 1110 1011 bits • Toggling Bits A & ~B 0010 1000 o The XOR operator is use to toggle a bit • Shifting Bits A^B 1010 1010 o The SHIFT operators are used to shift bits left or right • Bitmasks A >> 1 0011 0100 o Used to operate on bits in a field A >> 2 0001 1010 o For example – to only operate I the lowermost 2 bits of an 8 bit field are set A << 1 1101 0010 • #define LOWER_2_BITS 0x3 A << 2 1010 0100 • If ( ( val & LOWER_2_BITS ) == LOWER_2_BITS ) { do something }
Homework & Quiz #3 • Review the registers for the STM32 board and its peripherals • Homework Assignment #3 will consist of: o STM32 Registers o Bit Manipulation • This weeks quiz will focus on: o Data Types o STM32 Registers o Bit Manipulation