/** ****************************************************************************** * @file Audio_playback_and_record/src/main.c * @author MCD Application Team * @version V1.0.0 * @date 28-October-2011 * @brief Main program body ****************************************************************************** * @attention * * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. * *

© COPYRIGHT 2011 STMicroelectronics

*/ /* Includes ------------------------------------------------------------------*/ #include "main.h" #include "spi_3201.h" #include #include #include #include "stm32f4xx_rtc.h" /* Exported macro ------------------------------------------------------------*/ /* Select 3201: Chip Select pin low */ //#define 3201_CS_LOW() GPIO_ResetBits(GPIOB, GPIO_Pin_12) /* Deselect 3201: Chip Select pin high */ //#define 3201_CS_HIGH() GPIO_SetBits(GPIOB, GPIO_Pin_12) /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ #define ADC3_DR_ADDRESS ((uint32_t)0x4001224C) #define RTC_CLOCK_SOURCE_LSE /* LSE used as RTC source clock */ /* #define RTC_CLOCK_SOURCE_LSI */ /* LSI used as RTC source clock. The RTC Clock may varies due to LSI frequency dispersion. */ /* Private variables ---------------------------------------------------------*/ static __IO uint32_t TimingDelay; extern uint8_t NbrOfDataToTransfer; extern uint8_t NbrOfDataToRead; extern __IO uint8_t TxCounter; extern __IO uint16_t RxCounter; extern uint8_t RxBuffer[]; char *command; char *command_AT1 = "AT+CGATT?\r\n"; //char *command_AT2 = "AT+UPSD=0,1,"AIRTELGPRS.COM"\r\n"; //char command[]="vv, "as" "; unsigned char *Buffer; char bufnew [33]; char bufnew1 [33]; char bufnew2 [33]; char s[] = "fff"; unsigned char ab[64]; unsigned char bc[64]; unsigned char cd[64]; unsigned char de[64]; unsigned char ef[64]; unsigned char gh[64]; unsigned char hi[64]; unsigned char ij[64]; unsigned char jk[64]; unsigned int i; unsigned char d; unsigned char he; unsigned char value1; unsigned char value2h; unsigned char value3l; unsigned int value; unsigned int valuefin; int test; int test1; __IO uint16_t ADC3ConvertedValue = 0; __IO uint32_t ADC3ConvertedVoltage = 0; RCC_ClocksTypeDef RCC_Clocks; RTC_InitTypeDef RTC_InitStructure; RTC_TimeTypeDef RTC_TimeStructure; RTC_DateTypeDef RTC_DateStructure; RTC_TimeTypeDef RTC_TimeStampStructure; RTC_DateTypeDef RTC_TimeStampDateStructure; __IO uint32_t AsynchPrediv = 0, SynchPrediv = 0; RTC_InitTypeDef RTC_InitStructure; RTC_TimeTypeDef RTC_TimeStruct; RTC_TimeTypeDef RTC_TimeStructure; __IO uint8_t RepeatState = 0; __IO uint8_t RepeatState1 = 0; __IO uint8_t RepeatState2 = 0; __IO uint8_t RepeatState3 = 0; __IO uint8_t RepeatState4 = 0; __IO uint8_t RepeatState5 = 0; __IO uint8_t RepeatState6 = 0; __IO uint8_t RepeatState7 = 0; __IO uint8_t RepeatState8 = 0; __IO uint8_t RepeatState9 = 0; __IO uint8_t RepeatState10 = 0; __IO uint16_t CCR_Val = 16826; extern __IO uint8_t LED_Toggle; /* Private function prototypes -----------------------------------------------*/ static void TIM_LED_Config(void); void RTC_Config(void); void RTC_TimeRegulate(void); void RTC_TimeShow(void); void RCC_Configuration(void); void NVIC_Config(void); void GPIO_Configuration(void); void ADC3_CH12_DMA_Config(void); //void itoa(int,char *,int); char *strrev(char *string); int hexval(char ); char * itoa ( int value, char * str, int base ); int string_length(char*); void reverse(char*); /* Private functions ---------------------------------------------------------*/ void RCC_Configuration(void) { /* --------------------------- System Clocks Configuration -----------------*/ /* GPIOC clock enable */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); /* USART4 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE); /* GPIOC clock enable */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); /* USART5 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE); } void NVIC_Config(void) { NVIC_InitTypeDef NVIC_InitStructure; /* Enable the USARTx Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_UART4);/* UART4_TX */ GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_UART4);/* UART4_RX */ GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_UART5);/* UART4_TX */ GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_UART5);/* UART4_RX */ /*-------------------------- GPIO Configuration ----------------------------*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); /*-------------------------- GPIO Configuration ----------------------------*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD, &GPIO_InitStructure); } void USART4_Configuration(void) { USART_InitTypeDef USART_InitStructure; // USART_ClockInitTypeDef USART_ClockInitStruct; /* USARTx configuration ------------------------------------------------------*/ /* USARTx configured as follow: - BaudRate = 9600 baud - Word Length = 8 Bits - Two Stop Bit - Odd parity - Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled */ USART_InitStructure.USART_BaudRate = 9600; //115200; 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(UART4,&USART_InitStructure); //uart USART_Cmd(UART4, ENABLE); } void SendChar(char ch) { /* Wait till holding buffer empty */ while(USART_GetFlagStatus(UART4, USART_FLAG_TXE) == RESET); USART_SendData(UART4, (unsigned int) ch); } void SendString(char *s) { while(*s) { while(USART_GetFlagStatus(UART4, USART_FLAG_TXE) == RESET); USART_SendData(UART4, (unsigned int) *s++); } } unsigned int RecvChar(void) { unsigned int aa; /* Wait till holding buffer empty */ while(USART_GetFlagStatus(UART4, USART_FLAG_RXNE) == RESET); aa = (unsigned int) (USART_ReceiveData(UART4) & 0x7F); return aa; } void RecvString(unsigned char *buffer) { unsigned char data; do { while(USART_GetFlagStatus(UART4, USART_FLAG_RXNE) == RESET); // Wait for data to be received data = (unsigned char) (USART_ReceiveData(UART4) & 0x7F); *buffer = data; buffer++; // Increment the string pointer } while(data!='K'); *buffer='\0'; } /** * @brief Delay Function. * @param nCount:specifies the Delay time length. * @retval None */ void Delay(__IO uint32_t nCount) { while(nCount--) { } } /** * @brief Configures the RTC peripheral and select the clock source. * @param None * @retval None */ void RTC_Config(void) { /* Enable the PWR clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* Allow access to RTC */ PWR_BackupAccessCmd(ENABLE); /* Reset RTC Domain */ RCC_BackupResetCmd(ENABLE); RCC_BackupResetCmd(DISABLE); /* Allow access to RTC */ // PWR_RTCAccessCmd(ENABLE); /* Reset RTC Domain */ // RCC_RTCResetCmd(ENABLE); //RCC_RTCResetCmd(DISABLE); /* Enable the LSE OSC */ RCC_LSEConfig(RCC_LSE_ON); SendString("b7"); //Wait till LSE is ready while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) { } SendString("b8"); /* Select the RTC Clock Source */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); /* Configure the RTC data register and RTC prescaler */ RTC_InitStructure.RTC_AsynchPrediv = 0x7F; RTC_InitStructure.RTC_SynchPrediv = 0xFF; RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; RTC_Init(&RTC_InitStructure); /* Set the time to 00h 00mn 00s AM */ RTC_TimeStruct.RTC_H12 = RTC_H12_AM; RTC_TimeStruct.RTC_Hours = 0x00; RTC_TimeStruct.RTC_Minutes = 0x00; RTC_TimeStruct.RTC_Seconds = 0x00; RTC_SetTime(RTC_Format_BCD, &RTC_TimeStruct); /* Enable the RTC Clock */ RCC_RTCCLKCmd(ENABLE); /* Wait for RTC APB registers synchronisation */ RTC_WaitForSynchro(); } /** * @brief Display the current time on the Hyperterminal. * @param None * @retval None */ void RTC_TimeShow(void) { /* Get the current Time and Date */ SendString("aaa"); RTC_GetTime(RTC_Format_BIN, &RTC_TimeStructure); // printf("\n\r============== Current Time Display ============================\n\r"); //printf("\n\r The current time (Hour-Minute-Second) is : %0.2d:%0.2d:%0.2d \n\r", RTC_TimeStructure.RTC_Hours, RTC_TimeStructure.RTC_Minutes, RTC_TimeStructure.RTC_Seconds); SendString("bbb"); SendChar(RTC_TimeStructure.RTC_Hours); SendString(":"); SendChar(RTC_TimeStructure.RTC_Minutes); SendString(":"); SendChar(RTC_TimeStructure.RTC_Seconds); /* Unfreeze the RTC DR Register */ (void)RTC->DR; } char *itoa(int n, char *s, int b) //hex,dec,bin to string { static char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; int i=0, sign; if ((sign = n) < 0) n = -n; do { s[i++] = digits[n % b]; } while ((n /= b) > 0); if (sign < 0) s[i++] = '-'; s[i] = '\0'; reverse(s); return s; //strrev(s); } void reverse(char *string) { int length, c; char *begin, *end, temp; length = string_length(string); begin = string; end = string; for ( c = 0 ; c < ( length - 1 ) ; c++ ) end++; for ( c = 0 ; c < length/2 ; c++ ) { temp = *end; *end = *begin; *begin = temp; begin++; end--; } } int string_length(char *pointer) { int c = 0; while( *(pointer+c) != '\0' ) c++; return c; } char * utoa(unsigned int n) //unsigned int to string { char * res, buf[30]; // long enough for largest number unsigned int i, counter = 0; if (n == 0) buf[counter++] = '0'; for ( ; n; n /= 10) buf[counter++] = "0123456789"[n%10]; res = malloc(counter); for (i = 0; i < counter; ++i) res[i] = buf[counter - i - 1]; return res; } /* char * AscitoHex(char symbols) { //char symbols = "0123456789abcdef"; char result; unsigned i, lsd, hexNum; for (i = 0; i < 256; ++i) { hexNum = i; strcpy (result, "0"); do { lsd = hexNum % 16; result = symbols; hexNum /= 16; } while (hexNum); result = '\0'; return strrev (result); } } */ int hexval(char c) //ASCII representation of a hex value { int n; if ((c>='0') && (c<='9')) n = c - '0'; else if ((c>='A') && (c<='F')) n = c - 'A' + 10; else if ((c>='a') && (c<='f')) n = c - 'a' + 10; else n = 0; return n; } int main(void) { //int num = 123; //char buf[5]; int k1,k2; //SystemCoreClockUpdate(); /* Initialize LEDS */ STM_EVAL_LEDInit(LED3); STM_EVAL_LEDInit(LED4); STM_EVAL_LEDInit(LED5); STM_EVAL_LEDInit(LED6); /* Green Led On: start of application */ STM_EVAL_LEDOn(LED4); /* SysTick end of count event each 10ms */ RCC_GetClocksFreq(&RCC_Clocks); SysTick_Config(RCC_Clocks.HCLK_Frequency / 100); /* Configure TIM4 Peripheral to manage LEDs lighting */ //TIM_LED_Config(); //relay_config(); NVIC_Config(); //*** RCC_Configuration(); //*** GPIO_Configuration(); //**** USART4_Configuration(); //*** //USART5_Configuration(); /* Enable the uart receive interrupt: this interrupt is generated when the transmit data register is empty */ //USART_ITConfig(UART4, USART_IT_RXNE, ENABLE); //*** SendString("bbbb"); RTC_Config(); SendString("cccc"); while (1) { STM_EVAL_LEDOn(LED6); SendString("start\n\r"); RTC_TimeShow(); SendString("\n\rstop\n\r"); STM_EVAL_LEDOn(LED5); for(k1=0;k1<6500;k1++) { for(k2=0;k2<6500;k2++) { } //test= atoi ("234"); ***testing //sscanf(s, "%x", &test); //hex string to decimal integer value //itoa(test,bufnew1,10); ////decimal integer value to decimal string value //SendString(bufnew1); /* SendString("voltage="); sscanf(bufnew, "%x", &test1); //hex string to decimal integer value test1=test1 *4450/0xFFF; //*3300/0xFFF; itoa(test1,bufnew2,10); ////decimal integer value to decimal string value SendString(bufnew2); SendString("\n\r"); */ } } } /** * @brief ADC3 channel12 with DMA configuration * @param None * @retval None */ void ADC3_CH12_DMA_Config(void) { ADC_InitTypeDef ADC_InitStructure; ADC_CommonInitTypeDef ADC_CommonInitStructure; DMA_InitTypeDef DMA_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* Enable ADC3, DMA2 and GPIO clocks ****************************************/ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_GPIOC, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE); /* DMA2 Stream0 channel0 configuration **************************************/ DMA_InitStructure.DMA_Channel = DMA_Channel_2; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC3_DR_ADDRESS; DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADC3ConvertedValue; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; DMA_InitStructure.DMA_BufferSize = 1; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable; 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; DMA_Init(DMA2_Stream0, &DMA_InitStructure); DMA_Cmd(DMA2_Stream0, ENABLE); /* Configure ADC3 Channel12 pin as analog input ******************************/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOC, &GPIO_InitStructure); /* ADC Common Init **********************************************************/ ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2; ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; ADC_CommonInit(&ADC_CommonInitStructure); /* ADC3 Init ****************************************************************/ ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfConversion = 1; ADC_Init(ADC3, &ADC_InitStructure); /* ADC3 regular channel12 configuration *************************************/ ADC_RegularChannelConfig(ADC3, ADC_Channel_12, 1, ADC_SampleTime_3Cycles); /* Enable DMA request after last transfer (Single-ADC mode) */ ADC_DMARequestAfterLastTransferCmd(ADC3, ENABLE); /* Enable ADC3 DMA */ ADC_DMACmd(ADC3, ENABLE); /* Enable ADC3 */ ADC_Cmd(ADC3, ENABLE); } /** * @brief Configures the TIM Peripheral for Led toggling. * @param None * @retval None */ static void TIM_LED_Config(void) { TIM_OCInitTypeDef TIM_OCInitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; NVIC_InitTypeDef NVIC_InitStructure; uint16_t prescalervalue = 0; /* TIM4 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); /* Enable the TIM3 gloabal Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Initialize Leds mounted on STM324F4-EVAL board */ STM_EVAL_LEDInit(LED3); STM_EVAL_LEDInit(LED4); STM_EVAL_LEDInit(LED6); /* Compute the prescaler value */ prescalervalue = (uint16_t) ((SystemCoreClock ) / 550000) - 1; /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 65535; TIM_TimeBaseStructure.TIM_Prescaler = prescalervalue; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); /* Enable TIM4 Preload register on ARR */ TIM_ARRPreloadConfig(TIM4, ENABLE); /* TIM PWM1 Mode configuration: Channel */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = CCR_Val; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; /* Output Compare PWM1 Mode configuration: Channel2 */ TIM_OC1Init(TIM4, &TIM_OCInitStructure); TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Disable); /* TIM Interrupts enable */ TIM_ITConfig(TIM4, TIM_IT_CC1 , ENABLE); /* TIM4 enable counter */ TIM_Cmd(TIM4, ENABLE); } /** * @brief Retargets the C library printf function to the USART. * @param None * @retval None */ PUTCHAR_PROTOTYPE { /* Place your implementation of fputc here */ /* e.g. write a character to the USART */ USART_SendData(UART4, (uint8_t) ch); /* Loop until the end of transmission */ while (USART_GetFlagStatus(UART4, USART_FLAG_TC) == RESET) {} return ch; } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */ void assert_failed(uint8_t* file, uint32_t line) { /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* Infinite loop */ while (1) { } } #endif /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/