Import Mbed OS hard-float snapshot
This commit is contained in:
@@ -0,0 +1,136 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2010-2017 Analog Devices, Inc.
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* - 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.
|
||||
* - Modified versions of the software must be conspicuously marked as such.
|
||||
* - This software is licensed solely and exclusively for use with processors
|
||||
* manufactured by or for Analog Devices, Inc.
|
||||
* - 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.
|
||||
* - 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.
|
||||
* - 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.
|
||||
*
|
||||
* 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.
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef MBED_PERIPHERALNAMES_H
|
||||
#define MBED_PERIPHERALNAMES_H
|
||||
|
||||
#include "cmsis.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
OSC32KCLK = 0,
|
||||
} RTCName;
|
||||
|
||||
typedef enum {
|
||||
UART_0 = 0,
|
||||
UART_1 = 1,
|
||||
UART_2 = 2,
|
||||
UART_3 = 3,
|
||||
UART_4 = 4,
|
||||
} UARTName;
|
||||
|
||||
#define STDIO_UART_TX USBTX
|
||||
#define STDIO_UART_RX USBRX
|
||||
#define STDIO_UART UART_0
|
||||
|
||||
typedef enum {
|
||||
I2C_0 = 0,
|
||||
I2C_1 = 1,
|
||||
I2C_2 = 2,
|
||||
} I2CName;
|
||||
|
||||
#define TPM_SHIFT 8
|
||||
typedef enum {
|
||||
PWM_1 = (0 << TPM_SHIFT) | (0), // FTM0 CH0
|
||||
PWM_2 = (0 << TPM_SHIFT) | (1), // FTM0 CH1
|
||||
PWM_3 = (0 << TPM_SHIFT) | (2), // FTM0 CH2
|
||||
PWM_4 = (0 << TPM_SHIFT) | (3), // FTM0 CH3
|
||||
PWM_5 = (0 << TPM_SHIFT) | (4), // FTM0 CH4
|
||||
PWM_6 = (0 << TPM_SHIFT) | (5), // FTM0 CH5
|
||||
PWM_7 = (0 << TPM_SHIFT) | (6), // FTM0 CH6
|
||||
PWM_8 = (0 << TPM_SHIFT) | (7), // FTM0 CH7
|
||||
PWM_9 = (1 << TPM_SHIFT) | (0), // FTM1 CH0
|
||||
PWM_10 = (1 << TPM_SHIFT) | (1), // FTM1 CH1
|
||||
PWM_11 = (1 << TPM_SHIFT) | (2), // FTM1 CH2
|
||||
PWM_12 = (1 << TPM_SHIFT) | (3), // FTM1 CH3
|
||||
PWM_13 = (1 << TPM_SHIFT) | (4), // FTM1 CH4
|
||||
PWM_14 = (1 << TPM_SHIFT) | (5), // FTM1 CH5
|
||||
PWM_15 = (1 << TPM_SHIFT) | (6), // FTM1 CH6
|
||||
PWM_16 = (1 << TPM_SHIFT) | (7), // FTM1 CH7
|
||||
PWM_17 = (2 << TPM_SHIFT) | (0), // FTM2 CH0
|
||||
PWM_18 = (2 << TPM_SHIFT) | (1), // FTM2 CH1
|
||||
PWM_19 = (2 << TPM_SHIFT) | (2), // FTM2 CH2
|
||||
PWM_20 = (2 << TPM_SHIFT) | (3), // FTM2 CH3
|
||||
PWM_21 = (2 << TPM_SHIFT) | (4), // FTM2 CH4
|
||||
PWM_22 = (2 << TPM_SHIFT) | (5), // FTM2 CH5
|
||||
PWM_23 = (2 << TPM_SHIFT) | (6), // FTM2 CH6
|
||||
PWM_24 = (2 << TPM_SHIFT) | (7), // FTM2 CH7
|
||||
// could be 4 or could be 3... not sure what register
|
||||
// this is for... too much abstraction
|
||||
PWM_25 = (3 << TPM_SHIFT) | (0), // FTM3 CH0
|
||||
PWM_26 = (3 << TPM_SHIFT) | (1), // FTM3 CH1
|
||||
PWM_27 = (3 << TPM_SHIFT) | (2), // FTM3 CH2
|
||||
PWM_28 = (3 << TPM_SHIFT) | (3), // FTM3 CH3
|
||||
PWM_29 = (3 << TPM_SHIFT) | (4), // FTM3 CH4
|
||||
PWM_30 = (3 << TPM_SHIFT) | (5), // FTM3 CH5
|
||||
PWM_31 = (3 << TPM_SHIFT) | (6), // FTM3 CH6
|
||||
PWM_32 = (3 << TPM_SHIFT) | (7), // FTM3 CH7
|
||||
} PWMName;
|
||||
|
||||
typedef enum {
|
||||
ADC0_VIN0 = 0,
|
||||
ADC0_VIN1 = 1,
|
||||
ADC0_VIN2 = 2,
|
||||
ADC0_VIN3 = 3,
|
||||
ADC0_VIN4 = 4,
|
||||
ADC0_VIN5 = 5,
|
||||
ADC0_VIN6 = 6,
|
||||
ADC0_VIN7 = 7
|
||||
} ADCName;
|
||||
|
||||
typedef enum {
|
||||
DAC_0 = 0
|
||||
} DACName;
|
||||
|
||||
|
||||
typedef enum {
|
||||
SPI_0 = 0,
|
||||
SPI_1 = 1,
|
||||
SPI_2 = 2,
|
||||
} SPIName;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,111 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2010-2017 Analog Devices, Inc.
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* - 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.
|
||||
* - Modified versions of the software must be conspicuously marked as such.
|
||||
* - This software is licensed solely and exclusively for use with processors
|
||||
* manufactured by or for Analog Devices, Inc.
|
||||
* - 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.
|
||||
* - 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.
|
||||
* - 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.
|
||||
*
|
||||
* 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.
|
||||
******************************************************************************/
|
||||
|
||||
#include "PeripheralPins.h"
|
||||
|
||||
|
||||
/************UART***************/
|
||||
const PinMap PinMap_UART_TX[] = {
|
||||
{P0_10, UART_0, 1},
|
||||
{NC, NC, 0}
|
||||
};
|
||||
|
||||
const PinMap PinMap_UART_RX[] = {
|
||||
{P0_11, UART_0, 1},
|
||||
{NC, NC, 0}
|
||||
};
|
||||
|
||||
/************SPI***************/
|
||||
const PinMap PinMap_SPI_SCLK[] = {
|
||||
{P0_00, SPI_0, 1},
|
||||
{P1_06, SPI_1, 1},
|
||||
{P1_02, SPI_2, 1},
|
||||
{NC, NC, 0}
|
||||
};
|
||||
|
||||
const PinMap PinMap_SPI_MOSI[] = {
|
||||
{P0_01, SPI_0, 1},
|
||||
{P1_07, SPI_1, 1},
|
||||
{P1_03, SPI_2, 1},
|
||||
{NC, NC, 0}
|
||||
};
|
||||
|
||||
const PinMap PinMap_SPI_MISO[] = {
|
||||
{P0_02, SPI_0, 1},
|
||||
{P1_08, SPI_1, 1},
|
||||
{P1_04, SPI_2, 1},
|
||||
{NC, NC, 0}
|
||||
};
|
||||
|
||||
const PinMap PinMap_SPI_SSEL[] = {
|
||||
{P0_03, SPI_0, 1},
|
||||
{P1_09, SPI_1, 1},
|
||||
{P2_10, SPI_2, 1},
|
||||
{NC, NC, 0}
|
||||
};
|
||||
|
||||
/************ADC***************/
|
||||
const PinMap PinMap_ADC[] = {
|
||||
{P2_03, ADC0_VIN0, 1},
|
||||
{P2_04, ADC0_VIN1, 1},
|
||||
{P2_05, ADC0_VIN2, 1},
|
||||
{P2_06, ADC0_VIN3, 1},
|
||||
{P2_07, ADC0_VIN4, 1},
|
||||
{P2_08, ADC0_VIN5, 1},
|
||||
{P2_09, ADC0_VIN6, 1},
|
||||
{P2_10, ADC0_VIN7, 1},
|
||||
{NC, NC, 0}
|
||||
};
|
||||
|
||||
/************I2C***************/
|
||||
const PinMap PinMap_I2C_SDA[] = {
|
||||
{P0_05, I2C_0, 1},
|
||||
{NC, NC, 0}
|
||||
};
|
||||
|
||||
const PinMap PinMap_I2C_SCL[] = {
|
||||
{P0_04, I2C_0, 1},
|
||||
{NC, NC, 0}
|
||||
};
|
||||
|
||||
/************RTC***************/
|
||||
const PinMap PinMap_RTC[] = {
|
||||
{NC, OSC32KCLK, 0},
|
||||
};
|
||||
@@ -0,0 +1,67 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2010-2017 Analog Devices, Inc.
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* - 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.
|
||||
* - Modified versions of the software must be conspicuously marked as such.
|
||||
* - This software is licensed solely and exclusively for use with processors
|
||||
* manufactured by or for Analog Devices, Inc.
|
||||
* - 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.
|
||||
* - 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.
|
||||
* - 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.
|
||||
*
|
||||
* 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.
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef MBED_PERIPHERALPINS_H
|
||||
#define MBED_PERIPHERALPINS_H
|
||||
|
||||
#include "pinmap.h"
|
||||
#include "PeripheralNames.h"
|
||||
|
||||
/************RTC***************/
|
||||
extern const PinMap PinMap_RTC[];
|
||||
|
||||
/************ADC***************/
|
||||
extern const PinMap PinMap_ADC[];
|
||||
|
||||
/************I2C***************/
|
||||
extern const PinMap PinMap_I2C_SDA[];
|
||||
extern const PinMap PinMap_I2C_SCL[];
|
||||
|
||||
/************UART***************/
|
||||
extern const PinMap PinMap_UART_TX[];
|
||||
extern const PinMap PinMap_UART_RX[];
|
||||
|
||||
/************SPI***************/
|
||||
extern const PinMap PinMap_SPI_SCLK[];
|
||||
extern const PinMap PinMap_SPI_MOSI[];
|
||||
extern const PinMap PinMap_SPI_MISO[];
|
||||
extern const PinMap PinMap_SPI_SSEL[];
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,241 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2010-2017 Analog Devices, Inc.
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* - 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.
|
||||
* - Modified versions of the software must be conspicuously marked as such.
|
||||
* - This software is licensed solely and exclusively for use with processors
|
||||
* manufactured by or for Analog Devices, Inc.
|
||||
* - 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.
|
||||
* - 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.
|
||||
* - 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.
|
||||
*
|
||||
* 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.
|
||||
******************************************************************************/
|
||||
|
||||
#include "mbed_assert.h"
|
||||
#include "analogin_api.h"
|
||||
|
||||
#if DEVICE_ANALOGIN
|
||||
|
||||
#include "adi_adc_def.h"
|
||||
#include "pinmap.h"
|
||||
#include "PeripheralPins.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* ADC Device number */
|
||||
#define ADC_DEV_NUM (0u)
|
||||
|
||||
/* Memory Required for adc driver */
|
||||
static uint32_t DeviceMemory[(ADI_ADC_MEMORY_SIZE+3)/4];
|
||||
/* Active channel */
|
||||
static uint32_t adi_pin2channel(PinName pin);
|
||||
|
||||
/**
|
||||
* \defgroup hal_analogin Analogin hal functions
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** Initialize the analogin peripheral
|
||||
*
|
||||
* Configures the pin used by analogin.
|
||||
* @param obj The analogin object to initialize
|
||||
* @param pin The analogin pin name
|
||||
*/
|
||||
void analogin_init(analogin_t *obj, PinName pin)
|
||||
{
|
||||
ADI_ADC_HANDLE hDevice;
|
||||
bool bCalibrationDone = false;
|
||||
bool bReady = false;
|
||||
|
||||
ADCName peripheral;
|
||||
uint32_t function, channel;
|
||||
|
||||
memset(obj, 0, sizeof(analogin_t) );
|
||||
memset( DeviceMemory, 0, sizeof( DeviceMemory ) );
|
||||
|
||||
peripheral = (ADCName)pinmap_peripheral(pin, &PinMap_ADC[0]); // gives peripheral
|
||||
MBED_ASSERT(peripheral != (ADCName)NC);
|
||||
|
||||
/* verify read function */
|
||||
function = pinmap_function(pin, &PinMap_ADC[0]);
|
||||
MBED_ASSERT(function == 1);
|
||||
|
||||
/* Configure PORT2_MUX registers */
|
||||
pin_function(pin, function);
|
||||
|
||||
/* Configure active channel */
|
||||
channel = adi_pin2channel(pin);
|
||||
MBED_ASSERT(channel != 0xFFFFFFFF);
|
||||
obj->UserBuffer.nChannels = channel;
|
||||
|
||||
/* Set ACLK to CCLK/16 */
|
||||
adi_pwr_SetClockDivider(ADI_CLOCK_ACLK,16);
|
||||
|
||||
/* Set default values for conversion and delay cycles. This sets up a sampling rate of
|
||||
16kHz. The sampling frequency is worked out from the following:
|
||||
|
||||
if delay time > 0:
|
||||
Fs = ACLK / [((14 + sampling time) * oversample factor) + (delay time + 2)]
|
||||
if delay time = 0:
|
||||
Fs = ACLK / ((14 + sampling time) * oversample factor)
|
||||
|
||||
The sampling (or acquisition) and delay times are in number of ACLK clock cycles.
|
||||
*/
|
||||
obj->DelayCycles = 0;
|
||||
obj->SampleCycles = 88;
|
||||
|
||||
/* Open the ADC device */
|
||||
adi_adc_Open(ADC_DEV_NUM, DeviceMemory, sizeof(DeviceMemory), &hDevice);
|
||||
obj->hDevice = hDevice;
|
||||
|
||||
/* Power up ADC */
|
||||
adi_adc_PowerUp(hDevice, true);
|
||||
|
||||
/* Set ADC reference */
|
||||
adi_adc_SetVrefSource(hDevice, ADI_ADC_VREF_SRC_INT_2_50_V);
|
||||
|
||||
/* Enable ADC sub system */
|
||||
adi_adc_EnableADCSubSystem(hDevice, true);
|
||||
|
||||
/* Wait untilthe ADC is ready for sampling */
|
||||
while(bReady == false) {
|
||||
adi_adc_IsReady(hDevice, &bReady);
|
||||
}
|
||||
|
||||
/* Start calibration */
|
||||
adi_adc_StartCalibration(hDevice);
|
||||
|
||||
/* Wait until calibration is done */
|
||||
while (!bCalibrationDone) {
|
||||
adi_adc_IsCalibrationDone(hDevice, &bCalibrationDone);
|
||||
}
|
||||
|
||||
/* Set the delay time */
|
||||
adi_adc_SetDelayTime(hDevice, obj->DelayCycles);
|
||||
|
||||
/* Set the acquisition time. (Application need to change it based on the impedence) */
|
||||
adi_adc_SetAcquisitionTime(hDevice, obj->SampleCycles);
|
||||
|
||||
}
|
||||
|
||||
/** Read the input voltage, represented as a float in the range [0.0, 1.0]
|
||||
*
|
||||
* @param obj The analogin object
|
||||
* @return A floating value representing the current input voltage
|
||||
*/
|
||||
float analogin_read(analogin_t *obj)
|
||||
{
|
||||
float fl32 = (float)analogin_read_u16(obj)/(float)4095.0;
|
||||
|
||||
return(fl32);
|
||||
}
|
||||
|
||||
/** Read the value from analogin pin, represented as an unsigned 16bit value
|
||||
*
|
||||
* @param obj The analogin object
|
||||
* @return An unsigned 16bit value representing the current input voltage
|
||||
*/
|
||||
uint16_t analogin_read_u16(analogin_t *obj)
|
||||
{
|
||||
ADI_ADC_HANDLE hDevice = obj->hDevice;
|
||||
ADI_ADC_BUFFER *pAdcBuffer;
|
||||
uint32_t ADCsample;
|
||||
|
||||
obj->UserBuffer.pDataBuffer = &ADCsample;
|
||||
obj->UserBuffer.nNumConversionPasses = 1;
|
||||
obj->UserBuffer.nBuffSize = 1;
|
||||
|
||||
/* Submit the buffer to the driver */
|
||||
adi_adc_SubmitBuffer(hDevice, &obj->UserBuffer);
|
||||
|
||||
/* Enable the ADC */
|
||||
adi_adc_Enable(hDevice, true);
|
||||
|
||||
adi_adc_GetBuffer(hDevice, &pAdcBuffer);
|
||||
MBED_ASSERT(pAdcBuffer == &obj->UserBuffer);
|
||||
|
||||
return( (uint16_t)( ((uint16_t *)pAdcBuffer->pDataBuffer)[(pAdcBuffer->nNumConversionPasses) - 1]) );
|
||||
}
|
||||
|
||||
const PinMap *analogin_pinmap()
|
||||
{
|
||||
return PinMap_ADC;
|
||||
}
|
||||
|
||||
/* Retrieve the active channel corresponding to the input pin */
|
||||
static uint32_t adi_pin2channel(PinName pin)
|
||||
{
|
||||
|
||||
uint32_t activech;
|
||||
|
||||
switch(pin) {
|
||||
case ADC_VIN0:
|
||||
activech = ADI_ADC_CHANNEL_0;
|
||||
break;
|
||||
case ADC_VIN1:
|
||||
activech = ADI_ADC_CHANNEL_1;
|
||||
break;
|
||||
case ADC_VIN2:
|
||||
activech = ADI_ADC_CHANNEL_2;
|
||||
break;
|
||||
case ADC_VIN3:
|
||||
activech = ADI_ADC_CHANNEL_3;
|
||||
break;
|
||||
case ADC_VIN4:
|
||||
activech = ADI_ADC_CHANNEL_4;
|
||||
break;
|
||||
case ADC_VIN5:
|
||||
activech = ADI_ADC_CHANNEL_5;
|
||||
break;
|
||||
case ADC_VIN6:
|
||||
activech = ADI_ADC_CHANNEL_6;
|
||||
break;
|
||||
case ADC_VIN7:
|
||||
activech = ADI_ADC_CHANNEL_7;
|
||||
break;
|
||||
default:
|
||||
activech = (uint32_t) 0xFFFFFFFF;
|
||||
break;
|
||||
}
|
||||
|
||||
return ((uint32_t)activech);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**@}*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // #if DEVICE_ANALOGIN
|
||||
@@ -0,0 +1,47 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2010-2017 Analog Devices, Inc.
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* - 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.
|
||||
* - Modified versions of the software must be conspicuously marked as such.
|
||||
* - This software is licensed solely and exclusively for use with processors
|
||||
* manufactured by or for Analog Devices, Inc.
|
||||
* - 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.
|
||||
* - 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.
|
||||
* - 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.
|
||||
*
|
||||
* 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.
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef MBED_CMSIS_H
|
||||
#define MBED_CMSIS_H
|
||||
#define __C
|
||||
#include "adi_processor.h"
|
||||
#include "cmsis_nvic.h"
|
||||
#undef __C
|
||||
#endif
|
||||
@@ -0,0 +1,77 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2010-2018 Analog Devices, Inc.
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* - 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.
|
||||
* - Modified versions of the software must be conspicuously marked as such.
|
||||
* - This software is licensed solely and exclusively for use with processors
|
||||
* manufactured by or for Analog Devices, Inc.
|
||||
* - 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.
|
||||
* - 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.
|
||||
* - 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.
|
||||
*
|
||||
* 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.
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef MBED_CMSIS_NVIC_H
|
||||
#define MBED_CMSIS_NVIC_H
|
||||
|
||||
#include "cmsis.h"
|
||||
|
||||
#define NVIC_USER_IRQ_OFFSET 16
|
||||
#define NVIC_USER_IRQ_NUMBER 64
|
||||
#define NVIC_NUM_VECTORS (NVIC_USER_IRQ_OFFSET + NVIC_USER_IRQ_NUMBER)
|
||||
|
||||
#define NVIC_RAM_VECTOR_ADDRESS 0x20000000
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/** Set the ISR for IRQn
|
||||
*
|
||||
* Sets an Interrupt Service Routine vector for IRQn; if the feature is available, the vector table is relocated to SRAM
|
||||
* the first time this function is called
|
||||
* @param[in] IRQn The Interrupt Request number for which a vector will be registered
|
||||
* @param[in] vector The ISR vector to register for IRQn
|
||||
*/
|
||||
void NVIC_SetVector(IRQn_Type IRQn, uint32_t vector);
|
||||
|
||||
/** Get the ISR registered for IRQn
|
||||
*
|
||||
* Reads the Interrupt Service Routine currently registered for IRQn
|
||||
* @param[in] IRQn The Interrupt Request number the vector of which will be read
|
||||
* @return Returns the ISR registered for IRQn
|
||||
*/
|
||||
uint32_t NVIC_GetVector(IRQn_Type IRQn);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,48 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2010-2017 Analog Devices, Inc.
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* - 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.
|
||||
* - Modified versions of the software must be conspicuously marked as such.
|
||||
* - This software is licensed solely and exclusively for use with processors
|
||||
* manufactured by or for Analog Devices, Inc.
|
||||
* - 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.
|
||||
* - 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.
|
||||
* - 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.
|
||||
*
|
||||
* 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.
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef MBED_DEVICE_H
|
||||
#define MBED_DEVICE_H
|
||||
|
||||
#define DEVICE_ID_LENGTH 24
|
||||
|
||||
#include "objects.h"
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,91 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2010-2018 Analog Devices, Inc.
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* - 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.
|
||||
* - Modified versions of the software must be conspicuously marked as such.
|
||||
* - This software is licensed solely and exclusively for use with processors
|
||||
* manufactured by or for Analog Devices, Inc.
|
||||
* - 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.
|
||||
* - 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.
|
||||
* - 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.
|
||||
*
|
||||
* 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.
|
||||
******************************************************************************/
|
||||
|
||||
#if DEVICE_FLASH
|
||||
#include "flash_api.h"
|
||||
#include "flash_data.h"
|
||||
#include "mbed_critical.h"
|
||||
|
||||
// This file is automagically generated
|
||||
|
||||
// This is a flash algo binary blob. It is PIC (position independent code) that should be stored in RAM
|
||||
|
||||
static unsigned FLASH_ALGO[] = {
|
||||
0x2000B508,0x4A5B1E41,0x495B6011,0x21076211,0xBF006091,0x68094957,0x99009100,0x0104F001,
|
||||
0xD0F72900,0xF0019900,0xB1010130,0x21002001,0x62114A50,0xB5F0BD08,0x460F4606,0x4D4D4614,
|
||||
0x20013554,0x20007028,0x6048494A,0xD1072C01,0x6D004608,0x0001F000,0xF7FFB110,0xBDF0FFD1,
|
||||
0xE7FC2000,0x20004601,0x62104A42,0xB5084770,0x20004601,0x4B3F1E42,0x461A601A,0x4A3E6191,
|
||||
0x2206621A,0xBF00609A,0x68124A3A,0x9A009200,0x0204F002,0xD0F72A00,0xF0029A00,0xB1020230,
|
||||
0x22002001,0x621A4B33,0xB5FEBD08,0x460B4604,0x46252600,0x48304611,0x62384F2E,0xF04FE052,
|
||||
0x4F2C30FF,0x2B086038,0x6808D304,0x68486138,0xE02F6178,0x3CFFF04F,0xC000F8CD,0xC004F8CD,
|
||||
0x2B084668,0xE8DFD21A,0x1619F003,0x0A0D1013,0x798F0407,0xBF007187,0x7147794F,0x790FBF00,
|
||||
0xBF007107,0x70C778CF,0x788FBF00,0xBF007087,0x7047784F,0x780FBF00,0xE0007007,0xBF00BF00,
|
||||
0xC050F8DF,0xF8CC9F00,0x9F017010,0x7014F8CC,0xBF002308,0x60C5480F,0x4F0E2004,0x463860B8,
|
||||
0xF0006800,0xB1080030,0xE00D2601,0x4809BF00,0x90026800,0xF0009802,0x28000004,0x3B08D0F7,
|
||||
0x35083108,0xD1AA2B00,0x2000BF00,0x62384F01,0xBDFE4630,0x40018000,0x676C7565,0
|
||||
};
|
||||
|
||||
static const flash_algo_t flash_algo_config = {
|
||||
.init = 0x00000037,
|
||||
.uninit = 0x00000065,
|
||||
.erase_sector = 0x0000006F,
|
||||
.program_page = 0x000000AB,
|
||||
.static_base = 0x0000017C,
|
||||
.algo_blob = (uint32_t *)FLASH_ALGO
|
||||
};
|
||||
|
||||
static const sector_info_t sectors_info[] = {
|
||||
{0x0, 0x800},
|
||||
};
|
||||
|
||||
static const flash_target_config_t flash_target_config = {
|
||||
.page_size = 0x8, // minimal programmable unit size
|
||||
.flash_start = 0x0,
|
||||
.flash_size = 0x00040000,
|
||||
.sectors = sectors_info,
|
||||
.sector_info_count = sizeof(sectors_info) / sizeof(sector_info_t)
|
||||
};
|
||||
|
||||
void flash_set_target_config(flash_t *obj)
|
||||
{
|
||||
obj->flash_algo = &flash_algo_config;
|
||||
obj->target_config = &flash_target_config;
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,150 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2010-2017 Analog Devices, Inc.
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* - 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.
|
||||
* - Modified versions of the software must be conspicuously marked as such.
|
||||
* - This software is licensed solely and exclusively for use with processors
|
||||
* manufactured by or for Analog Devices, Inc.
|
||||
* - 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.
|
||||
* - 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.
|
||||
* - 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.
|
||||
*
|
||||
* 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.
|
||||
******************************************************************************/
|
||||
|
||||
#include "mbed_assert.h"
|
||||
#include "gpio_api.h"
|
||||
#include "pinmap.h"
|
||||
#include "adi_gpio.h"
|
||||
|
||||
|
||||
#define MUX_FUNC_0 0x0
|
||||
#define NUM_GPIO_PORTS 4
|
||||
/*******************************************************************************
|
||||
ADI_GPIO_DEV_DATA Instance memory containing memory pointer should
|
||||
guarantee 4 byte alignmnet.
|
||||
*******************************************************************************/
|
||||
extern uint32_t gpioMemory[(ADI_GPIO_MEMORY_SIZE + 3)/4];
|
||||
extern uint8_t gpio_initialized;
|
||||
|
||||
static uint16_t gpio_oen[NUM_GPIO_PORTS] = {0};
|
||||
static uint16_t gpio_output_val[NUM_GPIO_PORTS] = {0};
|
||||
|
||||
|
||||
/******************************************************************************
|
||||
Function definitions
|
||||
*****************************************************************************/
|
||||
uint32_t gpio_set(PinName pin)
|
||||
{
|
||||
MBED_ASSERT(pin != (PinName)NC);
|
||||
uint32_t pin_num = pin & 0xFF;
|
||||
|
||||
pin_function(pin, MUX_FUNC_0);
|
||||
|
||||
return (1 << pin_num);
|
||||
}
|
||||
|
||||
void gpio_init(gpio_t *obj, PinName pin)
|
||||
{
|
||||
obj->pin = pin;
|
||||
|
||||
if (pin == (PinName)NC) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Initialize the GPIO driver. This function
|
||||
// initializes the GPIO driver only once globally.
|
||||
if (!gpio_initialized) {
|
||||
adi_gpio_Init(gpioMemory, ADI_GPIO_MEMORY_SIZE);
|
||||
}
|
||||
|
||||
pin_function(pin, MUX_FUNC_0);
|
||||
}
|
||||
|
||||
void gpio_mode(gpio_t *obj, PinMode mode)
|
||||
{
|
||||
uint32_t pin = obj->pin;
|
||||
|
||||
pin_mode((PinName)pin, mode);
|
||||
}
|
||||
|
||||
void gpio_dir(gpio_t *obj, PinDirection direction)
|
||||
{
|
||||
MBED_ASSERT(obj->pin != (PinName)NC);
|
||||
uint32_t port = obj->pin >> GPIO_PORT_SHIFT;
|
||||
uint32_t pin_num = obj->pin & 0xFF;
|
||||
|
||||
if (direction == PIN_OUTPUT) {
|
||||
adi_gpio_OutputEnable(port, 1 << pin_num, true);
|
||||
// save the input/output configuration
|
||||
gpio_oen[port] |= (1 << pin_num);
|
||||
} else {
|
||||
adi_gpio_InputEnable(port, 1 << pin_num, true);
|
||||
// save the input/output configuration
|
||||
gpio_oen[port] &= (~(1 << pin_num));
|
||||
}
|
||||
}
|
||||
|
||||
void gpio_write(gpio_t *obj, int value)
|
||||
{
|
||||
MBED_ASSERT(obj->pin != (PinName)NC);
|
||||
uint32_t port = obj->pin >> GPIO_PORT_SHIFT;
|
||||
uint32_t pin_num = obj->pin & 0xFF;
|
||||
|
||||
if (value & 1) {
|
||||
adi_gpio_SetHigh(port, (1 << pin_num));
|
||||
|
||||
// save the output port value
|
||||
gpio_output_val[port] |= ((value & 1) << pin_num);
|
||||
} else {
|
||||
adi_gpio_SetLow(port, (1 << pin_num));
|
||||
|
||||
// save the output port value
|
||||
gpio_output_val[port] &= (~(1 << pin_num));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int gpio_read(gpio_t *obj)
|
||||
{
|
||||
MBED_ASSERT(obj->pin != (PinName)NC);
|
||||
uint32_t port = obj->pin >> GPIO_PORT_SHIFT;
|
||||
uint32_t pin_num = obj->pin & 0xFF;
|
||||
uint16_t Data;
|
||||
|
||||
// check whether the pin is configured as input or output
|
||||
if ((gpio_oen[port] >> pin_num) & 1) {
|
||||
Data = gpio_output_val[port] & (1 << pin_num);
|
||||
} else {
|
||||
// otherwise call GetData
|
||||
adi_gpio_GetData(port, (1 << pin_num), &Data);
|
||||
}
|
||||
|
||||
return ((((uint32_t)Data) >> pin_num) & 1);
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2010-2017 Analog Devices, Inc.
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* - 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.
|
||||
* - Modified versions of the software must be conspicuously marked as such.
|
||||
* - This software is licensed solely and exclusively for use with processors
|
||||
* manufactured by or for Analog Devices, Inc.
|
||||
* - 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.
|
||||
* - 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.
|
||||
* - 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.
|
||||
*
|
||||
* 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.
|
||||
******************************************************************************/
|
||||
|
||||
#include <drivers/gpio/adi_gpio.h>
|
||||
/*******************************************************************************
|
||||
ADI_GPIO_DEV_DATA Instance memory containing memory pointer should
|
||||
guarantee 4 byte alignmnet.
|
||||
*******************************************************************************/
|
||||
// ADI GPIO device driver state memory. Only one state memory is required globally.
|
||||
uint32_t gpioMemory[(ADI_GPIO_MEMORY_SIZE + 3)/4];
|
||||
|
||||
// Flag to indicate whether the GPIO driver has been initialized
|
||||
uint8_t gpio_initialized = 0;
|
||||
@@ -0,0 +1,330 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2010-2017 Analog Devices, Inc.
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* - 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.
|
||||
* - Modified versions of the software must be conspicuously marked as such.
|
||||
* - This software is licensed solely and exclusively for use with processors
|
||||
* manufactured by or for Analog Devices, Inc.
|
||||
* - 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.
|
||||
* - 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.
|
||||
* - 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.
|
||||
*
|
||||
* 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.
|
||||
******************************************************************************/
|
||||
|
||||
#include "gpio_irq_api.h"
|
||||
#include "adi_gpio.h"
|
||||
#include "adi_gpio_def.h"
|
||||
#include "ADuCM302x_device.h"
|
||||
|
||||
#if DEVICE_INTERRUPTIN
|
||||
|
||||
#define MAX_GPIO_LINES 16
|
||||
#define MAX_GPIO_PORTS ADI_GPIO_NUM_PORTS
|
||||
|
||||
typedef struct {
|
||||
unsigned int id;
|
||||
gpio_irq_event event;
|
||||
uint8_t int_enable;
|
||||
} gpio_chan_info_t;
|
||||
/*******************************************************************************
|
||||
ADI_GPIO_DEV_DATA Instance memory containing memory pointer should
|
||||
guarantee 4 byte alignmnet.
|
||||
*******************************************************************************/
|
||||
extern uint32_t gpioMemory[(ADI_GPIO_MEMORY_SIZE + 3)/4];
|
||||
extern uint8_t gpio_initialized;
|
||||
static gpio_chan_info_t channel_ids[MAX_GPIO_PORTS][MAX_GPIO_LINES];
|
||||
static gpio_irq_handler irq_handler = NULL;
|
||||
|
||||
|
||||
/** Local interrupt callback routine.
|
||||
*/
|
||||
static void gpio_irq_callback(void *pCBParam, uint32_t Event, void *pArg)
|
||||
{
|
||||
uint16_t pin = *(ADI_GPIO_DATA*)pArg;
|
||||
int index = 0;
|
||||
|
||||
// determine the index of the pin that caused the interrupt
|
||||
while (pin) {
|
||||
if (pin & 0x01) {
|
||||
// call the user ISR. The argument Event is the port number of the GPIO line.
|
||||
if (irq_handler != NULL)
|
||||
irq_handler((uint32_t)channel_ids[Event][index].id, channel_ids[Event][index].event);
|
||||
}
|
||||
index++;
|
||||
pin >>= 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** Function to get the IENA and IENB register values.
|
||||
* Added here temporarily until these are available in the BSP
|
||||
*/
|
||||
static ADI_GPIO_RESULT adi_gpio_GetGroupInterruptPins(const ADI_GPIO_PORT Port, const IRQn_Type eIrq,
|
||||
const ADI_GPIO_DATA Pins, uint16_t* const pValue)
|
||||
{
|
||||
ADI_GPIO_TypeDef *pReg[MAX_GPIO_PORTS] = {pADI_GPIO0, pADI_GPIO1, pADI_GPIO2};
|
||||
ADI_GPIO_TypeDef *pPort; /* pointer to port registers */
|
||||
uint16_t Value = 0u;
|
||||
|
||||
pPort = pReg[Port];
|
||||
|
||||
switch (eIrq) {
|
||||
case SYS_GPIO_INTA_IRQn:
|
||||
Value = pPort->IENA;
|
||||
break;
|
||||
case SYS_GPIO_INTB_IRQn:
|
||||
Value = pPort->IENB;
|
||||
break;
|
||||
default:
|
||||
break; /* This shall never reach */
|
||||
}
|
||||
|
||||
*pValue = (Value & Pins);
|
||||
return (ADI_GPIO_SUCCESS);
|
||||
}
|
||||
|
||||
|
||||
/** Function to get the interrupt polarity register content.
|
||||
* Added here temporarily until these are available in the BSP
|
||||
*/
|
||||
static ADI_GPIO_RESULT adi_gpio_GetGroupInterruptPolarity(const ADI_GPIO_PORT Port, const ADI_GPIO_DATA Pins,
|
||||
uint16_t* const pValue)
|
||||
{
|
||||
ADI_GPIO_TypeDef *pPort; /* pointer to port registers */
|
||||
ADI_GPIO_TypeDef *pReg[MAX_GPIO_PORTS] = {pADI_GPIO0, pADI_GPIO1, pADI_GPIO2};
|
||||
|
||||
pPort = pReg[Port];
|
||||
|
||||
*pValue = (pPort->POL & Pins);
|
||||
|
||||
return (ADI_GPIO_SUCCESS);
|
||||
}
|
||||
|
||||
|
||||
/** Function to clear the relevant interrupt enable bits in both the IENA and IENB registers
|
||||
* for the given GPIO pin.
|
||||
*/
|
||||
static void disable_pin_interrupt(ADI_GPIO_PORT port, uint32_t pin_number)
|
||||
{
|
||||
uint16_t int_reg_val;
|
||||
|
||||
// Read the current content of the IENA register
|
||||
adi_gpio_GetGroupInterruptPins(port, SYS_GPIO_INTA_IRQn, 1 << pin_number, &int_reg_val);
|
||||
|
||||
// clear the bit for the pin
|
||||
int_reg_val &= ~(1 << pin_number);
|
||||
|
||||
// write the interrupt register
|
||||
adi_gpio_SetGroupInterruptPins(port, SYS_GPIO_INTA_IRQn, int_reg_val);
|
||||
|
||||
// Do the same to IENB
|
||||
adi_gpio_GetGroupInterruptPins(port, SYS_GPIO_INTB_IRQn, 1 << pin_number, &int_reg_val);
|
||||
|
||||
// clear the bit for the pin
|
||||
int_reg_val &= ~(1 << pin_number);
|
||||
|
||||
// write the interrupt register
|
||||
adi_gpio_SetGroupInterruptPins(port, SYS_GPIO_INTB_IRQn, int_reg_val);
|
||||
}
|
||||
|
||||
|
||||
/** Function to set the relevant interrupt enable bits in either the IENA and IENB registers
|
||||
* for the given GPIO pin.
|
||||
*/
|
||||
static void enable_pin_interrupt(ADI_GPIO_PORT port, uint32_t pin_number, IRQn_Type eIrq)
|
||||
{
|
||||
uint16_t int_reg_val;
|
||||
|
||||
// Read the current interrupt enable register content
|
||||
adi_gpio_GetGroupInterruptPins(port, eIrq, 1 << pin_number, &int_reg_val);
|
||||
|
||||
// set the bit for the pin
|
||||
int_reg_val |= (1 << pin_number);
|
||||
|
||||
// write the interrupt register
|
||||
adi_gpio_SetGroupInterruptPins(port, eIrq, int_reg_val);
|
||||
}
|
||||
|
||||
|
||||
/** Initialize the GPIO IRQ pin
|
||||
*
|
||||
* @param obj The GPIO object to initialize
|
||||
* @param pin The GPIO pin name
|
||||
* @param handler The handler to be attached to GPIO IRQ
|
||||
* @param id The object ID (id != 0, 0 is reserved)
|
||||
* @return -1 if pin is NC, 0 otherwise
|
||||
*/
|
||||
int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32_t id)
|
||||
{
|
||||
uint32_t port = pin >> GPIO_PORT_SHIFT;
|
||||
uint32_t pin_num = pin & 0xFF;
|
||||
|
||||
// check for valid pin and ID
|
||||
if ((pin == NC) || (id == 0)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// make sure gpio driver has been initialized
|
||||
if (!gpio_initialized) {
|
||||
adi_gpio_Init(gpioMemory,ADI_GPIO_MEMORY_SIZE);
|
||||
gpio_initialized = 1;
|
||||
}
|
||||
|
||||
// save the handler
|
||||
if (handler) {
|
||||
irq_handler = handler;
|
||||
}
|
||||
|
||||
// disable the interrupt for the given pin
|
||||
disable_pin_interrupt((ADI_GPIO_PORT)port, pin_num);
|
||||
|
||||
// set the port pin as input
|
||||
adi_gpio_InputEnable(port, 1 << pin_num, true);
|
||||
|
||||
// save the ID for future reference
|
||||
channel_ids[port][pin_num].id = (uint32_t)id;
|
||||
channel_ids[port][pin_num].event = IRQ_NONE;
|
||||
channel_ids[port][pin_num].int_enable = 0;
|
||||
obj->id = id;
|
||||
obj->pinname = pin;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** Release the GPIO IRQ PIN
|
||||
*
|
||||
* @param obj The gpio object
|
||||
*/
|
||||
void gpio_irq_free(gpio_irq_t *obj)
|
||||
{
|
||||
uint32_t port = obj->pinname >> GPIO_PORT_SHIFT;
|
||||
uint32_t pin_num = obj->pinname & 0xFF;
|
||||
|
||||
// disable interrupt for the given pin
|
||||
gpio_irq_disable(obj);
|
||||
|
||||
// clear the status table
|
||||
channel_ids[port][pin_num].id = (uint32_t)0;
|
||||
channel_ids[port][pin_num].event = IRQ_NONE;
|
||||
channel_ids[port][pin_num].int_enable = 0;
|
||||
}
|
||||
|
||||
/** Enable/disable pin IRQ event
|
||||
*
|
||||
* @param obj The GPIO object
|
||||
* @param event The GPIO IRQ event
|
||||
* @param enable The enable flag
|
||||
*/
|
||||
void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
|
||||
{
|
||||
uint16_t int_polarity_reg;
|
||||
uint32_t port = obj->pinname >> GPIO_PORT_SHIFT;
|
||||
uint32_t pin_num = obj->pinname & 0xFF;
|
||||
|
||||
if (event == IRQ_NONE) {
|
||||
return;
|
||||
}
|
||||
|
||||
// read the current polarity register
|
||||
adi_gpio_GetGroupInterruptPolarity((ADI_GPIO_PORT)port, 1 << pin_num, &int_polarity_reg);
|
||||
|
||||
if (event == IRQ_RISE) {
|
||||
int_polarity_reg |= (1 << pin_num);
|
||||
} else {
|
||||
int_polarity_reg &= ~(1 << pin_num);
|
||||
}
|
||||
|
||||
// set the polarity register
|
||||
adi_gpio_SetGroupInterruptPolarity((ADI_GPIO_PORT)port, int_polarity_reg);
|
||||
|
||||
channel_ids[port][pin_num].event = event;
|
||||
|
||||
// enable interrupt for this pin if enable flag is set
|
||||
if (enable) {
|
||||
gpio_irq_enable(obj);
|
||||
} else {
|
||||
gpio_irq_disable(obj);
|
||||
}
|
||||
}
|
||||
|
||||
/** Enable GPIO IRQ
|
||||
*
|
||||
* This is target dependent, as it might enable the entire port or just a pin
|
||||
* @param obj The GPIO object
|
||||
*/
|
||||
void gpio_irq_enable(gpio_irq_t *obj)
|
||||
{
|
||||
uint32_t port = obj->pinname >> GPIO_PORT_SHIFT;
|
||||
uint32_t pin_num = obj->pinname & 0xFF;
|
||||
|
||||
if (channel_ids[port][pin_num].event == IRQ_NONE) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Group all RISE interrupts in INTA and FALL interrupts in INTB
|
||||
if (channel_ids[port][pin_num].event == IRQ_RISE) {
|
||||
// set the callback routine
|
||||
adi_gpio_RegisterCallback(SYS_GPIO_INTA_IRQn, gpio_irq_callback, obj);
|
||||
enable_pin_interrupt((ADI_GPIO_PORT)port, pin_num, SYS_GPIO_INTA_IRQn);
|
||||
} else if (channel_ids[port][pin_num].event == IRQ_FALL) {
|
||||
// set the callback routine
|
||||
adi_gpio_RegisterCallback(SYS_GPIO_INTB_IRQn, gpio_irq_callback, obj);
|
||||
enable_pin_interrupt((ADI_GPIO_PORT)port, pin_num, SYS_GPIO_INTB_IRQn);
|
||||
}
|
||||
|
||||
channel_ids[port][pin_num].int_enable = 1;
|
||||
}
|
||||
|
||||
/** Disable GPIO IRQ
|
||||
*
|
||||
* This is target dependent, as it might disable the entire port or just a pin
|
||||
* @param obj The GPIO object
|
||||
*/
|
||||
void gpio_irq_disable(gpio_irq_t *obj)
|
||||
{
|
||||
uint32_t port = obj->pinname >> GPIO_PORT_SHIFT;
|
||||
uint32_t pin_num = obj->pinname & 0xFF;
|
||||
|
||||
if (channel_ids[port][pin_num].event == IRQ_NONE) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Group all RISE interrupts in INTA and FALL interrupts in INTB
|
||||
if (channel_ids[port][pin_num].event == IRQ_RISE) {
|
||||
disable_pin_interrupt((ADI_GPIO_PORT)port, pin_num);
|
||||
}
|
||||
else if (channel_ids[port][pin_num].event == IRQ_FALL) {
|
||||
disable_pin_interrupt((ADI_GPIO_PORT)port, pin_num);
|
||||
}
|
||||
|
||||
channel_ids[port][pin_num].int_enable = 0;
|
||||
}
|
||||
|
||||
#endif // #if DEVICE_INTERRUPTIN
|
||||
@@ -0,0 +1,61 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2010-2017 Analog Devices, Inc.
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* - 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.
|
||||
* - Modified versions of the software must be conspicuously marked as such.
|
||||
* - This software is licensed solely and exclusively for use with processors
|
||||
* manufactured by or for Analog Devices, Inc.
|
||||
* - 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.
|
||||
* - 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.
|
||||
* - 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.
|
||||
*
|
||||
* 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.
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef MBED_GPIO_OBJECT_H
|
||||
#define MBED_GPIO_OBJECT_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
PinName pin;
|
||||
} gpio_t;
|
||||
|
||||
static inline int gpio_is_connected(const gpio_t *obj)
|
||||
{
|
||||
return obj->pin != (PinName)NC;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,244 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2010-2017 Analog Devices, Inc.
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* - 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.
|
||||
* - Modified versions of the software must be conspicuously marked as such.
|
||||
* - This software is licensed solely and exclusively for use with processors
|
||||
* manufactured by or for Analog Devices, Inc.
|
||||
* - 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.
|
||||
* - 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.
|
||||
* - 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.
|
||||
*
|
||||
* 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.
|
||||
******************************************************************************/
|
||||
|
||||
#include "mbed_assert.h"
|
||||
#include "i2c_api.h"
|
||||
|
||||
#if DEVICE_I2C
|
||||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "mbed_error.h"
|
||||
#include "PeripheralPins.h"
|
||||
#include "drivers/i2c/adi_i2c.h"
|
||||
|
||||
|
||||
|
||||
#if defined(BUILD_I2C_MI_DYNAMIC)
|
||||
#if defined(ADI_DEBUG)
|
||||
#warning "BUILD_I2C_MI_DYNAMIC is defined. Memory allocation for I2C will be dynamic"
|
||||
int adi_i2c_memtype = 0;
|
||||
#endif
|
||||
#else
|
||||
/*******************************************************************************
|
||||
ADI_I2C_DEV_DATA_TYPE Instance memory containing memory pointer should
|
||||
guarantee 4 byte alignmnet.
|
||||
*******************************************************************************/
|
||||
static uint32_t i2c_Mem[(ADI_I2C_MEMORY_SIZE + 3)/4];
|
||||
static ADI_I2C_HANDLE i2c_Handle;
|
||||
#if defined(ADI_DEBUG)
|
||||
#warning "BUILD_I2C_MI_DYNAMIC is NOT defined. Memory allocation for I2C will be static"
|
||||
int adi_i2c_memtype = 1;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
void i2c_init(i2c_t *obj, PinName sda, PinName scl)
|
||||
{
|
||||
uint32_t i2c_sda = pinmap_peripheral(sda, PinMap_I2C_SDA);
|
||||
uint32_t i2c_scl = pinmap_peripheral(scl, PinMap_I2C_SCL);
|
||||
ADI_I2C_HANDLE *pI2C_Handle;
|
||||
uint32_t *I2C_Mem;
|
||||
ADI_I2C_RESULT I2C_Return = ADI_I2C_SUCCESS;
|
||||
uint32_t I2C_DevNum = I2C_0; /* ADuCM3029 only has 1 I2C port */
|
||||
|
||||
|
||||
#if defined(BUILD_I2C_MI_DYNAMIC)
|
||||
I2C_DevNum = I2C_0;
|
||||
pI2C_Handle = &obj->I2C_Handle;
|
||||
obj->pI2C_Handle = pI2C_Handle;
|
||||
I2C_Mem = obj->I2C_Mem;
|
||||
#else
|
||||
I2C_DevNum = I2C_0;
|
||||
pI2C_Handle = &i2c_Handle;
|
||||
obj->pI2C_Handle = pI2C_Handle;
|
||||
I2C_Mem = &i2c_Mem[0];
|
||||
#endif
|
||||
|
||||
|
||||
obj->instance = pinmap_merge(i2c_sda, i2c_scl);
|
||||
MBED_ASSERT((int)obj->instance != NC);
|
||||
pinmap_pinout(sda, PinMap_I2C_SDA);
|
||||
pinmap_pinout(scl, PinMap_I2C_SCL);
|
||||
SystemCoreClockUpdate();
|
||||
I2C_Return = adi_i2c_Open(I2C_DevNum, I2C_Mem, ADI_I2C_MEMORY_SIZE, pI2C_Handle);
|
||||
if (I2C_Return) {
|
||||
obj->error = I2C_EVENT_ERROR;
|
||||
return;
|
||||
}
|
||||
I2C_Return = adi_i2c_Reset(*pI2C_Handle);
|
||||
if (I2C_Return) {
|
||||
obj->error = I2C_EVENT_ERROR;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int i2c_start(i2c_t *obj)
|
||||
{
|
||||
/* The Hardware does not support this feature. */
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
int i2c_stop(i2c_t *obj)
|
||||
{
|
||||
/* The Hardware does not support this feature. */
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
void i2c_frequency(i2c_t *obj, int hz)
|
||||
{
|
||||
ADI_I2C_HANDLE I2C_Handle;
|
||||
ADI_I2C_RESULT I2C_Return = ADI_I2C_SUCCESS;
|
||||
|
||||
|
||||
I2C_Handle = *obj->pI2C_Handle;
|
||||
I2C_Return = adi_i2c_SetBitRate(I2C_Handle, (uint32_t) hz);
|
||||
if (I2C_Return) {
|
||||
obj->error = I2C_EVENT_ERROR;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
|
||||
{
|
||||
ADI_I2C_RESULT I2C_Return;
|
||||
ADI_I2C_TRANSACTION I2C_inst;
|
||||
uint8_t I2C_PrologueData = 0x00;
|
||||
uint32_t I2C_Errors; /* HW Error result */
|
||||
ADI_I2C_HANDLE I2C_Handle;
|
||||
|
||||
|
||||
I2C_Handle = *obj->pI2C_Handle;
|
||||
I2C_Return = adi_i2c_SetSlaveAddress(I2C_Handle, (address & 0x0000FFFF));
|
||||
I2C_inst.pPrologue = &I2C_PrologueData;
|
||||
I2C_inst.nPrologueSize = 0;
|
||||
I2C_inst.pData = (uint8_t*) data;
|
||||
I2C_inst.nDataSize = length;
|
||||
I2C_inst.bReadNotWrite = true;
|
||||
I2C_inst.bRepeatStart = stop;
|
||||
I2C_Return = adi_i2c_ReadWrite(I2C_Handle, &I2C_inst, &I2C_Errors);
|
||||
if (I2C_Return) {
|
||||
obj->error = I2C_EVENT_ERROR;
|
||||
return -1;
|
||||
} else {
|
||||
return length;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
|
||||
{
|
||||
ADI_I2C_RESULT I2C_Return = ADI_I2C_SUCCESS;
|
||||
ADI_I2C_TRANSACTION I2C_inst;
|
||||
uint8_t I2C_PrologueData = 0x00;
|
||||
uint32_t I2C_Errors; /* HW Error result */
|
||||
ADI_I2C_HANDLE I2C_Handle;
|
||||
|
||||
|
||||
I2C_Handle = *obj->pI2C_Handle;
|
||||
I2C_Return = adi_i2c_SetSlaveAddress(I2C_Handle, (address & 0x0000FFFF));
|
||||
I2C_inst.pPrologue = &I2C_PrologueData;
|
||||
I2C_inst.nPrologueSize = 0;
|
||||
I2C_inst.pData = (uint8_t*) data;
|
||||
I2C_inst.nDataSize = length;
|
||||
I2C_inst.bReadNotWrite = false;
|
||||
I2C_inst.bRepeatStart = stop;
|
||||
I2C_Return = adi_i2c_ReadWrite(I2C_Handle, &I2C_inst, &I2C_Errors);
|
||||
if (I2C_Return) {
|
||||
obj->error = I2C_EVENT_ERROR;
|
||||
return -1;
|
||||
} else {
|
||||
return length;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void i2c_reset(i2c_t *obj)
|
||||
{
|
||||
ADI_I2C_RESULT I2C_Return;
|
||||
ADI_I2C_HANDLE I2C_Handle = *obj->pI2C_Handle;
|
||||
|
||||
I2C_Return = adi_i2c_Reset(I2C_Handle);
|
||||
if (I2C_Return) {
|
||||
obj->error = I2C_EVENT_ERROR;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int i2c_byte_read(i2c_t *obj, int last)
|
||||
{
|
||||
/* The Hardware does not support this feature. */
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
int i2c_byte_write(i2c_t *obj, int data)
|
||||
{
|
||||
/* The Hardware does not support this feature. */
|
||||
return -1;
|
||||
}
|
||||
|
||||
const PinMap *i2c_master_sda_pinmap()
|
||||
{
|
||||
return PinMap_I2C_SDA;
|
||||
}
|
||||
|
||||
const PinMap *i2c_master_scl_pinmap()
|
||||
{
|
||||
return PinMap_I2C_SCL;
|
||||
}
|
||||
|
||||
const PinMap *i2c_slave_sda_pinmap()
|
||||
{
|
||||
return PinMap_I2C_SDA;
|
||||
}
|
||||
|
||||
const PinMap *i2c_slave_scl_pinmap()
|
||||
{
|
||||
return PinMap_I2C_SCL;
|
||||
}
|
||||
|
||||
#endif // #if DEVICE_I2C
|
||||
@@ -0,0 +1,119 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2010-2017 Analog Devices, Inc.
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* - 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.
|
||||
* - Modified versions of the software must be conspicuously marked as such.
|
||||
* - This software is licensed solely and exclusively for use with processors
|
||||
* manufactured by or for Analog Devices, Inc.
|
||||
* - 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.
|
||||
* - 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.
|
||||
* - 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.
|
||||
*
|
||||
* 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.
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef MBED_OBJECTS_H
|
||||
#define MBED_OBJECTS_H
|
||||
|
||||
#include "cmsis.h"
|
||||
#include "PeripheralNames.h"
|
||||
#include "PinNames.h"
|
||||
#include "gpio_object.h"
|
||||
#include "adi_adc.h"
|
||||
#include "adi_rng.h"
|
||||
|
||||
#include "adi_i2c.h"
|
||||
#include "adi_spi.h"
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Add your custom header content here */
|
||||
struct gpio_irq_s {
|
||||
unsigned int id;
|
||||
PinName pinname;
|
||||
};
|
||||
|
||||
struct sleep_s {
|
||||
int temp;
|
||||
};
|
||||
|
||||
struct serial_s {
|
||||
int index;
|
||||
};
|
||||
|
||||
struct trng_s {
|
||||
ADI_RNG_HANDLE RNGhDevice;
|
||||
};
|
||||
|
||||
#define BUILD_I2C_MI_DYNAMIC
|
||||
struct i2c_s {
|
||||
uint32_t instance;
|
||||
uint32_t error;
|
||||
ADI_I2C_HANDLE *pI2C_Handle;
|
||||
#if defined(BUILD_I2C_MI_DYNAMIC)
|
||||
ADI_I2C_HANDLE I2C_Handle;
|
||||
/*******************************************************************************
|
||||
ADI_I2C_DEV_DATA_TYPE Instance memory containing memory pointer should
|
||||
guarantee 4 byte alignmnet.
|
||||
*******************************************************************************/
|
||||
uint32_t I2C_Mem[(ADI_I2C_MEMORY_SIZE + 3)/4];
|
||||
#endif
|
||||
};
|
||||
|
||||
#define BUILD_SPI_MI_DYNAMIC
|
||||
struct spi_s {
|
||||
uint32_t instance;
|
||||
uint32_t error;
|
||||
ADI_SPI_HANDLE *pSPI_Handle;
|
||||
#if defined(BUILD_SPI_MI_DYNAMIC)
|
||||
ADI_SPI_HANDLE SPI_Handle;
|
||||
/*******************************************************************************
|
||||
ADI_SPI_DEV_DATA_TYPE Instance memory containing memory pointer should
|
||||
guarantee 4 byte alignmnet.
|
||||
*******************************************************************************/
|
||||
uint32_t SPI_Mem[(ADI_SPI_MEMORY_SIZE + 3)/4];
|
||||
#endif
|
||||
};
|
||||
|
||||
struct analogin_s {
|
||||
ADI_ADC_HANDLE hDevice;
|
||||
ADI_ADC_BUFFER UserBuffer;
|
||||
uint8_t DelayCycles;
|
||||
uint8_t SampleCycles;
|
||||
};
|
||||
|
||||
#include "gpio_object.h"
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,105 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2010-2017 Analog Devices, Inc.
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* - 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.
|
||||
* - Modified versions of the software must be conspicuously marked as such.
|
||||
* - This software is licensed solely and exclusively for use with processors
|
||||
* manufactured by or for Analog Devices, Inc.
|
||||
* - 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.
|
||||
* - 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.
|
||||
* - 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.
|
||||
*
|
||||
* 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.
|
||||
******************************************************************************/
|
||||
|
||||
#include "mbed_assert.h"
|
||||
#include "pinmap.h"
|
||||
#include "mbed_error.h"
|
||||
|
||||
#include "PinNames.h"
|
||||
#include "adi_gpio.h"
|
||||
|
||||
void pin_function(PinName pin, int function)
|
||||
{
|
||||
// pin is composed of port and pin
|
||||
// function is the function number (the mux selection number shifted by the pin value
|
||||
// and written to pin mux register, each pin mux takes 2 bits hence multiplying by 2)
|
||||
|
||||
MBED_ASSERT(pin != (PinName)NC);
|
||||
|
||||
uint8_t port = pin >> GPIO_PORT_SHIFT;
|
||||
uint32_t cfg_reg, mask;
|
||||
volatile uint32_t *pGPIO_CFG;
|
||||
|
||||
switch (port) {
|
||||
case 0:
|
||||
pGPIO_CFG = (volatile uint32_t *)REG_GPIO0_CFG;
|
||||
break;
|
||||
case 1:
|
||||
pGPIO_CFG = (volatile uint32_t *)REG_GPIO1_CFG;
|
||||
break;
|
||||
case 2:
|
||||
pGPIO_CFG = (volatile uint32_t *)REG_GPIO2_CFG;
|
||||
break;
|
||||
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
cfg_reg = *pGPIO_CFG;
|
||||
// clear the corresponding 2 bit field first before writing the function
|
||||
// bits
|
||||
mask = ~(3 << (pin * 2));
|
||||
cfg_reg = (cfg_reg & mask) | (function << (pin*2));
|
||||
*pGPIO_CFG = cfg_reg;
|
||||
}
|
||||
|
||||
void pin_mode(PinName pin, PinMode mode)
|
||||
{
|
||||
MBED_ASSERT(pin != (PinName)NC);
|
||||
|
||||
uint8_t port = pin >> GPIO_PORT_SHIFT;
|
||||
uint32_t pin_reg_value = 2 ^ (0xFF & pin);
|
||||
|
||||
switch (mode) {
|
||||
case PullNone:
|
||||
adi_gpio_PullUpEnable((ADI_GPIO_PORT)port, (ADI_GPIO_DATA)pin_reg_value,false);
|
||||
break;
|
||||
|
||||
case PullDown:
|
||||
case PullUp:
|
||||
adi_gpio_PullUpEnable((ADI_GPIO_PORT)port, (ADI_GPIO_DATA)pin_reg_value,true);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2010-2017 Analog Devices, Inc.
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* - 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.
|
||||
* - Modified versions of the software must be conspicuously marked as such.
|
||||
* - This software is licensed solely and exclusively for use with processors
|
||||
* manufactured by or for Analog Devices, Inc.
|
||||
* - 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.
|
||||
* - 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.
|
||||
* - 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.
|
||||
*
|
||||
* 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.
|
||||
******************************************************************************/
|
||||
|
||||
#include "rtc_api.h"
|
||||
|
||||
#if DEVICE_RTC
|
||||
|
||||
#include "adi_rtc.h"
|
||||
#include "adi_pwr.h"
|
||||
|
||||
#define RTC_DEVICE_NUM 0
|
||||
/*******************************************************************************
|
||||
ADI_RTC_DEVICE Instance memory containing memory pointer should guarantee
|
||||
4 byte alignmnet.
|
||||
*******************************************************************************/
|
||||
static uint32_t aRtcDevMem0[(ADI_RTC_MEMORY_SIZE + 3)/4];
|
||||
static ADI_RTC_HANDLE hDevice0 = NULL;
|
||||
|
||||
|
||||
void rtc_init(void)
|
||||
{
|
||||
/* initialize driver */
|
||||
adi_rtc_Open(RTC_DEVICE_NUM,aRtcDevMem0,ADI_RTC_MEMORY_SIZE,&hDevice0);
|
||||
|
||||
adi_rtc_Enable(hDevice0, true);
|
||||
}
|
||||
|
||||
void rtc_free(void)
|
||||
{
|
||||
adi_rtc_Close(hDevice0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Little check routine to see if the RTC has been enabled
|
||||
* 0 = Disabled, 1 = Enabled
|
||||
*/
|
||||
int rtc_isenabled(void)
|
||||
{
|
||||
uint32_t ControlReg;
|
||||
|
||||
adi_rtc_GetControl (hDevice0, ADI_RTC_CONTROL_REGISTER_0,&ControlReg);
|
||||
|
||||
return((int) (ControlReg & BITM_RTC_CR0_CNTEN));
|
||||
}
|
||||
|
||||
time_t rtc_read(void)
|
||||
{
|
||||
time_t currentCount;
|
||||
|
||||
adi_rtc_GetCount(hDevice0, (uint32_t *)(¤tCount));
|
||||
|
||||
return(currentCount);
|
||||
}
|
||||
|
||||
void rtc_write(time_t t)
|
||||
{
|
||||
adi_rtc_SetCount (hDevice0, t);
|
||||
}
|
||||
|
||||
#endif // #if DEVICE_RTC
|
||||
@@ -0,0 +1,327 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2010-2017 Analog Devices, Inc.
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* - 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.
|
||||
* - Modified versions of the software must be conspicuously marked as such.
|
||||
* - This software is licensed solely and exclusively for use with processors
|
||||
* manufactured by or for Analog Devices, Inc.
|
||||
* - 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.
|
||||
* - 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.
|
||||
* - 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.
|
||||
*
|
||||
* 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.
|
||||
******************************************************************************/
|
||||
|
||||
#include "serial_api.h"
|
||||
|
||||
#if DEVICE_SERIAL
|
||||
|
||||
// math.h required for floating point operations for baud rate calculation
|
||||
#include <math.h>
|
||||
#include "mbed_assert.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "PeripheralPins.h"
|
||||
#include "drivers/uart/adi_uart.h"
|
||||
|
||||
#define ADI_UART_MEMORY_SIZE (ADI_UART_BIDIR_MEMORY_SIZE)
|
||||
|
||||
static ADI_UART_HANDLE hDevice;
|
||||
static uint32_t UartDeviceMem[(ADI_UART_MEMORY_SIZE + 3)/4];
|
||||
static uint32_t serial_irq_ids[2] = {0};
|
||||
static uart_irq_handler irq_handler = NULL;
|
||||
int stdio_uart_inited = 0;
|
||||
serial_t stdio_uart;
|
||||
static int rxbuffer[1];
|
||||
static int txbuffer[1];
|
||||
|
||||
static void uart_callback(void *pCBParam, uint32_t Event, void *pArg)
|
||||
{
|
||||
MBED_ASSERT(irq_handler);
|
||||
|
||||
if (Event == ADI_UART_EVENT_TX_BUFFER_PROCESSED)
|
||||
irq_handler(serial_irq_ids[0], TxIrq);
|
||||
else if (Event == ADI_UART_EVENT_RX_BUFFER_PROCESSED)
|
||||
irq_handler(serial_irq_ids[0], RxIrq);
|
||||
}
|
||||
|
||||
|
||||
void serial_free(serial_t *obj)
|
||||
{
|
||||
adi_uart_Close(hDevice);
|
||||
}
|
||||
|
||||
void serial_baud(serial_t *obj, int baudrate)
|
||||
{
|
||||
uint32_t uartdivc,uartdivm,uartdivn,uartosr;
|
||||
switch (baudrate) {
|
||||
default:
|
||||
case 9600:
|
||||
uartdivc= 22;
|
||||
uartdivm= 3;
|
||||
uartdivn= 1734;
|
||||
uartosr= 3;
|
||||
break;
|
||||
case 19200:
|
||||
uartdivc= 11;
|
||||
uartdivm= 3;
|
||||
uartdivn= 1735;
|
||||
uartosr= 3;
|
||||
break;
|
||||
case 38400:
|
||||
uartdivc= 17;
|
||||
uartdivm= 1;
|
||||
uartdivn= 0501;
|
||||
uartosr= 3;
|
||||
break;
|
||||
case 57600:
|
||||
uartdivc= 07;
|
||||
uartdivm= 2;
|
||||
uartdivn= 0031;
|
||||
uartosr= 3;
|
||||
break;
|
||||
case 115200:
|
||||
uartdivc= 07;
|
||||
uartdivm= 2;
|
||||
uartdivn= 0031;
|
||||
uartosr= 2;
|
||||
break;
|
||||
case 230400:
|
||||
uartdivc= 07;
|
||||
uartdivm= 2;
|
||||
uartdivn= 0031;
|
||||
uartosr= 1;
|
||||
break;
|
||||
case 460800:
|
||||
uartdivc= 07;
|
||||
uartdivm= 2;
|
||||
uartdivn= 0031;
|
||||
uartosr= 0;
|
||||
break;
|
||||
case 921600:
|
||||
uartdivc= 01;
|
||||
uartdivm= 1;
|
||||
uartdivn= 1563;
|
||||
uartosr= 2;
|
||||
break;
|
||||
case 1000000:
|
||||
uartdivc= 01;
|
||||
uartdivm= 1;
|
||||
uartdivn= 1280;
|
||||
uartosr= 2;
|
||||
break;
|
||||
case 1500000:
|
||||
uartdivc= 01;
|
||||
uartdivm= 2;
|
||||
uartdivn= 0341;
|
||||
uartosr= 1;
|
||||
break;
|
||||
case 3000000:
|
||||
uartdivc= 01;
|
||||
uartdivm= 2;
|
||||
uartdivn= 0341;
|
||||
uartosr= 0;
|
||||
break;
|
||||
case 4000000:
|
||||
uartdivc= 01;
|
||||
uartdivm= 1;
|
||||
uartdivn= 1280;
|
||||
uartosr= 0;
|
||||
break;
|
||||
case 5000000:
|
||||
uartdivc= 01;
|
||||
uartdivm= 1;
|
||||
uartdivn= 0614;
|
||||
uartosr= 0;
|
||||
break;
|
||||
case 6000000:
|
||||
uartdivc= 01;
|
||||
uartdivm= 1;
|
||||
uartdivn= 0171;
|
||||
uartosr= 0;
|
||||
break;
|
||||
case 6500000:
|
||||
uartdivc= 01;
|
||||
uartdivm= 1;
|
||||
uartdivn= 0000;
|
||||
uartosr= 0;
|
||||
break;
|
||||
}
|
||||
adi_uart_ConfigBaudRate(hDevice,uartdivc,uartdivm,uartdivn,uartosr);
|
||||
}
|
||||
|
||||
void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits)
|
||||
{
|
||||
int convertedparity = ADI_UART_NO_PARITY;
|
||||
int convertedstopbits = ADI_UART_ONE_STOPBIT;
|
||||
|
||||
if (stop_bits)
|
||||
convertedstopbits = ADI_UART_ONE_AND_HALF_TWO_STOPBITS;
|
||||
|
||||
if (parity == ParityOdd)
|
||||
convertedparity = ADI_UART_ODD_PARITY;
|
||||
else if (parity == ParityEven)
|
||||
convertedparity = ADI_UART_EVEN_PARITY;
|
||||
else if (parity == ParityForced1)
|
||||
convertedparity = ADI_UART_ODD_PARITY_STICKY;
|
||||
else if (parity == ParityForced0)
|
||||
convertedparity = ADI_UART_EVEN_PARITY_STICKY;
|
||||
|
||||
adi_uart_SetConfiguration(hDevice,convertedparity,convertedstopbits, (data_bits - 5));
|
||||
}
|
||||
|
||||
void serial_init(serial_t *obj, PinName tx, PinName rx)
|
||||
{
|
||||
uint32_t uart_tx = pinmap_peripheral(tx, PinMap_UART_TX);
|
||||
uint32_t uart_rx = pinmap_peripheral(rx, PinMap_UART_RX);
|
||||
|
||||
obj->index = pinmap_merge(uart_tx, uart_rx);
|
||||
MBED_ASSERT((int)obj->index != NC);
|
||||
|
||||
adi_uart_Open(0,ADI_UART_DIR_BIDIRECTION,UartDeviceMem,ADI_UART_MEMORY_SIZE,&hDevice);
|
||||
|
||||
serial_baud(obj, 9600);
|
||||
serial_format(obj, 8, ParityNone, 1);
|
||||
|
||||
pinmap_pinout(tx, PinMap_UART_TX);
|
||||
pinmap_pinout(rx, PinMap_UART_RX);
|
||||
|
||||
if (tx != NC) {
|
||||
pin_mode(tx, PullUp);
|
||||
}
|
||||
if (rx != NC) {
|
||||
pin_mode(rx, PullUp);
|
||||
}
|
||||
if (obj->index == STDIO_UART) {
|
||||
stdio_uart_inited = 1;
|
||||
memcpy(&stdio_uart, obj, sizeof(serial_t));
|
||||
}
|
||||
}
|
||||
|
||||
int serial_getc(serial_t *obj)
|
||||
{
|
||||
void *pBuff;
|
||||
uint32_t hw_error;
|
||||
|
||||
adi_uart_SubmitRxBuffer(hDevice, rxbuffer, 1, true);
|
||||
adi_uart_GetRxBuffer(hDevice, &pBuff, &hw_error);
|
||||
return rxbuffer[0];
|
||||
}
|
||||
|
||||
void serial_putc(serial_t *obj, int c)
|
||||
{
|
||||
void *pBuff;
|
||||
uint32_t hw_error;
|
||||
|
||||
txbuffer[0] = c;
|
||||
adi_uart_SubmitTxBuffer(hDevice,txbuffer, 1, true);
|
||||
adi_uart_GetTxBuffer(hDevice, &pBuff, &hw_error);
|
||||
return;
|
||||
}
|
||||
|
||||
int serial_readable(serial_t *obj)
|
||||
{
|
||||
bool bAvailable = false;
|
||||
adi_uart_IsRxBufferAvailable(hDevice, &bAvailable);
|
||||
return bAvailable;
|
||||
}
|
||||
|
||||
int serial_writable(serial_t *obj)
|
||||
{
|
||||
bool bAvailable = false;
|
||||
adi_uart_IsTxBufferAvailable(hDevice, &bAvailable);
|
||||
return bAvailable;
|
||||
}
|
||||
|
||||
void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
|
||||
{
|
||||
MBED_ASSERT(obj);
|
||||
|
||||
adi_uart_RegisterCallback(hDevice, &uart_callback, obj);
|
||||
}
|
||||
|
||||
void serial_pinout_tx(PinName tx)
|
||||
{
|
||||
pinmap_pinout(tx, PinMap_UART_TX);
|
||||
}
|
||||
|
||||
void serial_break_set(serial_t *obj)
|
||||
{
|
||||
adi_uart_ForceTxBreak(hDevice, true);
|
||||
}
|
||||
|
||||
void serial_break_clear(serial_t *obj)
|
||||
{
|
||||
adi_uart_ForceTxBreak(hDevice, false);
|
||||
}
|
||||
|
||||
void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id)
|
||||
{
|
||||
|
||||
MBED_ASSERT(obj);
|
||||
|
||||
irq_handler = handler;
|
||||
serial_irq_ids[0] = id;
|
||||
}
|
||||
|
||||
const PinMap *serial_tx_pinmap()
|
||||
{
|
||||
return PinMap_UART_TX;
|
||||
}
|
||||
|
||||
const PinMap *serial_rx_pinmap()
|
||||
{
|
||||
return PinMap_UART_RX;
|
||||
}
|
||||
|
||||
const PinMap *serial_cts_pinmap()
|
||||
{
|
||||
#if !DEVICE_SERIAL_FC
|
||||
static const PinMap PinMap_UART_CTS[] = {
|
||||
{NC, NC, 0}
|
||||
};
|
||||
#endif
|
||||
|
||||
return PinMap_UART_CTS;
|
||||
}
|
||||
|
||||
const PinMap *serial_rts_pinmap()
|
||||
{
|
||||
#if !DEVICE_SERIAL_FC
|
||||
static const PinMap PinMap_UART_RTS[] = {
|
||||
{NC, NC, 0}
|
||||
};
|
||||
#endif
|
||||
|
||||
return PinMap_UART_RTS;
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,221 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2010-2017 Analog Devices, Inc.
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* - 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.
|
||||
* - Modified versions of the software must be conspicuously marked as such.
|
||||
* - This software is licensed solely and exclusively for use with processors
|
||||
* manufactured by or for Analog Devices, Inc.
|
||||
* - 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.
|
||||
* - 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.
|
||||
* - 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.
|
||||
*
|
||||
* 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.
|
||||
******************************************************************************/
|
||||
|
||||
#include "sleep_api.h"
|
||||
|
||||
#if DEVICE_SLEEP
|
||||
|
||||
#include "adi_pwr.h"
|
||||
#include "adi_pwr_def.h"
|
||||
#include "adi_rtos_map.h"
|
||||
#include "ADuCM3029_device.h"
|
||||
#include "sleep.h"
|
||||
|
||||
/**
|
||||
* Function to put processor into sleep (FLEXI mode only).
|
||||
*/
|
||||
static void go_into_WFI(const ADI_PWR_POWER_MODE PowerMode)
|
||||
{
|
||||
uint32_t savedPriority;
|
||||
uint16_t savedWDT;
|
||||
uint32_t scrSetBits = 0u;
|
||||
uint32_t scrClrBits = 0u;
|
||||
ADI_INT_STATUS_ALLOC();
|
||||
|
||||
/* pre-calculate the sleep-on-exit set/clear bits, FLEXI mode only */
|
||||
scrSetBits |= SCB_SCR_SLEEPONEXIT_Msk;
|
||||
|
||||
/* wfi without deepsleep or sleep-on-exit */
|
||||
scrClrBits |= (uint32_t)(BITM_NVIC_INTCON0_SLEEPDEEP | BITM_NVIC_INTCON0_SLEEPONEXIT);
|
||||
|
||||
/* put all the power mode and system control mods inside a critical section */
|
||||
ADI_ENTER_CRITICAL_REGION();
|
||||
|
||||
{ /* these three lines must be in a success-checking loop if they are not inside critical section */
|
||||
/* Uninterruptable unlock sequence */
|
||||
pADI_PMG0->PWRKEY = ADI_PMG_KEY;
|
||||
|
||||
/* Clear the previous mode and set new mode */
|
||||
pADI_PMG0->PWRMOD =(uint32_t) ( ( pADI_PMG0->PWRMOD & (uint32_t) (~BITM_PMG_PWRMOD_MODE) ) | PowerMode );
|
||||
}
|
||||
|
||||
/* Update the SCR (sleepdeep and sleep-on-exit bits) */
|
||||
SCB->SCR = ((SCB->SCR | scrSetBits) & ~scrClrBits);
|
||||
|
||||
/* save current Base Priority Level */
|
||||
savedPriority = __get_BASEPRI();
|
||||
|
||||
/* NOTE: the watchdog timer (WDT) of the GlueMicro (ADuCM302x) is reset
|
||||
by the core hardware with every exit from low-power mode. Therefore,
|
||||
even though we may have disabled it during startup, it will reset
|
||||
itself on exit from every hibernation state. Therefore, to avoid
|
||||
unintended system resets every 30 seconds because of unexpected WDT
|
||||
timeouts, we save/restore the WDT control register around
|
||||
hibernation entry and exit.
|
||||
*/
|
||||
|
||||
/* save WDT control register */
|
||||
savedWDT = pADI_WDT0->CTL;
|
||||
|
||||
/* Set caller's priority threshold (left-justified) */
|
||||
__set_BASEPRI(0);
|
||||
|
||||
/* bus sync to insure register writes from interrupt handlers are always complete before WFI */
|
||||
__DSB();
|
||||
|
||||
/* Wait for interrupt */
|
||||
__WFI();
|
||||
|
||||
ADI_EXIT_CRITICAL_REGION();
|
||||
|
||||
ADI_ENTER_CRITICAL_REGION();
|
||||
|
||||
/* Restore previous base priority */
|
||||
__set_BASEPRI(savedPriority);
|
||||
|
||||
/* restore WDT control register */
|
||||
pADI_WDT0->CTL = savedWDT;
|
||||
|
||||
/* clear sleep-on-exit bit to avoid sleeping on exception return to thread level */
|
||||
SCB->SCR &= ~SCB_SCR_SLEEPONEXIT_Msk;
|
||||
|
||||
__DSB(); /* bus sync before re-enabling interrupts */
|
||||
|
||||
ADI_EXIT_CRITICAL_REGION();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Function to enable/disable clock gating for the available clocks.
|
||||
* PCLK overrides all the other clocks.
|
||||
*/
|
||||
void set_clock_gating(peripheral_clk_t eClk, int enable)
|
||||
{
|
||||
uint32_t flag;
|
||||
|
||||
switch (eClk) {
|
||||
case PCLK:
|
||||
flag = 1 << BITP_CLKG_CLK_CTL5_PERCLKOFF;
|
||||
break;
|
||||
case I2C_CLOCK:
|
||||
flag = 1 << BITP_CLKG_CLK_CTL5_UCLKI2COFF;
|
||||
break;
|
||||
case GPIO_CLOCK:
|
||||
flag = 1 << BITP_CLKG_CLK_CTL5_GPIOCLKOFF;
|
||||
break;
|
||||
case GPT0_CLOCK:
|
||||
flag = 1 << BITP_CLKG_CLK_CTL5_GPTCLK0OFF;
|
||||
break;
|
||||
case GPT1_CLOCK:
|
||||
flag = 1 << BITP_CLKG_CLK_CTL5_GPTCLK1OFF;
|
||||
break;
|
||||
case GPT2_CLOCK:
|
||||
flag = 1 << BITP_CLKG_CLK_CTL5_GPTCLK2OFF;
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
// if enable, set the bit otherwise clear the bit
|
||||
if (enable) {
|
||||
pADI_CLKG0_CLK->CTL5 |= flag;
|
||||
} else {
|
||||
pADI_CLKG0_CLK->CTL5 &= (~flag);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** Send the microcontroller to sleep
|
||||
*
|
||||
* The processor is setup ready for sleep, and sent to sleep using __WFI(). In this mode, the
|
||||
* system clock to the core is stopped until a reset or an interrupt occurs. This eliminates
|
||||
* dynamic power used by the processor, memory systems and buses. The processor, peripheral and
|
||||
* memory state are maintained, and the peripherals continue to work and can generate interrupts.
|
||||
*
|
||||
* The processor can be woken up by any internal peripheral interrupt or external pin interrupt.
|
||||
*
|
||||
* @note
|
||||
* The mbed interface semihosting is disconnected as part of going to sleep, and can not be restored.
|
||||
* Flash re-programming and the USB serial port will remain active, but the mbed program will no longer be
|
||||
* able to access the LocalFileSystem
|
||||
*
|
||||
* This mode puts the processor into FLEXI mode however the peripheral clocks are not gated
|
||||
* hence they are still active.
|
||||
*/
|
||||
void hal_sleep(void)
|
||||
{
|
||||
// set to go into the FLEXI mode where the processor is asleep and all peripherals are
|
||||
// still active
|
||||
go_into_WFI(ADI_PWR_MODE_FLEXI);
|
||||
}
|
||||
|
||||
|
||||
/** Send the microcontroller to deep sleep
|
||||
*
|
||||
* This processor is setup ready for deep sleep, and sent to sleep using __WFI(). This mode
|
||||
* has the same sleep features as sleep plus it powers down peripherals and clocks. All state
|
||||
* is still maintained.
|
||||
*
|
||||
* The processor can only be woken up by an external interrupt on a pin or a watchdog timer.
|
||||
*
|
||||
* @note
|
||||
* The mbed interface semihosting is disconnected as part of going to sleep, and can not be restored.
|
||||
* Flash re-programming and the USB serial port will remain active, but the mbed program will no longer be
|
||||
* able to access the LocalFileSystem
|
||||
*
|
||||
* This mode puts the processor into FLEXI mode and all the peripheral clocks are clock gated
|
||||
* hence they are inactive until interrupts are generated in which case the processor is awaken
|
||||
* from sleep.
|
||||
*/
|
||||
void hal_deepsleep(void)
|
||||
{
|
||||
// set clock gating to all the peripheral clocks
|
||||
set_clock_gating(PCLK, 1);
|
||||
|
||||
// set to go into the FLEXI mode with peripheral clocks gated.
|
||||
go_into_WFI(ADI_PWR_MODE_FLEXI);
|
||||
|
||||
// when exiting, clear all peripheral clock gating bits. This is done to enable clocks that aren't
|
||||
// automatically re-enabled out of sleep such as the GPIO clock.
|
||||
pADI_CLKG0_CLK->CTL5 = 0;
|
||||
}
|
||||
|
||||
#endif // #if DEVICE_SLEEP
|
||||
@@ -0,0 +1,68 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2010-2017 Analog Devices, Inc.
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* - 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.
|
||||
* - Modified versions of the software must be conspicuously marked as such.
|
||||
* - This software is licensed solely and exclusively for use with processors
|
||||
* manufactured by or for Analog Devices, Inc.
|
||||
* - 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.
|
||||
* - 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.
|
||||
* - 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.
|
||||
*
|
||||
* 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.
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef __SLEEP__H__
|
||||
#define __SLEEP__H__
|
||||
|
||||
|
||||
/* Enumeration to specify peripheral clock types:
|
||||
General purpose timer clocks 0-2,
|
||||
I2C clock,
|
||||
GPIO clock,
|
||||
RGB timer clock.
|
||||
Peripheral clock (PCLK) controls all the peripheral clocks, including
|
||||
all the clocks mentioned previously
|
||||
*/
|
||||
typedef enum {
|
||||
GPT0_CLOCK = 0,
|
||||
GPT1_CLOCK,
|
||||
GPT2_CLOCK,
|
||||
I2C_CLOCK,
|
||||
GPIO_CLOCK,
|
||||
PCLK
|
||||
} peripheral_clk_t;
|
||||
|
||||
|
||||
/* Function to enable/disable clock gating for the available clocks.
|
||||
PCLK overrides all the other clocks.
|
||||
*/
|
||||
void set_clock_gating(peripheral_clk_t eClk, int enable);
|
||||
|
||||
#endif // #ifndef __SLEEP_H__
|
||||
@@ -0,0 +1,410 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2010-2017 Analog Devices, Inc.
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* - 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.
|
||||
* - Modified versions of the software must be conspicuously marked as such.
|
||||
* - This software is licensed solely and exclusively for use with processors
|
||||
* manufactured by or for Analog Devices, Inc.
|
||||
* - 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.
|
||||
* - 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.
|
||||
* - 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.
|
||||
*
|
||||
* 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.
|
||||
******************************************************************************/
|
||||
|
||||
#include <math.h>
|
||||
#include "mbed_assert.h"
|
||||
|
||||
#include "spi_api.h"
|
||||
|
||||
#if DEVICE_SPI
|
||||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "mbed_error.h"
|
||||
#include "PeripheralPins.h"
|
||||
#include "drivers/spi/adi_spi.h"
|
||||
|
||||
|
||||
|
||||
#if defined(BUILD_SPI_MI_DYNAMIC)
|
||||
#if defined(ADI_DEBUG)
|
||||
#warning "BUILD_SPI_MI_DYNAMIC is defined. Memory allocation for SPI will be dynamic"
|
||||
int adi_spi_memtype = 0;
|
||||
#endif
|
||||
#else
|
||||
/*******************************************************************************
|
||||
ADI_SPI_DEV_DATA_TYPE Instance memory containing memory pointer should
|
||||
guarantee 4 byte alignmnet.
|
||||
*******************************************************************************/
|
||||
ADI_SPI_HANDLE spi_Handle0;
|
||||
uint32_t spi_Mem0[(ADI_SPI_MEMORY_SIZE + 3)/4];
|
||||
ADI_SPI_HANDLE spi_Handle1;
|
||||
uint32_t spi_Mem1[(ADI_SPI_MEMORY_SIZE + 3)/4];
|
||||
ADI_SPI_HANDLE spi_Handle2;
|
||||
uint32_t spi_Mem2[(ADI_SPI_MEMORY_SIZE + 3)/4];
|
||||
#if defined(ADI_DEBUG)
|
||||
#warning "BUILD_SPI_MI_DYNAMIC is NOT defined. Memory allocation for SPI will be static"
|
||||
int adi_spi_memtype = 1;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/** Initialize the SPI peripheral
|
||||
*
|
||||
* Configures the pins used by SPI, sets a default format and frequency, and enables the peripheral
|
||||
* @param[out] obj The SPI object to initialize
|
||||
* @param[in] mosi The pin to use for MOSI
|
||||
* @param[in] miso The pin to use for MISO
|
||||
* @param[in] sclk The pin to use for SCLK
|
||||
* @param[in] ssel The pin to use for SSEL
|
||||
*/
|
||||
void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel)
|
||||
{
|
||||
// determine the SPI to use
|
||||
uint32_t spi_mosi = pinmap_peripheral(mosi, PinMap_SPI_MOSI);
|
||||
uint32_t spi_miso = pinmap_peripheral(miso, PinMap_SPI_MISO);
|
||||
uint32_t spi_sclk = pinmap_peripheral(sclk, PinMap_SPI_SCLK);
|
||||
uint32_t spi_ssel = pinmap_peripheral(ssel, PinMap_SPI_SSEL);
|
||||
uint32_t spi_data = pinmap_merge(spi_mosi, spi_miso);
|
||||
uint32_t spi_cntl = pinmap_merge(spi_sclk, spi_ssel);
|
||||
ADI_SPI_HANDLE *pSPI_Handle;
|
||||
uint32_t *SPI_Mem;
|
||||
ADI_SPI_RESULT SPI_Return = ADI_SPI_SUCCESS;
|
||||
uint32_t nDeviceNum = 0;
|
||||
ADI_SPI_CHIP_SELECT spi_cs = ADI_SPI_CS_NONE;
|
||||
|
||||
|
||||
#if defined(BUILD_SPI_MI_DYNAMIC)
|
||||
if (mosi == SPI0_MOSI) {
|
||||
nDeviceNum = SPI_0;
|
||||
} else if (mosi == SPI1_MOSI) {
|
||||
nDeviceNum = SPI_1;
|
||||
} else if (mosi == SPI2_MOSI) {
|
||||
nDeviceNum = SPI_2;
|
||||
}
|
||||
pSPI_Handle = &obj->SPI_Handle;
|
||||
obj->pSPI_Handle = pSPI_Handle;
|
||||
SPI_Mem = obj->SPI_Mem;
|
||||
#else
|
||||
if (mosi == SPI0_MOSI) {
|
||||
nDeviceNum = SPI_0;
|
||||
pSPI_Handle = &spi_Handle0;
|
||||
SPI_Mem = &spi_Mem0[0];
|
||||
} else if (mosi == SPI1_MOSI) {
|
||||
nDeviceNum = SPI_1;
|
||||
pSPI_Handle = &spi_Handle1;
|
||||
SPI_Mem = &spi_Mem1[0];
|
||||
} else if (mosi == SPI2_MOSI) {
|
||||
nDeviceNum = SPI_2;
|
||||
pSPI_Handle = &spi_Handle2;
|
||||
SPI_Mem = &spi_Mem2[0];
|
||||
}
|
||||
obj->pSPI_Handle = pSPI_Handle;
|
||||
#endif
|
||||
|
||||
|
||||
obj->instance = pinmap_merge(spi_data, spi_cntl);
|
||||
MBED_ASSERT((int)obj->instance != NC);
|
||||
|
||||
// pin out the spi pins
|
||||
pinmap_pinout(mosi, PinMap_SPI_MOSI);
|
||||
pinmap_pinout(miso, PinMap_SPI_MISO);
|
||||
pinmap_pinout(sclk, PinMap_SPI_SCLK);
|
||||
if (ssel != NC) {
|
||||
pinmap_pinout(ssel, PinMap_SPI_SSEL);
|
||||
}
|
||||
|
||||
SystemCoreClockUpdate();
|
||||
SPI_Return = adi_spi_Open(nDeviceNum, SPI_Mem, ADI_SPI_MEMORY_SIZE, pSPI_Handle);
|
||||
if (SPI_Return) {
|
||||
obj->error = SPI_EVENT_ERROR;
|
||||
return;
|
||||
}
|
||||
|
||||
if (ssel != NC) {
|
||||
if ( (ssel == SPI0_CS0) || (ssel == SPI1_CS0) || (ssel == SPI2_CS0)) {
|
||||
spi_cs = ADI_SPI_CS0;
|
||||
} else if ( (ssel == SPI0_CS1) || (ssel == SPI1_CS1) || (ssel == SPI2_CS1)) {
|
||||
spi_cs = ADI_SPI_CS1;
|
||||
} else if ( (ssel == SPI0_CS2) || (ssel == SPI1_CS2) || (ssel == SPI2_CS2)) {
|
||||
spi_cs = ADI_SPI_CS2;
|
||||
} else if ( (ssel == SPI0_CS3) || (ssel == SPI1_CS3) || (ssel == SPI2_CS3)) {
|
||||
spi_cs = ADI_SPI_CS3;
|
||||
}
|
||||
|
||||
SPI_Return = adi_spi_SetChipSelect(*pSPI_Handle, spi_cs);
|
||||
if (SPI_Return) {
|
||||
obj->error = SPI_EVENT_ERROR;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** Release a SPI object
|
||||
*
|
||||
* TODO: spi_free is currently unimplemented
|
||||
* This will require reference counting at the C++ level to be safe
|
||||
*
|
||||
* Return the pins owned by the SPI object to their reset state
|
||||
* Disable the SPI peripheral
|
||||
* Disable the SPI clock
|
||||
* @param[in] obj The SPI object to deinitialize
|
||||
*/
|
||||
void spi_free(spi_t *obj)
|
||||
{
|
||||
ADI_SPI_HANDLE SPI_Handle;
|
||||
ADI_SPI_RESULT SPI_Return = ADI_SPI_SUCCESS;
|
||||
|
||||
SPI_Handle = *obj->pSPI_Handle;
|
||||
SPI_Return = adi_spi_Close(SPI_Handle);
|
||||
if (SPI_Return) {
|
||||
obj->error = SPI_EVENT_ERROR;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** Configure the SPI format
|
||||
*
|
||||
* Set the number of bits per frame, configure clock polarity and phase, shift order and master/slave mode.
|
||||
* The default bit order is MSB.
|
||||
* @param[in,out] obj The SPI object to configure
|
||||
* @param[in] bits The number of bits per frame
|
||||
* @param[in] mode The SPI mode (clock polarity, phase, and shift direction)
|
||||
* @param[in] slave Zero for master mode or non-zero for slave mode
|
||||
*
|
||||
** Configure the data transmission format
|
||||
*
|
||||
* @param bits Number of bits per SPI frame (4 - 16)
|
||||
* @param mode Clock polarity and phase mode (0 - 3)
|
||||
*
|
||||
* @code
|
||||
* mode | POL PHA
|
||||
* -----+--------
|
||||
* 0 | 0 0
|
||||
* 1 | 0 1
|
||||
* 2 | 1 0
|
||||
* 3 | 1 1
|
||||
* @endcode
|
||||
|
||||
bool phase;
|
||||
true : trailing-edge
|
||||
false : leading-edge
|
||||
|
||||
bool polarity;
|
||||
true : CPOL=1 (idle high) polarity
|
||||
false : CPOL=0 (idle-low) polarity
|
||||
*/
|
||||
void spi_format(spi_t *obj, int bits, int mode, int slave)
|
||||
{
|
||||
ADI_SPI_HANDLE SPI_Handle;
|
||||
ADI_SPI_RESULT SPI_Return = ADI_SPI_SUCCESS;
|
||||
bool phase;
|
||||
bool polarity;
|
||||
bool master;
|
||||
|
||||
|
||||
SPI_Handle = *obj->pSPI_Handle;
|
||||
|
||||
if ((uint32_t)mode & 0x1) {
|
||||
phase = true;
|
||||
}
|
||||
else {
|
||||
phase = false;
|
||||
}
|
||||
SPI_Return = adi_spi_SetClockPhase(SPI_Handle, phase);
|
||||
if (SPI_Return) {
|
||||
obj->error = SPI_EVENT_ERROR;
|
||||
return;
|
||||
}
|
||||
|
||||
if ((uint32_t)mode & 0x2) {
|
||||
polarity = true;
|
||||
}
|
||||
else {
|
||||
polarity = false;
|
||||
}
|
||||
SPI_Return = adi_spi_SetClockPolarity(SPI_Handle, polarity);
|
||||
if (SPI_Return) {
|
||||
obj->error = SPI_EVENT_ERROR;
|
||||
return;
|
||||
}
|
||||
|
||||
master = !((bool)slave);
|
||||
SPI_Return = adi_spi_SetMasterMode(SPI_Handle, master);
|
||||
if (SPI_Return) {
|
||||
obj->error = SPI_EVENT_ERROR;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** Set the SPI baud rate
|
||||
*
|
||||
* Actual frequency may differ from the desired frequency due to available dividers and bus clock
|
||||
* Configures the SPI peripheral's baud rate
|
||||
* @param[in,out] obj The SPI object to configure
|
||||
* @param[in] hz The baud rate in Hz
|
||||
*/
|
||||
void spi_frequency(spi_t *obj, int hz)
|
||||
{
|
||||
ADI_SPI_HANDLE SPI_Handle;
|
||||
ADI_SPI_RESULT SPI_Return = ADI_SPI_SUCCESS;
|
||||
|
||||
SPI_Handle = *obj->pSPI_Handle;
|
||||
SPI_Return = adi_spi_SetBitrate(SPI_Handle, (uint32_t) hz);
|
||||
if (SPI_Return) {
|
||||
obj->error = SPI_EVENT_ERROR;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** Write a byte out in master mode and receive a value
|
||||
*
|
||||
* @param[in] obj The SPI peripheral to use for sending
|
||||
* @param[in] value The value to send
|
||||
* @return Returns the value received during send
|
||||
*/
|
||||
int spi_master_write(spi_t *obj, int value)
|
||||
{
|
||||
ADI_SPI_TRANSCEIVER transceive;
|
||||
uint8_t TxBuf;
|
||||
uint8_t RxBuf;
|
||||
ADI_SPI_HANDLE SPI_Handle;
|
||||
ADI_SPI_RESULT SPI_Return = ADI_SPI_SUCCESS;
|
||||
|
||||
TxBuf = (uint8_t)value;
|
||||
|
||||
transceive.pReceiver = &RxBuf;
|
||||
transceive.ReceiverBytes = 1; /* link transceive data size to the remaining count */
|
||||
transceive.nRxIncrement = 1; /* auto increment buffer */
|
||||
transceive.pTransmitter = &TxBuf; /* initialize data attributes */
|
||||
transceive.TransmitterBytes = 1; /* link transceive data size to the remaining count */
|
||||
transceive.nTxIncrement = 1; /* auto increment buffer */
|
||||
|
||||
transceive.bDMA = false;
|
||||
transceive.bRD_CTL = false;
|
||||
SPI_Handle = *obj->pSPI_Handle;
|
||||
SPI_Return = adi_spi_MasterReadWrite(SPI_Handle, &transceive);
|
||||
if (SPI_Return) {
|
||||
obj->error = SPI_EVENT_ERROR;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return((int)RxBuf);
|
||||
}
|
||||
|
||||
|
||||
/** Write a block out in master mode and receive a value
|
||||
*
|
||||
* The total number of bytes sent and recieved will be the maximum of
|
||||
* tx_length and rx_length. The bytes written will be padded with the
|
||||
* value 0xff.
|
||||
*
|
||||
* @param[in] obj The SPI peripheral to use for sending
|
||||
* @param[in] tx_buffer Pointer to the byte-array of data to write to the device
|
||||
* @param[in] tx_length Number of bytes to write, may be zero
|
||||
* @param[in] rx_buffer Pointer to the byte-array of data to read from the device
|
||||
* @param[in] rx_length Number of bytes to read, may be zero
|
||||
* @param[in] write_fill Default data transmitted while performing a read
|
||||
* @returns
|
||||
* The number of bytes written and read from the device. This is
|
||||
* maximum of tx_length and rx_length.
|
||||
*/
|
||||
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, char write_fill)
|
||||
{
|
||||
ADI_SPI_TRANSCEIVER transceive;
|
||||
ADI_SPI_HANDLE SPI_Handle;
|
||||
ADI_SPI_RESULT SPI_Return = ADI_SPI_SUCCESS;
|
||||
|
||||
transceive.pReceiver = (uint8_t*)rx_buffer;
|
||||
transceive.ReceiverBytes = rx_length; /* link transceive data size to the remaining count */
|
||||
transceive.nRxIncrement = 1; /* auto increment buffer */
|
||||
transceive.pTransmitter = (uint8_t*)tx_buffer; /* initialize data attributes */
|
||||
transceive.TransmitterBytes = tx_length; /* link transceive data size to the remaining count */
|
||||
transceive.nTxIncrement = 1; /* auto increment buffer */
|
||||
|
||||
transceive.bDMA = false;
|
||||
transceive.bRD_CTL = false;
|
||||
SPI_Handle = *obj->pSPI_Handle;
|
||||
SPI_Return = adi_spi_MasterReadWrite(SPI_Handle, &transceive);
|
||||
if (SPI_Return) {
|
||||
obj->error = SPI_EVENT_ERROR;
|
||||
return -1;
|
||||
}
|
||||
else {
|
||||
return((int)tx_length);
|
||||
}
|
||||
}
|
||||
|
||||
const PinMap *spi_master_mosi_pinmap()
|
||||
{
|
||||
return PinMap_SPI_MOSI;
|
||||
}
|
||||
|
||||
const PinMap *spi_master_miso_pinmap()
|
||||
{
|
||||
return PinMap_SPI_MISO;
|
||||
}
|
||||
|
||||
const PinMap *spi_master_clk_pinmap()
|
||||
{
|
||||
return PinMap_SPI_SCLK;
|
||||
}
|
||||
|
||||
const PinMap *spi_master_cs_pinmap()
|
||||
{
|
||||
return PinMap_SPI_SSEL;
|
||||
}
|
||||
|
||||
const PinMap *spi_slave_mosi_pinmap()
|
||||
{
|
||||
return PinMap_SPI_MOSI;
|
||||
}
|
||||
|
||||
const PinMap *spi_slave_miso_pinmap()
|
||||
{
|
||||
return PinMap_SPI_MISO;
|
||||
}
|
||||
|
||||
const PinMap *spi_slave_clk_pinmap()
|
||||
{
|
||||
return PinMap_SPI_SCLK;
|
||||
}
|
||||
|
||||
const PinMap *spi_slave_cs_pinmap()
|
||||
{
|
||||
return PinMap_SPI_SSEL;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,143 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2010-2017 Analog Devices, Inc.
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* - 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.
|
||||
* - Modified versions of the software must be conspicuously marked as such.
|
||||
* - This software is licensed solely and exclusively for use with processors
|
||||
* manufactured by or for Analog Devices, Inc.
|
||||
* - 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.
|
||||
* - 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.
|
||||
* - 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.
|
||||
*
|
||||
* 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.
|
||||
******************************************************************************/
|
||||
|
||||
#if DEVICE_TRNG
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <drivers/rng/adi_rng.h>
|
||||
#include <drivers/pwr/adi_pwr.h>
|
||||
#include "adi_rng_def.h"
|
||||
#include "cmsis.h"
|
||||
#include "trng_api.h"
|
||||
#include "objects.h"
|
||||
|
||||
// Sampling counter values
|
||||
// Prescaler: 0 - 10
|
||||
// LenReload: 0 - 4095
|
||||
#define TRNG_CNT_VAL 4095
|
||||
#define TRNG_PRESCALER 2
|
||||
|
||||
/*******************************************************************************
|
||||
RNG Device memory is the instance of ADI_RNG_DEV_DATA_TYPE that contains
|
||||
pointers and requires 4 byte alignment. The use of uint8_t may cause memory
|
||||
access fault for some compilers which are not configured to handle unaligned
|
||||
accesses to SRAM. The size in uint8_t for RngDevMem is ADI_RNG_MEMORY_SIZE.
|
||||
The size in uint32_t for RngDevMem is recalculated to be
|
||||
(ADI_RNG_MEMORY_SIZE + 3)/4.
|
||||
*******************************************************************************/
|
||||
static uint32_t RngDevMem[(ADI_RNG_MEMORY_SIZE + 3)/4];
|
||||
|
||||
void trng_init(trng_t *obj)
|
||||
{
|
||||
ADI_RNG_HANDLE RNGhDevice;
|
||||
|
||||
// Open the device
|
||||
adi_rng_Open(0,RngDevMem,sizeof(RngDevMem),&RNGhDevice);
|
||||
|
||||
// Set sample length for the H/W RN accumulator
|
||||
adi_rng_SetSampleLen(RNGhDevice, TRNG_PRESCALER, TRNG_CNT_VAL);
|
||||
|
||||
// Disable buffering - single byte generation only
|
||||
adi_rng_EnableBuffering(RNGhDevice, false);
|
||||
|
||||
// Enable the TRNG
|
||||
adi_rng_Enable(RNGhDevice, true);
|
||||
|
||||
// Save device handle
|
||||
obj->RNGhDevice = RNGhDevice;
|
||||
}
|
||||
|
||||
void trng_free(trng_t *obj)
|
||||
{
|
||||
ADI_RNG_HANDLE RNGhDevice = obj->RNGhDevice;
|
||||
|
||||
adi_rng_Enable(RNGhDevice, false);
|
||||
adi_rng_Close(RNGhDevice);
|
||||
}
|
||||
|
||||
int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length)
|
||||
{
|
||||
ADI_RNG_HANDLE RNGhDevice = obj->RNGhDevice;
|
||||
bool bRNGRdy, bStuck;
|
||||
uint32_t i;
|
||||
volatile uint32_t nRandomNum;
|
||||
ADI_RNG_RESULT result;
|
||||
ADI_RNG_DEV_TYPE *pDevice = (ADI_RNG_DEV_TYPE*)RNGhDevice;
|
||||
|
||||
for (i = 0; i < length; i++) {
|
||||
// Loop until the device has data to be read
|
||||
do {
|
||||
result = adi_rng_GetRdyStatus(RNGhDevice, &bRNGRdy);
|
||||
if (result != ADI_RNG_SUCCESS) {
|
||||
return -1;
|
||||
}
|
||||
} while (!bRNGRdy);
|
||||
|
||||
// Check the STUCK bit to make sure the oscillator output isn't stuck
|
||||
result = adi_rng_GetStuckStatus(RNGhDevice, &bStuck);
|
||||
|
||||
// If the stuck bit is set, this means there may be a problem with RNG hardware,
|
||||
// exit with an error
|
||||
if ( (result != ADI_RNG_SUCCESS) || ((result == ADI_RNG_SUCCESS) && (bStuck)) ) {
|
||||
// Clear the STUCK bit by writing a 1 to it
|
||||
pDevice->pRNG->STAT |= BITM_RNG_STAT_STUCK;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Read the RNG
|
||||
result = adi_rng_GetRngData(RNGhDevice, (uint32_t*)(&nRandomNum));
|
||||
|
||||
if (result != ADI_RNG_SUCCESS) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Save the output
|
||||
output[i] = (uint8_t)(nRandomNum & 0xFF);
|
||||
}
|
||||
|
||||
*output_length = length;
|
||||
|
||||
// Clear nRandomNum on the stack before exiting
|
||||
nRandomNum = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,365 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2010-2018 Analog Devices, Inc.
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* - 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.
|
||||
* - Modified versions of the software must be conspicuously marked as such.
|
||||
* - This software is licensed solely and exclusively for use with processors
|
||||
* manufactured by or for Analog Devices, Inc.
|
||||
* - 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.
|
||||
* - 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.
|
||||
* - 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.
|
||||
*
|
||||
* 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.
|
||||
******************************************************************************/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <drivers/tmr/adi_tmr.h>
|
||||
#include <drivers/pwr/adi_pwr.h>
|
||||
#include <drivers/gpio/adi_gpio.h>
|
||||
|
||||
#ifndef BITM_TMR_RGB_CTL_EN
|
||||
#define BITM_TMR_RGB_CTL_EN BITM_TMR_CTL_EN
|
||||
#endif
|
||||
|
||||
typedef uint32_t timestamp_t;
|
||||
|
||||
// defined in mbed_us_ticker_api.c which calls the ticker_irq_handler() routine
|
||||
// defined in mbed_ticker_api.c
|
||||
void us_ticker_irq_handler(void);
|
||||
|
||||
static int us_ticker_inited = 0;
|
||||
|
||||
static ADI_TMR_CONFIG tmrConfig;
|
||||
|
||||
static volatile uint32_t Upper_count = 0, largecnt = 0;
|
||||
|
||||
static ADI_TMR_TypeDef * adi_tmr_registers[ADI_TMR_DEVICE_NUM] = {pADI_TMR0, pADI_TMR1, pADI_TMR2};
|
||||
|
||||
#if defined(__ADUCM302x__)
|
||||
static const IRQn_Type adi_tmr_interrupt[ADI_TMR_DEVICE_NUM] = {TMR0_EVT_IRQn, TMR1_EVT_IRQn, TMR2_EVT_IRQn};
|
||||
#elif defined(__ADUCM4x50__)
|
||||
static const IRQn_Type adi_tmr_interrupt[ADI_TMR_DEVICE_NUM] = {TMR0_EVT_IRQn, TMR1_EVT_IRQn, TMR2_EVT_IRQn, TMR_RGB_EVT_IRQn};
|
||||
#else
|
||||
#error TMR is not ported for this processor
|
||||
#endif
|
||||
|
||||
|
||||
/*---------------------------------------------------------------------------*
|
||||
Local functions
|
||||
*---------------------------------------------------------------------------*/
|
||||
static void GP1CallbackFunction(void *pCBParam, uint32_t Event, void * pArg)
|
||||
{
|
||||
Upper_count++;
|
||||
}
|
||||
|
||||
|
||||
static uint32_t get_current_time(void)
|
||||
{
|
||||
uint16_t tmrcnt0, tmrcnt1;
|
||||
uint32_t totaltmr0, totaltmr1;
|
||||
uint32_t uc1, tmrpend0, tmrpend1;
|
||||
|
||||
do {
|
||||
volatile uint32_t *ucptr = &Upper_count;
|
||||
|
||||
/*
|
||||
* Carefully coded to prevent race conditions. Do not make changes unless you understand all the
|
||||
* implications.
|
||||
*
|
||||
* Note this function can be called with interrupts globally disabled or enabled. It has been coded to work in both cases.
|
||||
*
|
||||
* TMR0 and TMR1 both run from the same synchronous clock. TMR0 runs at 26MHz and TMR1 runs at 26/256MHz.
|
||||
* TMR1 generates an interrupt every time it overflows its 16 bit counter. TMR0 runs faster and provides
|
||||
* the lowest 8 bits of the current time count. When TMR0 and TMR1 are combined, they provide 24 bits of
|
||||
* timer precision. i.e. (TMR0.CURCNT & 0xff) + (TMR1.CURCNT << 8)
|
||||
*
|
||||
* There are several race conditions protected against:
|
||||
* 1. TMR0 and TMR1 are both read at the same time, however, on rare occasions, one will have incremented before the other.
|
||||
* Therefore we read both timer counters, and check if the middle 8 bits match, if they don't then read the counts again
|
||||
* until they do. This ensures that one or the other counters are stable with respect to each other.
|
||||
*
|
||||
* 2. TMR1.CURCNT and Upper_count racing. Prevent this by disabling the TMR1 interrupt, which stops Upper_count increment interrupt (GP1CallbackFunction).
|
||||
* Then check pending bit of TMR1 to see if we missed Upper_count interrupt, and add it manually later.
|
||||
*
|
||||
* 3. Race between the TMR1 pend, and the TMR1.CURCNT read. Even with TMR1 interrupt disabled, the pend bit
|
||||
* may be set while TMR1.CURCNT is being read. We don't know if the pend bit matches the TMR1 state.
|
||||
* To prevent this, the pending bit is read twice, and we see if it matches; if it doesn't, loop around again.
|
||||
*
|
||||
* Note the TMR1 interrupt is enabled on each iteration of the loop to flush out any pending TMR1 interrupt,
|
||||
* thereby clearing any TMR1 pend's. This have no effect if this routine is called with interrupts globally disabled.
|
||||
*/
|
||||
|
||||
NVIC_DisableIRQ(adi_tmr_interrupt[ADI_TMR_DEVICE_GP1]); // Prevent Upper_count increment
|
||||
tmrpend0 = NVIC_GetPendingIRQ(adi_tmr_interrupt[ADI_TMR_DEVICE_GP1]);
|
||||
// Check if there is a pending interrupt for timer 1
|
||||
|
||||
__DMB(); // memory barrier: read GP0 before GP1
|
||||
|
||||
tmrcnt0 = adi_tmr_registers[ADI_TMR_DEVICE_GP0]->CURCNT; // to minimize skew, read both timers manually
|
||||
|
||||
__DMB(); // memory barrier: read GP0 before GP1
|
||||
|
||||
tmrcnt1 = adi_tmr_registers[ADI_TMR_DEVICE_GP1]->CURCNT; // read both timers manually
|
||||
|
||||
totaltmr0 = tmrcnt0; // expand to u32 bits
|
||||
totaltmr1 = tmrcnt1; // expand to u32 bits
|
||||
|
||||
tmrcnt0 &= 0xff00u;
|
||||
tmrcnt1 <<= 8;
|
||||
|
||||
__DMB();
|
||||
|
||||
uc1 = *ucptr; // Read Upper_count
|
||||
|
||||
tmrpend1 = NVIC_GetPendingIRQ(adi_tmr_interrupt[ADI_TMR_DEVICE_GP1]);
|
||||
// Check for a pending interrupt again. Only leave loop if they match
|
||||
|
||||
NVIC_EnableIRQ(adi_tmr_interrupt[ADI_TMR_DEVICE_GP1]); // enable interrupt on every loop to allow TMR1 interrupt to run
|
||||
} while ((tmrcnt0 != tmrcnt1) || (tmrpend0 != tmrpend1));
|
||||
|
||||
totaltmr1 <<= 8; // Timer1 runs 256x slower
|
||||
totaltmr1 += totaltmr0 & 0xffu; // Use last 8 bits of Timer0 as it runs faster
|
||||
// totaltmr1 now contain 24 bits of significance
|
||||
|
||||
if (tmrpend0) { // If an interrupt is pending, then increment local copy of upper count
|
||||
uc1++;
|
||||
}
|
||||
|
||||
uint64_t Uc = totaltmr1; // expand out to 64 bits unsigned
|
||||
Uc += ((uint64_t) uc1) << 24; // Add on the upper count to get the full precision count
|
||||
|
||||
// Divide Uc by 26 (26MHz converted to 1MHz) todo scale for other clock freqs
|
||||
|
||||
Uc *= 1290555u; // Divide total(1/26) << 25
|
||||
Uc >>= 25; // shift back. Fixed point avoid use of floating point divide.
|
||||
// Compiler does this inline using shifts and adds.
|
||||
|
||||
return Uc;
|
||||
}
|
||||
|
||||
|
||||
static void calc_event_counts(uint32_t timestamp)
|
||||
{
|
||||
uint32_t calc_time, blocks, offset;
|
||||
uint64_t aa;
|
||||
|
||||
calc_time = get_current_time();
|
||||
offset = timestamp - calc_time; // offset in useconds
|
||||
|
||||
if (offset > 0xf0000000u) // if offset is a really big number, assume that timer has already expired (i.e. negative)
|
||||
offset = 0u;
|
||||
|
||||
if (offset > 10u) { // it takes 10us to user timer routine after interrupt. Offset timer to account for that.
|
||||
offset -= 10u;
|
||||
} else
|
||||
offset = 0u;
|
||||
|
||||
aa = (uint64_t) offset;
|
||||
aa *= 26u; // convert from 1MHz to 26MHz clock. todo scale for other clock freqs
|
||||
|
||||
blocks = aa >> 7;
|
||||
blocks++; // round
|
||||
|
||||
largecnt = blocks>>1; // communicate to event_timer() routine
|
||||
}
|
||||
|
||||
static void event_timer()
|
||||
{
|
||||
if (largecnt) {
|
||||
uint32_t cnt = largecnt;
|
||||
|
||||
if (cnt > 65535u) {
|
||||
cnt = 0u;
|
||||
} else {
|
||||
cnt = 65536u - cnt;
|
||||
}
|
||||
|
||||
tmrConfig.nLoad = cnt;
|
||||
tmrConfig.nAsyncLoad = cnt;
|
||||
adi_tmr_ConfigTimer(ADI_TMR_DEVICE_GP2, &tmrConfig);
|
||||
adi_tmr_Enable(ADI_TMR_DEVICE_GP2, true);
|
||||
} else {
|
||||
tmrConfig.nLoad = 65535u;
|
||||
tmrConfig.nAsyncLoad = 65535u;
|
||||
adi_tmr_ConfigTimer(ADI_TMR_DEVICE_GP2, &tmrConfig);
|
||||
adi_tmr_Enable(ADI_TMR_DEVICE_GP2, true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Interrupt routine for timer 2
|
||||
*
|
||||
* largecnt counts how many timer ticks should be counted to reach timer event.
|
||||
* Each interrupt happens every 65536 timer ticks, unless there are less than 65536 ticks to count.
|
||||
* In that case do the remaining timers ticks.
|
||||
*
|
||||
* largecnt is a global that is used to communicate between event_timer and the interrupt routine
|
||||
* On entry, largecnt will be any value larger than 0.
|
||||
*/
|
||||
static void GP2CallbackFunction(void *pCBParam, uint32_t Event, void * pArg)
|
||||
{
|
||||
if (largecnt >= 65536u) {
|
||||
largecnt -= 65536u;
|
||||
} else {
|
||||
largecnt = 0;
|
||||
}
|
||||
|
||||
if (largecnt < 65536u) {
|
||||
adi_tmr_Enable(ADI_TMR_DEVICE_GP2, false);
|
||||
if (largecnt) {
|
||||
event_timer();
|
||||
} else {
|
||||
us_ticker_irq_handler();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*---------------------------------------------------------------------------*
|
||||
us_ticker HAL APIs
|
||||
*---------------------------------------------------------------------------*/
|
||||
void us_ticker_init(void)
|
||||
{
|
||||
if (us_ticker_inited) {
|
||||
// Disable ticker interrupt on reinitialization
|
||||
adi_tmr_Enable(ADI_TMR_DEVICE_GP2, false);
|
||||
return;
|
||||
}
|
||||
|
||||
us_ticker_inited = 1;
|
||||
|
||||
/*--------------------- GP TIMER INITIALIZATION --------------------------*/
|
||||
|
||||
/* Set up GP0 callback function */
|
||||
adi_tmr_Init(ADI_TMR_DEVICE_GP0, NULL, NULL, false);
|
||||
|
||||
/* Set up GP1 callback function */
|
||||
adi_tmr_Init(ADI_TMR_DEVICE_GP1, GP1CallbackFunction, NULL, true);
|
||||
|
||||
/* Set up GP1 callback function */
|
||||
adi_tmr_Init(ADI_TMR_DEVICE_GP2, GP2CallbackFunction, NULL, true);
|
||||
|
||||
/* Configure GP0 to run at 26MHz */
|
||||
tmrConfig.bCountingUp = true;
|
||||
tmrConfig.bPeriodic = true;
|
||||
tmrConfig.ePrescaler = ADI_TMR_PRESCALER_1; // TMR0 at 26MHz
|
||||
tmrConfig.eClockSource = ADI_TMR_CLOCK_PCLK; // TMR source is PCLK (most examples use HFOSC)
|
||||
tmrConfig.nLoad = 0;
|
||||
tmrConfig.nAsyncLoad = 0;
|
||||
tmrConfig.bReloading = false;
|
||||
tmrConfig.bSyncBypass = true; // Allow x1 prescale: requires PCLK as a clk
|
||||
adi_tmr_ConfigTimer(ADI_TMR_DEVICE_GP0, &tmrConfig);
|
||||
|
||||
/* Configure GP1 to have a period 256 times longer than GP0 */
|
||||
tmrConfig.nLoad = 0;
|
||||
tmrConfig.nAsyncLoad = 0;
|
||||
tmrConfig.ePrescaler = ADI_TMR_PRESCALER_256; // TMR1 = 26MHz/256
|
||||
adi_tmr_ConfigTimer(ADI_TMR_DEVICE_GP1, &tmrConfig);
|
||||
|
||||
/* Configure GP2 for doing event counts */
|
||||
tmrConfig.bCountingUp = true;
|
||||
tmrConfig.bPeriodic = true;
|
||||
tmrConfig.ePrescaler = ADI_TMR_PRESCALER_256; // TMR2 at 26MHz/256
|
||||
tmrConfig.eClockSource = ADI_TMR_CLOCK_PCLK; // TMR source is PCLK (most examples use HFOSC)
|
||||
tmrConfig.nLoad = 0;
|
||||
tmrConfig.nAsyncLoad = 0;
|
||||
tmrConfig.bReloading = false;
|
||||
tmrConfig.bSyncBypass = true; // Allow x1 prescale
|
||||
adi_tmr_ConfigTimer(ADI_TMR_DEVICE_GP2, &tmrConfig);
|
||||
|
||||
|
||||
/*------------------------- GP TIMER ENABLE ------------------------------*/
|
||||
|
||||
/* Manually enable both timers to get them started at the same time
|
||||
*
|
||||
*/
|
||||
adi_tmr_registers[ADI_TMR_DEVICE_GP0]->CTL |= (uint16_t) BITM_TMR_RGB_CTL_EN;
|
||||
adi_tmr_registers[ADI_TMR_DEVICE_GP1]->CTL |= (uint16_t) BITM_TMR_RGB_CTL_EN;
|
||||
}
|
||||
|
||||
uint32_t us_ticker_read()
|
||||
{
|
||||
uint32_t curr_time;
|
||||
|
||||
if (!us_ticker_inited) {
|
||||
us_ticker_init();
|
||||
}
|
||||
|
||||
curr_time = get_current_time();
|
||||
|
||||
return curr_time;
|
||||
}
|
||||
|
||||
void us_ticker_disable_interrupt(void)
|
||||
{
|
||||
adi_tmr_Enable(ADI_TMR_DEVICE_GP2, false);
|
||||
}
|
||||
|
||||
void us_ticker_clear_interrupt(void)
|
||||
{
|
||||
NVIC_ClearPendingIRQ(TMR2_EVT_IRQn);
|
||||
}
|
||||
|
||||
void us_ticker_set_interrupt(timestamp_t timestamp)
|
||||
{
|
||||
// if timestamp is already past, do not set interrupt
|
||||
if ((timestamp + 10) <= us_ticker_read()) return;
|
||||
/* timestamp is when interrupt should fire.
|
||||
*
|
||||
* This MUST not be called if another timer event is currently enabled.
|
||||
*
|
||||
*/
|
||||
calc_event_counts(timestamp); // use timestamp to calculate largecnt to control number of timer interrupts
|
||||
tmrConfig.ePrescaler = ADI_TMR_PRESCALER_256; // TMR2 at 26MHz/256
|
||||
event_timer(); // uses largecnt to initiate timer interrupts
|
||||
}
|
||||
|
||||
/** Set pending interrupt that should be fired right away.
|
||||
*
|
||||
* The ticker should be initialized prior calling this function.
|
||||
*
|
||||
* This MUST not be called if another timer event is currently enabled.
|
||||
*/
|
||||
void us_ticker_fire_interrupt(void)
|
||||
{
|
||||
largecnt = 1; // set a minimal interval so interrupt fire immediately
|
||||
tmrConfig.ePrescaler = ADI_TMR_PRESCALER_1; // TMR2 at 26MHz/1
|
||||
event_timer(); // enable the timer and interrupt
|
||||
}
|
||||
|
||||
void us_ticker_free(void)
|
||||
{
|
||||
adi_tmr_Enable(ADI_TMR_DEVICE_GP2, false);
|
||||
}
|
||||
|
||||
/*
|
||||
** EOF
|
||||
*/
|
||||
Reference in New Issue
Block a user