using "platforms/cpus/stm32l552.repl"

pwr: Python.PythonPeripheral @ sysbus 0x40007000
    size: 0x400
    initable: false
    script: '''
if request.IsRead:
    if request.Offset == 0x10:  # PWR_SR1
        request.Value = 1 << 15 # SMPSHPRDY
    else:
        request.Value = 0
'''

otp: Python.PythonPeripheral @ sysbus 0x0BFA0000
    size: 0x10000
    initable: False
    script: '''
import hashlib

if request.IsRead:
    if request.Offset in [0x590, 0x594, 0x598]: # Unique device ID registers
        register = (request.Offset - 0x590) / 4
        machineName = str(self.GetMachine()).encode('ascii')
        md5 = hashlib.md5(machineName).hexdigest()
        start = register * 8
        end = start + request.Length * 2 # 2 characters per byte
        request.Value = int(md5[start:end], 16)
    elif request.Offset == 0x5e0: # Flash size data register
        request.Value = 0x200 # Assume STM32L552CET6: 512kB of flash as defined in stm32l552.repl:flash
    else:
        self.WarningLog('Unhandled read from offset 0x{0:X}, returning {1}.'.format(request.Offset, request.Value))
elif request.IsWrite:
    self.WarningLog('Unhandled write to offset 0x{0:X}.'.format(request.Offset))
'''

/*
 * Clock tree described in figure 30 of the STM32L5 reference manual. The RAMN firmware configures
 * values as follow:
 * - SYST_CSR.CLKSOURCE == 1, so systick clock is AHB clock
 * - HSE as PLL source
 * - PLLN = 16
 * - PLLR = 2
 * - AHB prescaler = 1
 * - APB1 and APB2 prescalres = 1
 *
 * Note: as this is not the default configuration, other clock values exist before the clock
 * initialization by the firmware.
 *
 * As RAMN documentation suggests that the preferred external crystal frequency is 10MHz, then:
 * - systick a frequency is (HSE * PLLN) / PLLR / AHB Prescaler = 10 * 16 / 2 / 1 = 80MHz
 * - timers frequencies are: (HSE * PLLN) / PLLR / AHB Prescaler / APB{1,2} Prescaler = 10 * 16 / 2 / 1 = 80MHz
 */
nvic:
    systickFrequency: 80000000

timer1:
    frequency: 80000000

timer2:
    frequency: 80000000

timer3:
    frequency: 80000000

timer4:
    frequency: 80000000

timer5:
    frequency: 80000000

timer6:
    frequency: 80000000

timer7:
    frequency: 80000000

timer8:
    frequency: 80000000

timer15:
    frequency: 80000000

timer16:
    frequency: 80000000

timer17:
    frequency: 80000000
