James Reynolds / AD594x Driver
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ad5940mbedport.cpp Source File

ad5940mbedport.cpp

00001 /*!
00002  *****************************************************************************
00003   @file:    ad594xmbedport.cpp
00004   @author:  J. Reynolds (adapted from Analog Devices example code and D. Bill's code)
00005   @brief:   Serves as an interface for the Analog Devices AD594x library
00006 -----------------------------------------------------------------------------
00007 @license: https://github.com/analogdevicesinc/ad5940lib/blob/master/LICENSE (accessed on December 3, 2020)
00008 Copyright (c) 2019 Analog Devices, Inc.  All rights reserved.
00009 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
00010 - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
00011 - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.  
00012 - Modified versions of the software must be conspicuously marked as such.
00013 - This software is licensed solely and exclusively for use with processors/products manufactured by or for Analog Devices, Inc.
00014 - This software may not be combined or merged with other code in any manner that would cause the software to become subject to terms and conditions which differ from those listed here.
00015 - Neither the name of Analog Devices, Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
00016 - The use of this software may or may not infringe the patent rights of one or more patent holders.  This license does not release you from the requirement that you obtain separate licenses from these patent holders to use this software.
00017 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, TITLE, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANALOG DEVICES, INC. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, DAMAGES ARISING OUT OF CLAIMS OF INTELLECTUAL PROPERTY RIGHTS INFRINGEMENT; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00018 2019-01-10-7CBSD SLA
00019 This file is a MODIFIED VERSION of the source code provided from Analog Devices!
00020 *****************************************************************************/
00021 
00022 // D. Bill's code for the Arduino environment is accessible at https://github.com/IMTEK-Sensors/FreiStat_AD5940_AD5941
00023 
00024 #include "ad5940.h"
00025 #include "mbed.h"
00026 
00027 SPI spi(SPI_MOSI, SPI_MISO, SPI_SCK); // mosi, miso, sclk
00028 DigitalOut cs(A2);
00029 DigitalOut reset(A1);
00030 InterruptIn interrupt(A0);
00031 
00032 volatile static uint32_t ucInterrupted = 0;       /* Flag to indicate interrupt occurred */
00033 
00034 /**
00035     @brief Using SPI to transmit N bytes and return the received bytes. This function targets to
00036          provide a more efficient way to transmit/receive data.
00037     @param pSendBuffer :{0 - 0xFFFFFFFF}
00038       - Pointer to the data to be sent.
00039     @param pRecvBuff :{0 - 0xFFFFFFFF}
00040       - Pointer to the buffer used to store received data.
00041     @param length :{0 - 0xFFFFFFFF}
00042       - Data length in SendBuffer.
00043     @return None.
00044 **/
00045 void AD5940_ReadWriteNBytes(unsigned char *pSendBuffer, unsigned char *pRecvBuff, unsigned long length)
00046 {
00047   for (int i = 0; i < length; i++)
00048   {
00049     *pRecvBuff++ = spi.write(*pSendBuffer++);
00050   }
00051 }
00052 
00053 void AD5940_CsClr(void)
00054 {
00055     cs = 0;
00056 }
00057 
00058 void AD5940_CsSet(void)
00059 {
00060     cs = 1;
00061 }
00062 
00063 void AD5940_RstSet(void)
00064 {
00065     reset = 1;
00066 }
00067 
00068 void AD5940_RstClr(void)
00069 {
00070     reset = 0;
00071 }
00072 
00073 void AD5940_Delay10us(uint32_t time)
00074 {
00075     thread_sleep_for(time);
00076 }
00077 
00078 //declare the following function in the ad5940.h file if you use it in other .c files (that include ad5940.h):
00079 //unsigned long AD5940_GetMicros(void);
00080 //used for time tests:
00081 // unsigned long AD5940_GetMicros()
00082 // {
00083 //   return micros();
00084 // }
00085 
00086 uint32_t AD5940_GetMCUIntFlag(void)
00087 {
00088     return ucInterrupted;
00089 }
00090 
00091 uint32_t AD5940_ClrMCUIntFlag(void)
00092 {
00093     ucInterrupted = 1;
00094     return 1;
00095 }
00096 
00097 
00098 /* MCU related external line interrupt service routine */
00099 //The interrupt handler handles the interrupt to the MCU
00100 //when the AD5940 INTC pin generates an interrupt to alert the MCU that data is ready
00101 void Ext_Int0_Handler()
00102 {
00103     ucInterrupted = 1;
00104 }
00105 
00106 /* Functions that used to initialize MCU platform */
00107 
00108 uint32_t AD5940_MCUResourceInit(void *pCfg)
00109 {
00110   /* Step1, initialize SPI peripheral and its GPIOs for CS/RST */
00111   // Setup the spi for 8 bit data, high steady state clock,
00112   // second edge capture, with a 1MHz clock rate
00113     spi.format(8, 3);
00114     spi.frequency(1000000);
00115 
00116 
00117   /* Step2: initialize GPIO interrupt that connects to AD5940's interrupt output pin(Gp0, Gp3, Gp4, Gp6 or Gp7 ) */
00118   //init AD5940 interrupt pin
00119     interrupt.fall(&Ext_Int0_Handler);
00120   //chip select high to de-select AD5940 initially
00121   AD5940_CsSet();
00122   AD5940_RstSet();
00123   return 0;
00124 }
00125