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
  • НаукаНаука

Комментарии • 222

  • @PhilsLab
    @PhilsLab  2 года назад +9

    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

  • @evanlane1690
    @evanlane1690 2 года назад +180

    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.

    • @TheStuartstardust
      @TheStuartstardust 2 года назад +4

      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! 💪👍🤓

    • @PhilsLab
      @PhilsLab  2 года назад +17

      Thank you very much, Evan - glad you liked it!

    • @amandioreal2293
      @amandioreal2293 2 года назад +5

      I agree with you. It's time to raise the level of knowledge. Not every thing must be done for dummys.

  • @Basicguy1798
    @Basicguy1798 Год назад +10

    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!!

    • @PhilsLab
      @PhilsLab  Год назад +1

      Thank you very much, Debasish - very glad to hear that it was helpful!

  • @Phil659
    @Phil659 2 года назад +162

    Your voice makes me feel like I'm getting smarter

    • @PhilsLab
      @PhilsLab  2 года назад +7

      Haha thank you :)

  • @666aron
    @666aron 2 года назад +17

    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.

    • @PhilsLab
      @PhilsLab  2 года назад +1

      Thank you, Aron - hope you figured out the INA219 driver!

  • @chadkrause6574
    @chadkrause6574 2 года назад +6

    This simplifies things so much! I appreciate a walkthrough. I always see data sheets and gets intimidated by them. Thanks!

  • @Leezorc
    @Leezorc 2 года назад +2

    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.

  • @kenwallace6493
    @kenwallace6493 2 года назад +3

    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.

    • @PhilsLab
      @PhilsLab  2 года назад

      Thank you very much, Ken!

  • @micheleabolaffio7527
    @micheleabolaffio7527 2 года назад +9

    Incredible quality and detail of content! You are my go to reference for pcb design and STM32 programming! Keep up the good work!

    • @PhilsLab
      @PhilsLab  2 года назад

      Thank you very much, Michele!

  • @michaeloosthuizen9024
    @michaeloosthuizen9024 2 года назад +6

    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.

    • @PhilsLab
      @PhilsLab  2 года назад

      Thank you, Michael! Many more videos to come :)

  • @simeonshaffar3374
    @simeonshaffar3374 2 года назад +1

    Great! I understood the first 30 seconds then had absolutely no clue what you were talking about after that

  • @omaralkassab9079
    @omaralkassab9079 2 года назад +8

    As always, liked before watching

  • @adaminsanoff
    @adaminsanoff 2 года назад +6

    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.

    • @PhilsLab
      @PhilsLab  2 года назад +2

      Thank you very much, Adam - hope you got to play with the evaluation board since!

  • @shabadigash
    @shabadigash Год назад

    Brilliant explanation. Very easy to follow through. Thanks a bunch

  • @bgitego
    @bgitego 2 года назад +8

    The adxl series accelerometer has some of the best datasheets I've ever read.

    • @sayantanmaiti2513
      @sayantanmaiti2513 2 года назад +2

      Any datasheet from Analog Devices is a pleasure to read

    • @PhilsLab
      @PhilsLab  2 года назад +1

      I agree. I love the Analog Devices stuff.

  • @patrickcpe
    @patrickcpe 2 года назад +1

    Always really appreciate vids like this. You cover everything and go at a great pace👍🏻

    • @PhilsLab
      @PhilsLab  2 года назад

      Thank you, Patrick!

  • @bikerder
    @bikerder 6 месяцев назад

    Excellent video. Great detail but concise and to the point.

  • @buscoparan1772
    @buscoparan1772 2 года назад +1

    every new video of your channel doubles my knowledge, keep em coming!

    • @PhilsLab
      @PhilsLab  2 года назад

      Very glad to hear that, thank you :)

  • @DerDermin8tor
    @DerDermin8tor 2 года назад

    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.

  • @Avraforlaura
    @Avraforlaura 2 года назад +1

    I love your videos. Perfect format and method of delivering quality learning. Kudos from Denmark.

    • @PhilsLab
      @PhilsLab  2 года назад

      Thank you very much!

  • @johnproctor464
    @johnproctor464 2 года назад +2

    Thank you for taking the time to do this. good job.

    • @PhilsLab
      @PhilsLab  2 года назад

      Thanks for watching!

  • @Mriks007
    @Mriks007 Год назад

    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. 👍

  • @rafaelvisintin5981
    @rafaelvisintin5981 Месяц назад

    Thank you so much, Phil!

  • @frankbose544
    @frankbose544 2 года назад

    Love these stm32 vids super helpful as I'm moving on to more advance micros getting away from arduino

  • @rocketrocks836
    @rocketrocks836 2 года назад +4

    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.

  • @Seandotcom
    @Seandotcom 2 года назад +2

    Really thorough and well-explained. Good shit, dude

  • @EmbeddedEnigma
    @EmbeddedEnigma 2 года назад +1

    amazing video. Really help novice engineers like me to learn and always get amazed by how intelligent you are.

    • @PhilsLab
      @PhilsLab  2 года назад

      Thank you very much!

  • @OthmanAlikhan
    @OthmanAlikhan 2 года назад

    Thanks for the video, a very lovely and thorough walkthrough =)

  • @piratsik7691
    @piratsik7691 2 года назад

    Very cool. I feel like I should be paying for this. Great job!

  • @taiwantoklip
    @taiwantoklip 2 года назад +2

    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.

  • @edgarpisano
    @edgarpisano 2 года назад

    Thanks for your video this one is one of the best videos. Good explained and showing your workflow is for me a big +.

    • @PhilsLab
      @PhilsLab  2 года назад

      Thank you very much, Edgar!

  • @Frisky0563
    @Frisky0563 Год назад

    Nice video thank you for breaking down the process of your driver.

  • @wisnueepis3593
    @wisnueepis3593 7 месяцев назад

    more for embedded like this phil, such a great explanation!

  • @musenzerob2181
    @musenzerob2181 2 года назад +5

    great work bro.i have been waiting for this.you haven't updated us on progress the course.

  • @soufiane_krem
    @soufiane_krem 2 года назад +1

    Great video , this really helped me understand how a driver works.

    • @PhilsLab
      @PhilsLab  2 года назад

      Glad to hear that, thank you for watching :)

  • @abhignay
    @abhignay 2 года назад

    Thank You so much Phil! I was waiting for someone to make a video like this, definitely worth a sub :)

    • @PhilsLab
      @PhilsLab  2 года назад +1

      Very glad to hear that - thank you for watching!

  • @Benjamin-wr1el
    @Benjamin-wr1el Год назад

    Amazing search so much to fins this video great job

  • @marcdraco2189
    @marcdraco2189 2 года назад +3

    Damn Phil, just when I thought you can't get any smarter, you go and prove me wrong: again! LOVE IT!

    • @PhilsLab
      @PhilsLab  2 года назад +1

      Haha thank you, Marc!

  • @mail87523
    @mail87523 2 года назад +1

    As always a great high quality tutorial

    • @PhilsLab
      @PhilsLab  2 года назад

      Thank you very much!

  • @lucvanhove9639
    @lucvanhove9639 2 года назад +2

    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 😊👍👍👍👍👍👍

    • @PhilsLab
      @PhilsLab  2 года назад

      Thank you very much, Luc!

  • @Aiidn8665
    @Aiidn8665 2 года назад

    Welldone, it's a good design. I would use LDO with good/high PSSR for low noise applications.

  • @vovanikotin
    @vovanikotin 2 года назад

    Very good explanation!
    Waiting for quaternions.

  • @mawoodmain
    @mawoodmain 2 года назад +8

    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.

    • @crosswick
      @crosswick 2 года назад +2

      Interesting - could you please talk about what you mean by 'module' in this case? Is it a combination of .h and .c files?

  • @slagmaker6780
    @slagmaker6780 2 года назад

    Great explanation and showing working code.

  • @scottfoulk7480
    @scottfoulk7480 2 года назад

    Thank you for this - it is pure gold!

    • @PhilsLab
      @PhilsLab  2 года назад +1

      Thank you, Scott!

  • @tim_allen_jr
    @tim_allen_jr 6 месяцев назад

    Exactly what I needed ✨️

  • @GeorgeBenettM1
    @GeorgeBenettM1 Год назад

    thank you so much! this video is so good i learned a lot

  • @karlbyrne9
    @karlbyrne9 2 года назад

    Great video, very informative. Thank you.

    • @PhilsLab
      @PhilsLab  2 года назад

      Thanks for watching, Karl!

  • @IgorJeri
    @IgorJeri 2 года назад

    This was a fantastic video! Thank you

    • @PhilsLab
      @PhilsLab  2 года назад

      Thank you for watching, Igor!

  • @hernameplz6506
    @hernameplz6506 2 года назад

    This is super helpful! Pls make more vids like this. Thx

    • @PhilsLab
      @PhilsLab  2 года назад

      Thank you, more videos like this to come!

  • @karthick930
    @karthick930 2 года назад +1

    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 👍

  • @bona-fide
    @bona-fide 2 года назад +2

    Awesome and detailed. You can actually use the Microsoft calculator for bit conversion/manipulation. Just set it to programmer mode. Very useful and handy

    • @Kefford666
      @Kefford666 2 года назад

      Yeah it’s really useful! The macOS calculator has the same programmer mode too. Find myself using it a lot.

    • @bfournier1884
      @bfournier1884 2 года назад

      or even better, use GCC's 0b notation instead of 0x when the number corresponds to bits in registers

  • @10e999
    @10e999 2 года назад +4

    Great videos. I think it would be interesting to create a second video about writing a non-polling driver.

    • @t7732155980
      @t7732155980 2 года назад +3

      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.

  • @selimsagr446
    @selimsagr446 2 года назад +1

    Great as always.

    • @PhilsLab
      @PhilsLab  2 года назад

      Thank you, Selim!

  • @DiegoColl44
    @DiegoColl44 2 года назад

    Thanks.!! I've been looking for this video for years. LOL!

    • @PhilsLab
      @PhilsLab  2 года назад +1

      Glad to hear that, thanks Diego :)

  • @yamanseyravan2341
    @yamanseyravan2341 2 года назад

    Thanks a lot for the video, love your work, keep going! :D

    • @PhilsLab
      @PhilsLab  2 года назад +1

      Thank you, Yaman :)

  • @KevinStoriesTV
    @KevinStoriesTV 2 года назад

    thank you for great tutorial

  • @Bianchi77
    @Bianchi77 2 года назад

    Nice video, thanks :)

  • @appidydafoo
    @appidydafoo 2 года назад

    Fascinating, thank you

  • @haldorasgirson9463
    @haldorasgirson9463 2 года назад +1

    I used that same accelerometer in my last project. Fantastic specs, but it is not a cheap part.

  • @Drxxx
    @Drxxx 2 года назад

    great channel!

  • @mozone180
    @mozone180 2 года назад

    Amazing content🙏

  • @mata7648
    @mata7648 2 года назад

    Very useful video. Thanks. My temperature calculation is a bit different from yours. I got temp = (2111.25-tempRaw)/9.05.

  • @ajayjoseph4498
    @ajayjoseph4498 2 года назад

    Brilliant Phil

    • @PhilsLab
      @PhilsLab  2 года назад

      Thank you, Ajay!

  • @bixenbaer
    @bixenbaer Год назад +1

    At 27:27 you say 2 8 byte registers. Should be 2 8 bit registers right? Beside that: thank you!! Truly awesome !

  • @TheGhost13X
    @TheGhost13X 2 года назад

    Hi Phil, will you finish your control system videos in the near future? It would be awesome, thank you!

  • @piotrlenarczyk5803
    @piotrlenarczyk5803 2 года назад

    Thank you for video.

    • @PhilsLab
      @PhilsLab  2 года назад

      Thank you for watching, Piotr!

  • @bkcyigit
    @bkcyigit 2 года назад

    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

    • @PhilsLab
      @PhilsLab  2 года назад

      Thank you very much, Berkay :)

  • @amandioreal2293
    @amandioreal2293 2 года назад

    Very nice vídeo. Thank you

    • @PhilsLab
      @PhilsLab  2 года назад

      Thank you, Amandio :)

  • @srinirupan
    @srinirupan Год назад

    Could you make a video about driver development with the consideration of timing signals like a MEMS mic.

  • @CannonballCircuit
    @CannonballCircuit 11 месяцев назад

    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()?

  • @jackreacher3395
    @jackreacher3395 2 года назад

    You are the best bro
    Thank you

    • @PhilsLab
      @PhilsLab  2 года назад

      Thank you for watching!

  • @muzairafzal1529
    @muzairafzal1529 2 года назад

    Grrreeaatt work brother.... 😍

  • @clifforddicarlo9178
    @clifforddicarlo9178 Год назад

    What is the sense of having temperature if the acceleration read-outs are not temperature compensated?

  • @phillipmaser132
    @phillipmaser132 9 месяцев назад

    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?

  • @ravimruthyunjaya8026
    @ravimruthyunjaya8026 2 года назад

    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

    • @PhilsLab
      @PhilsLab  2 года назад

      Thanks, Ravi! That's only because I had paused the execution of the code via a breakpoint. The USB peripheral works as it should.

  • @arzamas1988
    @arzamas1988 2 года назад

    Thx!

  • @plextrumgames
    @plextrumgames 2 года назад +1

    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.

    • @ikbendusan
      @ikbendusan 2 года назад +3

      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

  • @ruben34
    @ruben34 Год назад

    24:00 I just use windows calculator for converting numbers between BIN HEX DEC, I find it quite useful.

  • @wahabshah2010
    @wahabshah2010 2 года назад

    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

  • @ShaunStruwig
    @ShaunStruwig Год назад +4

    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?

    • @XenoTravis
      @XenoTravis 8 месяцев назад

      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.

  • @kunalsalvi8382
    @kunalsalvi8382 2 года назад

    Loved the video, Phil. I saw your board didn't get recognized. How do you solve that issue?

    • @PhilsLab
      @PhilsLab  2 года назад

      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.

  • @seankayll9017
    @seankayll9017 2 года назад +1

    Great video, thanks. Forty odd quid for that sensor though, crikey. What is so special about it that makes it so expensive?

    • @t7732155980
      @t7732155980 2 года назад

      This sensor has low noise and high accuracy (and you can trust ADI to meet the spec)

    • @PhilsLab
      @PhilsLab  2 года назад

      Thanks, Sean! Low-noise, low-drift, etc. etc. make it pretty pricey.

  • @sosscs
    @sosscs 10 месяцев назад

    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

  • @datawolk
    @datawolk 2 года назад

    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.

  • @kitKat-by6kk
    @kitKat-by6kk Год назад

    As per others, Great content. Since the content is already pretyped, its easy for me to copy or compare..

  • @AboveEmAllProduction
    @AboveEmAllProduction 2 года назад

    Nice i wrote my own driver for my gtx 970 I got a few extra dps. I wrote the driver in ms basic.

  • @mhcbon4606
    @mhcbon4606 2 года назад

    pretty neat.

  • @lamyaahmed4077
    @lamyaahmed4077 Месяц назад

    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 ?

  • @Cash-sp8xi
    @Cash-sp8xi 8 месяцев назад

    Amazing voice

  • @stati5tik
    @stati5tik 5 месяцев назад

    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?

  • @fredhaeir3005
    @fredhaeir3005 Месяц назад

    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 :)

  • @mmk34
    @mmk34 2 года назад

    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?

  • @ambientsoda106
    @ambientsoda106 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...

  • @Kefford666
    @Kefford666 2 года назад +1

    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?

    • @666aron
      @666aron 2 года назад

      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).

  • @TheStuartstardust
    @TheStuartstardust 2 года назад

    I would use a union to join the incoming 8bits data - or is that a bad idea? 🤔

  • @frankbose544
    @frankbose544 2 года назад

    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

    • @frankbose544
      @frankbose544 2 года назад

      nm lol im a stm32 noob clearly the hal does this for you

  • @shashankshekhar6549
    @shashankshekhar6549 2 года назад

    Please Put some info. on how to select a stm32 for specific use.

  • @nononnomonohjghdgdshrsrhsjgd
    @nononnomonohjghdgdshrsrhsjgd 2 года назад +1

    How did you learn all these? What have you studied? Could you direct me to some courses?

  • @sudheerkumar5966
    @sudheerkumar5966 2 года назад

    Hello sir very good