status_code_t nvm_read_char(mem_type_t mem, uint32_t address, uint8_t *data) { switch (mem) { case INT_FLASH: *data = nvm_flash_read_byte((flash_addr_t)address); break; case INT_USERPAGE: nvm_user_sig_read_buffer((flash_addr_t)address, (void *)data, 1); break; case INT_EEPROM: *data = nvm_eeprom_read_byte((eeprom_addr_t)address); break; #if defined(USE_EXTMEM) && defined(CONF_BOARD_AT45DBX) case AT45DBX: if (!at45dbx_read_byte_open(address)) { return ERR_BAD_ADDRESS; } *data = at45dbx_read_byte(); at45dbx_read_close(); break; #endif default: return ERR_INVALID_ARG; } return STATUS_OK; }
/** * Check if an EEPROM page is equal to a memory buffer */ static bool is_eeprom_page_equal_to_buffer(uint8_t page_addr, uint8_t *buffer) { uint8_t i; for (i = 0; i < EEPROM_PAGE_SIZE; i++) { if (nvm_eeprom_read_byte(page_addr * EEPROM_PAGE_SIZE + i) != buffer[i]) { return false; } } return true; }
int main(void) { board_init(); sysclk_init(); adc_init(); //Offset is stored in EEPROM int adc_offset = 0; if (nvm_eeprom_read_byte(EEPROM_ADDR_ID) == EEPROM_ID){ adc_offset = nvm_eeprom_read_byte(EEPROM_ADDR_OFFSET_POS) - nvm_eeprom_read_byte(EEPROM_ADDR_OFFSET_NEG); } PORTE_DIRSET = MASK_DIGIT012; PORTD_DIRSET = 0xFF; int32_t i = 0; uint8_t disp = 0; uint8_t adccnt = 0; unsigned int adc_readings[AVG_READINGS]; //Setup ADC hardware adc_init(); //Main loop...measure VCC-INT while(1){ // Vref = 2.048V // 4096 count ADC (12-bit) // 2.048V / 4096count = 0.0005 V/Count // We want i to be result in mV // = 0.5 mV/count //So divide count by 2 to get mV reading if (adccnt < AVG_READINGS){ adc_readings[adccnt] = adc_get_unsigned_result(&MY_ADC, MY_ADC_CH); if (adc_readings[adccnt] < 0){ adc_readings[adccnt] = 0; } adccnt++; } else { int32_t adctemp = 0; for (adccnt = 0; adccnt < AVG_READINGS; adccnt++){ adctemp += (int32_t)(adc_readings[adccnt] + adc_offset); } i = adctemp / AVG_READINGS; //Limit negative values to 0 //i = i - 2048; if (i < 0) i = 0; i = i / 2; adccnt = 0; } //Switch between mV and V ranges if (i > 999){ if (disp == 0){ display((i / 10)%10, 0, 2); _delay_ms(2); } else if (disp == 1){ display((i / 100) % 10, 0, 1); _delay_ms(2); } else { display(i / 1000, 1, 0); _delay_ms(2); } } else { if (disp == 0){ display(i % 10, 0, 2); _delay_ms(2); } else if (disp == 1){ display((i / 10) % 10, 0, 1); _delay_ms(2); } else { display(i / 100, 0, 0); _delay_ms(2); } } disp++; if (disp > 2){ disp = 0; } adc_start_conversion(&MY_ADC, MY_ADC_CH); } }
void menu_init(void) { verbose = nvm_eeprom_read_byte(EE_verbose); }
// Supporting function implementation Motors::Motors() { // Set mode mode = ABSOLUTE; // Set current values currentX = NAN; currentY = NAN; currentZ = NAN; currentE = NAN; currentF = NAN; // Check if last Z value was recorded if(nvm_eeprom_read_byte(EEPROM_SAVED_Z_STATE_OFFSET)) // Set current Z to last recorded Z value nvm_eeprom_read_buffer(EEPROM_LAST_RECORDED_Z_VALUE_OFFSET, ¤tZ, EEPROM_LAST_RECORDED_Z_VALUE_LENGTH); // Otherwise else // Set current Z to not a number currentZ = NAN; // Configure motors ioport_set_pin_dir(MOTORS_ENABLE_PIN, IOPORT_DIR_OUTPUT); ioport_set_pin_level(MOTORS_ENABLE_PIN, IOPORT_PIN_LEVEL_HIGH); ioport_set_pin_dir(MOTORS_STEP_CONTROL_PIN, IOPORT_DIR_OUTPUT); ioport_set_pin_level(MOTORS_STEP_CONTROL_PIN, IOPORT_PIN_LEVEL_LOW); // Configure motor X Vref, direction, and step ioport_set_pin_dir(MOTOR_X_VREF_PIN, IOPORT_DIR_OUTPUT); pwm_init(&motorXVrefPwm, MOTOR_X_VREF_PWM_TIMER, MOTOR_X_VREF_PWM_CHANNEL, 5000); pwm_start(&motorXVrefPwm, 0); ioport_set_pin_dir(MOTOR_X_DIRECTION_PIN, IOPORT_DIR_OUTPUT); ioport_set_pin_level(MOTOR_X_DIRECTION_PIN, IOPORT_PIN_LEVEL_HIGH); ioport_set_pin_dir(MOTOR_X_STEP_PIN, IOPORT_DIR_OUTPUT); pwm_config motorXStepPwm; pwm_init(&motorXStepPwm, MOTOR_X_STEP_PWM_TIMER, MOTOR_X_STEP_PWM_CHANNEL, 5000); pwm_start(&motorXStepPwm, 50); // Configure motor Y Vref, direction, and step ioport_set_pin_dir(MOTOR_Y_VREF_PIN, IOPORT_DIR_OUTPUT); pwm_init(&motorYVrefPwm, MOTOR_Y_VREF_PWM_TIMER, MOTOR_Y_VREF_PWM_CHANNEL, 5000); pwm_start(&motorYVrefPwm, 0); ioport_set_pin_dir(MOTOR_Y_DIRECTION_PIN, IOPORT_DIR_OUTPUT); ioport_set_pin_level(MOTOR_Y_DIRECTION_PIN, IOPORT_PIN_LEVEL_LOW); ioport_set_pin_dir(MOTOR_Y_STEP_PIN, IOPORT_DIR_OUTPUT); pwm_config motorYStepPwm; pwm_init(&motorYStepPwm, MOTOR_Y_STEP_PWM_TIMER, MOTOR_Y_STEP_PWM_CHANNEL, 5000); pwm_start(&motorYStepPwm, 50); // Configure motor Z VREF, direction, and step ioport_set_pin_dir(MOTOR_Z_VREF_PIN, IOPORT_DIR_OUTPUT); pwm_init(&motorZVrefPwm, MOTOR_Z_VREF_PWM_TIMER, MOTOR_Z_VREF_PWM_CHANNEL, 5000); pwm_start(&motorZVrefPwm, 0); ioport_set_pin_dir(MOTOR_Z_DIRECTION_PIN, IOPORT_DIR_OUTPUT); ioport_set_pin_level(MOTOR_Z_DIRECTION_PIN, IOPORT_PIN_LEVEL_LOW); ioport_set_pin_dir(MOTOR_Z_STEP_PIN, IOPORT_DIR_OUTPUT); pwm_config motorZStepPwm; pwm_init(&motorZStepPwm, MOTOR_Z_STEP_PWM_TIMER, MOTOR_Z_STEP_PWM_CHANNEL, 5000); pwm_start(&motorZStepPwm, 50); // Configure motor E VREF, direction, step, and AISEN ioport_set_pin_dir(MOTOR_E_VREF_PIN, IOPORT_DIR_OUTPUT); pwm_init(&motorEVrefPwm, MOTOR_E_VREF_PWM_TIMER, MOTOR_E_VREF_PWM_CHANNEL, 5000); pwm_start(&motorEVrefPwm, 0); ioport_set_pin_dir(MOTOR_E_DIRECTION_PIN, IOPORT_DIR_OUTPUT); ioport_set_pin_level(MOTOR_E_DIRECTION_PIN, IOPORT_PIN_LEVEL_LOW); ioport_set_pin_dir(MOTOR_E_STEP_PIN, IOPORT_DIR_OUTPUT); pwm_config motorEStepPwm; pwm_init(&motorEStepPwm, MOTOR_E_STEP_PWM_TIMER, MOTOR_E_STEP_PWM_CHANNEL, 5000); pwm_start(&motorEStepPwm, 50); ioport_set_pin_dir(MOTOR_E_AISEN_PIN, IOPORT_DIR_INPUT); ioport_set_pin_mode(MOTOR_E_AISEN_PIN, IOPORT_MODE_PULLDOWN); }