hcl_spi_rpi.c
Go to the documentation of this file.
1 //==============================================================================
2 //
3 // hcl_spi_rpi.c - Seiko Epson Hardware Control Library
4 //
5 // This layer of indirection is for the Raspberry Pi specific implementation
6 // of the SPI protocol. It uses the wiringPi library for the low-level SPI
7 // transfers.
8 //
9 //
10 // THE SOFTWARE IS RELEASED INTO THE PUBLIC DOMAIN.
11 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 // NONINFRINGEMENT, SECURITY, SATISFACTORY QUALITY, AND FITNESS FOR A
14 // PARTICULAR PURPOSE. IN NO EVENT SHALL EPSON BE LIABLE FOR ANY LOSS, DAMAGE
15 // OR CLAIM, ARISING FROM OR IN CONNECTION WITH THE SOFTWARE OR THE USE OF THE
16 // SOFTWARE.
17 //
18 //==============================================================================
19 
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <wiringPiSPI.h>
24 
25 #include "hcl.h"
26 #include "hcl_spi.h"
27 #include "sensor_epsonCommon.h"
28 
29 static int mySpiFd;
30 int deviceOk(void);
31 
32 /*****************************************************************************
33 ** Function name: spiInit
34 ** Description: Initialize the RPI SPI library.
35 **
36 ** NOTE: This function assumes that the seInit function has been called first
37 ** and initialized the wiringPi library. Then the gpioInit called and setup all
38 ** the necessary pins. CS should be HIGH in case of any glitches on the SPI bus
39 ** during its setup. Attempts to perform low-level check of device ID register
40 ** for expected response for Epson device.
41 **
42 ** Parameters: SPI Mode, SPI clock speed
43 ** Return value: OK, or NG
44 *****************************************************************************/
45 int spiInit(uint8_t mode, uint32_t khzspeed) {
46  if (mode != SPI_MODE3) {
47  return NG;
48  }
49 
50  if ((mySpiFd = wiringPiSPISetupMode(SPI_CHAN, khzspeed, mode)) < 0) {
51  return NG;
52  }
53  if (!deviceOk()) {
54  printf("Invalid device response\n");
55  return NG;
56  }
57  return OK;
58 }
59 
60 /*****************************************************************************
61 ** Function name: spiRelease
62 ** Description: Release the SPI Interface.
63 **
64 ** Parameters: None
65 ** Return value: OK
66 *****************************************************************************/
67 int spiRelease(void) {
68  close(mySpiFd);
69  return OK;
70 }
71 
72 /*****************************************************************************
73 ** Function name: spiTransfer
74 ** Description: Initiates an 8-bit SPI transfer
75 ** Transfers out 8-bit on MOSI
76 ** Transfers in 8-bit on MISO
77 ** Parameters: 8-bit Data to Send to SPI Slave
78 ** Return value: 8-bit Data Received from SPI Slave
79 *****************************************************************************/
80 uint8_t spiTransfer(uint8_t value) {
81  wiringPiSPIDataRW(SPI_CHAN, &value, 1);
82  return value;
83 }
84 
85 /*****************************************************************************
86 ** Function name: deviceOk
87 ** Description: (Optional) Checks for Epson device.
88 ** In case the SPI interface to device
89 ** was left in inconsistent state.
90 ** Parameters: None
91 ** Return value: OK or NG
92 *****************************************************************************/
93 int deviceOk(void) {
94  int retry_count = 5;
95  const unsigned short ID_VAL_ = 0x5345;
96  unsigned short rxData[] = {0x00, 0x00};
97 
98  while (retry_count > 0) {
99  // Set WIN_ID = 0
100  selEpson();
101  spiTransfer(ADDR_WIN_CTRL | 0x80);
103  deselEpson();
104  epsonStall();
105 
106  // Read ID Register
107  selEpson();
109  spiTransfer(0x00);
110  epsonStall();
111  ;
112 
113  // Response
114  rxData[0] = spiTransfer(0x00);
115  rxData[1] = spiTransfer(0x00);
116  deselEpson();
117  epsonStall();
118  if ((rxData[0] << 8 | rxData[1]) == ID_VAL_) {
119  return OK;
120  }
121  retry_count--;
122  }
123  return NG;
124 }
sensor_epsonCommon.h
ADDR_WIN_CTRL
#define ADDR_WIN_CTRL
Definition: sensor_epsonCommon.h:220
deviceOk
int deviceOk(void)
Definition: hcl_spi_rpi.c:93
hcl.h
deselEpson
#define deselEpson()
Definition: sensor_epsonSpi.h:40
selEpson
#define selEpson()
Definition: sensor_epsonSpi.h:39
epsonStall
#define epsonStall()
Definition: sensor_epsonSpi.h:32
WIN_ID0
#define WIN_ID0
Definition: sensor_epsonCommon.h:222
ADDR_ID
#define ADDR_ID
Definition: sensor_epsonCommon.h:110
hcl_spi.h
spiInit
int spiInit(uint8_t mode, uint32_t khzspeed)
Definition: hcl_spi_rpi.c:45
SPI_MODE3
@ SPI_MODE3
Definition: hcl_spi.h:26
spiTransfer
uint8_t spiTransfer(uint8_t value)
Definition: hcl_spi_rpi.c:80
mySpiFd
static int mySpiFd
Definition: hcl_spi_rpi.c:29
NG
#define NG
Definition: hcl.h:30
SPI_CHAN
#define SPI_CHAN
Definition: hcl_spi.h:29
spiRelease
int spiRelease(void)
Definition: hcl_spi_rpi.c:67
OK
#define OK
Definition: hcl.h:26


ess_imu_driver
Author(s):
autogenerated on Wed Dec 11 2024 03:06:30