-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e9a9da9
commit cb500a3
Showing
14 changed files
with
430 additions
and
234 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/** | ||
* @file m24512.c | ||
* @brief Реализация функций для работы с EEPROM M24512 | ||
*/ | ||
|
||
#include "m24512.h" | ||
#include "i2c.h" | ||
#include "delay.h" | ||
|
||
/** @brief Время ожидания завершения записи в мс */ | ||
#define M24512_WRITE_TIMEOUT 5 | ||
|
||
/** | ||
* @brief Проверка занятости устройства | ||
* | ||
* @return Результат проверки | ||
* @retval 0 Устройство готово к работе | ||
* @retval 1 Устройство занято | ||
*/ | ||
static uint8_t M24512_IsBusy(void) | ||
{ | ||
I2C_Start(); | ||
I2C_WriteAddress(M24512_BASE_ADDR, w); | ||
I2C_Stop(); | ||
return 0; | ||
} | ||
|
||
/** | ||
* @brief Ожидание готовности устройства | ||
* | ||
* @return Результат ожидания | ||
* @retval 0 Устройство готово к работе | ||
* @retval 1 Таймаут ожидания готовности | ||
*/ | ||
static uint8_t M24512_WaitReady(void) | ||
{ | ||
uint8_t timeout = 100; /* Максимальное время ожидания ~500 мс */ | ||
|
||
while (M24512_IsBusy() && timeout) | ||
{ | ||
delay(5); | ||
timeout--; | ||
} | ||
|
||
return (timeout == 0); | ||
} | ||
|
||
uint8_t M24512_Init(void) | ||
{ | ||
I2C_Start(); | ||
I2C_WriteAddress_for_EEPROM(M24512_BASE_ADDR, w); | ||
return 0; | ||
} | ||
|
||
uint8_t M24512_WriteByte(uint16_t addr, uint8_t data) | ||
{ | ||
if (addr > M24512_MAX_ADDR) | ||
{ | ||
return 1; | ||
} | ||
|
||
if (M24512_WaitReady()) | ||
{ | ||
return 1; | ||
} | ||
|
||
I2C_Start(); | ||
I2C_WriteAddress_for_EEPROM(M24512_BASE_ADDR, w); | ||
I2C_WriteData((uint8_t)(addr >> 8)); | ||
I2C_WriteData((uint8_t)(addr & 0xFF)); | ||
I2C_WriteData(data); | ||
I2C_Stop(); | ||
|
||
delay(M24512_WRITE_TIMEOUT); | ||
|
||
return 0; | ||
} | ||
|
||
uint8_t M24512_ReadByte(uint16_t addr, uint8_t *data) | ||
{ | ||
if (addr > M24512_MAX_ADDR || data == 0) | ||
{ | ||
return 1; | ||
} | ||
|
||
if (M24512_WaitReady()) | ||
{ | ||
return 1; | ||
} | ||
|
||
I2C_Start(); | ||
I2C_WriteAddress(M24512_BASE_ADDR); | ||
I2C_WriteData((uint8_t)(addr >> 8)); | ||
I2C_WriteData((uint8_t)(addr & 0xFF)); | ||
|
||
I2C_Start(); | ||
I2C_WriteAddress(M24512_BASE_ADDR | 0x01); | ||
*data = I2C_ReadData_NACK(); | ||
I2C_Stop(); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/** | ||
* @file m24512.h | ||
* @brief Библиотека-драйвер для работы с EEPROM M24512 | ||
* | ||
* Этот файл содержит прототипы функций и необходимые определения | ||
* для работы с микросхемой энергонезависимой памяти M24512 по интерфейсу I2C. | ||
* | ||
* Основные характеристики M24512: | ||
* - Объем памяти: 512 Кбит (64 КБайт) | ||
* - Организация: 65536 x 8 бит | ||
* - Интерфейс: I2C (до 400 кГц) | ||
* - Напряжение питания: 2.5В - 5.5В | ||
* - Время записи страницы: 5 мс макс. | ||
*/ | ||
|
||
#ifndef M24512_H | ||
#define M24512_H | ||
|
||
#include <stdint.h> | ||
|
||
/** @defgroup M24512_Constants Константы для работы с M24512 | ||
* @{ | ||
*/ | ||
|
||
/** @brief Базовый адрес устройства M24512 на шине I2C */ | ||
#define M24512_BASE_ADDR 0xA0 | ||
|
||
/** @brief Максимальный адрес памяти */ | ||
#define M24512_MAX_ADDR 0xFFFF | ||
|
||
/** @brief Размер страницы памяти в байтах */ | ||
#define M24512_PAGE_SIZE 128 | ||
|
||
/** @} */ | ||
|
||
/** @defgroup M24512_Functions Функции для работы с M24512 | ||
* @{ | ||
*/ | ||
|
||
/** | ||
* @brief Инициализация M24512 | ||
* | ||
* Выполняет проверку наличия и доступности микросхемы памяти на шине I2C. | ||
* Необходимо вызвать перед началом работы с микросхемой. | ||
* | ||
* @return Результат инициализации | ||
* @retval 0 Инициализация выполнена успешно, устройство отвечает | ||
* @retval 1 Ошибка инициализации, устройство не отвечает | ||
*/ | ||
uint8_t M24512_Init(void); | ||
|
||
/** | ||
* @brief Запись байта данных по указанному адресу | ||
* | ||
* Записывает один байт данных по указанному адресу памяти. | ||
* После записи автоматически выполняется ожидание завершения | ||
* внутреннего цикла записи микросхемы. | ||
* | ||
* @param[in] addr Адрес для записи (0x0000 - 0xFFFF) | ||
* @param[in] data Байт данных для записи | ||
* | ||
* @return Результат операции записи | ||
* @retval 0 Запись выполнена успешно | ||
* @retval 1 Ошибка записи или некорректный адрес | ||
*/ | ||
uint8_t M24512_WriteByte(uint16_t addr, uint8_t data); | ||
|
||
/** | ||
* @brief Чтение байта данных с указанного адреса | ||
* | ||
* Читает один байт данных с указанного адреса памяти. | ||
* | ||
* @param[in] addr Адрес для чтения (0x0000 - 0xFFFF) | ||
* @param[out] data Указатель на переменную для сохранения прочитанного байта | ||
* | ||
* @return Результат операции чтения | ||
* @retval 0 Чтение выполнено успешно | ||
* @retval 1 Ошибка чтения, некорректный адрес или NULL указатель | ||
*/ | ||
uint8_t M24512_ReadByte(uint16_t addr, uint8_t *data); | ||
|
||
/** @} */ | ||
|
||
#endif /* M24512_H */ |
Oops, something went wrong.