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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
| void motor_on_irq(void *args) { rt_sem_release(&motor_on_sem); rt_pin_write(LED0_PIN, PIN_HIGH); }
void motor_off_irq(void *args) { rt_sem_release(&motor_off_sem); rt_pin_write(LED0_PIN, PIN_HIGH); }
static void motor_on_thread_entry() {
rt_size_t size; struct rt_can_msg can_motor_on_msg; can_motor_on_msg.ide = RT_CAN_STDID; can_motor_on_msg.rtr = RT_CAN_DTR; can_motor_on_msg.len = 8; can_motor_on_msg.data[0] = MOTOR_ON_CMD; can_motor_on_msg.data[1] = 0x00; can_motor_on_msg.data[2] = 0x00; can_motor_on_msg.data[3] = 0x00; can_motor_on_msg.data[4] = 0x00; can_motor_on_msg.data[5] = 0x00; can_motor_on_msg.data[6] = 0x00; can_motor_on_msg.data[7] = 0x00; while (1) { rt_sem_take(&motor_on_sem, RT_WAITING_FOREVER); can_motor_on_msg.id = MOTOR1_ID; size = rt_device_write(can_dev, 0, &can_motor_on_msg, sizeof(can_motor_on_msg));
can_motor_on_msg.id = MOTOR2_ID; size = rt_device_write(can_dev, 0, &can_motor_on_msg, sizeof(can_motor_on_msg));
can_motor_on_msg.id = MOTOR3_ID; size = rt_device_write(can_dev, 0, &can_motor_on_msg, sizeof(can_motor_on_msg));
can_motor_on_msg.id = MOTOR4_ID; size = rt_device_write(can_dev, 0, &can_motor_on_msg, sizeof(can_motor_on_msg));
can_motor_on_msg.id = MOTOR5_ID; size = rt_device_write(can_dev, 0, &can_motor_on_msg, sizeof(can_motor_on_msg));
can_motor_on_msg.id = MOTOR6_ID; size = rt_device_write(can_dev, 0, &can_motor_on_msg, sizeof(can_motor_on_msg));
can_motor_on_msg.id = MOTOR7_ID; size = rt_device_write(can_dev, 0, &can_motor_on_msg, sizeof(can_motor_on_msg));
can_motor_on_msg.id = MOTOR8_ID; size = rt_device_write(can_dev, 0, &can_motor_on_msg, sizeof(can_motor_on_msg)); } }
static void motor_off_thread_entry() {
rt_size_t size; struct rt_can_msg can_motor_off_msg; can_motor_off_msg.ide = RT_CAN_STDID; can_motor_off_msg.rtr = RT_CAN_DTR; can_motor_off_msg.len = 8; can_motor_off_msg.data[0] = MOTOR_OFF_CMD; can_motor_off_msg.data[1] = 0x00; can_motor_off_msg.data[2] = 0x00; can_motor_off_msg.data[3] = 0x00; can_motor_off_msg.data[4] = 0x00; can_motor_off_msg.data[5] = 0x00; can_motor_off_msg.data[6] = 0x00; can_motor_off_msg.data[7] = 0x00;
while (1) { rt_sem_take(&motor_off_sem, RT_WAITING_FOREVER); can_motor_off_msg.id = MOTOR1_ID; size = rt_device_write(can_dev, 0, &can_motor_off_msg, sizeof(can_motor_off_msg));
can_motor_off_msg.id = MOTOR2_ID; size = rt_device_write(can_dev, 0, &can_motor_off_msg, sizeof(can_motor_off_msg));
can_motor_off_msg.id = MOTOR3_ID; size = rt_device_write(can_dev, 0, &can_motor_off_msg, sizeof(can_motor_off_msg));
can_motor_off_msg.id = MOTOR4_ID; size = rt_device_write(can_dev, 0, &can_motor_off_msg, sizeof(can_motor_off_msg));
can_motor_off_msg.id = MOTOR5_ID; size = rt_device_write(can_dev, 0, &can_motor_off_msg, sizeof(can_motor_off_msg));
can_motor_off_msg.id = MOTOR6_ID; size = rt_device_write(can_dev, 0, &can_motor_off_msg, sizeof(can_motor_off_msg));
can_motor_off_msg.id = MOTOR7_ID; size = rt_device_write(can_dev, 0, &can_motor_off_msg, sizeof(can_motor_off_msg));
can_motor_off_msg.id = MOTOR8_ID; size = rt_device_write(can_dev, 0, &can_motor_off_msg, sizeof(can_motor_off_msg)); } }
static rt_err_t motor_on_off_thread_init(void) { rt_err_t res_on, res_off, res; rt_thread_t thread_on, thread_off; res = RT_EOK;
rt_pin_mode(KEY0_PIN, PIN_MODE_INPUT_PULLUP); rt_pin_attach_irq(KEY0_PIN, PIN_IRQ_MODE_FALLING, motor_on_irq, RT_NULL); rt_pin_irq_enable(KEY0_PIN, PIN_IRQ_ENABLE); rt_pin_mode(KEY1_PIN, PIN_MODE_INPUT_PULLUP); rt_pin_attach_irq(KEY1_PIN, PIN_IRQ_MODE_FALLING, motor_off_irq, RT_NULL); rt_pin_irq_enable(KEY1_PIN, PIN_IRQ_ENABLE); rt_sem_init(&motor_on_sem, "motor_on_sem", 0, RT_IPC_FLAG_FIFO); rt_sem_init(&motor_off_sem, "motor_off_sem", 0, RT_IPC_FLAG_FIFO);
thread_on = rt_thread_create("motor_on", motor_on_thread_entry, RT_NULL, 1024, 4, 10); if (thread_on != RT_NULL) { rt_thread_startup(thread_on); } else { res_on = RT_ERROR; } thread_off = rt_thread_create("motor_off", motor_off_thread_entry, RT_NULL, 1024, 4, 10); if (thread_off != RT_NULL) { rt_thread_startup(thread_off); } else { res_off = RT_ERROR; } if ((res_on == RT_ERROR) || (res_off == RT_ERROR)) { res = RT_ERROR; } return res; }
|