Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/ad-dyno1-ebz/src/Communication.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file Communication.c
3 * @brief Implementation of the Communication Driver for ADuC7023 processor.
4 * @author DBogdan ([email protected])
5 ********************************************************************************
6 * Copyright 2014(c) Analog Devices, Inc.
7 *
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 * - Neither the name of Analog Devices, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 * - The use of this software may or may not infringe the patent rights
22 * of one or more patent holders. This license does not release you
23 * from the requirement that you obtain separate licenses from these
24 * patent holders to use this software.
25 * - Use of the software either in source or binary form, must be run
26 * on or directly connected to an Analog Devices Inc. component.
27 *
28 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
30 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 *******************************************************************************/
39
40 /******************************************************************************/
41 /***************************** Include Files **********************************/
42 /******************************************************************************/
43 #include <ADuC7026.h>
44
45 /***************************************************************************//**
46 * @brief Initializes the SPI communication peripheral.
47 *
48 * @param lsbFirst - Transfer format (0 or 1).
49 * Example: 0x0 - MSB first.
50 * 0x1 - LSB first.
51 * @param clockFreq - SPI clock frequency (Hz).
52 * Example: 1000 - SPI clock frequency is 1 kHz.
53 * @param clockPol - SPI clock polarity (0 or 1).
54 * Example: 0x0 - Idle state for clock is a low level; active
55 * state is a high level;
56 * 0x1 - Idle state for clock is a high level; active
57 * state is a low level.
58 * @param clockEdg - SPI clock edge (0 or 1).
59 * Example: 0x0 - Serial output data changes on transition
60 * from idle clock state to active clock state;
61 * 0x1 - Serial output data changes on transition
62 * from active clock state to idle clock state.
63 *
64 * @return status - Result of the initialization procedure.
65 * Example: 0 - if initialization was successful;
66 * -1 - if initialization was unsuccessful.
67 *******************************************************************************/
68 char SPI_Init(char lsbFirst,
69 long clockFreq,
70 char clockPol,
71 char clockEdg)
72 {
73 long uClkFreq = 41780000;
74
75 /* Configure the SPI pins. */
76 //GP0CON &= ~0x33000000;
77 //GP0CON |= 0x11000000; // P0.7 - MOSI; P0.6 - MISO.
78
79 GP0CON &= ~0x30000000;
80 GP0CON |= 0x10000000; // P0.7 - MOSI;
81
82 GP1CON &= ~0x00000033;
83 GP1CON |= 0x00000011; // P1.1 - CS; P1.0 - SCLK.
84
85 SPIDIV = (uClkFreq / (2 * clockFreq)) - 1;
86
87 SPICON = 0x0043 | // Initiate transfer with a write to the SPITX register;
88 ((clockPol & 0x1) << 3) | // Set the polarity and the phase;
89 (!(clockEdg & 0x1) << 2); // Enable master mode; Enable the SPI.
90
91 return 0;
92 }
93
94 /***************************************************************************//**
95 * @brief Writes data to SPI.
96 *
97 * @param slaveDeviceId - The ID of the selected slave device.
98 * @param data - Data represents the write buffer.
99 * @param bytesNumber - Number of bytes to write.
100 *
101 * @return Number of written bytes.
102 *******************************************************************************/
103 char SPI_Write(char slaveDeviceId,
104 unsigned char* data,
105 char bytesNumber)
106 {
107 unsigned char byte = 0;
108
109 for(byte = 0; byte < bytesNumber; byte++)
110 {
111 SPITX = data[byte]; // Transmit data.
112 while((SPISTA & 0x0020) != 0x0020); // Wait for SPITX to be empty.
113 }
114
115 return bytesNumber;
116 }
117
118 /***************************************************************************//**
119 * @brief Reads data from SPI.
120 *
121 * @param slaveDeviceId - The ID of the selected slave device.
122 * @param data - Data represents the write buffer as an input parameter
123 * and the read buffer as an output parameter.
124 * @param bytesNumber - Number of bytes to read.
125 *
126 * @return Number of read bytes.
127 *******************************************************************************/
128 char SPI_Read(char slaveDeviceId,
129 unsigned char* data,
130 char bytesNumber)
131 {
132 unsigned char byte = 0;
133
134 for(byte = 0; byte < bytesNumber; byte++)
135 {
136 SPITX = data[byte]; // Transmit data.
137 while((SPISTA & 0x02) != 0x02); // Wait for SPITX to be empty.
138 data[byte] = SPIRX;
139 }
140
141 return bytesNumber;
142 }