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.

288 lines
7.4KB

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