Skip to content

Commit

Permalink
nRF/microbit soft PWM
Browse files Browse the repository at this point in the history
  • Loading branch information
gfwilliams committed Jan 13, 2016
1 parent f234b05 commit a08421a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/jsutils.h
Expand Up @@ -292,6 +292,7 @@ typedef int64_t JsSysTime;
#define BITFIELD_DECL(BITFIELD, N) uint32_t BITFIELD[(N+31)/32]
#define BITFIELD_GET(BITFIELD, N) ((BITFIELD[(N)>>5] >> ((N)&31))&1)
#define BITFIELD_SET(BITFIELD, N, VALUE) (BITFIELD[(N)>>5] = (BITFIELD[(N)>>5]& (uint32_t)~(1 << ((N)&31))) | (uint32_t)((VALUE)?(1 << ((N)&31)):0) )
#define BITFIELD_CLEAR(BITFIELD) memset(BITFIELD, 0, sizeof(BITFIELD)) ///< Clear all elements


static inline bool isWhitespace(char ch) {
Expand Down
26 changes: 23 additions & 3 deletions targets/nrf5x/jshardware.c
Expand Up @@ -49,6 +49,10 @@
*/

static int init = 0; // Temporary hack to get jsiOneSecAfterStartup() going.
// Whether a pin is being used for soft PWM or not
BITFIELD_DECL(jshPinSoftPWM, JSH_PIN_COUNT);

void TIMER1_IRQHandler(void) {
nrf_timer_task_trigger(NRF_TIMER1, NRF_TIMER_TASK_CLEAR);
nrf_timer_event_clear(NRF_TIMER1, NRF_TIMER_EVENT_COMPARE0);
Expand Down Expand Up @@ -79,11 +83,11 @@ unsigned int getNRFBaud(int baud) {
}


static int init = 0; // Temporary hack to get jsiOneSecAfterStartup() going.

void jshInit() {
jshInitDevices();
nrf_utils_lfclk_config_and_start();

BITFIELD_CLEAR(jshPinSoftPWM);

JshUSARTInfo inf;
jshUSARTInitInfo(&inf);
Expand All @@ -97,7 +101,7 @@ void jshInit() {
nrf_timer_bit_width_set(NRF_TIMER1, NRF_TIMER_BIT_WIDTH_32);
nrf_timer_frequency_set(NRF_TIMER1, NRF_TIMER_FREQ_1MHz); // hmm = only a few options here
// Irq setup
NVIC_SetPriority(TIMER1_IRQn, 15); // low - don't mess with BLE :)
NVIC_SetPriority(TIMER1_IRQn, 3); // low - don't mess with BLE :)
NVIC_ClearPendingIRQ(TIMER1_IRQn);
NVIC_EnableIRQ(TIMER1_IRQn);
nrf_timer_int_enable(NRF_TIMER1, NRF_TIMER_INT_COMPARE0_MASK );
Expand Down Expand Up @@ -186,6 +190,13 @@ bool jshPinGetValue(Pin pin) {

// Set the pin state
void jshPinSetState(Pin pin, JshPinState state) {
/* Make sure we kill software PWM if we set the pin state
* after we've started it */
if (BITFIELD_GET(jshPinSoftPWM, pin)) {
BITFIELD_SET(jshPinSoftPWM, pin, 0);
jstPinPWM(0,0,pin);
}

uint32_t ipin = (uint32_t)pinInfo[pin].pin;
switch (state) {
case JSHPINSTATE_UNDEFINED :
Expand Down Expand Up @@ -276,6 +287,15 @@ int jshPinAnalogFast(Pin pin) {
}

JshPinFunction jshPinAnalogOutput(Pin pin, JsVarFloat value, JsVarFloat freq, JshAnalogOutputFlags flags) {
/* we set the bit field here so that if the user changes the pin state
* later on, we can get rid of the IRQs */
if (!jshGetPinStateIsManual(pin)) {
BITFIELD_SET(jshPinSoftPWM, pin, 0);
jshPinSetState(pin, JSHPINSTATE_GPIO_OUT);
}
BITFIELD_SET(jshPinSoftPWM, pin, 1);
if (freq<=0) freq=50;
jstPinPWM(freq, value, pin);
return JSH_NOTHING;
} // if freq<=0, the default is used

Expand Down
3 changes: 2 additions & 1 deletion targets/stm32/jshardware.c
Expand Up @@ -69,7 +69,7 @@ static JsSysTime jshGetTimeForSecond();
Pin watchedPins[16];

// Whether a pin is being used for soft PWM or not
BITFIELD_DECL(jshPinSoftPWM, JSH_PIN_COUNT); // TODO: This should be set to all 0
BITFIELD_DECL(jshPinSoftPWM, JSH_PIN_COUNT);

// simple 4 byte buffers for SPI
#define JSH_SPIBUF_MASK 3 // 4 bytes
Expand Down Expand Up @@ -984,6 +984,7 @@ void jshInit() {
// reset some vars
for (i=0;i<16;i++)
watchedPins[i] = PIN_UNDEFINED;
BITFIELD_CLEAR(jshPinSoftPWM);

// enable clocks
#if defined(STM32F3)
Expand Down

0 comments on commit a08421a

Please sign in to comment.