atari-home.de - Foren
Software => Coding => Thema gestartet von: simonsunnyboy am Mo 20.12.2010, 18:15:47
-
Hallo zusammen,
ich habe mal zusammengefasst, wie man korrekten Zugriff auf Hardwareregister in reinem ANSI C (etwa Pure C oder auch gcc) realisiert.
/*
* Memory mapped I/O made easy with ANSI C
* commented by Matthias Arndt <marndt@asmsoftware.de>
*/
#include <stdint.h>
/* temporary valid location for demonstration purposes */
uint8_t storage;
/* Now the magic declaration pointer to a hw register:
* We point it to some known storage but ofcourse for pointing
* to a real I/O hardware register, one would supply a constant
* register address.
*
* a) declare it volatile because hardware I/O locations may change inside
* a different context (interrupt and/or hardware event)
* b) make the pointer address const between register name and the type
* definition so that noone may modify the pointer
* c) adding const before the declaration will declare a read/only register
*/
volatile uint8_t * const HWREG = &storage;
/* to point to real I/O you would use the following syntax: */
/* volatile uint8_t * const HWREG = (uint8_t *)0xF000; */
/* just some demo calls */
void task(void);
uint8_t access(void);
void task()
{
*HWREG = 0x5a; /* set I/O for demo purpose */
/* access to alter the pointer is forbidden! Uncomment to try! */
/* HWREG = (uint8_t *) 0xaaaa; */
}
uint8_t access()
{
return(*HWREG); /* read I/O */
}
Vllt hilft das ganze dem einen doer anderen ja mal weiter!
Frohes Coden!
ssb
-
Mach ich seit Jahr und Tag (680x0, Coldfire, AVR, Cortex-M3) mit diesen zwei Makros:
#define IOREAD(ADDR,SIZE) (*(volatile uint ## SIZE ## _t *)(ADDR))
#define IOWRITE(ADDR,SIZE,DATA) (*((volatile uint ## SIZE ## _t *)(ADDR)) = (DATA))