summaryrefslogtreecommitdiffstats
path: root/watch-library
diff options
context:
space:
mode:
authorjoeycastillo <joeycastillo@utexas.edu>2022-08-03 11:56:52 -0600
committerGitHub <noreply@github.com>2022-08-03 11:56:52 -0600
commite790a025787e0e1aa59b98b95e194cf4318d1578 (patch)
tree149bd53bbd46ebddd31957cc827a7c40ccf6f1a0 /watch-library
parent6d87f5a6268a9a516d8c577dfd71b39a5bfc384a (diff)
parentbcd3b666848214a735f37a5a4f08b157ba7bb3a1 (diff)
downloadSensor-Watch-e790a025787e0e1aa59b98b95e194cf4318d1578.tar.gz
Sensor-Watch-e790a025787e0e1aa59b98b95e194cf4318d1578.tar.bz2
Sensor-Watch-e790a025787e0e1aa59b98b95e194cf4318d1578.zip
Merge pull request #80 from joeycastillo/lfs
Movement: add a lil file system with lfs
Diffstat (limited to 'watch-library')
-rwxr-xr-xwatch-library/hardware/linker/saml22j18.ld13
-rw-r--r--watch-library/hardware/watch/watch.c4
-rw-r--r--watch-library/hardware/watch/watch_private.c20
-rw-r--r--watch-library/hardware/watch/watch_storage.c94
-rw-r--r--watch-library/shared/watch/watch.h13
-rw-r--r--watch-library/shared/watch/watch_private.h5
-rw-r--r--watch-library/shared/watch/watch_storage.h92
-rw-r--r--watch-library/simulator/main.c2
-rw-r--r--watch-library/simulator/shell.html9
-rw-r--r--watch-library/simulator/watch/watch.c4
-rw-r--r--watch-library/simulator/watch/watch_private.c4
-rw-r--r--watch-library/simulator/watch/watch_storage.c32
12 files changed, 281 insertions, 11 deletions
diff --git a/watch-library/hardware/linker/saml22j18.ld b/watch-library/hardware/linker/saml22j18.ld
index a9801509..211e5346 100755
--- a/watch-library/hardware/linker/saml22j18.ld
+++ b/watch-library/hardware/linker/saml22j18.ld
@@ -32,11 +32,18 @@ OUTPUT_FORMAT("elf32-littlearm", "elf32-littlearm", "elf32-littlearm")
OUTPUT_ARCH(arm)
SEARCH_DIR(.)
-/* Memory Spaces Definitions */
+/* Memory Space Definitions:
+ * 0x00000000-0x00002000: Bootloader (length 0x2000 or 8192 bytes)
+ * 0x00002000-0x0003C000: Firmware (length 0x3A000 or 237568 bytes)
+ * 0x0003C000-0x00040000: EEPROM Emulation (length 0x2000 or 8192 bytes)
+ * 0x20000000-0x20008000: RAM (length 0x8000 or 32768 bytes)
+ */
MEMORY
{
- rom (rx) : ORIGIN = 0x2000, LENGTH = 0x00040000-0x2000
- ram (rwx) : ORIGIN = 0x20000000, LENGTH = 0x00008000
+ bootloader (rx) : ORIGIN = 0x0, LENGTH = 0x2000
+ rom (rx) : ORIGIN = 0x2000, LENGTH = 0x00040000-0x2000-0x2000
+ eeprom (r) : ORIGIN = 0x00040000-0x2000, LENGTH = 0x2000
+ ram (rwx) : ORIGIN = 0x20000000, LENGTH = 0x00008000
}
/* The stack size used by the application. NOTE: you need to adjust according to your application. */
diff --git a/watch-library/hardware/watch/watch.c b/watch-library/hardware/watch/watch.c
index 32bbccbb..b3dc4e8d 100644
--- a/watch-library/hardware/watch/watch.c
+++ b/watch-library/hardware/watch/watch.c
@@ -41,3 +41,7 @@ void SYSTEM_Handler(void) {
bool watch_is_buzzer_or_led_enabled(void){
return hri_mclk_get_APBCMASK_TCC0_bit(MCLK);
}
+
+bool watch_is_usb_enabled(void) {
+ return USB->DEVICE.CTRLA.bit.ENABLE;
+}
diff --git a/watch-library/hardware/watch/watch_private.c b/watch-library/hardware/watch/watch_private.c
index 4b010d4a..cd607b8e 100644
--- a/watch-library/hardware/watch/watch_private.c
+++ b/watch-library/hardware/watch/watch_private.c
@@ -255,8 +255,15 @@ int _write(int file, char *ptr, int len) {
return 0;
}
-// this method could be overridden to read stuff from the USB console? but no need rn.
-int _read(void) {
+static char buf[256] = {0};
+
+int _read(int file, char *ptr, int len) {
+ (void)file;
+ int actual_length = strlen(buf);
+ if (actual_length) {
+ memcpy(ptr, buf, min(len, actual_length));
+ return actual_length;
+ }
return 0;
}
@@ -264,8 +271,17 @@ void USB_Handler(void) {
tud_int_handler(0);
}
+static void cdc_task(void) {
+ if (tud_cdc_n_available(0)) {
+ tud_cdc_n_read(0, buf, sizeof(buf));
+ } else {
+ memset(buf, 0, 256);
+ }
+}
+
void TC0_Handler(void) {
tud_task();
+ cdc_task();
TC0->COUNT8.INTFLAG.reg |= TC_INTFLAG_OVF;
}
diff --git a/watch-library/hardware/watch/watch_storage.c b/watch-library/hardware/watch/watch_storage.c
new file mode 100644
index 00000000..6c87be53
--- /dev/null
+++ b/watch-library/hardware/watch/watch_storage.c
@@ -0,0 +1,94 @@
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include "watch_storage.h"
+
+#define RWWEE_ADDR_START NVMCTRL_RWW_EEPROM_ADDR
+#define RWWEE_ADDR_END (NVMCTRL_RWW_EEPROM_ADDR + NVMCTRL_PAGE_SIZE * NVMCTRL_RWWEE_PAGES)
+#define NVM_MEMORY ((volatile uint16_t *)FLASH_ADDR)
+
+static bool _is_valid_address(uint32_t addr, uint32_t size) {
+ if ((addr < NVMCTRL_RWW_EEPROM_ADDR) || (addr > (NVMCTRL_RWW_EEPROM_ADDR + NVMCTRL_PAGE_SIZE * NVMCTRL_RWWEE_PAGES))) {
+ return false;
+ }
+ if ((addr + size > (NVMCTRL_RWW_EEPROM_ADDR + NVMCTRL_PAGE_SIZE * NVMCTRL_RWWEE_PAGES))) {
+ return false;
+ }
+
+ return true;
+}
+
+bool watch_storage_read(uint32_t row, uint32_t offset, uint8_t *buffer, uint32_t size) {
+ uint32_t address = RWWEE_ADDR_START + row * NVMCTRL_ROW_SIZE + offset;
+ if (!_is_valid_address(address, size)) return false;
+
+ uint32_t nvm_address = address / 2;
+ uint32_t i;
+ uint16_t data;
+
+ watch_storage_sync();
+
+ if (address % 2) {
+ data = NVM_MEMORY[nvm_address++];
+ buffer[0] = data >> 8;
+ i = 1;
+ } else {
+ i = 0;
+ }
+
+ while (i < size) {
+ data = NVM_MEMORY[nvm_address++];
+ buffer[i] = (data & 0xFF);
+ if (i < (size - 1)) {
+ buffer[i + 1] = (data >> 8);
+ }
+ i += 2;
+ }
+ return true;
+}
+
+bool watch_storage_write(uint32_t row, uint32_t offset, const uint8_t *buffer, uint32_t size) {
+ uint32_t address = RWWEE_ADDR_START + row * NVMCTRL_ROW_SIZE + offset;
+ if (!_is_valid_address(address, size)) return false;
+
+ watch_storage_sync();
+
+ uint32_t nvm_address = address / 2;
+ uint16_t i, data;
+
+ hri_nvmctrl_write_CTRLA_reg(NVMCTRL, NVMCTRL_CTRLA_CMD_PBC | NVMCTRL_CTRLA_CMDEX_KEY);
+ watch_storage_sync();
+
+ for (i = 0; i < size; i += 2) {
+ data = buffer[i];
+ if (i < NVMCTRL_PAGE_SIZE - 1) {
+ data |= (buffer[i + 1] << 8);
+ }
+ NVM_MEMORY[nvm_address++] = data;
+ }
+ hri_nvmctrl_write_ADDR_reg(NVMCTRL, address / 2);
+ hri_nvmctrl_write_CTRLA_reg(NVMCTRL, NVMCTRL_CTRLA_CMD_RWWEEWP | NVMCTRL_CTRLA_CMDEX_KEY);
+
+ return true;
+}
+
+bool watch_storage_erase(uint32_t row) {
+ uint32_t address = RWWEE_ADDR_START + row * NVMCTRL_ROW_SIZE;
+ if (!_is_valid_address(address, NVMCTRL_ROW_SIZE)) return false;
+
+ watch_storage_sync();
+ hri_nvmctrl_write_ADDR_reg(NVMCTRL, address / 2);
+ hri_nvmctrl_write_CTRLA_reg(NVMCTRL, NVMCTRL_CTRLA_CMD_RWWEEER | NVMCTRL_CTRLA_CMDEX_KEY);
+
+ return true;
+}
+
+bool watch_storage_sync(void) {
+ while (!hri_nvmctrl_get_interrupt_READY_bit(NVMCTRL)) {
+ // wait for flash to become ready
+ }
+
+ hri_nvmctrl_clear_STATUS_reg(NVMCTRL, NVMCTRL_STATUS_MASK);
+
+ return true;
+}
diff --git a/watch-library/shared/watch/watch.h b/watch-library/shared/watch/watch.h
index ce85eed3..b94d36fb 100644
--- a/watch-library/shared/watch/watch.h
+++ b/watch-library/shared/watch/watch.h
@@ -64,6 +64,7 @@
#include "watch_i2c.h"
#include "watch_spi.h"
#include "watch_uart.h"
+#include "watch_storage.h"
#include "watch_deepsleep.h"
#include "watch_private.h"
@@ -75,4 +76,16 @@
*/
bool watch_is_buzzer_or_led_enabled(void);
+/** @brief Returns true if USB is enabled.
+ */
+bool watch_is_usb_enabled(void);
+
+/** @brief Reads up to len bytes from the USB serial.
+ * @param file ignored, you can pass in 0
+ * @param ptr pointer to a buffer of at least len bytes
+ * @param len the number of bytes you wish to read, max 256.
+ * @return The number of bytes read, or zero if no bytes were read.
+ */
+int read(int file, char *ptr, int len);
+
#endif /* WATCH_H_ */ \ No newline at end of file
diff --git a/watch-library/shared/watch/watch_private.h b/watch-library/shared/watch/watch_private.h
index 7bb91d1f..9d55bc21 100644
--- a/watch-library/shared/watch/watch_private.h
+++ b/watch-library/shared/watch/watch_private.h
@@ -44,7 +44,8 @@ void _watch_enable_usb(void);
// this function ends up getting called by printf to log stuff to the USB console.
int _write(int file, char *ptr, int len);
-// this method could be overridden to read stuff from the USB console? but no need rn.
-int _read(void);
+// i thought this would be called by gets but it doesn't? anyway it does get called by read()
+// so that's our mechanism for reading data from the USB serial console.
+int _read(int file, char *ptr, int len);
#endif
diff --git a/watch-library/shared/watch/watch_storage.h b/watch-library/shared/watch/watch_storage.h
new file mode 100644
index 00000000..6026cbcf
--- /dev/null
+++ b/watch-library/shared/watch/watch_storage.h
@@ -0,0 +1,92 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2020 Joey Castillo
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+#ifndef _WATCH_STORAGE_H_INCLUDED
+#define _WATCH_STORAGE_H_INCLUDED
+////< @file watch_storage.h
+
+#include "watch.h"
+
+#ifndef NVMCTRL_ROW_SIZE
+#define NVMCTRL_ROW_SIZE 256
+#endif
+#ifndef NVMCTRL_PAGE_SIZE
+#define NVMCTRL_PAGE_SIZE 64
+#endif
+#ifndef NVMCTRL_RWWEE_PAGES
+#define NVMCTRL_RWWEE_PAGES 128
+#endif
+
+/** @addtogroup storage Flash Storage
+ * @brief This section covers functions related to the SAM L22's 8 kilobyte EEPROM emulation area.
+ * @details The SAM L22 inside Sensor Watch has a 256 kilobyte Flash memory array that can be
+ * programmed with whatever data we want. We use most of it to store the bootloader
+ * and the application code that runs on your wrist. The bootloader region is read-only,
+ * and the main application area is only writable by the bootloader (when you drag new
+ * code onto the WATCHBOOT drive). However! there's also a special 8 kilobyte region
+ * at the end of the Flash memory called the EEPROM Emulation Area. This EEPROM emulation
+ * area can be written or erased while the main Flash array is being read. This makes it
+ * super easy to work with, and useful for storing a small amount of non-volatile data that
+ * persists across reboots, even when power is lost.
+ * The functions in this section are very basic, and only cover reading and writing data
+ * in this area. The region is laid out as 32 rows consisting of 4 pages of 64 bytes.
+ * 32*4*64 = 8192 bytes. The area can be written one page at a time, but it can only be
+ * erased one row at a time. You can read at arbitrary word-aligned offsets within a row.
+ *
+ * ┌──────────────┬──────────────┬──────────────┬──────────────┐
+ * Row 0 │ 64 bytes │ 64 bytes │ 64 bytes │ 64 bytes │
+ * ├──────────────┼──────────────┼──────────────┼──────────────┤
+ * Row 1 │ 64 bytes │ 64 bytes │ 64 bytes │ 64 bytes │
+ * ├──────────────┼──────────────┼──────────────┼──────────────┤
+ * ... │ │ │ │ │
+ * ├──────────────┼──────────────┼──────────────┼──────────────┤
+ * Row 31 │ 64 bytes │ 64 bytes │ 64 bytes │ 64 bytes │
+ * └──────────────┴──────────────┴──────────────┴──────────────┘
+ */
+/// @{
+/** @brief Reads a range of bytes from the storage area.
+ * @param row The row you want to read.
+ * @param offset The offset from the beginning of the row.
+ * @param buffer A buffer of at least `size` bytes.
+ * @param size The number of bytes you wish to read.
+ */
+bool watch_storage_read(uint32_t row, uint32_t offset, uint8_t *buffer, uint32_t size);
+
+/** @brief Writes bytes to a page in the storage area. Note that the row should already be erased before writing.
+ * @param row The row containing the page you want to write.
+ * @param offset The offset from the beginning of the row. Must be a multiple of 64.
+ * @param buffer The buffer containing the bytes you wish to set.
+ * @param size The number of bytes you wish to write.
+ */
+bool watch_storage_write(uint32_t row, uint32_t offset, const uint8_t *buffer, uint32_t size);
+
+/** @brief Erases a row in the storage area, setting all its bytes to 0xFF.
+ * @param row The row you want to erase.
+ */
+bool watch_storage_erase(uint32_t row);
+
+/** @brief Waits for any pending writes to complete.
+ */
+bool watch_storage_sync(void);
+/// @}
+#endif
diff --git a/watch-library/simulator/main.c b/watch-library/simulator/main.c
index 14bf44e6..ac9db6ac 100644
--- a/watch-library/simulator/main.c
+++ b/watch-library/simulator/main.c
@@ -102,8 +102,6 @@ static void main_loop_set_sleeping(bool sleeping) {
}
int main(void) {
- printf("Hello, world!\n");
-
app_init();
_watch_init();
app_setup();
diff --git a/watch-library/simulator/shell.html b/watch-library/simulator/shell.html
index 2f560aba..80e1e2ea 100644
--- a/watch-library/simulator/shell.html
+++ b/watch-library/simulator/shell.html
@@ -323,6 +323,9 @@
<button onclick="getLocation()">Set location register (will prompt for access)</button>
<br>
+<input id="input" style="width: 500px"></input>
+<button id="submit" onclick="sendText()">Send</button>
+<br>
<textarea id="output" rows="8" style="width: 100%"></textarea>
<script type='text/javascript'>
@@ -365,10 +368,16 @@
};
lat = 0;
lon = 0;
+ tx = "";
function updateLocation(location) {
lat = Math.round(location.coords.latitude * 100);
lon = Math.round(location.coords.longitude * 100);
}
+ function sendText() {
+ var inputElement = document.getElementById('input');
+ tx = inputElement.value + "\n";
+ inputElement.value = "";
+ }
function showError(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
diff --git a/watch-library/simulator/watch/watch.c b/watch-library/simulator/watch/watch.c
index 1c965aad..749651af 100644
--- a/watch-library/simulator/watch/watch.c
+++ b/watch-library/simulator/watch/watch.c
@@ -3,3 +3,7 @@
bool watch_is_buzzer_or_led_enabled(void) {
return false;
}
+
+bool watch_is_usb_enabled(void) {
+ return true;
+}
diff --git a/watch-library/simulator/watch/watch_private.c b/watch-library/simulator/watch/watch_private.c
index 4ddc2182..3425341a 100644
--- a/watch-library/simulator/watch/watch_private.c
+++ b/watch-library/simulator/watch/watch_private.c
@@ -63,7 +63,7 @@ int _write(int file, char *ptr, int len) {
return 0;
}
-// this method could be overridden to read stuff from the USB console? but no need rn.
-int _read(void) {
+int _read(int file, char *ptr, int len) {
+ // TODO: hook to UI
return 0;
}
diff --git a/watch-library/simulator/watch/watch_storage.c b/watch-library/simulator/watch/watch_storage.c
new file mode 100644
index 00000000..27011807
--- /dev/null
+++ b/watch-library/simulator/watch/watch_storage.c
@@ -0,0 +1,32 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include "watch_storage.h"
+
+uint8_t storage[NVMCTRL_ROW_SIZE * NVMCTRL_RWWEE_PAGES];
+
+bool watch_storage_read(uint32_t row, uint32_t offset, uint8_t *buffer, uint32_t size) {
+ // printf("read row %ld offset %ld size %ld\n", row, offset, size);
+ memcpy(buffer, storage + row * NVMCTRL_ROW_SIZE + offset, size);
+
+ return true;
+}
+
+bool watch_storage_write(uint32_t row, uint32_t offset, const uint8_t *buffer, uint32_t size) {
+ // printf("write row %ld offset %ld size %ld\n", row, offset, size);
+ memcpy(storage + row * NVMCTRL_ROW_SIZE + offset, buffer, size);
+
+ return true;
+}
+
+bool watch_storage_erase(uint32_t row) {
+ // printf("erase row %ld\n", row);
+ memset(storage + row * NVMCTRL_ROW_SIZE, 0xff, NVMCTRL_ROW_SIZE);
+
+ return true;
+}
+
+bool watch_storage_sync(void) {
+ // nothing to do here!
+ return true;
+}