Adding ECUAL Drivers To Your STM32 Project & Configurations Options
|Previous Tutorial||Tutorial 20||Next Tutorial|
|Adding ECUAL Drivers To Your STM32 Project & Configurations Options|
|STM32 Course Home Page 🏠|
In this short tutorial, I’ll show you the exact steps in a step-by-step guide for adding any ECUAL driver to your STM32 project. And I’ll also explain what are the changes we’ll make in the software architecture we’re following here and why these changes will be made. The intention of this tutorial is to be a quick guide for all future work if somebody who wasn’t following this series of tutorials got stuck while adding any driver like Servo, Ultrasonic, LCD, StepperMotor, or whatever. I can refer him/her to this tutorial to know the software architecture we’re sticking to how to integrate this driver into the whole application project.
Required Components For LABs
All the example code/LABs/projects in the course are going to be done using those boards below.
- Nucleo32-L432KC (ARM Cortex-M4 @ 80MHz) or (eBay)
- Blue Pill STM32-F103 (ARM Cortex-M3 @ 72MHz) or (eBay)
- ST-Link v2 Debugger or (eBay)
|QTY||Component Name||🛒 Amazon.com||🛒 eBay.com|
|1||LEDs Kit||Amazon Amazon||eBay|
|1||Resistors Kit||Amazon Amazon||eBay|
|1||Capacitors Kit||Amazon Amazon||eBay & eBay|
|2||Jumper Wires Pack||Amazon Amazon||eBay & eBay|
|1||9v Battery or DC Power Supply||Amazon Amazon Amazon||eBay|
|1||Micro USB Cable||Amazon||eBay|
|1||Push Buttons||Amazon Amazon||eBay|
|1||Alphanumeric LCD Module||Amazon||eBay|
Some Extremely Useful Test Equipment For Troubleshooting:
- My Digital Storage Oscilloscope (DSO): Siglent SDS1104 (on Amazon.com) (on eBay)
- FeelTech DDS Function Generator: KKMoon FY6900 (on Amazon.com) (on eBay)
- Logic Analyzer (on Amazon.com) (on eBay)
Affiliate Disclosure: When you click on links in this section and make a purchase, this can result in this site earning a commission. Affiliate programs and affiliations include, but are not limited to, the eBay Partner Network (EPN) and Amazon.com.
Preface To The Software Architecture
Now, after 20 tutorials in this series, I think it’s time to make a small change in the way we’re writing applications’ code. The software architecture we’re sticking to was this one in the diagram below. As you can see the application layer is built upon the HAL and LL drivers from STMicroelectronics.
The Previous Software Layered Architecture
There were no issues in this and you can justify this by saying its a simple architecture and looks clear enough. However, proceeding with this setting will result in more problems in the future. You’ll end up with an application code that is tied up to the underlying HAL drivers (like a servo motor that is hooked to TIMER2, or whatever). This significantly reduces the application code portability across multiple platforms and even introduces the possibility of clashes on the same target MCU itself (like a servo motor being hooked to TIMER2 CH1 meanwhile ultrasonic driver is hooked to the same hardware TIMER).
There are a lot of examples where this simple way of architecting the software would fail and be painful to port or configure. And that’s why we’re going to make a small change to it. This will make it a little bit more complicated, and it should, but in the name of making our applications more portable and easily configurable. So, it’s OK!
Adding ECUAL Layer To The Software Layered Architecture
We’ll introduce a new layer called ECU Abstraction Layer (ECUAL) where we’ll populate our external drivers. The hardware drivers which interface the microcontroller to the outside world of sensors, IO units, displays, memories, and so on. Anything on the ECU level that supports the functionality of the MCU in the embedded system shall go into this layer.
An ECUAL driver is basically a set of functions that initialized the MCU HW via the HAL and does the calling of HAL functions, necessary calculations, algorithms, and utilities, in order to abstract the hardware handling from the application layer. So the application code doesn’t talk directly to TIMER1 or USART3 or whatever.
The ECUAL driver for the servo motor for example will check its configuration file to know which timer is assigned to it by the user (the application programmer) and therefore, initialize this timer and do its work. Now, the application turns ON and controls the servo motor without knowing or worrying about which timer is being used in the background. And the user (the programmer) can at any time open the servo driver configuration files and assign another timer or change the resolution or whatever.
Therefore, the application can have different separate configurations other than the ECUAL drivers. This significantly reduces code dependencies and the possibility of having conflicts in the software. The application can now be easily ported to another platform or target with a little bit of effort. And it can also be configured so easily to change the way it’s functioning.
Examples For ECUAL Drivers
We’ll be developing a handful of hardware drivers and add them to the ECUAL. And we’ve actually done the first one in the previous tutorial, it was LCD16x2 using 4bit mode and GPIO pins. Other drivers examples include the following:
- Servo Motor
- Stepper Motor
- 7Segments Display
- … The list goes on!
ECUAL Drivers Configurations
There are many ways in order to make your driver code configurable. I’ve decided to make the ECUAL drivers with a linking configuration. The drivers will have a couple of files for configuration parameters encapsulated in a structure that gets initialized by the user in the cfg.c file. This parameter will, therefore, be externed to my driver source code file like servo.c for example. The global cfg variable is now seen by the driver source file and can be accessed. After all source code files (.c) in the project are compiled to object files, the linking stage starts to resolve symbol dependencies and function calls, and so on. What matters is that the config global parameter that is externed into the driver code is linked in the linking stage and that’s why it’s called “Linking Configuration”.
The user (the application programmer) doesn’t have to re-compile the whole project if a change in the cfg parameter is intended. Only the cfg.c file has to be compiled separately and linked to other object files which is a shorter process in time.
Other methods of driver configuration include The Pre-Build and Post-Build configurations. I’d like to separate this topic in another article if it’s interesting to you. The only thing I wanted to say that I’ve chosen to make the ECUAL drivers with a linking configuration. And I’ve also explained how it works and whatsoever.
ECUAL Utilities & Math Dependency
Developing the ECUAL drivers will sometimes require using other utilities like the delay functions which we’ve developed in an earlier tutorial. I’ll stick to the DWT delay function and they should work on ARM Cortex-M3/M4 and any newer processor. However, the DWT doesn’t exist for old processors and that’s why we’ve developed another delay utility that uses a hardware timer for this job. I’ll just avoid this option and go with the DWT. I’ve just wanted to disclose this intention.
The ECUAL drivers will also at some points require some math functions or algorithms for filtering signals and some DSP stuff. In this case, we’ll develop the necessary math work and embed that into our projects in a separate math directory and that’s the way I’ll go in these cases.
I’ll try my best to make the upcoming tutorials & LABs clear enough and also give you a portable code that is easily configurable, testable, and readable with no additional complications.
How To Add An ECUAL Driver To A New STM32 Project?
Example To Add ECUAL Driver “LCD16x2”
Step1: Open CubeMX & Create New Project
Step2: Choose The Target MCU & Double-Click Its Name
Step3: Set The RCC External Clock Source
Step4: Go To The Clock Configuration
Step5: Set The System Clock To Be 72MHz Or Whatever
Step6: Name & Generate The Project Initialization Code For CubeIDE or The IDE You’re Using
That’s it for this example! The LCD doesn’t require any hardware configuration within CubeMX software
Step7: Open The Project In CubeIDE & Create A Source Code Directory Called “ECUAL”
Right-Click The Project Name in the navigator and create the new directory & name it ECUAL
|And Here it’s Created & Empty
Step8: Now, Copy The Driver Folder You’ve Downloaded And Paste It Into ECUAL
The Driver Folder .. Download it here
Right-Click the ECUAL in the CubeIDE project navigator and paste the folder. It should be added like this
Note that you’ll have to do the same steps for other drivers other than the LCD. It could be servo, stepper, or whatever. The steps are the same though.
Step9: Add The util Directory in the same manner as you’ve done with the ECUAL
Create a source folder in the project
Paste the util files. Those files are for delay functions used by the LCD16x2 driver
Step10: You can start developing your application now. But first, check the configuration parameters file
This shows you the exact GPIO pins that are being used by the driver and for which purposes. You can switch them around or do whatever you want.
Other ECUAL drivers will have different configuration parameters that you’ve to check out and edit if you want. And you can potentially add or remove any of which in order to customize your application and drivers. It’s your call!
Now, after being done with adding the driver, let’s test it. The following section is a simple application code in the main.c file to test the LCD driver.
STM32 LCD Display 16×2 Test LAB
|LAB Title||STM32 ECUAL Driver Addition & Test (LCD16x2)|
STM32 LCD Display 16×2 LAB17
Here is The Application Code For This LAB (main.c)
static void MX_GPIO_Init(void);
LCD_Write_String("It Works! GG izi");
The LAB Connections
The Result For LAB Testing
Did you find this helpful? If yes, please consider supporting this work and sharing these tutorials!
Stay tuned for the upcoming tutorials and don’t forget to SHARE these tutorials. And consider SUPPORTING this work to keep publishing free content just like this!
|Previous Tutorial||Tutorial 20||Next Tutorial|