You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
5.1KB

  1. // Copyright 2013 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.gillet@gmail.com)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. // See http://creativecommons.org/licenses/MIT/ for more information.
  24. #include <stm32f10x_conf.h>
  25. #include "tides/drivers/adc.h"
  26. #include "tides/drivers/dac.h"
  27. #include "tides/drivers/gate_input.h"
  28. #include "tides/drivers/gate_output.h"
  29. #include "tides/drivers/system.h"
  30. #include "tides/cv_scaler.h"
  31. #include "tides/generator.h"
  32. #include "tides/ui.h"
  33. using namespace tides;
  34. using namespace stmlib;
  35. Adc adc;
  36. CvScaler cv_scaler;
  37. Dac dac;
  38. GateOutput gate_output;
  39. GateInput gate_input;
  40. Generator generator;
  41. System sys;
  42. Ui ui;
  43. // Default interrupt handlers.
  44. extern "C" {
  45. void HardFault_Handler() { while (1); }
  46. void MemManage_Handler() { while (1); }
  47. void BusFault_Handler() { while (1); }
  48. void UsageFault_Handler() { while (1); }
  49. void NMI_Handler() { }
  50. void SVC_Handler() { }
  51. void DebugMon_Handler() { }
  52. void PendSV_Handler() { }
  53. }
  54. // These counters are used to divide the 96kHz DAC update clock into:
  55. // * a 48kHz or 6kHz clock for refreshing the DAC values.
  56. // * a 6kHz clock for reading and smoothing the ADC values (at the exception
  57. // of the LEVEL CV which is read at the sample rate).
  58. static uint32_t dac_divider = 0;
  59. static uint32_t adc_divider = 0;
  60. static const bool debug_rendering = false;
  61. extern "C" {
  62. void SysTick_Handler() {
  63. if (ui.mode() == UI_MODE_FACTORY_TESTING) {
  64. ui.UpdateFactoryTestingFlags(gate_input.Read(), adc.values());
  65. }
  66. ui.Poll();
  67. }
  68. static uint32_t saw_counter = 0;
  69. void TIM1_UP_IRQHandler(void) {
  70. if (TIM_GetITStatus(TIM1, TIM_IT_Update) == RESET) {
  71. return;
  72. }
  73. TIM_ClearITPendingBit(TIM1, TIM_IT_Update);
  74. dac.Update();
  75. if (ui.mode() == UI_MODE_FACTORY_TESTING) {
  76. if (dac.ready()) {
  77. dac.Write(saw_counter >> 16, saw_counter >> 16);
  78. saw_counter += 8947848;
  79. gate_output.Write(saw_counter & 0x80000000, saw_counter & 0x80000000);
  80. }
  81. } else {
  82. if (dac.ready()) {
  83. ++dac_divider;
  84. if (dac_divider >= generator.clock_divider()) {
  85. dac_divider = 0;
  86. const GeneratorSample& sample = generator.Process(gate_input.Read());
  87. uint32_t uni = sample.unipolar;
  88. int32_t bi = sample.bipolar;
  89. uint32_t level = cv_scaler.level();
  90. if (ui.mode() == UI_MODE_CALIBRATION_C2 ||
  91. ui.mode() == UI_MODE_CALIBRATION_C4 ||
  92. ui.mode() == UI_MODE_FACTORY_TESTING) {
  93. level = 65535; // Bypass VCA in calibration mode!
  94. }
  95. uni = uni * level >> 16;
  96. bi = -bi * level >> 16;
  97. dac.Write(uni, bi + 32768);
  98. if (!debug_rendering) {
  99. gate_output.Write(
  100. sample.flags & FLAG_END_OF_ATTACK,
  101. sample.flags & FLAG_END_OF_RELEASE);
  102. }
  103. ui.set_led_color(uni, sample.flags & FLAG_END_OF_ATTACK);
  104. }
  105. cv_scaler.ProcessSampleRate(adc.values());
  106. }
  107. ++adc_divider;
  108. if ((adc_divider & 7) == 0) {
  109. cv_scaler.ProcessControlRate(adc.values());
  110. }
  111. }
  112. }
  113. }
  114. void Init() {
  115. sys.Init(F_CPU / (48000 * 2) - 1, true);
  116. adc.Init(false);
  117. cv_scaler.Init();
  118. dac.Init();
  119. gate_output.Init();
  120. gate_input.Init();
  121. generator.Init();
  122. ui.Init(&generator, &cv_scaler);
  123. sys.StartTimers();
  124. }
  125. int main(void) {
  126. Init();
  127. while (1) {
  128. if (generator.writable_block()) {
  129. if (debug_rendering) {
  130. gate_output.Write(true, true);
  131. }
  132. /* in harmonic mode, the range button sets the waveform, not the pitch */
  133. if (generator.feature_mode_ == Generator::FEAT_MODE_HARMONIC) {
  134. generator.set_pitch_high_range(cv_scaler.pitch(), cv_scaler.fm());
  135. } else {
  136. generator.set_pitch(cv_scaler.pitch(), cv_scaler.fm());
  137. }
  138. if (generator.feature_mode_ == Generator::FEAT_MODE_RANDOM) {
  139. generator.set_pulse_width(cv_scaler.raw_attenuverter());
  140. }
  141. generator.set_shape(cv_scaler.shape());
  142. generator.set_slope(cv_scaler.slope());
  143. generator.set_smoothness(cv_scaler.smoothness());
  144. generator.FillBuffer();
  145. if (debug_rendering) {
  146. gate_output.Write(false, false);
  147. }
  148. }
  149. ui.DoEvents();
  150. }
  151. }