summaryrefslogtreecommitdiffstats
path: root/bsps/arm/beagle/include/bsp/spi.h
blob: ffda7edf6015de42597a652610b54d618ab7dea1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
 * @file
 *
 * @ingroup arm_beagle
 *
 * @brief SPI support API.
 *
 * Based on bsps/m68k/gen68360/spi/m360_spi.h
 */

/*
 * Copyright (c) 2018 Pierre-Louis Garnier <garnie_a@epita.fr>
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rtems.org/license/LICENSE.
 */

#ifndef LIBBSP_ARM_BEAGLE_SPI_H
#define LIBBSP_ARM_BEAGLE_SPI_H

#include <bsp.h>
#include <rtems/libi2c.h>
#include <rtems/irq.h>

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

#define BBB_SPI_TIMEOUT 1000

#define BBB_SPI_0_BUS_PATH "/dev/spi-0"

#define BBB_SPI_0_IRQ AM335X_INT_SPI0INT

typedef enum {
  SPI0,
  SPI1,
  SPI_COUNT
} bbb_spi_id_t;



typedef struct BEAGLE_SPI_BufferDescriptor_ {
    unsigned short      status;
    unsigned short      length;
    volatile void       *buffer;
} BEAGLE_SPI_BufferDescriptor_t;

typedef struct beagle_spi_softc {
  int                     initialized;
  rtems_id                task_id;
  uintptr_t               regs_base;
  rtems_vector_number     irq;
} beagle_spi_softc_t;

typedef struct {
  rtems_libi2c_bus_t  bus_desc;
  beagle_spi_softc_t softc;
} beagle_spi_desc_t;

/*
 * Initialize the driver
 *
 * Returns: o = ok or error code
 */
rtems_status_code beagle_spi_init
(
 rtems_libi2c_bus_t *bh                  /* bus specifier structure        */
);

/*
 * Receive some bytes from SPI device
 *
 * Returns: number of bytes received or (negative) error code
 */
int beagle_spi_read_bytes
(
 rtems_libi2c_bus_t *bh,                 /* bus specifier structure        */
 unsigned char *buf,                     /* buffer to store bytes          */
 int len                                 /* number of bytes to receive     */
);

/*
 * Send some bytes to SPI device
 *
 * Returns: number of bytes sent or (negative) error code
 */
int beagle_spi_write_bytes
(
 rtems_libi2c_bus_t *bh,                 /* bus specifier structure        */
 unsigned char *buf,                     /* buffer to send                 */
 int len                                 /* number of bytes to send        */
);

/*
 * Set SPI to desired baudrate/clock mode/character mode
 *
 * Returns: rtems_status_code
 */
rtems_status_code beagle_spi_set_tfr_mode
(
 rtems_libi2c_bus_t *bh,                 /* bus specifier structure        */
 const rtems_libi2c_tfr_mode_t *tfr_mode /* transfer mode info             */
);

/*
 * Perform selected ioctl function for SPI
 *
 * Returns: rtems_status_code
 */
int beagle_spi_ioctl
(
 rtems_libi2c_bus_t *bh,                 /* bus specifier structure        */
 int                 cmd,                /* ioctl command code             */
 void               *arg                 /* additional argument array      */
);

/*
 * Register SPI bus and devices
 *
 * Returns: Bus number or error code
 */
rtems_status_code bsp_register_spi
(
  const char         *bus_path,
  uintptr_t           register_base,
  rtems_vector_number irq
);

static inline rtems_status_code bbb_register_spi_0(void)
{
  return bsp_register_spi(
    BBB_SPI_0_BUS_PATH,
    AM335X_SPI0_BASE,
    BBB_SPI_0_IRQ
  );
}

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* LIBBSP_ARM_BEAGLE_SPI_H */