BMA421 Accelerometer #6844
Replies: 1 comment
-
Posted at 2021-04-30 by @fanoush
yes, both ways are possible. if you want to use that library 'as is' then linking it to your espruino build may be much easier. otherwise it can be done from inline C or even JS if it is not too complicated - just read/write registers according to datasheet and setting a watch on interrupt pin Posted at 2021-04-30 by Mark_wllms Thanks for that (sorry for the duplicate DM, thought it better to ask in forum). There is some simpler code from Daniel Thompson which might be easier to include. I think the BMA421 needs priming with a binary blob before initialising, which takes more steps. Posted at 2021-04-30 by @fanoush nice, looks like he removed the 423 from that. but still it is quite a lot of code. when one would start from scratch with datasheet and just write code for needed features it could be quite simple and short but it needs understanding the HW more so not sure it is worth it for you
You actually did not mention how you update it, does it have nordic bootloader ? or was it using Da Fit app? which exact espruino build and bootloader you have in the watch now? you built it yourself already? to help with this I have a bootloader that vibrates after poweron from dead battery and you have some time to touch it and enter bootloader so you can replace even completely broken build. For less broken builds using watchdog helps. also normal espruino bootloader build for your device checks if BTN1 is pressed at boot time and stays in bootloader. However capacitive sensors do not work well when already held before powering on so that's why I added code so it vibrates and you need to touch it later.
which upstream you mean, the BMA driver or espruino? the driver won't probably change for this old HW, they will possibly just add newer devices to it. Posted at 2021-04-30 by Mark_wllms It's a bit of mix at the moment. I am using your P8 SDK12 bootloader and the 2.09 firmware from @jeffmer. I update with the DaFlasher, but it does use the nordic bootloader, SD3.0. Is there an Espruino bootloader for this device? Would there be any advantage? Posted at 2021-04-30 by @fanoush
The one you have is Espruino bootloader however it is build for P8 so button BTN1 is expected on pin 17. And that pin is inverted and set as input with pull up at boot time, which may cause you some troubles if you have something else on that pin as even when changing mode later the value is still inverted.
yes, by default the button allows you to enter bootloader and skip running your saved code when it is held over 3 seconds at boot time - helps if you save broken js code (e.g. like calling Posted at 2021-05-06 by Mark_wllms I have tried... and failed. The watch still works, but my efforts to send config bytes to the BMA421 have somehow put it the chip into an unreponsive state. I suspect I have overwritten its NVRAM somehow, as it won't even respond to a reset command. Even powering down the watch doesn't seem to bring it back, and any reads from the BMA421 registers just return 0xFF. Any ideas? Posted at 2021-05-06 by @gfwilliams
Did you actually power it down by removing the battery? In these devices everything is always powered even when the watch is off, so if you'd configured the accelerometer you wouldn't expect it to reset until the battery was removed (or totally flattened).
Could be the I2C address has been reconfigured? Or it's been put into SPI mode? You could try accessing it by any other I2C addresses it might have Posted at 2021-05-06 by @fanoush what you mean by 'powering down' the watch, you took it apart and disconnected battery? or let the battery die so it boots from scratch when charger is attached? as for all FFs it may be some issue with i2c, Espruino has both HW i2c - the EDIT: oh gordon already responded with similar hints Posted at 2021-05-06 by Mark_wllms Thanks for the hints - I will explore further. It looks like SPI mode could be the reason. Writing to bit 1 of register 0x70 disables I2C. There are many 0x70 bytes in the config file, so I could easily have done that. It looks quite hard to overwrite the NVRAM, you need a distinct sequence of bytes (or a byte and a bit) that I can't find anywhere in my config file. Posted at 2021-05-07 by Mark_wllms Thank you both - it looks like that has worked. I think that I accidentally wrote to the register which switched the sensor to SPI mode, which is why I couldn't see it again. Posted at 2021-05-12 by Mark_wllms Ok, going back to my original question: I've taken the suggestion from @fanoush and just written a short module in javascript for the BMA421 from scratch. All it does it load the binary code to set up the step counter and then reads steps and xyz acceleration. It seems to all work, for not many lines of code, so I'm happy with that as an approach. Posted at 2021-05-12 by @fanoush
file from storage does not take extra memory, the read just gets direct pointer to bufferbacked by the flash storage, so you can get whole file, no need to split into blocks due to memory. not sure how big blocks i2c can write in one call, maybe there is no limit either, so depends what is reasonable for the sensor to receive in one write. BTW, maybe you can copy your BMA421 code here or link it, could be helpful for other people in future Posted at 2021-05-12 by Mark_wllms Thanks. Tried to open whole file with Storage.readArrayBuffer but ran out of heap memory, so chunked instead. I will try a larger chunk to see what works, now I can see if I am successful. I will upload code when I have tweaked it, and maybe submit somewhere as well. I am trying to write as a proper module, but I have zero JavaScript. It would help Pinetime owners, at least. I think only older P8s have BMA421. Posted at 2021-05-12 by @fanoush strange, the comment about not using RAM is only in this description https://www.espruino.com/Reference#l_Storage_read , not in readArrayBuffer, but they should be similar. if not, try read() and convert string via E.toArrayBuffer. I always used read() for some reason. Posted at 2021-05-12 by Mark_wllms Would I have a a problem with terminators (0xFF) in the string? There are a few in the data. Posted at 2021-05-12 by @fanoush not an issue with Storage api, it is the StorageFile api that has this issue (but allows appending to/extending the file - 0xff marks EOF in such files). Posted at 2021-05-13 by Mark_wllms This is current state of code - just enough to demonstrate working. Various things still need adding, e.g. events and scaling the accel vector.
BTW, I have no experience of JavaScript, only C/Pascal/Delphi (and a little python), so I'm not convinced by the object model here at all. Attachments: Posted at 2021-05-13 by @fanoush Thanks. In fact I do have one P8 with BMA421 so will test. I am not good at JS so cannot comment style, just few optimization tips
which does same thing as your lines 36-42. The array constructor with buffer shares same underlying data with no copy so you just get different view on same data (also any write to such arrays is shared so conversion can work in both directions). Of course it works only if you are sure about endianness and it matches what sensor uses. Or there is DataView https://www.espruino.com/Reference#DataView
Posted at 2021-05-13 by Mark_wllms Thanks. I was just reading https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays when you replied! Much neater. Posted at 2021-05-13 by Mark_wllms I should also divide x y and z by 16 as only 12 bits are used. Posted at 2021-05-19 by Mark_wllms At the moment it is all working, but I am polling the sensor 10 times a second to look for wrist twist, which is putting a bit of a strain on battery life, even when the watch isn't being worn. I'll look into the 'movement' interrupt, but am unsure if this is specific to BMA423. It won't hurt to try. The datasheet implies you can set a threshold movement in each axis to trigger the interrupt. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-04-29 by Mark_wllms
I have been using Espruino on an Gokoo M11 watch, with broadly similar specs to a Pinetime or older model of Colmi P8.
It includes a BMA421 accelerometer and an HRS3300 heart rate monitor. The former also offers built in step counter as seen at https://www.youtube.com/watch?v=pqnowZd2_KQ
I can use this C++ code library successfully (on the ATCWatch Arduino firmware) to step count and to wake the watch.
Is there any way to implement this for Espruino - or do I need to create my own build and compile the driver into it?
Beta Was this translation helpful? Give feedback.
All reactions