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.

286 lines
7.3KB

  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 "stmlib/dsp/dsp.h"
  25. #include "stmlib/system/bootloader_utils.h"
  26. #include "stmlib/system/system_clock.h"
  27. #include "warps/drivers/adc.h"
  28. #include "warps/drivers/codec.h"
  29. #include "warps/drivers/leds.h"
  30. #include "warps/drivers/switches.h"
  31. #include "warps/drivers/system.h"
  32. #include "warps/drivers/version.h"
  33. #include "warps/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 warps;
  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. Meter meter;
  47. Leds leds;
  48. Switches switches;
  49. PacketDecoder decoder;
  50. Demodulator demodulator;
  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 int32_t gain = 4096;
  72. volatile UiState ui_state;
  73. void UpdateLeds() {
  74. leds.Clear();
  75. bool blink = system_clock.milliseconds() & 128;
  76. int32_t color = meter.peak();
  77. color = color < 8192 ? (color - 8192) << 2 : ((color - 8192) * 341 >> 8);
  78. if (color < 0) {
  79. leds.set_main(0, (color + 32768) >> 7, 0);
  80. } else if (color < 16384) {
  81. leds.set_main(color >> 6, 255, 0);
  82. } else {
  83. leds.set_main(255, 255 - ((color - 16384) >> 6), 0);
  84. }
  85. switch (ui_state) {
  86. case UI_STATE_WAITING:
  87. leds.set_osc(blink ? 255 : 0, blink ? 255 : 0);
  88. break;
  89. case UI_STATE_RECEIVING:
  90. leds.set_osc(0, system_clock.milliseconds() & 32 ? 255 : 0);
  91. break;
  92. case UI_STATE_ERROR:
  93. leds.set_osc(blink ? 255 : 0, 0);
  94. leds.set_main(255, 0, 0);
  95. break;
  96. case UI_STATE_WRITING:
  97. leds.set_osc(0, 255);
  98. leds.set_main(0, 255, 0);
  99. break;
  100. }
  101. leds.Write();
  102. }
  103. void SysTick_Handler() {
  104. system_clock.Tick();
  105. int32_t gain_raw = adc.value(ADC_LEVEL_1_POT) >> 1;
  106. gain = gain_raw * gain_raw >> 14;
  107. switches.Debounce();
  108. if (switches.released(0)) {
  109. switch_released = true;
  110. }
  111. UpdateLeds();
  112. }
  113. }
  114. size_t discard_samples = 8000;
  115. void FillBuffer(Codec::Frame* input, Codec::Frame* output, size_t n) {
  116. adc.Convert();
  117. for (size_t i = 0; i < n; ++i) {
  118. input[i].l = Clip16(static_cast<int32_t>(input[i].l) * gain >> 12);
  119. }
  120. meter.Process(input, n);
  121. while (n--) {
  122. int32_t sample = (input->l >> 4) + 2048;
  123. if (!discard_samples) {
  124. demodulator.PushSample(sample);
  125. } else {
  126. --discard_samples;
  127. }
  128. *output = *input;
  129. ++output;
  130. ++input;
  131. }
  132. }
  133. static size_t current_address;
  134. static uint16_t packet_index;
  135. static uint32_t kSectorBaseAddress[] = {
  136. 0x08000000,
  137. 0x08004000,
  138. 0x08008000,
  139. 0x0800C000,
  140. 0x08010000,
  141. 0x08020000,
  142. 0x08040000,
  143. 0x08060000,
  144. 0x08080000,
  145. 0x080A0000,
  146. 0x080C0000,
  147. 0x080E0000
  148. };
  149. const uint32_t kBlockSize = 16384;
  150. const uint16_t kPacketsPerBlock = kBlockSize / kPacketSize;
  151. uint8_t rx_buffer[kBlockSize];
  152. void ProgramPage(const uint8_t* data, size_t size) {
  153. FLASH_Unlock();
  154. FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
  155. FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR);
  156. for (int32_t i = 0; i < 12; ++i) {
  157. if (current_address == kSectorBaseAddress[i]) {
  158. FLASH_EraseSector(i * 8, VoltageRange_3);
  159. }
  160. }
  161. const uint32_t* words = static_cast<const uint32_t*>(
  162. static_cast<const void*>(data));
  163. for (size_t written = 0; written < size; written += 4) {
  164. FLASH_ProgramWord(current_address, *words++);
  165. current_address += 4;
  166. }
  167. }
  168. void Init() {
  169. System sys;
  170. Version version;
  171. sys.Init(false);
  172. leds.Init();
  173. meter.Init(48000);
  174. version.Init();
  175. switches.Init();
  176. if (!codec.Init(!version.revised(), 48000)) { }
  177. if (!codec.Start(48, &FillBuffer)) { }
  178. adc.Init();
  179. sys.StartTimers();
  180. }
  181. void InitializeReception() {
  182. decoder.Init(20000);
  183. demodulator.Init(
  184. kModulationRate / kSampleRate * 4294967296.0,
  185. kSampleRate / kModulationRate,
  186. 2.0 * kSampleRate / kBitRate);
  187. demodulator.SyncCarrier(true);
  188. decoder.Reset();
  189. current_address = kStartAddress;
  190. packet_index = 0;
  191. ui_state = UI_STATE_WAITING;
  192. }
  193. int main(void) {
  194. InitializeReception();
  195. Init();
  196. bool exit_updater = !switches.pressed_immediate(0);
  197. while (!exit_updater) {
  198. bool error = false;
  199. if (demodulator.state() == DEMODULATOR_STATE_OVERFLOW) {
  200. error = true;
  201. } else {
  202. demodulator.ProcessAtLeast(32);
  203. }
  204. while (demodulator.available() && !error && !exit_updater) {
  205. uint8_t symbol = demodulator.NextSymbol();
  206. PacketDecoderState state = decoder.ProcessSymbol(symbol);
  207. switch (state) {
  208. case PACKET_DECODER_STATE_OK:
  209. {
  210. ui_state = UI_STATE_RECEIVING;
  211. memcpy(
  212. rx_buffer + (packet_index % kPacketsPerBlock) * kPacketSize,
  213. decoder.packet_data(),
  214. kPacketSize);
  215. ++packet_index;
  216. if ((packet_index % kPacketsPerBlock) == 0) {
  217. ui_state = UI_STATE_WRITING;
  218. ProgramPage(rx_buffer, kBlockSize);
  219. decoder.Reset();
  220. demodulator.SyncCarrier(false);
  221. } else {
  222. decoder.Reset();
  223. demodulator.SyncDecision();
  224. }
  225. }
  226. break;
  227. case PACKET_DECODER_STATE_ERROR_SYNC:
  228. case PACKET_DECODER_STATE_ERROR_CRC:
  229. error = true;
  230. break;
  231. case PACKET_DECODER_STATE_END_OF_TRANSMISSION:
  232. exit_updater = true;
  233. break;
  234. default:
  235. break;
  236. }
  237. }
  238. if (error) {
  239. ui_state = UI_STATE_ERROR;
  240. switch_released = false;
  241. while (!switch_released); // Polled in ISR
  242. InitializeReception();
  243. }
  244. }
  245. codec.Stop();
  246. adc.DeInit();
  247. Uninitialize();
  248. JumpTo(kStartAddress);
  249. while (1) { }
  250. }