void DataFlash_Block::WriteBlock(const void *pBuffer, uint16_t size) { if (!CardInserted() || !log_write_started || !_writes_enabled) { return; } while (size > 0) { uint16_t n = df_PageSize - df_BufferIdx; if (n > size) { n = size; } if (df_BufferIdx == 0) { // if we are at the start of a page we need to insert a // page header if (n > df_PageSize - sizeof(struct PageHeader)) { n = df_PageSize - sizeof(struct PageHeader); } struct PageHeader ph = { df_FileNumber, df_FilePage }; BlockWrite(df_BufferNum, df_BufferIdx, &ph, sizeof(ph), pBuffer, n); df_BufferIdx += n + sizeof(ph); } else { BlockWrite(df_BufferNum, df_BufferIdx, NULL, 0, pBuffer, n); df_BufferIdx += n; } size -= n; pBuffer = (const void *)(n + (uintptr_t)pBuffer); if (df_BufferIdx == df_PageSize) { FinishWrite(); df_FilePage++; } } }
/* show information about the device */ void DataFlash_Block::ShowDeviceInfo(AP_HAL::BetterStream *port) { if (!CardInserted()) { port->println_P(PSTR("No dataflash inserted")); return; } ReadManufacturerID(); port->printf_P(PSTR("Manufacturer: 0x%02x Device: 0x%04x\n"), (unsigned)df_manufacturer, (unsigned)df_device); port->printf_P(PSTR("NumPages: %u PageSize: %u\n"), (unsigned)df_NumPages+1, (unsigned)df_PageSize); }
bool DataFlash_Block::WritePrioritisedBlock(const void *pBuffer, uint16_t size, bool is_critical) { // is_critical is ignored - we're a ring buffer and never run out // of space. possibly if we do more complicated bandwidth // limiting we can reservice bandwidth based on is_critical if (!CardInserted() || !log_write_started || !_writes_enabled) { return false; } if (! WriteBlockCheckStartupMessages()) { return false; } while (size > 0) { uint16_t n = df_PageSize - df_BufferIdx; if (n > size) { n = size; } if (df_BufferIdx == 0) { // if we are at the start of a page we need to insert a // page header if (n > df_PageSize - sizeof(struct PageHeader)) { n = df_PageSize - sizeof(struct PageHeader); } struct PageHeader ph = { df_FileNumber, df_FilePage }; BlockWrite(df_BufferNum, df_BufferIdx, &ph, sizeof(ph), pBuffer, n); df_BufferIdx += n + sizeof(ph); } else { BlockWrite(df_BufferNum, df_BufferIdx, NULL, 0, pBuffer, n); df_BufferIdx += n; } size -= n; pBuffer = (const void *)(n + (uintptr_t)pBuffer); if (df_BufferIdx == df_PageSize) { FinishWrite(); df_FilePage++; } } return true; }
int main(void) { FIL log_file; int i, j, result; int file_number = 0; u8 configNumber = 1; u8 maxConfig; u8 *p; time_t rawtime; u8 debugState = 0; //debug off #ifdef DEBUG debug(); #endif RCC_Configuration(); GPIO_Configuration(); NVIC_Configuration(); SystickConfigure(); Timer3Init(); #ifdef UART1_DEBUG (void)USART1_Init(115200); #endif LedConfigureGPIO(); ButtonConfigureGPIO(); BuzzerConfigureGPIO(); #ifdef CAN_FEATURE_ENABLED GPIO_InitTypeDef GPIO_InitStructure; // Configure CAN pin: RX GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(GPIOA, &GPIO_InitStructure); // Configure CAN pin: TX GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); #endif MMC_PowerOff(); timer2=1000; while(timer2); MMC_PowerOn(); #ifdef UART1_DEBUG printf("\n\n%s %s\n", HW_VERSION, SW_VERSION); #endif ConfigureClock(); timer2=1000; while(timer2); buttonState = 0; CardInserted(); if (FR_OK != f_mount(0, &Fatfs[0])) { LedSetColor(amber, fast); BuzzerSetMode(0x83); #ifdef UART1_DEBUG printf("error mounting fat device\n"); #endif while(buttonState != 2); NVIC_GenerateSystemReset(); } if (ReadTimeDateFile()) { #ifdef UART1_DEBUG rawtime = RTC_GetCounter(); printf("Time set: %s\n", ctime (&rawtime) ); #endif } #ifdef DEBUG_LOG if (ReadDebugFile()) { debugState = 1; } #endif #ifdef CAN_FEATURE_ENABLED if (ReadCanFile()) { canDiag = 1; CAN_Open(); } #endif i = ReadSpeedFile(); if (i > 0) { KW1281_SPEED = i; autobaud = 0; } if (ReadDelayFile()) { user_defined_timings = 1; } if (!ReadConfig()) { LedSetColor(amber, fast); BuzzerSetMode(0x83); #ifdef UART1_DEBUG printf("error reading config\n"); #endif MMC_PowerOff(); while(buttonState != 2); NVIC_GenerateSystemReset(); } if (config[0][0] > 0) { maxConfig = 1; if (config[1][0] > 0) { maxConfig = 2; if (config[2][0] > 0) { maxConfig = 3; } } } else { LedSetColor(amber, fast); BuzzerSetMode(0x83); #ifdef UART1_DEBUG printf("error reading config\n"); #endif MMC_PowerOff(); while(buttonState != 2); NVIC_GenerateSystemReset(); } file_number = GetNextFileNumber(); #ifdef UART1_DEBUG printf("file number %d\n", file_number); #endif if (!file_number) { LedSetColor(amber, fast); BuzzerSetMode(0x83); #ifdef UART1_DEBUG printf("can't get number of next file\n"); #endif MMC_PowerOff(); while(buttonState != 2); NVIC_GenerateSystemReset(); } while(1) { i = USART2_GetData(); buttonState = 0; LedSetColor(configNumber, continous); BuzzerSetMode(configNumber); do { if (2 == buttonState) { configNumber = (configNumber < maxConfig)?configNumber+1:1; LedSetColor(configNumber, continous); BuzzerSetMode(configNumber); buttonState = 0; } } while (1 != buttonState); buttonState = 0; LedSetColor(green, fast); BuzzerSetMode(0x81); i = CreateLogFile(file_number, &log_file); i = f_printf(&log_file, "%s, sw_ver: %s\n", HW_VERSION, SW_VERSION); rawtime = RTC_GetCounter(); i = f_printf(&log_file, "Current time: %s\n", ctime (&rawtime)); timeSec = 0; time10MSec = 0; if (canDiag) { i = f_printf(&log_file, "CAN 500kbit\n\n"); LedSetColor(configNumber, slow); BuzzerSetMode(configNumber); result = vwtp(&config[configNumber-1], &log_file, debugState); switch (result) { case 0: timer2 = 500; i = f_printf(&log_file, "\n\nLogging terminated by user\n"); break; case 1: LedSetColor(red, fast); BuzzerSetMode(0x82); //communication error timer2 = 2000; i = f_printf(&log_file, "\n\nConnection lost\n"); break; case 2: i = CloseLogFile(&log_file); LedSetColor(amber, fast); BuzzerSetMode(0x83); //filesystem error MMC_PowerOff(); while(buttonState != 2); NVIC_GenerateSystemReset(); break; case 11: LedSetColor(red, fast); BuzzerSetMode(0x82); //communication error timer2 = 2000; i = f_printf(&log_file, "\n\nCannot connect with ECU\n", result); break; default: LedSetColor(red, fast); BuzzerSetMode(0x82); //communication error timer2 = 2000; i = f_printf(&log_file, "\n\nCommunication error, error code = %d\n", result); break; } } else //KW1281 diag { i = kw1281_max_init_attempts; do { result = ISO9141Init(&KW1281_SPEED); --i; if ((result>0) && (i>0)) { timer2=1000; while(timer2); } } while (result && i); if (0 == result) //connected with ECU { i = f_printf(&log_file, "Connected @ %d baud %s%s\n\n", KW1281_SPEED, autobaud?"(autodetected)":"(set by user)", user_defined_timings?", user defined timings":""); LedSetColor(configNumber, slow); BuzzerSetMode(configNumber); buttonState = 0; i = kw1281_diag(&config[configNumber-1], &log_file, debugState); switch (i) { case 0: timer2 = 500; i = f_printf(&log_file, "\n\nLogging terminated by user\n"); break; case 1: LedSetColor(red, fast); BuzzerSetMode(0x82); //communication error timer2 = 2000; i = f_printf(&log_file, "\n\nConnection lost\n"); break; case 2: default: i = CloseLogFile(&log_file); LedSetColor(amber, fast); BuzzerSetMode(0x83); //filesystem error MMC_PowerOff(); while(buttonState != 2); NVIC_GenerateSystemReset(); } } if (0 != result) { i = f_printf(&log_file, "Cannot connect with ECU: "); if (0xff == result) { i = f_printf(&log_file, "no response\n"); } else if (0x7f == result) { i = f_printf(&log_file, "sync error - wrong speed (set to %d baud)?!\n", KW1281_SPEED); } else if (0x10 == result) { i = f_printf(&log_file, "not a KW1281 protocol\n"); } else { i = f_printf(&log_file, "error code %d\n", result); } LedSetColor(red, fast); BuzzerSetMode(0x82); timer2 = 2000; } } // end of KW1281 diag i = CloseLogFile(&log_file); file_number++; while (timer2>0); } }