提交 19fcea52 编写于 作者: L lin

Modified pin driver and some format

上级 c19fb2ab
/*
* File : application.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2015, RT-Thread Development Team
* COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE
* 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 2 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2017-09-14 Haley the first version
*/
/**
* @addtogroup APOLLO2
*/
/*@{*/
#include <rtthread.h>
#include <stdint.h>
#ifdef RT_USING_FINSH
......@@ -24,9 +29,8 @@
#include <shell.h>
#endif
#include "hw_led.h"
#include "led.h"
ALIGN(RT_ALIGN_SIZE)
static rt_uint8_t led_stack[ 512 ];
static struct rt_thread led_thread;
......@@ -34,9 +38,6 @@ static void led_thread_entry(void* parameter)
{
unsigned int count=0;
rt_hw_led_init(0);
rt_hw_led_init(1);
while (1)
{
/* led1 on */
......@@ -56,18 +57,8 @@ static void led_thread_entry(void* parameter)
}
}
void rt_init_thread_entry(void* parameter)
int main(void)
{
#ifdef RT_USING_COMPONENTS_INIT
/* initialization RT-Thread Components */
rt_components_init();
#endif
}
int rt_application_init(void)
{
rt_thread_t init_thread;
rt_err_t result;
/* init led thread */
......@@ -77,17 +68,12 @@ int rt_application_init(void)
RT_NULL,
(rt_uint8_t*)&led_stack[0],
sizeof(led_stack),
7,
RT_THREAD_PRIORITY_MAX/3,
5);
if (result == RT_EOK)
{
rt_thread_startup(&led_thread);
}
init_thread = rt_thread_create("init", rt_init_thread_entry, RT_NULL, 1024,
RT_THREAD_PRIORITY_MAX / 3, 20);
if (init_thread != RT_NULL)
rt_thread_startup(init_thread);
return 0;
}
......
/*
* File : startup.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2015, RT-Thread Develop Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://openlab.rt-thread.com/license/LICENSE
*
* Change Logs:
* Date Author Notes
* 2017-09-11 Haley the first version
*/
#include <rthw.h>
#include <rtthread.h>
#include "board.h"
/**
* @addtogroup Apollo2
*/
/*@{*/
extern int rt_application_init(void);
#ifdef __CC_ARM
extern int Image$$RW_IRAM1$$ZI$$Limit;
#define AM_SRAM_BEGIN (&Image$$RW_IRAM1$$ZI$$Limit)
#elif __ICCARM__
#pragma section="HEAP"
#define AM_SRAM_BEGIN (__segment_end("HEAP"))
#else
extern int __bss_end;
#define NRF_SRAM_BEGIN (&__bss_end)
#endif
/**
* This function will startup RT-Thread RTOS.
*/
void rtthread_startup(void)
{
/* init board */
rt_hw_board_init();
/* show version */
rt_show_version();
/* init tick */
rt_system_tick_init();
/* init kernel object */
rt_system_object_init();
/* init timer system */
rt_system_timer_init();
#ifdef RT_USING_HEAP
rt_system_heap_init((void*)AM_SRAM_BEGIN, (void*)AM_SRAM_END);
#endif
/* init scheduler system */
rt_system_scheduler_init();
/* init application */
rt_application_init();
/* init timer thread */
rt_system_timer_thread_init();
/* init idle thread */
rt_thread_idle_init();
/* start scheduler */
rt_system_scheduler_start();
/* never reach here */
return ;
}
int main(void)
{
/* disable interrupt first */
// rt_hw_interrupt_disable();
/* startup RT-Thread RTOS */
rtthread_startup();
return 0;
}
/*@}*/
/*
* File : gpio.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
*
* 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 2 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2017-09-16 Haley the first version
*/
#include <rtthread.h>
#include <rtdevice.h>
#include "am_mcu_apollo.h"
#ifdef RT_USING_PIN
void am_pin_mode(rt_device_t dev, rt_base_t pin, rt_base_t mode)
{
if (mode == PIN_MODE_OUTPUT)
{
/* output setting */
am_hal_gpio_pin_config(pin, AM_HAL_GPIO_OUTPUT);
}
else if (mode == PIN_MODE_INPUT)
{
/* input setting: not pull. */
am_hal_gpio_pin_config(pin, AM_HAL_GPIO_INPUT);
}
else if (mode == PIN_MODE_INPUT_PULLUP)
{
/* input setting: pull up. */
am_hal_gpio_pin_config(pin, AM_HAL_GPIO_OPENDRAIN);
}
else
{
/* input setting:default. */
am_hal_gpio_pin_config(pin, AM_HAL_GPIO_INPUT);
}
}
void am_pin_write(rt_device_t dev, rt_base_t pin, rt_base_t value)
{
if (value == PIN_LOW)
{
am_hal_gpio_out_bit_clear(pin);
}
else
{
am_hal_gpio_out_bit_set(pin);
}
}
int am_pin_read(rt_device_t dev, rt_base_t pin)
{
int value = PIN_LOW;
if (am_hal_gpio_input_bit_read(pin) == 0)
{
value = PIN_LOW;
}
else
{
value = PIN_HIGH;
}
return value;
}
const static struct rt_pin_ops _am_pin_ops =
{
am_pin_mode,
am_pin_write,
am_pin_read,
};
int hw_pin_init(void)
{
rt_device_pin_register("pin", &_am_pin_ops, RT_NULL);
return 0;
}
INIT_BOARD_EXPORT(hw_pin_init);
#endif
/*@}*/
/*
* File : gpio.h
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
*
* 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 2 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2017-09-16 Haley the first version
*/
#ifndef __GPIO_H
#define __GPIO_H
int hw_pin_init(void);
#endif // __GPIO_H
/*
* File : hw_led.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2017, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE
*
* Change Logs:
* Date Author Notes
* 2017-09-14 Haley the first version
*/
#include <rtthread.h>
#include "am_mcu_apollo.h"
#include "hw_led.h"
#define AM_GPIO_LED0 46
#define AM_GPIO_LED1 47
#define AM_GPIO_LED2 48
#define AM_GPIO_LED3 49
#define AM_NUM_LEDS 4
rt_hw_led_t am_psLEDs[AM_NUM_LEDS] =
{
{AM_GPIO_LED0, AM_LED_ON_LOW | AM_LED_POL_DIRECT_DRIVE_M},
{AM_GPIO_LED1, AM_LED_ON_LOW | AM_LED_POL_DIRECT_DRIVE_M},
{AM_GPIO_LED2, AM_LED_ON_LOW | AM_LED_POL_DIRECT_DRIVE_M},
{AM_GPIO_LED3, AM_LED_ON_LOW | AM_LED_POL_DIRECT_DRIVE_M},
};
/**
* @brief Configures the necessary pins for an array of LEDs
*
* @param LEDNum is the LED number.
*
* This function configures a GPIO to drive an LED in a low-power way.
*
* @return None.
*/
void rt_hw_led_init(rt_uint32_t LEDNum)
{
rt_hw_led_t *psLED = am_psLEDs + LEDNum;
/* Handle Direct Drive Versus 3-State (with pull-up or no buffer) */
if ( AM_LED_POL_DIRECT_DRIVE_M & psLED->Polarity )
{
/* Configure the pin as a push-pull GPIO output */
am_hal_gpio_pin_config(psLED->GPIONumber, AM_HAL_GPIO_OUTPUT);
/* Enable the output driver, and set the output value to the LEDs "ON" state */
am_hal_gpio_out_enable_bit_set(psLED->GPIONumber);
am_hal_gpio_out_bit_replace(psLED->GPIONumber,
psLED->Polarity &
AM_LED_POL_DIRECT_DRIVE_M);
}
else
{
/* Configure the pin as a tri-state GPIO */
am_hal_gpio_pin_config(psLED->GPIONumber, AM_HAL_GPIO_3STATE);
/* Disable the output driver, and set the output value to the LEDs "ON" state */
am_hal_gpio_out_enable_bit_clear(psLED->GPIONumber);
am_hal_gpio_out_bit_replace(psLED->GPIONumber,
psLED->Polarity &
AM_LED_POL_DIRECT_DRIVE_M );
}
}
/**
* @brief Configures the necessary pins for an array of LEDs
*
* @param NumLEDs is the total number of LEDs in the array.
*
* This function configures the GPIOs for an array of LEDs.
*
* @return None.
*/
void rt_hw_led_array_init(rt_uint32_t NumLEDs)
{
/* Loop through the list of LEDs, configuring each one individually */
for ( int i = 0; i < NumLEDs; i++ )
{
rt_hw_led_init(i);
}
}
/**
* @brief Disables an array of LEDs
*
* @param NumLEDs is the total number of LEDs in the array.
*
* This function disables the GPIOs for an array of LEDs.
*
* @return None.
*/
void rt_hw_led_array_disable(rt_uint32_t NumLEDs)
{
rt_hw_led_t *psLEDs = am_psLEDs;
/* Loop through the list of LEDs, configuring each one individually */
for ( int i = 0; i < NumLEDs; i++ )
{
am_hal_gpio_pin_config((psLEDs + i)->GPIONumber, AM_HAL_GPIO_DISABLE);
}
}
/**
* @brief Turns on the requested LED.
*
* @param LEDNum is the LED number for the light to turn on.
*
* This function turns on a single LED.
*
* @return None.
*/
void rt_hw_led_on(rt_uint32_t LEDNum)
{
rt_hw_led_t *psLEDs = am_psLEDs;
/* Handle Direct Drive Versus 3-State (with pull-up or no buffer) */
if ( AM_LED_POL_DIRECT_DRIVE_M & psLEDs[LEDNum].Polarity )
{
/* Set the output to the correct state for the LED */
am_hal_gpio_out_bit_replace(psLEDs[LEDNum].GPIONumber,
psLEDs[LEDNum].Polarity &
AM_LED_POL_POLARITY_M );
}
else
{
/* Turn on the output driver for the LED */
am_hal_gpio_out_enable_bit_set(psLEDs[LEDNum].GPIONumber);
}
}
/**
* @brief Turns off the requested LED.
*
* @param LEDNum is the LED number for the light to turn off.
*
* This function turns off a single LED.
*
* @return None.
*/
void rt_hw_led_off(rt_uint32_t LEDNum)
{
rt_hw_led_t *psLEDs = am_psLEDs;
/* Handle Direct Drive Versus 3-State (with pull-up or no buffer) */
if ( AM_LED_POL_DIRECT_DRIVE_M & psLEDs[LEDNum].Polarity )
{
/* Set the output to the correct state for the LED */
am_hal_gpio_out_bit_replace(psLEDs[LEDNum].GPIONumber,
!(psLEDs[LEDNum].Polarity &
AM_LED_POL_POLARITY_M) );
}
else
{
/* Turn off the output driver for the LED */
am_hal_gpio_out_enable_bit_clear(psLEDs[LEDNum].GPIONumber);
}
}
/**
* @brief Toggles the requested LED.
*
* @param LEDNum is the LED number for the light to toggle.
*
* This function toggles a single LED.
*
* @return None.
*/
void rt_hw_led_toggle(rt_uint32_t LEDNum)
{
rt_hw_led_t *psLEDs = am_psLEDs;
/* Handle Direct Drive Versus 3-State (with pull-up or no buffer) */
if ( AM_LED_POL_DIRECT_DRIVE_M & psLEDs[LEDNum].Polarity )
{
am_hal_gpio_out_bit_toggle(psLEDs[LEDNum].GPIONumber);
}
else
{
/* Check to see if the LED pin is enabled */
if ( am_hal_gpio_out_enable_bit_get(psLEDs[LEDNum].GPIONumber) )
{
/* If it was enabled, turn if off */
am_hal_gpio_out_enable_bit_clear(psLEDs[LEDNum].GPIONumber);
}
else
{
/* If it was not enabled, turn if on */
am_hal_gpio_out_enable_bit_set(psLEDs[LEDNum].GPIONumber);
}
}
}
/**
* @brief Gets the state of the requested LED.
*
* @param LEDNum is the LED to check.
*
* This function checks the state of a single LED.
*
* @return 1(true) if the LED is on.
*/
int rt_hw_led_get(rt_uint32_t LEDNum)
{
rt_hw_led_t *psLEDs = am_psLEDs;
/* Handle Direct Drive Versus 3-State (with pull-up or no buffer) */
if ( AM_LED_POL_DIRECT_DRIVE_M & psLEDs[LEDNum].Polarity )
{
/* Mask to the GPIO bit position for this GPIO number */
uint64_t ui64Mask = 0x01l << psLEDs[LEDNum].GPIONumber;
/* Extract the state of this bit and return it */
return !!(am_hal_gpio_input_read() & ui64Mask);
}
else
{
return am_hal_gpio_out_enable_bit_get(
psLEDs[LEDNum].GPIONumber);
}
}
/**
* @brief Display a binary value using LEDs.
*
* @param NumLEDs is the number of LEDs in the array.
* @param Value is the value to display on the LEDs.
*
* This function displays a value in binary across an array of LEDs.
*
* @return None.
*/
void rt_hw_led_array_out(rt_uint32_t NumLEDs, rt_uint32_t Value)
{
for ( int i = 0; i < NumLEDs; i++ )
{
if ( Value & (1 << i) )
{
rt_hw_led_on(i);
}
else
{
rt_hw_led_off(i);
}
}
}
#ifdef RT_USING_FINSH
#include <finsh.h>
static rt_uint8_t led_inited = 0;
void led(rt_uint32_t led, rt_uint32_t value)
{
/* init led configuration if it's not inited. */
if (!led_inited)
{
// rt_hw_led_init(0);
// rt_hw_led_init(1);
led_inited = 1;
}
if ( led == 0 )
{
/* set led status */
switch (value)
{
case 0:
rt_hw_led_off(0);
break;
case 1:
rt_hw_led_on(0);
break;
default:
break;
}
}
if ( led == 1 )
{
/* set led status */
switch (value)
{
case 0:
rt_hw_led_off(1);
break;
case 1:
rt_hw_led_on(1);
break;
default:
break;
}
}
}
FINSH_FUNCTION_EXPORT(led, set led[0 - 1] on[1] or off[0].)
#endif
/*@}*/
/*
* File : hw_led.h
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2017, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE
*
* Change Logs:
* Date Author Notes
* 2017-09-14 Haley the first version
*/
#ifndef __HW_LED_H
#define __HW_LED_H
#include <rtthread.h>
/**
* @brief LED polarity macros
*
*/
#define AM_LED_POL_POLARITY_M 0x1
#define AM_LED_ON_HIGH 0x1
#define AM_LED_ON_LOW 0x0
/**
* @brief LED direct drive indicator macro
* Or this in with the polarity value to use the GPIO DATA register instead of
* the GPIO DATA ENABLE register to directly drive an LED buffer.
*/
#define AM_LED_POL_DIRECT_DRIVE_M 0x2
/**
* @brief Structure for keeping track of LEDs
*
*/
typedef struct
{
rt_uint32_t GPIONumber;
rt_uint32_t Polarity;
}
rt_hw_led_t;
/**
* @brief External function definitions
*
*/
void rt_hw_led_init(rt_uint32_t LEDNum);
void rt_hw_led_array_init(rt_uint32_t NumLEDs);
void rt_hw_led_array_disable(rt_uint32_t NumLEDs);
void rt_hw_led_on(rt_uint32_t LEDNum);
void rt_hw_led_off(rt_uint32_t LEDNum);
void rt_hw_led_toggle(rt_uint32_t LEDNum);
int rt_hw_led_get(rt_uint32_t LEDNum);
void rt_hw_led_array_out(rt_uint32_t NumLEDs, rt_uint32_t Value);
#endif // __HW_LED_H
/*
* File : hw_uart.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2017, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE
*
* Change Logs:
* Date Author Notes
* 2017-09-14 Haley the first version
*/
#ifndef __HW_UART_H_
#define __HW_UART_H_
#include <rtthread.h>
void rt_hw_uart_init(void);
void rt_hw_uart_send_string(char *pcString);
#endif // __HW_UART_H_
/*
* File :_led.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
*
* 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 2 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2017-09-14 Haley the first version
*/
#include <rtthread.h>
#include <rtdevice.h>
#include "am_mcu_apollo.h"
#include "board.h"
#define AM_GPIO_LED0 46
#define AM_GPIO_LED1 47
#define AM_GPIO_LED2 48
#define AM_GPIO_LED3 49
/**
* @brief Turns on the requested LED.
*
* @param LEDNum is the LED number for the light to turn on.
*
* This function turns on a single LED.
*
* @return None.
*/
void rt_hw_led_on(rt_uint8_t LEDNum)
{
if(LEDNum == 0)
rt_pin_write(AM_GPIO_LED0, PIN_LOW);
else if(LEDNum == 1)
rt_pin_write(AM_GPIO_LED1, PIN_LOW);
else if(LEDNum == 2)
rt_pin_write(AM_GPIO_LED2, PIN_LOW);
else if(LEDNum == 3)
rt_pin_write(AM_GPIO_LED3, PIN_LOW);
}
/**
* @brief Turns off the requested LED.
*
* @param LEDNum is the LED number for the light to turn off.
*
* This function turns off a single LED.
*
* @return None.
*/
void rt_hw_led_off(rt_uint8_t LEDNum)
{
if(LEDNum == 0)
rt_pin_write(AM_GPIO_LED0, PIN_HIGH);
else if(LEDNum == 1)
rt_pin_write(AM_GPIO_LED1, PIN_HIGH);
else if(LEDNum == 2)
rt_pin_write(AM_GPIO_LED2, PIN_HIGH);
else if(LEDNum == 3)
rt_pin_write(AM_GPIO_LED3, PIN_HIGH);
}
/**
* @brief Configures the necessary pins for an array of LEDs
*
* @param None.
*
* This function configures a GPIO to drive an LED in a low-power way.
*
* @return None.
*/
int rt_hw_led_init(void)
{
#if defined(RT_USING_LED0)
/* config led */
rt_pin_mode(AM_GPIO_LED0, PIN_MODE_OUTPUT);
/* turns off the led */
rt_hw_led_off(0);
#endif /* RT_USING_LED0 */
#if defined(RT_USING_LED1)
/* config led */
rt_pin_mode(AM_GPIO_LED1, PIN_MODE_OUTPUT);
/* turns off the led */
rt_hw_led_off(1);
#endif /* RT_USING_LED1 */
#if defined(RT_USING_LED2)
/* config led */
rt_pin_mode(AM_GPIO_LED2, PIN_MODE_OUTPUT);
/* turns off the led */
rt_hw_led_off(2);
#endif /* RT_USING_LED0 */
#if defined(RT_USING_LED3)
/* config led */
rt_pin_mode(AM_GPIO_LED3, PIN_MODE_OUTPUT);
/* turns off the led */
rt_hw_led_off(3);
#endif /* RT_USING_LED1 */
return 0;
}
#ifdef RT_USING_PIN
INIT_BOARD_EXPORT(rt_hw_led_init);
#endif
#ifdef RT_USING_FINSH
#include <finsh.h>
void led(rt_uint32_t led, rt_uint32_t state)
{
/* set led status */
switch (state)
{
case 0:
rt_hw_led_off(led);
break;
case 1:
rt_hw_led_on(led);
break;
default:
break;
}
}
FINSH_FUNCTION_EXPORT(led, turn led (0 - 3) on (1) or off (0).)
#endif
/*@}*/
/*
* File : led.h
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
*
* 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 2 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2017-09-14 Haley the first version
*/
#ifndef __HW_LED_H
#define __HW_LED_H
#include <rtthread.h>
/**
* @brief External function definitions
*
*/
void rt_hw_led_init(void);
void rt_hw_led_on(rt_uint8_t LEDNum);
void rt_hw_led_off(rt_uint8_t LEDNum);
#endif // __HW_LED_H
/*
* File : hw_uart.c
* File : uart.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2017, RT-Thread Development Team
* COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE
* 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 2 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2017-09-15 Haley the first version
*/
#include <rtdevice.h>
#include "am_mcu_apollo.h"
#include "hw_uart.h"
#include "board.h"
#include <rtdevice.h>
/* USART0 */
#define AM_UART0_INST 0
......@@ -37,7 +46,7 @@
struct am_uart
{
uint32_t uart_device;
uint32_t uart_interrupt;
uint32_t uart_interrupt;
};
/**
......@@ -70,17 +79,17 @@ static void rt_hw_uart_enable(struct am_uart* uart)
/* Enable the UART */
am_hal_uart_enable(uart->uart_device);
#if defined(RT_USING_UART0)
#if defined(RT_USING_UART0)
/* Make sure the UART RX and TX pins are enabled */
am_hal_gpio_pin_config(UART0_GPIO_TX, UART0_GPIO_CFG_TX);
am_hal_gpio_pin_config(UART0_GPIO_RX, UART0_GPIO_CFG_RX | AM_HAL_GPIO_PULL12K);
#endif /* RT_USING_UART0 */
#if defined(RT_USING_UART1)
am_hal_gpio_pin_config(UART0_GPIO_TX, UART0_GPIO_CFG_TX);
am_hal_gpio_pin_config(UART0_GPIO_RX, UART0_GPIO_CFG_RX | AM_HAL_GPIO_PULL12K);
#endif /* RT_USING_UART0 */
#if defined(RT_USING_UART1)
/* Make sure the UART RX and TX pins are enabled */
am_hal_gpio_pin_config(UART1_GPIO_TX, UART1_GPIO_CFG_TX);
am_hal_gpio_pin_config(UART1_GPIO_RX, UART1_GPIO_CFG_RX | AM_HAL_GPIO_PULL12K);
#endif /* RT_USING_UART1 */
am_hal_gpio_pin_config(UART1_GPIO_TX, UART1_GPIO_CFG_TX);
am_hal_gpio_pin_config(UART1_GPIO_RX, UART1_GPIO_CFG_RX | AM_HAL_GPIO_PULL12K);
#endif /* RT_USING_UART1 */
}
/**
......@@ -94,26 +103,26 @@ static void rt_hw_uart_enable(struct am_uart* uart)
*/
static void rt_hw_uart_disable(struct am_uart* uart)
{
/* Clear all interrupts before sleeping as having a pending UART interrupt burns power */
am_hal_uart_int_clear(uart->uart_device, 0xFFFFFFFF);
/* Clear all interrupts before sleeping as having a pending UART interrupt burns power */
am_hal_uart_int_clear(uart->uart_device, 0xFFFFFFFF);
/* Disable the UART */
am_hal_uart_disable(uart->uart_device);
/* Disable the UART */
am_hal_uart_disable(uart->uart_device);
#if defined(RT_USING_UART0)
#if defined(RT_USING_UART0)
/* Disable the UART pins */
am_hal_gpio_pin_config(UART0_GPIO_TX, AM_HAL_PIN_DISABLE);
am_hal_gpio_pin_config(UART0_GPIO_RX, AM_HAL_PIN_DISABLE);
#endif /* RT_USING_UART0 */
#if defined(RT_USING_UART1)
am_hal_gpio_pin_config(UART0_GPIO_TX, AM_HAL_PIN_DISABLE);
am_hal_gpio_pin_config(UART0_GPIO_RX, AM_HAL_PIN_DISABLE);
#endif /* RT_USING_UART0 */
#if defined(RT_USING_UART1)
/* Disable the UART pins */
am_hal_gpio_pin_config(UART1_GPIO_TX, AM_HAL_PIN_DISABLE);
am_hal_gpio_pin_config(UART1_GPIO_RX, AM_HAL_PIN_DISABLE);
#endif /* RT_USING_UART1 */
/* Disable the UART clock */
am_hal_uart_clock_disable(uart->uart_device);
am_hal_gpio_pin_config(UART1_GPIO_TX, AM_HAL_PIN_DISABLE);
am_hal_gpio_pin_config(UART1_GPIO_RX, AM_HAL_PIN_DISABLE);
#endif /* RT_USING_UART1 */
/* Disable the UART clock */
am_hal_uart_clock_disable(uart->uart_device);
}
/**
......@@ -129,9 +138,9 @@ static void rt_hw_uart_disable(struct am_uart* uart)
void rt_hw_uart_send_string(char *pcString)
{
am_hal_uart_string_transmit_polled(AM_UART0_INST, pcString);
/* Wait until busy bit clears to make sure UART fully transmitted last byte */
while ( am_hal_uart_flags_get(AM_UART0_INST) & AM_HAL_UART_FR_BUSY );
/* Wait until busy bit clears to make sure UART fully transmitted last byte */
while ( am_hal_uart_flags_get(AM_UART0_INST) & AM_HAL_UART_FR_BUSY );
}
static rt_err_t am_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
......@@ -141,29 +150,29 @@ static rt_err_t am_configure(struct rt_serial_device *serial, struct serial_conf
RT_ASSERT(serial != RT_NULL);
RT_ASSERT(cfg != RT_NULL);
uart = (struct am_uart *)serial->parent.user_data;
uart = (struct am_uart *)serial->parent.user_data;
RT_ASSERT(uart != RT_NULL);
/* Get the configure */
g_sUartConfig.ui32BaudRate = cfg->baud_rate;
g_sUartConfig.ui32DataBits = cfg->data_bits;
RT_ASSERT(uart != RT_NULL);
/* Get the configure */
g_sUartConfig.ui32BaudRate = cfg->baud_rate;
g_sUartConfig.ui32DataBits = cfg->data_bits;
if (cfg->stop_bits == STOP_BITS_1)
g_sUartConfig.bTwoStopBits = false;
else if (cfg->stop_bits == STOP_BITS_2)
g_sUartConfig.bTwoStopBits = true;
g_sUartConfig.ui32Parity = cfg->parity;
g_sUartConfig.ui32FlowCtrl = AM_HAL_UART_PARITY_NONE;
g_sUartConfig.ui32Parity = cfg->parity;
g_sUartConfig.ui32FlowCtrl = AM_HAL_UART_PARITY_NONE;
/* Configure the UART */
am_hal_uart_config(uart->uart_device, &g_sUartConfig);
/* Enable the UART */
am_hal_uart_enable(uart->uart_device);
return RT_EOK;
am_hal_uart_enable(uart->uart_device);
return RT_EOK;
}
static rt_err_t am_control(struct rt_serial_device *serial, int cmd, void *arg)
......@@ -174,24 +183,24 @@ static rt_err_t am_control(struct rt_serial_device *serial, int cmd, void *arg)
RT_ASSERT(serial != RT_NULL);
uart = (struct am_uart *)serial->parent.user_data;
RT_ASSERT(uart != RT_NULL);
RT_ASSERT(uart != RT_NULL);
switch (cmd)
{
/* disable interrupt */
case RT_DEVICE_CTRL_CLR_INT:
rt_hw_uart_disable(uart);
break;
/* enable interrupt */
case RT_DEVICE_CTRL_SET_INT:
rt_hw_uart_enable(uart);
break;
/* UART config */
case RT_DEVICE_CTRL_CONFIG :
break;
case RT_DEVICE_CTRL_CLR_INT:
rt_hw_uart_disable(uart);
break;
/* enable interrupt */
case RT_DEVICE_CTRL_SET_INT:
rt_hw_uart_enable(uart);
break;
/* UART config */
case RT_DEVICE_CTRL_CONFIG :
break;
}
return RT_EOK;
return RT_EOK;
}
static int am_putc(struct rt_serial_device *serial, char c)
......@@ -200,33 +209,33 @@ static int am_putc(struct rt_serial_device *serial, char c)
RT_ASSERT(serial != RT_NULL);
uart = (struct am_uart *)serial->parent.user_data;
RT_ASSERT(uart != RT_NULL);
am_hal_uart_char_transmit_polled(uart->uart_device, c);
return 1;
RT_ASSERT(uart != RT_NULL);
am_hal_uart_char_transmit_polled(uart->uart_device, c);
return 1;
}
static int am_getc(struct rt_serial_device *serial)
{
char c;
int ch;
struct am_uart* uart;
RT_ASSERT(serial != RT_NULL);
char c;
int ch;
struct am_uart* uart;
RT_ASSERT(serial != RT_NULL);
uart = (struct am_uart *)serial->parent.user_data;
RT_ASSERT(uart != RT_NULL);
RT_ASSERT(uart != RT_NULL);
ch = -1;
if (am_hal_uart_flags_get(uart->uart_device) & AM_HAL_UART_FR_RX_FULL)
if ((am_hal_uart_flags_get(uart->uart_device) & AM_HAL_UART_FR_RX_EMPTY) == 0)
{
am_hal_uart_char_receive_polled(uart->uart_device, &c);
ch = c & 0xff;
am_hal_uart_char_receive_polled(uart->uart_device, &c);
ch = c & 0xff;
}
return ch;
return ch;
}
/**
......@@ -236,35 +245,35 @@ static int am_getc(struct rt_serial_device *serial)
*/
static void uart_isr(struct rt_serial_device *serial)
{
uint32_t status;
RT_ASSERT(serial != RT_NULL);
struct am_uart *uart = (struct am_uart *) serial->parent.user_data;
RT_ASSERT(uart != RT_NULL);
uint32_t status;
RT_ASSERT(serial != RT_NULL);
struct am_uart *uart = (struct am_uart *) serial->parent.user_data;
RT_ASSERT(uart != RT_NULL);
/* Read the interrupt status */
status = am_hal_uart_int_status_get(uart->uart_device, false);
//rt_kprintf("status is %d\r\n", status);
/* Clear the UART interrupt */
am_hal_uart_int_clear(uart->uart_device, status);
status = am_hal_uart_int_status_get(uart->uart_device, false);
//rt_kprintf("status is %d\r\n", status);
/* Clear the UART interrupt */
am_hal_uart_int_clear(uart->uart_device, status);
if (status & (AM_HAL_UART_INT_RX_TMOUT))
{
rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_TIMEOUT);
rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
}
if (status & AM_HAL_UART_INT_RX)
{
rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
}
if (status & AM_HAL_UART_INT_TX)
{
//rt_hw_serial_isr(serial, RT_SERIAL_EVENT_TX_DONE);
}
{
rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
}
if (status & AM_HAL_UART_INT_TX)
{
// rt_hw_serial_isr(serial, RT_SERIAL_EVENT_TX_DONE);
}
}
static const struct rt_uart_ops am_uart_ops =
......@@ -280,7 +289,7 @@ static const struct rt_uart_ops am_uart_ops =
struct am_uart uart0 =
{
AM_UART0_INST,
AM_HAL_INTERRUPT_UART0
AM_HAL_INTERRUPT_UART0
};
static struct rt_serial_device serial0;
......@@ -301,7 +310,7 @@ void am_uart0_isr(void)
struct am_uart uart1 =
{
AM_UART1_INST,
AM_HAL_INTERRUPT_UART1
AM_HAL_INTERRUPT_UART1
};
static struct rt_serial_device serial1;
......@@ -309,7 +318,7 @@ void am_uart1_isr(void)
{
/* enter interrupt */
rt_interrupt_enter();
uart_isr(&serial1);
/* leave interrupt */
......@@ -319,17 +328,17 @@ void am_uart1_isr(void)
static void GPIO_Configuration(void)
{
#if defined(RT_USING_UART0)
#if defined(RT_USING_UART0)
/* Make sure the UART RX and TX pins are enabled */
am_hal_gpio_pin_config(UART0_GPIO_TX, UART0_GPIO_CFG_TX);
am_hal_gpio_pin_config(UART0_GPIO_RX, UART0_GPIO_CFG_RX | AM_HAL_GPIO_PULL12K);
#endif /* RT_USING_UART0 */
#if defined(RT_USING_UART1)
am_hal_gpio_pin_config(UART0_GPIO_TX, UART0_GPIO_CFG_TX);
am_hal_gpio_pin_config(UART0_GPIO_RX, UART0_GPIO_CFG_RX | AM_HAL_GPIO_PULL12K);
#endif /* RT_USING_UART0 */
#if defined(RT_USING_UART1)
/* Make sure the UART RX and TX pins are enabled */
am_hal_gpio_pin_config(UART1_GPIO_TX, UART1_GPIO_CFG_TX);
am_hal_gpio_pin_config(UART1_GPIO_RX, UART1_GPIO_CFG_RX | AM_HAL_GPIO_PULL12K);
#endif /* RT_USING_UART1 */
am_hal_gpio_pin_config(UART1_GPIO_TX, UART1_GPIO_CFG_TX);
am_hal_gpio_pin_config(UART1_GPIO_RX, UART1_GPIO_CFG_RX | AM_HAL_GPIO_PULL12K);
#endif /* RT_USING_UART1 */
}
static void RCC_Configuration(struct am_uart* uart)
......@@ -341,26 +350,25 @@ static void RCC_Configuration(struct am_uart* uart)
am_hal_uart_clock_enable(uart->uart_device);
/* Disable the UART before configuring it */
am_hal_uart_disable(uart->uart_device);
am_hal_uart_disable(uart->uart_device);
/* Configure the UART */
am_hal_uart_config(uart->uart_device, &g_sUartConfig);
am_hal_uart_config(uart->uart_device, &g_sUartConfig);
/* Enable the UART */
am_hal_uart_enable(uart->uart_device);
am_hal_uart_enable(uart->uart_device);
/* Enable the UART FIFO */
//am_hal_uart_fifo_config(uart->uart_device, AM_HAL_UART_TX_FIFO_1_2 | AM_HAL_UART_RX_FIFO_1_2);
am_hal_uart_fifo_config(uart->uart_device, AM_HAL_UART_TX_FIFO_1_2 | AM_HAL_UART_RX_FIFO_1_2);
}
static void NVIC_Configuration(struct am_uart* uart)
{
/* Enable interrupts */
am_hal_uart_int_enable(uart->uart_device, AM_HAL_UART_INT_RX);
/* Enable the uart interrupt in the NVIC */
am_hal_interrupt_enable(uart->uart_interrupt);
am_hal_uart_int_clear(uart->uart_device, 0xFFFFFFFF);
/* Enable interrupts */
am_hal_uart_int_enable(uart->uart_device, AM_HAL_UART_INT_RX_TMOUT | AM_HAL_UART_INT_RX);
/* Enable the uart interrupt in the NVIC */
am_hal_interrupt_enable(uart->uart_interrupt);
}
/**
......@@ -373,40 +381,40 @@ static void NVIC_Configuration(struct am_uart* uart)
void rt_hw_uart_init(void)
{
struct am_uart* uart;
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
GPIO_Configuration();
#if defined(RT_USING_UART0)
uart = &uart0;
config.baud_rate = BAUD_RATE_115200;
RCC_Configuration(uart);
NVIC_Configuration(uart);
RCC_Configuration(uart);
NVIC_Configuration(uart);
serial0.ops = &am_uart_ops;
serial0.config = config;
serial0.config = config;
/* register UART1 device */
rt_hw_serial_register(&serial0, "uart0",
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX |
RT_DEVICE_FLAG_INT_TX, uart);
RT_DEVICE_FLAG_INT_TX, uart);
#endif /* RT_USING_UART0 */
#if defined(RT_USING_UART1)
uart = &uart1;
config.baud_rate = BAUD_RATE_115200;
RCC_Configuration(uart);
NVIC_Configuration(uart);
RCC_Configuration(uart);
NVIC_Configuration(uart);
serial1.ops = &am_uart_ops;
serial1.config = config;
serial1.config = config;
/* register UART1 device */
rt_hw_serial_register(&serial1, "uart1",
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX |
RT_DEVICE_FLAG_INT_TX, uart);
RT_DEVICE_FLAG_INT_TX, uart);
#endif /* RT_USING_UART1 */
}
......
/*
* File : uart.h
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
*
* 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 2 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2017-09-14 Haley the first version
*/
#ifndef __UART_H_
#define __UART_H_
void rt_hw_uart_init(void);
#endif // __UART_H_
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册