Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch all enums to be within #defines #5

Merged
merged 1 commit into from
Oct 6, 2017
Merged

Switch all enums to be within #defines #5

merged 1 commit into from
Oct 6, 2017

Conversation

xC0000005
Copy link

This change allows a user to have, for instance, TEMP_4 and TEMP_BED definied but nothing else. The enums which are not defined move "out", allowing the first ones to take the slots in the enum, and since the array is sized on ADC_PIN_COUNT, we always have the right size data and in order.

This change allows a user to have, for instance, TEMP_4 and TEMP_BED definied but nothing else. The enums which are not defined move "out", allowing the first ones to take the slots in the enum, and since the array is sized on ADC_PIN_COUNT, we always have the right size data and in order.
@xC0000005
Copy link
Author

Forgot to mention - your question about sampling: According to this: https://stackoverflow.com/questions/42168388/stm32f103-adc-sampling-rate, it's approximately every 1us.

@victorpv victorpv merged commit f881d07 into victorpv:bugfix-2.0.x Oct 6, 2017
@victorpv
Copy link
Owner

victorpv commented Oct 6, 2017

Merged.
About the sampling rate, I think we should look to reduce the rate.
The samples are read every 2ms at max:
First the temp ISR is called every 1ms.
Then in one cycle it calls the function that starts the conversion in a pin (in our case just loads a variable from the DMA buffer).
Then in the next run, it reads that value.
It only does that for 1 pin at a time, so for 4 sensors it will only load, 1 in each run, so 8 runs of the ISR to read them all.

So for 1 temp pin, reads at 500Hz, for 2, at 250Hz, etc.

Given that, we can reduce the rate at which we capture ADC reads to just be faster than the highest rate, thats 500Hz or 2mS. Anything faster than that, will update the buffer more often than it's needed by the ISR, Since the ADC is more accurate with larger sample times, we should eliminate some noise from the readings.
The guys working in the LPC HAL have had some issues with ADC reads due to noise, and had to add aditional filtering.

If I get a chance I will look at changing the sampling speed. There should be something either on libmaple or in the ADC library.

@victorpv
Copy link
Owner

victorpv commented Oct 6, 2017

Did you get to test the steppers in the Malyan board?

@xC0000005
Copy link
Author

All you have to do is change the sample rate in the init (and perhaps adjust the prescalar). I'm working on getting the toolchain up and working with the malyan board. It uses a nonstandard bootloader, so I've been making a variant and reverse engineering their crc so I can get their board to accept my rom. Will test tonight to see if I've figured out that part. There's some real weirdness in which their reset vectors are set to values that aren't within the firmware binary's boundaries, and they vary with different firmware updates, so it's not an external function call that I can tell. I'll figure it out eventually.

@victorpv
Copy link
Owner

victorpv commented Oct 6, 2017

If you have an st-link, or any other debug probe, why don't you just save and replace their bootloader?
The sources for the maple/stm32duino ones are open, and only takes 8KB of flash.
About the weird vectors, perhaps their binary is encoded in some way, so the addreses would be scrambled?

@xC0000005
Copy link
Author

Several of the traces are not present in current revs and I'm lousy at soldering. I'm searching for an early rev main board which has JTAG connectors. Oddly enough, I broke the CRC on the firmware for the V2 easily - it's CCIT Poly, and I brute forced the seed values.

@victorpv
Copy link
Owner

victorpv commented Oct 7, 2017

Nice, would you mind explaining me how you did it? I'd like to decrypt the firmware for another closed source board, the Chitu 3d, that also uses an STM32F103, and when I looked at it the vector table was also scrambled. Did you use a tool that will try all seed values against the file?

@xC0000005
Copy link
Author

I spent a lot of time staring at it in a hex editor then made some educated guesses + work from the retargetable decompiler, then wrote a brute force program which calculated the "right" values for two different revisions, telling me I'd probably found the checksum. Where's your firmware? I'll look at it.

@xC0000005
Copy link
Author

BTW, tonight I actually managed to get the ST-LINK soldered to the right traces (I sacrificed a controller board) and discovered that Malyan set the read-protect bit. Result - I have a nice, empty mother board for me to test code on. First tests failed - the board hits an error() call during thermal manager init, but my soldering broke loose so I will have to wait until later this week to test it again.

@victorpv
Copy link
Owner

haha you wiped the firmware?
I tested basic functionality of the ADC by plugging the input to GND / VCC and reading the temp, and could see the value returned change between the upper and lower limits in the host (with pronterface)
So the ADC code was good, but perhaps you need to adjust the parameters to correctly match the resistors in the board or the PTC you use?

@xC0000005
Copy link
Author

Yup, I wiped the firmware. I soldered multiple thermisters up to test ADC this time around, it's working. I had code running on the Malyan board this morning, but my test board (which I bought as "bad" and has always run literally hot to the touch) died as I was working on figuring out pins. So now I have to hunt down a new one to get back to work.

@victorpv
Copy link
Owner

I had this theory about the F1 read out protection, but didn't spend much time on it:
So the memory can't be read from outside, but a program running can read the flash and print it out thru serial, usb, whichever way. That is confirmed and is actually within the specs. So if you can load your own code without wiping the bootloader, you can load a piece of code that reads the booloader memory and spit it out.
Additionally, you can write to RAM and run from RAM while the flash is protected, but if a debug probe is connected, then a program will fail and crash if trying to read from flash. But if you load the program to RAM, then set the boot pins to boot from ram, disconnect the debugger, and reboot, the MCU should boot from RAM, and hopefully since the debugger is now disconnected, the program could read the flash and spit it out. So even without knowing anything about the bootloader, you could load the dumper to RAM. I didn't get to test that fully. I did test to load the program to RAM, and see it crashing, but had the debugger connected. Only later I read that when a debugger probe is connected, and read protection enabled, programs can't read from flash.
Whenever I get a chance I'll try to test that further.

@xC0000005
Copy link
Author

xC0000005 commented Oct 11, 2017 via email

@xC0000005
Copy link
Author

So the plan of attack is simple - both V1 and V2 of this printer do a lousy job of validating their firmware. I found a function that traces a null terminated string in R0 to the display controller, and the plan (once I replace my board) is to patch this code into the firmware update, then listen on the display controller as it reads 128k worth of data one byte at a time and echoes it to the serial port. The only complication is ARM's literal pool making it more difficult to patch this in.

@xC0000005
Copy link
Author

xC0000005 commented Oct 12, 2017 via email

@victorpv
Copy link
Owner

I haven't had much time in the last few days, but I need to send you the link to that Chitu board firmware, in case you can figure out what kind of encryption they used, and finish mapping the pins out.
That one shouldn't have much trouble either running Marlin since everything in it is pretty standard other than the diplay, which uses a parallel interface, likely thru the FSMC.

@xC0000005
Copy link
Author

xC0000005 commented Oct 16, 2017 via email

@victorpv
Copy link
Owner

victorpv commented Oct 16, 2017

Don't know if you know all of this, but this is how USB enumeration works.
There needs to be a pull up resistor between DP pin and 3V3. That pull up must be connected during communication, and when disconnected it signals the host that the USB device has been disconnected.
On the F103 there is no internal resistor (in the F4 series there is), so there has to be a 1k5 resistor in the circuit between DP and 3v3. Now to switch between the bootloader and the serial port USB drivers, we need to signal the host a disconnect after the main program starts.

Some boards like the maple mini and others use a transistor controlled by a pin. You can see that in the maple mini schematic.

For boards that do not have that, I discovered that setting the DP pin to output pulldown and setting it to 0 for a short time, like a ms, was enough to pull the DP line low and signal the host the usb was disconnected. Then we set the pin back to input mode, and the pullup resistor pulls the line up again, and the host thinks something was connected, does a re-enumeration, and loads the serial driver.
Roger perfected that technique and added it to the core, so it is used when you compile selecting a board different than Maple or Maple Mini.
So the Malyan board may be either using a transistor, in which case you should select the board as Maple Mini, but change the USB DISC pin to the one the Malyan uses, or it may not have any transistor and have a pullup straight to 3v3 (like the blupill board does), in which case you need to select a Generic board when compiling, so the code to pull down the pin is used.
Try to trace the USB DP pin in the malyan to find the 1k5 resistor, and then find if thats connected to 3v3, or a transistor. If a transistor, trace which pin controls the transistor.
Easy way for force a re-enumeration if you can trace what the malyin does, is to either remove the 1k5 resistor from the board and use a wired one that you can disconnect and re-connect, or use a lower resistor to pull down the DP line for very short. That should cause the host to think the usb was been disconnected and reconnected and try to discover the device.

@xC0000005
Copy link
Author

xC0000005 commented Oct 17, 2017 via email

victorpv pushed a commit that referenced this pull request Dec 9, 2017
# This is the 1st commit message:

Choose the timer based on MCU defintion. Timer5 is not valid on C8/CB class boards, so use Timer4 for the step timer.

# This is the commit message #2:

Readme.md update

# This is the commit message #3:

Readme.md update

# This is the commit message #4:

Trying to get readme.md updated

# This is the commit message #5:

readme.md update
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants