STM32: Customizing - Paciente8159/uCNC GitHub Wiki
µCNC for STM32 can be configured/customized to fit different STM32 powered boards
Jump to section
Customizing boardmap
µCNC for STM32 can be customized either using the Web Config Tool or by manually modifing the source code files. For the latest method most configurations and pin assigning should be done in the corresponding boardmap file inside the uCNC/src/hal/boards/stm32/ directory and then the respective board file.
µCNC for STM32 similar to AVR but takes an extra step if you are using a board with a different STM32 flavor. It's necessary to create the boiler-plate project to fit your particular MCU. The Blue Pill source code is a good example on how to implement this. It's mostly written in plain C and it's mainly bare-metal (no vendor HAL or middleware). The only stack used is the tinyUSB library for the USB VCP port that works on a wide range of MCU's. The pinout configuration follows (almost all of) the STM32 Grbl pinout.
Important note on STM32F4
- STM32F4 EEPROM emulation is possible by using one of the last available sectors. This has some limitation depending on the size of the flash of the MCU. Usually STM32F4 MUCS have 4x 16Kb sectors, and (depending on the variant) have one adicional 64Kb sector and several other 128Kb sectors. For MCU with a total of 64Kb (4x 16Kb sectors) EEPROM emulation is not possible. In these cases storing settings will not be possible. That means that they have to be loaded at startup. Also coordinate systems will be lost on restart. For 128Kb devices (4x 16Kb sectors + 1x 64Kb sector), EEPROM emulation but the user should check if the total memory occupied by the firmware does not enter the 64Kb sector area. If that happens the EEPROM might delete part of the firmware and µCNC will become unstable. For 256Kb devices and above (4x 16Kb sectors + 1x 64Kb sector + 1(or more)x 128Kb sectors) the last 128KB sector. Again caution must be taken in account to not enter this region. This is specially important if the MCU already has an used area by a bootloader.
Important note on STM32H7
- STM32H7 lacks EEPROM emulation. Storing settings will not be possible. That means that they have to be loaded at startup. Also coordinate systems will be lost on restart.
Configure IO pin
This is similar to the general customization instructions
Input pin options
Weak input pull-ups
All input pins can have a weak pull-up activated to drive them high if unconnected. This is similar to the general customization instructions
External interrupt feature
All pins all repeatedly read by a soft polling routine. But for special function pins an interrupt driven event to force a reading and respective action can be also activated. This causes the response to be immediate and not depend on the pin reading routine cycle.
STM32 has several groups of External Interrupts (16/24/32 depending on the chip) that can be assign to one input of a single port. For example External Interrupt 0 can only be assigned to pin 0 of one of the Ports (A or B or C or ...). For Blue Pill the pin mapping can be checked here.
For Black Pill the pin mapping can be checked here.
This is similar to the general customization instructions
Configure communications
This is similar to the general customization instructions
Configure pwm clock
To configure the pwm clock, the used channel and timer must be supplied. To find the used channel register and timer we must check the MCU datasheet. Looking at the pin mapping shown above for Blue Pill and knowing that we are going to use pin A8 has the spindle pwm control we can see that the pwm on that pin is generated via Timer1 - Channel 1.
/Setup PWM
#define PWM0_BIT 8 //assigns PWM0 pin
#define PWM0_PORT A //assigns PWM0 pin
#define PWM0_CHANNEL 1
#define PWM0_TIMER 1
Configure analog channel
Although not used anywhere inside µCNC reading analog pins is possible. Beside configuring the pin like an input pin, two more definitions are needed to configure the AVR analog reading. These are setting the channel and the desired prescaller for the conversion. For example on STM32F1 ADC0 (Analog channel 0) is on pin A0. To configure it as µCNC's ANALOG0 input add the following code to the boardmap file:
#define ANALOG0_BIT 0
#define ANALOG0_PORT A
#define ANALOG0_CHANNEL 0 //STM32F1 channel 0
To make a read just do
uint8_t value = mcu_get_analog(ANALOG0);
Configure step generator timer
This is similar to the general customization instructions
Systick is used as the RTC Timer so the RTC_Timer configuration as not effect.
Configure servo pins
This is similar to the general customization instructions