static hal_result_t s_cmd_set_curr_tare(uint8_t *data_in, uint8_t *data_out, uint8_t *len_data_out) // CAN_CMD_SET_CURR_TARE { hal_result_t res = hal_res_OK; int16_t sender_status; adc_state_t adc_st; //****************************************************************************** //********************* IMPORTANTE*********************************************** //ferma la spediziobne di messaggi perchè la current tare è coinvolta nei calcoli sender_status = sender_is_running(); sender_stop(); switch(data_in[1]) { case 0: { calc_current_tare_init(); } break; case 1: { /* In this case I must stop adc because new tare values depend on adc values.*/ adc_st = adc_is_running(); adc_stop(); calc_current_tare_reset(); if(adc_st_started == adc_st) { adc_start(); } } break; case 2: { res = calc_current_tare_set_value(data_in[2], (data_in[3]<<8 | data_in[4])); } break; #warning -> acemor: nei default si devono mettere i break. default: { res = hal_res_NOK_wrongparam; } break; } if(sender_status) { sender_start(); } if(hal_res_OK != res) { return(hal_res_NOK_wrongparam); } *len_data_out = 0; return(res); }
void __attribute__((interrupt, no_auto_psv)) _DMA0Interrupt(void) { if(DmaBuffer == 0) { /* ProcessADCSamples(&BufferA[0][0]); ProcessADCSamples(&BufferA[1][0]); //ProcessADCSamples(&BufferA[2][0]); // we don't use AN2 ProcessADCSamples(&BufferA[3][0]); ProcessADCSamples(&BufferA[4][0]); ProcessADCSamples(&BufferA[5][0]); ProcessADCSamples(&BufferA[6][0]); ProcessADCSamples(&BufferA[7][0]); */ //printf("\r\n%u %u %u %u\r\n", BufferA[23][0], BufferA[23][1], BufferA[23][2], BufferA[23][3]); } else { /* ProcessADCSamples(&BufferB[0][0]); ProcessADCSamples(&BufferB[1][0]); //ProcessADCSamples(&BufferB[2][0]); // we don't use AN2 ProcessADCSamples(&BufferB[3][0]); ProcessADCSamples(&BufferB[4][0]); ProcessADCSamples(&BufferB[5][0]); ProcessADCSamples(&BufferB[6][0]); ProcessADCSamples(&BufferB[7][0]); */ adc_stop(); // we converted everything * 8 * 2buffers every 8ms } DmaBuffer ^= 1; IFS0bits.DMA0IF = 0; // Clear the DMA0 Interrupt Flag }
ADC_SET_CALLBACK(nxp, adc, callback, data) { if (callback == NULL) { adc_stop(adc); } adc->callback = callback; adc->data = data; return 0; }
/** * Enable/disable external power supply and start measurements **/ void setSensorStatus(void) { if (config.enable) { SENS_PORT |= _BV(SENS_PIN); adc_start(); windspeed_start(); led_on(); } else { SENS_PORT &= ~_BV(SENS_PIN); adc_stop(); windspeed_stop(); led_blink(); } }
static hal_result_t s_cmd_set_tx_mode(uint8_t *data_in, uint8_t *data_out, uint8_t *len_data_out) // CAN_CMD_SET_TXMODE: // set continuous or on demand tx 0x205 len 2 data 7 0/1 { sender_stop(); adc_stop(); switch(data_in[1]) { case 0://transmit calibrated data { CFG_6SG_BEHAV.tx_outMsg_mode = tx_outMsg_torqueData_on; s_tx_mode_check(); adc_start(); sender_start(); } break; case 1: //do acquisition, but do not transmit { CFG_6SG_BEHAV.tx_outMsg_mode = tx_outMsg_readOnly; adc_start(); } break; case 2: //debug mode { CFG_6SG_BEHAV.tx_outMsg_mode = tx_outMsg_off; } break; case 3: //TODO: transmit not calibrated data { CFG_6SG_BEHAV.tx_outMsg_mode = tx_outMsg_uncalibData_on; sendAmsg = 1; sendBmsg = 1; adc_start(); sender_start(); } break; case 4: //TODO: transmit calibrated and not calibrated { } break; } *len_data_out = 0; return(hal_res_OK); }
static void show_voltage(void) { uint32_t voltage; adc_init(); voltage = adc_get(AIN_BATTERY); adc_stop(); voltage = div_round(1100l * 1024, voltage); digits[0] = numbers[voltage / 10 / 10 / 10 % 10] | LCD_DOT; digits[1] = numbers[voltage / 10 / 10 % 10]; digits[2] = numbers[voltage / 10 % 10]; digits[3] = numbers[voltage % 10]; if (usb_enabled) digits[3] |= LCD_DOT; }
/************************************************************************* * Function Name: ADC_Inter_Handler * Parameters: none * * Return: none * * Description: ADC interrupt handler * *************************************************************************/ void ADC_Inter_Handler () { adc_stop(); touch_scr_detect_touch(Touch_data.Y); //update_touch_coordinates(); ADdata.I_current = ADDR3_bit.RESULT; ADdata.previous_raw_measurement = ADdata.current_raw_measurement; ADdata.current_raw_measurement = ADDR2_bit.RESULT; // ADdata.current_filtered_measurement = (Int32U)ADfilter(ADdata.current_raw_measurement); // ADdata.current_filtered_measurement = ADfilter(ADdata.current_raw_measurement); // Data = ADdata.current_raw_measurement; // Data = ADdata.current_filtered_measurement; ADdata.previous_filtered_measurement = ADdata.current_filtered_measurement; ADdata.current_filtered_measurement = ADfilter(ADdata.current_raw_measurement); Data = (Int32U)ADdata.current_filtered_measurement; //update_instance_p_p_values(ADdata.current_raw_measurement); V_RMS_2_accumulator += pow(Bits_2_Grid_Voltage(ADdata.current_raw_measurement),2.0); I_RMS_2_accumulator += pow((ADdata.I_current-512.)*I_scale,2.0); P_RMS_2_accumulator += (((ADdata.current_raw_measurement-512.)*V_scale_PP)*((ADdata.I_current-512.)*I_scale_PP)); //V_RMS_2_accumulator += pow(Data, 2.0); // V_RMS_2_accumulator += pow(ADdata.current_raw_measurement,2.0); // FIO0PIN_bit.P0_11 = 0; // clear interrupt VICADDRESS = 0; }
void adc_recalibrate(int sequencer_base) { /* To Recalibrate ADC Step: 1. Backup CMD register, because some value will be overwritten 2. Stop the ADC Sequencer Core. 3. Set the recalibration request bits 2. Start the ADC Sequencer Core. 5. Poll for RUN bit to be clear 6. Restore CMD register */ alt_u32 backup_cmd_reg; backup_cmd_reg = IORD_ALTERA_MODULAR_ADC_SEQUENCER_CMD_REG(sequencer_base); adc_stop(sequencer_base); ALTERA_MODULAR_ADC_SEQUENCER_MODE_RUN_RECALIBRATION(sequencer_base); adc_start(sequencer_base); while(IORD_ALTERA_MODULAR_ADC_SEQUENCER_CMD_REG(sequencer_base) & ALTERA_MODULAR_ADC_SEQUENCER_CMD_RUN_MSK); IOWR_ALTERA_MODULAR_ADC_SEQUENCER_CMD_REG(sequencer_base, backup_cmd_reg); }
static hal_result_t s_cmd_set_calib_tare(uint8_t *data_in, uint8_t *data_out, uint8_t *len_data_out) // CAN_CMD_SET_CALIB_TARE: { uint8_t i; int16_t ch_values[AN_CHANNEL_NUM]; hal_result_t res = hal_res_OK; int16_t sender_status; adc_state_t adc_st; sender_status = sender_is_running(); sender_stop(); switch(data_in[1]) { case 0: { memset( CFG_6SG_EEDATA.calibration_tare, 0, (sizeof(uint16_t)*AN_CHANNEL_NUM) ); } break; case 1: { adc_st =adc_is_running(); adc_stop(); adc_get_data(ch_values); ; if(adc_st_started == adc_st) { adc_start(); } for (i=0; i<AN_CHANNEL_NUM; i++) { CFG_6SG_EEDATA.calibration_tare[i]= -(ch_values[i]); } } break; case 2: { if(data_in[2] >= AN_CHANNEL_NUM) { res= hal_res_NOK_wrongparam; break; } CFG_6SG_EEDATA.calibration_tare[data_in[2]] = (data_in[3]<<8) | data_in[4]; } break; default: { res= hal_res_NOK_wrongparam; } break; } if(sender_status) { sender_start(); } if(hal_res_OK != res) { parser_data.ee_data_is_saved = 0; } *len_data_out = 0; return(res); }
static hal_result_t s_parse_can_loaderMsg(uint8_t *data_in, uint8_t *data_out, uint8_t *len_data_out) { uint8_t num_msg; static uint8_t addinfo_part=0; hal_can_frame_t frame; hal_result_t res = hal_res_OK; srcCode_version_info_t* src_ver_ptr = &(parser_data.SIXsg_config_ptr->gen_ee_data.verinfo); switch (data_in[0]) { #warning -> acemor: qui al CMD_BROADCAST si risponde con tre bytes: version, release, build mentre nel bootloader con due. ??? case CMD_BROADCAST: { CFG_6SG_BEHAV.tx_outMsg_mode = tx_outMsg_off; sender_stop(); adc_stop(); data_out[0] = CMD_BROADCAST; data_out[1] = BOARD_TYPE_6SG; data_out[2] = src_ver_ptr->exe_file.major; //Firmware version number for BOOTLOADER data_out[3] = src_ver_ptr->exe_file.minor; data_out[4] = 0; // data_out[4] = src_ver_ptr->exe_file.build; //Firmware build number. #warning VALE: --> cosa metto nel ver build??? *len_data_out = 5; } break; case CMD_BOARD: { hal_sys_systemreset(); //asm ("reset"); // Jump to bootloader code } break; case CMD_GET_ADDITIONAL_INFO: { frame.id = CAN_MSG_CLASS_LOADER | ( CFG_GEN_EEDATA.board_address << 4 ) | (0);; frame.id_type = hal_can_frameID_std; frame.frame_type = hal_can_frame_data; frame.size = 6; #warning -> acemor: cosa e' la additional info? sta nella parte rw comune sia a bootloader che ad applicazione? frame.data[0] = CMD_GET_ADDITIONAL_INFO; //since additional_info's length is 32 bytes and in each message write 4 bytes of additional_info, // i have to send 8 message for (num_msg = 0; num_msg< 8; num_msg++) { frame.data[1] = num_msg; memcpy(&frame.data[2], &CFG_6SG_EEDATA.additional_info[num_msg*4], 4); res = hal_can_put(hal_can_port1, &frame, hal_can_send_normprio_now); while(hal_res_NOK_busy == res) { res = hal_can_put(hal_can_port1, &frame, hal_can_send_normprio_now); } } *len_data_out = 255; } break; case CMD_SET_ADDITIONAL_INFO: { addinfo_part = data_in[1]; memcpy(&(CFG_6SG_EEDATA.additional_info[addinfo_part*4]), &data_in[2], 4); if (7 == addinfo_part) { addinfo_part=0; SIXsg_config_save_to_eeprom(parser_data.SIXsg_config_ptr); } else { addinfo_part++; } *len_data_out = 0; } break; default: { return(hal_res_NOK_wrongparam); } break; } //end switch return(res); }
static void twi_callback(uint8_t buffer_size, volatile uint8_t input_buffer_length, const volatile uint8_t *input_buffer, uint8_t volatile *output_buffer_length, volatile uint8_t *output_buffer) { uint8_t input; uint8_t command; uint8_t io; if(input_buffer_length < 1) return(build_reply(output_buffer_length, output_buffer, 0, 1, 0, 0)); input = input_buffer[0]; command = input & 0xf8; io = input & 0x07; switch(command) { case(0x00): // short / no-io { switch(io) { case(0x00): // identify { static const uint8_t replystring[] = { 0x4a, 0xfb, 0x06, 0x01, 0x00, 't', '8', '6', '1', 'a' }; return(build_reply(output_buffer_length, output_buffer, input, 0, sizeof(replystring), replystring)); } case(0x01): // 0x02 read ADC { uint8_t value; value = ADCW; if(ADCSRA & _BV(ADSC)) // conversion not ready return(build_reply(output_buffer_length, output_buffer, input, 5, 0, 0)); adc_stop(); uint8_t replystring[2]; replystring[0] = (value & 0xff00) >> 8; replystring[1] = (value & 0x00ff) >> 0; return(build_reply(output_buffer_length, output_buffer, input, 0, sizeof(replystring), replystring)); } case(0x02): // 0x02 DEBUG read timer0 counter { uint8_t value = timer0_get_counter(); return(build_reply(output_buffer_length, output_buffer, input, 0, 1, &value)); } case(0x03): // 0x03 DEBUG read timer1 counter { uint16_t value = pwm_timer1_get_counter(); uint8_t rv[2]; rv[0] = (value & 0xff00) >> 8; rv[1] = (value & 0x00ff) >> 0; return(build_reply(output_buffer_length, output_buffer, input, 0, sizeof(rv), rv)); } case(0x04): // 0x04 DEBUG read timer1 max { uint16_t value = pwm_timer1_get_max(); uint8_t rv[2]; rv[0] = (value & 0xff00) >> 8; rv[1] = (value & 0x00ff) >> 0; return(build_reply(output_buffer_length, output_buffer, input, 0, sizeof(rv), rv)); } case(0x05): // 0x05 read timer1 prescaler { uint8_t value = pwm_timer1_status(); return(build_reply(output_buffer_length, output_buffer, input, 0, sizeof(value), &value)); } case(0x06): // 0x06 DEBUG read timer0 entry / exit counter values { uint8_t value[2]; value[0] = timer0_debug_1; value[1] = timer0_debug_2; return(build_reply(output_buffer_length, output_buffer, input, 0, sizeof(value), value)); } case(0x07): // extended command { return(build_reply(output_buffer_length, output_buffer, input, 7, 0, 0)); } default: { return(build_reply(output_buffer_length, output_buffer, input, 7, 0, 0)); } } break; } case(0x10): // 0x10 read counter case(0x20): // 0x20 read / reset counter { if(io >= COUNTER_PORTS) return(build_reply(output_buffer_length, output_buffer, input, 3, 0, 0)); uint32_t counter = counters_meta[io].counter; if(command == 0x20) counters_meta[io].counter = 0; uint8_t replystring[4]; replystring[0] = (counter & 0xff000000) >> 24; replystring[1] = (counter & 0x00ff0000) >> 16; replystring[2] = (counter & 0x0000ff00) >> 8; replystring[3] = (counter & 0x000000ff) >> 0; return(build_reply(output_buffer_length, output_buffer, input, 0, sizeof(replystring), replystring)); } case(0x30): // read input { uint8_t value; if(io >= INPUT_PORTS) return(build_reply(output_buffer_length, output_buffer, input, 3, 0, 0)); value = !!(*input_ports[io].port & (1 << input_ports[io].bit)); return(build_reply(output_buffer_length, output_buffer, input, 0, 1, &value)); } case(0x40): // write output / softpwm { if(input_buffer_length < 2) return(build_reply(output_buffer_length, output_buffer, input, 4, 0, 0)); if(io >= OUTPUT_PORTS) return(build_reply(output_buffer_length, output_buffer, input, 3, 0, 0)); softpwm_meta[io].duty = input_buffer[1]; update_static_softpwm_ports(); timer0_start(); return(build_reply(output_buffer_length, output_buffer, input, 0, sizeof(softpwm_meta), (uint8_t *)softpwm_meta)); } case(0x50): // read output / softpwm { if(io >= OUTPUT_PORTS) return(build_reply(output_buffer_length, output_buffer, input, 3, 0, 0)); duty = softpwm_meta[io].duty; return(build_reply(output_buffer_length, output_buffer, input, 0, sizeof(duty), &duty)); } case(0x60): // write softpwm mode { if(input_buffer_length < 2) return(build_reply(output_buffer_length, output_buffer, input, 4, 0, 0)); if(io >= OUTPUT_PORTS) return(build_reply(output_buffer_length, output_buffer, input, 3, 0, 0)); uint8_t mode = input_buffer[1]; if(mode > 3) return(build_reply(output_buffer_length, output_buffer, input, 3, 0, 0)); softpwm_meta[io].pwm_mode = input_buffer[1]; return(build_reply(output_buffer_length, output_buffer, input, 0, sizeof(softpwm_meta), (uint8_t *)softpwm_meta)); } case(0x70): // read softpwm mode { if(io >= OUTPUT_PORTS) return(build_reply(output_buffer_length, output_buffer, input, 3, 0, 0)); uint8_t mode; mode = softpwm_meta[io].pwm_mode; return(build_reply(output_buffer_length, output_buffer, input, 0, sizeof(mode), &mode)); } case(0x80): // write pwm { if(input_buffer_length < 3) return(build_reply(output_buffer_length, output_buffer, input, 4, 0, 0)); if(io >= PWM_PORTS) return(build_reply(output_buffer_length, output_buffer, input, 3, 0, 0)); uint16_t value; value = input_buffer[1]; value <<= 8; value |= input_buffer[2]; pwm_timer1_set_pwm(io, value); return(build_reply(output_buffer_length, output_buffer, input, 0, 0, 0)); } case(0x90): // read pwm { if(io >= PWM_PORTS) return(build_reply(output_buffer_length, output_buffer, input, 3, 0, 0)); uint16_t value = pwm_timer1_get_pwm(io); uint8_t reply[2]; reply[0] = (value & 0xff00) >> 8; reply[1] = (value & 0x00ff) >> 0; return(build_reply(output_buffer_length, output_buffer, input, 0, sizeof(reply), reply)); } case(0xa0): // write pwm mode { if(input_buffer_length < 2) return(build_reply(output_buffer_length, output_buffer, input, 4, 0, 0)); if(io >= PWM_PORTS) return(build_reply(output_buffer_length, output_buffer, input, 3, 0, 0)); uint8_t mode = input_buffer[1]; if(mode > 3) return(build_reply(output_buffer_length, output_buffer, input, 3, 0, 0)); pwm_meta[io].pwm_mode = input_buffer[1]; return(build_reply(output_buffer_length, output_buffer, input, 0, sizeof(pwm_meta), (uint8_t *)pwm_meta)); } case(0xb0): // read pwm mode { if(io >= PWM_PORTS) return(build_reply(output_buffer_length, output_buffer, input, 3, 0, 0)); uint8_t mode; mode = pwm_meta[io].pwm_mode; return(build_reply(output_buffer_length, output_buffer, input, 0, sizeof(mode), &mode)); } case(0xc0): // start adc conversion { if(io > 1) return(build_reply(output_buffer_length, output_buffer, input, 3, 0, 0)); adc_start(io); return(build_reply(output_buffer_length, output_buffer, input, 0, 0, 0)); } case(0xf0): // twi stats { uint8_t replystring[2]; uint16_t stats; switch(io) { case(0x00): // disable { usi_twi_enable_stats(0); return(build_reply(output_buffer_length, output_buffer, input, 0, 0, 0)); } case(0x01): // enable { usi_twi_enable_stats(1); return(build_reply(output_buffer_length, output_buffer, input, 0, 0, 0)); } case(0x02): // read start conditions { stats = usi_twi_stats_start_conditions(); break; } case(0x03): // read stop conditions { stats = usi_twi_stats_stop_conditions(); break; } case(0x04): // read error conditions { stats = usi_twi_stats_error_conditions(); break; } case(0x05): // read overflow conditions { stats = usi_twi_stats_overflow_conditions(); break; } case(0x06): // read local frames { stats = usi_twi_stats_local_frames(); break; } case(0x07): // read idle calls { stats = usi_twi_stats_idle_calls(); break; } } replystring[0] = (stats & 0xff00) >> 8; replystring[1] = (stats & 0x00ff) >> 0; return(build_reply(output_buffer_length, output_buffer, input, 0, sizeof(replystring), replystring)); } default: { return(build_reply(output_buffer_length, output_buffer, input, 2, 0, 0)); } } return(build_reply(output_buffer_length, output_buffer, input, 2, 0, 0)); }