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.

305 lines
9.0KB

  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/system/bootloader_utils.h"
  25. #include "stmlib/system/system_clock.h"
  26. #include "clouds/drivers/codec.h"
  27. #include "clouds/drivers/leds.h"
  28. #include "clouds/drivers/switches.h"
  29. #include "clouds/drivers/system.h"
  30. #include "clouds/drivers/version.h"
  31. #include "clouds/meter.h"
  32. #include "stm_audio_bootloader/qpsk/packet_decoder.h"
  33. #include "stm_audio_bootloader/qpsk/demodulator.h"
  34. #include <cstring>
  35. using namespace clouds;
  36. using namespace stmlib;
  37. using namespace stm_audio_bootloader;
  38. const double kSampleRate = 48000.0;
  39. const double kModulationRate = 6000.0;
  40. const double kBitRate = 12000.0;
  41. const uint32_t kStartAddress = 0x08008000;
  42. Codec codec;
  43. Meter meter;
  44. Leds leds;
  45. Switches switches;
  46. PacketDecoder decoder;
  47. Demodulator demodulator;
  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. const int16_t lut_db[] = {
  62. -32768, -32768, -24576, -19783, -16384, -13746, -11591, -9770,
  63. -8192, -6799, -5554, -4428, -3399, -2453, -1578, -762,
  64. 0, 716, 1392, 2031, 2637, 3213, 3763, 4289,
  65. 4792, 5274, 5738, 6184, 6613, 7028, 7429, 7816,
  66. 8192, 8555, 8908, 9251, 9584, 9907, 10223, 10530,
  67. 10829, 11121, 11405, 11683, 11955, 12221, 12481, 12735,
  68. 12984, 13227, 13466, 13700, 13930, 14155, 14376, 14592,
  69. 14805, 15015, 15220, 15422, 15621, 15816, 16008, 16197,
  70. 16384, 16567, 16747, 16925, 17100, 17273, 17443, 17610,
  71. 17776, 17939, 18099, 18258, 18415, 18569, 18722, 18872,
  72. 19021, 19168, 19313, 19456, 19597, 19737, 19875, 20012,
  73. 20147, 20281, 20413, 20543, 20673, 20800, 20927, 21052,
  74. 21176, 21298, 21419, 21539, 21658, 21776, 21892, 22007,
  75. 22122, 22235, 22347, 22458, 22568, 22676, 22784, 22891,
  76. 22997, 23102, 23207, 23310, 23412, 23514, 23614, 23714,
  77. 23813, 23911, 24008, 24105, 24200, 24295, 24389, 24483,
  78. 24576, 24667, 24759, 24849, 24939, 25028, 25117, 25205,
  79. 25292, 25379, 25465, 25550, 25635, 25719, 25802, 25885,
  80. 25968, 26049, 26131, 26211, 26291, 26371, 26450, 26529,
  81. 26607, 26684, 26761, 26838, 26914, 26989, 27064, 27139,
  82. 27213, 27286, 27360, 27432, 27505, 27576, 27648, 27719,
  83. 27789, 27860, 27929, 27999, 28067, 28136, 28204, 28272,
  84. 28339, 28406, 28473, 28539, 28605, 28670, 28735, 28800,
  85. 28865, 28929, 28992, 29056, 29119, 29181, 29244, 29306,
  86. 29368, 29429, 29490, 29551, 29611, 29671, 29731, 29791,
  87. 29850, 29909, 29968, 30026, 30084, 30142, 30199, 30257,
  88. 30314, 30370, 30427, 30483, 30539, 30594, 30650, 30705,
  89. 30760, 30814, 30868, 30923, 30976, 31030, 31083, 31136,
  90. 31189, 31242, 31294, 31347, 31399, 31450, 31502, 31553,
  91. 31604, 31655, 31706, 31756, 31806, 31856, 31906, 31955,
  92. 32005, 32054, 32103, 32152, 32200, 32248, 32297, 32345,
  93. 32392, 32440, 32487, 32534, 32581, 32628, 32675, 32721,
  94. 32721,
  95. };
  96. enum UiState {
  97. UI_STATE_WAITING,
  98. UI_STATE_RECEIVING,
  99. UI_STATE_ERROR,
  100. UI_STATE_WRITING
  101. };
  102. volatile bool switch_released = false;
  103. volatile UiState ui_state;
  104. void UpdateLeds() {
  105. leds.Clear();
  106. leds.set_freeze(true);
  107. switch (ui_state) {
  108. case UI_STATE_WAITING:
  109. leds.set_freeze(system_clock.milliseconds() & 128);
  110. break;
  111. case UI_STATE_RECEIVING:
  112. leds.set_freeze(system_clock.milliseconds() & 32);
  113. leds.PaintBar(lut_db[meter.peak() >> 7]);
  114. break;
  115. case UI_STATE_ERROR:
  116. {
  117. bool on = system_clock.milliseconds() & 256;
  118. for (uint8_t i = 0; i < 4; ++i) {
  119. leds.set_status(i, on ? 255 : 0, 0);
  120. }
  121. }
  122. break;
  123. case UI_STATE_WRITING:
  124. {
  125. for (uint8_t i = 0; i < 4; ++i) {
  126. leds.set_status(i, 0, 255);
  127. }
  128. }
  129. break;
  130. }
  131. leds.Write();
  132. }
  133. void SysTick_Handler() {
  134. system_clock.Tick();
  135. switches.Debounce();
  136. if (switches.released(2)) {
  137. switch_released = true;
  138. }
  139. UpdateLeds();
  140. }
  141. }
  142. size_t discard_samples = 8000;
  143. void FillBuffer(Codec::Frame* input, Codec::Frame* output, size_t n) {
  144. meter.Process(input, n);
  145. while (n--) {
  146. int32_t sample = (input->l >> 4) + 2048;
  147. if (!discard_samples) {
  148. demodulator.PushSample(sample);
  149. } else {
  150. --discard_samples;
  151. }
  152. *output = *input;
  153. ++output;
  154. ++input;
  155. }
  156. }
  157. static size_t current_address;
  158. static uint16_t packet_index;
  159. static uint32_t kSectorBaseAddress[] = {
  160. 0x08000000,
  161. 0x08004000,
  162. 0x08008000,
  163. 0x0800C000,
  164. 0x08010000,
  165. 0x08020000,
  166. 0x08040000,
  167. 0x08060000,
  168. 0x08080000,
  169. 0x080A0000,
  170. 0x080C0000,
  171. 0x080E0000
  172. };
  173. const uint32_t kBlockSize = 16384;
  174. const uint16_t kPacketsPerBlock = kBlockSize / kPacketSize;
  175. uint8_t rx_buffer[kBlockSize];
  176. void ProgramPage(const uint8_t* data, size_t size) {
  177. FLASH_Unlock();
  178. FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
  179. FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR);
  180. for (int32_t i = 0; i < 12; ++i) {
  181. if (current_address == kSectorBaseAddress[i]) {
  182. FLASH_EraseSector(i * 8, VoltageRange_3);
  183. }
  184. }
  185. const uint32_t* words = static_cast<const uint32_t*>(
  186. static_cast<const void*>(data));
  187. for (size_t written = 0; written < size; written += 4) {
  188. FLASH_ProgramWord(current_address, *words++);
  189. current_address += 4;
  190. }
  191. }
  192. void Init() {
  193. System sys;
  194. Version version;
  195. sys.Init(false);
  196. leds.Init();
  197. meter.Init(48000);
  198. switches.Init();
  199. version.Init();
  200. if (!codec.Init(!version.revised(), 48000)) { }
  201. if (!codec.Start(32, &FillBuffer)) { }
  202. sys.StartTimers();
  203. }
  204. void InitializeReception() {
  205. decoder.Init(20000);
  206. demodulator.Init(
  207. kModulationRate / kSampleRate * 4294967296.0,
  208. kSampleRate / kModulationRate,
  209. 2.0 * kSampleRate / kBitRate);
  210. demodulator.SyncCarrier(true);
  211. decoder.Reset();
  212. current_address = kStartAddress;
  213. packet_index = 0;
  214. ui_state = UI_STATE_WAITING;
  215. }
  216. int main(void) {
  217. InitializeReception();
  218. Init();
  219. bool exit_updater = !switches.pressed_immediate(2);
  220. while (!exit_updater) {
  221. bool error = false;
  222. if (demodulator.state() == DEMODULATOR_STATE_OVERFLOW) {
  223. error = true;
  224. } else {
  225. demodulator.ProcessAtLeast(32);
  226. }
  227. while (demodulator.available() && !error && !exit_updater) {
  228. uint8_t symbol = demodulator.NextSymbol();
  229. PacketDecoderState state = decoder.ProcessSymbol(symbol);
  230. switch (state) {
  231. case PACKET_DECODER_STATE_OK:
  232. {
  233. ui_state = UI_STATE_RECEIVING;
  234. memcpy(
  235. rx_buffer + (packet_index % kPacketsPerBlock) * kPacketSize,
  236. decoder.packet_data(),
  237. kPacketSize);
  238. ++packet_index;
  239. if ((packet_index % kPacketsPerBlock) == 0) {
  240. ui_state = UI_STATE_WRITING;
  241. ProgramPage(rx_buffer, kBlockSize);
  242. decoder.Reset();
  243. demodulator.SyncCarrier(false);
  244. } else {
  245. decoder.Reset();
  246. demodulator.SyncDecision();
  247. }
  248. }
  249. break;
  250. case PACKET_DECODER_STATE_ERROR_SYNC:
  251. case PACKET_DECODER_STATE_ERROR_CRC:
  252. error = true;
  253. break;
  254. case PACKET_DECODER_STATE_END_OF_TRANSMISSION:
  255. exit_updater = true;
  256. break;
  257. default:
  258. break;
  259. }
  260. }
  261. if (error) {
  262. ui_state = UI_STATE_ERROR;
  263. switch_released = false;
  264. while (!switch_released); // Polled in ISR
  265. InitializeReception();
  266. }
  267. }
  268. codec.Stop();
  269. Uninitialize();
  270. JumpTo(kStartAddress);
  271. while (1) { }
  272. }