added optimized spread/pack utils + "high-speed" variants of DOut UU

sipo
Ondřej Hruška 7 years ago
parent 658b1befee
commit 7622cf0aaf
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 21
      platform/hw_utils.c
  2. 14
      platform/hw_utils.h
  3. 68
      units/digital_out/_dout_api.c
  4. 11
      units/digital_out/unit_dout.h

@ -240,34 +240,45 @@ char * pinmask2str_up(uint32_t pins, char *buffer)
return buffer; return buffer;
} }
/** Spread packed port pins using a mask */ /** spread a packed pinfield using a mask */
uint32_t pinmask_spread(uint32_t packed, uint32_t mask) uint32_t pinmask_spread_32(uint32_t packed, uint32_t mask)
{ {
uint32_t result = 0; uint32_t result = 0;
uint32_t poke = 1; uint32_t poke = 1;
if(packed == 0) return 0;
for (int i = 0; i<32; i++) { for (int i = 0; i<32; i++) {
if (mask & (1<<i)) { if (mask & 1) {
if (packed & poke) { if (packed & poke) {
result |= 1<<i; result |= 1<<i;
packed ^= poke;
if (packed == 0) break;
} }
poke <<= 1; poke <<= 1;
} }
mask >>= 1;
// if (mask == 0) break;
} }
return result; return result;
} }
/** Pack spread port pins using a mask */ /** Pack spread port pins using a mask */
uint32_t pinmask_pack(uint32_t spread, uint32_t mask) uint32_t pinmask_pack_32(uint32_t spread, uint32_t mask)
{ {
uint32_t result = 0; uint32_t result = 0;
uint32_t poke = 1; uint32_t poke = 1;
for (int i = 0; i<32; i++) { for (int i = 0; i<32; i++) {
if (mask & (1<<i)) { if (mask & 1) {
if (spread & (1<<i)) { if (spread & (1<<i)) {
result |= poke; result |= poke;
spread ^= (1<<i);
if (spread == 0) break;
} }
poke <<= 1; poke <<= 1;
} }
mask >>= 1;
// if (mask == 0) break;
} }
return result; return result;
} }

@ -98,7 +98,12 @@ char * pinmask2str_up(uint32_t pins, char *buffer);
* @param mask - positions of the bits (eg. 0x8803) * @param mask - positions of the bits (eg. 0x8803)
* @return - bits spread to their positions (always counting from right) * @return - bits spread to their positions (always counting from right)
*/ */
uint32_t pinmask_spread(uint32_t packed, uint32_t mask); uint32_t pinmask_spread_32(uint32_t packed, uint32_t mask);
static inline uint16_t pinmask_spread(uint16_t packed, uint16_t mask)
{
return (uint16_t) pinmask_spread_32(packed, mask);
}
/** /**
* Pack spread port pins using a mask * Pack spread port pins using a mask
@ -107,7 +112,12 @@ uint32_t pinmask_spread(uint32_t packed, uint32_t mask);
* @param mask - mask of the bits we want to pack (eg. 0x8803) * @param mask - mask of the bits we want to pack (eg. 0x8803)
* @return - packed bits, right aligned (eg. 0b1110) * @return - packed bits, right aligned (eg. 0b1110)
*/ */
uint32_t pinmask_pack(uint32_t spread, uint32_t mask); uint32_t pinmask_pack_32(uint32_t spread, uint32_t mask);
static inline uint16_t pinmask_pack(uint32_t spread, uint32_t mask)
{
return (uint16_t) pinmask_pack_32(spread, mask);
}
/** /**
* Convert spread port pin number to a packed index using a mask * Convert spread port pin number to a packed index using a mask

@ -9,56 +9,80 @@
#define DOUT_INTERNAL #define DOUT_INTERNAL
#include "_dout_internal.h" #include "_dout_internal.h"
error_t UU_DOut_Write(Unit *unit, uint16_t packed) error_t UU_DOut_Spread(Unit *unit, uint16_t packed, uint16_t *spread_out)
{ {
CHECK_TYPE(unit, &UNIT_DOUT); CHECK_TYPE(unit, &UNIT_DOUT);
*spread_out = UU_DOut_Spread_HS(unit, packed);
return E_SUCCESS;
}
uint16_t UU_DOut_Spread_HS(Unit *unit, uint16_t packed)
{
struct priv *priv = unit->data; struct priv *priv = unit->data;
uint16_t mask = priv->pins; uint16_t mask = priv->pins;
uint16_t spread = pinmask_spread(packed, mask); return pinmask_spread(packed, mask);
}
void UU_DOut_Write_HS(Unit *unit, uint16_t spread)
{
struct priv *priv = unit->data;
uint16_t mask = priv->pins;
uint16_t set = spread; uint16_t set = spread;
uint16_t reset = ((~spread) & mask); uint16_t reset = ((~spread) & mask);
priv->port->BSRR = set | (reset << 16); priv->port->BSRR = set | (reset << 16);
return E_SUCCESS;
} }
error_t UU_DOut_Set(Unit *unit, uint16_t packed) void UU_DOut_Set_HS(Unit *unit, uint16_t spread)
{ {
CHECK_TYPE(unit, &UNIT_DOUT);
struct priv *priv = unit->data; struct priv *priv = unit->data;
uint16_t mask = priv->pins;
uint16_t spread = pinmask_spread(packed, mask);
priv->port->BSRR = spread; priv->port->BSRR = spread;
return E_SUCCESS;
} }
error_t UU_DOut_Clear(Unit *unit, uint16_t packed) void UU_DOut_Clear_HS(Unit *unit, uint16_t spread)
{ {
CHECK_TYPE(unit, &UNIT_DOUT);
struct priv *priv = unit->data; struct priv *priv = unit->data;
uint16_t mask = priv->pins;
uint16_t spread = pinmask_spread(packed, mask);
priv->port->BSRR = (spread<<16); priv->port->BSRR = (spread<<16);
return E_SUCCESS;
} }
error_t UU_DOut_Toggle(Unit *unit, uint16_t packed) void UU_DOut_Toggle_HS(Unit *unit, uint16_t spread)
{ {
CHECK_TYPE(unit, &UNIT_DOUT);
struct priv *priv = unit->data; struct priv *priv = unit->data;
uint16_t mask = priv->pins; uint16_t mask = priv->pins;
uint16_t spread = pinmask_spread(packed, mask);
uint16_t flipped = (uint16_t) (~priv->port->ODR) & mask; uint16_t flipped = (uint16_t) (~priv->port->ODR) & mask;
uint16_t set = flipped & spread; uint16_t set = flipped & spread;
uint16_t reset = ((~flipped) & mask) & spread; uint16_t reset = ((~flipped) & mask) & spread;
priv->port->BSRR = set | (reset<<16); priv->port->BSRR = set | (reset<<16);
}
error_t UU_DOut_Write(Unit *unit, uint16_t packed)
{
CHECK_TYPE(unit, &UNIT_DOUT);
const uint16_t spread = UU_DOut_Spread_HS(unit, packed);
UU_DOut_Write_HS(unit, spread);
return E_SUCCESS;
}
error_t UU_DOut_Set(Unit *unit, uint16_t packed)
{
CHECK_TYPE(unit, &UNIT_DOUT);
const uint16_t spread = UU_DOut_Spread_HS(unit, packed);
UU_DOut_Set_HS(unit, spread);
return E_SUCCESS;
}
error_t UU_DOut_Clear(Unit *unit, uint16_t packed)
{
CHECK_TYPE(unit, &UNIT_DOUT);
const uint16_t spread = UU_DOut_Spread_HS(unit, packed);
UU_DOut_Clear_HS(unit, spread);
return E_SUCCESS;
}
error_t UU_DOut_Toggle(Unit *unit, uint16_t packed)
{
CHECK_TYPE(unit, &UNIT_DOUT);
const uint16_t spread = UU_DOut_Spread_HS(unit, packed);
UU_DOut_Toggle_HS(unit, spread);
return E_SUCCESS; return E_SUCCESS;
} }

@ -56,4 +56,15 @@ error_t UU_DOut_Toggle(Unit *unit, uint16_t packed);
*/ */
error_t UU_DOut_GetPinCount(Unit *unit, uint8_t *count); error_t UU_DOut_GetPinCount(Unit *unit, uint8_t *count);
// --- high-speed variants, without type checking and spreading ---
/** Spread a packed word */
error_t UU_DOut_Spread(Unit *unit, uint16_t packed, uint16_t *spread_out);
uint16_t UU_DOut_Spread_HS(Unit *unit, uint16_t packed);
void UU_DOut_Write_HS(Unit *unit, uint16_t spread);
void UU_DOut_Set_HS(Unit *unit, uint16_t spread);
void UU_DOut_Clear_HS(Unit *unit, uint16_t spread);
void UU_DOut_Toggle_HS(Unit *unit, uint16_t spread);
#endif //U_DOUT_H #endif //U_DOUT_H

Loading…
Cancel
Save