Skip to content

ALL MCU: Customizing

Paciente8159 edited this page Apr 17, 2023 · 24 revisions

µCNC for any supported MCU can be configured/customized to fit different boards

Jump to section

Understanding the HAL for ALL MCU

µCNC is designed so that a single file defines the way the board links to the HAL interface and is then used by the core code to control the CNC machine. µCNC HAL uses some fixed naming conventions to map the MCU i/o to the core HAL and autogenerate the needed code.

Naming conventions

Output pins - special

  • STEP# pin defines the step output pin that controls linear actuator driver.
    • STEP0 to STEP5 are the output pins to control the step signal up to 6 independent drivers.
    • STEP6 and STEP7 are the output pins used as shadow registers to drive dual drive linear actuators.
  • DIR# pin defines the dir output pin that controls the linear actuator driver.
    • DIR0 to DIR5 are the output pins to control the direction signal up to 6 independent drivers
  • STEP#_EN pin defines the enable output pin that controls the linear actuator driver.
    • STEP0_EN to STEP5_EN are the output pins to control the enable signal up to 6 independent drivers.
  • SERVO# pin defines the servo signal output pin that controls common servo motors (1-2ms tON with 20ms period).
    • SERVO0 to SERVO7 are the the servo signal output pins with up to 8 independent servos.

Input pins - special

  • LIMIT_# pin defines the input pin that controls end-stop switch detection.
    • LIMIT_X, LIMIT_Y, LIMIT_Z, LIMIT_A, LIMIT_B, LIMIT_C, LIMIT_X2, LIMIT_Y2 and LIMIT_Z2.
  • ESTOP, SAFETY_DOOR, FHOLD and CS_RES pin defines the input pins that controls user actions and safety features.
  • PROBE pin defines the input pin used for probing and tool length detection.

COM pins - special

  • TX pin defines the UART port tx pin.
  • RX pin defines the UART port rx.
  • USB_DP pin defines the USB D+ port pin if the MCU has USB hardware.
  • USB_DM pin defines the USB D- port pin if the MCU has USB hardware.

Output pins - generic

  • PWM# pin defines a pwm output pin.
    • PWM0 to PWM15 are the pwm output pins.
  • DOUT# pin defines a generic output pin.
    • DOUT0 to DOUT31 are the generic output pins.

Input pins - generic

  • ANALOG# pin defines an analog input pin.
    • ANALOG0 to ANALOG15 are the analog input pins.
  • DIN# pin defines a generic input pin.
    • DIN0 to DIN31 are the generic input pins. pin.

Customizing boardmap

Taking the boardmap_grbl.h file inside src\hal\boards\avr folder, has an example lets walk through each section of the file.

Configure IO pin

An HAL pin need to be mapped to a physical IO pin. The way this is done is by defining the IO PORT and BIT. This must be performed for every used pin

Note: Some MCU may not have ports but just a single GPIO register with numbered pins. In those cases if defined any PORT for a pin it will simply be ignored.

Note2: STEPx pins are an exception. Each kinematics defines the number of step actuators that the machine has via STEPPER_COUNT declaration. All steps for actuators 0 to STEPPER_COUNT will be calculated, but only STEPx pins defined will have the output pulse generated. This is used for example for a servo motor with encoder. The step position is generated and the PID moves the motor to sync the position calculated with the value of the motor encoder.

//set pin D2 as STEP0 (output pin) 
#define STEP0_PORT D
#define STEP0_BIT 2
//set pin B1 as LIMIT_X (input pin) 
#define LIMIT_X_PORT B
#define LIMIT_X_BIT 1
//set pin B3 as PWM0 (pwm output pin) 
#define PWM0_PORT B
#define PWM0_BIT 3

Input pin options

Weak input pull-ups

If the MCU supports this feature all input pins can have a weak pull-up activated to drive them high if unconnected.

To activate this option to a pin just declare:

//activates LIMIT_X pin weak pull-up 
#define LIMIT_X_PULLUP

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. AVR has two types of input interrupts. The interrupt on change feature that can be configure up to 3 ports and also an external interrupt feature assigned to several pins on different ports (check the MCU datasheet for specifications).

Interrupt on change feature

  1. The interrupt on change feature can be configured declaring the µCNC ISR feature on an input pin that supports this feature. Each MCU is different and may or may not support this for the desired IO pin. For example, enabling ISR on LIMIT_X is done like this:
#define LIMIT_X_ISR

Configure communications

For versions before 1.5.beta

Besides configuring TX/RX or USB_DM/USB_DP pin each MCU may have different implementations for the registers needed to configure communications, so go to the desired MCU customizing page.

For versions 1.5.beta and newer

µCNC uses a serial connection to communicate with the control PC/Software. The type of communication interface is defined in the config file (INTERFACE option).

Usually all MCUS have some sort of hardware serial port that can be used for this function. Besides configuring TX and RX pin the number of the used USART/UART port must also be defined. The value must be equal to the UART port number. If not defined a default port will be used (depends on the board/MCU).

//Use UART0
#define UART_PORT 0

For MCU that support USB it's also possible to use a virtual Serial Port via USB. In that case only USB_DM/USB_DP pins need to be defined.

For MCU that support WiFi it's possible to use both Serial and Wifi communications. To enable this feature you need to ENABLE_WIFI on the cnc_config.h file.

Configure pwm clock

Besides configuring PWM pins each MCU may have different implementations for the registers needed to configure PWM, so go to the desired MCU customizing page.

Configure analog channel

Besides configuring ANALOG pins each MCU may have different implementations for the registers needed to configure PWM, so go to the desired MCU customizing page.

To make a read just do

uint8_t value = mcu_get_analog(ANALOG0);

Configure step generator timer

All step and dir pins are modified inside a timer ISR (µCNC's heartbeat). For this a free (unused) 16/32-bit timer inside your MCU must be chosen. This timer must be exclusive to the step generation ISR. It cannot be used by other functions (like PWM generation or others). The internal RTC clock may be implemented with a second timer or a system timer if the MCU has one. On AVR this is done with a second timer. On ARM Cortex for example the SystemTimer is used. This second timer is assigned to keep internal time tracking and run tasks.

Depending on the MCU a default timer is assigned if not specified. For example for AVR Timer1 is used by default.

//Setup the Step Timer used has the heartbeat for µCNC
#define ITP_TIMER 1

//Setup the RTC Timer used has by µCNC
//Usually on ARM processors this is ignored since this is done by the Systick
#define RTC_TIMER 0

By default ALL MCU setup these timers internally. These options allow to override the default timers.

Configure servo pins

Besides configuring SERVO pins, on some MCU and additional timer might be needed. For this a free (unused) 16/32-bit timer inside your MCU must be chosen. This timer must be exclusive to the servo generation ISR. It cannot be used by other functions (like PWM generation or others).

Depending on the MCU a default timer is assigned if not specified. For example on AVR the same timer used for RTC does this job. On ARM Cortex MCU the servo timer uses an additional timer.

// Setup the Timer to be used exclusively by servos in µCNC.
// If no servos are configured then the timer is free for other functions (like PWM) (even if defined in the board)
// On STM32F1x cores this will default to Timer 3. Other MCU may use other timers as a default timer.
//#define SERVO_TIMER 3

Oneshot timer and Laser PPI

If the board supports this feature is possible to configure a timer to be used as a oneshot/run once timer. This timer is required to support Laser PPI and/or may be necessary by external modules added to µCNC.

//#define ONESHOT_TIMER 3
Clone this wiki locally