METE4400U_Lec04_EmbeddedSystemProgramming.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Transcript

Intro. to Real-Time Embedded Systems Lecture 4: Embedded System Programming Dr. Mitchell Rushton, Fall 2024 Embedded System Programming Main two choices are Assembly and C for programming microcontroller-based embedded systems C and Assembly programs are converted into a binar...

Intro. to Real-Time Embedded Systems Lecture 4: Embedded System Programming Dr. Mitchell Rushton, Fall 2024 Embedded System Programming Main two choices are Assembly and C for programming microcontroller-based embedded systems C and Assembly programs are converted into a binary executable that can be directly loaded into memory and executed by the CPU C and Assembly both produce highly optimized code, using minimal memory and processing power, which is important in resource-constrained embedded systems. Both languages provide direct low-level hardware access and precise control over execution, essential for real-time and embedded applications Why Use C for Embedded Systems? C code is much easier to write, understand, and maintain than Assembly C is portable and largely platform independent (In contrast to Assembly) C gives direct control over memory management and provides direct hardware access (In contrast to other languages like python or Java) Its dominant use means support is widespread and tools such as compilers and libraries exist for most (if not all) platforms Why Learn assembly? The compiler first translates C into assembly language. To understand whether the compiler is doing a reasonable job, you need to understand what it has produced. Gives greater insight into how a CPU and its instruction set works Sometimes performance can be optimized by writing assembly versions of functions. Inline Assembler lets you embedded assembly directly inside a C code program May be only option if C compiler or library support is not there Data Types On a computer, all data is stored in memory as a raw binary value How should we interpret the meaning of ‘01000001’ if it’s stored in a given memory address? The concept of datatypes allows us to impose meaning on an otherwise meaningless sequence of 1s and 0s: If we assume a datatype of char, 01000001 means the letter ‘A’ If we assume a datatype of uint8, 01000001 means the number 65 Datatypes also dictate the size of a data object in terms of number of bytes Data Types in C Common data types in C int: Represents integer values. Size may vary based on architecture (typically 16-bit, 32-bit). char: Stores a single character or small integer (typically 8 bits). float: Represents floating-point numbers (32-bit precision). double: Represents floating-point numbers with higher precision (64-bit). void: No value; used for pointers or functions that return no value. Datatype Modifiers signed/unsigned: Determines whether data can be negative or only positive. Ex. unsigned int: Only positive integers (0 to 65535 on 16-bit systems). short/long: Changes the size of integers. Ex. short int: Uses less memory (16 bits). Ex. long int: Uses more memory (typically 32 or 64 bits). Exact sizing for data types available can vary based on system architecture. Verify the types and sizes available for the platform you are using Pointers A pointer is a variable that stores the memory address of another variable Operations like addition/subtraction on pointers can be used to move to adjacent locations in memory. Common Uses: Dynamic memory allocation. Managing arrays and linked lists. Function pointers for callbacks. Can lead to bugs and cause your code to fail if you’re not careful Ensure that the data at the address pointed to still exists and that the pointer is pointing to the correct location Declaration and Use of Pointers in C/C++ Pointers can be declared in C by adding a * before the variable name: int *ptr; The memory address where a variable is stored can be obtained and assigned to a pointer by adding & suffix before the data variable: int data = 10; int *ptr = &data; // ptr contains data’s memory address The data stored at the memory address pointed to by a pointer can be obtained through what is called pointer `dereferencing’: int data = 10; int *ptr = &data; int *ptr2 = ptr; // ptr2 contains data’s memory address int data2 = *ptr; // data2 contains the value 10 Data Structures A data structure is a specialized format for organizing, processing, retrieving, and storing data. Data structures can be linear (data elements arranged sequentially) e.g., Arrays, Linked Lists, Stacks, Queues or nonlinear (data elements arranged in a hierarchical or interconnected fashion) e.g., Trees, Graphs Arrays An array is a collection of elements of the same data type stored in contiguous memory locations Fixed Size (cannot grow or shrink dynamically). Provide Random access through direct indexing Advantages: Fast Access: O(1) for accessing an element via index. Disadvantages: Fixed size leads to possible waste of memory or lack of flexibility. Insertion and deletion are costly (O(n) complexity). Great for storing data when the size is known in advance https://learningc.org Linked Lists A data structure consisting of nodes where each node contains a data element and a reference (pointer) to the next node in the list Advantages: Efficient insertions/deletions (O(1) if inserting/removing at head). No need for contiguous memory like arrays. Disadvantages: Sequential access (O(n) for searching or accessing specific elements). Extra memory required for storing pointers. Great for use when the number of data elements is not known in advance https://upload.wikimedia.org/wikipedia/commons/6/6d/Singly-linked-list.svg Types of Linked Lists Singly Linked List: Each node points to the next node Doubly Linked List: Each node points to both the next and previous node. Circular Linked List: Last node points back to the first node. https://upload.wikimedia.org/wikipedia/commons/5/5e/Doubly-linked-list.svg https://upload.wikimedia.org/wikipedia/commons/d/df/Circularly-linked-list.svg https://upload.wikimedia.org/wikipedia/commons/e/e4/Lifo_stack.svg Stacks A linear data structure following the LIFO (Last In, First Out) principle Comes with two main operations: Push: adds an element to the top of the stack Pop: removes the element off the top of the stack Can be implemented using either an array or a linked list Queues A linear data structure following the FIFO (First In, First Out) principle Comes with two main operations: Enqueue: Add an element to the end Dequeue: Remove the element from the front Can be implemented using either an array or a linked list https://upload.wikimedia.org/wikipedia/commons/5/52/Data_Queue.svg Trees and Graphs Non-linear data structures consisting of nodes connected by edges Operate under the same principle as linked lists Tree Graph https://upload.wikimedia.org/wikipedia/commons/5/5f/Tree_%28computer_science%29.svg https://upload.wikimedia.org/wikipedia/commons/2/23/Directed_graph_no_background.svg Additional Reading https://learningc.org

Tags

embedded systems C programming real-time systems
Use Quizgecko on...
Browser
Browser