Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reference/machine: added eeprom reference and machine docs #317

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ TinyGo, however, uses a register based calling convention. In fact it is somewha

Note that all native Go data types that are lowered to aggregate types in LLVM are expanded this way: `string`, slices, interfaces, and fat function pointers. This avoids some overhead in the C calling convention and makes the work of the LLVM optimizers easier.

* The WebAssembly target by default doesn't export or import `i64` (`int64`, `uint64`) parameters. Instead, it replaces them with `i64*`, allocating the value on the stack. In other words, imported functions are called with a 64-bit integer on the stack and exported functions must be called with a pointer to a 64-bit integer somewhere in linear memory.
* Some environments don't support `i64` parameters and return values (like old Node.js versions). Therefore, when targeting browsers, TinyGo will use a special ABI where these parameters and return values are returned with `i64`, allocating the value on the C stack. In other words, imported functions are called with a 64-bit integer on the stack and exported functions must be called with a pointer to a 64-bit integer somewhere in linear memory.

This is a workaround for a limitation in JavaScript, which only deals with doubles and can therefore only work with integers up to 32-bit in size (a 64-bit integer cannot be represented exactly in a double, a 32-bit integer can). It is expected that 64-bit integers will be [added in the near future] (https://github.com/WebAssembly/design/issues/1172) at which point this calling convention workaround may be removed. Also see [this wasm-bindgen issue](https://github.com/rustwasm/wasm-bindgen/issues/35).
WASI doesn't have this limitation so it will always use plain `i64` values.

Currently there are also non-browser WebAssembly execution environments that do not have this limitation. Use the -wasm-abi=generic flag to remove the behavior described above and enable emitting functions with i64 parameters directly.
We will eventually remove support for this workaround, as it is only really needed for very old Node.js versions.

* The WebAssembly target does not return variables directly that cannot be handled by JavaScript (see above about `i64`, also `struct`, `i64`, multiple return values, etc). Instead, they are stored into a pointer passed as the first parameter by the caller.

Expand Down
55 changes: 55 additions & 0 deletions content/docs/concepts/peripherals/eeprom.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
title: "EEPROM"
weight: 3
description: |
How to use the internal EEPROM.
---

For API documentation, see the [machine API](../../../reference/machine#eeprom).

The EEPROM is a form of non-volatile memory that is uusually integrated along side an MCU. EEPROMs allows you to store a relatively small amount of data by allowing bytes to be erased and reprogrammed. For the most part, EEPROMs are not integrated directly onto a board, but in some cases they are provided as a standard peripheral.

Although it may seem useful to have some type of data storage for your applications, the EEPROM is often limited in the amount of write cycles that it contains. This means that after a certain amount of writes, the EEPROM will not writeable, but you may still read data from it.
Comment on lines +10 to +12
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EEPROMs are almost always included directly on the chip, I think?
Also, here is a suggestion for the text:

Suggested change
The EEPROM is a form of non-volatile memory that is uusually integrated along side an MCU. EEPROMs allows you to store a relatively small amount of data by allowing bytes to be erased and reprogrammed. For the most part, EEPROMs are not integrated directly onto a board, but in some cases they are provided as a standard peripheral.
Although it may seem useful to have some type of data storage for your applications, the EEPROM is often limited in the amount of write cycles that it contains. This means that after a certain amount of writes, the EEPROM will not writeable, but you may still read data from it.
An EEPROM is a form of non-volatile memory that is usually integrated in an MCU. EEPROMs allow you to store relatively small amounts of data by allowing bytes to be erased and reprogrammed.
EEPROMs, like flash, can only be written a limited number of times but are not limited in how often they can be read. Therefore, they are mainly useful to store configuration values that do not change very often. The difference with flash is that individual bytes can be erased and that they usually have a longer lifespan.


## Controlling the EEPROM

Control of the EEPROM has been simplified through the `EEPROM` variable that can be accessed through the `machine` package.

Unlike most peripherals within TinyGo, you do not need to configure the EEPROM to use it.

Example usage of the EEPROM can be seen below.

```go
package main

import (
"machine"
"time"
)

// Address of the data to be stored.
// Addresses can be either in hex or dec.
const DataAddr = 0x10


func main() {
eeprom := machine.EEPROM0 // the onboard EEPROM

_, err := eeprom.WriteAt([]byte("Hello, World!"), DataAddr)
if err != nil{
panic(err)
}

// Create a buffer to read the data into
// We need to define a buffer with the correct size of the bytes we want to read.
buf := make([]byte, len("Hello, World!"))
_, err := eeprom.ReadAt(buf, DataAddr)
if err != nil {
panic(err)
}

fmt.Println(buf) // Hello, World!
}
```

This example will store a value, `Hello, World!`, into the address `0x10`.
39 changes: 39 additions & 0 deletions content/docs/reference/machine.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,45 @@ func (uart *UART) WriteByte(c byte) error
Write a single byte to the UART output.


## EEPROM

```go
type EEPROM struct {
}

var EEPROM0 = EEPROM{}
```

The `EEPROM` struct contains the collection of methods that can be used to interact with the EEPROM. The `EEPROM` struct can be accessed through the preinitialized variable: `EEPROM0`. Depending on the type of board, you may or may not have an EEPROM built-in.


```go
func (e EEPROM) WriteAt(data []byte, off int64) (int, error)
```

WriteAt writes len(data) bytes in the EEPROMs at the provided offset. This method implements the `io.WriterAt` interface.

```go
func (e EEPROM) WriteByteAt(value byte, addr int64) error
```

WriteByteAt performs the logic to writes a byte into the EEPROM at the given address.

```go
func (e EEPROM) ReadAt(buf []byte, off int64) (int, error)
```

ReadAt reads exactly len(buf) into buf at the offset. It will return the amount of bytes copied or an error if one exists.
The buffer cannot be empty, and an an error is thrown if fewer bytes are read than the size of the buffer.
This method implements the `io.ReaderAt` interface.

```go
func (e EEPROM) ReadByteAt(addr int64) (byte, error)
```

ReadByteAt reads and returns the byte at the specified address. An error is returned if there is a failure to read.


## Other

```go
Expand Down
18 changes: 3 additions & 15 deletions content/docs/reference/microcontrollers/esp32-coreboard-v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,7 @@ The esp32-coreboard-v2 is a development board based on the [Espressif ESP32](htt

### CLI Flashing on Linux

You need to install the Espressif toolchain for Linux to use TinyGo with the ESP32:

https://docs.espressif.com/projects/esp-idf/en/release-v3.0/get-started/linux-setup.html#standard-setup-of-toolchain-for-linux

In addition, you must install the `esptool` flashing tool:
You need to install the `esptool` flashing tool:

https://github.com/espressif/esptool#easy-installation

Expand All @@ -85,11 +81,7 @@ Now you should be able to flash your board as follows:

### CLI Flashing on macOS

You need to install the Espressif toolchain for macOS to use TinyGo with the ESP32:

https://docs.espressif.com/projects/esp-idf/en/release-v3.0/get-started/macos-setup.html

In addition, you must install the `esptool` flashing tool:
You need to install the `esptool` flashing tool:

https://github.com/espressif/esptool#easy-installation

Expand All @@ -106,11 +98,7 @@ Now you should be able to flash your board as follows:

### CLI Flashing on Windows

You need to install the Espressif toolchain for Windows to use TinyGo with the ESP32:

https://docs.espressif.com/projects/esp-idf/en/release-v3.0/get-started/windows-setup.html

In addition, you must install the `esptool` flashing tool:
You need to install the `esptool` flashing tool:

https://github.com/espressif/esptool#easy-installation

Expand Down
18 changes: 3 additions & 15 deletions content/docs/reference/microcontrollers/esp32-mini32.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,7 @@ The mini32 is a small development board based on the popular [Espressif ESP32](h

### CLI Flashing on Linux

You need to install the Espressif toolchain for Linux to use TinyGo with the ESP32:

https://docs.espressif.com/projects/esp-idf/en/release-v3.0/get-started/linux-setup.html#standard-setup-of-toolchain-for-linux

In addition, you must install the `esptool` flashing tool:
You need to install the `esptool` flashing tool:

https://github.com/espressif/esptool#easy-installation

Expand All @@ -85,11 +81,7 @@ Now you should be able to flash your board as follows:

### CLI Flashing on macOS

You need to install the Espressif toolchain for macOS to use TinyGo with the ESP32:

https://docs.espressif.com/projects/esp-idf/en/release-v3.0/get-started/macos-setup.html

In addition, you must install the `esptool` flashing tool:
You need to install the `esptool` flashing tool:

https://github.com/espressif/esptool#easy-installation

Expand All @@ -106,11 +98,7 @@ Now you should be able to flash your board as follows:

### CLI Flashing on Windows

You need to install the Espressif toolchain for Windows to use TinyGo with the ESP32:

https://docs.espressif.com/projects/esp-idf/en/release-v3.0/get-started/windows-setup.html

In addition, you must install the `esptool` flashing tool:
You need to install the `esptool` flashing tool:

https://github.com/espressif/esptool#easy-installation

Expand Down
18 changes: 3 additions & 15 deletions content/docs/reference/microcontrollers/m5stack-core2.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,7 @@ The [m5stack-core2](https://shop.m5stack.com/products/m5stack-core2-esp32-iot-de

### CLI Flashing on Linux

You need to install the Espressif toolchain for Linux to use TinyGo with the ESP32:

https://docs.espressif.com/projects/esp-idf/en/release-v3.0/get-started/linux-setup.html#standard-setup-of-toolchain-for-linux

In addition, you must install the `esptool` flashing tool:
You need to install the `esptool` flashing tool:

https://github.com/espressif/esptool#easy-installation

Expand All @@ -124,11 +120,7 @@ Now you should be able to flash your board as follows:

### CLI Flashing on macOS

You need to install the Espressif toolchain for macOS to use TinyGo with the ESP32:

https://docs.espressif.com/projects/esp-idf/en/release-v3.0/get-started/macos-setup.html

In addition, you must install the `esptool` flashing tool:
You need to install the `esptool` flashing tool:

https://github.com/espressif/esptool#easy-installation

Expand All @@ -145,11 +137,7 @@ Now you should be able to flash your board as follows:

### CLI Flashing on Windows

You need to install the Espressif toolchain for Windows to use TinyGo with the ESP32:

https://docs.espressif.com/projects/esp-idf/en/release-v3.0/get-started/windows-setup.html

In addition, you must install the `esptool` flashing tool:
You need to install the `esptool` flashing tool:

https://github.com/espressif/esptool#easy-installation

Expand Down
18 changes: 3 additions & 15 deletions content/docs/reference/microcontrollers/m5stack.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,7 @@ The [m5stack](https://docs.m5stack.com/en/core/basic) is a development board bas

### CLI Flashing on Linux

You need to install the Espressif toolchain for Linux to use TinyGo with the ESP32:

https://docs.espressif.com/projects/esp-idf/en/release-v3.0/get-started/linux-setup.html#standard-setup-of-toolchain-for-linux

In addition, you must install the `esptool` flashing tool:
You need to install the `esptool` flashing tool:

https://github.com/espressif/esptool#easy-installation

Expand All @@ -87,11 +83,7 @@ Now you should be able to flash your board as follows:

### CLI Flashing on macOS

You need to install the Espressif toolchain for macOS to use TinyGo with the ESP32:

https://docs.espressif.com/projects/esp-idf/en/release-v3.0/get-started/macos-setup.html

In addition, you must install the `esptool` flashing tool:
You need to install the `esptool` flashing tool:

https://github.com/espressif/esptool#easy-installation

Expand All @@ -108,11 +100,7 @@ Now you should be able to flash your board as follows:

### CLI Flashing on Windows

You need to install the Espressif toolchain for Windows to use TinyGo with the ESP32:

https://docs.espressif.com/projects/esp-idf/en/release-v3.0/get-started/windows-setup.html

In addition, you must install the `esptool` flashing tool:
You need to install the `esptool` flashing tool:

https://github.com/espressif/esptool#easy-installation

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ var (
)
```

```go
var EEPROM0 = EEPROM{}
```

EEPROM0 is the internal EEPROM on most AVRs.


```go
Expand Down Expand Up @@ -371,7 +376,6 @@ var (




### func CPUFrequency

```go
Expand Down Expand Up @@ -457,6 +461,47 @@ value of each parameter will use the peripheral's default settings.



## type EEPROM

```go
type EEPROM struct {
}
```

### func (e EEPROM) WriteAt
```go
func (e EEPROM) WriteAt(data []byte, off int64) (int, error)
```

WriteAt writes len(data) bytes in the EEPROMs at the provided offset.


### func (e EEPROM) WriteByteAt
```go
func (e EEPROM) WriteByteAt(value byte, addr int64) error
```

WriteByteAt performs the logic to writes a byte into the EEPROM at the given address.


### func (e EEPROM) ReadAt
```go
func (e EEPROM) ReadAt(buf []byte, off int64) (int, error)
```

ReadAt reads exactly len(buf) into buf at the offset. It will return the amount of bytes copied or an error if one exists.
The buffer cannot be empty, and an an error is thrown if fewer bytes are read than the size of the buffer.


### func (e EEPROM) ReadByteAt
```go
func (e EEPROM) ReadByteAt(addr int64) (byte, error)
```

ReadByteAt reads and returns the byte at the specified address. An error is returned if there is a failure to read.




## type I2C

Expand Down
Loading