Created
February 24, 2016 08:39
-
-
Save dekuNukem/9aa52b24698475c880e7 to your computer and use it in GitHub Desktop.
snippet of writing to EEPROM
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
void write_eeprom(uint16_t addr, uint8_t data) | |
{ | |
// first read the data on the current address, if it's the same we can save some time | |
HAL_GPIO_WritePin(CPU_CTRL_PORT, CPU_MREQ_PIN, LOW); | |
HAL_GPIO_WritePin(CPU_CTRL_PORT, CPU_RD_PIN, LOW); | |
HAL_GPIO_WritePin(CPU_CTRL_PORT, CPU_WR_PIN, HIGH); | |
uint8_t exsiting_byte = CPU_DATA_PORT->IDR & 0xff; | |
if(exsiting_byte == data) | |
return; | |
// if not the same, set up write by disabling read | |
HAL_GPIO_WritePin(CPU_CTRL_PORT, CPU_RD_PIN, HIGH); | |
// put address on bus first since it's latched on falling edge of 'WR | |
CPU_ADDR_PORT->ODR = addr; | |
HAL_GPIO_WritePin(CPU_CTRL_PORT, CPU_WR_PIN, LOW); | |
data_output(); | |
CPU_DATA_PORT->ODR = data; | |
// data latched on rising edge of 'WR | |
HAL_GPIO_WritePin(CPU_CTRL_PORT, CPU_WR_PIN, HIGH); | |
// switch back to input and keep polling to see if write is complete | |
data_input(); | |
HAL_GPIO_WritePin(CPU_CTRL_PORT, CPU_RD_PIN, LOW); | |
while(1) | |
{ | |
exsiting_byte = CPU_DATA_PORT->IDR; | |
if(exsiting_byte == data) | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment