First commit
This commit is contained in:
21
drivers/actuator/actuator.c
Normal file
21
drivers/actuator/actuator.c
Normal file
@@ -0,0 +1,21 @@
|
||||
#include "actuator.h"
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
#include <zephyr/sys/printk.h>
|
||||
|
||||
#define USER_NODE DT_PATH(zephyr_user)
|
||||
static const struct gpio_dt_spec do1 = GPIO_DT_SPEC_GET(USER_NODE, do1_gpios);
|
||||
static const struct gpio_dt_spec do2 = GPIO_DT_SPEC_GET(USER_NODE, do2_gpios);
|
||||
static const struct gpio_dt_spec do_en = GPIO_DT_SPEC_GET(USER_NODE, do_en_gpios);
|
||||
|
||||
void digital_out_init(void) {
|
||||
gpio_pin_configure_dt(&do1, GPIO_OUTPUT_INACTIVE);
|
||||
gpio_pin_configure_dt(&do2, GPIO_OUTPUT_INACTIVE);
|
||||
gpio_pin_configure_dt(&do_en, GPIO_OUTPUT_INACTIVE);
|
||||
}
|
||||
|
||||
void digital_out_set_do1(int state) { gpio_pin_set_dt(&do1, state); }
|
||||
void digital_out_set_do2(int state) { gpio_pin_set_dt(&do2, state); }
|
||||
void digital_out_set_do_en(int state) { gpio_pin_set_dt(&do_en, state); }
|
||||
void digital_out_toggle_do1(void) { gpio_pin_toggle_dt(&do1); }
|
||||
void digital_out_toggle_do2(void) { gpio_pin_toggle_dt(&do2); }
|
||||
void digital_out_toggle_do_en(void) { gpio_pin_toggle_dt(&do_en); }
|
||||
12
drivers/actuator/actuator.h
Normal file
12
drivers/actuator/actuator.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#ifndef ACTUATOR_H
|
||||
#define ACTUATOR_H
|
||||
|
||||
void digital_out_init(void);
|
||||
void digital_out_set_do1(int state);
|
||||
void digital_out_set_do2(int state);
|
||||
void digital_out_set_do_en(int state);
|
||||
void digital_out_toggle_do1(void);
|
||||
void digital_out_toggle_do2(void);
|
||||
void digital_out_toggle_do_en(void);
|
||||
|
||||
#endif // ACTUATOR_H
|
||||
50
drivers/battery_adc/battery_adc.c
Normal file
50
drivers/battery_adc/battery_adc.c
Normal file
@@ -0,0 +1,50 @@
|
||||
#include "battery_adc.h"
|
||||
#include <zephyr/drivers/adc.h>
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
#include <zephyr/sys/printk.h>
|
||||
|
||||
#define USER_NODE DT_PATH(zephyr_user)
|
||||
static const struct gpio_dt_spec btt_meas_en = GPIO_DT_SPEC_GET(USER_NODE, btt_meas_en_gpios);
|
||||
static const struct adc_dt_spec adc_channel = ADC_DT_SPEC_GET(USER_NODE);
|
||||
|
||||
static uint32_t sample_buffer;
|
||||
|
||||
struct adc_sequence sequence = {
|
||||
.buffer = &sample_buffer,
|
||||
.buffer_size = sizeof(sample_buffer),
|
||||
//.calibrate = true
|
||||
};
|
||||
|
||||
int battery_adc_init(void) {
|
||||
if (!adc_is_ready_dt(&adc_channel)) {
|
||||
printk("ADC not ready\n");
|
||||
return -ENODEV;
|
||||
}
|
||||
gpio_pin_configure_dt(&btt_meas_en, GPIO_OUTPUT_INACTIVE);
|
||||
int ret = 0;
|
||||
ret = adc_channel_setup_dt(&adc_channel);
|
||||
if(ret){
|
||||
return ret;
|
||||
}
|
||||
ret = adc_sequence_init_dt(&adc_channel, &sequence);
|
||||
if(ret){
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
int battery_measure_mv(int32_t *mv_out) {
|
||||
int ret = 0;
|
||||
gpio_pin_set_dt(&btt_meas_en, 1); // enable measurement
|
||||
k_sleep(K_MSEC(5));
|
||||
|
||||
|
||||
ret = adc_read(adc_channel.dev, &sequence);
|
||||
if (ret) return ret;
|
||||
|
||||
ret = adc_raw_to_millivolts_dt(&adc_channel, &sample_buffer);
|
||||
*mv_out = (int)((float)sample_buffer*((270.0f+110.0f)/270.0f));
|
||||
|
||||
gpio_pin_set_dt(&btt_meas_en, 0); // disable measurement
|
||||
|
||||
return ret;
|
||||
}
|
||||
9
drivers/battery_adc/battery_adc.h
Normal file
9
drivers/battery_adc/battery_adc.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#ifndef BATTERY_ADC_H
|
||||
#define BATTERY_ADC_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
int battery_adc_init(void);
|
||||
int battery_measure_mv(int32_t *mv_out);
|
||||
|
||||
#endif // BATTERY_ADC_H
|
||||
17
drivers/button/button.c
Normal file
17
drivers/button/button.c
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "button.h"
|
||||
#include <zephyr/sys/printk.h>
|
||||
|
||||
#define BUTTON_NODE DT_NODELABEL(button0)
|
||||
static const struct gpio_dt_spec button = GPIO_DT_SPEC_GET(BUTTON_NODE, gpios);
|
||||
|
||||
void button_init(void) {
|
||||
if (!device_is_ready(button.port)) {
|
||||
printk("Button device not ready\n");
|
||||
return;
|
||||
}
|
||||
gpio_pin_configure_dt(&button, GPIO_INPUT);
|
||||
}
|
||||
|
||||
int button_read(void) {
|
||||
return gpio_pin_get_dt(&button);
|
||||
}
|
||||
9
drivers/button/button.h
Normal file
9
drivers/button/button.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#ifndef BUTTON_H
|
||||
#define BUTTON_H
|
||||
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
|
||||
void button_init(void);
|
||||
int button_read(void);
|
||||
|
||||
#endif // BUTTON_H
|
||||
46
drivers/led/led.c
Normal file
46
drivers/led/led.c
Normal file
@@ -0,0 +1,46 @@
|
||||
#include "led.h"
|
||||
#include <zephyr/sys/printk.h>
|
||||
|
||||
/* Device tree bindings */
|
||||
#define LED0_NODE DT_ALIAS(led0)
|
||||
#define LED1_NODE DT_ALIAS(led1)
|
||||
|
||||
static const struct gpio_dt_spec led0 = GPIO_DT_SPEC_GET(LED0_NODE, gpios);
|
||||
static const struct gpio_dt_spec led1 = GPIO_DT_SPEC_GET(LED1_NODE, gpios);
|
||||
|
||||
static struct k_timer led0_timer;
|
||||
static struct k_timer led1_timer;
|
||||
|
||||
static void led0_timer_handler(struct k_timer *dummy) { gpio_pin_toggle_dt(&led0); }
|
||||
static void led1_timer_handler(struct k_timer *dummy) { gpio_pin_toggle_dt(&led1); }
|
||||
|
||||
void led_init(void) {
|
||||
if (!device_is_ready(led0.port) || !device_is_ready(led1.port)) {
|
||||
printk("LED devices not ready\n");
|
||||
return;
|
||||
}
|
||||
gpio_pin_configure_dt(&led0, GPIO_OUTPUT_INACTIVE);
|
||||
gpio_pin_configure_dt(&led1, GPIO_OUTPUT_INACTIVE);
|
||||
k_timer_init(&led0_timer, led0_timer_handler, NULL);
|
||||
k_timer_init(&led1_timer, led1_timer_handler, NULL);
|
||||
}
|
||||
|
||||
void led_set(int led_id, int state) {
|
||||
if (led_id == 0) gpio_pin_set_dt(&led0, state);
|
||||
else if (led_id == 1) gpio_pin_set_dt(&led1, state);
|
||||
}
|
||||
|
||||
void led_toggle(int led_id) {
|
||||
if (led_id == 0) gpio_pin_toggle_dt(&led0);
|
||||
else if (led_id == 1) gpio_pin_toggle_dt(&led1);
|
||||
}
|
||||
|
||||
void led_start_blink(int led_id, int interval_ms) {
|
||||
if (led_id == 0) k_timer_start(&led0_timer, K_MSEC(interval_ms), K_MSEC(interval_ms));
|
||||
else if (led_id == 1) k_timer_start(&led1_timer, K_MSEC(interval_ms), K_MSEC(interval_ms));
|
||||
}
|
||||
|
||||
void led_stop_blink(int led_id) {
|
||||
if (led_id == 0) k_timer_stop(&led0_timer);
|
||||
else if (led_id == 1) k_timer_stop(&led1_timer);
|
||||
}
|
||||
13
drivers/led/led.h
Normal file
13
drivers/led/led.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#ifndef LED_H
|
||||
#define LED_H
|
||||
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
#include <zephyr/kernel.h>
|
||||
|
||||
void led_init(void);
|
||||
void led_set(int led_id, int state);
|
||||
void led_toggle(int led_id);
|
||||
void led_start_blink(int led_id, int interval_ms);
|
||||
void led_stop_blink(int led_id);
|
||||
|
||||
#endif // LED_H
|
||||
16
drivers/temperature/temperature.c
Normal file
16
drivers/temperature/temperature.c
Normal file
@@ -0,0 +1,16 @@
|
||||
#include "temperature.h"
|
||||
#include <nrfx_temp.h>
|
||||
#include <zephyr/sys/printk.h>
|
||||
|
||||
int temperature_init(void) {
|
||||
nrfx_temp_config_t cfg = NRFX_TEMP_DEFAULT_CONFIG;
|
||||
return nrfx_temp_init(&cfg, NULL);
|
||||
}
|
||||
|
||||
int temperature_get_celsius(int32_t *temp_c) {
|
||||
nrfx_temp_measure();
|
||||
int32_t raw = nrfx_temp_result_get();
|
||||
/* Convert to °C (raw is 0.25 °C per LSB) */
|
||||
*temp_c = raw / 4;
|
||||
return 0;
|
||||
}
|
||||
9
drivers/temperature/temperature.h
Normal file
9
drivers/temperature/temperature.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#ifndef TEMPERATURE_H
|
||||
#define TEMPERATURE_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
int temperature_init(void);
|
||||
int temperature_get_celsius(int32_t *temp_c);
|
||||
|
||||
#endif // TEMPERATURE_H
|
||||
153
drivers/timer_count/timer_count.c
Normal file
153
drivers/timer_count/timer_count.c
Normal file
@@ -0,0 +1,153 @@
|
||||
#include "timer_count.h"
|
||||
|
||||
#define MEAS_PIN 11
|
||||
#define OSCILLATOR_INIT_DELAY_MS 5
|
||||
#define USER_NODE DT_PATH(zephyr_user)
|
||||
static const struct gpio_dt_spec hum_en = GPIO_DT_SPEC_GET(USER_NODE, hum_en_gpios);
|
||||
|
||||
uint32_t count = 0;
|
||||
uint32_t timer_status = 0;
|
||||
|
||||
void configure_clock_pin(uint32_t pin_number){
|
||||
nrf_gpio_cfg(
|
||||
pin_number,
|
||||
NRF_GPIO_PIN_DIR_INPUT,
|
||||
NRF_GPIO_PIN_INPUT_CONNECT,
|
||||
NRF_GPIO_PIN_PULLUP,
|
||||
NRF_GPIO_PIN_S0S1,
|
||||
NRF_GPIO_PIN_NOSENSE);
|
||||
*(volatile uint32_t *)(NRF_GPIOTE_BASE + 0x600 + (4 * pin_number)) = 1;
|
||||
}
|
||||
|
||||
void timer_init() //gate timer
|
||||
{
|
||||
NRF_TIMER1->TASKS_STOP = 1; //trigger stop
|
||||
NRF_TIMER1->MODE = TIMER_MODE_MODE_Timer; //mode timer
|
||||
NRF_TIMER1->PRESCALER = 4; // Fhck / 2^8 -1MHz
|
||||
//total gate time of timer 10000 - 100mS
|
||||
NRF_TIMER1->CC[0] = 50000; //end gate count
|
||||
NRF_TIMER1->CC[1] = 1; //start gate count. don't start at 0 to no start on clear.
|
||||
|
||||
NRF_TIMER1->BITMODE = (TIMER_BITMODE_BITMODE_32Bit << TIMER_BITMODE_BITMODE_Pos);
|
||||
|
||||
NRF_TIMER1->TASKS_CLEAR = 1; //trigger zero timer
|
||||
NRF_TIMER1->INTENSET = (TIMER_INTENSET_COMPARE0_Enabled << TIMER_INTENSET_COMPARE0_Pos); //interrupt on end gate count
|
||||
|
||||
NRF_TIMER1->EVENTS_COMPARE[0] = 0; //rest event flag for gate end
|
||||
NRF_TIMER1->EVENTS_COMPARE[1] = 0; //rest event flag for gate start
|
||||
}
|
||||
|
||||
static void counter_init() //actual counter
|
||||
{
|
||||
NRF_TIMER2->TASKS_STOP = 1; //trigger stop
|
||||
NRF_TIMER2->MODE = TIMER_MODE_MODE_Counter; //counter
|
||||
NRF_TIMER2->BITMODE = (TIMER_BITMODE_BITMODE_24Bit << TIMER_BITMODE_BITMODE_Pos);
|
||||
NRF_TIMER2->TASKS_CLEAR = 1; //trigger zero counter
|
||||
NRF_TIMER2->EVENTS_COMPARE[0] = 0; //reset event for compare
|
||||
}
|
||||
|
||||
//counter input pin setting
|
||||
static void gpiote_init(uint32_t pin) //Pin must be in port 0
|
||||
{
|
||||
NRF_GPIOTE->CONFIG[0] = 0x01 << 0; // MODE: Event
|
||||
NRF_GPIOTE->CONFIG[0] |= pin << 8; // Pin number
|
||||
NRF_GPIOTE->CONFIG[0] |= GPIOTE_CONFIG_POLARITY_LoToHi << 16; // Event rising edge
|
||||
}
|
||||
|
||||
//hardware trigger: stop of counter on event of end timer1 gate
|
||||
static void ppi_timer_stop_counter_init()
|
||||
{
|
||||
NRF_PPI->CHEN |= 1 << 2; //channel
|
||||
*(&(NRF_PPI->CH2_EEP)) = (uint32_t)&NRF_TIMER1->EVENTS_COMPARE[0]; //attach end gate event
|
||||
*(&(NRF_PPI->CH2_TEP)) = (uint32_t)&NRF_TIMER2->TASKS_STOP; //to counter stop trigger
|
||||
NRF_PPI->CHENSET |= 1 << 2; //set channel
|
||||
}
|
||||
|
||||
//hardware trigger: start of counter on event of begin timer1 (gate) event.gate
|
||||
static void ppi_timer_start_counter_init()
|
||||
{
|
||||
NRF_PPI->CHEN |= 1 << 4;// channel
|
||||
*(&(NRF_PPI->CH4_EEP)) = (uint32_t)&NRF_TIMER1->EVENTS_COMPARE[1]; //attach gate start event
|
||||
*(&(NRF_PPI->CH4_TEP)) = (uint32_t)&NRF_TIMER2->TASKS_START; //to counter start trigger
|
||||
NRF_PPI->CHENSET |= 1 << 4;
|
||||
}
|
||||
|
||||
|
||||
static void ppi_gpiote_counter_init()
|
||||
//hardware attachment for the gpiote that was selected above
|
||||
{
|
||||
NRF_PPI->CHEN |= 1 << 1;
|
||||
*(&(NRF_PPI->CH1_EEP)) = (uint32_t)&NRF_GPIOTE->EVENTS_IN[0]; //attach pin change
|
||||
*(&(NRF_PPI->CH1_TEP)) = (uint32_t)&NRF_TIMER2->TASKS_COUNT;//to one count
|
||||
NRF_PPI->CHENSET |= 1 << 1;
|
||||
}
|
||||
|
||||
//gate end interrupt handler. Count is done at hardware and percice interrupt execution isnt critical
|
||||
void TIMER1_IRQHandler(void) {
|
||||
if (NRF_TIMER1->EVENTS_COMPARE[0] != 0){ //end gate event
|
||||
NRF_TIMER1->EVENTS_COMPARE[0] = 0; //reset end gate flag
|
||||
NRF_TIMER1->EVENTS_COMPARE[1] = 0; //reset start gate flag
|
||||
NRF_TIMER2->TASKS_CAPTURE[0] = 1; //trigger get counter value
|
||||
count = NRF_TIMER2->CC[0];
|
||||
|
||||
NRF_TIMER1->TASKS_CLEAR = 1; //reset timer
|
||||
NRF_TIMER2->TASKS_CLEAR = 1; //reset counter
|
||||
|
||||
timer_status = STATE_FINISHED;
|
||||
NRF_TIMER1->TASKS_STOP = 1; //stop
|
||||
gpio_pin_set_dt(&hum_en, 0);
|
||||
//NRF_TIMER1->TASKS_START = 1; //start next count gate
|
||||
}
|
||||
}
|
||||
|
||||
void configure_measurement()
|
||||
{
|
||||
|
||||
int ret = 0;
|
||||
NVIC_ClearPendingIRQ(TIMER1_IRQn);
|
||||
NVIC_EnableIRQ(TIMER1_IRQn); //enable timer1 interrupt
|
||||
IRQ_CONNECT(TIMER1_IRQn, 5, TIMER1_IRQHandler, 0, 0); //link interrupt flag to handler
|
||||
|
||||
if (!device_is_ready(hum_en.port)) {
|
||||
printk("hum_en device not ready\n");
|
||||
return;
|
||||
}
|
||||
ret = gpio_pin_configure_dt(&hum_en, GPIO_OUTPUT_ACTIVE); if (ret < 0)
|
||||
{
|
||||
printk("Failed to configure hum_en\n");
|
||||
return;
|
||||
}
|
||||
|
||||
configure_clock_pin(MEAS_PIN);
|
||||
|
||||
counter_init();
|
||||
timer_init();
|
||||
gpiote_init(MEAS_PIN);
|
||||
ppi_gpiote_counter_init();
|
||||
ppi_timer_stop_counter_init();
|
||||
ppi_timer_start_counter_init();
|
||||
|
||||
}
|
||||
|
||||
void make_measurement()
|
||||
{
|
||||
if( timer_status == STATE_RUNNING) {
|
||||
//manage error here
|
||||
return;
|
||||
}
|
||||
|
||||
gpio_pin_set_dt(&hum_en, 1);
|
||||
k_sleep(K_MSEC(OSCILLATOR_INIT_DELAY_MS));
|
||||
|
||||
timer_status = STATE_RUNNING;
|
||||
NRF_TIMER1->TASKS_START = 1;
|
||||
}
|
||||
|
||||
uint32_t read_measurement(){
|
||||
if(timer_status != STATE_FINISHED) {
|
||||
return -timer_status;
|
||||
}
|
||||
else{
|
||||
return count;
|
||||
}
|
||||
}
|
||||
22
drivers/timer_count/timer_count.h
Normal file
22
drivers/timer_count/timer_count.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#ifndef TIMER_COUNT_H
|
||||
#define TIMER_COUNT_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/sys/printk.h>
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
|
||||
#include <hal/nrf_gpio.h>
|
||||
|
||||
#define STATE_DISABLED 0
|
||||
#define STATE_RUNNING 1
|
||||
#define STATE_FINISHED 2
|
||||
|
||||
void configure_clock_pin(uint32_t);
|
||||
void TIMER1_IRQHandler(void);
|
||||
void configure_measurement();
|
||||
void make_measurement();
|
||||
uint32_t read_measurement();
|
||||
|
||||
# endif
|
||||
Reference in New Issue
Block a user