{"id":3086,"date":"2024-12-20T05:28:49","date_gmt":"2024-12-20T05:28:49","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=3086"},"modified":"2024-12-20T05:28:51","modified_gmt":"2024-12-20T05:28:51","slug":"how-to-create-library-for-spi-sensor-part-2-creating-the-library","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=3086","title":{"rendered":"How to Create Library for SPI Sensor Part 2: Creating the library"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/FD7FA337-E1C0-40FA-AEF4-6F3C9271B7F9.png\" alt=\"\" class=\"wp-image-3087\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/FD7FA337-E1C0-40FA-AEF4-6F3C9271B7F9.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/FD7FA337-E1C0-40FA-AEF4-6F3C9271B7F9-300x300.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/FD7FA337-E1C0-40FA-AEF4-6F3C9271B7F9-150x150.png 150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/FD7FA337-E1C0-40FA-AEF4-6F3C9271B7F9-768x768.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/FD7FA337-E1C0-40FA-AEF4-6F3C9271B7F9-750x750.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/FD7FA337-E1C0-40FA-AEF4-6F3C9271B7F9-400x400.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/FD7FA337-E1C0-40FA-AEF4-6F3C9271B7F9-250x250.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>In the second part of creating a library for SPI sensor, we shall construct the source and header file to interface the sensor with STM32F4.<\/p>\n\n\n\n<p>In this guide, we shall cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Creating the header and source files.<\/li>\n\n\n\n<li>Gathering the registers.<\/li>\n\n\n\n<li>Developing the library.<\/li>\n\n\n\n<li>Retargting printf.<\/li>\n\n\n\n<li>Testing the library.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">4. Creating the Header and Source File:<\/h2>\n\n\n\n<p>After the project has been generated by STM32CubeMX within STM32CubeIDE, we shall add new source file and header file.<\/p>\n\n\n\n<p>Right click on Src folder and add new source file as following:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"486\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-07_07-03-43-1024x486.jpg\" alt=\"\" class=\"wp-image-2972\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-07_07-03-43-1024x486.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-07_07-03-43-300x142.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-07_07-03-43-768x364.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-07_07-03-43-1536x729.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-07_07-03-43-2048x972.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-07_07-03-43-1150x546.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-07_07-03-43-750x356.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-07_07-03-43-400x190.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-07_07-03-43-250x119.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Give a name for the source file, we shall name it as MPU9250.c as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"734\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-20_07-36-01-1024x734.jpg\" alt=\"\" class=\"wp-image-3088\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-20_07-36-01-1024x734.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-20_07-36-01-300x215.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-20_07-36-01-768x550.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-20_07-36-01-1150x824.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-20_07-36-01-750x538.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-20_07-36-01-400x287.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-20_07-36-01-250x179.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-20_07-36-01.jpg 1172w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>In similar manner, right click on Inc folder and add new header file with name of MPU9250.h.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. Gathering the Registers:<\/h2>\n\n\n\n<p>From part 1, we shall use the register map document of the MPU9250. <\/p>\n\n\n\n<p>In MPU9250.h header file, we shall declare most of the important register as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\/\/ Gyroscope Data Registers\n#define MPU9250_GYRO_XOUT_H       0x43  \/\/ High byte of X-axis gyroscope data\n#define MPU9250_GYRO_XOUT_L       0x44  \/\/ Low byte of X-axis gyroscope data\n#define MPU9250_GYRO_YOUT_H       0x45  \/\/ High byte of Y-axis gyroscope data\n#define MPU9250_GYRO_YOUT_L       0x46  \/\/ Low byte of Y-axis gyroscope data\n#define MPU9250_GYRO_ZOUT_H       0x47  \/\/ High byte of Z-axis gyroscope data\n#define MPU9250_GYRO_ZOUT_L       0x48  \/\/ Low byte of Z-axis gyroscope data\n\n\/\/ Gyroscope Configuration\n#define MPU9250_GYRO_CONFIG       0x1B  \/\/ Gyroscope configuration register\n\n\/\/ Accelerometer Data Registers\n#define MPU9250_ACCEL_XOUT_H      0x3B  \/\/ High byte of X-axis accelerometer data\n#define MPU9250_ACCEL_XOUT_L      0x3C  \/\/ Low byte of X-axis accelerometer data\n#define MPU9250_ACCEL_YOUT_H      0x3D  \/\/ High byte of Y-axis accelerometer data\n#define MPU9250_ACCEL_YOUT_L      0x3E  \/\/ Low byte of Y-axis accelerometer data\n#define MPU9250_ACCEL_ZOUT_H      0x3F  \/\/ High byte of Z-axis accelerometer data\n#define MPU9250_ACCEL_ZOUT_L      0x40  \/\/ Low byte of Z-axis accelerometer data\n\n\n\n\/\/ Accelerometer Configuration\n#define MPU9250_ACCEL_CONFIG      0x1C  \/\/ Accelerometer configuration register\n#define MPU9250_ACCEL_CONFIG_2    0x1D  \/\/ Accelerometer configuration 2 register\n\n#define USER_CTRL\t\t\t\t  0x6A  \/\/ User control register\n\n#define I2C_IF_DIS\t\t\t\t  0x10  \/\/ Disable I2C Mode and Enable SPI Mode<\/pre><\/div>\n\n\n\n<p>Here, we can find the configuration of the gyroscope and accelerometer, also another important register called user control which shall see how to use it later.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">6. Developing the Library:<\/h2>\n\n\n\n<p>After the declaration of the registers, declare the following enumeration to hold the acceleration range as following:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\/**\n * @brief Acceleration range options for the MPU9250 accelerometer.\n *\n * This enumeration defines the possible full-scale acceleration ranges\n * that can be configured for the MPU9250 accelerometer. The range determines\n * the sensitivity and maximum measurable acceleration.\n *\/\ntypedef enum\n{\n    ACC_FULL_SCALE_2_G = 0x00, \/**&lt; Full-scale range: \u00b12g *\/\n    ACC_FULL_SCALE_4_G = 0x08, \/**&lt; Full-scale range: \u00b14g *\/\n    ACC_FULL_SCALE_8_G = 0x10, \/**&lt; Full-scale range: \u00b18g *\/\n    ACC_FULL_SCALE_16_G = 0x18 \/**&lt; Full-scale range: \u00b116g *\/\n} ACC_Range_Typedef;<\/pre><\/div>\n\n\n\n<p>Next, we shall declare the initialization function of the accelerometer as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\/**\n * @brief Initialize the accelerometer of the MPU9250.\n *\n * This function configures the accelerometer section of the MPU9250.\n * It sets the acceleration range based on the input parameter.\n *\n * @param range The desired acceleration range to configure the MPU9250 accelerometer.\n *              The range should be of type `ACC_Range_Typedef`.\n *              It typically includes options like \u00b12g, \u00b14g, \u00b18g, or \u00b116g.\n *\n * @note This function does not return any value.\n *\/\nvoid MPU9250_Accel_Init(ACC_Range_Typedef range);<\/pre><\/div>\n\n\n\n<p>Next, the acceleration update function:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\/**\n * @brief Update the accelerometer data from the MPU9250.\n *\n * This function reads the latest accelerometer data from the MPU9250\n * and updates the internal data structure or variables storing the\n * acceleration values.\n *\n * @note Ensure that the MPU9250 is properly initialized before calling\n *       this function. This function does not return any value.\n *\/\nvoid MPU9250_Acceleration_Update(void);<\/pre><\/div>\n\n\n\n<p>Next, the functions to get the x, y and z accelerations as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\/**\n * @brief Get the acceleration value along the X-axis.\n *\n * This function retrieves the most recent acceleration value measured\n * along the X-axis by the MPU9250.\n *\n * @return The acceleration value along the X-axis in g (gravity) units.\n *\n * @note Ensure that the accelerometer data is updated by calling\n *       `MPU9250_Acceleration_Update()` before using this function.\n *\/\nfloat MPU9250_Get_AccelX(void);\n\n\/**\n * @brief Get the acceleration value along the Y-axis.\n *\n * This function retrieves the most recent acceleration value measured\n * along the Y-axis by the MPU9250.\n *\n * @return The acceleration value along the Y-axis in g (gravity) units.\n *\n * @note Ensure that the accelerometer data is updated by calling\n *       `MPU9250_Acceleration_Update()` before using this function.\n *\/\nfloat MPU9250_Get_AccelY(void);\n\n\/**\n * @brief Get the acceleration value along the Z-axis.\n *\n * This function retrieves the most recent acceleration value measured\n * along the Z-axis by the MPU9250.\n *\n * @return The acceleration value along the Z-axis in g (gravity) units.\n *\n * @note Ensure that the accelerometer data is updated by calling\n *       `MPU9250_Acceleration_Update()` before using this function.\n *\/\nfloat MPU9250_Get_AccelZ(void);\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Thats all for the header file, save it.<\/p>\n\n\n\n<p>Open MPU9250.c source, we start by including the MPU9250 header file, main.h and spi header file as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#include &quot;MPU9250.h&quot;\n\n#include &quot;main.h&quot;\n#include &quot;spi.h&quot;<\/pre><\/div>\n\n\n\n<p>Next, we need to declare a read flag as mentioned the datasheet of the MPU9250 as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"456\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-20_07-53-46-1024x456.jpg\" alt=\"\" class=\"wp-image-3089\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-20_07-53-46-1024x456.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-20_07-53-46-300x134.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-20_07-53-46-768x342.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-20_07-53-46-1536x684.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-20_07-53-46-2048x912.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-20_07-53-46-1150x512.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-20_07-53-46-750x334.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-20_07-53-46-400x178.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-20_07-53-46-250x111.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#define READ_FLAG   0x80<\/pre><\/div>\n\n\n\n<p>Declare a variable to hold the acceleration range passed by the user to be used later in the code as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">static float accelRange;<\/pre><\/div>\n\n\n\n<p>It is declared as static since it will be only used in the current MPU9250 source file and not accessible by any other source file.<\/p>\n\n\n\n<p>Declare the following two buffers:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">static uint8_t data[2];\nstatic uint8_t AccelerationData[6];<\/pre><\/div>\n\n\n\n<p>The data[2] which will hold the data to be transmitted to sensor while the accelerationData will hold the acceleration data measured by the sensor.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Next, we need to declare the read\/write functions for the sensor as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">static void MPU9250_Write(uint8_t *data, uint8_t len)\n{\n\tHAL_SPI_Transmit(&amp;hspi1, data, len, 100);\n}\n\n\nstatic void MPU9250_Read_Reg(uint8_t addr,uint8_t *data, uint8_t len)\n{\n\tuint8_t DummyBuffer[len];\n\n\tDummyBuffer[0]=addr|READ_FLAG;\n\n\tfor (int i=1;i&lt;len;i++)\n\t{\n\t\tDummyBuffer[i]=0x00;\n\t}\n\n\tHAL_SPI_TransmitReceive(&amp;hspi1, DummyBuffer, data, len, 100);\n}<\/pre><\/div>\n\n\n\n<p>For the write function:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">static void MPU9250_Write(uint8_t *data, uint8_t len)\n{\n\tHAL_SPI_Transmit(&amp;hspi1, data, len, 100);\n}\n<\/pre><\/div>\n\n\n\n<p>The function shall take the following parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Pointer to the data to be written to the sensor.<\/li>\n\n\n\n<li>Length of the data.<\/li>\n<\/ul>\n\n\n\n<p>In the function, we shall call:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">HAL_SPI_Transmit(&amp;hspi1, data, len, 100);<\/pre><\/div>\n\n\n\n<p>The function shall take the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>HandleTypeDef to SPI to be use which is SPI1 defined as hspi1 in this case.<\/li>\n\n\n\n<li>Data to be written.<\/li>\n\n\n\n<li>Length of the data.<\/li>\n\n\n\n<li>Timeout which is 100ms in this case.<\/li>\n<\/ul>\n\n\n\n<p>For the read register:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">static void MPU9250_Read_Reg(uint8_t addr,uint8_t *data, uint8_t len)<\/pre><\/div>\n\n\n\n<p>The function shall take the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Register address.<\/li>\n\n\n\n<li>Buffer to hold the data to be read.<\/li>\n\n\n\n<li>Length of the data.<\/li>\n<\/ul>\n\n\n\n<p>Within the function:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\tuint8_t DummyBuffer[len];\n\n\tDummyBuffer[0]=addr|READ_FLAG;\n\n\tfor (int i=1;i&lt;len;i++)\n\t{\n\t\tDummyBuffer[i]=0x00;\n\t}\n\n\tHAL_SPI_TransmitReceive(&amp;hspi1, DummyBuffer, data, len, 100);<\/pre><\/div>\n\n\n\n<p>First, create dummy buffer by the size passed to the function, this is needed to keep the clock of the SPI.<\/p>\n\n\n\n<p>Fill the first element of the buffer with the address ored with the read flag as mentioned in the datasheet of the sensor.<\/p>\n\n\n\n<p>File the rest of the buffer with zero.<\/p>\n\n\n\n<p>Next, we shall call:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">HAL_SPI_TransmitReceive(&amp;hspi1, DummyBuffer, data, len, 100);<\/pre><\/div>\n\n\n\n<p>This function shall write and read from SPI bus at the same time.<\/p>\n\n\n\n<p>It will take the following as parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>handleTypedef to the SPI which is hspi1 in this case.<\/li>\n\n\n\n<li>Data to be written which is in this case is dummy buffer.<\/li>\n\n\n\n<li>Buffer to the data to be read.<\/li>\n\n\n\n<li>Length of the data to be read\/write.<\/li>\n\n\n\n<li>Timeout of 100ms.<\/li>\n<\/ul>\n\n\n\n<p>Next, for selecting deselecting the sensor:<\/p>\n\n\n\n<p>It is as simple as setting the CS pin low when need to react with the sensor and high when not.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">static void MPU9250_Select(void)\n{\n\tHAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, RESET);\n}\n\nstatic void MPU9250_Deselect(void)\n{\n\tHAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, SET);\n}\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Next. for initialization of the accelerometer part:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">void MPU9250_Accel_Init(ACC_Range_Typedef range)\n{\n\tswitch(range)\n\t{\n\t  case ACC_FULL_SCALE_2_G:\n\t    accelRange = 2.0;\n\t    break;\n\t  case ACC_FULL_SCALE_4_G:\n\t    accelRange = 4.0;\n\t    break;\n\t  case ACC_FULL_SCALE_8_G:\n\t    accelRange = 8.0;\n\t    break;\n\t  case ACC_FULL_SCALE_16_G:\n\t    accelRange = 16.0;\n\t    break;\n\t  default:\n\t    return; \/\/ Return without writing invalid mode\n\t  }\n\n\tdata[0]=USER_CTRL; data[1]=I2C_IF_DIS;\n\n\tMPU9250_Select();\n\tMPU9250_Write(data,2);\n\tMPU9250_Deselect();\n\n\tdata[0]=MPU9250_ACCEL_CONFIG; data[1]=range;\n\tMPU9250_Select();\n\tMPU9250_Write(data,2);\n\tMPU9250_Deselect();\n\n}\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>The function shall take the acceleration range passed by the user.<\/p>\n\n\n\n<p>Check which value is passed, and store the equivalent of the range in accelRange.<\/p>\n\n\n\n<p>After that, we shall write to the sensor telling it that we are in SPI mode not in I2C by setting I2C_IF_DIS bit to 1 in user control register as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"730\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-20_08-12-37-1024x730.jpg\" alt=\"\" class=\"wp-image-3090\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-20_08-12-37-1024x730.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-20_08-12-37-300x214.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-20_08-12-37-768x548.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-20_08-12-37-1536x1095.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-20_08-12-37-1150x820.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-20_08-12-37-750x535.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-20_08-12-37-400x285.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-20_08-12-37-250x178.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-20_08-12-37.jpg 1840w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\tdata[0]=USER_CTRL; data[1]=I2C_IF_DIS;\n\n\tMPU9250_Select();\n\tMPU9250_Write(data,2);\n\tMPU9250_Deselect();<\/pre><\/div>\n\n\n\n<p>Next, set the acceleration range required by the user as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"464\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-20_08-15-08-1-1024x464.jpg\" alt=\"\" class=\"wp-image-3092\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-20_08-15-08-1-1024x464.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-20_08-15-08-1-300x136.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-20_08-15-08-1-768x348.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-20_08-15-08-1-1536x696.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-20_08-15-08-1-1150x521.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-20_08-15-08-1-750x340.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-20_08-15-08-1-400x181.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-20_08-15-08-1-250x113.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-20_08-15-08-1.jpg 1976w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\tdata[0]=MPU9250_ACCEL_CONFIG; data[1]=range;\n\tMPU9250_Select();\n\tMPU9250_Write(data,2);\n\tMPU9250_Deselect();\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Next, to update the sensor, it is as simple as reading the XOUT_H register as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">void MPU9250_Acceleration_Update(void)\n{\n\n\tMPU9250_Select();\n\n\tMPU9250_Read_Reg(MPU9250_ACCEL_XOUT_H,AccelerationData,6);\n\n\n\tMPU9250_Deselect();\n\n}<\/pre><\/div>\n\n\n\n<p>Since the sensor features auto increment to the register during the reading, it will automatically push the data of the next register with the next clock of the SPI.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>For the update function:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">static float MPU9250_accelGet(uint8_t highIndex, uint8_t lowIndex)\n{\n\t  int16_t v = ((int16_t) AccelerationData[highIndex]) &lt;&lt; 8 | AccelerationData[lowIndex];\n\t  return ((float) -v) * accelRange \/ (float) 0x8000;\n}\n\nfloat MPU9250_Get_AccelX(void)\n{\n  return MPU9250_accelGet(1, 2);\n}\n\nfloat MPU9250_Get_AccelY(void) {\n  return MPU9250_accelGet(3, 4);\n}\n\nfloat MPU9250_Get_AccelZ(void) {\n  return MPU9250_accelGet(5, 6);\n}\n<\/pre><\/div>\n\n\n\n<p>It is as following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Get the signed 16-bit data from the acceleration buffer.<\/li>\n\n\n\n<li>Multiply the result by the acceleration range.<\/li>\n\n\n\n<li>Divide the results by 0x8000 to ensure the correct conversion.<\/li>\n<\/ul>\n\n\n\n<p>Thats all for the source file.<\/p>\n\n\n\n<p>Save the project for now.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">7. Retargeting printf:<\/h2>\n\n\n\n<p>In order to retarget printf to use serial, create new source file in src folder with name of&nbsp;<strong><em>uartPrintf<\/em><\/strong>.c&nbsp;<\/p>\n\n\n\n<p>The content of the source file as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#include &quot;stdio.h&quot;\n\n#include &quot;usart.h&quot;\n\nint __io_putchar(int ch)\n{\n\tHAL_UART_Transmit(&amp;huart2, &amp;ch, 1, 10);\n\treturn ch;\n}<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">8. Testing the Library:<\/h2>\n\n\n\n<p>In main.c source file, first in user begin include section, include the following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#include &quot;stdio.h&quot;\n#include &quot;MPU9250.h&quot;<\/pre><\/div>\n\n\n\n<p>In user code begin PV, declare the following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">float AccX, AccY, AccZ;<\/pre><\/div>\n\n\n\n<p>In user code begin 2 in the main function:<\/p>\n\n\n\n<p>Initialize the accelerometer with scale of 2G as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">MPU9250_Accel_Init(ACC_FULL_SCALE_2_G);<\/pre><\/div>\n\n\n\n<p>In user code begin 3, update the sensor, get the new values and print them as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\t  MPU9250_Acceleration_Update();\n\n\t  \/*Get the acceleration data*\/\n\t  AccX=MPU9250_Get_AccelX();\n\t  AccY=MPU9250_Get_AccelY();\n\t  AccZ=MPU9250_Get_AccelZ();\n\n\t  printf(&quot;Ax=%0.3f \\t Ay=%0.3f \\t Az=%0.3f \\r\\n&quot;,AccX,AccY,AccZ);\n\t  HAL_Delay(100);<\/pre><\/div>\n\n\n\n<p>Note: In order to printf with float, you need to enable it a following:<\/p>\n\n\n\n<p>Right click on the project, then properties:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"719\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-07_17-21-41-1024x719.jpg\" alt=\"\" class=\"wp-image-2981\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-07_17-21-41-1024x719.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-07_17-21-41-300x211.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-07_17-21-41-768x539.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-07_17-21-41-1536x1078.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-07_17-21-41-2048x1438.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-07_17-21-41-1150x807.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-07_17-21-41-750x527.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-07_17-21-41-400x281.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-07_17-21-41-250x176.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Save the project, build it and run it on your board as following:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"28\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/image-1024x28.png\" alt=\"\" class=\"wp-image-2982\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/image-1024x28.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/image-300x8.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/image-768x21.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/image-1536x43.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/image-1150x32.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/image-750x21.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/image-400x11.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/image-250x7.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/image.png 1734w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Open your favourite serial terminal application, set the baudrate to be 115200 and you should get the following:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"618\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-13_11-17-36-1024x618.jpg\" alt=\"\" class=\"wp-image-3093\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-13_11-17-36-1024x618.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-13_11-17-36-300x181.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-13_11-17-36-768x463.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-13_11-17-36-1150x694.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-13_11-17-36-750x452.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-13_11-17-36-400x241.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-13_11-17-36-250x151.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/12\/2024-12-13_11-17-36.jpg 1386w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Note: The gyroscope part shall be left as task for the reader \ud83d\ude09 <\/p>\n\n\n\n<p>Happy coding \ud83d\ude09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the second part of creating a library for SPI sensor, we shall construct the source and header file to interface the sensor with STM32F4. In this guide, we shall cover the following: 4. Creating the Header and Source File: After the project has been generated by STM32CubeMX within STM32CubeIDE, we shall add new source [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,11,12],"tags":[],"class_list":["post-3086","post","type-post","status-publish","format-standard","hentry","category-embedded-systems","category-peripheral-drivers","category-stm32"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/3086"}],"collection":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3086"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/3086\/revisions"}],"predecessor-version":[{"id":3094,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/3086\/revisions\/3094"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3086"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3086"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3086"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}