Added func for simple periph clock toggling without the horrible if jungle

sipo
Ondřej Hruška 7 years ago
parent 137619d942
commit e2e4d91cf3
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 12
      framework/resources.c
  2. 2
      framework/unit_base.h
  3. 29
      platform/debug_uart.c
  4. 245
      platform/hw_utils.c
  5. 34
      platform/hw_utils.h
  6. 8
      platform/lock_jumper.c
  7. 8
      platform/platform.c
  8. 8
      platform/status_led.c
  9. 14
      units/digital_in/unit_din.c
  10. 22
      units/digital_out/unit_dout.c
  11. 16
      units/i2c/unit_i2c.c
  12. 6
      units/neopixel/unit_neopixel.c
  13. 28
      units/spi/unit_spi.c
  14. 42
      units/usart/unit_usart.c

@ -5,7 +5,7 @@
#include "platform.h"
#include "unit.h"
#include "resources.h"
#include "pin_utils.h"
#include "hw_utils.h"
#include "unit_registry.h"
static bool rsc_initialized = false;
@ -129,7 +129,7 @@ error_t rsc_claim_gpios(Unit *unit, char port_name, uint16_t pins)
for (int i = 0; i < 16; i++) {
if (pins & (1 << i)) {
Resource rsc = pin2resource(port_name, (uint8_t) i, &suc);
Resource rsc = hw_pin2resource(port_name, (uint8_t) i, &suc);
if (!suc) return E_BAD_CONFIG;
TRY(rsc_claim(unit, rsc));
@ -141,7 +141,7 @@ error_t rsc_claim_gpios(Unit *unit, char port_name, uint16_t pins)
error_t rsc_claim_pin(Unit *unit, char port_name, uint8_t pin)
{
bool suc = true;
Resource rsc = pin2resource(port_name, pin, &suc);
Resource rsc = hw_pin2resource(port_name, pin, &suc);
if (!suc) return E_BAD_CONFIG;
TRY(rsc_claim(unit, rsc));
return E_SUCCESS;
@ -214,7 +214,7 @@ void rsc_teardown(Unit *unit)
assert_param(unit != NULL);
rsc_dbg("Tearing down unit %s", unit->name);
deinit_unit_pins(unit);
hw_deinit_unit_pins(unit);
for (uint32_t i = 0; i < RSCMAP_LEN; i++) {
global_rscmap[i] &= ~unit->resources[i];
@ -256,7 +256,7 @@ void rsc_print_all_available(IniWriter *iw)
if (i%16 == 0) {
// here we print the previous port
if (bitmap != 0) {
iw_string(iw, str_pinmask(bitmap, iwbuffer));
iw_string(iw, pinmask2str(bitmap, iwbuffer));
bitmap = 0;
}
@ -271,7 +271,7 @@ void rsc_print_all_available(IniWriter *iw)
}
// the last one
if (bitmap != 0) {
iw_string(iw, str_pinmask(bitmap, iwbuffer));
iw_string(iw, pinmask2str(bitmap, iwbuffer));
}
iw_newline(iw);
iw_newline(iw);

@ -4,7 +4,7 @@
#include "platform.h"
#include "unit.h"
#include "pin_utils.h"
#include "hw_utils.h"
#include "resources.h"
#include "utils/str_utils.h"
#include "utils/malloc_safe.h"

@ -6,7 +6,7 @@
#include "framework/resources.h"
#include "debug_uart.h"
#include "plat_compat.h"
#include "pin_utils.h"
#include "hw_utils.h"
#if USE_DEBUG_UART
@ -56,11 +56,9 @@
/** Init the submodule. */
void DebugUart_Init(void)
{
bool suc = true;
// Debug UART
assert_param(E_SUCCESS == rsc_claim(&UNIT_SYSTEM, DEBUG_USART_RSC));
assert_param(E_SUCCESS == rsc_claim(&UNIT_SYSTEM, pin2resource(DEBUG_USART_PORT, DEBUG_USART_PIN, &suc)));
assert_param(suc);
assert_param(E_SUCCESS == rsc_claim_pin(&UNIT_SYSTEM, DEBUG_USART_PORT, DEBUG_USART_PIN));
}
/** Init the hardware peripheral - this is called early in the boot process */
@ -68,28 +66,10 @@ void DebugUart_PreInit(void)
{
// configure AF only if platform uses AF numbers
#if !PLAT_NO_AFNUM
configure_gpio_alternate(DEBUG_USART_PORT, DEBUG_USART_PIN, DEBUG_USART_AF);
hw_configure_gpio_af(DEBUG_USART_PORT, DEBUG_USART_PIN, DEBUG_USART_AF);
#endif
if (DEBUG_USART == USART1) {
__HAL_RCC_USART1_CLK_ENABLE();
}
else if (DEBUG_USART == USART2) {
__HAL_RCC_USART2_CLK_ENABLE();
}
else if (DEBUG_USART == USART3) {
__HAL_RCC_USART3_CLK_ENABLE();
}
#ifdef USART4
else if (DEBUG_USART == USART4) {
__HAL_RCC_USART4_CLK_ENABLE();
}
#endif
#ifdef USART5
else if (DEBUG_USART == USART5) {
__HAL_RCC_USART5_CLK_ENABLE();
}
#endif
hw_periph_clock_enable(DEBUG_USART);
// LL_GPIO_SetPinMode(GPIOA, LL_GPIO_PIN_2, LL_GPIO_MODE_ALTERNATE);
// LL_GPIO_SetPinOutputType(GPIOA, LL_GPIO_PIN_2, LL_GPIO_OUTPUT_PUSHPULL);
@ -118,7 +98,6 @@ void debug_write(const char *buf, uint16_t len)
ssize_t _write_r(struct _reent *rptr, int fd, const void *buf, size_t len)
{
trap("Use of newlib printf");
return len;
}
#else

@ -3,7 +3,7 @@
//
#include <utils/avrlibc.h>
#include "pin_utils.h"
#include "hw_utils.h"
#include "macro.h"
#define PINS_COUNT 16
@ -46,7 +46,7 @@ static GPIO_TypeDef * const port_periphs[] = {
COMPILER_ASSERT(PORTS_COUNT == ELEMENTS_IN_ARRAY(port_periphs));
/** Convert pin number to LL bitfield */
uint32_t pin2ll(uint8_t pin_number, bool *suc)
uint32_t hw_pin2ll(uint8_t pin_number, bool *suc)
{
assert_param(suc != NULL);
@ -60,7 +60,7 @@ uint32_t pin2ll(uint8_t pin_number, bool *suc)
}
/** Convert port name (A,B,C...) to peripheral struct pointer */
GPIO_TypeDef *port2periph(char port_name, bool *suc)
GPIO_TypeDef *hw_port2periph(char port_name, bool *suc)
{
assert_param(suc != NULL);
@ -75,7 +75,7 @@ GPIO_TypeDef *port2periph(char port_name, bool *suc)
}
/** Convert a pin to resource handle */
Resource pin2resource(char port_name, uint8_t pin_number, bool *suc)
Resource hw_pin2resource(char port_name, uint8_t pin_number, bool *suc)
{
assert_param(suc != NULL);
@ -116,7 +116,7 @@ bool parse_pin(const char *value, char *targetName, uint8_t *targetNumber)
}
/** Parse port name */
bool parse_port(const char *value, char *targetName)
bool parse_port_name(const char *value, char *targetName)
{
*targetName = (uint8_t) value[0];
if (!(*targetName >= 'A' && *targetName < 'A' + PORTS_COUNT)) return false;
@ -177,7 +177,7 @@ uint16_t parse_pinmask(const char *value, bool *suc)
}
/** Convert a pin bitmask to the ASCII format understood by str_parse_pinmask() */
char * str_pinmask(uint16_t pins, char *buffer)
char * pinmask2str(uint16_t pins, char *buffer)
{
char *b = buffer;
uint32_t start = 0;
@ -230,7 +230,7 @@ char * str_pinmask(uint16_t pins, char *buffer)
}
/** Spread packed port pins using a mask */
uint16_t port_spread(uint16_t packed, uint16_t mask)
uint16_t pinmask_spread(uint16_t packed, uint16_t mask)
{
uint16_t result = 0;
uint16_t poke = 1;
@ -246,7 +246,7 @@ uint16_t port_spread(uint16_t packed, uint16_t mask)
}
/** Pack spread port pins using a mask */
uint16_t port_pack(uint16_t spread, uint16_t mask)
uint16_t pinmask_pack(uint16_t spread, uint16_t mask)
{
uint16_t result = 0;
uint16_t poke = 1;
@ -262,7 +262,7 @@ uint16_t port_pack(uint16_t spread, uint16_t mask)
}
/** Configure unit pins as analog (part of unit teardown) */
void deinit_unit_pins(Unit *unit)
void hw_deinit_unit_pins(Unit *unit)
{
for (uint32_t rsc = R_PA0; rsc <= R_PF15; rsc++) {
if (RSC_IS_HELD(unit->resources, rsc)) {
@ -275,11 +275,15 @@ void deinit_unit_pins(Unit *unit)
}
/** Configure a pin to alternate function */
error_t configure_gpio_alternate(char port_name, uint8_t pin_num, uint32_t ll_af)
error_t hw_configure_gpio_af(char port_name, uint8_t pin_num, uint32_t ll_af)
{
#if PLAT_NO_AFNUM
trap("Illegal call to hw_configure_gpio_af() on this platform");
#else
bool suc = true;
GPIO_TypeDef *port = port2periph(port_name, &suc);
uint32_t ll_pin = pin2ll(pin_num, &suc);
GPIO_TypeDef *port = hw_port2periph(port_name, &suc);
uint32_t ll_pin = hw_pin2ll(pin_num, &suc);
if (!suc) return E_BAD_CONFIG;
if (pin_num < 8)
@ -289,19 +293,21 @@ error_t configure_gpio_alternate(char port_name, uint8_t pin_num, uint32_t ll_af
LL_GPIO_SetPinMode(port, ll_pin, LL_GPIO_MODE_ALTERNATE);
#endif
return E_SUCCESS;
}
/** Configure pins using sparse map */
error_t configure_sparse_pins(char port_name, uint16_t mask, GPIO_TypeDef **port_dest, uint32_t ll_mode, uint32_t ll_otype)
error_t hw_configure_sparse_pins(char port_name, uint16_t mask, GPIO_TypeDef **port_dest,
uint32_t ll_mode, uint32_t ll_otype)
{
bool suc = true;
GPIO_TypeDef *port = port2periph(port_name, &suc);
GPIO_TypeDef *port = hw_port2periph(port_name, &suc);
if (!suc) return E_BAD_CONFIG;
for (int i = 0; i < 16; i++) {
if (mask & (1<<i)) {
uint32_t ll_pin = pin2ll((uint8_t) i, &suc);
uint32_t ll_pin = hw_pin2ll((uint8_t) i, &suc);
LL_GPIO_SetPinMode(port, ll_pin, ll_mode);
LL_GPIO_SetPinOutputType(port, ll_pin, ll_otype);
LL_GPIO_SetPinSpeed(port, ll_pin, LL_GPIO_SPEED_FREQ_HIGH);
@ -314,3 +320,212 @@ error_t configure_sparse_pins(char port_name, uint16_t mask, GPIO_TypeDef **port
return E_SUCCESS;
}
void hw_periph_clock_enable(void *periph)
{
// GPIOs are enabled by default on start-up
// --- USART ---
if (periph == USART1) __HAL_RCC_USART1_CLK_ENABLE();
else if (periph == USART2) __HAL_RCC_USART2_CLK_ENABLE();
#ifdef USART3
else if (periph == USART3) __HAL_RCC_USART3_CLK_ENABLE();
#endif
#ifdef USART4
else if (periph == USART4) __HAL_RCC_USART4_CLK_ENABLE();
#endif
#ifdef USART5
else if (periph == USART5) __HAL_RCC_USART5_CLK_ENABLE();
#endif
// --- SPI ---
else if (periph == SPI1) __HAL_RCC_SPI1_CLK_ENABLE();
#ifdef SPI2
else if (periph == SPI2) __HAL_RCC_SPI2_CLK_ENABLE();
#endif
#ifdef SPI3
else if (periph == SPI3) __HAL_RCC_SPI3_CLK_ENABLE();
#endif
// --- I2C ---
else if (periph == I2C1) __HAL_RCC_I2C1_CLK_ENABLE();
else if (periph == I2C2) __HAL_RCC_I2C2_CLK_ENABLE();
#ifdef I2C3
else if (periph == I2C3) __HAL_RCC_I2C3_CLK_ENABLE();
#endif
// --- DMA ---
else if (periph == DMA1) __HAL_RCC_DMA1_CLK_ENABLE();
#ifdef DMA2
else if (periph == DMA2) __HAL_RCC_DMA2_CLK_ENABLE();
#endif
// --- TIM ---
else if (periph == TIM1) __HAL_RCC_TIM1_CLK_ENABLE();
else if (periph == TIM2) __HAL_RCC_TIM2_CLK_ENABLE();
else if (periph == TIM3) __HAL_RCC_TIM3_CLK_ENABLE();
#ifdef TIM4
else if (periph == TIM4) __HAL_RCC_TIM4_CLK_ENABLE();
#endif
#ifdef TIM5
else if (periph == TIM5) __HAL_RCC_TIM5_CLK_ENABLE();
#endif
#ifdef TIM6
else if (periph == TIM6) __HAL_RCC_TIM7_CLK_ENABLE();
#endif
#ifdef TIM7
else if (periph == TIM7) __HAL_RCC_TIM7_CLK_ENABLE();
#endif
#ifdef TIM8
else if (periph == TIM8) __HAL_RCC_TIM8_CLK_ENABLE();
#endif
#ifdef TIM9
else if (periph == TIM9) __HAL_RCC_TIM9_CLK_ENABLE();
#endif
#ifdef TIM11
else if (periph == TIM11) __HAL_RCC_TIM11_CLK_ENABLE();
#endif
#ifdef TIM12
else if (periph == TIM12) __HAL_RCC_TIM12_CLK_ENABLE();
#endif
#ifdef TIM13
else if (periph == TIM13) __HAL_RCC_TIM13_CLK_ENABLE();
#endif
#ifdef TIM14
else if (periph == TIM14) __HAL_RCC_TIM14_CLK_ENABLE();
#endif
#ifdef TIM15
else if (periph == TIM15) __HAL_RCC_TIM15_CLK_ENABLE();
#endif
#ifdef TIM16
else if (periph == TIM16) __HAL_RCC_TIM15_CLK_ENABLE();
#endif
#ifdef TIM17
else if (periph == TIM17) __HAL_RCC_TIM17_CLK_ENABLE();
#endif
// --- ADC ---
#ifdef ADC1
else if (periph == ADC1) __HAL_RCC_ADC1_CLK_ENABLE();
#endif
#ifdef ADC2
else if (periph == ADC2) __HAL_RCC_ADC2_CLK_ENABLE();
#endif
// --- DAC ---
#ifdef DAC1
else if (periph == DAC1) __HAL_RCC_DAC1_CLK_ENABLE();
#endif
#ifdef DAC2
else if (periph == DAC2) __HAL_RCC_DAC2_CLK_ENABLE();
#endif
else {
dbg("Periph 0x%p missing in hw clock enable func", periph);
trap("BUG");
}
}
void hw_periph_clock_disable(void *periph)
{
// GPIOs are enabled by default on start-up
// --- USART ---
if (periph == USART1) __HAL_RCC_USART1_CLK_DISABLE();
else if (periph == USART2) __HAL_RCC_USART2_CLK_DISABLE();
#ifdef USART3
else if (periph == USART3) __HAL_RCC_USART3_CLK_DISABLE();
#endif
#ifdef USART4
else if (periph == USART4) __HAL_RCC_USART4_CLK_DISABLE();
#endif
#ifdef USART5
else if (periph == USART5) __HAL_RCC_USART5_CLK_DISABLE();
#endif
// --- SPI ---
else if (periph == SPI1) __HAL_RCC_SPI1_CLK_DISABLE();
#ifdef SPI2
else if (periph == SPI2) __HAL_RCC_SPI2_CLK_DISABLE();
#endif
#ifdef SPI3
else if (periph == SPI3) __HAL_RCC_SPI3_CLK_DISABLE();
#endif
// --- I2C ---
else if (periph == I2C1) __HAL_RCC_I2C1_CLK_DISABLE();
else if (periph == I2C2) __HAL_RCC_I2C2_CLK_DISABLE();
#ifdef I2C3
else if (periph == I2C3) __HAL_RCC_I2C3_CLK_DISABLE();
#endif
// --- DMA ---
else if (periph == DMA1) __HAL_RCC_DMA1_CLK_DISABLE();
#ifdef DMA2
else if (periph == DMA2) __HAL_RCC_DMA2_CLK_DISABLE();
#endif
// --- TIM ---
else if (periph == TIM1) __HAL_RCC_TIM1_CLK_DISABLE();
else if (periph == TIM2) __HAL_RCC_TIM2_CLK_DISABLE();
else if (periph == TIM3) __HAL_RCC_TIM3_CLK_DISABLE();
#ifdef TIM4
else if (periph == TIM4) __HAL_RCC_TIM4_CLK_DISABLE();
#endif
#ifdef TIM5
else if (periph == TIM5) __HAL_RCC_TIM5_CLK_DISABLE();
#endif
#ifdef TIM6
else if (periph == TIM6) __HAL_RCC_TIM7_CLK_DISABLE();
#endif
#ifdef TIM7
else if (periph == TIM7) __HAL_RCC_TIM7_CLK_DISABLE();
#endif
#ifdef TIM8
else if (periph == TIM8) __HAL_RCC_TIM8_CLK_DISABLE();
#endif
#ifdef TIM9
else if (periph == TIM9) __HAL_RCC_TIM9_CLK_DISABLE();
#endif
#ifdef TIM11
else if (periph == TIM11) __HAL_RCC_TIM11_CLK_DISABLE();
#endif
#ifdef TIM12
else if (periph == TIM12) __HAL_RCC_TIM12_CLK_DISABLE();
#endif
#ifdef TIM13
else if (periph == TIM13) __HAL_RCC_TIM13_CLK_DISABLE();
#endif
#ifdef TIM14
else if (periph == TIM14) __HAL_RCC_TIM14_CLK_DISABLE();
#endif
#ifdef TIM15
else if (periph == TIM15) __HAL_RCC_TIM15_CLK_DISABLE();
#endif
#ifdef TIM16
else if (periph == TIM16) __HAL_RCC_TIM15_CLK_DISABLE();
#endif
#ifdef TIM17
else if (periph == TIM17) __HAL_RCC_TIM17_CLK_DISABLE();
#endif
// --- ADC ---
#ifdef ADC1
else if (periph == ADC1) __HAL_RCC_ADC1_CLK_DISABLE();
#endif
#ifdef ADC2
else if (periph == ADC2) __HAL_RCC_ADC2_CLK_DISABLE();
#endif
// --- DAC ---
#ifdef DAC1
else if (periph == DAC1) __HAL_RCC_DAC1_CLK_DISABLE();
#endif
#ifdef DAC2
else if (periph == DAC2) __HAL_RCC_DAC2_CLK_DISABLE();
#endif
else {
dbg("Periph 0x%p missing in hw clock disable func", periph);
trap("BUG");
}
}

@ -17,7 +17,7 @@
* @param suc - set to false on failure, left unchanged on success.
* @return LL_GPIO_PIN_x
*/
uint32_t pin2ll(uint8_t pin_number, bool *suc);
uint32_t hw_pin2ll(uint8_t pin_number, bool *suc);
/**
* Convert pin name and number to a resource enum
@ -27,7 +27,7 @@ uint32_t pin2ll(uint8_t pin_number, bool *suc);
* @param suc - set to false on failure, left unchanged on success
* @return the resource, or R_NONE
*/
Resource pin2resource(char port_name, uint8_t pin_number, bool *suc);
Resource hw_pin2resource(char port_name, uint8_t pin_number, bool *suc);
/**
* Convert port name to peripheral instance
@ -36,7 +36,7 @@ Resource pin2resource(char port_name, uint8_t pin_number, bool *suc);
* @param suc - set to false on failure, left unchanged on success.
* @return instance
*/
GPIO_TypeDef *port2periph(char port_name, bool *suc);
GPIO_TypeDef *hw_port2periph(char port_name, bool *suc);
/**
* Parse a pin name (e.g. PA0 or A0) to port name and pin number
@ -55,7 +55,7 @@ bool parse_pin(const char *str, char *targetName, uint8_t *targetNumber);
* @param targetName - output: port name (one character)
* @return success
*/
bool parse_port(const char *value, char *targetName);
bool parse_port_name(const char *value, char *targetName);
/**
* Parse a list of pin numbers with ranges and commands/semicolons to a bitmask.
@ -76,7 +76,7 @@ uint16_t parse_pinmask(const char *value, bool *suc);
* @param buffer - output string buffer
* @return the output buffer
*/
char * str_pinmask(uint16_t pins, char *buffer);
char * pinmask2str(uint16_t pins, char *buffer);
/**
* Spread packed port pins using a mask
@ -85,7 +85,7 @@ char * str_pinmask(uint16_t pins, char *buffer);
* @param mask - positions of the bits (eg. 0x8803)
* @return - bits spread to their positions (always counting from right)
*/
uint16_t port_spread(uint16_t packed, uint16_t mask);
uint16_t pinmask_spread(uint16_t packed, uint16_t mask);
/**
* Pack spread port pins using a mask
@ -94,7 +94,7 @@ uint16_t port_spread(uint16_t packed, uint16_t mask);
* @param mask - mask of the bits we want to pack (eg. 0x8803)
* @return - packed bits, right aligned (eg. 0b1110)
*/
uint16_t port_pack(uint16_t spread, uint16_t mask);
uint16_t pinmask_pack(uint16_t spread, uint16_t mask);
/**
* Set all GPIO resources held by unit to analog.
@ -102,7 +102,7 @@ uint16_t port_pack(uint16_t spread, uint16_t mask);
*
* @param unit - holding unit
*/
void deinit_unit_pins(Unit *unit);
void hw_deinit_unit_pins(Unit *unit);
/**
* Configure a GPIO pin to alternate function.
@ -112,7 +112,7 @@ void deinit_unit_pins(Unit *unit);
* @param ll_af - LL alternate function constant
* @return success
*/
error_t configure_gpio_alternate(char port_name, uint8_t pin_num, uint32_t ll_af);
error_t hw_configure_gpio_af(char port_name, uint8_t pin_num, uint32_t ll_af);
/**
* Configure multiple pins using the bitmap pattern
@ -124,7 +124,9 @@ error_t configure_gpio_alternate(char port_name, uint8_t pin_num, uint32_t ll_af
* @param ll_otype - LL output type (push/pull, opendrain)
* @return success
*/
error_t configure_sparse_pins(char port_name, uint16_t mask, GPIO_TypeDef **port_dest, uint32_t ll_mode, uint32_t ll_otype);
error_t hw_configure_sparse_pins(char port_name,
uint16_t mask, GPIO_TypeDef **port_dest,
uint32_t ll_mode, uint32_t ll_otype);
/** Helper struct for defining alternate mappings */
struct PinAF {
@ -133,4 +135,16 @@ struct PinAF {
uint8_t af;
};
/**
* Enable a peripheral clock
* @param periph - any peripheral
*/
void hw_periph_clock_enable(void *periph);
/**
* Disable a peripheral clock
* @param periph - any peripheral
*/
void hw_periph_clock_disable(void *periph);
#endif //GEX_PIN_UTILS_H

@ -7,7 +7,7 @@
#include "framework/settings.h"
#include "framework/resources.h"
#include "framework/system_settings.h"
#include "pin_utils.h"
#include "hw_utils.h"
#include "lock_jumper.h"
#include "status_led.h"
@ -27,14 +27,14 @@ void LockJumper_Init(void)
bool suc = true;
// Resolve and claim resource
Resource rsc = pin2resource(LOCK_JUMPER_PORT, LOCK_JUMPER_PIN, &suc);
Resource rsc = hw_pin2resource(LOCK_JUMPER_PORT, LOCK_JUMPER_PIN, &suc);
assert_param(suc);
assert_param(E_SUCCESS == rsc_claim(&UNIT_SYSTEM, rsc));
// Resolve pin
lock_periph = port2periph(LOCK_JUMPER_PORT, &suc);
lock_llpin = pin2ll(LOCK_JUMPER_PIN, &suc);
lock_periph = hw_port2periph(LOCK_JUMPER_PORT, &suc);
lock_llpin = hw_pin2ll(LOCK_JUMPER_PIN, &suc);
assert_param(suc);
// Configure for input

@ -16,6 +16,7 @@
#include "units/test/unit_test.h"
#include "units/usart/unit_usart.h"
#include "units/spi/unit_spi.h"
#include "hw_utils.h"
void plat_init_resources(void)
{
@ -25,11 +26,12 @@ void plat_init_resources(void)
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
#ifdef GPIOE
__HAL_RCC_GPIOE_CLK_ENABLE();
#endif
#ifdef GPIOF
__HAL_RCC_GPIOF_CLK_ENABLE();
hw_periph_clock_enable(DMA1);
#ifdef DMA2
hw_periph_clock_enable(DMA2);
#endif
// --- Common unit drivers ---

@ -5,7 +5,7 @@
#include "platform.h"
#include "framework/resources.h"
#include "status_led.h"
#include "pin_utils.h"
#include "hw_utils.h"
static GPIO_TypeDef *led_periph;
static uint32_t led_llpin;
@ -26,8 +26,8 @@ void Indicator_PreInit(void)
{
bool suc = true;
// Resolve pin
led_periph = port2periph(STATUS_LED_PORT, &suc);
led_llpin = pin2ll(STATUS_LED_PIN, &suc);
led_periph = hw_port2periph(STATUS_LED_PORT, &suc);
led_llpin = hw_pin2ll(STATUS_LED_PIN, &suc);
// Configure for output
LL_GPIO_SetPinMode(led_periph, led_llpin, LL_GPIO_MODE_OUTPUT);
@ -53,7 +53,7 @@ void Indicator_Init(void)
bool suc = true;
// Resolve and claim resource
Resource rsc = pin2resource(STATUS_LED_PORT, STATUS_LED_PIN, &suc);
Resource rsc = hw_pin2resource(STATUS_LED_PORT, STATUS_LED_PIN, &suc);
assert_param(suc);
assert_param(E_SUCCESS == rsc_claim(&UNIT_SYSTEM, rsc));

@ -57,7 +57,7 @@ static error_t DI_loadIni(Unit *unit, const char *key, const char *value)
struct priv *priv = unit->data;
if (streq(key, "port")) {
suc = parse_port(value, &priv->port_name);
suc = parse_port_name(value, &priv->port_name);
}
else if (streq(key, "pins")) {
priv->pins = parse_pinmask(value, &suc);
@ -85,13 +85,13 @@ static void DI_writeIni(Unit *unit, IniWriter *iw)
iw_entry(iw, "port", "%c", priv->port_name);
iw_comment(iw, "Pins (comma separated, supports ranges)");
iw_entry(iw, "pins", "%s", str_pinmask(priv->pins, unit_tmp512));
iw_entry(iw, "pins", "%s", pinmask2str(priv->pins, unit_tmp512));
iw_comment(iw, "Pins with pull-up");
iw_entry(iw, "pull-up", "%s", str_pinmask(priv->pullup, unit_tmp512));
iw_entry(iw, "pull-up", "%s", pinmask2str(priv->pullup, unit_tmp512));
iw_comment(iw, "Pins with pull-down");
iw_entry(iw, "pull-down", "%s", str_pinmask(priv->pulldown, unit_tmp512));
iw_entry(iw, "pull-down", "%s", pinmask2str(priv->pulldown, unit_tmp512));
#if PLAT_NO_FLOATING_INPUTS
iw_comment(iw, "NOTE: Pins use pull-up by default.\r\n");
@ -125,7 +125,7 @@ static error_t DI_init(Unit *unit)
priv->pullup &= priv->pins;
// --- Parse config ---
priv->port = port2periph(priv->port_name, &suc);
priv->port = hw_port2periph(priv->port_name, &suc);
if (!suc) return E_BAD_CONFIG;
// Claim all needed pins
@ -134,7 +134,7 @@ static error_t DI_init(Unit *unit)
uint16_t mask = 1;
for (int i = 0; i < 16; i++, mask <<= 1) {
if (priv->pins & mask) {
uint32_t ll_pin = pin2ll((uint8_t) i, &suc);
uint32_t ll_pin = hw_pin2ll((uint8_t) i, &suc);
// --- Init hardware ---
LL_GPIO_SetPinMode(priv->port, ll_pin, LL_GPIO_MODE_INPUT);
@ -176,7 +176,7 @@ error_t UU_DI_Read(Unit *unit, uint16_t *packed)
{
CHECK_TYPE(unit, &UNIT_DIN);
struct priv *priv = unit->data;
*packed = port_pack((uint16_t) priv->port->IDR, priv->pins);
*packed = pinmask_pack((uint16_t) priv->port->IDR, priv->pins);
return E_SUCCESS;
}

@ -54,7 +54,7 @@ static error_t DO_loadIni(Unit *unit, const char *key, const char *value)
struct priv *priv = unit->data;
if (streq(key, "port")) {
suc = parse_port(value, &priv->port_name);
suc = parse_port_name(value, &priv->port_name);
}
else if (streq(key, "pins")) {
priv->pins = parse_pinmask(value, &suc);
@ -82,13 +82,13 @@ static void DO_writeIni(Unit *unit, IniWriter *iw)
iw_entry(iw, "port", "%c", priv->port_name);
iw_comment(iw, "Pins (comma separated, supports ranges)");
iw_entry(iw, "pins", "%s", str_pinmask(priv->pins, unit_tmp512));
iw_entry(iw, "pins", "%s", pinmask2str(priv->pins, unit_tmp512));
iw_comment(iw, "Initially high pins");
iw_entry(iw, "initial", "%s", str_pinmask(priv->initial, unit_tmp512));
iw_entry(iw, "initial", "%s", pinmask2str(priv->initial, unit_tmp512));
iw_comment(iw, "Open-drain pins");
iw_entry(iw, "open-drain", "%s", str_pinmask(priv->open_drain, unit_tmp512));
iw_entry(iw, "open-drain", "%s", pinmask2str(priv->open_drain, unit_tmp512));
}
// ------------------------------------------------------------------------
@ -118,7 +118,7 @@ static error_t DO_init(Unit *unit)
priv->open_drain &= priv->pins;
// --- Parse config ---
priv->port = port2periph(priv->port_name, &suc);
priv->port = hw_port2periph(priv->port_name, &suc);
if (!suc) return E_BAD_CONFIG;
// Claim all needed pins
@ -126,7 +126,7 @@ static error_t DO_init(Unit *unit)
for (int i = 0; i < 16; i++) {
if (priv->pins & (1 << i)) {
uint32_t ll_pin = pin2ll((uint8_t) i, &suc);
uint32_t ll_pin = hw_pin2ll((uint8_t) i, &suc);
// --- Init hardware ---
LL_GPIO_SetPinMode(priv->port, ll_pin, LL_GPIO_MODE_OUTPUT);
@ -163,7 +163,7 @@ error_t UU_DO_Write(Unit *unit, uint16_t packed)
struct priv *priv = unit->data;
uint16_t mask = priv->pins;
uint16_t spread = port_spread(packed, mask);
uint16_t spread = pinmask_spread(packed, mask);
uint16_t set = spread;
uint16_t reset = ((~spread) & mask);
@ -177,7 +177,7 @@ error_t UU_DO_Set(Unit *unit, uint16_t packed)
struct priv *priv = unit->data;
uint16_t mask = priv->pins;
uint16_t spread = port_spread(packed, mask);
uint16_t spread = pinmask_spread(packed, mask);
priv->port->BSRR = spread;
return E_SUCCESS;
@ -189,7 +189,7 @@ error_t UU_DO_Clear(Unit *unit, uint16_t packed)
struct priv *priv = unit->data;
uint16_t mask = priv->pins;
uint16_t spread = port_spread(packed, mask);
uint16_t spread = pinmask_spread(packed, mask);
priv->port->BSRR = (spread<<16);
return E_SUCCESS;
@ -201,7 +201,7 @@ error_t UU_DO_Toggle(Unit *unit, uint16_t packed)
struct priv *priv = unit->data;
uint16_t mask = priv->pins;
uint16_t spread = port_spread(packed, mask);
uint16_t spread = pinmask_spread(packed, mask);
uint16_t flipped = (uint16_t) (~priv->port->ODR) & mask;
uint16_t set = flipped & spread;
@ -215,7 +215,7 @@ error_t UU_DO_GetPinCount(Unit *unit, uint8_t *count)
CHECK_TYPE(unit, &UNIT_DOUT);
struct priv *priv = unit->data;
uint32_t packed = port_pack(0xFFFF, priv->pins);
uint32_t packed = pinmask_pack(0xFFFF, priv->pins);
*count = (uint8_t)(32 - __CLZ(packed));
return E_SUCCESS;
}

@ -235,14 +235,10 @@ static error_t UI2C_init(Unit *unit)
TRY(rsc_claim_pin(unit, portname, pin_sda));
TRY(rsc_claim_pin(unit, portname, pin_scl));
configure_gpio_alternate(portname, pin_sda, af_i2c);
configure_gpio_alternate(portname, pin_scl, af_i2c);
hw_configure_gpio_af(portname, pin_sda, af_i2c);
hw_configure_gpio_af(portname, pin_scl, af_i2c);
if (priv->periph_num == 1) {
__HAL_RCC_I2C1_CLK_ENABLE();
} else {
__HAL_RCC_I2C2_CLK_ENABLE();
}
hw_periph_clock_enable(priv->periph);
/* Disable the selected I2Cx Peripheral */
LL_I2C_Disable(priv->periph);
@ -270,11 +266,7 @@ static void UI2C_deInit(Unit *unit)
assert_param(priv->periph);
LL_I2C_DeInit(priv->periph);
if (priv->periph_num == 1) {
__HAL_RCC_I2C1_CLK_DISABLE();
} else {
__HAL_RCC_I2C2_CLK_DISABLE();
}
hw_periph_clock_disable(priv->periph);
}
// Release all resources

@ -97,9 +97,9 @@ static error_t Npx_init(Unit *unit)
struct priv *priv = unit->data;
// --- Parse config ---
priv->ll_pin = pin2ll(priv->pin_number, &suc);
priv->port = port2periph(priv->port_name, &suc);
Resource rsc = pin2resource(priv->port_name, priv->pin_number, &suc);
priv->ll_pin = hw_pin2ll(priv->pin_number, &suc);
priv->port = hw_port2periph(priv->port_name, &suc);
Resource rsc = hw_pin2resource(priv->port_name, priv->pin_number, &suc);
if (!suc) return E_BAD_CONFIG;
// --- Claim resources ---

@ -101,7 +101,7 @@ static error_t USPI_loadIni(Unit *unit, const char *key, const char *value)
priv->lsb_first = (bool)str_parse_2(value, "MSB", 0, "LSB", 1, &suc);
}
else if (streq(key, "port")) {
suc = parse_port(value, &priv->ssn_port_name);
suc = parse_port_name(value, &priv->ssn_port_name);
}
else if (streq(key, "pins")) {
priv->ssn_pins = parse_pinmask(value, &suc);
@ -161,7 +161,7 @@ static void USPI_writeIni(Unit *unit, IniWriter *iw)
iw_entry(iw, "port", "%c", priv->ssn_port_name);
iw_comment(iw, "SS pins (comma separated, supports ranges)");
iw_entry(iw, "pins", "%s", str_pinmask(priv->ssn_pins, unit_tmp512));
iw_entry(iw, "pins", "%s", pinmask2str(priv->ssn_pins, unit_tmp512));
}
// ------------------------------------------------------------------------
@ -291,25 +291,21 @@ static error_t USPI_init(Unit *unit)
TRY(rsc_claim_pin(unit, spi_portname, pin_miso));
TRY(rsc_claim_pin(unit, spi_portname, pin_sck));
configure_gpio_alternate(spi_portname, pin_mosi, af_spi);
configure_gpio_alternate(spi_portname, pin_miso, af_spi);
configure_gpio_alternate(spi_portname, pin_sck, af_spi);
hw_configure_gpio_af(spi_portname, pin_mosi, af_spi);
hw_configure_gpio_af(spi_portname, pin_miso, af_spi);
hw_configure_gpio_af(spi_portname, pin_sck, af_spi);
// configure SSN GPIOs
{
// Claim all needed pins
TRY(rsc_claim_gpios(unit, priv->ssn_port_name, priv->ssn_pins));
TRY(configure_sparse_pins(priv->ssn_port_name, priv->ssn_pins, &priv->ssn_port,
TRY(hw_configure_sparse_pins(priv->ssn_port_name, priv->ssn_pins, &priv->ssn_port,
LL_GPIO_MODE_OUTPUT, LL_GPIO_OUTPUT_PUSHPULL));
// Set the initial state - all high
priv->ssn_port->BSRR = priv->ssn_pins;
}
if (priv->periph_num == 1) {
__HAL_RCC_SPI1_CLK_ENABLE();
} else {
__HAL_RCC_SPI2_CLK_ENABLE();
}
hw_periph_clock_enable(priv->periph);
// Configure SPI - must be configured under reset
LL_SPI_Disable(priv->periph);
@ -347,11 +343,7 @@ static void USPI_deInit(Unit *unit)
assert_param(priv->periph);
LL_SPI_DeInit(priv->periph);
if (priv->periph_num == 1) {
__HAL_RCC_SPI1_CLK_DISABLE();
} else {
__HAL_RCC_SPI2_CLK_DISABLE();
}
hw_periph_clock_disable(priv->periph);
}
// Release all resources
@ -430,7 +422,7 @@ error_t UU_SPI_Multicast(Unit *unit, uint16_t slaves,
const uint8_t *request, uint32_t req_len)
{
struct priv *priv= unit->data;
uint16_t mask = port_spread(slaves, priv->ssn_pins);
uint16_t mask = pinmask_spread(slaves, priv->ssn_pins);
priv->ssn_port->BRR = mask;
{
TRY(xfer_do(priv, request, NULL, req_len, 0, 0));
@ -446,7 +438,7 @@ error_t UU_SPI_Write(Unit *unit, uint8_t slave_num,
{
struct priv *priv= unit->data;
uint16_t mask = port_spread((uint16_t) (1 << slave_num), priv->ssn_pins);
uint16_t mask = pinmask_spread((uint16_t) (1 << slave_num), priv->ssn_pins);
priv->ssn_port->BRR = mask;
{
TRY(xfer_do(priv, request, response, req_len, resp_len, resp_skip));

@ -459,7 +459,7 @@ static error_t UUSART_ConfigurePins(Unit *unit)
if (pins_wanted[i]) {
if (mappings[i].port == 0) return E_BAD_CONFIG;
TRY(rsc_claim_pin(unit, mappings[i].port, mappings[i].pin));
configure_gpio_alternate(mappings[i].port, mappings[i].pin, mappings[i].af);
hw_configure_gpio_af(mappings[i].port, mappings[i].pin, mappings[i].af);
}
}
@ -488,25 +488,7 @@ static error_t UUSART_init(Unit *unit)
// --- Configure the peripheral ---
// Enable clock for the peripheral used
if (priv->periph_num == 1) {
__HAL_RCC_USART1_CLK_ENABLE();
}
else if (priv->periph_num == 2) {
__HAL_RCC_USART2_CLK_ENABLE();
}
else if (priv->periph_num == 3) {
__HAL_RCC_USART3_CLK_ENABLE();
}
#ifdef USART4
else if (priv->periph_num == 4) {
__HAL_RCC_USART4_CLK_ENABLE();
}
#endif
#ifdef USART5
else if (priv->periph_num == 5) {
__HAL_RCC_USART5_CLK_ENABLE();
}
#endif
hw_periph_clock_enable(priv->periph);
LL_USART_Disable(priv->periph);
{
@ -592,25 +574,7 @@ static void UUSART_deInit(Unit *unit)
LL_USART_DeInit(priv->periph);
// Disable clock
if (priv->periph_num == 1) {
__HAL_RCC_USART1_CLK_DISABLE();
}
else if (priv->periph_num == 2) {
__HAL_RCC_USART2_CLK_DISABLE();
}
else if (priv->periph_num == 3) {
__HAL_RCC_USART3_CLK_DISABLE();
}
#ifdef USART4
else if (priv->periph_num == 4) {
__HAL_RCC_USART4_CLK_DISABLE();
}
#endif
#ifdef USART5
else if (priv->periph_num == 5) {
__HAL_RCC_USART5_CLK_DISABLE();
}
#endif
hw_periph_clock_disable(priv->periph);
}
// Release all resources

Loading…
Cancel
Save