/** EPP periodic task of ESC side EEPROM emulation. * */ void EEP_process (void) { eep_stat_t stat; /* check for eeprom event */ if ((ESCvar.ALevent & ESCREG_ALEVENT_EEP) == 0) { return; } while (1) { /* read eeprom status */ ESC_read (ESCREG_EECONTSTAT, &stat, sizeof (eep_stat_t)); stat.contstat.reg = etohs(stat.contstat.reg); stat.addr = etohl(stat.addr); /* check busy flag, exit if job finished */ if (!stat.contstat.bits.busy) { return; } /* clear error bits */ stat.contstat.bits.csumErr = 0; stat.contstat.bits.eeLoading = 0; stat.contstat.bits.ackErr = 0; stat.contstat.bits.wrErr = 0; /* process commands */ switch (stat.contstat.bits.cmdReg) { case EEP_CMD_IDLE: break; case EEP_CMD_READ: case EEP_CMD_RELOAD: /* handle read request */ if (EEP_read (stat.addr * sizeof(uint16_t), eep_buf, EEP_READ_SIZE) != 0) { stat.contstat.bits.ackErr = 1; } else { ESC_write (ESCREG_EEDATA, eep_buf, EEP_READ_SIZE); } break; case EEP_CMD_WRITE: /* handle write request */ ESC_read (ESCREG_EEDATA, eep_buf, EEP_WRITE_SIZE); if (EEP_write (stat.addr * sizeof(uint16_t), eep_buf, EEP_WRITE_SIZE) != 0) { stat.contstat.bits.ackErr = 1; } break; default: stat.contstat.bits.ackErr = 1; } /* acknowledge command */ stat.contstat.reg = htoes(stat.contstat.reg); ESC_write (ESCREG_EECONTSTAT, &stat.contstat.reg, sizeof(uint16_t)); } }
static PT_THREAD( LOG_log(pt_t* pt) ) { u32 time; u8 is_filtered; u8 i; PT_BEGIN(pt); // systematically unlock the channel // because most of time no response is sent // when a response is needed, the channel will be locked DPT_unlock(&LOG.interf); switch ( LOG.state ) { case LOG_OFF: default: // empty the log fifo (void)FIFO_get(&LOG.in_fifo, &LOG.fr); // loop back for next frame PT_RESTART(pt); break; case LOG_RAM: #ifdef SAVE_IN_RAM_ENABLED #endif break; case LOG_EEPROM: // if address is out of range if ( LOG.eeprom_addr >= EEPROM_END_ADDR ) { // logging is no more possible // so quit PT_EXIT(pt); } break; case LOG_SDCARD: // if address is out of range if ( LOG.sdcard_addr >= SDCARD_END_ADDR ) { // logging is no more possible // so quit PT_EXIT(pt); } break; } // wait while no frame is present in the fifo PT_WAIT_WHILE(pt, KO == FIFO_get(&LOG.in_fifo, &LOG.fr)); // if it is a log command if ( (LOG.fr.cmde == FR_LOG_CMD) && (!LOG.fr.resp) ) { // treat it LOG_command(&LOG.fr); // send the response DPT_lock(&LOG.interf); PT_WAIT_UNTIL(pt, OK == DPT_tx(&LOG.interf, &LOG.fr)); DPT_unlock(&LOG.interf); // and wait till the next frame PT_RESTART(pt); } // filter the frame according to its origin is_filtered = OK; // by default, every frame is filtered for ( i = 0; i < sizeof(LOG.orig_filter); i++ ) { // passthrough or frame origin and filter acceptance match if ( (LOG.orig_filter[i] == 0x00) || (LOG.orig_filter[i] == LOG.fr.orig) ){ is_filtered = KO; break; } } // if frame is filtered away if ( is_filtered ) { // lop back for next frame PT_RESTART(pt); } // build the log packet LOG.block.index = LOG.index; time = TIME_get(); LOG.block.time[0] = (u8)(time >> 16); LOG.block.time[1] = (u8)(time >> 8); LOG.block.fr = LOG.fr; switch ( LOG.state ) { case LOG_OFF: default: // shall never happen but just in case // loop back for next frame PT_RESTART(pt); break; case LOG_RAM: #ifdef SAVE_IN_RAM_ENABLED LOG.ram_buffer[LOG.ram_index] = LOG.block; if ( LOG.ram_index < (RAM_BUFFER_SIZE - 1) ) { LOG.ram_index++; } #endif break; case LOG_EEPROM: // save it to eeprom PT_WAIT_UNTIL(pt, EEP_write(LOG.eeprom_addr, (u8*)&LOG.block, sizeof(log_t))); // wait until saving is done PT_WAIT_UNTIL(pt, EEP_is_fini()); break; case LOG_SDCARD: // save it to sdcard (fill the write buffer) PT_WAIT_UNTIL(pt, SD_write(LOG.sdcard_addr, (u8*)&LOG.block, sizeof(log_t))); // wait until saving is done break; } // loop back to treat the next frame to log PT_RESTART(pt); PT_END(pt); }