I'm currently developing a radio (nRF24) module driver on top of a homemade SPI driver. The driver is pretty simple: to send a message via the radio module, you need to send the message via SPI, prefixed with a command:

[![Sending a RF message](https://i.stack.imgur.com/cDE9b.png)](https://i.stack.imgur.com/cDE9b.png)

I got it working perfectly, but I'm struggling with the encapsulation of the user payload (i.e adding the NRF24\_CMD before the user payload).

As of today, I'm allocating a new char array for the SPI payload each time I'm sending a message. I'm writing the radio module command in the first byte of the array, and then copying the user payload as demonstrated here:

```c
void nrf24_send(char * payload, size_t length){
    char spi_payload[MAX_DATA];
    spi_payload[0] = RF_SEND_CMD;
    for(int i=0; i<length; i++){
        spi_payload[i+1] = payload[i];
    }
    spi_write(spi_payload, length + 1);
}

```

This generates a lot of pointless memory accesses just to copy the user payload and I feel that there is room for improvement.

**What would be an efficient way to encapsulate the user message ?**

**Is there a good practice in the driver development community for these kinds of problems ?**

FYI:

* I'm programming a microcontroller (MSP430FR5969) bare metal, so I don't have any dynamic allocation mechanisms implemented yet
* The MCU I'm using is equipped with a DMA

Here are a few ideas I had:

1. using memcpy instead of the for loop -> Better, the copy of the array would be faster, but there's still the need to copy the payload
2. tell the user to allocate the buffer and left the first byte empty -> No copy needed, but the user needs to know about the hardware, and I don't want that
3. create a c struct to hide the complexity (cf below) -> I'm afraid that alignments issues may arise, and since my NRF24\_CMD byte needs to be directly followed by the payload, I don't know if I can trust my compiler...

Hiding the complexity with a struct:

```c
struct nrf24_message_t {
   char cmd;
   char payload;
}

```

Thanks for your help!

Hugo