I use GCC compiler and C++ to write programs in the project. Theoretically, as long as the external memory is enabled and configured, the address allocation of variables can be completely handed over to the compiler, but the premise is that the memory enable and configuration code must be executed first after power-on reset. When using C or C++ to write programs, the first code to be executed after power-on reset is the startup code and constructor code automatically added by the compiler. I don't know how to make the memory enable and configuration code be executed first after power-on reset, so I wrote the following macro to control the access to the external memory.
code show as below:
//ex te rn_ ram .h
#ifndef _H_EXTERN_RAM_H_
#define _H_EXTERN_RAM_H_
#include "type.h"
#define DEF_EX_VAR(name) EX_VAR name;
#define INIT_EX_VAR(name,ele_size,ele_n) do{
name.size_ele=ele_size;
name.n_ele=ele_n;
name.addr=p;
p+=((ele_size)*(ele_n));
}while(0)
#define WR_EX_VAR(name, pos, pdat) do {
for(UINT8 iiiii="0";iiiii(name.addr+(pos)*name.size_ele)[iiiii]=*((INT8 *)pdat+iiiii);
} while (0)
#define RD_EX_VAR(name, pos, pdat) do {
for(UINT8 iiiii="0";iiiii*((INT8 *)pdat+iiiii)=((INT8 *)(name.addr+(pos)*name.size_ele))[iiiii];
} while (0);
#define GET_P_EX_VAR(name,pos) (name.addr+(pos)*name.size_ele)
typedef struct _tag_ex_var
{
UINT8 size_ele;
UINT16 n_ele;
INT8 *addr;
} EX_VAR;
#endif
Application Examples
//main.cpp
#include "includes.h"
INT8 *p=(INT8*)0x3100; //The address of external RAM starts from 0X3100
DEF_EX_VAR(test); //define an external variable
INT16 main (void)
{
UINT8 i;
INT16 tmp;
INT16 *p_ele;
MCUCR|=0X80; // Enable XRAM and configure it for the fastest access speed
INIT_EX_VAR(test, sizeof(INT16), 100); //External variable initialization, the variable has 100 elements, the element size is sizeof(INT16)
tmp = "0xaaaa";
for (i=0;i<100;i++)
{
WR_EX_VAR (test, i, &tmp); //Write an example to assign the value 0Xaaaa to all elements of the variable test
}
for (i=0;i<100;i++)
{
RD_EX_VAR (test, i, &tmp); //Read example
}
p_ele = (INT16 *)GET_P_EX_VAR (test, 32); //Get the pointer to the 32nd element of the variable test
while(1);
return 0x00;
}
Previous article:SD card interface programming for AVR microcontroller
Next article:The operation steps and process of AVR MCU compare match clear counter mode
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- BQ40Z50-R2 security mode introduction and password modification method
- Analog technology sensor circuit example explanation
- EEWORLD University Hall ---- Sharing of temperature measuring gun application solutions based on Lingdong MM32L series low-power MCU
- EEWORLD University Hall----Live Replay: Microchip uses secure elements for accessory authentication
- The C6748 timer is in 32-bit mode
- STM32F103RCT6 Send without permission
- protous 8.9 green version
- A brief discussion on the relationship between driving capability and timing
- FAQ_How to set the impedance of BlueNRG-12 ADC_V1.0
- How to ensure workplace safety? Check out the solutions RS can provide for you!