Posiada ktoś kod do obsługi eeprom po i2c napisanego na rejestrach?
Posiadam kostki eeprom 24LC128, 24C02, 24C08A oczywiście przykładowy kod możne być na inne eeprom

Kod: Zaznacz cały
void I2C_Init(void)
{
// I2C GPIO CONFIG
GPIOB->CRL |= GPIO_CRL_CNF6 | GPIO_CRL_CNF7 | GPIO_CRL_MODE6 | GPIO_CRL_MODE7;
// Software reset I2C
I2C1->CR1 |= I2C_CR1_SWRST;
I2C1->CR1 &= ~I2C_CR1_SWRST;
I2C1->CR1 &= ~I2C_CR1_PE;
I2C1->CR2 = I2C_CR2_FREQ_3;
I2C1->TRISE = 9;
I2C1->CCR = 0x24;
// Wlaczenie I2C
I2C1->CR1 |= I2C_CR1_PE | I2C_CR1_ACK;
I2C1->OAR1 = (1 << 14);
}
void I2C_Start(void)
{
I2C1->CR1 |= I2C_CR1_START;
while( !(I2C1->SR1 & I2C_SR1_SB) );
uint32_t tmp = I2C1->SR1;
}
void I2C_Stop(void)
{
while( !(I2C1->SR1 & I2C_SR1_BTF) );
I2C1->CR1 |= I2C_CR1_STOP;
}
void I2C_SendAddr(uint8_t address)
{
I2C1->DR = address;
while( !(I2C1->SR1 & I2C_SR1_ADDR) );
uint32_t dummy = I2C1->SR1;
dummy = I2C1->SR2;
}
void I2C_SendByte(uint8_t byte)
{
while( !(I2C1->SR1 & I2C_SR1_TXE) );
I2C1->DR = byte;
}
Kod: Zaznacz cały
void zapisi2c(uint8_t adresx, uint16_t subAddr, uint16_t dlugosc, uint8_t *buf) {
while (dlugosc--) {
I2C_Start();
I2C_SendAddr( adresx );
I2C_SendByte(subAddr);
I2C_SendByte(*buf++);
I2C_Stop();
delay_ms2(5);
subAddr++;
}
}
Kod: Zaznacz cały
zapisi2c( adres, 200, sizeof(tekst), tekst );
Kod: Zaznacz cały
void odczyti2c(uint8_t adresx, uint16_t subAddr, uint16_t len, uint8_t *buf) {
while (len--) {
I2C_Start();
I2C_SendAddr(adresx );
I2C_SendByte(subAddr);
I2C_Start();
I2C_SendAddr(adresx + 1);
*buf++ = I2C_ReadByte( 0 );
I2C_Stop();
subAddr++;
}
}
Kod: Zaznacz cały
I2C_ReadByte(uint8_t byte)
{
while( !(I2C1->SR1 & I2C_SR1_RXNE) );
byte = I2C1->DR ;
return byte;
}
Kod: Zaznacz cały
#include "stm32f1xx.h"
#include "LCD/hd44780.h"
void I2C_Start(void);
void I2C_Stop(void);
I2C_ReadByte(uint8_t byte);
void I2C_SendAddr(uint8_t address);
void I2C_SendByte(uint8_t byte);
void zapisi2c(uint8_t adresx, uint16_t subAddr, uint16_t dlugosc, uint8_t *buf);
void odczyti2c(uint8_t adresx, uint16_t subAddr, uint16_t len, uint8_t *buf);
void delay_ms2(int ms);
uint8_t tekst[] = "test2";
uint8_t bo[ 6 ];
#define adres 0xAC
int main(void)
{
RCC->APB2ENR = RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN | RCC_APB2ENR_IOPCEN ;
RCC->APB1ENR = RCC_APB1ENR_I2C1EN;
lcd_init();
lcd_cls();
lcd_locate(0,0);
lcd_str( "Uruchamianie ");
// I2C GPIO CONFIG
GPIOB->CRL |= GPIO_CRL_CNF6 | GPIO_CRL_CNF7 | GPIO_CRL_MODE6 | GPIO_CRL_MODE7;
// Software reset I2C
I2C1->CR1 |= I2C_CR1_SWRST;
I2C1->CR1 &= ~I2C_CR1_SWRST;
I2C1->CR1 &= ~I2C_CR1_PE;
I2C1->CR2 = I2C_CR2_FREQ_3;
I2C1->TRISE = 9;
I2C1->CCR = 0x24;
// Wlaczenie I2C
I2C1->CR1 |= I2C_CR1_PE | I2C_CR1_ACK;
I2C1->OAR1 = (1 << 14);
// zapisi2c( adres, 200, sizeof(tekst), tekst );
odczyti2c( adres, 200, sizeof(tekst), bo );
lcd_locate(2, 0);
lcd_str( (char*)bo );
while(1){
}
}
void zapisi2c(uint8_t adresx, uint16_t subAddr, uint16_t dlugosc, uint8_t *buf) {
while (dlugosc--) {
I2C_Start();
I2C_SendAddr( adresx );
I2C_SendByte(subAddr);
I2C_SendByte(*buf++);
I2C_Stop();
delay_ms2(5);
subAddr++;
}
}
// odczyt danych z pamięci EEPROM
void odczyti2c(uint8_t adresx, uint16_t subAddr, uint16_t len, uint8_t *buf) {
while (len--) {
I2C_Start();
I2C_SendAddr(adresx );
I2C_SendByte(subAddr);
I2C_Start();
I2C_SendAddr(adresx + 1);
*buf++ = I2C_ReadByte( 0 );
I2C_Stop();
subAddr++;
}
}
void I2C_Start(void)
{
I2C1->CR1 |= I2C_CR1_START;
while( !(I2C1->SR1 & I2C_SR1_SB) );
uint32_t tmp = I2C1->SR1;
}
void I2C_Stop(void)
{
while( !(I2C1->SR1 & I2C_SR1_BTF) );
I2C1->CR1 |= I2C_CR1_STOP;
}
void I2C_SendAddr(uint8_t address)
{
I2C1->DR = address;
while( !(I2C1->SR1 & I2C_SR1_ADDR) );
uint32_t dummy = I2C1->SR1;
dummy = I2C1->SR2;
}
void I2C_SendByte(uint8_t byte)
{
while( !(I2C1->SR1 & I2C_SR1_TXE) );
I2C1->DR = byte;
}
I2C_ReadByte(uint8_t byte)
{
while( !(I2C1->SR1 & I2C_SR1_RXNE) );
byte = I2C1->DR ;
return byte;
}
void delay_ms2(int ms) {
int c = 0;
SysTick->LOAD = 8000000 / 1000 - 1;
SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
SysTick->CTRL |= SysTick_CTRL_CLKSOURCE_Msk;
while (c < ms) {
while (!(SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk))
;
c++;
}
SysTick->CTRL &= ~ SysTick_CTRL_ENABLE_Msk;
}