123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
-
- #include "i2c_master_poll.h"
- extern __IO uint8_t I2C_timeout;
- #define dead_time() { }
- #define tout() (I2C_timeout)
- #define set_tout_ms(a) { I2C_timeout = a; }
- void I2C_Init(void) {
-
- GPIOB->ODR |= (GPIO_PIN_4 | GPIO_PIN_5);
- GPIOB->DDR |= (GPIO_PIN_4 | GPIO_PIN_5);
- GPIOB->CR2 |= (GPIO_PIN_4 | GPIO_PIN_5);
- #ifdef FAST_I2C_MODE
- I2C->FREQR = 16;
- I2C->CCRL = 15;
- I2C->CCRH = 0x80;
- I2C->TRISER = 5;
- #else
- I2C->FREQR = 8;
- I2C->CCRL = 40;
- I2C->CCRH = 0;
- I2C->TRISER = 9;
- #endif
- I2C->OARL = 0xA0;
- I2C->OARH |= 0x40;
- I2C->ITR = 1;
- I2C->CR2 |= 0x04;
- I2C->CR1 |= 0x01;
- }
- void I2C_ReadRegister(u8 u8_regAddr, u8 u8_NumByteToRead, u8 *ReadBuffer)
- {
-
- while(I2C->SR3 & I2C_SR3_BUSY && tout())
- {
- I2C->CR2 |= I2C_CR2_STOP;
- while(I2C->CR2 & I2C_CR2_STOP && tout());
- }
- I2C->CR2 |= I2C_CR2_ACK;
-
- I2C->CR2 |= I2C_CR2_START;
- while((I2C->SR1 & I2C_SR1_SB)==0 && tout());
-
- if(tout())
- {
- I2C->DR = (u8)(SLAVE_ADDRESS << 1);
- }
- while(!(I2C->SR1 & I2C_SR1_ADDR) && tout());
- dead_time();
- I2C->SR3;
-
- while(!(I2C->SR1 & I2C_SR1_TXE) && tout());
- if(tout())
- {
- I2C->DR = u8_regAddr;
- }
- while((I2C->SR1 & (I2C_SR1_TXE | I2C_SR1_BTF)) != (I2C_SR1_TXE | I2C_SR1_BTF) && tout());
- dead_time();
-
- #ifdef NO_RESTART
- I2C->CR2 |= I2C_CR2_STOP;
- while(I2C->CR2 & I2C_CR2_STOP && tout());
- #endif
-
- I2C->CR2 |= I2C_CR2_START;
- while((I2C->SR1 & I2C_SR1_SB)==0 && tout());
-
- if(tout())
- {
- I2C->DR = (u8)(SLAVE_ADDRESS << 1) | 1;
- }
- while(!(I2C->SR1 & I2C_SR1_ADDR) && tout());
-
- if (u8_NumByteToRead > 2)
- {
- I2C->SR3;
- while(u8_NumByteToRead > 3 && tout())
- {
- while(!(I2C->SR1 & I2C_SR1_BTF) && tout());
- *ReadBuffer++ = I2C->DR;
- --u8_NumByteToRead;
- }
-
- while(!(I2C->SR1 & I2C_SR1_BTF) && tout());
- I2C->CR2 &=~I2C_CR2_ACK;
- disableInterrupts();
- *ReadBuffer++ = I2C->DR;
- I2C->CR2 |= I2C_CR2_STOP;
- *ReadBuffer++ = I2C->DR;
- enableInterrupts();
- while(!(I2C->SR1 & I2C_SR1_RXNE) && tout());
- *ReadBuffer++ = I2C->DR;
- }
- else
- {
- if(u8_NumByteToRead == 2)
- {
- I2C->CR2 |= I2C_CR2_POS;
- disableInterrupts();
- I2C->SR3;
- I2C->CR2 &=~I2C_CR2_ACK;
- enableInterrupts();
- while(!(I2C->SR1 & I2C_SR1_BTF) && tout());
- disableInterrupts();
- I2C->CR2 |= I2C_CR2_STOP;
- *ReadBuffer++ = I2C->DR;
- enableInterrupts();
- *ReadBuffer = I2C->DR;
- }
- else
- {
- I2C->CR2 &=~I2C_CR2_ACK;;
- disableInterrupts();
- I2C->SR3;
- I2C->CR2 |= I2C_CR2_STOP;
- enableInterrupts();
- while(!(I2C->SR1 & I2C_SR1_RXNE) && tout());
- *ReadBuffer = I2C->DR;
- }
- }
-
- while((I2C->CR2 & I2C_CR2_STOP) && tout());
- I2C->CR2 &=~I2C_CR2_POS;
- }
- void I2C_WriteRegister(u8 u8_regAddr, u8 u8_NumByteToWrite, u8 *ReadBuffer)
- {
- while((I2C->SR3 & 2) && tout())
- {
- I2C->CR2 |= 2;
- while((I2C->CR2 & 2) && tout());
- }
-
- I2C->CR2 |= 1;
- while(((I2C->SR1 & 1)==0) && tout());
- dead_time();
- if(tout())
- {
- I2C->DR = (u8)(SLAVE_ADDRESS << 1);
- }
- while(!(I2C->SR1 & 2) && tout());
- dead_time();
- I2C->SR3;
- while(!(I2C->SR1 & 0x80) && tout());
- if(tout())
- {
- I2C->DR = u8_regAddr;
- }
- if(u8_NumByteToWrite)
- {
- while(u8_NumByteToWrite--)
- {
- while(!(I2C->SR1 & 0x80) && tout());
- I2C->DR = *ReadBuffer++;
- }
- }
- while(((I2C->SR1 & 0x84) != 0x84) && tout());
- dead_time();
-
- I2C->CR2 |= 2;
- while((I2C->CR2 & 2) && tout());
- }
- void I2C_ReadBytes(const u8 Addr, const u8 NumByteToRead, u8 *ReadBuffer) {
- u8 adr = Addr << 1;
- u8 n = NumByteToRead;
- set_tout_ms(10);
-
- while(I2C->SR3 & I2C_SR3_BUSY && tout())
- {
- I2C->CR2 |= I2C_CR2_STOP;
- while(I2C->CR2 & I2C_CR2_STOP && tout());
- }
- I2C->CR2 |= I2C_CR2_ACK;
-
- I2C->CR2 |= I2C_CR2_START;
- while((I2C->SR1 & I2C_SR1_SB)==0 && tout());
-
- if(tout())
- {
- I2C->DR = (adr | 1);
- }
- while(!(I2C->SR1 & I2C_SR1_ADDR) && tout());
-
- if (n > 2)
- {
- I2C->SR3;
- while(n > 3 && tout())
- {
- while(!(I2C->SR1 & I2C_SR1_BTF) && tout());
- *ReadBuffer++ = I2C->DR;
- --n;
- }
-
- while(!(I2C->SR1 & I2C_SR1_BTF) && tout());
- I2C->CR2 &=~I2C_CR2_ACK;
- disableInterrupts();
- *ReadBuffer++ = I2C->DR;
- I2C->CR2 |= I2C_CR2_STOP;
- *ReadBuffer++ = I2C->DR;
- enableInterrupts();
- while(!(I2C->SR1 & I2C_SR1_RXNE) && tout());
- *ReadBuffer++ = I2C->DR;
- }
- else
- {
- if(n == 2)
- {
- I2C->CR2 |= I2C_CR2_POS;
- disableInterrupts();
- I2C->SR3;
- I2C->CR2 &=~I2C_CR2_ACK;
- enableInterrupts();
- while(!(I2C->SR1 & I2C_SR1_BTF) && tout());
- disableInterrupts();
- I2C->CR2 |= I2C_CR2_STOP;
- *ReadBuffer++ = I2C->DR;
- enableInterrupts();
- *ReadBuffer = I2C->DR;
- }
- else
- {
- I2C->CR2 &=~I2C_CR2_ACK;;
- disableInterrupts();
- I2C->SR3;
- I2C->CR2 |= I2C_CR2_STOP;
- enableInterrupts();
- while(!(I2C->SR1 & I2C_SR1_RXNE) && tout());
- *ReadBuffer = I2C->DR;
- }
- }
-
- while((I2C->CR2 & I2C_CR2_STOP) && tout());
- I2C->CR2 &= ~I2C_CR2_POS;
- }
- void I2C_WriteBytes(const u8 Addr, const u8 NumByteToWrite, u8 *DataBuffer) {
- u8 adr = Addr << 1;
- u8 n = NumByteToWrite;
- set_tout_ms(10);
- while((I2C->SR3 & 2) && tout())
- {
- I2C->CR2 |= 2;
- while((I2C->CR2 & 2) && tout());
- }
-
- I2C->CR2 |= 1;
- while(((I2C->SR1 & 1)==0) && tout());
- dead_time();
- if(tout())
- {
- I2C->DR = adr;
- }
- while(!(I2C->SR1 & 2) && tout());
- dead_time();
- I2C->SR3;
- if(n)
- {
- while(n--)
- {
- while(!(I2C->SR1 & 0x80) && tout());
- I2C->DR = *DataBuffer++;
- }
- }
- while(((I2C->SR1 & 0x84) != 0x84) && tout());
- dead_time();
-
- I2C->CR2 |= 2;
- while((I2C->CR2 & 2) && tout());
- }
|