remote.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /*
  2. * Remote control interface
  3. *
  4. * Copyright (c) 2020 Michael Buesch <m@bues.ch>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include "compat.h"
  21. #include "remote.h"
  22. #include "adc.h"
  23. #include "arithmetic.h"
  24. #include "crc.h"
  25. #include "debug.h"
  26. #include "outputsp.h"
  27. #include "standby.h"
  28. #include "uart.h"
  29. #include "util.h"
  30. #include "watchdog.h"
  31. #include "battery.h"
  32. #include "eeprom.h"
  33. #define REMOTE_CHAN UART_CHAN_8BIT_0
  34. #define REMOTE_NR_SP 3u
  35. #define REMOTE_STANDBY_DELAY_MS 5000u
  36. /* Remote control message ID. */
  37. enum remote_msg_id {
  38. MSGID_NOP,
  39. MSGID_ACK,
  40. MSGID_NACK,
  41. MSGID_PING,
  42. MSGID_PONG,
  43. MSGID_GET_CONTROL,
  44. MSGID_CONTROL,
  45. MSGID_GET_SETPOINTS,
  46. MSGID_SETPOINTS,
  47. MSGID_GET_BATVOLT,
  48. MSGID_BATVOLT,
  49. };
  50. /* Remote control message. On-wire format. */
  51. struct remote_msg {
  52. uint8_t magic;
  53. #define MSG_MAGIC 0xAAu
  54. uint8_t id;
  55. uint8_t reserved;
  56. union {
  57. struct {
  58. } _packed nop;
  59. struct {
  60. } _packed ack;
  61. struct {
  62. } _packed nack;
  63. struct {
  64. } _packed ping;
  65. struct {
  66. } _packed pong;
  67. struct {
  68. } _packed get_control;
  69. struct {
  70. uint8_t flags;
  71. #define MSG_CTLFLG_ANADIS 0x01 /* Disable analog inputs. */
  72. #define MSG_CTLFLG_EEPDIS 0x02 /* Disable EEPROM. */
  73. } _packed control;
  74. struct {
  75. uint8_t flags;
  76. #define MSG_GETSPFLG_HSL 0x01 /* Get HSL instead of RGB. */
  77. } _packed get_setpoints;
  78. struct {
  79. uint8_t flags;
  80. #define MSG_SPFLG_HSL 0x01 /* Set HSL instead of RGB. */
  81. uint8_t nr_sp;
  82. le16_t sp[REMOTE_NR_SP];
  83. } _packed setpoints;
  84. struct {
  85. } _packed get_batvolt;
  86. struct {
  87. uint16_t meas_mv;
  88. uint16_t drop_mv;
  89. } _packed batvolt;
  90. uint8_t padding[8];
  91. } _packed;
  92. uint8_t crc;
  93. } _packed;
  94. /* Remote control state. */
  95. static struct {
  96. struct {
  97. uint8_t buf[sizeof(struct remote_msg)];
  98. uint8_t count;
  99. bool allocated;
  100. bool running;
  101. } tx;
  102. struct {
  103. uint8_t buf[sizeof(struct remote_msg)];
  104. uint8_t count;
  105. bool synchronized;
  106. } rx;
  107. uint16_t time_since_xfer_ms;
  108. } remote;
  109. /* Update the standby suppress state in the system.
  110. * Called with interrupts disabled. */
  111. static void remote_update_standby_suppress(void)
  112. {
  113. bool standby_suppress;
  114. uint8_t i;
  115. bool all_zero;
  116. if (!USE_REMOTE)
  117. return;
  118. standby_suppress = false;
  119. if (remote.time_since_xfer_ms < REMOTE_STANDBY_DELAY_MS)
  120. standby_suppress = true;
  121. if (!adc_analogpins_enabled()) {
  122. /* If any output is enabled, don't go to sleep. */
  123. all_zero = true;
  124. for (i = 0u; i < NR_PWM; i++)
  125. all_zero &= output_setpoint_get(i, false) == 0u;
  126. if (!all_zero)
  127. standby_suppress = true;
  128. }
  129. set_standby_suppress(STANDBY_SRC_REMOTE, standby_suppress);
  130. }
  131. /* Free a TX message buffer.
  132. * Called with interrupts disabled. */
  133. static void put_tx_buffer(struct remote_msg *msg)
  134. {
  135. /* There's only one TX buffer.
  136. * Free it. */
  137. remote.tx.allocated = false;
  138. }
  139. /* Allocate a new message buffer for transmission.
  140. * Called with interrupts disabled. */
  141. static struct remote_msg * get_tx_buffer(void)
  142. {
  143. struct remote_msg *msg = NULL;
  144. if (!USE_REMOTE)
  145. return NULL;
  146. /* There's only one TX buffer.
  147. * Return it, if it's not occupied. */
  148. if (!remote.tx.allocated) {
  149. remote.tx.allocated = true;
  150. msg = (struct remote_msg *)&remote.tx.buf[0];
  151. memset(msg, 0, sizeof(*msg));
  152. msg->magic = MSG_MAGIC;
  153. }
  154. return msg;
  155. }
  156. /* Transmit the next byte.
  157. * Called with interrupts disabled. */
  158. static void tx_next_byte(void)
  159. {
  160. if (!USE_REMOTE)
  161. return;
  162. uart_tx_byte(remote.tx.buf[remote.tx.count++],
  163. REMOTE_CHAN);
  164. if (remote.tx.count >= sizeof(struct remote_msg)) {
  165. remote.tx.running = false;
  166. put_tx_buffer((struct remote_msg *)&remote.tx.buf[0]);
  167. uart_tx_enable(false, REMOTE_CHAN);
  168. }
  169. }
  170. /* Start transmission of a message.
  171. * Called with interrupts disabled. */
  172. static void tx_start(struct remote_msg *msg)
  173. {
  174. if (!USE_REMOTE)
  175. return;
  176. msg->crc = crc8(msg, sizeof(*msg) - 1);
  177. remote.tx.count = 0;
  178. remote.tx.running = true;
  179. uart_tx_enable(true, REMOTE_CHAN);
  180. if (uart_tx_is_ready(REMOTE_CHAN))
  181. tx_next_byte();
  182. remote.time_since_xfer_ms = 0u;
  183. }
  184. /* Handle a received message.
  185. * Called with interrupts disabled. */
  186. static void remote_handle_rx_msg(const struct remote_msg *rxmsg)
  187. {
  188. struct remote_msg *txmsg;
  189. struct eeprom_data *eedata;
  190. uint8_t crc;
  191. uint8_t i;
  192. if (!USE_REMOTE)
  193. return;
  194. if (rxmsg->magic != MSG_MAGIC) /* Invalid magic byte */
  195. goto error_rxmsg;
  196. crc = crc8(rxmsg, sizeof(*rxmsg) - 1);
  197. if (crc != rxmsg->crc) /* CRC error */
  198. goto error_rxmsg;
  199. eedata = eeprom_get_data();
  200. switch (rxmsg->id) {
  201. case MSGID_NOP:
  202. break;
  203. case MSGID_ACK:
  204. break;
  205. case MSGID_NACK:
  206. break;
  207. case MSGID_PING:
  208. txmsg = get_tx_buffer();
  209. if (!txmsg)
  210. goto error_txalloc;
  211. txmsg->id = MSGID_PONG;
  212. tx_start(txmsg);
  213. break;
  214. case MSGID_PONG:
  215. break;
  216. case MSGID_GET_CONTROL:
  217. txmsg = get_tx_buffer();
  218. if (!txmsg)
  219. goto error_txalloc;
  220. txmsg->id = MSGID_CONTROL;
  221. if (!adc_analogpins_enabled())
  222. txmsg->control.flags |= MSG_CTLFLG_ANADIS;
  223. tx_start(txmsg);
  224. break;
  225. case MSGID_CONTROL:
  226. txmsg = get_tx_buffer();
  227. if (!txmsg)
  228. goto error_txalloc;
  229. /* Update control settings. */
  230. if (rxmsg->control.flags & MSG_CTLFLG_ANADIS)
  231. adc_analogpins_enable(false);
  232. else
  233. adc_analogpins_enable(true);
  234. /* Update settings in EEPROM. */
  235. if (eedata) {
  236. if (rxmsg->control.flags & MSG_CTLFLG_EEPDIS)
  237. eedata->flags |= EEPROM_FLAG_DIS;
  238. else
  239. eedata->flags &= (uint8_t)~EEPROM_FLAG_DIS;
  240. if (!(eedata->flags & EEPROM_FLAG_DIS)) {
  241. if (rxmsg->control.flags & MSG_CTLFLG_ANADIS)
  242. eedata->flags |= EEPROM_FLAG_ANADIS;
  243. else
  244. eedata->flags &= (uint8_t)~EEPROM_FLAG_ANADIS;
  245. }
  246. eeprom_store_data();
  247. }
  248. txmsg->id = MSGID_ACK;
  249. tx_start(txmsg);
  250. break;
  251. case MSGID_GET_SETPOINTS:
  252. txmsg = get_tx_buffer();
  253. if (!txmsg)
  254. goto error_txalloc;
  255. txmsg->id = MSGID_NACK;
  256. if (rxmsg->get_setpoints.flags & MSG_GETSPFLG_HSL) {
  257. if (NR_PWM == REMOTE_NR_SP) {
  258. le16_t sp;
  259. for (i = 0u; i < REMOTE_NR_SP; i++) {
  260. sp = to_le16(output_setpoint_get(i, true));
  261. txmsg->setpoints.sp[i] = sp;
  262. }
  263. txmsg->setpoints.flags = MSG_SPFLG_HSL;
  264. txmsg->setpoints.nr_sp = REMOTE_NR_SP;
  265. txmsg->id = MSGID_SETPOINTS;
  266. }
  267. } else {
  268. if (NR_PWM <= REMOTE_NR_SP) {
  269. le16_t sp;
  270. for (i = 0u; i < NR_PWM; i++) {
  271. sp = to_le16(output_setpoint_get(i, false));
  272. txmsg->setpoints.sp[i] = sp;
  273. }
  274. txmsg->setpoints.flags = 0u;
  275. txmsg->setpoints.nr_sp = NR_PWM;
  276. txmsg->id = MSGID_SETPOINTS;
  277. }
  278. }
  279. tx_start(txmsg);
  280. break;
  281. case MSGID_SETPOINTS:
  282. txmsg = get_tx_buffer();
  283. if (!txmsg)
  284. goto error_txalloc;
  285. txmsg->id = MSGID_NACK;
  286. if (rxmsg->setpoints.flags & MSG_SPFLG_HSL) {
  287. if (NR_PWM == REMOTE_NR_SP &&
  288. rxmsg->setpoints.nr_sp == REMOTE_NR_SP) {
  289. uint16_t hsl[REMOTE_NR_SP];
  290. /* Update HSL output setpoints. */
  291. for (i = 0u; i < REMOTE_NR_SP; i++)
  292. hsl[i] = from_le16(rxmsg->setpoints.sp[i]);
  293. output_setpoint_convert_hsl2rgb(&hsl[0]);
  294. output_setpoint_set(IF_MULTIPWM(0u,) true, 0u);
  295. /* Update setpoints in EEPROM. */
  296. if (eedata && !(eedata->flags & EEPROM_FLAG_DIS)) {
  297. eedata->flags |= EEPROM_FLAG_SPHSL;
  298. build_assert(ARRAY_SIZE(eedata->setpoints) == REMOTE_NR_SP);
  299. for (i = 0u; i < REMOTE_NR_SP; i++)
  300. eedata->setpoints[i] = hsl[i];
  301. eeprom_store_data();
  302. }
  303. txmsg->id = MSGID_ACK;
  304. }
  305. } else {
  306. if (NR_PWM <= REMOTE_NR_SP &&
  307. rxmsg->setpoints.nr_sp <= NR_PWM) {
  308. uint16_t rgb[REMOTE_NR_SP];
  309. /* Update RGB output setpoints. */
  310. for (i = 0u; i < rxmsg->setpoints.nr_sp; i++) {
  311. rgb[i] = from_le16(rxmsg->setpoints.sp[i]);
  312. output_setpoint_set(IF_MULTIPWM(i,) false, rgb[i]);
  313. }
  314. for (i = rxmsg->setpoints.nr_sp; i < NR_PWM; i++)
  315. output_setpoint_set(IF_MULTIPWM(i,) false, 0u);
  316. /* Update setpoints in EEPROM. */
  317. if (eedata && !(eedata->flags & EEPROM_FLAG_DIS)) {
  318. eedata->flags &= (uint8_t)~EEPROM_FLAG_SPHSL;
  319. build_assert(ARRAY_SIZE(eedata->setpoints) == REMOTE_NR_SP);
  320. for (i = 0u; i < rxmsg->setpoints.nr_sp; i++)
  321. eedata->setpoints[i] = rgb[i];
  322. for (i = rxmsg->setpoints.nr_sp; i < NR_PWM; i++)
  323. eedata->setpoints[i] = 0u;
  324. eeprom_store_data();
  325. }
  326. txmsg->id = MSGID_ACK;
  327. }
  328. }
  329. tx_start(txmsg);
  330. break;
  331. case MSGID_GET_BATVOLT:
  332. txmsg = get_tx_buffer();
  333. if (!txmsg)
  334. goto error_txalloc;
  335. txmsg->id = MSGID_BATVOLT;
  336. battery_get_voltage(&txmsg->batvolt.meas_mv,
  337. &txmsg->batvolt.drop_mv);
  338. tx_start(txmsg);
  339. break;
  340. case MSGID_BATVOLT:
  341. break;
  342. default:
  343. goto error_rxmsg;
  344. }
  345. return;
  346. error_txalloc:
  347. return;
  348. error_rxmsg:
  349. remote.rx.synchronized = false;
  350. return;
  351. }
  352. /* Ready to send new byte.
  353. * Called with interrupts disabled. */
  354. static void remote_tx_ready(void)
  355. {
  356. if (!USE_REMOTE)
  357. return;
  358. if (remote.tx.running)
  359. tx_next_byte();
  360. else
  361. uart_tx_enable(false, REMOTE_CHAN);
  362. }
  363. /* New byte received.
  364. * Called with interrupts disabled. */
  365. static void remote_rx(uint8_t data)
  366. {
  367. const struct remote_msg *msg;
  368. if (!USE_REMOTE)
  369. return;
  370. remote.time_since_xfer_ms = 0u;
  371. if (remote.rx.synchronized) {
  372. remote.rx.buf[remote.rx.count++] = data;
  373. if (remote.rx.count >= sizeof(struct remote_msg)) {
  374. remote.rx.count = 0;
  375. msg = (const struct remote_msg *)&(remote.rx.buf[0]);
  376. remote_handle_rx_msg(msg);
  377. }
  378. } else {
  379. remote.rx.count = 0;
  380. remote.rx.buf[remote.rx.count++] = data;
  381. if (data == MSG_MAGIC)
  382. remote.rx.synchronized = true;
  383. }
  384. remote_update_standby_suppress();
  385. }
  386. /* Handle wake up from deep sleep.
  387. * Called with interrupts disabled. */
  388. void remote_handle_deep_sleep_wakeup(void)
  389. {
  390. if (!USE_REMOTE)
  391. return;
  392. remote_update_standby_suppress();
  393. remote.rx.synchronized = false;
  394. }
  395. /* Handle watchdog interrupt.
  396. * Called with interrupts disabled. */
  397. void remote_handle_watchdog_interrupt(void)
  398. {
  399. if (!USE_REMOTE)
  400. return;
  401. remote.time_since_xfer_ms = add_sat_u16(remote.time_since_xfer_ms,
  402. watchdog_interval_ms());
  403. remote_update_standby_suppress();
  404. }
  405. /* Restore settings from EEPROM data.
  406. * Called with interrupts disabled. */
  407. void remote_restore_from_eeprom(void)
  408. {
  409. const struct eeprom_data *eedata;
  410. uint8_t i;
  411. if (!USE_REMOTE)
  412. return;
  413. eedata = eeprom_get_data();
  414. if (!eedata)
  415. return;
  416. if (eedata->flags & EEPROM_FLAG_DIS)
  417. return; /* EEPROM is disabled. */
  418. if (eedata->flags & EEPROM_FLAG_ANADIS) {
  419. adc_analogpins_enable(false);
  420. if (eedata->flags & EEPROM_FLAG_SPHSL) {
  421. if (NR_PWM == REMOTE_NR_SP) {
  422. output_setpoint_convert_hsl2rgb(&eedata->setpoints[0]);
  423. output_setpoint_set(IF_MULTIPWM(0u,) true, 0u);
  424. }
  425. } else {
  426. for (i = 0u; i < NR_PWM; i++) {
  427. output_setpoint_set(IF_MULTIPWM(i,)
  428. false,
  429. eedata->setpoints[i]);
  430. }
  431. }
  432. } else
  433. adc_analogpins_enable(true);
  434. remote_update_standby_suppress();
  435. }
  436. /* Initialize remote control. */
  437. void remote_init(void)
  438. {
  439. if (!USE_REMOTE)
  440. return;
  441. build_assert(sizeof(struct remote_msg) == 12);
  442. remote.time_since_xfer_ms = UINT16_MAX;
  443. uart_register_callbacks(remote_tx_ready,
  444. remote_rx,
  445. REMOTE_CHAN);
  446. set_standby_suppress(STANDBY_SRC_REMOTE, false);
  447. }