app_adc.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. };
  22. uint32_t mcu_adc_data[MCU_ADC_CHANNELS];
  23. volatile uint8_t adc_flag = 0;
  24. void adcCompleteCallback(ADC_HandleTypeDef *adc){
  25. adc_flag = 1;
  26. }
  27. nBusAppInterface_t *getMcuAdcDriver(){
  28. return &mcu_adc_driver;
  29. }
  30. void mcu_adc_init(void *hw_interface, void *hw_config){
  31. adc_handle = (ADC_HandleTypeDef*)hw_interface;
  32. HAL_ADC_RegisterCallback(adc_handle, HAL_ADC_CONVERSION_COMPLETE_CB_ID, adcCompleteCallback);
  33. HAL_ADCEx_Calibration_Start(adc_handle, ADC_SINGLE_ENDED);
  34. }
  35. void mcu_adc_reset(){
  36. HAL_ADC_Stop_DMA(adc_handle);
  37. HAL_ADCEx_Calibration_Start(adc_handle, ADC_SINGLE_ENDED);
  38. }
  39. void mcu_adc_start(){
  40. HAL_ADC_Start_DMA(adc_handle, (uint32_t*)&mcu_adc_data, MCU_ADC_CHANNELS);
  41. }
  42. void mcu_adc_stop(){
  43. HAL_ADC_Stop_DMA(adc_handle);
  44. }
  45. nBus_sensorType_t mcu_adc_getType(uint8_t sensor_index){
  46. if (sensor_index > mcu_adc_getSensorCount())
  47. return TYPE_UNKNOWN;
  48. return TYPE_PRESSURE;
  49. }
  50. uint8_t mcu_adc_getSensorCount(){
  51. return MCU_ADC_CHANNELS;
  52. }
  53. /**
  54. * @brief Return data from one or all sensors
  55. * @param sensor_index If is >0, return data from specified sensors.
  56. * If sensor_index=0, return data from all sensors.
  57. * @retval number of bytes in written to array *data
  58. * @note data format for:
  59. * sensor_index=0 sensor1_index[1B]|sensor1_data[2B]|sensor2_index[1B]|sensor2_data[2B]|...
  60. * sensor_index>0 sensor_index[1B]|sensor_data[2B]
  61. */
  62. uint8_t mcu_adc_getData(uint8_t sensor_index, uint8_t *data){
  63. if(adc_flag == 1){
  64. adc_flag = 0;
  65. if(sensor_index == 0) {
  66. for (uint8_t i=0; i<MCU_ADC_CHANNELS ;++i){
  67. data[i*3] = i+1;
  68. data[i*3+1] = ((uint8_t*)&mcu_adc_data)[i*2];
  69. data[i*3+2] = ((uint8_t*)&mcu_adc_data)[i*2+1];
  70. }
  71. return MCU_ADC_CHANNELS*(2+1);
  72. }
  73. data[0] = sensor_index;
  74. uint8_t index = 0+(sensor_index-1)/2*4;
  75. if (sensor_index & 1){
  76. data[1] = ((uint8_t*)&mcu_adc_data)[0+index];
  77. data[2] = ((uint8_t*)&mcu_adc_data)[1+index];
  78. } else {
  79. data[1] = ((uint8_t*)&mcu_adc_data)[2+index];
  80. data[2] = ((uint8_t*)&mcu_adc_data)[3+index];
  81. }
  82. return 3;
  83. }
  84. return 0;
  85. }
  86. uint8_t mcu_adc_setData(uint8_t *data){
  87. return 0;
  88. }
  89. uint8_t mcu_adc_getParam(uint8_t sensor_index, nBus_param_t param){
  90. return 0x00;
  91. }
  92. uint8_t mcu_adc_hasParam(uint8_t sensor_index, nBus_param_t param){
  93. return 0;
  94. }
  95. nBus_param_t mcu_adc_setParam(uint8_t sensor_index, nBus_param_t param, uint8_t value){
  96. return PARAM_NONE;
  97. }