mirror of
https://github.com/openmv/openmv.git
synced 2025-11-04 14:49:50 +08:00
222 lines
6.7 KiB
C
222 lines
6.7 KiB
C
/* Copyright (C) 2023 Alif Semiconductor - All Rights Reserved.
|
|
* Use, distribution and modification of this code is permitted under the
|
|
* terms stated in the Alif Semiconductor Software License Agreement
|
|
*
|
|
* You should have received a copy of the Alif Semiconductor Software
|
|
* License Agreement with this file. If not, please write to:
|
|
* contact@alifsemi.com, or visit: https://alifsemi.com/license
|
|
*/
|
|
/**************************************************************************//**
|
|
* @file : TSENS_Baremetal.c
|
|
* @author : Prabhakar kumar
|
|
* @email : prabhakar.kumar@alifsemi.com
|
|
* @version : V1.0.0
|
|
* @date : 21-AUG-2023
|
|
* @brief : Baremetal demo application code for ADC driver temperature sensor
|
|
* - Internal input of temperature in analog signal corresponding
|
|
* output is digital value.
|
|
* - Converted digital value are stored in user provided memory
|
|
* address.
|
|
* - That converted value is passed to the function to get the
|
|
* temperature of the board from the TSENS
|
|
* - use get_temperature (uint32_t adc_value) function
|
|
* and pass the temperature sensor value
|
|
*
|
|
* Hardware Connection:
|
|
* Common temperature sensor is internally connected to ADC12 6th channel
|
|
* of each instance.
|
|
* no hardware setup required.
|
|
* @Note : Shift the bits in the converted value to match a 12-bit format.
|
|
******************************************************************************/
|
|
|
|
/* System Includes */
|
|
#include <stdio.h>
|
|
#include "system_utils.h"
|
|
|
|
/* include for ADC Driver */
|
|
#include "Driver_ADC.h"
|
|
#include "temperature.h"
|
|
|
|
#include "se_services_port.h"
|
|
#include "RTE_Components.h"
|
|
#if defined(RTE_Compiler_IO_STDOUT)
|
|
#include "retarget_stdout.h"
|
|
#endif /* RTE_Compiler_IO_STDOUT */
|
|
|
|
/* single shot conversion scan use ARM_ADC_SINGLE_SHOT_CH_CONV*/
|
|
|
|
#define ADC_CONVERSION ARM_ADC_SINGLE_SHOT_CH_CONV
|
|
|
|
/* Instance for ADC12 */
|
|
extern ARM_DRIVER_ADC Driver_ADC122;
|
|
static ARM_DRIVER_ADC *ADCdrv = &Driver_ADC122;
|
|
|
|
#define TEMPERATURE_SENSOR ARM_ADC_CHANNEL_6
|
|
#define NUM_CHANNELS (8)
|
|
|
|
/* Demo purpose adc_sample*/
|
|
uint32_t adc_sample[NUM_CHANNELS];
|
|
|
|
volatile uint32_t num_samples = 0;
|
|
|
|
/*
|
|
* @func : void adc_conversion_callback(uint32_t event, uint8_t channel, uint32_t sample_output)
|
|
* @brief : adc conversion isr callback
|
|
* @return : NONE
|
|
*/
|
|
static void adc_conversion_callback(uint32_t event, uint8_t channel, uint32_t sample_output)
|
|
{
|
|
if (event & ARM_ADC_EVENT_CONVERSION_COMPLETE)
|
|
{
|
|
num_samples += 1;
|
|
|
|
/* Store the value for the respected channels */
|
|
adc_sample[channel] = sample_output;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @func : void adc_tsens_demo()
|
|
* @brief : adc temperature sensor demo
|
|
* - test to verify the temperature sensor of adc.
|
|
* - Internal input of temperature in analog signal corresponding
|
|
* output is digital value.
|
|
* - converted value is the allocated user memory address.
|
|
* @return : NONE
|
|
*/
|
|
void adc_tsens_demo()
|
|
{
|
|
float temp;
|
|
uint32_t service_error_code;
|
|
int32_t ret = 0;
|
|
uint32_t error_code = SERVICES_REQ_SUCCESS;
|
|
ARM_DRIVER_VERSION version;
|
|
|
|
/* Initialize the SE services */
|
|
se_services_port_init();
|
|
|
|
/* enable the 160 MHz clock */
|
|
error_code = SERVICES_clocks_enable_clock(se_services_s_handle,
|
|
/*clock_enable_t*/ CLKEN_CLK_160M,
|
|
/*bool enable */ true,
|
|
&service_error_code);
|
|
if(error_code)
|
|
{
|
|
printf("SE Error: 160 MHz clk enable = %d\n", error_code);
|
|
return;
|
|
}
|
|
|
|
printf("\t\t\n >>> ADC demo starting up!!! <<< \r\n");
|
|
|
|
version = ADCdrv->GetVersion();
|
|
printf("\r\n ADC version api:%X driver:%X...\r\n",version.api, version.drv);
|
|
|
|
/* Initialize ADC driver */
|
|
ret = ADCdrv->Initialize(adc_conversion_callback);
|
|
if (ret != ARM_DRIVER_OK){
|
|
printf("\r\n Error: ADC init failed\n");
|
|
return;
|
|
}
|
|
|
|
/* Power control ADC */
|
|
ret = ADCdrv->PowerControl(ARM_POWER_FULL);
|
|
if (ret != ARM_DRIVER_OK){
|
|
printf("\r\n Error: ADC Power up failed\n");
|
|
goto error_uninitialize;
|
|
}
|
|
|
|
/* set conversion mode */
|
|
ret = ADCdrv->Control(ARM_ADC_CONVERSION_MODE_CTRL, ADC_CONVERSION);
|
|
if (ret != ARM_DRIVER_OK){
|
|
printf("\r\n Error: ADC select conversion mode failed\n");
|
|
goto error_poweroff;
|
|
}
|
|
|
|
/* set initial channel */
|
|
ret = ADCdrv->Control(ARM_ADC_CHANNEL_INIT_VAL, TEMPERATURE_SENSOR);
|
|
if (ret != ARM_DRIVER_OK){
|
|
printf("\r\n Error: ADC channel init failed\n");
|
|
goto error_poweroff;
|
|
}
|
|
|
|
printf(">>> Allocated memory buffer Address is 0x%X <<<\n",(uint32_t)(adc_sample + TEMPERATURE_SENSOR));
|
|
/* Start ADC */
|
|
ret = ADCdrv->Start();
|
|
if (ret != ARM_DRIVER_OK){
|
|
printf("\r\n Error: ADC Start failed\n");
|
|
goto error_poweroff;
|
|
}
|
|
|
|
/* wait for timeout */
|
|
while (!(num_samples == 1));
|
|
|
|
temp = (float)get_temperature(adc_sample[TEMPERATURE_SENSOR]);
|
|
if (temp == ARM_DRIVER_ERROR){
|
|
printf("\r\n Error: Temperature is outside range\n");
|
|
goto error_poweroff;
|
|
}
|
|
else
|
|
{
|
|
printf("\n Current temp %.1f°C\n",temp);
|
|
}
|
|
|
|
/* Stop ADC */
|
|
ret = ADCdrv->Stop();
|
|
if (ret != ARM_DRIVER_OK){
|
|
printf("\r\n Error: ADC Stop failed\n");
|
|
goto error_poweroff;
|
|
}
|
|
|
|
printf("\n Temperature Reading completed \n");
|
|
printf("\n ---END--- \r\n wait forever >>> \n");
|
|
while(1);
|
|
|
|
error_poweroff:
|
|
|
|
/* Power off ADC peripheral */
|
|
ret = ADCdrv->PowerControl(ARM_POWER_OFF);
|
|
if (ret != ARM_DRIVER_OK)
|
|
{
|
|
printf("\r\n Error: ADC Power OFF failed.\r\n");
|
|
}
|
|
|
|
error_uninitialize:
|
|
|
|
/* Un-initialize ADC driver */
|
|
ret = ADCdrv->Uninitialize();
|
|
if(ret != ARM_DRIVER_OK)
|
|
{
|
|
printf("\r\n Error: ADC Uninitialize failed.\r\n");
|
|
}
|
|
/* disable the 160 MHz clock */
|
|
error_code = SERVICES_clocks_enable_clock(se_services_s_handle,
|
|
/*clock_enable_t*/ CLKEN_CLK_160M,
|
|
/*bool enable */ false,
|
|
&service_error_code);
|
|
if(error_code)
|
|
{
|
|
printf("SE Error: 160 MHz clk disable = %d\n", error_code);
|
|
return;
|
|
}
|
|
|
|
printf("\r\n ADC demo exiting...\r\n");
|
|
}
|
|
|
|
/* Define main entry point. */
|
|
int main()
|
|
{
|
|
#if defined(RTE_Compiler_IO_STDOUT_User)
|
|
int32_t ret;
|
|
ret = stdout_init();
|
|
if(ret != ARM_DRIVER_OK)
|
|
{
|
|
while(1)
|
|
{
|
|
}
|
|
}
|
|
#endif
|
|
/* Enter the demo Application. */
|
|
adc_tsens_demo();
|
|
return 0;
|
|
}
|