#include "stm32f10x.h"
#include <stm32f10x_gpio.h>
#include <stm32f10x_rcc.h>
#include <typedef.h>
#include <init.h>
#include "stm32f10x_adc.h"
#include "stm32f10x_dac.h"
#include "stm32f10x_usart.h"
#include "stm32f10x_dma.h"
#include "misc.h"

extern 	sRS485Data RS485_Data;


void InitHardware()
{
	GPIO_InitTypeDef 	GPIO_InitStructure;
	ADC_InitTypeDef  	ADC_InitStructure;
	DAC_InitTypeDef 	DAC_InitStructure;
	NVIC_InitTypeDef 	NVIC_InitStructure;
	DMA_InitTypeDef DMA_InitStructure;
	USART_InitTypeDef USART_InitStructure;




	////////////////////////////////////////////////////////////
	// Taktfrequenz einstellen
	//
	SystemInit();
	//
	// ENDE - Taktfrequenz einstellen
	////////////////////////////////////////////////////////////


	////////////////////////////////////////////////////////////
	// Aktivierung Ports etc.
	//
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);		// Port A
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);		// Port B
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);		// Port C
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);		// Port D
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);		// Port E
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);		// Analog -> Digital 1
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);			// Digital -> Analog
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);			// direct memory access 1
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);		// Clock

	GPIO_PinRemapConfig(GPIO_PartialRemap_USART3, ENABLE); 		// USART3 Remapping auf PC10, PC11, PC12
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);

	//
	// ENDE - Portaktivierung
	////////////////////////////////////////////////////////////

	
	////////////////////////////////////////////////////////////
	// Initialisierung UART (RS485)
	//



	/* DE Pin */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOC, &GPIO_InitStructure);

	/* TX Pin */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOC, &GPIO_InitStructure);

	/* RX Pin */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOC, &GPIO_InitStructure);



	/* DMA Channel 3 (USART RX) */
	NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel3_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);

	/* USART3 (TX)*/
	NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);

	/* DMA 1, Channel 2 for USART3 TX */
	DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(USART3->DR);
	DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&RS485_Data.RS485_transmit_array;
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
	DMA_InitStructure.DMA_BufferSize = 0;
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
	DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
	DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
	DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
	DMA_Init(DMA1_Channel2, &DMA_InitStructure);

	/* DMA 1, Channel 3 for USART3 RX */
	DMA_DeInit(DMA1_Channel3);
	DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(USART3->DR);
	DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&RS485_Data.RS485_receive_array;
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
	DMA_InitStructure.DMA_BufferSize = 0;
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
	DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
	DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
	DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
	DMA_Init(DMA1_Channel3, &DMA_InitStructure);

	DMA_ITConfig(DMA1_Channel3, DMA_IT_TC, ENABLE);

	USART_InitStructure.USART_BaudRate = 9600;
	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_Rx | USART_Mode_Tx;


	USART_Init(USART3, &USART_InitStructure);

	USART_DMACmd(USART3, USART_DMAReq_Tx, ENABLE);
	USART_DMACmd(USART3, USART_DMAReq_Rx, ENABLE);

	USART_ITConfig(USART3, USART_IT_TC, ENABLE);

	USART_Cmd(USART3, ENABLE);

	RS485_Transmit();
}

void USART3_IRQHandler(void)
{
	USART_ClearITPendingBit(USART3, USART_IT_TC);
	GPIO_WriteBit(GPIOC, GPIO_Pin_12, RESET);                // DE Pin low
}

void DMA1_Channel3_IRQHandler(void)
{
	/* all data received */
	DMA_ClearFlag(DMA1_FLAG_TC3);
}


Int32 RS485_Transmit()
{
	Int32 i32Retval = NO_ERROR;

	DMA_Cmd(DMA1_Channel3, DISABLE);
	DMA_Cmd(DMA1_Channel2, DISABLE);

	GPIO_WriteBit(GPIOC, GPIO_Pin_12, SET);

	DMA_SetCurrDataCounter(DMA1_Channel3, QUANTITY_RECEIVE_BYTES);
	DMA_SetCurrDataCounter(DMA1_Channel2, QUANTITY_TRANSMIT_BYTES);

	DMA_ClearFlag(DMA1_FLAG_TC3);
	DMA_ClearFlag(DMA1_FLAG_TC2);

	DMA_Cmd(DMA1_Channel3, ENABLE);
	DMA_Cmd(DMA1_Channel2, ENABLE);

	return i32Retval;
}