/** * Interrupt handler for advertiser reports */ void SWI0_IRQHandler(void) { nrf_report_t report; while(btle_hci_adv_report_get(&report)) { char buf[256]; switch (report.event.event_code) { case BTLE_VS_EVENT_NRF_LL_EVENT_SCAN_REQ_REPORT: snprintf(buf, 256, "Received scan req on ch. %i. Addr: %.02X:%.02X:%.02X:%.02X:%.02X:%.02X \tRSSI: -%i \t Packets (valid/invalid): %i/%i\r\n", report.event.params.nrf_scan_req_report_event.channel, report.event.params.nrf_scan_req_report_event.address[5], report.event.params.nrf_scan_req_report_event.address[4], report.event.params.nrf_scan_req_report_event.address[3], report.event.params.nrf_scan_req_report_event.address[2], report.event.params.nrf_scan_req_report_event.address[1], report.event.params.nrf_scan_req_report_event.address[0], report.event.params.nrf_scan_req_report_event.rssi, report.valid_packets, report.invalid_packets); uart_putstring((uint8_t*) buf); break; /* For now, the only event we care about is the scan req event. */ default: uart_putstring((uint8_t*) "Unknown adv evt.\r\n"); } } }
/** @brief Function for reading the data rate. */ void get_datarate(void) { uint8_t c; uart_putstring((const uint8_t *)"Enter data rate ('0'=250 Kbit/s, '1'=1 Mbit/s and '2'=2 Mbit/s):"); while (true) { c = uart_get(); if ((c >= '0') && (c <= '2')) { uart_put(c); break; } else { uart_put(BELL); } } if (c == '0') { mode_ = RADIO_MODE_MODE_Nrf_250Kbit; } else if (c == '1') { mode_ = RADIO_MODE_MODE_Nrf_1Mbit; } else { mode_ = RADIO_MODE_MODE_Nrf_2Mbit; } uart_putstring((const uint8_t *)"\r\n"); }
// log data over the serial port void sendreport(void) { uint32_t cpm; // This is the CPM value we will report if(tick) { // 1 second has passed, time to report data via UART tick = 0; // reset flag for the next interval if (overflow) { cpm = cps*60UL; mode = 2; overflow = 0; } else if (fastcpm > THRESHOLD) { // if cpm is too high, use the short term average instead mode = 1; cpm = fastcpm; // report cpm based on last 5 samples } else { mode = 0; cpm = slowcpm; // report cpm based on last 60 samples } // Send CPM value to the serial port uart_putstring_P(PSTR("CPS, ")); utoa(cps, serbuf, 10); // radix 10 uart_putstring(serbuf); uart_putstring_P(PSTR(", CPM, ")); ultoa(cpm, serbuf, 10); // radix 10 uart_putstring(serbuf); uart_putstring_P(PSTR(", uSv/hr, ")); // calculate uSv/hr based on scaling factor, and multiply result by 100 // so we can easily separate the integer and fractional components (2 decimal places) uint32_t usv_scaled = (uint32_t)(cpm*SCALE_FACTOR); // scale and truncate the integer part // this reports the integer part utoa((uint16_t)(usv_scaled/10000), serbuf, 10); uart_putstring(serbuf); uart_putchar('.'); // this reports the fractional part (2 decimal places) uint8_t fraction = (usv_scaled/100)%100; if (fraction < 10) uart_putchar('0'); // zero padding for <0.10 utoa(fraction, serbuf, 10); uart_putstring(serbuf); // Tell us what averaging method is being used if (mode == 2) { uart_putstring_P(PSTR(", INST")); } else if (mode == 1) { uart_putstring_P(PSTR(", FAST")); } else { uart_putstring_P(PSTR(", SLOW")); } // We're done reporting data, output a newline. uart_putchar('\n'); } }
/** @brief Function for reading the output power. */ void get_power(void) { uint8_t c; uart_putstring((const uint8_t *)"Enter output power ('0'=+4 dBm, '1'=0 dBm,...,'7'=-30 dBm):"); while (true) { c = uart_get(); if ((c >= '0') && (c <= '7')) { uart_put(c); break; } else { uart_put(BELL); } } switch(c) { case '0': txpower_ = RADIO_TXPOWER_TXPOWER_Pos4dBm; break; case '1': txpower_ = RADIO_TXPOWER_TXPOWER_0dBm; break; case '2': txpower_ = RADIO_TXPOWER_TXPOWER_Neg4dBm; break; case '3': txpower_ = RADIO_TXPOWER_TXPOWER_Neg8dBm; break; case '4': txpower_ = RADIO_TXPOWER_TXPOWER_Neg12dBm; break; case '5': txpower_ = RADIO_TXPOWER_TXPOWER_Neg16dBm; break; case '6': txpower_ = RADIO_TXPOWER_TXPOWER_Neg20dBm; break; case '7': // fall through default: txpower_ = RADIO_TXPOWER_TXPOWER_Neg30dBm; break; } uart_putstring((const uint8_t *)"\r\n"); }
int initUARTWithInterrupts(){ POINTVAL_(IRQ_DISABLE1) = 1<<29; //disable MINI UART interrupts (just for safety). NOTE that mini uart is 29 (AUX) ringInit(); unsigned int ra; /* Firstly set up GPIO pins*/ ra = POINTVAL_(GPFSEL1); //get current register value ra &= ~(0b111<<12); //unset registers 12-14 (for GPIO 14) ra |= 0b010<<12; //set alternative function 5, which enables UART (on GPIO 14) //the same for GPIO 15 ra &= ~(0b111<<15); //unset registers 12-14 (for GPIO 15) ra |= 0b010<<15; //set alternative function 5, which enables UART (on GPIO 15) POINTVAL_(GPFSEL1) = ra; //write set register value back /* Configure UART */ POINTVAL_(AUX_ENABLES) = 0b1; //set 1st bit to 1, which enables mini UART POINTVAL_(AUX_MU_IER_REG) = 0b101; //(THIS IS DIFFERENT FROM NON INTERRUPT UART) Enables receive interupts (note errors in doucmentation) POINTVAL_(AUX_MU_LCR_REG) = 0b11; // first bit must set to enable 8-bit mode, 2nd bit must be set for some reason so that //normal data are sent, but IMPORTANT..that is not mentioned in documentation !! POINTVAL_(AUX_MU_MCR_REG) = 0; // set to 0 - nothing used POINTVAL_(AUX_MU_IIR_REG) = 0b11000110; //(THIS IS DIFFERENT FROM NON INTERRUPT UART) (note errors in docs) POINTVAL_(AUX_MU_BAUD_REG) = MINIUART_BAUD_RATE; POINTVAL_(AUX_MU_CNTL_REG) = 0b11; //enable Tx, Rx uart_putstring("UART with Interrupts inited\r\n"); POINTVAL_(IRQ_ENABLE1) = 1<<29; return 0; }
int main(void) { /* Silence the compiler */ (void) g_sd_assert_pc; (void) g_evt; uart_init(); char start_msg[128]; sprintf(&start_msg[0], "\n| %s |---------------------------------------------------\r\n\n", __TIME__); uart_putstring((uint8_t*) &start_msg[0]); nrf_gpio_range_cfg_output(0, 30); for(int i = LED_START; i <= LED_STOP; ++i) nrf_gpio_pin_set(i); uint32_t error_code = sd_softdevice_enable((uint32_t)NRF_CLOCK_LFCLKSRC_XTAL_75_PPM, sd_assert_cb); APP_ERROR_CHECK(error_code); error_code = sd_nvic_EnableIRQ(SD_EVT_IRQn); APP_ERROR_CHECK(error_code); ble_setup(); /* Enable a generic SD advertiser to display concurrent operation */ nrf_adv_conn_init(); while (true) { sd_app_evt_wait(); } }
/** @brief Function for reading two digit decimal numbers from the serial port. Backspace is supported on the first char. */ static uint8_t get_dec2(void) { uint8_t buf[2]; uint8_t i = 0; uint8_t c; buf[0] = buf[1] = 0; while (i < 2) { c = uart_get(); if ((i > 0) && (c == BS)) { uart_put(c); i--; } else if ((c >= '0') && (c <= '9')) { uart_put(c); buf[i] = c - '0'; i++; } else { uart_put(BELL); } } uart_putstring((const uint8_t *)"\r\n"); return buf[0] * 10 + buf[1]; }
uint8_t wdc_read_multiblock ( uint32_t addr, uint8_t *sector, uint8_t numblocks ) { uint8_t errorcode; errorcode = drv_read_multiblock ( addr, sector, numblocks ); if ( errorcode > 0 ) { uart_putstring ( PSTR ( "ERROR: read error at address: " ), false ); uart_putdw_dec ( addr ); uart_putstring ( PSTR ( " / errorcode is: " ), false ); uart_putc_hex ( errorcode ); uart_put_nl(); return errorcode; } else { return 0; } }
uint8_t wdc_write_sector ( uint32_t addr, uint8_t *sector ) { uint8_t errorcode; errorcode = drv_write_block ( addr, sector ); if ( errorcode > 0 ) { uart_putstring ( PSTR ( "ERROR: write error at address: " ), false ); uart_putdw_dec ( addr ); uart_putstring ( PSTR ( " / errorcode is: " ), false ); uart_putc_hex ( errorcode ); uart_put_nl(); return errorcode; } else { return 0; } }
/**@brief Application main function. */ int main(void) { static uint8_t data_array[BLE_NUS_MAX_DATA_LEN]; static uint8_t index = 0; uint8_t newbyte; // Initialize leds_init(); timers_init(); gpiote_init(); buttons_init(); uart_init(); ble_stack_init(); gap_params_init(); services_init(); advertising_init(); conn_params_init(); sec_params_init(); uart_putstring(START_STRING); advertising_start(); // Enter main loop for (;;) { /*Stop reading new data if there are no ble buffers available */ if(ble_buffer_available) { if(app_uart_get(&newbyte) == NRF_SUCCESS) { data_array[index++] = newbyte; if (index >= (BLE_NUS_MAX_DATA_LEN)) { ble_buffer_available=ble_attempt_to_send(&data_array[0],index); if(ble_buffer_available) index=0; } } } /* Re-transmission if ble_buffer_available was set to false*/ if(tx_complete) { tx_complete=false; ble_buffer_available=ble_attempt_to_send(&data_array[0],index); if(ble_buffer_available) index =0; } power_manage(); } }
int initUART(){ unsigned int ra; /* Firstly set up GPIO pins*/ ra = POINTVAL_(GPFSEL1); //get current register value ra &= ~(0b111<<12); //unset registers 12-14 (for GPIO 14) ra |= 0b010<<12; //set alternative function 5, which enables UART (on GPIO 14) //the same for GPIO 15 ra &= ~(0b111<<15); //unset registers 12-14 (for GPIO 15) ra |= 0b010<<15; //set alternative function 5, which enables UART (on GPIO 15) POINTVAL_(GPFSEL1) = ra; //write set register value back /* Configure UART */ POINTVAL_(AUX_ENABLES) = 0b1; //set 1st bit to 1, which enables mini UART POINTVAL_(AUX_MU_IER_REG) = 0; //set to 0, because interrupts are not used POINTVAL_(AUX_MU_LCR_REG) = 0b11; // first bit must set to enable 8-bit mode, 2nd bit must be set for some reason so that //normal data are sent, but IMPORTANT..that is not mentioned in documentation !! POINTVAL_(AUX_MU_MCR_REG) = 0; // set to 0 - nothing used POINTVAL_(AUX_MU_IIR_REG) = 0; //in other tutorials this is set to 0xC6, but I think this is needed only when interrupts are used POINTVAL_(AUX_MU_BAUD_REG) = MINIUART_BAUD_RATE; POINTVAL_(AUX_MU_CNTL_REG) = 0b11; //enable Tx, Rx // DONT KNOW why these pull up/down steps are performed, thats why commented out, // but maybe they are needed?... anyway works without these steps // PUT32(GPPUD,0); //disable pull up/down control // for(ra=0;ra<150;ra++) dummy(ra); // PUT32(GPPUDCLK0,(1<<14)|(1<<15)); // for(ra=0;ra<150;ra++) dummy(ra); // PUT32(GPPUDCLK0,0); // PUT32(AUX_MU_CNTL_REG,3); uart_putstring("UART inited\r\n"); // while(1) // { // char readVal; // // 0th bit is set when data ar available in FIFO and can be read // while((POINTVAL_(AUX_MU_LSR_REG)&(0b1)) == 0){} // readVal = POINTVAL_(AUX_MU_IO_REG); // // uart_putc(readVal); // uart_putc('\n'); // } return 0; }
/** * App error check callback. Something went wrong, and we go into a blocking state, as continued * executing may lead to undefined behavoiur. */ void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t * p_file_name) { char buf[256]; sprintf(&buf[0], "ERROR: 0x%X, line: %d, file: %s\r\n", error_code, line_num, p_file_name); uart_putstring((uint8_t* ) buf); #if defined(BOARD_PCA10028) nrf_gpio_pin_clear(LED_1); nrf_gpio_pin_clear(LED_2); #elif defined(BOARD_PCA10031) nrf_gpio_pin_clear(LED_RGB_BLUE); #endif while(1); }
/**@brief Logging function, used for formated output on the UART. */ void test_logf(const char *fmt, ...) { int16_t res = 0; static uint8_t buf[150]; va_list args; va_start(args, fmt); res = vsnprintf((char*) buf, sizeof(buf), fmt, args); ASSERT(res >= 0 && res <= (sizeof buf) - 1); uart_putstring(buf); va_end(args); }
/** @brief Function for handling the UART Interrupt. */ void UART0_IRQHandler(void) { if (nrx > BUFFER_LENGTH) { uart_putstring((const uint8_t *)"BUFFER_OVERFLOW"); while(1) { // Do nothing. } } nrx++; rx_buffer[rxwp] = NRF_UART0->RXD; rxwp = (rxwp + 1) & (BUFFER_LENGTH - 1); NRF_UART0->EVENTS_RXDRDY = 0; }
void pcf8563_test(void) { uint8 temp = time[0]; pcf8563_time_get(time); pcf8563_time_sort(); if(temp != time[0]) { uart_putnstring((uint8 *)data_tmp, 11); uart_putnstring((uint8 *)time_tmp, 9); uart_putstring((uint8 *)week_tmp); uart_putenter(); } #ifndef USE_UCOS //OSTimeDly(OS_TICKS_PER_SEC/2); #endif #ifndef USE_UCOS //_delay_ms(1000); #endif }
/** @brief Function for main application entry. */ int main(void) { radio_tests_t test = RADIO_TEST_NOP; radio_tests_t cur_test = RADIO_TEST_NOP; init(); uart_config(TX_PIN_NUMBER, RX_PIN_NUMBER); uart_putstring((const uint8_t *)"RF Test\r\n"); NVIC_EnableIRQ(TIMER0_IRQn); __enable_irq(); while (true) { __WFI(); switch (uart_get()) { case 'a': while (true) { uart_putstring((const uint8_t *)"Enter start channel \ (two decimal digits, 00 to 80):"); channel_start_ = get_dec2(); if (channel_start_ <= 80) break; uart_putstring((const uint8_t *)"Channel must be between 0 and 80\r\n"); } test = cur_test; break; case 'b': while (true) { uart_putstring((const uint8_t *)"Enter end channel \ (two decimal digits, 00 to 80):"); channel_end_ = get_dec2(); if (channel_end_ <= 80) { break; } uart_putstring((const uint8_t *)"Channel must be between 0 and 80\r\n"); } test = cur_test; break; case 'c': test = RADIO_TEST_TXCC; break; case 'd': while (true) { uart_putstring((const uint8_t *)"Enter delay in ms \ (two decimal digits, 01 to 99):"); delayms_ = get_dec2(); if ((delayms_ > 0) && (delayms_ < 100)) { break; } uart_putstring((const uint8_t *)"Delay must be between 1 and 99\r\n"); } test = cur_test; break; case 'e': radio_sweep_end(); cur_test = RADIO_TEST_NOP; break; case 'm': get_datarate(); test = cur_test; break; case 'o': test = RADIO_TEST_TXMC; uart_putstring((const uint8_t *)"TX modulated carrier\r\n"); break; case 'p': get_power(); test = cur_test; break; case 'r': test = RADIO_TEST_RXSWEEP; uart_putstring((const uint8_t *)"RX Sweep\r\n"); break; case 's': print_parameters(); break; case 't': test = RADIO_TEST_TXSWEEP; uart_putstring((const uint8_t *)"TX Sweep\r\n"); break; case 'x': test = RADIO_TEST_RXC; uart_putstring((const uint8_t *)"RX constant carrier\r\n"); break; case 'h': // Fall through. default: help(); break; } switch (test) { case RADIO_TEST_TXCC: if (sweep) { radio_sweep_end(); sweep = false; } radio_tx_carrier(txpower_, mode_, channel_start_); cur_test = test; test = RADIO_TEST_NOP; break; case RADIO_TEST_TXMC: if (sweep) { radio_sweep_end(); sweep = false; } radio_modulated_tx_carrier(txpower_, mode_, channel_start_); cur_test = test; test = RADIO_TEST_NOP; break; case RADIO_TEST_TXSWEEP: radio_tx_sweep_start(txpower_, mode_, channel_start_, channel_end_, delayms_); sweep = true; cur_test = test; test = RADIO_TEST_NOP; break; case RADIO_TEST_RXC: if (sweep) { radio_sweep_end(); sweep = false; } radio_rx_carrier(mode_, channel_start_); cur_test = test; test = RADIO_TEST_NOP; break; case RADIO_TEST_RXSWEEP: radio_rx_sweep_start(mode_, channel_start_, channel_end_, delayms_); sweep = true; cur_test = test; test = RADIO_TEST_NOP; break; case RADIO_TEST_NOP: // Fall through. default: // No implementation needed. break; } } }
/** @brief Function for printing parameters to the serial port. */ void print_parameters(void) { uart_putstring((const uint8_t *)"Parameters:\r\n"); switch(mode_) { case RADIO_MODE_MODE_Nrf_250Kbit: uart_putstring((const uint8_t *)"Data rate...........: 250 Kbit/s\r\n"); break; case RADIO_MODE_MODE_Nrf_1Mbit: uart_putstring((const uint8_t *)"Data rate...........: 1 Mbit/s\r\n"); break; case RADIO_MODE_MODE_Nrf_2Mbit: uart_putstring((const uint8_t *)"Data rate...........: 2 Mbit/s\r\n"); break; } switch(txpower_) { case RADIO_TXPOWER_TXPOWER_Pos4dBm: uart_putstring((const uint8_t *)"TX Power............: +4 dBm\r\n"); break; case RADIO_TXPOWER_TXPOWER_0dBm: uart_putstring((const uint8_t *)"TX Power............: 0 dBm\r\n"); break; case RADIO_TXPOWER_TXPOWER_Neg4dBm: uart_putstring((const uint8_t *)"TX Power............: -4 dBm\r\n"); break; case RADIO_TXPOWER_TXPOWER_Neg8dBm: uart_putstring((const uint8_t *)"TX Power............: -8 dBm\r\n"); break; case RADIO_TXPOWER_TXPOWER_Neg12dBm: uart_putstring((const uint8_t *)"TX Power............: -12 dBm\r\n"); break; case RADIO_TXPOWER_TXPOWER_Neg16dBm: uart_putstring((const uint8_t *)"TX Power............: -16 dBm\r\n"); break; case RADIO_TXPOWER_TXPOWER_Neg20dBm: uart_putstring((const uint8_t *)"TX Power............: -20 dBm\r\n"); break; case RADIO_TXPOWER_TXPOWER_Neg30dBm: uart_putstring((const uint8_t *)"TX Power............: -30 dBm\r\n"); break; default: // No implementation needed. break; } uart_putstring((const uint8_t *)"(Start) Channel.....: "); print_dec2(channel_start_); uart_putstring((const uint8_t *)"\r\nEnd Channel.........: "); print_dec2(channel_end_); uart_putstring((const uint8_t *)"\r\nTime on each channel: "); print_dec2(delayms_); uart_putstring((const uint8_t *)" ms\r\n"); }
/** @brief Function for outputting usage info to the serial port. */ static void help(void) { uart_putstring((const uint8_t *)"Usage:\r\n"); uart_putstring((const uint8_t *)"a: Enter start channel for sweep/channel for constant carrier\r\n"); uart_putstring((const uint8_t *)"b: Enter end channel for sweep\r\n"); uart_putstring((const uint8_t *)"c: Start TX carrier\r\n"); uart_putstring((const uint8_t *)"d: Enter time on each channel (1ms-99ms)\r\n"); uart_putstring((const uint8_t *)"e: Cancel sweep/carrier\r\n"); uart_putstring((const uint8_t *)"m: Enter data rate\r\n"); uart_putstring((const uint8_t *)"o: Start modulated TX carrier\r\n"); uart_putstring((const uint8_t *)"p: Enter output power\r\n"); uart_putstring((const uint8_t *)"s: Print current delay, channels and so on\r\n"); uart_putstring((const uint8_t *)"r: Start RX sweep\r\n"); uart_putstring((const uint8_t *)"t: Start TX sweep\r\n"); uart_putstring((const uint8_t *)"x: Start RX carrier\r\n"); }