xyControl  0.1
Quadrotor Flight Controller on AVR Basis
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Groups
Files | Data Structures | Macros | Functions | Variables
External Memory Interface

Allows access to external RAM with bank-switching. More...

Files

file  xmem.h
 XMEM API Header.
 
file  xmem.c
 XMEM API Implementation.
 

Data Structures

struct  MallocState
 All Malloc related State. More...
 

Macros

#define MEMSWITCH(x)   uint8_t oldMemBank=xmemGetBank();if(oldMemBank!=x)xmemSetBank(x);
 Switch the bank, if needed. More...
 
#define MEMSWITCHBACK(x)   if(oldMemBank!=x)xmemSetBank(oldMemBank);
 Switch back to the last bank, if needed. More...
 
#define MEMBANKS   8
 Available Memory Banks. More...
 
#define BANK_GENERIC   0
 Generic Memory Bank. More...
 

Functions

void xmemInit (void)
 Initialize the External Memory Interface. More...
 
void xmemSetBank (uint8_t bank)
 Switch the active memory bank. More...
 
uint8_t xmemGetBank (void)
 Get the current memory bank. More...
 
void saveState (uint8_t bank)
 Save the current malloc state. More...
 
void restoreState (uint8_t bank)
 Restore the malloc state. More...
 

Variables

MallocState states [MEMBANKS]
 MallocState for all Memory Banks. More...
 
uint8_t currentBank
 Current active Memory Bank. More...
 
MallocState states [MEMBANKS]
 MallocState for all Memory Banks. More...
 
uint8_t currentBank = 0
 Current active Memory Bank. More...
 
void * __brkval
 Internal Malloc Heap-End Pointer. More...
 
void * __flp
 Internal Malloc Free List Pointer (State) More...
 

Detailed Description

Allows access to external RAM with bank-switching.

Macro Definition Documentation

#define BANK_GENERIC   0

Generic Memory Bank.

Definition at line 55 of file xmem.h.

Referenced by addMenuCommand(), addTask(), removeTask(), tasks(), tasksRegistered(), uartMenuPrintHelp(), and uartMenuTask().

#define MEMBANKS   8

Available Memory Banks.

Examples:
hardwareTest.c.

Definition at line 54 of file xmem.h.

Referenced by xmemInit(), and xmemSetBank().

#define MEMSWITCH (   x)    uint8_t oldMemBank=xmemGetBank();if(oldMemBank!=x)xmemSetBank(x);

Switch the bank, if needed.

Stores the old bank in a variable oldMemBank.

Parameters
xNew Bank

Definition at line 47 of file xmem.h.

Referenced by addTask(), removeTask(), tasks(), and tasksRegistered().

#define MEMSWITCHBACK (   x)    if(oldMemBank!=x)xmemSetBank(oldMemBank);

Switch back to the last bank, if needed.

Parameters
xNew (current) Bank

Definition at line 52 of file xmem.h.

Referenced by addTask(), removeTask(), tasks(), and tasksRegistered().

Function Documentation

void restoreState ( uint8_t  bank)

Restore the malloc state.

Parameters
bankLocation of state to load.

Definition at line 65 of file xmem.c.

References __brkval, __flp, MallocState::end, MallocState::fl, MallocState::start, and MallocState::val.

Referenced by xmemSetBank().

65  {
66  __malloc_heap_start = states[bank].start;
67  __malloc_heap_end = states[bank].end;
68  __brkval = states[bank].val;
69  __flp = states[bank].fl;
70 }
void saveState ( uint8_t  bank)

Save the current malloc state.

Parameters
bankCurrent Bank Number

Definition at line 55 of file xmem.c.

References __brkval, __flp, MallocState::end, MallocState::fl, MallocState::start, and MallocState::val.

Referenced by xmemInit(), and xmemSetBank().

55  {
56  states[bank].start = __malloc_heap_start;
57  states[bank].end = __malloc_heap_end;
58  states[bank].val = __brkval;
59  states[bank].fl = __flp;
60 }
uint8_t xmemGetBank ( void  )

Get the current memory bank.

Returns
Current Memory Bank.
Examples:
hardwareTest.c.

Definition at line 105 of file xmem.c.

References currentBank.

Referenced by addMenuCommand(), uartMenuPrintHelp(), and uartMenuTask().

105  {
106  return currentBank;
107 }
void xmemInit ( void  )

Initialize the External Memory Interface.

Definition at line 72 of file xmem.c.

References BANK0DDR, BANK0PIN, BANK0PORT, BANK1DDR, BANK1PIN, BANK1PORT, BANK2DDR, BANK2PIN, BANK2PORT, MEMBANKS, and saveState().

Referenced by xyInit().

72  {
73  BANK0DDR |= (1 << BANK0PIN);
74  BANK1DDR |= (1 << BANK1PIN);
75  BANK2DDR |= (1 << BANK2PIN);
76  BANK0PORT &= ~(1 << BANK0PIN);
77  BANK1PORT &= ~(1 << BANK1PIN);
78  BANK2PORT &= ~(1 << BANK2PIN);
79 
80  XMCRB = 0; // Use full address space
81  XMCRA = (1 << SRW11) | (1 << SRW10); // 3 Wait cycles
82  XMCRA |= (1 << SRE); // Enable XMEM
83 
84  for (uint8_t i = 0; i < MEMBANKS; i++) {
85  saveState(i);
86  }
87 }
void xmemSetBank ( uint8_t  bank)

Switch the active memory bank.

Parameters
bankNew Memory Bank
Examples:
hardwareTest.c.

Definition at line 89 of file xmem.c.

References BANK0PIN, BANK0PORT, BANK1PIN, BANK1PORT, BANK2PIN, BANK2PORT, currentBank, MEMBANKS, restoreState(), and saveState().

Referenced by addMenuCommand(), uartMenuPrintHelp(), and uartMenuTask().

89  {
90  if (bank < MEMBANKS) {
92 
93  BANK0PORT &= ~(1 << BANK0PIN);
94  BANK1PORT &= ~(1 << BANK1PIN);
95  BANK2PORT &= ~(1 << BANK2PIN);
96  BANK0PORT |= ((bank & 0x01) << BANK0PIN);
97  BANK1PORT |= (((bank & 0x02) >> 1) << BANK1PIN);
98  BANK2PORT |= (((bank & 0x04) >> 2) << BANK2PIN);
99 
100  currentBank = bank;
101  restoreState(bank);
102  }
103 }

Variable Documentation

void* __brkval

Internal Malloc Heap-End Pointer.

Referenced by restoreState(), and saveState().

void* __flp

Internal Malloc Free List Pointer (State)

Referenced by restoreState(), and saveState().

uint8_t currentBank = 0

Current active Memory Bank.

Definition at line 47 of file xmem.c.

Referenced by xmemGetBank(), and xmemSetBank().

uint8_t currentBank

Current active Memory Bank.

Definition at line 47 of file xmem.c.

Referenced by xmemGetBank(), and xmemSetBank().

MallocState for all Memory Banks.

Definition at line 46 of file xmem.c.

MallocState for all Memory Banks.

Definition at line 46 of file xmem.c.