Skip to content

Instantly share code, notes, and snippets.

@kschoos
Created June 30, 2019 20:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kschoos/535c6703ddfd06e26c5c1092d8513df4 to your computer and use it in GitHub Desktop.
Save kschoos/535c6703ddfd06e26c5c1092d8513df4 to your computer and use it in GitHub Desktop.
#include <stdbool.h>
#include <stdint.h>
#include "boards.h"
#include "bsp.h"
#include "app_timer.h"
#include "nordic_common.h"
#include "nrf_error.h"
#include "nrfx_timer.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#define BUTTON_PREV_ID 0 /**< Button used to switch the state. */
#define BUTTON_NEXT_ID 1 /**< Button used to switch the state. */
#define CYCLE_PERIOD 300
static const nrfx_timer_t m_timer0 = NRFX_TIMER_INSTANCE(0);
static const nrfx_timer_t m_timer1 = NRFX_TIMER_INSTANCE(1);
static const nrfx_timer_t m_timer3 = NRFX_TIMER_INSTANCE(3);
static uint8_t state = 0;
static const uint8_t num_states = 3;
void display_leds(uint8_t const val){
bsp_board_leds_off();
for(int i = 0; i < 4; i++)
if((1 << i) & val) bsp_board_led_on(i);
}
static void timer0_event_handler(nrf_timer_event_t event_type, void * p_context)
{
switch(state)
{
case 0:
case 1:
case 2:
break;
}
}
static void timer1_event_handler(nrf_timer_event_t event_type, void * p_context)
{
switch(state)
{
case 0:
break;
case 1:
nrfx_timer_increment(&m_timer0);
display_leds(nrfx_timer_capture(&m_timer0, NRF_TIMER_CC_CHANNEL0));
nrfx_timer_clear(&m_timer1);
break;
case 2:
nrfx_timer_enable(&m_timer3);
break;
}
}
static void timer3_event_handler(nrf_timer_event_t event_type, void * p_context)
{
switch(state)
{
case 0:
case 1:
break;
case 2:
bsp_board_leds_off();
switch(event_type){
case NRF_TIMER_EVENT_COMPARE0:
bsp_board_led_on(0);
break;
case NRF_TIMER_EVENT_COMPARE1:
bsp_board_led_on(1);
break;
case NRF_TIMER_EVENT_COMPARE2:
bsp_board_led_on(3);
break;
case NRF_TIMER_EVENT_COMPARE3:
bsp_board_led_on(2);
break;
case NRF_TIMER_EVENT_COMPARE4:
nrfx_timer_disable(&m_timer3);
break;
}
break;
}
}
void bsp_evt_handler(bsp_event_t evt)
{
uint32_t err_code;
switch (evt)
{
case BSP_EVENT_KEY_0:
if(state > 0) break;
nrfx_timer_increment(&m_timer0);
display_leds(nrfx_timer_capture(&m_timer0, NRF_TIMER_CC_CHANNEL0));
break;
case BSP_EVENT_KEY_1:
state = (state + 1) % num_states;
break;
default:
return;
}
}
/**@brief Function for initializing low frequency clock.
*/
void clock_initialization()
{
NRF_CLOCK->LFCLKSRC = (CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos);
NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
NRF_CLOCK->TASKS_LFCLKSTART = 1;
while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0)
{
// Do nothing.
}
}
/**@brief Function for initializing bsp module.
*/
void bsp_configuration()
{
uint32_t err_code;
err_code = bsp_init(BSP_INIT_LEDS | BSP_INIT_BUTTONS, bsp_evt_handler);
APP_ERROR_CHECK(err_code);
}
void timer0_initialization(){
// Check TIMER0 configuration for details.
nrfx_timer_config_t timer_cfg = NRFX_TIMER_DEFAULT_CONFIG;
ret_code_t err_code = nrfx_timer_init(&m_timer0, &timer_cfg, timer0_event_handler);
APP_ERROR_CHECK(err_code);
nrfx_timer_extended_compare(&m_timer0,
NRF_TIMER_CC_CHANNEL0,
16,
NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK,
true);
NRF_LOG_INFO("Timer 0 initialized.");
NRF_LOG_FLUSH();
nrfx_timer_enable(&m_timer0);
}
void timer1_initialization(){
nrfx_timer_config_t timer_cfg = NRFX_TIMER_DEFAULT_CONFIG;
timer_cfg.bit_width = 3;
timer_cfg.mode = 0;
NRF_LOG_INFO("Initializing Timer 1...");
ret_code_t err_code = nrfx_timer_init(&m_timer1, &timer_cfg, timer1_event_handler);
APP_ERROR_CHECK(err_code);
nrfx_timer_extended_compare(&m_timer1,
NRF_TIMER_CC_CHANNEL0,
0x7A12, // At the lowest frequency, this is exactly a second.
NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK,
true);
NRF_LOG_INFO("Timer 1 initialized.");
NRF_LOG_FLUSH();
nrfx_timer_enable(&m_timer1);
}
void timer2_initialization(){
// Check TIMER0 configuration for details.
nrfx_timer_config_t timer_cfg = NRFX_TIMER_DEFAULT_CONFIG;
timer_cfg.bit_width = 3;
timer_cfg.mode = 0;
NRF_LOG_INFO("Initializing Timer 3...");
ret_code_t err_code = nrfx_timer_init(&m_timer3, &timer_cfg, timer3_event_handler);
APP_ERROR_CHECK(err_code);
nrfx_timer_extended_compare(&m_timer3,
NRF_TIMER_CC_CHANNEL0,
nrfx_timer_ms_to_ticks(&m_timer3, CYCLE_PERIOD/5), // At the lowest frequency, this is exactly a second.
NULL,
true);
nrfx_timer_extended_compare(&m_timer3,
NRF_TIMER_CC_CHANNEL1,
2*nrfx_timer_ms_to_ticks(&m_timer3, CYCLE_PERIOD/5), // At the lowest frequency, this is exactly a second.
NULL,
true);
nrfx_timer_extended_compare(&m_timer3,
NRF_TIMER_CC_CHANNEL2,
3*nrfx_timer_ms_to_ticks(&m_timer3, CYCLE_PERIOD/5), // At the lowest frequency, this is exactly a second.
NULL,
true);
nrfx_timer_extended_compare(&m_timer3,
NRF_TIMER_CC_CHANNEL3,
4*nrfx_timer_ms_to_ticks(&m_timer3, CYCLE_PERIOD/5), // At the lowest frequency, this is exactly a second.
NULL,
true);
nrfx_timer_extended_compare(&m_timer3,
NRF_TIMER_CC_CHANNEL4,
5*nrfx_timer_ms_to_ticks(&m_timer3, CYCLE_PERIOD/5), // At the lowest frequency, this is exactly a second.
NRF_TIMER_SHORT_COMPARE4_CLEAR_MASK,
true);
NRF_LOG_INFO("Timer 2 initialized.");
NRF_LOG_FLUSH();
}
int main(void)
{
clock_initialization();
uint32_t err_code = app_timer_init();
APP_ERROR_CHECK(err_code);
APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
NRF_LOG_DEFAULT_BACKENDS_INIT();
NRF_LOG_INFO("Timer Playground started.");
NRF_LOG_FLUSH();
bsp_configuration();
NRF_LOG_INFO("Bsp configuration done.");
NRF_LOG_FLUSH();
timer0_initialization();
timer1_initialization();
timer2_initialization();
// bsp_indication_set(BSP_INDICATE_BONDING);
while (true)
{
NRF_LOG_FLUSH();
__SEV();
__WFE();
__WFE();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment