void timer_stop(tim_t dev) { if ((unsigned int)dev >= TIMER_NUMOF) { /* invalid timer */ return; } /* demultiplex to handle two types of hardware timers */ switch (_timer_variant(dev)) { case TIMER_PIT: pit_stop(_pit_index(dev)); return; case TIMER_LPTMR: lptmr_stop(_lptmr_index(dev)); return; default: return; } }
void pit1_isr(void) { if (--pload_tick_count) { /* continue current step */ if (pload_msg.u.steps.op[pload_step_index] == PLOAD_STEP_OP_RAMP) { pload_current += pload_msg.u.steps.arg0[pload_step_index]; dac_set(ma_to_dac((uint32_t)pload_current)); #if 0 /* DEBUG */ SERIAL_WRITE_STRING("dac_set:"); serial_write(uint32_to_string((uint32_t)pload_current), 8); SERIAL_WRITE_STRING("\r\n"); #endif /* DEBUG */ } goto on_done; } if ((++pload_step_index) == (uint32_t)pload_msg.u.steps.count) { #if 0 /* DEBUG */ SERIAL_WRITE_STRING("stopping\r\n"); #endif /* DEBUG */ pit_stop(1); goto on_done; } /* current step done, load next state */ switch (pload_msg.u.steps.op[pload_step_index]) { case PLOAD_STEP_OP_CONST: { const_case: pload_current = (int32_t)pload_msg.u.steps.arg0[pload_step_index]; dac_set(ma_to_dac((uint32_t)pload_current)); #if 0 /* DEBUG */ SERIAL_WRITE_STRING("dac_set:"); serial_write(uint32_to_string((uint32_t)pload_current), 8); SERIAL_WRITE_STRING("\r\n"); #endif /* DEBUG */ pload_tick_count = (uint32_t)pload_msg.u.steps.arg1[pload_step_index]; break ; } case PLOAD_STEP_OP_RAMP: { pload_current += (int32_t)pload_msg.u.steps.arg0[pload_step_index]; dac_set(ma_to_dac((uint32_t)pload_current)); #if 0 /* DEBUG */ SERIAL_WRITE_STRING("dac_set:"); serial_write(uint32_to_string((uint32_t)pload_current), 8); SERIAL_WRITE_STRING("\r\n"); #endif /* DEBUG */ pload_tick_count = (uint32_t)pload_msg.u.steps.arg1[pload_step_index]; break ; } case PLOAD_STEP_OP_WAIT: { pload_tick_count = (uint32_t)pload_msg.u.steps.arg1[pload_step_index]; break ; } case PLOAD_STEP_OP_REPEAT: { const int32_t repeat_count = pload_msg.u.steps.arg0[pload_step_index]; if ((++pload_repeat_index) == (uint32_t)repeat_count) { #if 0 /* DEBUG */ SERIAL_WRITE_STRING("stopping\r\n"); #endif /* DEBUG */ pit_stop(1); goto on_done; } if (repeat_count == -1) { pload_repeat_index = 0; } /* next step */ pload_step_index = 0; goto const_case; break ; } default: break ; } on_done: pit_clear_int(1); }
int main(void) { uint32_t msize; uint32_t rsize; #ifdef DAC_USE_VREF vref_setup(); #endif /* DAC_USE_VREF */ dac_setup(); dac_enable(); serial_setup(); msize = 0; while (1) { rsize = serial_get_rsize(); if (rsize > (sizeof(pload_msg) - msize)) rsize = sizeof(pload_msg) - msize; if (rsize == 0) continue ; /* stop the generator if active */ if (pload_flags & PLOAD_FLAG_IS_STARTED) { #if 0 /* DEBUG */ SERIAL_WRITE_STRING("stopping\r\n"); #endif /* DEBUG */ pload_flags &= ~PLOAD_FLAG_IS_STARTED; pit_stop(1); } serial_read((uint8_t*)&pload_msg + msize, rsize); msize += rsize; if (msize != sizeof(pload_msg)) continue ; /* new message */ msize = 0; if (pload_msg.op != PLOAD_MSG_OP_SET_STEPS) continue ; /* start the generator */ if ((pload_flags & PLOAD_FLAG_IS_STARTED) == 0) { #if 0 /* DEBUG */ SERIAL_WRITE_STRING("starting\r\n"); #endif /* DEBUG */ pload_step_index = 0; pload_tick_count = (uint32_t)pload_msg.u.steps.arg1[0]; pload_repeat_index = 0; pload_current = (int32_t)pload_msg.u.steps.arg0[0]; dac_set(ma_to_dac((uint32_t)pload_current)); #if 0 /* DEBUG */ SERIAL_WRITE_STRING("dac_set:"); serial_write(uint32_to_string((uint32_t)pload_current), 8); SERIAL_WRITE_STRING("\r\n"); #endif /* DEBUG */ pload_flags |= PLOAD_FLAG_IS_STARTED; pit_start(1, F_BUS / PLOAD_CLOCK_FREQ); } } return 0; }