summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhathach <[email protected]>2019-08-31 17:39:47 +0700
committerhathach <[email protected]>2019-08-31 17:52:24 +0700
commitc70c61ce2690be513c1399381b8fe796b05dbec9 (patch)
treef38d33df663bc846b12dc75c77246410aeec43b6
parentb7dbc98ab1ed7c8739ec89b5a3eeaea052b33d8a (diff)
add button read to board_test example
-rw-r--r--examples/device/board_test/src/main.c45
-rw-r--r--hw/bsp/lpcxpresso51u68/lpcxpresso51u68.c16
2 files changed, 28 insertions, 33 deletions
diff --git a/examples/device/board_test/src/main.c b/examples/device/board_test/src/main.c
index f6066a4f1..0126110ed 100644
--- a/examples/device/board_test/src/main.c
+++ b/examples/device/board_test/src/main.c
@@ -34,45 +34,34 @@
//--------------------------------------------------------------------+
/* Blink pattern
- * - 250 ms : device not mounted
- * - 1000 ms : device mounted
- * - 2500 ms : device is suspended
+ * - 250 ms : button is not pressed
+ * - 1000 ms : button is pressed (and hold)
*/
enum {
- BLINK_NOT_MOUNTED = 250,
- BLINK_MOUNTED = 1000,
- BLINK_SUSPENDED = 2500,
+ BLINK_PRESSED = 250,
+ BLINK_UNPRESSED = 1000
};
-static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED;
-
-void led_blinking_task(void);
-
-/*------------- MAIN -------------*/
int main(void)
{
board_init();
+ uint32_t start_ms = 0;
+ bool led_state = false;
+
while (1)
{
- led_blinking_task();
- }
-
- return 0;
-}
+ uint32_t interval_ms = board_button_read() ? BLINK_PRESSED : BLINK_UNPRESSED;
-//--------------------------------------------------------------------+
-// BLINKING TASK
-//--------------------------------------------------------------------+
-void led_blinking_task(void)
-{
- static uint32_t start_ms = 0;
- static bool led_state = false;
+ // Blink every interval ms
+ if ( !(board_millis() - start_ms < interval_ms) )
+ {
+ start_ms = board_millis();
- // Blink every interval ms
- if ( board_millis() - start_ms < blink_interval_ms) return; // not enough time
- start_ms += blink_interval_ms;
+ board_led_write(led_state);
+ led_state = 1 - led_state; // toggle
+ }
+ }
- board_led_write(led_state);
- led_state = 1 - led_state; // toggle
+ return 0;
}
diff --git a/hw/bsp/lpcxpresso51u68/lpcxpresso51u68.c b/hw/bsp/lpcxpresso51u68/lpcxpresso51u68.c
index 6d3e8e241..3786f97fb 100644
--- a/hw/bsp/lpcxpresso51u68/lpcxpresso51u68.c
+++ b/hw/bsp/lpcxpresso51u68/lpcxpresso51u68.c
@@ -53,10 +53,17 @@ void board_init(void)
NVIC_SetPriority(USB0_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
#endif
- // LED
- gpio_pin_config_t led_config = { kGPIO_DigitalOutput, 0, };
GPIO_PortInit(GPIO, LED_PORT);
+ GPIO_PortInit(GPIO, BUTTON_PORT);
+
+ // LED
+ gpio_pin_config_t const led_config = { kGPIO_DigitalOutput, 0};
GPIO_PinInit(GPIO, LED_PORT, LED_PIN, &led_config);
+ board_led_write(true);
+
+ // Button
+ gpio_pin_config_t const button_config = { kGPIO_DigitalInput, 0};
+ GPIO_PinInit(GPIO, BUTTON_PORT, BUTTON_PIN, &button_config);
}
//--------------------------------------------------------------------+
@@ -71,8 +78,7 @@ void board_led_write(bool state)
uint32_t board_button_read(void)
{
// active low
-// return Chip_GPIO_GetPinState(LPC_GPIO, BUTTON_PORT, BUTTON_PIN) ? 0 : 1;
- return 0;
+ return 1-GPIO_PinRead(GPIO, BUTTON_PORT, BUTTON_PIN);
}
int board_uart_read(uint8_t* buf, int len)
@@ -91,7 +97,7 @@ int board_uart_write(void const * buf, int len)
#if CFG_TUSB_OS == OPT_OS_NONE
volatile uint32_t system_ticks = 0;
-void SysTick_Handler (void)
+void SysTick_Handler(void)
{
system_ticks++;
}