How To Write A Driver (STM32, I2C, Datasheet) - Phil's Lab #30
HTML-код
- Опубликовано: 1 июн 2024
- ⏵Hardware and PCB design course: www.phils-lab.net/courses
How to write an I2C driver from scratch in C for an STM32F4 microcontroller interfacing with an accelerometer (Analog Devices ADXL355) using the datasheet and ST's HAL (hardware abstraction layer).
Going through schematic, STM32CubeIDE setup, low-level functions, initialisation, setting registers, reading registers, configuring the sensor, reading data, and finally testing the driver.
Free trial of Altium Designer: www.altium.com/yt/philslab
Visit jlcpcb.com/RHS for $2 for five 2-layer PCBs and $5 for five 4-layer PCBs.
Patreon: / phils94
Git: github.com/pms67
[TIMESTAMPS]
00:00 Introduction
00:28 Sensor (ADXL355, JLCPCB)
01:22 Altium Designer
01:42 Sensor Board Schematic and PCB (KiCad)
03:38 STM32F4 Schematic (LittleBrain)
04:16 STM32CubeIDE Setup
06:40 Basic Project Structure
07:55 Driver Header File
16:11 Driver Source File (Low-Level Functions)
18:41 Driver Source File (Sensor Initialisation and Setup)
26:18 Driver Source File (Temperature Measurement)
30:21 Driver Source File (Acceleration Measurements)
34:10 Testing the Driver
ID: QIBvbJtYjWuHiTG0uCoK - Наука
New hardware and PCB design course on mixed-signal embedded systems just released!
⏵Course content: www.phils-lab.net/courses
⏵Course sign-up: phils-lab-shop.fedevel.education
Really great content. Thanks for this process breakdown... it's hard to find videos that are a bit more involved than "how to program Arduino Uno with blink LED" and still show the process for people new to the concept.
Only thing missing is all the failures to get to the press compile and it works - or is it just me that never can program correct the first time 🤔🤓😬🤣
Anyway - agree great content and detail level! 💪👍🤓
Thank you very much, Evan - glad you liked it!
I agree with you. It's time to raise the level of knowledge. Not every thing must be done for dummys.
I will be super duper honest! This is the best tutorial I have come across that literally is like a tour of how a low level fw engg works on a typical day, at least ideally(unless pulling out one's hair when trying to copy/paste/adapt a pre-written driver) when he/she has a new sensor to bring up. Fantastic explanation and the way you keep going back and forth to the datasheet helps a lot.
I was confused about how to know the sensor-specific init sequence since other stuff like data read/write via SPI & I2C is common knowledge but sensor-specific sequences are sometimes a mystery.
Thank you for a zillion times!!
Thank you very much, Debasish - very glad to hear that it was helpful!
Your voice makes me feel like I'm getting smarter
Haha thank you :)
Well this video certainly is handy right now, since I'm struggling with an INA219 interfacing. For some reason even after all these years of electronics I2C is the bane of my existence.
Thank you, Aron - hope you figured out the INA219 driver!
This simplifies things so much! I appreciate a walkthrough. I always see data sheets and gets intimidated by them. Thanks!
Again clear, concise straight to the point. I havent written in C in a long time. When I heard struct it strucked me a little.
Phil, like Jakob Sorber, is a gold standard for organizing and writing C programs. I'm not a fan of HAL but he temps me to grit my teeth and try it.
Thank you very much, Ken!
Incredible quality and detail of content! You are my go to reference for pcb design and STM32 programming! Keep up the good work!
Thank you very much, Michele!
Truly awesome stuff. Thanks for making videos that distill your experiences for the rest of us. Please keep doing what you are doing - I'm off to binge some more.
Thank you, Michael! Many more videos to come :)
Great! I understood the first 30 seconds then had absolutely no clue what you were talking about after that
As always, liked before watching
About two years ago I bought the evaluation board of this sensor. Since then I had no time to play with it. Now the time has come. As always, Phil has made an exceptionally useful video.
Thank you very much, Adam - hope you got to play with the evaluation board since!
Brilliant explanation. Very easy to follow through. Thanks a bunch
The adxl series accelerometer has some of the best datasheets I've ever read.
Any datasheet from Analog Devices is a pleasure to read
I agree. I love the Analog Devices stuff.
Always really appreciate vids like this. You cover everything and go at a great pace👍🏻
Thank you, Patrick!
Excellent video. Great detail but concise and to the point.
every new video of your channel doubles my knowledge, keep em coming!
Very glad to hear that, thank you :)
Great explanation and straight forward. We add also defines for the spec Version of development parts. Rather than getting questions, users might Spot changes themselve.
I love your videos. Perfect format and method of delivering quality learning. Kudos from Denmark.
Thank you very much!
Thank you for taking the time to do this. good job.
Thanks for watching!
Well done! I like all your videos and i think is the best channel on youtube in this topic.
The way you explain it is very understandable and logical, thank you for the excellent work you share with us. 👍
Thank you so much!
Thank you so much, Phil!
Love these stm32 vids super helpful as I'm moving on to more advance micros getting away from arduino
This is SO useful,thank you so much! i think it would be a very good idea to start a series about how to write drivers.
Thank you!
Really thorough and well-explained. Good shit, dude
Thanks, Sean!
amazing video. Really help novice engineers like me to learn and always get amazed by how intelligent you are.
Thank you very much!
Thanks for the video, a very lovely and thorough walkthrough =)
Very cool. I feel like I should be paying for this. Great job!
Great lecture,Specially to me (none STEM educated back ground--pure software-background ed newbie )
I hope can have more examples of how to read the spec and implementation of interfaces and protocols
Thanks million again.
Thanks for your video this one is one of the best videos. Good explained and showing your workflow is for me a big +.
Thank you very much, Edgar!
Nice video thank you for breaking down the process of your driver.
Thanks, Donnie.
more for embedded like this phil, such a great explanation!
great work bro.i have been waiting for this.you haven't updated us on progress the course.
Indeed. I'm also waiting for the updates on it.
Great video , this really helped me understand how a driver works.
Glad to hear that, thank you for watching :)
Thank You so much Phil! I was waiting for someone to make a video like this, definitely worth a sub :)
Very glad to hear that - thank you for watching!
Amazing search so much to fins this video great job
Damn Phil, just when I thought you can't get any smarter, you go and prove me wrong: again! LOVE IT!
Haha thank you, Marc!
As always a great high quality tutorial
Thank you very much!
Super 5 star education. Thanks a lot because I had a problem with a driver that I change for stm32 but failed. Now I have more details to find the problem 😊👍👍👍👍👍👍
Thank you very much, Luc!
Welldone, it's a good design. I would use LDO with good/high PSSR for low noise applications.
Very good explanation!
Waiting for quaternions.
Thank you!
Great video, very professional job. Only comment I would give is that this structure is not particularly portable, when I write drivers like this I tend to declare my i2c read/write functions as private statics in the module or in a separate module of its own in larger projects and contain all the hal specific code in there. Specifically I would not use the st Hal's status enum as this is platform specific.
If you do this you can trivially reuse the driver on multiple microcontrollers with only a few minutes of work.
Interesting - could you please talk about what you mean by 'module' in this case? Is it a combination of .h and .c files?
Great explanation and showing working code.
Thanks!
Thank you for this - it is pure gold!
Thank you, Scott!
Exactly what I needed ✨️
thank you so much! this video is so good i learned a lot
Great video, very informative. Thank you.
Thanks for watching, Karl!
This was a fantastic video! Thank you
Thank you for watching, Igor!
This is super helpful! Pls make more vids like this. Thx
Thank you, more videos like this to come!
Hi @Phill you way of PCB design using Kicad is nice. if possible please post video STM32 with RS485 communication , differential routing, length matching, impedance matching concept in Kicad. Thanks 👍
Awesome and detailed. You can actually use the Microsoft calculator for bit conversion/manipulation. Just set it to programmer mode. Very useful and handy
Yeah it’s really useful! The macOS calculator has the same programmer mode too. Find myself using it a lot.
or even better, use GCC's 0b notation instead of 0x when the number corresponds to bits in registers
Great videos. I think it would be interesting to create a second video about writing a non-polling driver.
Actually, this is a non-polling driver. What Phil is doing is a very short ISR (Interrupt Service Routine) (see time 34:34 lines 67-76 in the code) that set a flag. It is common practice that an ISR writes to a Semaphor and the thread/process is waiting on this Semaphore. Phil does not have OS therefore he is using flag instead of interrupt, some interrupt might be lost in this implementation.
Great as always.
Thank you, Selim!
Thanks.!! I've been looking for this video for years. LOL!
Glad to hear that, thanks Diego :)
Thanks a lot for the video, love your work, keep going! :D
Thank you, Yaman :)
thank you for great tutorial
Thanks!
Nice video, thanks :)
Fascinating, thank you
Thank you!
I used that same accelerometer in my last project. Fantastic specs, but it is not a cheap part.
great channel!
Thank you!
Amazing content🙏
Thank you!
Very useful video. Thanks. My temperature calculation is a bit different from yours. I got temp = (2111.25-tempRaw)/9.05.
Brilliant Phil
Thank you, Ajay!
At 27:27 you say 2 8 byte registers. Should be 2 8 bit registers right? Beside that: thank you!! Truly awesome !
Hi Phil, will you finish your control system videos in the near future? It would be awesome, thank you!
Thank you for video.
Thank you for watching, Piotr!
thats wonderfull contet If I had searched all my life I wouldn't have found it i liked video and follow your channel it deserves it
Thank you very much, Berkay :)
Very nice vídeo. Thank you
Thank you, Amandio :)
Could you make a video about driver development with the consideration of timing signals like a MEMS mic.
Hey Phil! In your struct, is it unnecessary to have the I2C_HandleTypeDef typed pointer initialized to NULL or to a random block of memory using malloc()?
You are the best bro
Thank you
Thank you for watching!
Grrreeaatt work brother.... 😍
Thank you!
What is the sense of having temperature if the acceleration read-outs are not temperature compensated?
Is it possible to convert Acceleration data to Velocity and be at this speed with this sensor? Can I use this velocity and time to find distance let's say 500 feet down the road? which would be position?
Great video. At time 36:47 I noticed that the "USB device not recognized" popped up from windows after you plugged in your custom board. Did the USB peripheral work? I have been struggling getting custom boards to work with the USB peripheral and I am getting that same pop up
Thanks, Ravi! That's only because I had paused the execution of the code via a breakpoint. The USB peripheral works as it should.
Thx!
Hey, great video! I'm just curious if at 31:40 you could have avoided right shifting the entire expression by 12, if you had instead left shifted x3 by 12, then x2 by 4 and then right shifted x1 by 4 also. I see your methodology, but it makes more sense to be to shift the bits exactly where they need to be.
your proposed method doesn't set the sign bit which means it doesn't work for negative numbers; bit shifting a negative number to the right shifts with 1s intead of 0s, preserving the negative number
24:00 I just use windows calculator for converting numbers between BIN HEX DEC, I find it quite useful.
Good point, thanks!
Hey Phil.. really nice content ..
Just one question is how are the jumper wire connections from "ADXL355" to the "Litte Brain" boards ?
5V
ACC_INT1
ACC_INT2
ACC_DRDY
ACC_NSC_SCL
ACC_SCLK_VSSIO
ACC_MOSI_SDA
ACC_MISO_ASEL
3V3
GND
I2C1_SCL
I2C2_SDA
3.3V
ADC1_IN0
ADC1_IN1
GND
Out of interest, do you ever write testsuites for the software you write? Coming from a web development background I just wondered if this kind of approach is common in the embedded world?
It gets hard since you can't fire a bunch of test data at the software.
But some companies will do the extra leg work to make an internal way to save and emulate real world values.
Loved the video, Phil. I saw your board didn't get recognized. How do you solve that issue?
Thank you! The board was not recognised only because I set a breakpoint/paused the execution of the code. The USB peripheral works fine otherwise.
Great video, thanks. Forty odd quid for that sensor though, crikey. What is so special about it that makes it so expensive?
This sensor has low noise and high accuracy (and you can trust ADI to meet the spec)
Thanks, Sean! Low-noise, low-drift, etc. etc. make it pretty pricey.
is the software at 8:06 is what is called a "device driver" ? the software disk that for example comes with a printer and other devices? or this is another thing that's just called a driver? because I didn't hear say "device driver" but only a "driver". thanks
I build my libraries for STM exactly the same. Except that I don't put the low level functions in my header file, as it is not relevant to the main.
As per others, Great content. Since the content is already pretyped, its easy for me to copy or compare..
Nice i wrote my own driver for my gtx 970 I got a few extra dps. I wrote the driver in ms basic.
pretty neat.
Thanks!
can i use this for my uni project this is the the Topic of the project :(Basic Device Driver: Make a simple driver for hardware devices.) is it possible ?
Amazing voice
Its a weird question but how exactly does "ReadRegisters" work? I mean, why do you need an usb port if you can you call read registers without utilizing usb?
inside the ADXL355_Initialise function: when reading the device ID, shouldn't you directly return from function if the status was not HAL_OK? You did not initialize regData, hence if a read is a fail, wouldn't you be comparing a non-initialized variable with the constants (ADXL355_DEVICE_ID and ADXL355_MEMS_ID) in the following lines of code?
another question: What is the goal of having an error count?
also TIL that initialize in British is initialise :)
Nice video, I only I have question, when reading your accelerometer with the board sitting flat on the table, all x, y and z reading for accretion should be zero I think, but in your example z axis reading was 9.8 you said, so not sure why this is so?
it should be 9.8
Hi Mushaq, the reason for downwards 'acceleration' is the force of gravity (equal 9.8 m/s^2).
Hi I thought the data sheets are supposed to have an open transaction, then functions to call to access data, and then close the transaction - but in general I'm not seeing data sheets that allow drivers to be created for several devices in general .
How does one know what language a driver for a device can be created in and how to used the data sheet function with the IDE being used?
As I've wanted to write a library for depth sensing cameras for the Arduino...but as said before driver creation is an element many programmers have no idea how to do...Luckily I had a university lecturer who knew how to do things like this...
Funnily enough I just finished writing an STM32 I2C driver for a sensor today. In the past I’ve only done PIC drivers.
Why not SPI for an IMU if you don’t mind me asking?
I don't know the reason behind the SPI/I2C choice, but here's my reasoning. Usually I2C is fast enough for multiple chained IC readings, and you only need two supply and two communication wires (+ interrupt if that's your thing). SPI has 4 comm wires to start with and every chip has an additional chip select (and the interrupts).
I would use a union to join the incoming 8bits data - or is that a bad idea? 🤔
i thought the lsb of the slave address has to be 1 to read from the slave maybe ive missed someting but you have left shifted the address by 1 making the lsb a 0 to write or dose it have to do with the hal_memread
nm lol im a stm32 noob clearly the hal does this for you
Please Put some info. on how to select a stm32 for specific use.
How did you learn all these? What have you studied? Could you direct me to some courses?
Hello sir very good