Skip to content

Commit

Permalink
Merge pull request #13 from harp-tech/fw-receive_motor_control_on-_rx
Browse files Browse the repository at this point in the history
Receive motor control using serial.
  • Loading branch information
filcarv authored Oct 29, 2024
2 parents 875d576 + 2fc282c commit 98e8ba4
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
14 changes: 12 additions & 2 deletions Firmware/VestibularVrH2/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ void hwbp_app_initialize(void)
/* Define versions */
uint8_t hwH = 1;
uint8_t hwL = 0;
uint8_t fwH = 0;
uint8_t fwL = 5;
uint8_t fwH = 1;
uint8_t fwL = 0;
uint8_t ass = 0;

/* Start core */
Expand Down Expand Up @@ -90,6 +90,16 @@ void core_callback_initialize_hardware(void)

/* Initialize encoder */
init_quadrature_encoder();

/* Initialize serial with 100 KHz */
uint16_t BSEL = 19;
int8_t BSCALE = 0;

USARTD0_CTRLC = USART_CMODE_ASYNCHRONOUS_gc | USART_PMODE_DISABLED_gc | USART_CHSIZE_8BIT_gc;
USARTD0_BAUDCTRLA = *((uint8_t*)&BSEL);
USARTD0_BAUDCTRLB = (*(1+(uint8_t*)&BSEL) & 0x0F) | ((BSCALE<<4) & 0xF0);
USARTD0_CTRLB = USART_RXEN_bm;
USARTD0_CTRLA |= (INT_LEVEL_LOW << 4);
}

void core_callback_reset_registers(void)
Expand Down
1 change: 1 addition & 0 deletions Firmware/VestibularVrH2/app_ios_and_regs.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
void init_ios(void)
{ /* Configure input pins */
io_pin2in(&PORTB, 0, PULL_IO_TRISTATE, SENSE_IO_EDGES_BOTH); // STOP_SWITCH
io_pin2in(&PORTD, 2, PULL_IO_TRISTATE, SENSE_IO_EDGES_BOTH); // RX

/* Configure input interrupts */
io_set_int(&PORTB, INT_LEVEL_LOW, 0, (1<<0), false); // STOP_SWITCH
Expand Down
45 changes: 45 additions & 0 deletions Firmware/VestibularVrH2/interrupts.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,50 @@ ISR(ADCA_CH0_vect, ISR_NAKED)
app_regs.REG_ANALOG_INPUT = get_analog_input();
core_func_send_event(ADD_REG_ANALOG_INPUT, false);

reti();
}

/************************************************************************/
/* EXTERNAL MOTOR CONTROL */
/************************************************************************/
bool external_control_first_byte = true;
int16_t motor_pulse_interval;

ISR(USARTD0_RXC_vect, ISR_NAKED)
{
if (external_control_first_byte)
{
external_control_first_byte = false;

motor_pulse_interval = USARTD0_DATA;

timer_type0_enable(&TCD0, TIMER_PRESCALER_DIV64, 100, INT_LEVEL_LOW); // 200 us
}
else
{
external_control_first_byte = true;

int16_t temp = USARTD0_DATA;

motor_pulse_interval |= (temp << 8) & 0xFF00;

timer_type0_stop(&TCD0);

app_regs.REG_ANALOG_INPUT = motor_pulse_interval;
core_func_send_event(ADD_REG_ANALOG_INPUT, true);

app_write_REG_IMMEDIATE_PULSES(&motor_pulse_interval);
}

reti();
}


ISR(TCD0_OVF_vect, ISR_NAKED)
{
external_control_first_byte = true;

timer_type0_stop(&TCD0);

reti();
}
Binary file modified Firmware/VestibularVrH2/registers.xls
Binary file not shown.

0 comments on commit 98e8ba4

Please sign in to comment.