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.

138 lines
3.6KB

  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 "clouds/cv_scaler.h"
  25. #include "clouds/drivers/codec.h"
  26. #include "clouds/drivers/debug_pin.h"
  27. #include "clouds/drivers/debug_port.h"
  28. #include "clouds/drivers/system.h"
  29. #include "clouds/drivers/version.h"
  30. #include "clouds/dsp/granular_processor.h"
  31. #include "clouds/meter.h"
  32. #include "clouds/resources.h"
  33. #include "clouds/settings.h"
  34. #include "clouds/ui.h"
  35. // #define PROFILE_INTERRUPT 1
  36. using namespace clouds;
  37. using namespace stmlib;
  38. GranularProcessor processor;
  39. Codec codec;
  40. DebugPort debug_port;
  41. CvScaler cv_scaler;
  42. Meter meter;
  43. Settings settings;
  44. Ui ui;
  45. // Pre-allocate big blocks in main memory and CCM. No malloc here.
  46. uint8_t block_mem[118784];
  47. uint8_t block_ccm[65536 - 128] __attribute__ ((section (".ccmdata")));
  48. int __errno;
  49. // Default interrupt handlers.
  50. extern "C" {
  51. void NMI_Handler() { }
  52. void HardFault_Handler() { while (1); }
  53. void MemManage_Handler() { while (1); }
  54. void BusFault_Handler() { while (1); }
  55. void UsageFault_Handler() { while (1); }
  56. void SVC_Handler() { }
  57. void DebugMon_Handler() { }
  58. void PendSV_Handler() { }
  59. }
  60. extern "C" {
  61. void SysTick_Handler() {
  62. ui.Poll();
  63. if (settings.freshly_baked()) {
  64. if (debug_port.readable()) {
  65. uint8_t command = debug_port.Read();
  66. uint8_t response = ui.HandleFactoryTestingRequest(command);
  67. debug_port.Write(response);
  68. }
  69. }
  70. }
  71. }
  72. void FillBuffer(Codec::Frame* input, Codec::Frame* output, size_t n) {
  73. #ifdef PROFILE_INTERRUPT
  74. TIC
  75. #endif // PROFILE_INTERRUPT
  76. cv_scaler.Read(processor.mutable_parameters());
  77. processor.Process((ShortFrame*)input, (ShortFrame*)output, n);
  78. meter.Process(processor.parameters().freeze ? output : input, n);
  79. #ifdef PROFILE_INTERRUPT
  80. TOC
  81. #endif // PROFILE_INTERRUPT
  82. }
  83. void Init() {
  84. System sys;
  85. Version version;
  86. sys.Init(true);
  87. version.Init();
  88. // Init granular processor.
  89. processor.Init(
  90. block_mem, sizeof(block_mem),
  91. block_ccm, sizeof(block_ccm));
  92. settings.Init();
  93. cv_scaler.Init(settings.mutable_calibration_data());
  94. meter.Init(32000);
  95. ui.Init(&settings, &cv_scaler, &processor, &meter);
  96. bool master = !version.revised();
  97. if (!codec.Init(master, 32000)) {
  98. ui.Panic();
  99. }
  100. if (!codec.Start(32, &FillBuffer)) {
  101. ui.Panic();
  102. }
  103. if (settings.freshly_baked()) {
  104. #ifdef PROFILE_INTERRUPT
  105. DebugPin::Init();
  106. #else
  107. debug_port.Init();
  108. #endif // PROFILE_INTERRUPT
  109. }
  110. sys.StartTimers();
  111. }
  112. int main(void) {
  113. Init();
  114. while (1) {
  115. ui.DoEvents();
  116. processor.Prepare();
  117. }
  118. }