fanoush / ds-d6

stuff for Desay/MPOW DS-D6 nRF52832 bracelet
162 stars 27 forks source link

golang watchdog #17

Open vendelin8 opened 2 years ago

vendelin8 commented 2 years ago

Hi,

I got my serial to ttl, and was able to flash my ds-d6 through serial. Now I'm trying to add a watchdog, reading all related stuff, not really working. Maybe you can help.

I don't have a debugger, so I'm trying to get info of vibrations, but it's hard when it breaks :)

I have something like this now:

... main ...
    if nrf.POWER.GetRESETREAS_DOG() == 1 { // reset from watchdog
        nrf.POWER.SetGPREGRET(nrf.POWER.GetGPREGRET() | 0xB1) // reset to DFU
        arm.SystemReset()
        return
    }
    if nrf.CLOCK.GetLFCLKRUN_STATUS() == 0 { // some bug they mentioned
        nrf.CLOCK.SetLFCLKRUN_STATUS(1)
    }

    nrf.WDT.CONFIG.Set((nrf.WDT_CONFIG_HALT_Pause << nrf.WDT_CONFIG_HALT_Pos) | (nrf.WDT_CONFIG_SLEEP_Run << nrf.WDT_CONFIG_SLEEP_Pos))
    nrf.WDT.CRV.Set(16 * 32768)                // 16 sec. timout
    nrf.WDT.RREN.SetBits(nrf.WDT_RREN_RR0_Msk) //Enable reload register 0
    nrf.WDT.TASKS_START.Set(1)
    for {
        time.Sleep(time.Second * 15)
        nrf.WDT.RR[0].Set(nrf.WDT_RR_RR_Reload) //Reload watchdog register 0
    }

Do you have any idea why or how? Thanks!

fanoush commented 2 years ago

describe what not really working means

btw, you should clear reset reason you don't care for anymore, it is cumulative, nobody clears it for you automatically

vendelin8 commented 2 years ago

not really working means that I can't access the device any more, I can dfu only from serial when the battery dies.

About clearing GPREGRET, I couldn't find any example stating that. And if I would, what should be set? 0 For all bits?

fanoush commented 2 years ago

GPREGRET is also cumulative, that's the point of it actually to retain data across resets but I meant the RESETREAS register, if it reboots by watchdog the flag stays there until you clear it so you may get infinite reboot loop to DFU due to if nrf.POWER.GetRESETREAS_DOG() == 1

overall the best answers you'll find in Nordic documentation - https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.nrf52832.ps.v1.1/power.html?cp=4_2_0_17_8_2#register.RESETREAS

I can't access the device any more, any I can dfu only from serial when the battery dies.

If you don't want to take apart the device and use SWD then it is more about blindly doing trial and error, there are many things that can go wrong until bluetooth is working. Now It is quite likely the code won't get even to first line of your main code. Enabling watchdog looks more or less good. Different example is e.g. here https://github.com/espruino/Espruino/blob/master/targets/nrf5x_dfu/main.c#L218

However with DS-D6 you have the serial on usb pins so you could log stuff there from your code (or even implement some debug prompt reacting to commands over serial). Also if you cannot sacrifice some nrf52 watch there are cheap nrf52832 modules on Aliexpress that can be used as a devboard to get generic NRF52 code working with debugger and then DS-D6 can be used just to test code related to screen/sensors.

It is a bit worse now after COVID but SWD debug dongle with some nrf52 devboard was for US$5 total. Now it is more like $10-15 total (e.g. this plus possibly this and this but there are many other choices). I also have plenty of those (nrf52 watches, dongles, nrf52 devboards, cables) so if you want and can pay by paypal I can supply you with any such stuff even including spare DS-D6 already opened.

fanoush commented 2 years ago

and btw the code you pasted - if it works perfectly like designed it would ping the watchdog and do nothing so there is really no way out of it as it is now. So you could ping watchdog only if button is not held or device is not on charger or something so that you could actually trigger watchdog and DFU.

vendelin8 commented 2 years ago

I already opened one. There's a red LED on my CH340G, that was on when it was connected to ds-d6 only. Now it's on only when I plug it into my laptop. I thought I broke the ds-d6 somehow, but now I'm sure that CH340G is wrong somehow. I think it's not just the LED, because since that, I couldn't program the ds-d6, that's why I opened it. Never mind, I already ordered some more CH340G-s locally, they are cheap.

Yeah, that makes sense to get a SWD debug set. If you don't need them, I'd buy a set from you, paypal works for me.

I have a nRF52832 bluetooth dongle I could use for testing. I can program it with SWD debugger only, right?

So please pack a small set just enough to SWD program these things, and let me know how much and your paypal. Revolut also works for me if you like.

And thanks.

fanoush commented 2 years ago

Oh so when you have DS-D6 already opened you can use SWD directly on it, there are two test points in the corner, the easiest without soldering is to use springloaded pogo pins. Then it is similar to this photo https://github.com/fanoush/ds-d6/blob/master/photos/bluepill-swd-serial-setup.jpg but with springloaded pins it has much better contact. With DS-D6 I use it with so called 'third hand' e.g. https://www.aliexpress.com/item/32815027285.html it stays connected due to being pressed down by its own weight.

Anyway - the SWD debugger - there are many choices, even the $4 Raspberry Pico can act as one e.g. with this uf2 file https://github.com/majbthrd/DapperMime/releases/tag/20210225

I am mostly using STLINK V2 clone dongles with original STLINK firmware - it is often fastest and pretty stable but it cannot unlock protected/locked nrf52 chips (and has no usb to serial). The second or sometimes even first choice is to use so called DAPLink a.k.a. CMSIS-DAP firmware. That is standard debugger FW provided by ARM Ltd. https://github.com/ARMmbed/DAPLink That one can also unlock locked nrf52 chips and does not need any drivers and at the same time it can also act as USB to serial like CH340G. So with DS-D6 you can connect it to usb to get serial and via two more wires also to SWD and debug it with serial output.

I have one spare bought from https://www.aliexpress.com/item/1005003081217572.html so can send that one for $4.70 plus shipping and could add cable plus few springloaded pins fitting that cable.

I also have plenty of those handy STLINK V2 dongles as described here https://github.com/fanoush/EspruinoBoards/tree/master/STLINKV2 Recently I bought few from this shop https://www.aliexpress.com/item/1005001621626894.html so that would be for $3.70 - I can either keep original STLINK FW there or can send one reflashed with CMSIS-DAP firmware so it would work exactly as that DAPLink dongle mentione earlier. They are in fact ARM based so you can reflash one with the other over SWD so having two dongles could make sense.

Then I am not sure if you want nrf52832 devboard too or DS-D6 is enough? I can sent that previously linked nrf52 module with test board for $9 total - advantage is that you have every nrf52832 pin available and that module has external 32khz crystal fitted (unlike DS-D6). Or I also use board called E104-BT5032A-TB - that one has no crystal so act like DS-D6 (and hang in same way when code requires external 32kHz crystal) and it also has usb to serial adapter on board which can be completely disconnected via jumpers and it also has two buttons and two leds - however only subset of nrf52 pins are available, some are completely missing. More info here https://www.ebyte.com/en/product-view-news.html?id=956 This one I cannot see on aliexpress now for sane price (one is here https://www.aliexpress.com/item/1005003297052472.html ). would sell that one for $9 too.

So the choice is yours let me know what you want :-) I think yo ucan find some way to send me private message/email with details. You can also PM me via gitter https://gitter.im/nRF51822-Arduino-Mbed-smart-watch/Lobby Depending on your country and total weight trackable registered shipping to you can be in $6-$10 range.

vendelin8 commented 2 years ago

Now you mention, I figured out that I have a nrf52840 dongle https://hken.rs-online.com/web/p/wireless-adapters-wifi-dongles/1769054/ . It's in the tinyUSB list https://github.com/hathach/tinyusb/blob/master/docs/reference/supported.rst , that can be used with https://github.com/majbthrd/DapperMime . As far as I understand. I'll try to use that as a programmer, and if it doesn't work, I'll ask, or buy some other parts from you.

For dev board, I also have a nrf52832 dongle that looks like https://www.aliexpress.com/item/32950640108.html . My only concern with it is that I don't know how to check if it's in dfu when testing watchdog. Anyway, an opened ds-d6 is fine for now.

Missing 32kHz crystal is not a problem, it can be set in the board descriptor file for tinygo that it doesn't have one.

Thanks.