diff --git a/code/b-parasite/Makefile b/code/b-parasite/Makefile index 062f041..f98161b 100644 --- a/code/b-parasite/Makefile +++ b/code/b-parasite/Makefile @@ -48,6 +48,7 @@ SRC_FILES += \ $(SDK_ROOT)/modules/nrfx/drivers/src/nrfx_gpiote.c \ $(SDK_ROOT)/modules/nrfx/drivers/src/nrfx_rtc.c \ $(SDK_ROOT)/modules/nrfx/drivers/src/prs/nrfx_prs.c \ + $(SDK_ROOT)/modules/nrfx/drivers/src/nrfx_pwm.c \ $(SDK_ROOT)/modules/nrfx/drivers/src/nrfx_uart.c \ $(SDK_ROOT)/modules/nrfx/drivers/src/nrfx_uarte.c \ $(SDK_ROOT)/components/libraries/bsp/bsp.c \ @@ -62,6 +63,7 @@ SRC_FILES += \ $(SDK_ROOT)/components/softdevice/common/nrf_sdh_ble.c \ $(SDK_ROOT)/components/softdevice/common/nrf_sdh_soc.c \ $(PROJ_DIR)/prst/ble.c \ + $(PROJ_DIR)/prst/pwm.c \ $(PROJ_DIR)/prst/rtc.c \ # Include folders common to all targets diff --git a/code/b-parasite/config/prst_config.h b/code/b-parasite/config/prst_config.h index d1c4936..4f4276d 100644 --- a/code/b-parasite/config/prst_config.h +++ b/code/b-parasite/config/prst_config.h @@ -1,10 +1,16 @@ #ifndef _PRST_CONFIG_H_ #define _PRST_CONFIG_H_ +#include "nrf_gpio.h" + // Deep sleep. #define PRST_DEEP_SLEEP_IN_SECONDS 2 // BLE. #define PRST_BLE_ADV_NAME "Prst" +// PWM. +#define PRST_PWM_PIN NRF_GPIO_PIN_MAP(0, 29) +#define PRST_PWM_FREQUENCY 500000 + #endif // _PRST_CONFIG_H_ \ No newline at end of file diff --git a/code/b-parasite/config/sdk_config.h b/code/b-parasite/config/sdk_config.h index 0f2fcb6..ee7c58e 100644 --- a/code/b-parasite/config/sdk_config.h +++ b/code/b-parasite/config/sdk_config.h @@ -2710,13 +2710,13 @@ // NRFX_PWM_ENABLED - nrfx_pwm - PWM peripheral driver //========================================================== #ifndef NRFX_PWM_ENABLED -#define NRFX_PWM_ENABLED 0 +#define NRFX_PWM_ENABLED 1 #endif // NRFX_PWM0_ENABLED - Enable PWM0 instance #ifndef NRFX_PWM0_ENABLED -#define NRFX_PWM0_ENABLED 0 +#define NRFX_PWM0_ENABLED 1 #endif // NRFX_PWM1_ENABLED - Enable PWM1 instance @@ -4949,7 +4949,7 @@ // PWM_ENABLED - nrf_drv_pwm - PWM peripheral driver - legacy layer //========================================================== #ifndef PWM_ENABLED -#define PWM_ENABLED 0 +#define PWM_ENABLED 1 #endif // PWM_DEFAULT_CONFIG_OUT0_PIN - Out0 pin <0-31> @@ -5049,7 +5049,7 @@ #ifndef PWM0_ENABLED -#define PWM0_ENABLED 0 +#define PWM0_ENABLED 1 #endif // PWM1_ENABLED - Enable PWM1 instance diff --git a/code/b-parasite/src/main.c b/code/b-parasite/src/main.c index 56d6dae..43be309 100644 --- a/code/b-parasite/src/main.c +++ b/code/b-parasite/src/main.c @@ -16,6 +16,7 @@ #include "nrf_sdh_ble.h" #include "nrf_soc.h" #include "prst/ble.h" +#include "prst/pwm.h" #include "prst/rtc.h" // P0.03 @@ -79,6 +80,8 @@ int main(void) { log_init(); leds_init(); power_management_init(); + prst_pwm_init(); + prst_pwm_start(); prst_ble_init(); prst_rtc_set_callback(rtc_callback); diff --git a/code/b-parasite/src/prst/ble.c b/code/b-parasite/src/prst/ble.c index eed18de..7c13f2d 100644 --- a/code/b-parasite/src/prst/ble.c +++ b/code/b-parasite/src/prst/ble.c @@ -58,9 +58,6 @@ static uint8_t adv_handle_ = BLE_GAP_ADV_SET_HANDLE_NOT_SET; static ble_gap_adv_params_t adv_params_; static void init_advertisement_data() { - UNUSED_VARIABLE(adv_data_); - UNUSED_VARIABLE(advdata_service_data_); - // We'll just broadcast our data, so we disallow connections and scan // requests. adv_params_.properties.type = diff --git a/code/b-parasite/src/prst/pwm.c b/code/b-parasite/src/prst/pwm.c new file mode 100644 index 0000000..bcafed4 --- /dev/null +++ b/code/b-parasite/src/prst/pwm.c @@ -0,0 +1,50 @@ +#include "prst/pwm.h" + +#include +#include +#include +#include +#include + +#include "prst_config.h" + +#define PRST_PWM_BASE_FREQ NRF_PWM_CLK_16MHz + +static nrf_drv_pwm_t m_pwm0 = NRF_DRV_PWM_INSTANCE(0); + +static nrf_pwm_values_common_t seq_values_[] = {8}; +static const nrf_pwm_sequence_t seq_ = { + .values.p_common = seq_values_, + .length = NRF_PWM_VALUES_LENGTH(seq_values_), + .repeats = 0, + .end_delay = 0}; + +void prst_pwm_init() { + UNUSED_VARIABLE(seq_); + + nrf_drv_pwm_config_t const config0 = { + .output_pins = + { + PRST_PWM_PIN | NRF_DRV_PWM_PIN_INVERTED, // channel 0 + NRF_DRV_PWM_PIN_NOT_USED, // channel 1 + NRF_DRV_PWM_PIN_NOT_USED, // channel 2 + NRF_DRV_PWM_PIN_NOT_USED, // channel 3 + }, + .irq_priority = APP_IRQ_PRIORITY_LOWEST, + // This is the hal PRESCALER. + .base_clock = NRF_PWM_CLK_16MHz, + // This is the hal COUNTERTOP. + .count_mode = NRF_PWM_MODE_UP_AND_DOWN, + // .top_value = (uint16_t)(1e16 / PRST_PWM_FREQUENCY), + .top_value = 16, + .load_mode = NRF_PWM_LOAD_COMMON, + .step_mode = NRF_PWM_STEP_AUTO}; + APP_ERROR_CHECK(nrf_drv_pwm_init(&m_pwm0, &config0, NULL)); +} + +void prst_pwm_start() { + APP_ERROR_CHECK( + nrf_drv_pwm_simple_playback(&m_pwm0, &seq_, 1, NRF_DRV_PWM_FLAG_LOOP)); +} + +void prst_pwm_stop() {} \ No newline at end of file diff --git a/code/b-parasite/src/prst/pwm.h b/code/b-parasite/src/prst/pwm.h new file mode 100644 index 0000000..3c22bb5 --- /dev/null +++ b/code/b-parasite/src/prst/pwm.h @@ -0,0 +1,10 @@ +#ifndef _PRST_PWM_H_ +#define _PRST_PWM_H_ + +void prst_pwm_init(); + +void prst_pwm_start(); + +void prst_pwm_stop(); + +#endif // _PRST_PWM_H_ \ No newline at end of file