|
Forum Index : Microcontroller and PC projects : PicoMite - integrated compact C-compiler / C-runtime
| Author | Message | ||||
| bfwolf Senior Member Joined: 03/01/2025 Location: GermanyPosts: 281 |
OK, I see. No chance to change `pm_var()` to not raising a MMBasic error but instead returning a NULL pointer? Edit: "or simply declare the variable in BASIC beforehand" - yes, possible, too, but that requires knowledge about the internals of the CSUB from outside in the BASIC program - "not very beautiful".. You are right! The correct way would be (if static would be possible): static struct globals_s *globals = (static struct globals_s*)0; if (globals == (static struct globals_s*)0) { globals = GetMemory(...); // Allocate memory on 1st usage. } I know: "no politics" in this forum.. - but: "Ukraine, hold on!" ![]() Edited 2026-11-20 03:49 by bfwolf |
||||
| MikeV73 Regular Member Joined: 03/07/2025 Location: UkrainePosts: 54 |
Hi bfwolf, I think there's a much simpler solution to your use case, and it already works with the existing CSUB API. No changes to `pm_var()` or `pm_dim()` are needed. You can use a BASIC `INTEGER` variable as an opaque context handle and pass it by reference to a CSUB. The same CSUB handles both initialization and subsequent calls: - If the handle is zero, allocate a new context using `GetMemory()` and return its address through the argument. - Otherwise, use the existing context. context.c: #include <stdint.h> #include "PicoCFunctions.h" struct globals_s { uint32_t counter; uint32_t values[100]; }; void Context(long long *handle) { struct globals_s *ctx; if (*handle == 0) { ctx = GetMemory(sizeof(*ctx)); *handle = (long long)(uintptr_t)ctx; } else { ctx = (struct globals_s *)(uintptr_t)*handle; } ctx->counter++; ctx->values[0] = ctx->counter; } MMBasic: CSUB Context INTEGER "context.c" END CSUB INTEGER ctx1, ctx2 Context ctx1 Context ctx1 Context ctx1 Context ctx2 Context ctx2 Here `ctx1` and `ctx2` represent two completely independent instances of the same C structure. Each is allocated on its first call and retains its state across subsequent calls. There's no need for static variables in the CSUB, no lookup of BASIC variables by name, and no special initialization or cleanup hooks. When the next BASIC `RUN` resets the heap, both contexts are automatically reclaimed along with their BASIC handles. You can also explicitly release a context using `FreeMemory()` when it's no longer needed, and set its handle back to zero. So effectively, BASIC owns the context handles, while C owns the internal structure and implementation. I think this is a cleaner solution, especially when multiple independent contexts are needed. And thank you for your kind words. Much appreciated! |
||||
| Briano Newbie Joined: 20/01/2026 Location: CanadaPosts: 29 |
Would this firmware be a good place to begin learning "C", along with a learning-C book? .....Brian |
||||
| JohnS Guru Joined: 18/11/2011 Location: United KingdomPosts: 4404 |
I'd start to learn on a PC because any errors (bad pointer, array index out of range, divide by zero, ...) will at least not crash the whole machine leaving you to wonder why. You can also use a debugger on a PC which can catch such and show where. John |
||||
| MikeV73 Regular Member Joined: 03/07/2025 Location: UkrainePosts: 54 |
It depends on goal and limitations. In case you do not want to ocupy PC and free it for other person, it may have some profit. For other cases, I guess, it is better to use normal computer with a lot of helpers, IDEs, internat access to fast review hints and solve issue, and so on.. Also, may be usefult for you: https://coddy.tech/ - some interactive cources. You can goofle others, there are not only one. WBR. MikeV73. |
||||
| EDNEDN Guru Joined: 18/02/2023 Location: United StatesPosts: 311 |
My personal opinion is you don't want to try to learn C by using BASIC to host it. It is going to be very difficult. You are better off bringing up Microsoft's Visual Studio and putting simple programs into it and compiling them. And the Integrated Development Environment as a very powerful debugger where you can step through things and see what is happening. That would be a more productive path. |
||||
| toml_12953 Guru Joined: 13/02/2015 Location: United StatesPosts: 751 |
Although it's not a full C, it has enough of the language so that you can do the examples in many beginner books. I use it with the book, Learning to Program in C by Thomas Plum. In the book, they use the command "write" to send output to the display but you have to use printf in this C compiler. It's a minor difference but one that could stump a total beginner who blindly types in source code. I'd recommend loading this firmware and trying it. If you don't like it, then it's a simple matter to reload the stock firmware. Just remember to save any important data on an SD card, not on drive A:. Drive A: will be overwritten when you go between the firmware versions. |
||||
| PhenixRising Guru Joined: 07/11/2023 Location: United KingdomPosts: 2168 |
I beg to differ. I am learning sooo much by writing in Basic and having Gemini convert/translate to C and then run it on the PicoMite. |
||||
| toml_12953 Guru Joined: 13/02/2015 Location: United StatesPosts: 751 |
Why are you using AI when you could learn so much more by doing the translating yourself? That's how I learn languages. I take a program in one language I know then convert it to a language I don't know. I look up each statement as I use it. By the time I'm done, I know a lot more about the target language than if I used AI. If I used AI, I wouldn't have the experience to remember what I learned. |
||||
| lizby Guru Joined: 17/05/2016 Location: United StatesPosts: 3891 |
If the object is to learn C, that's one thing, but if the object is to make your chosen hardware perform fast enough to accomplish what you want it to, then using AI to code the C which enables it makes a lot of sense. PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on FOTS |
||||
| toml_12953 Guru Joined: 13/02/2015 Location: United StatesPosts: 751 |
You're right. It seemed to me that he wants to learn C rather than optimize for a particular platform. |
||||
| bfwolf Senior Member Joined: 03/01/2025 Location: GermanyPosts: 281 |
Yes, I see it the same way! The C compiler in "Picomite with C" is certainly not for beginners. As soon as pointers come into play, things get risky; you really need a debugger or a runtime system capable of catching pointer-related errors. For a beginner, MS Visual Studio is also a bit "too much" — there’s just so much to navigate. That holds true even for the free version. If @Briano is working on a Windows PC, I can recommend this system: https://www.codeblocks.org/ It’s free, fairly easy to get the hang of, and includes templates for most common tasks. It comes with the GNU compiler and a full-featured debugger. The IDE is also available for Linux and macOS. Another option — older, but apparently still being developed by Embarcadero (and, I believe, once the foundation for Code::Blocks) — is: https://www.dev-cpp.com/ Regards, bfwolf |
||||
| Briano Newbie Joined: 20/01/2026 Location: CanadaPosts: 29 |
|
||||
| MikeV73 Regular Member Joined: 03/07/2025 Location: UkrainePosts: 54 |
I'd also like to thank everyone who has participated in testing and discussing PicoMite with native C support! Your feedback, questions, bug reports and suggestions are very valuable. They help identify issues, improve compatibility, refine the documentation and make the firmware more useful. Special thanks to those who have already tried the RC builds and shared their results. The project is still evolving, and community participation is an important part of that process. Thanks again to everyone involved! WBR, MikeV73. |
||||
| PhenixRising Guru Joined: 07/11/2023 Location: United KingdomPosts: 2168 |
Your feedback, questions, bug reports and suggestions are very valuable. They help identify issues, improve compatibility, refine the documentation and make the firmware more useful. Special thanks to those who have already tried the RC builds and shared their results. The project is still evolving, and community participation is an important part of that process. Thanks again to everyone involved! WBR, MikeV73. It's thanks to you, Mike These are incredible times and many of us are a bit overwhelmed with all the new developments. I'm still trying to catch up with matherp's stuff along with your enhancements. I have said it before and I'll say it again; what we have here is the most exciting thing happening in the MCU world |
||||
| Mixtel90 Guru Joined: 05/10/2019 Location: United KingdomPosts: 9387 |
From your perspective I'd like to see something to do with the built-in security of the RP2350 chip made accessible in some way. Something like preventing installation of or updating to an unsigned version of MMBasic would be cool. You don't want a hacked version being put onto a machine that could mangle someone. You can lock your code but that's not the same thing. We live in a time of hacking. Avoid wireless connections (although Bluetooth can be reasonable), don't put gear on a local ethernet either - there could be a dongle hidden somewhere in an office. Encrypt, encrypt and encrypt again. Mick Zilog Inside! nascom.info for Nascom & Gemini |
||||
| PhenixRising Guru Joined: 07/11/2023 Location: United KingdomPosts: 2168 |
We live in a time of hacking. Avoid wireless connections (although Bluetooth can be reasonable), don't put gear on a local ethernet either - there could be a dongle hidden somewhere in an office. Encrypt, encrypt and encrypt again. Yeah, the RP2350 security mention caught my attention when watching the video The wireless thing, I dunno. I was recently at a steel supplier and the overhead gantry-cranes were constantly moving huge bundles of steel tubes over the heads of the workforce. I noticed that the operator's pendant was wireless. I frequently come-across the dual-redundant safety systems on my travels (SICK/Pilz/Telemecanique) and they are all easily bypassed with jumper wires. These wireless e-stops are pretty cool: ![]() But they still have a hard-wired controller-box that can be bypassed. I was thinking about an e-stop that would communicate wirelessly, directly with the MCU with a regular heartbeat and a short, coded message. Obviously, it would have to be a fail-to-safe system and a temporary password-protected override. When troubleshooting, sometimes you need to bypass the e-stops and if the maintenance person forgets to remove the wire jumpers when he's fixed the problem..... |
||||
| The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2026 |