Browse Source

Move I2C func to separate module.

Vladimir N. Shilov 3 years ago
parent
commit
fdf79adb42
6 changed files with 151 additions and 131 deletions
  1. 20 0
      Inc/i2c.h
  2. 1 4
      Inc/main.h
  3. 4 0
      MNC-IN12x5.cbp
  4. 1 0
      Makefile
  5. 124 0
      Src/i2c.c
  6. 1 127
      Src/main.c

+ 20 - 0
Inc/i2c.h

@@ -0,0 +1,20 @@
+#pragma once
+#ifndef I2C_H
+#define I2C_H
+
+/* I2C Status */
+#define I2C_RET_OK    (int8_t)0
+#define I2C_RET_NACK  (int8_t)-1
+#define I2C_RET_ERR   (int8_t)-2
+
+typedef enum {
+  I2C_Ret_OK  = 0,
+  I2C_Ret_NACK = -1,
+  I2C_Ret_ERR = -2
+} i2c_status_t;
+
+int8_t i2c_check_err(void);
+int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len);
+int8_t user_i2c_write(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len);
+
+#endif // I2C_H

+ 1 - 4
Inc/main.h

@@ -50,6 +50,7 @@ extern "C" {
 /* Private includes ----------------------------------------------------------*/
 /* USER CODE BEGIN Includes */
 #include "gpio.h"
+#include "i2c.h"
 #include "ds3231.h"
 #include "bme280.h"
 #include "rtos.h"
@@ -118,10 +119,6 @@ typedef union {
 
 /* Exported constants --------------------------------------------------------*/
 /* USER CODE BEGIN EC */
-#define I2C_RET_OK    (int8_t)0
-#define I2C_RET_NACK  (int8_t)-1
-#define I2C_RET_ERR   (int8_t)-2
-
 /* USER CODE END EC */
 
 /* Exported macro ------------------------------------------------------------*/

+ 4 - 0
MNC-IN12x5.cbp

@@ -109,6 +109,7 @@
 		<Unit filename="Inc/ds3231.h" />
 		<Unit filename="Inc/event-system.h" />
 		<Unit filename="Inc/gpio.h" />
+		<Unit filename="Inc/i2c.h" />
 		<Unit filename="Inc/list_event.h" />
 		<Unit filename="Inc/main.h" />
 		<Unit filename="Inc/rtos.h" />
@@ -125,6 +126,9 @@
 		<Unit filename="Src/event-system.c">
 			<Option compilerVar="CC" />
 		</Unit>
+		<Unit filename="Src/i2c.c">
+			<Option compilerVar="CC" />
+		</Unit>
 		<Unit filename="Src/main.c">
 			<Option compilerVar="CC" />
 		</Unit>

+ 1 - 0
Makefile

@@ -41,6 +41,7 @@ BUILD_DIR = build
 C_SOURCES =  \
 Src/main.c \
 Src/stm32g0xx_it.c \
+Src/i2c.c \
 Src/ds3231.c \
 Src/bme280.c \
 Src/rtos.c \

+ 124 - 0
Src/i2c.c

@@ -0,0 +1,124 @@
+#include "main.h"
+
+/**
+ * @brief Check I2C for errors.
+ * @retval I2C return code
+ */
+int8_t i2c_check_err(void) {
+  int8_t r = I2C_RET_OK;
+
+  if ((I2C1->ISR & I2C_ISR_NACKF) != 0) {
+  /* device not present */
+    r = I2C_RET_NACK;
+  } else if ((I2C1->ISR & (I2C_ISR_ARLO | I2C_ISR_BERR)) != 0) {
+  /* other error */
+    r = I2C_RET_ERR;
+  }
+
+  if (r != I2C_RET_OK) {
+  /* restart I2C and clear flags */
+    I2C1->CR1 &= ~I2C_CR1_PE;
+    while ((I2C1->CR1 & I2C_CR1_PE) != 0) {};
+    I2C1->CR1 |= I2C_CR1_PE;
+  }
+
+  return r;
+}
+
+/**
+ * @brief Read len bytes from I2C bus to data by reg_addr.
+ * @retval I2C return code
+ */
+int8_t user_i2c_read(const uint8_t id, const uint8_t reg_addr, uint8_t *data, const uint16_t len) {
+  int8_t r = I2C_RET_OK;
+
+  Flag.I2C_RX_End = 0;
+  Flag.I2C_RX_Err = 0;
+  Flag.I2C_TX_Err = 0;
+
+  /* wait for i2c */
+  while ( I2C1->ISR & I2C_ISR_BUSY ) { __NOP(); };
+
+  /* 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 );
+  /* gen START */
+  I2C1->CR2 |= ( I2C_CR2_START );
+
+  /* wait for byte request or any error */
+  while ((I2C1->ISR & (I2C_ISR_ARLO | I2C_ISR_BERR | I2C_ISR_NACKF | I2C_ISR_TXE)) == 0) { __NOP(); };
+
+  if ((I2C2->ISR & I2C_ISR_TXE) != 0) {
+  /* device ok, send reg addr */
+    I2C1->TXDR = reg_addr;
+  } else {
+    r = i2c_check_err();
+    if (r != I2C_RET_OK) {
+      Flag.I2C_TX_Err = 1;
+      return r;
+    }
+  }
+
+  /* wait for i2c or any error */
+  while (((I2C1->ISR & I2C_ISR_BUSY) != 0) && ((I2C1->ISR & (I2C_ISR_ARLO | I2C_ISR_BERR | I2C_ISR_NACKF)) == 0)) { __NOP(); };
+  r = i2c_check_err();
+  if (r != I2C_RET_OK) {
+    Flag.I2C_TX_Err = 1;
+    return r;
+  }
+
+  /* prepare dma channel for receiving data */
+  DMA1_Channel2->CMAR = (uint32_t)data;
+  DMA1_Channel2->CPAR = (uint32_t)&(I2C1->RXDR);
+  DMA1_Channel2->CNDTR = len;
+  DMA1_Channel2->CCR |= DMA_CCR_EN;
+
+  /* prepare i2c for receiving data */
+  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 */
+  I2C1->CR1 |= ( I2C_CR1_RXDMAEN );
+  I2C1->CR2 |= ( I2C_CR2_START );
+
+  /* wait for receiving data */
+  while ((Flag.I2C_RX_End == 0) && (Flag.I2C_RX_Err == 0)) { __NOP(); };
+
+  return r;
+}
+
+/**
+ * @brief Write len bytes to I2C bus from data by reg_addr.
+ * @retval I2C return code
+ */
+int8_t user_i2c_write(const uint8_t id, const uint8_t reg_addr, uint8_t *data, const uint16_t len) {
+  int8_t r = I2C_RET_OK;
+
+  Flag.I2C_TX_End = 0;
+  Flag.I2C_TX_Err = 0;
+
+  DMA1_Channel3->CMAR = (uint32_t)data;
+  DMA1_Channel3->CPAR = (uint32_t)&(I2C1->TXDR);
+  DMA1_Channel3->CNDTR = len;
+
+  while ( I2C1->ISR & I2C_ISR_BUSY ) {};
+
+  I2C1->CR2 &= ~( I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RD_WRN);
+  I2C1->CR2 |= ( id | (len + 1) << I2C_CR2_NBYTES_Pos );
+  I2C1->CR2 |= ( I2C_CR2_START );
+
+  while ((I2C1->ISR & (I2C_ISR_ARLO | I2C_ISR_BERR | I2C_ISR_NACKF | I2C_ISR_TXE)) == 0) { __NOP(); };
+  if ((I2C2->ISR & I2C_ISR_TXE) != 0) {
+    I2C1->TXDR = reg_addr;
+  } else {
+    r = i2c_check_err();
+    if (r != I2C_RET_OK) {
+      Flag.I2C_TX_Err = 1;
+      return r;
+    }
+  }
+
+  DMA1_Channel3->CCR |= DMA_CCR_EN;
+  I2C1->CR1 |= ( I2C_CR1_TXDMAEN );
+
+  return r;
+}

+ 1 - 127
Src/main.c

@@ -103,9 +103,6 @@ static void MX_TIM17_Init(void);
 static void MX_USART1_UART_Init(void);
 /* USER CODE BEGIN PFP */
 static void showDigits(uint8_t * dig);
-int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len);
-int8_t user_i2c_write(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len);
-int8_t i2c_check_err(void);
 static void sensor_Init(void);
 static void sensorStartMeasure(void);
 static void sensorGetData(void);
@@ -253,130 +250,7 @@ int main(void)
     __WFI();
   }
   /* USER CODE END 3 */
-}
-
-/**
- * @brief Check I2C fjr errors.
- * @retval I2C return code
- */
-int8_t i2c_check_err(void) {
-  int8_t r = I2C_RET_OK;
-
-  if ((I2C1->ISR & I2C_ISR_NACKF) != 0) {
-  /* device not present */
-    r = I2C_RET_NACK;
-  } else if ((I2C1->ISR & (I2C_ISR_ARLO | I2C_ISR_BERR)) != 0) {
-  /* other error */
-    r = I2C_RET_ERR;
-  }
-
-  if (r != I2C_RET_OK) {
-  /* restart I2C and clear flags */
-    I2C1->CR1 &= ~I2C_CR1_PE;
-    while ((I2C1->CR1 & I2C_CR1_PE) != 0) {};
-    I2C1->CR1 |= I2C_CR1_PE;
-  }
-
-  return r;
-}
-
-/**
- * @brief Read len bytes from I2C bus to data by reg_addr.
- * @retval I2C return code
- */
-int8_t user_i2c_read(const uint8_t id, const uint8_t reg_addr, uint8_t *data, const uint16_t len) {
-  int8_t r = I2C_RET_OK;
-
-  Flag.I2C_RX_End = 0;
-  Flag.I2C_RX_Err = 0;
-  Flag.I2C_TX_Err = 0;
-
-  /* wait for i2c */
-  while ( I2C1->ISR & I2C_ISR_BUSY ) { __NOP(); };
-
-  /* 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 );
-  /* gen START */
-  I2C1->CR2 |= ( I2C_CR2_START );
-
-  /* wait for byte request or any error */
-  while ((I2C1->ISR & (I2C_ISR_ARLO | I2C_ISR_BERR | I2C_ISR_NACKF | I2C_ISR_TXE)) == 0) { __NOP(); };
-
-  if ((I2C2->ISR & I2C_ISR_TXE) != 0) {
-  /* device ok, send reg addr */
-    I2C1->TXDR = reg_addr;
-  } else {
-    r = i2c_check_err();
-    if (r != I2C_RET_OK) {
-      Flag.I2C_TX_Err = 1;
-      return r;
-    }
-  }
-
-  /* wait for i2c or any error */
-  while (((I2C1->ISR & I2C_ISR_BUSY) != 0) && ((I2C1->ISR & (I2C_ISR_ARLO | I2C_ISR_BERR | I2C_ISR_NACKF)) == 0)) { __NOP(); };
-  r = i2c_check_err();
-  if (r != I2C_RET_OK) {
-    Flag.I2C_TX_Err = 1;
-    return r;
-  }
-
-  /* prepare dma channel for receiving data */
-  DMA1_Channel2->CMAR = (uint32_t)data;
-  DMA1_Channel2->CPAR = (uint32_t)&(I2C1->RXDR);
-  DMA1_Channel2->CNDTR = len;
-  DMA1_Channel2->CCR |= DMA_CCR_EN;
-
-  /* prepare i2c for receiving data */
-  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 */
-  I2C1->CR1 |= ( I2C_CR1_RXDMAEN );
-  I2C1->CR2 |= ( I2C_CR2_START );
-
-  /* wait for receiving data */
-  while ((Flag.I2C_RX_End == 0) && (Flag.I2C_RX_Err == 0)) { __NOP(); };
-
-  return r;
-}
-
-/**
- * @brief Write len bytes to I2C bus from data by reg_addr.
- * @retval I2C return code
- */
-int8_t user_i2c_write(const uint8_t id, const uint8_t reg_addr, uint8_t *data, const uint16_t len) {
-  int8_t r = I2C_RET_OK;
-
-  Flag.I2C_TX_End = 0;
-  Flag.I2C_TX_Err = 0;
-
-  DMA1_Channel3->CMAR = (uint32_t)data;
-  DMA1_Channel3->CPAR = (uint32_t)&(I2C1->TXDR);
-  DMA1_Channel3->CNDTR = len;
-
-  while ( I2C1->ISR & I2C_ISR_BUSY ) {};
-
-  I2C1->CR2 &= ~( I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RD_WRN);
-  I2C1->CR2 |= ( id | (len + 1) << I2C_CR2_NBYTES_Pos );
-  I2C1->CR2 |= ( I2C_CR2_START );
-
-  while ((I2C1->ISR & (I2C_ISR_ARLO | I2C_ISR_BERR | I2C_ISR_NACKF | I2C_ISR_TXE)) == 0) { __NOP(); };
-  if ((I2C2->ISR & I2C_ISR_TXE) != 0) {
-    I2C1->TXDR = reg_addr;
-  } else {
-    r = i2c_check_err();
-    if (r != I2C_RET_OK) {
-      Flag.I2C_TX_Err = 1;
-      return r;
-    }
-  }
-
-  DMA1_Channel3->CCR |= DMA_CCR_EN;
-  I2C1->CR1 |= ( I2C_CR1_TXDMAEN );
-
-  return r;
-}
+} /* End of mine() */
 
 /**
   * Sensor