SparkFun Iridium 9604  v1.0.0
Library for the Iridium 9604 Hybrid LTE-M Cellular + Iridium Satellite (SBD) + GNSS IoT module
Loading...
Searching...
No Matches
sfe_platform.h
Go to the documentation of this file.
1
41#pragma once
42
43#if defined(ARDUINO)
44
45// ---------------------------------------------------------------------------------------------
46// Arduino
47// ---------------------------------------------------------------------------------------------
48
49#define SFE_IRIDIUM_ARDUINO 1
50
51#include <Arduino.h>
52
54{
55 typedef String sfe_string_t;
56
57 static inline unsigned long sfe_millis(void) { return millis(); }
58 static inline unsigned long sfe_micros(void) { return micros(); }
59 static inline void sfe_delay(unsigned long ms) { delay(ms); }
60 static inline void sfe_yield(void) { yield(); }
61 static inline void sfe_pin_output(int pin) { pinMode(pin, OUTPUT); }
62 static inline void sfe_pin_input(int pin) { pinMode(pin, INPUT); }
63 static inline void sfe_pin_write(int pin, bool high) { digitalWrite(pin, high ? HIGH : LOW); }
64 static inline void sfe_string_from_double(sfe_string_t &str, double value, int decimalPlaces)
65 {
66 str = String(value, decimalPlaces); // Pass int: the second parameter type differs between cores (unsigned int on ESP32, unsigned char on AVR)
67 }
68} // namespace SparkFun_Iridium_9604
69
70#elif defined(ESP_PLATFORM)
71
72// ---------------------------------------------------------------------------------------------
73// Native ESP-IDF
74// ---------------------------------------------------------------------------------------------
75
76#define SFE_IRIDIUM_ESP_IDF 1
77
78#include <stdint.h>
79#include <stddef.h>
80#include <stdio.h>
81#include <stdlib.h>
82#include <string.h>
83#include <math.h>
84#include <string>
85
86#include "freertos/FreeRTOS.h"
87#include "freertos/task.h"
88#include "esp_timer.h"
89#include "esp_rom_sys.h"
90#include "driver/gpio.h"
91
92#ifndef DEC
93#define DEC 10
94#endif
95#ifndef HEX
96#define HEX 16
97#endif
98
100{
101 typedef std::string sfe_string_t;
102
103 static inline unsigned long sfe_millis(void) { return (unsigned long)(esp_timer_get_time() / 1000ULL); }
104 static inline unsigned long sfe_micros(void) { return (unsigned long)esp_timer_get_time(); }
105
106 // sfe_delay: the library uses short delays inside its polling loops to "allow an RTOS to get an elbow in".
107 // With the ESP-IDF default FreeRTOS tick of 100Hz, pdMS_TO_TICKS(1) is zero - and vTaskDelay(0) only
108 // yields to tasks of equal priority. So: delays shorter than one tick are done as a short busy-wait
109 // followed by a yield; longer delays use vTaskDelay. Setting CONFIG_FREERTOS_HZ=1000 (recommended - see
110 // the examples' sdkconfig.defaults) makes every delay a true sleep.
111 static inline void sfe_delay(unsigned long ms)
112 {
113 if (ms == 0)
114 {
115 taskYIELD();
116 return;
117 }
118 TickType_t ticks = pdMS_TO_TICKS(ms);
119 if (ticks == 0)
120 {
121 esp_rom_delay_us((uint32_t)(ms * 1000UL));
122 taskYIELD();
123 }
124 else
125 {
126 vTaskDelay(ticks);
127 }
128 }
129
130 // sfe_yield: Arduino yield() equivalent. Called from inside the library's busy-wait loops.
131 // vTaskDelay(1) lets the IDLE task run (feeding the task watchdog) - taskYIELD() alone would not.
132 // The loops which call this are waiting for UART data: at 115200 baud the UART RX FIFO / driver
133 // ring buffer holds the data while we sleep, so nothing is lost.
134 static inline void sfe_yield(void)
135 {
136 vTaskDelay(1);
137 }
138
139 // gpio_config() also selects the GPIO function for the pad (like Arduino pinMode does).
140 // The output level should be set with sfe_pin_write() before calling sfe_pin_output(), so the pin
141 // does not glitch - the library always does this.
142 static inline void sfe_pin_output(int pin)
143 {
144 gpio_config_t conf = {};
145 conf.pin_bit_mask = 1ULL << pin;
146 conf.mode = GPIO_MODE_OUTPUT;
147 conf.pull_up_en = GPIO_PULLUP_DISABLE;
148 conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
149 conf.intr_type = GPIO_INTR_DISABLE;
150 gpio_config(&conf);
151 }
152 // A high-impedance input - the library relies on the Iridium 9604's internal pull-ups on PWR_ON and RESET_N
153 static inline void sfe_pin_input(int pin)
154 {
155 gpio_config_t conf = {};
156 conf.pin_bit_mask = 1ULL << pin;
157 conf.mode = GPIO_MODE_INPUT;
158 conf.pull_up_en = GPIO_PULLUP_DISABLE;
159 conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
160 conf.intr_type = GPIO_INTR_DISABLE;
161 gpio_config(&conf);
162 }
163 static inline void sfe_pin_write(int pin, bool high) { gpio_set_level((gpio_num_t)pin, high ? 1 : 0); }
164
165 static inline void sfe_string_from_double(sfe_string_t &str, double value, int decimalPlaces)
166 {
167 char buf[40];
168 snprintf(buf, sizeof(buf), "%.*f", decimalPlaces, value);
169 str = buf;
170 }
171} // namespace SparkFun_Iridium_9604
172
173#else
174
175#error "SparkFun Iridium 9604: unsupported platform. Arduino (ARDUINO) or ESP-IDF (ESP_PLATFORM) is required."
176
177#endif
Definition: nmeaMessage.h:30