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.

159 lines
4.1KB

  1. // Copyright 2014 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 "stmlib/utils/dsp.h"
  26. #include "stmlib/utils/ring_buffer.h"
  27. #include "stmlib/system/system_clock.h"
  28. #include "peaks/drivers/dac.h"
  29. #include "peaks/drivers/debug_pin.h"
  30. #include "peaks/drivers/gate_input.h"
  31. #include "peaks/drivers/system.h"
  32. #include "peaks/calibration_data.h"
  33. #include "peaks/io_buffer.h"
  34. #include "peaks/processors.h"
  35. #include "peaks/ui.h"
  36. using namespace peaks;
  37. using namespace std;
  38. using namespace stmlib;
  39. Adc adc;
  40. CalibrationData calibration_data;
  41. Dac dac;
  42. GateInput gate_input;
  43. IOBuffer io_buffer;
  44. Leds leds;
  45. Switches switches;
  46. System sys;
  47. Ui ui;
  48. extern "C" {
  49. void HardFault_Handler(void) { while (1); }
  50. void MemManage_Handler(void) { while (1); }
  51. void BusFault_Handler(void) { while (1); }
  52. void UsageFault_Handler(void) { while (1); }
  53. void NMI_Handler(void) { }
  54. void SVC_Handler(void) { }
  55. void DebugMon_Handler(void) { }
  56. void PendSV_Handler(void) { }
  57. }
  58. extern "C" {
  59. uint16_t counter = 0;
  60. void SysTick_Handler() {
  61. ui.Poll();
  62. }
  63. GateFlags gate_flags[2];
  64. void TIM1_UP_IRQHandler(void) {
  65. // DAC refresh at 48kHz
  66. if (!(TIM1->SR & TIM_IT_Update)) {
  67. return;
  68. }
  69. TIM1->SR = (uint16_t)~TIM_IT_Update;
  70. bool wrote_both_channels = dac.Update();
  71. if (!wrote_both_channels) {
  72. return;
  73. }
  74. uint32_t external_gate_inputs = gate_input.Read();
  75. uint32_t buttons = ui.ReadPanelGateState();
  76. uint32_t gate_inputs = external_gate_inputs | buttons;
  77. IOBuffer::Slice slice = io_buffer.NextSlice(1);
  78. for (size_t i = 0; i < kNumChannels; ++i) {
  79. gate_flags[i] = ExtractGateFlags(
  80. gate_flags[i],
  81. gate_inputs & (1 << i));
  82. dac.Write(i, slice.block->output[i][slice.frame_index]);
  83. }
  84. // A hack to make channel 1 aware of what's going on in channel 2. Used to
  85. // reset the sequencer.
  86. slice.block->input[0][slice.frame_index] = gate_flags[0] \
  87. | (gate_flags[1] << 4) \
  88. | (buttons & 1 ? GATE_FLAG_FROM_BUTTON : 0);
  89. slice.block->input[1][slice.frame_index] = gate_flags[1] \
  90. | (buttons & 2 ? GATE_FLAG_FROM_BUTTON : 0);
  91. }
  92. }
  93. int16_t output_buffer[kBlockSize];
  94. void Process(IOBuffer::Block* block, size_t size) {
  95. // DebugPin::High();
  96. ui.PollPots();
  97. for (size_t i = 0; i < kNumChannels; ++i) {
  98. if (ui.calibrating()) {
  99. fill(&output_buffer[0], &output_buffer[size], 0);
  100. } else {
  101. processors[i].Process(block->input[i], output_buffer, size);
  102. }
  103. ui.set_led_brightness(i, output_buffer[0]);
  104. for (size_t j = 0; j < size; ++j) {
  105. block->output[i][j] = calibration_data.DacCode(i, output_buffer[j]);
  106. }
  107. }
  108. // DebugPin::Low();
  109. }
  110. void Init() {
  111. sys.Init(F_CPU / 96000 - 1, true);
  112. system_clock.Init();
  113. gate_input.Init();
  114. io_buffer.Init();
  115. dac.Init();
  116. calibration_data.Init();
  117. processors[0].Init(0);
  118. processors[1].Init(1);
  119. ui.Init(&calibration_data);
  120. // DebugPin::Init();
  121. sys.StartTimers();
  122. }
  123. int main(void) {
  124. Init();
  125. while (1) {
  126. ui.DoEvents();
  127. io_buffer.Process(&Process);
  128. }
  129. }