lpc51u68 85063 i2c驱动

#define IOCON_PIO_DIGITAL_EN 0x80u /*!< Enables digital function /
#define IOCON_PIO_FUNC1 0x01u /
!< Selects pin function 1 /
#define IOCON_PIO_I2CDRIVE_LOW 0x00u /
!< Low drive: 4 mA /
#define IOCON_PIO_I2CFILTER_EN 0x00u /
!< I2C 50 ns glitch filter enabled /
#define IOCON_PIO_I2CSLEW_I2C 0x00u /
!< I2C mode /
#define IOCON_PIO_INPFILT_OFF 0x0100u /
!< Input filter disabled /
#define IOCON_PIO_INV_DI 0x00u /
!< Input function is not inverted /
#define IOCON_PIO_MODE_INACT 0x00u /
!< No addition pin function /
#define IOCON_PIO_OPENDRAIN_DI 0x00u /
!< Open drain is disabled /
#define IOCON_PIO_SLEW_STANDARD 0x00u /
!< Standard mode, output slew rate control is enabled /
#define PIN0_IDX 0u /
!< Pin number for pin 0 in a port 0 /
#define PIN1_IDX 1u /
!< Pin number for pin 1 in a port 0 /
#define PIN18_IDX 18u /
!< Pin number for pin 18 in a port 0 /
#define PIN20_IDX 20u /
!< Pin number for pin 20 in a port 0 /
#define PORT0_IDX 0u /
!< Port index */

#define EXAMPLE_I2C_MASTER_BASE (I2C5_BASE)
#define I2C_MASTER_CLOCK_FREQUENCY (12000000)

#define EXAMPLE_I2C_MASTER ((I2C_Type *)EXAMPLE_I2C_MASTER_BASE)

#define I2C_MASTER_SLAVE_ADDR_7BIT (0x51U)
#define I2C_BAUDRATE (100000) /* 100K /
#define I2C_DATA_LENGTH (7) /
MAX is 256 */

uint8_t g_master_txBuff[I2C_DATA_LENGTH];
uint8_t g_master_rxBuff[I2C_DATA_LENGTH];
uint8_t c8563_Store111[4]={0x00,0x38,0x13,0x05}; /写入时间初值:星期一 07:59:00/
i2c_master_handle_t g_m_handle;

/******************************************************************************

  • 名称:

  • 描述:

  • 参数: [in] 无

  •   [out] 无
    
  • 返回: 无
    *******************************************************************************/
    void HAL_I2c_Init(void)
    {

    /* Function assigned for the Cortex-M0P /
    CLOCK_EnableClock(kCLOCK_Iocon); /
    Enables the clock for the IOCON block. 0 = Disable; 1 = Enable.: 0x01u */

    /* attach 12 MHz clock to FLEXCOMM4 (I2C master) /
    CLOCK_AttachClk(kFRO12M_to_FLEXCOMM5);
    /
    reset FLEXCOMM for I2C */
    RESET_PeripheralReset(kFC5_RST_SHIFT_RSTn);

    const uint32_t port0_pin18_config = (
    IOCON_PIO_FUNC1 | /* Pin is configured as FC4_RTS_SCL_SSEL1 /
    IOCON_PIO_I2CSLEW_I2C | /
    I2C mode /
    IOCON_PIO_INV_DI | /
    Input function is not inverted /
    IOCON_PIO_DIGITAL_EN | /
    Enables digital function /
    IOCON_PIO_INPFILT_OFF | /
    Input filter disabled /
    IOCON_PIO_I2CDRIVE_LOW | /
    Low drive: 4 mA /
    IOCON_PIO_I2CFILTER_EN /
    I2C 50 ns glitch filter enabled /
    );
    IOCON_PinMuxSet(IOCON, PORT0_IDX, PIN18_IDX, port0_pin18_config); /
    PORT0 PIN18 (coords: 3) is configured as FC5_RTS_SCL_SSEL1 */

    const uint32_t port0_pin20_config = (
    IOCON_PIO_FUNC1 | /* Pin is configured as FC4_CTS_SDA_SSEL0 /
    IOCON_PIO_I2CSLEW_I2C | /
    I2C mode /
    IOCON_PIO_INV_DI | /
    Input function is not inverted /
    IOCON_PIO_DIGITAL_EN | /
    Enables digital function /
    IOCON_PIO_INPFILT_OFF | /
    Input filter disabled /
    IOCON_PIO_I2CDRIVE_LOW | /
    Low drive: 4 mA /
    IOCON_PIO_I2CFILTER_EN /
    I2C 50 ns glitch filter enabled /
    );
    IOCON_PinMuxSet(IOCON, PORT0_IDX, PIN20_IDX, port0_pin20_config); /
    PORT0 PIN20 (coords: 4) is configured as FC5_CTS_SDA_SSEL0 */

    i2c_master_config_t masterConfig;
    /*

    • masterConfig.debugEnable = false;
    • masterConfig.ignoreAck = false;
    • masterConfig.pinConfig = kI2C_2PinOpenDrain;
    • masterConfig.baudRate_Bps = 100000U;
    • masterConfig.busIdleTimeout_ns = 0;
    • masterConfig.pinLowTimeout_ns = 0;
    • masterConfig.sdaGlitchFilterWidth_ns = 0;
    • masterConfig.sclGlitchFilterWidth_ns = 0;
      */
      I2C_MasterGetDefaultConfig(&masterConfig);

    /* Change the default baudrate configuration */
    masterConfig.baudRate_Bps = I2C_BAUDRATE;

    /* Initialize the I2C master peripheral */
    I2C_MasterInit(EXAMPLE_I2C_MASTER, &masterConfig, I2C_MASTER_CLOCK_FREQUENCY);
    #if 0
    HAL_I2C_WriteTest(0x04,0x30);
    HAL_I2C_WriteTest(0x05,0x59);
    HAL_I2C_WriteTest(0x06,0x08);
    HAL_I2C_WriteTest(0x07,0);
    #endif
    }

int HAL_I2C_WriteTest(uint8_t deviceAddress,uint8_t devicedata)
{
status_t reVal = kStatus_Fail;
// uint8_t deviceAddress = 0x04U;
// uint8_t devicedata = 0x06U;
//uint8_t c8563_Store[4]={0x00,0x38,0x13,0x05}; /写入时间初值:星期一 07:59:00/

/* Send master blocking data to slave */
if (kStatus_Success == I2C_MasterStart(EXAMPLE_I2C_MASTER, I2C_MASTER_SLAVE_ADDR_7BIT, kI2C_Write))
{
    /* subAddress = 0x01, data = g_master_txBuff - write to slave.
      start + slaveaddress(w) + subAddress + length of data buffer + data buffer + stop*/
    reVal = I2C_MasterWriteBlocking(EXAMPLE_I2C_MASTER, &deviceAddress, 1, kI2C_TransferNoStopFlag);
    if (reVal != kStatus_Success)
    {
        return -1;
    }
    
    reVal = I2C_MasterWriteBlocking(EXAMPLE_I2C_MASTER, &devicedata, 1, kI2C_TransferNoStopFlag);
    if (reVal != kStatus_Success)
    {
        return -1;
    }
   

    reVal = I2C_MasterStop(EXAMPLE_I2C_MASTER);
    if (reVal != kStatus_Success)
    {
        return -1;
    }
}

return 1;

}

int HAL_I2C_ReadTest()
{
status_t reVal = kStatus_Fail;
uint8_t deviceAddress = 0x04U;
#if 0
while(1)
{
I2C_MasterStart(EXAMPLE_I2C_MASTER, I2C_MASTER_SLAVE_ADDR_7BIT, kI2C_Write);
I2C_MasterWriteBlocking(EXAMPLE_I2C_MASTER, &deviceAddress, 1, kI2C_TransferNoStopFlag);

}
#endif
/* Send master blocking data to slave /
if (kStatus_Success == I2C_MasterStart(EXAMPLE_I2C_MASTER, I2C_MASTER_SLAVE_ADDR_7BIT, kI2C_Write))
{
/
subAddress = 0x01, data = g_master_txBuff - write to slave.
start + slaveaddress(w) + subAddress + length of data buffer + data buffer + stop*/
reVal = I2C_MasterWriteBlocking(EXAMPLE_I2C_MASTER, &deviceAddress, 1, kI2C_TransferDefaultFlag);
if (reVal != kStatus_Success)
{
return -1;
}
/*
reVal = I2C_MasterWriteBlocking(EXAMPLE_I2C_MASTER, g_master_txBuff, 4, 0);
if (reVal != kStatus_Success)
{
return -1;
}
*/

    reVal = I2C_MasterStop(EXAMPLE_I2C_MASTER);
    if (reVal != kStatus_Success)
    {
        return -1;
    }
}


/* Receive blocking data from slave */
/* subAddress = 0x01, data = g_master_rxBuff - read from slave.
  start + slaveaddress(w) + subAddress + repeated start + slaveaddress(r) + rx data buffer + stop */
if (kStatus_Success == I2C_MasterStart(EXAMPLE_I2C_MASTER, I2C_MASTER_SLAVE_ADDR_7BIT, kI2C_Write))
{
    /* subAddress = 0x01, data = g_master_txBuff - write to slave.
      start + slaveaddress(w) + subAddress + length of data buffer + data buffer + stop*/
    reVal = I2C_MasterWriteBlocking(EXAMPLE_I2C_MASTER, &deviceAddress, 1, kI2C_TransferNoStopFlag);
    if (reVal != kStatus_Success)
    {
        return -1;
    }

    reVal = I2C_MasterRepeatedStart(EXAMPLE_I2C_MASTER, I2C_MASTER_SLAVE_ADDR_7BIT, kI2C_Read);
    if (reVal != kStatus_Success)
    {
        return -1;
    }

    reVal = I2C_MasterReadBlocking(EXAMPLE_I2C_MASTER, g_master_rxBuff, I2C_DATA_LENGTH , 0);
    if (reVal != kStatus_Success)
    {
        return -1;
    }

    reVal = I2C_MasterStop(EXAMPLE_I2C_MASTER);
    if (reVal != kStatus_Success)
    {
        return -1;
    }
}



#if 0
PRINTF("%2x : %2x :%2x\r\n", g_master_rxBuff[2]&0x3f,g_master_rxBuff[1]&0x7f,g_master_rxBuff[0]&0x7f);
#endif

return 1;
}

/******************************************************************************

  • 名称: int2hex

  • 描述: 10进制转化成16进制 转化规则是 21->0x21 12->0x12

  • 参数: [in] 无

  •   [out] 无
    
  • 返回: 无
    *******************************************************************************/
    int int2hex(int n)
    {
    int i = 0;
    int j = 0;
    int e = 1;
    int len;
    char res[5];
    int iRetVal = 0;
    memset(res,0,sizeof(res));
    sprintf(res,"%d",n);
    len = strlen(res);
    for(i=0;i<len;i++)
    {
    e =1;
    for(j = 0; j <(len-i-1); j++)
    {
    e = 16;
    }
    iRetVal += ( res[i]-48)
    (e);
    }

    return iRetVal;
    }

/******************************************************************************

  • 名称: RTC_DRV_SetDatetime

  • 描述:

  • 参数: [in] 无

  •   [out] 无
    
  • 返回: 无
    *******************************************************************************/
    bool RTC_DRV_SetDatetime(uint32_t instance, rtc_datetime_t *datetime)
    {

    uint16_t year; /!< Range from 0 to 99./
    uint8_t month; /!< Range from 1 to 12./
    uint8_t day; /!< Range from 1 to 31 (depending on month)./
    uint8_t hour; /!< Range from 0 to 23./
    uint8_t minute; /!< Range from 0 to 59./
    uint8_t second; /!< Range from 0 to 59./

    //设置的时间要求是16机制的,要转化成16进制的
    second = int2hex(datetime->second);
    hour = int2hex(datetime->hour);
    day = int2hex(datetime->day);
    month = int2hex(datetime->month);
    year = int2hex(datetime->year);
    minute = int2hex(datetime->minute);

    HAL_I2C_WriteTest(0x04,second);
    HAL_I2C_WriteTest(0x05,minute);
    HAL_I2C_WriteTest(0x06,hour);
    HAL_I2C_WriteTest(0x07,day);
    HAL_I2C_WriteTest(0x09,month);
    HAL_I2C_WriteTest(0x0A,year);
    return true;

}

/******************************************************************************

  • 名称: RTC_DRV_GetDatetime

  • 描述:

  • 参数: [in] 无

  •   [out] 无
    
  • 返回: 无
    *******************************************************************************/
    void RTC_DRV_GetDatetime(uint32_t instance, rtc_datetime_t *datetime)
    {
    char abuf[5];

    HAL_I2C_ReadTest();//读取时间

    //获取到的都是16机制的,要转化成10进制的
    sprintf(abuf,"%x",g_master_rxBuff[0]&0x7f);
    datetime->second = atoi(abuf);

    sprintf(abuf,"%x",g_master_rxBuff[1]&0x7f);
    datetime->minute = atoi(abuf);

    sprintf(abuf,"%x",g_master_rxBuff[2]&0x3f);
    datetime->hour = atoi(abuf);

    sprintf(abuf,"%x",g_master_rxBuff[3]&0x3f);
    datetime->day = atoi(abuf);

    sprintf(abuf,"%x",g_master_rxBuff[5]&0x7f);
    datetime->month = atoi(abuf);

    sprintf(abuf,"%x",g_master_rxBuff[6]&0x7f);
    datetime->year = atoi(abuf);

    #if 1
    printf(“year =%2d,mon = %2d,day = %2d\r\n”,datetime->year,datetime->month,datetime->day);
    printf(“hour =%2d,min = %2d,sec = %2d\r\n”,datetime->hour,datetime->minute,datetime->second);
    #endif

}
void HAL_I2cInit(void)
{
HAL_I2c_Init();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值