wxString cbReadFileContents(wxFile& file, wxFontEncoding encoding) { wxString st; cbRead(file, st, encoding); return st; }
int main(void) { // Initiera SPI som slav SPI_SLAVE_init(); // Initiera UART UART_init(); // Sätt på interrupts sei(); // Skapa meddelandebuffert för meddelanden som ska skickas till styrenheten CircularBuffer messageQueue; cbInit(&messageQueue, 32); uint8_t spiMsg[32]; uint8_t spiType; uint8_t spiLen; uint8_t spiMsgR[32]; uint8_t spiTypeR; uint8_t spiLenR; uint8_t uartMsg[16]; uint8_t uartType; uint8_t uartLen; uint8_t stopped = 0; while(1){ // Kolla om det har kommit ett helt meddelande från PCn if(UART_readMessage(uartMsg,&uartType,&uartLen)){ // Kolla om det är ett nödstoppsanrop if(uartType == TYPE_EMERGENCY_STOP){ SETBIT(PORTB, PORTB3); stopped=1; }else{ // Annars, lägg hela meddelandet i meddelandebuffern cbWrite(&messageQueue, (uartType<<5)|uartLen); for(uint8_t i=0; i < uartLen; i++) { cbWrite(&messageQueue, uartMsg[i]); } } if(uartType == TYPE_CHANGE_PARM && stopped==1) { CLEARBIT(PORTB, PORTB3); stopped=0; } } // Kolla om vi har tagit emot ett helt meddelande från styrenheten if(SPI_SLAVE_read(spiMsg, &spiType, &spiLen)) { if(spiType == TYPE_REQUEST_PC_MESSAGE) { // Om det är en efterfrågan av PC-meddelanden, skicka det första meddelandet i buffern, om det finns något if(cbBytesUsed(&messageQueue)!=0) { uint8_t head = cbPeek(&messageQueue); uint8_t len = head&0b00011111; uint8_t type = 0b11100000&head; type = type>>5; if(len < cbBytesUsed(&messageQueue) && cbBytesUsed(&messageQueue) != 0) { head = cbRead(&messageQueue); // Läs headern igen for(uint8_t i = 0; i < len; i++) { spiMsgR[i]=cbRead(&messageQueue); } SPI_SLAVE_write(spiMsgR, type, len); } else { SPI_SLAVE_write(spiMsgR, TYPE_NO_PC_MESSAGES, 0); } } else { SPI_SLAVE_write(spiMsgR, TYPE_NO_PC_MESSAGES, 0); } }else if(spiType == TYPE_MAP_DATA)
DWORD CNdasEventSubscriber:: Run() { _ASSERTE(NULL != m_hDataEvent); _ASSERTE(INVALID_HANDLE_VALUE != m_hThread); _ASSERTE(NULL != m_hThreadStopEvent); // Why? // _ASSERTE(m_hThread == ::GetCurrentThread()); OVERLAPPED ov = {0}; ov.hEvent = m_hDataEvent; BOOL bStopThread = FALSE; HANDLE hPipe = WaitServer(bStopThread); while (!bStopThread) { NDAS_EVENT_MESSAGE message = {0}; DWORD cbMessage = sizeof(NDAS_EVENT_MESSAGE); DWORD cbRead(0); BOOL fRead = ::ReadFile( hPipe, &message, cbMessage, &cbRead, &ov); if (!fRead && ERROR_IO_PENDING != ::GetLastError()) { // failure! DPErrorEx(_FT("ReadFile failed: ")); ::CloseHandle(hPipe); hPipe = WaitServer(bStopThread); continue; } if (fRead) { ::SetEvent(m_hDataEvent); } HANDLE hWaitHandles[2] = { m_hThreadStopEvent, m_hDataEvent }; DWORD dwWaitResult = ::WaitForMultipleObjects( 2, hWaitHandles, FALSE, INFINITE); if (WAIT_OBJECT_0 == dwWaitResult) { // // Thread stop event // bStopThread = TRUE; } else if (WAIT_OBJECT_0 + 1 == dwWaitResult) { DWORD cbRead(0); BOOL fData = ::GetOverlappedResult(hPipe, &ov, &cbRead, TRUE); NDAS_EVENT_INFO eventInfo = {0}; eventInfo.EventType = message.EventType; switch (eventInfo.EventType) { case NDAS_EVENT_TYPE_DEVICE_STATUS_CHANGED: case NDAS_EVENT_TYPE_DEVICE_PROPERTY_CHANGED: eventInfo.DeviceInfo = message.DeviceEventInfo; break; case NDAS_EVENT_TYPE_UNITDEVICE_PROPERTY_CHANGED: eventInfo.UnitDeviceInfo = message.UnitDeviceEventInfo; break; case NDAS_EVENT_TYPE_LOGICALDEVICE_STATUS_CHANGED: case NDAS_EVENT_TYPE_LOGICALDEVICE_DISCONNECTED: case NDAS_EVENT_TYPE_LOGICALDEVICE_RECONNECTING: case NDAS_EVENT_TYPE_LOGICALDEVICE_RECONNECTED: case NDAS_EVENT_TYPE_LOGICALDEVICE_EMERGENCY: case NDAS_EVENT_TYPE_LOGICALDEVICE_ALARMED: eventInfo.LogicalDeviceInfo = message.LogicalDeviceEventInfo; break; case NDAS_EVENT_TYPE_LOGICALDEVICE_ENTRY_CHANGED: case NDAS_EVENT_TYPE_DEVICE_ENTRY_CHANGED: case NDAS_EVENT_TYPE_TERMINATING: break; case NDAS_EVENT_TYPE_SURRENDER_REQUEST: eventInfo.SurrenderRequestInfo = message.SurrenderRequestInfo; break; case NDAS_EVENT_TYPE_PERIODIC: default: break; } if (NDAS_EVENT_TYPE_PERIODIC != message.EventType) { CallEventProc(::GetLastError(), &eventInfo); } } else { DPWarningEx(_FT("Wait failed: ")); } } if (INVALID_HANDLE_VALUE != hPipe) { ::CloseHandle(hPipe); } return 0; }
static HANDLE CreatePipeConnection(LPOVERLAPPED lpOverlapped) { HANDLE hPipe = ::CreateFile( NDAS_EVENT_PIPE_NAME, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL); if (INVALID_HANDLE_VALUE == hPipe) { DPErrorEx(_FT("Connecting to %s failed: "), NDAS_EVENT_PIPE_NAME); return INVALID_HANDLE_VALUE; } // // We should read the event version info immediately // NDAS_EVENT_MESSAGE message = {0}; DWORD cbMessage = sizeof(NDAS_EVENT_MESSAGE); DWORD cbRead(0); BOOL fSuccess = ::ReadFile( hPipe, &message, cbMessage, &cbRead, lpOverlapped); if (!fSuccess && ERROR_IO_PENDING != ::GetLastError()) { DPErrorEx(_FT("Retrieving version information failed on ReadFile: ")); ::CloseHandle(hPipe); return INVALID_HANDLE_VALUE; } if (fSuccess) { fSuccess = ::SetEvent(lpOverlapped->hEvent); _ASSERTE(fSuccess); } // // If we cannot retrieve the version information // in a timeout interval or the version information // is mismatch, an error is returned // const DWORD dwTimeout = 3000; // 3 sec timeout DWORD dwWaitResult = ::WaitForSingleObject( lpOverlapped->hEvent, dwTimeout); if (dwWaitResult == WAIT_TIMEOUT) { DPError(_FT("Retrieving version information timed out.")); ::CloseHandle(hPipe); return INVALID_HANDLE_VALUE; } if (dwWaitResult != WAIT_OBJECT_0) { DPErrorEx(_FT("Retrieving version information failed: ")); ::CloseHandle(hPipe); return INVALID_HANDLE_VALUE; } fSuccess = ::GetOverlappedResult( hPipe, lpOverlapped, &cbRead, TRUE); if (!fSuccess) { DPErrorEx(_FT("Getting overlapped result failed: ")); ::CloseHandle(hPipe); return INVALID_HANDLE_VALUE; } if (NDAS_EVENT_TYPE_VERSION_INFO != message.EventType) { DPError(_FT("Getting Event Version Info failed: ") _T("Type expected %d, received %d.\n"), NDAS_EVENT_TYPE_VERSION_INFO, message.EventType); ::CloseHandle(hPipe); ::SetLastError(NDASUSER_ERROR_EVENT_VERSION_MISMATCH); return INVALID_HANDLE_VALUE; } if (NDAS_EVENT_VERSION_MAJOR != message.VersionInfo.MajorVersion || NDAS_EVENT_VERSION_MINOR != message.VersionInfo.MinorVersion) { DPErrorEx(_FT("Event version mismatch: ") _T("Version expected %d.%d, received %d.%d"), NDAS_EVENT_VERSION_MAJOR, NDAS_EVENT_VERSION_MINOR, message.VersionInfo.MajorVersion, message.VersionInfo.MinorVersion); ::CloseHandle(hPipe); ::SetLastError(NDASUSER_ERROR_EVENT_VERSION_MISMATCH); return INVALID_HANDLE_VALUE; } return hPipe; }
int main(void) { char buffer[20]; int i = 0; unsigned int favg[MAX_FG_DEVICES] = {0}; unsigned int count[MAX_FG_DEVICES] = {0}; unsigned int min_count[MAX_FG_DEVICES]; unsigned int old[MAX_FG_DEVICES] = {0}; struct param_set pset; /* init min_count */ for(i = 0; i < MAX_FG_DEVICES; i++) min_count[i] = BUFFER_SIZE-2; discoverPeriphery(); scub_base = (unsigned short*)find_device_adr(GSI, SCU_BUS_MASTER); BASE_ONEWIRE = (unsigned int*)find_device_adr(CERN, WR_1Wire); scub_irq_base = (unsigned int*)find_device_adr(GSI, SCU_IRQ_CTRL); //lm32_irq_endp = (unsigned int*)find_device_adr(GSI, IRQ_ENDPOINT); disp_reset(); disp_put_c('\f'); init(); mprintf("scub_irq_base is: 0x%x\n", scub_irq_base); //mprintf("irq_endp is: 0x%x\n", lm32_irq_endp); while(1); //while(1) { // mprintf("global_msi: 0x%x\n", &global_msi); // _irq_entry(); //} /* wait until buffers are filled */ for(i = 0; i < MAX_FG_DEVICES; i++) { while(cbgetCount((struct circ_buffer *)&fg_buffer, i) < BUFFER_SIZE - 5) usleep(10000); } while(1) { //updateTemps(); for(i = 0; i < MAX_FG_DEVICES; i++) { if (!cbisEmpty((struct circ_buffer *)&fg_buffer, i)) { cbRead((struct circ_buffer *)&fg_buffer, i, &pset); if((pset.coeff_c % 1000) == 0) { mprintf("cb[%d]: fcnt: %d fmin: %d favg: %d coeff_a: %x coeff_b: 0x%x coeff_c: 0x%x\n", i, count[i], min_count[i], favg[i]/1000, pset.coeff_a, pset.coeff_b, pset.coeff_c); favg[i] = 0; } if (old[i] + 1 != pset.coeff_c) { mprintf("cb[%d]: buffer value not consistent old: %x coeff_c: %x\n", i, old[i], pset.coeff_c); cbDump((struct circ_buffer *)&fg_buffer, i); return(1); } old[i] = pset.coeff_c; } count[i] = cbgetCount((struct circ_buffer *)&fg_buffer, i); favg[i] += count[i]; if (count[i] < min_count[i]) min_count[i] = count[i]; if (count[i] == 0) { mprintf("cb[%d] fcnt: %d\n", i, count[i]); cbDump((struct circ_buffer *)&fg_buffer, i); return(1); } } usleep(800); //placeholder for fg software //if (fg_control) { // init(); // fg_control = 0; //} } return(0); }