#include "project.h" #define OT_THM_OUT GPIO9 #define OT_THM_OUT_PORT GPIOB #define OT_BLR_OUT GPIO8 #define OT_BLR_OUT_PORT GPIOB typedef struct tx_phy { int busy; unsigned half_bit; uint8_t data[4]; } TX_Phy; static TX_Phy p_thm, p_blr; #if 0 static uint32_t cycle_diff (uint32_t a, uint32_t b) { return b - a; } #endif static int ot_phy_tx_worker (TX_Phy *p) { int ret = p->half_bit & 1; unsigned bit; if (p->half_bit < 2) ret ^= 1; else if (p->half_bit < 66) { bit = (p->half_bit >> 1) - 1; if (p->data[bit >> 3] & (0x80 >> (bit & 7))) ret ^= 1; } else if (p->half_bit < 68) ret ^= 1; p->half_bit++; if (p->half_bit == 68) { p->half_bit = 0; p->busy = 0; } return ret; } void ot_phy_tx_tick (void) { int v; if (p_thm.busy) { v = ot_phy_tx_worker (&p_thm); if (v) CLEAR (OT_THM_OUT); else SET (OT_THM_OUT); } if (p_blr.busy) { v = ot_phy_tx_worker (&p_blr); if (v) CLEAR (OT_BLR_OUT); else SET (OT_BLR_OUT); } } int ot_tx_thm (uint8_t *data) { if (p_thm.busy) return -1; if (!data) return 0; led_blink(); memcpy (p_thm.data, data, sizeof (p_thm.data)); p_thm.busy = 1; return 0; } int ot_tx_blr (uint8_t *data) { if (p_blr.busy) return -1; if (!data) return 0; led_blink(); memcpy (p_blr.data, data, sizeof (p_blr.data)); p_blr.busy = 1; return 0; } void ot_phy_tx_init (void) { MAP_OUTPUT_PP (OT_THM_OUT); MAP_OUTPUT_PP (OT_BLR_OUT); }