Project

General

Profile

crc problems accessing REST services on esp-link from mecrisp-stellaris forth

Added by mawhin 9 months ago

So.

In brief, I'm trying to use an stm32f103 bluepill running mecrisp to access the REST functions of the ESP-01.

So far, I've stolen from the work of https://hackaday.com/2017/02/13/hacking-on-the-weirdest-esp-module/ , and compiled and installed el-client on a nano clone so as to capture the bytes transferred between the nano and the esp. This along with the source code of el-client.

With me so far? Never mind, I'll press on.

My current trouble is I can't get the CRC right, and I think it might be to do with word size.

The forth I'm using:

: crc+ ( running_sum new_byte -- new_running_sum ) 
    xor
    dup 8 rshift swap 8 lshift or
    dup $ff00 and 4 lshift xor
    dup 8 rshift 4 rshift xor 
    dup $ff00 and 5 rshift xor
;

matches up nicely so far as I can see with C++ nicked from el-client:

uint16_t ELClient::crc16Add(unsigned char b, uint16_t acc)
{
  acc ^= b;
  acc = (acc >> 8) | (acc << 8);
  acc ^= (acc & 0xff00) << 4;
  acc ^= (acc >> 8) >> 4;
  acc ^= (acc & 0xff00) >> 5;
  return acc;
}

But the numbers don't come out the same. In fact the checksum converges after a handful of iterations on $fffff777.

like this: (input number, crcbefore, crcafter)

00000015 00000000 0001473C
00000000 0001473C 0144FEC1
00000002 0144FEC1 44F7FF05
00000000 44F7FF05 F7FFFF37
00000000 F7FFFF37 FFFFF77F
00000000 FFFFF77F FFFFF777
00000000 FFFFF777 FFFFF777
00000000 FFFFF777 FFFFF777

I'm intuitively convinced that this is to do with mecrisp using 32-bit integers and this is a 16-bit algorithm, but I can't get my head round it. Anyone wanna advise me upside the head on this?

I have tried using '$ffff and' to cut off the top 16 bits, which does stop the sequence converging, but still doesn't give the right answers. I'm a bit lost.

Best regards

Mart


Replies (3)

RE: crc problems accessing REST services on esp-link from mecrisp-stellaris forth - Added by jcw 9 months ago

I'd try adding $FFFF and at the end of your crc+. As you can see, the result has data in bits 16..31, these interfere when reused.

RE: crc problems accessing REST services on esp-link from mecrisp-stellaris forth - Added by mawhin 9 months ago

After much thinking and fiddling, I've cracked it. Well that bit, anyway. I had tried chopping off the top 16 bits with "$ffff and" at the end of the crc+. No joy.

So I fiddled and reasoned and extracted the crc routines from el-client and built a little program, just to checksum the string 'mecrisp' and display each step. And I got sidetracked for a bit with the difference between a logical and arithmetic left shift, and to cut a looooong story short, it turns out that it's the left shifts pushing the high bits resulting from the xors that cause the problem, and inserting '$ffff and' (or a little word that does that) after every left shift made the number match up.

Working code:

: l>h ( word32 - word16 ) \ zeros the top 16 bits
    $ffff and ;

\ CRC accumulator
: crc+ ( running_sum new_byte -- ) 
    xor
    dup 8 rshift swap 8 lshift l>h or
    dup $ff00 and 4 lshift l>h xor
    dup 8 rshift 4 rshift xor
    dup $ff00 and 5 rshift xor
    l>h
;

Next up is to get the esp to recognise that I'm sending it stuff. It doesn't seem to be having any of it at the moment. Either I'm formatting the packets wrong, which I don't think I am, or I'm missing something so far as the serial connection goes, which I don't really know much about, so I guess that's where I'm missing.

Thanks again. I'm very hopeful that I can get this combination working - a network enabled forth engine suits the shit outta me. I'm halfway through a vertical plotter build, and the firmware is forth. I've mechanicals to work out at present, and it would be very nice to (a) run the servo that lifts the pen off the drawing surface off an esp-01 and a little circuit, rather than trailing a wire, and (b) be able to pop the whole plotter on 'the wifi', with a web interface.

I'm a way away. But I know it can be done, and all the source is out there. Apparently if I print the source out and bang my face with it...... @D

Bests

Mart

RE: crc problems accessing REST services on esp-link from mecrisp-stellaris forth - Added by jcw 9 months ago

Ah yes, of course, all the left shifts.

That final l>h can now be dropped again, I think, but that's splitting hairs...

    (1-3/3)