first cut at turnon scripts for EasyTimer v2
[fw/altos] / src / kernel / ao_storage.c
index 73b31c6420005cc7ee75d65a19c5e1b0e6302842..a2c3738de6094d58d7fa2d31bd65f080da312214 100644 (file)
 #include <ao.h>
 #include <ao_storage.h>
 
-#define AO_STORAGE_DATA_SIZE   256
+#define AO_STORAGE_DATA_SIZE   128
 
 static uint8_t storage_data[AO_STORAGE_DATA_SIZE];
+static uint8_t storage_mutex;
 
 uint8_t
 ao_storage_read(ao_pos_t pos, void *v_buf, uint16_t len)
@@ -38,7 +39,7 @@ ao_storage_read(ao_pos_t pos, void *v_buf, uint16_t len)
                /* Compute portion of transfer within
                 * a single block
                 */
-               this_off = (uint16_t) pos & (ao_storage_unit - 1);
+               this_off = (uint16_t) (pos & (ao_storage_unit - 1));
                this_len = ao_storage_unit - this_off;
                if (this_len > len)
                        this_len = len;
@@ -69,7 +70,7 @@ ao_storage_write(ao_pos_t pos, void *v_buf, uint16_t len)
                /* Compute portion of transfer within
                 * a single block
                 */
-               this_off = (uint16_t) pos & (ao_storage_unit - 1);
+               this_off = (uint16_t) (pos & (ao_storage_unit - 1));
                this_len = ao_storage_unit - this_off;
                if (this_len > len)
                        this_len = len;
@@ -91,22 +92,31 @@ ao_storage_is_erased(uint32_t pos)
        uint32_t        read_pos;
        uint32_t        read_len;
        uint32_t        i;
+       uint8_t         ret = 1;
 
+       ao_storage_setup();
+       ao_mutex_get(&storage_mutex);
        read_pos = pos;
        read_len = ao_storage_block;
        while (read_len) {
                uint32_t this_time = AO_STORAGE_DATA_SIZE;
                if (this_time > read_len)
                        this_time = read_len;
-               if (!ao_storage_read(read_pos, storage_data, this_time))
-                       return 0;
+               if (!ao_storage_read(read_pos, storage_data, (uint16_t) this_time)) {
+                       ret = 0;
+                       goto done;
+               }
                for (i = 0; i < this_time; i++)
-                       if (storage_data[i] != 0xff)
-                               return 0;
+                       if (storage_data[i] != AO_STORAGE_ERASED_BYTE) {
+                               ret = 0;
+                               goto done;
+                       }
                read_pos += this_time;
                read_len -= this_time;
        }
-       return 1;
+done:
+       ao_mutex_put(&storage_mutex);
+       return ret;
 }
 
 uint8_t
@@ -144,25 +154,29 @@ static void
 ao_storage_dump(void) 
 {
        uint32_t block;
-       uint8_t i, j;
+       uint8_t i, j, k;
 
        block = ao_cmd_hex();
        if (ao_cmd_status != ao_cmd_success)
                return;
-       for (i = 0; ; i += 8) {
+       ao_mutex_get(&storage_mutex);
+       for (i = 0; ; i += AO_STORAGE_DATA_SIZE) {
                if (ao_storage_read((block << 8) + i,
                                    storage_data,
-                                   8)) {
-                       ao_cmd_put16((uint16_t) i);
-                       for (j = 0; j < 8; j++) {
-                               putchar(' ');
-                               ao_cmd_put8(storage_data[j]);
+                                   AO_STORAGE_DATA_SIZE)) {
+                       for (k = 0; k < AO_STORAGE_DATA_SIZE; k += 8) {
+                               ao_cmd_put16((uint16_t) i + k);
+                               for (j = 0; j < 8; j++) {
+                                       putchar(' ');
+                                       ao_cmd_put8(storage_data[k + j]);
+                               }
+                               putchar ('\n');
                        }
-                       putchar ('\n');
                }
-               if (i == 248)
+               if (i == 256 - AO_STORAGE_DATA_SIZE)
                        break;
        }
+       ao_mutex_put(&storage_mutex);
 }
 
 #if HAS_STORAGE_DEBUG
@@ -214,6 +228,9 @@ ao_storage_zapall(void)
 
 #if AO_STORAGE_TEST
 
+#define AO_STORAGE_TEST_SIZE   256
+static uint8_t storage_test[AO_STORAGE_TEST_SIZE];
+
 static void
 ao_storage_failure(uint32_t pos, char *format, ...)
 {
@@ -230,16 +247,16 @@ ao_storage_check_block(uint32_t pos, uint8_t value)
        uint32_t        offset;
        uint32_t        byte;
 
-       for (offset = 0; offset < ao_storage_block; offset += sizeof (storage_data)) {
-               if (!ao_storage_read(pos + offset, storage_data, sizeof (storage_data))) {
+       for (offset = 0; offset < ao_storage_block; offset += sizeof (storage_test)) {
+               if (!ao_storage_read(pos + offset, storage_test, sizeof (storage_test))) {
                        ao_storage_failure(pos + offset, "read failed\n");
                        return 0;
                }
-               for (byte = 0; byte < sizeof (storage_data); byte++)
-                       if (storage_data[byte] != value) {
+               for (byte = 0; byte < sizeof (storage_test); byte++)
+                       if (storage_test[byte] != value) {
                                ao_storage_failure(pos + offset + byte,
                                                   "want %02x got %02x\n",
-                                                  value, storage_data[byte]);
+                                                  value, storage_test[byte]);
                                return 0;
                        }
        }
@@ -252,10 +269,10 @@ ao_storage_fill_block(uint32_t pos, uint8_t value)
        uint32_t        offset;
        uint32_t        byte;
 
-       for (byte = 0; byte < sizeof (storage_data); byte++)
-               storage_data[byte] = value;
-       for (offset = 0; offset < ao_storage_block; offset += sizeof (storage_data)) {
-               if (!ao_storage_write(pos + offset, storage_data, sizeof (storage_data))) {
+       for (byte = 0; byte < sizeof (storage_test); byte++)
+               storage_test[byte] = value;
+       for (offset = 0; offset < ao_storage_block; offset += sizeof (storage_test)) {
+               if (!ao_storage_write(pos + offset, storage_test, sizeof (storage_test))) {
                        ao_storage_failure(pos + offset, "write failed\n");
                        return 0;
                }
@@ -269,17 +286,17 @@ ao_storage_check_incr_block(uint32_t pos)
        uint32_t        offset;
        uint32_t        byte;
 
-       for (offset = 0; offset < ao_storage_block; offset += sizeof (storage_data)) {
-               if (!ao_storage_read(pos + offset, storage_data, sizeof (storage_data))) {
+       for (offset = 0; offset < ao_storage_block; offset += sizeof (storage_test)) {
+               if (!ao_storage_read(pos + offset, storage_test, sizeof (storage_test))) {
                        ao_storage_failure(pos + offset, "read failed\n");
                        return 0;
                }
-               for (byte = 0; byte < sizeof (storage_data); byte++) {
+               for (byte = 0; byte < sizeof (storage_test); byte++) {
                        uint8_t value = offset + byte;
-                       if (storage_data[byte] != value) {
+                       if (storage_test[byte] != value) {
                                ao_storage_failure(pos + offset + byte,
                                                   "want %02x got %02x\n",
-                                                  value, storage_data[byte]);
+                                                  value, storage_test[byte]);
                                return 0;
                        }
                }
@@ -293,10 +310,10 @@ ao_storage_fill_incr_block(uint32_t pos)
        uint32_t        offset;
        uint32_t        byte;
 
-       for (offset = 0; offset < ao_storage_block; offset += sizeof (storage_data)) {
-               for (byte = 0; byte < sizeof (storage_data); byte++)
-                       storage_data[byte] = offset + byte;
-               if (!ao_storage_write(pos + offset, storage_data, sizeof (storage_data))) {
+       for (offset = 0; offset < ao_storage_block; offset += sizeof (storage_test)) {
+               for (byte = 0; byte < sizeof (storage_test); byte++)
+                       storage_test[byte] = offset + byte;
+               if (!ao_storage_write(pos + offset, storage_test, sizeof (storage_test))) {
                        ao_storage_failure(pos + offset, "write failed\n");
                        return 0;
                }
@@ -369,13 +386,13 @@ ao_storage_fill(void)
                return;
        printf("erase "); flush();
        ao_storage_erase(0, ao_storage_log_max);
-       for (pos = 0; pos < sizeof (storage_data); pos++)
-               storage_data[pos] = (uint8_t) pos;
-       for (pos = 0; pos < ao_storage_log_max; pos += sizeof (storage_data)) {
+       for (pos = 0; pos < sizeof (storage_test); pos++)
+               storage_test[pos] = (uint8_t) pos;
+       for (pos = 0; pos < ao_storage_log_max; pos += sizeof (storage_test)) {
                if ((pos & 0xffff) == 0) {
                        printf("Fill 0x%x\n", pos); flush();
                }
-               ao_storage_write(pos, storage_data, sizeof (storage_data));
+               ao_storage_write(pos, storage_test, sizeof (storage_test));
        }
        printf("Fill complete\n");
 }