让我们说我正在写一个" Device Tree Blob"对于bcm2835 RPi芯片,但在C ++文件中,而不是.dts
文件。目的是练习C ++和OS概念。
我希望不仅可以封装寄存器地址,还可以封装访问这些地址的函数,并且只将顶级用途公开为API函数。
在C++
中,这可能是内部类,对于一个大的BCM2835类是这样的:
//bcm2835.h
class BMC2835 : public ARMCpu
{
public:
void ACKLedOn(void);
void ACKLdOff(void);
void ACKLedBlink(void);
// I2C write to device (this would be called by the device driver)
// It would ensure that I2C is setup, etc, etc
void I2C_Device_Write(I2C_Device* device, uint8_t* buffer);
private:
// Physical addresses for various peripheral register sets
/// Base Physical Address of the BCM 2835 peripheral registers
const uint32_t BCM2835_PERI_BASE = 0x20000000;
class GPIO()
{
private:
/// Base Physical Address of the Pads registers
const uint32_t BCM2835_GPIO_PADS = (BCM2835_PERI_BASE + 0x100000)
/// Sets the Function Select register for the given pin, which configures
/// the pin as Input, Output or one of the 6 alternate functions.
void bcm2835_gpio_fsel(uint8_t pin, uint8_t mode);
}
class I2C()
{
private:
const uint32_t BCM2835_CORE_CLK_HZ = 250000000 ;///< 250 MHz
// Register masks for BSC_C
const uint32_t BCM2835_BSC_C_I2CEN = 0x00008000;///< I2C Enable, 0 = disabled, 1 = enabled
const uint32_t BCM2835_BSC_C_INTR = 0x00000400;///< Interrupt on RX
const uint32_t BCM2835_BSC_C_INTT = 0x00000200;///< Interrupt on TX
void bcm2835_i2c_begin(void);
void bcm2835_i2c_write(uint8_t address, uint8* pbuffer);
}
}
然后我还可以为64位的BCM2837提供一个类,例如,处理LED的方式非常不同。
//bcm2837.h
class BCM2837 : public ARMCpu
{
public:
// LED is now a very different Implementation with Mailbox
// but exposed to Kernel as API
void ACKLedOn(void);
void ACKLdOff(void);
void ACKLedBlink(void);
...
...
}
我确信这种方法存在许多问题。在您包含SPI
,UART
等内容之后,似乎最让我烦恼的是单个班级的长度。
即使ARMCpu
具有良好的设计性和100%的虚拟性(我宁愿在嵌入式设备中避免使用),每个CPU类仍然相当冗长且难以阅读和维护。
有没有办法在C ++中实现这种类型的私有级访问更容易?
答案 0 :(得分:3)
将每个芯片放在自己的.cpp文件中,并在该文件中声明所有那些私有的内部事物(而不是在标题中)。您可以将它们包装在匿名命名空间中,以防止它们暴露给链接器。