#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"


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;


  	  uint8_t usart_receive_array[] = {0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA};
  	  uint8_t usart_transmit_array[] = {0x12, 0x34, 0x56, 0x56, 0x56, 0x56, 0x56, 0x56};

////////////////////////////////////////////////////////////
// Taktfrequenz einstellen
//
  	  SystemInit();
//
// ENDE - Taktfrequenz einstellen
////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////
// Aktivierung Ports etc.
//
	  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
	  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
	  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);
	  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
	  RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
  	  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
      	  RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);

 	  GPIO_PinRemapConfig(GPIO_PartialRemap_USART3, ENABLE); 	// USART3 Remapping auf PC10, PC11, PC12
  	  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);

//
// ENDE - Portaktivierung
////////////////////////////////////////////////////////////




////////////////////////////////////////////////////////////
// Initialisierung USART (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) &usart_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) &usart_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);

	  	  /* start transmission */
	  	  DMA_Cmd(DMA1_Channel3, DISABLE);
	  	  DMA_Cmd(DMA1_Channel2, DISABLE);

	  	  GPIO_WriteBit(GPIOC, GPIO_Pin_12, SET);                // DE Pin high

	  	  DMA_SetCurrDataCounter(DMA1_Channel3, 6);
	  	  DMA_SetCurrDataCounter(DMA1_Channel2, 8);

	  	  DMA_ClearFlag(DMA1_FLAG_TC3);
	  	  DMA_ClearFlag(DMA1_FLAG_TC2);
	  	  DMA_Cmd(DMA1_Channel3, ENABLE);
	  	  DMA_Cmd(DMA1_Channel2, ENABLE);
	
	  	  while(1){}
}

void USART2_IRQHandler(void)
{
	USART_ClearITPendingBit(USART2, USART_IT_TC);
	GPIO_WriteBit(GPIOA, GPIO_Pin_0, RESET);                // DE Pin low
}

void DMA1_Channel6_IRQHandler(void)
{
	/* all data received */
	DMA_ClearFlag(DMA1_FLAG_TC6);
}