app_adc.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * app_adc.c
  3. *
  4. * Created on: Nov 10, 2023
  5. * Author: juraj
  6. */
  7. #include "app_adc.h"
  8. ADC_HandleTypeDef *adc_handle;
  9. nBusAppInterface_t mcu_adc_driver = {
  10. mcu_adc_init,
  11. mcu_adc_reset,
  12. mcu_adc_getType,
  13. mcu_adc_getSensorCount,
  14. mcu_adc_getData,
  15. mcu_adc_setData,
  16. mcu_adc_hasParam,
  17. mcu_adc_getParam,
  18. mcu_adc_setParam,
  19. mcu_adc_start,
  20. mcu_adc_stop,
  21. mcu_adc_read,
  22. //mcu_adc_store,
  23. };
  24. uint32_t mcu_adc_data[MCU_ADC_CHANNELS];
  25. volatile uint8_t adc_flag = 0;
  26. void adcCompleteCallback(ADC_HandleTypeDef *adc){
  27. adc_flag = 1;
  28. }
  29. nBusAppInterface_t *getMcuAdcDriver(){
  30. return &mcu_adc_driver;
  31. }
  32. void mcu_adc_init(void *hw_interface, void *hw_config){
  33. adc_handle = (ADC_HandleTypeDef*)hw_interface;
  34. HAL_ADC_RegisterCallback(adc_handle, HAL_ADC_CONVERSION_COMPLETE_CB_ID, adcCompleteCallback);
  35. HAL_ADCEx_Calibration_Start(adc_handle, ADC_SINGLE_ENDED);
  36. }
  37. void mcu_adc_reset(){
  38. HAL_ADC_Stop_DMA(adc_handle);
  39. HAL_ADCEx_Calibration_Start(adc_handle, ADC_SINGLE_ENDED);
  40. }
  41. nBus_statusType_t mcu_adc_start(){
  42. HAL_ADC_Start_DMA(adc_handle, (uint32_t*)&mcu_adc_data, MCU_ADC_CHANNELS);
  43. return STATUS_SUCCESS;
  44. }
  45. nBus_statusType_t mcu_adc_stop(){
  46. HAL_ADC_Stop_DMA(adc_handle);
  47. return STATUS_SUCCESS;
  48. }
  49. nBus_sensorType_t mcu_adc_getType(uint8_t sensor_index){
  50. nBus_sensorCount_t sc = mcu_adc_getSensorCount();
  51. if (sensor_index > sc.read_only_count)
  52. return TYPE_UNKNOWN;
  53. return TYPE_PRESSURE_GAUGE;
  54. }
  55. nBus_sensorCount_t mcu_adc_getSensorCount(){
  56. nBus_sensorCount_t sc = {MCU_ADC_CHANNELS, 0};
  57. return sc;
  58. }
  59. /**
  60. * @brief Return data from one or all sensors
  61. * @param sensor_index If is >0, return data from specified sensors.
  62. * If sensor_index=0, return data from all sensors.
  63. * @retval number of bytes in written to array *data
  64. * @note data format for:
  65. * sensor_index=0 sensor1_index[1B]|sensor1_data[2B]|sensor2_index[1B]|sensor2_data[2B]|...
  66. * sensor_index>0 sensor_index[1B]|sensor_data[2B]
  67. */
  68. uint8_t mcu_adc_getData(uint8_t sensor_index, uint8_t *data){
  69. if(adc_flag == 1){
  70. adc_flag = 0;
  71. if(sensor_index == 0) {
  72. for (uint8_t i=0; i<MCU_ADC_CHANNELS ;++i){
  73. data[i*3] = i+1;
  74. data[i*3+1] = ((uint8_t*)&mcu_adc_data)[i*2];
  75. data[i*3+2] = ((uint8_t*)&mcu_adc_data)[i*2+1];
  76. }
  77. return MCU_ADC_CHANNELS*(2+1);
  78. }
  79. data[0] = sensor_index;
  80. uint8_t index = 0+(sensor_index-1)/2*4;
  81. if (sensor_index & 1){
  82. data[1] = ((uint8_t*)&mcu_adc_data)[0+index];
  83. data[2] = ((uint8_t*)&mcu_adc_data)[1+index];
  84. } else {
  85. data[1] = ((uint8_t*)&mcu_adc_data)[2+index];
  86. data[2] = ((uint8_t*)&mcu_adc_data)[3+index];
  87. }
  88. return 3;
  89. }
  90. return 0;
  91. }
  92. nBus_statusType_t mcu_adc_setData(uint8_t *data, uint8_t count, uint8_t *response){
  93. return STATUS_FAIL;
  94. }
  95. int32_t mcu_adc_getParam(uint8_t sensor_index, nBus_param_t param){
  96. return 0x00;
  97. }
  98. uint8_t mcu_adc_hasParam(uint8_t sensor_index, nBus_param_t param){
  99. return 0;
  100. }
  101. nBus_statusType_t mcu_adc_setParam(uint8_t sensor_index, nBus_param_t param, int32_t value){
  102. return STATUS_NOT_SUPPORTED;
  103. }
  104. void mcu_adc_read(void){
  105. }
  106. uint8_t mcu_adc_store(void){
  107. return 0;
  108. }