void generator () { int8_t ret; uint8_t i; printf("Hello from DV/DRP test!\n"); net_init(); dvdrp_init(); // main_t = mos_thread_current(); com_ioctl(IFACE_RADIO, CC1000_TX_POWER, 1); //indexes don't neet to match name values attribs[TEMP_VALUE].name = TEMP_VALUE; attribs[LIGHT_VALUE].name = LIGHT_VALUE; attribs[NODEID_VALUE].name = NODEID_VALUE; //fill in data attribs[NODEID_VALUE].value = mos_node_id_get (); ret = net_proto_set(DVDRP_PROTO_ID); if(ret == NET_PROTO_INVALID) { printf("Invalid proto\n"); } alarm.func = wakeup; i = 0; while(1) { stack_buf.size = 0; dev_mode (DEV_MICA2_TEMP, DEV_MODE_ON); attribs[TEMP_VALUE].value = 0; dev_read (DEV_MICA2_TEMP, &attribs[TEMP_VALUE].value, 1); attribs[LIGHT_VALUE].value = 0; dev_read (DEV_MICA2_LIGHT, &attribs[LIGHT_VALUE].value, 1); dev_mode (DEV_MICA2_LIGHT, DEV_MODE_OFF); //1 = # of attributes printf("sending, light is %C.\n", attribs[LIGHT_VALUE].value); ret = net_event_send(1, &stack_buf, ATTRIB_COUNT, &attribs); if(ret) //ret == 0 if no valid routes mos_led_on(0); //mos_led_toggle(1); // printf("crc errs: %d ",cc1000_get_crc_errors()); // printf("sync errs: %d \n",cc1000_get_sync_errors()); // timeout_attach(t, wakeup); // mos_thread_suspend(); mos_alarm(&alarm, 1, 0); mos_sem_wait(&wakeup_sem); } printf("End test.\n"); }
void start(void) { uint16_t count; uint8_t index; buffer = (char *)mos_mem_alloc((uint16_t)BUFFER_SIZE); memset(buffer, 'X', BUFFER_SIZE); /* For safety, erase entire FLASH */ dev_ioctl(DEV_TELOS_FLASH, TELOS_FLASH_BULK_ERASE); /* Turn on the FLASH */ dev_mode(DEV_TELOS_FLASH, DEV_MODE_ON); /* Acquire lock on FLASH and preliminarly * write 64 bits of data */ dev_open(DEV_TELOS_FLASH); count = dev_write(DEV_TELOS_FLASH, "abcdefgh", 8); printf("%d bytes of data have been written to FLASH memory\n", count); dev_close(DEV_TELOS_FLASH); /* Perform experiments over R/W pointer to FLASH */ /* Experiment#1 - flash is on, lock is free; aquire lock and read * without using any SEEK function */ dev_open(DEV_TELOS_FLASH); count = dev_read(DEV_TELOS_FLASH, buffer, 1); printf("#1 : %c has been read from FLASH memory\n", buffer[0]); dev_close(DEV_TELOS_FLASH); /* Move pointer */ dev_open(DEV_TELOS_FLASH); dev_ioctl(DEV_TELOS_FLASH, DEV_SEEK, 3); dev_close(DEV_TELOS_FLASH); /* Experiment#2 - flash is on, lock is free; aquire lock, read single data, * write single data and read multiple data from it */ dev_open(DEV_TELOS_FLASH); dev_read(DEV_TELOS_FLASH, buffer, 1); printf("#2 : %c has been read\n" " : FLASH memory written\n", buffer[0]); dev_write(DEV_TELOS_FLASH, "l", 1); count = dev_read(DEV_TELOS_FLASH, buffer, 1); printf(" : %d bytes have been read from FLASH memory: ", count); for(index = 0; index < count; index++) { printf("%c ", buffer[index]); } printf("\n"); dev_close(DEV_TELOS_FLASH); /* Release lock and free resources */ dev_close(DEV_TELOS_FLASH); dev_mode(DEV_TELOS_FLASH, DEV_MODE_OFF); mos_mem_free(buffer); return; }
void get_battery() { uint16_t voltage; uint16_t high_val; uint16_t low_val; dev_open(DEV_MICA2_BATTERY); dev_mode(DEV_MICA2_BATTERY, DEV_MODE_ON); //mos_mdelay(200); dev_read(DEV_MICA2_BATTERY, &voltage, sizeof(voltage)); high_val = voltage / 1000; low_val = (uint16_t)(voltage - (1000 * high_val)); dev_mode(DEV_MICA2_BATTERY, DEV_MODE_OFF); dev_close(DEV_MICA2_BATTERY); printf("Battery is at %d.%d volts, %d\n", high_val, low_val, voltage); //printf("Battery is ADC level is at %d bits\n", voltage); }
trap_retval ReqRead_user_keyboard( void ) { struct _console_ctrl *con; unsigned con_num; int con_hdl; int con_mode; char chr; //NYI: what about QNX windows? static char con_name[] = "/dev/conXX"; unsigned timeout; read_user_keyboard_req *acc; read_user_keyboard_ret *ret; # define FIRST_DIGIT (sizeof( con_name ) - 3) acc = GetInPtr( 0 ); ret = GetOutPtr( 0 ); timeout = acc->wait * 10; if( timeout == 0 ) timeout = -1; ret->key = '\0'; con = console_open( 2, O_WRONLY ); if( con == NULL ) { return( sizeof( *ret ) ); } con_num = console_active( con, -1 ); console_close( con ); con_name[ FIRST_DIGIT + 0 ] = (con_num / 10) + '0'; con_name[ FIRST_DIGIT + 1 ] = (con_num % 10) + '0'; con_hdl = open( con_name, O_RDONLY ); if( con_hdl < 0 ) { if( timeout == -1 ) timeout = 50; sleep( timeout / 10 ); return( sizeof( *ret ) ); } con_mode = dev_mode( con_hdl, 0, _DEV_MODES ); if( dev_read( con_hdl, &chr, 1, 1, 0, timeout, 0, 0 ) == 1 ) { if( chr == 0xff ) { read( con_hdl, &chr, 1 ); chr = '\0'; } ret->key = chr; } dev_mode( con_hdl, con_mode, _DEV_MODES ); close( con_hdl ); return( sizeof( *ret ) ); }
void start(void) { // get an exclusive lock on the GPS driver. this is // actually unnecessary, since no other threads are // present/trying to use the GPS, but it's good // practice. dev_open(DEV_MICA2_GPS); dev_mode(DEV_MICA2_GPS, DEV_MODE_ON); // tell the driver that we want to read a gps_gga_t // structure when we call dev_read(). The other // option is to read a string which may or may not // be GGA data (ie, might be some other packet type) dev_ioctl(DEV_MICA2_GPS, MICA2_GPS_READ_GGA); while(1) { // read the next available GGA packet into our structure. dev_read(DEV_MICA2_GPS, &gga, sizeof(gga)); // only print gga packets which contain some information if (gga.satellite_count > 0) { // print the data. this function parses all the // information for us, so that we don't have to. mica2_gps_print_gga(&gga); } else { printf("no satellites found; sleeping.\n"); // send the system to idle mode for 5 seconds. mos_thread_sleep(5000); } } // if the while loop above were not infinite, // it would be good practice to turn off the device // and release our exclusive lock on it. dev_mode(DEV_MICA2_GPS, DEV_MODE_OFF); dev_close(DEV_MICA2_GPS); }
void send_packet(uint8_t packet_id) { static uint8_t inited = 0; uint16_t temp; uint16_t hum; if (!inited) { inited = 1; dev_open(DEV_MSP_HUMIDITY); if (dev_mode(DEV_MSP_HUMIDITY, DEV_MODE_ON) == DEV_FAILURE) { no_sensor = 1; } } mos_mutex_lock(&send_mutex); if (no_sensor) { temp = rand(); hum = rand(); } else { dev_read(DEV_MSP_TEMPERATURE, &temp, sizeof(temp)); dev_read(DEV_MSP_HUMIDITY, &hum, sizeof(hum)); } buf.size = 13; buf.data[0] = packet_id; buf_insert_WORD(buf.data, 1, mos_node_id_get()); buf_insert_WORD(buf.data, 3, temp); buf_insert_WORD(buf.data, 5, hum); if (no_sensor) { buf_insert_WORD(buf.data, 7, rand()); buf_insert_WORD(buf.data, 9, rand()); } else { buf_insert_WORD(buf.data, 7, adc_get_conversion16(4)); buf_insert_WORD(buf.data, 9, adc_get_conversion16(5)); } buf_insert_WORD(buf.data, 11, crc_compute(buf.data, 11)); com_send(IFACE_RADIO, &buf); mos_mutex_unlock(&send_mutex); }
void start(void) { mos_led_on(1); #ifdef PLATFORM_MICA_ANY dev_mode(DEV_MICA2_LIGHT, DEV_MODE_ON); dev_read(DEV_MICA2_LIGHT,&seed,1); dev_read(DEV_MICA2_LIGHT,(&seed)+1,1); dev_mode(DEV_MICA2_LIGHT, DEV_MODE_OFF); #elif PLATFORM_TELOSB seed = adc_get_conversion16(4); #endif printf("SEED: %x\n", seed); mos_led_off(1); srand(seed); producer1_alarm.func = sem_alarm_func; consumer1_alarm.func = sem_alarm_func; producer2_alarm.func = sem_alarm_func; consumer2_alarm.func = sem_alarm_func; producer1_alarm.data = (void*)&full1; consumer1_alarm.data = (void*)&empty1; producer2_alarm.data = (void*)&full2; consumer2_alarm.data = (void*)&empty2; mos_sem_init(&empty1,5); mos_sem_init(&full1,0); mos_sem_init(&mux1,1); mos_sem_init(&empty2,5); mos_sem_init(&full2,0); mos_sem_init(&mux2,1); mos_thread_new(consumer2, 128, PRIORITY_NORMAL); mos_thread_new(consumer1, 128, PRIORITY_NORMAL); mos_thread_new(producer2, 128, PRIORITY_NORMAL); mos_thread_new(producer1, 128, PRIORITY_NORMAL); }
void poll() { uint8_t i; uint16_t j; uint8_t x, y; dev_mode(DEV_ADC, DEV_MODE_ON); for(i = 0; i < 40; i++) { dev_ioctl(DEV_ADC, ADC_SET_CHANNEL, 1); dev_read(DEV_ADC, &x, sizeof(x)); dev_ioctl(DEV_ADC, ADC_SET_CHANNEL, 2); dev_read(DEV_ADC, &y, sizeof(y)); printf("%C\t%C\n",x,y); for(j=0;j<0xffff;j++); for(j=0;j<0xffff;j++); for(j=0;j<0xffff;j++); for(j=0;j<0xffff;j++); } }
void start(void) { uint8_t index; uint16_t delay; uint16_t val; pkt.size = 16; //turn on rssi dev_mode(DEV_AVR_RSSI, DEV_MODE_ON); while(1) { //receive a bunch of samples, then send for (index = 0; index < 16; index++) { //take sample dev_read (DEV_AVR_RSSI, &val, 2); printf("%d ", val); //wait //mos_mdelay(200); } printf("\n"); } }
void sense_thread() { uint8_t i=0,a; uint32_t hu,te,lt1,lt2; dev_open(DEV_MSP_HUMIDITY); if (dev_mode(DEV_MSP_HUMIDITY, DEV_MODE_ON) == DEV_FAILURE) { printf("SHT15 failed to initialize\n"); return; } mos_thread_set_suspend_state(SUSPEND_STATE_SLEEP); //sleep to let the initialization on the reader finish mos_thread_sleep(1000); // printf("Temperature, Humidity, Light1, Light2, Possible temp, Possible humid,\n"); send_buf.size=12; while(1) { dev_read(DEV_MSP_TEMPERATURE, (void*)&lasttemp, sizeof(uint16_t)); dev_read(DEV_MSP_HUMIDITY, (void*)&lasthumid, sizeof(uint16_t)); temp[i]=lasttemp; humid[i]=lasthumid; lastlight1 = adc_get_conversion16(4); lastlight2 = adc_get_conversion16(5); light1[i]=lastlight1; light2[i]=lastlight2; if(i==7)//every 8th time, agregate and send. { hu=0; te=0; lt1=0; lt2=0; for(a=0;a<8;a++) { te+=temp[a]; hu+=humid[a]; lt1+=light1[a]; lt2+=light2[a]; } //divide by 8 lt1=lt1>>3; lt2=lt2>>3; te=te>>3; hu=hu>>3; //CSV format if(temp_conversion[te>>6]>=0) printf("%l,%l,%l,%l,%d,%d\n", te, hu, lt1, lt2,temp_conversion[te>>6],humid_conversion[hu>>5]); else printf("%l,%l,%l,%l,-%d,%d\n", te, hu, lt1, lt2,-temp_conversion[te>>6],humid_conversion[hu>>5]); mos_led_on(2); com_send(IFACE_RADIO,&send_buf); mos_thread_sleep(381); mos_led_off(2); }
int __attribute__ ((section (".data"))) reprogram_helper(uint32_t ex_flash_addr, int type) { uint32_t addr = ex_flash_addr; uint16_t c_checksum; uint16_t a_checksum; boolean write = (type == RP_WRITE); if (write) mos_disable_ints(); dev_open(DEV_TELOS_FLASH); dev_mode(DEV_TELOS_FLASH, DEV_MODE_ON); if (!write) printf("verifying...\n"); if (write) erase_flash(0x4000, MSP_FLASH_ERASE_ALL); dev_ioctl(DEV_TELOS_FLASH, DEV_SEEK, addr); dev_read(DEV_TELOS_FLASH, buffer, 6); uint16_t sect_count = *(uint16_t*)(buffer + 4); //printf("%d sectors to write\n", sect_count); //printf("looking for file at %x\n", (uint16_t)ex_flash_addr); uint16_t sect_size; uint16_t sect_addr; // skip past header addr += 6; dev_ioctl(DEV_TELOS_FLASH, DEV_SEEK, addr); int i; for(i = 0; i < sect_count; ++i) { // read the sector size LED_ON(i + 1); dev_ioctl(DEV_TELOS_FLASH, DEV_SEEK, addr); dev_read(DEV_TELOS_FLASH, §_size, 2); addr += 2; dev_ioctl(DEV_TELOS_FLASH, DEV_SEEK, addr); // read the sector address dev_read(DEV_TELOS_FLASH, §_addr, 2); addr += 2; // begin computing checksum; c_checksum = 0; c_checksum ^= sect_size; c_checksum ^= sect_addr; //printf("[%d] %d bytes at address %x\n", i, sect_size, sect_addr); uint16_t blocks = sect_size / MSP_FLASH_BLOCK_SIZE; uint16_t left_over = sect_size % MSP_FLASH_BLOCK_SIZE; while(blocks--) { dev_ioctl(DEV_TELOS_FLASH, DEV_SEEK, addr); dev_read(DEV_TELOS_FLASH, buffer, sizeof(buffer)); msp_flash_address = sect_addr; if (write) dev_write(DEV_MSP_FLASH, buffer, sizeof(buffer)); else { uint16_t* iter = (uint16_t*)buffer; uint16_t* iter_end = (uint16_t*)(buffer + sizeof(buffer)); for( ; iter != iter_end; ++iter) c_checksum ^= *iter; } sect_addr += sizeof(buffer); addr += sizeof(buffer); } if (left_over) { dev_ioctl(DEV_TELOS_FLASH, DEV_SEEK, addr); dev_read(DEV_TELOS_FLASH, buffer, left_over); msp_flash_address = sect_addr; if (write) dev_write(DEV_MSP_FLASH, buffer, left_over); else { uint16_t* iter = (uint16_t*)buffer; uint16_t* iter_end = (uint16_t*)(buffer + left_over); for( ; iter != iter_end; ++iter) c_checksum ^= *iter; } addr += left_over; } dev_ioctl(DEV_TELOS_FLASH, DEV_SEEK, addr); dev_read(DEV_TELOS_FLASH, &a_checksum, sizeof(a_checksum)); //printf("checksum (computed): %x\n checksum (actual): %x\n", c_checksum, a_checksum); if (!write) { if (c_checksum != a_checksum) return FALSE; } addr += 2; dev_ioctl(DEV_TELOS_FLASH, DEV_SEEK, addr); } if (write) asm volatile ("br #0x4000h\n"); // shhh //dev_close(DEV_TELOS_FLASH); return TRUE; }
int main( int argc, char *argv[] ) { int opt, file_count = 0; pname = basename(argv[0]); if ( argc < 2 ) { Puterr( "insufficient arguments\n" ); exit(1); } prefix[0] = '\0'; /* No prefix and forced file name off by default */ logfile[0] = '\0'; /* No activity log file by default */ force = newer = relaxed = today = verbose = quiet = qterm = FALSE; make_dir = TRUE; onesec = ONESEC; retry_limit = RETRY_MAX; if (optind==0) /* 16-bit */ ++optind; /* Skip over command name */ if ( *argv[optind] == '/' ) { /*-- Check for a device over-ride ----*/ if ( ( mdm = open( argv[1], O_RDWR ) ) == -1 ) { Puterr( "unable to open device '%s'\n", argv[1] ); exit( 1 ); } ++optind; /* Skip over specified device name */ } else { /*-- No device override so use stdin/stdout to communicate ----*/ struct stat statbuf; strcpy( device, ttyname( 0 ) ); if( fstat( 0, &statbuf ) != -1 && S_ISSOCK(statbuf.st_mode) ) { socket = 1; mdm = 0; } else if ( ( mdm = open( device, O_RDWR ) ) == -1 ) { Puterr( "unable to open stdin as r/w\n" ); exit( 1 ); } use_stdin = TRUE; } signal( SIGALRM, (void *)&timeout ); init_mdm(); /* Setup the selected modem port and initialize */ /* any variables associated with the protocol. */ if ( ( *argv[optind] | ' ' ) == 'r') sending = FALSE; else if ( ( *argv[optind] | ' ' ) == 's' && argc > 2 ) sending = TRUE; else { Puterr( "Invalid argument '%s' (must be 'se' or 're')\n", argv[optind] ); exit( 1 ); } ++optind; /*---- Ready for command line options ----*/ while( ( opt = getopt( argc, argv, "FmnqrtuvVQf:l:p:s:" ) ) != -1 ) { switch( opt ) { case 'F': flashfile = TRUE; break; case 'f': strcpy( prefix, optarg ); force = TRUE; break; case 'l': strcpy( logfile, optarg ); break; case 'm': make_dir = FALSE; break; case 'n': newer = TRUE; break; case 'r': relaxed = TRUE; onesec <<= 2; /* Timeouts 4 times larger */ retry_limit <<= 1; /* Twice as many retries */ break; case 's': sub_packet_size = atoi( optarg ); sub_packet_size >>= 3; /* Divide by 8 for sub_packet */ if ( !sub_packet_size ) { Puterr( "packet size too small\n" ); exit( 1 ); } if ( sub_packet_size > MAXSIZE ) { Puterr( "packet size too large (max %d)\n", MAXSIZE*8); exit( 1 ); } break; case 't': today = TRUE; break; case 'u': unlink_file = TRUE; break; case 'V': case 'v': verbose = TRUE; break; case 'q': quiet = TRUE; break; case 'Q': qterm = TRUE; break; case 'p': strcpy( prefix, optarg ); break; case '?': if ( !extra_option( (char *)&optopt ) ) { Puterr( "unknown option '%s'\n", optopt ); exit( 1 ); } break; default: ++file_count; /* Count filenames */ continue; /* but don't null them */ } } if ( file_count > 1 ) multi_send_check(); /* Check if multi-send OK */ final_init(); /* Last changes before starting */ #ifndef __QNXNTO__ if ( !use_stdin ) dev_mode( 0, 0, _DEV_MODES ); /* Raw console input for abort requests */ #endif if ( sending ) send_files( argc - optind, (signed char **)(argv + optind) ); else receive_files( argc - optind, (signed char **)(argv + optind) ); done( 0 ); /* Protocol dependant termination routine */ return 0; }
void start(void) { mos_led_on(1); #ifdef PLATFORM_MICA_ANY dev_mode(DEV_MICA2_LIGHT, DEV_MODE_ON); dev_read(DEV_MICA2_LIGHT,&seed,1); dev_read(DEV_MICA2_LIGHT,(&seed)+1,1); dev_mode(DEV_MICA2_LIGHT, DEV_MODE_OFF); #elif PLATFORM_TELOSB seed = adc_get_conversion16(4); #endif printf("SEED: %x\n", seed); mos_led_off(1); srand(seed); fired1=1; fired2=1; fired3=1; fired4=1; alarm1.func = sem_alarm_func; alarm2.func = sem_alarm_func; alarm3.func = sem_alarm_func; alarm4.func = sem_alarm_func; alarm1.data = (void*)&fired1; alarm2.data = (void*)&fired2; alarm3.data = (void*)&fired3; alarm4.data = (void*)&fired4; alarm1.reset_to = 0; alarm2.reset_to = 0; alarm3.reset_to = 0; alarm4.reset_to = 0; while(1) { if(fired1 == 1) { alarm1.msecs = (((uint16_t)rand() % 100) + 1); fired1=0; mos_alarm(&alarm1); mos_led_toggle(0); #ifdef verbose printf("1 fired!\n"); #endif } if(fired2 == 1) { alarm2.msecs = (((uint16_t)rand() % 100) + 1); fired2=0; mos_alarm(&alarm2); mos_led_toggle(1); #ifdef verbose printf("2 fired!\n"); #endif } if(fired3 == 1) { alarm3.msecs = (((uint16_t)rand() % 100) + 1); fired3=0; mos_alarm(&alarm3); mos_led_toggle(2); #ifdef verbose printf("3 fired!\n"); #endif } if(fired4 == 1) { alarm4.msecs = (((uint16_t)rand() % 100) + 1); fired4=0; mos_alarm(&alarm4); // mos_led_toggle(2); #ifdef verbose printf("4 fired!\n"); #endif } #ifdef verbose print_clock_list(); #endif } }
void get_light() { dev_mode(DEV_MICA2_LIGHT, DEV_MODE_ON); get_val(DEV_MICA2_LIGHT); }