Code import

This commit is contained in:
2017-12-15 23:52:14 +01:00
commit 56a935cdd0
103 changed files with 14510 additions and 0 deletions
+204
View File
@@ -0,0 +1,204 @@
//
// Created by MightyPork on 2017/11/25.
//
#include "utils/avrlibc.h"
#include "comm/messages.h"
#include "unit_base.h"
#include "unit_neopixel.h"
#include "ws2812.h"
/** Private data structure */
struct priv {
char port_name;
uint8_t pin_number;
uint16_t pixels;
uint32_t ll_pin;
GPIO_TypeDef *port;
};
// ------------------------------------------------------------------------
/** Load from a binary buffer stored in Flash */
static void Npx_loadBinary(Unit *unit, PayloadParser *pp)
{
struct priv *priv = unit->data;
priv->port_name = pp_char(pp);
priv->pin_number = pp_u8(pp);
priv->pixels = pp_u16(pp);
}
/** Write to a binary buffer for storing in Flash */
static void Npx_writeBinary(Unit *unit, PayloadBuilder *pb)
{
struct priv *priv = unit->data;
pb_char(pb, priv->port_name);
pb_u8(pb, priv->pin_number);
pb_u16(pb, priv->pixels);
}
// ------------------------------------------------------------------------
/** Parse a key-value pair from the INI file */
static bool Npx_loadIni(Unit *unit, const char *key, const char *value)
{
bool suc = true;
struct priv *priv = unit->data;
if (streq(key, "pin")) {
suc = str_parse_pin(value, &priv->port_name, &priv->pin_number);
}
else if (streq(key, "pixels")) {
priv->pixels = (uint16_t) avr_atoi(value);
}
else {
return false;
}
return suc;
}
/** Generate INI file section for the unit */
static void Npx_writeIni(Unit *unit, IniWriter *iw)
{
struct priv *priv = unit->data;
iw_comment(iw, "Data pin");
iw_entry(iw, "pin", "%c%d", priv->port_name, priv->pin_number);
iw_comment(iw, "Number of pixels");
iw_entry(iw, "pixels", "%d", priv->pixels);
}
// ------------------------------------------------------------------------
/** Allocate data structure and set defaults */
static bool Npx_preInit(Unit *unit)
{
bool suc = true;
struct priv *priv = unit->data = calloc_ck(1, sizeof(struct priv), &suc);
CHECK_SUC();
// some defaults
priv->pin_number = 0;
priv->port_name = 'A';
priv->pixels = 1;
return true;
}
/** Finalize unit set-up */
static bool Npx_init(Unit *unit)
{
bool suc = true;
struct priv *priv = unit->data;
// --- Parse config ---
priv->ll_pin = plat_pin2ll(priv->pin_number, &suc);
priv->port = plat_port2periph(priv->port_name, &suc);
Resource rsc = plat_pin2resource(priv->port_name, priv->pin_number, &suc);
if (!suc) {
unit->status = E_BAD_CONFIG;
return false;
}
// --- Claim resources ---
if (!rsc_claim(unit, rsc)) return false;
// --- Init hardware ---
LL_GPIO_SetPinMode(priv->port, priv->ll_pin, LL_GPIO_MODE_OUTPUT);
LL_GPIO_SetPinOutputType(priv->port, priv->ll_pin, LL_GPIO_OUTPUT_PUSHPULL);
LL_GPIO_SetPinSpeed(priv->port, priv->ll_pin, LL_GPIO_SPEED_FREQ_HIGH);
// clear strip
ws2812_clear(priv->port, priv->ll_pin, priv->pixels);
return true;
}
/** Tear down the unit */
static void Npx_deInit(Unit *unit)
{
struct priv *priv = unit->data;
// configure the pin as analog
LL_GPIO_SetPinMode(priv->port, priv->ll_pin, LL_GPIO_MODE_ANALOG);
// Release all resources
rsc_teardown(unit);
// Free memory
free(unit->data);
unit->data = NULL;
}
// ------------------------------------------------------------------------
enum PinCmd_ {
CMD_CLEAR = 0,
CMD_LOAD = 1,
CMD_LOAD_U32_LE = 2,
CMD_LOAD_U32_BE = 3,
};
/** Handle a request message */
static bool Npx_handleRequest(Unit *unit, TF_ID frame_id, uint8_t command, PayloadParser *pp)
{
(void)pp;
struct priv *priv = unit->data;
switch (command) {
case CMD_CLEAR:
ws2812_clear(priv->port, priv->ll_pin, priv->pixels);
break;
case CMD_LOAD:
if (pp_length(pp) != priv->pixels*3) goto bad_count;
ws2812_load_raw(priv->port, priv->ll_pin, pp->current, priv->pixels);
break;
case CMD_LOAD_U32_LE:
if (pp_length(pp) != priv->pixels*4) goto bad_count;
ws2812_load_sparse(priv->port, priv->ll_pin, pp->current, priv->pixels, 0);
break;
case CMD_LOAD_U32_BE:
if (pp_length(pp) != priv->pixels*4) goto bad_count;
ws2812_load_sparse(priv->port, priv->ll_pin, pp->current, priv->pixels, 1);
break;
default:
sched_respond_bad_cmd(frame_id);
return false;
}
return true;
bad_count:
sched_respond_err(frame_id, "BAD PIXEL COUNT");
return false;
}
// ------------------------------------------------------------------------
/** Unit template */
const UnitDriver UNIT_NEOPIXEL = {
.name = "NEOPIXEL",
.description = "Neopixel RGB LED strip",
// Settings
.preInit = Npx_preInit,
.cfgLoadBinary = Npx_loadBinary,
.cfgWriteBinary = Npx_writeBinary,
.cfgLoadIni = Npx_loadIni,
.cfgWriteIni = Npx_writeIni,
// Init
.init = Npx_init,
.deInit = Npx_deInit,
// Function
.handleRequest = Npx_handleRequest,
};
+12
View File
@@ -0,0 +1,12 @@
//
// Created by MightyPork on 2017/11/25.
//
#ifndef U_NEOPIXEL_H
#define U_NEOPIXEL_H
#include "unit.h"
extern const UnitDriver UNIT_NEOPIXEL;
#endif //U_NEOPIXEL_H
+95
View File
@@ -0,0 +1,95 @@
#include "platform.h"
#include "ws2812.h"
static //inline __attribute__((always_inline))
void ws2812_byte(GPIO_TypeDef *port, uint32_t ll_pin, uint8_t b)
{
__disable_irq();
for (register volatile uint8_t i = 0; i < 8; i++) {
LL_GPIO_SetOutputPin(port, ll_pin);
// duty cycle determines bit value
if (b & 0x80) {
for(uint32_t _i = 0; _i < 10; _i++) asm volatile("nop");
LL_GPIO_ResetOutputPin(port, ll_pin);
for(uint32_t _i = 0; _i < 10; _i++) asm volatile("nop");
} else {
for(uint32_t _i = 0; _i < 5; _i++) asm volatile("nop");
LL_GPIO_ResetOutputPin(port, ll_pin);
for(uint32_t _i = 0; _i < 10; _i++) asm volatile("nop");
}
b <<= 1; // shift to next bit
}
__enable_irq();
}
/** Set many RGBs */
void ws2812_load(GPIO_TypeDef *port, uint32_t ll_pin, uint32_t *rgbs, uint32_t count)
{
vPortEnterCritical();
for (uint32_t i = 0; i < count; i++) {
uint32_t rgb = *rgbs++;
ws2812_byte(port, ll_pin, rgb_g(rgb));
ws2812_byte(port, ll_pin, rgb_r(rgb));
ws2812_byte(port, ll_pin, rgb_b(rgb));
}
vPortExitCritical();
// TODO: Delay 50 us
}
/** Set many RGBs from packed stream */
void ws2812_load_raw(GPIO_TypeDef *port, uint32_t ll_pin, uint8_t *rgbs, uint32_t count)
{
vPortEnterCritical();
uint8_t b, g, r;
for (uint32_t i = 0; i < count; i++) {
r = *rgbs++;
g = *rgbs++;
b = *rgbs++;
ws2812_byte(port, ll_pin, g);
ws2812_byte(port, ll_pin, r);
ws2812_byte(port, ll_pin, b);
}
vPortExitCritical();
// TODO: Delay 50 us
}
/** Set many RGBs from uint32 stream */
void ws2812_load_sparse(GPIO_TypeDef *port, uint32_t ll_pin, uint8_t *rgbs, uint32_t count, bool bigendian)
{
vPortEnterCritical();
uint8_t b, g, r;
for (uint32_t i = 0; i < count; i++) {
if (bigendian) {
rgbs++; // skip
b = *rgbs++;
g = *rgbs++;
r = *rgbs++;
} else {
r = *rgbs++;
g = *rgbs++;
b = *rgbs++;
rgbs++; // skip
}
ws2812_byte(port, ll_pin, g);
ws2812_byte(port, ll_pin, r);
ws2812_byte(port, ll_pin, b);
}
vPortExitCritical();
// TODO: Delay 50 us
}
/** Set many RGBs */
void ws2812_clear(GPIO_TypeDef *port, uint32_t ll_pin, uint32_t count)
{
vPortEnterCritical();
for (uint32_t i = 0; i < count*3; i++) {
ws2812_byte(port, ll_pin, 0);
}
vPortExitCritical();
// TODO: Delay 50 us
}
+73
View File
@@ -0,0 +1,73 @@
#pragma once
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
/* Exported macros -----------------------------------------------------------*/
/**
* @brief Compose an RGB color.
* @param r, g, b - components 0xFF
* @returns integer 0xRRGGBB
*/
#define rgb(r, g, b) (((0xFF & (r)) << 16) | ((0xFF & (g)) << 8) | (0xFF & (b)))
/* Get components */
#define rgb_r(rgb) (uint8_t)(((rgb) >> 16) & 0xFF)
#define rgb_g(rgb) (uint8_t)(((rgb) >> 8) & 0xFF)
#define rgb_b(rgb) (uint8_t)((rgb) & 0xFF)
typedef union {
struct {
uint8_t r;
uint8_t g;
uint8_t b;
};
uint32_t num;
} xrgb_t;
/* Exported functions --------------------------------------------------------*/
/**
* @brief Set color of multiple chained RGB leds
*
* @param port
* @param ll_pin
* @param rgbs - array of colors (0xRRGGBB)
* @param count - number of pixels
*/
void ws2812_load(GPIO_TypeDef *port, uint32_t ll_pin, uint32_t *rgbs, uint32_t count);
/**
* Load RGBs from a packed byte stream
*
* @param port
* @param ll_pin
* @param rgbs - packed R,G,B, R,G,B, ... array
* @param count - number of pixels (triplets)
*/
void ws2812_load_raw(GPIO_TypeDef *port, uint32_t ll_pin, uint8_t *rgbs, uint32_t count);
/**
* Load all pixels with BLACK (0,0,0)
*
* @param port
* @param ll_pin
* @param count - number of pixels
*/
void ws2812_clear(GPIO_TypeDef *port, uint32_t ll_pin, uint32_t count);
/**
* Load from a stream of 32-bit numbers (4th or 1st byte skipped)
* @param port
* @param ll_pin
* @param rgbs - payload
* @param count - number of pixels
* @param bigendian - big endian ordering
*/
void ws2812_load_sparse(GPIO_TypeDef *port, uint32_t ll_pin, uint8_t *rgbs, uint32_t count, bool bigendian);