summaryrefslogtreecommitdiff
path: root/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/spi.c
blob: cd4c55b0e91ba962ffd1e6c4108d9480b4099bc3 (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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
/**************************************************************************//**
  \file  spi.c

  \brief Implementation of USART SPI mode, hardware-independent module.

  \author
      Atmel Corporation: http://www.atmel.com \n
      Support email: avr@atmel.com

    Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
    Licensed under Atmel's Limited License Agreement (BitCloudTM).

  \internal
    History:
      29/05/08 A. Khromykh - Created
 ******************************************************************************/
/******************************************************************************
 *   WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK.  *
 *   EXPERT USERS SHOULD PROCEED WITH CAUTION.                                *
 ******************************************************************************/

/******************************************************************************
                   Includes section
******************************************************************************/
#include <spi.h>

/******************************************************************************
                   Global functions prototypes section
******************************************************************************/
void halSetUsartSpiConfig(HAL_SpiDescriptor_t *descriptor);
void halSetSlaveSpiConfig(HAL_SpiDescriptor_t *descriptor);

/******************************************************************************
                   External global variables section
******************************************************************************/
extern HAL_UsartDescriptor_t *halPointDescrip[NUM_USART_CHANNELS];
#if defined(ATMEGA128RFA1)
  extern HAL_SpiDescriptor_t *halRealSpiDescripPointer;
  extern volatile uint8_t rxSlaveBuffer[HAL_SPI_RX_BUFFER_LENGTH];
#endif

/******************************************************************************
                   Static functions prototypes section
******************************************************************************/
static bool isClosedPd(void *pointer);
static bool isOpenedPd(void *pointer);
static int halCheckUsartDescriptor(HAL_SpiDescriptor_t *descriptor, bool(* predicate)(void *));
static int halOpenUsartSpi(HAL_SpiDescriptor_t *descriptor);
static int halCloseUsartSpi(HAL_SpiDescriptor_t *descriptor);
static int halFillServiceInfo(HAL_SpiDescriptor_t *descriptor, uint8_t *buffer, uint16_t length, uint8_t transac);
static int halWriteUsartSpi(HAL_SpiDescriptor_t *descriptor, uint8_t *buffer, uint16_t length);
static int halReadUsartSpi(HAL_SpiDescriptor_t *descriptor, uint8_t *buffer, uint16_t length);
#if defined(ATMEGA128RFA1)
static int halCheckSpiDescriptor(HAL_SpiDescriptor_t *descriptor, bool(* predicate)(void *));
static int halOpenRealSpi(HAL_SpiDescriptor_t *descriptor);
static int halCloseRealSpi(HAL_SpiDescriptor_t *descriptor);
static int halWriteRealSpi(HAL_SpiDescriptor_t *descriptor, uint8_t *buffer, uint16_t length);
static int halReadRealSpi(HAL_SpiDescriptor_t *descriptor, uint8_t *buffer, uint16_t length);
#endif

/******************************************************************************
                   Implementations section
******************************************************************************/
/**************************************************************************//**
\brief Predicate for check of closed station.
\param[in]
    pointer - pointer to some descriptor.
\return
   true - interface is closed;
   false - interface is not closed.
******************************************************************************/
static bool isClosedPd(void *pointer)
{
  return pointer ? false : true;
}

/**************************************************************************//**
\brief Predicate for check of opened station.
\param[in]
    pointer - pointer to some descriptor.
\return
   true - interface is opened;
   false - interface is not opened.
******************************************************************************/
static bool isOpenedPd(void *pointer)
{
  return pointer ? true : false;
}

/**************************************************************************//**
\brief Check correctenss of the usart spi descriptor.
\param[in]
   descriptor - pointer to the usart spi descriptor.
\param[in]
   predicate - check station predicate.
\return
   interface index - interface is opened;
   -1 - interface is not opened.
******************************************************************************/
static int halCheckUsartDescriptor(HAL_SpiDescriptor_t *descriptor, bool(* predicate)(void *))
{
  int i;

  if (NULL == descriptor)
    return -1;

  if (false == halIsUsartChannelCorrect(descriptor->tty))
    return -1;

  i = HAL_GET_INDEX_BY_CHANNEL(descriptor->tty);
  if (false == predicate((void *)halPointDescrip[i]))
    return -1;

  return i;
}

#if defined(ATMEGA128RFA1)
/**************************************************************************//**
\brief Check correctenss of the real spi descriptor.
\param[in]
   descriptor - pointer to the real spi descriptor.
\param[in]
   predicate - check station predicate.
\return
    0 - interface is opened;
   -1 - interface is not opened.
******************************************************************************/
static int halCheckSpiDescriptor(HAL_SpiDescriptor_t *descriptor, bool(* predicate)(void *))
{
  if (NULL == descriptor)
    return -1;

  if (SPI_CHANNEL_2 != descriptor->tty)
    return -1;

  if (false == predicate((void *)halRealSpiDescripPointer))
    return -1;

  return 0;
}
#endif

/**************************************************************************//**
\brief Configure usart in mspi mode.
\param[in]
    descriptor - pointer to the usart spi descriptor.
\return
   -1 - there not are free resources.
    0 - SPI channel is ready.
******************************************************************************/
static int halOpenUsartSpi(HAL_SpiDescriptor_t *descriptor)
{
  int i; // Descriptor index

  i = halCheckUsartDescriptor(descriptor, isClosedPd);
  if (-1 == i)
    return -1;

  if (NULL != descriptor->callback)
  {
    descriptor->spiDescriptor.txCallback = descriptor->callback;
    descriptor->spiDescriptor.tty = descriptor->tty;
  }

  halPointDescrip[i] = &descriptor->spiDescriptor;
  halSetUsartSpiConfig(descriptor);
  return 0;
}

#if defined(ATMEGA128RFA1)
/**************************************************************************//**
\brief Configure spi.
\param[in]
    descriptor - pointer to the spi descriptor.
\return
   -1 - there are no free resources.
    0 - SPI channel is ready.
******************************************************************************/
static int halOpenRealSpi(HAL_SpiDescriptor_t *descriptor)
{
  if (-1 == halCheckSpiDescriptor(descriptor, isClosedPd))
    return -1;

  halRealSpiDescripPointer = descriptor;
  halRealSpiDescripPointer->spiDescriptor.service.txPointOfRead = 0;
  halRealSpiDescripPointer->spiDescriptor.service.txPointOfWrite = 0;
  halRealSpiDescripPointer->spiDescriptor.service.rxPointOfRead = 0;
  halRealSpiDescripPointer->spiDescriptor.service.rxPointOfWrite = 0;
  halSetSlaveSpiConfig(descriptor);
  return 0;
}
#endif

/**************************************************************************//**
\brief Open the SPI interface.
\param[in]
    descriptor - pointer to the spi descriptor.
\return
   -1 - there not are free resources.
    0 - SPI channel is ready.
******************************************************************************/
int HAL_OpenSpi(HAL_SpiDescriptor_t *descriptor)
{
#if defined(ATMEGA128RFA1)
  if (SPI_CHANNEL_2 == descriptor->tty)
    return halOpenRealSpi(descriptor);
  else
#endif
    return halOpenUsartSpi(descriptor);
}

/**************************************************************************//**
\brief Clear the usart channel and pins.
\param[in]
  descriptor - pointer to the spi descriptor.
\return
   0 - success
  -1 - channel was not opened.
******************************************************************************/
static int halCloseUsartSpi(HAL_SpiDescriptor_t *descriptor)
{
  int i;

  i = halCheckUsartDescriptor(descriptor, isOpenedPd);
  if (-1 == i)
    return -1;

  halPointDescrip[i] = NULL;
  halClearUsartSpi(descriptor->tty);
  return 0;
}

#if defined(ATMEGA128RFA1)
/**************************************************************************//**
\brief Clear the spi channel and pins.
\param[in]
  descriptor - pointer to the spi descriptor.
\return
  Returns 0 on success or -1 if channel was not opened.
******************************************************************************/
static int halCloseRealSpi(HAL_SpiDescriptor_t *descriptor)
{
  if (-1 == halCheckSpiDescriptor(descriptor, isClosedPd))
    return -1;

  halRealSpiDescripPointer = NULL;
  halClearRealSpi();
  return 0;
}
#endif

/**************************************************************************//**
\brief Close the SPI channel and pins.
\param[in]
  descriptor - pointer to the spi descriptor.
\return
  Returns 0 on success or -1 if channel was not opened.
******************************************************************************/
int HAL_CloseSpi(HAL_SpiDescriptor_t *descriptor)
{
#if defined(ATMEGA128RFA1)
  if (SPI_CHANNEL_2 == descriptor->tty)
    return halCloseRealSpi(descriptor);
  else
#endif
    return halCloseUsartSpi(descriptor);
}

/**************************************************************************//**
\brief Fill service structure for bus transaction.
\param[in]
   descriptor - pointer to the spi descriptor.
\param[in]
   buffer -pointer to data buffer.
\param[in]
   length - length of the data buffer.
\param[in]
   transac - bus transaction type.
\return
   -1 - interface is busy;
    0 - success.
******************************************************************************/
static int halFillServiceInfo(HAL_SpiDescriptor_t *descriptor, uint8_t *buffer, uint16_t length, uint8_t transac)
{
  HalUsartService_t *halBufferControl;

  halBufferControl = &descriptor->spiDescriptor.service;
  if (halBufferControl->txPointOfWrite != halBufferControl->txPointOfRead)
    return -1; // there is unsent data

  descriptor->spiDescriptor.txBuffer = buffer;
  descriptor->spiDescriptor.txBufferLength = 0;
  halBufferControl->txPointOfWrite = length;
  halBufferControl->txPointOfRead = 0;
  descriptor->spiDescriptor.rxBuffer = buffer;
  descriptor->spiDescriptor.flowControl = transac;
  return 0;
}

/**************************************************************************//**
\brief Writes a length bytes to the usart. \n
 Callback function will be used to notify about the finishing transmitting.
\param[in]
  descriptor -  pointer to spi descriptor
\param[in]
  buffer     -  pointer to application data buffer;
\param[in]
  length     -  number bytes for transfer;
\return
  -1 - spi module was not opened, there is unsent data, pointer to the data or
       the length are zero; \n
   0 - on success or a number; \n
   Number of written bytes if the synchronous method is used(callback is NULL).
******************************************************************************/
static int halWriteUsartSpi(HAL_SpiDescriptor_t *descriptor, uint8_t *buffer, uint16_t length)
{
  int i;

  if (!buffer || !length)
    return -1;

  i = halCheckUsartDescriptor(descriptor, isOpenedPd);
  if (-1 == i)
    return -1;

  if (&descriptor->spiDescriptor != halPointDescrip[i])
    return -1; // incorrect descriptor

  if (NULL != descriptor->callback)
  {
    if (-1 == halFillServiceInfo(descriptor, buffer, length, USART_SPI_WRITE_MODE))
      return -1;

    halEnableUsartSpiRxcInterrupt(descriptor->tty);
    halEnableUsartSpiDremInterrupt(descriptor->tty);
    return 0;
  }
  else
  {
    return halSyncUsartSpiWriteData(descriptor->tty, buffer, length);
  }
}

#if defined(ATMEGA128RFA1)
/**************************************************************************//**
\brief Writes a length bytes to the spi. \n
\param[in]
  descriptor -  pointer to spi descriptor
\param[in]
  buffer     -  pointer to application data buffer;
\param[in]
  length     -  number bytes for transfer;
\return
  -1 - spi module was not opened, there is unsent data, pointer to the data or
       the length are zero; \n
   0 - on success or a number; \n
******************************************************************************/
static int halWriteRealSpi(HAL_SpiDescriptor_t *descriptor, uint8_t *buffer, uint16_t length)
{
  if (!buffer || !length)
    return -1;

  if (-1 == halCheckSpiDescriptor(descriptor, isOpenedPd))
    return -1;

  if (descriptor != halRealSpiDescripPointer)
    return -1; // incorrect descriptor

  if (-1 == halFillServiceInfo(descriptor, buffer, length, USART_SPI_WRITE_MODE))
    return -1;

  halSendSpiByte(*buffer);
  return 0;
}
#endif

#if defined(ATMEGA128RFA1)
/**************************************************************************//**
\brief Put next byte to the spi.
******************************************************************************/
void halSpiTxByteComplete(void)
{
  HalUsartService_t *halBufferControl;

  halBufferControl = &halRealSpiDescripPointer->spiDescriptor.service;

  if (halBufferControl->txPointOfWrite != halBufferControl->txPointOfRead)
    halSendSpiByte(halRealSpiDescripPointer->spiDescriptor.txBuffer[++halBufferControl->txPointOfRead]);
}
#endif

/**************************************************************************//**
\brief Writes a length bytes to the SPI. \n
 Callback function will be used to notify about the finishing transmitting.
 (only for master spi)
\param[in]
  descriptor -  pointer to spi descriptor
\param[in]
  buffer     -  pointer to application data buffer;
\param[in]
  length     -  number bytes for transfer;
\return
  -1 - spi module was not opened, there is unsent data, pointer to the data or
       the length are zero; \n
   0 - on success or a number; \n
   Number of written bytes if the synchronous method is used(callback is NULL), \n
   only for master spi.
******************************************************************************/
int HAL_WriteSpi(HAL_SpiDescriptor_t *descriptor, uint8_t *buffer, uint16_t length)
{
#if defined(ATMEGA128RFA1)
  if (SPI_CHANNEL_2 == descriptor->tty)
    return halWriteRealSpi(descriptor, buffer, length);
  else
#endif
    return halWriteUsartSpi(descriptor, buffer, length);
}

/**************************************************************************//**
\brief Reads a number of bytes from the usart.\n
 Callback function will be used to notify when the activity is finished.\n
 The read data is placed to the buffer.
\param[in]
  descriptor - pointer to HAL_SpiDescriptor_t structure
\param[in]
  buffer -  pointer to the application data buffer
\param[in]
  length -  number of bytes to transfer
\return
  -1 - spi module was not opened, or there is unsent data, or the pointer to
   data or the length are NULL; \n
   0 - success; \n
   Number of written bytes if the synchronous method is used(callback is NULL).
******************************************************************************/
static int halReadUsartSpi(HAL_SpiDescriptor_t *descriptor, uint8_t *buffer, uint16_t length)
{
  HAL_UsartDescriptor_t *spiDescrip;
  int i;

  if (!buffer || !length)
    return -1;

  i = halCheckUsartDescriptor(descriptor, isOpenedPd);
  if (-1 == i)
    return -1;

  spiDescrip = &descriptor->spiDescriptor;
  if (spiDescrip != halPointDescrip[i])
    return -1; // incorrect descriptor

  if (NULL != descriptor->callback)
  {
    if (-1 == halFillServiceInfo(descriptor, buffer, length, USART_SPI_READ_MODE))
      return -1;

    halEnableUsartSpiRxcInterrupt(descriptor->tty);
    halEnableUsartSpiDremInterrupt(descriptor->tty);
    return 0;
  }
  else
  {
    return halSyncUsartSpiReadData(descriptor->tty, buffer, length);
  }
}

#if defined(ATMEGA128RFA1)
/**************************************************************************//**
\brief Reads a number of bytes from spi internal buffer and places them to the buffer.
\param[in]
  descriptor - pointer to HAL_SpiDescriptor_t structure
\param[in]
  buffer -  pointer to the application data buffer
\param[in]
  length -  number of bytes to transfer
\return
  -1 - spi module was not opened, or there is unsent data, or the pointer to
   data or the length are NULL; \n
   Number of read bytes from spi internal buffer.
******************************************************************************/
static int halReadRealSpi(HAL_SpiDescriptor_t *descriptor, uint8_t *buffer, uint16_t length)
{
  uint16_t           wasRead = 0;
  uint16_t           poW;
  uint16_t           poR;
  HalUsartService_t *halBufferControl;

  if (!buffer || !length)
    return -1;

  if (-1 == halCheckSpiDescriptor(descriptor, isOpenedPd))
    return -1;

  if (descriptor != halRealSpiDescripPointer)
    return -1; // incorrect descriptor

  halBufferControl = &halRealSpiDescripPointer->spiDescriptor.service;
  ATOMIC_SECTION_ENTER
    poW = halBufferControl->rxPointOfWrite;
    poR = halBufferControl->rxPointOfRead;
  ATOMIC_SECTION_LEAVE

  while ((poR != poW) && (wasRead < length))
  {
    buffer[wasRead] = rxSlaveBuffer[poR];
    if (HAL_SPI_RX_BUFFER_LENGTH == ++poR)
      poR = 0;
    wasRead++;
  }

  ATOMIC_SECTION_ENTER
    halBufferControl->rxPointOfRead = poR;
    halBufferControl->rxBytesInBuffer -= wasRead;
  ATOMIC_SECTION_LEAVE

  return wasRead;
}
#endif

/**************************************************************************//**
\brief For master : writes a number of bytes to the spi.\n
  Callback function will be used to notify when the activity is finished.\n
  The read data is placed to the buffer. \n
  For slave: reads a number of bytes from internal spi buffer and writes them \n
  to application buffer.
\param[in]
  descriptor - pointer to HAL_SpiDescriptor_t structure
\param[in]
  buffer -  pointer to the application data buffer
\param[in]
  length -  number of bytes to transfer
\return
  -1 - spi module was not opened, or there is unsent data, or the pointer to
   data or the length are NULL; \n
   0 - success for master; \n
   Number of written bytes if the synchronous method is used(callback is NULL) for master \n
   or number of read bytes from internal buffer to the application buffer for slave.
******************************************************************************/
int HAL_ReadSpi(HAL_SpiDescriptor_t *descriptor, uint8_t *buffer, uint16_t length)
{
#if defined(ATMEGA128RFA1)
  if (SPI_CHANNEL_2 == descriptor->tty)
    return halReadRealSpi(descriptor, buffer, length);
  else
#endif
    return halReadUsartSpi(descriptor, buffer, length);
}

#if defined(ATMEGA128RFA1)
/**************************************************************************//**
\brief Puts the received byte to the cyclic buffer.

\param[in]
  data - data to put.
******************************************************************************/
void halSpiRxBufferFiller(uint8_t data)
{
  uint16_t           old;
  HalUsartService_t *halBufferControl;

  if (NULL == halRealSpiDescripPointer)
  {// abnormal
    halClearRealSpi();
    return;
  }

  halBufferControl = &halRealSpiDescripPointer->spiDescriptor.service;
  old = halBufferControl->rxPointOfWrite;

  if (HAL_SPI_RX_BUFFER_LENGTH == ++halBufferControl->rxPointOfWrite)
    halBufferControl->rxPointOfWrite = 0;

  if (halBufferControl->rxPointOfWrite == halBufferControl->rxPointOfRead)
  { // Buffer full.
    halBufferControl->rxPointOfWrite = old;
    return;
  } // Buffer full.

  rxSlaveBuffer[old] = data;
  halBufferControl->rxBytesInBuffer++;
}
#endif

#if defined(ATMEGA128RFA1)
/**************************************************************************//**
\brief Slave spi reception complete interrupt handler.
******************************************************************************/
void halSpiRxByteComplete(void)
{
  uint16_t number;

  ATOMIC_SECTION_ENTER
    number = halRealSpiDescripPointer->spiDescriptor.service.rxBytesInBuffer;
  ATOMIC_SECTION_LEAVE

  if (number)
    if (NULL != halRealSpiDescripPointer->slave_callback)
      halRealSpiDescripPointer->slave_callback(number);
}
#endif

// eof spi.c