| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- /*
- * app_adc.c
- *
- * Created on: Nov 10, 2023
- * Author: juraj
- */
- #include "app_adc.h"
- ADC_HandleTypeDef *adc_handle;
- nBusAppInterface_t mcu_adc_driver = {
- mcu_adc_init,
- mcu_adc_reset,
- mcu_adc_getType,
- mcu_adc_getSensorCount,
- mcu_adc_getData,
- mcu_adc_setData,
- mcu_adc_hasParam,
- mcu_adc_getParam,
- mcu_adc_setParam,
- mcu_adc_start,
- mcu_adc_stop,
- mcu_adc_read,
- //mcu_adc_store,
- };
- uint32_t mcu_adc_data[MCU_ADC_CHANNELS];
- volatile uint8_t adc_flag = 0;
- void adcCompleteCallback(ADC_HandleTypeDef *adc){
- adc_flag = 1;
- }
- nBusAppInterface_t *getMcuAdcDriver(){
- return &mcu_adc_driver;
- }
- void mcu_adc_init(void *hw_interface, void *hw_config){
- adc_handle = (ADC_HandleTypeDef*)hw_interface;
- HAL_ADC_RegisterCallback(adc_handle, HAL_ADC_CONVERSION_COMPLETE_CB_ID, adcCompleteCallback);
- HAL_ADCEx_Calibration_Start(adc_handle, ADC_SINGLE_ENDED);
- }
- void mcu_adc_reset(){
- HAL_ADC_Stop_DMA(adc_handle);
- HAL_ADCEx_Calibration_Start(adc_handle, ADC_SINGLE_ENDED);
- }
- nBus_statusType_t mcu_adc_start(){
- HAL_ADC_Start_DMA(adc_handle, (uint32_t*)&mcu_adc_data, MCU_ADC_CHANNELS);
- return STATUS_SUCCESS;
- }
- nBus_statusType_t mcu_adc_stop(){
- HAL_ADC_Stop_DMA(adc_handle);
- return STATUS_SUCCESS;
- }
- nBus_sensorType_t mcu_adc_getType(uint8_t sensor_index){
- nBus_sensorCount_t sc = mcu_adc_getSensorCount();
- if (sensor_index > sc.read_only_count)
- return TYPE_UNKNOWN;
- return TYPE_PRESSURE_GAUGE;
- }
- nBus_sensorCount_t mcu_adc_getSensorCount(){
- nBus_sensorCount_t sc = {MCU_ADC_CHANNELS, 0};
- return sc;
- }
- /**
- * @brief Return data from one or all sensors
- * @param sensor_index If is >0, return data from specified sensors.
- * If sensor_index=0, return data from all sensors.
- * @retval number of bytes in written to array *data
- * @note data format for:
- * sensor_index=0 sensor1_index[1B]|sensor1_data[2B]|sensor2_index[1B]|sensor2_data[2B]|...
- * sensor_index>0 sensor_index[1B]|sensor_data[2B]
- */
- uint8_t mcu_adc_getData(uint8_t sensor_index, uint8_t *data){
- if(adc_flag == 1){
- adc_flag = 0;
- if(sensor_index == 0) {
- for (uint8_t i=0; i<MCU_ADC_CHANNELS ;++i){
- data[i*3] = i+1;
- data[i*3+1] = ((uint8_t*)&mcu_adc_data)[i*2];
- data[i*3+2] = ((uint8_t*)&mcu_adc_data)[i*2+1];
- }
- return MCU_ADC_CHANNELS*(2+1);
- }
- data[0] = sensor_index;
- uint8_t index = 0+(sensor_index-1)/2*4;
- if (sensor_index & 1){
- data[1] = ((uint8_t*)&mcu_adc_data)[0+index];
- data[2] = ((uint8_t*)&mcu_adc_data)[1+index];
- } else {
- data[1] = ((uint8_t*)&mcu_adc_data)[2+index];
- data[2] = ((uint8_t*)&mcu_adc_data)[3+index];
- }
- return 3;
- }
- return 0;
- }
- nBus_statusType_t mcu_adc_setData(uint8_t *data, uint8_t count, uint8_t *response){
- return STATUS_FAIL;
- }
- int32_t mcu_adc_getParam(uint8_t sensor_index, nBus_param_t param){
- return 0x00;
- }
- uint8_t mcu_adc_hasParam(uint8_t sensor_index, nBus_param_t param){
- return 0;
- }
- nBus_statusType_t mcu_adc_setParam(uint8_t sensor_index, nBus_param_t param, int32_t value){
- return STATUS_NOT_SUPPORTED;
- }
- void mcu_adc_read(void){
- }
- uint8_t mcu_adc_store(void){
- return 0;
- }
|