Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions UnipolarStepperDriver/UnipolarStepperDriver.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
#include "usi_i2c_master.h"

char step_current;
char step_state;
int8_t step_state;
char step_max;

char step_mode;
char step_dir;
char step_enabled;
volatile int step_count = 0;
volatile int step_compare = 0;
volatile uint16_t step_compare = 0;

char serial_i2c_buffer[8];
char serial_i2c_buffer_pos;
Expand All @@ -39,14 +39,14 @@ volatile char step_table[16] = {0b00001000, 0b00100000, 0b00010000, 0b01000000,

//Double Phase
0b00101000, 0b00110000, 0b01010000, 0b01001000,

//Half Stepping
0b00001000, 0b00101000, 0b00100000, 0b00110000,

0b00010000, 0b01010000, 0b01000000, 0b01001000};

inline void status_led_on();
inline void status_led_off();
void status_led_on();
void status_led_off();
inline void process_serial_message();

void set_rgb_led(char led);
Expand Down Expand Up @@ -112,7 +112,7 @@ ISR(TIMER1_COMPA_vect)
set_rgb_led(0b00000001);
if(--step_state < 0) step_state = step_max;
}

if(step_enabled == 1)
{
if(step_mode == 0)
Expand Down Expand Up @@ -154,7 +154,7 @@ ISR(TIMER0_COMPB_vect)
}

ISR(TIMER0_COMPA_vect)
{
{
if(step_enabled == 1)
{
PORTD |= step_current;
Expand Down Expand Up @@ -186,11 +186,11 @@ int main()
USIDR = 0xFF;
USICR = (0 << USISIE) | (0 << USIOIE) | (1 << USIWM1) | (0 << USIWM0) | (1 << USICS1) | (0 << USICS0) | (1 << USICLK) | (0 << USITC);
USISR = (1 << USISIF) | (1 << USIOIF) | (1 << USIPF) | (1 << USIDC) | (0x00 << USICNT0);
USI_Slave_register_buffer[0] = (unsigned char*)&step_compare;
USI_Slave_register_buffer[1] = (unsigned char*)(&step_compare)+1;
USI_Slave_register_buffer[2] = (unsigned char*)&step_count;
USI_Slave_register_buffer[3] = (unsigned char*)(&step_count)+1;

USI_Slave_register_buffer[0] = (char*)&step_compare;
USI_Slave_register_buffer[1] = (char*)(&step_compare)+1;
USI_Slave_register_buffer[2] = (char*)&step_count;
USI_Slave_register_buffer[3] = (char*)(&step_count)+1;
USI_Slave_register_buffer[4] = &step_dir;
USI_Slave_register_buffer[5] = &step_mode;
USI_Slave_register_buffer[6] = &step_enabled;
Expand All @@ -205,17 +205,17 @@ int main()
}
}

inline void process_serial_message()
void process_serial_message()
{
if(serial_available() > 2)
{
status_led_on();

char buffer[3];
serial_read_buffer(buffer, 3);

switch(buffer[0])
{
{
//Set I2C Address
case 0x22:
usi_i2c_slave_address = buffer[1];
Expand All @@ -239,14 +239,14 @@ inline void process_serial_message()
char addr = buffer[2] << 1 | 1;
serial_i2c_buffer[0] = addr;
USI_I2C_Master_Start_Transmission(serial_i2c_buffer, buffer[1]+1);
for(char i = 1; i <= buffer[1]; i++)
for(uint8_t i = 1; i <= buffer[1]; i++)
{
serial_transmit_byte(serial_i2c_buffer[i]);
}
}
break;
}
status_led_off();
status_led_off();
}
}

Expand All @@ -259,7 +259,7 @@ void fill_i2c_buffer_from_serial(char len, char addr, char rw)
//Put address into i2c buffer
serial_i2c_buffer[0] = addr;

for(char i = 1; i <= len; i++)
for(uint8_t i = 1; i <= len; i++)
{
while(serial_available() < 1);
serial_i2c_buffer[i] = serial_read();
Expand Down
2 changes: 1 addition & 1 deletion UnipolarStepperDriver/serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ unsigned char serial_read()
return 0;
}
char value = serial_buffer[0];
for(char i = 1; i < SERIAL_BUFFER_LENGTH; i++)
for(uint8_t i = 1; i < SERIAL_BUFFER_LENGTH; i++)
{
serial_buffer[i-1] = serial_buffer[i];
}
Expand Down
2 changes: 2 additions & 0 deletions UnipolarStepperDriver/serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ unsigned char serial_available();
// Pulls the first byte from the serial port buffer and deletes it
unsigned char serial_read();

unsigned int serial_read_buffer(char* data, int len);

// Serial_Flush
// Flush the Serial buffer
// Deletes any data currently in the read buffer
Expand Down
30 changes: 17 additions & 13 deletions UnipolarStepperDriver/usi_i2c_master.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
////USI Master State Information///////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

enum
enum __attribute__ ((__packed__))
{
USI_MASTER_ADDRESS,
USI_MASTER_WRITE,
Expand All @@ -66,7 +66,7 @@ char USI_I2C_Master_Transfer(char USISR_temp)
USI_I2C_WAIT_HIGH();
USI_CLOCK_STROBE(); //SCL Negative Edge
} while (!(USISR&(1<<USIOIF))); //Do until transfer is complete

USI_I2C_WAIT_LOW();

return USIDR;
Expand Down Expand Up @@ -96,7 +96,7 @@ char USI_I2C_Master_Start_Transmission(char *msg, char msg_size)
USI_SET_SCL_LOW();
USI_I2C_WAIT_LOW();
USI_SET_SDA_HIGH();

/////////////////////////////////////////////////////////////////

do
Expand All @@ -119,7 +119,7 @@ char USI_I2C_Master_Start_Transmission(char *msg, char msg_size)
{
USI_I2C_Master_State = USI_MASTER_READ;
}
//Fall through to WRITE to transmit the address byte
// fall through

///////////////////////////////////////////////////////////////////
// Write Operation //
Expand All @@ -132,7 +132,7 @@ char USI_I2C_Master_Start_Transmission(char *msg, char msg_size)
USI_SET_SCL_LOW();

USIDR = *(msg); //Load data

msg++; //Increment buffer pointer

USI_I2C_Master_Transfer(USISR_TRANSFER_8_BIT);
Expand All @@ -158,11 +158,11 @@ char USI_I2C_Master_Start_Transmission(char *msg, char msg_size)
USI_SET_SDA_INPUT();

(*msg) = USI_I2C_Master_Transfer(USISR_TRANSFER_8_BIT);

msg++;

USI_SET_SDA_OUTPUT();

if(msg_size == 1)
{
USIDR = 0xFF; //Load NACK to end transmission
Expand All @@ -178,18 +178,22 @@ char USI_I2C_Master_Start_Transmission(char *msg, char msg_size)

}while(--msg_size); //Do until all data is read/written


/////////////////////////////////////////////////////////////////
// Send Stop Condition //
/////////////////////////////////////////////////////////////////

USI_SET_SDA_LOW(); // Pull SDA low.
USI_I2C_WAIT_LOW();
USI_SET_SCL_INPUT(); // Release SCL.
while( !(PIN_USI & (1<<PIN_USI_SCL)) ); // Wait for SCL to go high.
USI_I2C_WAIT_LOW();

USI_SET_SCL_INPUT(); // Release SCL.

while( !(PIN_USI & (1<<PIN_USI_SCL)) ); // Wait for SCL to go high.

USI_I2C_WAIT_HIGH();
USI_SET_SDA_INPUT(); // Release SDA.
while( !(PIN_USI & (1<<PIN_USI_SDA)) ); // Wait for SDA to go high.
USI_SET_SDA_INPUT(); // Release SDA.

while( !(PIN_USI & (1<<PIN_USI_SDA)) ); // Wait for SDA to go high.

return 1;
}
2 changes: 1 addition & 1 deletion UnipolarStepperDriver/usi_i2c_master.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@

//USI I2C Master Transceiver Start
// Starts the transceiver to send or receive data based on the r/w bit
char USI_I2C_Master_Transceiver_Start(char *msg, char msg_size);
char USI_I2C_Master_Start_Transmission(char *msg, char msg_size);

#endif
5 changes: 3 additions & 2 deletions UnipolarStepperDriver/usi_i2c_slave.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ char usi_i2c_mode;
// need to read/write in your code. This abstracts the buffer and makes it easier to write directly
// to values in your code.
char* USI_Slave_register_buffer[USI_SLAVE_REGISTER_COUNT];
char USI_Slave_internal_address = 0;
uint8_t USI_Slave_internal_address = 0;
char USI_Slave_internal_address_set = 0;

enum
Expand Down Expand Up @@ -211,7 +211,8 @@ ISR(USI_OVERFLOW_vect)
USISR = USI_SLAVE_SET_START_COND_USISR;
return;
}
//else: fall through into SEND_DATA
//else:
// fall through

/////////////////////////////////////////////////////////////////////////
// Case USI_SLAVE_SEND_DATA //
Expand Down
10 changes: 10 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[platformio]
src_dir = UnipolarStepperDriver/

[env]
build_src_flags = -Wall -Wextra

[env:default]
platform = atmelavr
board = attiny2313
build_type = release