mirror of
https://github.com/marqs85/ossc
synced 2026-11-27 16:32:17 +03:00
Initial public release (FW 0.64)
This commit is contained in:
commit
388c464f63
264 files changed
+66416
No files matched your search
@@ -0,0 +1,33 @@
|
||||
#ifndef __I2C_OPENCORES_H__
|
||||
#define __I2C_OPENCORES_H__
|
||||
|
||||
|
||||
#include "alt_types.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif /* __cplusplus */
|
||||
|
||||
|
||||
|
||||
|
||||
void I2C_init(alt_u32 base,alt_u32 clk,alt_u32 speed);
|
||||
int I2C_start(alt_u32 base, alt_u32 add, alt_u32 read);
|
||||
alt_u32 I2C_read(alt_u32 base,alt_u32 last);
|
||||
alt_u32 I2C_write(alt_u32 base,alt_u8 data, alt_u32 last);
|
||||
alt_u32 SPI_read(alt_u32 base);
|
||||
void SPI_write(alt_u32 base,alt_u8 data);
|
||||
#define I2C_OK (0)
|
||||
#define I2C_ACK (0)
|
||||
#define I2C_NOACK (1)
|
||||
#define I2C_ABITRATION_LOST (2)
|
||||
|
||||
#define I2C_OPENCORES_INSTANCE(name, dev) extern int alt_no_storage
|
||||
#define I2C_OPENCORES_INIT(name, dev) while (0)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* __cplusplus */
|
||||
|
||||
#endif /* __I2C_OPENCORES_H__ */
|
||||
@@ -0,0 +1,38 @@
|
||||
# *******************************************************************************
|
||||
# * *
|
||||
# * License Agreement *
|
||||
# * *
|
||||
# * Copyright (c) 2003 Altera Corporation, San Jose, California, USA. *
|
||||
# * All rights reserved. *
|
||||
# * *
|
||||
# * Permission is hereby granted, free of charge, to any person obtaining a *
|
||||
# * copy of this software and associated documentation files (the "Software"), *
|
||||
# * to deal in the Software without restriction, including without limitation *
|
||||
# * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
|
||||
# * and/or sell copies of the Software, and to permit persons to whom the *
|
||||
# * Software is furnished to do so, subject to the following conditions: *
|
||||
# * *
|
||||
# * The above copyright notice and this permission notice shall be included in *
|
||||
# * all copies or substantial portions of the Software. *
|
||||
# * *
|
||||
# * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
|
||||
# * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
|
||||
# * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
|
||||
# * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
|
||||
# * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
|
||||
# * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
|
||||
# * DEALINGS IN THE SOFTWARE. *
|
||||
# * *
|
||||
# * This agreement shall be governed in all respects by the laws of the State *
|
||||
# * of California and by the laws of the United States of America. *
|
||||
# * *
|
||||
# * Altera does not recommend, suggest or require that this reference design *
|
||||
# * file be used in conjunction or combination with any other product. *
|
||||
# *******************************************************************************
|
||||
|
||||
# List all source files supplied by this component.
|
||||
|
||||
C_LIB_SRCS += i2c_opencores.c
|
||||
|
||||
ASM_LIB_SRCS +=
|
||||
|
||||
@@ -0,0 +1,206 @@
|
||||
|
||||
#include "alt_types.h"
|
||||
#include "i2c_opencores_regs.h"
|
||||
#include "i2c_opencores.h"
|
||||
|
||||
// #define I2C_DEBUG
|
||||
//int I2C_init(alt_u32 base,alt_u32 clk, alt_u32 speed)
|
||||
//int I2C_start(alt_u32 base, alt_u32 add, alt_u32 write);
|
||||
//alt_u32 I2C_read(alt_u32 base);
|
||||
//int I2C_write(alt_u32 base, alt_u8 data);
|
||||
//int I2C_stop(alt_u32 base);
|
||||
|
||||
/* these functions are polled only. */
|
||||
/* all functions wait until the I2C is done before exiting */
|
||||
|
||||
|
||||
/****************************************************************
|
||||
int I2C_init
|
||||
This function inititlizes the prescalor for the scl
|
||||
and then enables the core. This must be run before
|
||||
any other i2c code is executed
|
||||
inputs
|
||||
base = the base address of the component
|
||||
clk = freuqency of the clock driving this component ( in Hz)
|
||||
speed = SCL speed ie 100K, 400K ... (in Hz)
|
||||
15-OCT-07 initial release
|
||||
*****************************************************************/
|
||||
void I2C_init(alt_u32 base,alt_u32 clk,alt_u32 speed)
|
||||
{
|
||||
alt_u32 prescale = (clk/( 5 * speed))-1;
|
||||
#ifdef I2C_DEBUG
|
||||
printf(" Initializing I2C at 0x%x, \n\twith clock speed 0x%x \n\tand SCL speed 0x%x \n\tand prescale 0x%x\n",base,clk,speed,prescale);
|
||||
#endif
|
||||
IOWR_I2C_OPENCORES_CTR(base, 0x00); /* turn off the core*/
|
||||
|
||||
IOWR_I2C_OPENCORES_CR(base, I2C_OPENCORES_CR_IACK_MSK); /* clearn any pening IRQ*/
|
||||
|
||||
IOWR_I2C_OPENCORES_PRERLO(base, (0xff & prescale)); /* load low presacle bit*/
|
||||
|
||||
IOWR_I2C_OPENCORES_PRERHI(base, (0xff & (prescale>>8))); /* load upper prescale bit */
|
||||
|
||||
IOWR_I2C_OPENCORES_CTR(base, I2C_OPENCORES_CTR_EN_MSK); /* turn on the core*/
|
||||
|
||||
}
|
||||
|
||||
/****************************************************************
|
||||
int I2C_start
|
||||
Sets the start bit and then sends the first byte which
|
||||
is the address of the device + the write bit.
|
||||
inputs
|
||||
base = the base address of the component
|
||||
add = address of I2C device
|
||||
read = 1== read 0== write
|
||||
return value
|
||||
0 if address is acknowledged
|
||||
1 if address was not acknowledged
|
||||
15-OCT-07 initial release
|
||||
*****************************************************************/
|
||||
int I2C_start(alt_u32 base, alt_u32 add, alt_u32 read)
|
||||
{
|
||||
#ifdef I2C_DEBUG
|
||||
printf(" Start I2C at 0x%x, \n\twith address 0x%x \n\tand read 0x%x \n\tand prescale 0x%x\n",base,add,read);
|
||||
#endif
|
||||
|
||||
/* transmit the address shifted by one and the read/write bit*/
|
||||
IOWR_I2C_OPENCORES_TXR(base, ((add<<1) + (0x1 & read)));
|
||||
|
||||
/* set start and write bits which will start the transaction*/
|
||||
IOWR_I2C_OPENCORES_CR(base, I2C_OPENCORES_CR_STA_MSK | I2C_OPENCORES_CR_WR_MSK );
|
||||
|
||||
/* wait for the trnasaction to be over.*/
|
||||
while( IORD_I2C_OPENCORES_SR(base) & I2C_OPENCORES_SR_TIP_MSK);
|
||||
|
||||
/* now check to see if the address was acknowledged */
|
||||
if(IORD_I2C_OPENCORES_SR(base) & I2C_OPENCORES_SR_RXNACK_MSK)
|
||||
{
|
||||
#ifdef I2C_DEBUG
|
||||
printf("\tNOACK\n");
|
||||
#endif
|
||||
return (I2C_NOACK);
|
||||
}
|
||||
else
|
||||
{
|
||||
#ifdef I2C_DEBUG
|
||||
printf("\tACK\n");
|
||||
#endif
|
||||
return (I2C_ACK);
|
||||
}
|
||||
}
|
||||
|
||||
/****************************************************************
|
||||
int I2C_read
|
||||
assumes that any addressing and start
|
||||
has already been done.
|
||||
reads one byte of data from the slave. on the last read
|
||||
we don't acknowldge and set the stop bit.
|
||||
inputs
|
||||
base = the base address of the component
|
||||
last = on the last read there must not be a ack
|
||||
|
||||
return value
|
||||
byte read back.
|
||||
15-OCT-07 initial release
|
||||
*****************************************************************/
|
||||
alt_u32 I2C_read(alt_u32 base,alt_u32 last)
|
||||
{
|
||||
#ifdef I2C_DEBUG
|
||||
printf(" Read I2C at 0x%x, \n\twith last0x%x\n",base,last);
|
||||
#endif
|
||||
if( last)
|
||||
{
|
||||
/* start a read and no ack and stop bit*/
|
||||
IOWR_I2C_OPENCORES_CR(base, I2C_OPENCORES_CR_RD_MSK |
|
||||
I2C_OPENCORES_CR_NACK_MSK | I2C_OPENCORES_CR_STO_MSK);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* start read*/
|
||||
IOWR_I2C_OPENCORES_CR(base, I2C_OPENCORES_CR_RD_MSK );
|
||||
}
|
||||
/* wait for the trnasaction to be over.*/
|
||||
while( IORD_I2C_OPENCORES_SR(base) & I2C_OPENCORES_SR_TIP_MSK);
|
||||
|
||||
/* now read the data */
|
||||
return (IORD_I2C_OPENCORES_RXR(base));
|
||||
|
||||
}
|
||||
|
||||
/****************************************************************
|
||||
int I2C_write
|
||||
assumes that any addressing and start
|
||||
has already been done.
|
||||
writes one byte of data from the slave.
|
||||
If last is set the stop bit set.
|
||||
inputs
|
||||
base = the base address of the component
|
||||
data = byte to write
|
||||
last = on the last read there must not be a ack
|
||||
|
||||
return value
|
||||
0 if address is acknowledged
|
||||
1 if address was not acknowledged
|
||||
15-OCT-07 initial release
|
||||
*****************************************************************/
|
||||
alt_u32 I2C_write(alt_u32 base,alt_u8 data, alt_u32 last)
|
||||
{
|
||||
#ifdef I2C_DEBUG
|
||||
printf(" Read I2C at 0x%x, \n\twith data 0x%x,\n\twith last0x%x\n",base,data,last);
|
||||
#endif
|
||||
/* transmit the data*/
|
||||
IOWR_I2C_OPENCORES_TXR(base, data);
|
||||
|
||||
if( last)
|
||||
{
|
||||
/* start a write with ack and stop bit*/
|
||||
IOWR_I2C_OPENCORES_CR(base, I2C_OPENCORES_CR_WR_MSK |
|
||||
I2C_OPENCORES_CR_STO_MSK);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* start write with ack */
|
||||
IOWR_I2C_OPENCORES_CR(base, I2C_OPENCORES_CR_WR_MSK );
|
||||
}
|
||||
/* wait for the trnasaction to be over.*/
|
||||
while( IORD_I2C_OPENCORES_SR(base) & I2C_OPENCORES_SR_TIP_MSK);
|
||||
|
||||
/* now check to see if the address was acknowledged */
|
||||
if(IORD_I2C_OPENCORES_SR(base) & I2C_OPENCORES_SR_RXNACK_MSK)
|
||||
{
|
||||
#ifdef I2C_DEBUG
|
||||
printf("\tNOACK\n");
|
||||
#endif
|
||||
return (I2C_NOACK);
|
||||
}
|
||||
else
|
||||
{
|
||||
#ifdef I2C_DEBUG
|
||||
printf("\tACK\n");
|
||||
#endif
|
||||
return (I2C_ACK);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
alt_u32 SPI_read(alt_u32 base)
|
||||
{
|
||||
/* start read*/
|
||||
IOWR_I2C_OPENCORES_CR(base, I2C_OPENCORES_CR_RD_MSK|I2C_OPENCORES_CR_NACK_MSK|I2C_OPENCORES_CR_SPIM_MSK );
|
||||
|
||||
/* wait for the trnasaction to be over.*/
|
||||
while( IORD_I2C_OPENCORES_SR(base) & I2C_OPENCORES_SR_TIP_MSK);
|
||||
|
||||
/* now read the data */
|
||||
return (IORD_I2C_OPENCORES_RXR(base));
|
||||
}
|
||||
|
||||
void SPI_write(alt_u32 base,alt_u8 data) {
|
||||
/* transmit the data*/
|
||||
IOWR_I2C_OPENCORES_TXR(base, data);
|
||||
|
||||
/* start write */
|
||||
IOWR_I2C_OPENCORES_CR(base, I2C_OPENCORES_CR_WR_MSK|I2C_OPENCORES_CR_NACK_MSK|I2C_OPENCORES_CR_SPIM_MSK );
|
||||
|
||||
/* wait for the trnasaction to be over.*/
|
||||
while( IORD_I2C_OPENCORES_SR(base) & I2C_OPENCORES_SR_TIP_MSK);
|
||||
}
|
||||
Reference in new issue
Block a user