The “sensorless” guys figured out that the default settings for the B-G431B-ESC1 are suboptimal and there is documentation from STM supporting that. In order to improve the performance of the board one has to modify one constant in variant_B_G431B_ESC1.cpp (FLASH_LATENCY_8 → FLASH_LATENCY_4) in one line of code (it is the only occurence in there). This speeds up code execution from flash significantly. I have implemented it and everything was great and fast, except for one thing: Reading from SPI is a problem now. In my setup with an SPI connected AS5047 sensor I cannot read the magnitude of the magnetic field anymore, the sensor will always return the position instead. My best guess is that this is a timing issue (only one bit differs between the address for reading the angle (3FFE or 3FFF) and reading the magnitude (3FFD). I have tried this in different setups and it is repeatable, even without power to the motor, just USB power and turning the motor by hand. To further complicate things, my cheap, crappy USB scope is to slow to analyse this and if I hook up the same cheap thing as logic analyzer, it fails with 8 wait states as well, so I might be facing an electrical problem. A simple code sequence to test the behaviour is:
int m1 = sensor.readMagnitude();
Normaly the readings should be fairly stable, but in the bad case It reads the angle of the sensor.
Any ideas or similar observations? It would be a pitty if we cannot use the optimized settings for the flash latency…
If the above is a red herring, then you could try slowing the clock down to 250,000. Technically it is possible to switch speed for each beginTransaction so you could read magnitude slow and angle fast.
I tried to follow your hints, but I am using the ckass MagneticSensorAS5047, which looks quite different from you posted. and it is directly derived from Sensor and not MagneticSensor. While checking this however, I found that here is another class MagneticSensorAS5047U, which would match my sensor exactly, but I am not aware of any relevant differences between those sensors. Also, I could not identify any functional differences in the code, but I cannot simply replace one of the two by the other, since many names are different. Also, I tried lowering the speed, but it made no difference.
Meanwhile I also tried the MagneticSensorAS5047U and reduced my program to the bare minimum, but no change. 8 waitstates are fine, with 4 the sensor fails. It’s a pitty, since 30% more speed are really tempting. Nevertheless, I’ll give up here, since I do not really need it and without a proper scope I just don’t know what is going on on the SPI. I will see if I can borrow a scope and then come back to it, but that’s going to take some time.
I have something like this, a Hantek 6022BL and it is supposed to work up to 20MHz. The software either the free one or the one from Hantek is however not anything I would call fun to use. I am never sure what I do there, it takes 15 minutes to get it work since it requires special USB drivers, trigger functions are very much restricted and the PC/Laptop is never where you need the scope. In theory the scope should be fine for this purpose, but either I am to stupid to operate it or it just isn’t suitable. Or, maybe I just want a RigolDHO900… I could have an analog problem as well since connecting the logic analyzer probes also leads to wrong SPI readings, but it still doesn’t explain the dependence on flash wait states, since it is a hardware SPI. Anyway, don’t worry, it is more a nice to have for me than a real problem, 5kHz loop frequency are easy enough (but 8kHz…).
I just got a new rigol dho814, its nice. The $12 logic analyzer with sigrok pulseview software is better for troubleshooting logic problems with SPI than the scope, but obviously can’t do analog troubleshooting.
If connecting the scope changes the behavior it sounds like an analog problem. I’m not sure why the flash latency would impact it other than when stuff goes faster, little stuff can start to matter more.
Being that you’re using the bg431esc with an spi sensor, what’s the cabling to the sensor look like? If possible, you could use the ABI interface on your sensor with the stm32 hw encoder class, and get loop rates of 20-35khz similar to what I reported on discord. I assume the pin assignments conflict with the SPI bus, so you’d need to program the sensor, then switch the wiring over.
The AS5047U is a “more professional” version of the sensor, with some extra features. These include some filtering functions the D and P versions don’t have, and importantly, 24 and 32 bit SPI transfers including CRC checks.
The U version supports 16 bit transfers on read operations (then there is no CRC) but for SPI writes you have to use 24 or 32bit transfers, and include a CRC checksum.
The MagneticSensorAS5047U implements the 24bit SPI writes, compared to the MagneticSensorAS5047 class, which only handles 16 bit SPI reads/writes. So it is important to use the right driver, or you won’t be able to write any settings.
I am aware of those differences, but since I am only reading from the sensor it shouldn’t matter and in fact, it works all fine with the 8WS settings. Nevertheless, I followed your advice of course and have also tried the MagneticSensorAS5047U class with and without swapped code lines and the effect was exactly the same.
Anyway, as I wrote before, for me this is a nice to have since everything is working fast enough with 8WS. I just would like to understand the problem and make sure it is not a real problem which is obscured in the 8WS case.
Just as background info: I have four sensors in action, two are AS5147 and two are AS5047U (should be at least, if JLC did it right). This comes from the time when you had to take which ever components you could get… I want to stay with SPI since I want to detect loose magnets in an early stage to avoid fried ECSs etc. My magnets are invisible and difficult to reach, so monitoring the magnetic magnitude is a good feature. SPI also allows me to use sensor calibration.
Finally I found some time to look into this problem again and I believe I found the reason for the failure. Between two command sequences, the CS signal is kept low for most of the time and only before the start of the next sequence it is briefly taken high and then immediately low again. In the good case it is high for app. 1us, in the bad case for app. 250ns and therefore out of spec for the sensor (>350ns required). I did not yet look into the code, but, @runger, is there a reason for keeping the CS low for so long? Is that required for anything (SSI…)?
The good news is, I got it to work somehow, the sad news is that it’s neither clean nor do I fully understand all potential side effects. The main problem seems that my CS pin is one, which is supported by the hardware as CS (PA15=SPI1_NSS). This leads to conflicts and the HAL wants handle the CS instead of leaving it to the SPI class. After the HAl initialized SPI, the pin mode is reset and it can’t be used as GPIO anymore. The work around is to reset the mode again in AS5047U::spi_transfer16(). In principle, it would be nice, to leave the CS handling to the hardware, but I did not get that to work, nor could I tell it to leave the port untouched. It is not trivial, since we have various layers of software that deal with the CS pin. SimpleFOC checks if it is <0, the SPI class checks if it is NC, which is an enum resolving to 0xffffffff, which may or may not be <0, depending on the data type and finally, there is the HAL, which does its own thing that I did not yet understand.
OMG, as so often, the problem is in front of the computer! I completely misunderstood the usage and passed the pin to both objects! If the problem fixes the code, it works fine (as far as I can tell so far)!