STM32L1 micropython port

Discussion and questions about boards that can run MicroPython but don't have a dedicated forum.
Target audience: Everyone interested in running MicroPython on other hardware.
Jolatomme
Posts: 6
Joined: Sun Feb 03, 2019 8:37 pm

STM32L1 micropython port

Post by Jolatomme » Sun Feb 03, 2019 9:38 pm

Dear Micropython community,
I am new to this forum and would like to contribute to extend micropython support
to STM32L152 device. I have quickly searched in the forum a topic covering L1 devices but
did not find anything. If I missed something I do apologize.
I am analog circuit designer not familiar at all with github and software development.
Hopefully I am familiar with python.
L152 device is based on a cortex M3 core. It has 512kb of flash and 80kb of SRAM.
So micropython should fit inside quite easily (I hope). For testing, I have STM32L152 NUCLEO board.
To startup the port, I began with the easy stuff :
1 - create a board NUCLEO_STM32L152RE in /ports/stm32/boards with :
mpconfigboard.h
mpconfigboard.mk
pins.csv
stm32l1xx_hal_conf.h (from STM32 cube)
2 - create the _af file : stm32l152_af.csv (I used the suggested tip of using a spreadsheet by dhylands)
3 - copy start-up script from ST : stm32l152xe.ld
4 - copy STM32 HAL, CMSIS and clean them as suggested on github (remove trailing characters, ...)
5 - dry run for a first compilation :
gmake BOARD=NUCLEO_L152RE V=1 CROSS_COMPILE=/usr/local/gcc-arm-embedded/bin/arm-none-eabi-
and then as expected, I got some compilation errors in : dma.c, flash.c, flashbdev.c, adc.c
#error Unsupported MCU series

So I guess I am now in the hard part (in deep chip).
Looking in dma.c, I saw several #ifdef referring to various chips.
I guess I will have to add some specific code section like : elif defined(STM32L1) ...
for each files and with specific declaration for the STM32L152 architecture.

I am currently stuck there in dma.c
I was wondering if any procedure, guide, tutorial, hints, tips ... already exists to help this porting job or if I have to go the hard way by opening files one by one, try to understand them and adjust the settings until compilation succeeds? And then How to debug ?
I was also wondering if anybody is interested in this port ?
Thanks,
Jo.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: STM32L1 micropython port

Post by dhylands » Sun Feb 03, 2019 10:27 pm

Yeah - there is no miracle solution here. You need to open each file that needs the MCU support and add the appropriate support.

As far as testing and debugging, I normally start simple, try to just get a UART going and then move onto the other peripherals that you want.

In theory you can use a debugger with the nucleo boards. I've always managed to get by by using prints. I normally also wind up spending quite a bit of time reading the reference manual for the part in question.

Jolatomme
Posts: 6
Joined: Sun Feb 03, 2019 8:37 pm

Re: STM32L1 micropython port

Post by Jolatomme » Mon Feb 04, 2019 8:50 am

Hi Dave,
Thank you for your reply. This confirms what I thought.
Since I am not very experienced with software development, what is the best way to share code and progress ?
I guess it is to early to ask for a git pull request but how to share on-going stuff if someone is also interested ?
Regards,
Jo.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: STM32L1 micropython port

Post by dhylands » Mon Feb 04, 2019 5:43 pm

I'd start by forking the micropython repository and then creating a branch to work on.

Once you've got a branch and pushed it to your repository, anybody else can use it.

Try to rebase your branch with the master branch fairly often to minimize the amount of effort required to keep things up to date.
https://guides.github.com/introduction/flow/

Jolatomme
Posts: 6
Joined: Sun Feb 03, 2019 8:37 pm

Re: STM32L1 micropython port

Post by Jolatomme » Tue Feb 05, 2019 12:02 am

Hi Dave,
Thanks for reply. I followed your suggestion. This is my first contact with github and ... it is interesting ...
I created 2 forks on github for micropython (branch STM32L152-port) and stm32lib (branch L1-1.8).
The problem I have is stm32lib is pointing towards another branch inside my STM32L152-port : d752828b36.
I would have expected to to be able to choose for my branch of stm32lib. But it seems not to be the case.
One green button I have is : compare and pull request. But I guess it is too soon for a pull request.
But anyway, the job is initiated ... I will try to commit on regular basis.
Right now pins compilation is passing which means _af file and pin mapping should be OK.
Regards,
Jo.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: STM32L1 micropython port

Post by dhylands » Tue Feb 05, 2019 12:48 am

You'll want to change where the repository associated with submodule comes from as well. If you look in the top of the micropython repository you'll see a file called .gitmodules. You'll want to change this to point to your fork of the stm32lib and also point to the branch you're using in stm32lib.

After making these changes make sure you checkin the modified .gitmodules file into your branch (in microipython) that you're working on.

After making any changes to .gitmodules, or switch branches with a different .gitmodules file you'll want to run 'git subodule update`

Jolatomme
Posts: 6
Joined: Sun Feb 03, 2019 8:37 pm

Re: STM32L1 micropython port

Post by Jolatomme » Tue Feb 05, 2019 11:36 pm

Hi Dave,
Thank you very much for your replies and your patience.
OK I think I did the job with .gitmodules, I can see in the web interface the stm32lib (branch L1-1.8).
But still it is not mapped automatically. I have to select the dedicated port when I go to /lib directory.
So for sure I missed something.
I also made a mistake, and I do apologize. I wanted to submit a pull request on the mpconfigboard.h for the
NUCLEO STM32L476RG to enable CAN, RNG and DAC. But since I also added some directory to support STM32L1,
the pull request was mentionning all the files.
I need to figure out how to perform a pull request from the web interface for only one file.
Regards,
Jo.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: STM32L1 micropython port

Post by dhylands » Wed Feb 06, 2019 5:22 am

When you create a commit you can choose which files are included. If you've already got the files commited then the next best thing to do is to checkout your tree from before you made any changes and create a new branch. Then cherry pick the files you want to add and make a PR from that new branch.

Jolatomme
Posts: 6
Joined: Sun Feb 03, 2019 8:37 pm

Re: STM32L1 micropython port

Post by Jolatomme » Thu Feb 21, 2019 10:48 pm

Hi Dave,Ok I got your point and I wanted to give you some news.
I managed to find my way for compilation up to powerctrl.c file.
But I am stuck there for a while. I do not understand how to configure PLL.
I would like to use the 8MHz HSE, PLLMul=12, PLLDiv=3 to set the clocks.
Generating the clock configuration with cubeMx will lead to :

Code: Select all

void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /**Configure the main internal regulator output voltage 
  */
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  /**Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
  RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV3;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /**Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  {
    Error_Handler();
  }
}
powerctrl.c file seems the right place to include this code but I am wondering how to insert it the right way.
I am wondering also if using HSE is the best choice for micropython.
Regards,
Jo.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: STM32L1 micropython port

Post by dhylands » Fri Feb 22, 2019 5:51 am

The CPU clock initialization takes place in system_stm32.c

You probably won't be able to take your cubeMx code directly but will need to modify system_stm32.c appropriately along with the the config files in your board directory.

Most of the boards use HSE, but I'm pretty sure that there are some which use HSI instead.

Post Reply