Lin Team / Mbed 2 deprecated AD7190_LoRa

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Communication.cpp Source File

Communication.cpp

00001 /***************************************************************************//**
00002  *   @file   Communication.c
00003  *   @brief  Implementation of Communication Driver.
00004  *   @author DBogdan (dragos.bogdan@analog.com)
00005 ********************************************************************************
00006  * Copyright 2012-2015(c) Analog Devices, Inc.
00007  *
00008  * All rights reserved.
00009  *
00010  * Redistribution and use in source and binary forms, with or without
00011  * modification, are permitted provided that the following conditions are met:
00012  *  - Redistributions of source code must retain the above copyright
00013  *    notice, this list of conditions and the following disclaimer.
00014  *  - Redistributions in binary form must reproduce the above copyright
00015  *    notice, this list of conditions and the following disclaimer in
00016  *    the documentation and/or other materials provided with the
00017  *    distribution.
00018  *  - Neither the name of Analog Devices, Inc. nor the names of its
00019  *    contributors may be used to endorse or promote products derived
00020  *    from this software without specific prior written permission.
00021  *  - The use of this software may or may not infringe the patent rights
00022  *    of one or more patent holders.  This license does not release you
00023  *    from the requirement that you obtain separate licenses from these
00024  *    patent holders to use this software.
00025  *  - Use of the software either in source or binary form, must be run
00026  *    on or directly connected to an Analog Devices Inc. component.
00027  *
00028  * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
00029  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
00030  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00031  * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
00032  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00033  * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
00034  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00035  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00036  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00037  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00038  *
00039 *******************************************************************************/
00040 
00041 /******************************************************************************/
00042 /* Include Files                                                              */
00043 /******************************************************************************/
00044 #include "Communication.h"
00045 #include "stdlib.h"
00046 #include "mbed.h"
00047 /***************************************************************************//**
00048  * @brief Initializes the SPI communication peripheral.
00049  *
00050  * @param lsbFirst - Transfer format (0 or 1).
00051  *                   Example: 0x0 - MSB first.
00052  *                            0x1 - LSB first.
00053  * @param clockFreq - SPI clock frequency (Hz).
00054  *                    Example: 1000 - SPI clock frequency is 1 kHz.
00055  * @param clockPol - SPI clock polarity (0 or 1).
00056  *                   Example: 0x0 - Idle state for clock is a low level; active
00057  *                                  state is a high level;
00058  *                        0x1 - Idle state for clock is a high level; active
00059  *                                  state is a low level.
00060  * @param clockEdg - SPI clock edge (0 or 1).
00061  *                   Example: 0x0 - Serial output data changes on transition
00062  *                                  from idle clock state to active clock state;
00063  *                            0x1 - Serial output data changes on transition
00064  *                                  from active clock state to idle clock state.
00065  *
00066  * @return status - Result of the initialization procedure.
00067  *                  Example: 1 - if initialization was successful;
00068  *                           0 - if initialization was unsuccessful.
00069 *******************************************************************************/
00070 #define PRINT_SPI_INFO
00071 
00072 
00073 unsigned char read_buff[AD7190_READ_BUFFER_SIZE];
00074 
00075 
00076 unsigned char SPI_Init(unsigned char lsbFirst,
00077                        unsigned long clockFreq,
00078                        unsigned char clockPol,
00079                        unsigned char clockEdg)
00080 {
00081     /* Add your code here. */
00082     spi.format(8,3);
00083     spi.frequency(clockFreq);
00084     memset(read_buff, 0, AD7190_READ_BUFFER_SIZE);
00085     return 1;
00086 }
00087 
00088 /***************************************************************************//**
00089  * @brief Reads data from SPI.
00090  *
00091  * @param data - Data represents the write buffer as an input parameter and the
00092  *               read buffer as an output parameter.
00093  * @param bytesNumber - Number of bytes to read.
00094  *
00095  * @return Number of read bytes.
00096 *******************************************************************************/
00097 unsigned char SPI_Read(unsigned char* data,
00098                        unsigned char bytesNumber)
00099 {
00100     /* Add your code here. */
00101     int r_cnt = 0;
00102 
00103     if(bytesNumber > AD7190_READ_BUFFER_SIZE) return 0;
00104 
00105     #ifdef PRINT_SPI_INFO
00106  //       pc.printf("[SPI-OTR] 0x%02x\r\n", data[0]);
00107     #endif
00108 
00109     data[r_cnt] = spi.write(data[0]);
00110     bytesNumber --;
00111     r_cnt++;
00112 
00113     #ifdef PRINT_SPI_INFO
00114   //      pc.printf("[SPI-IN ] ");
00115     #endif
00116     while(bytesNumber > 0){
00117         data[r_cnt] = spi.write(0xFF);
00118        
00119         #ifdef PRINT_SPI_INFO
00120    //         pc.printf("0x%02x\t", data[r_cnt]);
00121         #endif
00122 
00123         r_cnt++;
00124         bytesNumber --;
00125     }
00126 
00127     #ifdef PRINT_SPI_INFO
00128     //    pc.printf("\r\n");
00129     #endif
00130     
00131     return r_cnt;
00132 }
00133 
00134 /***************************************************************************//**
00135  * @brief Writes data to SPI.
00136  *
00137  * @param data - Data represents the write buffer.
00138  * @param bytesNumber - Number of bytes to write.
00139  *
00140  * @return Number of written bytes.
00141 *******************************************************************************/
00142 unsigned char SPI_Write(unsigned char* data,
00143                         unsigned char bytesNumber)
00144 {
00145     /* Add your code here. */
00146     memset(read_buff, 0, AD7190_READ_BUFFER_SIZE);
00147     int w_cnt = 0;
00148 
00149     if(bytesNumber > AD7190_READ_BUFFER_SIZE) return 0;
00150     
00151     #ifdef PRINT_SPI_INFO
00152   //      pc.printf("[SPI-OUT] ");
00153     #endif
00154 
00155     while(bytesNumber > 0){
00156         read_buff[w_cnt] = spi.write(data[w_cnt]);
00157 
00158         #ifdef PRINT_SPI_INFO
00159   //          pc.printf("0x%02x\t", data[w_cnt]);
00160         #endif
00161         
00162         w_cnt++;
00163         bytesNumber --;
00164     }
00165 
00166     #ifdef PRINT_SPI_INFO
00167   //      pc.printf("\r\n[SPI-RTN] ");
00168         for(int i=0; i<w_cnt; i++){
00169    //         pc.printf("0x%02x\t", read_buff[w_cnt]);
00170         }
00171    //     pc.printf("\r\n");
00172     #endif
00173     
00174     return  w_cnt;
00175 }