acc_reg_protocol.c File Reference
#include <inttypes.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "acc_reg_protocol.h"

Go to the source code of this file.

Macros

#define REG_INVALID_ADDRESS   0xffffU
 
#define REG_ERROR_READ_ADDRESS   0xeeeeee00U
 
#define REG_ERROR_READ_OF_WO_REG   0xeeeeee01U
 

Enumerations

enum  acc_reg_mode_t { REG_MODE_READ_WRITE, REG_MODE_READ, REG_MODE_WRITE, REG_MODE_ERROR }
 Register access mode. More...
 
enum  acc_reg_protocol_state_t {
  REG_STATE_WAIT_FOR_ADDRESS, REG_STATE_WAIT_FOR_READ_OR_WRITE, REG_STATE_WAIT_FOR_WRITE, REG_STATE_WAIT_FOR_READ,
  REG_STATE_NACK_NEXT_WRITE
}
 Register protocol state. More...
 

Functions

static const acc_reg_protocol_tget_register_struct (uint16_t register_address)
 Get register struct from protocol struct. More...
 
static void set_address (uint8_t *buffer)
 Set current register address. More...
 
static bool register_is_writable (void)
 Return true if register is writable. More...
 
static void read_register (uint8_t *buffer)
 Read register at current register address and increase address. More...
 
static bool write_register (uint8_t *buffer)
 Write register at current register address and increase address. More...
 
void acc_reg_protocol_setup (const acc_reg_protocol_t *protocol_struct, uint16_t register_count)
 Setup register protocol. More...
 
void acc_reg_protocol_reset (void)
 Reset register protocol. More...
 
bool acc_reg_protocol_data_nack (void)
 Should protocol NACK the next data. More...
 
void acc_reg_protocol_data_in (uint8_t *buffer, size_t data_in_length)
 Handle data input to the register protocol. More...
 
void acc_reg_protocol_data_out (uint8_t *buffer, size_t data_out_length)
 Handle data input from the register protocol. More...
 
uint32_t acc_reg_protocol_get_error_flags (void)
 Get the error flags for the register protocol. More...
 
int32_t acc_reg_protocol_float_to_int32_milli (float value)
 Convert 1000 * float to int32. More...
 
uint32_t acc_reg_protocol_float_to_uint32_milli (float value)
 Convert 1000 * float to uint32. More...
 
float acc_reg_protocol_int32_milli_to_float (int32_t value)
 Convert int32 / 1000 to float. More...
 
float acc_reg_protocol_uint32_milli_to_float (uint32_t value)
 Convert uint32 / 1000 to float. More...
 

Variables

static const acc_reg_protocol_treg_protocol_struct = NULL
 
static uint16_t reg_register_count = 0
 
static acc_reg_protocol_state_t reg_state = REG_STATE_WAIT_FOR_ADDRESS
 
static uint16_t reg_address = 0xffffU
 
static uint32_t reg_error_flags = 0
 

Macro Definition Documentation

◆ REG_ERROR_READ_ADDRESS

#define REG_ERROR_READ_ADDRESS   0xeeeeee00U

Definition at line 39 of file acc_reg_protocol.c.

◆ REG_ERROR_READ_OF_WO_REG

#define REG_ERROR_READ_OF_WO_REG   0xeeeeee01U

Definition at line 40 of file acc_reg_protocol.c.

◆ REG_INVALID_ADDRESS

#define REG_INVALID_ADDRESS   0xffffU

Definition at line 38 of file acc_reg_protocol.c.

Enumeration Type Documentation

◆ acc_reg_mode_t

Register access mode.

Enumerator
REG_MODE_READ_WRITE 
REG_MODE_READ 
REG_MODE_WRITE 
REG_MODE_ERROR 

Definition at line 17 of file acc_reg_protocol.c.

◆ acc_reg_protocol_state_t

Register protocol state.

Enumerator
REG_STATE_WAIT_FOR_ADDRESS 
REG_STATE_WAIT_FOR_READ_OR_WRITE 
REG_STATE_WAIT_FOR_WRITE 
REG_STATE_WAIT_FOR_READ 
REG_STATE_NACK_NEXT_WRITE 

Definition at line 29 of file acc_reg_protocol.c.

Function Documentation

◆ 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.

Parameters
[in]bufferThe data to be processed by the protocol
[in]data_in_lengthThe byte length of the data to be processed
  • Read only register
  • No valid address, read is still possoble
  • Write register failed
  • Next register is read only

Definition at line 230 of file acc_reg_protocol.c.

◆ acc_reg_protocol_data_nack()

bool acc_reg_protocol_data_nack ( void  )

Should protocol NACK the next data.

Return true if protocol should nack the next data write

Definition at line 218 of file acc_reg_protocol.c.

◆ 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.

Parameters
[in]bufferThe data buffer to be filled with data
[in]data_out_lengthThe byte length of the data to be read

Definition at line 294 of file acc_reg_protocol.c.

◆ acc_reg_protocol_float_to_int32_milli()

int32_t acc_reg_protocol_float_to_int32_milli ( float  value)

Convert 1000 * float to int32.

Parameters
[in]valueA float value
Returns
The float value * 1000 as an int32

Definition at line 330 of file acc_reg_protocol.c.

◆ acc_reg_protocol_float_to_uint32_milli()

uint32_t acc_reg_protocol_float_to_uint32_milli ( float  value)

Convert 1000 * float to uint32.

Parameters
[in]valueA float value
Returns
The float value * 1000 as an uint32

Definition at line 336 of file acc_reg_protocol.c.

◆ acc_reg_protocol_get_error_flags()

uint32_t acc_reg_protocol_get_error_flags ( void  )

Get the error flags for the register protocol.

The error flags will be cleared by this call

ACC_REG_ERROR_FLAG_PROTOCOL_STATE_ERROR The flag will be set if for example the protocol got unexpected data

ACC_REG_ERROR_FLAG_PACKET_LENGTH_ERROR The flag will be set if for example the protocol got data with unexpected length

ACC_REG_ERROR_FLAG_ADDRESS_ERROR The flag will be set if a register address outside the address space is used

ACC_REG_ERROR_FLAG_WRITE_FAILED The flag will be set if a register write failed

ACC_REG_ERROR_FLAG_WRITE_TO_READ_ONLY The flag will be set if a read only register is written

Return The protocol error flags

Definition at line 319 of file acc_reg_protocol.c.

◆ acc_reg_protocol_int32_milli_to_float()

float acc_reg_protocol_int32_milli_to_float ( int32_t  value)

Convert int32 / 1000 to float.

Parameters
[in]valueAn int32 value
Returns
The int32 value / 1000 as a float

Definition at line 342 of file acc_reg_protocol.c.

◆ acc_reg_protocol_reset()

void acc_reg_protocol_reset ( void  )

Reset register protocol.

This function should be called when a new transaction start.

Definition at line 211 of file acc_reg_protocol.c.

◆ acc_reg_protocol_setup()

void acc_reg_protocol_setup ( const acc_reg_protocol_t protocol_struct,
uint16_t  register_count 
)

Setup register protocol.

Parameters
[in]protocol_structThe protocol struct with read/write functions
[in]register_countThe number of registers in the struct

Definition at line 202 of file acc_reg_protocol.c.

◆ acc_reg_protocol_uint32_milli_to_float()

float acc_reg_protocol_uint32_milli_to_float ( uint32_t  value)

Convert uint32 / 1000 to float.

Parameters
[in]valueAn uint32 value
Returns
The uint32 value / 1000 as a float

Definition at line 351 of file acc_reg_protocol.c.

◆ get_register_struct()

static const acc_reg_protocol_t* get_register_struct ( uint16_t  register_address)
static

Get register struct from protocol struct.

Parameters
[in]register_addressThe address of the register
Returns
A pointer to the register struct, NULL if the address is invalid

Definition at line 55 of file acc_reg_protocol.c.

◆ read_register()

static void read_register ( uint8_t *  buffer)
static

Read register at current register address and increase address.

Parameters
[out]bufferThe output data buffer

Definition at line 114 of file acc_reg_protocol.c.

◆ register_is_writable()

static bool register_is_writable ( void  )
static

Return true if register is writable.

Definition at line 101 of file acc_reg_protocol.c.

◆ set_address()

static void set_address ( uint8_t *  buffer)
static

Set current register address.

Parameters
[in]bufferThe input data buffer

Definition at line 80 of file acc_reg_protocol.c.

◆ write_register()

static bool write_register ( uint8_t *  buffer)
static

Write register at current register address and increase address.

Parameters
[in]bufferThe input data buffer

Definition at line 156 of file acc_reg_protocol.c.

Variable Documentation

◆ reg_address

uint16_t reg_address = 0xffffU
static

Definition at line 45 of file acc_reg_protocol.c.

◆ reg_error_flags

uint32_t reg_error_flags = 0
static

Definition at line 46 of file acc_reg_protocol.c.

◆ reg_protocol_struct

const acc_reg_protocol_t* reg_protocol_struct = NULL
static

Definition at line 42 of file acc_reg_protocol.c.

◆ reg_register_count

uint16_t reg_register_count = 0
static

Definition at line 43 of file acc_reg_protocol.c.

◆ reg_state

Definition at line 44 of file acc_reg_protocol.c.