本版本是初級版本,編程與2016年5月1日下午。在HMC5983上做了初步測試,單仍然不能確保是否完全正確。但是因為答應沐紫五一更新,所以就敢在晚上提交了代碼。
下面是硬件連接:
1 串口使用UART1(PA9-Tx,PA10-Rx),SPI使用SPI1(PB3-SCK,PB4-MISO,PB5-MOSI,PA15-NSS).
具體的Nucleo板子的連接可參考L073的圖紙,這里簡單列舉一下連接:
CN10的27/29/31和CN7的17作為SPI口使用。CN7的18提供5V,12和16提供3.3V,20/22提供接地電源。
PS:
就像我帖子里所說,這里通過串口來調試SPI設備的接口。因為只是測試,所以還沒有寫上位機界面,而且如果要寫上位機界面,串口不會提供這么多信息的。串口的接受到時候也會改成中斷方式。
另外代碼邏輯可能有不嚴謹的地方,請高手指正。謝謝!
常常遇到人們在調試一臺新的I2C設備或者SPI設備的時候,沒有方便的設備可用來驗證身邊的硬件是好是壞。也遇到過人們對于自己拿到的CAN或者RS485等設備不知道這些設備的狀態或者數據格式。這些日常的問題催發了需求。當然有人已經做了類似的工作,但是價格和封閉性是一個弊端,F在想做一款這樣的小產品,代碼和電路圖都公開。測試期間可以利用L073的平臺來實現,等成熟時在做原理圖,美化上位機等。
大致的功能:
I2C從設備,SPI從設備的讀取。
CAN總線命令解析,可以選擇時候短幀或者長幀
TTL、232/485解析(主要是收發器不一樣)
上位機準備用QT或者C#編寫。
好吧就這么多。
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
分割線|||2016.04.27
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
沐紫姐姐留言了,收到板子有一段時間了。可是只是做了幾個簡單的測試,總線檢測儀的項目一直沒有正式開始。
好吧,是時候說明一下這款總線檢測儀的詳細情況。
PS:本來想加入CAN檢測的功能,可是詳細看了一下這個系列,發現的確不支持CAN總線。不過不要緊,如果這個項目在L0上實現,可以移植到別的上面。畢竟STM32的代碼移植性很高。
第一階段,打算分別測試和編寫各種類型總線,包括I2C,SPI,UART(IrDA、LIN、Modubus)。另外添加幾個ADC采集的引腳,和PWM的輸出引腳。方便測試。打算兩周完成。
第二階段,基于RTOS編寫調度和接口程序用于與上位機通訊。起初想用現成的串口工具,然后用QT編寫一個界面用于測試。打算三周。
第三階段,做板子,并優化界面。打算三周。
詳細一點的現成調度相關的東西,稍后在寫。五一期間,先貢獻I2C和SPI讀取的實驗。然后慢慢添加Modubus之類的,畢竟手邊這種工具不多。
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
分割線|||2016.5.01 SPI總線測試(測試設備HMC5983)
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
撐著五一測試了一下SPI接口,寫了一個通過UART測試SPI的簡單程序,這只屬于最初步的工作。
下面先代碼先放上。
L073_SPI.rar
(6.67 MB, 下載次數: 19)
2016-6-17 17:27 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
本版本是初級版本,編程與2016年5月1日下午。在HMC5983上做了初步測試,單仍然不能確保是否完全正確。但是因為答應沐紫五一更新,所以就趕在晚上提交了代碼。
下面是硬件連接:
1 串口使用UART1(PA9-Tx,PA10-Rx),SPI使用SPI1(PB3-SCK,PB4-MISO,PB5-MOSI,PA15-NSS).
具體的Nucleo板子的連接可參考L073的圖紙,這里簡單列舉一下連接:
CN10的27/29/31和CN7的17作為SPI口使用。CN7的18提供5V,12和16提供3.3V,20/22提供接地電源。
PS:
就像我帖子里所說,這里通過串口來調試SPI設備的接口。因為只是測試,所以還沒有寫上位機界面,而且如果要寫上位機界面,串口不會提供這么多信息的。串口的接受到時候也會改成中斷方式。
另外代碼邏輯可能有不嚴謹的地方,請高手指正。謝謝!
順便附上調試的照片:
230302kcj9b2m6vlrgy9gh.jpg (1.89 MB, 下載次數: 90)
下載附件
2016-6-17 18:58 上傳
230311dsmjjipjmm6mz074.jpg (2.04 MB, 下載次數: 85)
下載附件
2016-6-17 18:58 上傳
這是昨天拍的,在公司里做變得時,順便賠了一下接口,可是沒來得及編程。
230645inmv288g8rtmn42s.jpg (1.57 MB, 下載次數: 66)
下載附件
2016-6-17 18:58 上傳
這是今天在家里測試的。
命令提示能夠在串口中看到,大致就是三個字節長度的命令。
具體的要發送的東西,要看相關芯片手冊。這里就不具體講了?梢粤粼谥笥袝r間再說。
231108w29928qg1nwe2xqx.png (48.67 KB, 下載次數: 89)
下載附件
2016-6-17 18:58 上傳
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
分割線|||2016.5.? I2C總線測試(測試設備MC8607)
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
其實這個已經在F401上測試過,有代碼,可是暫時還沒有整理和移植到F073,看看明天有時間嗎。因為明天要組裝一個書柜。我這過得真是勞動節。
- /**
- ******************************************************************************
- * File Name : main.c
- * Description : Main program body
- ******************************************************************************
- *
- * COPYRIGHT(c) 2016 STMicroelectronics
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * 3. Neither the name of STMicroelectronics nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "stm32l0xx_hal.h"
- #include "spi.h"
- #include "usart.h"
- #include "gpio.h"
- /* USER CODE BEGIN Includes */
- /* USER CODE END Includes */
- /* Private variables ---------------------------------------------------------*/
- /* USER CODE BEGIN PV */
- /* Private variables ---------------------------------------------------------*/
- #define SPI_PIPE_LEN 20
- //#define UART_PIPE_LEN 20
- uint8_t AR_SPI_TX[SPI_PIPE_LEN] = {0};
- uint8_t AR_SPI_RX[SPI_PIPE_LEN] = {0};
- //uint8_t AR_UART_TX[SPI_PIPE_LEN] = {0};
- //uint8_t AR_UART_RX[SPI_PIPE_LEN] = {0};
- uint32_t baudRateList[5] = {(uint32_t)SPI_BAUDRATEPRESCALER_32,(uint32_t)SPI_BAUDRATEPRESCALER_64,(uint32_t)SPI_BAUDRATEPRESCALER_128,(uint32_t)SPI_BAUDRATEPRESCALER_16,(uint32_t)SPI_BAUDRATEPRESCALER_8};
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- void Error_Handle(HAL_StatusTypeDef _state);
- void spiTask0(uint8_t len);
- void spiTask1(uint8_t cmd, uint8_t len);
- void spiTask2(uint8_t len);
- void spiTask3(uint8_t _CPOL, uint8_t _CPHA);
- void spiTask4(uint8_t _firstBit,uint8_t _baudrate);
- /* USER CODE BEGIN PFP */
- /* Private function prototypes -----------------------------------------------*/
- /* USER CODE END PFP */
- /* USER CODE BEGIN 0 */
- /* USER CODE END 0 */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- uint32_t counter = 0;
- uint8_t spiCmd[3] = {0};
- HAL_StatusTypeDef state;
- /* USER CODE END 1 */
-
- /* MCU Configuration----------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* Configure the system clock */
- SystemClock_Config();
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_SPI1_Init();
- MX_USART1_UART_Init();
- /* USER CODE BEGIN 2 */
- printf("instruction is like this CMD0 CMD1 CMD2. CMD0 is direction, CMD1 is what you want to input, CMD2 is length ");
- printf("CMD0 is direction:0-just get data, 1-send a command and get data,2-send lots of data ");
- printf("when CMD0 is 2, CMD2 means how long what you will send ");
- printf("when CMD0 is 3, you can config spi. CMD1 means CPOL(0-LOW,1-HIGH,other no change),CMD2 means CPHA(0:1st edge, 1:2ns edge,other- no change) ");
- printf("when CMD0 is 4, you can config spi. CMD1 means First bit select(0-MSB,1-LSB,other-no change).CMD2 Baud rate(0-1Mbps,1-500Kbps,2-250Kbps,3-2Mbps,4-4Mbps,other-nochange) ");
- printf("When CMD0 is 5, give some tips again.");
- printf("You can't give a too long parameter for CMD2. It should be not bigger than %d",(uint8_t)SPI_PIPE_LEN);
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- printf("counter is %d ",counter++);
- // HAL_GPIO_TogglePin(LD2_GPIO_Port,LD2_Pin);
- // HAL_Delay(500);
- HAL_GPIO_WritePin(LD2_GPIO_Port,LD2_Pin,GPIO_PIN_SET);
- printf("Please give me a instruction for SPI ");
- state = HAL_UART_Receive(&huart1,spiCmd,3,0xffffffff);
- if( state != HAL_OK){
- printf("UART is ");
- Error_Handle(state);
- }
- else{
- HAL_GPIO_WritePin(LD2_GPIO_Port,LD2_Pin,GPIO_PIN_RESET);
- switch(spiCmd[0]){
- case 0: printf("you want to get %d data from spi device",spiCmd[2]);spiTask0(spiCmd[2]);break;
- case 1: printf("you want to send command:0x%X to spi device",spiCmd[1]);spiTask1(spiCmd[1],spiCmd[2]);break;
- case 2: printf("you want to input %d byte data",spiCmd[2]);spiTask2(spiCmd[2]);break;
- case 3: printf("you want to config CPOL and CPHA.");break;
- case 4: printf("you want to config First bit and Baudrate.");break;
- default: printf("what you have input is wrong");break;
- }
- }
- }
- /* USER CODE END 3 */
- }
- /** System Clock Configuration
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct;
- RCC_ClkInitTypeDef RCC_ClkInitStruct;
- RCC_PeriphCLKInitTypeDef PeriphClkInit;
- __HAL_RCC_PWR_CLK_ENABLE();
- __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
- RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_8;
- RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2;
- HAL_RCC_OscConfig(&RCC_OscInitStruct);
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
- |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1);
- PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
- PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
- HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
- HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
- HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
- /* SysTick_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
- }
- /* USER CODE BEGIN 4 */
- void Error_Handle(HAL_StatusTypeDef _state){
- switch(_state){
- case HAL_ERROR: printf("error");break;
- case HAL_TIMEOUT: printf("timeout");break;
- case HAL_BUSY: printf("busy");break;
- default: printf(" in unknow error");break;
- }
- }
- void spiTask0(uint8_t len){
- HAL_StatusTypeDef st = HAL_SPI_Receive(&hspi1,AR_SPI_RX,len,1000);
- if(st != HAL_OK){
- printf("SPI receive task is ");
- Error_Handle(st);
- return;
- }
- else{
- printf("so what you want to get is:");
- for(uint8_t cir=0;cir<len;cir++){
- if(cir%3 == 0) printf(" ");
- printf("0x%X ",AR_SPI_RX[cir]);
- }
- }
- }
- void spiTask1(uint8_t cmd, uint8_t len){
- uint8_t send = cmd;
- if(len == 0) len = 1;
- else if(len > SPI_PIPE_LEN){
- len = SPI_PIPE_LEN;
- }
- HAL_StatusTypeDef st = HAL_SPI_TransmitReceive(&hspi1,&send,AR_SPI_RX,len,1000);
- if(st != HAL_OK){
- printf("SPI transmit task is ");
- Error_Handle(st);
- return;
- }
- else{
- printf("Device have succeed to receive your command ");
- printf(" and device give you some data:");
- for(uint8_t cir=0;cir<len;cir++){
- if(cir%3 == 0) printf(" ");
- printf("0x%X ",AR_SPI_RX[cir]);
- }
- printf("");
- }
- }
- void spiTask2(uint8_t len){
- if(len == 0) len = 1;
- else if(len > SPI_PIPE_LEN){
- len = SPI_PIPE_LEN;
- }
- HAL_StatusTypeDef state = HAL_UART_Receive(&huart1,AR_SPI_TX,len,0xffff);
- if( state != HAL_OK){
- printf("UART is ");
- Error_Handle(state);
- return;
- }
- else{
- printf("what you want to get from spi deceive is:");
- for(uint8_t cir2 = 0;cir2<len;cir2++){
- if(cir2%3 == 0) printf(" ");
- printf("0x%X ",AR_SPI_TX[cir2]);
- }
- printf("");
- }
- }
- void spiTask3(uint8_t _CPOL, uint8_t _CPHA){
- if( (_CPOL < 2) || (_CPHA < 2) ){
- if(_CPOL == 0){
- hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
- printf("CPOL will set to LOW and ");
- }
- else if(_CPOL == 1){
- hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
- printf("CPOL will set to HIGH and ");
- }
- if(_CPHA == 0){
- hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
- printf("CPHA will set to 1st Edge ");
- }
- else if(_CPHA == 1){
- hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
- printf("CPHA will set to 2nd Edge ");
- }
- HAL_SPI_Init(&hspi1);
- }
- }
- void spiTask4(uint8_t _firstBit,uint8_t _baudrate){
- uint16_t myBaudRate[5] ={1000,500,250,2000,4000};
- if( (_firstBit < 2) || (_baudrate < 5) ){
- if(_firstBit == 0){
- hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
- printf("First bit will set to MSB and ");
- }
- else if(_firstBit == 1){
- hspi1.Init.FirstBit = SPI_FIRSTBIT_LSB;
- printf("First bit will set to LSB and ");
- }
- if(_baudrate <= 4){
- hspi1.Init.BaudRatePrescaler = baudRateList[_baudrate];
- printf("BaudRate will set to %dKbps",myBaudRate[_baudrate]);
- }
- HAL_SPI_Init(&hspi1);
- }
- }
- /* USER CODE END 4 */
- #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 CODE BEGIN 6 */
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d", file, line) */
- /* USER CODE END 6 */
- }
- #endif
- /**
- * @}
- */
- /**
- * @}
- */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
復制代碼
</len;cir2++){
</len;cir++){
</len;cir++){
|