Skip to content

Conversation

@ofauchon
Copy link
Contributor

@ofauchon ofauchon commented Jan 5, 2021

This PR provides support for :

  • STM32L0 microcontroller series.
  • Dragino LGT92 device (which is based on stm32l072czt6 )

Working:

  • LPUART and UART
  • GPIOs
  • SPI
  • Timers

Not working yet:

  • i2c
  • power saving (LP modes)

Copy link
Member

@aykevl aykevl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, looks good to me! I have a number of code cleanliness issues but nothing major.

Comment on lines 59 to 73
stm32.RCC.IOPENR.SetBits(stm32.RCC_IOPENR_IOPAEN)
stm32.RCC.IOPENR.SetBits(stm32.RCC_IOPENR_IOPBEN)
stm32.RCC.IOPENR.SetBits(stm32.RCC_IOPENR_IOPCEN)

// Enable NVM (eeprom)
stm32.RCC.AHBENR.SetBits(stm32.RCC_AHBENR_MIFEN)

// Enable Leds GPIO output
LED_RED.Configure(PinConfig{Mode: PinOutput})
LED_GREEN.Configure(PinConfig{Mode: PinOutput})
LED_BLUE.Configure(PinConfig{Mode: PinOutput})

LED_RED.Low()
LED_GREEN.Low()
LED_BLUE.Low()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you doing this in the board file?

  • Configuring outputs needs to be done by the program, unless it's really necessary they should be configured by a program that wants to use these LEDs.
  • Is there any support for EEPROM? If not, I would suggest leaving it disabled (enabling unused peripherals may cost power).
  • If you need to enable the GPIO peripheral (stm32.RCC.IOPENR.SetBits(stm32.RCC_IOPENR_IOPAEN)), this should be done in Pin.Configure. It looks like this is already done, so these lines are unnecessary.

Comment on lines 114 to 117
// case 5:
// return stm32.GPIOF
// case 6:
// return stm32.GPIOG
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If GPIOF etc. don't exist, you don't need to include them here. If you want to explain why you left them out, you could add a comment saying "GPIOF etc. do not exist on this chip".

Comment on lines 188 to 191
// case unsafe.Pointer(stm32.TIM8): // TIM8 clock enable
// stm32.RCC.APB2ENR.SetBits(stm32.RCC_APB2ENR_TIM8EN)
// case unsafe.Pointer(stm32.TIM1): // TIM1 clock enable
// stm32.RCC.APB2ENR.SetBits(stm32.RCC_APB)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is again code that should be removed: TIM1 and TIM8 do not seem to exist on this chip.


// These are based on APB2 clock frquency (84MHz on the discovery board)
// TODO: also include the MCU/APB clock setting in the equation
switch true {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can leave out the true here, a switch can also work as a replacement for a long if / else if / else chain.

Suggested change
switch true {
switch {

conf = stm32.SPI_PCLK_2
default:
// None of the specific baudrates were selected; choose the lowest speed
conf = stm32.SPI_PCLK_256
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we'd default to 4MHz, but if that is not possible you could use a value just below that.
You can add such a check at the top by checking for 0:

if config.Frequency == 0 {
    config.Frequency = 4e6
}

Comment on lines 77 to 78
case localFrequency < 328125:
conf = stm32.SPI_PCLK_256
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be useful to make this calculation explicit. For example:

const apb2Frequency = 84e6

// ...

switch {
case localFrequency >= apb2Frequency / 2:
    conf = stm32.SPI_PCLK_2
case localFrequency >= apb2Frequency / 4:
    conf = stm32.SPI_PCLK_4
// ...
case localFrequency >= apb2Frequency / 128:
    conf = stm32.SPI_PCLK_128
default:
    conf = stm32.SPI_PCLK_256
}

This way the calculation is very explicit. This kind of calculation is always done at compile time, so you don't need to worry about any overhead it might introduce.
You can take a look here as an example: #1446

Comment on lines 19 to 20
"flash-method": "openocd",
"openocd-interface": "stlink-v2",
Copy link
Member

@aykevl aykevl Jan 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"flash-method": "openocd",
"openocd-interface": "stlink-v2",

These two lines are specific to the board, so shouldn't be in the chip specification.

"--target=armv6m-none-eabi",
"-Qunused-arguments"
],
"linkerscript": "targets/stm32l072czt6.ld",
Copy link
Member

@aykevl aykevl Jan 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"linkerscript": "targets/stm32l072czt6.ld",

Because this linker script is for a more specific chip (with a given amount of memory), it shouldn't be included in the more generic chip family specification.

@aykevl
Copy link
Member

aykevl commented Jan 5, 2021

And before I forget: can you also add a smoke test to the Makefile? You can see a list of commands there already to run the smoke tests.

Add default LED declaration so example compiles
Remove useless hardware init and comments
Move all machine_stm32l0x2 code back to machine_stm32l0 as it'll probably work for stm32l0x1
Move Flash-method,linkerscript... from chip target to board target json
@ofauchon ofauchon changed the title Support for STM32L0x MCUs and Dragino LGT92 device Support for STM32L0 MCUs and Dragino LGT92 device Jan 6, 2021
@ofauchon
Copy link
Contributor Author

ofauchon commented Jan 7, 2021

Hi @aykevl,

Thanks for your detailed review, and all the suggestions.
I think I made all the proposed changes.

Please tell me if you have other improvements.

Olivier

Copy link
Member

@aykevl aykevl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the fixes! Looks good to me now.

@deadprogram
Copy link
Member

I will squash/merge this PR now. Thank you very much for the contribution @ofauchon

@deadprogram deadprogram merged commit 65caf77 into tinygo-org:dev Jan 8, 2021
@ofauchon ofauchon deleted the stm32l0 branch May 31, 2021 20:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants