35 #if defined(PIOS_INCLUDE_RE1_FPGA)
38 #include "stm32f4xx_rcc.h"
84 enum pios_re1fpga_register {
85 RE1FPGA_REG_HWREV = 0x00,
86 RE1FPGA_REG_CFG = 0x01,
87 RE1FPGA_REG_CTL = 0x02,
88 RE1FPGA_REG_BLACK = 0x03,
89 RE1FPGA_REG_WHITE = 0x04,
90 RE1FPGA_REG_THR = 0x05,
91 RE1FPGA_REG_XCFG = 0x06,
92 RE1FPGA_REG_XCFG2 = 0x07,
93 RE1FPGA_REG_IRCFG = 0x08,
94 RE1FPGA_REG_IRDATA = 0x09,
95 RE1FPGA_REG_LED = 0x0F,
98 struct re1_shadow_reg {
111 enum pios_re1fpga_dev_magic {
112 PIOS_RE1FPGA_DEV_MAGIC = 0xbadfed42,
119 struct re1_shadow_reg shadow_reg;
120 enum pios_re1fpga_dev_magic
magic;
123 #define MIN(a, b) ({ __typeof__ (a) _a = (a); __typeof__ (b) _b = (b); _a < _b ? _a : _b; })
126 static struct re1fpga_dev *dev;
128 static int32_t PIOS_RE1FPGA_WriteReg(uint8_t reg, uint8_t
data, uint8_t mask);
129 static int32_t PIOS_RE1FPGA_WriteRegDirect(
enum pios_re1fpga_register reg, uint8_t
data);
130 static uint8_t PIOS_RE1FPGA_ReadReg(uint8_t reg);
131 static void update_shadow_regs();
132 int32_t PIOS_RE1FPGA_SetLEDs(
const uint8_t * led_data, uint16_t n_leds);
141 struct re1fpga_dev *re1fpga_dev;
143 re1fpga_dev = (
struct re1fpga_dev *)
PIOS_malloc(
sizeof(*re1fpga_dev));
147 re1fpga_dev->magic = PIOS_RE1FPGA_DEV_MAGIC;
152 volatile uint8_t test;
160 dev = PIOS_RE1FPGA_alloc(cfg);
164 dev->spi_id = spi_id;
165 dev->slave_num = slave_num;
170 GPIO_Init(dev->cfg->cdone_pin.gpio, (GPIO_InitTypeDef *) & (dev->cfg->cdone_pin.init));
171 GPIO_Init(dev->cfg->cresetb_pin.gpio, (GPIO_InitTypeDef *) & (dev->cfg->cresetb_pin.init));
174 GPIO_ResetBits(dev->cfg->cresetb_pin.gpio, dev->cfg->cresetb_pin.init.GPIO_Pin);
176 GPIO_SetBits(dev->cfg->cresetb_pin.gpio, dev->cfg->cresetb_pin.init.GPIO_Pin);
178 for (
int i=0;
i<100;
i++) {
179 if (GPIO_ReadInputDataBit(dev->cfg->cdone_pin.gpio, dev->cfg->cdone_pin.init.GPIO_Pin))
184 if (!GPIO_ReadInputDataBit(dev->cfg->cdone_pin.gpio, dev->cfg->cdone_pin.init.GPIO_Pin))
189 GPIO_Init(dev->cfg->mco_pin.gpio, (GPIO_InitTypeDef *) & (dev->cfg->mco_pin.init));
190 GPIO_PinAFConfig(dev->cfg->mco_pin.gpio,
191 __builtin_ctz(dev->cfg->mco_pin.init.GPIO_Pin),
193 RCC_MCO1Config(RCC_MCO1Source_HSE, RCC_MCO1Div_1);
197 GPIO_Init(dev->cfg->rst_pin.gpio, (GPIO_InitTypeDef *) & (dev->cfg->rst_pin.init));
198 GPIO_ResetBits(dev->cfg->rst_pin.gpio, dev->cfg->rst_pin.init.GPIO_Pin);
204 GPIO_SetBits(dev->cfg->rst_pin.gpio, dev->cfg->rst_pin.init.GPIO_Pin);
206 GPIO_ResetBits(dev->cfg->rst_pin.gpio, dev->cfg->rst_pin.init.GPIO_Pin);
210 PIOS_RE1FPGA_WriteRegDirect(RE1FPGA_REG_CFG, 0x00);
211 PIOS_RE1FPGA_WriteRegDirect(RE1FPGA_REG_CTL, 0x00);
212 PIOS_RE1FPGA_WriteRegDirect(RE1FPGA_REG_BLACK, 35);
213 PIOS_RE1FPGA_WriteRegDirect(RE1FPGA_REG_BLACK, 110);
214 PIOS_RE1FPGA_WriteRegDirect(RE1FPGA_REG_THR, 120);
215 PIOS_RE1FPGA_WriteRegDirect(RE1FPGA_REG_XCFG, 0x08);
216 PIOS_RE1FPGA_WriteRegDirect(RE1FPGA_REG_XCFG2, 0x10);
217 PIOS_RE1FPGA_WriteRegDirect(RE1FPGA_REG_IRCFG, 0x00);
220 update_shadow_regs();
229 static int32_t PIOS_RE1FPGA_Validate(
struct re1fpga_dev *dev)
233 if (dev->magic != PIOS_RE1FPGA_DEV_MAGIC)
235 if (dev->spi_id == 0)
244 static int32_t PIOS_RE1FPGA_ClaimBus()
246 if (PIOS_RE1FPGA_Validate(dev) != 0)
261 static int32_t PIOS_RE1FPGA_ReleaseBus()
263 if (PIOS_RE1FPGA_Validate(dev) != 0)
279 static int32_t PIOS_RE1FPGA_WriteReg(
enum pios_re1fpga_register reg, uint8_t
data, uint8_t mask)
283 if (PIOS_RE1FPGA_Validate(dev) != 0)
287 case RE1FPGA_REG_HWREV:
289 case RE1FPGA_REG_CFG:
290 cur_reg = &dev->shadow_reg.reg_cfg;
292 case RE1FPGA_REG_CTL:
293 cur_reg = &dev->shadow_reg.reg_ctl;
295 case RE1FPGA_REG_BLACK:
296 cur_reg = &dev->shadow_reg.reg_black;
298 case RE1FPGA_REG_WHITE:
299 cur_reg = &dev->shadow_reg.reg_white;
301 case RE1FPGA_REG_THR:
302 cur_reg = &dev->shadow_reg.reg_thr;
304 case RE1FPGA_REG_XCFG:
305 cur_reg = &dev->shadow_reg.reg_xcfg;
307 case RE1FPGA_REG_XCFG2:
308 cur_reg = &dev->shadow_reg.reg_xcfg2;
310 case RE1FPGA_REG_IRCFG:
311 cur_reg = &dev->shadow_reg.reg_ircfg;
315 case RE1FPGA_REG_LED:
319 uint8_t new_data = (*cur_reg & ~mask) | (data & mask);
322 if (new_data == *cur_reg) {
328 return PIOS_RE1FPGA_WriteRegDirect(reg, new_data);
331 static int32_t PIOS_RE1FPGA_WriteRegDirect(
enum pios_re1fpga_register reg, uint8_t data)
334 if (PIOS_RE1FPGA_Validate(dev) != 0)
337 if (PIOS_RE1FPGA_ClaimBus() != 0)
343 PIOS_RE1FPGA_ReleaseBus();
353 static uint8_t PIOS_RE1FPGA_ReadReg(
enum pios_re1fpga_register reg)
357 PIOS_RE1FPGA_ClaimBus();
362 PIOS_RE1FPGA_ReleaseBus();
371 static void update_shadow_regs(
void)
373 dev->shadow_reg.reg_hwrev = PIOS_RE1FPGA_ReadReg(RE1FPGA_REG_HWREV);
374 dev->shadow_reg.reg_cfg = PIOS_RE1FPGA_ReadReg(RE1FPGA_REG_CFG);
375 dev->shadow_reg.reg_ctl = PIOS_RE1FPGA_ReadReg(RE1FPGA_REG_CTL);
376 dev->shadow_reg.reg_black = PIOS_RE1FPGA_ReadReg(RE1FPGA_REG_BLACK);
377 dev->shadow_reg.reg_white = PIOS_RE1FPGA_ReadReg(RE1FPGA_REG_WHITE);
378 dev->shadow_reg.reg_thr = PIOS_RE1FPGA_ReadReg(RE1FPGA_REG_THR);
379 dev->shadow_reg.reg_xcfg = PIOS_RE1FPGA_ReadReg(RE1FPGA_REG_XCFG);
380 dev->shadow_reg.reg_xcfg2 = PIOS_RE1FPGA_ReadReg(RE1FPGA_REG_XCFG2);
381 dev->shadow_reg.reg_ircfg = PIOS_RE1FPGA_ReadReg(RE1FPGA_REG_IRCFG);
389 return dev->shadow_reg.reg_hwrev;
399 switch (ir_protocol) {
410 return PIOS_RE1FPGA_WriteReg(RE1FPGA_REG_IRCFG, value, 0x0F);
421 if (PIOS_RE1FPGA_ClaimBus() != 0)
428 PIOS_RE1FPGA_ReleaseBus();
445 return PIOS_RE1FPGA_WriteReg(RE1FPGA_REG_CFG, data, 0x01);
455 data |= bidrectional << 1;
458 return PIOS_RE1FPGA_WriteReg(RE1FPGA_REG_CFG, data, 0x06);
471 return PIOS_RE1FPGA_WriteReg(RE1FPGA_REG_CFG, data, 0x18);
486 return PIOS_RE1FPGA_WriteReg(RE1FPGA_REG_CFG, data, 0x40);
495 uint8_t data = enable;
497 return PIOS_RE1FPGA_WriteReg(RE1FPGA_REG_CTL, data, 0x01);
512 return PIOS_RE1FPGA_WriteReg(RE1FPGA_REG_CFG, value, 0x20);
520 PIOS_RE1FPGA_WriteReg(RE1FPGA_REG_BLACK, black, 0xFF);
521 PIOS_RE1FPGA_WriteReg(RE1FPGA_REG_WHITE, white, 0xFF);
529 return PIOS_RE1FPGA_WriteReg(RE1FPGA_REG_THR, threshold, 0xFF);
543 uint8_t value = 8 + x_offset;
544 PIOS_RE1FPGA_WriteReg(RE1FPGA_REG_XCFG, value, 0x0F);
552 x_scale = (x_scale & 0x0F) << 4;
553 PIOS_RE1FPGA_WriteReg(RE1FPGA_REG_XCFG, x_scale, 0xF0);
564 cfg = (enabled << 6) | (x_shift_right & 0x3F);
565 PIOS_RE1FPGA_WriteReg(RE1FPGA_REG_XCFG2, cfg, 0xFF);
571 struct ws2811_pixel_data_s {
578 #define WS2811_MAGIC 0x31313832
582 struct ws2811_pixel_data_s pixel_data[0];
592 sizeof(
struct ws2811_pixel_data_s) * max_leds);
598 dev->
magic = WS2811_MAGIC;
618 if (idx > ws2811_dev->max_idx) {
619 ws2811_dev->max_idx = idx;
622 ws2811_dev->pixel_data[idx] = (
struct ws2811_pixel_data_s)
623 { .r = r, .g = g, .b = b };
641 if (PIOS_RE1FPGA_ClaimBus() != 0)
644 n_leds =
MIN(ws2811_dev->max_idx + 1, 1024);
648 sizeof(
struct ws2811_pixel_data_s) * n_leds);
650 PIOS_RE1FPGA_ReleaseBus();
657 uint16_t n_leds =
MIN(ws2811_dev->max_idx + 1, 1024);
void PIOS_RE1FPGA_SetBwLevels(uint8_t black, uint8_t white)
int32_t PIOS_RE1FPGA_Init(pios_spi_t spi_id, uint32_t slave_num, const struct pios_re1fpga_cfg *cfg, bool load_config)
int PIOS_WS2811_get_num_leds(ws2811_dev_t dev)
Find out how many LEDs are configured on an interface.
int32_t PIOS_RE1FPGA_SetBuzzerType(enum pios_re1fpga_buzzer_types type)
Main PiOS header to include all the compiled in PiOS options.
int32_t PIOS_SPI_RC_PinSet(pios_spi_t spi_dev, uint32_t slave_id, bool pin_value)
pios_re1fpga_buzzer_types
int32_t PIOS_SPI_ClaimBus(pios_spi_t spi_dev)
void * PIOS_malloc(size_t size)
int32_t PIOS_RE1FPGA_Buzzer(bool enable)
int PIOS_WS2811_init(ws2811_dev_t *dev_out, const struct pios_ws2811_cfg *cfg, int max_leds)
Allocate and initialise WS2811 device.
uint8_t data[XFER_BYTES_PER_PACKET]
void PIOS_RE1FPGA_SetXOffset(int8_t x_offset)
static struct flyingpicmd_cfg_fa cfg
void PIOS_RE1FPGA_SetXScale(uint8_t x_scale)
int32_t PIOS_RE1FPGA_MPTxPinMode(bool bidrectional, bool invert)
void PIOS_WS2811_set_all(ws2811_dev_t dev, uint8_t r, uint8_t g, uint8_t b)
Sets all LEDs to a color value.
int32_t PIOS_RE1FPGA_SetSyncThreshold(uint8_t threshold)
uint8_t PIOS_SPI_TransferByte(pios_spi_t spi_dev, uint8_t b)
int32_t PIOS_RE1FPGA_SetNotificationLedColor(enum pios_re1fpga_led_colors led_colors)
int32_t PIOS_SPI_TransferBlock(pios_spi_t spi_dev, const uint8_t *send_buffer, uint8_t *receive_buffer, uint16_t len)
uint8_t PIOS_RE1FPGA_GetHWRevision()
int32_t PIOS_RE1FPGA_SetIRProtocol(enum pios_re1fpga_ir_protocols ir_protocol)
int32_t PIOS_RE1FPGA_MPTxPinPullUpDown(bool enable, bool pullup)
void PIOS_WS2811_set(ws2811_dev_t dev, int idx, uint8_t r, uint8_t g, uint8_t b)
Set a given LED to a color value.
void PIOS_WS2811_trigger_update(ws2811_dev_t dev)
Trigger an update of the LED strand.
pios_re1fpga_ir_protocols
int32_t PIOS_SPI_ReleaseBus(pios_spi_t spi_dev)
int32_t PIOS_RE1FPGA_SerialRxInvert(bool invert)
void PIOS_RE1FPGA_Set3DConfig(enum pios_video_3d_mode mode, uint8_t x_shift_right)
int32_t PIOS_RE1FPGA_SetIRData(const uint8_t *ir_data, uint8_t n_bytes)
#define PIOS_Assert(test)
int32_t PIOS_DELAY_WaitmS(uint32_t mS)