Skip to content
Snippets Groups Projects
Commit 3f6d9017 authored by Sergi Hernandez's avatar Sergi Hernandez
Browse files

Merge branch 'cherry-pick-83d051bb' into 'dynamixel_manager'

Solved some bugs in the memory module.

See merge request !3
parents 165cda32 6f8d92d1
No related branches found
No related tags found
2 merge requests!6Dynamixel manager,!3Solved some bugs in the memory module.
......@@ -3,6 +3,7 @@
COMPILE_OPTS = -mlittle-endian -mthumb -mthumb-interwork
COMPILE_OPTS += -Wall -O2 -fno-common
#COMPILE_OPTS += -Wall -g -fno-common
COMPILE_OPTS += -ffreestanding -nostdlib
COMPILE_OPTS_M4_FPU = -mfloat-abi=hard -mfpu=fpv4-sp-d16 -mcpu=cortex-m4
......
......@@ -2,8 +2,8 @@
# modified by zerom for WinARM 8/2010
COMPILE_OPTS = -mlittle-endian -mthumb -mthumb-interwork
COMPILE_OPTS += -Wall -O2 -fno-common
#COMPILE_OPTS += -Wall -g -fno-common
#COMPILE_OPTS += -Wall -O2 -fno-common
COMPILE_OPTS += -Wall -g -fno-common
COMPILE_OPTS += -ffreestanding -nostdlib
COMPILE_OPTS_M4_FPU = -mfloat-abi=hard -mfpu=fpv4-sp-d16 -mcpu=cortex-m4
......
......@@ -4,15 +4,15 @@
#include "mem_module.h"
#define MAX_NUM_MEM_MODULES 16
#define EEPROM_SIZE 256
#define RAM_SIZE 4096
#define EEPROM_SIZE 32
#define RAM_SIZE 256
typedef struct
{
unsigned char num_mem_modules;
TMemModule *mem_modules[MAX_NUM_MEM_MODULES];
unsigned char (*eeprom_write_data)(unsigned short int address,unsigned short int data);
unsigned char (*eeprom_read_data)(unsigned short int address,unsigned short int *data);
unsigned short int (*eeprom_write_data)(unsigned short int address,unsigned short int data);
unsigned short int (*eeprom_read_data)(unsigned short int address,unsigned short int *data);
}TMemory;
void mem_init(TMemory *memory);
......
#include "memory.h"
#include "mem_module.h"
/* private variables */
unsigned char memory_total_eeprom;
unsigned char memory_total_ram;
/* private functions */
unsigned char mem_in_window(unsigned short int mem_start_address,unsigned short int mem_length,unsigned short int start_address,unsigned short int length)
{
unsigned short int end_mem=mem_start_address+mem_length;
unsigned short int end_address=start_address+length;
unsigned short int end_mem=mem_start_address+mem_length-1;
unsigned short int end_address=start_address+length-1;
if((mem_start_address>=start_address && mem_start_address<end_address) ||
(end_mem>=start_address && end_mem<end_address) ||
(start_address>=mem_start_address && start_address<end_mem) ||
(end_address>=mem_start_address && end_address<end_mem))
if((mem_start_address>=start_address && mem_start_address<=end_address) ||
(end_mem>=start_address && end_mem<=end_address) ||
(start_address>=mem_start_address && start_address<=end_mem) ||
(end_address>=mem_start_address && end_address<=end_mem))
return 0x01;
else
return 0x00;
......@@ -26,6 +30,10 @@ void mem_init(TMemory *memory)
memory->mem_modules[i]=0x00000000;
memory->eeprom_write_data=0x00000000;
memory->eeprom_read_data=0x00000000;
/* initialize internal variables */
memory_total_eeprom=0;
memory_total_ram=0;
}
void mem_initialize_data(TMemory *memory)
......@@ -34,23 +42,32 @@ void mem_initialize_data(TMemory *memory)
unsigned short data;
unsigned short int i,j;
unsigned char eeprom_data[EEPROM_SIZE];
unsigned short int actual_address,actual_length;
if(memory->eeprom_read_data!=0x00000000)
{
for(i=0;i<EEPROM_SIZE;i++)
{
memory->eeprom_read_data(i,&data);
eeprom_data[i]=data&0x00FF;
if(memory->eeprom_read_data(i,&data))
eeprom_data[i]=0x00;
else
eeprom_data[i]=data&0x00FF;
}
for(i=0;i<memory->num_mem_modules;i++)
{
mem_module=memory->mem_modules[i];
for(j=0;j<mem_module->num_eeprom_segments;j++)
{
if(mem_in_window(mem_module->eeprom_segments[j].start_address,mem_module->eeprom_segments[i].length,0x0000,EEPROM_SIZE))
if(mem_in_window(mem_module->eeprom_segments[j].start_address,mem_module->eeprom_segments[j].length,0x0000,EEPROM_SIZE))
{
if(mem_module->write_cmd!=0x00000000)
mem_module->write_cmd(0x0000,EEPROM_SIZE,eeprom_data);
{
actual_address=mem_module->eeprom_segments[j].start_address;
actual_length=EEPROM_SIZE-mem_module->eeprom_segments[j].start_address;
if((actual_address+actual_length)>(mem_module->eeprom_segments[j].start_address+mem_module->eeprom_segments[j].length))
actual_length-=((actual_address+actual_length)-(mem_module->eeprom_segments[j].start_address+mem_module->eeprom_segments[j].length));
mem_module->write_cmd(actual_address,actual_length,&eeprom_data[actual_address]);
}
}
}
}
......@@ -102,6 +119,23 @@ unsigned char mem_add_module(TMemory *memory, TMemModule *module)
}
}
}
// check available memory
for(new_seg=0;new_seg<module->num_ram_segments;new_seg++)
{
memory_total_ram+=module->ram_segments[new_seg].length;
if(memory_total_ram>RAM_SIZE)
return 0x00;
}
for(new_seg=0;new_seg<module->num_eeprom_segments;new_seg++)
{
memory_total_eeprom+=module->eeprom_segments[new_seg].length;
if(memory_total_eeprom>EEPROM_SIZE)
return 0x00;
memory_total_ram+=module->eeprom_segments[new_seg].length;
if(memory_total_ram>RAM_SIZE)
return 0x00;
}
memory->mem_modules[memory->num_mem_modules]=module;
memory->num_mem_modules++;
return 0x01;
......@@ -114,27 +148,54 @@ void mem_do_write(TMemory *memory,unsigned short int start_address,unsigned shor
{
TMemModule *mem_module;
unsigned char i,j,k;
unsigned short int actual_address,actual_length;
for(i=0;i<memory->num_mem_modules;i++)
{
mem_module=memory->mem_modules[i];
for(j=0;j<mem_module->num_ram_segments;j++)
{
if(mem_in_window(mem_module->ram_segments[j].start_address,mem_module->ram_segments[i].length,start_address,length))
if(mem_in_window(mem_module->ram_segments[j].start_address,mem_module->ram_segments[j].length,start_address,length))
{
if(mem_module->write_cmd!=0x00000000)
mem_module->write_cmd(start_address,length,data);
{
if(start_address<mem_module->ram_segments[j].start_address)
{
actual_address=mem_module->ram_segments[j].start_address;
actual_length=length-(mem_module->ram_segments[j].start_address-start_address);
}
else
{
actual_address=start_address;
actual_length=length;
}
if((actual_address+actual_length)>(mem_module->ram_segments[j].start_address+mem_module->ram_segments[j].length))
actual_length-=((actual_address+actual_length)-(mem_module->ram_segments[j].start_address+mem_module->ram_segments[j].length));
mem_module->write_cmd(actual_address,actual_length,&data[actual_address-start_address]);
}
}
}
for(j=0;j<mem_module->num_eeprom_segments;j++)
{
if(mem_in_window(mem_module->eeprom_segments[j].start_address,mem_module->eeprom_segments[i].length,start_address,length))
if(mem_in_window(mem_module->eeprom_segments[j].start_address,mem_module->eeprom_segments[j].length,start_address,length))
{
if(start_address<mem_module->eeprom_segments[j].start_address)
{
actual_address=mem_module->eeprom_segments[j].start_address;
actual_length=length-(mem_module->eeprom_segments[j].start_address-start_address);
}
else
{
actual_address=start_address;
actual_length=length;
}
if((actual_address+actual_length)>(mem_module->eeprom_segments[j].start_address+mem_module->eeprom_segments[j].length))
actual_length-=((actual_address+actual_length)-(mem_module->eeprom_segments[j].start_address+mem_module->eeprom_segments[j].length));
if(mem_module->write_cmd!=0x00000000)
mem_module->write_cmd(start_address,length,data);
mem_module->write_cmd(actual_address,actual_length,&data[actual_address-start_address]);
if(memory->eeprom_write_data!=0x00000000)
for(k=start_address;k<EEPROM_SIZE || k<start_address+length;k++)
memory->eeprom_write_data(k,data[start_address-mem_module->eeprom_segments[j].start_address]);
for(k=actual_address;k<(actual_address+actual_length);k++)
memory->eeprom_write_data(k,data[k-start_address]);
}
}
}
......@@ -144,24 +205,53 @@ void mem_do_read(TMemory *memory,unsigned short int start_address,unsigned short
{
TMemModule *mem_module;
unsigned char i,j;
unsigned short int actual_address,actual_length;
for(i=0;i<memory->num_mem_modules;i++)
{
mem_module=memory->mem_modules[i];
for(j=0;j<mem_module->num_ram_segments;j++)
{
if(mem_in_window(mem_module->ram_segments[j].start_address,mem_module->ram_segments[i].length,start_address,length))
if(mem_in_window(mem_module->ram_segments[j].start_address,mem_module->ram_segments[j].length,start_address,length))
{
if(mem_module->read_cmd!=0x00000000)
mem_module->read_cmd(start_address,length,data);
{
if(start_address<mem_module->ram_segments[j].start_address)
{
actual_address=mem_module->ram_segments[j].start_address;
actual_length=length-(mem_module->ram_segments[j].start_address-start_address);
}
else
{
actual_address=start_address;
actual_length=length;
}
if((actual_address+actual_length)>(mem_module->ram_segments[j].start_address+mem_module->ram_segments[j].length))
actual_length-=((actual_address+actual_length)-(mem_module->ram_segments[j].start_address+mem_module->ram_segments[j].length));
mem_module->read_cmd(actual_address,actual_length,&data[actual_address-start_address]);
}
}
}
for(j=0;j<mem_module->num_eeprom_segments;j++)
{
if(mem_in_window(mem_module->eeprom_segments[j].start_address,mem_module->eeprom_segments[i].length,start_address,length))
if(mem_in_window(mem_module->eeprom_segments[j].start_address,mem_module->eeprom_segments[j].length,start_address,length))
{
if(mem_module->read_cmd!=0x00000000)
mem_module->read_cmd(start_address,length,data);
{
if(start_address<mem_module->eeprom_segments[j].start_address)
{
actual_address=mem_module->eeprom_segments[j].start_address;
actual_length=length-(mem_module->eeprom_segments[j].start_address-start_address);
}
else
{
actual_address=start_address;
actual_length=length;
}
if((actual_address+actual_length)>(mem_module->eeprom_segments[j].start_address+mem_module->eeprom_segments[j].length))
actual_length-=((actual_address+actual_length)-(mem_module->eeprom_segments[j].start_address+mem_module->eeprom_segments[j].length));
mem_module->read_cmd(actual_address,actual_length,&data[actual_address-start_address]);
}
}
}
}
......
......@@ -3,6 +3,7 @@
COMPILE_OPTS = -mlittle-endian -mthumb -mthumb-interwork
COMPILE_OPTS += -Wall -O2 -fno-common
#COMPILE_OPTS += -Wall -g -fno-common
COMPILE_OPTS += -ffreestanding -nostdlib
COMPILE_OPTS_M4_FPU = -mfloat-abi=hard -mfpu=fpv4-sp-d16 -mcpu=cortex-m4
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment