Skip to content

Commit

Permalink
Merge pull request #568 from Paciente8159/74hc595-pio
Browse files Browse the repository at this point in the history
RP2040 custom 74hc595 via PIO
  • Loading branch information
Paciente8159 committed Nov 14, 2023
2 parents 7cecd5d + e38334a commit 23239ef
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 3 deletions.
41 changes: 39 additions & 2 deletions uCNC/src/hal/boards/rp2040/boardmap_rpi_pico.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ extern "C"
// Setup control input pins
// #define ESTOP_BIT 0
// #define ESTOP_PORT A
//#define ESTOP_ISR
// #define ESTOP_ISR

// Setup com pins
#define RX_BIT 1
Expand Down Expand Up @@ -85,7 +85,44 @@ extern "C"
#define I2C_CLK_BIT 27
#define I2C_DATA_BIT 26
#define I2C_PORT 1
// #define I2C_ADDRESS 1
#define I2C_ADDRESS 1

/**
* This is an example of how to use RP2040 PIO to control
* up to 4 chainned 74hc595 (32 output pins) using only 3 pins
* from the board.
* The 3 pins should be sequential (for example GPIO's 26, 27 and 28)
*
* RP2040 does not yet support software generate PWM
*
* **/

// // Use PIO to shift data to 74HC595 shift registers (up to 4)
// // IO pins should be sequencial GPIO pins starting by data, then clock then the latch pin
// #define IC74HC595_CUSTOM_SHIFT_IO //Enables custom MCU data shift transmission. In RP2040 that is via a PIO
// #define IC74HC595_PIO_DATA 26
// #define IC74HC595_PIO_CLK 27
// #define IC74HC595_PIO_LATCH 28
// // enabling IC74HC595_CUSTOM_SHIFT_IO will force IC74HC595_COUNT to be set to 4 no matter what
// // support up to 4 chained 74HC595. Less can be used (overflow bits will be discarded like in the ESP32 I2S implementation)
// #define IC74HC595_COUNT 4

// #define STEP0_EN_IO_OFFSET 0
// #define STEP0_IO_OFFSET 1
// #define DIR0_IO_OFFSET 2
// #define STEP1_EN_IO_OFFSET 3
// #define STEP1_IO_OFFSET 4
// #define DIR1_IO_OFFSET 5
// #define STEP2_EN_IO_OFFSET 6
// #define STEP2_IO_OFFSET 7
// #define DIR2_IO_OFFSET 8
// #define STEP3_EN_IO_OFFSET 9
// #define STEP3_IO_OFFSET 10
// #define DIR3_IO_OFFSET 11
// #define STEP4_EN_IO_OFFSET 12
// #define STEP4_IO_OFFSET 13
// #define DIR4_IO_OFFSET 14
// #define DOUT0_IO_OFFSET 15

#ifdef __cplusplus
}
Expand Down
57 changes: 57 additions & 0 deletions uCNC/src/hal/mcus/rp2040/ic74hc595.pio
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
;
;Name: ic74hc595.pio
;Description: Implements a custom PIO for 74HC595 shift register in µCNC for RP2040.
;
;Copyright: Copyright (c) João Martins
;Author: João Martins
;Date: 02-11-2023
;
;µCNC is free software: you can redistribute it and/or modify
;it under the terms of the GNU General Public License as published by
;the Free Software Foundation, either version 3 of the License, or
;(at your option) any later version. Please see <http://www.gnu.org/licenses/>
;
;µCNC is distributed WITHOUT ANY WARRANTY;
;Also without the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
;See the GNU General Public License for more details.
;

.program ic74hc595
.side_set 1 opt

pull ; shift TX to OSR
set x, 31 ; load loop counter
set pins, 0 ; set all pins low (latch)
bitloop: ; loop bits
out pins, 1 side 0 ; Shift 1 bit from OSR to the data pin and the remaining down
jmp x-- bitloop side 1 ; decrement loop and bring clock up
set pins, 2 ; set 3rd pin (4=0b100) up (latch)

% c-sdk {
#include "hardware/clocks.h"

static inline void ic74hc595_program_init(PIO pio, uint sm, uint offset, uint pin_data, uint pin_clk, uint pin_latch, uint freq) {
pio_sm_config c = ic74hc595_program_get_default_config(offset);
sm_config_set_out_pins(&c, pin_data, 1); // define one pin to respond to the out instruction
sm_config_set_set_pins(&c, pin_clk, 2); // define all pins to respond to the set instruction
sm_config_set_sideset_pins(&c, pin_clk); // define clock as the side set base pin (and only)
// Only support MSB-first in this example code (shift to left, no auto push/pull, threshold=nbits)
sm_config_set_out_shift(&c, false, false, 32);
// All pins output
pio_sm_set_consecutive_pindirs(pio, sm, pin_data, 3, true);
pio_gpio_init(pio, pin_data);
pio_gpio_init(pio, pin_clk);
pio_gpio_init(pio, pin_latch);
// We only need TX, so get an 8-deep FIFO!
sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_TX);
// SM transmits 1 bit per 8 execution cycles.
float div = (float)clock_get_hz(clk_sys) / (freq << 1);
sm_config_set_clkdiv(&c, div);
pio_sm_init(pio, sm, offset, &c);
pio_sm_set_enabled(pio, sm, true);
}

static inline void ic74hc595_program_write(PIO pio, uint sm, uint out) {
pio_sm_put_blocking(pio, sm, out);
}
%}
68 changes: 68 additions & 0 deletions uCNC/src/hal/mcus/rp2040/ic74hc595.pio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// -------------------------------------------------- //
// This file is autogenerated by pioasm; do not edit! //
// -------------------------------------------------- //

#pragma once

#if !PICO_NO_HARDWARE
#include "hardware/pio.h"
#endif

// --------- //
// ic74hc595 //
// --------- //

#define ic74hc595_wrap_target 0
#define ic74hc595_wrap 5

static const uint16_t ic74hc595_program_instructions[] = {
// .wrap_target
0x80a0, // 0: pull block
0xe03f, // 1: set x, 31
0xe000, // 2: set pins, 0
0x7001, // 3: out pins, 1 side 0
0x1843, // 4: jmp x--, 3 side 1
0xe002, // 5: set pins, 2
// .wrap
};

#if !PICO_NO_HARDWARE
static const struct pio_program ic74hc595_program = {
.instructions = ic74hc595_program_instructions,
.length = 6,
.origin = -1,
};

static inline pio_sm_config ic74hc595_program_get_default_config(uint offset) {
pio_sm_config c = pio_get_default_sm_config();
sm_config_set_wrap(&c, offset + ic74hc595_wrap_target, offset + ic74hc595_wrap);
sm_config_set_sideset(&c, 2, true, false);
return c;
}

#include "hardware/clocks.h"
static inline void ic74hc595_program_init(PIO pio, uint sm, uint offset, uint pin_data, uint pin_clk, uint pin_latch, uint freq) {
pio_sm_config c = ic74hc595_program_get_default_config(offset);
sm_config_set_out_pins(&c, pin_data, 1); // define one pin to respond to the out instruction
sm_config_set_set_pins(&c, pin_clk, 2); // define all pins to respond to the set instruction
sm_config_set_sideset_pins(&c, pin_clk); // define clock as the side set base pin (and only)
// Only support MSB-first in this example code (shift to left, no auto push/pull, threshold=nbits)
sm_config_set_out_shift(&c, false, false, 32);
// All pins output
pio_sm_set_consecutive_pindirs(pio, sm, pin_data, 3, true);
pio_gpio_init(pio, pin_data);
pio_gpio_init(pio, pin_clk);
pio_gpio_init(pio, pin_latch);
// We only need TX, so get an 8-deep FIFO!
sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_TX);
// SM transmits 1 bit per 8 execution cycles.
float div = (float)clock_get_hz(clk_sys) / (freq << 1);
sm_config_set_clkdiv(&c, div);
pio_sm_init(pio, sm, offset, &c);
pio_sm_set_enabled(pio, sm, true);
}
static inline void ic74hc595_program_write(PIO pio, uint sm, uint out) {
pio_sm_put_blocking(pio, sm, out);
}

#endif
42 changes: 42 additions & 0 deletions uCNC/src/hal/mcus/rp2040/mcu_rp2040.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,44 @@ extern void rp2040_eeprom_flush(void);

uint8_t rp2040_pwm[16];

#ifdef IC74HC595_CUSTOM_SHIFT_IO

#ifndef IC74HC595_PIO_FREQ
#define IC74HC595_PIO_FREQ 20000000UL
#endif

#if IC74HC595_COUNT != 4
#error "IC74HC595_COUNT must be 4 to use ESP32 I2S mode for IO shifting"
#endif

#ifdef IC74HC595_HAS_PWMS
#warning "IC74HC595 on RP2040 does not support soft PWM yet!"
#endif

#include "pico/stdlib.h"
#include "hardware/pio.h"
#include "ic74hc595.pio.h"

static PIO pio_ic74hc595;
static uint sm_ic74hc595;

void ic74hc595_pio_init()
{
pio_ic74hc595 = pio0;
sm_ic74hc595 = 0;
uint offset = pio_add_program(pio_ic74hc595, &ic74hc595_program);
ic74hc595_program_init(pio_ic74hc595, sm_ic74hc595, offset, IC74HC595_PIO_DATA, IC74HC595_PIO_CLK, IC74HC595_PIO_LATCH, IC74HC595_PIO_FREQ);
}

// disable this function
// IO will be updated at a fixed rate
MCU_CALLBACK void ic74hc595_shift_io_pins(void)
{
ic74hc595_program_write(pio_ic74hc595, sm_ic74hc595, *((volatile uint32_t *)&ic74hc595_io_pins[0]));
}

#endif

void mcu_din_isr(void)
{
mcu_inputs_changed_cb();
Expand Down Expand Up @@ -266,6 +304,10 @@ static void mcu_usart_init(void)
void mcu_init(void)
{
mcu_io_init();

#ifdef IC74HC595_CUSTOM_SHIFT_IO
ic74hc595_pio_init();
#endif
mcu_usart_init();

pinMode(LED_BUILTIN, OUTPUT);
Expand Down
8 changes: 8 additions & 0 deletions uCNC/src/hal/mcus/rp2040/mcumap_rp2040.h
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,14 @@ extern "C"
#define SERVO_ALARM 1
#define ONESHOT_ALARM 2

#ifdef IC74HC595_CUSTOM_SHIFT_IO
#ifdef IC74HC595_COUNT
#undef IC74HC595_COUNT
#endif
// forces IC74HC595_COUNT to 4 to prevent errors
#define IC74HC595_COUNT 4
#endif

#define __timer_irq__(X) TIMER_IRQ_##X
#define _timer_irq_(X) __timer_irq__(X)

Expand Down
4 changes: 3 additions & 1 deletion uCNC/src/modules/ic74hc595.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#define ic74hc595_delay() mcu_delay_cycles(IC74HC595_DELAY_CYCLES)
#if (IC74HC595_COUNT != 0)
volatile uint8_t ic74hc595_io_pins[IC74HC595_COUNT];
#ifndef IC74HC595_CUSTOM_SHIFT_IO
MCU_CALLBACK void __attribute__((weak)) ic74hc595_shift_io_pins(void)
{

Expand All @@ -53,7 +54,7 @@ MCU_CALLBACK void __attribute__((weak)) ic74hc595_shift_io_pins(void)
{
do
{
memcpy(pins, (const void*)ic74hc595_io_pins, IC74HC595_COUNT);
memcpy(pins, (const void *)ic74hc595_io_pins, IC74HC595_COUNT);
mcu_clear_output(IC74HC595_LATCH);
for (uint8_t i = IC74HC595_COUNT; i != 0;)
{
Expand Down Expand Up @@ -92,3 +93,4 @@ MCU_CALLBACK void __attribute__((weak)) ic74hc595_shift_io_pins(void)
{
}
#endif
#endif

0 comments on commit 23239ef

Please sign in to comment.