From 9c9492567f48501e24abc952563818228eab1833 Mon Sep 17 00:00:00 2001 From: rbaron Date: Sun, 13 Nov 2022 11:20:31 +0100 Subject: [PATCH] PWM kinda works --- code/nrf-connect/dts/bindings/pwm-fixed.yaml | 18 +++++++++++ code/nrf-connect/nrf52840dk_nrf52840.overlay | 23 ++++++------- code/nrf-connect/prj.conf | 1 + code/nrf-connect/src/main.c | 13 +++++++- code/nrf-connect/src/prst/adc.c | 3 ++ code/nrf-connect/src/prst/adc.h | 34 ++++++++++++++++++++ 6 files changed, 78 insertions(+), 14 deletions(-) create mode 100644 code/nrf-connect/dts/bindings/pwm-fixed.yaml create mode 100644 code/nrf-connect/src/prst/adc.c create mode 100644 code/nrf-connect/src/prst/adc.h diff --git a/code/nrf-connect/dts/bindings/pwm-fixed.yaml b/code/nrf-connect/dts/bindings/pwm-fixed.yaml new file mode 100644 index 0000000..7b111df --- /dev/null +++ b/code/nrf-connect/dts/bindings/pwm-fixed.yaml @@ -0,0 +1,18 @@ +# Hello + +description: A fixed frequency & pulse PWM. + +compatible: pwm-fixed + +include: base.yaml + +properties: + pwms: + type: phandle-array + required: true + description: the PWM spec. + + pulse: + required: true + type: int + description: The period of the PWM pulse. \ No newline at end of file diff --git a/code/nrf-connect/nrf52840dk_nrf52840.overlay b/code/nrf-connect/nrf52840dk_nrf52840.overlay index 63796e9..4193d47 100644 --- a/code/nrf-connect/nrf52840dk_nrf52840.overlay +++ b/code/nrf-connect/nrf52840dk_nrf52840.overlay @@ -1,17 +1,5 @@ -// To get started, press Ctrl+Space (or Option+Esc) to bring up the completion menu and view the available nodes. - -// You can also use the buttons in the sidebar to perform actions on nodes. -// Actions currently available include: - -// * Enabling / disabling the node -// * Adding the bus to a bus -// * Removing the node -// * Connecting ADC channels - -// For more help, browse the DeviceTree documentation at https: //docs.zephyrproject.org/latest/guides/dts/index.html -// You can also visit the nRF DeviceTree extension documentation at https: //nrfconnect.github.io/vscode-nrf-connect/devicetree/nrfdevicetree.html - &pinctrl { + /* Configure pwm0 instance to use pin 5. */ pwm0_default: pwm0_default { group1 { psels = ; @@ -26,6 +14,7 @@ }; }; + /* Configure i2c0 instance to use pins 24 (SDA) & 13 (SCL). */ i2c0_default: i2c0_default { group1 { psels = , @@ -49,3 +38,11 @@ label = "SHTC3"; }; }; + +/ { + soil_pwm: soil_pwm { + compatible = "pwm-fixed"; + pwms = <&pwm0 0 PWM_MSEC(100) PWM_POLARITY_INVERTED>; + pulse = ; + }; +}; \ No newline at end of file diff --git a/code/nrf-connect/prj.conf b/code/nrf-connect/prj.conf index fcb7350..a7d8b01 100644 --- a/code/nrf-connect/prj.conf +++ b/code/nrf-connect/prj.conf @@ -1,3 +1,4 @@ CONFIG_LOG=y +CONFIG_PWM=y CONFIG_CBPRINTF_FP_SUPPORT=y CONFIG_I2C=y \ No newline at end of file diff --git a/code/nrf-connect/src/main.c b/code/nrf-connect/src/main.c index 0fab6a1..8e00ace 100644 --- a/code/nrf-connect/src/main.c +++ b/code/nrf-connect/src/main.c @@ -1,4 +1,5 @@ #include +#include #include #include @@ -6,6 +7,16 @@ LOG_MODULE_REGISTER(main, LOG_LEVEL_DBG); +// PWM_DT_SPEC_GET(DT_LABEL(pwm0)); +static const struct pwm_dt_spec soil_pwm_dt = + PWM_DT_SPEC_GET(DT_NODELABEL(soil_pwm)); +static const uint32_t pulse = DT_PROP(DT_NODELABEL(soil_pwm), pulse); + void main(void) { - prst_shtc3_read_t shtc3_read = prst_shtc3_read(); + // prst_shtc3_read_t shtc3_read = prst_shtc3_read(); + pwm_set_pulse_dt(&soil_pwm_dt, pulse); + + while (true) { + k_msleep(500); + } } diff --git a/code/nrf-connect/src/prst/adc.c b/code/nrf-connect/src/prst/adc.c new file mode 100644 index 0000000..ac63d0c --- /dev/null +++ b/code/nrf-connect/src/prst/adc.c @@ -0,0 +1,3 @@ +#include "adc.h" + +prst_adc_photo_sensor_t prst_adc_photo_read(double battery_voltage) {} \ No newline at end of file diff --git a/code/nrf-connect/src/prst/adc.h b/code/nrf-connect/src/prst/adc.h new file mode 100644 index 0000000..3b568b8 --- /dev/null +++ b/code/nrf-connect/src/prst/adc.h @@ -0,0 +1,34 @@ +#ifndef _PRST_ADC_H_ +#define _PRST_ADC_H_ + +#include + +typedef struct prst_adc_batt_val { + int16_t raw; + uint16_t millivolts; + double voltage; +} prst_adc_batt_read_t; + +typedef struct prst_adc_soil_moisture { + int16_t raw; + // A value from 0 (completely dry) to 2^10 (completely wet). + uint16_t relative; + double percentage; +} prst_adc_soil_moisture_t; + +typedef struct prst_adc_photo_sensor { + int16_t raw; + double voltage; + // Value in lux. + uint16_t brightness; +} prst_adc_photo_sensor_t; + +void prst_adc_init(); + +prst_adc_batt_read_t prst_adc_batt_read(); + +prst_adc_soil_moisture_t prst_adc_soil_read(double battery_voltage); + +prst_adc_photo_sensor_t prst_adc_photo_read(double battery_voltage); + +#endif // _PRST_ADC_H_ \ No newline at end of file