Advertisement
Guest User

Untitled

a guest
Aug 19th, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.76 KB | None | 0 0
  1. /**
  2.   ******************************************************************************
  3.   * @file    main.c
  4.   * @author  Ac6
  5.   * @version V1.0
  6.   * @date    01-December-2013
  7.   * @brief   Default main function.
  8.   ******************************************************************************
  9. */
  10.  
  11. // std periph lib
  12. #include "stm32f4xx.h"
  13. #include "stm32f4xx_gpio.h"
  14. #include "stm32f4xx_rcc.h"
  15. #include "stm32f4xx_usart.h"
  16. #include "stm32f4xx_dac.h"
  17. #include "stm32f4xx_adc.h"
  18. #include "stm32f4xx_dma.h"
  19. #include "stm32f4xx_tim.h"
  20. #include "misc.h"
  21.  
  22.  
  23. // std c lib
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <stdarg.h>
  27. #include <ctype.h>
  28.  
  29. //! --
  30. #define BUF_SIZE      1024    //power of 2
  31.  
  32.  
  33. #define PWM_ELEMENTS 28
  34.  
  35. const u32 PWM_Buffer[PWM_ELEMENTS] = { // Sine Table
  36.   50,  100,  150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650, 700, 750, 800, 850, 900, 950,
  37.  1000, 1050, 1100, 1150, 1200, 1250, 1300, 1350, 1399};
  38.  
  39. typedef struct {
  40.     unsigned int in;
  41.     unsigned int out;
  42.     char buf[BUF_SIZE];
  43. } buf_st;
  44.  
  45. //! -- usart
  46. buf_st rxBuf = { 0, 0, };
  47. buf_st txBuf = { 0, 0, };
  48.  
  49. __IO static unsigned int tx_restart = 1;
  50. __IO static unsigned int rx_ready = 0;
  51.  
  52. #define RxBufLen ( (unsigned short)(rxBuf.in - rxBuf.out) )
  53. #define TxBufLen ( (unsigned short)(txBuf.in - txBuf.out) )
  54.  
  55. char str[BUF_SIZE];
  56.  
  57. //! -- periph setup
  58. void SetupBuffers();
  59. void SetupUSART();
  60. void SetupPins();
  61. void SetupTIM2();
  62. void DMA_Configuration();
  63.  
  64. #include "stm32f4xx.h"
  65.  
  66. //! -- usart functions
  67. int UGetChar();
  68. int UPutChar(int c);
  69. int uputstr(char *str, int len);
  70. int uprintf(char *format, ...);
  71. int ugetstr(char *buf, int buflen);
  72.  
  73. //! -- IRQ Handlers
  74. void USART2_IRQHandler(void) {
  75.     buf_st *p;
  76.  
  77.     if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) { // read interrupt
  78.         USART_ClearITPendingBit(USART2, USART_IT_RXNE); // clear interrupt ????
  79.         p = &rxBuf;
  80.  
  81.         if (((p->in - p->out) & ~(BUF_SIZE - 1)) == 0) {
  82.             char ch = (USART2->DR & 0x1FF);
  83.             if (ch == '\n')
  84.                 rx_ready++;
  85.             p->buf[p->in & (BUF_SIZE - 1)] = ch;
  86.             p->in++;
  87.         } else {
  88.             rx_ready++;
  89.         }
  90.     }
  91.     if (USART_GetITStatus(USART2, USART_IT_TXE) != RESET) { //
  92.         USART_ClearITPendingBit(USART2, USART_IT_TXE);
  93.         p = &txBuf;
  94.  
  95.         if (p->in != p->out) { // jesli jest cos do wyslania
  96.             USART2->DR = (p->buf[p->out & (BUF_SIZE - 1)] & 0x1FF);
  97.             p->out++;
  98.             tx_restart = 0;
  99.         } else {
  100.             tx_restart = 1;
  101.             USART2->CR1 &= ~USART_FLAG_TXE; // disable tx irq if nothing to send
  102.         }
  103.     }
  104. }
  105.  
  106.  
  107. int main(void)
  108. {
  109.     SetupBuffers();
  110.     SetupUSART();
  111.     SetupPins();
  112.     DMA_Configuration();
  113.     SetupTIM2();
  114.  
  115.  
  116.     uprintf("Ready!\n");
  117.     while(1){
  118.     char *pstr = str;
  119.             if (ugetstr(pstr, BUF_SIZE) != -1) { // nadeszla komenda wiec ja przetwarzamy
  120.                 uprintf("%s\n", pstr);
  121.             }
  122.     }
  123. }
  124.  
  125. void SetupBuffers() {
  126.     txBuf.in = 0;
  127.     txBuf.out = 0;
  128.     tx_restart = 1;
  129.  
  130.     rxBuf.in = 0;
  131.     rxBuf.out = 0;
  132. }
  133.  
  134. void SetupUSART() {
  135.     GPIO_InitTypeDef GPIO_InitStructure;
  136.     USART_InitTypeDef USART_InitStructure;
  137.     NVIC_InitTypeDef NVIC_InitStructure;
  138.  
  139.     //! USART setup ============================================
  140.     // Enable the APB1 periph clock for USART2
  141.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  142.     // Enable the GPIOA clock, used by pins PD5, PD6
  143.     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
  144.  
  145.     // Setup the GPIO pins for        Tx           Rx
  146.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6;
  147.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  148.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  149.     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  150.     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  151.     GPIO_Init(GPIOD, &GPIO_InitStructure);
  152.  
  153.     // Connect PD5 and PD6 with the USART2 Alternate Function
  154.     GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_USART2);
  155.     GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_USART2);
  156.  
  157.     USART_InitStructure.USART_BaudRate = 9600;
  158.     USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  159.     USART_InitStructure.USART_StopBits = USART_StopBits_1;
  160.     USART_InitStructure.USART_Parity = USART_Parity_No;
  161.     USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  162.     USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
  163.     USART_Init(USART2, &USART_InitStructure);
  164.  
  165.     USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); // rx not empty
  166.     USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
  167.  
  168.     // enable usart irq
  169.     NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
  170.     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  171.     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  172.     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  173.     NVIC_Init(&NVIC_InitStructure);
  174.  
  175.     // Enable the USART2 peripheral
  176.     USART_Cmd(USART2, ENABLE);
  177. }
  178.  
  179. int UPutChar(int c) {
  180.     buf_st *p = &txBuf;
  181.  
  182.     if (TxBufLen >= (BUF_SIZE - 1))
  183.         return (-1);
  184.  
  185.     p->buf[p->in & (BUF_SIZE - 1)] = c;
  186.     p->in++;
  187.  
  188.     if (tx_restart) {
  189.         tx_restart = 0;
  190.         USART2->CR1 |= USART_FLAG_TXE; // enable tx irq
  191.     }
  192.     return 0;
  193. }
  194.  
  195. int UGetChar() {
  196.     buf_st *p = &rxBuf;
  197.  
  198.     if (RxBufLen == 0)
  199.         return 0; // eof
  200.  
  201.     return (p->buf[(p->out++) & (BUF_SIZE - 1)]);
  202. }
  203.  
  204. int uputstr(char *str, int len) {
  205.     buf_st *p = &txBuf;
  206.     __IO int idx;
  207.     int i;
  208.  
  209.     idx = p->in;
  210.     for (i = 0; i < len; i++) {
  211.         p->buf[idx & (BUF_SIZE - 1)] = str[i] & 0x1FF;
  212.         idx++;
  213.     }
  214.  
  215.     __disable_irq();
  216.     if (tx_restart) {
  217.         tx_restart = 0;
  218.         p->in = idx;
  219.         USART2->CR1 |= USART_FLAG_TXE; // enable tx irq
  220.     } else {
  221.         p->in = idx;
  222.     }
  223.     __enable_irq();
  224.  
  225.     return i;
  226. }
  227. int uprintf(char *format, ...) {
  228.     while (TxBufLen > 0);
  229.     buf_st *p = &txBuf;
  230.     char tmp[256];
  231.     va_list arg;
  232.     int done;
  233.  
  234.     va_start(arg, format);
  235.     done = vsprintf(tmp, format, arg);
  236.     va_end(arg);
  237.  
  238.     if (done) {
  239.         __IO int idx;
  240.         int i;
  241.         int len = strlen(tmp);
  242.         idx = p->in;
  243.         for (i = 0; i < len; i++) {
  244.             p->buf[idx & (BUF_SIZE - 1)] = tmp[i] & 0x1FF;
  245.             idx++;
  246.         }
  247.  
  248.         __disable_irq();
  249.         if (tx_restart) {
  250.             tx_restart = 0;
  251.             p->in = idx;
  252.             USART2->CR1 |= USART_FLAG_TXE; // enable tx irq
  253.         } else {
  254.             p->in = idx;
  255.         }
  256.         __enable_irq();
  257.     }
  258.     return done;
  259. }
  260.  
  261. int ugetstr(char *buf, int buflen) {
  262.     if (rx_ready > 0) {
  263.         int cnt = 0;
  264.  
  265.         while (cnt < buflen) {
  266.             char c = toupper(UGetChar()); // to upper
  267.             if (c == '\n' || c == '\0') {
  268.                 buf[cnt] = 0;
  269.                 break;
  270.             }
  271.             buf[cnt] = c;
  272.             cnt++;
  273.         }
  274.         rx_ready--;
  275.         return cnt;
  276.     }
  277.     return -1;
  278. }
  279.  
  280. void SetupPins(void) {
  281.     GPIO_InitTypeDef GPIO_InitStruct;
  282.  
  283.     /* Clock for GPIOD */
  284.     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
  285.  
  286.     /* Alternating functions for pins */
  287.     GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_TIM2);
  288.  
  289.         /* Set pins */
  290.     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12;
  291.     GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  292.     GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
  293.     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
  294.     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
  295.  
  296.     GPIO_Init(GPIOD, &GPIO_InitStruct);
  297. }
  298. void SetupTIM2() {
  299.  
  300.     TIM_TimeBaseInitTypeDef TIM2_InitStructure;
  301.  
  302.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  303.  
  304.     TIM_TimeBaseStructInit(&TIM2_InitStructure);
  305.     TIM2_InitStructure.TIM_Period = 1399;
  306.     TIM2_InitStructure.TIM_Prescaler = 59999;
  307.     TIM2_InitStructure.TIM_ClockDivision = 0;
  308.     TIM2_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
  309.     TIM_TimeBaseInit(TIM2, &TIM2_InitStructure);
  310.     TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
  311.  
  312.  
  313.     TIM_OCInitTypeDef TIM_OCStruct;
  314.  
  315.     TIM_OCStruct.TIM_OCMode = TIM_OCMode_PWM1;
  316.     TIM_OCStruct.TIM_OutputState = TIM_OutputState_Enable;
  317.     TIM_OCStruct.TIM_OCPolarity = TIM_OCPolarity_Low;
  318.     TIM_OCStruct.TIM_Pulse = 50; /* 25% duty cycle */
  319.     TIM_OCStruct.TIM_OCIdleState = TIM_OCIdleState_Reset;
  320.     TIM_OC1Init(TIM2, &TIM_OCStruct);
  321.     TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
  322.  
  323.     /* Associating DMA and TIM2 Update (DMA1 Channel 3, Stream 1) */
  324.     TIM_DMACmd(TIM2, TIM_DMA_Update, ENABLE);
  325.  
  326.     TIM_Cmd(TIM2, ENABLE);
  327.  
  328.     NVIC_InitTypeDef NVIC_InitStruct;
  329.     NVIC_InitStruct.NVIC_IRQChannel = TIM2_IRQn;
  330.     NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
  331.     NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
  332.     NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
  333.     NVIC_Init(&NVIC_InitStruct);
  334.  
  335. }
  336.  
  337. void TIM2_IRQHandler()
  338. {
  339.     // Checks whether the TIM2 interrupt has occurred or not
  340.     if (TIM_GetITStatus(TIM2, TIM_IT_Update))
  341.     {
  342.         // Toggle orange LED (GPIO13)
  343.  
  344.         //TIM2->ARR = TIM2->ARR - 100;
  345.         TIM2->CCR1 = TIM2->ARR/2;
  346.         uprintf("ARR: %d CCR1: %d\n", TIM2->ARR, TIM2->CCR1);
  347.  
  348.         // Clears the TIM2 interrupt pending bit
  349.         TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
  350.     }
  351. }
  352.  
  353. void DMA_Configuration(void)
  354. {
  355.   DMA_InitTypeDef DMA_InitStruct;
  356.  
  357.   /* TIM2_UP - DMA1, Channel 3, Stream 1 */
  358.  
  359.   /* DMA1 clock enable */
  360.   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
  361.  
  362.   DMA_InitStruct.DMA_Channel            = DMA_Channel_3;
  363.   DMA_InitStruct.DMA_BufferSize         = PWM_ELEMENTS;
  364.   DMA_InitStruct.DMA_DIR                = DMA_DIR_MemoryToPeripheral;
  365.   DMA_InitStruct.DMA_Mode               = DMA_Mode_Circular;
  366.   DMA_InitStruct.DMA_FIFOMode           = DMA_FIFOMode_Disable;
  367.   DMA_InitStruct.DMA_FIFOThreshold      = DMA_FIFOThreshold_HalfFull;
  368.   DMA_InitStruct.DMA_Memory0BaseAddr    = (uint32_t)&PWM_Buffer[0];
  369.   DMA_InitStruct.DMA_MemoryDataSize     = DMA_MemoryDataSize_Word;
  370.   DMA_InitStruct.DMA_MemoryInc          = DMA_MemoryInc_Enable;
  371.   DMA_InitStruct.DMA_MemoryBurst        = DMA_MemoryBurst_Single;
  372.   DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)&TIM2->ARR;
  373.   DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
  374.   DMA_InitStruct.DMA_PeripheralInc      = DMA_PeripheralInc_Disable;
  375.   DMA_InitStruct.DMA_PeripheralBurst    = DMA_PeripheralBurst_Single;
  376.   DMA_InitStruct.DMA_Priority           = DMA_Priority_VeryHigh;
  377.  
  378.   DMA_Init(DMA1_Stream1, &DMA_InitStruct);
  379.  
  380.   /* Enabling DMA */
  381.   DMA_Cmd(DMA1_Stream1, ENABLE);
  382. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement