728x90

728x90

Monday, November 17, 2025

STM32 Blue Pill HSPI1 and MCP23S17 GPIO Example

The MCP23S17 is a 16-bit I/O expander chip that use the Serial Peripheral Interface (SPI). It has two 8-bit general purpose I/O port GPIOA and GPIOB. Its internal registers is similar to the MCP23017 that use the I2C interface. This chip a simple 28-Pin DIP or SMD package suitable for student or electronic hobbyists. For more detail about using this chip please visit this post.

STM32 Blue Pill HSPI1 and MCP23S17 GPIO Example
Simulation Program

Since it's a general I/O port we can use this this chip to control LEDs, relays, input switches, matrix key pad, LCD etc.

STM32 Blue Pill HSPI1 and MCP23S17 GPIO Example

 

In this example I use the HSPI1 communication interface of the STM32F103C8T6 to read the input from from GPIOB and then send to the output port, GPIOA. This chip has internal programmable weak pull up resistors for both ports.

The micro-controller is 64MHz, driven from its internal 8MHz HSI and PLL.

STM32 Blue Pill HSPI1 and MCP23S17 GPIO Example
Clock Configuration

The SPI mode is set to Full Duplex Master Mode with NSS disabled. Since the NSS pin is activated every 8-bit clock pulse. In this situation the Chip Select (Active Low) pin is only active from Low to High every three 24 clock pulses or three data packets. Its baud rate is set to 4MBits per seconds since it has problem when receiving SPI data. It misses some data bits at higher baud rate. 

STM32 Blue Pill HSPI1 and MCP23S17 GPIO Example
SPI Setting

 

Once transmission session contain three data bytes,

  1. Address of this chip (0x40 for writing and 0x41  for reading) since the address pins A2:A1 are wired to GND.
  2. Address of its internal register, for example the IODIRA locate at the address 0x00
  3. And data to be written to that registers, for instance writing 0x00 to IODIRA making GPIOA as digital output.

In this section I use the,

  1. HAL_SPI_Transmit
  2. HAL_SPI_Receive 

There are a lot of SPI function in the HAL libraries but I found these two functions very easy to handle. 

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2025 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"

  22. /* Private variables ---------------------------------------------------------*/
  23. SPI_HandleTypeDef hspi1;

  24. /* Private function prototypes -----------------------------------------------*/
  25. void SystemClock_Config(void);
  26. static void MX_GPIO_Init(void);
  27. static void MX_SPI1_Init(void);
  28. /* USER CODE BEGIN PFP */

  29. /*mcp23x17 registers at bank0*/
  30. #define IODIRA_0 0x00
  31. #define IODIRB_0 0x01
  32. #define IPOLA_0 0x02
  33. #define IPOLB_0 0x03
  34. #define GPINTENA 0x04
  35. #define GPINTENB 0x05
  36. #define GPPUA_0 0x0C
  37. #define GPPUB_0 0x0D
  38. #define GPIOA_0 0x12
  39. #define GPIOB_0 0x13
  40. #define OLATA_0 0x14
  41. #define OLATB_0 0x15

  42. void mcp23s17_send(uint8_t address, uint8_t data){
  43. uint8_t temp[3];
  44. temp[0]=0x40;
  45. temp[1]=address;
  46. temp[2]=data;
  47. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,0);
  48. HAL_SPI_Transmit(&hspi1, temp, 3, 1);
  49. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,1);
  50. }

  51. uint8_t mcp23s17_receive(uint8_t addr){

  52. uint8_t temp[2], data=0;
  53. temp[0]=0x41;
  54. temp[1]=addr;

  55. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,0);
  56. HAL_SPI_Transmit(&hspi1, temp, 2, 1);
  57. HAL_SPI_Receive(&hspi1,&data,1,1);
  58. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,1);

  59. return data;
  60. }

  61. int main(void)
  62. {

  63. /* MCU Configuration--------------------------------------------------------*/

  64. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  65. HAL_Init();

  66. /* Configure the system clock */
  67. SystemClock_Config();

  68. /* Initialize all configured peripherals */
  69. MX_GPIO_Init();
  70. MX_SPI1_Init();

  71. HAL_SPI_Init(&hspi1);

  72. uint8_t data;

  73. //HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,1);
  74. mcp23s17_send(IODIRA_0,0x00);
  75. mcp23s17_send(IODIRB_0,0xFF);
  76. mcp23s17_send(GPPUB_0,0xFF);

  77. for(uint8_t i=0;i<8;i++){
  78. static uint8_t data=0;
  79. data|=1<<i;
  80. mcp23s17_send(OLATA_0,data);
  81. HAL_Delay(100);
  82. }
  83. HAL_Delay(3000);
  84. while (1)
  85. {
  86. data = mcp23s17_receive(GPIOB_0);
  87. mcp23s17_send(OLATA_0,data);

  88. }
  89. /* USER CODE END 3 */
  90. }

  91. /**
  92. * @brief System Clock Configuration
  93. * @retval None
  94. */
  95. void SystemClock_Config(void)
  96. {
  97. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  98. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  99. /** Initializes the RCC Oscillators according to the specified parameters
  100. * in the RCC_OscInitTypeDef structure.
  101. */
  102. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  103. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  104. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  105. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  106. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
  107. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
  108. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  109. {
  110. Error_Handler();
  111. }
  112. /** Initializes the CPU, AHB and APB buses clocks
  113. */
  114. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  115. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  116. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  117. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  118. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  119. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  120. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  121. {
  122. Error_Handler();
  123. }
  124. }

  125. /**
  126. * @brief SPI1 Initialization Function
  127. * @param None
  128. * @retval None
  129. */
  130. static void MX_SPI1_Init(void)
  131. {

  132. /* USER CODE BEGIN SPI1_Init 0 */

  133. /* USER CODE END SPI1_Init 0 */

  134. /* USER CODE BEGIN SPI1_Init 1 */

  135. /* USER CODE END SPI1_Init 1 */
  136. /* SPI1 parameter configuration*/
  137. hspi1.Instance = SPI1;
  138. hspi1.Init.Mode = SPI_MODE_MASTER;
  139. hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  140. hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  141. hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  142. hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  143. hspi1.Init.NSS = SPI_NSS_SOFT;
  144. hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
  145. hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  146. hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  147. hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  148. hspi1.Init.CRCPolynomial = 10;
  149. if (HAL_SPI_Init(&hspi1) != HAL_OK)
  150. {
  151. Error_Handler();
  152. }
  153. /* USER CODE BEGIN SPI1_Init 2 */

  154. /* USER CODE END SPI1_Init 2 */

  155. }

  156. /**
  157. * @brief GPIO Initialization Function
  158. * @param None
  159. * @retval None
  160. */
  161. static void MX_GPIO_Init(void)
  162. {
  163. GPIO_InitTypeDef GPIO_InitStruct = {0};

  164. /* GPIO Ports Clock Enable */
  165. __HAL_RCC_GPIOA_CLK_ENABLE();

  166. /*Configure GPIO pin Output Level */
  167. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);

  168. /*Configure GPIO pin : PA4 */
  169. GPIO_InitStruct.Pin = GPIO_PIN_4;
  170. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  171. GPIO_InitStruct.Pull = GPIO_NOPULL;
  172. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  173. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  174. }

  175. /* USER CODE BEGIN 4 */

  176. /* USER CODE END 4 */

  177. /**
  178. * @brief This function is executed in case of error occurrence.
  179. * @retval None
  180. */
  181. void Error_Handler(void)
  182. {
  183. /* USER CODE BEGIN Error_Handler_Debug */
  184. /* User can add his own implementation to report the HAL error return state */
  185. __disable_irq();
  186. while (1)
  187. {
  188. }
  189. /* USER CODE END Error_Handler_Debug */
  190. }

  191. #ifdef USE_FULL_ASSERT
  192. /**
  193. * @brief Reports the name of the source file and the source line number
  194. * where the assert_param error has occurred.
  195. * @param file: pointer to the source file name
  196. * @param line: assert_param error line source number
  197. * @retval None
  198. */
  199. void assert_failed(uint8_t *file, uint32_t line)
  200. {
  201. /* USER CODE BEGIN 6 */
  202. /* User can add his own implementation to report the file name and line number,
  203. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  204. /* USER CODE END 6 */
  205. }
  206. #endif /* USE_FULL_ASSERT */

  207. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/



I just use the simulator since I don't have this chip in my own lab. It was release many years now that I could not find it at local store. It is available from some popular online store such as Ali-Express, E bay or Amazon. 

STM32 Blue Pill HSPI1 and MCP23S17 GPIO Example
Input Reading #1

 
STM32 Blue Pill HSPI1 and MCP23S17 GPIO Example
Input Reading #2

Click here to download its source file. 


 



No comments:

Post a Comment

320x50

Search This Blog

tyro-728x90