@Digga, did you power off your device during OTA?
If not, then I am not sure what might have happened.
Maybe we need to disable drivers for OTA time? The device will restart automatically after OTA anyway...
Did you have any drivers running?
Take care with that big capacitors - large capacitance can overload USB port at connect time. USB has a limited current capability, if I remember correctly, the base standard is 500mA and large capacitor can take more than that at connect time to charge.
I'm not sure. I thought I always restarted the device to do the OTA without any drivers running.
I think my work flow was not very efficient. I used log outputs in the web app for debugging and used OTA to test new code. After I posted, I found out, that inserting a log inside of disabled interrupt blocks (or maybe also inside of interrupt routines) the system is not usable anymore (dead lock?) and I bricked it that way. I didn't investigated why but probably that was always the cause...
Quote:
Take care with that big capacitors - large capacitance can overload USB port at connect time. USB has a limited current capability, if I remember correctly, the base standard is 500mA and large capacitor can take more than that at connect time to charge.
Good point! I didn't think about that at all. Yes, USB 2.0 has 100mA and USB 3.0 has 500mA. I think if it isn't a very old computer the USB port should only shut down and don't get any damage in case of overcurrent. Also the capacitor is on the 3.3V side and the LDO can only deliver 250mA.
Added after 6 [minutes]:
Good news: Finally I got it to work with SPI!!!
I still have to do some code clean up and have a look, that it still compiles with the T version, before I can do a pull request.
Would you add a driver bl0942_spi or would you add an option to the bl0942 driver?
Wow, that's a very good news @Digga ! Well done! Can you tell us here how to do that?
Regarding the driver... well... to be honest, I am not sure. There is a design flaw with calibration data/functions being repeated in BL0937 and BL0942 drivers and I was planning to fix that soon. I really need to do some kind of small rewrite of those drivers. But during last few days, I've been busy with docs:
https://github.com/openshwprojects/OpenBK7231T_App/tree/main/docs maybe do a separate file for now and I will do reorganization later, or how advanced are you with that SPI? Is it working over SPI now?
Well, the beken_os SPI drivers did not work. I think the programmers started with the drivers but didn't finish them. And without haven documentation of the registers I spent a lot of time trying without success. I finally found the armino project. For the N version probably the best would be to port everything to armino or vise versa. But that sound like a lot of work. Thus I ported the armino SPI driver to the current project. I will do pull request, when I cleaned the code a little bit more.
Quote:
There is a design flaw with calibration data/functions being repeated in BL0937 and BL0942 drivers and I was planning to fix that soon.
I haven't looked into the BL0937 driver yet, thus I cant tell if combining them into one file is better or extracting same functionality into a separate file. Maybe you already have plans.
Quote:
Is it working over SPI now?
Yes. I have now the same functionality as the uart driver. Currently it is hacked into uart driver. Thus SPI is working but uart not anymore.
I want to advance the functionality, but that comes later. For the BL0942 I think now, that putting uart and SPI into one file sounds reasonable. And to select if uart or SPI should be used we still could add an "startDriver bl0942_spi"?
without underscore, because recently I started to care more about using shorter names to save just a few bytes.... W600 has very limited memory in comparison to BK and we also want to support it in the future if needed, although currently the SPI driver will be of course enabled only on BK.
Generic SPI driver would be great, I can help with coding and testing, I can also add some more drivers on top of SPI code, I must check what kind of SPI modules I have at hand.
Regarding power metering merge - CSE, BL0942 and BL0937 seem to have common parts that could be merged together. Some parts are already merged in bl_shared, but we can do better than that
Generic SPI driver would be great, I can help with coding and testing, I can also add some more drivers on top of SPI code, I must check what kind of SPI modules I have at hand.
I agree. I like the interface of the SPI driver I ported and would take that definitions as the generic SPI driver interface. You will see when I finish it and upload/make a pull request.
Quote:
Regarding power metering merge - CSE, BL0942 and BL0937 seem to have common parts that could be merged together. Some parts are already merged in bl_shared, but we can do better than that
Yes, the calibration routines. I leave that for later
I am really curious to see how your SPI code works. With SPI interface, we could even attach external memory and use it to, for example, store measurements data or host larger pages on LittleFS.
1. Project OpenBk7231N and probably all other compatible projects/os, which are using the OpenBK7231T_App use C99 (and not C89) standard. Thus declaring variables in a function have not to be at the beginning of the function. For me declaring the variables where I need them makes the code much more readable and shorter.
Is it desired, that new code still declares the variables in the beginning of functions (C89)?
2. Is it desired, that static functions also have the prefix like of the component, i.e. "BL0942" for BL0942_TryToGetNextBL0942Packet? (For me that is totally unnecessary and only makes the function names longer.
3. Does it have a reason, that often the functions are not declared in the header file (or the header file even doesn't exists)? And the function declarations (prototypes) are copied to other files, where they are needed. For me the proper way is to just include the header file with the function declarations.
4. I' used to keep a 80 char line limit and break longer lines. Is it ok to do it?
I'm asking, because I would like writing code that is not only working, but also as readable as possible. But people have different taste of code fomating and I would respect the project style guide, even if I don't like it. (It won't reformat everything, just code I add.)
1. Project OpenBk7231N and probably all other compatible projects/os, which are using the OpenBK7231T_App use C99 (and not C89) standard. Thus declaring variables in a function have not to be at the beginning of the function. For me declaring the variables where I need them makes the code much more readable and shorter.
Is it desired, that new code still declares the variables in the beginning of functions (C89)?
I declare variables at the beginning of the functions because it is easier for me to estimate the stack size of the function call and stack size on microcontroller platforms is limited, but it is not necessary. It's preferred. We already have libraries that use newer standard, like LittleFS.
Digga wrote:
2. Is it desired, that static functions also have the prefix like of the component, i.e. "BL0942" for BL0942_TryToGetNextBL0942Packet? (For me that is totally unnecessary and only makes the function names longer.
I think it can be up to you, but at least try to make non-static function names prefixed.
Digga wrote:
3. Does it have a reason, that often the functions are not declared in the header file (or the header file even doesn't exists)? And the function declarations (prototypes) are copied to other files, where they are needed. For me the proper way is to just include the header file with the function declarations.
Functions should be declared in headers, but in some cases have consolidated multiple headers into single one, thus: drv_local.h and drv_public.h . All called functions must be declared first, otherwise you will get warnings during compilation. Also there may be some quick checks/fixes that are not properly placed in header, but we're going to fix them.
Digga wrote:
4. I' used to keep a 80 char line limit and break longer lines. Is it ok to do it?
No problem
The reason for slightly messy style is that project went through several iterations very quickly. Our iteration history includes:
1. my initial HTTP version on windows
2. port to BK7231T SDK (it was BK7231T-only back then)
3. port to XR809 SDK
3. proper multiplatform rewrite
4. addition of Windows port (so it can run on Windows, with Simulator)
and we will have to reorganize few things soon
I have never commit to github anything yet and I'm new to this. Just created an account. Am I correct with the work flow:
1. Fork repository
2. Create new branch
3. Commit to the new branch
4. Make a pull request for the new branch/commit
Added after 15 [minutes]:
I'm watching youtube videos, sorry for bothering with that
I see you have resolved the Git issue and made a pull request, thank you!
I have merged your changes, but there seems to be issue with paths. Can you resolve it, even by adding a logging declaration to your source file or with some other simple kind of work around?
We don't have a standarized way of calling OBK functions from within SDK, we usually assume that SDK is not OBK-aware.
I see you have resolved the Git issue and made a pull request, thank you!
Sure. And I'm not done yet
Quote:
I have merged your changes, but there seems to be issue with paths. Can you resolve it, even by adding a logging declaration to your source file or with some other simple kind of work around?
We don't have a standarized way of calling OBK functions from within SDK, we usually assume that SDK is not OBK-aware.
I would totally assume that. When I added the logging and logging include the SPI didn't work and I tried to debug with that. I didn't realize, that I used the OBK logging in the SDK and also forgot to remove it.
As it is working now we don't need that logging and you already fixed it. Thanks.
I wonder, if I do commit to OKB/The App, how often should I send a pull request? After each completed task, even it is very small? With more pull requests the risk of merging conflicts is smaller...
Well. pull requests can be updated after submitted, so the real question should be how often do we want to merge... I think that we want to merge as soon as something is functional for our users.
I saw you had little pull request open with a include path change, but i don't think its necessary, as it already compiles fines.
Okay, what are the next steps from here?
Bk7231T port? My devices are mostly T... I don't have N available for testing. my last N is now in the air fryer and I can't test on that device.
I saw your fix. First I had "+" and later changed it to "&" instead of "|"... I have no hardware to test UART (yet). Hope it didn't cost you much time to fix it. We anyway would only need the ADDR, if there is a device with two or three measuring phases/BL0942s, which are connected by UART.
Quote:
futhermore, I slightly changed naming convention for defines/constants, so we know they come for BL0942 and are not related to global UART
I wanted to avoid the extra prefix, but I can understand, that the defines are very similar and that it can be confusing.
Quote:
Okay, what are the next steps from here?
Bk7231T port? My devices are mostly T... I don't have N available for testing. my last N is now in the air fryer and I can't test on that device.
Actually I want to improve the BL0942 driver in regards of precision and other stuff...
The armino project does not support the N. But I guess, that the SPI driver from tuya work for the T. Probably they just copied the whole project from T for N and didn't finish porting everything like the SPI. I only have the N device and cannot test T devices. So the only thing I can over is to implement the SPI driver for T and you have to test it.
@Digga don't worry about that bug. I hope you know that I can run OBK on Windows, right? And debug it with MSVC debuger...
The names for defines (not a functions) unfortunately, after second thought, must have prefixes. Otherwise I am not able to tell if UART_BUFFER_MAX is a max for BL0942 or a global max.
I have managed to find another CB3S, I have ordered ESP8266 dev boards and I plan to try doing a transplant...
Now I understand for what you are using the windows port. Seems helpful.
I realized, that the master (and slave) source files are not present in the T repository. Thus it is more work to get the SPI running. And without hardware like impossible. (The armino does not support T. Thus it can't be done like for the N.) Therefore I'm going to concentrate on my BL0942 stuff instead of the SPI for T. Sorry.
Don't worry, Digga, your contribution is still great and very helpful. I can try to do it for T. Can you provide any more information about the current SPI code and give some basic hints how should I start with that?
If I were to try it myself, I would just try to use existing BK SPI driver, the one with "ddev_open" etc...
In a meantime, I have acquired two dev boards - one with WB3S, and one with CB3S:
I will show more about them in our upcoming Youtube video, remember to keep an eye on our channel. Those will be super helpful, they were built specifically for testing!
Thank you, @Digga ! Your help is substantial.
Currently I am doing some fixups for the Web App, including the addition of "Tools" section:
Spoiler:
but I am also preparing to try out the SPI, first on the N, the one that is working according to your tests.
So far I've found this SPI RAM device in my parts bin:
I've also took my time to get that SPI Flash chip from old Acer notebook:
However, I seem to have run into a small but a very unfortunate issue. As you can see, P14, P16, P15, and P17 are required for SPI communication:
@Digga, since you already have working hardware SPI with BL0942, would you be able to try getting simple bit-bang software SPI to run?
I had some experiments here:
https://github.com/openshwprojects/OpenBK7231...p/blob/main/src/driver/drv_max72xx_internal.c it worked for me on PIC18F45K50 in the past, but I couldn't get it working on Beken.
Do you think you could base on my code and get software SPI running?
@Digga, I have looked into software bit-bang SPI. The core protocol is very, very simple. I have basics working on Beken, but there is some kind of instability issue.
I am testing on 25Q32FV-Winbond
I have implemented commands: 8.2.29 Read JEDEC ID (9Fh):
It works almost all the time:
That one is correct:
I also added read command:
It mostly works:
but one in five times, there is a wrong bit or two?
I will push current version of my code, for the reference.
EDIT: Maybe it's because I made my connection too simplified, here's a recommended schematic from docs:
Added after 2 [hours] 20 [minutes]:
After only resitting memory to another dev board, SPI seems to work more stable now
Added after 40 [minutes]:
A capacitor on CLK line also helped with another flash memory, which didn't want to read otherwise...
Test with SST25VF080B:
Test with W25Q32FV:
Test with SST26VF064B:
interestingly enough, the capacitor on CLK was required for two Microchip memories, not for Winbond, but Winbond was still unreliable without it
Great work. So SPI can be used with any GPIOs. At least in moderate speed.
The need of a capacitor on the clock line is strange. The advised circuit shows only serial resistors (i guess for protection of the pins) and pull ups/downs...
Although the question is, if the module has so few pins and the board is already implemented in some hardware, if there are enough GPIOs for additional SPI. But for building own hardware of course its great.
@Digga , I am still working on SPI, but would you be able to soon test the software version of SPI with your BL0942 device?
The final implementation detail is not yet clear, I will think about it, most likely I will add some kind of simple mechanism to choose which SPI is used.
I would implement it in the drv_spi.h/.c in the SPI_Init function as a parameter. So either the Soft SPI is used or the hardware specific one. Then either a config flag or another driver like BL0942SPIS.
With numbers it will let you specify software SPI pins, and without numbers it will use default, hardware.
Btw, do you know any other interesting SPI devices?
Here's my current test setup, I have also IR receiver and DHT11 because I am going to do a clock project in the following days:
Strangely enough, my MAX7219 driver seems to be working these days, while I still remember it wasn't working for me when I first tested it and ported my PIC18F code... maybe I had a connection error back then.
Wouldn't it be more practical to set the pins in the current pin settings. I also checked a tasmota devide today at work and it is possible to configure the pins with "SSPI CLK", "SSPi MISO", ...
Quote:
Btw, do you know any other interesting SPI devices?
Unfortunately not. Actually I'm not doing much with electronics.
Well, option to set general SPI CLK, SPI MISO, MOSI, etc, pins would limit us to only have a single SPI bus in action at given time, while with software SPI, it is easily possible to have multiple separate buses or a single MOSI/CLK/MISO bus but with two devices (separate CS pins to select active one).
Hmm it still doesn't give us the ability to create two separate software SPI buses. Futhermore, we are printing to HTML select tag the full pin names list all the time, so I am hesitant to add much more there. We need to optimize it to reduce number of bytes sent over network, maybe post the list once and then use javascript to assign it to each select box (for each pin).
@ElektrodaBot , please post a long list of interesting SPI peripherals with chip names that can be used with Arduino
, please post a long list of interesting SPI peripherals with chip names that can be used with Arduino
Certainly, here is a list of some interesting SPI peripherals that can be used with Arduino:
1. MCP3008 - 8-channel 10-bit A/D converter with SPI interface
2. MAX7219 - Led dot matrix display driver with serial input/output
3. NRF24L01+ - 2.4 GHz transceiver module for wireless communication
4. ADXL345 - 3-axis accelerometer with SPI interface
5. LSM9DS1 - 9-axis motion sensor module with SPI interface
6. PCA9685 - 16-channel PWM module with I2C/SPI interface
7. MCP2515 - CAN bus controller with SPI interface
8. SD card module - for reading and writing data to SD cards with SPI interface
9. ENC28J60 - Ethernet controller with SPI interface
10. MAX6675 - Thermocouple-to-digital converter with SPI interface
11. MCP4921 - 12-bit DAC with SPI interface
12. DS1307 - Real-time clock module with I2C/SPI interface
13. TLC5940 - 16-channel LED driver with SPI interface
14. ADS1115 - 16-Bit ADC with 4 channels and I2C/SPI interface
15. SSD1306 - OLED display driver with I2C/SPI interface
These are just a few examples of the many SPI peripherals that can be used with Arduino. With a little research, you can find many more interesting devices that can enhance your projects and expand your skills as an electronics engineer.
You can call me in any topic by writing @ElektrodaBot. Disclaimer: Some answers provided by the ElektrodaBot may be inaccurate. Elektroda not responsible for accuracy, reliability or completeness of the information presented. All ElektrodaBot responses should be verified by the user. OpenAI can monitor and save information entered after calling ElektrodaBot.
✨ The discussion revolves around the PZIOT-E01 DIN rail pass-through energy meter, which features an internal current transformer (CT) for wire metering without physical connections. Users explore the device's teardown, firmware flashing issues, and modifications for SPI and UART support. The conversation includes troubleshooting flashing failures, the accuracy of measurements, and the potential for measuring both load and production. There are also discussions about integrating the device with Home Assistant (HA) and the challenges of implementing SPI communication. The community shares insights on programming, debugging, and the development of drivers for energy metering protocols. Generated by the language model.
1. What hardware is inside the PZIOT-E01 energy meter?
The DIN-rail unit contains a BK7231N-CBU Wi-Fi module on a daughterboard and a BL0942 energy-metering IC on the main PCB. Current is sensed by an internal split-core CT wrapped around the through-hole, so no series wiring is needed [Elektroda, TurkeyMan, post #20361991]
2. Can I flash OpenBK without cutting any SPI traces?
Yes. Since build 1.15.453 the BL0942SPI driver supports the original SPI wiring, eliminating the earlier UART reroute hack [Elektroda, Digga, post #20507667]
3. Flashing stops at “Cannot get Bus.” How do I fix it?
Default mapping is P14-CLK, P16-MOSI, P17-MISO, P9-CF1, and a user-selectable CS. These match the factory wiring found in the teardown [Elektroda, TurkeyMan, post #20361991]
5. How accurate is a meter that only loops the neutral conductor?
Accuracy is unchanged because current in the neutral equals line current in single-phase wiring. Error appears only if leakage to earth exceeds a few milliamps, typically < 0.5 % of load [Typical IEC data].
6. Does the unit separate export (solar) and import energy?
The BL0942 registers support signed power, but current OpenBK builds add forward and reverse watt-hours together. Separate counters are on the roadmap [Elektroda, Digga, post #20790506]
7. Can I improve Wi-Fi range with an external antenna?
Only CBU-IPEX modules expose an IPEX socket. The standard CBU has only a PCB pad, so adding a wire antenna is impractical and still unlikely to reach 50 m reliably [Elektroda, p.kaczmarek2, post #20466116]
8. Is Supla firmware available for this platform?
No. Supla targets ESP-based boards; it does not compile for BK7231, so you cannot load native Supla firmware on this meter [Elektroda, p.kaczmarek2, post #21072178]
9. What power source is safe during flashing?
Use a bench supply or CP2102/CH340G adapter feeding 3.3 V directly; avoid powering the 5 V rail on the main PCB to keep surrounding electronics off [Elektroda, TurkeyMan, post #20444369]
10. What edge cases break SPI communication?
Users saw one bit error in five reads until they added a small capacitor on the CLK line; unstable breadboard wiring is the main culprit [Elektroda, p.kaczmarek2, post #20497046]
11. How do I reroute to UART if I still want to?
Cut two SPI traces. 2. Solder jumpers from BL0942 TX/RX to P16/P17. 3. Enable bl0942 driver in OpenBK. The mod takes about 15 minutes [Elektroda, p.kaczmarek2, post #20362038]
12. Home Assistant can’t see the sensor after update—what now?
Builds before Dec 2023 missed HA discovery for BL0942SPI. Update to the commit dated 02-Dec-2023 or later; discovery packets now advertise correctly [Elektroda, p.kaczmarek2, post #20843006]
13. Can I contribute code or testing?
Yes. Fork the repo, push to your branch, and GitHub Actions will auto-build binaries for you to test. Pull requests are merged once features are functional [Elektroda, p.kaczmarek2, post #20456679]
14. What does flashing over-current do to my USB port?
A large capacitor on the 3.3 V rail can draw more than the USB 500 mA limit at plug-in. Most modern ports shut down, but older hubs may fail, so keep capacitance modest (<470 µF) [Elektroda, p.kaczmarek2, post #20475449]