> For the complete documentation index, see [llms.txt](https://wiki.purduesigbots.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wiki.purduesigbots.com/software/general/embedded-programming-tips.md).

# Embedded Programming Tips

By no means is this guide meant to be an *exhaustive* list of all considerations when programming on an embedded system. However, these are some of the most important rules and conditions to keep in mind when writing low-level code running *"on the metal"*, along with what may be found in [Code Style](/software/organizing-code/code-styling-guide.md) and [Debugging](/software/general/debugging.md):

**Hardware:**

1. **Read the datasheet**. The datasheet is *always* right. Check the corner cases. Check the header files. Errors can be hiding anywhere.
2. **Check the hardware**:
3. Is there power where it belongs? Is it the right voltage?
4. Are jumpers and connections configured correctly?
5. Are external devices plugged in as intended?
6. Is the system alive? Try adding code that continuously responds to stimuli to debug potential lock-ups and resets.

**Execution path**:

1. Can the program leave the main function? *Doing so will probably execute unintended code.*
2. Do all enabled interrupts have a valid (even if blank) interrupt service routine?
3. Is the watchdog and reset control *(only available on some processors)* configured as desired?

**Interrupts:**

1. Can interrupts occur here? Should they be able to?
2. Is this section timing-critical? Should interrupts be temporarily disabled?

**Bit-shifts and bit-masks**:

1. Can a multiply or divide instruction be replaced with a [bit shift](/software/general/bit-shift.md)?
2. Does each bit mask have the correct polarity?
3. Is the order of operations (| and & evaluate *after* == for example) respected?
4. Do bit shifts properly account for a signed or unsigned variable?

**Memory usage**:

1. Can this variable be declared with a type using fewer bits (possibly using unsigned types)?
2. Are dynamic memory allocation calls checked for memory exhaustion?
3. Will the stack overflow into global variables or the heap?
4. Are there warnings about sections overlapping after the link? This is almost certainly a sign that a program is too big.

**Performance**:

1. Will overflow cause a problem with narrow variables?
2. Will the use of variables larger than the native variable size cause expensive extended-precision arithmetic?
3. Are large items passed by value (which may clandestinely copy them)?
4. Could a hardware peripheral perform the task of this software routine?

## Contributing Teams to this Article:

* [BLRS](https://purduesigbots.com/) (Purdue SIGBots)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://wiki.purduesigbots.com/software/general/embedded-programming-tips.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
