Procházet zdrojové kódy

I2C work. Code clean.

Vladimir N. Shilov před 2 roky
rodič
revize
08d9c4ffc1
4 změnil soubory, kde provedl 21 přidání a 108 odebrání
  1. 5 0
      ReadMe.txt
  2. 2 3
      src/board.c
  3. 7 70
      src/i2c.c
  4. 7 35
      src/main.c

+ 5 - 0
ReadMe.txt

@@ -73,3 +73,8 @@ Display Ctrl	HT1632C (устаревшая, не лучший выбор)
 Внутренняя подтяжка OD выходов для I2C всё таки нужна.
 
 Прерывания от часов идут с частотой 1024 Гц, кажется я такое уже видел...
+---
+2022.09.20
+
+Запаял другую DS3231 - и всё заработало...
+Можно двигаться дальше.

+ 2 - 3
src/board.c

@@ -96,8 +96,7 @@ static void GPIO_Init(void)
   // Pull-Up
   GPIOA->PUPDR = (GPIOA->PUPDR & ~(GPIO_PUPDR_PUPDR8 | GPIO_PUPDR_PUPDR15)) \
                 | (GPIO_PUPDR_PUPDR8_1 | GPIO_PUPDR_PUPDR15_0);
-  GPIOB->PUPDR = (GPIOA->PUPDR & ~(GPIO_PUPDR_PUPDR6 | GPIO_PUPDR_PUPDR7)) \
-                | (GPIO_PUPDR_PUPDR6_0 | GPIO_PUPDR_PUPDR7_0);
+  GPIOB->PUPDR = (GPIOA->PUPDR & ~(GPIO_PUPDR_PUPDR6 | GPIO_PUPDR_PUPDR7)) | (GPIO_PUPDR_PUPDR6_0 | GPIO_PUPDR_PUPDR7_0);
   // High Speed
   GPIOA->OSPEEDR = (GPIO_OSPEEDR_OSPEEDR8|GPIO_OSPEEDR_OSPEEDR15);
   GPIOB->OSPEEDR = (GPIO_OSPEEDR_OSPEEDR3|GPIO_OSPEEDR_OSPEEDR5 \
@@ -118,7 +117,7 @@ static void GPIO_Init(void)
    PA5 - SW3 / AlarmSet
    PA6 - SW5 / AlarmOff
    PA7 - SW4 / Bright
-   PA12 - RTC IRQ / Exti
+   PA12 - RTC IRQ / Exti, Float
   */
   GPIOA->PUPDR |= (GPIO_PUPDR_PUPDR0_0|GPIO_PUPDR_PUPDR1_0|GPIO_PUPDR_PUPDR2_0 \
                 |GPIO_PUPDR_PUPDR3_0|GPIO_PUPDR_PUPDR4_0|GPIO_PUPDR_PUPDR5_0 \

+ 7 - 70
src/i2c.c

@@ -17,25 +17,6 @@ static i2c_status_t st_Read = I2C_Ret_OK;
 /* Private function prototypes */
 static i2c_status_t i2c_check_err(void);
 
-static inline void i2c_start(void) {
-  // Send 'Start' condition, and wait for acknowledge.
-  I2C1->CR2 |=  (I2C_CR2_START);
-  while ((I2C1->CR2 & I2C_CR2_START)) {}
-}
-
-static inline void i2c_stop(void) {
-  // Send 'Stop' condition, and wait for acknowledge.
-  I2C1->CR2 |=  (I2C_CR2_STOP);
-  while ((I2C1->CR2 & I2C_CR2_STOP)) {}
-  // Reset the ICR ('Interrupt Clear Register') event flag.
-  I2C1->ICR |=  (I2C_ICR_STOPCF);
-  while ((I2C1->ICR & I2C_ICR_STOPCF)) {}
-}
-
-static void i2c_write_byte(uint8_t dat);
-static uint8_t i2c_read_byte(void);
-static uint8_t i2c_read_register(uint8_t reg_addr);
-
 /**
  * @brief Check I2C bus for errors.
  * @retval I2C return code
@@ -81,7 +62,8 @@ i2c_status_t user_i2c_read(const uint8_t id, const uint8_t reg_addr, uint8_t *da
   /* prepare i2c for sending reg addr */
   I2C1->CR2 &= ~(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RD_WRN);
   I2C1->CR2 |= (id | (1 << I2C_CR2_NBYTES_Pos));
-  i2c_start();
+  I2C1->CR2 |=  (I2C_CR2_START);
+  while ((I2C1->CR2 & I2C_CR2_START)) {}
 
   /* wait for byte request or any error */
   while ((I2C1->ISR & (I2C_ISR_TIMEOUT | I2C_ISR_ARLO | I2C_ISR_BERR | I2C_ISR_NACKF | I2C_ISR_TXE)) == 0) {
@@ -111,7 +93,8 @@ i2c_status_t user_i2c_read(const uint8_t id, const uint8_t reg_addr, uint8_t *da
   I2C1->CR2 &= ~(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RD_WRN);
   I2C1->CR2 |= (id | (len << I2C_CR2_NBYTES_Pos) | I2C_CR2_RD_WRN);
   /* launch receiving */
-  i2c_start();
+  I2C1->CR2 |=  (I2C_CR2_START);
+  while ((I2C1->CR2 & I2C_CR2_START)) {}
 
   /* receiving data */
   while (len) {
@@ -146,12 +129,10 @@ i2c_status_t user_i2c_write(const uint8_t id, const uint8_t reg_addr, uint8_t *d
 
   I2C1->CR2 &= ~(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RD_WRN);
   I2C1->CR2 |= (id | ((len + 1) << I2C_CR2_NBYTES_Pos ));
-  i2c_start();
-
-  while ((I2C1->ISR & (I2C_ISR_ARLO | I2C_ISR_BERR | I2C_ISR_NACKF | I2C_ISR_TXE)) == 0) {
-    __NOP();
-  }
+  I2C1->CR2 |=  (I2C_CR2_START);
+  while ((I2C1->CR2 & I2C_CR2_START)) {}
 
+  while ((I2C1->ISR & (I2C_ISR_ARLO | I2C_ISR_BERR | I2C_ISR_NACKF | I2C_ISR_TXE)) == 0) { __NOP(); }
   if ((I2C1->ISR & I2C_ISR_TXE) != 0) {
     I2C1->TXDR = reg_addr;
   } else {
@@ -178,47 +159,3 @@ i2c_status_t user_i2c_write(const uint8_t id, const uint8_t reg_addr, uint8_t *d
   Flag.I2C_TX_End = 1;
   return r;
 }
-
-static void i2c_write_byte(uint8_t dat) {
-  I2C1->TXDR = (I2C1->TXDR & 0xFFFFFF00) | dat;
-  // Wait for one of these ISR bits:
-  // 'TXIS' ("ready for next byte")
-  // 'TC'   ("transfer complete")
-  while (!(I2C1->ISR & (I2C_ISR_TXIS | I2C_ISR_TC))) {}
-  // (Also of interest: 'TXE' ("TXDR register is empty") and
-  //  'TCR' ("transfer complete, and 'RELOAD' is set."))
-}
-
-static uint8_t i2c_read_byte(void) {
-  // Wait for a byte of data to be available, then read it.
-  while (!(I2C1->ISR & I2C_ISR_RXNE)) {}
-  return (I2C1->RXDR & 0xFF);
-}
-
-static uint8_t i2c_read_register(uint8_t reg_addr) {
-  // Set '1 byte to send.'
-  I2C1->CR2 &= ~(I2C_CR2_NBYTES);
-  I2C1->CR2 |=  (0x01 << I2C_CR2_NBYTES_Pos);
-  // Start the I2C write transmission.
-  i2c_start();
-  // Send the register address.
-  i2c_write_byte(reg_addr);
-  // Stop the I2C write transmission.
-  i2c_stop();
-  // Set '1 byte to receive.'
-  I2C1->CR2 &= ~(I2C_CR2_NBYTES);
-  I2C1->CR2 |=  (0x01 << I2C_CR2_NBYTES_Pos);
-  // Set 'read' I2C direction.
-  I2C1->CR2 |=  (I2C_CR2_RD_WRN);
-  // Start the I2C read transmission.
-  i2c_start();
-  // Read the transmitted data.
-  uint8_t read_result = i2c_read_byte();
-  // Stop the I2C read transmission.
-  i2c_stop();
-  // Set 'write' I2C direction again.
-  I2C1->CR2 &= ~(I2C_CR2_RD_WRN);
-
-  // Return the read value.
-  return read_result;
-}

+ 7 - 35
src/main.c

@@ -44,46 +44,16 @@ int main(void)
 
   /* Real-Time clock */
   i2c_status = RTC_Init();
-  tdelay_ms(500);
-  i2c_status = RTC_Init();
-  tdelay_ms(500);
-  i2c_status = RTC_Init();
   if (i2c_status != I2C_Ret_OK) {
     bip_Bip();
-    if (i2c_status != I2C_Ret_NACK) {
-      display_String("RTC Init Error: NACK\0", 2);
-    } else if (i2c_status != I2C_Ret_Bsy) {
-      display_String("RTC Init Error: Busy\0", 2);
-    } else if (i2c_status != I2C_Ret_Tout) {
-      display_String("RTC Init Error: Time out\0", 2);
-    } else if (i2c_status != I2C_Ret_Err) {
-      display_String("RTC Init Error: other error\0", 2);
-    }
+    display_String("RTC_Init Error\0", 1);
   }
   i2c_status = RTC_ReadTime(&Clock);
   if (i2c_status != I2C_Ret_OK) {
     bip_Bip();
-    if (i2c_status != I2C_Ret_NACK) {
-      display_String("RTC_ReadTime Error: NACK\0", 2);
-    } else if (i2c_status != I2C_Ret_Bsy) {
-      display_String("RTC_ReadTime Error: Busy\0", 2);
-    } else if (i2c_status != I2C_Ret_Tout) {
-      display_String("RTC_ReadTime Error: Time out\0", 2);
-    } else if (i2c_status != I2C_Ret_Err) {
-      display_String("RTC_ReadTime Error: other error\0", 2);
-    }
+    display_String("RTC_ReadTime Error\0", 1);
   }
 
-/*
-  tdelay_ms(2000);
-  display_String("For Time setting press 'SET' button long...\0", 2);
-  bip_Bip();
-  tdelay_ms(675);
-  bip_Bip();
-  tdelay_ms(675);
-  bip_Bip();
-  tdelay_ms(1000);
-*/
   Show_MMSS();
 
   /* Infinite loop */
@@ -92,9 +62,11 @@ int main(void)
     if (Flag.RTC_IRQ != 0) {
       Flag.RTC_IRQ = 0;
       i2c_status = RTC_ReadTime(&Clock);
-      display_Char((uint8_t)i2c_status+'0', 0);
-      display_WriteBuffer();
-      //Show_MMSS();
+      if (i2c_status == I2C_Ret_OK) {
+        Show_MMSS();
+      } else {
+        display_String("I2C Err\0", 1);
+      }
     }
 
     RTOS_DispatchTask();