diff --git a/bsp/src/stm32f4/Nano-particle/bsp.c b/bsp/src/stm32f4/Nano-particle/bsp.c --- a/bsp/src/stm32f4/Nano-particle/bsp.c +++ b/bsp/src/stm32f4/Nano-particle/bsp.c @@ -50,6 +50,16 @@ int bsp_init() printf("================================================================\n\r"); return 1; } +void bsp_GPIO_init() +{ + gpio_t LED_list[]={LED1, LED2, LED3}; + gpio_t LEDx; + for (int i=0;2;i++) + { + LEDx=gpioopen(LED_list[i]); + gpiosetdir(&LEDx,gpiooutdir); + } +} void bsp_GPIO_init() { diff --git a/bsp/src/stm32f4/Nano-particle/bsp.h b/bsp/src/stm32f4/Nano-particle/bsp.h --- a/bsp/src/stm32f4/Nano-particle/bsp.h +++ b/bsp/src/stm32f4/Nano-particle/bsp.h @@ -27,8 +27,9 @@ #define __MAX_OPENED_FILES__ 32 #define __FS_ROOT_SIZE__ 32 -#define LED1 PD12 -#define LED2 PD13 +#define LED1 PA0 +#define LED2 PA1 +#define LED3 PA2 extern float VREF0; diff --git a/bsp/src/stm32f4/STM32F4Discovery/STM32F4Discovery.pro b/bsp/src/stm32f4/STM32F4Discovery/STM32F4Discovery.pro --- a/bsp/src/stm32f4/STM32F4Discovery/STM32F4Discovery.pro +++ b/bsp/src/stm32f4/STM32F4Discovery/STM32F4Discovery.pro @@ -11,7 +11,7 @@ SOURCES += bsp.c HEADERS += bsp.h -LIBS+= -lgpio -luart -li2c -lspi -lpwm +LIBS+= -lgpio -luart -li2c -lspi -lpwm -ldac BSPFILE = bsp.pri diff --git a/bsp/src/stm32f4/stm32f4.pro b/bsp/src/stm32f4/stm32f4.pro --- a/bsp/src/stm32f4/stm32f4.pro +++ b/bsp/src/stm32f4/stm32f4.pro @@ -11,8 +11,7 @@ SUBDIRS += STM32F4Discovery \ OPLAYER \ STM32-E407 \ STM32F429Discovery \ - STM32F4Discovery_35LCD \ - Nano-particle + STM32F4Discovery_35LCD diff --git a/include/PERIPHERALS/adc.h b/include/PERIPHERALS/adc.h deleted file mode 100644 --- a/include/PERIPHERALS/adc.h +++ /dev/null @@ -1,93 +0,0 @@ -/*------------------------------------------------------------------------------ --- This file is a part of the libuc, microcontroler library --- Copyright (C) 2012, Alexis Jeandet --- --- This program is free software; you can redistribute it and/or modify --- it under the terms of the GNU General Public License as published by --- the Free Software Foundation; either version 3 of the License, or --- (at your option) any later version. --- --- This program is distributed in the hope that it will be useful, --- but WITHOUT ANY WARRANTY; without even the implied warranty of --- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the --- GNU General Public License for more details. --- --- You should have received a copy of the GNU General Public License --- along with this program; if not, write to the Free Software --- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -------------------------------------------------------------------------------- --- Author : Alexis Jeandet --- Mail : alexis.jeandet@gmail.com --------------------------------------------------------------------------------*/ -/* -*/ - -#ifndef ADC_H -#define ADC_H -#include - -#ifdef __cplusplus - extern "C" { -#endif - - -typedef struct adc_t -{ - void* _dev; - int cfg; - int rate; -}adc_t; - - -typedef enum -{ - adc6bits = 0x1, - adc7bits = 0x2, - adc8bits = 0x3, - adc9bits = 0x4, - adc10bits = 0x5, - adc11bits = 0x6, - adc12bits = 0x7, - adc13bits = 0x8, - adc14bits = 0x9, - adc15bits = 0xA, - adc16bits = 0xB, - adc17bits = 0xC, - adc18bits = 0xD, - adc19bits = 0xE, - adc20bits = 0xF, - adc21bits = 0x10, - adc22bits = 0x11, - adc23bits = 0x12, - adc24bits = 0x13 -}adcbits_t; - -#define ADCBITSMASK 0xF - -typedef enum -{ - adcsingle = 0x00, - adcmaster = 0x10 -}adcconvmode_t; - -#define ADCCONVMODEMASK 0x10 - - - - -#ifdef __cplusplus - } -#endif -#endif //ADC_H - - - - - - - - - - - - diff --git a/include/PERIPHERALS/dac.h b/include/PERIPHERALS/dac.h new file mode 100644 --- /dev/null +++ b/include/PERIPHERALS/dac.h @@ -0,0 +1,33 @@ +#ifndef DAC_LIB_H +#define DAC_LIB_H + +#define DAC1_12bit_right_align 0x40007408 +#define DAC1_12bit_left_align 0x4000740C +#define DAC1_8bit_right_align 0x40007410 +#define DAC2_12bit_right_align 0x40007414 +#define DAC2_12bit_left_align 0x40007418 +#define DAC2_8bit_right_align 0x4000741C +#define DAC_Dual_12bit_right_align 0x40007420 +#define DAC_Dual_12bit_left_align 0x40007424 +#define DAC_Dual_8bit_right_align 0x40007428 +#define DAC_Dual_8bit_left_align 0x4000742C + +#define DAC1 0 +#define DAC2 1 + +#define timer2 1 +#define timer3 2 +#define timer4 3 +#define timer5 4 +#define timer6 5 +#define timer7 6 +#define timer8 7 + + +extern int dacopen(uint32_t pin); +extern int dacsetconfig(int timer, int buffer_size, int dac,uint32_t dac_type, uint32_t freq,uint32_t data); + + + + +#endif // DAC_LIB_H diff --git a/include/core.h b/include/core.h --- a/include/core.h +++ b/include/core.h @@ -36,6 +36,7 @@ extern void reset_AHB2(); extern void reset_APB1(); extern void reset_APB2(); extern void configureSysTick(); +extern void delay_us(uint32_t value); extern void delay_100us(uint32_t value); extern void SysTick_Handler(void); diff --git a/src/stm32f4/DAC/dac.c b/src/stm32f4/DAC/dac.c new file mode 100644 --- /dev/null +++ b/src/stm32f4/DAC/dac.c @@ -0,0 +1,127 @@ +#include +//#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define DAC_DHR12R2_ADDRESS 0x40007414 +#define DAC_DHR8R1_ADDRESS 0x40007410 + +#define DAC1_12bit_right_align 0x40007408 +#define DAC1_12bit_left_align 0x4000740C +#define DAC1_8bit_right_align 0x40007410 +#define DAC2_12bit_right_align 0x40007414 +#define DAC2_12bit_left_align 0x40007418 +#define DAC2_8bit_right_align 0x4000741C +#define DAC_Dual_12bit_right_align 0x40007420 +#define DAC_Dual_12bit_left_align 0x40007424 +#define DAC_Dual_8bit_right_align 0x40007428 +#define DAC_Dual_8bit_left_align 0x4000742C + + + +const TIM_TypeDef* _timer_dev_table[14]={TIM1,TIM2,TIM3,TIM4,TIM5,TIM6,TIM7, + TIM8,TIM9,TIM10,TIM11,TIM12,TIM13,TIM14}; +#define DAC1 0 +#define DAC2 1 + +#define timer2 1 +#define timer3 2 +#define timer4 3 +#define timer5 4 +#define timer6 5 +#define timer7 6 +#define timer8 7 + +DAC_InitTypeDef DAC_InitStructure; + +int dacopen(uint32_t pin) +{ +#define GPIOGETPORT(gpio) ((GPIO_TypeDef*)(((((uint32_t)gpio) & (uint32_t)0x0000FF00)*(uint32_t)4) + (uint32_t)GPIOA)) +#define GPIOPORTNUM(gpio) (((uint32_t)(gpio) & (uint32_t)0x0000FF00)>>(uint32_t)8) + RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1 , ENABLE); + RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE); + gpio_t DACOUT; + DACOUT = gpioopen(pin); + DACOUT |= gpioan | gpionopulltype; + gpiosetconfig(&DACOUT); + return 0; +} + +int dacsetconfig(int timer, int buffer_size, int dac,uint32_t dac_type, uint32_t freq,uint32_t data) +{ + TIM_TypeDef* tim = _timer_dev_table[timer]; + TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; + if(timer>=timer2 && timer <= timer7)RCC_APB1PeriphClockCmd((1<<(timer-1)), ENABLE); + if(timer==timer8)RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE); + uint32_t timfreq = (81300)*buffer_size/(freq); + TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); + TIM_TimeBaseStructure.TIM_Period = timfreq; + TIM_TimeBaseStructure.TIM_Prescaler = 0; + TIM_TimeBaseStructure.TIM_ClockDivision = 0; + TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; + TIM_TimeBaseInit(tim, &TIM_TimeBaseStructure); + TIM_SelectOutputTrigger(tim, TIM_TRGOSource_Update); + TIM_Cmd(tim, ENABLE); + + DMA_InitTypeDef DMA_InitStructure; + if (timer == timer2)DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO; + if (timer == timer4)DAC_InitStructure.DAC_Trigger = DAC_Trigger_T4_TRGO; + if (timer == timer5)DAC_InitStructure.DAC_Trigger = DAC_Trigger_T5_TRGO; + if (timer == timer6)DAC_InitStructure.DAC_Trigger = DAC_Trigger_T6_TRGO; + if (timer == timer7)DAC_InitStructure.DAC_Trigger = DAC_Trigger_T7_TRGO; + if (timer == timer8)DAC_InitStructure.DAC_Trigger = DAC_Trigger_T8_TRGO; + DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None; + DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable; + if (dac == DAC1){ + DAC_Init(DAC_Channel_1, &DAC_InitStructure); + DMA_DeInit(DMA1_Stream5); + } + + if (dac == DAC2){ + DAC_Init(DAC_Channel_2, &DAC_InitStructure); + DMA_DeInit(DMA1_Stream6); + } + + DMA_InitStructure.DMA_Channel = DMA_Channel_7; + DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)dac_type; + DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)data; + DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; + DMA_InitStructure.DMA_BufferSize = buffer_size; + DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; + DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; + DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; + DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; + DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; + DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; + DMA_InitStructure.DMA_Priority = DMA_Priority_High; + DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; + DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; + DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; + DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; + + if (dac == DAC1){ + DMA_Init(DMA1_Stream5, &DMA_InitStructure); + DMA_Cmd(DMA1_Stream5, ENABLE); + DAC_DMACmd(DAC_Channel_1, ENABLE); + DAC_Cmd(DAC_Channel_1, ENABLE); + + } + if (dac == DAC2){ + DMA_Init(DMA1_Stream6, &DMA_InitStructure); + DMA_Cmd(DMA1_Stream6, ENABLE); + DAC_DMACmd(DAC_Channel_2, ENABLE); + DAC_Cmd(DAC_Channel_2, ENABLE); + + } + return 0; + +} + + diff --git a/src/stm32f4/stm32f4.pro b/src/stm32f4/stm32f4.pro --- a/src/stm32f4/stm32f4.pro +++ b/src/stm32f4/stm32f4.pro @@ -8,6 +8,7 @@ SUBDIRS = CORE/core.pro \ SPI/spi.pro \ I2C/i2c.pro \ PWM/pwm.pro \ + DAC/dac.pro\ SDCARD-SDIO/sdcard-sdio.pro