Merge pull request #966 from arturo182/nrf_analogio

nrf: Rewrite the AnalogIn common-hal using nrfx
This commit is contained in:
Scott Shawcroft 2018-06-28 13:54:34 -07:00 committed by GitHub
commit a0425d9134
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 48 additions and 1420 deletions

View File

@ -91,15 +91,8 @@ LIBS += -L $(dir $(LIBGCC_FILE_NAME)) -lgcc
SRC_HAL = $(addprefix hal/,\
hal_uart.c \
hal_uarte.c \
hal_time.c \
hal_timer.c \
hal_twi.c \
hal_adc.c \
hal_adce.c \
hal_gpio.c \
hal_rng.c \
hal_pwm.c \
)
SRC_NRFX = $(addprefix nrfx/,\

View File

@ -4,6 +4,7 @@
* The MIT License (MIT)
*
* Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
* Copyright (c) 2018 Artur Pacholec
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@ -25,24 +26,19 @@
*/
#include "common-hal/analogio/AnalogIn.h"
#include <string.h>
#include "py/gc.h"
#include "py/nlr.h"
#include "py/runtime.h"
#include "py/binary.h"
#include "py/mphal.h"
#include "shared-bindings/analogio/AnalogIn.h"
#include "nrf.h"
void common_hal_analogio_analogin_construct(analogio_analogin_obj_t* self, const mcu_pin_obj_t *pin) {
if (!pin->adc_channel) {
// No ADC function on that pin
#include "nrfx_saadc.h"
#include "nrf_gpio.h"
#define CHANNEL_NO 0
void common_hal_analogio_analogin_construct(analogio_analogin_obj_t *self, const mcu_pin_obj_t *pin) {
if (pin->adc_channel == 0)
mp_raise_ValueError("Pin does not have ADC capabilities");
}
hal_gpio_cfg_pin(pin->port, pin->pin, HAL_GPIO_MODE_INPUT, HAL_GPIO_PULL_DISABLED);
self->pin = pin;
}
@ -51,66 +47,61 @@ bool common_hal_analogio_analogin_deinited(analogio_analogin_obj_t *self) {
}
void common_hal_analogio_analogin_deinit(analogio_analogin_obj_t *self) {
if (common_hal_analogio_analogin_deinited(self)) {
if (common_hal_analogio_analogin_deinited(self))
return;
}
reset_pin(self->pin->pin);
nrf_gpio_cfg_default(NRF_GPIO_PIN_MAP(self->pin->port, self->pin->pin));
self->pin = mp_const_none;
}
void analogin_reset() {
}
uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) {
// Something else might have used the ADC in a different way,
// so we completely re-initialize it.
// Something else might have used the ADC in a different way,
// so we completely re-initialize it.
int16_t value;
nrf_saadc_value_t value;
NRF_SAADC->RESOLUTION = SAADC_RESOLUTION_VAL_14bit;
NRF_SAADC->ENABLE = 1;
const nrf_saadc_channel_config_t config = {
.resistor_p = NRF_SAADC_RESISTOR_DISABLED,
.resistor_n = NRF_SAADC_RESISTOR_DISABLED,
.gain = NRF_SAADC_GAIN1_6,
.reference = NRF_SAADC_REFERENCE_INTERNAL,
.acq_time = NRF_SAADC_ACQTIME_3US,
.mode = NRF_SAADC_MODE_SINGLE_ENDED,
.burst = NRF_SAADC_BURST_DISABLED,
.pin_p = self->pin->adc_channel,
.pin_n = self->pin->adc_channel,
};
for (int i = 0; i < 8; i++) {
NRF_SAADC->CH[i].PSELN = SAADC_CH_PSELP_PSELP_NC;
NRF_SAADC->CH[i].PSELP = SAADC_CH_PSELP_PSELP_NC;
}
nrf_saadc_resolution_set(NRF_SAADC_RESOLUTION_14BIT);
nrf_saadc_oversample_set(NRF_SAADC_OVERSAMPLE_DISABLED);
nrf_saadc_enable();
NRF_SAADC->CH[0].CONFIG = ((SAADC_CH_CONFIG_RESP_Bypass << SAADC_CH_CONFIG_RESP_Pos) & SAADC_CH_CONFIG_RESP_Msk)
| ((SAADC_CH_CONFIG_RESP_Bypass << SAADC_CH_CONFIG_RESN_Pos) & SAADC_CH_CONFIG_RESN_Msk)
| ((SAADC_CH_CONFIG_GAIN_Gain1_6 << SAADC_CH_CONFIG_GAIN_Pos) & SAADC_CH_CONFIG_GAIN_Msk)
| ((SAADC_CH_CONFIG_REFSEL_Internal << SAADC_CH_CONFIG_REFSEL_Pos) & SAADC_CH_CONFIG_REFSEL_Msk)
| ((SAADC_CH_CONFIG_TACQ_3us << SAADC_CH_CONFIG_TACQ_Pos) & SAADC_CH_CONFIG_TACQ_Msk)
| ((SAADC_CH_CONFIG_MODE_SE << SAADC_CH_CONFIG_MODE_Pos) & SAADC_CH_CONFIG_MODE_Msk);
NRF_SAADC->CH[0].PSELN = self->pin->adc_channel;
NRF_SAADC->CH[0].PSELP = self->pin->adc_channel;
for (uint32_t i = 0; i < NRF_SAADC_CHANNEL_COUNT; i++)
nrf_saadc_channel_input_set(i, NRF_SAADC_INPUT_DISABLED, NRF_SAADC_INPUT_DISABLED);
nrf_saadc_channel_init(CHANNEL_NO, &config);
nrf_saadc_buffer_init(&value, 1);
NRF_SAADC->RESULT.PTR = (uint32_t)&value;
NRF_SAADC->RESULT.MAXCNT = 1;
nrf_saadc_task_trigger(NRF_SAADC_TASK_START);
while (nrf_saadc_event_check(NRF_SAADC_EVENT_STARTED) == 0);
nrf_saadc_event_clear(NRF_SAADC_EVENT_STARTED);
NRF_SAADC->TASKS_START = 0x01UL;
nrf_saadc_task_trigger(NRF_SAADC_TASK_SAMPLE);
while (nrf_saadc_event_check(NRF_SAADC_EVENT_END) == 0);
nrf_saadc_event_clear(NRF_SAADC_EVENT_END);
while (!NRF_SAADC->EVENTS_STARTED);
NRF_SAADC->EVENTS_STARTED = 0x00UL;
nrf_saadc_task_trigger(NRF_SAADC_TASK_STOP);
while (nrf_saadc_event_check(NRF_SAADC_EVENT_STOPPED) == 0);
nrf_saadc_event_clear(NRF_SAADC_EVENT_STOPPED);
NRF_SAADC->TASKS_SAMPLE = 0x01UL;
nrf_saadc_disable();
while (!NRF_SAADC->EVENTS_END);
NRF_SAADC->EVENTS_END = 0x00UL;
if (value < 0)
value = 0;
NRF_SAADC->TASKS_STOP = 0x01UL;
while (!NRF_SAADC->EVENTS_STOPPED);
NRF_SAADC->EVENTS_STOPPED = 0x00UL;
if (value < 0) {
value = 0;
}
NRF_SAADC->ENABLE = 0;
// Map value to from 14 to 16 bits
return (value << 2);
// Map value to from 14 to 16 bits
return (value << 2);
}
float common_hal_analogio_analogin_get_reference_voltage(analogio_analogin_obj_t *self) {

View File

@ -36,6 +36,4 @@ typedef struct {
const mcu_pin_obj_t * pin;
} analogio_analogin_obj_t;
void analogin_reset(void);
#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_ANALOGIO_ANALOGIN_H

View File

@ -1,129 +0,0 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2017 Glenn Ruben Bakke
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdio.h>
#include "mphalport.h"
#include "hal_adc.h"
#ifdef HAL_ADC_MODULE_ENABLED
#define ADC_REF_VOLTAGE_IN_MILLIVOLTS (1200) // Reference voltage (in milli volts) used by ADC while doing conversion.
#define ADC_PRE_SCALING_COMPENSATION (3) // The ADC is configured to use VDD with 1/3 prescaling as input. And hence the result of conversion is to be multiplied by 3 to get the actual value of the battery voltage.
#define DIODE_FWD_VOLT_DROP_MILLIVOLTS (270) // Typical forward voltage drop of the diode (Part no: SD103ATW-7-F) that is connected in series with the voltage supply. This is the voltage drop when the forward current is 1mA. Source: Data sheet of 'SURFACE MOUNT SCHOTTKY BARRIER DIODE ARRAY' available at www.diodes.com.
#define ADC_RESULT_IN_MILLI_VOLTS(ADC_VALUE)\
((((ADC_VALUE) * ADC_REF_VOLTAGE_IN_MILLIVOLTS) / 255) * ADC_PRE_SCALING_COMPENSATION)
static const uint32_t hal_adc_input_lookup[] = {
ADC_CONFIG_PSEL_AnalogInput0 << ADC_CONFIG_PSEL_Pos,
ADC_CONFIG_PSEL_AnalogInput1 << ADC_CONFIG_PSEL_Pos,
ADC_CONFIG_PSEL_AnalogInput2 << ADC_CONFIG_PSEL_Pos,
ADC_CONFIG_PSEL_AnalogInput3 << ADC_CONFIG_PSEL_Pos,
ADC_CONFIG_PSEL_AnalogInput4 << ADC_CONFIG_PSEL_Pos,
ADC_CONFIG_PSEL_AnalogInput5 << ADC_CONFIG_PSEL_Pos,
ADC_CONFIG_PSEL_AnalogInput6 << ADC_CONFIG_PSEL_Pos,
ADC_CONFIG_PSEL_AnalogInput7 << ADC_CONFIG_PSEL_Pos
};
static uint8_t battery_level_in_percent(const uint16_t mvolts)
{
uint8_t battery_level;
if (mvolts >= 3000) {
battery_level = 100;
} else if (mvolts > 2900) {
battery_level = 100 - ((3000 - mvolts) * 58) / 100;
} else if (mvolts > 2740) {
battery_level = 42 - ((2900 - mvolts) * 24) / 160;
} else if (mvolts > 2440) {
battery_level = 18 - ((2740 - mvolts) * 12) / 300;
} else if (mvolts > 2100) {
battery_level = 6 - ((2440 - mvolts) * 6) / 340;
} else {
battery_level = 0;
}
return battery_level;
}
uint16_t hal_adc_channel_value(hal_adc_config_t const * p_adc_conf) {
ADC_BASE->INTENSET = ADC_INTENSET_END_Msk;
ADC_BASE->CONFIG = (ADC_CONFIG_RES_8bit << ADC_CONFIG_RES_Pos)
| (ADC_CONFIG_INPSEL_AnalogInputTwoThirdsPrescaling << ADC_CONFIG_INPSEL_Pos)
| (ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos)
| (hal_adc_input_lookup[p_adc_conf->channel])
| (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos);
ADC_BASE->EVENTS_END = 0;
ADC_BASE->ENABLE = ADC_ENABLE_ENABLE_Enabled;
ADC_BASE->EVENTS_END = 0;
ADC_BASE->TASKS_START = 1;
while (!ADC_BASE->EVENTS_END) {
;
}
uint8_t adc_result;
ADC_BASE->EVENTS_END = 0;
adc_result = ADC_BASE->RESULT;
ADC_BASE->TASKS_STOP = 1;
return adc_result;
}
uint16_t hal_adc_battery_level(void) {
ADC_BASE->INTENSET = ADC_INTENSET_END_Msk;
ADC_BASE->CONFIG = (ADC_CONFIG_RES_8bit << ADC_CONFIG_RES_Pos)
| (ADC_CONFIG_INPSEL_SupplyOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos)
| (ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos)
| (ADC_CONFIG_PSEL_Disabled << ADC_CONFIG_PSEL_Pos)
| (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos);
ADC_BASE->EVENTS_END = 0;
ADC_BASE->ENABLE = ADC_ENABLE_ENABLE_Enabled;
ADC_BASE->EVENTS_END = 0;
ADC_BASE->TASKS_START = 1;
while (!ADC_BASE->EVENTS_END) {
;
}
uint8_t adc_result;
uint16_t batt_lvl_in_milli_volts;
ADC_BASE->EVENTS_END = 0;
adc_result = ADC_BASE->RESULT;
ADC_BASE->TASKS_STOP = 1;
batt_lvl_in_milli_volts = ADC_RESULT_IN_MILLI_VOLTS(adc_result) + DIODE_FWD_VOLT_DROP_MILLIVOLTS;
return battery_level_in_percent(batt_lvl_in_milli_volts);
}
#endif // HAL_ADC_MODULE_ENABLED

View File

@ -1,75 +0,0 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2017 Glenn Ruben Bakke
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef HAL_ADC_H__
#define HAL_ADC_H__
#include <stdint.h>
#include "nrf.h"
#if NRF51
#define ADC_IRQ_NUM ADC_IRQn
#define ADC_BASE ((NRF_ADC_Type *)NRF_ADC_BASE)
#define HAL_ADC_Type NRF_ADC_Type
#else
#define ADC_IRQ_NUM SAADC_IRQn
#define ADC_BASE ((NRF_SAADC_Type *)NRF_SAADC_BASE)
#define HAL_ADC_Type NRF_SAADC_Type
#endif
typedef enum {
HAL_ADC_CHANNEL_2 = 2,
HAL_ADC_CHANNEL_3,
HAL_ADC_CHANNEL_4,
HAL_ADC_CHANNEL_5,
HAL_ADC_CHANNEL_6,
HAL_ADC_CHANNEL_7,
} hal_adc_channel_t;
/**
* @brief ADC Configuration Structure definition
*/
typedef struct {
hal_adc_channel_t channel;
} hal_adc_config_t;
/**
* @brief ADC handle Structure definition
*/
typedef struct __ADC_HandleTypeDef {
hal_adc_config_t config; /* ADC config parameters */
} ADC_HandleTypeDef;
uint16_t hal_adc_channel_value(hal_adc_config_t const * p_adc_conf);
uint16_t hal_adc_battery_level(void);
#endif // HAL_ADC_H__

View File

@ -1,118 +0,0 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2017 Glenn Ruben Bakke
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "mphalport.h"
#include "hal_adc.h"
#ifdef HAL_ADCE_MODULE_ENABLED
static const uint32_t hal_adc_input_lookup_pos[] = {
SAADC_CH_PSELP_PSELP_AnalogInput0 << SAADC_CH_PSELP_PSELP_Pos,
SAADC_CH_PSELP_PSELP_AnalogInput1 << SAADC_CH_PSELP_PSELP_Pos,
SAADC_CH_PSELP_PSELP_AnalogInput2 << SAADC_CH_PSELP_PSELP_Pos,
SAADC_CH_PSELP_PSELP_AnalogInput3 << SAADC_CH_PSELP_PSELP_Pos,
SAADC_CH_PSELP_PSELP_AnalogInput4 << SAADC_CH_PSELP_PSELP_Pos,
SAADC_CH_PSELP_PSELP_AnalogInput5 << SAADC_CH_PSELP_PSELP_Pos,
SAADC_CH_PSELP_PSELP_AnalogInput6 << SAADC_CH_PSELP_PSELP_Pos,
SAADC_CH_PSELP_PSELP_AnalogInput7 << SAADC_CH_PSELP_PSELP_Pos
};
#define HAL_ADCE_PSELP_NOT_CONNECTED (SAADC_CH_PSELP_PSELP_NC << SAADC_CH_PSELP_PSELP_Pos)
#define HAL_ADCE_PSELP_VDD (SAADC_CH_PSELP_PSELP_VDD << SAADC_CH_PSELP_PSELP_Pos)
/*static const uint32_t hal_adc_input_lookup_neg[] = {
SAADC_CH_PSELN_PSELN_AnalogInput0 << SAADC_CH_PSELN_PSELN_Pos,
SAADC_CH_PSELN_PSELN_AnalogInput1 << SAADC_CH_PSELN_PSELN_Pos,
SAADC_CH_PSELN_PSELN_AnalogInput2 << SAADC_CH_PSELN_PSELN_Pos,
SAADC_CH_PSELN_PSELN_AnalogInput3 << SAADC_CH_PSELN_PSELN_Pos,
SAADC_CH_PSELN_PSELN_AnalogInput4 << SAADC_CH_PSELN_PSELN_Pos,
SAADC_CH_PSELN_PSELN_AnalogInput5 << SAADC_CH_PSELN_PSELN_Pos,
SAADC_CH_PSELN_PSELN_AnalogInput6 << SAADC_CH_PSELN_PSELN_Pos,
SAADC_CH_PSELN_PSELN_AnalogInput7 << SAADC_CH_PSELN_PSELN_Pos
};*/
#define HAL_ADCE_PSELN_NOT_CONNECTED (SAADC_CH_PSELN_PSELN_NC << SAADC_CH_PSELN_PSELN_Pos)
#define HAL_ADCE_PSELN_VDD (SAADC_CH_PSELN_PSELN_VDD << SAADC_CH_PSELN_PSELN_Pos)
uint16_t hal_adc_channel_value(hal_adc_config_t const * p_adc_conf) {
int16_t result = 0;
// configure to use VDD/4 and gain 1/4
ADC_BASE->CH[0].CONFIG = (SAADC_CH_CONFIG_GAIN_Gain1_4 << SAADC_CH_CONFIG_GAIN_Pos)
| (SAADC_CH_CONFIG_MODE_SE << SAADC_CH_CONFIG_MODE_Pos)
| (SAADC_CH_CONFIG_REFSEL_VDD1_4 << SAADC_CH_CONFIG_REFSEL_Pos)
| (SAADC_CH_CONFIG_RESN_Bypass << SAADC_CH_CONFIG_RESN_Pos)
| (SAADC_CH_CONFIG_RESP_Bypass << SAADC_CH_CONFIG_RESP_Pos)
| (SAADC_CH_CONFIG_TACQ_3us << SAADC_CH_CONFIG_TACQ_Pos);
// positive input
ADC_BASE->CH[0].PSELP = hal_adc_input_lookup_pos[p_adc_conf->channel]; // HAL_ADCE_PSELP_VDD;
ADC_BASE->CH[0].PSELN = HAL_ADCE_PSELN_NOT_CONNECTED;
ADC_BASE->RESOLUTION = SAADC_RESOLUTION_VAL_8bit << SAADC_RESOLUTION_VAL_Pos;
ADC_BASE->RESULT.MAXCNT = 1;
ADC_BASE->RESULT.PTR = (uint32_t)&result;
ADC_BASE->SAMPLERATE = SAADC_SAMPLERATE_MODE_Task << SAADC_SAMPLERATE_MODE_Pos;
ADC_BASE->ENABLE = SAADC_ENABLE_ENABLE_Enabled << SAADC_ENABLE_ENABLE_Pos;
// calibrate ADC
ADC_BASE->TASKS_CALIBRATEOFFSET = 1;
while (ADC_BASE->EVENTS_CALIBRATEDONE == 0) {
;
}
ADC_BASE->EVENTS_CALIBRATEDONE = 0;
while (ADC_BASE->STATUS == (SAADC_STATUS_STATUS_Busy << SAADC_STATUS_STATUS_Pos)) {
;
}
// start the ADC
ADC_BASE->TASKS_START = 1;
while (ADC_BASE->EVENTS_STARTED == 0) {
;
}
ADC_BASE->EVENTS_STARTED = 0;
// sample ADC
ADC_BASE->TASKS_SAMPLE = 1;
while (ADC_BASE->EVENTS_END == 0) {
;
}
ADC_BASE->EVENTS_END = 0;
ADC_BASE->TASKS_STOP = 1;
while (ADC_BASE->EVENTS_STOPPED == 0) {
;
}
ADC_BASE->EVENTS_STOPPED = 0;
return result;
}
uint16_t hal_adc_battery_level(void) {
return 0;
}
#endif // HAL_ADCE_MODULE_ENABLED

View File

@ -1,116 +0,0 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2017 Glenn Ruben Bakke
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "hal_gpio.h"
#include "mphalport.h"
#include "hal_irq.h"
#define GPIOTE_IRQ_NUM GPIOTE_IRQn
#define GPIOTE_BASE ((NRF_GPIOTE_Type *)NRF_GPIOTE_BASE)
#define HAL_GPIOTE_Type NRF_GPIOTE_Type
static hal_gpio_event_callback_t m_callback;
void hal_gpio_register_callback(hal_gpio_event_callback_t cb) {
m_callback = cb;
#if 0
hal_gpio_event_config_t config;
config.channel = HAL_GPIO_EVENT_CHANNEL_0;
config.event = HAL_GPIO_POLARITY_EVENT_HIGH_TO_LOW;
config.init_level = 1;
config.pin = 13;
config.port = 0;
// start LFCLK if not already started
if (NRF_CLOCK->LFCLKSTAT == 0) {
NRF_CLOCK->TASKS_LFCLKSTART = 1;
while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0);
NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
}
hal_irq_enable(GPIOTE_IRQ_NUM);
hal_irq_priority(GPIOTE_IRQ_NUM, 3);
hal_gpio_event_config(&config);
#endif
}
void hal_gpio_event_config(hal_gpio_event_config_t const * p_config) {
#if 0
hal_gpio_cfg_pin(p_config->port, p_config->pin, HAL_GPIO_MODE_INPUT, HAL_GPIO_PULL_UP);
uint8_t channel = (uint8_t)p_config->channel;
GPIOTE_BASE->CONFIG[channel] = \
GPIOTE_CONFIG_MODE_Event << GPIOTE_CONFIG_MODE_Pos \
| p_config->pin << GPIOTE_CONFIG_PSEL_Pos \
| p_config->event \
| p_config->init_level << GPIOTE_CONFIG_OUTINIT_Pos;
GPIOTE_BASE->INTENSET = 1 << channel;
GPIOTE_BASE->EVENTS_IN[channel] = 0;
#endif
}
#if 0
void GPIOTE_IRQHandler(void) {
if (GPIOTE_BASE->EVENTS_IN[0]) {
GPIOTE_BASE->EVENTS_IN[0] = 0;
m_callback(HAL_GPIO_EVENT_CHANNEL_0);
}
if (GPIOTE_BASE->EVENTS_IN[1]) {
GPIOTE_BASE->EVENTS_IN[1] = 0;
m_callback(HAL_GPIO_EVENT_CHANNEL_1);
}
if (GPIOTE_BASE->EVENTS_IN[2]) {
GPIOTE_BASE->EVENTS_IN[2] = 0;
m_callback(HAL_GPIO_EVENT_CHANNEL_2);
}
if (GPIOTE_BASE->EVENTS_IN[3]) {
GPIOTE_BASE->EVENTS_IN[3] = 0;
m_callback(HAL_GPIO_EVENT_CHANNEL_3);
}
if (GPIOTE_BASE->EVENTS_IN[4]) {
GPIOTE_BASE->EVENTS_IN[4] = 0;
m_callback(HAL_GPIO_EVENT_CHANNEL_4);
}
if (GPIOTE_BASE->EVENTS_IN[5]) {
GPIOTE_BASE->EVENTS_IN[5] = 0;
m_callback(HAL_GPIO_EVENT_CHANNEL_5);
}
if (GPIOTE_BASE->EVENTS_IN[6]) {
GPIOTE_BASE->EVENTS_IN[6] = 0;
m_callback(HAL_GPIO_EVENT_CHANNEL_6);
}
if (GPIOTE_BASE->EVENTS_IN[7]) {
GPIOTE_BASE->EVENTS_IN[7] = 0;
m_callback(HAL_GPIO_EVENT_CHANNEL_7);
}
}
#endif // if 0

View File

@ -1,116 +0,0 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2017 Glenn Ruben Bakke
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef HAL_IRQ_H__
#define HAL_IRQ_H__
#include <stdint.h>
#include "nrf.h"
#if BLUETOOTH_SD
#include "py/nlr.h"
#include "ble_drv.h"
#define BLUETOOTH_STACK_ENABLED() (ble_drv_stack_enabled())
#define NRF52
#include "nrf_nvic.h"
#endif // BLUETOOTH_SD
static inline void hal_irq_clear(uint32_t irq_num) {
#if BLUETOOTH_SD
if (BLUETOOTH_STACK_ENABLED() == 1) {
if (sd_nvic_ClearPendingIRQ(irq_num) != 0) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
"IRQ (%d) clear error", irq_num));
}
} else
#endif // BLUETOOTH_SD
{
NVIC_ClearPendingIRQ(irq_num);
}
}
static inline void hal_irq_enable(uint32_t irq_num) {
hal_irq_clear(irq_num);
#if BLUETOOTH_SD
if (BLUETOOTH_STACK_ENABLED() == 1) {
if (sd_nvic_EnableIRQ(irq_num) != 0) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
"IRQ (%d) enable error", irq_num));
}
} else
#endif // BLUETOOTH_SD
{
NVIC_EnableIRQ(irq_num);
}
}
static inline void hal_irq_disable(uint32_t irq_num) {
#if BLUETOOTH_SD
if (BLUETOOTH_STACK_ENABLED() == 1) {
if (sd_nvic_DisableIRQ(irq_num) != 0) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
"IRQ (%d) disable error", irq_num));
}
} else
#endif // BLUETOOTH_SD
{
NVIC_DisableIRQ(irq_num);
}
}
static inline void hal_irq_priority(uint32_t irq_num, uint8_t priority) {
#if BLUETOOTH_SD
if (BLUETOOTH_STACK_ENABLED() == 1) {
if (sd_nvic_SetPriority(irq_num, priority) != 0) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
"IRQ (%d) priority error", irq_num, priority));
}
} else
#endif // BLUETOOTH_SD
{
NVIC_SetPriority(irq_num, priority);
}
}
static inline void hal_irq_pending(uint32_t irq_num) {
#if BLUETOOTH_SD
if (BLUETOOTH_STACK_ENABLED() == 1) {
if (sd_nvic_SetPendingIRQ(irq_num) != 0) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
"IRQ (%d) pending error", irq_num));
}
} else
#endif // BLUETOOTH_SD
{
NVIC_SetPendingIRQ(irq_num);
}
}
#endif // HAL_IRQ_H__

View File

@ -1,118 +0,0 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Glenn Ruben Bakke
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdint.h>
#include <stdio.h>
#include "mphalport.h"
#include "hal_pwm.h"
#ifdef HAL_PWM_MODULE_ENABLED
#define PWM_COUNTER_TOP 16000 // 16MHz divided by 16000-> 1ms
volatile uint16_t g_pwm_seq[4];
volatile uint16_t g_pwm_period;
static const uint32_t hal_pwm_frequency_lookup[] = {
PWM_PRESCALER_PRESCALER_DIV_1, // 16MHz
PWM_PRESCALER_PRESCALER_DIV_2, // 8MHz
PWM_PRESCALER_PRESCALER_DIV_4, // 4MHz
PWM_PRESCALER_PRESCALER_DIV_8, // 2MHz
PWM_PRESCALER_PRESCALER_DIV_16, // 1MHz
PWM_PRESCALER_PRESCALER_DIV_32, // 500kHz
PWM_PRESCALER_PRESCALER_DIV_64, // 250kHz
PWM_PRESCALER_PRESCALER_DIV_128 // 125kHz
};
void hal_pwm_init(NRF_PWM_Type * p_instance, hal_pwm_init_t const * p_pwm_init) {
g_pwm_period = p_pwm_init->period;
uint16_t pulse_width = ((g_pwm_period * p_pwm_init->duty)/100);
if (p_pwm_init->pulse_width > 0) {
pulse_width = p_pwm_init->pulse_width;
}
if (p_pwm_init->mode == HAL_PWM_MODE_HIGH_LOW) {
g_pwm_seq[0] = g_pwm_period - pulse_width;
g_pwm_seq[1] = g_pwm_period - pulse_width;
} else {
g_pwm_seq[0] = pulse_width;
g_pwm_seq[1] = pulse_width;
}
g_pwm_seq[2] = 0;
g_pwm_seq[3] = 0;
p_instance->PSEL.OUT[0] = (p_pwm_init->pwm_pin << PWM_PSEL_OUT_PIN_Pos)
| (PWM_PSEL_OUT_CONNECT_Connected << PWM_PSEL_OUT_CONNECT_Pos);
p_instance->ENABLE = (PWM_ENABLE_ENABLE_Enabled << PWM_ENABLE_ENABLE_Pos);
p_instance->MODE = (PWM_MODE_UPDOWN_Up << PWM_MODE_UPDOWN_Pos);
p_instance->PRESCALER = (hal_pwm_frequency_lookup[p_pwm_init->freq] << PWM_PRESCALER_PRESCALER_Pos);
p_instance->COUNTERTOP = (p_pwm_init->period << PWM_COUNTERTOP_COUNTERTOP_Pos);
p_instance->LOOP = (PWM_LOOP_CNT_Disabled << PWM_LOOP_CNT_Pos);
p_instance->DECODER = (PWM_DECODER_LOAD_Individual << PWM_DECODER_LOAD_Pos)
| (PWM_DECODER_MODE_RefreshCount << PWM_DECODER_MODE_Pos);
p_instance->SEQ[0].PTR = ((uint32_t)(g_pwm_seq) << PWM_SEQ_PTR_PTR_Pos);
p_instance->SEQ[0].CNT = ((sizeof(g_pwm_seq) / sizeof(uint16_t)) << PWM_SEQ_CNT_CNT_Pos);
p_instance->SEQ[0].REFRESH = 0;
p_instance->SEQ[0].ENDDELAY = 0;
}
void hal_pwm_start(NRF_PWM_Type * p_instance) {
p_instance->TASKS_SEQSTART[0] = 1;
}
void hal_pwm_stop(NRF_PWM_Type * p_instance) {
p_instance->TASKS_SEQSTART[0] = 0;
p_instance->ENABLE = (PWM_ENABLE_ENABLE_Disabled << PWM_ENABLE_ENABLE_Pos);
}
void hal_pwm_freq_set(NRF_PWM_Type * p_instance, uint16_t freq) {
#if 0
p_instance->PRESCALER = (hal_pwm_frequency_lookup[freq] << PWM_PRESCALER_PRESCALER_Pos);
#endif
}
void hal_pwm_period_set(NRF_PWM_Type * p_instance, uint16_t period) {
#if 0
g_pwm_period = period;
p_instance->COUNTERTOP = (g_pwm_period << PWM_COUNTERTOP_COUNTERTOP_Pos);
#endif
}
void hal_pwm_duty_set(NRF_PWM_Type * p_instance, uint8_t duty) {
#if 0
uint16_t duty_cycle = ((g_pwm_period * duty)/100);
g_pwm_seq[0] = duty_cycle;
g_pwm_seq[1] = duty_cycle;
#endif
}
#endif // HAL_PWM_MODULE_ENABLED

View File

@ -1,100 +0,0 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Glenn Ruben Bakke
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef HAL_PWM_H__
#define HAL_PWM_H__
#include <stdint.h>
#include "nrf.h"
#define PWM0 ((NRF_PWM_Type *)NRF_PWM0_BASE)
#define PWM0_IRQ_NUM PWM1_IRQn
#define PWM1 ((NRF_PWM_Type *)NRF_PWM1_BASE)
#define PWM1_IRQ_NUM PWM1_IRQn
#define PWM2 ((NRF_PWM_Type *)NRF_PWM2_BASE)
#define PWM2_IRQ_NUM PWM2_IRQn
#ifdef NRF52840_XXAA
#define PWM3 ((NRF_PWM_Type *)NRF_PWM3_BASE)
#define PWM3_IRQ_NUM PWM3_IRQn
#endif
/**
* @brief PWM frequency type definition
*/
typedef enum {
HAL_PWM_FREQ_16Mhz = 0,
HAL_PWM_FREQ_8Mhz,
HAL_PWM_FREQ_4Mhz,
HAL_PWM_FREQ_2Mhz,
HAL_PWM_FREQ_1Mhz,
HAL_PWM_FREQ_500khz,
HAL_PWM_FREQ_250khz,
HAL_PWM_FREQ_125khz
} hal_pwm_freq_t;
/**
* @brief PWM mode type definition
*/
typedef enum {
HAL_PWM_MODE_LOW_HIGH = 0,
HAL_PWM_MODE_HIGH_LOW
} hal_pwm_mode_t;
typedef struct {
uint8_t pwm_pin;
hal_pwm_freq_t freq;
uint8_t duty;
uint16_t pulse_width;
uint16_t period;
hal_pwm_mode_t mode;
} hal_pwm_init_t;
/**
* @brief PWM handle Structure definition
*/
typedef struct __PWM_HandleTypeDef
{
NRF_PWM_Type *instance; /* PWM registers base address */
hal_pwm_init_t init; /* PWM initialization parameters */
} PWM_HandleTypeDef;
void hal_pwm_init(NRF_PWM_Type * p_instance, hal_pwm_init_t const * p_pwm_init);
void hal_pwm_freq_set(NRF_PWM_Type * p_instance, uint16_t freq);
void hal_pwm_period_set(NRF_PWM_Type * p_instance, uint16_t period);
void hal_pwm_duty_set(NRF_PWM_Type * p_instance, uint8_t duty);
void hal_pwm_start(NRF_PWM_Type * p_instance);
void hal_pwm_stop(NRF_PWM_Type * p_instance);
#endif // HAL_PWM_H__

View File

@ -1,99 +0,0 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Glenn Ruben Bakke
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "mphalport.h"
#include "hal_timer.h"
#include "hal_irq.h"
#ifdef HAL_TIMER_MODULE_ENABLED
static hal_timer_app_callback m_callback;
void hal_timer_callback_set(hal_timer_app_callback callback) {
m_callback = callback;
}
void hal_timer_init(hal_timer_conf_t const * p_timer_conf) {
NRF_TIMER_Type * p_timer = TIMER_BASE(p_timer_conf->id);
p_timer->CC[0] = 1000 * p_timer_conf->period;
p_timer->MODE = TIMER_MODE_MODE_Timer;
p_timer->BITMODE = TIMER_BITMODE_BITMODE_24Bit << TIMER_BITMODE_BITMODE_Pos;
p_timer->PRESCALER = 4; // 1 us
p_timer->INTENSET = TIMER_INTENSET_COMPARE0_Msk;
p_timer->SHORTS = (TIMER_SHORTS_COMPARE0_CLEAR_Enabled << TIMER_SHORTS_COMPARE0_CLEAR_Pos);
p_timer->TASKS_CLEAR = 1;
hal_irq_priority(TIMER_IRQ_NUM(p_timer_conf->id), p_timer_conf->irq_priority);
}
void hal_timer_start(uint8_t id) {
NRF_TIMER_Type * p_timer = TIMER_BASE(id);
p_timer->TASKS_CLEAR = 1;
hal_irq_enable(TIMER_IRQ_NUM(id));
p_timer->TASKS_START = 1;
}
void hal_timer_stop(uint8_t id) {
NRF_TIMER_Type * p_timer = TIMER_BASE(id);
hal_irq_disable(TIMER_IRQ_NUM(id));
p_timer->TASKS_STOP = 1;
}
static void common_irq_handler(uint8_t id) {
NRF_TIMER_Type * p_timer = TIMER_BASE(id);
if (p_timer->EVENTS_COMPARE[0]) {
p_timer->EVENTS_COMPARE[0] = 0;
m_callback(id);
}
}
void TIMER0_IRQHandler(void) {
common_irq_handler(0);
}
#if (MICROPY_PY_MACHINE_SOFT_PWM != 1)
void TIMER1_IRQHandler(void) {
common_irq_handler(1);
}
#endif
void TIMER2_IRQHandler(void) {
common_irq_handler(2);
}
void TIMER3_IRQHandler(void) {
common_irq_handler(3);
}
void TIMER4_IRQHandler(void) {
common_irq_handler(4);
}
#endif // HAL_TIMER_MODULE_ENABLED

View File

@ -1,65 +0,0 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Glenn Ruben Bakke
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef HAL_TIMER_H__
#define HAL_TIMER_H__
#include "nrf.h"
#define TIMER_BASE_POINTERS (const uint32_t[]){NRF_TIMER0_BASE, \
NRF_TIMER1_BASE, \
NRF_TIMER1_BASE, \
NRF_TIMER1_BASE, \
NRF_TIMER2_BASE}
#define TIMER_IRQ_VALUES (const uint32_t[]){TIMER0_IRQn, \
TIMER1_IRQn, \
TIMER2_IRQn, \
TIMER3_IRQn, \
TIMER4_IRQn}
#define TIMER_BASE(x) ((NRF_TIMER_Type *)TIMER_BASE_POINTERS[x])
#define TIMER_IRQ_NUM(x) (TIMER_IRQ_VALUES[x])
typedef void (*hal_timer_app_callback)(uint8_t id);
/**
* @brief Timer Configuration Structure definition
*/
typedef struct {
uint8_t id;
uint32_t period;
uint8_t irq_priority;
} hal_timer_conf_t;
void hal_timer_callback_set(hal_timer_app_callback callback);
void hal_timer_init(hal_timer_conf_t const * p_timer_config);
void hal_timer_start(uint8_t id);
void hal_timer_stop(uint8_t id);
#endif // HAL_TIMER_H__

View File

@ -1,133 +0,0 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2017 Glenn Ruben Bakke
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "mphalport.h"
#include "hal_twi.h"
#ifdef HAL_TWI_MODULE_ENABLED
static const uint32_t hal_twi_frequency_lookup[] = {
TWI_FREQUENCY_FREQUENCY_K100, // 100 kbps
TWI_FREQUENCY_FREQUENCY_K250, // 250 kbps
TWI_FREQUENCY_FREQUENCY_K400, // 400 kbps
};
void hal_twi_master_init(NRF_TWI_Type * p_instance, hal_twi_init_t const * p_twi_init) {
#if NRF52840_XXAA
p_instance->PSEL.SCL = p_twi_init->scl_pin->pin;
p_instance->PSEL.SDA = p_twi_init->sda_pin->pin;
p_instance->PSEL.SCL |= (p_twi_init->scl_pin->port << TWI_PSEL_SCL_PORT_Pos);
p_instance->PSEL.SDA |= (p_twi_init->sda_pin->port << TWI_PSEL_SDA_PORT_Pos);
#else
p_instance->PSELSCL = p_twi_init->scl_pin->pin;
p_instance->PSELSDA = p_twi_init->sda_pin->pin;
#endif
p_instance->FREQUENCY = hal_twi_frequency_lookup[p_twi_init->freq];
p_instance->ENABLE = (TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos);
}
#include <stdio.h>
void hal_twi_master_tx(NRF_TWI_Type * p_instance,
uint8_t addr,
uint16_t transfer_size,
const uint8_t * tx_data,
bool stop) {
uint16_t number_of_txd_bytes = 0;
p_instance->ADDRESS = addr;
p_instance->EVENTS_TXDSENT = 0;
p_instance->TXD = tx_data[number_of_txd_bytes];
p_instance->TASKS_STARTTX = 1;
while (number_of_txd_bytes < transfer_size) {
// wait for the transaction complete
while (p_instance->EVENTS_TXDSENT == 0) {
;
}
number_of_txd_bytes++;
// TODO: This could go one byte out of bound.
p_instance->TXD = tx_data[number_of_txd_bytes];
p_instance->EVENTS_TXDSENT = 0;
}
if (stop) {
p_instance->EVENTS_STOPPED = 0;
p_instance->TASKS_STOP = 1;
while (p_instance->EVENTS_STOPPED == 0) {
;
}
}
}
void hal_twi_master_rx(NRF_TWI_Type * p_instance,
uint8_t addr,
uint16_t transfer_size,
uint8_t * rx_data,
bool stop) {
uint16_t number_of_rxd_bytes = 0;
p_instance->ADDRESS = addr;
p_instance->EVENTS_RXDREADY = 0;
p_instance->TASKS_STARTRX = 1;
while (number_of_rxd_bytes < transfer_size) {
// wait for the transaction complete
while (p_instance->EVENTS_RXDREADY == 0) {
;
}
rx_data[number_of_rxd_bytes] = p_instance->RXD;
p_instance->EVENTS_RXDREADY = 0;
number_of_rxd_bytes++;
}
if (stop) {
p_instance->EVENTS_STOPPED = 0;
p_instance->TASKS_STOP = 1;
while (p_instance->EVENTS_STOPPED == 0) {
;
}
}
}
void hal_twi_slave_init(NRF_TWI_Type * p_instance, hal_twi_init_t const * p_twi_init) {
}
#endif // HAL_TWI_MODULE_ENABLED

View File

@ -1,105 +0,0 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2017 Glenn Ruben Bakke
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef HAL_TWI_H__
#define HAL_TWI_H__
#include <stdbool.h>
#include "nrf.h"
#define TWI_BASE_POINTERS (const uint32_t[]){NRF_TWI0_BASE, NRF_TWI1_BASE}
#define TWI_BASE(x) ((NRF_TWI_Type *)TWI_BASE_POINTERS[x])
#define TWI_IRQ_VALUES (const uint32_t[]){SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQn, \
SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1_IRQn}
/**
* @brief TWIM Configuration Structure definition
*/
typedef struct {
} hal_twim_init_t;
/**
* @brief TWIS Configuration Structure definition
*/
typedef struct {
} hal_twis_init_t;
/**
* @brief TWI clock frequency type definition
*/
typedef enum {
HAL_TWI_FREQ_100_Kbps = 0,
HAL_TWI_FREQ_250_Kbps,
HAL_TWI_FREQ_400_Kbps
} hal_twi_clk_freq_t;
/**
* @brief TWI role type definition
*/
typedef enum {
HAL_TWI_MASTER,
HAL_TWI_SLAVE
} hal_twi_role_t;
/**
* @brief TWI Configuration Structure definition
*/
typedef struct {
uint8_t id; /* TWI instance id */
const pin_obj_t * scl_pin; /* TWI SCL pin */
const pin_obj_t * sda_pin; /* TWI SDA pin */
hal_twi_role_t role; /* TWI master/slave */
hal_twi_clk_freq_t freq; /* TWI frequency */
} hal_twi_init_t;
/**
* @brief TWI handle Structure definition
*/
typedef struct __TWI_HandleTypeDef
{
NRF_TWI_Type *instance; /* TWI register base address */
hal_twi_init_t init; /* TWI initialization parameters */
} TWI_HandleTypeDef;
void hal_twi_master_init(NRF_TWI_Type * p_instance, hal_twi_init_t const * p_twi_init);
void hal_twi_master_tx(NRF_TWI_Type * p_instance,
uint8_t addr,
uint16_t transfer_size,
const uint8_t * tx_data,
bool stop);
void hal_twi_master_rx(NRF_TWI_Type * p_instance,
uint8_t addr,
uint16_t transfer_size,
uint8_t * rx_data,
bool stop);
void hal_twi_slave_init(NRF_TWI_Type * p_instance, hal_twi_init_t const * p_twi_init);
#endif // HAL_TWI_H__

View File

@ -1,180 +0,0 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Glenn Ruben Bakke
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdint.h>
#include <stdio.h>
#include "mphalport.h"
#include "hal_uart.h"
#include "hal_irq.h"
#ifdef HAL_UARTE_MODULE_ENABLED
#include "nrf.h"
#ifndef NRF52
#error "Device not supported."
#endif
#define UART_BASE(x) ((NRF_UART_Type *)UART_BASE_POINTERS[x])
#define UART_IRQ_NUM(x) (UART_IRQ_VALUES[x])
#define TX_BUF_SIZE 1
#define RX_BUF_SIZE 1
static const uint32_t hal_uart_baudrate_lookup[] = {
UARTE_BAUDRATE_BAUDRATE_Baud1200, ///< 1200 baud.
UARTE_BAUDRATE_BAUDRATE_Baud2400, ///< 2400 baud.
UARTE_BAUDRATE_BAUDRATE_Baud4800, ///< 4800 baud.
UARTE_BAUDRATE_BAUDRATE_Baud9600, ///< 9600 baud.
UARTE_BAUDRATE_BAUDRATE_Baud14400, ///< 14400 baud.
UARTE_BAUDRATE_BAUDRATE_Baud19200, ///< 19200 baud.
UARTE_BAUDRATE_BAUDRATE_Baud28800, ///< 28800 baud.
UARTE_BAUDRATE_BAUDRATE_Baud38400, ///< 38400 baud.
UARTE_BAUDRATE_BAUDRATE_Baud57600, ///< 57600 baud.
UARTE_BAUDRATE_BAUDRATE_Baud76800, ///< 76800 baud.
UARTE_BAUDRATE_BAUDRATE_Baud115200, ///< 115200 baud.
UARTE_BAUDRATE_BAUDRATE_Baud230400, ///< 230400 baud.
UARTE_BAUDRATE_BAUDRATE_Baud250000, ///< 250000 baud.
UARTE_BAUDRATE_BAUDRATE_Baud460800, ///< 460800 baud.
UARTE_BAUDRATE_BAUDRATE_Baud921600, ///< 921600 baud.
UARTE_BAUDRATE_BAUDRATE_Baud1M, ///< 1000000 baud.
};
void nrf_sendchar(NRF_UART_Type * p_instance, int ch) {
hal_uart_char_write(p_instance, ch);
}
void hal_uart_init(NRF_UART_Type * p_instance, hal_uart_init_t const * p_uart_init) {
NRF_UARTE_Type * uarte_instance = (NRF_UARTE_Type *)p_instance;
hal_gpio_cfg_pin(p_uart_init->tx_pin->port, p_uart_init->tx_pin->pin, HAL_GPIO_MODE_OUTPUT, HAL_GPIO_PULL_DISABLED);
hal_gpio_pin_set(p_uart_init->tx_pin->port, p_uart_init->tx_pin->pin);
hal_gpio_cfg_pin(p_uart_init->tx_pin->port, p_uart_init->rx_pin->pin, HAL_GPIO_MODE_INPUT, HAL_GPIO_PULL_DISABLED);
uarte_instance->BAUDRATE = (hal_uart_baudrate_lookup[p_uart_init->baud_rate]);
uint32_t hwfc = (p_uart_init->flow_control)
? (UARTE_CONFIG_HWFC_Enabled << UARTE_CONFIG_HWFC_Pos)
: (UARTE_CONFIG_HWFC_Disabled << UARTE_CONFIG_HWFC_Pos);
uint32_t parity = (p_uart_init->use_parity)
? (UARTE_CONFIG_PARITY_Included << UARTE_CONFIG_PARITY_Pos)
: (UARTE_CONFIG_PARITY_Excluded << UARTE_CONFIG_PARITY_Pos);
uarte_instance->CONFIG = (uint32_t)hwfc | (uint32_t)parity;
uarte_instance->PSEL.RXD = p_uart_init->rx_pin->pin;
uarte_instance->PSEL.TXD = p_uart_init->tx_pin->pin;
#if NRF52840_XXAA
uarte_instance->PSEL.RXD |= (p_uart_init->rx_pin->port << UARTE_PSEL_RXD_PORT_Pos);
uarte_instance->PSEL.TXD |= (p_uart_init->tx_pin->port << UARTE_PSEL_TXD_PORT_Pos);
#endif
if (hwfc) {
hal_gpio_cfg_pin(p_uart_init->cts_pin->port, p_uart_init->cts_pin->pin, HAL_GPIO_MODE_INPUT, HAL_GPIO_PULL_DISABLED);
hal_gpio_cfg_pin(p_uart_init->rts_pin->port, p_uart_init->rts_pin->pin, HAL_GPIO_MODE_OUTPUT, HAL_GPIO_PULL_DISABLED);
hal_gpio_pin_set(p_uart_init->rts_pin->port, p_uart_init->rts_pin->pin);
uarte_instance->PSEL.RTS = p_uart_init->rts_pin->pin;
uarte_instance->PSEL.CTS = p_uart_init->cts_pin->pin;
#if NRF52840_XXAA
uarte_instance->PSEL.RTS |= (p_uart_init->rx_pin->port << UARTE_PSEL_RTS_PORT_Pos);
uarte_instance->PSEL.CTS |= (p_uart_init->rx_pin->port << UARTE_PSEL_CTS_PORT_Pos);
#endif
}
hal_irq_priority(p_uart_init->irq_num, p_uart_init->irq_priority);
hal_irq_enable(p_uart_init->irq_num);
uarte_instance->INTENSET = (UARTE_INTENSET_ENDRX_Set << UARTE_INTENSET_ENDRX_Pos);
uarte_instance->INTENSET = (UARTE_INTENSET_ENDTX_Set << UARTE_INTENSET_ENDTX_Pos);
uarte_instance->ENABLE = (UARTE_ENABLE_ENABLE_Enabled << UARTE_ENABLE_ENABLE_Pos);
uarte_instance->EVENTS_ENDTX = 0;
uarte_instance->EVENTS_ENDRX = 0;
}
hal_uart_error_t hal_uart_char_write(NRF_UART_Type * p_instance, uint8_t ch) {
NRF_UARTE_Type * uarte_instance = (NRF_UARTE_Type *)p_instance;
uarte_instance->ERRORSRC = 0;
static volatile uint8_t m_tx_buf[TX_BUF_SIZE];
(void)m_tx_buf;
uarte_instance->INTENCLR = (UARTE_INTENSET_ENDTX_Set << UARTE_INTENSET_ENDTX_Pos);
m_tx_buf[0] = ch;
uarte_instance->TXD.PTR = (uint32_t)((uint8_t *)m_tx_buf);
uarte_instance->TXD.MAXCNT = (uint32_t)sizeof(m_tx_buf);
uarte_instance->TASKS_STARTTX = 1;
while((0 == uarte_instance->EVENTS_ENDTX));
uarte_instance->EVENTS_ENDTX = 0;
uarte_instance->TASKS_STOPTX = 1;
uarte_instance->INTENSET = (UARTE_INTENSET_ENDTX_Set << UARTE_INTENSET_ENDTX_Pos);
return uarte_instance->ERRORSRC;
}
hal_uart_error_t hal_uart_char_read(NRF_UART_Type * p_instance, uint8_t * ch) {
NRF_UARTE_Type * uarte_instance = (NRF_UARTE_Type *)p_instance;
uarte_instance->ERRORSRC = 0;
static volatile uint8_t m_rx_buf[RX_BUF_SIZE];
uarte_instance->INTENCLR = (UARTE_INTENSET_ENDRX_Set << UARTE_INTENSET_ENDRX_Pos);
uarte_instance->RXD.PTR = (uint32_t)((uint8_t *)m_rx_buf);
uarte_instance->RXD.MAXCNT = (uint32_t)sizeof(m_rx_buf);
uarte_instance->TASKS_STARTRX = 1;
while ((0 == uarte_instance->EVENTS_ENDRX));
uarte_instance->EVENTS_ENDRX = 0;
uarte_instance->TASKS_STOPRX = 1;
uarte_instance->INTENSET = (UARTE_INTENSET_ENDRX_Set << UARTE_INTENSET_ENDRX_Pos);
*ch = (uint8_t)m_rx_buf[0];
return uarte_instance->ERRORSRC;
}
#endif // HAL_UARTE_MODULE_ENABLED