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.

168 lines
4.9KB

  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/plotter.h"
  33. #include "tides/ui.h"
  34. using namespace tides;
  35. using namespace stmlib;
  36. Adc adc;
  37. CvScaler cv_scaler;
  38. Dac dac;
  39. GateOutput gate_output;
  40. GateInput gate_input;
  41. Generator generator;
  42. Plotter plotter;
  43. System sys;
  44. Ui ui;
  45. // Default interrupt handlers.
  46. extern "C" {
  47. void HardFault_Handler() { while (1); }
  48. void MemManage_Handler() { while (1); }
  49. void BusFault_Handler() { while (1); }
  50. void UsageFault_Handler() { while (1); }
  51. void NMI_Handler() { }
  52. void SVC_Handler() { }
  53. void DebugMon_Handler() { }
  54. void PendSV_Handler() { }
  55. }
  56. // These counters are used to divide the 96kHz DAC update clock into:
  57. // * a 48kHz or 6kHz clock for refreshing the DAC values.
  58. // * a 6kHz clock for reading and smoothing the ADC values (at the exception
  59. // of the LEVEL CV which is read at the sample rate).
  60. static uint32_t dac_divider = 0;
  61. static uint32_t adc_divider = 0;
  62. static const bool debug_rendering = false;
  63. extern "C" {
  64. void SysTick_Handler() {
  65. if (ui.mode() == UI_MODE_FACTORY_TESTING) {
  66. ui.UpdateFactoryTestingFlags(gate_input.Read(), adc.values());
  67. }
  68. ui.Poll();
  69. }
  70. static uint32_t saw_counter = 0;
  71. void TIM1_UP_IRQHandler(void) {
  72. if (TIM_GetITStatus(TIM1, TIM_IT_Update) == RESET) {
  73. return;
  74. }
  75. TIM_ClearITPendingBit(TIM1, TIM_IT_Update);
  76. dac.Update();
  77. if (ui.mode() == UI_MODE_FACTORY_TESTING) {
  78. if (dac.ready()) {
  79. dac.Write(saw_counter >> 16, saw_counter >> 16);
  80. saw_counter += 8947848;
  81. gate_output.Write(saw_counter & 0x80000000, saw_counter & 0x80000000);
  82. }
  83. } else if (ui.mode() == UI_MODE_PAQUES) {
  84. if (dac.ready()) {
  85. plotter.Run();
  86. dac.Write(plotter.x(), plotter.y());
  87. }
  88. } else {
  89. if (dac.ready()) {
  90. ++dac_divider;
  91. if (dac_divider >= generator.clock_divider()) {
  92. dac_divider = 0;
  93. const GeneratorSample& sample = generator.Process(gate_input.Read());
  94. uint32_t uni = sample.unipolar;
  95. int32_t bi = sample.bipolar;
  96. uint32_t level = cv_scaler.level();
  97. if (ui.mode() >= UI_MODE_CALIBRATION_C2) {
  98. level = 65535; // Bypass VCA in calibration mode!
  99. }
  100. uni = uni * level >> 16;
  101. bi = -bi * level >> 16;
  102. dac.Write(uni, bi + 32768);
  103. if (!debug_rendering) {
  104. gate_output.Write(
  105. sample.flags & FLAG_END_OF_ATTACK,
  106. sample.flags & FLAG_END_OF_RELEASE);
  107. }
  108. ui.set_led_color(uni, sample.flags & FLAG_END_OF_ATTACK);
  109. }
  110. cv_scaler.ProcessSampleRate(adc.values());
  111. }
  112. ++adc_divider;
  113. if ((adc_divider & 7) == 0) {
  114. cv_scaler.ProcessControlRate(adc.values());
  115. }
  116. }
  117. }
  118. }
  119. #include "tides/easter_egg/plotter_program.h"
  120. void Init() {
  121. sys.Init(F_CPU / (48000 * 2) - 1, true);
  122. adc.Init(false);
  123. cv_scaler.Init();
  124. dac.Init();
  125. gate_output.Init();
  126. gate_input.Init();
  127. generator.Init();
  128. plotter.Init(plotter_program, sizeof(plotter_program) / sizeof(PlotInstruction));
  129. ui.Init(&generator, &cv_scaler);
  130. sys.StartTimers();
  131. }
  132. int main(void) {
  133. Init();
  134. while (1) {
  135. if (generator.writable_block()) {
  136. if (debug_rendering) {
  137. gate_output.Write(true, true);
  138. }
  139. generator.set_pitch(cv_scaler.pitch());
  140. generator.set_shape(cv_scaler.shape());
  141. generator.set_slope(cv_scaler.slope());
  142. generator.set_smoothness(cv_scaler.smoothness());
  143. generator.Process();
  144. if (debug_rendering) {
  145. gate_output.Write(false, false);
  146. }
  147. }
  148. ui.DoEvents();
  149. }
  150. }