From e9e6d9df75b8f7c288fd26b26677e43b3eaf8287 Mon Sep 17 00:00:00 2001 From: Angoosh Date: Fri, 12 Nov 2021 12:03:44 +0100 Subject: [PATCH] hello --- AT24CSW.c | 42 ++++++++++++++++++++++++++++++++++++++++++ AT24CSW.h | 30 ++++++++++++++++++++++++++++++ MCP40D18.c | 21 +++++++++++++++++++++ MCP40D18.h | 23 +++++++++++++++++++++++ 4 files changed, 116 insertions(+) create mode 100644 AT24CSW.c create mode 100644 AT24CSW.h create mode 100644 MCP40D18.c create mode 100644 MCP40D18.h diff --git a/AT24CSW.c b/AT24CSW.c new file mode 100644 index 0000000..bea6146 --- /dev/null +++ b/AT24CSW.c @@ -0,0 +1,42 @@ +/* + * AT24CSW.c + * + * Created on: Nov 12, 2021 + * Author: angoosh + */ + + +#include "AT24CSW.h" +#include "main.h" + +extern I2C_HandleTypeDef AT24CSW_I2C_HANDLE; + +int AT24CSW_Read_Memory(uint8_t address, uint8_t size, uint8_t *data); +int AT24CSW_Page_Write(uint8_t address, uint8_t *bytes); +int AT24CSW_Byte_Write(uint8_t address, uint8_t byte); + +int AT24CSW_Byte_Write(uint8_t address, uint8_t byte){ + uint8_t data[2] = {address,byte}; + HAL_I2C_Master_Transmit(&AT24CSW_I2C_HANDLE, (AT24CSW_ADDRESS << 1), data, 3, 0xFFFF); + return 0; +} + +int AT24CSW_Page_Write(uint8_t address, uint8_t *bytes){ + uint8_t data[9]; + data[0] = address; + for(int i = 0; i < 8; i++){ + data[i+1] = bytes[i]; + } + HAL_I2C_Master_Transmit(&AT24CSW_I2C_HANDLE, (AT24CSW_ADDRESS << 1), data, 3, 0xFFFF); + return 0; +} + +int AT24CSW_Read_Memory(uint8_t address, uint8_t size, uint8_t *data){ + uint8_t memContent[256]; + HAL_I2C_Master_Receive(&AT24CSW_I2C_HANDLE, (AT24CSW_ADDRESS << 1), memContent, 0xFF, 0xFFFF); + + for(int i = 0; i < size; i++){ + data[i] = memContent[address+i]; + } + return 0; +} diff --git a/AT24CSW.h b/AT24CSW.h new file mode 100644 index 0000000..d213436 --- /dev/null +++ b/AT24CSW.h @@ -0,0 +1,30 @@ +/* + * AT24CSW.h + * + * Created on: Nov 12, 2021 + * Author: angoosh + */ + + +/* + * Page wite writes 8 bytes to whole page or memory row => bytes A7-A3 are the same + * Word address 11111111 is reserved + */ +#ifndef INC_AT24CSW_H_ +#define INC_AT24CSW_H_ + +#include "stm32l0xx_hal.h" + +#define PRODUCT_LAST_NUMBER 0 + +#define AT24CSW_ADDRESS 0b1010000 + PRODUCT_LAST_NUMBER +#define AT24CSW_SEC_ADDRESS 0b1011000 + PRODUCT_LAST_NUMBER + + + + +int AT24CSW_Read_Memory(uint8_t address, uint8_t size, uint8_t *data); +int AT24CSW_Page_Write(uint8_t address, uint8_t *bytes); +int AT24CSW_Byte_Write(uint8_t address, uint8_t byte); + +#endif /* INC_AT24CSW_H_ */ diff --git a/MCP40D18.c b/MCP40D18.c new file mode 100644 index 0000000..89c4453 --- /dev/null +++ b/MCP40D18.c @@ -0,0 +1,21 @@ +/* + * MCP40D18.c + * + * Created on: Sep 16, 2021 + * Author: angoosh + */ +#include "MCP40D18.h" +#include "main.h" + +void SetPotPosition(uint8_t pos); + +extern I2C_HandleTypeDef MCP40D18_I2C_HANDLE; + + +void SetPotPosition(uint8_t pos){ + if(pos < 128){ + uint8_t data[2] = {0,pos}; + HAL_I2C_Master_Transmit(&MCP40D18_I2C_HANDLE, (MCP_I2C_ADDRESS << 1), data, 2, 0xFFFF); + } +} + diff --git a/MCP40D18.h b/MCP40D18.h new file mode 100644 index 0000000..66c5d5e --- /dev/null +++ b/MCP40D18.h @@ -0,0 +1,23 @@ +/* + * MCP40D18.h + * + * Created on: Sep 16, 2021 + * Author: angoosh + */ + +#ifndef INC_MCP40D18_H_ +#define INC_MCP40D18_H_ + +#include "stm32l0xx_hal.h" + +#define IS_ALT_ADDRESS +#ifdef IS_ALT_ADDRESS +#define MCP_I2C_ADDRESS 0x3E //0111110 +#else +#define MCP_I2C_ADDRESS 0x2E //0101110 +#endif + + +void SetPotPosition(uint8_t pos); + +#endif /* INC_MCP40D18_H_ */