Fix NACK handling for flash write

This commit is contained in:
Olaf Rempel 2020-02-01 23:02:19 +01:00
parent 0416a2f536
commit 5bdbb430e7
2 changed files with 21 additions and 8 deletions

View File

@ -10,10 +10,10 @@ Currently the following AVR MCUs are supported:
AVR MCU | Flash bytes used (.text + .data) | Bootloader region size
--- | --- | ---
atmega8 | 706 (0x2C2) | 512 words
atmega88 | 726 (0x2D6) | 512 words
atmega168 | 726 (0x2D6) | 512 words
atmega328p | 726 (0x2D6) | 512 words
atmega8 | 718 (0x2CE) | 512 words
atmega88 | 740 (0x2E4) | 512 words
atmega168 | 740 (0x2E4) | 512 words
atmega328p | 740 (0x2E4) | 512 words
(Compiled on Ubuntu 18.04 LTS (gcc 5.4.0 / avr-libc 2.0.0) with EEPROM and LED support)

21
main.c
View File

@ -280,13 +280,21 @@ static uint8_t TWI_data_write(uint8_t bcnt, uint8_t data)
switch (cmd)
{
case CMD_ACCESS_FLASH:
buf[bcnt -4] = data;
if (bcnt >= sizeof(buf) +3)
{
write_flash_page();
uint8_t pos = bcnt -4;
buf[pos] = data;
if (pos >= (sizeof(buf) -2))
{
ack = 0x00;
}
if (pos >= (sizeof(buf) -1))
{
write_flash_page();
}
break;
}
#if (EEPROM_SUPPORT)
case CMD_ACCESS_EEPROM:
@ -364,7 +372,6 @@ static void TWI_vect(void)
if (TWI_data_write(bcnt++, TWDR) == 0x00)
{
control &= ~(1<<TWEA);
bcnt = 0;
}
break;
@ -380,8 +387,14 @@ static void TWI_vect(void)
/* prev. SLA+W, data received, NACK returned -> IDLE */
case 0x88:
TWI_data_write(bcnt++, TWDR);
/* fall through */
/* STOP or repeated START -> IDLE */
case 0xA0:
bcnt = 0;
/* fall through */
/* prev. SLA+R, data sent, NACK returned -> IDLE */
case 0xC0:
LED_RT_OFF();