summaryrefslogtreecommitdiff
path: root/libloragw/src/loragw_spi.native.c
blob: 6a9b64d207986c5a7ff54f4068a05b5ad8c370f5 (plain)
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
 / _____)             _              | |
( (____  _____ ____ _| |_ _____  ____| |__
 \____ \| ___ |    (_   _) ___ |/ ___)  _ \
 _____) ) ____| | | || |_| ____( (___| | | |
(______/|_____)_|_|_| \__)_____)\____)_| |_|
  (C)2013 Semtech-Cycleo

Description:
	Host specific functions to address the LoRa concentrator registers through
	a SPI interface.
	Single-byte read/write and burst read/write.
	Does not handle pagination.
	Could be used with multiple SPI ports in parallel (explicit file descriptor)

License: Revised BSD License, see LICENSE.TXT file include in the project
Maintainer: Sylvain Miermont
*/


/* -------------------------------------------------------------------------- */
/* --- DEPENDANCIES --------------------------------------------------------- */

#include <stdint.h>		/* C99 types */
#include <stdio.h>		/* printf fprintf */
#include <stdlib.h>		/* malloc free */
#include <unistd.h>		/* lseek, close */
#include <fcntl.h>		/* open */
#include <string.h>		/* memset */

#include <sys/ioctl.h>
#include <linux/spi/spidev.h>

#include "loragw_spi.h"

/* -------------------------------------------------------------------------- */
/* --- PRIVATE MACROS ------------------------------------------------------- */

#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
#if DEBUG_SPI == 1
	#define DEBUG_MSG(str)				fprintf(stderr, str)
	#define DEBUG_PRINTF(fmt, args...)	fprintf(stderr,"%s:%d: "fmt, __FUNCTION__, __LINE__, args)
	#define CHECK_NULL(a)				if(a==NULL){fprintf(stderr,"%s:%d: ERROR: NULL POINTER AS ARGUMENT\n", __FUNCTION__, __LINE__);return LGW_SPI_ERROR;}
#else
	#define DEBUG_MSG(str)
	#define DEBUG_PRINTF(fmt, args...)
	#define CHECK_NULL(a)				if(a==NULL){return LGW_SPI_ERROR;}
#endif

/* -------------------------------------------------------------------------- */
/* --- PRIVATE CONSTANTS ---------------------------------------------------- */

#define READ_ACCESS		0x00
#define WRITE_ACCESS	0x80
#define SPI_SPEED		8000000
#define SPI_DEV_PATH	"/dev/spidev0.0"

/* -------------------------------------------------------------------------- */
/* --- PUBLIC FUNCTIONS DEFINITION ------------------------------------------ */

/* SPI initialization and configuration */
int lgw_spi_open(void **spi_target_ptr) {
	int *spi_device = NULL;
	int dev;
	int a=0, b=0;
	int i;
	
	/* check input variables */
	CHECK_NULL(spi_target_ptr); /* cannot be null, must point on a void pointer (*spi_target_ptr can be null) */
	
	/* allocate memory for the device descriptor */
	spi_device = malloc(sizeof(int));
	if (spi_device == NULL) {
		DEBUG_MSG("ERROR: MALLOC FAIL\n");
		return LGW_SPI_ERROR;
	}
	
	/* open SPI device */
	dev = open(SPI_DEV_PATH, O_RDWR);
	if (dev < 0) {
		DEBUG_MSG("SPI port fail to open\n");
		return LGW_SPI_ERROR;
	}
	
	/* setting SPI mode to 'mode 0' */
	i = SPI_MODE_0;
	a = ioctl(dev, SPI_IOC_WR_MODE, &i);
	b = ioctl(dev, SPI_IOC_RD_MODE, &i);
	if ((a < 0) || (b < 0)) {
		DEBUG_MSG("ERROR: SPI PORT FAIL TO SET IN MODE 0\n");
		close(dev);
		free(spi_device);
		return LGW_SPI_ERROR;
	}
	
	/* setting SPI max clk (in Hz) */
	i = SPI_SPEED;
	a = ioctl(dev, SPI_IOC_WR_MAX_SPEED_HZ, &i);
	b = ioctl(dev, SPI_IOC_RD_MAX_SPEED_HZ, &i);
	if ((a < 0) || (b < 0)) {
		DEBUG_MSG("ERROR: SPI PORT FAIL TO SET MAX SPEED\n");
		close(dev);
		free(spi_device);
		return LGW_SPI_ERROR;
	}
	
	/* setting SPI to MSB first */
	i = 0;
	a = ioctl(dev, SPI_IOC_WR_LSB_FIRST, &i);
	b = ioctl(dev, SPI_IOC_RD_LSB_FIRST, &i);
	if ((a < 0) || (b < 0)) {
		DEBUG_MSG("ERROR: SPI PORT FAIL TO SET MSB FIRST\n");
		close(dev);
		free(spi_device);
		return LGW_SPI_ERROR;
	}
	
	/* setting SPI to 8 bits per word */
	i = 0; 
	a = ioctl(dev, SPI_IOC_WR_BITS_PER_WORD, &i);
	b = ioctl(dev, SPI_IOC_RD_BITS_PER_WORD, &i);
	if ((a < 0) || (b < 0)) {
		DEBUG_MSG("ERROR: SPI PORT FAIL TO SET 8 BITS-PER-WORD\n");
		close(dev);
		return LGW_SPI_ERROR;
	}
	
	*spi_device = dev;
	*spi_target_ptr = (void *)spi_device;
	DEBUG_MSG("Note: SPI port opened and configured ok\n");	
	return LGW_SPI_SUCCESS;
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

/* SPI release */
int lgw_spi_close(void *spi_target) {
	int spi_device;
	int a;
	
	/* check input variables */
	CHECK_NULL(spi_target);
	
	/* close file & deallocate file descriptor */
	spi_device = *(int *)spi_target; /* must check that spi_target is not null beforehand */
	a = close(spi_device);
	free(spi_target);
	
	/* determine return code */
	if (a < 0) {
		DEBUG_MSG("ERROR: SPI PORT FAILED TO CLOSE\n");
		return LGW_SPI_ERROR;
	} else {
		DEBUG_MSG("Note: SPI port closed\n");
		return LGW_SPI_SUCCESS;
	}
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

/* Simple write */
int lgw_spi_w(void *spi_target, uint8_t address, uint8_t data) {
	int spi_device;
	uint8_t out_buf[2];
	struct spi_ioc_transfer k;
	int a;
	
	/* check input variables */
	CHECK_NULL(spi_target);
	if ((address & 0x80) != 0) {
		DEBUG_MSG("WARNING: SPI address > 127\n");
	}
	
	spi_device = *(int *)spi_target; /* must check that spi_target is not null beforehand */
	
	/* prepare frame to be sent */
	out_buf[0] = WRITE_ACCESS | (address & 0x7F);
	out_buf[1] = data;
	
	/* I/O transaction */
	memset(&k, 0, sizeof(k)); /* clear k */
	k.tx_buf = (unsigned long) out_buf;
	k.len = ARRAY_SIZE(out_buf);
	k.speed_hz = SPI_SPEED;
	k.cs_change = 1;
	k.bits_per_word = 8;
	a = ioctl(spi_device, SPI_IOC_MESSAGE(1), &k);
	
	/* determine return code */
	if (a != 2) {
		DEBUG_MSG("ERROR: SPI WRITE FAILURE\n");
		return LGW_SPI_ERROR;
	} else {
		DEBUG_MSG("Note: SPI write success\n");
		return LGW_SPI_SUCCESS;
	}
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

/* Simple read */
int lgw_spi_r(void *spi_target, uint8_t address, uint8_t *data) {
	int spi_device;
	uint8_t out_buf[2];
	uint8_t in_buf[ARRAY_SIZE(out_buf)];
	struct spi_ioc_transfer k;
	int a;
	
	/* check input variables */
	CHECK_NULL(spi_target);
	if ((address & 0x80) != 0) {
		DEBUG_MSG("WARNING: SPI address > 127\n");
	}
	CHECK_NULL(data);
	
	spi_device = *(int *)spi_target; /* must check that spi_target is not null beforehand */
	
	/* prepare frame to be sent */
	out_buf[0] = READ_ACCESS | (address & 0x7F);
	out_buf[1] = 0x00;
	
	/* I/O transaction */
	memset(&k, 0, sizeof(k)); /* clear k */
	k.tx_buf = (unsigned long) out_buf;
	k.rx_buf = (unsigned long) in_buf;
	k.len = ARRAY_SIZE(out_buf);
	k.cs_change = 1;
	a = ioctl(spi_device, SPI_IOC_MESSAGE(1), &k);
	
	/* determine return code */
	if (a != 2) {
		DEBUG_MSG("ERROR: SPI READ FAILURE\n");
		return LGW_SPI_ERROR;
	} else {
		DEBUG_MSG("Note: SPI read success\n");
		*data = in_buf[1];
		return LGW_SPI_SUCCESS;
	}
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

/* Burst (multiple-byte) write */
int lgw_spi_wb(void *spi_target, uint8_t address, uint8_t *data, uint16_t size) {
	int spi_device;
	uint8_t command;
	struct spi_ioc_transfer k[2];
	int size_to_do, chunk_size, offset;
	int byte_transfered = 0;
	int i;
	
	/* check input parameters */
	CHECK_NULL(spi_target);
	if ((address & 0x80) != 0) {
		DEBUG_MSG("WARNING: SPI address > 127\n");
	}
	CHECK_NULL(data);
	if (size == 0) {
		DEBUG_MSG("ERROR: BURST OF NULL LENGTH\n");
		return LGW_SPI_ERROR;
	}
	
	spi_device = *(int *)spi_target; /* must check that spi_target is not null beforehand */
	
	/* prepare command byte */
	command = WRITE_ACCESS | (address & 0x7F);
	size_to_do = size;
	
	/* I/O transaction */
	memset(&k, 0, sizeof(k)); /* clear k */
	k[0].tx_buf = (unsigned long) &command;
	k[0].len = 1;
	k[0].cs_change = 0;
	k[1].cs_change = 1;
	for (i=0; size_to_do > 0; ++i) {
		chunk_size = (size_to_do < LGW_BURST_CHUNK) ? size_to_do : LGW_BURST_CHUNK;
		offset = i * LGW_BURST_CHUNK;
		k[1].tx_buf = (unsigned long)(data + offset);
		k[1].len = chunk_size;
		byte_transfered += (ioctl(spi_device, SPI_IOC_MESSAGE(2), &k) - 1 );
		DEBUG_PRINTF("BURST WRITE: to trans %d # chunk %d # transferred %d \n", size_to_do, chunk_size, byte_transfered);
		size_to_do -= chunk_size; /* subtract the quantity of data already transferred */
	}
	
	/* determine return code */
	if (byte_transfered != size) {
		DEBUG_MSG("ERROR: SPI BURST WRITE FAILURE\n");
		return LGW_SPI_ERROR;
	} else {
		DEBUG_MSG("Note: SPI burst write success\n");
		return LGW_SPI_SUCCESS;
	}
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

/* Burst (multiple-byte) read */
int lgw_spi_rb(void *spi_target, uint8_t address, uint8_t *data, uint16_t size) {
	int spi_device;
	uint8_t command;
	struct spi_ioc_transfer k[2];
	int size_to_do, chunk_size, offset;
	int byte_transfered = 0;
	int i;
	
	/* check input parameters */
	CHECK_NULL(spi_target);
	if ((address & 0x80) != 0) {
		DEBUG_MSG("WARNING: SPI address > 127\n");
	}
	CHECK_NULL(data);
	if (size == 0) {
		DEBUG_MSG("ERROR: BURST OF NULL LENGTH\n");
		return LGW_SPI_ERROR;
	}
	
	spi_device = *(int *)spi_target; /* must check that spi_target is not null beforehand */
	
	/* prepare command byte */
	command = READ_ACCESS | (address & 0x7F);
	size_to_do = size;
	
	/* I/O transaction */
	memset(&k, 0, sizeof(k)); /* clear k */
	k[0].tx_buf = (unsigned long) &command;
	k[0].len = 1;
	k[0].cs_change = 0;
	k[1].cs_change = 1;
	for (i=0; size_to_do > 0; ++i) {
		chunk_size = (size_to_do < LGW_BURST_CHUNK) ? size_to_do : LGW_BURST_CHUNK;
		offset = i * LGW_BURST_CHUNK;
		k[1].rx_buf = (unsigned long)(data + offset);
		k[1].len = chunk_size;
		byte_transfered += (ioctl(spi_device, SPI_IOC_MESSAGE(2), &k) - 1 );
		DEBUG_PRINTF("BURST READ: to trans %d # chunk %d # transferred %d \n", size_to_do, chunk_size, byte_transfered);
		size_to_do -= chunk_size;  /* subtract the quantity of data already transferred */
	}
	
	/* determine return code */
	if (byte_transfered != size) {
		DEBUG_MSG("ERROR: SPI BURST READ FAILURE\n");
		return LGW_SPI_ERROR;
	} else {
		DEBUG_MSG("Note: SPI burst read success\n");
		return LGW_SPI_SUCCESS;
	}
}

/* --- EOF ------------------------------------------------------------------ */