Created
January 8, 2016 22:32
-
-
Save rkujawa/c929a71d0c644a44acb4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <proto/exec.h> | |
#include <exec/types.h> | |
#include <exec/memory.h> | |
#include <exec/interrupts.h> | |
#include <hardware/intbits.h> | |
#define CLOCKPORT_BASE 0xD80001 | |
#define CLOCKPORT_STRIDE 4 | |
#define I2CSTA 0 | |
#define I2CTO 0 | |
#define I2CDAT 2 | |
#define I2CADR 1 | |
#define I2CCON 3 | |
#define I2CCON_CR0 (1 << 0) | |
#define I2CCON_CR1 (1 << 1) | |
#define I2CCON_CR2 (1 << 2) | |
#define I2CCON_CR_88KHZ (0x4) | |
#define I2CCON_CR_59KHZ (0x5) | |
#define I2CCON_CR_MASK (0x7) | |
#define I2CCON_SI (1 << 3) | |
#define I2CCON_STO (1 << 4) | |
#define I2CCON_STA (1 << 5) | |
#define I2CCON_ENSIO (1 << 6) | |
#define I2CCON_AA (1 << 7) | |
#pragma dontwarn 113 | |
UBYTE clockport_read(UBYTE reg); | |
void clockport_write(UBYTE reg, UBYTE val); | |
void pca9564_isr(void); | |
static UBYTE *cp = CLOCKPORT_BASE; | |
static const BOOL debug = TRUE; | |
static int inttest = 0; | |
UBYTE | |
clockport_read(UBYTE reg) | |
{ | |
UBYTE v; | |
UBYTE *ptr; | |
ptr = cp + (reg * CLOCKPORT_STRIDE); | |
v = *ptr; | |
if (debug) | |
printf("DEBUG: read %x from %p\n", (int) v, (void*) ptr); | |
return v; | |
} | |
void | |
clockport_write(UBYTE reg, UBYTE value) | |
{ | |
UBYTE *ptr; | |
ptr = cp + (reg * CLOCKPORT_STRIDE); | |
if (debug) | |
printf("DEBUG: write %x to %p\n", (int) value, (void*) ptr); | |
*ptr = value; | |
} | |
int main(void) | |
{ | |
struct Interrupt *int6; | |
UBYTE ctrl; | |
UBYTE void_data; | |
if (int6 = AllocMem(sizeof(struct Interrupt), MEMF_PUBLIC|MEMF_CLEAR)) { | |
int6->is_Node.ln_Type = NT_INTERRUPT; | |
int6->is_Node.ln_Pri = -60; | |
int6->is_Node.ln_Name = "PCA9564"; | |
int6->is_Data = (APTR)&void_data; | |
int6->is_Code = pca9564_isr; | |
AddIntServer(INTB_EXTER, int6); | |
} else { | |
printf("Can't allocate memory for interrupt node\n"); | |
return 1; | |
} | |
ctrl = I2CCON_CR_59KHZ | I2CCON_ENSIO; | |
clockport_write(I2CCON, ctrl); | |
Delay(50); | |
printf("gonna send start\n"); | |
ctrl |= I2CCON_STA; | |
clockport_write(I2CCON, ctrl); /* send START condition */ | |
/* Wait for signal here */ | |
/* then do things... */ | |
Delay(50); | |
ctrl = clockport_read(I2CCON); | |
printf("I2CCON is now: %x\n", ctrl); | |
RemIntServer(INTB_EXTER, int6); | |
FreeMem(int6, sizeof(struct Interrupt)); | |
printf("ISR was called %d times\n", inttest); | |
return 0; | |
} | |
/* Interrupt service routine. */ | |
void | |
pca9564_isr(void) | |
{ | |
UBYTE *conptr; | |
/* | |
* so lame, | |
* instead, acknowledge hw interrupt here, then signal the main program, | |
* continue there | |
*/ | |
conptr = cp + (I2CCON * CLOCKPORT_STRIDE); | |
*conptr = 0; | |
inttest++; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment