# Lecture notes
## Architecture
RISC-V (Reduced instruction set)
Use EEPROM to save configuration or other things (easy to write to from program)
FLASH (harder to write to)


## Microcontrollers
* AVR
* ARM

RP2040
With PIO you can create custom periphirals

## Programming
Python recommended but I'm using C++ anyway.
Rust is also usable but very hard to set up.

bootloader, Program to load programs

Arduino
board + toolchain + libraries + IDE + bootloader + Header programming


Simulation: WowKi
tinkercircuit


this week:
Browse through the datasheet of your microcontroller
write a program for  a microcontroller
simulate it.

try multiple dev environments

## Lecture notes local thursday

First C got introduced and then 20 years ago C++ got introduced. And the arduino IDE by a clever italian. 

LED's blink at 400-600 Terahertz humans can't see that but chickens can.
Micropython can't run on every microcontroller
C++ runs on anything
Rust isn't matured enough for microcontrollers and embedded


sending code to the avr microcontrollers
`avrdude`

You can run AVR's without bootloader's

Arduino bootloader connects the board to the arduino software.

The bus in a microcontroller is a data highway
I2C multiplexer. Can add more pins to a microcontroller using I2C or you can  add several devices with the same address.
ADC - analog digital convertor
DAC - digital analog converter 

In the past 5 volt was used. Nowadays a lot of 3.3 volt is used.

Always add a LED on a custom board.

Arduino uno made 20 years ago.

PlatformIO

### RP2040
Add the board library to the arduino boards manager.
`File > Prefrences > additional boards url's`
then add it in this link. `https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json`
Then you can select the board in the connect menu and then it installs the toolchain.


comment in your code. Comment why it works.

What happens when you press compile and upload
`Starts compiling > Starts linking > opens communication with microcontroller > uploads firmware > done`


basic arduino commands
* digitalWrite(pin, LOW/HIGH)


One of the most used things. Pulse Width Modulation (PWM).  

Current flows from high to low

Connecting a LED using the microcontroller as a ground is better but it also reverses the programming LOW and HIGH.
It's better because it can handle more power when it is the ground of the led

## PlatformIO + vscode

benefits using vscode
* Intellisense
* Git in the editor

First create new project

Then you can select the MCU and the framework

### For unsupported boards:
Click the nearest board. And follow the community instructions


Change platformio.ini
from
```ini
[env:pico]
platform = https://github.com/maxgerhardt/platform-raspberrypi.git
board = pico
framework = arduino
board_build.core = earlephilhower
```

to
```ini
[env]
platform = https://github.com/maxgerhardt/platform-raspberrypi.git
framework = arduino
board_build.core = earlephilhower
board_build.filesystem_size = 0.5m
[env:seeed_xiao_rp2040]
board = seeed_xiao_rp2040
```
### What is every folder
* src - for source code
* lib - project specific libraries

### How to add libraries
Click the platformIO icon
Then click the libraries button
Then you can search a library and click add to project.

![alt text](image.png)

After that it will edit platformio.ini and there you will see the library added to the project. It's also in `.pio/libdeps`

## Micropython
With micropython an OS is installed.
You can get the firmware for here. https://micropython.org/download/RPI_PICO/

To upload the firmware you need to connect it to your laptop and set the microcontroller in boot mode by pressing: hold boot > click reset > release boot. Now it will appear on your laptop as a drive. Now you can delete the old UF2 file and drop in the new micropython firmware. It should automaticly unmount itself and start the micropython firmware.

Now if you install the Raspberry Pi Pico package on vscode you can create a new project and once the project is created and you've connected the microcontroller. A new project will pop up with a terminal under it.

![alt text](image-1.png)

`Control + Shift + P` in vscode

![alt text](image-2.png)

Make sure the file is named boot.py otherwise it won't start when the microcontroller gets power.

Pulseview - to view .vcd files from logic analysers

## Very important
On linux machines you need to install platformio-udev. Otherwise you can't upload to your board. 


