Browse Source

I2C EEPROM timeout fix

pull/1/head
Sergey 5 years ago
parent
commit
2809a86822
  1. 13
      Marlin/src/HAL/shared/eeprom_i2c_at24.cpp
  2. 7
      Marlin/src/module/mks_wifi/dwt.cpp

13
Marlin/src/HAL/shared/eeprom_i2c_at24.cpp

@ -37,6 +37,8 @@ void eeprom_hw_init(void){
PB6 SCL Alternate function open drain PB6 SCL Alternate function open drain
PB7 SDA Alternate function open drain PB7 SDA Alternate function open drain
*/ */
dwt_init();
RCC->APB2ENR |= RCC_APB2ENR_IOPBEN|RCC_APB2ENR_AFIOEN; RCC->APB2ENR |= RCC_APB2ENR_IOPBEN|RCC_APB2ENR_AFIOEN;
PORTB->CRL |= (GPIO_CRL_MODE6|GPIO_CRL_MODE7|GPIO_CRL_CNF6|GPIO_CRL_CNF7); PORTB->CRL |= (GPIO_CRL_MODE6|GPIO_CRL_MODE7|GPIO_CRL_CNF6|GPIO_CRL_CNF7);
@ -91,23 +93,26 @@ static uint8_t i2c_write(const uint8_t hw_adr, uint8_t *data, uint32_t len){
DEBUG("i2c write at %d val %0X",data[0],data[1]); DEBUG("i2c write at %d val %0X",data[0],data[1]);
dwt_settimeout(I2C_TIMEOUT); dwt_settimeout(I2C_TIMEOUT);
DEBUG("Wait busy");
while(I2C1->SR2 & I2C_SR2_BUSY) {CHECK_TIMEOUT;}; while(I2C1->SR2 & I2C_SR2_BUSY) {CHECK_TIMEOUT;};
I2C1->CR1 = I2C_CR1_PE | I2C_CR1_START; I2C1->CR1 = I2C_CR1_PE | I2C_CR1_START;
dwt_settimeout(I2C_TIMEOUT); dwt_settimeout(I2C_TIMEOUT);
DEBUG("Wait SB");
while(!(I2C1->SR1 & I2C_SR1_SB)) {CHECK_TIMEOUT;}; while(!(I2C1->SR1 & I2C_SR1_SB)) {CHECK_TIMEOUT;};
I2C1->DR = (hw_adr & 0xFE); I2C1->DR = (hw_adr & 0xFE);
if(!waitSRBitSet(I2C_SR1_ADDR)) return false; if(!waitSRBitSet(I2C_SR1_ADDR)) return false;
(void)I2C1->SR2; (void)I2C1->SR2;
while(len--){ while(len--){
if(!waitSRBitSet(I2C_SR1_TXE)) return false; if(!waitSRBitSet(I2C_SR1_TXE)) return false;
I2C1->DR = *data++; I2C1->DR = *data++;
} }
dwt_settimeout(I2C_TIMEOUT); dwt_settimeout(I2C_TIMEOUT);
DEBUG("Wait BTF");
while(!((I2C1->SR1 & I2C_SR1_TXE) && (I2C1->SR1 & I2C_SR1_BTF))) {CHECK_TIMEOUT;}; while(!((I2C1->SR1 & I2C_SR1_TXE) && (I2C1->SR1 & I2C_SR1_BTF))) {CHECK_TIMEOUT;};
I2C1->CR1 = I2C_CR1_PE | I2C_CR1_STOP; I2C1->CR1 = I2C_CR1_PE | I2C_CR1_STOP;
@ -117,12 +122,14 @@ static uint8_t i2c_write(const uint8_t hw_adr, uint8_t *data, uint32_t len){
static uint8_t i2c_read(const uint8_t hw_adr, uint16_t addr, uint8_t *data, uint32_t len){ static uint8_t i2c_read(const uint8_t hw_adr, uint16_t addr, uint8_t *data, uint32_t len){
dwt_settimeout(I2C_TIMEOUT); dwt_settimeout(I2C_TIMEOUT);
DEBUG("Wait busy");
while(I2C1->SR2 & I2C_SR2_BUSY) {CHECK_TIMEOUT;}; while(I2C1->SR2 & I2C_SR2_BUSY) {CHECK_TIMEOUT;};
//Запись адреса //Запись адреса
I2C1->CR1 = I2C_CR1_PE | I2C_CR1_START; I2C1->CR1 = I2C_CR1_PE | I2C_CR1_START;
dwt_settimeout(I2C_TIMEOUT); dwt_settimeout(I2C_TIMEOUT);
DEBUG("Wait SB");
while(!(I2C1->SR1 & I2C_SR1_SB)) {CHECK_TIMEOUT;}; //Условие старт while(!(I2C1->SR1 & I2C_SR1_SB)) {CHECK_TIMEOUT;}; //Условие старт
@ -134,12 +141,14 @@ static uint8_t i2c_read(const uint8_t hw_adr, uint16_t addr, uint8_t *data, uint
I2C1->DR = addr%256; //адрес в памяти отправлен I2C1->DR = addr%256; //адрес в памяти отправлен
dwt_settimeout(I2C_TIMEOUT); dwt_settimeout(I2C_TIMEOUT);
DEBUG("Wait BTF");
while(!((I2C1->SR1 & I2C_SR1_TXE) && (I2C1->SR1 & I2C_SR1_BTF))) {CHECK_TIMEOUT;}; while(!((I2C1->SR1 & I2C_SR1_TXE) && (I2C1->SR1 & I2C_SR1_BTF))) {CHECK_TIMEOUT;};
//Чтение //Чтение
I2C1->CR1 = I2C_CR1_PE | I2C_CR1_START | I2C_CR1_ACK; I2C1->CR1 = I2C_CR1_PE | I2C_CR1_START | I2C_CR1_ACK;
dwt_settimeout(I2C_TIMEOUT); dwt_settimeout(I2C_TIMEOUT);
DEBUG("Wait SB");
while(!(I2C1->SR1 & I2C_SR1_SB)) {CHECK_TIMEOUT;}; while(!(I2C1->SR1 & I2C_SR1_SB)) {CHECK_TIMEOUT;};
I2C1->DR = hw_adr|1; I2C1->DR = hw_adr|1;
@ -167,7 +176,7 @@ static bool waitSRBitSet(uint32_t Bit){
return false; return false;
} }
if(dwt_get_timeout() == 0){ if(dwt_get_timeout() == 0){
ERROR("Timeout"); ERROR("Timeout %0X",Bit);
return false; return false;
} }

7
Marlin/src/module/mks_wifi/dwt.cpp

@ -15,19 +15,22 @@ void dwt_settimeout(uint32_t time){
time = MAX_TIMEOUT; time = MAX_TIMEOUT;
} }
//DEBUG("Set timeout %d %d",time, time * (SYS_CLK / 1000));
timeout = time * (SYS_CLK / 1000); timeout = time * (SYS_CLK / 1000);
DWT->CYCCNT = 0; DWT->CYCCNT = 0;
} }
uint32_t dwt_get_timeout(void){ uint32_t dwt_get_timeout(void){
uint32_t current_timer = DWT->CYCCNT; volatile uint32_t current_timer = DWT->CYCCNT;
if(current_timer >= timeout) { if(current_timer >= timeout) {
DEBUG("TO %d %d",current_timer,timeout);
timeout = 0; timeout = 0;
return 0; return 0;
} }
return current_timer; return 1;
} }

Loading…
Cancel
Save