Data.h
Go to the documentation of this file.
1 /****
2  * Data.h
3  *
4  * Copyright 2018 mikee47 <mike@sillyhouse.net>
5  *
6  * This file is part of the HardwareSPI Library
7  *
8  * This library is free software: you can redistribute it and/or modify it under the terms of the
9  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with this library.
16  * If not, see <https://www.gnu.org/licenses/>.
17  *
18  * @author: 11 December 2018 - mikee47 <mike@sillyhouse.net>
19  *
20  * Definitions for an SPI Request Packet containing data and settings for a transfer.
21  * A single transfer may use 1 or more transactions.
22  *
23  ****/
24 
25 #pragma once
26 
27 #include <cstdint>
28 #include <sming_attr.h>
29 
30 namespace HSPI
31 {
43 struct Data {
44  union {
45  uint8_t data8;
46  uint16_t data16;
47  uint32_t data32;
48  uint8_t data[4];
49  void* ptr;
50  const void* cptr;
51  uint8_t* ptr8;
52  };
53  uint16_t length : 15;
54  uint16_t isPointer : 1;
55 
56  Data()
57  {
58  clear();
59  }
60 
64  __forceinline void clear()
65  {
66  data32 = 0;
67  length = 0;
68  isPointer = 0;
69  }
70 
76  __forceinline void set(const void* data, uint16_t count)
77  {
78  cptr = data;
79  length = count;
80  isPointer = 1;
81  }
82 
83  void* get()
84  {
85  return isPointer ? ptr : data;
86  }
87 
98  __forceinline void set8(uint8_t data)
99  {
100  set32(data, 1);
101  }
102 
107  __forceinline void set16(uint16_t data)
108  {
109  set32(data, 2);
110  }
111 
117  __forceinline void set32(uint32_t data, uint8_t len = 4)
118  {
119  data32 = data;
120  length = len;
121  isPointer = 0;
122  }
123 
125 };
126 
127 } // namespace HSPI
Definition: Common.h:35
Specifies a block incoming or outgoing data.
Definition: Data.h:43
uint16_t length
Number of bytes of data.
Definition: Data.h:53
void set16(uint16_t data)
Set to single 16-bit value.
Definition: Data.h:107
uint16_t isPointer
If set, data is referenced indirectly, otherwise it's stored directly.
Definition: Data.h:54
uint32_t data32
Definition: Data.h:47
uint8_t data[4]
Definition: Data.h:48
void set(const void *data, uint16_t count)
Set to reference external data block.
Definition: Data.h:76
void * ptr
Pointer to data.
Definition: Data.h:49
uint8_t * ptr8
Definition: Data.h:51
Data()
Definition: Data.h:56
const void * cptr
Definition: Data.h:50
void * get()
Definition: Data.h:83
uint16_t data16
Definition: Data.h:46
void set32(uint32_t data, uint8_t len=4)
Set to 32-bit data.
Definition: Data.h:117
void clear()
Reset to zero-length.
Definition: Data.h:64
uint8_t data8
Definition: Data.h:45
void set8(uint8_t data)
Set to single 8-bit value.
Definition: Data.h:98