acc_reg_protocol.h
Go to the documentation of this file.
1 // Copyright (c) Acconeer AB, 2023
2 // All rights reserved
3 
4 #ifndef ACC_REG_PROTOCOL_H_
5 #define ACC_REG_PROTOCOL_H_
6 
7 #include <stdbool.h>
8 #include <stddef.h>
9 #include <stdint.h>
10 
11 #define ACC_REG_PROTOCOL_ADDRESS_LENGTH (2U)
12 #define ACC_REG_PROTOCOL_REGDATA_LENGTH (4U)
13 
14 #define ACC_REG_ERROR_FLAG_PROTOCOL_STATE_ERROR (1U << 0U)
15 #define ACC_REG_ERROR_FLAG_PACKET_LENGTH_ERROR (1U << 1U)
16 #define ACC_REG_ERROR_FLAG_ADDRESS_ERROR (1U << 2U)
17 #define ACC_REG_ERROR_FLAG_WRITE_FAILED (1U << 3U)
18 #define ACC_REG_ERROR_FLAG_WRITE_TO_READ_ONLY (1U << 4U)
19 
20 /**
21  * @brief Function to read a register
22  *
23  * @param[out] data The data to be read
24  */
25 typedef void (acc_reg_read_func_t)(uint32_t *data);
26 
27 /**
28  * @brief Function to writes a register
29  *
30  * @param[in] data The data to be written
31  * @return The access result OK/ERROR
32  */
33 typedef bool (acc_reg_write_func_t)(const uint32_t data);
34 
35 /**
36  * @brief Register access mode and functions struct
37  */
38 typedef struct
39 {
40  uint16_t address;
44 
45 /**
46  * @brief Setup register protocol
47  *
48  * @param[in] protocol_struct The protocol struct with read/write functions
49  * @param[in] register_count The number of registers in the struct
50  */
51 void acc_reg_protocol_setup(const acc_reg_protocol_t *protocol_struct, uint16_t register_count);
52 
53 
54 /**
55  * @brief Reset register protocol
56  *
57  * This function should be called when a new transaction start.
58  */
59 void acc_reg_protocol_reset(void);
60 
61 
62 /**
63  * @brief Should protocol NACK the next data
64  *
65  * Return true if protocol should nack the next data write
66  */
68 
69 
70 /**
71  * @brief Handle data input to the register protocol
72  *
73  * @param[in] buffer The data to be processed by the protocol
74  * @param[in] data_in_length The byte length of the data to be processed
75  */
76 void acc_reg_protocol_data_in(uint8_t *buffer, size_t data_in_length);
77 
78 
79 /**
80  * @brief Handle data input from the register protocol
81  *
82  * @param[in] buffer The data buffer to be filled with data
83  * @param[in] data_out_length The byte length of the data to be read
84  */
85 void acc_reg_protocol_data_out(uint8_t *buffer, size_t data_out_length);
86 
87 
88 /**
89  * @brief Get the error flags for the register protocol
90  *
91  * The error flags will be cleared by this call
92  *
93  * ACC_REG_ERROR_FLAG_PROTOCOL_STATE_ERROR
94  * The flag will be set if for example the protocol got unexpected data
95  *
96  * ACC_REG_ERROR_FLAG_PACKET_LENGTH_ERROR
97  * The flag will be set if for example the protocol got data with unexpected length
98  *
99  * ACC_REG_ERROR_FLAG_ADDRESS_ERROR
100  * The flag will be set if a register address outside the address space is used
101  *
102  * ACC_REG_ERROR_FLAG_WRITE_FAILED
103  * The flag will be set if a register write failed
104  *
105  * ACC_REG_ERROR_FLAG_WRITE_TO_READ_ONLY
106  * The flag will be set if a read only register is written
107  *
108  * Return The protocol error flags
109  */
110 uint32_t acc_reg_protocol_get_error_flags(void);
111 
112 
113 //
114 // Register protocol utility functions used for
115 // conversion between uint32/int32 and float
116 //
117 
118 
119 /**
120  * @brief Convert 1000 * float to int32
121  *
122  * @param[in] value A float value
123  * @return The float value * 1000 as an int32
124  */
125 int32_t acc_reg_protocol_float_to_int32_milli(float value);
126 
127 
128 /**
129  * @brief Convert 1000 * float to uint32
130  *
131  * @param[in] value A float value
132  * @return The float value * 1000 as an uint32
133  */
134 uint32_t acc_reg_protocol_float_to_uint32_milli(float value);
135 
136 
137 /**
138  * @brief Convert int32 / 1000 to float
139  *
140  * @param[in] value An int32 value
141  * @return The int32 value / 1000 as a float
142  */
143 float acc_reg_protocol_int32_milli_to_float(int32_t value);
144 
145 
146 /**
147  * @brief Convert uint32 / 1000 to float
148  *
149  * @param[in] value An uint32 value
150  * @return The uint32 value / 1000 as a float
151  */
152 float acc_reg_protocol_uint32_milli_to_float(uint32_t value);
153 
154 
155 #endif
acc_reg_protocol_float_to_uint32_milli
uint32_t acc_reg_protocol_float_to_uint32_milli(float value)
Convert 1000 * float to uint32.
Definition: acc_reg_protocol.c:336
acc_reg_protocol_data_in
void acc_reg_protocol_data_in(uint8_t *buffer, size_t data_in_length)
Handle data input to the register protocol.
Definition: acc_reg_protocol.c:230
acc_reg_protocol_get_error_flags
uint32_t acc_reg_protocol_get_error_flags(void)
Get the error flags for the register protocol.
Definition: acc_reg_protocol.c:319
acc_reg_protocol_t::write
acc_reg_write_func_t * write
Definition: acc_reg_protocol.h:42
acc_reg_write_func_t
bool() acc_reg_write_func_t(const uint32_t data)
Function to writes a register.
Definition: acc_reg_protocol.h:33
acc_reg_protocol_setup
void acc_reg_protocol_setup(const acc_reg_protocol_t *protocol_struct, uint16_t register_count)
Setup register protocol.
Definition: acc_reg_protocol.c:202
acc_reg_protocol_data_nack
bool acc_reg_protocol_data_nack(void)
Should protocol NACK the next data.
Definition: acc_reg_protocol.c:218
acc_reg_protocol_float_to_int32_milli
int32_t acc_reg_protocol_float_to_int32_milli(float value)
Convert 1000 * float to int32.
Definition: acc_reg_protocol.c:330
acc_reg_protocol_uint32_milli_to_float
float acc_reg_protocol_uint32_milli_to_float(uint32_t value)
Convert uint32 / 1000 to float.
Definition: acc_reg_protocol.c:351
acc_reg_protocol_int32_milli_to_float
float acc_reg_protocol_int32_milli_to_float(int32_t value)
Convert int32 / 1000 to float.
Definition: acc_reg_protocol.c:342
acc_reg_protocol_t::address
uint16_t address
Definition: acc_reg_protocol.h:40
acc_reg_protocol_t
Register access mode and functions struct.
Definition: acc_reg_protocol.h:38
acc_reg_protocol_data_out
void acc_reg_protocol_data_out(uint8_t *buffer, size_t data_out_length)
Handle data input from the register protocol.
Definition: acc_reg_protocol.c:294
acc_reg_protocol_reset
void acc_reg_protocol_reset(void)
Reset register protocol.
Definition: acc_reg_protocol.c:211
acc_reg_read_func_t
void() acc_reg_read_func_t(uint32_t *data)
Function to read a register.
Definition: acc_reg_protocol.h:25
acc_reg_protocol_t::read
acc_reg_read_func_t * read
Definition: acc_reg_protocol.h:41