Skip to content

Commit

Permalink
Fix CRC DR read if peripheral clock is disabled (#83)
Browse files Browse the repository at this point in the history
* Fix CRC DR read if peripheral clock is disabled
  • Loading branch information
vintagepc authored Sep 18, 2021
1 parent aa91f4d commit 0aea92d
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion hw/arm/prusa/stm32f407/stm32.h
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ typedef struct Stm32Rcc Stm32Rcc;
/* Checks if the specified peripheral clock is enabled.
* Generates a hardware error if not.
*/
void stm32_rcc_check_periph_clk(Stm32Rcc *s, stm32_periph_t periph);
bool stm32_rcc_check_periph_clk(Stm32Rcc *s, stm32_periph_t periph);

/* Sets the IRQ to be called when the specified peripheral clock changes
* frequency. */
Expand Down
4 changes: 3 additions & 1 deletion hw/arm/prusa/stm32f407/stm32_rcc.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "stm32_rcc.h"
/* PUBLIC FUNCTIONS */

void stm32_rcc_check_periph_clk(Stm32Rcc *s, stm32_periph_t periph)
bool stm32_rcc_check_periph_clk(Stm32Rcc *s, stm32_periph_t periph)
{
Clk_p clk = &s->PERIPHCLK[periph];

Expand All @@ -14,7 +14,9 @@ void stm32_rcc_check_periph_clk(Stm32Rcc *s, stm32_periph_t periph)
*/
printf("Warning: You are attempting to use the stm32_rcc peripheral while "
"its clock is disabled.\n");
return false;
}
return true;
}

void stm32_rcc_set_periph_clk_irq(
Expand Down
10 changes: 9 additions & 1 deletion hw/arm/prusa/stm32f407/stm32f2xx_crc.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "stm32f2xx_crc.h"
#include "migration/vmstate.h"
#include "qemu/log.h"
#include "stm32_rcc.h"

#define R_CRC_DR (0x00 / 4)
#define R_CRC_DR_RESET 0xffffffff
Expand Down Expand Up @@ -123,7 +124,14 @@ f2xx_crc_read(void *arg, hwaddr addr, unsigned int size)
}
switch(addr) {
case R_CRC_DR:
return s->crc;
if (s->rcc && stm32_rcc_check_periph_clk(s->rcc, STM32_CRC))
{
return s->crc;
}
else
{
return 0;
}
case R_CRC_IDR:
return s->idr;
}
Expand Down
4 changes: 4 additions & 0 deletions hw/arm/prusa/stm32f407/stm32f2xx_crc.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include "qemu-common.h"
#include "hw/sysbus.h"

typedef struct Stm32Rcc Stm32Rcc;

#define TYPE_STM32F2XX_CRC "stm32f2xx-crc"
OBJECT_DECLARE_SIMPLE_TYPE(f2xx_crc,STM32F2XX_CRC);

Expand All @@ -38,6 +40,8 @@ typedef struct f2xx_crc {

uint32_t crc;
uint8_t idr;

Stm32Rcc* rcc;
} f2xx_crc;

#endif // STM32F2XX_CRC_H
1 change: 1 addition & 0 deletions hw/arm/prusa/stm32f407/stm32f407_soc.c
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ static void stm32f407_soc_realize(DeviceState *dev_soc, Error **errp)
sysbus_mmio_map(busdev, 0, 0x40002800);

dev = DEVICE(&s->crc);
s->crc.rcc = (Stm32Rcc*)&s->rcc;
if (!sysbus_realize(SYS_BUS_DEVICE(&s->crc),errp))
return;
busdev = SYS_BUS_DEVICE(dev);
Expand Down
1 change: 1 addition & 0 deletions hw/arm/prusa/stm32f407/stm32f4xx_iwdg.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ static void stm32f4xx_iwdg_reset(DeviceState *dev)
stm32f4xx_iwdg *s = STM32F4XX_IWDG(dev);
if (s->timer) {
timer_del(s->timer);
g_free(s->timer);
s->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, stm32f4xx_iwdg_fire, s);
}
memset(&s->regs, 0, sizeof(s->regs));
Expand Down

0 comments on commit 0aea92d

Please sign in to comment.