bool memory_free(void* alloc_memory) { // WARNING! 由于没有对数据进行清理,所以会有内存泄漏.. 2014/7/2 memory_alloc_header alloc_header; memory_alloc_ender alloc_ender; memory_alloc_header last_alloc_header; memory_alloc_ender last_alloc_ender; memory_address null_address=(memory_address)INVALID_MEMORY_ADDRESS; if (INVALID_MEMORY_ADDRESS==(int)alloc_memory) return false; if (INVALID_MEMORY_ADDRESS!=(int)first_alloc) { sram_read(first_alloc,&alloc_header,sizeof(memory_alloc_header)); while(true) { sram_read((void*)((long)alloc_header.alloc_address+alloc_header.length),&alloc_ender,sizeof(memory_alloc_ender)); if (alloc_header.alloc_address==alloc_memory) { if (INVALID_MEMORY_ADDRESS==(long)alloc_header.point_last && INVALID_MEMORY_ADDRESS==(long)alloc_ender.point_next) { first_alloc=(point_memory_alloc_header)INVALID_MEMORY_ADDRESS; } else if (INVALID_MEMORY_ADDRESS==(long)alloc_header.point_last && INVALID_MEMORY_ADDRESS!=(long)alloc_ender.point_next) { first_alloc=alloc_ender.point_next; sram_write(alloc_ender.point_next,&null_address,sizeof(memory_address)); } else if (INVALID_MEMORY_ADDRESS!=(long)alloc_header.point_last && INVALID_MEMORY_ADDRESS!=(long)alloc_ender.point_next) { sram_read(alloc_header.point_last,&last_alloc_header,sizeof(memory_alloc_header)); sram_read((void*)((long)last_alloc_header.alloc_address+last_alloc_header.length),&last_alloc_ender,sizeof(memory_alloc_ender)); last_alloc_ender.point_next=alloc_ender.point_next; sram_write((void*)((long)last_alloc_header.alloc_address+last_alloc_header.length),&last_alloc_ender,sizeof(memory_alloc_ender)); sram_write(alloc_ender.point_next,&alloc_header.point_last,sizeof(memory_address)); } else if (INVALID_MEMORY_ADDRESS!=(long)alloc_header.point_last && INVALID_MEMORY_ADDRESS==(long)alloc_ender.point_next) { sram_write((void*)((long)alloc_header.point_last+sizeof(memory_alloc_header)+alloc_header.length),&null_address,sizeof(memory_address)); } memory_statistics_calculate(-(sizeof(alloc_header)+alloc_header.length+sizeof(memory_alloc_ender))); return true; } if (INVALID_MEMORY_ADDRESS==(long)alloc_ender.point_next) break; sram_read(alloc_ender.point_next,&alloc_header,sizeof(memory_alloc_header)); } } return false; }
void* memory_alloc(const long memory_alloc_length) { memory_alloc_header new_alloc_header; memory_alloc_ender new_alloc_ender; memory_alloc_header find_alloc_header; memory_alloc_ender find_alloc_ender; memory_address exchange_address=(memory_address)INVALID_MEMORY_ADDRESS; long memory_block_distance=0; long new_alloc_length=sizeof(memory_alloc_header)+memory_alloc_length+sizeof(memory_alloc_ender); if (0<memory_alloc_length && new_alloc_length<kernel_memory_statistics.memory_available) { if (INVALID_MEMORY_ADDRESS!=(int)first_alloc) { sram_read(first_alloc,&find_alloc_header,sizeof(memory_alloc_header)); sram_read((void*)((long)find_alloc_header.alloc_address+find_alloc_header.length),&find_alloc_ender,sizeof(memory_alloc_ender)); while (INVALID_MEMORY_ADDRESS!=(long)find_alloc_ender.point_next) { memory_block_distance=(long)find_alloc_ender.point_next-(long)find_alloc_header.alloc_address-find_alloc_header.length-sizeof(memory_alloc_ender); if (new_alloc_length<=memory_block_distance) { new_alloc_header.point_last=(void*)((long)find_alloc_header.alloc_address-sizeof(memory_alloc_header)); new_alloc_header.alloc_address=(void*)(SRAM_ADDRESS_START+(long)find_alloc_header.alloc_address+find_alloc_header.length+sizeof(memory_alloc_ender)+sizeof(memory_alloc_header)); new_alloc_header.length=memory_alloc_length; new_alloc_header.alloc_header_flag='F'; new_alloc_header.data_protect=READ_WRITE; new_alloc_header.data_share=SHARE_NO; new_alloc_header.data_kernel=0; sram_write((void*)((long)new_alloc_header.alloc_address-sizeof(memory_alloc_header)),&new_alloc_header,sizeof(memory_alloc_header)); new_alloc_ender.point_next=(void*)(SRAM_ADDRESS_START+(unsigned long)find_alloc_ender.point_next); sram_write((void*)((long)new_alloc_header.alloc_address+new_alloc_header.length),&new_alloc_ender,sizeof(memory_alloc_ender)); exchange_address=(memory_address)((long)new_alloc_header.alloc_address-sizeof(memory_alloc_header)); sram_write(find_alloc_ender.point_next,&exchange_address,sizeof(memory_address)); sram_write((void*)((long)find_alloc_header.alloc_address+find_alloc_header.length),&exchange_address,sizeof(memory_address)); memory_statistics_calculate(sizeof(memory_alloc_header)+memory_alloc_length+sizeof(memory_alloc_ender)); return new_alloc_header.alloc_address; } sram_read(find_alloc_ender.point_next,&find_alloc_header,sizeof(memory_alloc_header)); sram_read((void*)((long)find_alloc_header.alloc_address+find_alloc_header.length),&find_alloc_ender,sizeof(memory_alloc_ender)); } if (new_alloc_length<=(kernel_memory_statistics.memory_size-((long)find_alloc_header.alloc_address+find_alloc_header.length+sizeof(memory_alloc_ender)))) { new_alloc_header.point_last=(void*)((long)find_alloc_header.alloc_address-sizeof(memory_alloc_header)); new_alloc_header.alloc_address=(void*)(SRAM_ADDRESS_START+(long)find_alloc_header.alloc_address+find_alloc_header.length+sizeof(memory_alloc_ender)+sizeof(memory_alloc_header)); new_alloc_header.length=memory_alloc_length; new_alloc_header.alloc_header_flag='F'; new_alloc_header.data_protect=READ_WRITE; new_alloc_header.data_share=SHARE_NO; new_alloc_header.data_kernel=0; sram_write((void*)((long)new_alloc_header.alloc_address-sizeof(memory_alloc_header)),&new_alloc_header,sizeof(memory_alloc_header)); new_alloc_ender.point_next=(void*)INVALID_MEMORY_ADDRESS; sram_write((void*)((long)new_alloc_header.alloc_address+new_alloc_header.length),&new_alloc_ender,sizeof(memory_alloc_ender)); exchange_address=(memory_address)((long)new_alloc_header.alloc_address-sizeof(new_alloc_header)); sram_write((void*)((long)find_alloc_header.alloc_address+find_alloc_header.length),&exchange_address,sizeof(memory_address)); memory_statistics_calculate(sizeof(memory_alloc_header)+memory_alloc_length+sizeof(memory_alloc_ender)); return new_alloc_header.alloc_address; } return (void*)INVALID_MEMORY_ADDRESS; } first_alloc=0; new_alloc_header.point_last=(void*)INVALID_MEMORY_ADDRESS; new_alloc_header.alloc_address=(void*)(SRAM_ADDRESS_START+sizeof(memory_alloc_header)); new_alloc_header.length=memory_alloc_length; new_alloc_header.alloc_header_flag='F'; new_alloc_header.data_protect=READ_WRITE; new_alloc_header.data_share=SHARE_NO; new_alloc_header.data_kernel=0; sram_write(0,&new_alloc_header,sizeof(memory_alloc_header)); new_alloc_ender.point_next=(void*)INVALID_MEMORY_ADDRESS; sram_write((void*)((long)new_alloc_header.alloc_address+memory_alloc_length),&new_alloc_ender,sizeof(memory_alloc_ender)); memory_statistics_calculate(sizeof(memory_alloc_header)+memory_alloc_length+sizeof(memory_alloc_ender)); return new_alloc_header.alloc_address; } return (void*)INVALID_MEMORY_ADDRESS; }
int main() { //uint8_t test_result=0; //************************( 1 )******************************************************* DDRD|=(1<<6)|(1<<7);//set LED pins as output //PORTD&=~(1<<3);//vibration off uart_init();//Keep this as first init so that text can be sent out in others uart_puts("Starting up...."); _delay_ms(500); PORTD|=1<<6; _delay_ms(500); PORTD&=~(1<<6); spi_init(); //initialize SPI bus as master init_tcnt2();//set up timer (RTC) init_twi(); //initialize TWI interface sei(); uart_puts("Pre Init..."); PORTB&=~(1<<sensor1_cs);//select sensor SPIinit_MPU(MPU9250_FULL_SCALE_4G,MPU9250_GYRO_FULL_SCALE_500DPS);//init sensor PORTB|=(1<<sensor1_cs);//deselect sensor //init_MPU(MPU9250_FULL_SCALE_4G,MPU9250_GYRO_FULL_SCALE_500DPS, MPU9250_DEFAULT_ADDRESS); //initialize the 9axis sensor //init_MPU(0,0, 0xD1); //initialize the 9axis sensor //init_MPU(MPU9250_FULL_SCALE_4G,MPU9250_GYRO_FULL_SCALE_500DPS, MPU9250_DEFAULT_ADDRESS); //initialize the 9axis sensor sram_init();//initialize sram uart_puts("Post Init..."); //vibrate(100); //Send feedback showing complete setup PORTD |=(1<<6)|(1<<7); PORTD &=~(1<<7); _delay_ms(200); PORTD |=(1<<7); PORTD &=~(1<<6);// blinks both lights to show the program is starting _delay_ms(200); PORTD |=(1<<6); _delay_ms(1000); record_shot(); while(1){} //*****************STOP POINT********************** /* test_result = test_com(0, MPU9250_DEFAULT_ADDRESS); if(test_result) { uart_puts("MPU Status: OK\n"); } else uart_puts("MPU Status: FAILURE... You suck!\n"); */ char rx_char; uint16_t i=0;//used for for loops //*****Fix me******* //should set to int 0 not char '0' but the ascii zero prints better for now for(i=0;i<512;i++){sd_buf[i]='0';}//sets inital buffer to zero values uint16_t shot_count=0; while(1) { //************************( 2 )******************************************************* PORTD &=~(1<<7); _delay_ms(2000);//Show red light for 2 sec, then turn green and start shot PORTD |=(1<<7); PORTD &=~(1<<6); //************************( 3 )******************************************************* //vibrate(100); record_shot();//record a shot char shots_s[10]; itoa(num_records,shots_s,10); uart_puts("In 20 seconds The number of records was: "); uart_puts(shots_s); uart_putc('\n'); print_shot(); shot_count++; itoa(shot_count,shots_s,10); uart_puts("Shot Number: "); uart_puts(shots_s); uart_putc('\n'); //print_shot(); PORTD |=(1<<6);//turn off light //vibrate(100);_delay_ms(100);vibrate(100); //Double vibration showing end of shot //check_voltage();//Check system voltage _delay_ms(5000);//wait 60 seconds //************************( 6 )******************************************************* continue; //start over and take another shot uart_puts("Starting Testing\n\n"); uart_putc('\r'); rx_char=uart_getc(); if(rx_char=='c') { uart_puts("Command line:\n\n"); rx_char=uart_getc(); while(rx_char!='c') { if(rx_char=='s'){} if(rx_char=='w'){} if(rx_char=='r'){}//buffer gets set to sector!! if(rx_char=='i'){} rx_char=uart_getc(); } } // uint8_t i=0; //Load values into 10 memory locations //Zero is only skipped to avoid loading zero into the byte //since when the byte is read back, zero could also mean communication failed for(i=48;i<=57;i++) { sram_write(i,add_m,add_h,i);//only lower address byte is incremented //uart_putc(i); }//end loading for loop //change value in one spot in array as build in "error" sram_write(50,add_m,add_h,100); //check values in first 10 memory locations, one should be "wrong" for(i=48;i<57;i++) { _delay_ms(100); PORTB |=(1<<1)|(1<<2); _delay_ms(100); if (sram_read(i,add_m,add_h)==i) { PORTB &=~(1<<1); uart_puts("passed\n"); }//Byte read back correct GREEN light else{PORTB &=~(1<<2);uart_puts("FAILED!!\n");}//Byte Read back was incorrect RED light }//end for loop for checking values PORTB |=(1<<1)|(1<<2);//both lights off uart_puts("****Tesst finished*****\r\r"); _delay_ms(1500);//wait 1.5 seconds before starting again } //end while } //end main
int main(int argc, char *argv[]) { off_t offset; uint16_t ored; uint16_t val; int i; int n; int stepsz; int write; int error; if (argc < 4) { fprintf(stderr, "Usage: sram <r/w> <n> <stepsz> [orv]\n"); exit(1); } if (argc == 5) ored = (int16_t)strtol(argv[4], NULL, 16); else ored = 0; n = atoi(argv[2]); stepsz = atoi(argv[3]); if (*argv[1] == 'w') { write = 1; printf("Write mode enabled \n"); } else { write = 0; printf("Read mode enabled \n"); } if ((error = sram_open())) { perror("sram_open"); exit(1); } printf("SRAM opened!\n"); sleep(1); for (i = 0; i < n; i++) { offset = i * (stepsz <<1); if (write) { val = (uint16_t)((i<<1) | ored); if ((error = sram_write(offset, &val, sizeof(val)))) { fprintf(stderr, "Error during sram_write\n"); exit(1); } } else { if ((error = sram_read(offset, &val, sizeof(val)))) { fprintf(stderr, "Error during sram_read\n"); exit(1); } printf("@%#lx: %hx\n", (unsigned long)offset, val); } sleep(1); } sleep(1); sram_close(); return 0; }
void record_shot() { int16_t ax=0,ay=0,az=0; // int16_t gx,gy,gz; // int16_t mx,my,mz; add_l=0; add_m=0;//reset addresses to zero add_h=0; data_count=0; uart_puts("Shot Started...\n"); //get 20 seconds of data //each sensor is 14 bytes of data //each byte is roughtly 2.5 ms //uint16_t i=0; //int16_t x=0; uint8_t data[50]; char addL[10]; //char addM[5]; //char addH[5]; count_t=0; num_records=0; while(1) { _delay_ms(500); PORTB&=~(1<<sensor1_cs);//select sensor SPIgetAccel(&ax,&ay,&az); PORTB|=(1<<sensor1_cs);//deselect sensor //getAccel(&ax,&ay,&az,MPU9250_DEFAULT_ADDRESS); itoa(ax,addL,10); uart_puts("X axis: "); uart_puts(addL); itoa(ay,addL,10); uart_puts("Y axis: "); uart_puts(addL); itoa(az,addL,10); uart_puts("Z axis: "); uart_puts(addL); } //*********************************stop******************* while(count_t<20000)//for full shot change back to 20000************ { num_records++; //_delay_us(1000000); // sram_write(add_l,add_m,add_h,(uint8_t)(count_t>>8)); add_inc(); data_count++; // sram_write(add_l,add_m,add_h,(uint8_t)(count_t)); add_inc(); data_count++; // uart_puts("\nPre-getAccel"); uint8_t i; for(i=0;i<3;i++) { _delay_us(1250); getAccel(&ax,&ay,&az, MPU9250_DEFAULT_ADDRESS);//fetch all axis compass readings data[0]=(int8_t)(ax>>8); data[1]=(int8_t)ax; data[2]=(int8_t)(ay>>8); data[3]=(int8_t)ay; data[4]=(int8_t)(az>>8); data[5]=(int8_t)az; /* uart_puts("\nX- "); itoa(ax,addL,10); uart_puts(addL); uart_puts("\tY- "); itoa(ay,addL,10); uart_puts(addL); uart_puts("\tZ- "); itoa(az,addL,10); uart_puts(addL); */ /* getGyro(&ax,&ay,&az, MPU9250_ALT_DEFAULT_ADDRESS);//fetch all axis compass readings data[6]=(int8_t)(ax>>8); data[7]=(int8_t)ax; data[8]=(int8_t)(ay>>8); data[9]=(int8_t)ay; data[10]=(int8_t)(az>>8); data[11]=(int8_t)az; getAccel(&ax,&ay,&az, MPU9250_DEFAULT_ADDRESS);//fetch all axis compass readings data[12]=(int8_t)(ax>>8); data[13]=(int8_t)ax; data[14]=(int8_t)(ay>>8); data[15]=(int8_t)ay; data[16]=(int8_t)(az>>8); data[17]=(int8_t)az; */ /* uart_puts("\nX2- "); itoa(ax,addL,10); uart_puts(addL); uart_puts(" Y2- "); itoa(ay,addL,10); uart_puts(addL); uart_puts(" Z2- "); itoa(az,addL,10); uart_puts(addL); */ /* getGyro(&ax,&ay,&az, MPU9250_DEFAULT_ADDRESS);//fetch all axis compass readings data[18]=(int8_t)(ax>>8); data[19]=(int8_t)ax; data[20]=(int8_t)(ay>>8); data[21]=(int8_t)ay; data[22]=(int8_t)(az>>8); data[23]=(int8_t)az; */ uint8_t _i; for(_i=0;_i<18;_i++) { sram_write(add_l,add_m,add_h,data[_i]); add_inc(); data_count++; } }//end for loop }//end timing while loop }//end record_shot