sipo implemented

remotes/ondrovo/add-sipo
Ondřej Hruška 7 years ago
parent 98900cdb3b
commit 9cff75554b
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 1
      gex.mk
  2. 5
      platform/hw_utils.c
  3. 2
      platform/platform.c
  4. 94
      units/sipo/_sipo_api.c
  5. 3
      units/sipo/_sipo_init.c
  6. 50
      units/sipo/_sipo_internal.h
  7. 10
      units/sipo/_sipo_settings.c
  8. 80
      units/sipo/unit_sipo.c

@ -14,6 +14,7 @@ GEX_SRC_DIR = \
User/units/i2c \ User/units/i2c \
User/units/spi \ User/units/spi \
User/units/adc \ User/units/adc \
User/units/sipo \
User/TinyFrame \ User/TinyFrame \
User/CWPack \ User/CWPack \
User/tasks User/tasks

@ -240,6 +240,8 @@ char * pinmask2str_up(uint32_t pins, char *buffer)
return buffer; return buffer;
} }
#pragma GCC push_options
#pragma GCC optimize ("O2")
/** spread a packed pinfield using a mask */ /** spread a packed pinfield using a mask */
uint32_t pinmask_spread_32(uint32_t packed, uint32_t mask) uint32_t pinmask_spread_32(uint32_t packed, uint32_t mask)
{ {
@ -247,7 +249,7 @@ uint32_t pinmask_spread_32(uint32_t packed, uint32_t mask)
uint32_t poke = 1; uint32_t poke = 1;
if(packed == 0) return 0; if(packed == 0) return 0;
for (int i = 0; i<32; i++) { for (int i = 0; i < 32; i++) {
if (mask & 1) { if (mask & 1) {
if (packed & poke) { if (packed & poke) {
result |= 1<<i; result |= 1<<i;
@ -295,6 +297,7 @@ uint8_t pinmask_translate(uint32_t mask, uint8_t index)
} }
return 0; return 0;
} }
#pragma GCC pop_options
/** Configure unit pins as analog (part of unit teardown) */ /** Configure unit pins as analog (part of unit teardown) */
void hw_deinit_unit_pins(Unit *unit) void hw_deinit_unit_pins(Unit *unit)

@ -17,6 +17,7 @@
#include "units/test/unit_test.h" #include "units/test/unit_test.h"
#include "units/usart/unit_usart.h" #include "units/usart/unit_usart.h"
#include "units/spi/unit_spi.h" #include "units/spi/unit_spi.h"
#include "units/sipo/unit_sipo.h"
#include "hw_utils.h" #include "hw_utils.h"
void plat_init_resources(void) void plat_init_resources(void)
@ -86,6 +87,7 @@ void plat_init_resources(void)
ureg_add_type(&UNIT_USART); ureg_add_type(&UNIT_USART);
ureg_add_type(&UNIT_1WIRE); ureg_add_type(&UNIT_1WIRE);
ureg_add_type(&UNIT_ADC); ureg_add_type(&UNIT_ADC);
ureg_add_type(&UNIT_SIPO);
// Free all present resources // Free all present resources
{ {

@ -9,3 +9,97 @@
#define SIPO_INTERNAL #define SIPO_INTERNAL
#include "_sipo_internal.h" #include "_sipo_internal.h"
static void send_pulse(bool pol, GPIO_TypeDef *port, uint32_t ll)
{
if (pol) {
LL_GPIO_SetOutputPin(port, ll);
}
else {
LL_GPIO_ResetOutputPin(port, ll);
}
__asm_loop(2);
if (pol) {
LL_GPIO_ResetOutputPin(port, ll);
}
else {
LL_GPIO_SetOutputPin(port, ll);
}
}
#pragma GCC push_options
#pragma GCC optimize ("O2")
error_t UU_SIPO_Write(Unit *unit, const uint8_t *buffer, uint16_t buflen)
{
CHECK_TYPE(unit, &UNIT_SIPO);
struct priv *priv = unit->data;
if (buflen % priv->data_width != 0) {
dbg("Buflen %d vs width %d", (int)buflen, (int)priv->data_width);
return E_BAD_COUNT; // must be a multiple of the channel count
}
// buffer contains data for the individual data pins, back to back as AAA BBB CCC (whole bytes)
const uint8_t data_width = priv->data_width;
const uint16_t bytelen = buflen / data_width;
const uint16_t mask = priv->data_pins;
uint8_t offsets[16];
for (int i=0; i<16; i++) offsets[i] = (uint8_t) (bytelen * i);
for (int32_t bn = bytelen - 1; bn >= 0; bn--) {
// send the byte
for (int32_t i = 0; i < 8; i++) {
uint16_t packed = 0;
for (int32_t j = data_width - 1; j >= 0; j--) {
packed |= (buffer[bn + offsets[j]] >> i) & 1;
if (j > 0) packed <<= 1;
}
uint16_t spread = pinmask_spread(packed, mask);
priv->data_port->BSRR = spread | (((~spread) & mask) << 16);
// Shift clock pulse
send_pulse(priv->shift_pol, priv->shift_port, priv->shift_ll);
}
}
send_pulse(priv->store_pol, priv->store_port, priv->store_ll);
return E_SUCCESS;
}
#pragma GCC pop_options
error_t UU_SIPO_DirectData(Unit *unit, uint16_t data_packed)
{
CHECK_TYPE(unit, &UNIT_SIPO);
struct priv *priv = unit->data;
uint16_t spread = pinmask_spread(data_packed, priv->data_pins);
priv->data_port->BSRR = spread | (((~spread) & priv->data_pins) << 16);
return E_SUCCESS;
}
error_t UU_SIPO_DirectClear(Unit *unit)
{
CHECK_TYPE(unit, &UNIT_SIPO);
struct priv *priv = unit->data;
send_pulse(priv->clear_pol, priv->clear_port, priv->clear_ll);
return E_SUCCESS;
}
error_t UU_SIPO_DirectShift(Unit *unit)
{
CHECK_TYPE(unit, &UNIT_SIPO);
struct priv *priv = unit->data;
send_pulse(priv->shift_pol, priv->shift_port, priv->shift_ll);
return E_SUCCESS;
}
error_t UU_SIPO_DirectStore(Unit *unit)
{
CHECK_TYPE(unit, &UNIT_SIPO);
struct priv *priv = unit->data;
send_pulse(priv->store_pol, priv->store_port, priv->store_ll);
return E_SUCCESS;
}

@ -108,6 +108,9 @@ error_t USIPO_init(Unit *unit)
else else
LL_GPIO_SetOutputPin(priv->clear_port, priv->clear_ll); LL_GPIO_SetOutputPin(priv->clear_port, priv->clear_ll);
// initial clear
UU_SIPO_DirectClear(unit);
return E_SUCCESS; return E_SUCCESS;
} }

@ -67,4 +67,54 @@ error_t USIPO_init(Unit *unit);
/** Tear down the unit */ /** Tear down the unit */
void USIPO_deInit(Unit *unit); void USIPO_deInit(Unit *unit);
// ------------------------------------------------------------------------
/**
* Write a buffer to the pins.
* Buffer contains data for the individual channels, sequentially (AAAAAA BBBBBB CCCCCC ...)
* The bytes are sent LSB first, from the last byte (e.g. 1,2,3 - 3 is sent first, LSB-first).
*
* The chunks order is from the lowest to the highest bit
*
* @param unit
* @param buffer - buffer of data to send
* @param buflen - number of bytes in the buffer
* @return success
*/
error_t UU_SIPO_Write(Unit *unit, const uint8_t *buffer, uint16_t buflen);
/**
* Direct access to the output data pins (may be useful for debugging, or circuits that use them
* for something else when not loading a new value).
*
* @param unit
* @param data_packed - packed data to set on the output (right-aligned, highest to lowest pin)
* @return success
*/
error_t UU_SIPO_DirectData(Unit *unit, uint16_t data_packed);
/**
* Send a clear pulse.
*
* @param unit
* @return success
*/
error_t UU_SIPO_DirectClear(Unit *unit);
/**
* Send a shift pulse.
*
* @param unit
* @return success
*/
error_t UU_SIPO_DirectShift(Unit *unit);
/**
* Send a store pulse.
*
* @param unit
* @return success
*/
error_t UU_SIPO_DirectStore(Unit *unit);
#endif //GEX_F072_SIPO_INTERNAL_H #endif //GEX_F072_SIPO_INTERNAL_H

@ -103,20 +103,20 @@ void USIPO_writeIni(Unit *unit, IniWriter *iw)
{ {
struct priv *priv = unit->data; struct priv *priv = unit->data;
iw_comment(iw, "Store pin & its active edge (1-rising,0-falling)");
iw_entry(iw, "store-pin", "%c%d", priv->store_pname, priv->store_pnum);
iw_entry(iw, "store-pol", "%d", priv->store_pol);
iw_comment(iw, "Shift pin & its active edge"); iw_comment(iw, "Shift pin & its active edge");
iw_entry(iw, "shift-pin", "%c%d", priv->shift_pname, priv->shift_pnum); iw_entry(iw, "shift-pin", "%c%d", priv->shift_pname, priv->shift_pnum);
iw_entry(iw, "shift-pol", "%d", priv->shift_pol); iw_entry(iw, "shift-pol", "%d", priv->shift_pol);
iw_comment(iw, "Store pin & its active edge (1-rising,0-falling)");
iw_entry(iw, "store-pin", "%c%d", priv->store_pname, priv->store_pnum);
iw_entry(iw, "store-pol", "%d", priv->store_pol);
iw_comment(iw, "Clear pin & its active level"); iw_comment(iw, "Clear pin & its active level");
iw_entry(iw, "clear-pin", "%c%d", priv->clear_pname, priv->clear_pnum); iw_entry(iw, "clear-pin", "%c%d", priv->clear_pname, priv->clear_pnum);
iw_entry(iw, "clear-pol", "%d", priv->clear_pol); iw_entry(iw, "clear-pol", "%d", priv->clear_pol);
iw_comment(iw, "Data port and pins"); iw_comment(iw, "Data port and pins");
iw_entry(iw, "data-port", "%c", priv->data_pname); iw_entry(iw, "data-port", "%c", priv->data_pname);
iw_entry(iw, "data-pins", "%s", pinmask2str(priv->data_pins, unit_tmp512)); iw_entry(iw, "data-pins", "%s", pinmask2str_up(priv->data_pins, unit_tmp512));
} }

@ -9,68 +9,42 @@
#include "_sipo_internal.h" #include "_sipo_internal.h"
error_t UU_SIPO_Write(Unit *unit, const uint8_t *buffer, uint16_t buflen)
{
struct priv *priv = unit->data;
if (buflen % priv->data_width != 0) {
return E_BAD_COUNT; // must be a multiple of the channel count
}
// buffer contains data for the individual data pins, back to back as AAA BBB CCC (whole bytes)
const uint16_t bytelen = buflen / priv->data_width;
for (int bn = 0; bn < bytelen; bn--) {
// send the byte
for (int i = 0; i < 8; i++) {
uint16_t packed = 0;
for (int j = 0; j < priv->data_width; j++) {
packed |= (bool) (buffer[bn + j * bytelen] & (1 << i));
packed <<= 1;
}
uint16_t spread = pinmask_spread(packed, priv->data_pins);
uint16_t set = spread;
uint16_t reset = ((~spread) & priv->data_pins);
priv->data_port->BSRR = set | (reset << 16);
if (priv->shift_pol) {
LL_GPIO_SetOutputPin(priv->shift_port, priv->shift_ll);
LL_GPIO_ResetOutputPin(priv->shift_port, priv->shift_ll);
}
else {
LL_GPIO_ResetOutputPin(priv->shift_port, priv->shift_ll);
LL_GPIO_SetOutputPin(priv->shift_port, priv->shift_ll);
}
}
}
if (priv->store_pol) {
LL_GPIO_SetOutputPin(priv->store_port, priv->store_ll);
LL_GPIO_ResetOutputPin(priv->store_port, priv->store_ll);
}
else {
LL_GPIO_ResetOutputPin(priv->store_port, priv->store_ll);
LL_GPIO_SetOutputPin(priv->store_port, priv->store_ll);
}
}
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
enum SipoCmd_ { enum SipoCmd_ {
CMD_WRITE, CMD_WRITE = 0,
CMD_DIRECT_DATA = 1,
CMD_DIRECT_SHIFT = 2,
CMD_DIRECT_CLEAR = 3,
CMD_DIRECT_STORE = 4,
}; };
/** Handle a request message */ /** Handle a request message */
static error_t USIPO_handleRequest(Unit *unit, TF_ID frame_id, uint8_t command, PayloadParser *pp) static error_t USIPO_handleRequest(Unit *unit, TF_ID frame_id, uint8_t command, PayloadParser *pp)
{ {
switch (command) { switch (command) {
case CMD_WRITE:; case CMD_WRITE:
uint32_t len; {
const uint8_t *tail = pp_tail(pp, &len); uint32_t len;
UU_SIPO_Write(unit, (uint8_t *) tail, (uint16_t) len); const uint8_t *tail = pp_tail(pp, &len);
TRY(UU_SIPO_Write(unit, (uint8_t *) tail, (uint16_t) len));
}
return E_SUCCESS;
case CMD_DIRECT_DATA:
TRY(UU_SIPO_DirectData(unit, pp_u16(pp)));
return E_SUCCESS;
case CMD_DIRECT_CLEAR:
TRY(UU_SIPO_DirectClear(unit));
return E_SUCCESS;
case CMD_DIRECT_SHIFT:
TRY(UU_SIPO_DirectShift(unit));
return E_SUCCESS;
case CMD_DIRECT_STORE:
TRY(UU_SIPO_DirectStore(unit));
return E_SUCCESS; return E_SUCCESS;
default: default:

Loading…
Cancel
Save