Juraj Ďuďák 1 ماه پیش
والد
کامیت
f64f93b624
2فایلهای تغییر یافته به همراه185 افزوده شده و 0 حذف شده
  1. 24 0
      inc/HAL_impl.h
  2. 161 0
      src/HAL_impl.c

+ 24 - 0
inc/HAL_impl.h

@@ -0,0 +1,24 @@
+#ifndef _HAL_IMPLEMENTATION_H_
+#define _HAL_IMPLEMENTATION_H_
+
+#include "HAL_STM32_config.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+void initiliaze_SPI(void);
+int spi_master_read_register(uint8_t reg, uint8_t* rbuffer, uint32_t rlen);
+int spi_master_write_register(uint8_t reg, const uint8_t* wbuffer, uint32_t wlen);
+
+void initiliaze_I2C(void);
+int i2c_master_write_register(uint8_t address, uint8_t reg, uint32_t len, const uint8_t *data);
+int i2c_master_read_register(uint8_t address, uint8_t reg, uint32_t len, uint8_t *buff);
+
+#ifdef __cplusplus
+}
+#endif
+
+
+#endif

+ 161 - 0
src/HAL_impl.c

@@ -0,0 +1,161 @@
+#include "HAL_impl.h"
+
+// Funkcia na nastavenie CS pinu
+static inline void CS_Select(void) {
+    HAL_GPIO_WritePin(CHIP_SELECT_PORT, CHIP_SELECT_PIN, GPIO_PIN_RESET);
+}
+
+static inline void CS_Deselect(void) {
+    HAL_GPIO_WritePin(CHIP_SELECT_PORT, CHIP_SELECT_PIN, GPIO_PIN_SET);
+}
+
+
+/*---------------------------------------------------------------------*/
+/*                                SPI                                  */
+/*---------------------------------------------------------------------*/
+
+void initiliaze_SPI(void)
+{
+    CS_Deselect();
+    uint8_t dummy = 0x00;
+    HAL_SPI_Transmit(&SPI_INSTANCE, &dummy, 1, HAL_MAX_DELAY);
+}
+
+
+int spi_master_read_register(uint8_t reg, uint8_t* rbuffer, uint32_t rlen)
+{
+#ifdef HAL_SPI_MODULE_ENABLED
+
+	uint8_t dummy = 0x00;
+	uint8_t status = HAL_SPI_Transmit(&SPI_INSTANCE, &dummy, 1, HAL_MAX_DELAY);
+	if (status != HAL_OK) return status;
+
+	CS_Select();
+
+    // 3. Adresovanie registra
+    uint8_t cmd = (reg & 0x7F) | 0x80;
+    status = HAL_SPI_Transmit(&SPI_INSTANCE, &cmd, 1, HAL_MAX_DELAY);
+
+    // 4. Čítanie dát
+    for (uint32_t indi = 0; indi < rlen; indi++) {
+        uint8_t rx = 0x00;
+        status = HAL_SPI_TransmitReceive(&SPI_INSTANCE, &rx, &rbuffer[indi], 1, HAL_MAX_DELAY);
+        if (status != HAL_OK) {
+            CS_Deselect();
+            return status;
+        }
+    }
+
+    // 5. Chip select HIGH
+    CS_Deselect();
+
+#endif // HAL_SPI_MODULE_ENABLED
+
+    return 0;
+}
+
+int spi_master_write_register(uint8_t reg, const uint8_t* wbuffer, uint32_t wlen)
+{
+
+#ifdef HAL_SPI_MODULE_ENABLED
+
+	HAL_StatusTypeDef status;
+	uint8_t dummy = 0x00;
+	uint8_t addr  = (reg & 0x7F) | 0x00;
+
+	// 1. Dummy transfer (pôvodne SPI.transfer(0x00) mimo transakcie)
+	status = HAL_SPI_Transmit(&SPI_INSTANCE, &dummy, 1, HAL_MAX_DELAY);
+	if (status != HAL_OK) return -1;
+
+	// 2. Chip Select LOW
+	CS_Select();
+
+	// 3. Odoslanie adresy registra
+	status = HAL_SPI_Transmit(&SPI_INSTANCE, &addr, 1, HAL_MAX_DELAY);
+	if (status != HAL_OK) {
+		CS_Deselect();
+		return -2;
+	}
+
+	// 4. Odoslanie všetkých dát
+	if (wlen > 0) {
+		status = HAL_SPI_Transmit(&SPI_INSTANCE, (uint8_t*)wbuffer, wlen, HAL_MAX_DELAY);
+		if (status != HAL_OK) {
+			CS_Deselect();
+			return -3;
+		}
+	}
+
+	// 5. Chip Select HIGH
+	CS_Deselect();
+
+#endif // HAL_SPI_MODULE_ENABLED
+
+	return 0;
+}
+
+/*---------------------------------------------------------------------*/
+/*                                I2C                                  */
+/*---------------------------------------------------------------------*/
+
+void initiliaze_I2C(void)
+{
+#ifdef HAL_I2C_MODULE_ENABLED
+	I2C_Address = I2C_ADDRESS;
+#endif
+}
+
+
+
+int i2c_master_write_register(uint8_t address, uint8_t reg, uint32_t len, const uint8_t *data)
+{
+
+#ifdef HAL_I2C_MODULE_ENABLED
+
+	HAL_StatusTypeDef status = HAL_I2C_Mem_Write(
+        &I2C_INSTANCE,
+        (address << 1),       // STM32 expects 8-bit address
+        reg,                  // Register address
+        I2C_MEMADD_SIZE_8BIT, // Register size
+        (uint8_t *)data,
+        len,
+        HAL_MAX_DELAY
+    );
+
+    if (status != HAL_OK)
+    {
+
+        return -1;
+    }
+
+#endif // HAL_I2C_MODULE_ENABLED
+
+    return 0;
+}
+
+int i2c_master_read_register(uint8_t address, uint8_t reg, uint32_t len, uint8_t *buff)
+{
+
+#ifdef HAL_I2C_MODULE_ENABLED
+
+    HAL_StatusTypeDef status = HAL_I2C_Mem_Read(
+        &I2C_INSTANCE,
+        (address << 1),       // STM32 expects 8-bit address
+        reg,                  // Register address
+        I2C_MEMADD_SIZE_8BIT, // Register size
+        buff,
+        len,
+        HAL_MAX_DELAY
+    );
+
+    if (status != HAL_OK)
+    {
+        return -1;
+    }
+
+#endif // HAL_I2C_MODULE_ENABLED
+
+    return 0;
+}
+
+