• Hello ES! We could use some help to get us past the finish line on building the new knowledgebase for the forum.
    Can you donate? Please see our fundraising page. Thank you!

FOC Open Source Firmware for Bafang CAN bus controllers with GD32F303 processor

There are slight differences in the M820, that require some changes in the code. The z-signal of the encoder is on PC8 instead of PB8 and the number of pole pairs is 7, not 5.
I guess there will be further differences, at least the throttle signal is on the same pin as on the M510/560 and works now, after I added the wire from the harnish connector to the controller PCB.

IMG_20260511_172319_932~2.jpg
IMG_20260511_173513_630~2.jpg
IMG_20260511_173619_995~2.jpg
IMG_20260511_203953_129~2.jpg
 
The z-signal of the encoder is on PC8 instead of PB8 and the number of pole pairs is 7, not 5.
Next finding: the MT6816 encoder is configured to UVW mode on the M820, not to ABZ mode like on the M510/560.
So the rotor position signal is the same as the standard 120° hallsensor pattern on most hub motors. I can simply reuse the algorithms from the CRA101.C hub motor controller branch, hopefully.

1778655362642.png
1778655298787.png
 
I can simply reuse the algorithms from the CRA101.C hub motor controller branch, hopefully.
Done, the motor runs. :cool:
Really noisy on the video, I had some issues to reassemble the housing yesterday. A very high force was necessary to bring the bearing of the first gear stage back in position. But the current draw at idle run seems to be OK. The maximum cadence at 47V is about 150/min, so I guess this is OK also.


regards
stancecoke
 
Last edited:
Can you explain how to do a memory dump?
Here a more detailed description :)

Connect the STLink to the PCB. I strongly recommend to use the STLinkV2 shown in the picture. The very cheap clones that can be found on Ebay, AliExpress are dying like flies...
1778832409614.png


Download and install the ST-Link Utility. The newer STM CubeProgrammer doesn't work with clone processors.
Start the Utility.
1. press the "connect" button
2. set the size to 0x40000 (256k memory, 256*1024 = 262144 in dec, 0x40000 in hex)
3. click on "save". Use the ending .bin for the dump.
That's all ;)

1778833284529.png

You can dump the memory by command line with the open OCD debugger also, I don't know, which way you prefer.

I've identified the next difference in the processor pin functions, the PAS1 and PAS2 signals are on PC12 and PD2. Further functions like brake or lights are not validated yet.

1778836694794.png

Next step is testing under load to validate the current calibration factors. But I have to build adapter plates from the M400 (G330) bike frame to the M820 motor first :)
Done. But how mad is Bafang??? You need different cranks on the M820.

regards
stancecoke
 
Last edited:
Hm, the M820 doesn't convince me at the moment. I don't know, if it uses a different torquesensor, but there is high friction between the crank shaft and the motor under load. If you drive the wheel by the throttle and try to hold the crank shaft by hand (no cranks installed yet), it is almost impossible. Without load there is almost no friction. So it seems there is some deformation in the motor housing and/or torquesensor housing under load, that leads to increasing friction with the load. As long, as you are pedaling, this is irrelevant of course, as there is no relative movement between the chainring and the crank. Maybe this is the reason, why Bafang offers no throttle on the M820, even though the connectors are present on the wiring harness and the controller board :unsure:

With throttle and high load, the transition from 6 step to 360° extrapolation at start from standstill fails at the moment, but I guess, this will be no issue, if you are pedaling additionally...
I've not implemented the angle correction to keep u_d low at high cadence yet. But I don't know if it is worth to do that, as the maximum power of the M820 is quite limited anyway....
1779030173014.png
 
Last edited:
In my opinion, the m820 is simply not supposed to compete directly with the m510, which is why there is no such extensive configuration for the user. It can't dissipate heat as well as it would generate from a throttle..
 
Almost certainly we will have to look at ways of cooling the M820 controller board if we plan to use more than 650W. With the EBICS firmware I’m hoping we can get a much better and more tuneable motor offering an enhanced riding experience as it fits that road / gravel bike useage really well.
 
With the EBICS firmware
I got ISIS cranks for the M820 today, so I went for the first test ride on the road. It works so far, but the PAS/Torque timeout is not reliable, the chainring turns much too long, after stopping pedaling. I have no idea what causes this issue yet. The iq setpoints drops to zero immedeatly, but the PI control can't bring the actual motor current to zero. I'll have to tune the PI control parameters obviously :unsure:

1779129158202.png


The temperature reading doesn't work yet, also.

But maybe good enough for early Beta testers ;)
As with the M510/M560, you have to run the position sensor calibration first after flashing the bin file.
But the procedure is only a quite long very small movement of the chainwheel. If the E30 in the display disappears, the procedure is finished.

Btw I've tested the throttle with the original BAFANG firmware, it works without problems.

@jaxkubqq: in this plot you can see, that the speed limit is working. The motor power is ramped down between 25 an 27 kph in the first part of the graph. in the second part, I activated the offroad mode and the motor gives power at speeds higher than 25 kph...

1779129849960.png

1779130089086.png
 

Attachments

  • M820_v0.001.zip
    29.7 KB · Views: 3
Last edited:
Yes, you're right. The speed limit is working correctly. When the legal flag is enabled, you have to set the main speed limit to 25 km/h, and switching to 'off-road' mode disables the limit properly.
It would be a good idea to add a description of how the legal flag feature works to GitHub; there's a detailed explanation in post #164 if anyone needs to find it.

I still think it would be best—if possible in legal mode—to disable the throttle while pedaling (according to regulations, the throttle shouldn't function during pedaling either). Additionally, it would be great to enable a smooth power ramp-up for the throttle when using it as a 'walk assist' mode up to 6 km/h in legal mode.
Thanks for your work.
 
to disable the throttle while pedaling (according to regulations, the throttle shouldn't function during pedaling either).
In Europe it is allowed, as long as you are pedaling. There are many commercial bikes out there, that are working this way.
See here, for example:

A throttle, on the other hand, is not strictly required on a pedelec—but contrary to persistent rumors, it is not prohibited either. As mentioned, the only prohibition is on being able to draw power from the motor on an electric bicycle that does not require registration or licensing (i.e., a pedelec) without pedaling.
This means that on such a vehicle, regardless of the throttle position (if one is present)—including at full throttle—the motor must either drop back to the start-up assist speed or shut off completely (in systems without start-up assist) when the rider is not pedaling.

Translated with DeepL.com (free version)
 
The website you linked belongs to a private e-bike parts dealer, so it shouldn't really be taken as an official legal source. Besides, Austria recently updated its e-bike regulations to align with the rest of the EU, and this article doesn't look like it's very fresh.

Every source I’ve checked consistently states that the assistance must be activated strictly by pedaling. Even if a throttle works only while you are pedaling, you are still delivering and modulating the assistance level with your hand. The regulations clearly state that the power must be controlled solely by the pedaling action itself.

So the key takeaway here is: yes, we are pedaling, but at the exact same time, we are regulating the power output via the throttle, which is not permitted.

Also, right now, the 'walk assist' mode operates without having to hold down the button, which is more convenient, but it's also against the regulations."
 
Every source I’ve checked consistently states that the assistance must be activated strictly by pedaling.
It is activated by pedaling. With the moment you stop pedaling, the motor stops also. There are millions of bikes on the roads, that are releasing the motor power just by turning the cranks. All simple PAS systems without torquesensor are working this way.

See the official EU REGULATION (EU) No 168/2013:
(h) pedal cycles with pedal assistance which are equipped with an auxiliary electric motor having a maximum continuous rated power of less than or equal to 250 W, where the output of the motor is cut off when the cyclist stops pedalling and is otherwise progressively reduced and finally cut off before the vehicle speed reaches 25 km/h;
 
Last edited:
I tested the firmware using an 11S battery during calibration. M820v0.001
During Position Sensor Calibration (performed with the chain removed), the chainring did not rotate at all. I could hear and feel some reaction from the motor, so the controller was clearly trying to do something, but there was no visible movement of the chainring.
However, I still managed to get the motor running and did a short test ride.
One major issue is a very long overrun after stopping pedaling — the motor keeps assisting for roughly 2 seconds after cadence input stops.
Another issue:
Walk Assist does not seem to have any speed limiting. When activated, it spins with full power instead of limiting the motor speed to a safe
 
First, thank you for testing!
One major issue is a very long overrun after stopping pedaling
It works so far, but the PAS/Torque timeout is not reliable, the chainring turns much too long, after stopping pedaling
This issue is known ;)
Walk Assist does not seem to have any speed limiting.
That's because the pedals are turning when the chain wheel is turning. So the controller thinks you are pedaling. But this is easy to solve...
 
Also, right now, the 'walk assist' mode operates without having to hold down the button, which is more convenient, but it's also against the regulations."
Walk Assist does not seem to have any speed limiting.
I've fixed both now. The walk assist starts only, if the level is set to walk mode and the down button is pressed. The speed is limited to 6kph.


But I still have no idea, why there is such an extrem lag in the reaction of the PI control. I have to do further testings ... :unsure:

1779303333277.png
 
Last edited:
Looks like too low P and/or too high I gain
in throttle operation it looks much better:
1779362734285.png



With torquesensor and pedaling, the graphs looks quite different. U_d is not dropping fast enough, as the PI control "sees", the current is low already. :unsure:
Of course, I could switch off the PWM if the setpoint is zero, but this will lead to a "Plock" again, I think.
1779363403773.png

Edit: I've switched the PI-control from integer to float arithmetics and tuned the P and I factors.
It looks much better now, but not perfect yet. It needs some more fine tuning.

1779378022585.png

Finally, I reset the integral part of the PI-control now, if the setpoint is zero and i_q drops below 50 (ADC counts)

This leads to a satisfying behaviour. I'll go for a testride on the road now. If it's OK, I'll post the recent .bin file for the M820

1779386339019.png
 
Last edited:
Hm, the control loop starts oszillating on the push assistance (red arrow in the graph), so I have to reduce the factors or the setpoint a little.

1779388109524.png

But in normal riding the behaviour is OK, with quite little overrun. This is an uphill ride with moderate assistance:
1779388480420.png

regards
stancecoke
 

Attachments

  • EBiCS_for_M820_v0.002.zip
    30 KB · Views: 1
Last edited:
This feedback concerns software version 002 (installed today), tested on an 11S battery, and using the DPC-245 display.
Walk Assist not working – The icon appears, but no response when holding or releasing. This function worked previously, now it doesn't.
Autocalibration after chain removal – Worked on the second attempt. At the end, the sprocket spins extremely fast, which feels unsafe. Question: Can this final speed be reduced, or is it required?
Overrun unchanged – Overrun length is the same as version 1. If I slightly backpedal, the power cuts immediately – no overrun in that case.
Power delivery roughness – Under heavy load (strong acceleration), power feels jerky and less smooth than in previous versions.
 
Sorry newbie here wanna ask simple question.
I have M560 motor and I only have Besst Tools. Can I just install the firmware with Besst Tools? and if after I try the firmware and it doesn't suits me can i reflash to original Bafang firmware??
 
Walk Assist not working – The icon appears, but no response when holding or releasing
Then your display is not sending the button down flag with the CAN protocol. The button down pulls down the voltage on the On/Off button pin to a certain value so this analogue voltage would have to be checked. I simply returned to the old logic, where you don't have to hold the button, but just have to switch to the walk mode level.

Overrun unchanged
I guess, you didn't increased the decay parameter from the default setting? I've set the default value to 255 now and have the integral part is reset as soon as the iq setpoint is zero.

Power delivery roughness
I've set the PI control parameters a little less agressive.

The attached bin file is not tested!

Can I just install the firmware with Besst Tools?
Yes, and you can return to the original Bafang firmware at any time.

I don't know, if someone ever tried to set the parameters with the BESST tool also. Maybe this works also even :unsure:

Edit:
I went for a test ride, I'm quite satisfied with the behaviour now :cool:
I had to set the multiplier to 20 to get sufficient assistance in sport mode, but that's just the process of fine tuning the parmeters to your personal preferences. The M820 bootloader seems to erase the whole flash memory on the firmware update, so you have to restore all your personal settings after flashing. The position sensor calibration has to be redone also. That's different to the BL3 and BL38, they don't erase the memory page of the virtual EEPROM. That's more convenient of course.
Sometimes the setpoint drops quite sharply, I don't now why, but you don't notice that during riding. Perhaps I'm really pedaling that unevenly ....

1779462718188.png
 

Attachments

  • EBiCS_for_M820_v0.0021.zip
    30 KB · Views: 3
Last edited:
Back
Top