spark_abacus/global.c

138 lines
4.2 KiB
C
Raw Normal View History

/* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/** global definitions and methods (code)
* @file global.c
* @author King Kévin <kingkevin@cuvoodoo.info>
* @date 2016
*/
/* standard libraries */
#include <stdint.h> // standard integer types
#include <stdlib.h> // general utilities
/* STM32 (including CM3) libraries */
#include <libopencm3/stm32/rcc.h> // real-time control clock library
#include <libopencm3/stm32/gpio.h> // general purpose input output library
#include <libopencm3/stm32/timer.h> // timer library
#include <libopencm3/cm3/nvic.h> // interrupt handler
#include <libopencm3/stm32/exti.h> // external interrupt defines
#include "global.h" // common methods
2016-10-03 17:38:48 +02:00
#include "string.h" // memory utilities
volatile bool button_flag = false;
2016-10-03 17:38:48 +02:00
void * memmem(const void *l, size_t l_len, const void *s, size_t s_len)
{
register char *cur, *last;
const char *cl = (const char *)l;
const char *cs = (const char *)s;
/* we need something to compare */
if (l_len == 0 || s_len == 0)
return NULL;
/* "s" must be smaller or equal to "l" */
if (l_len < s_len)
return NULL;
/* special case where s_len == 1 */
if (s_len == 1)
return memchr(l, (int)*cs, l_len);
/* the last position where its possible to find "s" in "l" */
last = (char *)cl + l_len - s_len;
for (cur = (char *)cl; cur <= last; cur++)
if (cur[0] == cs[0] && memcmp(cur, cs, s_len) == 0)
return cur;
return NULL;
}
char* b2s(uint64_t binary, uint8_t rjust)
{
static char string[64+1] = {0}; // the string representation to return
2016-08-20 23:24:49 +02:00
uint8_t bit = LENGTH(string)-1; // the index of the bit to print
string[bit--] = '\0'; // terminate string
while (binary) {
if (binary & 1) {
string[bit--] = '1';
} else {
string[bit--] = '0';
}
binary >>= 1;
}
2016-08-20 23:24:49 +02:00
while (64-bit-1<rjust && bit>0) {
string[bit--] = '0';
}
2016-08-20 23:24:49 +02:00
return string;
}
/** switch on board LED */
void led_on(void)
{
#if defined(SYSTEM_BOARD) || defined(BLUE_PILL)
gpio_clear(GPIO(LED_PORT), GPIO(LED_PIN));
#elif defined(MAPLE_MINI)
gpio_set(GPIO(LED_PORT), GPIO(LED_PIN));
#endif
}
/** switch off board LED */
void led_off(void)
{
#if defined(SYSTEM_BOARD) || defined(BLUE_PILL)
gpio_set(GPIO(LED_PORT), GPIO(LED_PIN));
#elif defined(MAPLE_MINI)
gpio_clear(GPIO(LED_PORT), GPIO(LED_PIN));
#endif
}
/** toggle board LED */
void led_toggle(void)
{
gpio_toggle(GPIO(LED_PORT), GPIO(LED_PIN));
}
void board_setup(void)
{
// setup LED
rcc_periph_clock_enable(RCC_GPIO(LED_PORT)); // enable clock for LED
gpio_set_mode(GPIO(LED_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(LED_PIN)); // set LED pin to 'output push-pull'
led_off(); // switch off LED per default
// setup button
2016-08-18 11:25:11 +02:00
#if defined(BUTTON_PORT) && defined(BUTTON_PIN)
rcc_periph_clock_enable(RCC_GPIO(BUTTON_PORT)); // enable clock for button
gpio_set_mode(GPIO(BUTTON_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO(BUTTON_PIN)); // set button pin to input
gpio_clear(GPIO(BUTTON_PORT), GPIO(BUTTON_PIN)); // pull down to be able to detect button push (go high)
rcc_periph_clock_enable(RCC_AFIO); // enable alternate function clock for external interrupt
exti_select_source(EXTI(BUTTON_PIN), GPIO(BUTTON_PORT)); // mask external interrupt of this pin only for this port
2016-08-18 11:25:11 +02:00
exti_set_trigger(EXTI(BUTTON_PIN), EXTI_TRIGGER_RISING); // trigger when button is pressed
exti_enable_request(EXTI(BUTTON_PIN)); // enable external interrupt
nvic_enable_irq(NVIC_EXTI_IRQ(BUTTON_PIN)); // enable interrupt
#endif
}
2016-08-18 11:25:11 +02:00
#if defined(BUTTON_PIN)
/** interrupt service routine called when button is pressed */
2016-08-18 11:25:11 +02:00
void EXTI_ISR(BUTTON_PIN)(void)
{
exti_reset_request(EXTI(BUTTON_PIN)); // reset interrupt
button_flag = true; // perform button action
}
#endif