No luck. It had the same error message, redeclaration of the variable. Here's 
the whole code for context. The other templates may be affecting the outcome of 
the one I quoted previously in this discussion. Solving this isn't critical. I 
have a working implementation in Nim. I am trying for an optimization in 
assembly in part to make my library efficient and also in part to learn how and 
when to best use asm in conjunction with Nim.
    
    
    # main_8.nim
    #
    # Try to allow this chained rmw construction:
    # RCC.AHB1ENR.GPIOAEN(1).write()
    
    import std/bitops
    import std/volatile
    
    type
      RegisterAddr = uint32
      RegisterVal = uint32
    
    # RCC peripheral
    type RCC_Base = distinct RegisterAddr
    const RCC* = RCC_Base(0x40023800)
    
    # RCC.AHB1ENR register
    type RCC_AHB1ENR_Val* = distinct RegisterVal
    type RCC_AHB1ENR_Ptr = ptr RCC_AHB1ENR_Val
    const RCC_AHB1ENR_Offset = 0x30'u32
    const RCC_AHB1ENR = cast[RCC_AHB1ENR_Ptr](RCC.uint32 + RCC_AHB1ENR_Offset)
    
    # reg read
    template AHB1ENR*(base: static RCC_Base): RCC_AHB1ENR_Val =
      volatileLoad(RCC_AHB1ENR)
    
    # chained field modify
    template GPIOAEN*(regVal: RCC_AHB1ENR_Val, fieldVal: uint32): 
RCC_AHB1ENR_Val {.dirty.} =
      {.emit: ["asm (\"BFI %0, %1, #0, #1\"\n\t: \"=r\" (", regVal, ")\n\t: 
\"r\" (", fieldVal, "));\n"].}
      regVal
    
    # write the modified fields to the reg
    template write*(regVal: RCC_AHB1ENR_Val) =
      volatileStore(RCC_AHB1ENR, regVal)
    
    proc main() =
      RCC.AHB1ENR         # reg read
         .GPIOAEN(1'u32)  # field modify
         .write()         # reg write
    
    when isMainModule:
      main()
    
    
    Run

Reply via email to