3 ms·
Bitbanging like this is fine for synchronous protocols... but if you're trying to work with an async one like this one, there's just no safe way to do it in C u
by marcan_42 5y ago
Bitbanging like this is fine for synchronous protocols... but if you're trying to work with an async one like this one, there's just no safe way to do it in C unless your have a lot of leeway in your timing. Even if you get it to work today, there's no telling how a compiler update might change the instruction generation enough to change the timing and screw it up.
I know it's cliché to tell people to go code in assembler but... this is where you do it, and why you do it. And it's easy. Seriously. These things are so simple it is not hard at all to learn how to code for them in asm. And it opens up a whole world of cool timing hacks you just can't do in C.
I haven't done much microcontroller stuff recently, but here's a thing I wrote for an ATtiny a while ago. This kind of tight timing bit banging hack is just not possible to do reliably in C.
https://github.com/marcan/sigmafix https://github.com/marcan/sigmafix
- spenczar5 5y agoI would absolutely love to write assembly for the most time-sensitive parts, while keeping C for the parts that act as a USB HID library interfacing with the host computer. It really does feel like it would be so much simpler to be able to drop down into a little assembly sequence for reading back the response from the keyboard. But - to your point about Arduino's gaps - I have just no idea whatsoever how to build something like that. Not so much how to write the assembly - I've written a bit, and I'm sure I could work that out. No, I mean literally how I link and assemble the final program and get it to work on the chip. I don't know whether that's just an Arduino problem, either. I find this stuff hard to google, but maybe I'm using the wrong terms - these are the perils of learning as you go.
- marcan_42 5y agoHeh, I wrote a USB stack for PICs in assembler way back and... yeah, I wouldn't recommend that :-) (the official stack was written in C, but I refused to use C compilers for PICs at the time because they were expensive and terrible; things got better with SDCC, but oh god C18 was bad - thankfully C works very nicely on AVRs). But yes, mixing asm and C should be a lot easier and well documented. The repo I linked is a simple example of how to do it stand-alone, without any libs or Arduino anything, but... yeah. This isn't exactly an Arduino problem; the "template project" issue is pretty common across the embedded industry. But for something as popular as Arduino not to have done a better job is unfortunate.
- unwind 5y agoYou'll figure it out, I'm sure! You can probably continue to piggy-back on the Arduino environment by using inline assembly (the asm("") statement), but it's annoying since you need to keep the assembly in a C++ string literal. In the meantime, please read the manual or look up some tutorials on Youtube on how to use the oscilloscope. They're complicated for sure, but I think you had a popular hobbyist model and there should be help to be found. Failing that, look for the word "cursor" on the front panel; cursors is usually how you measure things and it's like at the core of scope functionality so it should not be hard.
- raverbashing 5y ago> Bitbanging like this is fine for synchronous protocols Yes, it might be (easier if the timings are more flexible), but a naive sleep (that might be trying to use an internal timer) will throw you off That's exactly my point, they would need assembler for that (or at least "predictable" C instructions)