modify the coding style and add change logs

This commit is contained in:
LYH-ux 2020-07-01 23:44:57 +08:00
parent aebdcaccbb
commit 2aaeea751e
21 changed files with 720 additions and 1010 deletions

View File

@ -7,51 +7,37 @@
* Date Author Notes * Date Author Notes
* 2017-07-24 Tanek the first version * 2017-07-24 Tanek the first version
* 2018-11-12 Ernest Chen modify copyright * 2018-11-12 Ernest Chen modify copyright
* 2020-06-27 AHTYDHD modify to adapt in TM4C123
*/ */
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include "hw_ints.h" #include "inc/hw_sysctl.h"
#include "hw_sysctl.h" #include "inc/hw_memmap.h"
#include "hw_memmap.h" #include "driverlib/fpu.h"
#include "hw_types.h" #include "driverlib/sysctl.h"
#include "fpu.h" #include "driverlib/systick.h"
#include "debug.h"
#include "pin_map.h"
#include "rom.h"
#include "sysctl.h"
#include "systick.h"
#include "hw_ints.h"
#include "board.h" #include "board.h"
// Holds the system core clock, which is the system clock
// frequency supplied to the SysTick timer and the processor
// core clock.
uint32_t SystemCoreClock; uint32_t SystemCoreClock;
/* this function set the system clock */
void SystemCoreClockUpdate(void) void SystemCoreClockUpdate(void)
{ {
FPULazyStackingEnable();
FPULazyStackingEnable(); /* Set the clocking to run directly from the crystal. 50MHz*/
//
// Set the clocking to run directly from the crystal.
//
SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ |
SYSCTL_OSC_MAIN); SYSCTL_OSC_MAIN);
SystemCoreClock = SysCtlClockGet();
SystemCoreClock = SysCtlClockGet();
} }
/* this funtion set the Systick and enable systick int */
void SysTickConfig() void SysTickConfig()
{ {
SysTickDisable(); SysTickDisable();
SysTickPeriodSet(SystemCoreClock/RT_TICK_PER_SECOND); SysTickPeriodSet(SystemCoreClock / RT_TICK_PER_SECOND);
SysTickIntEnable(); SysTickIntEnable();
SysTickEnable(); SysTickEnable();
} }
/** /**
@ -65,44 +51,32 @@ void rt_hw_board_init()
SysTickConfig(); SysTickConfig();
#ifdef RT_USING_SERIAL #ifdef RT_USING_SERIAL
rt_hw_usart_init(); rt_hw_usart_init();
#endif #endif
#ifdef RT_USING_PIN #ifdef RT_USING_PIN
rt_hw_pin_init(); rt_hw_pin_init();
#endif #endif
#ifdef RT_USING_PWM #ifdef RT_USING_PWM
rt_hw_pwm_init(); rt_hw_pwm_init();
#endif #endif
/* Call components board initial (use INIT_BOARD_EXPORT()) */ /* Call components board initial (use INIT_BOARD_EXPORT()) */
#ifdef RT_USING_COMPONENTS_INIT #ifdef RT_USING_COMPONENTS_INIT
rt_components_board_init(); rt_components_board_init();
#endif #endif
/* set the console device */
rt_console_set_device(RT_CONSOLE_DEVICE_NAME); rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
#if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP) #if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END); rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
#endif #endif
} }
void SysTick_Handler(void) void SysTick_Handler(void)
{ {
/* enter interrupt */ /* enter interrupt */
rt_interrupt_enter(); rt_interrupt_enter();
rt_tick_increase(); rt_tick_increase();
/* leave interrupt */ /* leave interrupt */
rt_interrupt_leave(); rt_interrupt_leave();
} }

View File

@ -1,32 +1,40 @@
#ifndef _BOARD_H_ /*
#define _BOARD_H_ * Copyright (c) 2006-2019, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-06-27 AHTYDHD the first version
*/
#ifndef __BOARD_H__
#define __BOARD_H__
#include <rthw.h> #include <rthw.h>
#include <rtthread.h> #include <rtthread.h>
#if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP) #if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
#define TM4C123_SRAM1_START (0x20000000)
#define TM4C123_SRAM1_START (0x20000000)
#define TM4C123_SRAM1_END (TM4C123_SRAM1_START + 32 * 1024) // end address = 0x20000000(base adddress) + 32K(RAM size) #define TM4C123_SRAM1_END (TM4C123_SRAM1_START + 32 * 1024) // end address = 0x20000000(base adddress) + 32K(RAM size)
#if defined(__CC_ARM) || defined(__CLANG_ARM) #if defined(__CC_ARM) || defined(__CLANG_ARM)
extern int Image$$RW_IRAM$$ZI$$Limit; // RW_IRAM extern int Image$$RW_IRAM$$ZI$$Limit; // RW_IRAM
#define HEAP_BEGIN ((void *)&Image$$RW_IRAM$$ZI$$Limit) #define HEAP_BEGIN ((void *)&Image$$RW_IRAM$$ZI$$Limit)
#elif __ICCARM__
#pragma section="HEAP"
#define HEAP_BEGIN (__segment_end("HEAP"))
#else
extern int _ebss;
#define HEAP_BEGIN ((void *)&_ebss)
#endif #endif
#define HEAP_END TM4C123_SRAM1_END #define HEAP_END TM4C123_SRAM1_END
#endif #endif
#ifdef RT_USING_PIN #ifdef RT_USING_PIN
#include "drv_gpio.h" #include "drv_gpio.h"
#endif /* RT_USING_PIN */ #endif /* RT_USING_PIN */
#ifdef RT_USING_SERIAL #ifdef RT_USING_SERIAL
#include "drv_uart.h" #include "drv_uart.h"
#endif /* RT_USING_SERIAL */ #endif /* RT_USING_SERIAL */
@ -39,9 +47,6 @@ extern int Image$$RW_IRAM$$ZI$$Limit; // RW_IRAM
#include "drv_spi.h" #include "drv_spi.h"
#endif /* RT_USING_SPI*/ #endif /* RT_USING_SPI*/
#endif /*__BOARD_H__*/
/************************** end of file ******************/
#endif /*_BOARD_H_*/

View File

@ -1,77 +1,88 @@
#include "stdbool.h" /*
#include "stdint.h" * Copyright (c) 2006-2019, RT-Thread Development Team
#include "hw_memmap.h" *
#include "pin_map.h" * SPDX-License-Identifier: Apache-2.0
#include "sysctl.h" *
#include "pwm.h" * Change Logs:
#include "gpio.h" * Date Author Notes
#include "uart.h" * 2020-06-27 AHTYDHD the first version
#include "adc.h" */
#include "ssi.h"
#include <rtthread.h>
#include <stdbool.h>
#include <stdint.h>
#include "inc/hw_memmap.h"
#include "driverlib/pin_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "tm4c123_config.h" #include "tm4c123_config.h"
#ifdef RT_USING_SERIAL
#include "driverlib/uart.h"
#endif /* RT_USING_SERIAL */
#ifdef RT_USING_ADC
#include "driverlib/adc.h"
#endif /* RT_USING_ADC */
#ifdef RT_USING_PWM
#include "driverlib/pwm.h"
#endif /* RT_USING_PWM */
#ifdef RT_USING_SPI
#include "driverlib/ssi.h"
#endif /* RT_USING_SPI */
#ifdef RT_USING_SERIAL
void uart_hw_config(void) void uart_hw_config(void)
{ {
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0); /* UART0 */
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
GPIOPinConfigure(GPIO_PA0_U0RX); GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX); GPIOPinConfigure(GPIO_PA1_U0TX);
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1); GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
/* UART1 */
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART1); SysCtlPeripheralEnable(SYSCTL_PERIPH_UART1);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
GPIOPinConfigure(GPIO_PC4_U1RX); GPIOPinConfigure(GPIO_PC4_U1RX);
GPIOPinConfigure(GPIO_PC5_U1TX); GPIOPinConfigure(GPIO_PC5_U1TX);
GPIOPinTypeUART(GPIO_PORTC_BASE, GPIO_PIN_4 | GPIO_PIN_5); GPIOPinTypeUART(GPIO_PORTC_BASE, GPIO_PIN_4 | GPIO_PIN_5);
} }
#endif /* RT_USING_SERIAL */
#ifdef RT_USING_ADC
void pwm_hw_config(void)
{
SysCtlPWMClockSet(SYSCTL_PWMDIV_2);
//GPIO port D needs to be enabled so these pins can be used.
SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
// Configure the GPIO pin muxing to select PWM functions for these pins.
GPIOPinConfigure(GPIO_PF2_M1PWM6);
GPIOPinConfigure(GPIO_PF3_M1PWM7);
// Configure the GPIO pad for PWM function on pins PF2 and PF3
GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_2|GPIO_PIN_3);
}
void adc_hw_config(void) void adc_hw_config(void)
{ {
// The ADC0 peripheral must be enabled for use. /* ADC0 */
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0); SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
// GPIO port D needs to be enabled
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
GPIOPinTypeADC(GPIO_PORTD_BASE, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3);
// Select the analog ADC function for these pins.
GPIOPinTypeADC(GPIO_PORTD_BASE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
} }
#endif /* RT_USING_ADC */
#ifdef RT_USING_PWM
void pwm_hw_config(void)
{
/* PWM7 (PWM1 module,M1PWM6 and M1PWM7) */
SysCtlPWMClockSet(SYSCTL_PWMDIV_2);
SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
GPIOPinConfigure(GPIO_PF2_M1PWM6);
GPIOPinConfigure(GPIO_PF3_M1PWM7);
GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_2 | GPIO_PIN_3);
}
#endif /* RT_USING_PWM */
#ifdef RT_USING_SPI
void spi_hw_config(void) void spi_hw_config(void)
{ {
/* SPI0 */
SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI0); SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
GPIOPinConfigure(GPIO_PA2_SSI0CLK); GPIOPinConfigure(GPIO_PA2_SSI0CLK);
//GPIOPinConfigure(GPIO_PA3_SSI0FSS);
GPIOPinConfigure(GPIO_PA4_SSI0RX); GPIOPinConfigure(GPIO_PA4_SSI0RX);
GPIOPinConfigure(GPIO_PA5_SSI0TX); GPIOPinConfigure(GPIO_PA5_SSI0TX);
GPIOPinTypeSSI(GPIO_PORTA_BASE, GPIO_PIN_5 | GPIO_PIN_4 | GPIO_PIN_2); GPIOPinTypeSSI(GPIO_PORTA_BASE, GPIO_PIN_5 | GPIO_PIN_4 | GPIO_PIN_2);
} }
#endif /* RT_USING_SPI */
/************************** end of file ******************/

View File

@ -1,23 +1,38 @@
#ifndef _TM4C123GH6PZ_CONFIG_H_ /*
#define _TM4C123GH6PZ_CONFIG_H_ * Copyright (c) 2006-2019, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-06-27 AHTYDHD the first version
*/
#ifndef __TM4C123GH6PZ_CONFIG_H__
#define __TM4C123GH6PZ_CONFIG_H__
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
void uart_hw_config(void); #ifdef RT_USING_SERIAL
void pwm_hw_config(void); void uart_hw_config(void);
#endif /* RT_USING_SERIAL */
#ifdef RT_USING_ADC
void adc_hw_config(void); void adc_hw_config(void);
void spi_hw_config(void); #endif /* RT_USING_ADC */
#ifdef RT_USING_PWM
void pwm_hw_config(void);
#endif /* RT_USING_PWM */
#ifdef RT_USING_SPI
void spi_hw_config(void);
#endif /* RT_USING_SPI */
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif /*__TM4C123GH6PZ_CONFIG_H__*/
#endif /*_TM4C123GH6PZ_CONFIG_H_*/ /************************** end of file ******************/

View File

@ -1,13 +1,20 @@
#ifndef _ADC_CONFIG_H_ /*
#define _ADC_CONFIG_H_ * Copyright (c) 2006-2019, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-06-27 AHTYDHD the first version
*/
#ifndef __ADC_CONFIG_H__
#define __ADC_CONFIG_H__
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#ifdef BSP_USING_ADC0 #ifdef BSP_USING_ADC0
#ifndef ADC0_CONFIG #ifndef ADC0_CONFIG
#define ADC0_CONFIG \ #define ADC0_CONFIG \
@ -36,10 +43,10 @@ extern "C" {
#endif /* ADC1_CONFIG */ #endif /* ADC1_CONFIG */
#endif /* BSP_USING_ADC1 */ #endif /* BSP_USING_ADC1 */
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif /*__ADC_CONFIG_H__*/
#endif /*_ADC_CONFIG_H_*/ /************************** end of file ******************/

View File

@ -1,7 +1,15 @@
#ifndef _PWM_CONFIG_H_ /*
#define _PWM_CONFIG_H_ * Copyright (c) 2006-2019, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-06-27 AHTYDHD the first version
*/
#ifndef __PWM_CONFIG_H__
#define __PWM_CONFIG_H__
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -102,13 +110,11 @@ extern "C" {
} }
#endif /* PWM7_CONFIG */ #endif /* PWM7_CONFIG */
#endif /* BSP_USING_PWM7 */ #endif /* BSP_USING_PWM7 */
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif /*__PWM_CONFIG_H__*/
#endif /*_PWM_CONFIG_H_*/ /************************** end of file ******************/

View File

@ -1,3 +1,13 @@
/*
* Copyright (c) 2006-2019, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-06-27 AHTYDHD the first version
*/
#ifndef __SPI_CONFIG_H__ #ifndef __SPI_CONFIG_H__
#define __SPI_CONFIG_H__ #define __SPI_CONFIG_H__
@ -17,7 +27,6 @@ extern "C" {
#endif /* SPI0_BUS_CONFIG */ #endif /* SPI0_BUS_CONFIG */
#endif /* BSP_USING_SPI0 */ #endif /* BSP_USING_SPI0 */
#ifdef BSP_USING_SPI1 #ifdef BSP_USING_SPI1
#ifndef SPI1_BUS_CONFIG #ifndef SPI1_BUS_CONFIG
#define SPI1_BUS_CONFIG \ #define SPI1_BUS_CONFIG \
@ -38,7 +47,6 @@ extern "C" {
#endif /* SPI2_BUS_CONFIG */ #endif /* SPI2_BUS_CONFIG */
#endif /* BSP_USING_SPI2 */ #endif /* BSP_USING_SPI2 */
#ifdef BSP_USING_SPI3 #ifdef BSP_USING_SPI3
#ifndef SPI3_BUS_CONFIG #ifndef SPI3_BUS_CONFIG
#define SPI3_BUS_CONFIG \ #define SPI3_BUS_CONFIG \
@ -54,3 +62,5 @@ extern "C" {
#endif #endif
#endif /*__SPI_CONFIG_H__ */ #endif /*__SPI_CONFIG_H__ */
/************************** end of file ******************/

View File

@ -1,3 +1,12 @@
/*
* Copyright (c) 2006-2019, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-06-27 AHTYDHD the first version
*/
#ifndef __UART_CONFIG_H__ #ifndef __UART_CONFIG_H__
#define __UART_CONFIG_H__ #define __UART_CONFIG_H__
@ -6,7 +15,6 @@
extern "C" { extern "C" {
#endif #endif
#if defined(BSP_USING_UART0) #if defined(BSP_USING_UART0)
#ifndef UART0_CONFIG #ifndef UART0_CONFIG
#define UART0_CONFIG \ #define UART0_CONFIG \
@ -63,5 +71,6 @@ extern "C" {
} }
#endif #endif
#endif #endif /* __UART_CONFIG_H__ */
/************************** end of file ******************/

View File

@ -1,19 +1,24 @@
#include "drv_adc.h" /*
* Copyright (c) 2006-2019, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-06-27 AHTYDHD the first version
*/
#include "drv_adc.h"
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include "inc/hw_memmap.h"
#include "hw_memmap.h" #include "driverlib/adc.h"
#include "adc.h" #include "driverlib/sysctl.h"
#include "sysctl.h"
#ifdef RT_USING_ADC #ifdef RT_USING_ADC
#include "adc_config.h" #include "adc_config.h"
#include "tm4c123_config.h" #include "tm4c123_config.h"
#include <string.h>
#define LOG_TAG "drv.adc" #define LOG_TAG "drv.adc"
#include <drv_log.h> #include <drv_log.h>
@ -23,11 +28,9 @@ static struct tm4c123_adc_config adc_config[] =
#ifdef BSP_USING_ADC0 #ifdef BSP_USING_ADC0
ADC0_CONFIG, ADC0_CONFIG,
#endif #endif
#ifdef BSP_USING_ADC1 #ifdef BSP_USING_ADC1
ADC1_CONFIG, ADC1_CONFIG,
#endif #endif
}; };
struct tm4c123_adc struct tm4c123_adc
@ -36,56 +39,46 @@ struct tm4c123_adc
struct rt_adc_device adc_device; struct rt_adc_device adc_device;
}; };
static struct tm4c123_adc adc_obj[sizeof(adc_config) / sizeof(adc_config[0])]={0}; static struct tm4c123_adc adc_obj[sizeof(adc_config) / sizeof(adc_config[0])] = {0};
static rt_err_t tm4c123_adc_enabled(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled) static rt_err_t tm4c123_adc_enabled(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled)
{ {
RT_ASSERT(device != RT_NULL); RT_ASSERT(device != RT_NULL);
//stm32_adc_handler = device->parent.user_data; struct tm4c123_adc_config *config = (struct tm4c123_adc_config *)device->parent.user_data;
if (enabled) if (enabled)
{ {
ADCSequenceEnable(ADC0_BASE, 2); ADCSequenceEnable(config->adcbase, config->sequence);
ADCIntClear(ADC0_BASE, 2); ADCIntClear(config->adcbase, config->sequence);
} }
else else
{ {
ADCSequenceDisable(ADC0_BASE, 2); ADCSequenceDisable(config->adcbase, config->sequence);
} }
return RT_EOK; return RT_EOK;
} }
static rt_err_t tm4c123_get_adc_value(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value) static rt_err_t tm4c123_get_adc_value(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value)
{ {
RT_ASSERT(device != RT_NULL); RT_ASSERT(device != RT_NULL);
RT_ASSERT(value != RT_NULL); RT_ASSERT(value != RT_NULL);
uint32_t pui32ADC0Value[4]={0};
// Trigger the ADC conversion. uint32_t pui32ADC0Value[4] = {0};
// struct tm4c123_adc_config *config = (struct tm4c123_adc_config *)device->parent.user_data;
ADCProcessorTrigger(ADC0_BASE, 2);
// /* Trigger the ADC conversion. */
// Wait for conversion to be completed. ADCProcessorTrigger(config->adcbase, config->sequence);
//
while(!ADCIntStatus(ADC0_BASE, 2, false)) while (!ADCIntStatus(config->adcbase, config->sequence, false))
{ {
} }
// /* Clear the ADC interrupt flag. */
// Clear the ADC interrupt flag. ADCIntClear(config->adcbase, config->sequence);
//
ADCIntClear(ADC0_BASE, 2);
//
// Read ADC Value.
//
ADCSequenceDataGet(ADC0_BASE, 2, pui32ADC0Value);
/* Read ADC Value. */
ADCSequenceDataGet(config->adcbase, config->sequence, pui32ADC0Value);
/* get ADC value */ /* get ADC value */
*value = (rt_uint32_t)pui32ADC0Value[channel]; *value = (rt_uint32_t)pui32ADC0Value[channel];
@ -102,39 +95,39 @@ static const struct rt_adc_ops tm4c123_adc_ops =
static rt_err_t tm4c123_hw_adc_init(struct tm4c123_adc *device) static rt_err_t tm4c123_hw_adc_init(struct tm4c123_adc *device)
{ {
uint32_t adcbase = device->config->adcbase; uint32_t adcbase = device->config->adcbase;
uint32_t sequencenum = device->config->sequence; uint32_t sequencenum = device->config->sequence;
ADCSequenceConfigure(adcbase, sequencenum, ADCSequenceConfigure(adcbase, sequencenum,
device->config->trigermode, 0); device->config->trigermode, 0);
ADCSequenceStepConfigure(ADC0_BASE, 2, 0, ADC_CTL_CH7 ); ADCSequenceStepConfigure(adcbase, sequencenum, 0, ADC_CTL_CH7);
ADCSequenceStepConfigure(ADC0_BASE, 2, 1, ADC_CTL_CH6 | ADC_CTL_IE ); ADCSequenceStepConfigure(adcbase, sequencenum, 1, ADC_CTL_CH6 | ADC_CTL_IE);
ADCSequenceStepConfigure(ADC0_BASE, 2, 2, ADC_CTL_CH5 ); ADCSequenceStepConfigure(adcbase, sequencenum, 2, ADC_CTL_CH5);
//Tell the ADC logic /*Tell the ADC logic
// that this is the last conversion on sequence 3 (ADC_CTL_END). that this is the last conversion on sequence 3 (ADC_CTL_END). */
ADCSequenceStepConfigure(ADC0_BASE, 2, 3, ADC_CTL_CH4 | ADC_CTL_IE | ADCSequenceStepConfigure(adcbase, sequencenum, 3, ADC_CTL_CH4 | ADC_CTL_IE |
ADC_CTL_END); ADC_CTL_END);
return RT_EOK; return RT_EOK;
} }
static int tm4c123_adc_init(void) static int tm4c123_adc_init(void)
{ {
int i = 0; int i = 0;
rt_size_t obj_num = sizeof(adc_obj) / sizeof(struct tm4c123_adc); rt_size_t obj_num = sizeof(adc_obj) / sizeof(struct tm4c123_adc);
rt_err_t result = RT_EOK; rt_err_t result = RT_EOK;
adc_hw_config(); adc_hw_config();
for (i = 0; i < obj_num; i++) for (i = 0; i < obj_num; i++)
{ {
/* ADC init */ /* ADC init */
adc_obj[i].config = &adc_config[i]; adc_obj[i].config = &adc_config[i];
if(tm4c123_hw_adc_init(&adc_obj[i])!= RT_EOK)
if (tm4c123_hw_adc_init(&adc_obj[i]) != RT_EOK)
{ {
LOG_E("%s init failed", adc_obj[i].config->name); LOG_E("%s init failed", adc_obj[i].config->name);
result = -RT_ERROR; result = -RT_ERROR;
@ -143,9 +136,9 @@ static int tm4c123_adc_init(void)
else else
{ {
LOG_D("%s init success", adc_obj[i].config->name); LOG_D("%s init success", adc_obj[i].config->name);
/* register adc device */ /* register adc device */
if (rt_hw_adc_register(&adc_obj[i].adc_device, adc_obj[i].config->name, &tm4c123_adc_ops,RT_NULL) == RT_EOK) if (rt_hw_adc_register(&adc_obj[i].adc_device, adc_obj[i].config->name, &tm4c123_adc_ops, &adc_config[i]) == RT_EOK)
{ {
LOG_D("%s register success", adc_obj[i].config->name); LOG_D("%s register success", adc_obj[i].config->name);
} }
@ -162,3 +155,5 @@ static int tm4c123_adc_init(void)
INIT_APP_EXPORT(tm4c123_adc_init); INIT_APP_EXPORT(tm4c123_adc_init);
#endif /*RT_UING_ADC*/ #endif /*RT_UING_ADC*/
/************************** end of file ******************/

View File

@ -1,6 +1,16 @@
#ifndef _DRV_ADC_H_ #ifndef _DRV_ADC_H_
#define _DRV_ADC_H_ #define _DRV_ADC_H_
/*
* Copyright (c) 2006-2019, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-06-27 AHTYDHD the first version
*/
#include<stdint.h> #include<stdint.h>
#include<rtthread.h> #include<rtthread.h>
#include<rtdevice.h> #include<rtdevice.h>
@ -8,13 +18,14 @@
struct tm4c123_adc_config struct tm4c123_adc_config
{ {
const char *name; const char *name;
uint32_t adcbase; uint32_t adcbase;
uint32_t channel; uint32_t channel;
uint32_t sequence; uint32_t sequence;
uint32_t trigermode; uint32_t trigermode;
uint32_t sequencepriority; uint32_t sequencepriority;
}; };
#endif /*_DRV_ADC_H_*/ #endif /*_DRV_ADC_H_*/
/************************** end of file ******************/

View File

@ -1,31 +0,0 @@
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018-12-5 SummerGift first version
*/
#ifndef __DRV_FLASH_H__
#define __DRV_FLASH_H__
#include <rtthread.h>
#include "rtdevice.h"
#include <rthw.h>
#include <drv_common.h>
#ifdef __cplusplus
extern "C" {
#endif
int stm32_flash_read(rt_uint32_t addr, rt_uint8_t *buf, size_t size);
int stm32_flash_write(rt_uint32_t addr, const rt_uint8_t *buf, size_t size);
int stm32_flash_erase(rt_uint32_t addr, size_t size);
#ifdef __cplusplus
}
#endif
#endif /* __DRV_FLASH_H__ */

View File

@ -1,197 +0,0 @@
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018-12-5 SummerGift first version
*/
#include "board.h"
#ifdef BSP_USING_ON_CHIP_FLASH
#include "drv_config.h"
#include "drv_flash.h"
#if defined(PKG_USING_FAL)
#include "fal.h"
#endif
//#define DRV_DEBUG
#define LOG_TAG "drv.flash"
#include <drv_log.h>
/**
* @brief Gets the page of a given address
* @param Addr: Address of the FLASH Memory
* @retval The page of a given address
*/
static uint32_t GetPage(uint32_t addr)
{
uint32_t page = 0;
page = RT_ALIGN_DOWN(addr, FLASH_PAGE_SIZE);
return page;
}
/**
* Read data from flash.
* @note This operation's units is word.
*
* @param addr flash address
* @param buf buffer to store read data
* @param size read bytes size
*
* @return result
*/
int stm32_flash_read(rt_uint32_t addr, rt_uint8_t *buf, size_t size)
{
size_t i;
if ((addr + size) > STM32_FLASH_END_ADDRESS)
{
LOG_E("read outrange flash size! addr is (0x%p)", (void *)(addr + size));
return -RT_EINVAL;
}
for (i = 0; i < size; i++, buf++, addr++)
{
*buf = *(rt_uint8_t *) addr;
}
return size;
}
/**
* Write data to flash.
* @note This operation's units is word.
* @note This operation must after erase. @see flash_erase.
*
* @param addr flash address
* @param buf the write data buffer
* @param size write bytes size
*
* @return result
*/
int stm32_flash_write(rt_uint32_t addr, const rt_uint8_t *buf, size_t size)
{
rt_err_t result = RT_EOK;
rt_uint32_t end_addr = addr + size;
if (addr % 4 != 0)
{
LOG_E("write addr must be 4-byte alignment");
return -RT_EINVAL;
}
if ((end_addr) > STM32_FLASH_END_ADDRESS)
{
LOG_E("write outrange flash size! addr is (0x%p)", (void *)(addr + size));
return -RT_EINVAL;
}
HAL_FLASH_Unlock();
while (addr < end_addr)
{
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, addr, *((rt_uint32_t *)buf)) == HAL_OK)
{
if (*(rt_uint32_t *)addr != *(rt_uint32_t *)buf)
{
result = -RT_ERROR;
break;
}
addr += 4;
buf += 4;
}
else
{
result = -RT_ERROR;
break;
}
}
HAL_FLASH_Lock();
if (result != RT_EOK)
{
return result;
}
return size;
}
/**
* Erase data on flash.
* @note This operation is irreversible.
* @note This operation's units is different which on many chips.
*
* @param addr flash address
* @param size erase bytes size
*
* @return result
*/
int stm32_flash_erase(rt_uint32_t addr, size_t size)
{
rt_err_t result = RT_EOK;
uint32_t PAGEError = 0;
/*Variable used for Erase procedure*/
FLASH_EraseInitTypeDef EraseInitStruct;
if ((addr + size) > STM32_FLASH_END_ADDRESS)
{
LOG_E("ERROR: erase outrange flash size! addr is (0x%p)\n", (void *)(addr + size));
return -RT_EINVAL;
}
HAL_FLASH_Unlock();
/* Fill EraseInit structure*/
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
EraseInitStruct.PageAddress = GetPage(addr);
EraseInitStruct.NbPages = (size + FLASH_PAGE_SIZE - 1) / FLASH_PAGE_SIZE;
if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK)
{
result = -RT_ERROR;
goto __exit;
}
__exit:
HAL_FLASH_Lock();
if (result != RT_EOK)
{
return result;
}
LOG_D("erase done: addr (0x%p), size %d", (void *)addr, size);
return size;
}
#if defined(PKG_USING_FAL)
static int fal_flash_read(long offset, rt_uint8_t *buf, size_t size);
static int fal_flash_write(long offset, const rt_uint8_t *buf, size_t size);
static int fal_flash_erase(long offset, size_t size);
const struct fal_flash_dev stm32_onchip_flash = { "onchip_flash", STM32_FLASH_START_ADRESS, STM32_FLASH_SIZE, FLASH_PAGE_SIZE, {NULL, fal_flash_read, fal_flash_write, fal_flash_erase} };
static int fal_flash_read(long offset, rt_uint8_t *buf, size_t size)
{
return stm32_flash_read(stm32_onchip_flash.addr + offset, buf, size);
}
static int fal_flash_write(long offset, const rt_uint8_t *buf, size_t size)
{
return stm32_flash_write(stm32_onchip_flash.addr + offset, buf, size);
}
static int fal_flash_erase(long offset, size_t size)
{
return stm32_flash_erase(stm32_onchip_flash.addr + offset, size);
}
#endif
#endif /* BSP_USING_ON_CHIP_FLASH */

View File

@ -1,56 +1,45 @@
#include <rthw.h> /*
#include <rtthread.h> * Copyright (c) 2006-2019, RT-Thread Development Team
#include <rtdevice.h> *
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-06-27 AHTYDHD the first version
*/
#include "drv_gpio.h"
#include <rthw.h>
#include <rtdevice.h>
#include "drv_gpio.h"
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include "hw_memmap.h" #include "inc/hw_memmap.h"
#include "sysctl.h" #include "driverlib/sysctl.h"
#include "gpio.h" #include "driverlib/gpio.h"
#include "pin_map.h" #include "driverlib/pin_map.h"
#include "interrupt.h"
#include "rom_map.h"
#include "drv_gpio.h"
#ifdef RT_USING_PIN #ifdef RT_USING_PIN
static const struct pin_index pins[] = static const struct pin_index pins[] =
{ {
_TM4C_PIN(0 ,F, 0 ), _TM4C_PIN(0, F, 0),
_TM4C_PIN(1 ,F, 1 ), _TM4C_PIN(1, F, 1),
_TM4C_PIN(2 ,F, 2 ), _TM4C_PIN(2, F, 2),
_TM4C_PIN(3 ,F, 3 ), _TM4C_PIN(3, F, 3),
_TM4C_PIN(4 ,F, 4 ) _TM4C_PIN(4, F, 4)
}; };
/* this is pin_irq map, reserved for update */
static struct rt_pin_irq_hdr pin_irq_hdr_tab[] = static struct rt_pin_irq_hdr pin_irq_hdr_tab[] =
{ {
{-1, 0, RT_NULL, RT_NULL}, {-1, 0, RT_NULL, RT_NULL},
{-1, 0, RT_NULL, RT_NULL},
{-1, 0, RT_NULL, RT_NULL},
{-1, 0, RT_NULL, RT_NULL},
{-1, 0, RT_NULL, RT_NULL},
{-1, 0, RT_NULL, RT_NULL},
{-1, 0, RT_NULL, RT_NULL},
{-1, 0, RT_NULL, RT_NULL},
{-1, 0, RT_NULL, RT_NULL},
{-1, 0, RT_NULL, RT_NULL},
{-1, 0, RT_NULL, RT_NULL},
{-1, 0, RT_NULL, RT_NULL},
{-1, 0, RT_NULL, RT_NULL},
{-1, 0, RT_NULL, RT_NULL},
{-1, 0, RT_NULL, RT_NULL},
{-1, 0, RT_NULL, RT_NULL}
}; };
static uint32_t pin_irq_enable_mask = 0;
static uint32_t pin_irq_enable_mask=0;
#define ITEM_NUM(items) sizeof(items) / sizeof(items[0]) #define ITEM_NUM(items) sizeof(items) / sizeof(items[0])
static const struct pin_index *get_pin(uint8_t pin) static const struct pin_index *get_pin(uint8_t pin)
{ {
const struct pin_index *index; const struct pin_index *index;
@ -68,7 +57,6 @@ static const struct pin_index *get_pin(uint8_t pin)
return index; return index;
}; };
static void tm4c123_pin_mode(rt_device_t dev, rt_base_t pin, rt_base_t mode) static void tm4c123_pin_mode(rt_device_t dev, rt_base_t pin, rt_base_t mode)
{ {
const struct pin_index *index; const struct pin_index *index;
@ -77,104 +65,82 @@ static void tm4c123_pin_mode(rt_device_t dev, rt_base_t pin, rt_base_t mode)
{ {
return; return;
} }
if(mode == PIN_MODE_INPUT) if (mode == PIN_MODE_INPUT)
{ {
GPIOPinTypeGPIOInput(index ->gpioBaseAddress, index->pin); GPIOPinTypeGPIOInput(index ->gpioBaseAddress, index->pin);
} }
else if(mode == PIN_MODE_OUTPUT) else if (mode == PIN_MODE_OUTPUT)
{ {
GPIOPinTypeGPIOOutput(index->gpioBaseAddress, index->pin); GPIOPinTypeGPIOOutput(index->gpioBaseAddress, index->pin);
} }
else if(mode == PIN_MODE_INPUT_PULLUP) else if (mode == PIN_MODE_INPUT_PULLUP)
{ {
GPIODirModeSet(index->gpioBaseAddress, index->pin, GPIO_DIR_MODE_IN);
// GPIOPadConfigSet(index->gpioBaseAddress, index->pin, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);
// Make the pin(s) be inputs. }
// else if (mode == PIN_MODE_INPUT_PULLDOWN)
GPIODirModeSet(index->gpioBaseAddress, index->pin, GPIO_DIR_MODE_IN); {
// GPIODirModeSet(index->gpioBaseAddress, index->pin, GPIO_DIR_MODE_IN);
// Set the pad(s) for standard pullup operation. GPIOPadConfigSet(index->gpioBaseAddress, index->pin, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPD);
// }
GPIOPadConfigSet(index->gpioBaseAddress, index->pin, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU); else if (mode == PIN_MODE_OUTPUT_OD)
} {
else if(mode == PIN_MODE_INPUT_PULLDOWN) GPIOPadConfigSet(index->gpioBaseAddress, index->pin, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_OD);
{ GPIODirModeSet(index->gpioBaseAddress, index->pin, GPIO_DIR_MODE_OUT);
// }
// Make the pin(s) be inputs.
//
GPIODirModeSet(index->gpioBaseAddress, index->pin, GPIO_DIR_MODE_IN);
//
// Set the pad(s) for standard pulldown operation.
//
GPIOPadConfigSet(index->gpioBaseAddress, index->pin, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPD);
}
else if(mode == PIN_MODE_OUTPUT_OD)
{
//
// Set the pad(s) for standard push-pull operation.
GPIOPadConfigSet(index->gpioBaseAddress, index->pin, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_OD);
//
// Make the pin(s) be outputs.
GPIODirModeSet(index->gpioBaseAddress, index->pin, GPIO_DIR_MODE_OUT);
}
} }
static void tm4c123_pin_write(rt_device_t dev, rt_base_t pin, rt_base_t ui8Val) static void tm4c123_pin_write(rt_device_t dev, rt_base_t pin, rt_base_t ui8Val)
{ {
const struct pin_index *index; const struct pin_index *index;
index = get_pin(pin); index = get_pin(pin);
if( index == RT_NULL) if (index == RT_NULL)
{ {
return; return;
} }
if(ui8Val) if (ui8Val)
{ {
GPIOPinWrite(index ->gpioBaseAddress, index->pin, index->pin ); GPIOPinWrite(index ->gpioBaseAddress, index->pin, index->pin);
} }
else else
{ {
GPIOPinWrite(index ->gpioBaseAddress, index->pin, 0 ); GPIOPinWrite(index ->gpioBaseAddress, index->pin, 0);
} }
} }
static int tm4c123_pin_read(rt_device_t dev, rt_base_t pin) static int tm4c123_pin_read(rt_device_t dev, rt_base_t pin)
{ {
const struct pin_index *index; const struct pin_index *index;
int value = 0; int value = 0;
index = get_pin(pin); index = get_pin(pin);
if( index == RT_NULL) if (index == RT_NULL)
{ {
return value; return value;
} }
value = GPIOPinRead(index ->gpioBaseAddress , index ->pin ); value = GPIOPinRead(index ->gpioBaseAddress, index ->pin);
return value; return value;
} }
static rt_err_t tm4c123_pin_attach_irq(rt_device_t device, rt_int32_t pin, rt_uint32_t mode, void (*hdr)(void *args), void *args ) static rt_err_t tm4c123_pin_attach_irq(rt_device_t device, rt_int32_t pin, rt_uint32_t mode, void (*hdr)(void *args), void *args)
{ {
//const struct pin_index *index; /* this is interface for pin_irq, reserved for update. */
return RT_EOK; return RT_EOK;
} }
static rt_err_t tm4c123_pin_dettach_irq(rt_device_t device, rt_int32_t pin) static rt_err_t tm4c123_pin_dettach_irq(rt_device_t device, rt_int32_t pin)
{ {
//const struct pin_index *index; /* this is interface for pin_irq, reserved for update. */
return RT_EOK; return RT_EOK;
} }
static rt_err_t tm4c123_pin_irq_enable(rt_device_t device, rt_base_t pin, static rt_err_t tm4c123_pin_irq_enable(rt_device_t device, rt_base_t pin,
rt_uint32_t enabled) rt_uint32_t enabled)
{ {
//const struct pin_index *index; /* this is interface for pin_irq_enable, reserved for update. */
//const struct pin_irq_map *irqmap;
return RT_EOK; return RT_EOK;
} }
@ -197,3 +163,5 @@ int rt_hw_pin_init(void)
INIT_BOARD_EXPORT(rt_hw_pin_init); INIT_BOARD_EXPORT(rt_hw_pin_init);
#endif /*RT_USING_PIN*/ #endif /*RT_USING_PIN*/
/************************** end of file ******************/

View File

@ -1,19 +1,28 @@
/*
* Copyright (c) 2006-2019, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-06-27 AHTYDHD the first version
*/
#ifndef _DRV_GPIO_H_ #ifndef _DRV_GPIO_H_
#define _DRV_GPIO_H_ #define _DRV_GPIO_H_
#include <stdint.h> #include <stdint.h>
#include <rtthread.h> #include <rtthread.h>
#define _TM4C_PIN(index, gpioport, gpio_index) \ #define _TM4C_PIN(index, gpioport, gpio_index) \
{ \ { \
index, GPIO_PORT##gpioport##_BASE, GPIO_PIN_##gpio_index \ index, GPIO_PORT##gpioport##_BASE, GPIO_PIN_##gpio_index \
} }
#define _TM4C_PIN_RESERVE \ #define _TM4C_PIN_RESERVE \
{ \ { \
-1, 0, 0 \ -1, 0, 0 \
} }
/* TM4C123 GPIO driver*/ /* TM4C123 GPIO driver*/
struct pin_index struct pin_index
@ -27,3 +36,4 @@ extern int rt_hw_pin_init(void);
#endif /*_DRV_GPIO_H_*/ #endif /*_DRV_GPIO_H_*/
/************************** end of file ******************/

View File

@ -13,15 +13,15 @@
*/ */
#ifndef LOG_TAG #ifndef LOG_TAG
#define DBG_TAG "drv" #define DBG_TAG "drv"
#else #else
#define DBG_TAG LOG_TAG #define DBG_TAG LOG_TAG
#endif /* LOG_TAG */ #endif /* LOG_TAG */
#ifdef DRV_DEBUG #ifdef DRV_DEBUG
#define DBG_LVL DBG_LOG #define DBG_LVL DBG_LOG
#else #else
#define DBG_LVL DBG_INFO #define DBG_LVL DBG_INFO
#endif /* DRV_DEBUG */ #endif /* DRV_DEBUG */
#include <rtdbg.h> #include <rtdbg.h>

View File

@ -1,22 +1,28 @@
/*
* Copyright (c) 2006-2019, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-06-27 AHTYDHD the first version
*/
#include "drv_pwm.h" #include "drv_pwm.h"
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include "inc/hw_memmap.h"
#include "hw_memmap.h" #include "driverlib/pwm.h"
#include "pwm.h" #include "driverlib/sysctl.h"
#include "sysctl.h"
#ifdef RT_USING_PWM #ifdef RT_USING_PWM
#include "pwm_config.h" #include "pwm_config.h"
#include "tm4c123_config.h" #include "tm4c123_config.h"
#include <string.h>
#define LOG_TAG "drv.pwm" #define LOG_TAG "drv.pwm"
#include <drv_log.h> #include <drv_log.h>
enum enum
{ {
#ifdef BSP_USING_PWM0 #ifdef BSP_USING_PWM0
@ -43,7 +49,6 @@ enum
#ifdef BSP_USING_PWM7 #ifdef BSP_USING_PWM7
PWM7_INDEX, PWM7_INDEX,
#endif #endif
}; };
static struct tm4c123_pwm_config pwm_config[] = static struct tm4c123_pwm_config pwm_config[] =
@ -51,31 +56,24 @@ static struct tm4c123_pwm_config pwm_config[] =
#ifdef BSP_USING_PWM0 #ifdef BSP_USING_PWM0
PWM0_CONFIG, PWM0_CONFIG,
#endif #endif
#ifdef BSP_USING_PWM1 #ifdef BSP_USING_PWM1
PWM1_CONFIG, PWM1_CONFIG,
#endif #endif
#ifdef BSP_USING_PWM2 #ifdef BSP_USING_PWM2
PWM2_CONFIG, PWM2_CONFIG,
#endif #endif
#ifdef BSP_USING_PWM3 #ifdef BSP_USING_PWM3
PWM3_CONFIG, PWM3_CONFIG,
#endif #endif
#ifdef BSP_USING_PWM4 #ifdef BSP_USING_PWM4
PWM4_CONFIG, PWM4_CONFIG,
#endif #endif
#ifdef BSP_USING_PWM5 #ifdef BSP_USING_PWM5
PWM5_CONFIG, PWM5_CONFIG,
#endif #endif
#ifdef BSP_USING_PWM6 #ifdef BSP_USING_PWM6
PWM6_CONFIG, PWM6_CONFIG,
#endif #endif
#ifdef BSP_USING_PWM7 #ifdef BSP_USING_PWM7
PWM7_CONFIG, PWM7_CONFIG,
#endif #endif
@ -83,254 +81,245 @@ static struct tm4c123_pwm_config pwm_config[] =
static struct tm4c123_pwm pwm_obj[sizeof(pwm_config) / sizeof(pwm_config[0])] = {0}; static struct tm4c123_pwm pwm_obj[sizeof(pwm_config) / sizeof(pwm_config[0])] = {0};
static rt_err_t tm4c123_pwm_control(struct rt_device_pwm *device, int cmd, void *arg); static rt_err_t tm4c123_pwm_control(struct rt_device_pwm *device, int cmd, void *arg);
static struct rt_pwm_ops drv_ops = static struct rt_pwm_ops drv_ops =
{ {
tm4c123_pwm_control tm4c123_pwm_control
}; };
static rt_err_t drv_pwm_enable(char* name, struct rt_pwm_configuration *configuration, rt_bool_t enable) static rt_err_t drv_pwm_enable(char *name, struct rt_pwm_configuration *configuration, rt_bool_t enable)
{ {
/* Converts the channel number to the channel number of Hal library */
int num = name[3] - 0x30;
int num = name[3]-0x30;
if (!enable) if (!enable)
{ {
if( num<=3 ) if (num <= 3)
{ {
PWMOutputState(PWM0_BASE, PWM_OUT_0_BIT<<(num*2+(configuration->channel-1)), false); /* Converts the channel number to the channel number of Hal library */
} PWMOutputState(PWM0_BASE, PWM_OUT_0_BIT << (num * 2 + (configuration->channel - 1)), false);
}
else else
{ {
PWMOutputState(PWM1_BASE, PWM_OUT_0_BIT<<((num%4)*2+(configuration->channel-1)), false); PWMOutputState(PWM1_BASE, PWM_OUT_0_BIT << ((num % 4) * 2 + (configuration->channel - 1)), false);
} }
} }
else else
{ {
if( num<=3 ) if (num <= 3)
{ {
PWMOutputState(PWM0_BASE, PWM_OUT_0_BIT<<(num*2+(configuration->channel-1)), true); PWMOutputState(PWM0_BASE, PWM_OUT_0_BIT << (num * 2 + (configuration->channel - 1)), true);
} }
else else
{ {
PWMOutputState(PWM1_BASE, PWM_OUT_0_BIT<<((num%4)*2+(configuration->channel-1)), true); PWMOutputState(PWM1_BASE, PWM_OUT_0_BIT << ((num % 4) * 2 + (configuration->channel - 1)), true);
} }
} }
return RT_EOK; return RT_EOK;
} }
static rt_err_t drv_pwm_get(char* name, struct rt_pwm_configuration *configuration) static rt_err_t drv_pwm_get(char *name, struct rt_pwm_configuration *configuration)
{ {
switch(name[3]) switch (name[3])
{ {
case '0': case '0':
configuration->period = PWMGenPeriodGet(PWM0_BASE, PWM_GEN_0); configuration->period = PWMGenPeriodGet(PWM0_BASE, PWM_GEN_0);
configuration->pulse= PWMPulseWidthGet(PWM0_BASE, PWM_OUT_0+(uint32_t)(configuration->channel-1)); configuration->pulse = PWMPulseWidthGet(PWM0_BASE, PWM_OUT_0 + (uint32_t)(configuration->channel - 1));
break; break;
case '1': case '1':
configuration->period = PWMGenPeriodGet(PWM0_BASE, PWM_GEN_1); configuration->period = PWMGenPeriodGet(PWM0_BASE, PWM_GEN_1);
configuration->pulse= PWMPulseWidthGet(PWM0_BASE, PWM_OUT_2+(uint32_t)(configuration->channel-1)); configuration->pulse = PWMPulseWidthGet(PWM0_BASE, PWM_OUT_2 + (uint32_t)(configuration->channel - 1));
break; break;
case '2': case '2':
configuration->period = PWMGenPeriodGet(PWM0_BASE, PWM_GEN_2); configuration->period = PWMGenPeriodGet(PWM0_BASE, PWM_GEN_2);
configuration->pulse= PWMPulseWidthGet(PWM0_BASE, PWM_OUT_4+(uint32_t)(configuration->channel-1)); configuration->pulse = PWMPulseWidthGet(PWM0_BASE, PWM_OUT_4 + (uint32_t)(configuration->channel - 1));
break; break;
case '3': case '3':
configuration->period = PWMGenPeriodGet(PWM0_BASE, PWM_GEN_3); configuration->period = PWMGenPeriodGet(PWM0_BASE, PWM_GEN_3);
configuration->pulse= PWMPulseWidthGet(PWM0_BASE, PWM_OUT_6+(uint32_t)(configuration->channel-1)); configuration->pulse = PWMPulseWidthGet(PWM0_BASE, PWM_OUT_6 + (uint32_t)(configuration->channel - 1));
break; break;
case '4': case '4':
configuration->period = PWMGenPeriodGet(PWM1_BASE, PWM_GEN_0); configuration->period = PWMGenPeriodGet(PWM1_BASE, PWM_GEN_0);
configuration->pulse= PWMPulseWidthGet(PWM1_BASE, PWM_OUT_0+(uint32_t)(configuration->channel-1)); configuration->pulse = PWMPulseWidthGet(PWM1_BASE, PWM_OUT_0 + (uint32_t)(configuration->channel - 1));
break; break;
case '5': case '5':
configuration->period = PWMGenPeriodGet(PWM1_BASE, PWM_GEN_1); configuration->period = PWMGenPeriodGet(PWM1_BASE, PWM_GEN_1);
configuration->pulse= PWMPulseWidthGet(PWM1_BASE, PWM_OUT_2+(uint32_t)(configuration->channel-1)); configuration->pulse = PWMPulseWidthGet(PWM1_BASE, PWM_OUT_2 + (uint32_t)(configuration->channel - 1));
break; break;
case '6': case '6':
configuration->period = PWMGenPeriodGet(PWM1_BASE, PWM_GEN_2); configuration->period = PWMGenPeriodGet(PWM1_BASE, PWM_GEN_2);
configuration->pulse= PWMPulseWidthGet(PWM1_BASE, PWM_OUT_4+(uint32_t)(configuration->channel-1)); configuration->pulse = PWMPulseWidthGet(PWM1_BASE, PWM_OUT_4 + (uint32_t)(configuration->channel - 1));
break; break;
case '7': case '7':
configuration->period = PWMGenPeriodGet(PWM1_BASE, PWM_GEN_3); configuration->period = PWMGenPeriodGet(PWM1_BASE, PWM_GEN_3);
configuration->pulse= PWMPulseWidthGet(PWM1_BASE, PWM_OUT_6+(uint32_t)(configuration->channel-1)); configuration->pulse = PWMPulseWidthGet(PWM1_BASE, PWM_OUT_6 + (uint32_t)(configuration->channel - 1));
break; break;
default: default:
break; break;
} }
return RT_EOK; return RT_EOK;
} }
static rt_err_t drv_pwm_set(char *name, struct rt_pwm_configuration *configuration) static rt_err_t drv_pwm_set(char *name, struct rt_pwm_configuration *configuration)
{ {
uint32_t sysPwmClock = SysCtlPWMClockGet();
switch(name[3])
{
case '0':
PWMGenPeriodSet(PWM0_BASE, PWM_GEN_0, configuration->period/1000*(sysPwmClock/1000000)); // t(s)/(1/f) = ticks ns/1000/1000000
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0+(uint32_t)(configuration->channel-1),configuration->pulse/1000*(sysPwmClock/1000000));
PWMGenEnable(PWM0_BASE, PWM_GEN_0);
break;
case '1':
PWMGenPeriodSet(PWM0_BASE, PWM_GEN_1, configuration->period/1000*(sysPwmClock/1000000));
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0+(uint32_t)(configuration->channel-1),configuration->pulse/1000*(sysPwmClock/1000000));
PWMGenEnable(PWM0_BASE, PWM_GEN_1);
break;
case '2':
PWMGenPeriodSet(PWM0_BASE, PWM_GEN_2, configuration->period/1000*(sysPwmClock/1000000));
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0+(uint32_t)(configuration->channel-1),configuration->pulse/1000*(sysPwmClock/1000000));
PWMGenEnable(PWM0_BASE, PWM_GEN_2);
break;
case '3':
PWMGenPeriodSet(PWM0_BASE, PWM_GEN_3, configuration->period/1000*(sysPwmClock/1000000));
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0+(uint32_t)(configuration->channel-1),configuration->pulse/1000*(sysPwmClock/1000000));
PWMGenEnable(PWM0_BASE, PWM_GEN_3);
break;
case '4':
PWMGenPeriodSet(PWM1_BASE, PWM_GEN_0, configuration->period/1000*(sysPwmClock/1000000));
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_0+(uint32_t)(configuration->channel-1),configuration->pulse/1000*(sysPwmClock/1000000));
PWMGenEnable(PWM1_BASE, PWM_GEN_0);
break;
case '5':
PWMGenPeriodSet(PWM1_BASE, PWM_GEN_1, configuration->period/1000*(sysPwmClock/1000000));
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_2+(uint32_t)(configuration->channel-1),configuration->pulse/1000*(sysPwmClock/1000000));
PWMGenEnable(PWM1_BASE, PWM_GEN_1);
break;
case '6':
PWMGenPeriodSet(PWM1_BASE, PWM_GEN_2, configuration->period/1000*(sysPwmClock/1000000));
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_4+(uint32_t)(configuration->channel-1),configuration->pulse/1000*(sysPwmClock/1000000));
PWMGenEnable(PWM1_BASE, PWM_GEN_2);
break;
case '7':
PWMGenPeriodSet(PWM1_BASE, PWM_GEN_3, configuration->period/1000*(sysPwmClock/1000000));
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_6+(uint32_t)(configuration->channel-1),configuration->pulse/1000*(sysPwmClock/1000000));
PWMGenEnable(PWM1_BASE, PWM_GEN_3);
break;
default:
break;
}
return RT_EOK; uint32_t sysPwmClock = SysCtlPWMClockGet();
switch (name[3])
{
case '0':
PWMGenPeriodSet(PWM0_BASE, PWM_GEN_0, configuration->period / 1000 * (sysPwmClock / 1000000)); // t(s)/(1/f) = ticks ns/1000/1000000
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0 + (uint32_t)(configuration->channel - 1), configuration->pulse / 1000 * (sysPwmClock / 1000000));
PWMGenEnable(PWM0_BASE, PWM_GEN_0);
break;
case '1':
PWMGenPeriodSet(PWM0_BASE, PWM_GEN_1, configuration->period / 1000 * (sysPwmClock / 1000000));
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0 + (uint32_t)(configuration->channel - 1), configuration->pulse / 1000 * (sysPwmClock / 1000000));
PWMGenEnable(PWM0_BASE, PWM_GEN_1);
break;
case '2':
PWMGenPeriodSet(PWM0_BASE, PWM_GEN_2, configuration->period / 1000 * (sysPwmClock / 1000000));
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0 + (uint32_t)(configuration->channel - 1), configuration->pulse / 1000 * (sysPwmClock / 1000000));
PWMGenEnable(PWM0_BASE, PWM_GEN_2);
break;
case '3':
PWMGenPeriodSet(PWM0_BASE, PWM_GEN_3, configuration->period / 1000 * (sysPwmClock / 1000000));
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0 + (uint32_t)(configuration->channel - 1), configuration->pulse / 1000 * (sysPwmClock / 1000000));
PWMGenEnable(PWM0_BASE, PWM_GEN_3);
break;
case '4':
PWMGenPeriodSet(PWM1_BASE, PWM_GEN_0, configuration->period / 1000 * (sysPwmClock / 1000000));
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_0 + (uint32_t)(configuration->channel - 1), configuration->pulse / 1000 * (sysPwmClock / 1000000));
PWMGenEnable(PWM1_BASE, PWM_GEN_0);
break;
case '5':
PWMGenPeriodSet(PWM1_BASE, PWM_GEN_1, configuration->period / 1000 * (sysPwmClock / 1000000));
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_2 + (uint32_t)(configuration->channel - 1), configuration->pulse / 1000 * (sysPwmClock / 1000000));
PWMGenEnable(PWM1_BASE, PWM_GEN_1);
break;
case '6':
PWMGenPeriodSet(PWM1_BASE, PWM_GEN_2, configuration->period / 1000 * (sysPwmClock / 1000000));
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_4 + (uint32_t)(configuration->channel - 1), configuration->pulse / 1000 * (sysPwmClock / 1000000));
PWMGenEnable(PWM1_BASE, PWM_GEN_2);
break;
case '7':
PWMGenPeriodSet(PWM1_BASE, PWM_GEN_3, configuration->period / 1000 * (sysPwmClock / 1000000));
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_6 + (uint32_t)(configuration->channel - 1), configuration->pulse / 1000 * (sysPwmClock / 1000000));
PWMGenEnable(PWM1_BASE, PWM_GEN_3);
break;
default:
break;
}
return RT_EOK;
} }
static rt_err_t tm4c123_pwm_control(struct rt_device_pwm *device, int cmd, void *arg) static rt_err_t tm4c123_pwm_control(struct rt_device_pwm *device, int cmd, void *arg)
{ {
struct rt_pwm_configuration *configuration = (struct rt_pwm_configuration *)arg; struct rt_pwm_configuration *configuration = (struct rt_pwm_configuration *)arg;
switch (cmd) switch (cmd)
{ {
case PWM_CMD_ENABLE: case PWM_CMD_ENABLE:
return drv_pwm_enable(device->parent.parent.name, configuration, RT_TRUE); return drv_pwm_enable(device->parent.parent.name, configuration, RT_TRUE);
case PWM_CMD_DISABLE: case PWM_CMD_DISABLE:
return drv_pwm_enable(device->parent.parent.name, configuration, RT_FALSE); return drv_pwm_enable(device->parent.parent.name, configuration, RT_FALSE);
case PWM_CMD_SET: case PWM_CMD_SET:
return drv_pwm_set(device->parent.parent.name, configuration); return drv_pwm_set(device->parent.parent.name, configuration);
case PWM_CMD_GET: case PWM_CMD_GET:
return drv_pwm_get(device->parent.parent.name, configuration); return drv_pwm_get(device->parent.parent.name, configuration);
default: default:
return RT_EINVAL; return RT_EINVAL;
} }
} }
static rt_err_t tm4c123_hw_pwm_init(struct tm4c123_pwm *device) static rt_err_t tm4c123_hw_pwm_init(struct tm4c123_pwm *device)
{ {
rt_err_t result = RT_EOK; rt_err_t result = RT_EOK;
RT_ASSERT(device != RT_NULL); RT_ASSERT(device != RT_NULL);
pwm_hw_config();
pwm_hw_config(); switch (device->config->name[3])
{
switch( device->config->name[3]) case '0':
{ PWMGenConfigure(PWM0_BASE, PWM_GEN_0, device->config->counterMode |
case '0': device->config->syncMode);
PWMGenConfigure(PWM0_BASE, PWM_GEN_0, device->config->counterMode | break;
device->config->syncMode); case '1':
break; PWMGenConfigure(PWM0_BASE, PWM_GEN_1, device->config->counterMode |
case '1': device->config->syncMode);
PWMGenConfigure(PWM0_BASE, PWM_GEN_1, device->config->counterMode | break;
device->config->syncMode); case '2':
break; PWMGenConfigure(PWM0_BASE, PWM_GEN_2, device->config->counterMode |
case '2': device->config->syncMode);
PWMGenConfigure(PWM0_BASE, PWM_GEN_2, device->config->counterMode | break;
device->config->syncMode); case '3':
break; PWMGenConfigure(PWM0_BASE, PWM_GEN_3, device->config->counterMode |
case '3': device->config->syncMode);
PWMGenConfigure(PWM0_BASE, PWM_GEN_3, device->config->counterMode | break;
device->config->syncMode); case '4':
break; PWMGenConfigure(PWM1_BASE, PWM_GEN_0, device->config->counterMode |
case '4': device->config->syncMode);
PWMGenConfigure(PWM1_BASE, PWM_GEN_0, device->config->counterMode | break;
device->config->syncMode); case '5':
break; PWMGenConfigure(PWM1_BASE, PWM_GEN_1, device->config->counterMode |
case '5': device->config->syncMode);
PWMGenConfigure(PWM1_BASE, PWM_GEN_1, device->config->counterMode | break;
device->config->syncMode); case '6':
break; PWMGenConfigure(PWM1_BASE, PWM_GEN_2, device->config->counterMode |
case '6': device->config->syncMode);
PWMGenConfigure(PWM1_BASE, PWM_GEN_2, device->config->counterMode | break;
device->config->syncMode); case '7':
break; PWMGenConfigure(PWM1_BASE, PWM_GEN_3, device->config->counterMode |
case '7': device->config->syncMode);
PWMGenConfigure(PWM1_BASE, PWM_GEN_3, device->config->counterMode | break;
device->config->syncMode); default:
break; LOG_E("%s PWMGenConfigure failed", device->config->name);
default: result = -RT_ERROR;
LOG_E("%s PWMGenConfigure failed", device->config->name); return result;
result = -RT_ERROR; }
return result;
}
return result; return result;
} }
int rt_hw_pwm_init(void) int rt_hw_pwm_init(void)
{ {
int i = 0; int i = 0;
rt_size_t obj_num = sizeof(pwm_obj) / sizeof(struct tm4c123_pwm); rt_size_t obj_num = sizeof(pwm_obj) / sizeof(struct tm4c123_pwm);
rt_err_t result = RT_EOK; rt_err_t result = RT_EOK;
for (i = 0 ; i < obj_num; i++)
for(i=0 ; i< obj_num;i++) {
{ pwm_obj[i].config = &pwm_config[i];
pwm_obj[i].pwm_device.ops = &drv_ops;
pwm_obj[i].config = &pwm_config[i]; /*pwm_init*/
pwm_obj[i].pwm_device.ops = &drv_ops; if (tm4c123_hw_pwm_init(&pwm_obj[i]) != RT_EOK)
/*pwm_init*/ {
if(tm4c123_hw_pwm_init(&pwm_obj[i])!= RT_EOK) LOG_E("%s init failed", pwm_obj[i].config->name);
{ result = -RT_ERROR;
LOG_E("%s init failed", pwm_obj[i].config->name); return result;
}
else
{
LOG_D("%s init success", pwm_obj[i].config->name);
/* register pwm device */
if (rt_device_pwm_register(&pwm_obj[i].pwm_device, pwm_obj[i].config->name, &drv_ops, RT_NULL) == RT_EOK)
{
LOG_D("%s register success", pwm_obj[i].config->name);
}
else
{
LOG_E("%s register failed", pwm_obj[i].config->name);
result = -RT_ERROR; result = -RT_ERROR;
return result; }
} }
else }
{ return result;
LOG_D("%s init success", pwm_obj[i].config->name);
/* register pwm device */
if (rt_device_pwm_register(&pwm_obj[i].pwm_device, pwm_obj[i].config->name, &drv_ops,RT_NULL) == RT_EOK)
{
LOG_D("%s register success", pwm_obj[i].config->name);
}
else
{
LOG_E("%s register failed", pwm_obj[i].config->name);
result = -RT_ERROR;
}
}
}
return result;
} }
#endif #endif /* RT_USING_PWM */
/************************** end of file ******************/

View File

@ -1,28 +1,36 @@
#ifndef _DRV_PWM_H_ /*
#define _DRV_PWM_H_ * Copyright (c) 2006-2019, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-06-27 AHTYDHD the first version
*/
#ifndef __DRV_PWM_H__
#define __DRV_PWM_H__
#include<rtdevice.h> #include<rtdevice.h>
#include<rthw.h> #include<rthw.h>
struct tm4c123_pwm struct tm4c123_pwm
{ {
struct tm4c123_pwm_config *config; struct tm4c123_pwm_config *config;
struct rt_device_pwm pwm_device; struct rt_device_pwm pwm_device;
}; };
/* tm4c123 config class */ /* tm4c123 config class */
struct tm4c123_pwm_config struct tm4c123_pwm_config
{ {
rt_uint8_t channel; rt_uint8_t channel;
char *name; char *name;
uint32_t counterMode; uint32_t counterMode;
uint32_t syncMode; uint32_t syncMode;
}; };
int rt_hw_pwm_init(void); int rt_hw_pwm_init(void);
#endif /*__DRV_PWM_H__*/
#endif /*_DRV_PWM_H_*/ /************************** end of file ******************/

View File

@ -1,17 +1,24 @@
#include "drv_spi.h" /*
* Copyright (c) 2006-2019, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-06-27 AHTYDHD the first version
*/
#include "drv_spi.h"
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include "inc/hw_memmap.h"
#include "hw_memmap.h" #include "driverlib/ssi.h"
#include "ssi.h" #include "driverlib/gpio.h"
#include "gpio.h" #include "driverlib/sysctl.h"
#include "sysctl.h"
#ifdef RT_USING_SPI #ifdef RT_USING_SPI
#if defined(BSP_USING_SPI0) || defined(BSP_USING_SPI1) || defined(BSP_USING_SPI2) || defined(BSP_USING_SPI3) #if defined(BSP_USING_SPI0) || defined(BSP_USING_SPI1) || defined(BSP_USING_SPI2) || defined(BSP_USING_SPI3)
/* this driver can be disabled at menuconfig → RT-Thread Components → Device Drivers */ /* this driver can be disabled at menuconfig → RT-Thread Components → Device Drivers */
#include "tm4c123_config.h" #include "tm4c123_config.h"
#include "spi_config.h" #include "spi_config.h"
@ -21,7 +28,6 @@
#define LOG_TAG "drv.spi" #define LOG_TAG "drv.spi"
#include <drv_log.h> #include <drv_log.h>
enum enum
{ {
#ifdef BSP_USING_SPI0 #ifdef BSP_USING_SPI0
@ -64,11 +70,11 @@ static rt_err_t tm4c123_spi_configure(struct tm4c123_spi *spi_drv, struct rt_spi
{ {
RT_ASSERT(spi_drv != RT_NULL); RT_ASSERT(spi_drv != RT_NULL);
RT_ASSERT(cfg != RT_NULL); RT_ASSERT(cfg != RT_NULL);
uint32_t ui32Protocol,ui32Mode; uint32_t ui32Protocol, ui32Mode;
uint32_t ui32BitRate = (uint32_t)cfg->max_hz; uint32_t ui32BitRate = (uint32_t)cfg->max_hz;
uint32_t ui32DataWidth = (uint32_t)cfg->data_width; uint32_t ui32DataWidth = (uint32_t)cfg->data_width;
uint32_t pui32DataRx[1]; uint32_t pui32DataRx[1];
rt_uint8_t ui8Protocol = 0; rt_uint8_t ui8Protocol = 0;
if (cfg->mode & RT_SPI_SLAVE) if (cfg->mode & RT_SPI_SLAVE)
@ -96,36 +102,35 @@ static rt_err_t tm4c123_spi_configure(struct tm4c123_spi *spi_drv, struct rt_spi
else else
{ {
ui8Protocol += 0; ui8Protocol += 0;
} }
switch( ui8Protocol) switch (ui8Protocol)
{ {
case 0: case 0:
ui32Protocol = SSI_FRF_MOTO_MODE_0; ui32Protocol = SSI_FRF_MOTO_MODE_0;
break; break;
case 1: case 1:
ui32Protocol = SSI_FRF_MOTO_MODE_1; ui32Protocol = SSI_FRF_MOTO_MODE_1;
break; break;
case 2: case 2:
ui32Protocol = SSI_FRF_MOTO_MODE_2; ui32Protocol = SSI_FRF_MOTO_MODE_2;
break; break;
case 3: case 3:
ui32Protocol = SSI_FRF_MOTO_MODE_3; ui32Protocol = SSI_FRF_MOTO_MODE_3;
break; break;
default: default:
ui32Protocol = SSI_FRF_MOTO_MODE_0; ui32Protocol = SSI_FRF_MOTO_MODE_0;
break; break;
} }
SSIConfigSetExpClk(spi_drv->config->base, SysCtlClockGet(), ui32Protocol, SSIConfigSetExpClk(spi_drv->config->base, SysCtlClockGet(), ui32Protocol,
ui32Mode, ui32BitRate, ui32DataWidth); ui32Mode, ui32BitRate, ui32DataWidth);
LOG_D("ssiclk freq: %d, SPI limiting freq: %d",SysCtlClockGet(),cfg->max_hz); LOG_D("ssiclk freq: %d, SPI limiting freq: %d", SysCtlClockGet(), cfg->max_hz);
/* DMA configuration */
SSIEnable(spi_drv->config->base); SSIEnable(spi_drv->config->base);
while(SSIDataGetNonBlocking(SSI0_BASE, &pui32DataRx[0])) while (SSIDataGetNonBlocking(SSI0_BASE, &pui32DataRx[0]))
{ {
} }
@ -140,9 +145,9 @@ static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *
rt_size_t message_length; rt_size_t message_length;
rt_uint8_t *recv_buf; rt_uint8_t *recv_buf;
const rt_uint8_t *send_buf; const rt_uint8_t *send_buf;
uint32_t ReadData=0; uint32_t ReadData = 0;
int i = 0; int i = 0;
RT_ASSERT(device != RT_NULL); RT_ASSERT(device != RT_NULL);
RT_ASSERT(device->bus != RT_NULL); RT_ASSERT(device->bus != RT_NULL);
RT_ASSERT(device->bus->parent.user_data != RT_NULL); RT_ASSERT(device->bus->parent.user_data != RT_NULL);
@ -164,27 +169,27 @@ static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *
message_length = message->length; message_length = message->length;
recv_buf = message->recv_buf; recv_buf = message->recv_buf;
send_buf = message->send_buf; send_buf = message->send_buf;
if (message->send_buf && message->recv_buf) if (message->send_buf && message->recv_buf)
{ {
for(i=0; i< message_length; i++) for (i = 0; i < message_length; i++)
{ {
SSIDataPut(spi_drv->config->base, (uint32_t)send_buf[i]); SSIDataPut(spi_drv->config->base, (uint32_t)send_buf[i]);
while(SSIBusy(spi_drv->config->base)) while (SSIBusy(spi_drv->config->base))
{ {
} }
SSIDataGet(spi_drv->config->base, &ReadData); SSIDataGet(spi_drv->config->base, &ReadData);
recv_buf[i] = (unsigned char)ReadData; recv_buf[i] = (unsigned char)ReadData;
} }
} }
else if (message->send_buf) else if (message->send_buf)
{ {
for(i=0; i< message_length; i++) for (i = 0; i < message_length; i++)
{ {
SSIDataPut(spi_drv->config->base, (uint32_t)send_buf[i]); SSIDataPut(spi_drv->config->base, (uint32_t)send_buf[i]);
while(SSIBusy(spi_drv->config->base)) while (SSIBusy(spi_drv->config->base))
{ {
} }
SSIDataGet(spi_drv->config->base, &ReadData); SSIDataGet(spi_drv->config->base, &ReadData);
@ -192,19 +197,19 @@ static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *
} }
else else
{ {
for(i=0; i< message_length; i++) for (i = 0; i < message_length; i++)
{ {
SSIDataPut(spi_drv->config->base, (uint32_t)0xff); SSIDataPut(spi_drv->config->base, (uint32_t)0xff);
while(SSIBusy(spi_drv->config->base)) while (SSIBusy(spi_drv->config->base))
{ {
} }
SSIDataGet(spi_drv->config->base, &ReadData); SSIDataGet(spi_drv->config->base, &ReadData);
recv_buf[i] = (unsigned char)ReadData; recv_buf[i] = (unsigned char)ReadData;
} }
} }
LOG_D("%s transfer done", spi_drv->config->bus_name); LOG_D("%s transfer done", spi_drv->config->bus_name);
if (message->cs_release) if (message->cs_release)
{ {
GPIOPinWrite(cs->portbase, cs->GPIO_Pin, cs->GPIO_Pin); GPIOPinWrite(cs->portbase, cs->GPIO_Pin, cs->GPIO_Pin);
@ -254,7 +259,7 @@ static int rt_hw_spi_bus_init(void)
/** /**
* Attach the spi device to SPI bus, this function must be used after initialization. * Attach the spi device to SPI bus, this function must be used after initialization.
*/ */
rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, uint32_t portindex ,uint32_t cs_gpiobase, uint32_t cs_gpio_pin) rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, uint32_t portindex, uint32_t cs_gpiobase, uint32_t cs_gpio_pin)
{ {
RT_ASSERT(bus_name != RT_NULL); RT_ASSERT(bus_name != RT_NULL);
RT_ASSERT(device_name != RT_NULL); RT_ASSERT(device_name != RT_NULL);
@ -264,11 +269,10 @@ rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name,
struct tm4c123_hw_spi_cs *cs_pin; struct tm4c123_hw_spi_cs *cs_pin;
/* initialize the cs pin && select the slave*/ /* initialize the cs pin && select the slave*/
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA+portindex); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA + portindex);
GPIOPinTypeGPIOOutput(cs_gpiobase, cs_gpio_pin); GPIOPinTypeGPIOOutput(cs_gpiobase, cs_gpio_pin);
GPIOPinWrite(cs_gpiobase, cs_gpio_pin, cs_gpio_pin); GPIOPinWrite(cs_gpiobase, cs_gpio_pin, cs_gpio_pin);
/* attach the device to spi bus*/ /* attach the device to spi bus*/
spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device)); spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
RT_ASSERT(spi_device != RT_NULL); RT_ASSERT(spi_device != RT_NULL);
@ -299,7 +303,7 @@ int rt_hw_spi_init(void)
} }
INIT_BOARD_EXPORT(rt_hw_spi_init); INIT_BOARD_EXPORT(rt_hw_spi_init);
#endif /* defined(BSP_USING_SPI0) || defined(BSP_USING_SPI1) || defined(BSP_USING_SPI2) || defined(BSP_USING_SPI3) */ #endif /* defined(BSP_USING_SPI0) || defined(BSP_USING_SPI1) || defined(BSP_USING_SPI2) || defined(BSP_USING_SPI3) */
#endif /*RT_USING_SPI*/ #endif /*RT_USING_SPI*/
/************************** end of file ******************/

View File

@ -1,5 +1,15 @@
#ifndef __DRV_SPI_H_ /*
#define __DRV_SPI_H_ * Copyright (c) 2006-2019, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-06-27 AHTYDHD the first version
*/
#ifndef __DRV_SPI_H__
#define __DRV_SPI_H__
#include <rtdevice.h> #include <rtdevice.h>
#include <rthw.h> #include <rthw.h>
@ -16,7 +26,7 @@ struct tm4c123_hw_spi_cs
struct tm4c123_spi_config struct tm4c123_spi_config
{ {
uint32_t base; uint32_t base;
char * bus_name; char *bus_name;
}; };
/* tm4c123 spi dirver class */ /* tm4c123 spi dirver class */
@ -24,7 +34,6 @@ struct tm4c123_spi
{ {
struct tm4c123_spi_config *config; struct tm4c123_spi_config *config;
struct rt_spi_configuration *cfg; struct rt_spi_configuration *cfg;
struct rt_spi_bus spi_bus; struct rt_spi_bus spi_bus;
}; };
@ -35,11 +44,6 @@ struct tm4c123_spi_device
char *device_name; char *device_name;
}; };
#define SPI_USING_RX_DMA_FLAG (1<<0) #endif /*__DRV_SPI_H__ */
#define SPI_USING_TX_DMA_FLAG (1<<1)
#endif /*__DRV_SPI_H_ */
/************************** end of file ******************/

View File

@ -1,20 +1,27 @@
/*
#include <stdint.h> * Copyright (c) 2006-2019, RT-Thread Development Team
#include <stdbool.h> *
#include "hw_memmap.h" * SPDX-License-Identifier: Apache-2.0
#include "hw_ints.h" *
#include "sysctl.h" * Change Logs:
#include "gpio.h" * Date Author Notes
#include "pin_map.h" * 2020-06-27 AHTYDHD the first version
#include "interrupt.h" */
#include "rom_map.h"
#include "uart.h"
#include "drv_uart.h" #include "drv_uart.h"
#include "uart_config.h" #include <stdint.h>
#include "tm4c123_config.h" #include <stdbool.h>
#include "inc/hw_memmap.h"
#include "inc/hw_ints.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"
#include "driverlib/interrupt.h"
#include "driverlib/uart.h"
#ifdef RT_USING_SERIAL #ifdef RT_USING_SERIAL
#include "uart_config.h"
#include "tm4c123_config.h"
#define LOG_TAG "drv.uart" #define LOG_TAG "drv.uart"
#include <drv_log.h> #include <drv_log.h>
@ -23,42 +30,36 @@
#error "Please define at least one BSP_USING_UARTx" #error "Please define at least one BSP_USING_UARTx"
#endif #endif
enum { enum
#ifdef BSP_USING_UART0
UART0_INDEX,
#endif
#ifdef BSP_USING_UART1
UART1_INDEX,
#endif
#ifdef BSP_USING_UART2
UART2_INDEX,
#endif
#ifdef BSP_USING_UART3
UART3_INDEX,
#endif
};
uint32_t uart_intbase[]=
{ {
#ifdef BSP_USING_UART0 #ifdef BSP_USING_UART0
INT_UART0, UART0_INDEX,
#endif #endif
#ifdef BSP_USING_UART1 #ifdef BSP_USING_UART1
INT_UART1, UART1_INDEX,
#endif #endif
#ifdef BSP_USING_UART2 #ifdef BSP_USING_UART2
INT_UART2, UART2_INDEX,
#endif #endif
#ifdef BSP_USING_UART3 #ifdef BSP_USING_UART3
INT_UART3 UART3_INDEX,
#endif #endif
};
uint32_t uart_intbase[] =
{
#ifdef BSP_USING_UART0
INT_UART0,
#endif
#ifdef BSP_USING_UART1
INT_UART1,
#endif
#ifdef BSP_USING_UART2
INT_UART2,
#endif
#ifdef BSP_USING_UART3
INT_UART3
#endif
}; };
static struct tm4c123_uart_config uart_config[] = static struct tm4c123_uart_config uart_config[] =
@ -66,7 +67,7 @@ static struct tm4c123_uart_config uart_config[] =
#ifdef BSP_USING_UART0 #ifdef BSP_USING_UART0
UART0_CONFIG, UART0_CONFIG,
#endif #endif
#ifdef BSP_USING_UART1 #ifdef BSP_USING_UART1
UART1_CONFIG, UART1_CONFIG,
#endif #endif
@ -74,44 +75,23 @@ static struct tm4c123_uart_config uart_config[] =
#ifdef BSP_USING_UART2 #ifdef BSP_USING_UART2
UART2_CONFIG, UART2_CONFIG,
#endif #endif
#ifdef BSP_USING_UART3 #ifdef BSP_USING_UART3
UART3_CONFIG, UART3_CONFIG,
#endif #endif
}; };
static struct tm4c123_uart uart_obj[sizeof(uart_config) / sizeof(uart_config[0])] = {0}; static struct tm4c123_uart uart_obj[sizeof(uart_config) / sizeof(uart_config[0])] = {0};
//struct serial_configure
//{
// rt_uint32_t baud_rate; /* ??? */
// rt_uint32_t data_bits :4; /* ??? */
// rt_uint32_t stop_bits :2; /* ??? */
// rt_uint32_t parity :2; /* ????? */
// rt_uint32_t bit_order :1; /* ?????????? */
// rt_uint32_t invert :1; /* ?? */
// rt_uint32_t bufsz :16; /* ????????? */
// rt_uint32_t reserved :4; /* ??? */
//};
//rtservice.h
//#define rt_container_of(ptr, type, member) ((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member)))
static rt_err_t tm4c123_configure(struct rt_serial_device *serial, struct serial_configure *cfg) static rt_err_t tm4c123_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
{ {
struct tm4c123_uart *uart; struct tm4c123_uart *uart;
RT_ASSERT(serial != RT_NULL); RT_ASSERT(serial != RT_NULL);
RT_ASSERT(cfg != RT_NULL); RT_ASSERT(cfg != RT_NULL);
uart = rt_container_of(serial, struct tm4c123_uart, serial); uart = rt_container_of(serial, struct tm4c123_uart, serial);
UARTFIFOLevelSet(uart->config->uartbase, UART_FIFO_TX1_8, UART_FIFO_RX1_8);
UARTFIFOLevelSet(uart->config->uartbase, UART_FIFO_TX1_8, UART_FIFO_RX1_8);
UARTConfigSetExpClk(uart->config->uartbase, SysCtlClockGet(), uart->config->baudrate, UARTConfigSetExpClk(uart->config->uartbase, SysCtlClockGet(), uart->config->baudrate,
uart->config->mode); uart->config->mode);
return RT_EOK; return RT_EOK;
} }
@ -119,35 +99,23 @@ static rt_err_t tm4c123_configure(struct rt_serial_device *serial, struct serial
static rt_err_t tm4c123_control(struct rt_serial_device *serial, int cmd, void *arg) static rt_err_t tm4c123_control(struct rt_serial_device *serial, int cmd, void *arg)
{ {
struct tm4c123_uart *uart; struct tm4c123_uart *uart;
#ifdef RT_SERIAL_USING_DMA
rt_ubase_t ctrl_arg = (rt_ubase_t)arg;
#endif
RT_ASSERT(serial != RT_NULL); RT_ASSERT(serial != RT_NULL);
uart = rt_container_of(serial, struct tm4c123_uart, serial); uart = rt_container_of(serial, struct tm4c123_uart, serial);
switch (cmd) switch (cmd)
{ {
/* disable interrupt */ /* disable interrupt */
case RT_DEVICE_CTRL_CLR_INT: case RT_DEVICE_CTRL_CLR_INT:
/* disable rx irq */ /* disable rx irq */
IntDisable(uart->uartintbase); IntDisable(uart->uartintbase);
/* disable interrupt */ UARTIntDisable(uart->config->uartbase, UART_INT_RX);
//UARTIntDisable(UART0_BASE, UART_INT_RX | UART_INT_RT);
UARTIntDisable(uart->config->uartbase, UART_INT_RX);
break; break;
/* enable interrupt */ /* enable interrupt */
case RT_DEVICE_CTRL_SET_INT: case RT_DEVICE_CTRL_SET_INT:
/* enable rx irq */ /* enable rx irq */
IntEnable(uart->uartintbase); IntEnable(uart->uartintbase);
//UARTIntEnable(UART0_BASE, UART_INT_RX | UART_INT_RT); UARTIntEnable(uart->config->uartbase, UART_INT_RX);
UARTIntEnable(uart->config->uartbase, UART_INT_RX);
break; break;
#ifdef RT_SERIAL_USING_DMA
case RT_DEVICE_CTRL_CONFIG:
break;
#endif
} }
return RT_EOK; return RT_EOK;
} }
@ -158,8 +126,7 @@ static int tm4c123_putc(struct rt_serial_device *serial, char c)
RT_ASSERT(serial != RT_NULL); RT_ASSERT(serial != RT_NULL);
uart = rt_container_of(serial, struct tm4c123_uart, serial); uart = rt_container_of(serial, struct tm4c123_uart, serial);
UARTCharPut(uart->config->uartbase, c);
UARTCharPut(uart->config->uartbase, c);
return 1; return 1;
} }
@ -168,6 +135,7 @@ static int tm4c123_getc(struct rt_serial_device *serial)
int ch; int ch;
struct tm4c123_uart *uart; struct tm4c123_uart *uart;
RT_ASSERT(serial != RT_NULL); RT_ASSERT(serial != RT_NULL);
uart = rt_container_of(serial, struct tm4c123_uart, serial); uart = rt_container_of(serial, struct tm4c123_uart, serial);
ch = -1; ch = -1;
ch = UARTCharGetNonBlocking(uart->config->uartbase); ch = UARTCharGetNonBlocking(uart->config->uartbase);
@ -176,27 +144,7 @@ static int tm4c123_getc(struct rt_serial_device *serial)
static rt_size_t tm4c123_dma_transmit(struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size, int direction) static rt_size_t tm4c123_dma_transmit(struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size, int direction)
{ {
struct tm4c123_uart *uart; /* this is an interface for uart dma, reserved for uptate. */
RT_ASSERT(serial != RT_NULL);
uart = rt_container_of(serial, struct tm4c123_uart, serial);
if (size == 0)
{
return 0;
}
// if (RT_SERIAL_DMA_TX == direction)
// {
// if (HAL_UART_Transmit_DMA(&uart->handle, buf, size) == HAL_OK)
// {
// return size;
// }
// else
// {
// return 0;
// }
// }
return 0; return 0;
} }
@ -214,30 +162,21 @@ static const struct rt_uart_ops tm4c123_uart_ops =
* *
* @param serial serial device * @param serial serial device
*/ */
static void uart_isr(struct rt_serial_device *serial) static void uart_isr(struct rt_serial_device *serial)
{ {
struct tm4c123_uart *uart; struct tm4c123_uart *uart;
uint32_t ui32Ints; uint32_t ui32Ints;
#ifdef RT_SERIAL_USING_DMA
rt_size_t recv_total_index, recv_len;
rt_base_t level;
#endif
RT_ASSERT(serial != RT_NULL); RT_ASSERT(serial != RT_NULL);
uart = rt_container_of(serial, struct tm4c123_uart, serial); uart = rt_container_of(serial, struct tm4c123_uart, serial);
ui32Ints = UARTIntStatus(uart->config->uartbase, true); ui32Ints = UARTIntStatus(uart->config->uartbase, true);
UARTIntClear(uart->config->uartbase, ui32Ints); UARTIntClear(uart->config->uartbase, ui32Ints);
/* UART in mode Receiver -------------------------------------------------*/ /* UART in mode Receiver -------------------------------------------------*/
if(ui32Ints & (UART_INT_RX | UART_INT_RT)) if (ui32Ints & (UART_INT_RX | UART_INT_RT))
{ {
rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND); rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
} }
} }
#if defined(BSP_USING_UART0) #if defined(BSP_USING_UART0)
@ -250,8 +189,7 @@ void UART0IntHandler(void)
/* leave interrupt */ /* leave interrupt */
rt_interrupt_leave(); rt_interrupt_leave();
} }
#endif #endif /* BSP_USING_UART0 */
#if defined(BSP_USING_UART1) #if defined(BSP_USING_UART1)
void UART1IntHandler(void) void UART1IntHandler(void)
@ -263,42 +201,11 @@ void UART1IntHandler(void)
/* leave interrupt */ /* leave interrupt */
rt_interrupt_leave(); rt_interrupt_leave();
} }
//#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART1_RX_USING_DMA)
//void UART1_DMA_RX_IRQHandler(void)
//{
// /* enter interrupt */
// rt_interrupt_enter();
// HAL_DMA_IRQHandler(&uart_obj[UART1_INDEX].dma_rx.handle);
// /* leave interrupt */
// rt_interrupt_leave();
//}
//#endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_UART1_RX_USING_DMA) */
//#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART1_TX_USING_DMA)
//void UART1_DMA_TX_IRQHandler(void)
//{
// /* enter interrupt */
// rt_interrupt_enter();
// HAL_DMA_IRQHandler(&uart_obj[UART1_INDEX].dma_tx.handle);
// /* leave interrupt */
// rt_interrupt_leave();
//}
//#endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_UART1_TX_USING_DMA) */
#endif /* BSP_USING_UART1 */ #endif /* BSP_USING_UART1 */
static void tm4c123_uart_get_dma_config(void) static void tm4c123_uart_get_dma_config(void)
{ {
#ifdef BSP_USING_UART1 /* this is an interface for uart dma, reserved for update */
#endif
} }
int rt_hw_usart_init(void) int rt_hw_usart_init(void)
@ -307,14 +214,11 @@ int rt_hw_usart_init(void)
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
rt_err_t result = 0; rt_err_t result = 0;
//tm4c123_uart_get_dma_config();
uart_hw_config(); uart_hw_config();
for (int i = 0; i < obj_num; i++) for (int i = 0; i < obj_num; i++)
{ {
uart_obj[i].config = &uart_config[i]; uart_obj[i].config = &uart_config[i];
uart_obj[i].uartintbase = uart_intbase[i]; uart_obj[i].uartintbase = uart_intbase[i];
uart_obj[i].serial.ops = &tm4c123_uart_ops; uart_obj[i].serial.ops = &tm4c123_uart_ops;
uart_obj[i].serial.config = config; uart_obj[i].serial.config = config;
/* register UART device */ /* register UART device */
@ -329,7 +233,6 @@ int rt_hw_usart_init(void)
return result; return result;
} }
#endif /* RT_USING_SERIAL */ #endif /* RT_USING_SERIAL */
/************************** end of file ******************/

View File

@ -1,5 +1,15 @@
#ifndef _DRV_UART_H_ /*
#define _DRV_UART_H_ * Copyright (c) 2006-2019, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-06-27 AHTYDHD the first version
*/
#ifndef __DRV_UART_H__
#define __DRV_UART_H__
#include <stdint.h> #include <stdint.h>
#include <rthw.h> #include <rthw.h>
@ -10,29 +20,28 @@
struct tm4c123_uart_config struct tm4c123_uart_config
{ {
const char *name; const char *name;
uint32_t uartbase; uint32_t uartbase;
uint32_t baudrate; uint32_t baudrate;
uint32_t mode; uint32_t mode;
//struct dma_config *dma_rx; //struct dma_config *dma_rx;
//struct dma_config *dma_tx; //struct dma_config *dma_tx;
}; };
/* tm4c123 uart dirver class */ /* tm4c123 uart dirver class */
struct tm4c123_uart struct tm4c123_uart
{ {
struct tm4c123_uart_config *config; struct tm4c123_uart_config *config;
uint32_t uartintbase; uint32_t uartintbase;
#ifdef RT_SERIAL_USING_DMA #ifdef RT_SERIAL_USING_DMA
#endif #endif
rt_uint16_t uart_dma_flag; rt_uint16_t uart_dma_flag;
struct rt_serial_device serial; struct rt_serial_device serial;
}; };
extern int rt_hw_usart_init(void); extern int rt_hw_usart_init(void);
#endif /*_DRV_UART_H_*/ #endif /*__DRV_UART_H__*/
/************************** end of file ******************/