File: OctaveSPILibrary.cpp

package info (click to toggle)
octave-arduino 0.12.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,616 kB
  • sloc: cpp: 3,221; python: 438; makefile: 152; xml: 22; sh: 1
file content (230 lines) | stat: -rw-r--r-- 4,871 bytes parent folder | download | duplicates (2)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
 * Octave arduino spi interface
 * Copyright (C) 2018 John Donoghue <john.donoghue@ieee.org>
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/
#include "settings.h"
#include "OctaveSPILibrary.h"

#define ARDUINO_CONFIGSPI   1
#define ARDUINO_READ_WRITE_SPI 2

#ifdef USE_SPI
#include <SPI.h>

class SPIDevice
{
  #define USED 1
  #define ENABLED 2
public:
  uint8_t flags;
  uint8_t cspin;
  uint8_t bitorder;
  uint8_t mode;

  SPIDevice();
  uint8_t init(uint8_t id, uint8_t mode, uint8_t order);
  uint8_t free();
  void set_cs(uint8_t state);

  int transfer(uint8_t *data, int sz);
  SPISettings settings();
};

SPIDevice::SPIDevice ()
{
  flags = 0;
}

uint8_t
SPIDevice::init (uint8_t id, uint8_t spi_mode, uint8_t spi_bitorder)
{
  flags = USED|ENABLED;
  cspin = id;

  if (spi_mode == 0) mode = SPI_MODE0;
  else if (spi_mode == 1) mode = SPI_MODE1;
  else if (spi_mode == 2) mode = SPI_MODE2;
  else if (spi_mode == 3) mode = SPI_MODE3;
  else mode = SPI_MODE0;

  bitorder = spi_bitorder;

  return 0;
}

uint8_t
SPIDevice::free ()
{
  flags = 0;
  return 0;
}

void SPIDevice::set_cs(uint8_t state)
{
  digitalWrite (cspin, state); 
}

SPISettings SPIDevice::settings()
{
  return SPISettings(4000000, bitorder==0 ? MSBFIRST : LSBFIRST , mode);
}

int
SPIDevice::transfer (uint8_t *data, int sz)
{
  SPI.transfer (data, sz);
  return 0;
}

#define MAX_SPI_DEVICES 5
static SPIDevice spidevs[MAX_SPI_DEVICES];

SPIDevice *
getSPI (uint8_t id)
{
  uint8_t i;
  SPIDevice * unused = 0;

  for (i=0; i<MAX_SPI_DEVICES; i++) 
    {
      if (spidevs[i].flags & USED) 
        {
          if (spidevs[i].cspin == id)
            return &spidevs[i];
        }
      else if (!unused)
        {
          unused = &spidevs[i];
        }
    }
  return unused;
}

#endif

OctaveSPILibrary::OctaveSPILibrary (OctaveArduinoClass &oc) 
{

  libName = "SPI";

  oc.registerLibrary (this);
}

void
OctaveSPILibrary::commandHandler (uint8_t cmdID, uint8_t* data, uint8_t datasz)
{
  switch (cmdID)
    {
#ifdef USE_SPI        
      case ARDUINO_CONFIGSPI:
        {
          if (datasz == 4)
            {
              // spi id   0 (cs pin)
              // enable   1
              // mode ?   2
              // byte order 3
              // TODO: bit rate
              SPIDevice * dev = getSPI (data[0]);

	      if(dev == 0)
	        {
                  sendErrorMsg_P (ERRORMSG_INVALID_DEVICE);
		  return;
	        }

              if (data[1] == 1)
                {
                  //        cspin    mode     bitorder
		  dev->init(data[0], data[2], data[3]);

                  // TODO: first call only ?
                  SPI.begin ();

                  dev->set_cs(HIGH);
                }
              else
                {
                  // TODO: last call only
                  // SPI.end ();

		  dev->free();
                }
              sendResponseMsg (cmdID,data, 2);
            }
          else if(datasz == 1)
            {
              SPIDevice * dev = getSPI (data[0]);

	      if(dev == 0 || (dev->flags&USED)==0)
	        {
                  sendErrorMsg_P (ERRORMSG_INVALID_DEVICE);
		  return;
	        }

              // TODO: last call only
              // SPI.end ();

              dev->free();
            }
          else
            {
              sendInvalidNumArgsMsg ();
            }
          break;
        }
      case ARDUINO_READ_WRITE_SPI:
      
        if (datasz >= 2)
          {
            SPIDevice * dev = getSPI (data[0]);

            if(dev == 0 || (dev->flags&USED)==0)
              {
                sendErrorMsg_P (ERRORMSG_INVALID_DEVICE);
		return;
	      }

            // begin transaction
            SPI.beginTransaction (dev->settings());

            // set CS low
            dev->set_cs(LOW); 
            delay (1);

            // transfer the bytes
	    dev->transfer(&data[1], datasz-1);

            // set CS hi
            dev->set_cs(HIGH); 
            delay (1);
            // endtransaction
            SPI.endTransaction ();

            sendResponseMsg (cmdID, data, datasz);
          }
        else
          {
            sendInvalidNumArgsMsg ();
          }
        break;  
#endif

      default:
        sendUnknownCmdIDMsg ();
        break;
    }
}