/*==============================================================================
 * Шаблон проекта для К1921ВК035 с использованием бибилотеки PLIB035
 *------------------------------------------------------------------------------
 * ЦНИИ СЭТ, Разваляев Алексей <wot890089@mail.ru>
 *==============================================================================
 *                                  ЦНИИ СЭТ
 *==============================================================================
 */

//-- Includes ------------------------------------------------------------------
#include "main.h"

void restart_receive(void);
void heartbit(void);
//-- Defines -------------------------------------------------------------------
uint8_t rxbuff[10] = {0};
uint8_t txbuff[20] = {0};
uint16_t seq0_buff[2][100] = {0};
uint16_t seq1_buff[2][100] = {0};
//-- Peripheral init functions -------------------------------------------------
void periph_init()
{
  sysclk_init();
  uart_init_first();
  adc_init_first();
  tmr_init_first();
  gpio_init();
#ifdef RETARGET
  retarget_init();
#endif
  NVIC_SetAllPriorities();
  printf("\nAll peripherals inited, SYSCLK = %3d MHz\n", (int)(SystemCoreClock / 1E6));
  
  
  UART_Start(&huart1, UART_FIFOLevel_1_8, UART_FIFOLevel_1_8);
  
//  TMR_Set_Callback(&htmr2, TMR_Callback_Update, &heartbit);
  TMR_Start(&htmr0, DISABLE);
  TMR_Start(&htmr1, DISABLE); 
  TMR_Start(&htmr2, ENABLE);
  ADC_SEQ_Set_Callback(&hadc, ADC_SEQ_Num_1, ADC_Callback_SeqCplt, &heartbit);
//  ADC_SEQ_Start(&hadc, ADC_SEQ_Num_0, seq0_buff, 100);
  ADC_SEQ_Start(&hadc, ADC_SEQ_Num_1, NULL, 100);
}

//-- Main ----------------------------------------------------------------------
uint32_t startmillis = 0;
uint32_t prev_tick;
int main()
{ 
  periph_init();
  
//  UART_Receive_IT(&huart1, rxbuff, sizeof(rxbuff));
  TMR_Delay_Start(&htmr0, &prev_tick);
  ADC_SEQ_SoftwareStart();
  while (1) {
    if(TMR_Delay_Done(&htmr0, 2000000, &prev_tick))
    {
      TMR_Delay_Start(&htmr0, &prev_tick);
      GPIO_ToggleBits(GPIOA, GPIO_Pin_8);
      startmillis = millis();
//      ADC_SEQ_SoftwareStart();
    }
    TMR_Delay(&htmr0, 100000);
//    GPIO_ToggleBits(GPIOA, GPIO_Pin_7);
//    UART_Transmit_IT(&huart1, (uint8_t*)"Hello World: ", sizeof("Hello World: ")-1);
//    int len = sprintf((char*)txbuff, "\r\nValue: %d", ADC_Channel_GetValue(&hadc, ADC_CH_Num_3));
//    UART_Transmit_IT(&huart1, txbuff, len);
  };
  //return 0;
}

void restart_receive(void)
{
//  UART_Receive_IT(&huart1, rxbuff, sizeof(rxbuff));
//  UART_Transmit_IT(&huart1, rxbuff, sizeof(rxbuff));
}

char tickbuff[40] = {0};
void heartbit(void)
{
  static uint32_t prev_tick = 0;
  int len = sprintf(tickbuff, "\r\nTick: %d, Value: %d", TMR_Get_Cnt(&htmr0) - prev_tick, ADC_Channel_GetValue(&hadc, ADC_CH_Num_3));
  prev_tick = TMR_Get_Cnt(&htmr0);
  UART_Transmit_IT(&huart1, (uint8_t *)tickbuff, len);
}

//-- Assert --------------------------------------------------------------------
void Error_Handler(void)
{
  __disable_irq();
  while (1)
  {
  } 
}

#if defined USE_FULL_ASSERT
void assert_failed(uint8_t* file, uint32_t line)
{
  printf("Assert failed: file %s on line %d\n", file, (int)line);
  while (1) {
  };
}
#endif /* USE_FULL_ASSERT */
