# The Basics

The first `boot.py` will be executed when the interpreter starts. This file should contain only code associated with configuration. Then `main.py` will run. It is within this `main.py` that you will place your entry point code.

## Setting the AMPY_PORT variable.

All of the commands below specify a serial port in their execution. We can use the `AMPY_PORT` environmental variable in our terminal session to avoid having to specify this port everytime.

```
export AMPY_PORT=/dev/cu.SLAB_USBtoUART
```

## Uploading files with Ampy

First review the list of commands that the `ampy` tool provides.

```
ampy --help
```
We can view the contents of the current file system:

```
ampy --port /serial/port ls
```

We can execute a command immediately on a connected board:

```
ampy --port /serial/port run test.py
```

We can also upload the code directly to the board. Notice we are passing the upload path after the file in our current directory. So, this command is placing our test.py file on the board in a file under the root dir named `main.py`. 
```
ampy --port /serial/port run test.py /main.py
```

We can pull the file from the board to our current working directory if we'd like so:

```
ampy --port /serial/port get main.py ./dirp.py
```


## Connecting to a micropython board using screen on a Mac

```
screen /dev/cu.SLAB_USBtoUART 115200
```
To detach from a screen session first press `Ctrl-A` then `Ctrl-D`.

To resume a detach session use:

```
screen -r
```

To kill a screen session press `Ctrl-A` then `k`. You will be prompted to confirm with `y\n` whether to kill the session or not.

## Memory Usage

These two functions can help you determine approximate memory usage.

```
import gc
import os

def df():
    
    s = os.statvfs('//')
    return ('{0} MB'.format((s[0]*s[3])/1048576))

def free(full=False):
    gc.collect()
    F = gc.mem_free()
    A = gc.mem_alloc()
    T = F+A
    P = '{0:.2f}%'.format(F/T*100)
    if not full: return P
    else : return ('Total:{0} Free:{1} ({2})'.format(T,F,P))
```

# installing micropython on esp32

1. Delete flash

```
esptool.py --port /dev/cu.SLAB_USBtoUART erase_flash
```

For an ESP32-S3
```
esptool.py --chip esp32-s3 --port /dev/cu.SLAB_USBtoUART erase_flash
```
2. If you have external spi ram (4MB) you want to use the spiram bin.
```
esptool.py --chip esp32 --port /dev/cu.SLAB_USBtoUART --baud 460800 write_flash -z 0x1000 ./binaries/esp32-idf4-20210202-v1.14.bin
```

For an ESP32-S3
```
esptool.py --chip esp32-s3 --port /dev/cu.SLAB_USBtoUART --baud 460800 write_flash -z 0x0000 ./binaries/GENERIC_S3_SPIRAM-20220117-v1.18.bin
```
