Mario Hüttel
69c00ff3d3
* Add digio module for controlling LEDs, Loudspeaker, and the Digital IOs * General code improvements
121 lines
2.7 KiB
C
121 lines
2.7 KiB
C
#include <digio.h>
|
|
#include <stm32f4xx.h>
|
|
#include <clock-enable-manager.h>
|
|
#include <stm32-gpio-macros.h>
|
|
#include <helper-macros/helper-macros.h>
|
|
|
|
static const uint8_t digio_pins[] = {DIGIO_PINS};
|
|
static const uint8_t digio_default_io[] = {DIGIO_INOUT_DEFAULT};
|
|
static const uint8_t digio_default_altfunc[] = {DIGIO_ALTFUNC_DEFAULT};
|
|
|
|
static void digio_setup_pin_int(uint8_t bit_no, uint8_t in_out, uint8_t alt_func)
|
|
{
|
|
DIGIO_PORT->MODER &= MODER_DELETE(bit_no);
|
|
switch (in_out) {
|
|
case 2:
|
|
DIGIO_PORT->MODER |= ALTFUNC(bit_no);
|
|
SETAF(DIGIO_PORT, bit_no, alt_func);
|
|
break;
|
|
case 1:
|
|
DIGIO_PORT->MODER |= OUTPUT(bit_no);
|
|
break;
|
|
case 0: /* expected fallthrough */
|
|
default:
|
|
break;
|
|
}
|
|
|
|
}
|
|
|
|
void digio_setup_default_all()
|
|
{
|
|
unsigned int i;
|
|
|
|
rcc_manager_enable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(DIGIO_RCC_MASK));
|
|
|
|
for (i = 0; i < COUNT_OF(digio_pins); i++) {
|
|
digio_setup_pin_int(digio_pins[i], digio_default_io[i], digio_default_altfunc[i]);
|
|
if (digio_default_io[i] == 1)
|
|
digio_set(i, 0);
|
|
}
|
|
}
|
|
|
|
void digio_setup_pin(uint8_t num, uint8_t in_out, uint8_t alt_func)
|
|
{
|
|
if (num >= COUNT_OF(digio_pins))
|
|
return;
|
|
digio_setup_pin_int(digio_pins[num], in_out, alt_func);
|
|
}
|
|
|
|
void digio_set(uint8_t num, int val)
|
|
{
|
|
if (num >= COUNT_OF(digio_pins))
|
|
return;
|
|
if (val)
|
|
DIGIO_PORT->ODR |= (1<<digio_pins[num]);
|
|
else
|
|
DIGIO_PORT->ODR &= ~(1<<digio_pins[num]);
|
|
}
|
|
|
|
int digio_get(uint8_t num)
|
|
{
|
|
if (num >= COUNT_OF(digio_pins))
|
|
return -1;
|
|
|
|
if ((DIGIO_PORT->MODER & (0x3<<digio_pins[num])) == OUTPUT(digio_pins[num]))
|
|
return (DIGIO_PORT->ODR & (1<<digio_pins[num]) ? 1 : 0);
|
|
else
|
|
return (DIGIO_PORT->IDR & (1<<digio_pins[num]) ? 1 : 0);
|
|
}
|
|
|
|
static const uint8_t led_pins[] = {LED_PINS};
|
|
|
|
void led_setup()
|
|
{
|
|
unsigned int i;
|
|
|
|
rcc_manager_enable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(LED_RCC_MASK));
|
|
for (i = 0; i < COUNT_OF(led_pins); i++) {
|
|
LED_PORT->MODER &= MODER_DELETE(led_pins[i]);
|
|
LED_PORT->MODER |= OUTPUT(led_pins[i]);
|
|
}
|
|
}
|
|
|
|
void led_set(uint8_t num, int val)
|
|
{
|
|
if (num >= COUNT_OF(led_pins))
|
|
return;
|
|
if (val)
|
|
LED_PORT->ODR |= (1<<led_pins[num]);
|
|
else
|
|
LED_PORT->ODR &= ~(1<<led_pins[num]);
|
|
}
|
|
|
|
int led_get(uint8_t num)
|
|
{
|
|
if (num >= COUNT_OF(led_pins))
|
|
return -1;
|
|
|
|
return ((LED_PORT->ODR & (1<<led_pins[num])) ? 1 : 0);
|
|
}
|
|
|
|
void loudspeaker_setup()
|
|
{
|
|
rcc_manager_enable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(LOUDSPEAKER_RCC_MASK));
|
|
|
|
LOUDSPEAKER_PORT->MODER &= MODER_DELETE(LOUDSPEAKER_PIN);
|
|
LOUDSPEAKER_PORT->MODER |= OUTPUT(LOUDSPEAKER_PIN);
|
|
|
|
loudspeaker_set(0);
|
|
}
|
|
void loudspeaker_set(int val)
|
|
{
|
|
if (val)
|
|
LOUDSPEAKER_PORT->ODR |= (1<<LOUDSPEAKER_PIN);
|
|
else
|
|
LOUDSPEAKER_PORT->ODR &= ~(1<<LOUDSPEAKER_PIN);
|
|
}
|
|
int loudspeaker_get()
|
|
{
|
|
return ((LOUDSPEAKER_PORT->ODR & (1<<LOUDSPEAKER_PIN)) ? 1 : 0);
|
|
}
|