-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
83 lines (82 loc) · 2.26 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "parser.h"
#include "queue.h"
#include "protocol/constants.h"
#include "protocol/message_structs.h"
#include "hardware.h"
#include "parameters.h"
#include "stepper.h"
#include "control_isr.h"
#include "config.h"
#include "peripheral.h"
#include "homing.h"
#include <usb_serial.h>
#include <mk20dx128.h>
#include <pin_config.h>
int main(void){
configure_nvic();
initialize_i2c(I2C_BASE_ADDRESS + read_i2c_address());
// Configure all of the hardware and internal state
initialize_motion_queue();
initialize_parser();
reset_parameters();
initialize_stepper_state();
set_microstepping(DEFAULT_MICROSTEPPING);
reset_hardware();
float_sync_line();
while(1){
if(parser.status == PARSER_ERR){
send_response(IMC_RSP_UNKNOWN,0);
initialize_parser();
continue;
}
if(parser.status == PARSER_NEW_EVENT){
switch(parser.packet_type){
case IMC_MSG_INITIALIZE:
initialize_motion_queue();
initialize_parser();
// Unlike out first initialization round, don't reset parameters
initialize_stepper_state();
float_sync_line();
response.init.slave_hw_ver = 0;
response.init.slave_fw_ver = 0;
response.init.queue_depth = MOTION_QUEUE_LENGTH;
send_response(IMC_RSP_OK,sizeof(rsp_initialize_t));
break;
case IMC_MSG_GETPARAM:
handle_get_parameter(&parser.packet.get_param, &response.param);
send_response(IMC_RSP_OK,sizeof(rsp_get_param_t));
break;
case IMC_MSG_SETPARAM:
handle_set_parameter(&parser.packet.set_param);
send_response(IMC_RSP_OK,0);
break;
case IMC_MSG_QUEUEMOVE:
{
int space = enqueue_block(&parser.packet.move);
send_response(space < 0 ? IMC_RSP_QUEUEFULL : IMC_RSP_OK,0);
// If we're adding moves in idle state, make sure that the sync interface is listening
if(st.state == STATE_IDLE)
enable_sync_interrupt();
}
break;
case IMC_MSG_STATUS:
response.status.queued_moves = queue_length();
if(st.state == STATE_ERROR){
response.status.status = parameters.error_low;
}else{
response.status.status = IMC_ERR_NONE;
}
send_response(IMC_RSP_OK,sizeof(rsp_status_t));
break;
case IMC_MSG_HOME:
send_response(IMC_RSP_OK,0);
enter_homing_routine();
break;
case IMC_MSG_QUICKSTOP:
send_response(IMC_RSP_ERROR,0);
break;
}
initialize_parser();
}
}
}