int main(void) { system_init(); delay_init(); struct port_config pin; port_get_config_defaults(&pin); pin.direction = PORT_PIN_DIR_OUTPUT; port_pin_set_config(LED0_PIN, &pin); port_pin_set_output_level(LED0_PIN, LED0_INACTIVE); while (true) { for (int i = 0; i < 5; i++) { port_pin_toggle_output_level(LED0_PIN); delay_s(1); } for (int i = 0; i < 50; i++) { port_pin_toggle_output_level(LED0_PIN); delay_ms(100); } for (int i = 0; i < 5000; i++) { port_pin_toggle_output_level(LED0_PIN); delay_cycles(100); } } }
static void TASK_Heartbeat( void* pvParameters ) { /* Nothing to do here.... yet */ UNUSED(pvParameters); for(;;) { port_pin_toggle_output_level(LED0_PIN); port_pin_toggle_output_level(LED1_PIN); vTaskDelay(500); } }
void radioSendData(uint8_t *data, uint8_t size) { /* if (appDataReqBusy || 0 == appChannelBufferPtr) { return; } */ if (radioDataReqBusy) { // puts("radio busy"); return; } //memcpy(appDataReqBuffer, appUartBuffer, appUartBufferPtr); radioDataReq.dstAddr = 1 - APP_ADDR; radioDataReq.dstEndpoint = APP_ENDPOINT; radioDataReq.srcEndpoint = APP_ENDPOINT; radioDataReq.options = NWK_OPT_ENABLE_SECURITY; radioDataReq.data = data; //appDataReqBuffer; // radioDataReq.size = size;//sizeof(data); //appUartBufferPtr; // radioDataReq.confirm = radioDataConf; NWK_DataReq(&radioDataReq); printf("sending \n\r channel 0 value = %u\n\r channel 1 value = %u\n\r", data[0],data[1]); //appUartBufferPtr = 0; radioDataReqBusy = true; port_pin_toggle_output_level(LED_0_PIN); }
/*---------------------------------------------------------------------------*/ void i2c_master_interface_init(void) { if (!initialized) { struct i2c_master_config config_i2c_master; /* Toggle SCL for some time, this solves I2C periperhal problem */ struct port_config pin_conf; port_get_config_defaults(&pin_conf); pin_conf.direction = PORT_PIN_DIR_OUTPUT; port_pin_set_config(PIN_PA13, &pin_conf); for (int i = 0; i < 1000; i++) { port_pin_toggle_output_level(PIN_PA13); clock_wait(CLOCK_SECOND / 1000); } /* Initialize config structure and software module. */ i2c_master_get_config_defaults(&config_i2c_master); config_i2c_master.baud_rate = 400; config_i2c_master.pinmux_pad0 = SENSORS_I2C_SERCOM_PINMUX_PAD0; config_i2c_master.pinmux_pad1 = SENSORS_I2C_SERCOM_PINMUX_PAD1; /* Initialize and enable device with config. */ i2c_master_init(&i2c_master_instance, SENSORS_I2C_MODULE, &config_i2c_master); i2c_master_enable(&i2c_master_instance); initialized = true; } }
/** * \brief Toggles LED for Blink indication * * This function will Toggle BOOT LED every iDiffDelay milliseconds for a period of iMsDelay * Used to indicate different errors based on count of LED blinks * */ static void led_blink(int iMsDelay, int iDiffDelay) { while(iMsDelay >= iDiffDelay) { port_pin_toggle_output_level(BOOT_LED); iMsDelay = iMsDelay - iDiffDelay; delay_ms(iDiffDelay); } }
//! [setup_14] void event_counter(struct events_resource *resource) { if(events_is_interrupt_set(resource, EVENTS_INTERRUPT_DETECT)) { port_pin_toggle_output_level(LED_0_PIN); event_count++; events_ack_interrupt(resource, EVENTS_INTERRUPT_DETECT); } }
bool radioDataInd(NWK_DataInd_t *ind) { /*for (uint8_t i = 0; i < ind->size; i++) { sio2host_putchar(ind->data[i]); } */ uint8_t ch0value = ind->data[0]; uint8_t ch1value = ind->data[1]; printf("received \n\r Channel 0 value = %u\n\r Channel 1 value = %u\n\r", ch0value, ch1value); port_pin_toggle_output_level(LED_0_PIN); return true; }
static void NRF_Task(void *params) { //static uint8_t NRF_Recieve_Array[100]; static uint8_t ArrayIn[50]; //static uint8_t ArrayOut[50]; uint8_t PayloadOut[5] = {'J', 'U', 'S', 'T', 'I'}; static uint8_t XMitState = 0; //static uint8_t SendNotGet = 1; //static uint8_t Serial_Recieve_Array[100]; while(1) { xSemaphoreTake(display_mutex, portMAX_DELAY); #if 1 if(0 == XMitState) { Set_NRF24L_Tx_Mode(); port_pin_toggle_output_level(LED_0_PIN); SendNewPayload(PayloadOut, 5); Clear_NRF_Int_Flags(); XMitState = 1; } else if(1 == XMitState) { Set_NRF24L_Rx_Mode(); XMitState = 2; } else { if(0x0E == Read_Status()) { //port_pin_set_output_level(LED_0_PIN, 0); oled1_set_led_state(&oled1, OLED1_LED3_ID, false); } else { oled1_set_led_state(&oled1, OLED1_LED3_ID, true); ReadPayload(ArrayIn, 5); } XMitState = 0; } #endif xSemaphoreGive(display_mutex); vTaskDelay(NRF_TASK_DELAY); } }
//! [callback] void rtc_match_callback(void) { /* Do something on RTC alarm match here */ port_pin_toggle_output_level(LED_0_PIN); /* Set new alarm in 5 seconds */ //! [alarm_mask] alarm.mask = RTC_CALENDAR_ALARM_MASK_SEC; //! [alarm_mask] //! [set_alarm] alarm.time.second += 5; alarm.time.second = alarm.time.second % 60; rtc_calendar_set_alarm(&rtc_instance, &alarm, RTC_CALENDAR_ALARM_0); //! [set_alarm] }
int main(void) { //! [add_main] system_init(); struct rtc_calendar_time time; time.year = 2012; time.month = 12; time.day = 31; time.hour = 23; time.minute = 59; time.second = 59; configure_rtc_calendar(); /* Set current time. */ rtc_calendar_set_time(&rtc_instance, &time); rtc_calendar_swap_time_mode(&rtc_instance); //! [add_main] //! [main_imp] //! [main_loop] while (true) { //! [main_loop] //! [check_alarm_match] if (rtc_calendar_is_alarm_match(&rtc_instance, RTC_CALENDAR_ALARM_0)) { //! [check_alarm_match] //! [alarm_match_action] /* Do something on RTC alarm match here */ port_pin_toggle_output_level(LED_0_PIN); //! [alarm_match_action] //! [clear_alarm_match] rtc_calendar_clear_alarm_match(&rtc_instance, RTC_CALENDAR_ALARM_0); //! [clear_alarm_match] } } //! [main_imp] }
static void prvQueueReceiveTask( void *pvParameters ) { unsigned long ulReceivedValue; /* Check the task parameter is as expected. */ configASSERT( ( ( unsigned long ) pvParameters ) == mainQUEUE_RECEIVE_PARAMETER ); for( ;; ) { /* Wait until something arrives in the queue - this task will block indefinitely provided INCLUDE_vTaskSuspend is set to 1 in FreeRTOSConfig.h. */ xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY ); /* To get here something must have been received from the queue, but is it the expected value? If it is, toggle the LED. */ if( ulReceivedValue == 100UL ) { /* Toggle the LED. */ port_pin_toggle_output_level( LED_0_PIN ); ulReceivedValue = 0U; } } }
//! [callback_funcs] static void tcc_callback_to_toggle_led( struct tcc_module *const module_inst) { port_pin_toggle_output_level(LED0_PIN); }
/** * @brief Continues processing a data indication from the TAL for * non-polling and non-scanning states of the MAC * (mac_poll_state == MAC_POLL_IDLE, mac_scan_state == MAC_SCAN_IDLE). * * @param b_ptr Pointer to the buffer header. * @param f_ptr Pointer to the frame_info_t structure. * * @return bool True if frame has been processed, or false otherwise. */ static bool process_data_ind_not_transient(buffer_t *b_ptr, frame_info_t *f_ptr) { bool processed_in_not_transient = false; /* * We are in MAC_POLL_IDLE and MAC_SCAN_IDLE now, * so continue with the real MAC states. */ switch (mac_state) { #if (MAC_START_REQUEST_CONFIRM == 1) case MAC_PAN_COORD_STARTED: { switch (mac_parse_data.frame_type) { case FCF_FRAMETYPE_MAC_CMD: { switch (mac_parse_data.mac_command) { #if (MAC_ASSOCIATION_INDICATION_RESPONSE == 1) case ASSOCIATIONREQUEST: mac_process_associate_request(b_ptr); processed_in_not_transient = true; break; #endif /* (MAC_ASSOCIATION_INDICATION_RESPONSE == 1) */ #if (MAC_DISASSOCIATION_BASIC_SUPPORT == 1) case DISASSOCIATIONNOTIFICATION: mac_process_disassociate_notification(b_ptr); processed_in_not_transient = true; break; #endif /* (MAC_DISASSOCIATION_BASIC_SUPPORT == 1) */ #if (MAC_INDIRECT_DATA_FFD == 1) case DATAREQUEST: if (indirect_data_q.size > 0) { mac_process_data_request(b_ptr); processed_in_not_transient = true; } else { mac_handle_tx_null_data_frame(); } break; #endif /* (MAC_INDIRECT_DATA_FFD == 1) */ #if (MAC_ORPHAN_INDICATION_RESPONSE == 1) case ORPHANNOTIFICATION: mac_process_orphan_notification(b_ptr); processed_in_not_transient = true; break; #endif /* (MAC_ORPHAN_INDICATION_RESPONSE == 1) */ case BEACONREQUEST: mac_process_beacon_request(b_ptr); processed_in_not_transient = true; break; #if (MAC_PAN_ID_CONFLICT_AS_PC == 1) case PANIDCONFLICTNOTIFICAION: mac_sync_loss(MAC_PAN_ID_CONFLICT); break; #endif /* (MAC_PAN_ID_CONFLICT_AS_PC == 1) */ #ifdef GTS_SUPPORT case GTSREQUEST: mac_process_gts_request(b_ptr); processed_in_not_transient = true; #endif /* GTS_SUPPORT */ default: break; } } break; case FCF_FRAMETYPE_DATA: mac_process_data_frame(b_ptr); processed_in_not_transient = true; break; #if (MAC_PAN_ID_CONFLICT_AS_PC == 1) case FCF_FRAMETYPE_BEACON: /* PAN-Id conflict detection as PAN-Coordinator. */ /* Node is not scanning. */ check_for_pan_id_conflict_as_pc(false); break; #endif /* (MAC_PAN_ID_CONFLICT_AS_PC == 1) */ default: break; } } break; /* MAC_PAN_COORD_STARTED */ #endif /* (MAC_START_REQUEST_CONFIRM == 1) */ case MAC_IDLE: #if (MAC_ASSOCIATION_REQUEST_CONFIRM == 1) case MAC_ASSOCIATED: #endif /* (MAC_ASSOCIATION_REQUEST_CONFIRM == 1) */ #if (MAC_START_REQUEST_CONFIRM == 1) case MAC_COORDINATOR: #endif /* (MAC_START_REQUEST_CONFIRM == 1) */ { /* Is it a Beacon from our parent? */ switch (mac_parse_data.frame_type) { #if (MAC_SYNC_REQUEST == 1) case FCF_FRAMETYPE_BEACON: { uint32_t beacon_tx_time_symb; /* Check for PAN-Id conflict being NOT a PAN * Corodinator. */ #if (MAC_PAN_ID_CONFLICT_NON_PC == 1) if (mac_pib.mac_AssociatedPANCoord && (MAC_IDLE != mac_state)) { check_for_pan_id_conflict_non_pc(false); } #endif /* (MAC_PAN_ID_CONFLICT_NON_PC == 1) */ /* Check if the beacon is received from my * parent. */ if ((mac_parse_data.src_panid == tal_pib.PANId) && (((mac_parse_data.src_addr_mode == FCF_SHORT_ADDR) && (mac_parse_data.src_addr. short_address == mac_pib.mac_CoordShortAddress)) || ((mac_parse_data.src_addr_mode == FCF_LONG_ADDR) && (mac_parse_data.src_addr. long_address == mac_pib.mac_CoordExtendedAddress)))) { beacon_tx_time_symb = TAL_CONVERT_US_TO_SYMBOLS( f_ptr->time_stamp); #if (_DEBUG_ > 0) retval_t set_status = #endif set_tal_pib_internal(macBeaconTxTime, (void *)&beacon_tx_time_symb); #if (_DEBUG_ > 0) Assert(MAC_SUCCESS == set_status); #endif if ((MAC_SYNC_TRACKING_BEACON == mac_sync_state) || (MAC_SYNC_BEFORE_ASSOC == mac_sync_state) ) { uint32_t nxt_bcn_tm; uint32_t beacon_int_symb; /* Process a received beacon. */ mac_process_beacon_frame(b_ptr); /* Initialize beacon tracking * timer. */ { retval_t tmr_start_res = FAILURE; #ifdef BEACON_SUPPORT if (tal_pib.BeaconOrder < NON_BEACON_NWK) { beacon_int_symb = TAL_GET_BEACON_INTERVAL_TIME( tal_pib.BeaconOrder); } else #endif /* BEACON_SUPPORT */ { beacon_int_symb = TAL_GET_BEACON_INTERVAL_TIME( BO_USED_FOR_MAC_PERS_TIME); } pal_timer_stop( T_Beacon_Tracking_Period); #if (_DEBUG_ > 0) if (pal_is_timer_running( T_Beacon_Tracking_Period)) { Assert( "Bcn tmr running" == 0); } #endif do { /* * Calculate the * time for next * beacon * transmission */ beacon_tx_time_symb = tal_add_time_symbols( beacon_tx_time_symb, beacon_int_symb); /* * Take into * account the * time taken by * the radio to * wakeup from * sleep state */ nxt_bcn_tm = tal_sub_time_symbols( beacon_tx_time_symb, TAL_RADIO_WAKEUP_TIME_SYM << ( tal_pib . BeaconOrder + 2)); tmr_start_res = pal_timer_start( T_Beacon_Tracking_Period, TAL_CONVERT_SYMBOLS_TO_US( nxt_bcn_tm), TIMEOUT_ABSOLUTE, ( FUNC_PTR)mac_t_tracking_beacons_cb, NULL); } while (MAC_SUCCESS != tmr_start_res); #ifdef GTS_DEBUG port_pin_toggle_output_level( DEBUG_PIN1); port_pin_set_output_level( DEBUG_PIN2, 0); #endif } /* * Initialize superframe timer * if required only * for devices because * Superframe timer is already * running for * coordinator. */ /* TODO */ if (MAC_ASSOCIATED == mac_state) { mac_superframe_state = MAC_ACTIVE_CAP; /* Check whether the * radio needs to be * woken up. */ mac_trx_wakeup(); /* Set transceiver in rx * mode, otherwise it * may stay in * TRX_OFF). */ tal_rx_enable(PHY_RX_ON); if (tal_pib. SuperFrameOrder < tal_pib . BeaconOrder) { pal_timer_start( T_Superframe, TAL_CONVERT_SYMBOLS_TO_US( TAL_GET_SUPERFRAME_DURATION_TIME( tal_pib . SuperFrameOrder)), TIMEOUT_RELATIVE, ( FUNC_PTR)mac_t_start_inactive_device_cb, NULL); #ifdef GTS_DEBUG port_pin_set_output_level( DEBUG_PIN2, 1); #endif } #ifdef GTS_SUPPORT if (mac_final_cap_slot < FINAL_CAP_SLOT_DEFAULT) { uint32_t gts_tx_time = ( TAL_CONVERT_SYMBOLS_TO_US( TAL_GET_SUPERFRAME_DURATION_TIME( tal_pib . SuperFrameOrder)) >> 4) * ( mac_final_cap_slot + 1); pal_timer_start( T_CAP, gts_tx_time, TIMEOUT_RELATIVE, ( FUNC_PTR)mac_t_gts_cb, NULL); #ifdef GTS_DEBUG port_pin_set_output_level( DEBUG_PIN3, 1); #endif } #endif /* GTS_SUPPORT */ } /* Initialize missed beacon * timer. */ mac_start_missed_beacon_timer(); /* A device that is neither * scanning nor polling shall go * to sleep now. */ if ( (MAC_COORDINATOR != mac_state) && (MAC_SCAN_IDLE == mac_scan_state) && (MAC_POLL_IDLE == mac_poll_state) ) { /* * If the last received * beacon frame from our * parent * has indicated pending * broadbast data, we * need to * stay awake, until the * broadcast data has * been received. */ if (! mac_bc_data_indicated) { /* Set radio to * sleep if * allowed */ mac_sleep_trans(); } } } else if (MAC_SYNC_ONCE == mac_sync_state) { mac_process_beacon_frame(b_ptr); /* Do this after processing the * beacon. */ mac_sync_state = MAC_SYNC_NEVER; /* A device that is neither * scanning nor polling shall go * to sleep now. */ if ( (MAC_COORDINATOR != mac_state) && (MAC_SCAN_IDLE == mac_scan_state) && (MAC_POLL_IDLE == mac_poll_state) ) { /* * If the last received * beacon frame from our * parent * has indicated pending * broadbast data, we * need to * stay awake, until the * broadcast data has * been received. */ if (! mac_bc_data_indicated) { /* Set radio to * sleep if * allowed */ mac_sleep_trans(); } } } else { /* Process the beacon frame */ bmm_buffer_free(b_ptr); } processed_in_not_transient = true; } else {
void watchdog_early_warning_callback(void) { port_pin_toggle_output_level(LED_0_PIN); }
/** * \brief Set Toggles LED0 on Xplained board */ void led_toggle(void) { port_pin_toggle_output_level(LED_0_PIN); }
/** Handler for the device SysTick module, called when the SysTick counter * reaches the set period. * * \note As this is a raw device interrupt, the function name is significant * and must not be altered to ensure it is hooked into the device's * vector table. */ void SysTick_Handler(void) { port_pin_toggle_output_level(LED_0_PIN); }
/** * \brief Configures the DAC in event triggered mode. * * Configures the DAC to use the module's default configuration, with output * channel mode configured for event triggered conversions. * * \param dev_inst Pointer to the DAC module software instance to initialize */ static void configure_dac(struct dac_module *dac_module) { struct dac_config config; struct dac_chan_config channel_config; /* Get the DAC default configuration */ dac_get_config_defaults(&config); /* Switch to GCLK generator 0 */ config.clock_source = GCLK_GENERATOR_0; dac_init(dac_module, DAC, &config); /* Get the default DAC channel config */ dac_chan_get_config_defaults(&channel_config); /* Set the channel configuration, and enable it */ dac_chan_set_config(dac_module, DAC_CHANNEL_0, &channel_config); dac_chan_enable(dac_module, DAC_CHANNEL_0); /* Enable event triggered conversions */ struct dac_events events = { .on_event_start_conversion = true }; dac_enable_events(dac_module, &events); dac_enable(dac_module); } /** * \brief Configures the TC to generate output events at the sample frequency. * * Configures the TC in Frequency Generation mode, with an event output once * each time the audio sample frequency period expires. * * \param dev_inst Pointer to the TC module software instance to initialize */ static void configure_tc(struct tc_module *tc_module) { struct tc_config config; tc_get_config_defaults(&config); config.clock_source = GCLK_GENERATOR_0; config.wave_generation = TC_WAVE_GENERATION_MATCH_FREQ; tc_init(tc_module, TC3, &config); /* Enable periodic event output generation */ struct tc_events events = { .generate_event_on_overflow = true }; tc_enable_events(tc_module, &events); /* Set the timer top value to alter the overflow frequency */ tc_set_top_value(tc_module, system_gclk_gen_get_hz(GCLK_GENERATOR_0) / sample_rate); tc_enable(tc_module); } /** * \brief Configures the event system to link the sample timer to the DAC. * * Configures the event system, linking the TC module used for the audio sample * rate timing to the DAC, so that a new conversion is triggered each time the * DAC receives an event from the timer. */ static void configure_events(struct events_resource *event) { struct events_config config; events_get_config_defaults(&config); config.generator = EVSYS_ID_GEN_TC3_OVF; config.path = EVENTS_PATH_ASYNCHRONOUS; events_allocate(event, &config); events_attach_user(event, EVSYS_ID_USER_DAC_START); } /** * \brief Main application routine */ int main(void) { struct dac_module dac_module; struct tc_module tc_module; struct events_resource event; /* Initialize all the system clocks, pm, gclk... */ system_init(); /* Enable the internal bandgap to use as reference to the DAC */ system_voltage_reference_enable(SYSTEM_VOLTAGE_REFERENCE_BANDGAP); /* Module configuration */ configure_tc(&tc_module); configure_dac(&dac_module); configure_events(&event); /* Start the sample trigger timer */ tc_start_counter(&tc_module); while (true) { while (port_pin_get_input_level(SW0_PIN) == SW0_INACTIVE) { /* Wait for the button to be pressed */ } port_pin_toggle_output_level(LED0_PIN); for (uint32_t i = 0; i < number_of_samples; i++) { dac_chan_write(&dac_module, DAC_CHANNEL_0, wav_samples[i]); while (!(DAC->INTFLAG.reg & DAC_INTFLAG_EMPTY)) { /* Wait for data buffer to be empty */ } } while (port_pin_get_input_level(SW0_PIN) == SW0_ACTIVE) { /* Wait for the button to be depressed */ } } }
//--------------------Functions------------------ void USART_RX_callback(const struct usart_module *const usart_module) { port_pin_toggle_output_level(LED0_PIN); }
void usart_write_callback(const struct usart_module *const usart_module) { port_pin_toggle_output_level(LED_0_PIN); }
////////////////////////////////////////////////////////////////////////// // TCC Callback Function // -> This function only toggles the LED when TCC is triggered static void tcc_callback_function( struct tcc_module *const module_instance ) { usart_write_buffer_job(&irda_master, slave_tx_buffer, 1); // Send only one character port_pin_toggle_output_level(LED_DEBUG); // Toggle LED }
/** Set up the measurement and comparison timer events. * - Configure the reference timer to generate an event upon comparison * match with channel 0. * - Configure the measurement timer to trigger a capture when an event is * received. */ static void setup_tc_events(void) { /* Enable incoming events on on measurement timer */ struct tc_events events_calib = { .on_event_perform_action = true }; tc_enable_events(&tc_calib, &events_calib); /* Generate events from the reference timer on channel 0 compare match */ struct tc_events events_comp = { .generate_event_on_compare_channel[0] = true }; tc_enable_events(&tc_comp, &events_comp); tc_enable(&tc_calib); tc_enable(&tc_comp); } /** Set up the event system, linking the measurement and comparison timers so * that events generated from the reference timer are linked to the measurement * timer. */ static void setup_events(struct events_resource *event) { struct events_config config; events_get_config_defaults(&config); /* The event channel detects rising edges of the reference timer output * event */ config.edge_detect = EVENTS_EDGE_DETECT_RISING; config.path = EVENTS_PATH_SYNCHRONOUS; config.generator = CONF_EVENT_GENERATOR_ID; events_allocate(event, &config); events_attach_user(event, CONF_EVENT_USED_ID); } /** Set up the USART for transmit-only communication at a fixed baud rate. */ static void setup_usart_channel(void) { struct usart_config cdc_uart_config; usart_get_config_defaults(&cdc_uart_config); /* Configure the USART settings and initialize the standard I/O library */ cdc_uart_config.mux_setting = EDBG_CDC_SERCOM_MUX_SETTING; cdc_uart_config.pinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0; cdc_uart_config.pinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1; cdc_uart_config.pinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2; cdc_uart_config.pinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3; cdc_uart_config.baudrate = 115200; stdio_serial_init(&usart_edbg, EDBG_CDC_MODULE, &cdc_uart_config); usart_enable(&usart_edbg); } /** Set up the clock output pin so that the current system clock frequency can * be monitored via an external frequency counter or oscilloscope. */ static void setup_clock_out_pin(void) { struct system_pinmux_config pin_mux; system_pinmux_get_config_defaults(&pin_mux); /* MUX out the system clock to a I/O pin of the device */ pin_mux.mux_position = CONF_CLOCK_PIN_MUX; system_pinmux_pin_set_config(CONF_CLOCK_PIN_OUT, &pin_mux); } /** Retrieves the current system clock frequency, computed from the reference * clock. * * \return Current system clock frequency in Hz. */ static uint32_t get_osc_frequency(void) { /* Clear any existing match status on the measurement timer */ tc_clear_status(&tc_comp, TC_STATUS_CHANNEL_0_MATCH); /* Restart both measurement and reference timers */ tc_start_counter(&tc_calib); tc_start_counter(&tc_comp); /* Wait for the measurement timer to signal a compare match */ while (!(tc_get_status(&tc_comp) & TC_STATUS_CHANNEL_0_MATCH)) { /* Wait for channel 0 match */ } /* Compute the real clock frequency from the measurement timer count and * reference count */ uint64_t tmp = tc_get_capture_value(&tc_calib, TC_COMPARE_CAPTURE_CHANNEL_0); return ((tmp * REFERENCE_CLOCK_HZ) >> CALIBRATION_RESOLUTION); } int main(void) { struct events_resource event; /* System initialization */ system_init(); delay_init(); /* Module initialization */ setup_tc_channels(); setup_tc_events(); setup_events(&event); setup_clock_out_pin(); /* Init the variables with default calibration settings */ uint8_t frange_cal = SYSCTRL->OSC8M.bit.FRANGE; uint8_t temp_cal = SYSCTRL->OSC8M.bit.CALIB >> TEMP_CAL_OFFSET; uint8_t comm_cal = SYSCTRL->OSC8M.bit.CALIB & COMM_CAL_MAX; /* Set the calibration test range */ uint8_t frange_cal_min = max((frange_cal - CONF_FRANGE_CAL), FRANGE_CAL_MIN); uint8_t frange_cal_max = min((frange_cal + CONF_FRANGE_CAL), FRANGE_CAL_MAX); uint8_t temp_cal_min = max((temp_cal - CONF_TEMP_CAL), TEMP_CAL_MIN); uint8_t temp_cal_max = min((temp_cal + CONF_TEMP_CAL), TEMP_CAL_MAX); /* Variables to track the previous and best calibration settings */ uint16_t comm_best = 0; uint8_t frange_best = 0; uint32_t freq_best = 0; uint32_t freq_before = get_osc_frequency(); /* Run calibration loop */ for (frange_cal = frange_cal_min; frange_cal <= frange_cal_max; frange_cal++) { for (temp_cal = temp_cal_min; temp_cal <= temp_cal_max; temp_cal++) { for (comm_cal = COMM_CAL_MIN; comm_cal <= COMM_CAL_MAX; comm_cal++) { /* Set the test calibration values */ system_clock_source_write_calibration( SYSTEM_CLOCK_SOURCE_OSC8M, (temp_cal << 7) | comm_cal, frange_cal); /* Wait for stabilization */ delay_cycles(1000); /* Compute the deltas of the current and best system clock * frequencies, save current settings if they are closer to the * ideal frequency than the previous best values */ uint32_t freq_current = get_osc_frequency(); if (abs(freq_current - TARGET_FREQUENCY) < abs(freq_best - TARGET_FREQUENCY)) { freq_best = freq_current; comm_best = comm_cal; frange_best = frange_cal; port_pin_set_output_level(LED_0_PIN, LED_0_ACTIVE); } else { port_pin_set_output_level(LED_0_PIN, !LED_0_ACTIVE); } } } } /* Set the found best calibration values */ system_clock_source_write_calibration( SYSTEM_CLOCK_SOURCE_OSC8M, (temp_cal << 7) | comm_best, frange_best); /* Setup USART module to output results */ setup_usart_channel(); /* Write previous and current frequency and new calibration settings to the * USART */ printf("Freq Before: %lu\r\n", freq_before); printf("Freq Best: %lu\r\n", freq_best); printf("Freq Range: %u\r\n", frange_best); printf("Calib Value: 0x%x\r\n", (temp_cal << 7) | comm_best); /* Rapidly flash the board LED to signal the calibration completion */ while (1) { port_pin_toggle_output_level(LED_0_PIN); delay_ms(200); } }
/* See the description at the top of this file. */ static void prvCheckTimerCallback( xTimerHandle xTimer ) { static long lChangedTimerPeriodAlready = pdFALSE; static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0; unsigned long ulErrorFound = pdFALSE; /* Check all the demo and test tasks to ensure that they are all still running, and that none have detected an error. */ if( xAreDynamicPriorityTasksStillRunning() != pdPASS ) { ulErrorFound = pdTRUE; } if( xAreBlockTimeTestTasksStillRunning() != pdPASS ) { ulErrorFound = pdTRUE; } if( xAreCountingSemaphoreTasksStillRunning() != pdPASS ) { ulErrorFound = pdTRUE; } if( xAreRecursiveMutexTasksStillRunning() != pdPASS ) { ulErrorFound = pdTRUE; } /* Check that the register test 1 task is still running. */ if( ulLastRegTest1Value == ulRegTest1LoopCounter ) { ulErrorFound = pdTRUE; } ulLastRegTest1Value = ulRegTest1LoopCounter; /* Check that the register test 2 task is still running. */ if( ulLastRegTest2Value == ulRegTest2LoopCounter ) { ulErrorFound = pdTRUE; } ulLastRegTest2Value = ulRegTest2LoopCounter; if( xAreQueueSetTasksStillRunning() != pdPASS ) { ulErrorFound = pdTRUE; } if( xIsQueueOverwriteTaskStillRunning() != pdPASS ) { ulErrorFound = pdTRUE; } if( xAreGenericQueueTasksStillRunning() != pdPASS ) { ulErrorFound = pdTRUE; } if( xAreQueuePeekTasksStillRunning() != pdPASS ) { ulErrorFound = pdTRUE; } /* Toggle the check LED to give an indication of the system status. If the LED toggles every mainCHECK_TIMER_PERIOD_MS milliseconds then everything is ok. A faster toggle indicates an error. */ port_pin_toggle_output_level( LED_0_PIN ); /* Have any errors been latched in ulErrorFound? If so, shorten the period of the check timer to mainERROR_CHECK_TIMER_PERIOD_MS milliseconds. This will result in an increase in the rate at which the LED toggles. */ if( ulErrorFound != pdFALSE ) { if( lChangedTimerPeriodAlready == pdFALSE ) { lChangedTimerPeriodAlready = pdTRUE; /* This call to xTimerChangePeriod() uses a zero block time. Functions called from inside of a timer callback function must *never* attempt to block. */ xTimerChangePeriod( xTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK ); } } }
static void _rtc_timer_int_cb(void) { port_pin_toggle_output_level(PIN_PB30); }