Skip to content

Commit

Permalink
加入 fputc 函數,支援 printf 03/02
Browse files Browse the repository at this point in the history
  • Loading branch information
Hom authored and Hom committed Mar 2, 2015
1 parent 1ca40f5 commit e7e5a38
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 85 deletions.
95 changes: 67 additions & 28 deletions Software/BSR_TestUART/Program/Modules/module_rs232.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/*====================================================================================================*/
/*====================================================================================================*/
#include <stdio.h>

#include "stm32f1_system.h"
#include "stm32f1_usart.h"
#include "module_rs232.h"
Expand Down Expand Up @@ -57,12 +59,39 @@ void RS232_Config( void )
USART_InitStruct.USART_HardwareFlowControl = USARTx_HARDWARECTRL;
USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USARTx, &USART_InitStruct);

USART_Cmd(USARTx, ENABLE);

USART_ClearFlag(USARTx, USART_FLAG_TC);
}
/*====================================================================================================*/
/*====================================================================================================*
**函數 : RS232_SendByte
**功能 : Send Byte
**輸入 : *pWord
**輸出 : None
**使用 : RS232_SendByte('A');
**====================================================================================================*/
/*====================================================================================================*/
void RS232_SendByte( int8_t SendByte )
{
UART_SendByte(USARTx, &SendByte);
}
/*====================================================================================================*/
/*====================================================================================================*
**函數 : RS232_SendData
**功能 : 發送資料
**輸入 : *SendData, DataLen
**輸出 : None
**使用 : RS232_SendData(SendData, DataLen);
**====================================================================================================*/
/*====================================================================================================*/
void RS232_SendData( int8_t *SendData, uint16_t DataLen )
{
UART_SendData(USARTx, SendData, DataLen);
}
/*====================================================================================================*/
/*====================================================================================================*
**函數 : RS232_SendStr
**功能 : 發送字串
**輸入 : *pWord
Expand Down Expand Up @@ -100,16 +129,44 @@ void RS232_SendNum( StrType Type, uint8_t NumLen, int32_t SendData )
}
/*====================================================================================================*/
/*====================================================================================================*
**函數 : RS232_SendData
**功能 : 發送資料
**輸入 : *SendData, DataLen
**函數 : RS232_RecvByte
**功能 : Recv Byte
**輸入 : None
**輸出 : RecvByte
**使用 : RecvByte = RS232_RecvByte();
**====================================================================================================*/
/*====================================================================================================*/
int8_t RS232_RecvByte( void )
{
int8_t RecvByte = 0;
UART_RecvByte(USARTx, &RecvByte);
return RecvByte;
}
/*====================================================================================================*/
/*====================================================================================================*
**函數 : RS232_RecvData
**功能 : 接收資料
**輸入 : *RecvData, DataLen
**輸出 : None
**使用 : RS232_SendData(SendData, DataLen);
**使用 : RS232_RecvData(RecvData, DataLen);
**====================================================================================================*/
/*====================================================================================================*/
void RS232_SendData( int8_t *SendData, uint16_t DataLen )
void RS232_RecvData( int8_t *RecvData, uint16_t DataLen )
{
UART_SendData(USARTx, SendData, DataLen);
UART_RecvData(USARTx, RecvData, DataLen);
}
/*====================================================================================================*/
/*====================================================================================================*
**函數 : RS232_RecvDataWTO
**功能 : 接收資料, 並加入 Timeout 功能
**輸入 : *RecvData, DataLen, TimeoutMs
**輸出 : State
**使用 : RS232_RecvDataWTO(RecvData, DataLen, 200);
**====================================================================================================*/
/*====================================================================================================*/
int8_t RS232_RecvDataWTO( int8_t *RecvData, uint16_t DataLen, int32_t TimeoutMs )
{
return UART_RecvDataWTO(USARTx, RecvData, DataLen, TimeoutMs);
}
/*====================================================================================================*/
/*====================================================================================================*
Expand Down Expand Up @@ -150,30 +207,12 @@ int8_t RS232_RecvStrWTO( int8_t *pWord, int32_t TimeoutMs )
return SUCCESS;
}
/*====================================================================================================*/
/*====================================================================================================*
**函數 : RS232_RecvData
**功能 : 接收資料
**輸入 : *RecvData, DataLen
**輸出 : None
**使用 : RS232_RecvData(RecvData, DataLen);
**====================================================================================================*/
/*====================================================================================================*/
void RS232_RecvData( int8_t *RecvData, uint16_t DataLen )
{
UART_RecvData(USARTx, RecvData, DataLen);
}
/*====================================================================================================*/
/*====================================================================================================*
**函數 : RS232_RecvDataWTO
**功能 : 接收資料, 並加入 Timeout 功能
**輸入 : *RecvData, DataLen, TimeoutMs
**輸出 : State
**使用 : RS232_RecvDataWTO(RecvData, DataLen, 200);
**====================================================================================================*/
/*====================================================================================================*/
int8_t RS232_RecvDataWTO( int8_t *RecvData, uint16_t DataLen, int32_t TimeoutMs )
int fputc( int ch, FILE *f )
{
return UART_RecvDataWTO(USARTx, RecvData, DataLen, TimeoutMs);
USARTx->DR = ((uint8_t)ch & (uint16_t)0x00FF);
while(!(USART1->SR & USART_FLAG_TXE));
return (ch);
}
/*====================================================================================================*/
/*====================================================================================================*/
8 changes: 5 additions & 3 deletions Software/BSR_TestUART/Program/Modules/module_rs232.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
/*====================================================================================================*/
/*====================================================================================================*/
void RS232_Config( void );
void RS232_SendByte( int8_t SendByte );
void RS232_SendData( int8_t *SendData, uint16_t DataLen );
void RS232_SendStr( int8_t *pWord );
void RS232_SendNum( StrType Type, uint8_t NumLen, int32_t SendData );
void RS232_SendData( int8_t *SendData, uint16_t DataLen );
void RS232_RecvStr( int8_t *pWord );
int8_t RS232_RecvStrWTO( int8_t *pWord, int32_t TimeoutMs );
int8_t RS232_RecvByte( void );
void RS232_RecvData( int8_t *RecvData, uint16_t DataLen );
int8_t RS232_RecvDataWTO( int8_t *RecvData, uint16_t DataLen, int32_t TimeoutMs );
void RS232_RecvStr( int8_t *pWord );
int8_t RS232_RecvStrWTO( int8_t *pWord, int32_t TimeoutMs );
/*====================================================================================================*/
/*====================================================================================================*/
#endif
22 changes: 12 additions & 10 deletions Software/BSR_TestUART/Program/experiment_stm32f1.c
Original file line number Diff line number Diff line change
@@ -1,34 +1,36 @@
/*====================================================================================================*/
/*====================================================================================================*/
#include <stdio.h>

#include "stm32f1_system.h"
#include "experiment_stm32f1.h"
#include "module_rs232.h"
/*====================================================================================================*/
/*====================================================================================================*/
#define RECV_DATA_SIZE 1
#define RECV_DATA_TIMEOUT 500

int main( void )
{
int8_t State = ERROR;
int8_t RecvData = 0;
static uint8_t i = 0;
static uint8_t RecvData = 0;
static int8_t State = ERROR;

GPIO_Config();
RS232_Config();

LED_R = 0;

RS232_SendStr((int8_t*)"\r\nHello World!\r\n\r\n");
printf("\r\nHello World!\r\n\r\n");

while(1) {
LED_G = !LED_G;
State = RS232_RecvDataWTO(&RecvData, RECV_DATA_SIZE, RECV_DATA_TIMEOUT);
if(State == ERROR)
RS232_SendStr((int8_t*)"Timeout ... \r\n");
State = RS232_RecvDataWTO((int8_t*)&RecvData, 1, 200);
if(State == ERROR) {
printf("Timeout ... %d\r\n", i);
i = (i == 255) ? 0 : i + 1;
}
else if(RecvData == 0x0D) // if press enter
RS232_SendStr((int8_t*)"\r\n");
else
RS232_SendData(&RecvData, RECV_DATA_SIZE);
RS232_SendData((int8_t*)&RecvData, 1);
}
}
/*====================================================================================================*/
Expand Down
62 changes: 62 additions & 0 deletions Software/BSR_TestUART/Program/experiment_stm32f1_it.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*====================================================================================================*/
/*====================================================================================================*/
#include "stm32f1_system.h"
#include "module_rs232.h"
/*====================================================================================================*/
/*====================================================================================================*/
void NMI_Handler( void ) { while(1); }
void HardFault_Handler( void ) { while(1); }
void MemManage_Handler( void ) { while(1); }
void BusFault_Handler( void ) { while(1); }
void UsageFault_Handler( void ) { while(1); }
void SVC_Handler( void ) { while(1); }
void DebugMon_Handler( void ) { while(1); }
void PendSV_Handler( void ) { while(1); }
/*====================================================================================================*/
/*====================================================================================================*/
//void SysTick_Handler( void );
//void WWDG_IRQHandler( void );
//void PVD_IRQHandler( void );
//void TAMPER_IRQHandler( void );
//void RTC_IRQHandler( void );
//void FLASH_IRQHandler( void );
//void RCC_IRQHandler( void );
//void EXTI0_IRQHandler( void );
//void EXTI1_IRQHandler( void );
//void EXTI2_IRQHandler( void );
//void EXTI3_IRQHandler( void );
//void EXTI4_IRQHandler( void );
//void DMA1_Channel1_IRQHandler( void );
//void DMA1_Channel2_IRQHandler( void );
//void DMA1_Channel3_IRQHandler( void );
//void DMA1_Channel4_IRQHandler( void );
//void DMA1_Channel5_IRQHandler( void );
//void DMA1_Channel6_IRQHandler( void );
//void DMA1_Channel7_IRQHandler( void );
//void ADC1_2_IRQHandler( void );
//void USB_HP_CAN1_TX_IRQHandler( void );
//void USB_LP_CAN1_RX0_IRQHandler( void );
//void CAN1_RX1_IRQHandler( void );
//void CAN1_SCE_IRQHandler( void );
//void EXTI9_5_IRQHandler( void );
//void TIM1_BRK_IRQHandler( void );
//void TIM1_UP_IRQHandler( void );
//void TIM1_TRG_COM_IRQHandler( void );
//void TIM1_CC_IRQHandler( void );
//void TIM2_IRQHandler( void );
//void TIM3_IRQHandler( void );
//void TIM4_IRQHandler( void );
//void I2C1_EV_IRQHandler( void );
//void I2C1_ER_IRQHandler( void );
//void I2C2_EV_IRQHandler( void );
//void I2C2_ER_IRQHandler( void );
//void SPI1_IRQHandler( void );
//void SPI2_IRQHandler( void );
//void USART1_IRQHandler( void );
//void USART2_IRQHandler( void );
//void USART3_IRQHandler( void );
//void EXTI15_10_IRQHandler( void );
//void RTCAlarm_IRQHandler( void );
//void USBWakeUp_IRQHandler( void );
/*====================================================================================================*/
/*====================================================================================================*/
Loading

0 comments on commit e7e5a38

Please sign in to comment.