Sending Struct on SPI?

Sending Struct on SPI?

Another short article for embedded developers like me who are learning

We all use structures in our C / C++ programing most of the time and we also use spi bus as well

95% of the time we just use spi bus to communicate with some sort of sensor or peripheral on bus only receiving bytes which are very much presented to us in formalized and agreed upon structured data way

Like i am sending you byte and you reply me with a byte

What if i want to send a whole structure on spi bus which contains something like this

struct

and my spi bus data register is only 8bit long how can i send a 32 bit unsigned integer with it and also a float etc,

is it even possible and lets say we are sending this data to some other microcontroller on pcb will it be able to receive it?

lets find a way to do it

Problem statement

we have both on sending and receiving side a buffer which is only 8bits long it can send and receive bytes perfectly and we want to send a struct which has all kind of data types so we need to serialize this struct into bytes


We need a memory in C which is 8 bits long in form of stack or array and contains the data our structure has

To get this memory we can use a union which is a shared memory we can put our struct in it and shape the union in form of byte arrays each element only 8bit long here is how we can do it

union containing the struct

Then we can copy our data of struct into a union object like this

copying main struct obj into union

then we can simply type cast our union object into uint8_t pointer and send that to our spi bus

This is the result i get on my logic analyzer

It is accurate if we ignore the endianness of esp32 which is little endian chip

also don't forget to pack your struct otherwise you will be sending extra bytes of garbage values which is bad

Hammad Ali

Embedded C/C++ Developer | BareMetal and RTOS Developer | Device Driver Developer

10 个月

One way of doing that could be to create a buffer of the size of the struct and use memcpy with increments to copy the data in the serial buffer. However, it is crude and error prone. This way is much effective!

Haseeb Zaib

Embedded Software Engineer | C/C++ enthusiasts | Embedded Linux | Application Designer | hardware designer | IOT specialist

10 个月

Mostly i use this same technique to convert 16bits or 32bits to individual 8bits. Never thought about it using with spi the same way.

要查看或添加评论,请登录

Isfandyar Qureshi的更多文章

  • Embedded Talks Ep 5: Multi-Level Feedback Queue Scheduling

    Embedded Talks Ep 5: Multi-Level Feedback Queue Scheduling

    In Episode 4, we discussed various scheduling algorithms and their trade-offs. However, a fundamental problem with…

  • Embedded Talks Ep 4: Scheduling Algorithms

    Embedded Talks Ep 4: Scheduling Algorithms

    An operating system must provide an scheduler that is tasked with job of handling multiple process/tasks Before we dive…

  • Embedded Talks Ep3 : Context Switching

    Embedded Talks Ep3 : Context Switching

    Context switching refers to the process where cpu halt program currently executing save its context and switch to…

  • Embedded Talks Ep2 : A Process/TASK/THREAD

    Embedded Talks Ep2 : A Process/TASK/THREAD

    Concepts an embedded engineer must know: Process: A process is simply a running program; at any instant of in time…

  • Embedded Talk Ep.1

    Embedded Talk Ep.1

    Concepts an embedded software engineer must know Operating System: An operating system is a piece of software that…

  • WHY IT SUCKS TO BE EMBEDDED ENGINEER!!!!

    WHY IT SUCKS TO BE EMBEDDED ENGINEER!!!!

    This article is a personal experience and observation over my 6-7 years as an electronics/embedded engineer. Your…

    69 条评论
  • Common Issue with SPI

    Common Issue with SPI

    Spi is very common protocol and is used very often it has its perk of multiple sensors on common bus etc Hardware used…

    1 条评论
  • MOCKING THE MYSTERY IN UNIT TESTING

    MOCKING THE MYSTERY IN UNIT TESTING

    Greeting everyone are you a software engineer making apps or are you an embedded iot developer. This article is for…

  • IS YOUR SPI GITCHY ?????

    IS YOUR SPI GITCHY ?????

    #embeddedsystems #spi #embeddedengineer Have you even face a scenario where your spi run sometimes very well and…

    8 条评论
  • RTOS DEBUGGING IN REAL TIME USING OZONE AND SYSTEM VIEWER

    RTOS DEBUGGING IN REAL TIME USING OZONE AND SYSTEM VIEWER

    Finally got my stlink-V2 on nucleo board to debug rtos providing me to see at what instance which task is running at…