app_adc.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. void mcu_adc_start(){
  42. HAL_ADC_Start_DMA(adc_handle, (uint32_t*)&mcu_adc_data, MCU_ADC_CHANNELS);
  43. }
  44. void mcu_adc_stop(){
  45. HAL_ADC_Stop_DMA(adc_handle);
  46. }
  47. nBus_sensorType_t mcu_adc_getType(uint8_t sensor_index){
  48. if (sensor_index > mcu_adc_getSensorCount())
  49. return TYPE_UNKNOWN;
  50. return TYPE_PRESSURE;
  51. }
  52. uint8_t mcu_adc_getSensorCount(){
  53. return MCU_ADC_CHANNELS;
  54. }
  55. /**
  56. * @brief Return data from one or all sensors
  57. * @param sensor_index If is >0, return data from specified sensors.
  58. * If sensor_index=0, return data from all sensors.
  59. * @retval number of bytes in written to array *data
  60. * @note data format for:
  61. * sensor_index=0 sensor1_index[1B]|sensor1_data[2B]|sensor2_index[1B]|sensor2_data[2B]|...
  62. * sensor_index>0 sensor_index[1B]|sensor_data[2B]
  63. */
  64. uint8_t mcu_adc_getData(uint8_t sensor_index, uint8_t *data){
  65. if(adc_flag == 1){
  66. adc_flag = 0;
  67. if(sensor_index == 0) {
  68. for (uint8_t i=0; i<MCU_ADC_CHANNELS ;++i){
  69. data[i*3] = i+1;
  70. data[i*3+1] = ((uint8_t*)&mcu_adc_data)[i*2];
  71. data[i*3+2] = ((uint8_t*)&mcu_adc_data)[i*2+1];
  72. }
  73. return MCU_ADC_CHANNELS*(2+1);
  74. }
  75. data[0] = sensor_index;
  76. uint8_t index = 0+(sensor_index-1)/2*4;
  77. if (sensor_index & 1){
  78. data[1] = ((uint8_t*)&mcu_adc_data)[0+index];
  79. data[2] = ((uint8_t*)&mcu_adc_data)[1+index];
  80. } else {
  81. data[1] = ((uint8_t*)&mcu_adc_data)[2+index];
  82. data[2] = ((uint8_t*)&mcu_adc_data)[3+index];
  83. }
  84. return 3;
  85. }
  86. return 0;
  87. }
  88. uint8_t mcu_adc_setData(uint8_t *data){
  89. return 0;
  90. }
  91. int32_t mcu_adc_getParam(uint8_t sensor_index, nBus_param_t param){
  92. return 0x00;
  93. }
  94. uint8_t mcu_adc_hasParam(uint8_t sensor_index, nBus_param_t param){
  95. return 0;
  96. }
  97. nBus_param_t mcu_adc_setParam(uint8_t sensor_index, nBus_param_t param, int32_t value){
  98. return PARAM_NONE;
  99. }
  100. void mcu_adc_read(void){
  101. }
  102. uint8_t mcu_adc_store(void){
  103. return 0;
  104. }