Skip to content

Commit 8fcfe5d

Browse files
committed
CRC.crc32_from_address
1 parent 98a1983 commit 8fcfe5d

File tree

5 files changed

+55
-9
lines changed

5 files changed

+55
-9
lines changed

mrbgems/picoruby-crc/sig/crc.rbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
module CRC
22
def self.crc32: (?String string, ?Integer crc) -> Integer
3+
def self.crc32_from_address: (Integer address, Integer length, ?Integer crc) -> Integer
34
end

mrbgems/picoruby-crc/src/mruby/crc.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,31 @@ mrb_crc_s_crc32(mrb_state *mrb, mrb_value klass)
2020
return mrb_int_value(mrb, crc_value);
2121
}
2222

23+
/*
24+
* CRC.crc32_from_address(address, length, crc = nil) -> Integer
25+
*/
26+
static mrb_value
27+
mrb_crc_s_crc32_from_address(mrb_state *mrb, mrb_value klass)
28+
{
29+
mrb_int address;
30+
mrb_int length;
31+
mrb_int crc = 0;
32+
mrb_get_args(mrb, "ii|i", &address, &length, &crc);
33+
if (address == 0) {
34+
mrb_raise(mrb, E_ARGUMENT_ERROR, "Address must not be NULL");
35+
}
36+
uint32_t crc_value = generate_crc32((uint8_t *)((uintptr_t)address), (size_t)length, (uint32_t)crc);
37+
return mrb_int_value(mrb, crc_value);
38+
}
39+
40+
2341
void
2442
mrb_picoruby_crc_gem_init(mrb_state* mrb)
2543
{
2644
struct RClass *module_CRC = mrb_define_module_id(mrb, MRB_SYM(CRC));
2745

2846
mrb_define_class_method_id(mrb, module_CRC, MRB_SYM(crc32), mrb_crc_s_crc32, MRB_ARGS_OPT(2));
47+
mrb_define_class_method_id(mrb, module_CRC, MRB_SYM(crc32_from_address), mrb_crc_s_crc32_from_address, MRB_ARGS_ARG(2,1));
2948
}
3049

3150
void

mrbgems/picoruby-crc/src/mrubyc/crc.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,40 @@ c_crc_crc32(mrbc_vm *vm, mrbc_value v[], int argc)
1818
SET_INT_RETURN(0);
1919
return;
2020
} else if (string.tt != MRBC_TT_STRING) {
21-
mrbc_raise(vm, MRBC_CLASS(TypeError), "string expected");
21+
mrbc_raise(vm, MRBC_CLASS(TypeError), "Expected a String type for the first argument");
2222
return;
2323
}
2424
uint32_t crc_value = generate_crc32((uint8_t *)string.string->data, (size_t)string.string->size, (uint32_t)crc);
2525
SET_INT_RETURN(crc_value);
2626
}
2727

28+
/*
29+
* CRC.crc32_from_address(address, length, crc = nil) -> Integer
30+
*/
31+
static void
32+
c_crc_crc32_from_address(mrbc_vm *vm, mrbc_value v[], int argc)
33+
{
34+
uintptr_t address = (uintptr_t)GET_INT_ARG(1);
35+
if (address == 0) {
36+
mrbc_raise(vm, MRBC_CLASS(ArgumentError), "Address must not be NULL");
37+
return;
38+
}
39+
mrbc_int_t length = GET_INT_ARG(2);
40+
mrbc_int_t crc;
41+
if (argc < 3) {
42+
crc = 0;
43+
} else {
44+
crc = GET_INT_ARG(3);
45+
}
46+
uint32_t crc_value = generate_crc32((uint8_t *)address, (size_t)length, (uint32_t)crc);
47+
SET_INT_RETURN(crc_value);
48+
}
49+
2850
void
2951
mrbc_crc_init(mrbc_vm *vm)
3052
{
3153
mrbc_class *module_CRC = mrbc_define_module(vm, "CRC");
3254

3355
mrbc_define_method(vm, module_CRC, "crc32", c_crc_crc32);
56+
mrbc_define_method(vm, module_CRC, "crc32_from_address", c_crc_crc32_from_address);
3457
}

mrbgems/picoruby-shell/mrblib/shell.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,28 @@ def self.ensure_system_file(path, code, crc = nil)
4040
print "Checking: #{path}"
4141
File.open(path, "r") do |f|
4242
actual_len = f.size
43-
actual_code = f.read if 0 < actual_len
44-
sleep_ms 100
45-
actual_crc = CRC.crc32(actual_code)
43+
actual_crc = if f.respond_to?(:physical_address)
44+
CRC.crc32_from_address(f.physical_address, code.size)
45+
else
46+
actual_code = f.read if 0 < actual_len
47+
CRC.crc32(actual_code)
48+
end
4649
if (actual_len == code.length) && ( crc.nil? || (actual_crc == crc) )
4750
puts " ... OK (#{code.length} bytes)"
4851
return
4952
else
50-
puts " ... NG. Updating... (len: #{code.size}<=>#{actual_len} crc: #{crc}<=>#{actual_crc})"
53+
puts " ... NG! (len: #{code.size}<=>#{actual_len} crc: #{crc}<=>#{actual_crc})"
5154
end
5255
end
5356
File.unlink(path)
5457
sleep_ms 100
5558
else
5659
File.open(path, "w") do |f|
57-
puts "Creating: #{path}"
60+
puts " Writing: #{path}"
5861
f.expand(code.length) if f.respond_to?(:expand)
5962
f.write(code)
6063
end
61-
sleep_ms 10
64+
sleep_ms 100
6265
end
6366
end
6467
File.unlink(path) if File.file?(path)

mrbgems/picoruby-shell/sig/shell.rbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ class Shell
2525
def self.setup_sdcard: (SPI spi) -> void
2626

2727
def self.setup_root_volume: (Symbol drive, ?label: String) -> void
28-
def self.ensure_system_file: (String path, String code, Integer | nil crc) -> void
29-
def self.setup_system_files: (?(String | nil) root) -> void
28+
def self.ensure_system_file: (String path, String code, ?(Integer | nil) crc) -> void
29+
def self.setup_system_files: (?(String | nil) root, ?force: bool) -> void
3030
def self.bootstrap: (String file) -> bool
3131

3232
def initialize: (?clean: bool) -> void

0 commit comments

Comments
 (0)