Skip to content

Commit

Permalink
pbioconfig: Introduce pbioconfig.h
Browse files Browse the repository at this point in the history
This adds a new pbioconfig.h file to allow configuration the PBIO library on a per-application basis. Using header files is more consistent with MicroPython and more versitale than relying on using Makefiles to select configuration.
  • Loading branch information
dlech committed Jun 1, 2019
1 parent c5ba549 commit 90adb5f
Show file tree
Hide file tree
Showing 28 changed files with 97 additions and 37 deletions.
5 changes: 5 additions & 0 deletions bricks/HUB4/pbioconfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2019 David Lechner

#define PBIO_CONFIG_ENABLE_DEINIT (0)
#define PBIO_CONFIG_ENABLE_SYS (1)
5 changes: 5 additions & 0 deletions bricks/MOVEHUB/pbioconfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2019 David Lechner

#define PBIO_CONFIG_ENABLE_DEINIT (0)
#define PBIO_CONFIG_ENABLE_SYS (1)
4 changes: 4 additions & 0 deletions bricks/NXT/pbioconfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2019 David Lechner

#define PBIO_CONFIG_ENABLE_SYS (1)
4 changes: 4 additions & 0 deletions bricks/debug/pbioconfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2019 David Lechner

#define PBIO_CONFIG_ENABLE_SYS (1)
4 changes: 1 addition & 3 deletions bricks/ev3dev/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ INC += -I$(TOP)/ports/pybricks/lib/pbio/platform/ev3dev_stretch
INC += -I$(TOP)/ports/pybricks/extmod
INC += -I$(TOP)/ports/unix

PBIO_OPT = -DPBIO_CONFIG_ENABLE_DEINIT

# compiler settings
CWARN = -Wall -Werror
CWARN += -Wpointer-arith -Wuninitialized
CFLAGS = $(INC) $(CWARN) -std=gnu99 -DUNIX $(CFLAGS_MOD) $(COPT) $(CFLAGS_EXTRA) $(PBIO_OPT)
CFLAGS = $(INC) $(CWARN) -std=gnu99 -DUNIX $(CFLAGS_MOD) $(COPT) $(CFLAGS_EXTRA)

# Debugging/Optimization
ifdef DEBUG
Expand Down
3 changes: 3 additions & 0 deletions bricks/ev3dev/pbioconfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2019 David Lechner

4 changes: 1 addition & 3 deletions bricks/stm32.mk
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,9 @@ OPENOCD ?= openocd
OPENOCD_CONFIG ?= openocd_stm32f$(CPU_FAMILY).cfg
TEXT0_ADDR ?= 0x08000000

PBIO_OPT = -DPBIO_CONFIG_ENABLE_SYS

CFLAGS_CORTEX_M0 = -mthumb -mtune=cortex-m0 -mcpu=cortex-m0 -msoft-float
CFLAGS_CORTEX_M4 = -mthumb -mtune=cortex-m4 -mabi=aapcs-linux -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard -fsingle-precision-constant -Wdouble-promotion
CFLAGS = $(INC) -Wall -Werror -std=c99 -nostdlib $(CFLAGS_CORTEX_M$(CPU_FAMILY)) $(COPT) $(PBIO_OPT)
CFLAGS = $(INC) -Wall -Werror -std=c99 -nostdlib $(CFLAGS_CORTEX_M$(CPU_FAMILY)) $(COPT)
LDFLAGS = -nostdlib -T $(PBIO_PLATFORM).ld -Map=$@.map --cref --gc-sections

# Tune for Debugging or Optimization
Expand Down
5 changes: 3 additions & 2 deletions lib/pbio/drv/adc/adc_stm32f0.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdbool.h>
#include <stdint.h>

#include <pbio/config.h>
#include <pbio/error.h>
#include "sys/process.h"

Expand Down Expand Up @@ -73,7 +74,7 @@ pbio_error_t pbdrv_adc_get_ch(uint8_t ch, uint16_t *value) {
return PBIO_SUCCESS;
}

#ifdef PBIO_CONFIG_ENABLE_DEINIT
#if PBIO_CONFIG_ENABLE_DEINIT
static void pbdrv_adc_exit() {
// REVISIT: do we need timeouts here?
ADC1->CR |= ADC_CR_ADSTP;
Expand All @@ -87,7 +88,7 @@ static void pbdrv_adc_exit() {
PROCESS_THREAD(pbdrv_adc_process, ev, data) {
// TODO: use DMA for background updates and add filtering
// PROCESS_POLLHANDLER(pbdrv_adc_poll());
#ifdef PBIO_CONFIG_ENABLE_DEINIT
#if PBIO_CONFIG_ENABLE_DEINIT
PROCESS_EXITHANDLER(pbdrv_adc_exit());
#endif

Expand Down
3 changes: 2 additions & 1 deletion lib/pbio/drv/debug/button.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright (c) 2018 David Lechner

#include <pbio/button.h>
#include <pbio/config.h>
#include <pbio/error.h>
#include <pbio/port.h>

Expand All @@ -13,7 +14,7 @@ void _pbdrv_button_init(void) {
GPIOC->MODER = (GPIOC->MODER & ~GPIO_MODER_MODER13_Msk) | (0 << GPIO_MODER_MODER13_Pos);
}

#ifdef PBIO_CONFIG_ENABLE_DEINIT
#if PBIO_CONFIG_ENABLE_DEINIT
void _pbdrv_button_deinit(void) { }
#endif

Expand Down
5 changes: 3 additions & 2 deletions lib/pbio/drv/debug/light.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
// Copyright (c) 2018 David Lechner

#include <pbdrv/light.h>
#include <pbio/port.h>
#include <pbio/config.h>
#include <pbio/error.h>
#include <pbio/port.h>

#include "stm32f446xx.h"

Expand Down Expand Up @@ -52,7 +53,7 @@ void _pbdrv_light_init(void) {
TIM4->CCR2 = 0;
}

#ifdef PBIO_CONFIG_ENABLE_DEINIT
#if PBIO_CONFIG_ENABLE_DEINIT
// turn off the light
void _pbdrv_light_deinit(void) {
TIM12->CR1 &= ~TIM_CR1_CEN;
Expand Down
3 changes: 2 additions & 1 deletion lib/pbio/drv/ev3dev_stretch/button.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <unistd.h>

#include <pbio/button.h>
#include <pbio/config.h>
#include <pbio/error.h>
#include <pbio/port.h>

Expand All @@ -15,7 +16,7 @@ void _pbdrv_button_init(void) {
f_btn = open("/dev/input/by-path/platform-gpio_keys-event", O_RDONLY);
}

#ifdef PBIO_CONFIG_ENABLE_DEINIT
#if PBIO_CONFIG_ENABLE_DEINIT
void _pbdrv_button_deinit(void) {
close(f_btn);
f_btn = -1;
Expand Down
5 changes: 3 additions & 2 deletions lib/pbio/drv/ev3dev_stretch/light.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
#include <stdio.h>

#include <pbdrv/light.h>
#include <pbio/port.h>
#include <pbio/config.h>
#include <pbio/error.h>
#include <pbio/port.h>

#define NLEDS 4

Expand Down Expand Up @@ -38,7 +39,7 @@ void _pbdrv_light_init(void) {
}
}

#ifdef PBIO_CONFIG_ENABLE_DEINIT
#if PBIO_CONFIG_ENABLE_DEINIT
void _pbdrv_light_deinit(void) {
for (int led = 0; led < NLEDS; led++) {
if (f_brightness[led]) {
Expand Down
3 changes: 2 additions & 1 deletion lib/pbio/drv/ev3dev_stretch/motor.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <string.h>

#include <pbdrv/motor.h>
#include <pbio/config.h>


#define MAX_PATH_LENGTH 120
Expand Down Expand Up @@ -166,7 +167,7 @@ void _pbdrv_motor_init(void) {
}
}

#ifdef PBIO_CONFIG_ENABLE_DEINIT
#if PBIO_CONFIG_ENABLE_DEINIT
void _pbdrv_motor_deinit(void) {
for(pbio_port_t port = PBDRV_CONFIG_FIRST_MOTOR_PORT; port <= PBDRV_CONFIG_LAST_MOTOR_PORT; port++) {
sysfs_close_and_reset(port);
Expand Down
3 changes: 2 additions & 1 deletion lib/pbio/drv/hub4/bluetooth.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <stdio.h>
#include <string.h>

#include "pbio/config.h"
#include "pbio/error.h"
#include "pbio/event.h"
#include "pbsys/sys.h"
Expand Down Expand Up @@ -151,7 +152,7 @@ static void spi_init() {
NVIC_EnableIRQ(EXTI2_3_IRQn);
}

#ifdef PBIO_CONFIG_ENABLE_DEINIT
#if PBIO_CONFIG_ENABLE_DEINIT
static void bluetooth_deinit() {
// nRESET
// set PB6 output low
Expand Down
3 changes: 2 additions & 1 deletion lib/pbio/drv/hub4/button.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright (c) 2018 David Lechner

#include <pbio/button.h>
#include <pbio/config.h>
#include <pbio/error.h>
#include <pbio/port.h>

Expand All @@ -15,7 +16,7 @@ void _pbdrv_button_init(void) {
GPIOC->MODER = (GPIOC->MODER & ~GPIO_MODER_MODER13_Msk) | (0 << GPIO_MODER_MODER13_Pos);
}

#ifdef PBIO_CONFIG_ENABLE_DEINIT
#if PBIO_CONFIG_ENABLE_DEINIT
void _pbdrv_button_deinit(void) { }
#endif

Expand Down
5 changes: 3 additions & 2 deletions lib/pbio/drv/hub4/light.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
// Copyright (c) 2018 David Lechner

#include <pbdrv/light.h>
#include <pbio/port.h>
#include <pbio/config.h>
#include <pbio/error.h>
#include <pbio/port.h>

#include "stm32f030xc.h"

Expand Down Expand Up @@ -49,7 +50,7 @@ void _pbdrv_light_init(void) {
TIM15->CCR2 = 0;
}

#ifdef PBIO_CONFIG_ENABLE_DEINIT
#if PBIO_CONFIG_ENABLE_DEINIT
// turn off the light
void _pbdrv_light_deinit(void) {
TIM15->CR1 &= ~TIM_CR1_CEN;
Expand Down
5 changes: 3 additions & 2 deletions lib/pbio/drv/hub4/motor.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "stm32f030xc.h"

#include <pbdrv/motor.h>
#include <pbio/config.h>

void _pbdrv_motor_init(void) {
// it isn't clear what PB2 does yet, but tacho doesn't work without setting it high.
Expand Down Expand Up @@ -242,11 +243,11 @@ pbio_error_t pbdrv_motor_get_id(pbio_port_t port, pbio_iodev_type_id_t *id) {
}

*id = iodev->info->type_id;

return PBIO_SUCCESS;
}

#ifdef PBIO_CONFIG_ENABLE_DEINIT
#if PBIO_CONFIG_ENABLE_DEINIT
void _pbdrv_motor_deinit(void) {
// disable the PWM timers
TIM3->CR1 &= TIM_CR1_CEN;
Expand Down
3 changes: 2 additions & 1 deletion lib/pbio/drv/move_hub/bluetooth.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <stdio.h>
#include <string.h>

#include "pbio/config.h"
#include "pbio/error.h"
#include "pbio/event.h"
#include "pbsys/sys.h"
Expand Down Expand Up @@ -151,7 +152,7 @@ static void spi_init() {
NVIC_EnableIRQ(EXTI2_3_IRQn);
}

#ifdef PBIO_CONFIG_ENABLE_DEINIT
#if PBIO_CONFIG_ENABLE_DEINIT
static void bluetooth_deinit() {
// nRESET
// set PB6 output low
Expand Down
3 changes: 2 additions & 1 deletion lib/pbio/drv/move_hub/button.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright (c) 2018 David Lechner

#include <pbio/button.h>
#include <pbio/config.h>
#include <pbio/error.h>
#include <pbio/port.h>

Expand All @@ -15,7 +16,7 @@ void _pbdrv_button_init(void) {
GPIOC->MODER = (GPIOC->MODER & ~GPIO_MODER_MODER13_Msk) | (0 << GPIO_MODER_MODER13_Pos);
}

#ifdef PBIO_CONFIG_ENABLE_DEINIT
#if PBIO_CONFIG_ENABLE_DEINIT
void _pbdrv_button_deinit(void) { }
#endif

Expand Down
5 changes: 3 additions & 2 deletions lib/pbio/drv/move_hub/light.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
// Copyright (c) 2018 David Lechner

#include <pbdrv/light.h>
#include <pbio/port.h>
#include <pbio/config.h>
#include <pbio/error.h>
#include <pbio/port.h>

#include "stm32f070xb.h"

Expand Down Expand Up @@ -49,7 +50,7 @@ void _pbdrv_light_init(void) {
TIM15->CCR2 = 0;
}

#ifdef PBIO_CONFIG_ENABLE_DEINIT
#if PBIO_CONFIG_ENABLE_DEINIT
// turn off the light
void _pbdrv_light_deinit(void) {
TIM15->CR1 &= ~TIM_CR1_CEN;
Expand Down
3 changes: 2 additions & 1 deletion lib/pbio/drv/move_hub/motor.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "stm32f070xb.h"

#include <pbdrv/motor.h>
#include <pbio/config.h>

#define PBIO_MOTOR_BUF_SIZE 32 // must be power of 2!

Expand Down Expand Up @@ -515,7 +516,7 @@ pbio_error_t pbdrv_motor_get_id(pbio_port_t port, pbio_iodev_type_id_t *id) {
return PBIO_ERROR_INVALID_PORT;
}

#ifdef PBIO_CONFIG_ENABLE_DEINIT
#if PBIO_CONFIG_ENABLE_DEINIT
void _pbdrv_motor_deinit(void) {
// disable the PWM timers
TIM1->CR1 &= TIM_CR1_CEN;
Expand Down
3 changes: 2 additions & 1 deletion lib/pbio/include/pbdrv/button.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <pbdrv/config.h>

#include <pbio/button.h>
#include <pbio/config.h>
#include <pbio/error.h>
#include <pbio/port.h>

Expand All @@ -32,7 +33,7 @@ void _pbdrv_button_init(void);
* Releases the low level button driver. No button functions can be called after
* calling this function.
*/
#ifdef PBIO_CONFIG_ENABLE_DEINIT
#if PBIO_CONFIG_ENABLE_DEINIT
void _pbdrv_button_deinit(void);
#else
static inline void _pbdrv_button_deinit(void) { }
Expand Down
3 changes: 2 additions & 1 deletion lib/pbio/include/pbdrv/light.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include <pbdrv/config.h>

#include <pbio/config.h>
#include <pbio/error.h>
#include <pbio/light.h>
#include <pbio/port.h>
Expand All @@ -31,7 +32,7 @@ void _pbdrv_light_init(void);
* Releases the low level light driver. No light functions can be called after
* calling this function.
*/
#ifdef PBIO_CONFIG_ENABLE_DEINIT
#if PBIO_CONFIG_ENABLE_DEINIT
void _pbdrv_light_deinit(void);
#else
static inline void _pbdrv_light_deinit(void) { }
Expand Down
3 changes: 2 additions & 1 deletion lib/pbio/include/pbdrv/motor.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include <pbdrv/config.h>
#include <pbdrv/ioport.h>
#include <pbio/config.h>
#include <pbio/error.h>
#include <pbio/port.h>

Expand All @@ -35,7 +36,7 @@ void _pbdrv_motor_init(void);
* Releases the low level motor driver. No motor functions can be called after
* calling this function.
*/
#ifdef PBIO_CONFIG_ENABLE_DEINIT
#if PBIO_CONFIG_ENABLE_DEINIT
void _pbdrv_motor_deinit(void);
#else
static inline void _pbdrv_motor_deinit(void) { }
Expand Down
22 changes: 22 additions & 0 deletions lib/pbio/include/pbio/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2019 David Lechner

#ifndef _PBIO_CONFIG_H_
#define _PBIO_CONFIG_H_

// This file should be defined by applications that use the PBIO library.
#include "pbioconfig.h"

#ifndef PBIO_CONFIG_ENABLE_SYS
#define PBIO_CONFIG_ENABLE_SYS (0)
#endif

#ifndef PBIO_CONFIG_ENABLE_DEINIT
#define PBIO_CONFIG_ENABLE_DEINIT (1)
#endif

#ifndef PBIO_CONFIG_DISABLE_UARTDEV
#define PBIO_CONFIG_DISABLE_UARTDEV (0)
#endif

#endif // _PBIO_CONFIG_H_
Loading

0 comments on commit 90adb5f

Please sign in to comment.