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.

300 lines
7.7KB

  1. // Copyright 2015 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 "stmlib/dsp/dsp.h"
  25. #include "stmlib/system/bootloader_utils.h"
  26. #include "stmlib/system/system_clock.h"
  27. #include "rings/drivers/adc.h"
  28. #include "rings/drivers/codec.h"
  29. #include "rings/drivers/leds.h"
  30. #include "rings/drivers/switches.h"
  31. #include "rings/drivers/system.h"
  32. #include "rings/drivers/version.h"
  33. #include "rings/meter.h"
  34. #include "stm_audio_bootloader/qpsk/packet_decoder.h"
  35. #include "stm_audio_bootloader/qpsk/demodulator.h"
  36. #include <cstring>
  37. using namespace rings;
  38. using namespace stmlib;
  39. using namespace stm_audio_bootloader;
  40. const double kSampleRate = 48000.0;
  41. const double kModulationRate = 6000.0;
  42. const double kBitRate = 12000.0;
  43. const uint32_t kStartAddress = 0x08008000;
  44. Adc adc;
  45. Codec codec;
  46. Demodulator demodulator;
  47. Leds leds;
  48. Meter meter;
  49. PacketDecoder decoder;
  50. Switches switches;
  51. int __errno;
  52. // Default interrupt handlers.
  53. extern "C" {
  54. void NMI_Handler() { }
  55. void HardFault_Handler() { while (1); }
  56. void MemManage_Handler() { while (1); }
  57. void BusFault_Handler() { while (1); }
  58. void UsageFault_Handler() { while (1); }
  59. void SVC_Handler() { }
  60. void DebugMon_Handler() { }
  61. void PendSV_Handler() { }
  62. }
  63. extern "C" {
  64. enum UiState {
  65. UI_STATE_WAITING,
  66. UI_STATE_RECEIVING,
  67. UI_STATE_ERROR,
  68. UI_STATE_WRITING
  69. };
  70. volatile bool switch_released = false;
  71. volatile UiState ui_state;
  72. volatile int32_t gain = 4096;
  73. volatile uint8_t pot_index = 0;
  74. void UpdateLeds() {
  75. switch (ui_state) {
  76. case UI_STATE_WAITING:
  77. {
  78. // Alternate orange.
  79. bool on = system_clock.milliseconds() & 128;
  80. leds.set(0, on, on);
  81. leds.set(1, !on, !on);
  82. }
  83. break;
  84. case UI_STATE_RECEIVING:
  85. {
  86. // First LED: fast green. Second LED: peak-meter.
  87. int32_t peak = meter.peak();
  88. bool on = system_clock.milliseconds() & 32;
  89. uint8_t pwm = system_clock.milliseconds() & 15;
  90. leds.set(0, false, on);
  91. if (peak < 8192) {
  92. leds.set(1, false, (peak >> 9) >= pwm);
  93. } else if (peak < 16384) {
  94. leds.set(1, ((peak - 8192) >> 9) >= pwm, true);
  95. } else if (peak < 16384 + 8192) {
  96. leds.set(1, true, ((peak - 16384 - 8192) >> 9) < pwm);
  97. } else {
  98. leds.set(1, true, false);
  99. }
  100. }
  101. break;
  102. case UI_STATE_ERROR:
  103. {
  104. // Alternate red.
  105. bool on = system_clock.milliseconds() & 64;
  106. leds.set(0, on, 0);
  107. leds.set(1, !on, 0);
  108. }
  109. break;
  110. case UI_STATE_WRITING:
  111. {
  112. // Full orange.
  113. leds.set(0, true, true);
  114. leds.set(1, true, true);
  115. }
  116. break;
  117. }
  118. leds.Write();
  119. }
  120. void SysTick_Handler() {
  121. system_clock.Tick();
  122. adc.Convert();
  123. int32_t gain_raw = adc.value(ADC_CHANNEL_POT_FREQUENCY) >> 1;
  124. gain = gain_raw * gain_raw >> 16;
  125. switches.Debounce();
  126. if (switches.released(0)) {
  127. switch_released = true;
  128. }
  129. UpdateLeds();
  130. }
  131. }
  132. size_t discard_samples = 8000;
  133. void FillBuffer(Codec::Frame* input, Codec::Frame* output, size_t n) {
  134. for (size_t i = 0; i < n; ++i) {
  135. input[i].r = Clip16(static_cast<int32_t>(input[i].r) * gain >> 12);
  136. }
  137. meter.Process(input, n);
  138. while (n--) {
  139. if (!discard_samples) {
  140. demodulator.PushSample(2048 + (input->r >> 4));
  141. } else {
  142. --discard_samples;
  143. }
  144. *output = *input;
  145. ++output;
  146. ++input;
  147. }
  148. }
  149. static size_t current_address;
  150. static uint16_t packet_index;
  151. static uint32_t kSectorBaseAddress[] = {
  152. 0x08000000,
  153. 0x08004000,
  154. 0x08008000,
  155. 0x0800C000,
  156. 0x08010000,
  157. 0x08020000,
  158. 0x08040000,
  159. 0x08060000,
  160. 0x08080000,
  161. 0x080A0000,
  162. 0x080C0000,
  163. 0x080E0000
  164. };
  165. const uint32_t kBlockSize = 16384;
  166. const uint16_t kPacketsPerBlock = kBlockSize / kPacketSize;
  167. uint8_t rx_buffer[kBlockSize];
  168. void ProgramPage(const uint8_t* data, size_t size) {
  169. FLASH_Unlock();
  170. FLASH_ClearFlag(
  171. FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
  172. FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR| FLASH_FLAG_PGSERR);
  173. for (int32_t i = 0; i < 12; ++i) {
  174. if (current_address == kSectorBaseAddress[i]) {
  175. FLASH_EraseSector(i * 8, VoltageRange_3);
  176. }
  177. }
  178. const uint32_t* words = static_cast<const uint32_t*>(
  179. static_cast<const void*>(data));
  180. for (size_t written = 0; written < size; written += 4) {
  181. FLASH_ProgramWord(current_address, *words++);
  182. current_address += 4;
  183. }
  184. }
  185. void Init() {
  186. System sys;
  187. Version version;
  188. sys.Init(false);
  189. leds.Init();
  190. meter.Init(48000);
  191. switches.Init();
  192. version.Init();
  193. if (!codec.Init(!version.revised(), 48000)) { }
  194. if (!codec.Start(24, &FillBuffer)) { }
  195. sys.StartTimers();
  196. adc.Init();
  197. }
  198. void InitializeReception() {
  199. decoder.Init(20000);
  200. demodulator.Init(
  201. kModulationRate / kSampleRate * 4294967296.0,
  202. kSampleRate / kModulationRate,
  203. 2.0 * kSampleRate / kBitRate);
  204. demodulator.SyncCarrier(true);
  205. decoder.Reset();
  206. current_address = kStartAddress;
  207. packet_index = 0;
  208. ui_state = UI_STATE_WAITING;
  209. }
  210. int main(void) {
  211. InitializeReception();
  212. Init();
  213. bool exit_updater = !switches.pressed_immediate(0);
  214. while (!exit_updater) {
  215. bool error = false;
  216. if (demodulator.state() == DEMODULATOR_STATE_OVERFLOW) {
  217. error = true;
  218. } else {
  219. demodulator.ProcessAtLeast(32);
  220. }
  221. while (demodulator.available() && !error && !exit_updater) {
  222. uint8_t symbol = demodulator.NextSymbol();
  223. PacketDecoderState state = decoder.ProcessSymbol(symbol);
  224. switch (state) {
  225. case PACKET_DECODER_STATE_OK:
  226. {
  227. ui_state = UI_STATE_RECEIVING;
  228. memcpy(
  229. rx_buffer + (packet_index % kPacketsPerBlock) * kPacketSize,
  230. decoder.packet_data(),
  231. kPacketSize);
  232. ++packet_index;
  233. if ((packet_index % kPacketsPerBlock) == 0) {
  234. ui_state = UI_STATE_WRITING;
  235. ProgramPage(rx_buffer, kBlockSize);
  236. decoder.Reset();
  237. demodulator.SyncCarrier(false);
  238. } else {
  239. decoder.Reset();
  240. demodulator.SyncDecision();
  241. }
  242. }
  243. break;
  244. case PACKET_DECODER_STATE_ERROR_SYNC:
  245. case PACKET_DECODER_STATE_ERROR_CRC:
  246. error = true;
  247. break;
  248. case PACKET_DECODER_STATE_END_OF_TRANSMISSION:
  249. exit_updater = true;
  250. break;
  251. default:
  252. break;
  253. }
  254. }
  255. if (error) {
  256. ui_state = UI_STATE_ERROR;
  257. switch_released = false;
  258. while (!switch_released); // Polled in ISR
  259. InitializeReception();
  260. }
  261. }
  262. codec.Stop();
  263. adc.DeInit();
  264. Uninitialize();
  265. JumpTo(kStartAddress);
  266. while (1) { }
  267. }