Browse Source

add board_button_read()

hathach 7 years ago
parent
commit
8457d0d14c

+ 5 - 0
examples/device/cdc_msc_hid/ses/samd21/samd21.emProject

@@ -67,6 +67,11 @@
                     <file file_name="../../../../../hw/mcu/microchip/samd/asf4/samd21/hpl/sysctrl/hpl_sysctrl.c" />
                   </folder>
                 </folder>
+                <folder Name="hal">
+                  <folder Name="src">
+                    <file file_name="../../../../../hw/mcu/microchip/samd/asf4/samd21/hal/src/hal_atomic.c" />
+                  </folder>
+                </folder>
               </folder>
             </folder>
           </folder>

+ 1 - 1
examples/device/cdc_msc_hid/src/main.c

@@ -135,7 +135,7 @@ void usb_hid_task(void)
   if ( board_millis() < start_ms + interval_ms) return; // not enough time
   start_ms += interval_ms;
 
-  uint32_t const btn = board_buttons();
+  uint32_t const btn = board_button_read();
 
   if ( tud_suspended() && btn )
   {

+ 1 - 1
examples/device/cdc_msc_hid_freertos/src/main.c

@@ -158,7 +158,7 @@ void usb_hid_task(void* params)
   if ( board_millis() < start_ms + interval_ms) return; // not enough time
   start_ms += interval_ms;
 
-  uint32_t const btn = board_buttons();
+  uint32_t const btn = board_button_read();
 
   /*------------- Keyboard -------------*/
   if ( tud_hid_keyboard_ready() )

+ 15 - 22
hw/bsp/board.h

@@ -46,6 +46,7 @@
 
 //--------------------------------------------------------------------+
 // Board Porting API
+// For simplicity, only one LED and one Button are used
 //--------------------------------------------------------------------+
 
 // Initialize on-board peripherals : led, button, uart and USB
@@ -54,9 +55,9 @@ void board_init(void);
 // Turn LED on or off
 void board_led_control(bool state);
 
-// Get the current state of buttons on the board
-// \return Bitmask where a '1' means active (pressed), a '0' means inactive.
-uint32_t board_buttons(void);
+// Get the current state of button
+// a '1' means active (pressed), a '0' means inactive.
+uint32_t board_button_read(void);
 
 // Get characters from UART
 int board_uart_read(uint8_t* buf, int len);
@@ -65,28 +66,20 @@ int board_uart_read(uint8_t* buf, int len);
 int board_uart_write(void const * buf, int len);
 
 #if CFG_TUSB_OS == OPT_OS_NONE
-
-// Get current milliseconds, must be implemented in board.c when no OS is used
-uint32_t board_millis(void);
-
+  // Get current milliseconds, must be implemented when no RTOS is used
+  uint32_t board_millis(void);
 #elif CFG_TUSB_OS == OPT_OS_FREERTOS
-
-static inline uint32_t board_millis(void)
-{
-  return ( ( ((uint64_t) xTaskGetTickCount()) * 1000) / configTICK_RATE_HZ );
-}
-
+  static inline uint32_t board_millis(void)
+  {
+    return ( ( ((uint64_t) xTaskGetTickCount()) * 1000) / configTICK_RATE_HZ );
+  }
 #elif CFG_TUSB_OS == OPT_OS_MYNEWT
-
-static inline uint32_t board_millis(void)
-{
-  return os_time_ticks_to_ms32( os_time_get() );
-}
-
+  static inline uint32_t board_millis(void)
+  {
+    return os_time_ticks_to_ms32( os_time_get() );
+  }
 #elif
-
-#error "Need to implement board_millis() for this OS"
-
+  #error "Need to implement board_millis() for this OS"
 #endif
 
 //--------------------------------------------------------------------+

+ 1 - 1
hw/bsp/ea4088qs/board_ea4088qs.c

@@ -127,7 +127,7 @@ static bool button_read(uint8_t id)
 }
 #endif
 
-uint32_t board_buttons(void)
+uint32_t board_button_read(void)
 {
   uint32_t result = 0;
 

+ 1 - 1
hw/bsp/ea4357/board_ea4357.c

@@ -269,7 +269,7 @@ static bool button_read(uint8_t id)
 }
 #endif
 
-uint32_t board_buttons(void)
+uint32_t board_button_read(void)
 {
   uint32_t result = 0;
 

+ 1 - 1
hw/bsp/lpcxpresso11u68/board_lpcxpresso11u68.c

@@ -121,7 +121,7 @@ void board_led_control(bool state)
 //--------------------------------------------------------------------+
 // Buttons
 //--------------------------------------------------------------------+
-uint32_t board_buttons(void)
+uint32_t board_button_read(void)
 {
 //  for(uint8_t i=0; i<BOARD_BUTTON_COUNT; i++) GPIOGetPinValue(buttons[i].port, buttons[i].pin);
 //  return GPIOGetPinValue(buttons[0].port, buttons[0].pin) ? 0 : 1; // button is active low

+ 1 - 1
hw/bsp/lpcxpresso1347/board_lpcxpresso1347.c

@@ -144,7 +144,7 @@ static bool button_read(uint8_t id)
 }
 #endif
 
-uint32_t board_buttons(void)
+uint32_t board_button_read(void)
 {
   uint32_t result = 0;
 

+ 1 - 1
hw/bsp/lpcxpresso1769/board_lpcxpresso1769.c

@@ -180,7 +180,7 @@ static bool button_read(uint8_t id)
 }
 #endif
 
-uint32_t board_buttons(void)
+uint32_t board_button_read(void)
 {
   uint32_t result = 0;
 

+ 1 - 1
hw/bsp/mcb1800/board_mcb1800.c

@@ -193,7 +193,7 @@ static bool button_read(uint8_t id)
 }
 */
 
-uint32_t board_buttons(void)
+uint32_t board_button_read(void)
 {
   uint32_t result = 0;
 

+ 2 - 1
hw/bsp/metro_m0_express/board.mk

@@ -19,7 +19,8 @@ SRC_C += \
 	hw/mcu/microchip/samd/asf4/samd21/gcc/system_samd21.c \
 	hw/mcu/microchip/samd/asf4/samd21/hpl/gclk/hpl_gclk.c \
 	hw/mcu/microchip/samd/asf4/samd21/hpl/pm/hpl_pm.c \
-	hw/mcu/microchip/samd/asf4/samd21/hpl/sysctrl/hpl_sysctrl.c
+	hw/mcu/microchip/samd/asf4/samd21/hpl/sysctrl/hpl_sysctrl.c \
+	hw/mcu/microchip/samd/asf4/samd21/hal/src/hal_atomic.c
 
 INC += \
 	-I$(TOP)/hw/mcu/microchip/samd/asf4/samd21/ \

+ 10 - 4
hw/bsp/metro_m0_express/board_metro_m0_express.c

@@ -39,6 +39,7 @@
 // MACRO TYPEDEF CONSTANT ENUM DECLARATION
 //--------------------------------------------------------------------+
 #define LED_PIN      17
+#define BUTTON_PIN   14
 
 /* Referenced GCLKs, should be initialized firstly */
 #define _GCLK_INIT_1ST (1 << 0 | 1 << 1)
@@ -63,6 +64,10 @@ void board_init(void)
   gpio_set_pin_direction(LED_PIN, GPIO_DIRECTION_OUT);
   gpio_set_pin_level(LED_PIN, 0);
 
+  // Button init
+  gpio_set_pin_direction(BUTTON_PIN, GPIO_DIRECTION_IN);
+  gpio_set_pin_pull_mode(BUTTON_PIN, GPIO_PULL_UP);
+
 #if CFG_TUSB_OS  == OPT_OS_NONE
   // 1ms tick timer (samd SystemCoreClock may not correct)
   SysTick_Config(CONF_CPU_FREQUENCY / 1000);
@@ -101,10 +106,12 @@ void board_led_control(bool state)
   gpio_set_pin_level(LED_PIN, state);
 }
 
+uint32_t board_button_read(void)
+{
+  // button is active low
+  return gpio_get_pin_level(BUTTON_PIN) ? 0 : 1;
+}
 
-/*------------------------------------------------------------------*/
-/* TUSB HAL MILLISECOND
- *------------------------------------------------------------------*/
 #if CFG_TUSB_OS  == OPT_OS_NONE
 volatile uint32_t system_ticks = 0;
 
@@ -118,5 +125,4 @@ uint32_t board_millis(void)
   return system_ticks;
 }
 
-
 #endif

+ 2 - 1
hw/bsp/metro_m4_express/board.mk

@@ -21,7 +21,8 @@ SRC_C += \
 	hw/mcu/microchip/samd/asf4/samd51/hpl/gclk/hpl_gclk.c \
 	hw/mcu/microchip/samd/asf4/samd51/hpl/mclk/hpl_mclk.c \
 	hw/mcu/microchip/samd/asf4/samd51/hpl/osc32kctrl/hpl_osc32kctrl.c \
-	hw/mcu/microchip/samd/asf4/samd51/hpl/oscctrl/hpl_oscctrl.c
+	hw/mcu/microchip/samd/asf4/samd51/hpl/oscctrl/hpl_oscctrl.c \
+	hw/mcu/microchip/samd/asf4/samd51/hal/src/hal_atomic.c
 
 INC += \
 	-I$(TOP)/hw/mcu/microchip/samd/asf4/samd51/ \

+ 11 - 1
hw/bsp/metro_m4_express/board_metro_m4_express.c

@@ -35,7 +35,8 @@
 //--------------------------------------------------------------------+
 // MACRO TYPEDEF CONSTANT ENUM DECLARATION
 //--------------------------------------------------------------------+
-#define LED_PIN         16
+#define LED_PIN      16
+#define BUTTON_PIN   (32 + 17)
 
 /* Referenced GCLKs, should be initialized firstly */
 #define _GCLK_INIT_1ST 0xFFFFFFFF
@@ -61,6 +62,10 @@ void board_init(void)
   gpio_set_pin_direction(LED_PIN, GPIO_DIRECTION_OUT);
   gpio_set_pin_level(LED_PIN, 0);
 
+  // Button init
+  gpio_set_pin_direction(BUTTON_PIN, GPIO_DIRECTION_IN);
+  gpio_set_pin_pull_mode(BUTTON_PIN, GPIO_PULL_UP);
+
 #if CFG_TUSB_OS  == OPT_OS_NONE
   // 1ms tick timer (samd SystemCoreClock may not correct)
   SysTick_Config(CONF_CPU_FREQUENCY / 1000);
@@ -90,6 +95,11 @@ void board_led_control(bool state)
   gpio_set_pin_level(LED_PIN, state);
 }
 
+uint32_t board_button_read(void)
+{
+  // button is active low
+  return gpio_get_pin_level(BUTTON_PIN) ? 0 : 1;
+}
 
 /*------------------------------------------------------------------*/
 /* TUSB HAL MILLISECOND

+ 6 - 16
hw/bsp/pca10056/board_pca10056.c

@@ -42,10 +42,7 @@
 #define LED_PIN         13
 #define LED_STATE_ON    0
 
-uint8_t _button_pins[] = { 11, 12, 24, 25 };
-
-#define BUTTON_COUNT  sizeof(_button_pins)
-
+#define BUTTON_PIN      11
 
 /*------------------------------------------------------------------*/
 /* TUSB HAL MILLISECOND
@@ -79,12 +76,12 @@ void board_init(void)
   NRF_CLOCK->LFCLKSRC = (uint32_t)((CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos) & CLOCK_LFCLKSRC_SRC_Msk);
   NRF_CLOCK->TASKS_LFCLKSTART = 1UL;
 
-  // LEDs
+  // LED
   nrf_gpio_cfg_output(LED_PIN);
   board_led_control(false);
 
   // Button
-  for(uint8_t i=0; i<BUTTON_COUNT; i++) nrf_gpio_cfg_input(_button_pins[i], NRF_GPIO_PIN_PULLUP);
+  nrf_gpio_cfg_input(BUTTON_PIN, NRF_GPIO_PIN_PULLUP);
 
 #if CFG_TUSB_OS == OPT_OS_NONE
   // 1ms tick timer
@@ -134,17 +131,10 @@ void board_led_control(bool state)
   nrf_gpio_pin_write(LED_PIN, state ? LED_STATE_ON : (1-LED_STATE_ON));
 }
 
-uint32_t board_buttons(void)
+uint32_t board_button_read(void)
 {
-  uint32_t ret = 0;
-
-  for(uint8_t i=0; i<BUTTON_COUNT; i++)
-  {
-    // button is active LOW
-    ret |= (nrf_gpio_pin_read(_button_pins[i]) ? 0 : (1 << i));
-  }
-
-  return ret;
+  // button is active LOW
+  return (nrf_gpio_pin_read(BUTTON_PIN) ? 0 : 1);
 }
 
 int board_uart_read(uint8_t* buf, int len)