/** Initialize key ADC detection Initialize key ADC detection @param void @return void */ void KeyADC_Init(void) { if (adc_open(KEY_ADC_CH_1) != E_OK) { debug_err(("Can't open ADC for key group 1\r\n")); return; } if (adc_open(KEY_ADC_CH_2) != E_OK) { debug_err(("Can't open ADC for key group 2\r\n")); return; } // VolDet_Init() already enable ADC and set extra sampling count, sampling rate // and average. // Set channel divider for key channel to 18, key will detect per 9.728 ms // Real sampling rate will be 1953.125 / (channel divider + 1) = 102.796 Hz adc_setChDivider(KEY_ADC_CH_1, 18); // Set mode for key group 1 detection adc_setMode(KEY_ADC_CH_1, TRUE/*continuous mode*/, FALSE/*disable interrupt*/, NULL); // Set mode for key group 2 detection adc_setMode(KEY_ADC_CH_2, TRUE/*continuous mode*/, FALSE/*disable interrupt*/, NULL); // Enable adc control logic adc_enable(); //adc_triggerOneShot(KEY_ADC_CH_1); //adc_triggerOneShot(KEY_ADC_CH_2); }
void IO_InitADC(void) { // Key ADC channel, channel 2 if (adc_open(ADC_CH_KEYDET_KEY1) != E_OK) { DBG_ERR("Can't open ADC channel for key key1 detection\r\n"); return; } //650 Range is 250K Hz ~ 2M Hz adc_setConfig(ADC_CONFIG_ID_OCLK_FREQ, 250000); //250K Hz //key key1 detection adc_setChConfig(ADC_CH_KEYDET_KEY1, ADC_CH_CONFIG_ID_SAMPLE_FREQ, 10000); //10K Hz, sample once about 100 us for CONTINUOUS mode adc_setChConfig(ADC_CH_KEYDET_KEY1, ADC_CH_CONFIG_ID_SAMPLE_MODE, (VOLDET_ADC_MODE) ? ADC_CH_SAMPLEMODE_CONTINUOUS : ADC_CH_SAMPLEMODE_ONESHOT); adc_setChConfig(ADC_CH_KEYDET_KEY1, ADC_CH_CONFIG_ID_INTEN, FALSE); // Battery channel, channel 0 if (adc_open(ADC_CH_VOLDET_BATTERY) != E_OK) { DBG_ERR("Can't open ADC channel for battery voltage detection\r\n"); return; } //battery voltage detection adc_setChConfig(ADC_CH_VOLDET_BATTERY, ADC_CH_CONFIG_ID_SAMPLE_FREQ, 10000); //10K Hz, sample once about 100 us for CONTINUOUS mode adc_setChConfig(ADC_CH_VOLDET_BATTERY, ADC_CH_CONFIG_ID_SAMPLE_MODE, (VOLDET_ADC_MODE) ? ADC_CH_SAMPLEMODE_CONTINUOUS : ADC_CH_SAMPLEMODE_ONESHOT); adc_setChConfig(ADC_CH_VOLDET_BATTERY, ADC_CH_CONFIG_ID_INTEN, FALSE); // Enable adc control logic adc_setEnable(TRUE); Delay_DelayMs(15); //wait ADC stable //for pwr on speed up }
/** Initialize voltage detection Initialize voltage detection for battery and flash @param void @return void */ void VolDet_Init(void) { if (adc_open(VOLDET_BATTERY_ADC_CH) != E_OK) { debug_err(("VOLDET ERR: Can't open ADC channel for battery detection\r\n")); return; } #if (VOLDET_FLASH_LIGHT == ENABLE) if (adc_open(VOLDET_FLASH_ADC_CH) != E_OK) { debug_err(("VOLDET ERR: Can't open ADC channel for flash detection\r\n")); return; } #endif // Set extra sample count to 0, ADC will change channel every 16 clocks adc_setExtraSampCnt(0); // Set sampling average to 2, each channel will sample twice and return average adc_setSampleAvg(ADC_SAMPAVG_2); // Set sampling rate per channel to 1953.125 Hz // Sampling rate per channel = 8,000,000 / (15 + 1) / 8 / (16 + 0) / 2 = 1953.125 // 15 of (15 + 1) is sampling divider, valid value: 0 ~ 15 // 8 is total channel number, fixed // 0 of (16 + 0) is extra sample count // 2 is sampling average // Parameter 1 is useless in NT96630 (Only support sequential mode) adc_setControl(15, TRUE); // Set channel divider for battery to 255, battery will be detected per 131.072 ms // Real sampling rate will be 1953.125 / (channel divider + 1) = 7.629 Hz adc_setChDivider(VOLDET_BATTERY_ADC_CH, 255); // Set mode for battery voltage detection adc_setMode(VOLDET_BATTERY_ADC_CH, VOLDET_ADC_MODE, FALSE, NULL); #if (VOLDET_FLASH_LIGHT == ENABLE) // Set channel divider for flash light voltage to 255 // Real sampling rate will be 1953.125 / (channel divider + 1) = 7.629 Hz adc_setChDivider(VOLDET_FLASH_ADC_CH, 255); // Set mode for flash voltage detection adc_setMode(VOLDET_FLASH_ADC_CH, VOLDET_ADC_MODE, FALSE, NULL); #endif // Enable adc control logic adc_enable(); #if (VOLDET_ADC_CONT_MODE == DISABLE) adc_triggerOneShot(VOLDET_BATTERY_ADC_CH); #if (VOLDET_FLASH_LIGHT == ENABLE) adc_triggerOneShot(VOLDET_FLASH_ADC_CH); #endif #endif }
/** Initialize voltage detection Initialize voltage detection for battery and flash @param void @return void */ void VolDet_Init(void) { if (adc_open(VOLDET_BATTERY_ADC_CH) != E_OK) { debug_err(("VOLDET ERR: Can't open ADC channel for battery detection\r\n")); return; } #if (VOLDET_FLASH_LIGHT == ENABLE) if (adc_open(VOLDET_FLASH_ADC_CH) != E_OK) { debug_err(("VOLDET ERR: Can't open ADC channel for flash detection\r\n")); return; } #endif // ADC sampling rate = source_clock / (2*(sampling_divider + 1)) / total_channel / (16 + extra_sampling_count) / (channel_divider + 1) / sampling_average_times // Source clock = 12MHz, total channel = 3 // Set extra sample count to 0, ADC will change channel every 16 clocks adc_setExternalSampCnt(0); // Set sampling average to 2, each channel will sample twice and return average adc_setSampleAvg(ADC_SAMPAVG_2); // Set sampling divider to 15 (5 bit, range 0 ~ 31) adc_setControl(15, TRUE); // Set channel divider for battery to 255, so ADC sampling rate for battery is // 12MHz / (2*(15+1)) / 3 / (16+0) / (255+1) / 2 = 15Hz adc_setChDivider(VOLDET_BATTERY_ADC_CH, 255); // Set mode for battery voltage detection adc_setMode(VOLDET_BATTERY_ADC_CH, VOLDET_ADC_MODE, FALSE, NULL); #if (VOLDET_FLASH_LIGHT == ENABLE) // Set channel divider for flash light voltage to 255, so ADC sampling rate for flash voltage is // 12MHz / (2*(15+1)) / 3 / (16+0) / (255+1) / 2 = 15Hz adc_setChDivider(VOLDET_FLASH_ADC_CH, 255); // Set mode for flash voltage detection adc_setMode(VOLDET_FLASH_ADC_CH, VOLDET_ADC_MODE, FALSE, NULL); #endif // Enable adc control logic adc_enable(); #if (VOLDET_ADC_CONT_MODE == DISABLE) adc_triggerOneShot(VOLDET_BATTERY_ADC_CH); #if (VOLDET_FLASH_LIGHT == ENABLE) adc_triggerOneShot(VOLDET_FLASH_ADC_CH); #endif #endif }
/** Initialize voltage detection Initialize voltage detection for battery and flash @param void @return void */ void VolDet_Init(void) { /*if (adc_open(VOLDET_FLASH_ADC_CH) != E_OK) { debug_err(("VOLDET ERR: Can't open ADC channel for flash detection\r\n")); return; }*/ if (adc_open(VOLDET_BATTERY_ADC_CH) != E_OK) { debug_err(("VOLDET ERR: Can't open ADC channel for battery detection\r\n")); return; } // Set sampling rate to 40KHz and sequential sampling mode // Each channel will sample once about 100 us adc_setControl(36, TRUE); // Set mode for flash voltage detection //adc_setMode(VOLDET_FLASH_ADC_CH, TRUE, FALSE, NULL); // Set mode for battery voltage detection adc_setMode(VOLDET_BATTERY_ADC_CH, TRUE, FALSE, NULL); // Enable adc control logic adc_enable(); }
/* Open the light sensor object for reading values. The sensor is usually powered * on or wakeup from sleep mode in the sensor's open() function. * * @attention * @todo * The PIN initialization can be executed in target_init() or in light_open(). * The following implementation assumes you have already initialized PIN configurations * before calling light_open() or in adc_open(). */ TiLightSensor * light_open( TiLightSensor * light, uint8 id, TiAdcAdapter * adc ) { light->id = id; light->adc = adc; adc_open( adc, adc->id, NULL, NULL, 0 ); return light; }
int main(void) { struct freedv *f; short buf[FREEDV_NSAMPLES]; int nin, nout; /* init all the drivers for various peripherals */ sm1000_leds_switches_init(); dac_open(4*DAC_BUF_SZ); adc_open(); f = freedv_open(FREEDV_MODE_1600); /* LEDs into a known state */ led_pwr(1); led_ptt(0); led_rt(0); led_err(0); /* TODO: [ ] UT analog interfaces from file IO [ ] UTs for simultaneous tx & rx on analog interfaces [ ] measure CPU load of various parts with a blinky [ ] detect program assert type errors with a blinky [ ] timer tick function to measure 10ms-ish type times [ ] switch debouncing? [ ] light led with bit errors */ while(1) { if(switch_ptt()) { /* Transmit -------------------------------------------------------------------------*/ /* ADC2 is the SM1000 microphone, DAC1 is the modulator signal we send to radio tx */ if (adc2_read(buf, FREEDV_NSAMPLES) == FREEDV_NSAMPLES) { freedv_tx(f, buf, buf); dac1_write(buf, FREEDV_NSAMPLES); led_ptt(1); led_rt(0); led_err(0); } } else { /* Receive --------------------------------------------------------------------------*/ /* ADC1 is the demod in signal from the radio rx, DAC2 is the SM1000 speaker */ nin = freedv_nin(f); f->total_bit_errors = 0; if (adc1_read(buf, nin) == nin) { nout = freedv_rx(f, buf, buf); dac2_write(buf, nout); led_ptt(0); led_rt(f->fdmdv_stats.sync); led_err(f->total_bit_errors); } } } /* while(1) ... */ }
/* * INITIALIZATION AND MISC */ void setup_1(void) { uint8_t i; IFI_Initialization(); /* Initialize serial port communication. */ statusflag.b.NEW_SPI_DATA = 0; usart1_open(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH, kBaud115); delay1ktcy(50); /* Make the master control all PWMs (for now) */ txdata.pwm_mask.a = 0xFF; for (i = IX_DIGITAL(1); i <= IX_DIGITAL(CT_DIGITAL); ++i) { digital_setup(i, false); } /* Init ADC */ /* Setup the number of analog sensors. The PIC defines a series of 15 * ADC constants in mcc18/h/adc.h that are all of the form 0b1111xxxx, * where x counts the number of DIGITAL ports. In total, there are * sixteen ports numbered from 0ANA to 15ANA. */ if (NUM_ANALOG_VALID(USER_CT_ANALOG) && USER_CT_ANALOG > 0) { /* ADC_FOSC: Based on a baud_115 value of 21, given the formula * FOSC/(16(X + 1)) in table 18-1 of the PIC18F8520 doc the * FOSC is 40Mhz. * Also according to the doc, section 19.2, the * ADC Freq needs to be at least 1.6us or 0.625MHz. 40/0.625=64 * (Also, see table 19-1 in the chip doc) */ #if defined(MCC18) OpenADC( ADC_FOSC_64 & ADC_RIGHT_JUST & ( 0xF0 | (16 - USER_CT_ANALOG) ) , ADC_CH0 & ADC_INT_OFF & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS ); #elif defined(SDCC) adc_open( ADC_CHN_0, ADC_FOSC_64, ADC_CFG_16A, ADC_FRM_RJUST | ADC_INT_OFF | ADC_VCFG_VDD_VSS ); #else #error "Bad Comp" #endif } else { /* TODO: Handle the error. */ puts("ADC is disabled"); } }
/** Initialize voltage detection Initialize voltage detection for battery and flash @param void @return void */ void VolDet_Init(void) { if (adc_open(VOLDET_BATTERY_ADC_CH) != E_OK) { debug_err(("VOLDET ERR: Can't open ADC channel for battery detection\r\n")); return; } if (adc_open(VOLDET_FLASH_ADC_CH) != E_OK) { debug_err(("VOLDET ERR: Can't open ADC channel for flash detection\r\n")); return; } //#NT#2009/10/21#Philex Lin - begin if (adc_open(VOLDET_TV_EARPHONE_ADC_CH) != E_OK) { debug_err(("VOLDET ERR: Can't open ADC channel for TV_EarPhone detection\r\n")); return; } //#NT#2009/10/21#Philex Lin - end // Set sampling rate to 40KHz and sequential sampling mode // Each channel will sample once about 100 us adc_setControl(36, TRUE); // Set mode for battery voltage detection adc_setMode(VOLDET_BATTERY_ADC_CH, VOLDET_ADC_MODE, FALSE, NULL); // Set mode for flash voltage detection adc_setMode(VOLDET_FLASH_ADC_CH, VOLDET_ADC_MODE, FALSE, NULL); //#NT#2009/10/21#Philex Lin - begin // Set mode for TV/EarPhone voltage detection adc_setMode(VOLDET_TV_EARPHONE_ADC_CH, VOLDET_ADC_MODE, FALSE, NULL); //#NT#2009/10/21#Philex Lin - end // Enable adc control logic adc_enable(); #if (VOLDET_ADC_CONT_MODE == DISABLE) adc_triggerOneShot(VOLDET_BATTERY_ADC_CH); adc_triggerOneShot(VOLDET_FLASH_ADC_CH); #endif }
static be_jse_symbol_t * adc_module_handle_cb(be_jse_vm_ctx_t *execInfo, be_jse_symbol_t *var, const char *name){ if(0 == strcmp(name,"open")){ return adc_open(); } if(0 == strcmp(name,"read")){ return adc_read(); } if(0 == strcmp(name,"close")){ return adc_close(); } return (BE_JSE_FUNC_UNHANDLED); }
/** Initialize voltage detection Initialize voltage detection for battery and flash @param void @return void */ void VolDet_Init(void) { if (adc_open(VOLDET_FLASH_ADC_CH) != E_OK) { debug_err(("VOLDET ERR: Can't open ADC channel for flash detection\r\n")); return; } // Set mode for flash voltage detection adc_setMode(VOLDET_FLASH_ADC_CH, TRUE, FALSE, NULL); // Enable adc control logic adc_enable(); }
int main(int argc, char *argv[]) { SystemInit(); gpio_init(); machdep_profile_init (); adc_open(4*DAC_BUF_SZ); dac_open(4*DAC_BUF_SZ); printf("Starting power_ut\n"); c2speedtest(CODEC2_MODE_1600, "stm_in.raw"); printf("Finished\n"); return 0; }
void initADC() { LATA = 0xFF; // set all A to inputs /* ADC_FOSC_64: conversion clock, table 21-1, => 1TAD = 1,33 us (has to be between 0,7 us and 25 us) ADC_RIGHT_JUST: Least significant bits ADC_6_TAD: acquisition time, 6 TAD's used for good conversion, minimal 1,4 us required ADC_INT_OFF: no interrupts ADC_VREFPLUS_VDD and ADC_VREFMINUS_VSS: use PIC ground and source as voltage reference 0b1010: pins AN0-AN4 configured as analog */ #ifdef SDCC_pic16 adc_open(ADC_CHN_0, ADC_FOSC_64 | ADC_ACQT_6, ADC_CFG_5A, ADC_FRM_RJUST | ADC_INT_OFF | ADC_VCFG_VDD_VSS); #else OpenADC(ADC_FOSC_64 & ADC_RIGHT_JUST & ADC_6_TAD, ADC_CH0 & ADC_INT_OFF & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS, 0b1010); // Configuring ADC #endif }
int main(void) { short buf[N]; float sd1, sd2; adc_open(2*N); printf("Starting!\n"); while(1) { while(adc1_read(buf, N) == -1); sd1 = calc_sd(buf, N); while(adc2_read(buf, N) == -1); sd2 = calc_sd(buf, N); printf("adc1: %5.1f adc2: %5.1f\n", (double)sd1, (double)sd2); } }
/** Initialize key ADC detection Initialize key ADC detection @param void @return void */ void KeyADC_Init(void) { debug_ind(("KeyADC_Init\r\n")); if (adc_open(KEY_ADC_CH_1) != E_OK) { debug_err(("Can't open ADC for key group 1\r\n")); return; } // Set sampling rate to 40KHz and sequential sampling mode // Each channel will sample once about 100 us adc_setControl(36, TRUE); // Set mode for key group 1 detection adc_setMode(KEY_ADC_CH_1, TRUE/*continuous mode*/, FALSE/*disable interrupt*/, NULL); // Enable adc control logic adc_enable(); //adc_triggerOneShot(ADC_CHANNEL_1); }
int main(void) { unsigned short unsigned_buf[N]; short buf[N]; int sam; int i, j, fifo_sz; FILE *fadc; fadc = fopen("adc.raw", "wb"); if (fadc == NULL) { printf("Error opening output file: adc.raw\n\nTerminating....\n"); exit(1); } fifo_sz = ADC_TUNER_BUF_SZ; printf("Starting! bufs: %d %d\n", BUFS, fifo_sz); adc_open(fifo_sz); adc_set_tuner_en(0); /* dump raw samples, no tuner */ sm1000_leds_switches_init(); for (i=0; i<BUFS; i++) { while(adc1_read((short*)unsigned_buf, N) == -1); /* convert to signed */ for(j=0; j<N; j++) { sam = (int)unsigned_buf[j] - 32768; buf[j] = sam; } /* most of the time will be spent here */ GPIOE->ODR |= (1 << 3); fwrite(buf, sizeof(short), N, fadc); GPIOE->ODR &= ~(1 << 3); } fclose(fadc); printf("Finished!\n"); }
/** Initialize voltage detection Initialize voltage detection for battery and flash @param void @return void */ void VolDet_Init(void) { if (adc_open(VOLDET_BATTERY_ADC_CH) != E_OK) { debug_err(("VOLDET ERR: Can't open ADC channel for battery detection\r\n")); return; } // Set sampling rate to 40KHz and sequential sampling mode // Each channel will sample once about 100 us adc_setControl(36, TRUE); // Set mode for battery voltage detection adc_setMode(VOLDET_BATTERY_ADC_CH, VOLDET_ADC_MODE, FALSE, NULL); // Enable adc control logic adc_enable(); #if (VOLDET_ADC_CONT_MODE == DISABLE) adc_triggerOneShot(VOLDET_BATTERY_ADC_CH); #endif }
int main(void) { short buf[SINE_SAMPLES]; int i; dac_open(4*DAC_BUF_SZ); adc_open(ADC_FS_16KHZ, 4*ADC_BUF_SZ); sm1000_leds_switches_init(); while (1) { /* keep DAC FIFOs topped up */ while(adc2_read(buf, SINE_SAMPLES) == -1); if (!switch_select()) { for(i=0; i<SINE_SAMPLES; i++) buf[i] = aSine[i]; } dac2_write(buf, SINE_SAMPLES); } }
int main(int argc, char **argv){ setvbuf (stdout, NULL, _IONBF, 0); // needed to print to the command line if (adc_open() != 1){ // open the ADC spi channel exit(1); // if the SPI bus fails to open exit the program } while (1){ clearscreen(); printf("Pin 1: %G \n", adc_read_voltage(1, 0)); // read the voltage from channel 1 in single ended mode printf("Pin 2: %G \n", adc_read_voltage(2, 0)); // read the voltage from channel 2 in single ended mode printf("Pin 3: %G \n", adc_read_voltage(3, 0)); // read the voltage from channel 3 in single ended mode printf("Pin 4: %G \n", adc_read_voltage(4, 0)); // read the voltage from channel 4 in single ended mode printf("Pin 5: %G \n", adc_read_voltage(5, 0)); // read the voltage from channel 5 in single ended mode printf("Pin 6: %G \n", adc_read_voltage(6, 0)); // read the voltage from channel 6 in single ended mode printf("Pin 7: %G \n", adc_read_voltage(7, 0)); // read the voltage from channel 7 in single ended mode printf("Pin 8: %G \n", adc_read_voltage(8, 0)); // read the voltage from channel 8 in single ended mode usleep(200000); // sleep 0.2 seconds } return (0); }
int main(void){ short buf[N]; FILE *fadc; int i, bufs; fadc = fopen("adc.raw", "wb"); if (fadc == NULL) { printf("Error opening input file: adc.raw\n\nTerminating....\n"); exit(1); } bufs = FS*REC_TIME_SECS/N; printf("Starting!\n"); adc_open(4*N); for(i=0; i<bufs; i++) { while(adc2_read(buf, N) == -1); fwrite(buf, sizeof(short), N, fadc); printf("adc_overflow1: %d adc_overflow2: %d \n", adc_overflow1, adc_overflow2); } fclose(fadc); printf("Finished!\n"); }
int main(void) { struct freedv *f; short adc16k[FDMDV_OS_TAPS_16K+FREEDV_NSAMPLES_16K]; short dac16k[FREEDV_NSAMPLES_16K]; short adc8k[FREEDV_NSAMPLES]; short dac8k[FDMDV_OS_TAPS_8K+FREEDV_NSAMPLES]; SWITCH_STATE ss; int nin, nout, i; /* init all the drivers for various peripherals */ SysTick_Config(SystemCoreClock/168000); /* 1 kHz SysTick */ sm1000_leds_switches_init(); dac_open(4*DAC_BUF_SZ); adc_open(4*ADC_BUF_SZ); f = freedv_open(FREEDV_MODE_1600); /* put outputs into a known state */ led_pwr(1); led_ptt(0); led_rt(0); led_err(0); not_cptt(1); /* clear filter memories */ for(i=0; i<FDMDV_OS_TAPS_16K; i++) adc16k[i] = 0.0; for(i=0; i<FDMDV_OS_TAPS_8K; i++) dac8k[i] = 0.0; ss.state = SS_IDLE; ss.mode = ANALOG; while(1) { iterate_select_state_machine(&ss); if (switch_ptt()) { /* Transmit -------------------------------------------------------------------------*/ /* ADC2 is the SM1000 microphone, DAC1 is the modulator signal we send to radio tx */ if (adc2_read(&adc16k[FDMDV_OS_TAPS_16K], FREEDV_NSAMPLES_16K) == 0) { GPIOE->ODR = (1 << 3); fdmdv_16_to_8_short(adc8k, &adc16k[FDMDV_OS_TAPS_16K], FREEDV_NSAMPLES); if (ss.mode == ANALOG) { for(i=0; i<FREEDV_NSAMPLES; i++) dac8k[FDMDV_OS_TAPS_8K+i] = adc8k[i]; fdmdv_8_to_16_short(dac16k, &dac8k[FDMDV_OS_TAPS_8K], FREEDV_NSAMPLES); dac1_write(dac16k, FREEDV_NSAMPLES_16K); } if (ss.mode == DV) { freedv_tx(f, &dac8k[FDMDV_OS_TAPS_8K], adc8k); fdmdv_8_to_16_short(dac16k, &dac8k[FDMDV_OS_TAPS_8K], FREEDV_NSAMPLES); dac1_write(dac16k, FREEDV_NSAMPLES_16K); } if (ss.mode == TONE) { while(dac1_write((short*)aSine, SINE_SAMPLES) == 0); } led_ptt(1); led_rt(0); led_err(0); not_cptt(0); GPIOE->ODR &= ~(1 << 3); } } else { /* Receive --------------------------------------------------------------------------*/ not_cptt(1); led_ptt(0); /* ADC1 is the demod in signal from the radio rx, DAC2 is the SM1000 speaker */ if (ss.mode == ANALOG) { /* force analog bypass when select down */ if (adc1_read(&adc16k[FDMDV_OS_TAPS_16K], FREEDV_NSAMPLES_16K) == 0) { fdmdv_16_to_8_short(adc8k, &adc16k[FDMDV_OS_TAPS_16K], FREEDV_NSAMPLES); for(i=0; i<FREEDV_NSAMPLES; i++) dac8k[FDMDV_OS_TAPS_8K+i] = adc8k[i]; fdmdv_8_to_16_short(dac16k, &dac8k[FDMDV_OS_TAPS_8K], FREEDV_NSAMPLES); dac2_write(dac16k, FREEDV_NSAMPLES_16K); led_rt(0); led_err(0); } } else { /* regular DV mode */ nin = freedv_nin(f); nout = nin; f->total_bit_errors = 0; if (adc1_read(&adc16k[FDMDV_OS_TAPS_16K], 2*nin) == 0) { GPIOE->ODR = (1 << 3); fdmdv_16_to_8_short(adc8k, &adc16k[FDMDV_OS_TAPS_16K], nin); nout = freedv_rx(f, &dac8k[FDMDV_OS_TAPS_8K], adc8k); fdmdv_8_to_16_short(dac16k, &dac8k[FDMDV_OS_TAPS_8K], nout); dac2_write(dac16k, 2*nout); led_rt(f->fdmdv_stats.sync); led_err(f->total_bit_errors); GPIOE->ODR &= ~(1 << 3); } } } } /* while(1) ... */ }
int main(void) { uint16 value, count; uint8 len; char * request; char * response; char * payload; char * msg = "welcome to node..."; TiTimerAdapter * timeradapter; TiTimerManager * vtm; TiTimer * mac_timer; TiCc2420Adapter * cc; TiFrameRxTxInterface * rxtx; TiNioAcceptor * nac; TiAloha * mac; TiAdcAdapter * adc; TiLumSensor * lum; TiDataTreeNetwork * dtp; TiFrame * rxbuf; TiFrame * txbuf; target_init(); led_open(); led_on( LED_ALL ); hal_delay( 500 ); led_off( LED_ALL ); rtl_init( (void *)dbio_open(38400), (TiFunDebugIoPutChar)dbio_putchar, (TiFunDebugIoGetChar)dbio_getchar, hal_assert_report ); dbc_write( msg, strlen(msg) ); timeradapter = timer_construct( (void *)(&m_timeradapter), sizeof(m_timeradapter) ); vtm = vtm_construct( (void*)&m_vtm, sizeof(m_vtm) ); cc = cc2420_construct( (char *)(&m_cc), sizeof(TiCc2420Adapter) ); nac = nac_construct( &m_nacmem[0], NAC_SIZE ); mac = aloha_construct( (char *)(&m_aloha), sizeof(TiAloha) ); dtp = dtp_construct( (char *)(&m_dtp), sizeof(TiDataTreeNetwork) ); adc = adc_construct( (void *)&m_adc, sizeof(TiAdcAdapter) ); lum = lum_construct( (void *)&m_lum, sizeof(TiLumSensor) ); txbuf = frame_open( (char*)(&m_txbuf), FRAME_HOPESIZE(MAX_IEEE802FRAME154_SIZE), 3, 20, 0 ); rxbuf = frame_open( (char*)(&m_rxbuf), FRAME_HOPESIZE(MAX_IEEE802FRAME154_SIZE), 3, 20, 0 ); // timeradapter is used by the vtm(virtual timer manager). vtm require to enable the // period interrupt modal of vtm //timeradapter = timer_open( timeradapter, 0, NULL, NULL, 0x01 ); vtm = vtm_open( vtm, timeradapter, CONFIG_VTM_RESOLUTION ); cc = cc2420_open(cc, 0, NULL, NULL, 0x00 ); rxtx = cc2420_interface( cc, &m_rxtx ); mac_timer = vtm_apply( vtm ); mac_timer = vti_open( mac_timer, NULL, mac_timer); hal_assert( rxtx != NULL ); nac = nac_open( nac, rxtx, CONFIG_NIOACCEPTOR_RXQUE_CAPACITY, CONFIG_NIOACCEPTOR_TXQUE_CAPACITY); hal_assert( nac != NULL ); mac = aloha_open( mac, rxtx,nac, CONFIG_NODE_CHANNEL, CONFIG_NODE_PANID, CONFIG_NODE_ADDRESS,mac_timer, NULL, NULL,0x01); adc = adc_open( adc, 0, NULL, NULL, 0 ); lum = lum_open( lum, 0, adc ); dtp = dtp_open( dtp, mac, CONFIG_NODE_ADDRESS, NULL, NULL, 0x00 ); //todo cc2420_setchannel( cc, CONFIG_NODE_CHANNEL ); cc2420_setrxmode( cc ); // enable RX mode cc2420_setpanid( cc, CONFIG_NODE_PANID ); // network identifier, seems no use in sniffer mode cc2420_setshortaddress( cc, CONFIG_NODE_ADDRESS ); // in network address, seems no use in sniffer mode cc2420_enable_autoack( cc ); //todo cc2420_settxpower( cc, CC2420_POWER_1);//cc2420_settxpower( cc, CC2420_POWER_2); cc2420_enable_autoack( cc ); // ledtune = ledtune_construct( (void*)(&m_ledtune), sizeof(m_ledtune), vti ); // ledtune = ledtune_open( ledtune ); /* assert: all the above open() functions return non NULL values */ hal_assert((timeradapter != NULL) && (cc != NULL) && (mac != NULL) && (adc != NULL) && (lum != NULL) && (rxbuf != NULL) && (txbuf != NULL) && (dtp != NULL)); hal_enable_interrupts(); dtp->state = DTP_STATE_IDLE;//todo for testing 临时用这一句必须删掉 //todo for testing //dtp->root = 0x01;//todo for testing //dtp->parent = 0x03;//todo for testing /* while ( 1)//todo for testing { response = frame_startptr( txbuf ); value = lum_value( lum ); payload = DTP_PAYLOAD_PTR(response); payload[0] = 0x13; payload[1] = 0x14; if (dtp_send_response(dtp, txbuf, 0x01) > 0) { led_toggle( LED_RED);//todo for testing } dtp_evolve( dtp, NULL ); hal_delay( 2000);//todo for testing } */ while(1) { /* Only the following two kinds of frames will be put into "rxbuf" by dtp_recv() * - broadcast frames. the destination address of these frames are 0xFFFF. * - destination is the current node. */ //dbo_putchar(0x33); len = dtp_recv( dtp, rxbuf, 0x00 ); if (len > 0) { //ieee802frame154_dump( rxbuf); request = frame_startptr( rxbuf ); switch (DTP_CMDTYPE(request)) { /* if the frame is DTP_DATA_REQUEST, then the node will measure the data and * encapsulate the data into the txbuf, which is a TiOpenFrame and sent it back. */ case DTP_DATA_REQUEST: //payload = DTP_PAYLOAD_PTR( frame_startptr(txbuf) ); //ledtune_write( ledtune, MAKE_WORD(payload[1], payload[0]) ); // response frame = PHY Length 1B // + Frame Control 2B // + Sequence No 1B // + Destination Pan & Address 4B // + Source Pan & Address 4B // + DTP Section 15B //opf_cast( txbuf, 50, OPF_DEF_FRAMECONTROL_DATA_ACK ); response = frame_startptr( txbuf ); value = lum_value( lum ); DTP_SET_MAX_HOPCOUNT( response,0x03);//todo for testing payload = DTP_PAYLOAD_PTR(response); //payload[0] = 0x17;//todo 第三个节点数据 //payload[1] = 0x18;//todo 第三个节点数据 //payload[1] = 0x13; //payload[2] = 0x14; payload[1] = 0x15;//todo 另一个节点 payload[2] = 0x16;//todo 另一个节点 /* call dtp_send_response() to send the data in txbuf out. * * modified by zhangwei on 20091230 * - Bug fix. In the past, there's no delay between two adjacent * dtp_send_response() calls. This policy is too ambitious and this * node will occupy the whole time so that the other nodes will lost * chances to send, or encounter much higher frame collision probabilities. * so I add a little time delay here. * Attention the delay time here shouldn't be too large because * we don't want the hal_delay() to occupy all the CPU time. If this * occurs, it may lead to unnecessary frame lossing. */ // try some times until the RESPONSE is successfully sent for (count=0; count<10; count++) { //hal_delay( 500); if (dtp_send_response(dtp, txbuf, 0x03) > 0) { led_toggle( LED_RED);//todo for testing break; } //hal_delay( 50 ); } break; default: //hal_assert(false); break; } } nac_evolve( nac,NULL);//todo for tesitng aloha_evolve( mac,NULL);//todo for testing dtp_evolve( dtp, NULL ); hal_delay( 50 ); } }
void init_sumo ( void ) { OSCCONbits.IRCF = 7; // Set internal clock to 16 Mhz ANSELHbits.ANS11 = 0; // Make RX digital // 115200,8,n,1: 138 - 64MHz // 19200,8,n,1: 832 - 64MHz; 207 - 16MHz // 9600,8,n,1: 129 - 20 MHz; 1249 - 48MHz; 1666 - 64MHz usart_open(USART_TX_INT_OFF & // disable TX interrupt USART_RX_INT_ON & // enable RX interrupt USART_BRGH_HIGH & // Use High BRGH USART_CONT_RX & // Receive continuous data USART_EIGHT_BIT & // Use 8 bit USART_ASYNCH_MODE, // Use Asynchronous mode 207); // 19200 for 16 Mhz #if defined (__SDCC) stdout = STREAM_USART; // Set stdout to serial stream #else baudUSART(BAUD_16_BIT_RATE & BAUD_IDLE_CLK_LOW & BAUD_WAKEUP_OFF & BAUD_AUTO_OFF & 0x48); // 16 bit BRG #endif #if defined (__SDCC) adc_open(ADC_CHN_0, // Set channel 0 ADC_FOSC_16 | ADC_ACQT_8, // Configure Acquisition frequency ADC_CFG_7A, // Set the number of channels to use ADC_FRM_RJUST | // Adjust result to the right ADC_INT_OFF | // Disable interrupts ADC_VCFG_VDD_VSS | // Configure reference voltages ADC_NVCFG_VSS | // Negative reference to VSS ADC_PVCFG_VDD); // Positive reference to VDD #else OpenADC(ADC_RIGHT_JUST & ADC_FOSC_16 & ADC_8_TAD, ADC_CH0 & ADC_INT_OFF, ADC_REF_VDD_VDD & ADC_REF_VDD_VSS, 0b0000111111110000); #endif T0CONbits.T08BIT = 0; // 16 bit mode T0CONbits.T0CS = 0; // Source is internal T0CONbits.PSA = 1; // Disable prescaler T0CONbits.T0PS = 7; // Prescaler to 1:256 TMR0H = 0; TMR0L = 0; // Reset Timer0 to 0x0000 INTCONbits.TMR0IF = 0; // Clear interrupt flag INTCONbits.TMR0IE = 1; // Enable Interruption from timer0 T0CONbits.TMR0ON = 1; // Start timer0 write_timer_0(Ticks4NextInterrupt); Servo.FL = 1; Servo.FR = 1; Servo.BL = 1; Servo.BR = 1; FL_SERVO_TRIS = 0; FR_SERVO_TRIS = 0; BL_SERVO_TRIS = 0; BR_SERVO_TRIS = 0; RCONbits.IPEN = 0; // Interruption Priority Disabled INTCONbits.PEIE = 1; // Peripheral Interrupt Enabled INTCONbits.GIE = 1; // Global Interrupt Enable }