Advertisement
Guest User

fatfs-spi-f4discovery

a guest
Mar 22nd, 2018
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.16 KB | None | 0 0
  1. /**
  2.  ******************************************************************************
  3.  * @file           : main.c
  4.  * @brief          : Main program body
  5.  ******************************************************************************
  6.  * This notice applies to any and all portions of this file
  7.  * that are not between comment pairs USER CODE BEGIN and
  8.  * USER CODE END. Other portions of this file, whether
  9.  * inserted by the user or by software development tools
  10.  * are owned by their respective copyright owners.
  11.  *
  12.  * Copyright (c) 2018 STMicroelectronics International N.V.
  13.  * All rights reserved.
  14.  *
  15.  * Redistribution and use in source and binary forms, with or without
  16.  * modification, are permitted, provided that the following conditions are met:
  17.  *
  18.  * 1. Redistribution of source code must retain the above copyright notice,
  19.  *    this list of conditions and the following disclaimer.
  20.  * 2. Redistributions in binary form must reproduce the above copyright notice,
  21.  *    this list of conditions and the following disclaimer in the documentation
  22.  *    and/or other materials provided with the distribution.
  23.  * 3. Neither the name of STMicroelectronics nor the names of other
  24.  *    contributors to this software may be used to endorse or promote products
  25.  *    derived from this software without specific written permission.
  26.  * 4. This software, including modifications and/or derivative works of this
  27.  *    software, must execute solely and exclusively on microcontroller or
  28.  *    microprocessor devices manufactured by or for STMicroelectronics.
  29.  * 5. Redistribution and use of this software other than as permitted under
  30.  *    this license is void and will automatically terminate your rights under
  31.  *    this license.
  32.  *
  33.  * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
  34.  * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
  35.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  36.  * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
  37.  * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
  38.  * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  39.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  41.  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  42.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  43.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  44.  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45.  *
  46.  ******************************************************************************
  47.  */
  48. /* Includes ------------------------------------------------------------------*/
  49. #include "main.h"
  50. #include "stm32f4xx_hal.h"
  51. #include "fatfs.h"
  52.  
  53. /* USER CODE BEGIN Includes */
  54. #include "defines.h"
  55. /* USER CODE END Includes */
  56.  
  57. /* Private variables ---------------------------------------------------------*/
  58. SPI_HandleTypeDef hspi1;
  59.  
  60. /* USER CODE BEGIN PV */
  61. /* Private variables ---------------------------------------------------------*/
  62. FATFS FatFs;                                                                                                // fatfs object
  63. FIL fil;                                                                                                    // file object
  64. /* USER CODE END PV */
  65.  
  66. /* Private function prototypes -----------------------------------------------*/
  67. void SystemClock_Config(void);
  68. static void MX_GPIO_Init(void);
  69. static void MX_SPI1_Init(void);
  70.  
  71. /* USER CODE BEGIN PFP */
  72. /* Private function prototypes -----------------------------------------------*/
  73.  
  74. /* USER CODE END PFP */
  75.  
  76. /* USER CODE BEGIN 0 */
  77.  
  78. /* USER CODE END 0 */
  79.  
  80. /**
  81.  * @brief  The application entry point.
  82.  *
  83.  * @retval None
  84.  */
  85. int main(void)
  86. {
  87.     /* USER CODE BEGIN 1 */
  88.  
  89.     /* USER CODE END 1 */
  90.  
  91.     /* MCU Configuration----------------------------------------------------------*/
  92.  
  93.     /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  94.     HAL_Init();
  95.  
  96.     /* USER CODE BEGIN Init */
  97.  
  98.     /* USER CODE END Init */
  99.  
  100.     /* Configure the system clock */
  101.     SystemClock_Config();
  102.  
  103.     /* USER CODE BEGIN SysInit */
  104.  
  105.     /* USER CODE END SysInit */
  106.  
  107.     /* Initialize all configured peripherals */
  108.     MX_GPIO_Init();
  109.     MX_SPI1_Init();
  110.     MX_FATFS_Init();
  111.     /* USER CODE BEGIN 2 */
  112.     if (f_mount(&FatFs, "0:", 0) == FR_OK)                                                                  // mount the default driver 0:"SD"
  113.     {
  114.         HAL_GPIO_WritePin(GPIOD, RED_LED_Pin, GPIO_PIN_SET);                                                // mounted OK, turn on RED LED
  115.         //if (f_open(&fil, "0:1stfile.txt", FA_CREATE_ALWAYS | FA_WRITE | FA_READ) == FR_OK)                // try to open file
  116.         FRESULT fopen = f_open(&fil, "0:1stfile.txt", FA_CREATE_ALWAYS | FA_WRITE | FA_READ);
  117.         if (fopen == FR_OK)
  118.         {
  119.             HAL_GPIO_WritePin(GPIOD, GREEN_LED_Pin, GPIO_PIN_SET);                                          // file opened, turn off RED and turn on GREEN LED
  120.             HAL_GPIO_WritePin(GPIOD, RED_LED_Pin, GPIO_PIN_RESET);
  121.  
  122.             //if (f_puts("First string in my file\n", &fil) > 0)                                            // f_puts returns number of character encoding units written to the file. when the funciont failed EOF (-1) will be returned
  123.             FRESULT fputs = f_puts("First string in my file\n", &fil);
  124.             if (fputs > 0)
  125.                 HAL_GPIO_WritePin(GPIOD, GREEN_LED_Pin | RED_LED_Pin, GPIO_PIN_SET);                        // turn on both leds
  126.  
  127.             f_close(&fil);                                                                                  // close file, don't forget this!
  128.         }
  129.         f_mount(NULL, "0:", 0);                                                                             // unmount drive, don't forget this!
  130.     }
  131.     /* USER CODE END 2 */
  132.  
  133.     /* Infinite loop */
  134.     /* USER CODE BEGIN WHILE */
  135.     while (1)
  136.     {
  137.         /* USER CODE END WHILE */
  138.  
  139.         /* USER CODE BEGIN 3 */
  140.     }
  141.     /* USER CODE END 3 */
  142. }
  143.  
  144. /**
  145.  * @brief System Clock Configuration
  146.  * @retval None
  147.  */
  148. void SystemClock_Config(void)
  149. {
  150.     RCC_OscInitTypeDef RCC_OscInitStruct;
  151.     RCC_ClkInitTypeDef RCC_ClkInitStruct;
  152.  
  153.     /**Configure the main internal regulator output voltage
  154.      */
  155.     __HAL_RCC_PWR_CLK_ENABLE();
  156.  
  157.     __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  158.  
  159.     /**Initializes the CPU, AHB and APB busses clocks
  160.      */
  161.     RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  162.     RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  163.     RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  164.     RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  165.     RCC_OscInitStruct.PLL.PLLM = 8;
  166.     RCC_OscInitStruct.PLL.PLLN = 336;
  167.     RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  168.     RCC_OscInitStruct.PLL.PLLQ = 7;
  169.     if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  170.     {
  171.         _Error_Handler(__FILE__, __LINE__);
  172.     }
  173.  
  174.     /**Initializes the CPU, AHB and APB busses clocks
  175.      */
  176.     RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  177.             |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  178.     RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  179.     RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  180.     RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  181.     RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  182.  
  183.     if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  184.     {
  185.         _Error_Handler(__FILE__, __LINE__);
  186.     }
  187.  
  188.     /**Configure the Systick interrupt time
  189.      */
  190.     HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  191.  
  192.     /**Configure the Systick
  193.      */
  194.     HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  195.  
  196.     /* SysTick_IRQn interrupt configuration */
  197.     HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  198. }
  199.  
  200. /* SPI1 init function */
  201. static void MX_SPI1_Init(void)
  202. {
  203.     /* SPI1 parameter configuration*/
  204.     hspi1.Instance = SPI1;
  205.     hspi1.Init.Mode = SPI_MODE_MASTER;
  206.     hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  207.     hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  208.     hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  209.     hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  210.     hspi1.Init.NSS = SPI_NSS_SOFT;
  211.     hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
  212.     hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  213.     hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  214.     hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  215.     hspi1.Init.CRCPolynomial = 10;
  216.     if (HAL_SPI_Init(&hspi1) != HAL_OK)
  217.     {
  218.         _Error_Handler(__FILE__, __LINE__);
  219.     }
  220. }
  221.  
  222. /** Configure pins as
  223.  * Analog
  224.  * Input
  225.  * Output
  226.  * EVENT_OUT
  227.  * EXTI
  228.  */
  229. static void MX_GPIO_Init(void)
  230. {
  231.     GPIO_InitTypeDef GPIO_InitStruct;
  232.  
  233.     /* GPIO Ports Clock Enable */
  234.     __HAL_RCC_GPIOH_CLK_ENABLE();
  235.     __HAL_RCC_GPIOA_CLK_ENABLE();
  236.     __HAL_RCC_GPIOD_CLK_ENABLE();
  237.     __HAL_RCC_GPIOB_CLK_ENABLE();
  238.  
  239.     /*Configure GPIO pin Output Level */
  240.     HAL_GPIO_WritePin(GPIOD, GREEN_LED_Pin|ORANGE_LED_Pin|RED_LED_Pin|BLUE_LED_Pin, GPIO_PIN_RESET);
  241.  
  242.     /*Configure GPIO pin Output Level */
  243.     HAL_GPIO_WritePin(SPI1_CS_GPIO_Port, SPI1_CS_Pin, GPIO_PIN_RESET);
  244.  
  245.     /*Configure GPIO pins : GREEN_LED_Pin ORANGE_LED_Pin RED_LED_Pin BLUE_LED_Pin */
  246.     GPIO_InitStruct.Pin = GREEN_LED_Pin|ORANGE_LED_Pin|RED_LED_Pin|BLUE_LED_Pin;
  247.     GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  248.     GPIO_InitStruct.Pull = GPIO_NOPULL;
  249.     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  250.     HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  251.  
  252.     /*Configure GPIO pin : SPI1_CS_Pin */
  253.     GPIO_InitStruct.Pin = SPI1_CS_Pin;
  254.     GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  255.     GPIO_InitStruct.Pull = GPIO_NOPULL;
  256.     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  257.     HAL_GPIO_Init(SPI1_CS_GPIO_Port, &GPIO_InitStruct);
  258. }
  259.  
  260. /* USER CODE BEGIN 4 */
  261.  
  262. /* USER CODE END 4 */
  263.  
  264. /**
  265.  * @brief  This function is executed in case of error occurrence.
  266.  * @param  file: The file name as string.
  267.  * @param  line: The line in file as a number.
  268.  * @retval None
  269.  */
  270. void _Error_Handler(char *file, int line)
  271. {
  272.     /* USER CODE BEGIN Error_Handler_Debug */
  273.     /* User can add his own implementation to report the HAL error return state */
  274.     while(1)
  275.     {
  276.     }
  277.     /* USER CODE END Error_Handler_Debug */
  278. }
  279.  
  280. #ifdef  USE_FULL_ASSERT
  281. /**
  282.  * @brief  Reports the name of the source file and the source line number
  283.  *         where the assert_param error has occurred.
  284.  * @param  file: pointer to the source file name
  285.  * @param  line: assert_param error line source number
  286.  * @retval None
  287.  */
  288. void assert_failed(uint8_t* file, uint32_t line)
  289. {
  290.     /* USER CODE BEGIN 6 */
  291.     /* User can add his own implementation to report the file name and line number,
  292.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  293.     /* USER CODE END 6 */
  294. }
  295. #endif /* USE_FULL_ASSERT */
  296.  
  297. /**
  298.  * @}
  299.  */
  300.  
  301. /**
  302.  * @}
  303.  */
  304.  
  305. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement