第一次提交
This commit is contained in:
626
ETH/UdpServer/User/interface_24c02.c
Normal file
626
ETH/UdpServer/User/interface_24c02.c
Normal file
@@ -0,0 +1,626 @@
|
||||
|
||||
|
||||
#include <debug.h>
|
||||
#include "interface_config.h"
|
||||
#include "ch32v30x_i2c.h"
|
||||
|
||||
//#define _nop_() Delay_Us(1);
|
||||
//#define somenop {_nop_();_nop_();_nop_();_nop_();_nop_();}
|
||||
#define bit bool
|
||||
|
||||
#define MASTER_TEST_LEN 100//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E0B7A2>100<30><30><EFBFBD>ֽڵ<D6BD><DAB5><EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
|
||||
#define SCL_GPIO GPIOB
|
||||
#define SCL_PIN GPIO_Pin_10
|
||||
#define SDA_GPIO GPIOB
|
||||
#define SDA_PIN GPIO_Pin_11
|
||||
|
||||
//void iic_start(void);
|
||||
//void iic_stop(void);
|
||||
//void iic_ack(bit ackbit);
|
||||
//void iic_sendbyte(unsigned char byt);
|
||||
//void wrbyte_24c02(unsigned int add,unsigned char dat);
|
||||
//void delay(unsigned char t);
|
||||
|
||||
//bit iic_waitack(void);
|
||||
//unsigned char i2c_recbyte(void);
|
||||
unsigned char rdbyte_24c02(unsigned int add);
|
||||
//unsigned char wrEE_CRC_Bak(unsigned char add,unsigned char *eeData,unsigned char len,unsigned char addbak);
|
||||
//unsigned char rdEE_CRC_Bak(unsigned char add,unsigned char *eeData,unsigned char len,unsigned char addbak);
|
||||
|
||||
|
||||
////FM25CL64ָ<34><EFBFBD><EEB6A8>
|
||||
#define FM25CL64_WREN 0x06 //ʹ<><CAB9>
|
||||
#define FM25CL64_WRDI 0x04 //ʧ<><CAA7>
|
||||
#define FM25CL64_RDSR 0x05 //<2F><>״̬
|
||||
#define FM25CL64_WRSR 0x01 //д״̬
|
||||
#define FM25CL64_READ 0x03 //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
#define FM25CL64_WRITE 0x02 //д<><D0B4><EFBFBD><EFBFBD>
|
||||
|
||||
#define MAX_FM25CL64_LEN 8192//8k<38>ֽ<EFBFBD>
|
||||
|
||||
|
||||
/*********************************************************************
|
||||
* @fn IIC_Init
|
||||
*
|
||||
* @brief Initializes the IIC peripheral.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void iicInit(I2C_TypeDef *I2Cx, u32 bound, u16 address)
|
||||
{
|
||||
GPIO_InitTypeDef GPIO_InitStructure = { 0 };
|
||||
// I2C_InitTypeDef I2C_InitTSturcture = { 0 };
|
||||
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
|
||||
// if(I2Cx == I2C1)//<2F>ж<EFBFBD><D0B6><EFBFBD>I2C1<43><31><EFBFBD><EFBFBD>I2C2
|
||||
// {
|
||||
// RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);
|
||||
// }
|
||||
|
||||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
|
||||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||||
GPIO_Init(GPIOB, &GPIO_InitStructure);
|
||||
|
||||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
|
||||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||||
GPIO_Init(GPIOB, &GPIO_InitStructure);
|
||||
|
||||
// I2C_InitTSturcture.I2C_ClockSpeed = bound;
|
||||
// I2C_InitTSturcture.I2C_Mode = I2C_Mode_I2C;
|
||||
// I2C_InitTSturcture.I2C_DutyCycle = I2C_DutyCycle_16_9;
|
||||
// I2C_InitTSturcture.I2C_OwnAddress1 = address;
|
||||
// I2C_InitTSturcture.I2C_Ack = I2C_Ack_Enable;
|
||||
// I2C_InitTSturcture.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
|
||||
// I2C_Init(I2Cx, &I2C_InitTSturcture);
|
||||
|
||||
// I2C_Cmd(I2Cx, ENABLE);
|
||||
|
||||
rm3100Init();//<2F><>ʼ<EFBFBD><CABC>RM3100
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#ifdef _EEROM_CRC_
|
||||
#define POLY 0x1021
|
||||
/**
|
||||
* Calculating CRC-16 in 'C'
|
||||
* @para addr, start of data
|
||||
* @para num, length of data
|
||||
* @para crc, incoming CRC
|
||||
*/
|
||||
unsigned int crc16(unsigned char *addr, int num, unsigned int crc)
|
||||
{
|
||||
int i;
|
||||
for (; num > 0; num--) /* Step through bytes in memory */
|
||||
{
|
||||
crc = crc ^ ((*addr) << 8); /* Fetch byte from memory, XOR into CRC top byte*/
|
||||
addr++;
|
||||
for (i = 0; i < 8; i++) /* Prepare to rotate 8 bits */
|
||||
{
|
||||
if (crc & 0x8000) /* b15 is set... */
|
||||
crc = (crc << 1) ^ POLY; /* rotate and XOR with polynomic */
|
||||
else /* b15 is clear... */
|
||||
crc <<= 1; /* just rotate */
|
||||
} /* Loop for 8 bits */
|
||||
crc &= 0xFFFF; /* Ensure CRC remains 16-bit value */
|
||||
} /* Loop until num=0 */
|
||||
return(crc); /* Return updated CRC */
|
||||
}
|
||||
|
||||
//д<><D0B4><EFBFBD><EFBFBD>ȷ<EFBFBD><C8B7><EFBFBD>Զ<EFBFBD>дһ<D0B4><D2BB><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͳ<EFBFBD><CDB2><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD>
|
||||
unsigned char wrEE_CRC_Bak(unsigned char add,unsigned char *eeData,unsigned char len,unsigned char addbak)
|
||||
{
|
||||
uword crc_Res;
|
||||
unsigned char index;
|
||||
unsigned char flagEEwrOK;
|
||||
|
||||
crc_Res = crc16(eeData, len-2, 0xffff); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD>У<EFBFBD><D0A3>
|
||||
eeData[len-2] = (ubyte)(crc_Res );
|
||||
eeData[len-1] = (ubyte)(crc_Res >> 8);
|
||||
|
||||
for(index=0;index<len;index++) //д<><D0B4><EFBFBD><EFBFBD>
|
||||
{WDT_vServiceWDT();
|
||||
wrbyte_24c02( add + index, eeData[index]);
|
||||
}
|
||||
flagEEwrOK = 1;
|
||||
|
||||
for(index=0;index<len;index++) //У<><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
{WDT_vServiceWDT();
|
||||
if(rdbyte_24c02( add + index) != eeData[index])
|
||||
{
|
||||
flagEEwrOK = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(1 == flagEEwrOK) //
|
||||
{
|
||||
for(index=0;index<len;index++) //д<><D0B4><EFBFBD>ݺ<EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
{WDT_vServiceWDT();
|
||||
wrbyte_24c02( addbak + index, eeData[index]);
|
||||
}
|
||||
flagEEwrOK = 2;
|
||||
for(index=0;index<len;index++)
|
||||
{WDT_vServiceWDT();
|
||||
if(rdbyte_24c02( addbak + index) != eeData[index])
|
||||
{ flagEEwrOK = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
flagEEwrOK = 0;
|
||||
//.........
|
||||
}
|
||||
return flagEEwrOK;
|
||||
}
|
||||
unsigned char rdEE_CRC_Bak(unsigned char add,unsigned char *eeData,unsigned char len,unsigned char addbak)
|
||||
{
|
||||
uword crc_Res;
|
||||
unsigned char index;
|
||||
unsigned char reEEok = 0;
|
||||
|
||||
for(index=0;index<len;index++) //<2F><><EFBFBD><EFBFBD><EFBFBD>ݺ<EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
{WDT_vServiceWDT();
|
||||
eeData[index] = rdbyte_24c02(add + index);
|
||||
}
|
||||
crc_Res = crc16(eeData, len-2, 0xffff); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD>У<EFBFBD><D0A3>
|
||||
if( crc_Res == ((uword)eeData[len-1] << 8) + (uword)eeData[len-2]) //<2F>Աȶ<D4B1>ȡ<EFBFBD><C8A1>У<EFBFBD><D0A3>ֵ <20><>ȷ<EFBFBD><C8B7><EFBFBD><EFBFBD>EEֵ<45><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȷ...
|
||||
{
|
||||
reEEok = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
for(index=0;index<len;index++) //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݺ<EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
{WDT_vServiceWDT();
|
||||
eeData[index] = rdbyte_24c02(addbak + index);
|
||||
}
|
||||
crc_Res = crc16(eeData, len-2, 0xffff); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD>У<EFBFBD><D0A3>
|
||||
if( crc_Res == ((uword)eeData[len-1] << 8) + (uword)eeData[len-2]) //<2F>Աȶ<D4B1>ȡ<EFBFBD><C8A1>У<EFBFBD><D0A3>ֵ <20><>ȷ<EFBFBD><C8B7><EFBFBD><EFBFBD>EEֵ<45><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȷ...
|
||||
{
|
||||
reEEok = 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
reEEok = 0;
|
||||
}
|
||||
}
|
||||
return reEEok;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
void SDAOut(bit c)
|
||||
{
|
||||
GPIO_WriteBit(SDA_GPIO, SDA_PIN, c);
|
||||
}
|
||||
|
||||
void SCLOut(bit c)
|
||||
{
|
||||
GPIO_WriteBit(SCL_GPIO, SCL_PIN, c);
|
||||
}
|
||||
|
||||
uint8_t readSDA(void)
|
||||
{
|
||||
return GPIO_ReadInputDataBit(SDA_GPIO, SDA_PIN);
|
||||
}
|
||||
|
||||
|
||||
void iic_start(void)
|
||||
{
|
||||
SDAOut(1);
|
||||
_nop_();
|
||||
SCLOut(1);
|
||||
somenop;
|
||||
SDAOut(0);
|
||||
somenop;
|
||||
SCLOut(0);
|
||||
}
|
||||
|
||||
void iic_stop(void)
|
||||
{
|
||||
SDAOut(0);
|
||||
_nop_();
|
||||
SCLOut(1);
|
||||
somenop;
|
||||
SDAOut(1);
|
||||
}
|
||||
|
||||
void iic_ack(bit ackbit)
|
||||
{
|
||||
if(ackbit)
|
||||
SDAOut(0);
|
||||
else
|
||||
SDAOut(1);
|
||||
somenop;
|
||||
SCLOut(1);
|
||||
somenop;
|
||||
SCLOut(0);
|
||||
SDAOut(1);
|
||||
somenop;
|
||||
}
|
||||
|
||||
bit iic_waitack(void)
|
||||
{
|
||||
SDAOut(1);
|
||||
somenop;
|
||||
SCLOut(1);
|
||||
// sdrv_pinctrl_set_input_select(GPIO_Y3, PIN_IS_CMOS_SCHMITT);
|
||||
// sdrv_gpio_set_pin_direction(GPIO_Y3, GPIO_DIR_IN);
|
||||
somenop;
|
||||
if(readSDA())
|
||||
{
|
||||
SCLOut(0);
|
||||
iic_stop();
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
SCLOut(0);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
void iic_sendbyte(unsigned char byt)
|
||||
{
|
||||
unsigned char i;
|
||||
for(i=0;i<8;i++)
|
||||
{
|
||||
if(byt&0x80)
|
||||
SDAOut(1);
|
||||
else
|
||||
SDAOut(0);
|
||||
somenop;
|
||||
SCLOut(1);
|
||||
byt <<= 1;
|
||||
somenop;
|
||||
SCLOut(0);
|
||||
}
|
||||
}
|
||||
|
||||
unsigned char iic_recbyte(void)
|
||||
{
|
||||
unsigned char da = 0;
|
||||
unsigned char i = 0;
|
||||
for(i=0;i<8;i++)
|
||||
{
|
||||
SCLOut(1);
|
||||
// sdrv_pinctrl_set_input_select(GPIO_Y3, PIN_IS_CMOS_SCHMITT);
|
||||
// sdrv_gpio_set_pin_direction(GPIO_Y3, GPIO_DIR_IN);
|
||||
somenop;
|
||||
da <<= 1;
|
||||
|
||||
// P1_IOCR01 = 0x0020; // load port control register 1
|
||||
if(readSDA())
|
||||
da |= 0x01;
|
||||
SCLOut(0);
|
||||
somenop;
|
||||
}
|
||||
return da;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>ģ<EFBFBD><C4A3>I2C<32>汾<EFBFBD><E6B1BE>
|
||||
* @param reg_addr : <20><>ʼ<EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>ַ
|
||||
* @param pData : <20>洢<EFBFBD><E6B4A2>ȡ<EFBFBD><C8A1><EFBFBD>ݵ<EFBFBD><DDB5><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>
|
||||
* @param len : <20><><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>
|
||||
* @retval <20><><EFBFBD><EFBFBD>״̬<D7B4><CCAC>0-<2D>ɹ<EFBFBD><C9B9><EFBFBD>1-ʧ<><CAA7>
|
||||
*/
|
||||
uint8_t rm3100ReadRegs(uint8_t reg_addr, uint8_t *pData, uint16_t len)
|
||||
{
|
||||
// 1. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
|
||||
iic_start();
|
||||
|
||||
// 2. <20><><EFBFBD><EFBFBD><EFBFBD>豸<EFBFBD><E8B1B8>ַ<EFBFBD><D6B7>дģʽ<C4A3><CABD>
|
||||
iic_sendbyte(RM3100_ADDRESS); // <20><>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>1λ<31><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ0<CEBB><30>ʾд
|
||||
if (!iic_waitack())
|
||||
{ // <20>ȴ<EFBFBD>ACK
|
||||
iic_stop();
|
||||
return 1;
|
||||
}
|
||||
|
||||
// 3. <20><><EFBFBD>ͼĴ<CDBC><C4B4><EFBFBD><EFBFBD><EFBFBD>ַ
|
||||
iic_sendbyte(reg_addr);
|
||||
if (!iic_waitack())
|
||||
{
|
||||
iic_stop();
|
||||
return 1;
|
||||
}
|
||||
|
||||
// 4. <20>ظ<EFBFBD><D8B8><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
|
||||
iic_start();
|
||||
|
||||
// 5. <20><><EFBFBD><EFBFBD><EFBFBD>豸<EFBFBD><E8B1B8>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>ģʽ<C4A3><CABD>
|
||||
iic_sendbyte((RM3100_ADDRESS) | 0x01); // <20><><EFBFBD><EFBFBD>λ1<CEBB><31>ʾ<EFBFBD><CABE>
|
||||
if (!iic_waitack())
|
||||
{
|
||||
iic_stop();
|
||||
return 1;
|
||||
}
|
||||
|
||||
// 6. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
while (len > 0)
|
||||
{
|
||||
*pData++ = iic_recbyte(); // <20><>ȡ<EFBFBD>ֽ<EFBFBD>
|
||||
len--;
|
||||
|
||||
// <20><><EFBFBD><EFBFBD>ACK/NACK
|
||||
if (len > 0)
|
||||
{
|
||||
iic_ack(1); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ֽڣ<D6BD><DAA3><EFBFBD><EFBFBD><EFBFBD>ACK
|
||||
}
|
||||
else
|
||||
{
|
||||
iic_ack(0); // <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ֽڣ<D6BD><DAA3><EFBFBD><EFBFBD><EFBFBD>NACK
|
||||
}
|
||||
}
|
||||
|
||||
// 7. <20><><EFBFBD><EFBFBD>ֹͣ<CDA3><D6B9><EFBFBD><EFBFBD>
|
||||
iic_stop();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
unsigned char wrbyte_24c02(unsigned int add,unsigned char dat)
|
||||
{
|
||||
#if EN_24C02
|
||||
// uint8_t device_add = 0xA0; // <20>̶<EFBFBD><CCB6>豸<EFBFBD><E8B1B8>ַ
|
||||
uint8_t addr_high = (add >> 8) & 0xFF; // <20><>ַ<EFBFBD><D6B7><EFBFBD>ֽ<EFBFBD>
|
||||
uint8_t addr_low = add & 0xFF; // <20><>ַ<EFBFBD><D6B7><EFBFBD>ֽ<EFBFBD>
|
||||
//-------------------------------------------------
|
||||
if(add > E2_PAGE_SIZE)
|
||||
{
|
||||
return 1;//<2F><>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>
|
||||
}
|
||||
|
||||
iic_start();
|
||||
iic_sendbyte(EEPROM_ADDRESS);
|
||||
iic_waitack();
|
||||
|
||||
iic_sendbyte(addr_high);
|
||||
iic_waitack();
|
||||
|
||||
iic_sendbyte(addr_low);
|
||||
iic_waitack();
|
||||
|
||||
iic_sendbyte(dat);
|
||||
iic_waitack();
|
||||
iic_stop();
|
||||
#else
|
||||
if(add >= MAX_FM25CL64_LEN)//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>
|
||||
{
|
||||
printf("E2 len error!\r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
m_tx_buf_u8[0] = FM25CL64_WREN;//дʹ<D0B4><CAB9>
|
||||
if (sdrv_spi_sync_transmit(&g_master, m_tx_buf_u8, m_rx_buf_u8,1,1))
|
||||
{
|
||||
printf("E2 write Failed!\r\n");
|
||||
}
|
||||
|
||||
m_tx_buf_u8[0] = FM25CL64_WRITE;//д<><D0B4><EFBFBD><EFBFBD>
|
||||
m_tx_buf_u8[1] = (uint8_t)( add >> 8);// <20><>8λ<38><CEBB>ַ
|
||||
m_tx_buf_u8[2] = (uint8_t)( add );//
|
||||
m_tx_buf_u8[3] = dat;//
|
||||
|
||||
if (sdrv_spi_sync_transmit(&g_master, m_tx_buf_u8, m_rx_buf_u8,4,4))
|
||||
{
|
||||
printf("E2 write Failed!\r\n");
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned char rdbyte_24c02(unsigned int add)
|
||||
{
|
||||
#if EN_24C02
|
||||
uint8_t addr_high = (add >> 8) & 0xFF;
|
||||
uint8_t addr_low = add & 0xFF;
|
||||
uint8_t data = 0;
|
||||
//-------------------------------------------------
|
||||
|
||||
// Device Address 1100 000 R/W
|
||||
iic_start();
|
||||
iic_sendbyte(EEPROM_ADDRESS);
|
||||
iic_waitack();
|
||||
iic_sendbyte(addr_high);
|
||||
iic_waitack();
|
||||
iic_sendbyte(addr_low);
|
||||
iic_waitack();
|
||||
|
||||
iic_start();
|
||||
iic_sendbyte(EEPROM_ADDRESS | 0x01);
|
||||
iic_waitack();
|
||||
data = iic_recbyte();
|
||||
iic_ack(0);
|
||||
iic_stop();
|
||||
//CAN_sendAck(add, da);
|
||||
return data;
|
||||
#else
|
||||
if(add >= MAX_FM25CL64_LEN)//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>
|
||||
{
|
||||
printf("E2 len error!\r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
m_tx_buf_u8[0] = FM25CL64_READ;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
m_tx_buf_u8[1] = (uint8_t)( add >> 8);// <20><>8λ<38><CEBB>ַ
|
||||
m_tx_buf_u8[2] = (uint8_t)( add );//
|
||||
m_tx_buf_u8[3] = 0xFF;//
|
||||
|
||||
if (sdrv_spi_sync_transmit(&g_master, m_tx_buf_u8, m_rx_buf_u8,4,4))
|
||||
{
|
||||
printf("E2 write Failed!\r\n");
|
||||
}
|
||||
|
||||
return m_rx_buf_u8[3];
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief <20><><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>ģ<EFBFBD><C4A3>I2C<32>汾<EFBFBD><E6B1BE>
|
||||
* @param reg_addr : <20><>ʼ<EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>ַ
|
||||
* @param values : Ҫд<D2AA><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>
|
||||
* @param len : <20><><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>
|
||||
* @retval <20><><EFBFBD><EFBFBD>״̬<D7B4><CCAC>0-<2D>ɹ<EFBFBD><C9B9><EFBFBD>1-ʧ<><CAA7>
|
||||
*/
|
||||
uint8_t rm3100WriteMultiReg(uint8_t reg_addr, const uint8_t *values, uint8_t len)
|
||||
{
|
||||
// 1. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
|
||||
iic_start();
|
||||
|
||||
// 2. <20><><EFBFBD><EFBFBD><EFBFBD>豸<EFBFBD><E8B1B8>ַ<EFBFBD><D6B7>дģʽ<C4A3><CABD>
|
||||
iic_sendbyte(RM3100_ADDRESS); // <20><>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>1λ<31><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ0<CEBB><30>ʾд
|
||||
if (!iic_waitack()) { // <20>ȴ<EFBFBD>ACK
|
||||
iic_stop();
|
||||
return 1;
|
||||
}
|
||||
|
||||
// 3. <20><><EFBFBD>ͼĴ<CDBC><C4B4><EFBFBD><EFBFBD><EFBFBD>ַ
|
||||
iic_sendbyte(reg_addr);
|
||||
if (!iic_waitack()) {
|
||||
iic_stop();
|
||||
return 1;
|
||||
}
|
||||
|
||||
// 4. ѭ<><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
for (uint8_t i = 0; i < len; i++) {
|
||||
iic_sendbyte(values[i]);
|
||||
if (!iic_waitack()) { // ÿ<><C3BF><EFBFBD><EFBFBD><EFBFBD>ݺ<EFBFBD><DDBA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ACK
|
||||
iic_stop();
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// 5. <20><><EFBFBD><EFBFBD>ֹͣ<CDA3><D6B9><EFBFBD><EFBFBD>
|
||||
iic_stop();
|
||||
return 0;
|
||||
}
|
||||
|
||||
//<2F>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><D7BC><EFBFBD><EFBFBD>
|
||||
uint8_t rm3100CheckDataReady(void)
|
||||
{
|
||||
uint8_t cResult = 0;
|
||||
|
||||
rm3100ReadRegs(0x34, &cResult, 1);
|
||||
cResult = cResult&0x80;
|
||||
|
||||
return cResult;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void rm3100Init(void)
|
||||
{
|
||||
uint8_t CCR[6] = {0, 200, 0, 200, 0, 200};//200
|
||||
|
||||
rm3100WriteMultiReg(ADDR_CCX, CCR, 6);//<2F><><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>poll<6C><6C><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
|
||||
uint8_t data = POLL_XYZ;
|
||||
rm3100WriteMultiReg(ADDR_POLL, &data, 1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//<2F>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><D7BC><EFBFBD><EFBFBD>
|
||||
uint8_t RM3100_GetData(short *x,short *y,short*z)
|
||||
{
|
||||
static long Mag_Data[3] = {0};
|
||||
uint8_t temp[9]={0};
|
||||
uint8_t poll_request = POLL_XYZ;
|
||||
static uint8_t cNoDataCnt = 0;
|
||||
//------------------------------------------------
|
||||
if(rm3100CheckDataReady() == 0x80)
|
||||
{//data ready
|
||||
//<2F><>ȡ9<C8A1><39><EFBFBD>ֽ<EFBFBD>
|
||||
rm3100ReadRegs(0x24, temp, 9);
|
||||
|
||||
Mag_Data[0]= (long)temp[0]<<16 | (long)temp[1]<<8 | temp[2];
|
||||
Mag_Data[1] = (long)temp[3]<<16 | (long)temp[4]<<8 | temp[5];
|
||||
Mag_Data[2] = (long)temp[6]<<16 | (long)temp[7]<<8 | temp[8];
|
||||
|
||||
for(uint8_t i=0; i<3; i++)//<2F><><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>
|
||||
{
|
||||
if(Mag_Data[i]&0x00800000)
|
||||
Mag_Data[i] |= 0xff000000;
|
||||
}
|
||||
|
||||
*y = Mag_Data[0];
|
||||
*x = Mag_Data[1];
|
||||
*z = Mag_Data[2];
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>poll<6C><6C><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
|
||||
rm3100WriteMultiReg(ADDR_POLL, &poll_request, 1);
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
cNoDataCnt +=5;
|
||||
if (cNoDataCnt >= 20)
|
||||
{
|
||||
cNoDataCnt = 0;
|
||||
rm3100WriteMultiReg(ADDR_POLL, &poll_request, 1);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user