Rleg  2
 All Data Structures Files Functions Variables Typedefs Macros Groups Pages
Functions
Function of Encoder AMT203

Functions

int enc_read_pos (int spi_dev, unsigned short int *data)
 READ POSITION. More...
 
int enc_zero_set (int spi_dev)
 SET ZERO POINT. More...
 
int enc_wait_for_ack (int spi_dev, uint8_t ack, int max_errors)
 send command and delay between reads More...
 

Detailed Description

Function Documentation

int enc_read_pos ( int  spi_dev,
unsigned short int *  data 
)

READ POSITION.

Parameters
spi_devCommunication Port
[in]*dataData to read
Returns
flag with SUCCESS or FAILURE

Check this function, in case of error put delay before each byte data

Definition at line 37 of file encoder_functions.c.

References ARRAY_SIZE, enc_wait_for_ack(), ENCODER_NO_OP, ENCODER_RD_POS, FAILURE, INT_MAX, spi_trans_bytes(), and SUCCESS.

Referenced by read_all_data().

37  {
38 
39  uint8_t send[] = {ENCODER_RD_POS,ENCODER_NO_OP};
40  uint8_t receive[ARRAY_SIZE(send)] = {0, };
41  uint8_t buffer[ARRAY_SIZE(send)] = {0, } ;
42 
43  /* Send command */
44  spi_trans_bytes(spi_dev,send,receive,1);
45  if (enc_wait_for_ack(spi_dev, ENCODER_RD_POS, INT_MAX) != SUCCESS) {
46  return FAILURE;
47  }
48 
49  send[0]=ENCODER_NO_OP;
50 
51  if(spi_trans_bytes(spi_dev,send,receive,2) != SUCCESS) {
52  return FAILURE;
53  }
54 
55  buffer[0] = receive[0];
56 
60  if(spi_trans_bytes(spi_dev,send,receive,2) != SUCCESS) {
61  return FAILURE;
62  }
63 
64  buffer[1] = receive[0];
65 
66  *data = buffer[0]*256+buffer[1];
67 
68  //printf("\nreceive = 0x %2x %2x %d\n",buffer[0],buffer[1],data);
69 
70  return SUCCESS;
71 }
int enc_wait_for_ack(int spi_dev, uint8_t ack, int max_errors)
send command and delay between reads
#define INT_MAX
int spi_trans_bytes(int spi_dev, uint8_t *send, uint8_t *receive, int n)
TRANSFER N BYTES.
#define ARRAY_SIZE(a)
Definition: spi_functions.h:29
#define FAILURE
Definition: calibration.h:7
#define ENCODER_NO_OP
#define ENCODER_RD_POS
#define SUCCESS
Definition: calibration.h:6

Here is the call graph for this function:

Here is the caller graph for this function:

int enc_wait_for_ack ( int  spi_dev,
uint8_t  ack,
int  max_errors 
)

send command and delay between reads

Parameters
spi_devCommunication Dev
[in]dataData to write
Returns
flag with SUCCESS or FAILURE

Definition at line 73 of file encoder_functions.c.

References ENCODER_NO_OP, ENCODER_WAIT_RESP, FAILURE, spi_trans_bytes(), and SUCCESS.

Referenced by enc_read_pos(), and enc_zero_set().

74 {
75  int errors = 0;
76  uint8_t send = ENCODER_NO_OP;
77  uint8_t receive = 1;
78 
79  //nanosleep(10);
80  //spi_trans_bytes(spi_dev,send,receive,1);
81  while (receive != ack) {
82  spi_trans_bytes(spi_dev,&send,&receive,1);
83  //printf("\nReceive = 0x%x \n",receive);
84  if (receive != ENCODER_WAIT_RESP) {
85  errors++;
86  if (errors > max_errors) { return FAILURE; }
87  }
88  }
89 
90 
91  return SUCCESS;
92 }
int spi_trans_bytes(int spi_dev, uint8_t *send, uint8_t *receive, int n)
TRANSFER N BYTES.
#define FAILURE
Definition: calibration.h:7
#define ENCODER_NO_OP
#define ENCODER_WAIT_RESP
#define SUCCESS
Definition: calibration.h:6

Here is the call graph for this function:

Here is the caller graph for this function:

int enc_zero_set ( int  spi_dev)

SET ZERO POINT.

Parameters
spi_devCommunication Dev
[in]dataData to write
Returns
flag with SUCCESS or FAILURE

Definition at line 19 of file encoder_functions.c.

References enc_wait_for_ack(), ENCODER_EEPROM_WR, ENCODER_SET_ZERO_PT, FAILURE, INT_MAX, spi_trans_bytes(), and SUCCESS.

19  {
20 
21  uint8_t send = ENCODER_SET_ZERO_PT;
22  uint8_t receive = 0;
23 
24  /* Send command */
25  spi_trans_bytes(spi_dev,&send,&receive,1);
27  return FAILURE;
28  }
29 
30  /* Note:
31  * From datasheet, the encoder must be power-cycled after this command
32  * for the new zero position to be used in the position calculation. */
33 
34  return SUCCESS;
35 }
#define ENCODER_EEPROM_WR
int enc_wait_for_ack(int spi_dev, uint8_t ack, int max_errors)
send command and delay between reads
#define INT_MAX
int spi_trans_bytes(int spi_dev, uint8_t *send, uint8_t *receive, int n)
TRANSFER N BYTES.
#define FAILURE
Definition: calibration.h:7
#define SUCCESS
Definition: calibration.h:6
#define ENCODER_SET_ZERO_PT

Here is the call graph for this function: