Zephyr: Clock setup fixed and LEDs implemented
This commit is contained in:
+72
-4
@@ -140,7 +140,76 @@ extern "C" void TIM22_IRQHandler(void)
|
||||
charlieplexing();
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif // STM32
|
||||
|
||||
#elif defined(__ZEPHYR__)
|
||||
|
||||
#include <zephyr.h>
|
||||
#include <drivers/gpio.h>
|
||||
|
||||
#define SLEEP_TIME_MS (1000/60/NUM_LEDS) // 60 Hz
|
||||
|
||||
void leds_update_thread()
|
||||
{
|
||||
int led_count = 0;
|
||||
int flicker_count = 0;
|
||||
bool flicker_state = 1;
|
||||
struct device *led_devs[NUM_LED_PINS];
|
||||
|
||||
for (int pin = 0; pin < NUM_LED_PINS; pin++) {
|
||||
led_devs[pin] = device_get_binding(led_ports[pin]);
|
||||
}
|
||||
|
||||
leds_init();
|
||||
|
||||
while (1) {
|
||||
|
||||
// could be increased to value >NUM_LEDS to reduce on-time
|
||||
if (led_count >= NUM_LEDS) {
|
||||
led_count = 0;
|
||||
}
|
||||
|
||||
if (flicker_count > 30) {
|
||||
flicker_count = 0;
|
||||
flicker_state = !flicker_state;
|
||||
}
|
||||
|
||||
if (led_count < NUM_LEDS && (
|
||||
led_states[led_count] == LED_STATE_ON ||
|
||||
(led_states[led_count] == LED_STATE_FLICKER && flicker_state == 1) ||
|
||||
(led_states[led_count] == LED_STATE_BLINK && blink_state == 1)
|
||||
)) {
|
||||
for (int pin_number = 0; pin_number < NUM_LED_PINS; pin_number++) {
|
||||
switch (led_pin_setup[led_count][pin_number]) {
|
||||
case PIN_HIGH:
|
||||
gpio_pin_configure(led_devs[pin_number], led_pins[pin_number], GPIO_DIR_OUT);
|
||||
gpio_pin_write(led_devs[pin_number], led_pins[pin_number], 1);
|
||||
break;
|
||||
case PIN_LOW:
|
||||
gpio_pin_configure(led_devs[pin_number], led_pins[pin_number], GPIO_DIR_OUT);
|
||||
gpio_pin_write(led_devs[pin_number], led_pins[pin_number], 0);
|
||||
break;
|
||||
case PIN_FLOAT:
|
||||
gpio_pin_configure(led_devs[pin_number], led_pins[pin_number], GPIO_DIR_IN);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
// all pins floating
|
||||
for (int pin_number = 0; pin_number < NUM_LED_PINS; pin_number++) {
|
||||
gpio_pin_configure(led_devs[pin_number], led_pins[pin_number], GPIO_DIR_IN);
|
||||
}
|
||||
}
|
||||
|
||||
led_count++;
|
||||
flicker_count++;
|
||||
|
||||
k_sleep(SLEEP_TIME_MS);
|
||||
}
|
||||
}
|
||||
|
||||
#endif // ZEPHYR or MBED
|
||||
|
||||
void leds_init(bool enabled)
|
||||
{
|
||||
@@ -154,11 +223,11 @@ void leds_init(bool enabled)
|
||||
trigger_timeout[led] = -1;
|
||||
}
|
||||
}
|
||||
#ifdef __MBED__
|
||||
timer_start(NUM_LEDS * 60); // 60 Hz
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif /* UNIT_TEST */
|
||||
|
||||
void leds_set_charging(bool enabled)
|
||||
{
|
||||
charging = enabled;
|
||||
@@ -241,7 +310,6 @@ void leds_update_soc(int soc, bool load_off_low_soc)
|
||||
int blink_chg = LED_STATE_ON;
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef LED_PWR
|
||||
led_states[LED_PWR] = blink_chg;
|
||||
trigger_timeout[LED_PWR] = -1;
|
||||
|
||||
+9
-1
@@ -32,10 +32,18 @@ enum LedState {
|
||||
LED_STATE_FLICKER
|
||||
};
|
||||
|
||||
/** Initialize LEDs and start timer for charlieplexing
|
||||
/**
|
||||
* Initialize LEDs and start timer for charlieplexing. This function only needs to be called if
|
||||
* used in a single-thread application (mbed)
|
||||
*/
|
||||
void leds_init(bool enabled = true);
|
||||
|
||||
#ifdef __ZEPHYR__
|
||||
/** Main thread for LED control in Zephyr, performs pin initializations and charlieplexing at 60 Hz
|
||||
*/
|
||||
void leds_update_thread();
|
||||
#endif
|
||||
|
||||
/** Enables/disables dedicated charging LED if existing or
|
||||
* blinks SOC LED when solar power is coming in.
|
||||
*/
|
||||
|
||||
+16
-3
@@ -18,6 +18,8 @@
|
||||
|
||||
#include <zephyr.h>
|
||||
#include <stdio.h>
|
||||
#include <device.h>
|
||||
#include <drivers/gpio.h>
|
||||
|
||||
#include "thingset.h" // handles access to internal data via communication interfaces
|
||||
#include "pcb.h" // hardware-specific settings
|
||||
@@ -75,19 +77,30 @@ extern ThingSet ts; // defined in data_objects.cpp
|
||||
|
||||
time_t timestamp; // current unix timestamp (independent of time(NULL), as it is user-configurable)
|
||||
|
||||
#define USB_PWR_EN_PORT DT_ALIAS_USB_PWR_EN_GPIOS_CONTROLLER
|
||||
#define USB_PWR_EN_PIN DT_ALIAS_USB_PWR_EN_GPIOS_PIN
|
||||
|
||||
void main(void)
|
||||
{
|
||||
u32_t cnt = 0;
|
||||
struct device *dev_usb_en;
|
||||
|
||||
dev_usb_en = device_get_binding(USB_PWR_EN_PORT);
|
||||
gpio_pin_configure(dev_usb_en, USB_PWR_EN_PIN, GPIO_DIR_OUT);
|
||||
gpio_pin_write(dev_usb_en, USB_PWR_EN_PIN, 1);
|
||||
|
||||
printf("Booting Libre Solar Charge Controller: %s\n", CONFIG_BOARD);
|
||||
|
||||
while (1) {
|
||||
|
||||
k_sleep(1000);
|
||||
gpio_pin_write(dev_usb_en, USB_PWR_EN_PIN, cnt % 2);
|
||||
cnt++;
|
||||
k_sleep(5000);
|
||||
}
|
||||
}
|
||||
|
||||
//K_THREAD_DEFINE(ts_serial_id, 4096, thingset_serial_thread, NULL, NULL, NULL, 5, 0, K_NO_WAIT);
|
||||
|
||||
//K_THREAD_DEFINE(leds_id, 256, leds_update_thread, NULL, NULL, NULL, 4, 0, K_NO_WAIT);
|
||||
K_THREAD_DEFINE(leds_id, 256, leds_update_thread, NULL, NULL, NULL, 4, 0, K_NO_WAIT);
|
||||
|
||||
//K_THREAD_DEFINE(uext_thread, 1024, uext_process_1s, NULL, NULL, NULL, 6, 0, K_NO_WAIT);
|
||||
|
||||
|
||||
@@ -17,13 +17,15 @@
|
||||
#ifndef MPPT_1210_HUS_0V4_H
|
||||
#define MPPT_1210_HUS_0V4_H
|
||||
|
||||
#define DEVICE_TYPE "MPPT-1210-HUS"
|
||||
#define HARDWARE_VERSION "v0.4"
|
||||
|
||||
#ifdef __MBED__
|
||||
#include "mbed.h"
|
||||
#elif defined(__ZEPHYR__)
|
||||
#include <zephyr.h>
|
||||
#endif
|
||||
|
||||
#define DEVICE_TYPE "MPPT-1210-HUS"
|
||||
#define HARDWARE_VERSION "v0.4"
|
||||
|
||||
// specify features of charge controller
|
||||
#define FEATURE_DCDC_CONVERTER 1
|
||||
#define FEATURE_PWM_SWITCH 0
|
||||
@@ -68,13 +70,31 @@ enum pin_state_t { PIN_HIGH, PIN_LOW, PIN_FLOAT };
|
||||
#define LED_RXTX 3 // LED4, used to indicate when sending data
|
||||
#define LED_LOAD 4 // LED5
|
||||
|
||||
#ifdef __MBED__
|
||||
// LED pins and pin state configuration to switch above LEDs on
|
||||
#define NUM_LED_PINS 5
|
||||
|
||||
#ifdef __MBED__
|
||||
static const PinName led_pins[NUM_LED_PINS] = {
|
||||
// GND SOC12 SOC3 RXTX LOAD
|
||||
PB_14, PB_13, PB_2, PB_11, PB_10
|
||||
};
|
||||
#elif defined(__ZEPHYR__)
|
||||
static const char *led_ports[NUM_LED_PINS] = {
|
||||
DT_ALIAS_LED_A_GPIOS_CONTROLLER,
|
||||
DT_ALIAS_LED_B_GPIOS_CONTROLLER,
|
||||
DT_ALIAS_LED_C_GPIOS_CONTROLLER,
|
||||
DT_ALIAS_LED_D_GPIOS_CONTROLLER,
|
||||
DT_ALIAS_LED_E_GPIOS_CONTROLLER
|
||||
};
|
||||
static const int led_pins[NUM_LED_PINS] = {
|
||||
DT_ALIAS_LED_A_GPIOS_PIN, // GND
|
||||
DT_ALIAS_LED_B_GPIOS_PIN, // SOC12
|
||||
DT_ALIAS_LED_C_GPIOS_PIN, // SOC3
|
||||
DT_ALIAS_LED_D_GPIOS_PIN, // RXTX
|
||||
DT_ALIAS_LED_E_GPIOS_PIN // LOAD
|
||||
};
|
||||
#endif // MBED or ZEPHYR
|
||||
|
||||
static const pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = {
|
||||
{ PIN_HIGH, PIN_LOW, PIN_LOW, PIN_LOW, PIN_LOW }, // LED1
|
||||
{ PIN_LOW, PIN_HIGH, PIN_LOW, PIN_LOW, PIN_LOW }, // LED2
|
||||
@@ -82,7 +102,6 @@ static const pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = {
|
||||
{ PIN_LOW, PIN_LOW, PIN_LOW, PIN_HIGH, PIN_LOW }, // LED4
|
||||
{ PIN_LOW, PIN_LOW, PIN_LOW, PIN_LOW, PIN_HIGH } // LED5
|
||||
};
|
||||
#endif // __MBED__
|
||||
|
||||
// pin definition only needed in adc_dma.cpp to detect if they are present on the PCB
|
||||
#define PIN_ADC_TEMP_FETS PA_5
|
||||
|
||||
Reference in New Issue
Block a user