35#include <zephyr/device.h>
36#include <zephyr/drivers/adc.h>
37#include <zephyr/drivers/dac.h>
38#include <zephyr/drivers/gpio.h>
39#include <zephyr/drivers/hwinfo.h>
40#include <zephyr/drivers/i2c.h>
41#include <zephyr/drivers/sensor.h>
42#include <zephyr/drivers/spi.h>
43#include <zephyr/kernel.h>
44#include <zephyr/logging/log.h>
45#include <zephyr/sys/reboot.h>
55 error_msg{
"Error in watchdog initialization",
"errwdt",
EWDT})
65 error_msg{
"Sensor: device not ready.",
"devnotready",
87 error_msg{
"Sensor: device failed to read VBAT.",
"vbaterror",
105 const struct device *m_dev;
114 gpio_pin_configure_dt(m_gpio, GPIO_OUTPUT_INACTIVE | m_gpio->dt_flags);
116 gpio_pin_configure_dt(m_gpio, GPIO_INPUT | m_gpio->dt_flags);
123 gpio_pin_toggle_dt(m_gpio);
129 int ret = gpio_pin_get_dt(m_gpio);
136 int ret = gpio_pin_get_raw(m_gpio->port, m_gpio->pin);
143 gpio_pin_set_dt(m_gpio, s);
149 gpio_pin_set_raw(m_gpio->port, m_gpio->pin, s);
156 gpio_dt_flags_t mask = 0xFFFF;
157 mask ^= GPIO_OUTPUT_ACTIVE;
159 gpio_pin_configure_dt(m_gpio, GPIO_INPUT | (m_gpio->dt_flags & mask));
161 gpio_dt_flags_t mask = 0xFFFF;
163 gpio_pin_configure_dt(m_gpio, GPIO_OUTPUT | GPIO_OUTPUT_INACTIVE |
164 (m_gpio->dt_flags & mask));
169 const struct gpio_dt_spec *m_gpio;
185 spi_bsp(
const struct device *dev,
const struct spi_config &spi_cfg);
188 read(uint8_t *rx,
const uint8_t *tx,
size_t tx_len,
size_t rx_len)
override;
191 write(
const uint8_t *tx,
size_t len)
override;
194 const struct device *m_dev;
195 const struct spi_config m_spi_cfg;
196 struct k_mutex m_mtx;
207 const struct gpio_dt_spec *
cs)
226 delay_us(
size_t us)
override
232 busy_wait_us(
size_t us)
override
240 return k_uptime_get();
257 i2c_bsp(
const struct device *i2c_dev);
260 read(uint16_t addr, uint8_t *rx,
size_t rxlen,
const uint8_t *tx,
264 read(uint16_t addr, uint8_t start_addr, uint8_t *rx,
size_t len);
267 write(uint16_t addr,
const uint8_t *tx,
size_t len);
270 write(uint16_t addr, uint8_t start_addr,
const uint8_t *tx,
size_t len);
272 const struct device *
276 const struct device *m_i2c;
292 adc_bsp(
const struct adc_dt_spec &adc_channel);
307 const struct adc_dt_spec &m_adc_channel;
308 uint16_t m_adc_sample;
309 struct adc_sequence m_sequence;
315 dac_bsp(
const struct device *dac_dev, uint8_t channel, uint8_t resolution)
316 :
dac(resolution, 3.3f), m_dac(dac_dev), m_channel(channel)
318 const struct dac_channel_cfg dac_ch_cfg = {
319 .channel_id = channel, .resolution = resolution, .buffered =
true};
320 dac_channel_setup(m_dac, &dac_ch_cfg);
338 dac_write_value(m_dac, m_channel, x);
342 const struct device *m_dac;
343 const uint8_t m_channel;
346template <
typename T, const
size_t LEN>
352 k_msgq_init(&m_mq, m_buffer,
sizeof(T), LEN);
358 return k_msgq_num_used_get(&m_mq);
364 return k_msgq_num_free_get(&m_mq);
368 put(
const T &msg,
size_t timeout_ms)
370 return k_msgq_put(&m_mq, &msg, K_MSEC(timeout_ms));
376 return k_msgq_put(&m_mq, &msg, K_NO_WAIT);
382 return k_msgq_peek(&m_mq, msg);
388 return k_msgq_purge(&m_mq);
392 get(T *msg,
size_t timeout_ms)
394 return k_msgq_get(&m_mq, msg, K_MSEC(timeout_ms));
400 return k_msgq_get(&m_mq, msg, K_NO_WAIT);
405 char __aligned(4) m_buffer[LEN * sizeof(T)];
void stop()
Stops the ADC.
uint32_t read()
Get the ADC value.
float voltage()
Get the ADC voltage.
void start()
Starts the ADC.
adc_bsp(const struct adc_dt_spec &adc_channel)
adc_initialization_exception(string_type file_name, numeric_type line)
void start()
A virtual method to start the DAC.
dac_bsp(const struct device *dac_dev, uint8_t channel, uint8_t resolution)
void stop()
A virtual method to stop the DAC.
device_not_ready_exception(string_type file_name, numeric_type line)
void set_direction(lib::bsp::gpio::direction dir)
Set the direction of the GPIO.
void toggle()
Toggles the GPIO pin if it is configured as output. Has no effect if it is conigured as input.
void set(bool s)
Sets the logical output of the pin. For example, if the pin has been configured as active low,...
gpio_bsp(const struct gpio_dt_spec *io, bool output=true)
void set_raw(bool s)
Sets the physical output of the pin.
bool get_raw()
Gets the physical level of the GPIO pin. For example, if the input level is 0V, this method will retu...
bool get()
Gets the logical level of the GPIO pin. For example, if the pin has been configured as active low,...
i2c_bsp_exception(string_type file_name, numeric_type line)
const struct device * dev()
Get the I2C device.
void read(uint16_t addr, uint8_t *rx, size_t rxlen, const uint8_t *tx, size_t txlen)
Performs an I2C read operation.
void write(uint16_t addr, const uint8_t *tx, size_t len)
Performs an I2C write operation.
i2c_bsp(const struct device *i2c_dev)
Chrono device abstraction.
virtual int64_t time_ms()=0
Retrieves the current time in milliseconds.
dac(uint16_t resolution, float vref)
Construct a new DAC object.
virtual void set_voltage(float volts)
Sets the output voltage of the DAC.
gpio(direction dir=direction::INPUT)
Construct a new GPIO object.
@ INPUT
GPIO pin is configured as input.
Message queue device abstraction with custom maximum number of messages.
Sensor device abstraction.
A class representing error messages in the SatNOGS-COMMS system.
severity
Severity levels of exceptions.
@ MINOR
Failure having minimal impact.
@ MAJOR
Failure causing minor mission degradation.
@ CRITICAL
Failure causing mission degradation or significant damage.
exception(severity sev, const char *file, int lineno, const error_msg &err_msg)
Constructor for the exception class.
int get(T *msg, size_t timeout_ms)
A virtual method that retrieves a message from the queue.
size_t size()
A virtual method that returns the current number of messages in the queue.
int peek(T *msg)
A virtual method that peeks at the next message in the queue without removing it.
int put_isr(const T &msg)
A virtual method that enqueues a message into the queue from an interrupt context.
int get_isr(T *msg)
A virtual method that retrieves a message from the queue from an interrupt context.
int put(const T &msg, size_t timeout_ms)
A virtual method that enqueues a message into the queue.
float vref()
Get the VREF from SoCs designated internal ADC channel.
float vbat()
Get the VBAT from SoCs designated internal ADC channel.
sensor_bsp(const struct device *dev)
spi_bsp_exception(string_type file_name, numeric_type line)
void read(uint8_t *rx, const uint8_t *tx, size_t tx_len, size_t rx_len) override
Performs an SPI read operation.
spi_bsp(const struct device *dev, const struct spi_config &spi_cfg)
void write(const uint8_t *tx, size_t len) override
Performs an SPI write operation.
lib::bsp::gpio & cs() override
Returns a GPIO control handle for the CS of the SPI. If this is performed automatically by the hardwa...
spi_manual_cs_bsp(const struct device *dev, const struct spi_config &spi_cfg, const struct gpio_dt_spec *cs)
v_bat_exception(string_type file_name, numeric_type line)
watchdog_exception(string_type file_name, numeric_type line)
#define ESPI
Error in SPI.
#define EI2C
Error in I2C.
#define EWDT
Watchdog error.
#define EVBAT
Error in VBAT readings.
#define EADCINIT
Error in ADC readings.
#define EDEVNOTREADY
Device not ready.
#define EDEVNOTCONF
Device not configured.