kopia lustrzana https://github.com/gnea/grbl
fix: SPI flash
rodzic
c2e42f2475
commit
c7b5ea98fd
|
@ -64,7 +64,7 @@
|
|||
#include "stepper.h"
|
||||
#include "jog.h"
|
||||
#include "vcm.h"
|
||||
#include "spi.h"
|
||||
#include "mo_spi.h"
|
||||
|
||||
// ---------------------------------------------------------------------------------------
|
||||
// COMPILE-TIME ERROR CHECKING OF DEFINE VALUES:
|
||||
|
|
|
@ -66,6 +66,10 @@ int main(void)
|
|||
if (bit_istrue(settings.flags,BITFLAG_HOMING_ENABLE)) { sys.state = STATE_ALARM; }
|
||||
#endif
|
||||
|
||||
vcm_init();
|
||||
motor_spi_init();
|
||||
PORTD |= (1 << PORTD7);
|
||||
|
||||
// Grbl initialization loop upon power-up or a system abort. For the latter, all processes
|
||||
// will return to this loop to be cleanly re-initialized.
|
||||
for(;;) {
|
||||
|
@ -91,8 +95,6 @@ int main(void)
|
|||
coolant_init();
|
||||
limits_init();
|
||||
probe_init();
|
||||
vcm_init();
|
||||
motor_spi_init();
|
||||
plan_reset(); // Clear block buffer and planner variables
|
||||
st_reset(); // Clear stepper subsystem variables.
|
||||
|
||||
|
|
|
@ -0,0 +1,139 @@
|
|||
|
||||
#include "grbl.h"
|
||||
#define CS1_PORT PORTC // Chip Select for Driver 1
|
||||
#define CS1_PIN PC0
|
||||
|
||||
#define CS2_PORT PORTC // Chip Select for Driver 2
|
||||
#define CS2_PIN PC1
|
||||
|
||||
void debug_print(const char *msg)
|
||||
{
|
||||
while (*msg)
|
||||
{
|
||||
serial_write(*msg++); // Send each character over serial
|
||||
}
|
||||
}
|
||||
|
||||
// Chip Select Pin Definitions
|
||||
#define CS1_PIN PC0 // Chip Select for Driver 1
|
||||
#define CS2_PIN PC1 // Chip Select for Driver 2
|
||||
|
||||
// SPI Timing Parameters (in ns)
|
||||
#define SPI_SCLK_PERIOD 100
|
||||
#define SPI_SETUP_TIME 20
|
||||
#define SPI_HOLD_TIME 30
|
||||
|
||||
// Function to initialize SPI communication
|
||||
void SPI_init() {
|
||||
// Set MOSI, SCK, and SS as output
|
||||
DDRB |= (1 << PB3) | (1 << PB5) | (1 << PB2);
|
||||
|
||||
// Set Chip Select pins as outputs
|
||||
DDRC |= (1 << CS1_PIN) | (1 << CS2_PIN);
|
||||
|
||||
// Deselect all chips
|
||||
PORTC |= (1 << CS1_PIN) | (1 << CS2_PIN);
|
||||
|
||||
// Enable SPI, Set as Master, Clock rate fck/16
|
||||
SPCR = (1 << SPE) | (1 << MSTR) | (1 << SPR0);
|
||||
}
|
||||
|
||||
// Function to send 16-bit SPI frame
|
||||
void SPI_send_frame(uint8_t cs_pin, uint16_t address, uint16_t data) {
|
||||
debug_print("Sending SPI frame...\n");
|
||||
|
||||
// Construct 16-bit SDI frame
|
||||
// MSB format: Command Byte (8-bit)
|
||||
// [B15:B14] = 00 (standard frame)
|
||||
// [B13:B8] = Address (6 bits)
|
||||
// [B7:B0] = Write Command (W0 = 0)
|
||||
uint16_t command_byte = (0 << 14) | ((address & 0x3F) << 8) | (0 << 7);
|
||||
|
||||
// Full 16-bit frame
|
||||
uint16_t sdi_frame = command_byte | (data & 0xFF);
|
||||
|
||||
debug_print("Pulling CS low...\n");
|
||||
|
||||
delay_ms(50);
|
||||
|
||||
// Select the chip
|
||||
PORTC &= ~(1 << cs_pin);
|
||||
|
||||
// Delay to meet nSCS setup time
|
||||
_delay_us(0.05);
|
||||
|
||||
// Send high byte first (MSB)
|
||||
SPDR = (sdi_frame >> 8) & 0xFF;
|
||||
while(!(SPSR & (1 << SPIF)));
|
||||
|
||||
// Send low byte
|
||||
SPDR = sdi_frame & 0xFF;
|
||||
while(!(SPSR & (1 << SPIF)));
|
||||
|
||||
// Deselect the chip
|
||||
PORTC |= (1 << cs_pin);
|
||||
}
|
||||
|
||||
void SPI_read_frame(uint8_t cs_pin, uint16_t address) {
|
||||
|
||||
uint16_t *data;
|
||||
|
||||
// Construct 16-bit SDI frame
|
||||
// MSB format: Command Byte (8-bit)
|
||||
// [B15:B14] = 00 (standard frame)
|
||||
// [B13:B8] = Address (6 bits)
|
||||
// [B7:B0] = Read Command (R0 = 1)
|
||||
uint16_t command_byte = (0 << 14) | ((address & 0x3F) << 8) | (1 << 7);
|
||||
|
||||
// Full 16-bit frame
|
||||
uint16_t sdi_frame = command_byte;
|
||||
|
||||
// Select the chip
|
||||
PORTC &= ~(1 << cs_pin);
|
||||
|
||||
// Delay to meet nSCS setup time
|
||||
_delay_us(0.05);
|
||||
|
||||
// Send high byte first (MSB)
|
||||
SPDR = (sdi_frame >> 8) & 0xFF;
|
||||
while(!(SPSR & (1 << SPIF)));
|
||||
|
||||
// Send low byte
|
||||
SPDR = sdi_frame & 0xFF;
|
||||
while(!(SPSR & (1 << SPIF)));
|
||||
|
||||
// Read the data
|
||||
*data = SPDR;
|
||||
|
||||
// Deselect the chip
|
||||
PORTC |= (1 << cs_pin);
|
||||
|
||||
char debug_msg[50];
|
||||
|
||||
debug_print("Data read from SPI frame...\n");
|
||||
debug_print("Data: ");
|
||||
sprintf(debug_msg, "0x%04X", *data);
|
||||
debug_print(debug_msg);
|
||||
debug_print("\n");
|
||||
}
|
||||
|
||||
void motor_spi_init() {
|
||||
if (MACHINE_TYPE == BAMBOO) {
|
||||
SPI_init();
|
||||
debug_print("Initializing SPI for stepper drivers...\n");
|
||||
|
||||
// Configure Chip Select pins as outputs
|
||||
DDRC |= (1 << CS1_PIN) | (1 << CS2_PIN);
|
||||
CS1_PORT |= (1 << CS1_PIN); // Set CS high (deselect)
|
||||
CS2_PORT |= (1 << CS2_PIN); // Set CS high (deselect)
|
||||
|
||||
// Send configuration to Driver 1
|
||||
SPI_send_frame(CS1_PIN, 0x04, 0x07);
|
||||
// Send configuration to Driver 2
|
||||
SPI_send_frame(CS2_PIN, 0x04, 0x07);
|
||||
|
||||
SPI_read_frame(CS1_PIN, 0x04);
|
||||
|
||||
debug_print("SPI initialization complete.\n");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
#ifndef mo_spi_h
|
||||
#define mo_spi_h
|
||||
|
||||
void motor_spi_init();
|
||||
|
||||
#endif
|
58
grbl/spi.c
58
grbl/spi.c
|
@ -1,58 +0,0 @@
|
|||
#include "grbl.h"
|
||||
|
||||
#define CS1_PORT PORTC // Chip Select for Driver 1
|
||||
#define CS1_PIN PC0
|
||||
|
||||
#define CS2_PORT PORTC // Chip Select for Driver 2
|
||||
#define CS2_PIN PC1
|
||||
|
||||
#define SPI_DDR DDRB
|
||||
#define SPI_PORT PORTB
|
||||
#define MOSI PB3
|
||||
#define MISO PB4
|
||||
#define SCK PB5
|
||||
|
||||
void spi_init() {
|
||||
SPI_DDR |= (1 << MOSI) | (1 << SCK); // Set MOSI and SCK as output
|
||||
SPI_DDR &= ~(1 << MISO); // Set MISO as input
|
||||
SPCR = (1 << SPE) | (1 << MSTR) | (1 << SPR0); // Enable SPI, Master, Fclk/16
|
||||
}
|
||||
|
||||
void spi_write(uint8_t cs_port, uint8_t cs_pin, uint8_t address, uint8_t data) {
|
||||
// Pull CS low to select device
|
||||
cs_port &= ~(1 << cs_pin);
|
||||
_delay_us(1); // Short delay for setup
|
||||
|
||||
// Send address
|
||||
SPDR = address;
|
||||
while (!(SPSR & (1 << SPIF))); // Wait for transmission to complete
|
||||
|
||||
// Send data
|
||||
SPDR = data;
|
||||
while (!(SPSR & (1 << SPIF))); // Wait for transmission to complete
|
||||
|
||||
// Pull CS high to deselect device
|
||||
cs_port |= (1 << cs_pin);
|
||||
_delay_us(1); // Short delay for setup
|
||||
}
|
||||
|
||||
/**
|
||||
* CTRL1 Control Register (address = 0x04) [Default = 0Fh]
|
||||
* BIT = 2-0
|
||||
* SETTING = DECAY MODE
|
||||
* 111b = Smart tune Ripple Control
|
||||
*/
|
||||
void motor_spi_init() {
|
||||
if (MACHINE_TYPE == BAMBOO) {
|
||||
// Configure Chip Select pins as outputs
|
||||
DDRC |= (1 << CS1_PIN) | (1 << CS2_PIN);
|
||||
CS1_PORT |= (1 << CS1_PIN); // Set CS high (deselect)
|
||||
CS2_PORT |= (1 << CS2_PIN); // Set CS high (deselect)
|
||||
|
||||
// Send configuration to Driver 1
|
||||
spi_write(CS1_PORT, CS1_PIN, 0x04, 0x07);
|
||||
|
||||
// Send configuration to Driver 2
|
||||
spi_write(CS2_PORT, CS2_PIN, 0x04, 0x07);
|
||||
}
|
||||
}
|
|
@ -1,6 +0,0 @@
|
|||
#ifndef spi_h
|
||||
#define spi_h
|
||||
|
||||
void motor_spi_init();
|
||||
|
||||
#endif
|
|
@ -5,7 +5,7 @@ void vcm_init()
|
|||
if (MACHINE_TYPE == BAMBOO)
|
||||
{
|
||||
DDRD |= (1 << DDD7);
|
||||
// Set pin 7 to HIGH
|
||||
PORTD |= (1 << PORTD7);
|
||||
// Set pin 7 to LOW
|
||||
PORTD &= (1 << PORTD7);
|
||||
}
|
||||
}
|
Ładowanie…
Reference in New Issue