static void tlv5618_write(Tlv5618 *ctx, uint16_t val) { TLV5618_CSLOW(ctx->cs_pin); kfile_putc(val >> 8, ctx->ch); kfile_putc(val & 0xFF, ctx->ch); kfile_flush(ctx->ch); TLV5618_CSHIGH(ctx->cs_pin); }
/** * Close file \a fd. * This is a generic implementation that only flush the file. */ int kfile_genericClose(struct KFile *fd) { return kfile_flush(fd); }
/** * Reopen file \a fd. * This is a generic implementation that only flush file * and reset seek_pos to 0. */ struct KFile * kfile_genericReopen(struct KFile *fd) { kfile_flush(fd); kfile_seek(fd, 0, KSM_SEEK_SET); return fd; }
int main(void) { // SD fat filesystem context FATFS fs; // Context files that we would manage FatFile ini_file; FatFile log_file; init(); LOG_INFO("SD fat example project %s: %d times\n", VERS_HOST, VERS_BUILD); while (1) { bool sd_ok = true; FRESULT result; // Setting info INISetting ini_set; memset(&ini_set, 0, sizeof(ini_set)); sd_ok = sd_init(&spi_dma.fd); if (sd_ok) { LOG_INFO("Mount FAT filesystem.\n"); if ((result = f_mount(0, &fs)) != FR_OK) { LOG_ERR("Mounting FAT volumes error[%d]\n", result); sd_ok = false; } LOG_INFO("Read setting from ini file: %s\n", INI_FILE_NAME); if (sd_ok && ((result = fatfile_open(&ini_file, INI_FILE_NAME, FA_READ)) != FR_OK)) { LOG_ERR("Could not open ini file: %s error[%d,]\n", INI_FILE_NAME, result); sd_ok = false; } if (sd_ok) { /* * If sd is ok, we read all setting from INI file. * NOTE: if one ini key or section was not found into INI * file, the iniparser return the defaul value. */ ini_getString(&ini_file.fd, LOG_SECTION, LOG_NAME_KEY, "default.log", ini_set.name, sizeof(ini_set.name)); LOG_INFO("Log file name [%s]\n", ini_set.name); char tmp[25]; ini_getString(&ini_file.fd, LOG_SECTION, LOG_ON_SERIAL, "1", tmp, sizeof(tmp)); ini_set.log_on_serial = atoi(tmp); LOG_INFO("Log serial [%d]\n", ini_set.log_on_serial); ini_getString(&ini_file.fd, LOG_SECTION, LOG_ON_FILE, "1", tmp, sizeof(tmp)); ini_set.log_on_file = atoi(tmp); LOG_INFO("Log sd [%d]\n", ini_set.log_on_file); ini_getString(&ini_file.fd, LOG_SECTION, LOG_SAMPLE_TIME, "500", tmp, sizeof(tmp)); ini_set.sample_time = atoi(tmp); LOG_INFO("Sample time [%ld]\n", ini_set.sample_time); ini_getString(&ini_file.fd, SERIAL_LOG, SERIAL_PORT, "0", tmp, sizeof(tmp)); ini_set.port = atoi(tmp); LOG_INFO("Serial port [%d]\n", ini_set.port); ini_getString(&ini_file.fd, SERIAL_LOG, SERIAL_BAUD, "115200", tmp, sizeof(tmp)); ini_set.baud = atoi(tmp); LOG_INFO("Serial buad [%d]\n", ini_set.baud); ini_getString(&ini_file.fd, LOG_FORMAT_SEC, FORMAT_LINE_HEADER, "BeRTOS: ", ini_set.line_header, sizeof(ini_set.line_header)); LOG_INFO("Serial line header[%s]\n", ini_set.line_header); ini_getString(&ini_file.fd, LOG_FORMAT_SEC, FORMAT_FIELD_SEP, ",", ini_set.field_sep, sizeof(ini_set.field_sep)); LOG_INFO("Serial char sep[%s]\n", ini_set.field_sep); ini_getString(&ini_file.fd, TEMPERATURE, TEMP_UNIT_LABEL, "C", ini_set.temp_unit_label, sizeof(ini_set.temp_unit_label)); LOG_INFO("Temp unit label[%s]\n", ini_set.temp_unit_label); ini_getString(&ini_file.fd, PRESSURE, PRESS_UNIT_LABEL, "hPa", ini_set.press_unit_label, sizeof(ini_set.press_unit_label)); LOG_INFO("Press unit label[%s]\n", ini_set.press_unit_label); ini_getString(&ini_file.fd, VOLTAGE, VOLTAGE_UNIT_LABEL, "V", ini_set.voltage_unit_label, sizeof(ini_set.voltage_unit_label)); LOG_INFO("Press unit label[%s]\n", ini_set.voltage_unit_label); } } if (ini_set.log_on_serial) { // Init serial log port ser_init(&log_port, ini_set.port); ser_setbaudrate(&log_port, ini_set.baud); LOG_INFO("SERIAL init..port[%d] buad[%d]\n", ini_set.port, ini_set.baud); } char log_string[160]; memset(log_string, 0, sizeof(log_string)); // Supplay voltage uint16_t vdd = ADC_RANGECONV(adc_read(SUPPLAY_VOLTAGE_CH), 0, ADC_SUPPLAY_VOLTAGE); // Read temperature int16_t tmp = tmp123_read(&temp_sensor_bus.fd); // Read pressure uint16_t vout = ADC_RANGECONV(adc_read(PRESSURE_SENSOR_CH), 0, vdd); int16_t press = mpxx6115a_press(vout, vdd); /* * Format string whit read data * line_header + temp + temp_unit_label + field_sep + press + press_unit_label + field_sep + vdd + voltage_unit_label */ int wr_len = sprintf(log_string, "%s%d.%01d%s%s%d%s%s%d.%d%s\r\n", ini_set.line_header, tmp / 10, ABS(tmp % 10), ini_set.temp_unit_label, ini_set.field_sep, press, ini_set.press_unit_label, ini_set.field_sep, vdd / 1000, ABS(vdd % 1000), ini_set.voltage_unit_label); /* * if SD is ok, try to open log file and write our data and, only * if by configuration we have enable the log on file */ if (sd_ok && ini_set.log_on_file) { // Open log file and do not overwrite the previous log file result = fatfile_open(&log_file, ini_set.name, FA_OPEN_EXISTING | FA_WRITE); // If the log file there isn't we create the new one if (result == FR_NO_FILE) { result = fatfile_open(&log_file, ini_set.name, FA_CREATE_NEW | FA_WRITE); LOG_INFO("Create the log file: %s\n", ini_set.name); } if ( result == FR_OK) { LOG_INFO("Opened log file '%s' size %ld\n", ini_set.name, log_file.fat_file.fsize); // To append data we should go to end of file before to start to write kfile_seek(&log_file.fd, 0, KSM_SEEK_END); int len = kfile_write(&log_file.fd, log_string, wr_len); // Flush data and close the files. kfile_flush(&log_file.fd); kfile_close(&log_file.fd); // Unmount always to prevent accidental sd remove. f_mount(0, NULL); LOG_INFO("Wrote [%d]\n", len); } else { LOG_ERR("Unable to open file: '%s' error[%d]\n", ini_set.name, result); } } // If by configuration we have enable the log on serial, we log it if (ini_set.log_on_serial) kfile_write(&log_port.fd, log_string, wr_len); timer_delay(ini_set.sample_time); } return 0; }
/* * AT+MODE=[0|1|2] */ static bool cmd_switch_mode(Serial* pSer, char* value, size_t len){ bool modeOK = false; if(len > 0 ){ int i = atoi(value); if(i == (int)currentMode && i == g_settings.run_mode){ // already in this mode, bail out. return true; } modeOK = true; switch(i){ case MODE_CFG: // Enter COMMAND/CONFIG MODE currentMode = MODE_CFG; g_ax25.pass_through = 0; // parse ax25 frames ser_purge(pSer); // clear all rx/tx buffer SERIAL_PRINT_P(pSer,PSTR("Enter Config mode\r\n")); break; #if MOD_KISS case MODE_KISS: // Enter KISS MODE currentMode = MODE_KISS; g_ax25.pass_through = 1; // don't parse ax25 frames ser_purge(pSer); // clear serial rx/tx buffer SERIAL_PRINT_P(pSer,PSTR("Enter KISS mode\r\n")); break; #endif #if MOD_TRACKER case MODE_TRACKER: currentMode = MODE_TRACKER; kfile_printf_P((KFile*)pSer,PSTR("Enter Tracker mode\r\n")); kfile_flush((KFile*)pSer); ser_purge(pSer); // should enable the tracker/gps tracker_init_gps(); break; #endif #if MOD_DIGI case MODE_DIGI: // DIGI MODE currentMode = MODE_DIGI; g_ax25.pass_through = 0; // need parse ax25 frames SERIAL_PRINT_P(pSer,PSTR("Enter Digi mode\r\n")); break; #endif default: // unknown mode modeOK = false; break; } // end of switch // save to settings/run_mode if(modeOK){ if(currentMode != g_settings.run_mode){ settings_set_params(SETTINGS_RUN_MODE,¤tMode,1); settings_save(); } }else{ SERIAL_PRINTF_P(pSer,PSTR("Invalid mode %s, [0|1|2|3] is accepted\r\n"),value); } }else{ // no parameters, just dump the mode SERIAL_PRINTF_P(pSer,PSTR("Current mode %d/%d\r\n"),currentMode,g_settings.run_mode); } return true; }