Autor Thema: >PURE C: Memory mapped I/O made easy with ANSI C  (Gelesen 14708 mal)

0 Mitglieder und 1 Gast betrachten dieses Thema.

Offline simonsunnyboy

  • Moderator
  • *****
  • Beiträge: 1.808
  • Rock'n'Roll is the thing - Jerry Lee is the king!
>PURE C: Memory mapped I/O made easy with ANSI C
« 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
Paradize - ST Offline Tournament
Stay cool, stay Atari!
1x2600jr, 1x1040STFm, 1x1040STE 4MB+TOS2.06+SatanDisk, 1xF030 14MB+FPU+NetUS-Bee

Arne

  • Gast
Re: >PURE C: Memory mapped I/O made easy with ANSI C
« Antwort #1 am: Mi 22.12.2010, 09:35:50 »
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))