Add PIC32MX GPIO handling; add button/LED support for the Sure PIC32MX

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@4040 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo 2011-10-11 20:50:10 +00:00
parent 86b488777d
commit 1c15909faa
5 changed files with 358 additions and 6 deletions

View File

@ -3,7 +3,7 @@
* include/arch/board/board.h
*
* Copyright (C) 2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions

View File

@ -102,6 +102,24 @@
#define LED_SIGNAL 4 /* N/C N/C ON N/C N/C N/C OFF N/C */
#define LED_ASSERTION 4 /* N/C N/C ON N/C N/C N/C OFF N/C */
#define LED_PANIC 5 /* N/C N/C N/C ON N/C N/C N/C OFF */
#define LED_NVALUES 6
/* Button Definitions *******************************************************/
/* The Sure PIC32MX board has three buttons.
*
* SW1 (SW_UP, left arrow) RB3 Pulled high, Grounded/low when depressed
* SW2 (SW_DOWN, down/right arrow) RB2 Pulled high, Grounded/low when depressed
* SW3 (SW_OK, right arrow) RB4 Pulled high, Grounded/low when depressed
*/
#define BUTTON_SW1 0
#define BUTTON_SW2 1
#define BUTTON_SW3 2
#define NUM_BUTTONS 3
#define BUTTON_SW1_BIT (1 << BUTTON_SW1)
#define BUTTON_SW2_BIT (1 << BUTTON_SW2)
#define BUTTON_SW3_BIT (1 << BUTTON_SW3)
/****************************************************************************
* Public Types
@ -124,6 +142,42 @@ extern "C" {
#define EXTERN extern
#endif
/****************************************************************************
* Button support.
*
* Description:
* up_buttoninit() must be called to initialize button resources. After
* that, up_buttons() may be called to collect the current state of all
* buttons or up_irqbutton() may be called to register button interrupt
* handlers.
*
* After up_buttoninit() has been called, up_buttons() may be called to
* collect the state of all buttons. up_buttons() returns an 8-bit bit set
* with each bit associated with a button. See the BUTTON_*_BIT
* definitions in board.h for the meaning of each bit.
*
* up_irqbutton() may be called to register an interrupt handler that will
* be called when a button is depressed or released. The ID value is a
* button enumeration value that uniquely identifies a button resource.
* See the BUTTON_* definitions in board.h for the meaning of enumeration
* value. The previous interrupt handler address is returned (so that it
* may restored, if so desired).
*
* When an interrupt occurs, it is due to a change on the GPIO input pin
* associated with the button. In that case, all attached change
* notification handlers will be called. Each handler must maintain state
* and determine if the unlying GPIO button input value changed.
*
****************************************************************************/
#ifdef CONFIG_ARCH_BUTTONS
EXTERN void up_buttoninit(void);
EXTERN uint8_t up_buttons(void);
#ifdef CONFIG_ARCH_IRQBUTTONS
EXTERN xcpt_t up_irqbutton(int id, xcpt_t irqhandler);
#endif
#endif
#undef EXTERN
#ifdef __cplusplus
}

View File

@ -2,7 +2,7 @@
# configs/sure-pic32mx/src/Makefile
#
# Copyright (C) 2011 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
# Author: Gregory Nutt <gnutt@nuttx.org>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
@ -44,6 +44,10 @@ ifeq ($(CONFIG_ARCH_LEDS),y)
CSRCS += up_leds.c
endif
ifeq ($(CONFIG_ARCH_BUTTONS),y)
CSRCS += up_buttons.c
endif
ifeq ($(CONFIG_NSH_ARCHINIT),y)
CSRCS += up_nsh.c
endif

View File

@ -0,0 +1,210 @@
/****************************************************************************
* configs/sure-pic32mx/src/up_buttons.c
*
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
#include <stdbool.h>
#include <debug.h>
#include <arch/board/board.h>
#include "chip.h"
#include "up_arch.h"
#include "pic32mx-internal.h"
#include "pic32mx-ioport.h"
#include "pic32mx-adc.h"
#include "sure-internal.h"
#ifdef CONFIG_ARCH_BUTTONS
/****************************************************************************
* Definitions
****************************************************************************/
/* The Sure PIC32MX board has three buttons.
*
* SW1 (SW_UP, left arrow) RB3 Pulled high, Grounded/low when depressed
* SW2 (SW_DOWN, down/right arrow) RB2 Pulled high, Grounded/low when depressed
* SW3 (SW_OK, right arrow) RB4 Pulled high, Grounded/low when depressed
*
* Internal pull-ups are not required since the LEDs are pull-up externally.
* Change notification interrupts are not *automatically* enabled. Change
* notification will be enabled when pic32mx_gpioattach() is called.
*/
#define GPIO_SW1 (GPIO_INPUT|GPIO_INT|GPIO_PORTB|GPIO_PIN_3)
#define GPIO_SW2 (GPIO_INPUT|GPIO_INT|GPIO_PORTB|GPIO_PIN_2)
#define GPIO_SW3 (GPIO_INPUT|GPIO_INT|GPIO_PORTB|GPIO_PIN_4)
/* Change notification numbers:
* RB3 -> CN5
* RB2 -> CN2
* RB4 -> CN6
*/
#define CN_SW1 5
#define CN_SW2 2
#define CN_SW3 6
/****************************************************************************
* Private Data
****************************************************************************/
/* Pin configuration for each button */
static const uint16_t g_buttonset[NUM_BUTTONS] =
{
BUTTON_SW1 BUTTON_SW2, BUTTON_SW3
}
/* Change notification number for each button */
#ifdef CONFIG_ARCH_IRQBUTTONS
static const uint8_t g_buttoncn[NUM_BUTTONS] =
{
CN_SW1, CN_SW2, CN_SW3
}
#endif
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: up_buttoninit
*
* Description:
* up_buttoninit() must be called to initialize button resources. After
* that, up_buttons() may be called to collect the current state of all
* buttons or up_irqbutton() may be called to register button interrupt
* handlers.
*
****************************************************************************/
void up_buttoninit(void)
{
int i;
/* Configure input pins */
for (i = 0; i < NUM_BUTTONS; i++)
{
pic32mx_configgpio(g_buttonset[i]);
}
/* Change AN2/AN3/AN4 to digital */
putreg32(0xffff, PIC32MX_ADC_CFG);
}
/****************************************************************************
* Name: up_buttons
****************************************************************************/
uint8_t up_buttons(void)
{
uint8_t ret = 0;
int id;
/* Configure input pins */
for (id = 0; id < NUM_BUTTONS; id++)
{
if (pic32mx_gpioread(g_buttonset[id]))
{
ret |= (1 << id);
}
}
return ret;
}
/************************************************************************************
* Button support.
*
* Description:
* up_buttoninit() must be called to initialize button resources. After
* that, up_buttons() may be called to collect the current state of all
* buttons or up_irqbutton() may be called to register button interrupt
* handlers.
*
* After up_buttoninit() has been called, up_buttons() may be called to
* collect the state of all buttons. up_buttons() returns an 8-bit bit set
* with each bit associated with a button. See the BUTTON_*_BIT and JOYSTICK_*_BIT
* definitions in board.h for the meaning of each bit.
*
* up_irqbutton() may be called to register an interrupt handler that will
* be called when a button is depressed or released. The ID value is a
* button enumeration value that uniquely identifies a button resource. See the
* BUTTON_* and JOYSTICK_* definitions in board.h for the meaning of enumeration
* value. The previous interrupt handler address is returned (so that it may
* restored, if so desired).
*
* Interrupts are automatically enabled when the button handler is attached and
* automatically disabled when the button handler is detached.
*
* When an interrupt occurs, it is due to a change on the GPIO input pin
* associated with the button. In that case, all attached change
* notification handlers will be called. Each handler must maintain state
* and determine if the unlying GPIO button input value changed.
*
************************************************************************************/
#ifdef CONFIG_ARCH_IRQBUTTONS
xcpt_t up_irqbutton(int id, xcpt_t irqhandler)
{
xcpt_t oldhandler = NULL;
if (id < NUM_BUTTONS)
{
oldhandler = pic32mx_gpioattach(g_buttonset[id], g_buttoncn[id], irqhandler);
if (irqbuttron)
{
pic32mx_gpioirqenable(g_buttoncn[id]);
}
}
return oldhandler;
}
#endif
#endif /* CONFIG_ARCH_BUTTONS */

View File

@ -3,7 +3,7 @@
* arch/arm/src/board/up_leds.c
*
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@ -51,6 +51,7 @@
#include "up_internal.h"
#include "pic32mx-internal.h"
#include "pic32mx-ioport.h"
#include "sure-internal.h"
#ifdef CONFIG_ARCH_LEDS
@ -58,6 +59,7 @@
/****************************************************************************
* Definitions
****************************************************************************/
/* LED Configuration ********************************************************/
/* The Sure PIC32MX board has five LEDs. One (D4, lablel "Power") is not
* controllable by software. Four are controllable by software:
*
@ -78,6 +80,18 @@
* LED_PANIC 5 N/C N/C N/C ON N/C N/C N/C OFF
*/
#define GPIO_USB_LED (GPIO_OUTPUT|GPIO_VALUE_ONE|GPIO_PORTD|GPIO_PIN7)
#define GPIO_SD_LED (GPIO_OUTPUT|GPIO_VALUE_ONE|GPIO_PORTD|GPIO_PIN8)
#define GPIO_FLASH_LED (GPIO_OUTPUT|GPIO_VALUE_ONE|GPIO_PORTD|GPIO_PIN9)
#define GPIO_ERROR_LED (GPIO_OUTPUT|GPIO_VALUE_ONE|GPIO_PORTD|GPIO_PIN10)
/* LED Management Definitions ***********************************************/
#define LED_OFF 0
#define LED_ON 1
#define LED_NC 2
/* Debug ********************************************************************/
/* Enables debug output from this file (needs CONFIG_DEBUG with
* CONFIG_DEBUG_VERBOSE too)
*/
@ -99,13 +113,72 @@
#endif
/****************************************************************************
* Private types
****************************************************************************/
struct led_setting_s
{
uint8_t usb : 2;
uint8_t sd : 2;
uint8_t flash : 2;
uint8_t error : 2;
};
/****************************************************************************
* Private Data
****************************************************************************/
static const g_ledonvalues[LED_NVALUES] =
{
{LED_OFF, LED_OFF, LED_OFF, LED_OFF},
{LED_ON, LED_OFF, LED_NC, LED_NC},
{LED_OFF, LED_ON, LED_NC, LED_NC},
{LED_ON, LED_ON, LED_NC, LED_NC},
{LED_NC, LED_NC, LED_ON, LED_NC},
{LED_NC, LED_NC, LED_NC, LED_ON},
};
static const g_ledoffvalues[LED_NVALUES] =
{
{LED_NC, LED_NC, LED_NC, LED_NC},
{LED_NC, LED_NC, LED_NC, LED_NC},
{LED_NC, LED_NC, LED_NC, LED_NC},
{LED_NC, LED_NC, LED_NC, LED_NC},
{LED_NC, LED_NC, LED_OFF, LED_NC},
{LED_NC, LED_NC, LED_NC, LED_OFF}
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: up_ledinit
****************************************************************************/
void up_setleds(struct led_setting_s *setting)
{
if (setting->usb != LED_NC)
{
pic32mx_gpiowrite(GPIO_USB_LED, setting->usb != LED_ON);
}
if (setting->sd != LED_NC)
{
pic32mx_gpiowrite(GPIO_SD_LED, setting->sd != LED_ON);
}
if (setting->flash != LED_NC)
{
pic32mx_gpiowrite(GPIO_FLASH_LED, setting->flash != LED_ON);
}
if (setting->error != LED_NC)
{
pic32mx_gpiowrite(GPIO_ERROR_LED, setting->error != LED_ON);
}
}
/****************************************************************************
* Public Functions
****************************************************************************/
@ -116,7 +189,12 @@
void up_ledinit(void)
{
#warning "Missing logic"
/* Configure output pins */
pic32mx_configgpio(GPIO_USB_LED);
pic32mx_configgpio(GPIO_SD_LED);
pic32mx_configgpio(GPIO_FLASH_LED);
pic32mx_configgpio(GPIO_ERROR_LED);
}
/****************************************************************************
@ -125,7 +203,10 @@ void up_ledinit(void)
void up_ledon(int led)
{
#warning "Missing logic"
if (led < LED_NVALUES)
{
up_setleds(&g_ledonvalues[led]);
}
}
/****************************************************************************
@ -134,6 +215,9 @@ void up_ledon(int led)
void up_ledoff(int led)
{
#warning "Missing logic"
if (led < LED_NVALUES)
{
up_setleds(&g_ledoffvalues[led]);
}
}
#endif /* CONFIG_ARCH_LEDS */