328 lines
9.1 KiB
C
328 lines
9.1 KiB
C
/********************************** (C) COPYRIGHT *******************************
|
||
* File Name : debug.c
|
||
* Author : WCH
|
||
* Version : V1.0.0
|
||
* Date : 2021/06/06
|
||
* Description : This file contains all the functions prototypes for UART
|
||
* Printf , Delay functions.
|
||
*********************************************************************************
|
||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||
* Attention: This software (modified or not) and binary are used for
|
||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||
*******************************************************************************/
|
||
#include "debug.h"
|
||
|
||
static uint8_t p_us = 0;
|
||
static uint16_t p_ms = 0;
|
||
|
||
#define DEBUG_DATA0_ADDRESS ((volatile uint32_t*)0xE0000380)
|
||
#define DEBUG_DATA1_ADDRESS ((volatile uint32_t*)0xE0000384)
|
||
|
||
/*********************************************************************
|
||
* @fn Delay_Init
|
||
*
|
||
* @brief Initializes Delay Funcation.
|
||
*
|
||
* @return none
|
||
*/
|
||
void Delay_Init(void)
|
||
{
|
||
p_us = SystemCoreClock / 8000000;
|
||
p_ms = (uint16_t)p_us * 1000;
|
||
}
|
||
|
||
/*********************************************************************
|
||
* @fn Delay_Us
|
||
*
|
||
* @brief Microsecond Delay Time.
|
||
*
|
||
* @param n - Microsecond number.
|
||
*
|
||
* @return None
|
||
*/
|
||
void Delay_Us(uint32_t n)
|
||
{
|
||
uint32_t i;
|
||
|
||
SysTick->SR &= ~(1 << 0);
|
||
i = (uint32_t)n * p_us;
|
||
|
||
SysTick->CMP = i;
|
||
SysTick->CTLR |= (1 << 4);
|
||
SysTick->CTLR |= (1 << 5) | (1 << 0);
|
||
|
||
while((SysTick->SR & (1 << 0)) != (1 << 0))
|
||
;
|
||
SysTick->CTLR &= ~(1 << 0);
|
||
}
|
||
|
||
/*********************************************************************
|
||
* @fn Delay_Ms
|
||
*
|
||
* @brief Millisecond Delay Time.
|
||
*
|
||
* @param n - Millisecond number.
|
||
*
|
||
* @return None
|
||
*/
|
||
void Delay_Ms(uint32_t n)
|
||
{
|
||
uint32_t i;
|
||
|
||
SysTick->SR &= ~(1 << 0);
|
||
i = (uint32_t)n * p_ms;
|
||
|
||
SysTick->CMP = i;
|
||
SysTick->CTLR |= (1 << 4);
|
||
SysTick->CTLR |= (1 << 5) | (1 << 0);
|
||
|
||
while((SysTick->SR & (1 << 0)) != (1 << 0))
|
||
;
|
||
SysTick->CTLR &= ~(1 << 0);
|
||
}
|
||
|
||
/*********************************************************************
|
||
* @fn USART_Printf_Init
|
||
*
|
||
* @brief Initializes the USARTx peripheral.
|
||
*
|
||
* @param baudrate - USART communication baud rate.
|
||
*
|
||
* @return None
|
||
*/
|
||
void USART_Printf_Init(uint32_t baudrate)
|
||
{
|
||
GPIO_InitTypeDef GPIO_InitStructure;
|
||
USART_InitTypeDef USART_InitStructure;
|
||
|
||
#if(DEBUG == DEBUG_UART1)
|
||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
|
||
|
||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
|
||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
||
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
||
|
||
#elif(DEBUG == DEBUG_UART2)
|
||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
|
||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
|
||
|
||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
|
||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
||
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
||
|
||
#elif(DEBUG == DEBUG_UART3)
|
||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
|
||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
|
||
|
||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
|
||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
||
GPIO_Init(GPIOB, &GPIO_InitStructure);
|
||
|
||
#endif
|
||
|
||
USART_InitStructure.USART_BaudRate = baudrate;
|
||
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
|
||
USART_InitStructure.USART_StopBits = USART_StopBits_1;
|
||
USART_InitStructure.USART_Parity = USART_Parity_No;
|
||
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
|
||
USART_InitStructure.USART_Mode = USART_Mode_Tx;
|
||
|
||
#if(DEBUG == DEBUG_UART1)
|
||
USART_Init(USART1, &USART_InitStructure);
|
||
USART_Cmd(USART1, ENABLE);
|
||
|
||
#elif(DEBUG == DEBUG_UART2)
|
||
USART_Init(USART2, &USART_InitStructure);
|
||
USART_Cmd(USART2, ENABLE);
|
||
|
||
#elif(DEBUG == DEBUG_UART3)
|
||
USART_Init(USART3, &USART_InitStructure);
|
||
USART_Cmd(USART3, ENABLE);
|
||
|
||
#endif
|
||
}
|
||
|
||
/*********************************************************************
|
||
* @fn SDI_Printf_Enable
|
||
*
|
||
* @brief Initializes the SDI printf Function.
|
||
*
|
||
* @param None
|
||
*
|
||
* @return None
|
||
*/
|
||
void SDI_Printf_Enable(void)
|
||
{
|
||
*(DEBUG_DATA0_ADDRESS) = 0;
|
||
Delay_Init();
|
||
Delay_Ms(1);
|
||
}
|
||
|
||
/*********************************************************************
|
||
* @fn _write
|
||
*
|
||
* @brief Support Printf Function
|
||
*
|
||
* @param *buf - UART send Data.
|
||
* size - Data length
|
||
*
|
||
* @return size: Data length
|
||
*/
|
||
__attribute__((used)) int _write(int fd, char *buf, int size)
|
||
{
|
||
int i = 0;
|
||
|
||
#if (SDI_PRINT == SDI_PR_OPEN)
|
||
int writeSize = size;
|
||
|
||
do
|
||
{
|
||
|
||
/**
|
||
* data0 data1 8 bytes
|
||
* data0 The lowest byte storage length, the maximum is 7
|
||
*
|
||
*/
|
||
|
||
while( (*(DEBUG_DATA0_ADDRESS) != 0u))
|
||
{
|
||
|
||
}
|
||
|
||
if(writeSize>7)
|
||
{
|
||
*(DEBUG_DATA1_ADDRESS) = (*(buf+i+3)) | (*(buf+i+4)<<8) | (*(buf+i+5)<<16) | (*(buf+i+6)<<24);
|
||
*(DEBUG_DATA0_ADDRESS) = (7u) | (*(buf+i)<<8) | (*(buf+i+1)<<16) | (*(buf+i+2)<<24);
|
||
|
||
i += 7;
|
||
writeSize -= 7;
|
||
}
|
||
else
|
||
{
|
||
*(DEBUG_DATA1_ADDRESS) = (*(buf+i+3)) | (*(buf+i+4)<<8) | (*(buf+i+5)<<16) | (*(buf+i+6)<<24);
|
||
*(DEBUG_DATA0_ADDRESS) = (writeSize) | (*(buf+i)<<8) | (*(buf+i+1)<<16) | (*(buf+i+2)<<24);
|
||
|
||
writeSize = 0;
|
||
}
|
||
|
||
} while (writeSize);
|
||
|
||
|
||
#else
|
||
for(i = 0; i < size; i++)
|
||
{
|
||
#if(DEBUG == DEBUG_UART1)
|
||
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
|
||
USART_SendData(USART1, *buf++);
|
||
#elif(DEBUG == DEBUG_UART2)
|
||
while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
|
||
USART_SendData(USART2, *buf++);
|
||
#elif(DEBUG == DEBUG_UART3)
|
||
while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
|
||
USART_SendData(USART3, *buf++);
|
||
#endif
|
||
}
|
||
#endif
|
||
return size;
|
||
}
|
||
|
||
/*********************************************************************
|
||
* @fn _sbrk
|
||
*
|
||
* @brief Change the spatial position of data segment.
|
||
*
|
||
* @return size: Data length
|
||
*/
|
||
__attribute__((used)) void *_sbrk(ptrdiff_t incr)
|
||
{
|
||
extern char _end[];
|
||
extern char _heap_end[];
|
||
static char *curbrk = _end;
|
||
|
||
if ((curbrk + incr < _end) || (curbrk + incr > _heap_end))
|
||
return NULL - 1;
|
||
|
||
curbrk += incr;
|
||
return curbrk - incr;
|
||
}
|
||
|
||
|
||
/**
|
||
* @brief <20><>ʼ<EFBFBD><CABC>LED<45><44><EFBFBD><EFBFBD>IO<49><4F>, <20><>ʹ<EFBFBD><CAB9>ʱ<EFBFBD><CAB1>
|
||
* @param <20><>
|
||
* @retval <20><>
|
||
*/
|
||
void led_init(void)
|
||
{
|
||
GPIO_InitTypeDef gpio_init_struct;
|
||
LED0_GPIO_CLK_ENABLE(); /* LED0ʱ<30><CAB1>ʹ<EFBFBD><CAB9> */
|
||
LED1_GPIO_CLK_ENABLE(); /* LED1ʱ<31><CAB1>ʹ<EFBFBD><CAB9> */
|
||
LED2_GPIO_CLK_ENABLE(); /* LED2ʱ<32><CAB1>ʹ<EFBFBD><CAB9> */
|
||
INPUT_GPIO_CLK_ENABLE(); /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>ʹ<EFBFBD><CAB9> */
|
||
OUTPUT1_GPIO_CLK_ENABLE(); /* <20><><EFBFBD><EFBFBD>1<EFBFBD><31><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>ʹ<EFBFBD><CAB9> */
|
||
OUTPUT2_GPIO_CLK_ENABLE(); /* <20><><EFBFBD><EFBFBD>2<EFBFBD><32><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>ʹ<EFBFBD><CAB9> */
|
||
|
||
gpio_init_struct.GPIO_Pin = LED0_GPIO_PIN; /* LED0<44><30><EFBFBD><EFBFBD> */
|
||
gpio_init_struct.GPIO_Mode = GPIO_Mode_Out_PP; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||
gpio_init_struct.GPIO_Speed = GPIO_Speed_50MHz; /* <20><><EFBFBD><EFBFBD> */
|
||
GPIO_Init(LED0_GPIO_PORT, &gpio_init_struct); /* <20><>ʼ<EFBFBD><CABC>LED0<44><30><EFBFBD><EFBFBD> */
|
||
|
||
gpio_init_struct.GPIO_Pin = LED1_GPIO_PIN; /* LED1<44><31><EFBFBD><EFBFBD> */
|
||
GPIO_Init(LED1_GPIO_PORT, &gpio_init_struct); /* <20><>ʼ<EFBFBD><CABC>LED1<44><31><EFBFBD><EFBFBD> */
|
||
|
||
gpio_init_struct.GPIO_Pin = LED2_GPIO_PIN; /* LED2<44><32><EFBFBD><EFBFBD> */
|
||
GPIO_Init(LED2_GPIO_PORT, &gpio_init_struct); /* <20><>ʼ<EFBFBD><CABC>LED2<44><32><EFBFBD><EFBFBD> */
|
||
|
||
gpio_init_struct.GPIO_Pin = INPUT_GPIO_PIN; /* INPUT<55><54><EFBFBD><EFBFBD> */
|
||
gpio_init_struct.GPIO_Mode = GPIO_Mode_IPU; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||
GPIO_Init(INPUT_GPIO_PORT, &gpio_init_struct); /* <20><>ʼ<EFBFBD><CABC>INPUT<55><54><EFBFBD><EFBFBD> */
|
||
|
||
gpio_init_struct.GPIO_Pin = OUTPUT1_GPIO_PIN | OUTPUT2_GPIO_PIN; /* OUTPUT<55><54><EFBFBD><EFBFBD> */
|
||
gpio_init_struct.GPIO_Mode = GPIO_Mode_Out_PP; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||
GPIO_Init(OUTPUT1_GPIO_PORT, &gpio_init_struct); /* <20><>ʼ<EFBFBD><CABC>OUTPUT<55><54><EFBFBD><EFBFBD> */
|
||
|
||
|
||
LED0(1); /* <20>ر<EFBFBD> LED0 */
|
||
LED1(1); /* <20>ر<EFBFBD> LED1 */
|
||
LED2(1); /* <20>ر<EFBFBD> LED2 */
|
||
OUTPUT1(0); /* <20><><EFBFBD><EFBFBD>DO1 */
|
||
OUTPUT2(0); /* <20><><EFBFBD><EFBFBD>DO2 */
|
||
}
|
||
|
||
|
||
void check_input(void)
|
||
{
|
||
if (GPIO_ReadInputDataBit(INPUT_GPIO_PORT, INPUT_GPIO_PIN) == Bit_RESET) {
|
||
LED0(0);
|
||
} else {
|
||
LED0(1);
|
||
}
|
||
}
|
||
|
||
|
||
void set_outpot(uint8_t pd8_state, uint8_t pd9_state)
|
||
{
|
||
if(pd8_state)
|
||
GPIO_SetBits(OUTPUT1_GPIO_PORT, OUTPUT1_GPIO_PIN); /* PD8<44><38><EFBFBD><EFBFBD> */
|
||
else
|
||
GPIO_ResetBits(OUTPUT1_GPIO_PORT, OUTPUT1_GPIO_PIN); /* PD8<44><38><EFBFBD><EFBFBD> */
|
||
|
||
if(pd9_state)
|
||
GPIO_SetBits(OUTPUT2_GPIO_PORT, OUTPUT2_GPIO_PIN); /* PD9<44><39><EFBFBD><EFBFBD> */
|
||
else
|
||
GPIO_ResetBits(OUTPUT2_GPIO_PORT, OUTPUT2_GPIO_PIN); /* PD9<44><39><EFBFBD><EFBFBD> */
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|