Skip to content

Commit f875e27

Browse files
committed
initial proof of concept PWM
1 parent 791e99a commit f875e27

File tree

2 files changed

+52
-19
lines changed

2 files changed

+52
-19
lines changed

Makefile

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
# # GENDIR=/home/mydir/mygendir
6868
# SETDEFINES=FileDefines # settings which are called after definitions for board are done
6969
# # SETDEFINES=/home/mydir/myDefines
70-
# UNSUPPORTEDMAKE=FileUnsu# Adds additional files from unsupported sources(means not supported by Gordon) to actual make
70+
# UNSUPPORTEDMAKE=FileUnsu# Adds additional files from unsupported sources(means not supported by Gordon) to actual make
7171
# # UNSUPPORTEDMAKE=/home/mydir/unsupportedCommands
7272
# PROJECTNAME=myBigProject# Sets projectname
7373
# BLACKLIST=fileBlacklist # Removes javascript commands given in a file from compilation and therefore from project defined firmware
@@ -673,7 +673,7 @@ USE_HASHLIB=1
673673
USE_GRAPHICS=1
674674
USE_CRYPTO=1
675675
USE_TLS=1
676-
USE_TELNET=1
676+
USE_TELNET=1
677677
#USE_LCD_SDL=1
678678

679679
ifdef MACOSX
@@ -688,7 +688,7 @@ endif
688688
endif
689689
endif
690690

691-
#set or reset defines like USE_GRAPHIC from an external file to customize firmware
691+
#set or reset defines like USE_GRAPHIC from an external file to customize firmware
692692
ifdef SETDEFINES
693693
include $(SETDEFINES)
694694
endif
@@ -878,11 +878,11 @@ INCLUDE += -I$(ROOT)/libs/math
878878
WRAPPERSOURCES += libs/math/jswrap_math.c
879879
ifeq ($(FAMILY),ESP8266)
880880
# special ESP8266 maths lib that doesn't go into RAM
881-
LIBS += -lmirom
881+
LIBS += -lmirom
882882
LDFLAGS += -L$(ROOT)/targets/esp8266
883883
else
884884
# everything else uses normal maths lib
885-
LIBS += -lm
885+
LIBS += -lm
886886
endif
887887
endif
888888

@@ -1091,7 +1091,7 @@ libs/crypto/mbedtls/library/cipher_wrap.c \
10911091
libs/crypto/mbedtls/library/md.c \
10921092
libs/crypto/mbedtls/library/md_wrap.c \
10931093
libs/crypto/mbedtls/library/oid.c \
1094-
libs/crypto/mbedtls/library/pkcs5.c
1094+
libs/crypto/mbedtls/library/pkcs5.c
10951095
endif
10961096
endif
10971097

@@ -1290,7 +1290,7 @@ ifeq ($(FAMILY), NRF51)
12901290

12911291
NRF5X=1
12921292
NRF5X_SDK_PATH=$(ROOT)/targetlibs/nrf5x/nrf5_sdk
1293-
1293+
12941294
# ARCHFLAGS are shared by both CFLAGS and LDFLAGS.
12951295
ARCHFLAGS = -mcpu=cortex-m0 -mthumb -mabi=aapcs -mfloat-abi=soft # Use nRF51 makefiles provided in SDK as reference.
12961296

@@ -1312,7 +1312,7 @@ ifeq ($(FAMILY), NRF51)
13121312
NRF_BOOTLOADER = $(ROOT)/targetlibs/nrf5x/nrf5_singlebank_bl_hex/nrf51_s130_singlebank_bl.hex
13131313
NFR_BL_START_ADDR = 0x3C000
13141314
NRF_BOOTLOADER_SETTINGS = $(ROOT)/targetlibs/nrf5x/nrf5_singlebank_bl_hex/bootloader_settings_nrf51.hex # This file writes 0x3FC00 with 0x01 so we can flash the application with the bootloader.
1315-
1315+
13161316
endif
13171317

13181318
endif # FAMILY == NRF51
@@ -1324,7 +1324,7 @@ ifeq ($(FAMILY), NRF52)
13241324

13251325
# ARCHFLAGS are shared by both CFLAGS and LDFLAGS.
13261326
ARCHFLAGS = -mcpu=cortex-m4 -mthumb -mabi=aapcs -mfloat-abi=hard -mfpu=fpv4-sp-d16
1327-
1327+
13281328
# nRF52 specific.
13291329
INCLUDE += -I$(NRF5X_SDK_PATH)/../nrf52_config
13301330
INCLUDE += -I$(NRF5X_SDK_PATH)/components/softdevice/s132/headers
@@ -1444,12 +1444,12 @@ ifdef NRF5X
14441444

14451445
# Just try and get rid of the compile warnings.
14461446
CFLAGS += -Wno-sign-conversion -Wno-conversion -Wno-unused-parameter -fomit-frame-pointer #this is for device manager in nordic sdk
1447-
DEFINES += -DBLUETOOTH -D$(BOARD)
1447+
DEFINES += -DBLUETOOTH -D$(BOARD)
14481448

14491449
ARM = 1
14501450
ARM_HAS_OWN_CMSIS = 1 # Nordic uses its own CMSIS files in its SDK, these are up-to-date.
14511451
INCLUDE += -I$(ROOT)/targetlibs/nrf5x -I$(NRF5X_SDK_PATH)
1452-
1452+
14531453
TEMPLATE_PATH = $(ROOT)/targetlibs/nrf5x/nrf5x_linkers # This is where the common linker for both nRF51 & nRF52 is stored.
14541454
LDFLAGS += -L$(TEMPLATE_PATH)
14551455

@@ -1486,6 +1486,7 @@ ifdef NRF5X
14861486
INCLUDE += -I$(NRF5X_SDK_PATH)/components/libraries/trace
14871487
INCLUDE += -I$(NRF5X_SDK_PATH)/components/softdevice/common/softdevice_handler
14881488
INCLUDE += -I$(NRF5X_SDK_PATH)/components/drivers_nrf/twi_master
1489+
INCLUDE += -I$(NRF5X_SDK_PATH)/components/drivers_nrf/hal/nrf_pwm
14891490

14901491
TARGETSOURCES += \
14911492
$(NRF5X_SDK_PATH)/components/libraries/util/app_error.c \
@@ -1507,7 +1508,7 @@ ifdef NRF5X
15071508
$(NRF5X_SDK_PATH)/components/softdevice/common/softdevice_handler/softdevice_handler.c \
15081509
$(NRF5X_SDK_PATH)/components/drivers_nrf/hal/nrf_nvmc.c \
15091510
$(NRF5X_SDK_PATH)/components/drivers_nrf/twi_master/nrf_drv_twi.c \
1510-
$(NRF5X_SDK_PATH)/components/drivers_nrf/hal/nrf_adc.c
1511+
$(NRF5X_SDK_PATH)/components/drivers_nrf/hal/nrf_adc.c
15111512
# $(NRF5X_SDK_PATH)/components/libraries/util/nrf_log.c
15121513

15131514
ifdef USE_BOOTLOADER
@@ -1669,7 +1670,7 @@ else ifdef EFM32
16691670
LDFLAGS += $(OPTIMIZEFLAGS) $(ARCHFLAGS)
16701671
LDFLAGS += -Wl,--start-group -lgcc -lc -lnosys -Wl,--end-group
16711672
else
1672-
LDFLAGS += $(OPTIMIZEFLAGS) $(ARCHFLAGS)
1673+
LDFLAGS += $(OPTIMIZEFLAGS) $(ARCHFLAGS)
16731674
endif
16741675

16751676
ifdef EMBEDDED
@@ -1721,7 +1722,7 @@ ESPTOOL ?= $(ESP8266_SDK_ROOT)/esptool/esptool.py
17211722
INCLUDE += -I$(ESP8266_SDK_ROOT)/include -I$(ROOT)/targets/esp8266
17221723
endif # ESP8266
17231724

1724-
# Adds additional files from unsupported sources(means not supported by Gordon) to actual make
1725+
# Adds additional files from unsupported sources(means not supported by Gordon) to actual make
17251726
ifdef UNSUPPORTEDMAKE
17261727
include $(UNSUPPORTEDMAKE)
17271728
endif
@@ -1760,7 +1761,7 @@ ifdef USE_NET
17601761
# hack to ensure that Pico/etc have all possible firmware configs listed
17611762
$(Q)python scripts/build_board_json.py $(WRAPPERSOURCES) $(DEFINES) -DUSE_WIZNET=1 -DUSE_CC3000=1 -B$(BOARD)
17621763
else
1763-
$(Q)python scripts/build_board_json.py $(WRAPPERSOURCES) $(DEFINES) -B$(BOARD)
1764+
$(Q)python scripts/build_board_json.py $(WRAPPERSOURCES) $(DEFINES) -B$(BOARD)
17641765
endif
17651766

17661767

@@ -1789,7 +1790,7 @@ $(PLATFORM_CONFIG_FILE): boards/$(BOARD).py scripts/build_platform_config.py
17891790
$(Q)python scripts/build_platform_config.py $(BOARD) $(HEADERFILENAME)
17901791

17911792
# skips compiling and linking, if NO_COMPILE is defined
1792-
# Generation of temporary files and setting of wrappersources is already done this moment
1793+
# Generation of temporary files and setting of wrappersources is already done this moment
17931794
ifndef NO_COMPILE
17941795

17951796
compile=$(CC) $(CFLAGS) $< -o $@
@@ -1867,7 +1868,7 @@ ifdef USE_CRYPTO
18671868
$(Q)$(OBJCOPY) --rename-section .rodata=.irom0.text libs/crypto/mbedtls/library/sha1.o
18681869
$(Q)$(OBJCOPY) --rename-section .rodata=.irom0.text libs/crypto/mbedtls/library/sha256.o
18691870
$(Q)$(OBJCOPY) --rename-section .rodata=.irom0.text libs/crypto/mbedtls/library/sha512.o
1870-
endif
1871+
endif
18711872
$(Q)$(LD) $(OPTIMIZEFLAGS) -nostdlib -Wl,--no-check-sections -Wl,-static -r -o $@ $(OBJS)
18721873
$(Q)$(OBJCOPY) --rename-section .text=.irom0.text --rename-section .literal=.irom0.literal $@
18731874

targets/nrf5x/jshardware.c

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
#ifdef NRF52
4242
#include "nrf_saadc.h"
43+
#include "nrf_pwm.h"
4344
#else
4445
#include "nrf_adc.h"
4546
#endif
@@ -426,14 +427,45 @@ int jshPinAnalogFast(Pin pin) {
426427
JshPinFunction jshPinAnalogOutput(Pin pin, JsVarFloat value, JsVarFloat freq, JshAnalogOutputFlags flags) {
427428
/* we set the bit field here so that if the user changes the pin state
428429
* later on, we can get rid of the IRQs */
429-
if (!jshGetPinStateIsManual(pin)) {
430+
/* if (!jshGetPinStateIsManual(pin)) {
430431
BITFIELD_SET(jshPinSoftPWM, pin, 0);
431432
jshPinSetState(pin, JSHPINSTATE_GPIO_OUT);
432433
}
433434
BITFIELD_SET(jshPinSoftPWM, pin, 1);
434435
if (freq<=0) freq=50;
435436
jstPinPWM(freq, value, pin);
436-
return JSH_NOTHING;
437+
return JSH_NOTHING;*/
438+
439+
NRF_PWM_Type *pwm = NRF_PWM0;
440+
jshPinSetState(pin, JSHPINSTATE_GPIO_OUT);
441+
uint32_t out_pins[NRF_PWM_CHANNEL_COUNT] = { pin, NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED };
442+
nrf_pwm_pins_set(pwm, out_pins);
443+
nrf_pwm_enable(pwm);
444+
nrf_pwm_configure(pwm,
445+
NRF_PWM_CLK_1MHz, NRF_PWM_MODE_UP, 32767 /* top value - 15 bits, not 16! */);
446+
nrf_pwm_decoder_set(pwm,
447+
NRF_PWM_LOAD_INDIVIDUAL, // allow all 4 channels to be used
448+
NRF_PWM_STEP_TRIGGERED); // Only step on NEXTSTEP task
449+
450+
nrf_pwm_shorts_set(pwm, 0);
451+
nrf_pwm_int_set(pwm, 0);
452+
nrf_pwm_event_clear(pwm, NRF_PWM_EVENT_LOOPSDONE);
453+
nrf_pwm_event_clear(pwm, NRF_PWM_EVENT_SEQEND0);
454+
nrf_pwm_event_clear(pwm, NRF_PWM_EVENT_SEQEND1);
455+
nrf_pwm_event_clear(pwm, NRF_PWM_EVENT_STOPPED);
456+
nrf_pwm_event_clear(pwm, NRF_PWM_EVENT_STOPPED);
457+
458+
static uint16_t pwmValues[4];
459+
pwmValues[0] = 32767 - (uint16_t)(value*32767);
460+
nrf_pwm_loop_set(pwm, PWM_LOOP_CNT_Disabled);
461+
nrf_pwm_seq_ptr_set( pwm, 0, pwmValues);
462+
nrf_pwm_seq_cnt_set( pwm, 0, 4);
463+
nrf_pwm_seq_refresh_set( pwm, 0, 0);
464+
nrf_pwm_seq_end_delay_set(pwm, 0, 0);
465+
466+
nrf_pwm_task_trigger(pwm, NRF_PWM_TASK_SEQSTART0);
467+
// nrf_pwm_disable(pwm);
468+
437469
} // if freq<=0, the default is used
438470

439471
void jshPinPulse(Pin pin, bool pulsePolarity, JsVarFloat pulseTime) {

0 commit comments

Comments
 (0)