/* * Configure and start event timers. */ void cpu_initclocks_bsp(void) { int base, div; timer[0] = et_find(timername[0], ET_FLAGS_PERIODIC, ET_FLAGS_PERIODIC); if (timer[0] == NULL) timer[0] = et_find(NULL, ET_FLAGS_PERIODIC, ET_FLAGS_PERIODIC); if (timer[0] == NULL) panic("No usable event timer found!"); et_init(timer[0], timer1cb, NULL, NULL); timer[1] = et_find(timername[1][0] ? timername[1] : NULL, ET_FLAGS_PERIODIC, ET_FLAGS_PERIODIC); if (timer[1]) et_init(timer[1], timer2cb, NULL, NULL); /* * We honor the requested 'hz' value. * We want to run stathz in the neighborhood of 128hz. * We would like profhz to run as often as possible. */ if (singlemul == 0) { if (hz >= 1500 || (hz % 128) == 0) singlemul = 1; else if (hz >= 750) singlemul = 2; else singlemul = 4; } if (timer[1] == NULL) { base = round_freq(timer[0], hz * singlemul); singlemul = max((base + hz / 2) / hz, 1); hz = (base + singlemul / 2) / singlemul; if (base <= 128) stathz = base; else { div = base / 128; if (div >= singlemul && (div % singlemul) == 0) div++; stathz = base / div; } profhz = stathz; while ((profhz + stathz) <= 128 * 64) profhz += stathz; profhz = round_freq(timer[0], profhz); } else { hz = round_freq(timer[0], hz); stathz = round_freq(timer[1], 127); profhz = round_freq(timer[1], stathz * 64); } tick = 1000000 / hz; ET_LOCK(); cpu_restartclocks(); ET_UNLOCK(); }
/* mark interface up */ void etc_up(etc_info_t *etc) { etc->up = TRUE; et_init(etc->et, ET_INIT_DEF_OPTIONS); }
/* mark interface up */ void etc_up(etc_info_t *etc) { etc->up = TRUE; et_init(etc->et); }
/* Report or change the active event timers hardware. */ static int sysctl_kern_eventtimer_timer1(SYSCTL_HANDLER_ARGS) { char buf[32]; struct eventtimer *et; int error; ET_LOCK(); et = timer[0]; snprintf(buf, sizeof(buf), "%s", et->et_name); ET_UNLOCK(); error = sysctl_handle_string(oidp, buf, sizeof(buf), req); ET_LOCK(); et = timer[0]; if (error != 0 || req->newptr == NULL || strcmp(buf, et->et_name) == 0) { ET_UNLOCK(); return (error); } et = et_find(buf, ET_FLAGS_PERIODIC, ET_FLAGS_PERIODIC); if (et == NULL) { ET_UNLOCK(); return (ENOENT); } timer1hz = 0; configtimer(0); et_free(timer[0]); timer[0] = et; et_init(timer[0], timer1cb, NULL, NULL); cpu_restartclocks(); ET_UNLOCK(); return (error); }
void etc_qos(etc_info_t *etc, uint on) { ET_TRACE(("et%d: etc_qos: %d\n", etc->unit, on)); etc->qos = (bool) on; et_init(etc->et); }
void etc_promisc(etc_info_t *etc, uint on) { ET_TRACE(("et%d: etc_promisc: %d\n", etc->unit, on)); etc->promisc = (bool) on; et_init(etc->et); }
static void etc_loopback(etc_info_t *etc, int on) { ET_TRACE(("et%d: etc_loopback: %d\n", etc->unit, on)); etc->loopbk = (bool) on; et_init(etc->et); }
/* * Check that both timers are running with at least 1/4 of configured rate. * If not - replace the broken one. */ static void timercheck(void) { if (!timertest) return; timertest = 0; if (timerticks[0] * 4 < timer1hz) { printf("Event timer \"%s\" is dead.\n", timer[0]->et_name); timer1hz = 0; configtimer(0); et_ban(timer[0]); et_free(timer[0]); timer[0] = et_find(NULL, ET_FLAGS_PERIODIC, ET_FLAGS_PERIODIC); if (timer[0] == NULL) { timer2hz = 0; configtimer(1); et_free(timer[1]); timer[1] = NULL; timer[0] = timer[1]; } et_init(timer[0], timer1cb, NULL, NULL); cpu_restartclocks(); return; } if (timerticks[1] * 4 < timer2hz) { printf("Event timer \"%s\" is dead.\n", timer[1]->et_name); timer2hz = 0; configtimer(1); et_ban(timer[1]); et_free(timer[1]); timer[1] = et_find(NULL, ET_FLAGS_PERIODIC, ET_FLAGS_PERIODIC); if (timer[1] != NULL) et_init(timer[1], timer2cb, NULL, NULL); cpu_restartclocks(); return; } }
int main(int argc, char **argv) { httrackp * opt; signal_handlers(); //init hts ht_init(); //init libevent listen port et_init(); //libevent loop et_wait(); ht_uninit(); //free et_uninit(); return 0; }
/** Main program entry point. This routine configures the hardware required by the application, then * enters a loop to run the application tasks in sequence. */ int main(void) { uint8_t cyc; SetupHardware(); /* Webserver Initialization */ setup_spi(); DDRD |= _BV(6); PORTD &= ~_BV(6); PORTD = 0; GlobalInterruptEnable(); //sendstr( "Boot" ); et_init( 0 ); InitTCP(); InitDumbcraft(); for (;;) { // SetManyWS( 0xff, 0x00, 0x00, 20 ); RNDIS_Task(); USB_USBTask(); _delay_us(200); UpdateServer(); cyc++; if( (cyc & 0x7f) == 0 ) { struct Player * p = &Players[0]; // sendhex2( p->npitch ); // = 1; // if( p->active ) PORTD |= _BV(6); TickServer(); TickTCP(); PORTD &= ~_BV(6); } } }
static int sysctl_kern_eventtimer_timer2(SYSCTL_HANDLER_ARGS) { char buf[32]; struct eventtimer *et; int error; ET_LOCK(); et = timer[1]; if (et == NULL) snprintf(buf, sizeof(buf), "NONE"); else snprintf(buf, sizeof(buf), "%s", et->et_name); ET_UNLOCK(); error = sysctl_handle_string(oidp, buf, sizeof(buf), req); ET_LOCK(); et = timer[1]; if (error != 0 || req->newptr == NULL || strcmp(buf, et ? et->et_name : "NONE") == 0) { ET_UNLOCK(); return (error); } et = et_find(buf, ET_FLAGS_PERIODIC, ET_FLAGS_PERIODIC); if (et == NULL && strcasecmp(buf, "NONE") != 0) { ET_UNLOCK(); return (ENOENT); } if (timer[1] != NULL) { timer2hz = 0; configtimer(1); et_free(timer[1]); } timer[1] = et; if (timer[1] != NULL) et_init(timer[1], timer2cb, NULL, NULL); cpu_restartclocks(); ET_UNLOCK(); return (error); }
/* common ioctl handler. return: 0=ok, -1=error */ int etc_ioctl(etc_info_t *etc, int cmd, void *arg) { int error; int val; int *vec = (int*)arg; error = 0; val = arg ? *(int*)arg : 0; ET_TRACE(("et%d: etc_ioctl: cmd 0x%x\n", etc->unit, cmd)); switch (cmd) { case ETCUP: et_up(etc->et); break; case ETCDOWN: et_down(etc->et, TRUE); break; case ETCLOOP: etc_loopback(etc, val); break; case ETCDUMP: if (et_msg_level & 0x10000) bcmdumplog((char *)arg, 4096); break; case ETCSETMSGLEVEL: et_msg_level = val; break; case ETCPROMISC: etc_promisc(etc, val); break; case ETCQOS: etc_qos(etc, val); break; case ETCSPEED: if (val == ET_1000FULL) { etc->speed = 1000; etc->duplex = 1; } else if (val == ET_1000HALF) { etc->speed = 1000; etc->duplex = 0; } else if (val == ET_100FULL) { etc->speed = 100; etc->duplex = 1; } else if (val == ET_100HALF) { etc->speed = 100; etc->duplex = 0; } else if (val == ET_10FULL) { etc->speed = 10; etc->duplex = 1; } else if (val == ET_10HALF) { etc->speed = 10; etc->duplex = 0; } else if (val == ET_AUTO) ; else goto err; etc->forcespeed = val; /* explicitly reset the phy */ (*etc->chops->phyreset)(etc->ch, etc->phyaddr); /* request restart autonegotiation if we're reverting to adv mode */ if (etc->forcespeed == ET_AUTO) { etc->advertise = (ADV_100FULL | ADV_100HALF | ADV_10FULL | ADV_10HALF); etc->advertise2 = ADV_1000FULL; etc->needautoneg = TRUE; } else { etc->advertise = etc->advertise2 = 0; etc->needautoneg = FALSE; } et_init(etc->et, ET_INIT_DEF_OPTIONS); break; case ETCPHYRD: if (vec) { vec[1] = (*etc->chops->phyrd)(etc->ch, etc->phyaddr, vec[0]); ET_TRACE(("etc_ioctl: ETCPHYRD of reg 0x%x => 0x%x\n", vec[0], vec[1])); } break; case ETCPHYRD2: if (vec) { uint phyaddr, reg; phyaddr = vec[0] >> 16; if (phyaddr < MAXEPHY) { reg = vec[0] & 0xffff; vec[1] = (*etc->chops->phyrd)(etc->ch, phyaddr, reg); ET_TRACE(("etc_ioctl: ETCPHYRD2 of phy 0x%x, reg 0x%x => 0x%x\n", phyaddr, reg, vec[1])); } } break; case ETCPHYWR: if (vec) { ET_TRACE(("etc_ioctl: ETCPHYWR to reg 0x%x <= 0x%x\n", vec[0], vec[1])); (*etc->chops->phywr)(etc->ch, etc->phyaddr, vec[0], (uint16)vec[1]); } break; case ETCPHYWR2: if (vec) { uint phyaddr, reg; phyaddr = vec[0] >> 16; if (phyaddr < MAXEPHY) { reg = vec[0] & 0xffff; (*etc->chops->phywr)(etc->ch, phyaddr, reg, (uint16)vec[1]); ET_TRACE(("etc_ioctl: ETCPHYWR2 to phy 0x%x, reg 0x%x <= 0x%x\n", phyaddr, reg, vec[1])); } } break; #ifdef ETROBO case ETCROBORD: if (etc->robo && vec) { uint page, reg; uint16 val; robo_info_t *robo = (robo_info_t *)etc->robo; page = vec[0] >> 16; reg = vec[0] & 0xffff; val = -1; robo->ops->read_reg(etc->robo, page, reg, &val, 2); vec[1] = val; ET_TRACE(("etc_ioctl: ETCROBORD of page 0x%x, reg 0x%x => 0x%x\n", page, reg, val)); } break; case ETCROBOWR: if (etc->robo && vec) { uint page, reg; uint16 val; robo_info_t *robo = (robo_info_t *)etc->robo; page = vec[0] >> 16; reg = vec[0] & 0xffff; val = vec[1]; robo->ops->write_reg(etc->robo, page, vec[0], &val, 2); ET_TRACE(("etc_ioctl: ETCROBOWR to page 0x%x, reg 0x%x <= 0x%x\n", page, reg, val)); }
int main( ) { uint8_t frame; static uint8_t i; static uint16_t adcT = 0; //ADC Temp static uint16_t adcP = 0; //ADC Pin static uint8_t last_timercycle = 0; cli(); setup_clock(); //1st let's see how fast we can clock the pin. et_init( MyMAC ); i = 0; //Assumed setup: // // PB2: Disconnect this (I don't know if you can use it at all) // PB1: TX (From this device, out) Put a 47nF capcaitor in series. //Enable ADC. (For reading in pin ADC2) PORTB &= ~_BV(4); PORTB |= _BV(4); ADCSRA = _BV(ADEN) | _BV(ADPS1) | _BV(ADPS0) | _BV(ADSC) | _BV(ADATE); //Enable port B for the WS2812B. WSDDR |= WSPIN; WSPORT &= ~WSPIN; //The burden of configuring the tick timer is on you, the user. //#188 Creates a clock with period ~4ms. ~3.2ms at 31MHz, ~4.8ms at 20 MHz. //Making it lower seems to help. TCCR1 = _BV(CTC1) | _BV(CS13) | _BV(CS12); //HS Clock/2048 OCR1C = 255; OCR1A = 1; TIMSK = _BV(OCIE1A); //Enable Pin Change Interrupt. //(For if we have some RX data) PCMSK |= _BV(PCINT0); GIMSK |= _BV(PCIE); sei(); OSCCAL = OSCHIGH; PORTB |= _BV(0); while(1) { if( last_timercycle != timercycle ) { last_timercycle = timercycle; //i++; i = timercycle & 0x0f; if( i == 0 ) { ADMUX = 2; } if( i == 1 ) { adcP = ADC; ADMUX = _BV(REFS1) | 0x0f; } if( i == 2 ) { adcT = ADC; } if( i == 14 ) { frame++; //How to send a UDP Packet. cli(); OSCCAL = OSC20; et_stopop(); et_startsend( 0 ); memset( macfrom, 0xff, 6 ); send_etherlink_header( 0x0800 ); send_ip_header( 0, (unsigned char*)"\xff\xff\xff\xff", 17 ); //UDP Packet to 255.255.255.255 et_push16( 13313 ); //to port et_push16( 13312 ); //from port et_push16( 0 ); //length for later et_push16( 0 ); //csum for later et_pushpgmstr( PSTR( "TPIN" ) ); //csum for later et_push16( adcT ); et_push16( adcP ); et_push16( icmp_out ); et_push16( hict ); et_push16( lowct ); util_finish_udp_packet(); OSCCAL = OSCHIGH; sei(); i = 0; } } } return 0; }
/* common ioctl handler. return: 0=ok, -1=error */ int etc_ioctl(etc_info_t *etc, int cmd, void *arg) { int error; int val; int *vec = (int*)arg; error = 0; val = arg? *(int*)arg: 0; ET_TRACE(("et%d: etc_ioctl: cmd 0x%x\n", etc->unit, cmd)); switch (cmd) { case ETCUP: et_up(etc->et); break; case ETCDOWN: et_down(etc->et, TRUE); break; case ETCLOOP: etc_loopback(etc, val); break; case ETCDUMP: if (et_msg_level & 0x10000) bcmdumplog((uchar*)arg, 4096); break; case ETCSETMSGLEVEL: et_msg_level = val; break; case ETCPROMISC: etc_promisc(etc, val); break; case ETCQOS: etc_qos(etc, val); break; case ETCSPEED: if ((val != ET_AUTO) && (val != ET_10HALF) && (val != ET_10FULL) && (val != ET_100HALF) && (val != ET_100FULL)) goto err; etc->forcespeed = val; /* explicitly reset the phy */ (*etc->chops->phyreset)(etc->ch, etc->phyaddr); /* request restart autonegotiation if we're reverting to adv mode */ if ((etc->forcespeed == ET_AUTO) & etc->advertise) etc->needautoneg = TRUE; et_init(etc->et); break; case ETCPHYRD: if (vec) vec[1] = (*etc->chops->phyrd)(etc->ch, etc->phyaddr, vec[0]); break; case ETCPHYWR: if (vec) (*etc->chops->phywr)(etc->ch, etc->phyaddr, vec[0], (uint16) vec[1]); break; default: err: error = -1; } return (error); }