#include <mbed.h>
#include <stdio.h>
#include <string.h>
#include "platform/mbed_thread.h"
#include <BufferedSerial.h>
#include "main.h"
#include "usart.h"
#include "gpio.h"
void SystemClock_Config(void);
#define UART2_TX_PIN PA_2
#define UART2_RX_PIN PA_3
static UnbufferedSerial rtx(UART2_TX_PIN, UART2_RX_PIN, 115200);
#define my_usart_LIB_en 1
#include "my_gpio_lib_v3_2.h"
#include "my_uart_lib_v3_0.h"
void SystemClock_Config(void);
DigitalOut led1(PA_5, 1);
DigitalOut led2(PA_6, 1);
DigitalOut led3(PA_7, 1);
DigitalOut led4(PB_6, 1);
// tick timer 클래스 객체선언
Ticker my_tick_1;
Ticker my_tick_2;
Ticker my_tick_3;
uint16_t t_loop = 0;
uint8_t rbuf_2[20] = "song12345\n";
uint8_t rbuf_22[20] = " smga_bcd\n";
unsigned char d_buf[20] = "RTX ALL OK\n\n";
uint8_t rx_bufd;
void my_time_1()
{
led1 = !led1;
}
void my_time_2()
{
led4 = !led4;
}
void my_time_3()
{
led3 = !led3;
}
void my_time()
{
// 1ms
t_loop++;
if(t_loop > 200) // 200ms
{
t_loop = 0;
HAL_GPIO_TogglePin( led2_GPIO_Port, GPIO_PIN_6);
}
}
// rx irq func
void rx_irq()
{
//if(rtx.read(&rx_buf, 1)) //로 할수도 있음
if(rtx.readable()) // while(rtx.readable()) 로 할수도 있음
{
rx_end_flag_2 = 1;
rtx.read(&rx_bufd, 1);
}
}
int main() {
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_USART2_UART_Init();
MX_USART3_UART_Init();
// put your setup code here, to run once:
my_tick_1.attach(&my_time_1, 1.0); // 1sec
my_tick_2.attach(&my_time_2, 0.5); // 0.5
my_tick_3.attach(&my_time_3, 0.1); // 100sec
// my_tick_3.attach_us(&my_time_3, 0.1); // 100sec
// uart rx interrupt 활성화
rtx.attach(&rx_irq, SerialBase::RxIrq);
while(1) {
if(rx_end_flag_2 == 1)
{
rtx.write("rx_data = ", 10);
rtx.write(&rx_bufd, 1);
rx_end_flag_2 = 0;
}
// put your main code here, to run repeatedly:
//led4 = !led4;
/*
// Test 1. = Mbed default Serial tx func Test
printf("consol out\n");
putchar('T');
puts(" HAL/Buffer Serial Test OK");
// Test 2. = Mbed Buffer Serial tx func Test
//buffer Serial tx
rtx.write("Buffer Serial\n", 14);
rtx.write(d_buf, sizeof(d_buf));
rtx.write("song_! ok\n", 11);
// test 3 == HAL LIB
tx(rbuf_2, 2);
//tx_send('K', 2);
my_printf(" Test OK... \n");
*/
HAL_Delay(500);
}
}
//==============================================
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL8;
RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
// Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
// Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */