📄 i2c.c
字号:
#include "i2c.h"/* Initializes I2C Module */void i2c_init(T_i2c *sti2c){ /* Disable core and interrupts */ sti2c->u16control = 0; /* Prescale to 10 - 50M/5*100K-1 = 100 = 0x64 */ sti2c->u16prescale_h = 0; sti2c->u16prescale_l = 0x63; /* Enable core without interrupts */ sti2c->u16control = 0x80;}/* Starts transfer with start bit and sends first byte, no stop bit sent *//* Returns one if slave acknowledged */Uint8 i2c_init_byte(T_i2c *sti2c, Uint8 addr,Uint8 mode){ Uint8 x; /* Load tx reg with lsb cleared for wite op */ sti2c->u16buf = (addr | mode) & 0xff; /* Set STA and WR */ sti2c->u16cmdstat = i2c_cmd_sta | i2c_cmd_wr; /* Wait for tip to go low */ while (1) { if ((sti2c->u16cmdstat & i2c_status_tip) == 0) break; } //while (sti2c->u16cmdstat & i2c_status_tip); //x = sti2c->u16cmdstat; x = sti2c->u16cmdstat; /* Transfer has completed, read ack status */ if (x & i2c_status_rxack) return 0; return 1;}/* Sends byte after init condition has already occurred, waits for ack *//* This is for the last byte which will terminate transfer */Uint8 i2c_close_byte(T_i2c *sti2c, Uint8 val, Uint8 mode){ Uint8 x; if (mode == i2c_mode_write) { sti2c->u16buf = val & 0xff; /* Set STO and WR */ sti2c->u16cmdstat = i2c_cmd_sto | i2c_cmd_wr; } else if (mode == i2c_mode_read) { /* Set ack bit to be NACK to terminate transfer */ sti2c->u16cmdstat = i2c_cmd_sto | i2c_cmd_rd | i2c_cmd_ack; } /* Wait for tip to go low */ while (sti2c->u16cmdstat & i2c_status_tip); if (mode == i2c_mode_write) { /* Transfer has completed, read ack status */ if (sti2c->u16cmdstat & i2c_status_rxack) return 0; return 1; } else { /* Read back data */ x = sti2c->u16buf & 0xff; return x; }}Uint8 i2c_mid_byte(T_i2c *sti2c, Uint8 val, Uint8 mode){ Uint8 x; if (mode == i2c_mode_write) { sti2c->u16buf = val & 0xff; /* Set WR */ sti2c->u16cmdstat = i2c_cmd_wr; } else { /* Set ack bit to be ACK to indicate another byte is requested */ sti2c->u16cmdstat = i2c_cmd_rd; } /* Wait for tip to go low */ while (sti2c->u16cmdstat & i2c_status_tip); if (mode == i2c_mode_write) { /* Transfer has completed, read ack status */ if (sti2c->u16cmdstat & i2c_status_rxack) return 0; return 1; } else { /* Read back data */ x = sti2c->u16buf & 0xff; return x; }}/********* User peripherals ***********/Uint8 i2c_get_temp(T_i2c *psti2c){ /* Set pointer */ if (!i2c_init_byte(psti2c, I2C_TEMP_ADDR, i2c_mode_write)) return 0; /* Write to address 0 */ if (!i2c_mid_byte(psti2c, 0, i2c_mode_write)) return 0; /* Initiation read request */ if (!i2c_init_byte(psti2c, I2C_TEMP_ADDR, i2c_mode_read)) return 0; /* Read back single byte */ return i2c_close_byte(psti2c, 0, i2c_mode_read);}/* Time should be in format:Byte 0->Byte 2<h> <m> <s>*/Uint32 rtc_timetoint(Uint8 h, Uint8 m, Uint8 s){ return (Uint32) (h*3600 + m*60 + s);}/* Parse Uint32 value into hours, minutes and seconds */void rtc_inttotime(Uint32 date_val, Uint8 *time_string){ Uint32 tmplong; /* Reduce to only 24h of time */ tmplong = date_val % (3600*24); /* Hours */ time_string[0] = tmplong/3600; tmplong = tmplong % 3600; /* Minutes */ time_string[1] = tmplong/60; /* Seconds */ time_string[2] = tmplong % 60;}Uint8 i2c_rtc_settime(T_i2c *psti2c, Uint32 timeval){ Uint8 tmp; if (!i2c_init_byte(psti2c, I2C_RTC_ADDR, i2c_mode_write)) return 0; if (!i2c_mid_byte(psti2c, 0, i2c_mode_write)) return 0; tmp = (Uint8) (timeval & 0xff); if (!i2c_mid_byte(psti2c, tmp, i2c_mode_write)) return 0; tmp = (Uint8) ((timeval >> 8) & 0xff); if (!i2c_mid_byte(psti2c, tmp, i2c_mode_write)) return 0; tmp = (Uint8) ((timeval >> 16) & 0xff); if (!i2c_mid_byte(psti2c, tmp, i2c_mode_write)) return 0; tmp = (Uint8) ((timeval >> 24) & 0xff); if (!i2c_mid_byte(psti2c, tmp, i2c_mode_write)) return 0; if (!i2c_close_byte(psti2c, 0, i2c_mode_write)) return 0; return 1; }Uint32 i2c_rtc_gettime(T_i2c *psti2c){ Uint32 timeval; Uint8 tmp; if (!i2c_init_byte(psti2c, I2C_RTC_ADDR, i2c_mode_write)) return 0; if (!i2c_mid_byte(psti2c, 0, i2c_mode_write)) return 0; if (!i2c_init_byte(psti2c, I2C_RTC_ADDR, i2c_mode_read)) return 0; timeval = 0; tmp = i2c_mid_byte(psti2c, 0, i2c_mode_read) & 0xff; timeval = (Uint32) tmp; tmp = ((i2c_mid_byte(psti2c, 0, i2c_mode_read)) & 0xff); timeval |= (Uint32) (tmp << 8); tmp = ((i2c_mid_byte(psti2c, 0, i2c_mode_read)) & 0xff); timeval |= (Uint32) (tmp << 16); tmp = ((i2c_close_byte(psti2c, 0, i2c_mode_read)) & 0xff); timeval |= (Uint32) (tmp << 24); return timeval; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -