/*----------------------------------------------------------------------------*/ int32_t psio_select(struct mtcp_thread_context *ctxt) { struct psio_private_context *ppc; mtcp_manager_t mtcp; struct timeval cur_ts; int i, ret; ppc = (struct psio_private_context *) ctxt->io_private_context; mtcp = ctxt->mtcp_manager; gettimeofday(&cur_ts, NULL); if (!ppc->rx_avail || ppc->event.tx_nids) { for (i = 0; i < CONFIG.eths_num; i++) { if (ppc->w_chunk_buf[i].cnt > 0) NID_SET(i, ppc->event.tx_nids); if (mtcp->n_sender[i]->control_list_cnt > 0 || mtcp->n_sender[i]->send_list_cnt > 0 || mtcp->n_sender[i]->ack_list_cnt > 0) { if (cur_ts.tv_sec > ppc->last_tx_set[i].tv_sec || cur_ts.tv_usec > ppc->last_tx_set[i].tv_usec) { NID_SET(i, ppc->event.tx_nids); ppc->last_tx_set[i] = cur_ts; } } } TRACE_SELECT("BEFORE: rx_avail: %d, tx_avail: %d, event.rx_nids: %0x, event.tx_nids: %0x\n", ppc->rx_avail, ppc->tx_avail, ppc->event.rx_nids, ppc->event.tx_nids); mtcp->is_sleeping = TRUE; ret = ps_select(&ppc->handle, &ppc->event); mtcp->is_sleeping = FALSE; #if TIME_STAT gettimeofday(&select_ts, NULL); UpdateStatCounter(&mtcp->rtstat.select, TimeDiffUs(&select_ts, &xmit_ts)); #endif if (ret < 0) { if (errno != EAGAIN && errno != EINTR) { perror("ps_select"); exit(EXIT_FAILURE); } if (errno == EINTR) { STAT_COUNT(mtcp->runstat.rounds_select_intr); } } else { TRACE_SELECT("ps_select(): event.rx_nids: %0x, event.tx_nids: %0x\n", ppc->event.rx_nids, ppc->event.tx_nids); if (ppc->event.rx_nids != 0) { STAT_COUNT(mtcp->runstat.rounds_select_rx); } if (ppc->event.tx_nids != 0) { for (i = 0; i < CONFIG.eths_num; i++) { if (NID_ISSET(i, ppc->event.tx_nids)) { NID_SET(i, ppc->tx_avail); } } STAT_COUNT(mtcp->runstat.rounds_select_tx); } } TRACE_SELECT("AFTER: rx_avail: %d, tx_avail: %d, event.rx_nids: %d, event.tx_nids: %d\n", ppc->rx_avail, ppc->tx_avail, ppc->event.rx_nids, ppc->event.tx_nids); STAT_COUNT(mtcp->runstat.rounds_select); } /* reset psio parameters */ ppc->event.timeout = PS_SELECT_TIMEOUT; NID_ZERO(ppc->event.rx_nids); NID_ZERO(ppc->event.tx_nids); NID_ZERO(ppc->rx_avail); //NID_ZERO(ppc->tx_avail); return 0; }
int main(void) { int a = 0; unsigned char data_mix = 0, data_mix_old = 0; SDEV_TYPE1 S1 = {0}, *S1_p = &S1; char comm_buffer[MAXSTRLEN]; int update_rate = 4; int eresult = 0, records = 0, file_errors = 0; // Init remote device data S1.obits.out_byte = 0xff; // Enable multi-vectored interrupts INTEnableSystemMultiVectoredInt(); //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //STEP 1. Configure cache, wait states and peripheral bus clock // Configure the device for maximum performance but do not change the PBDIV // Given the options, this function will change the flash wait states, RAM // wait state and enable prefetch cache but will not change the PBDIV. // The PBDIV value is already set via the pragma FPBDIV option above.. SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE); srand(ReadCoreTimer()); // Seed the pseudo random generator // SCLK1 pin 25 - SD pin 5 PORTSetPinsDigitalOut(IOPORT_B, BIT_14); // Clock output // SCLK2 pin 26 - PIC pin 18 PORTSetPinsDigitalOut(IOPORT_B, BIT_15); // Clock output PPSInput(2, SDI1, RPB8); //Assign SDI1 to pin 17 - SD pin 7 PORTSetPinsDigitalIn(IOPORT_B, BIT_8); // Input for SDI1 PPSInput(3, SDI2, RPB13); //Assign SDI2 to pin 24 PORTSetPinsDigitalIn(IOPORT_B, BIT_13); // Input for SDI2 PPSOutput(2, RPB11, SDO1); // Set 22 pin as output for SDO1 - SD pin 2 PPSOutput(2, RPB5, SDO2); // Set 14 pin as output for SDO2 PORTSetPinsDigitalOut(IOPORT_B, BIT_2); // CS line pin 6 - SD pin 1 mPORTBSetBits(BIT_2); // deselect SDCARD PORTSetPinsDigitalOut(IOPORT_B, BIT_3); // select pin enable for SPI slaves bit 2 to 74hc138 mPORTBSetBits(BIT_3); // deselect Slave1 bit2 PORTSetPinsDigitalOut(IOPORT_A, BIT_3); // spare digital output mPORTASetBits(BIT_3); // //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Diag Led pins PORTSetPinsDigitalOut(IOPORT_A, BIT_0 | BIT_1); // bi-color LED PORTSetPinsDigitalOut(IOPORT_B, BIT_0 | BIT_1); // programming inputs /device select outputs address for SPI slaves bits [0..1] 74hc138 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // STEP 3. initialize the port pin states = outputs low mPORTASetBits(BIT_0 | BIT_1); mPORTBClearBits(BIT_0 | BIT_1); RTC_Init(); // Start Timer5 for background processes and 1ms soft timers Blink_Init(); // Start Timer4 background blink driver blink_led(RED_LED, LED_ON, FALSE); init_spi_ports(); ps_select(0); // select the pic18 slave1 on spi port 2, select 0 // send some text to clean the buffers SpiStringWrite("\r\n \n\r"); eresult = f_mount(&FatFs, "0:", 1); if (eresult) { sprintf(comm_buffer, "\r\n Mount error %i ", eresult); SpiStringWrite(comm_buffer); /* Register work area to the logical drive 1 */ blink_led(GREEN_LED, LED_OFF, FALSE); blink_led(RED_LED, LED_ON, FALSE); } else { Show_MMC_Info(); blink_led(RED_LED, LED_OFF, FALSE); blink_led(GREEN_LED, LED_ON, TRUE); } /* Create destination file on the drive 1 */ eresult = f_open(&File[0], "0:logfile.txt", FA_CREATE_ALWAYS | FA_WRITE); while (1) { // loop and move data V.Timer1 = update_rate; while (V.Timer1); /* loop back testing */ data_mix_old = data_mix; // save the old data data_mix = rand(); // make new data if (S1_p->char_ready || !S1_p->ibits.in_bits.eject) { S1_p->rec_data = S1_p->rec_tmp; // led1_green(); } else { // led1_off(); } S1_p->ibits.in_byte = SpiPortWrite(S1_p->obits.out_byte); if (S1_p->ibits.in_bits.card_detect) { S1_p->obits.out_bits.eject_led = OFF; SD_NOTRDY = STA_NOINIT; } else { S1_p->obits.out_bits.eject_led = ~S1_p->obits.out_bits.eject_led; } for (S1_p->chan_no = 0; S1_p->chan_no < 4; S1_p->chan_no++) { S1_p->adc_data[S1_p->chan_no] = SpiADCRead(S1_p->chan_no); } a = 0; if (((records % 100)) == 0 && !eresult) { if (S1_p->char_ready) { snprintf(comm_buffer, 64, "\r\n %x , %i, %x , %i , %i , %i , %i ", S1_p->rec_data, records, S1_p->ibits.in_byte, S1_p->adc_data[0], S1_p->adc_data[1], S1_p->adc_data[2], S1_p->adc_data[3]); } else { snprintf(comm_buffer, 64, "\r\n %x , %i , %i , %i , %i , %i ", S1_p->ibits.in_byte, records, S1_p->adc_data[0], S1_p->adc_data[1], S1_p->adc_data[2], S1_p->adc_data[3]); } S1_p->rec_tmp = SpiStringWrite(comm_buffer); } if (SpiSerialReadOk() || !S1_p->ibits.in_bits.eject) { S1_p->char_ready = TRUE; if ((S1_p->rec_tmp == 'f') || !S1_p->ibits.in_bits.eject) { if (S1_p->ibits.in_bits.card_detect) { // no disk blink_led(0, LED_ON, FALSE); S1_p->obits.out_bits.eject_led = OFF; S1_p->ibits.in_byte = SpiPortWrite(S1_p->obits.out_byte); SpiStringWrite("\r\n Insert Disk! y/n "); while (!SpiSerialReadReady() && S1_p->ibits.in_bits.card_detect) S1_p->ibits.in_byte = SpiPortWrite(S1_p->obits.out_byte); V.Timer1 = update_rate; while (V.Timer1); if (!S1_p->ibits.in_bits.card_detect) { SpiStringWrite("/\r\n Mounting Disk "); S1_p->obits.out_bits.eject_led = ON; S1_p->ibits.in_byte = SpiPortWrite(S1_p->obits.out_byte); eresult = f_mount(&FatFs, "0:", 1); if (eresult) { sprintf(comm_buffer, "\r\n Mount error %i ", eresult); SpiStringWrite(comm_buffer); /* Register work area to the logical drive 1 */ blink_led(GREEN_LED, LED_OFF, FALSE); blink_led(RED_LED, LED_ON, FALSE); } else { Show_MMC_Info(); blink_led(RED_LED, LED_OFF, FALSE); blink_led(GREEN_LED, LED_ON, TRUE); } /* Create destination file on the drive 1 */ eresult = f_open(&File[0], "0:logfile.txt", FA_CREATE_ALWAYS | FA_WRITE); SpiStringWrite("\r\n Mount Complete "); S1_p->ibits.in_byte = SpiPortWrite(S1_p->obits.out_byte); if (S1_p->ibits.in_bits.card_detect) SD_NOTRDY = STA_NOINIT; } } else { // Eject current disk blink_led(0, LED_ON, FALSE); S1_p->obits.out_bits.eject_led = OFF; S1_p->ibits.in_byte = SpiPortWrite(S1_p->obits.out_byte); f_close(&File[0]); SpiStringWrite("\r\n Eject Disk? y/n "); while (!SpiSerialReadReady() && !S1_p->ibits.in_bits.card_detect) S1_p->ibits.in_byte = SpiPortWrite(S1_p->obits.out_byte); V.Timer1 = update_rate; while (V.Timer1); if (S1_p->ibits.in_bits.card_detect) { SpiStringWrite("\r\n Ejecting Disk "); S1_p->obits.out_bits.eject_led = ON; S1_p->ibits.in_byte = SpiPortWrite(S1_p->obits.out_byte); if (f_mount(0, "0:", 0)) SpiStringWrite("\r\n UMount error "); /* Unregister work area from the logical drive 1 */ SpiStringWrite("\r\n Ejection Complete "); eresult = 1; S1_p->ibits.in_byte = SpiPortWrite(S1_p->obits.out_byte); if (S1_p->ibits.in_bits.card_detect) SD_NOTRDY = STA_NOINIT; } } } } else { S1_p->char_ready = FALSE; } V.Timer1 = update_rate; while (V.Timer1); // only GREEN if the data sent and received match or eject button is pressed if (S1_p->ibits.in_bits.eject && (S1_p->rec_data != data_mix_old)) { // blink_led(0, LED_ON, TRUE); } else { // blink_led(0, LED_ON, FALSE); } if (!eresult) { a = f_puts(comm_buffer, &File[0]); blink_led(RED_LED, LED_OFF, FALSE); blink_led(GREEN_LED, LED_ON, TRUE); records++; } else { file_errors++; if ((file_errors % 100) == 0) { SpiStringWrite("\r\n not logged "); blink_led(GREEN_LED, LED_OFF, FALSE); blink_led(RED_LED, LED_ON, FALSE); } } if (a == (-1)) { if ((file_errors % 100) == 0) { sprintf(comm_buffer, "\r\n File Write error %i ", a); SpiStringWrite(comm_buffer); blink_led(GREEN_LED, LED_OFF, FALSE); blink_led(RED_LED, LED_ON, FALSE); } } f_sync(&File[0]); } }