Cheetah Software  1.0
rt_spi.h File Reference
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/spi/spidev.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <spi_command_t.hpp>
#include <spi_data_t.hpp>
#include <spi_torque_t.hpp>
+ Include dependency graph for rt_spi.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  spine_cmd_t
 
struct  spine_data_t
 

Macros

#define K_EXPECTED_COMMAND_SIZE   256
 
#define K_WORDS_PER_MESSAGE   66
 
#define K_EXPECTED_DATA_SIZE   116
 
#define K_KNEE_OFFSET_POS   4.35f
 
#define BYTE_TO_BINARY_PATTERN   "%c%c%c%c%c%c%c%c"
 
#define BYTE_TO_BINARY(byte)
 

Functions

void init_spi ()
 
void spi_send_receive (spi_command_t *command, spi_data_t *data)
 
void spi_driver_run ()
 
spi_data_t * get_spi_data ()
 
spi_command_t * get_spi_command ()
 

Macro Definition Documentation

#define BYTE_TO_BINARY (   byte)
Value:
(byte & 0x80 ? '1' : '0'), (byte & 0x40 ? '1' : '0'), \
(byte & 0x20 ? '1' : '0'), (byte & 0x10 ? '1' : '0'), \
(byte & 0x08 ? '1' : '0'), (byte & 0x04 ? '1' : '0'), \
(byte & 0x02 ? '1' : '0'), (byte & 0x01 ? '1' : '0')

Definition at line 33 of file rt_spi.h.

#define BYTE_TO_BINARY_PATTERN   "%c%c%c%c%c%c%c%c"

Definition at line 32 of file rt_spi.h.

#define K_EXPECTED_COMMAND_SIZE   256

Definition at line 27 of file rt_spi.h.

#define K_EXPECTED_DATA_SIZE   116

Definition at line 29 of file rt_spi.h.

#define K_KNEE_OFFSET_POS   4.35f

Definition at line 30 of file rt_spi.h.

#define K_WORDS_PER_MESSAGE   66

Definition at line 28 of file rt_spi.h.

Function Documentation

spi_command_t* get_spi_command ( )

Definition at line 332 of file rt_spi.cpp.

References spi_command_drv.

332  {
333  // printf("Get spi command\n");
334  return &spi_command_drv;
335 }
spi_command_t spi_command_drv
Definition: rt_spi.cpp:27

+ Here is the caller graph for this function:

spi_data_t* get_spi_data ( )

Definition at line 337 of file rt_spi.cpp.

References spi_data_drv.

337 { return &spi_data_drv; }
spi_data_t spi_data_drv
Definition: rt_spi.cpp:28

+ Here is the caller graph for this function:

void init_spi ( )

Definition at line 110 of file rt_spi.cpp.

References K_EXPECTED_COMMAND_SIZE, K_EXPECTED_DATA_SIZE, spi_command_drv, spi_data_drv, spi_mutex, and spi_open().

110  {
111  // check sizes:
112  size_t command_size = sizeof(spi_command_t);
113  size_t data_size = sizeof(spi_data_t);
114 
115  memset(&spi_command_drv, 0, sizeof(spi_command_drv));
116  memset(&spi_data_drv, 0, sizeof(spi_data_drv));
117 
118  if (pthread_mutex_init(&spi_mutex, NULL) != 0)
119  printf("[ERROR: RT SPI] Failed to create spi data mutex\n");
120 
121  if (command_size != K_EXPECTED_COMMAND_SIZE) {
122  printf("[RT SPI] Error command size is %ld, expected %d\n", command_size,
124  } else
125  printf("[RT SPI] command size good\n");
126 
127  if (data_size != K_EXPECTED_DATA_SIZE) {
128  printf("[RT SPI] Error data size is %ld, expected %d\n", data_size,
130  } else
131  printf("[RT SPI] data size good\n");
132 
133  printf("[RT SPI] Open\n");
134  spi_open();
135 }
#define K_EXPECTED_COMMAND_SIZE
Definition: rt_spi.h:27
#define K_EXPECTED_DATA_SIZE
Definition: rt_spi.h:29
pthread_mutex_t spi_mutex
Definition: rt_spi.cpp:31
spi_data_t spi_data_drv
Definition: rt_spi.cpp:28
int spi_open()
Definition: rt_spi.cpp:137
spi_command_t spi_command_drv
Definition: rt_spi.cpp:27

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void spi_driver_run ( )

Definition at line 319 of file rt_spi.cpp.

References fake_spine_control(), publish_spi_torque(), spi_command_drv, spi_data_drv, spi_mutex, spi_send_receive(), and spi_torque.

319  {
320  // do spi board calculations
321  for (int i = 0; i < 4; i++) {
323  }
325 
326  // in here, the driver is good
327  pthread_mutex_lock(&spi_mutex);
329  pthread_mutex_unlock(&spi_mutex);
330 }
void spi_send_receive(spi_command_t *command, spi_data_t *data)
Definition: rt_spi.cpp:260
void publish_spi_torque(spi_torque_t *data)
Definition: rt_spi_lcm.cpp:16
pthread_mutex_t spi_mutex
Definition: rt_spi.cpp:31
void fake_spine_control(spi_command_t *cmd, spi_data_t *data, spi_torque_t *torque_out, int board_num)
Definition: rt_spi.cpp:62
spi_torque_t spi_torque
Definition: rt_spi.cpp:29
spi_data_t spi_data_drv
Definition: rt_spi.cpp:28
spi_command_t spi_command_drv
Definition: rt_spi.cpp:27

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void spi_send_receive ( spi_command_t *  command,
spi_data_t *  data 
)

Definition at line 260 of file rt_spi.cpp.

References K_WORDS_PER_MESSAGE, spi_1_fd, spi_2_fd, spi_bits_per_word, spi_driver_iterations, spi_to_spine(), and spine_to_spi().

260  {
261  // update driver status flag
263  data->spi_driver_status = spi_driver_iterations << 16;
264 
265  // transmit and receive buffers
266  uint16_t tx_buf[K_WORDS_PER_MESSAGE];
267  uint16_t rx_buf[K_WORDS_PER_MESSAGE];
268 
269  for (int spi_board = 0; spi_board < 2; spi_board++) {
270  // copy command into spine type:
271  spi_to_spine(command, &g_spine_cmd, spi_board * 2);
272 
273  // pointers to command/data spine array
274  uint16_t *cmd_d = (uint16_t *)&g_spine_cmd;
275  uint16_t *data_d = (uint16_t *)&g_spine_data;
276 
277  // zero rx buffer
278  memset(rx_buf, 0, K_WORDS_PER_MESSAGE * sizeof(uint16_t));
279 
280  // copy into tx buffer flipping bytes
281  for (int i = 0; i < K_WORDS_PER_MESSAGE; i++)
282  tx_buf[i] = (cmd_d[i] >> 8) + ((cmd_d[i] & 0xff) << 8);
283  // tx_buf[i] = __bswap_16(cmd_d[i]);
284 
285  // each word is two bytes long
286  size_t word_len = 2; // 16 bit word
287 
288  // spi message struct
289  struct spi_ioc_transfer spi_message[1];
290 
291  // zero message struct.
292  memset(spi_message, 0, 1 * sizeof(struct spi_ioc_transfer));
293 
294  // set up message struct
295  for (int i = 0; i < 1; i++) {
296  spi_message[i].bits_per_word = spi_bits_per_word;
297  spi_message[i].cs_change = 1;
298  spi_message[i].delay_usecs = 0;
299  spi_message[i].len = word_len * 66;
300  spi_message[i].rx_buf = (uint64_t)rx_buf;
301  spi_message[i].tx_buf = (uint64_t)tx_buf;
302  }
303 
304  // do spi communication
305  int rv = ioctl(spi_board == 0 ? spi_1_fd : spi_2_fd, SPI_IOC_MESSAGE(1),
306  &spi_message);
307  (void)rv;
308 
309  // flip bytes the other way
310  for (int i = 0; i < 30; i++)
311  data_d[i] = (rx_buf[i] >> 8) + ((rx_buf[i] & 0xff) << 8);
312  // data_d[i] = __bswap_16(rx_buf[i]);
313 
314  // copy back to data
315  spine_to_spi(data, &g_spine_data, spi_board * 2);
316  }
317 }
static spine_data_t g_spine_data
Definition: rt_spi.cpp:25
unsigned char spi_bits_per_word
Definition: rt_spi.cpp:15
int spi_2_fd
Definition: rt_spi.cpp:20
void spine_to_spi(spi_data_t *data, spine_data_t *spine_data, int leg_0)
Definition: rt_spi.cpp:235
#define K_WORDS_PER_MESSAGE
Definition: rt_spi.h:28
static spine_cmd_t g_spine_cmd
Definition: rt_spi.cpp:24
int spi_driver_iterations
Definition: rt_spi.cpp:186
void spi_to_spine(spi_command_t *cmd, spine_cmd_t *spine_cmd, int leg_0)
Definition: rt_spi.cpp:189
int spi_1_fd
Definition: rt_spi.cpp:19

+ Here is the call graph for this function:

+ Here is the caller graph for this function: