/*------------------------------------------------------------------------------*/ u8_t tr1001_send(void) { u8_t *hdr; u16_t hdrlen; u8_t *data; u16_t datalen; int i; hdr = &uip_buf[UIP_LLH_LEN]; hdrlen = UIP_TCPIP_HLEN; data = uip_appdata; if(uip_len < UIP_TCPIP_HLEN) { datalen = 0; } else { datalen = uip_len - UIP_TCPIP_HLEN; } /* Prepare the transmission. */ prepare_transmission(); /* Send first preamble byte. */ send(0xaa); /* Send second preamble byte. */ send(0xaa); /* Send sync byte. */ send(0x0ff); /* Send first start byte. */ send(0x01); /* Send second start byte. */ send(0x07f); /* Send packet header. */ send2(TR1001_TYPE_DATA); send2(++packet_id); send2(uip_len >> 8); send2(uip_len & 0xff); /* Send packet data. */ for(i = 0; i < hdrlen; ++i) { send2(hdr[i]); } for(i = 0;i < datalen; ++i) { send2(data[i]); } /* Send trailing bytes. */ send(0xaa); send(0xaa); /* Turn on reception again. */ rxon(); return UIP_FW_OK; }
static void wpipromiscuous(void *arg, int on) { Ether *edev; Ctlr *ctlr; edev = arg; ctlr = edev->ctlr; qlock(ctlr); ctlr->prom = on; rxon(edev, ctlr->wifi->bss); qunlock(ctlr); }
/*---------------------------------------------------------------------------*/ static int tr1001_on(void) { if(onoroff == ON) { return 1; } ENERGEST_ON(ENERGEST_TYPE_LISTEN); onoroff = ON; rxon(); rxclear(); return 1; }
/*------------------------------------------------------------------------------*/ void tr1001_init(void) { /* Turn on reception. */ rxon(); /* Reset reception state. */ rxclear(); UCTL0 = CHAR; /* 8-bit character */ UTCTL0 = SSEL1; /* UCLK = SMCLK */ #define TR1001_SPEED 19200 if(TR1001_SPEED == 19200) { /* Set TR1001 to 19200 */ UBR00 = 0x80; /* 2,457MHz/19200 = 128 -> 0x80 */ UBR10 = 0x00; /* */ UMCTL0 = 0x00; /* no modulation */ } else if(TR1001_SPEED == 38400) { /* Set TR1001 to 38400 */ UBR00 = 0x40; /* 2,457MHz/38400 = 64 -> 0x40 */ UBR10 = 0x00; /* */ UMCTL0 = 0x00; /* no modulation */ } else if(TR1001_SPEED == 57600) { UBR00 = 0x2a; /* 2,457MHz/57600 = 42.7 -> 0x2A */ UBR10 = 0x00; /* */ UMCTL0 = 0x5b; /* */ } else if(TR1001_SPEED == 115200) { UBR00 = 0x15; /* 2,457MHz/115200 = 21.4 -> 0x15 */ UBR10 = 0x00; /* */ UMCTL0 = 0x4a; /* */ } #if 0 /* 19200 bps */ UBR00 = 0x80; /* 2,457MHz/19200 = 128 -> 0x80 */ UBR10 = 0x00; /* */ UMCTL0 = 0x00; /* no modulation */ #endif /* 0 */ ME1 |= UTXE0 + URXE0; /* Enable USART0 TXD/RXD */ /* Turn on receive interrupt. */ IE1 |= URXIE0; }
/*------------------------------------------------------------------------------*/ u8_t tr1001_ack(void) { /* Prepare the transmission. */ prepare_transmission(); /* Send first preamble byte. */ send(0xaa); /* Send second preamble byte. */ send(0xaa); /* Send sync byte. */ send(0x0ff); /* Send first start byte. */ send(0x01); /* Send second start byte. */ send(0x07f); /* Send packet header. */ send2(TR1001_TYPE_ACK); send2(((struct tr1001_hdr *)uip_buf)->id); send2(0); send2(0); /* Send trailing bytes. */ send(0xaa); send(0xaa); /* Turn on reception again. */ rxon(); /* beep();*/ return UIP_FW_OK; }
static void wpirecover(void *arg) { Ether *edev; Ctlr *ctlr; edev = arg; ctlr = edev->ctlr; while(waserror()) ; for(;;){ tsleep(&up->sleep, return0, 0, 4000); qlock(ctlr); for(;;){ if(ctlr->broken == 0) break; if(ctlr->power) poweroff(ctlr); if((csr32r(ctlr, Gpc) & RfKill) == 0) break; if(reset(ctlr) != nil) break; if(boot(ctlr) != nil) break; ctlr->bcastnodeid = -1; ctlr->bssnodeid = -1; ctlr->aid = 0; rxon(edev, ctlr->wifi->bss); break; } qunlock(ctlr); } }
static void transmit(Wifi *wifi, Wnode *wn, Block *b) { uchar c[Tcmdsize], *p; Ether *edev; Ctlr *ctlr; Wifipkt *w; int flags, nodeid, rate, timeout; char *err; edev = wifi->ether; ctlr = edev->ctlr; qlock(ctlr); if(ctlr->attached == 0 || ctlr->broken){ qunlock(ctlr); freeb(b); return; } if((wn->channel != ctlr->channel) || (!ctlr->prom && (wn->aid != ctlr->aid || memcmp(wn->bssid, ctlr->bssid, Eaddrlen) != 0))) rxon(edev, wn); if(b == nil){ /* association note has no data to transmit */ qunlock(ctlr); return; } flags = 0; timeout = 3; nodeid = ctlr->bcastnodeid; p = wn->minrate; w = (Wifipkt*)b->rp; if((w->a1[0] & 1) == 0){ flags |= TFlagNeedACK; if(BLEN(b) > 512-4) flags |= TFlagNeedRTS|TFlagFullTxOp; if((w->fc[0] & 0x0c) == 0x08 && ctlr->bssnodeid != -1){ timeout = 0; nodeid = ctlr->bssnodeid; p = wn->maxrate; } } qunlock(ctlr); rate = 0; if(p >= wpirates && p < &wpirates[nelem(ratetab)]) rate = p - wpirates; memset(p = c, 0, sizeof(c)); put16(p, BLEN(b)), p += 2; put16(p, 0), p += 2; /* lnext */ put32(p, flags), p += 4; *p++ = ratetab[rate].plcp; *p++ = nodeid; *p++ = 0; /* tid */ *p++ = 0; /* security */ p += 16+8; /* key/iv */ put32(p, 0), p += 4; /* fnext */ put32(p, 0xffffffff), p += 4; /* livetime infinite */ *p++ = 0xff; *p++ = 0x0f; *p++ = 7; *p++ = 15; put16(p, timeout), p += 2; put16(p, 0), p += 2; /* txop */ if((err = qcmd(ctlr, 0, 28, c, p - c, b)) != nil){ print("transmit: %s\n", err); freeb(b); } }