summaryrefslogtreecommitdiff
path: root/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halSpi.c
blob: b0c8bf0a610d315d12886bdbc43d8638fd6c454a (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
/**************************************************************************//**
\file  halSpi.c

\brief Implementation of USART SPI mode.

\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/06/07 E. Ivanov - Created
******************************************************************************/
/******************************************************************************
 *   WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK.  *
 *   EXPERT USERS SHOULD PROCEED WITH CAUTION.                                *
 ******************************************************************************/
/******************************************************************************
                   Includes section
******************************************************************************/
#include <spi.h>

/******************************************************************************
                   Define(s) section
******************************************************************************/
#define UDORD0                  2
#define UCPHA0                  1
#define UCPOL0                  0
#define SPI_CLOCK_MODE_AMOUNT   4
#define SPI_DATA_ORDER_AMOUNT   2

/******************************************************************************
                   Implementations section
******************************************************************************/
/******************************************************************************
Set the parameters of USART to work at SPI mode.
Parameters:
  descriptor - pointer to the spi descriptor.
Returns:
  none.
******************************************************************************/
void halSetUsartSpiConfig(HAL_SpiDescriptor_t *descriptor)
{
  uint8_t clockMode[SPI_CLOCK_MODE_AMOUNT] = {((0 << UCPOL0) | (0 << UCPHA0)),
                                              ((0 << UCPOL0) | (1 << UCPHA0)),
                                              ((1 << UCPOL0) | (0 << UCPHA0)),
                                              ((1 << UCPOL0) | (1 << UCPHA0))};
  uint8_t dataOrder[SPI_DATA_ORDER_AMOUNT] = {(0 << UDORD0),
                                              (1 << UDORD0)};

  // setting of the spi gpio direct
  if (SPI_CHANNEL_0 == descriptor->tty)
    GPIO_USART0_EXTCLK_make_out();
  else
    GPIO_USART1_EXTCLK_make_out();

  UBRRn(descriptor->tty) = 0;
  // Set MSPI mode
  UCSRnC(descriptor->tty) = (1 << UMSEL01) | (1 << UMSEL00);
  // Set clock mode and data order
  UCSRnC(descriptor->tty) |= (dataOrder[descriptor->dataOrder] | clockMode[descriptor->clockMode]);
  // Enable receiver and transmitter
  UCSRnB(descriptor->tty) = (1 << RXEN0) | (1 << TXEN0);
  // Set baud rate
  UBRRn(descriptor->tty) = descriptor->baudRate;
}

/******************************************************************************
Disables USART channel.
Parameters:
  tty  -  spi channel.
******************************************************************************/
void halClearUsartSpi(SpiChannel_t tty)
{
  if (SPI_CHANNEL_0 == tty)
    GPIO_USART0_EXTCLK_make_in();
  else
    GPIO_USART1_EXTCLK_make_in();

  UCSRnB(tty) = 0x00; // disable
}

/******************************************************************************
Write a length bytes to the SPI.
Parameters:
  tty    -  spi channel
  buffer -  pointer to application data buffer;
  length -  number bytes for transfer;
Returns:
  number of written bytes
******************************************************************************/
uint16_t halSyncUsartSpiWriteData(SpiChannel_t tty, uint8_t *buffer, uint16_t length)
{
  uint16_t i;
  uint8_t temp;

  for (i = 0; i < length; i++)
  {
    // Wait for empty transmit buffer
    while (!(UCSRnA(tty) & (1 << UDRE0)));
    // Send data
    UDRn(tty) = *(buffer + i);
    // Wait for data to be received
    while (!(UCSRnA(tty) & (1 << RXC0)));
    // receives data to clear received usart buffer
    temp = UDRn(tty);
    (void)temp;
  }
  return i;
}

/******************************************************************************
Write & read a length bytes to & from the SPI.
Parameters:
  tty    -  spi channel
  buffer -  pointer to application data buffer;
  length -  number bytes for transfer;
Returns:
  number of written & read bytes
******************************************************************************/
uint16_t halSyncUsartSpiReadData(SpiChannel_t tty, uint8_t *buffer, uint16_t length)
{
  uint16_t i;

  for (i = 0; i < length; i++)
  {
    // Wait for empty transmit buffer
    while (!(UCSRnA(tty) & (1 << UDRE0)));
    // Send data
    UDRn(tty) = *(buffer + i);
    // Wait for data to be received
    while (!(UCSRnA(tty) & (1 << RXC0)));
    // Receive data
    *(buffer + i) = UDRn(tty);
  }
  return i;
}

//end of halSpi.c