AMTS/Mieke/SW/ODD/eeprom.c

103 lines
3.7 KiB
C
Raw Normal View History

2018-04-03 15:32:24 +00:00
/*
Demo program for the 2017/18 open door day at HTL Hollabrunn
Copyright (C) 2018 Andreas Mieke
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2018-03-18 22:43:59 +00:00
#include "eeprom.h"
extern volatile uint32_t SysTickCnt;
2018-04-03 15:18:28 +00:00
// EEPROM addresse
2018-03-18 22:43:59 +00:00
#define EEPROM_ADDR (uint8_t)0xA0 // 0b10100000
2018-04-03 15:18:28 +00:00
// ---- Vendor address part
// --- User address part
// - Keep free for R/W bit (set by I2C_Send7bitAddress())
2018-03-18 22:43:59 +00:00
uint32_t last_write_tick = 0;
void eeprom_init(void)
{
2018-04-03 15:18:28 +00:00
// Init I2C1
2018-03-18 22:43:59 +00:00
i2c1_init();
}
void eeprom_read(uint16_t address, uint8_t *data, uint16_t length)
{
uint16_t cur_pos;
2018-04-03 15:18:28 +00:00
// Send address of EEPROM and start address on EEPROM
2018-03-18 22:43:59 +00:00
I2C_GenerateSTART(I2C1, ENABLE);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1, EEPROM_ADDR, I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1, (address & 0xFF00) >> 8);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_SendData(I2C1, (address & 0x00FF));
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
2018-04-03 15:18:28 +00:00
// Switch to receive mode
2018-03-18 22:43:59 +00:00
I2C_GenerateSTART(I2C1, ENABLE);
I2C_AcknowledgeConfig(I2C1, ENABLE);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1, EEPROM_ADDR, I2C_Direction_Receiver);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
2018-04-03 15:18:28 +00:00
// Read all bytes and disable ACK on last byte
2018-03-18 22:43:59 +00:00
for(cur_pos = 0; cur_pos < length; cur_pos++) {
if(cur_pos == length - 1) {
I2C_AcknowledgeConfig(I2C1, DISABLE);
}
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
data[cur_pos] = I2C_ReceiveData(I2C1);
}
2018-04-03 15:18:28 +00:00
// Send stop condition
2018-03-18 22:43:59 +00:00
I2C_GenerateSTOP(I2C1, ENABLE);
}
void eeprom_write(uint16_t address, uint8_t *data, uint16_t length)
{
uint8_t cur_page = 0;
uint16_t cur_pos = 0;
address = address & 0xFFA0;
2018-04-03 15:18:28 +00:00
// If more than one page is needed, cycle over the pages
2018-03-18 22:43:59 +00:00
for(cur_page = 0; cur_page <= ((length-1)/64); cur_page++) {
2018-04-03 15:18:28 +00:00
// Wait 5 ms for the write cycle (see datasheet)
2018-03-18 22:43:59 +00:00
while((SysTickCnt - last_write_tick) <= 5);
2018-04-03 15:18:28 +00:00
// Send start condition
2018-03-18 22:43:59 +00:00
I2C_GenerateSTART(I2C1, ENABLE);
2018-04-03 15:18:28 +00:00
// Send EEPROM address and start address of data to send
2018-03-18 22:43:59 +00:00
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1, EEPROM_ADDR, I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1, (address & 0xFF00) >> 8);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_SendData(I2C1, (address & 0x00FF));
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
2018-04-03 15:18:28 +00:00
// Send max 64 bytes (1 page) of data
2018-03-18 22:43:59 +00:00
for(; (cur_pos < length) && (cur_pos%64 <= 63); cur_pos++) {
I2C_SendData(I2C1, data[cur_pos]);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
}
2018-04-03 15:18:28 +00:00
// Generate stop condition and calculate address of next page (if needed)
2018-03-18 22:43:59 +00:00
I2C_GenerateSTOP(I2C1, ENABLE);
address += 0x0040;
last_write_tick = SysTickCnt;
}
}