/*********************************************************************************************
Copyright <2024> <Icore Technology (Nanjing)  Co.,Ltd>
All Rights Reserved,
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of
conditions and the following disclaimer in the documentation and/or other materials provided 
with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to 
endorse or promote products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS 
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
OF THE POSSIBILITY OF SUCH DAMAGE.
**********************************************************************************************/

/*
*********************************************************************************************************
*                                              rx32h6xx
*                                           Library Function
*
*                                   Copyright 2024, RX Tech, Corp.
*                                        All Rights Reserved
*
*
* Project      : rx32h6xx
* File         : main.c
* By           : RX_SD_Team
* Version      : V1.0.0
* Description  : Only rx32h6xx
*********************************************************************************************************
*/
#include "main.h"

/*
*********************************************************************************************************
*                                           Global macro/Structure
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*                                             Global variable
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*                                           Global Function declaration
*********************************************************************************************************
*/

void Delay_us( uint32_t us )
{
	uint32_t Fsys;
	Fsys=RCC_Get_SystemClock()/1000000;
	//maximum:199728us 
	if( us*Fsys > SysTick_LOAD_RELOAD_Msk ) return;
	SysTick->CTRL = 0;      //Disable SysTick
	SysTick->LOAD = Fsys*us;//Set count number:72*us, 72 means 1us when SystemCoreClock is 72MHz
	SysTick->VAL  = 0;      //Clear current count number and count flag
	SysTick->CTRL = 5;      //Enable SysTick and Select SystemCoreClock as SysTick clock source
	while((SysTick->CTRL&0x10000)==0);//Wait for count flag set
	SysTick->CTRL = 0;      //Disable SysTick
}

void Delay_ms( uint32_t ms )
{
	uint32_t i;
	for(i=ms;i>0;i--)
	{
		Delay_us(1000);
	}
}

void SystemClock_Config(void)
{
	// set flash lantency
	FLASH_Set_Latency(FLASH_Latency_3);
	
  RCC_Enable_HSI();
  while(RCC_Get_HSIRDY() != 1)
  {
  }
  RCC_Set_PLLMUL(RCC_CFGR_PLLMUL_19x);
  RCC_Enable_PLL();
  while(RCC_Get_PLLRDY() != 1)
  {
  }
  RCC_Set_AHBPrescaler(RCC_CFGR_HPRE_DIV1);
  RCC_Set_APB1Prescaler(RCC_CFGR_PPRE1_DIV2);
  RCC_Set_APB2Prescaler(RCC_CFGR_PPRE2_DIV1);
  RCC_Set_SysClkSource(RCC_CFGR_SW_PLL);
  while(RCC_Get_SysClkSource() != RCC_CFGR_SWS_PLL)
  {
  }
  RCC_Set_ADCClkSource(RCC_CFGR_ADCCLK_SEL_PLL2);
  RCC_Set_ADCPrescaler(RCC_CFGR_ADCPRE_DIV5);
}

void ADC_GPIO_Init(void)
{
	RCC_Enable_AHBClock(RCC_AHBENR_IOPBEN | RCC_AHBENR_IOPCEN);
	
	GPIO_InitTypeDef  GPIO_InitStruct = {0};
  GPIO_InitStruct.Mode 				= GPIO_MODE_ANALOG;
  GPIO_InitStruct.Pull 				= GPIO_PUPDR_Floating;
  GPIO_InitStruct.Speed 			= GPIO_OSPEEDR_High;
	GPIO_InitStruct.Alternate 	= GPIO_AF0;
	
  GPIO_InitStruct.Pin = GPIO_PIN_4;											//PC4    ADC channel 1
  GPIO_Init(GPIOC,&GPIO_InitStruct);
	
  GPIO_InitStruct.Pin = GPIO_PIN_2;											//PC2    ADC channel 2
  GPIO_Init(GPIOC,&GPIO_InitStruct);	
	
  GPIO_InitStruct.Pin = GPIO_PIN_1;											//PC1    ADC channel 3
  GPIO_Init(GPIOC,&GPIO_InitStruct);		
	
  GPIO_InitStruct.Pin = GPIO_PIN_0;											//PC0    ADC channel 4
  GPIO_Init(GPIOC,&GPIO_InitStruct);			
	
	GPIO_InitStruct.Pin = GPIO_PIN_7;											//PB7    ADC channel 5
  GPIO_Init(GPIOB,&GPIO_InitStruct);	
	
	GPIO_InitStruct.Pin = GPIO_PIN_6;											//PB6    ADC channel 6
  GPIO_Init(GPIOB,&GPIO_InitStruct);
	
	GPIO_InitStruct.Pin = GPIO_PIN_5;											//PB5    ADC channel 7
  GPIO_Init(GPIOB,&GPIO_InitStruct);
}

void ADC_Init(ADC_TypeDef *ADCx, uint32_t SequencerNbRanks, uint32_t continuous)
{
  __IO uint32_t uhADCxConvertedData[8];
	
	RCC_Enable_AHBClock(RCC_AHBENR_ADC1EN);
	ADC_GPIO_Init();
	
	ADC_Reg_InitTypeDef  ADC_Reg_InitStruct = {0};
	ADC_REG_RankInitTypeDef ADC_REG_RankInitStruct = {0};
	
	ADC_Reg_InitStruct.TriggerSource 							= ADC_CR2_EXTSEL_SWSTART;                       
	ADC_Reg_InitStruct.Continuous 								= continuous;                              
	ADC_Reg_InitStruct.ScanMode 									= ADC_CR1_SCAN_ENABLE;                                                                
	ADC_Reg_InitStruct.DataAlignment							= ADC_CR2_ALIGN_RIGHT;
	ADC_Reg_InitStruct.AnalogWatchDog 						= ADC_CR1_AWD_DISABLE;
	ADC_Reg_InitStruct.AWDChannelGroup 						= ADC_AWD_DISABLE;
	ADC_Reg_InitStruct.RegularDiscont							= ADC_CR1_DISCEN_DISABLE;
	ADC_Reg_InitStruct.RegularDiscontNumber       = ADC_CR1_DISCONT_DISABLE;
	ADC_Reg_InitStruct.EOCOption									=	ADC_EOC_OPT_DISABLE;
	ADC_Reg_Init(ADC1,&ADC_Reg_InitStruct);
	
	
	ADC_REG_RankInitStruct.Length = SequencerNbRanks;
	ADC_REG_RankInitStruct.Rank[0].Channel = ADC_CHANNEL_0;
	ADC_REG_RankInitStruct.Rank[0].SamplingTime = ADC_SAMPLINGTIME_2p5CYCLES;
	ADC_REG_RankInitStruct.Rank[1].Channel = ADC_CHANNEL_1;
	ADC_REG_RankInitStruct.Rank[1].SamplingTime = ADC_SAMPLINGTIME_2p5CYCLES;
	ADC_REG_RankInitStruct.Rank[2].Channel = ADC_CHANNEL_2;
	ADC_REG_RankInitStruct.Rank[2].SamplingTime = ADC_SAMPLINGTIME_2p5CYCLES;
	ADC_REG_RankInitStruct.Rank[3].Channel = ADC_CHANNEL_3;
	ADC_REG_RankInitStruct.Rank[3].SamplingTime = ADC_SAMPLINGTIME_2p5CYCLES;
	ADC_REG_RankInitStruct.Rank[4].Channel = ADC_CHANNEL_4;
	ADC_REG_RankInitStruct.Rank[4].SamplingTime = ADC_SAMPLINGTIME_2p5CYCLES;
	ADC_REG_RankInitStruct.Rank[5].Channel = ADC_CHANNEL_5;
	ADC_REG_RankInitStruct.Rank[5].SamplingTime = ADC_SAMPLINGTIME_2p5CYCLES;
	ADC_REG_RankInitStruct.Rank[6].Channel = ADC_CHANNEL_6;
	ADC_REG_RankInitStruct.Rank[6].SamplingTime = ADC_SAMPLINGTIME_2p5CYCLES;
	ADC_REG_RankInitStruct.Rank[7].Channel = ADC_CHANNEL_7;
	ADC_REG_RankInitStruct.Rank[7].SamplingTime = ADC_SAMPLINGTIME_2p5CYCLES;
	ADC_REG_RankInit(ADC1,&ADC_REG_RankInitStruct);
	
  
  /* Enable ADC */
  ADC_Enable_ADON(ADCx);
	
	Delay_us(200);
	
	/* Run ADC self calibration */
	ADC_Trig_CAL(ADCx);
	while (ADC_Get_CAL(ADCx) != 0);


	/* Clear flag ADC group regular end of unitary conversion */
	ADC_Clear_SR(ADCx,ADC_SR_EOC);
	
	/* Start ADC group regular conversion */
	ADC_Start_SW(ADCx); 

	/*wait flag ADC group regular end of unitary conversion */
	while (ADC_Get_SR(ADCx,ADC_SR_EOC) == 0);
	
	/* Clear flag ADC group regular end of unitary conversion */
	ADC_Clear_SR(ADCx,ADC_SR_EOC);
	
	 /* (data scale corresponds to ADC resolution: 16 bits) */
	uhADCxConvertedData[0] =   ADC_Read_DRx(ADCx, ADC_DR1);
	uhADCxConvertedData[1] =   ADC_Read_DRx(ADCx, ADC_DR2);
	uhADCxConvertedData[2] =   ADC_Read_DRx(ADCx, ADC_DR3);
	uhADCxConvertedData[3] =   ADC_Read_DRx(ADCx, ADC_DR4);
	uhADCxConvertedData[4] =   ADC_Read_DRx(ADCx, ADC_DR5);
	uhADCxConvertedData[5] =   ADC_Read_DRx(ADCx, ADC_DR6);
	uhADCxConvertedData[6] =   ADC_Read_DRx(ADCx, ADC_DR7);
	uhADCxConvertedData[7] =   ADC_Read_DRx(ADCx, ADC_DR8);
	
	printf("convert channel %d result is %x \r\n",ADC_CHANNEL_0&0x1F,uhADCxConvertedData[0]);
	printf("convert channel %d result is %x \r\n",ADC_CHANNEL_1&0x1F,uhADCxConvertedData[1]);
	printf("convert channel %d result is %x \r\n",ADC_CHANNEL_2&0x1F,uhADCxConvertedData[2]);
	printf("convert channel %d result is %x \r\n",ADC_CHANNEL_3&0x1F,uhADCxConvertedData[3]);
	printf("convert channel %d result is %x \r\n",ADC_CHANNEL_4&0x1F,uhADCxConvertedData[4]);
	printf("convert channel %d result is %x \r\n",ADC_CHANNEL_5&0x1F,uhADCxConvertedData[5]);
	printf("convert channel %d result is %x \r\n",ADC_CHANNEL_6&0x1F,uhADCxConvertedData[6]);
	printf("convert channel %d result is %x \r\n",ADC_CHANNEL_7&0x1F,uhADCxConvertedData[7]);
	printf("\r\n");

	
  /* Disable ADC */
  ADC_Disable_ADON(ADCx);
}

void UART2_Init_Async( uint32_t baudrate )     // UART2  ³υΚΌ»―
{
	//PD4  -> UART2_RX
	//PD5  -> UART2_TX
	
	GPIO_InitTypeDef GPIO_InitStruct  = {0};
	UART_InitTypeDef UART_InitStruct = {0};
	
	RCC_Enable_AHBClock(RCC_AHBENR_IOPDEN);
	RCC_Enable_APB1Clock(RCC_APB1ENR_UART2EN);

	UART_InitStruct.BaudRate             = baudrate;
	UART_InitStruct.DataWidth            = UART_DATAWIDTH_8B;
	UART_InitStruct.Parity               = UART_PARITY_NONE;
	UART_InitStruct.StopBits             = UART_STOPBITS_1;
	UART_InitStruct.TransferDirection    = UART_DIRECTION_TX_RX;
	UART_Init(UART2,&UART_InitStruct);

	GPIO_InitStruct.Alternate = GPIO_AF5;
	GPIO_InitStruct.Mode      = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull      = GPIO_PUPDR_Pullup;
	GPIO_InitStruct.Speed     = GPIO_OSPEEDR_High;
	GPIO_InitStruct.Pin       = GPIO_PIN_4;
	GPIO_Init(GPIOD, &GPIO_InitStruct);
	
	GPIO_InitStruct.Alternate = GPIO_AF5;
	GPIO_InitStruct.Mode      = GPIO_MODE_AF_PP;	
	GPIO_InitStruct.Pull      = GPIO_PUPDR_Floating;
	GPIO_InitStruct.Speed     = GPIO_OSPEEDR_High;
	GPIO_InitStruct.Pin       = GPIO_PIN_5;
	GPIO_Init(GPIOD, &GPIO_InitStruct);

	UART_Enable(UART2);
}


int main(void)
{   
	SystemClock_Config();
	UART2_Init_Async(115200);
	ADC_Init(ADC1,ADC_SQRL_SCAN_8RANKS,ADC_CR2_CONT_SINGLE);

  while(1)
  {
  }
}
