本版本是初级版本,编程与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等设备不知道这些设备的状态或者数据格式。这些日常的问题催发了需求。当然有人已经做了类似的工作,但是价格和封闭性是一个弊端。现在想做一款这样的小产品,代码和电路图都公开。测试期间可以利用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年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设备的接口。因为只是测试,所以还没有写上位机界面,而且如果要写上位机界面,串口不会提供这么多信息的。串口的接受到时候也会改成中断方式。
另外代码逻辑可能有不严谨的地方,请高手指正。谢谢!
顺便附上调试的照片:
这是昨天拍的,在公司里做变得时,顺便赔了一下接口,可是没来得及编程。
这是今天在家里测试的。
命令提示能够在串口中看到,大致就是三个字节长度的命令。
具体的要发送的东西,要看相关芯片手册。这里就不具体讲了。可以留在之后有时间再说。
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
分割线|||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++){
|