Advertisement
Guest User

Untitled

a guest
Aug 1st, 2015
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.54 KB | None | 0 0
  1. /*
  2.  * This program turns on the 4 leds of the stm32f4 discovery board
  3.  * one after another.
  4.  * It defines shortcut definitions for the led pins and
  5.  * stores the order of the leds in an array which is being
  6.  * iterated in a loop.
  7.  *
  8.  * This program is free human culture like poetry, mathematics
  9.  * and science. You may use it as such.
  10.  */
  11.  
  12. /* stm32f4_discovery.h is located in Utilities/STM32F4-Discovery
  13.  * and defines the GPIO Pins where the leds are connected.
  14.  * Including this header also includes stm32f4xx.h and
  15.  * stm32f4xx_conf.h, which includes stm32f4xx_gpio.h
  16.  */
  17. #include "stm32f4_discovery.h"
  18.  
  19. #define MAX_STRLEN 12
  20. volatile char received_string[MAX_STRLEN+1];
  21. /*
  22.  * UART
  23.  * USART_InitTypeDef usart1init = {
  24.  *      .USART_BaudRate = 9600,
  25.  *      .USART_WordLength = 8,
  26.  *      .USART_StopBits = 0,
  27.  *      .USART_Parity = 0,
  28.  *      .USART_Mode = 0,
  29.  *      .USART_HardwareFlowControl = 0};
  30.  * */
  31.  
  32. /*
  33.  * USART
  34.  * USART_ClockInitTypeDef usart1clk = {
  35.  *      .USART_Clock = 0,
  36.  *      .USART_CPOL = 0,
  37.  *      .USART_CPHA = 0,
  38.  *      .USART_LastBit = 0};
  39.  * */
  40.  
  41. #define USART_GPIO_PORT (GPIOA)
  42.  
  43. /* We make code more readable by giving the led-pins
  44.  * symbolic names depending on the led color.
  45.  * The values LED4_PIN etc are defined in stm32f4_discovery.h
  46.  * as GPIO_Pin_12 etc.
  47.  */
  48. #define GREEN  LED4_PIN
  49. #define ORANGE LED3_PIN
  50. #define RED    LED5_PIN
  51. #define BLUE   LED6_PIN
  52. #define ALL_LEDS (GREEN | ORANGE | RED | BLUE) // all leds
  53.  
  54. /* This is how long we wait in the delay function. */
  55. #define PAUSE_LONG  4000000L
  56. #define PAUSE_SHORT 1000000L
  57.  
  58. /* The GPIO port where the leds are connected
  59.  * (same pin numbers are present on many GPIO ports).
  60.  * Leds are connected to pins 12 through 15 on GPIO port D,
  61.  * so we use the port GPIOD.
  62.  * GPIOD is just a memory address casted to a GPIO_TypeDef pointer
  63.  * GPIO_TypeDef is defined in stm32f4xx.h
  64.  */
  65. #define LEDS_GPIO_PORT (GPIOD)
  66.  
  67. /* This array stores the led order used to switch them on and off.
  68.  * We use this order by iterating over the array.
  69.  * LEDn is the number of user leds on the discovery board
  70.  * and is defined in stm32f4_discovery.h.
  71.  */
  72. static uint16_t leds[LEDn] = {GREEN, ORANGE, RED, BLUE};
  73.  
  74.  
  75. /* Structure storing the information used to intialize
  76.  * some GPIO port, in our case the port D, where the
  77.  * leds are connected.
  78.  * It is declared in stm32f4xx_gpio.h.
  79.  */
  80. GPIO_InitTypeDef GPIO_InitStructure;
  81.  
  82.  
  83. /* A simple time consuming function.
  84.  * For a more real-world one,
  85.  * we would use timers and interrupts. */
  86. static void delay(__IO uint32_t nCount)
  87. {
  88.     while(nCount--)
  89.         __asm("nop"); // do nothing
  90. }
  91.  
  92.  
  93. static void setup_usart(uint32_t baudrate)
  94. {
  95.  
  96.     GPIO_InitTypeDef GPIO_InitStruct;
  97.  
  98.     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); // USART
  99.  
  100.     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
  101.     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
  102.     GPIO_InitStruct.GPIO_Speed = GPIO_OType_PP;
  103.     GPIO_InitStruct.GPIO_OType = GPIO_PuPd_UP;
  104.     GPIO_InitStruct.GPIO_PuPd = GPIO_Speed_100MHz;
  105.     GPIO_Init(GPIOA, &GPIO_InitStruct);
  106.    
  107.     GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
  108.     GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
  109.  
  110.     USART_InitTypeDef USART_InitStruct;
  111.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // USART
  112.  
  113.     USART_InitStruct.USART_BaudRate = baudrate;
  114.     USART_InitStruct.USART_WordLength = USART_WordLength_8b;
  115.     USART_InitStruct.USART_StopBits = USART_StopBits_1;
  116.     USART_InitStruct.USART_Parity = USART_Parity_No;
  117.     USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
  118.     USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  119.     USART_Init(USART1, &USART_InitStruct);
  120.    
  121.     USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  122.  
  123.  
  124.     /*
  125.      * Enable RX interrupt
  126.      */
  127.  
  128.    
  129.     NVIC_InitTypeDef NVIC_InitStruct;
  130.     NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;
  131.     NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
  132.     NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
  133.     NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
  134.     NVIC_Init(&NVIC_InitStruct);
  135.    
  136.     USART_Cmd(USART1, ENABLE);
  137.        
  138. }
  139.  
  140. /* Initialize the GPIOD port.
  141.  * See also the comments beginning stm32f4xx_gpio.c
  142.  * (found in the library)
  143.  */
  144. static void setup_leds(void)
  145. {
  146.    
  147.  
  148.     /* Enable the GPIOD peripheral clock before we
  149.      * actually setup GPIOD.
  150.      * This function is declared in stm32f4xx_rcc.h
  151.      * and implemented in stm32f4xx_rcc.c
  152.      */
  153.     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); // LED
  154.  
  155.  
  156.     /* which pins we select to setup
  157.      * every pin number is mapped to a bit number */
  158.     GPIO_InitStructure.GPIO_Pin   = ALL_LEDS;
  159.     /* pins in output mode */
  160.     GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_OUT;
  161.     /* high clock speed for the selected pins
  162.      * see stm32f4xx_gpio.h for different speeds
  163.      * (enum GPIOSpeed_TypeDef) */
  164.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  165.     /* operating output type for the selected pins
  166.      * see the enum GPIOOType_TypeDef in stm32f4xx_gpio.h
  167.      * for different values.
  168.      * PP stands for "push/pull", OD stands for "open drain"
  169.      * google for "push pull vs open drain" */
  170.     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  171.     /* operating Pull-up/Pull down for the selected pins */
  172.     GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
  173.  
  174.     /* Write this data into memory at the address
  175.      * mapped to GPIO device port D, where the led pins
  176.      * are connected */
  177.     GPIO_Init(LEDS_GPIO_PORT, &GPIO_InitStructure);
  178.     /* This call resolves in
  179.      * GPIO_Init((GPIO_TypeDef *) 0X40020C00, &GPIO_InitStructure)
  180.      * where 0X40020C00 is the memory address mapped to
  181.      * the GPIOD port. Without the library we would have to know all
  182.      * these memory addresses. */
  183. }
  184.  
  185. /* Turn on the color leds one after another.
  186.  * The order of the leds is defined in the array leds above.
  187.  * The functions GPIO_SetBits and GPIO_ResetBits are declared
  188.  * in stm32f4xx_gpio.h and implemented in stm32f4xx_gpio.c.
  189.  *
  190.  * You might want to look at the implementation in order to see
  191.  * how this works.
  192.  * Basically, this works by using the memory map mechanism of the
  193.  * Cortex-M4: the pins of the GPIO port D are mapped to special
  194.  * memory addresses which these function write to.
  195.  * The exact addresses are represented by the fields of the
  196.  * GPIO_TypeDef structure (that is by their offsets).
  197.  * See also the structure GPIO_TypeDef in stm32f4xx.h.
  198.  */
  199. static void led_round(void)
  200. {
  201.     int i;
  202.     for (i = 0; i < LEDn; i++)
  203.     {
  204.         /* turn on led */
  205.         GPIO_SetBits(LEDS_GPIO_PORT, leds[i]);
  206.         /* wait a while */
  207.         delay(PAUSE_LONG);
  208.         /* turn off all leds */
  209.         GPIO_ResetBits(LEDS_GPIO_PORT, ALL_LEDS);
  210.     }
  211. }
  212.  
  213. /* Turn all leds on and off 4 times. */
  214. static void flash_all_leds(void)
  215. {
  216.     int i;
  217.     for (i = 0; i < 4; i++)
  218.     {
  219.         /* Turn on all user leds */
  220.         GPIO_SetBits(LEDS_GPIO_PORT, ALL_LEDS);
  221.         /* Wait a short time */
  222.         delay(PAUSE_SHORT);
  223.         /* Turn off all leds */
  224.         GPIO_ResetBits(LEDS_GPIO_PORT, ALL_LEDS);
  225.         /* Wait again before looping */
  226.         delay(PAUSE_SHORT);
  227.     }
  228. }
  229.  
  230. void USART_puts(USART_TypeDef* USARTx, volatile char *s) {
  231.     while (*s) {
  232.         while(!(USARTx->SR & 0x00000040));
  233.         USART_SendData(USARTx, *s);
  234.         *s++;
  235.     }
  236. }
  237.  
  238.  
  239. void TxUSART(char byte)
  240. {
  241.     while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
  242.     USART_SendData(USART2, (uint8_t)byte);
  243. }
  244.  
  245.  
  246. void USART1_IRQHandler(void)
  247. {
  248.     if (USART_GetITStatus(USART1, USART_IT_RXNE)) {
  249.         static uint8_t cnt = 0;
  250.         char t = USART1->DR;
  251.        
  252.        
  253.         if((t != '\n') && (cnt < MAX_STRLEN)) {
  254.             received_string[cnt] = t;
  255.             cnt++;
  256.         }
  257.         else {
  258.             cnt=0;
  259.             USART_puts(USART1, received_string);
  260.         };
  261.        
  262.         flash_all_leds();
  263.     }
  264.  
  265. }
  266.  
  267.  
  268.  
  269. /* Main function, the entry point of this program.
  270.  * The main function is called from the startup code in file
  271.  * Libraries/CMSIS/ST/STM32F4xx/Source/Templates/TrueSTUDIO/startup_stm32f4xx.s
  272.  * (line 101)
  273.  */
  274. int main(void)
  275. {
  276.     setup_leds();
  277.     setup_usart(9600);
  278.  
  279.     //led_round();
  280.     //flash_all_leds();
  281.     //
  282.  
  283.     USART_puts(USART1, "init complete! Hello World!\r\n");
  284.  
  285.     while (1)
  286.     {
  287.         led_round();
  288.         //flash_all_leds();
  289.         //USART_puts(USART1, "FUN ");
  290.     }
  291.  
  292.     return 0; // never returns actually
  293. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement