Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- ******************************************************************************
- * @file main.c
- * @author Ac6
- * @version V1.0
- * @date 01-December-2013
- * @brief Default main function.
- ******************************************************************************
- */
- // std periph lib
- #include "stm32f4xx.h"
- #include "stm32f4xx_gpio.h"
- #include "stm32f4xx_rcc.h"
- #include "stm32f4xx_usart.h"
- #include "stm32f4xx_dac.h"
- #include "stm32f4xx_adc.h"
- #include "stm32f4xx_dma.h"
- #include "stm32f4xx_tim.h"
- #include "misc.h"
- // std c lib
- #include <stdio.h>
- #include <string.h>
- #include <stdarg.h>
- #include <ctype.h>
- //! --
- #define BUF_SIZE 1024 //power of 2
- #define PWM_ELEMENTS 28
- const u32 PWM_Buffer[PWM_ELEMENTS] = { // Sine Table
- 50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650, 700, 750, 800, 850, 900, 950,
- 1000, 1050, 1100, 1150, 1200, 1250, 1300, 1350, 1399};
- typedef struct {
- unsigned int in;
- unsigned int out;
- char buf[BUF_SIZE];
- } buf_st;
- //! -- usart
- buf_st rxBuf = { 0, 0, };
- buf_st txBuf = { 0, 0, };
- __IO static unsigned int tx_restart = 1;
- __IO static unsigned int rx_ready = 0;
- #define RxBufLen ( (unsigned short)(rxBuf.in - rxBuf.out) )
- #define TxBufLen ( (unsigned short)(txBuf.in - txBuf.out) )
- char str[BUF_SIZE];
- //! -- periph setup
- void SetupBuffers();
- void SetupUSART();
- void SetupPins();
- void SetupTIM2();
- void DMA_Configuration();
- #include "stm32f4xx.h"
- //! -- usart functions
- int UGetChar();
- int UPutChar(int c);
- int uputstr(char *str, int len);
- int uprintf(char *format, ...);
- int ugetstr(char *buf, int buflen);
- //! -- IRQ Handlers
- void USART2_IRQHandler(void) {
- buf_st *p;
- if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) { // read interrupt
- USART_ClearITPendingBit(USART2, USART_IT_RXNE); // clear interrupt ????
- p = &rxBuf;
- if (((p->in - p->out) & ~(BUF_SIZE - 1)) == 0) {
- char ch = (USART2->DR & 0x1FF);
- if (ch == '\n')
- rx_ready++;
- p->buf[p->in & (BUF_SIZE - 1)] = ch;
- p->in++;
- } else {
- rx_ready++;
- }
- }
- if (USART_GetITStatus(USART2, USART_IT_TXE) != RESET) { //
- USART_ClearITPendingBit(USART2, USART_IT_TXE);
- p = &txBuf;
- if (p->in != p->out) { // jesli jest cos do wyslania
- USART2->DR = (p->buf[p->out & (BUF_SIZE - 1)] & 0x1FF);
- p->out++;
- tx_restart = 0;
- } else {
- tx_restart = 1;
- USART2->CR1 &= ~USART_FLAG_TXE; // disable tx irq if nothing to send
- }
- }
- }
- int main(void)
- {
- SetupBuffers();
- SetupUSART();
- SetupPins();
- DMA_Configuration();
- SetupTIM2();
- uprintf("Ready!\n");
- while(1){
- char *pstr = str;
- if (ugetstr(pstr, BUF_SIZE) != -1) { // nadeszla komenda wiec ja przetwarzamy
- uprintf("%s\n", pstr);
- }
- }
- }
- void SetupBuffers() {
- txBuf.in = 0;
- txBuf.out = 0;
- tx_restart = 1;
- rxBuf.in = 0;
- rxBuf.out = 0;
- }
- void SetupUSART() {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- //! USART setup ============================================
- // Enable the APB1 periph clock for USART2
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
- // Enable the GPIOA clock, used by pins PD5, PD6
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
- // Setup the GPIO pins for Tx Rx
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- // Connect PD5 and PD6 with the USART2 Alternate Function
- GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_USART2);
- GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_USART2);
- 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_Tx | USART_Mode_Rx;
- USART_Init(USART2, &USART_InitStructure);
- USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); // rx not empty
- USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
- // enable usart irq
- NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- // Enable the USART2 peripheral
- USART_Cmd(USART2, ENABLE);
- }
- int UPutChar(int c) {
- buf_st *p = &txBuf;
- if (TxBufLen >= (BUF_SIZE - 1))
- return (-1);
- p->buf[p->in & (BUF_SIZE - 1)] = c;
- p->in++;
- if (tx_restart) {
- tx_restart = 0;
- USART2->CR1 |= USART_FLAG_TXE; // enable tx irq
- }
- return 0;
- }
- int UGetChar() {
- buf_st *p = &rxBuf;
- if (RxBufLen == 0)
- return 0; // eof
- return (p->buf[(p->out++) & (BUF_SIZE - 1)]);
- }
- int uputstr(char *str, int len) {
- buf_st *p = &txBuf;
- __IO int idx;
- int i;
- idx = p->in;
- for (i = 0; i < len; i++) {
- p->buf[idx & (BUF_SIZE - 1)] = str[i] & 0x1FF;
- idx++;
- }
- __disable_irq();
- if (tx_restart) {
- tx_restart = 0;
- p->in = idx;
- USART2->CR1 |= USART_FLAG_TXE; // enable tx irq
- } else {
- p->in = idx;
- }
- __enable_irq();
- return i;
- }
- int uprintf(char *format, ...) {
- while (TxBufLen > 0);
- buf_st *p = &txBuf;
- char tmp[256];
- va_list arg;
- int done;
- va_start(arg, format);
- done = vsprintf(tmp, format, arg);
- va_end(arg);
- if (done) {
- __IO int idx;
- int i;
- int len = strlen(tmp);
- idx = p->in;
- for (i = 0; i < len; i++) {
- p->buf[idx & (BUF_SIZE - 1)] = tmp[i] & 0x1FF;
- idx++;
- }
- __disable_irq();
- if (tx_restart) {
- tx_restart = 0;
- p->in = idx;
- USART2->CR1 |= USART_FLAG_TXE; // enable tx irq
- } else {
- p->in = idx;
- }
- __enable_irq();
- }
- return done;
- }
- int ugetstr(char *buf, int buflen) {
- if (rx_ready > 0) {
- int cnt = 0;
- while (cnt < buflen) {
- char c = toupper(UGetChar()); // to upper
- if (c == '\n' || c == '\0') {
- buf[cnt] = 0;
- break;
- }
- buf[cnt] = c;
- cnt++;
- }
- rx_ready--;
- return cnt;
- }
- return -1;
- }
- void SetupPins(void) {
- GPIO_InitTypeDef GPIO_InitStruct;
- /* Clock for GPIOD */
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
- /* Alternating functions for pins */
- GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_TIM2);
- /* Set pins */
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12;
- GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_Init(GPIOD, &GPIO_InitStruct);
- }
- void SetupTIM2() {
- TIM_TimeBaseInitTypeDef TIM2_InitStructure;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
- TIM_TimeBaseStructInit(&TIM2_InitStructure);
- TIM2_InitStructure.TIM_Period = 1399;
- TIM2_InitStructure.TIM_Prescaler = 59999;
- TIM2_InitStructure.TIM_ClockDivision = 0;
- TIM2_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM2, &TIM2_InitStructure);
- TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
- TIM_OCInitTypeDef TIM_OCStruct;
- TIM_OCStruct.TIM_OCMode = TIM_OCMode_PWM1;
- TIM_OCStruct.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCStruct.TIM_OCPolarity = TIM_OCPolarity_Low;
- TIM_OCStruct.TIM_Pulse = 50; /* 25% duty cycle */
- TIM_OCStruct.TIM_OCIdleState = TIM_OCIdleState_Reset;
- TIM_OC1Init(TIM2, &TIM_OCStruct);
- TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
- /* Associating DMA and TIM2 Update (DMA1 Channel 3, Stream 1) */
- TIM_DMACmd(TIM2, TIM_DMA_Update, ENABLE);
- TIM_Cmd(TIM2, ENABLE);
- NVIC_InitTypeDef NVIC_InitStruct;
- NVIC_InitStruct.NVIC_IRQChannel = TIM2_IRQn;
- NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStruct);
- }
- void TIM2_IRQHandler()
- {
- // Checks whether the TIM2 interrupt has occurred or not
- if (TIM_GetITStatus(TIM2, TIM_IT_Update))
- {
- // Toggle orange LED (GPIO13)
- //TIM2->ARR = TIM2->ARR - 100;
- TIM2->CCR1 = TIM2->ARR/2;
- uprintf("ARR: %d CCR1: %d\n", TIM2->ARR, TIM2->CCR1);
- // Clears the TIM2 interrupt pending bit
- TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
- }
- }
- void DMA_Configuration(void)
- {
- DMA_InitTypeDef DMA_InitStruct;
- /* TIM2_UP - DMA1, Channel 3, Stream 1 */
- /* DMA1 clock enable */
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
- DMA_InitStruct.DMA_Channel = DMA_Channel_3;
- DMA_InitStruct.DMA_BufferSize = PWM_ELEMENTS;
- DMA_InitStruct.DMA_DIR = DMA_DIR_MemoryToPeripheral;
- DMA_InitStruct.DMA_Mode = DMA_Mode_Circular;
- DMA_InitStruct.DMA_FIFOMode = DMA_FIFOMode_Disable;
- DMA_InitStruct.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
- DMA_InitStruct.DMA_Memory0BaseAddr = (uint32_t)&PWM_Buffer[0];
- DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
- DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
- DMA_InitStruct.DMA_MemoryBurst = DMA_MemoryBurst_Single;
- DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)&TIM2->ARR;
- DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
- DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
- DMA_InitStruct.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
- DMA_InitStruct.DMA_Priority = DMA_Priority_VeryHigh;
- DMA_Init(DMA1_Stream1, &DMA_InitStruct);
- /* Enabling DMA */
- DMA_Cmd(DMA1_Stream1, ENABLE);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement