SI5338 时钟芯片驱动
该时钟芯片可以做到任意频率的时钟输出
控制接口为IIC接口,主要的资料为 :
AN428.pdf
Si5338-RM.pdf
Si5338芯片手册
我们首先应该芯片手册,然后对应寄存器说明为Si5338-RM.pdf,AN428里给了一个单片机的demo
此外由于寄存器比较多,我们可以借助官方提供的工具软件
设置好参数通过export
可获取对应的寄存器的值
下面是大概的IIC时序代码:
/*
- Si5338.c
- Created on: 2020年9月13日
-
Author: Administrator
*/
#include"Si5338.h"
/### (FPGA -> scl si5338) — BIT_5
void Si5338_SCL_SET(unsigned char x)
{
GPIO_DIRM_2 = GPIO_DIRM_2 | (0x00000001 << 4); // 设定为输出(1:输出;0:输入)
GPIO_OUT_EN_2 = GPIO_OUT_EN_2 | (0x00000001 << 4); // 输出使能
if(x != 0) GPIO_DATA_O_WO = GPIO_DATA_O_WO | (0x00000001 << 4);
else GPIO_DATA_O_WO = GPIO_DATA_O_WO & (~(0x00000001 << 4));
}
/### (FPGA -> SDA) — BIT_6
void Si5338_SDA_SET(unsigned char x)
{
GPIO_DIRM_2 = GPIO_DIRM_2 | (0x00000001 << 6); // 设定为输出(1:输出;0:输入)
GPIO_OUT_EN_2 = GPIO_OUT_EN_2 | (0x00000001 << 6); // 输出使能
if(x != 0) GPIO_DATA_O_WO = GPIO_DATA_O_WO | (0x00000001 << 6);
else GPIO_DATA_O_WO = GPIO_DATA_O_WO & (~(0x00000001 << 6));
}
unsigned int Si5338_SDA_GET_DATA()
{
unsigned int tmp_data=0;
GPIO_DIRM_2 = GPIO_DIRM_2 & (~(0x00000001 << 6)); // 设定为输入(1:输出;0:输入)
GPIO_OUT_EN_2 = GPIO_OUT_EN_2 & (~(0x00000001 << 6)); // 输出使能
tmp_data= (GPIO_DATA_I_RO & (0x00000001 << 6));
return tmp_data;
}
void IIC_Start(void)
{
Si5338_SCL_SET(1);
udelay(20);
Si5338_SDA_SET(1);
udelay(200);
Si5338_SDA_SET(0);
udelay(200);
Si5338_SCL_SET(0);
}
void IIC_Stop(void)
{
Si5338_SCL_SET(0);
udelay(10);
Si5338_SDA_SET(0);
udelay(100);
Si5338_SCL_SET(1);
udelay(100);
Si5338_SDA_SET(1);
udelay(50);
}
void IIC_SEND_ACK(void)
{
Si5338_SCL_SET(0);
udelay(10);
Si5338_SDA_SET(0);
udelay(50);
Si5338_SCL_SET(1);
udelay(50);
Si5338_SCL_SET(0);
}
void IIC_WAIT_ACK(void)
{
u8 tempTime=0;
// Si5338_SDA_SET(1);
Si5338_SDA_GET_DATA();
Si5338_SCL_SET(0);
udelay(100);
Si5338_SCL_SET(1);
udelay(50);
while(Si5338_SDA_GET_DATA()!=0x00)
{
tempTime++;
if(tempTime>250)
break;
}
udelay(50);
Si5338_SCL_SET(0);
}
void IIC_SEND_NOACK(void)
{
Si5338_SDA_SET(1);
Si5338_SCL_SET(0);
udelay(10);
udelay(50);
Si5338_SCL_SET(1);
udelay(50);
Si5338_SCL_SET(0);
}
void IIC_Send_BYTE(uint8_t data)
{
uint8_t i=0;
for(i=0;i<=7;i++)
{
if((data&0x80)!=0x00)
Si5338_SDA_SET(1);
else
Si5338_SDA_SET(0);
data=data<<1;
udelay(20);
Si5338_SCL_SET(1);
udelay(100);
Si5338_SCL_SET(0);
udelay(20);
}
}
//*********************************************************
//iic 总线接收一个字节数据
//*******************************************************
uint8_t IIC_Rec_BYTE(void)
{
uint8_t i=0;
uint8_t tmp_data=0;
Si5338_SDA_SET(1);
for(i=0;i<=7;i++)
{
Si5338_SCL_SET(1);
udelay(100);
tmp_data=tmp_data<<1;
if(Si5338_SDA_GET_DATA()!=0x00)
{
tmp_data|=0x01;
}
else
{
tmp_data|=0x00;
}
udelay(50);
Si5338_SCL_SET(0);
}
return tmp_data;
}
//
uint8_t Read_Addr_Byte(uint8_t Addr)
{
uint8_t reg =0;
IIC_Start();
IIC_Send_BYTE(SI5338_SLAVE_ADDR0);
IIC_WAIT_ACK();
IIC_Send_BYTE(218);
IIC_WAIT_ACK();
IIC_Stop();
IIC_Start();
IIC_Send_BYTE(SI5338_SLAVE_ADDR1);
IIC_WAIT_ACK();
IIC_Send_BYTE(Addr);
IIC_WAIT_ACK();
reg=IIC_Rec_BYTE(); //OEB_ALL = 1
IIC_SEND_NOACK();
IIC_Stop();
return reg;
}
void IIC_Wr_Addr_Bytes(uint8_t Addr, uint8_t data)
{
IIC_Start();
IIC_Send_BYTE(SI5338_SLAVE_ADDR0);
IIC_WAIT_ACK();
IIC_Send_BYTE(Addr);
IIC_WAIT_ACK();
IIC_Send_BYTE(data); //DIS_LOL = 1
IIC_WAIT_ACK();
IIC_Stop();
}
void Si5338_Register_Config(uint16_t Index)
{
T_Reg_Data curr_register;
uint8_t curr_chip_val =0;
uint8_t clear_curr_val=0;
uint8_t clear_new_val=0;
uint8_t combined=0;
curr_register = Reg_Store[Index];
if(curr_register.Reg_Mask != 0x00) {
if(curr_register.Reg_Mask == 0xFF) {
// do a write transaction only
// since the mask is all ones
IIC_Start();
IIC_Send_BYTE(SI5338_SLAVE_ADDR0);
IIC_WAIT_ACK();
IIC_Send_BYTE(curr_register.Reg_Addr);
IIC_WAIT_ACK();
IIC_Send_BYTE( curr_register.Reg_Val); //OEB_ALL = 1
IIC_WAIT_ACK();
IIC_Stop();
}
else
{
//do a read-modify-write
IIC_Start();
IIC_Send_BYTE(SI5338_SLAVE_ADDR0);
IIC_WAIT_ACK();
IIC_Send_BYTE(curr_register.Reg_Addr);
IIC_WAIT_ACK();
IIC_Stop();
IIC_Start();
IIC_Send_BYTE(SI5338_SLAVE_ADDR1);
IIC_WAIT_ACK();
IIC_Send_BYTE(curr_register.Reg_Addr);
IIC_WAIT_ACK();
curr_chip_val=IIC_Rec_BYTE(); //OEB_ALL = 1
IIC_SEND_NOACK();
IIC_Stop();
clear_curr_val = curr_chip_val & ~curr_register.Reg_Mask;
clear_new_val = curr_register.Reg_Val & curr_register.Reg_Mask;
combined = clear_new_val | clear_curr_val;
// I2C_ByteWrite(curr.Reg_Addr, combined);
IIC_Start();
IIC_Send_BYTE(SI5338_SLAVE_ADDR0);
IIC_WAIT_ACK();
IIC_Send_BYTE(curr_register.Reg_Addr);
IIC_WAIT_ACK();
IIC_Send_BYTE( combined); //OEB_ALL = 1
IIC_WAIT_ACK();
IIC_Stop();
}
}
}