adc.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (C) 2023 Daniele Lacamera <root@danielinux.net>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <stdint.h>
  18. #include "adc.h"
  19. /* ADC */
  20. #define APB2_CLOCK_ER (*(volatile uint32_t *)(0x40023844))
  21. #define ADC3_APB2_CLOCK_ER_VAL (1 << 10)
  22. #define ADC3_BASE (0x40012200)
  23. #define ADC_COM_BASE (0x40012300)
  24. #define ADC_COM_CCR (*(volatile uint32_t *)(ADC_COM_BASE + 0x04))
  25. #define ADC3_SR (*(volatile uint32_t *)(ADC3_BASE + 0x00))
  26. #define ADC3_CR1 (*(volatile uint32_t *)(ADC3_BASE + 0x04))
  27. #define ADC3_CR2 (*(volatile uint32_t *)(ADC3_BASE + 0x08))
  28. #define ADC3_SMPR1 (*(volatile uint32_t *)(ADC3_BASE + 0x0c))
  29. #define ADC3_SMPR2 (*(volatile uint32_t *)(ADC3_BASE + 0x10))
  30. #define ADC3_SQR3 (*(volatile uint32_t *)(ADC3_BASE + 0x34))
  31. #define ADC3_DR (*(volatile uint32_t *)(ADC3_BASE + 0x4c))
  32. #define ADC_CR1_SCAN (1 << 8)
  33. #define ADC_CR2_EN (1 << 0)
  34. #define ADC_CR2_CONT (1 << 1)
  35. #define ADC_CR2_SWSTART (1 << 30)
  36. #define ADC_SR_EOC (1 << 1)
  37. #define ADC_SMPR_SMP_480CYC (0x7)
  38. // A0: ADC3_IN0
  39. // F6: ADC3_IN4
  40. // F8: ADC3_IN6
  41. // F9: ADC3_IN7
  42. int adc_init(void)
  43. {
  44. int i;
  45. uint32_t val;
  46. /* Enable clock */
  47. APB2_CLOCK_ER |= ADC3_APB2_CLOCK_ER_VAL;
  48. /* Power off */
  49. ADC3_CR2 &= ~(ADC_CR2_EN);
  50. /* Set common clock prescaler */
  51. ADC_COM_CCR &= ~(0x03 << 16);
  52. /* Disable scan mode */
  53. ADC3_CR1 &= ~(ADC_CR1_SCAN);
  54. /* Set one-shot (disable continuous mode) */
  55. ADC3_CR2 &= ~(ADC_CR2_CONT);
  56. /* Set sample time for all channels */
  57. val = ADC3_SMPR1;
  58. for (i = 0; i < 10; i++) {
  59. val |= ADC_SMPR_SMP_480CYC << (i * 3);
  60. ADC3_SMPR1 = val;
  61. val = ADC3_SMPR2;
  62. for (i = 10; i < 18; i++)
  63. val |= ADC_SMPR_SMP_480CYC << ((i-10) * 3);
  64. }
  65. ADC3_SMPR2 = val;
  66. ADC3_CR2 &= ~(ADC_CR2_EN);
  67. return 0;
  68. }
  69. void pot_read(uint32_t *potl, uint32_t *potr)
  70. {
  71. if (potl) {
  72. ADC3_SQR3 |= (POTL_CHANNEL);
  73. ADC3_CR2 |= ADC_CR2_EN;
  74. ADC3_CR2 |= ADC_CR2_SWSTART;
  75. while (ADC3_CR2 & ADC_CR2_SWSTART);;
  76. while ((ADC3_SR & ADC_SR_EOC) == 0);;
  77. *potl = ADC3_DR;
  78. if (*potl < 15)
  79. *potl = 0;
  80. if (*potl > 4080)
  81. *potl = 4096;
  82. ADC3_SQR3 &= ~(POTL_CHANNEL);
  83. }
  84. if (potr) {
  85. ADC3_SQR3 |= (POTR_CHANNEL);
  86. ADC3_CR2 |= ADC_CR2_EN;
  87. ADC3_CR2 |= ADC_CR2_SWSTART;
  88. while (ADC3_CR2 & ADC_CR2_SWSTART);;
  89. while ((ADC3_SR & ADC_SR_EOC) == 0);;
  90. *potr = ADC3_DR;
  91. if (*potr < 15)
  92. *potr = 0;
  93. if (*potr > 4080)
  94. *potr = 4096;
  95. ADC3_SQR3 &= ~(POTR_CHANNEL);
  96. }
  97. }
  98. void joy_read(uint32_t *joy_x, uint32_t *joy_y)
  99. {
  100. if (joy_x) {
  101. ADC3_SQR3 |= (JOYX_CHANNEL);
  102. ADC3_CR2 |= ADC_CR2_EN;
  103. ADC3_CR2 |= ADC_CR2_SWSTART;
  104. while (ADC3_CR2 & ADC_CR2_SWSTART);;
  105. while ((ADC3_SR & ADC_SR_EOC) == 0);;
  106. *joy_x = ADC3_DR;
  107. ADC3_SQR3 &= ~(JOYX_CHANNEL);
  108. }
  109. if (joy_y) {
  110. ADC3_SQR3 |= (JOYY_CHANNEL);
  111. ADC3_CR2 |= ADC_CR2_EN;
  112. ADC3_CR2 |= ADC_CR2_SWSTART;
  113. while (ADC3_CR2 & ADC_CR2_SWSTART);;
  114. while ((ADC3_SR & ADC_SR_EOC) == 0);;
  115. *joy_y = ADC3_DR;
  116. ADC3_SQR3 &= ~(JOYY_CHANNEL);
  117. }
  118. }