2020-06-25 06:09 AM
Hello,
I'm a new new member and user of STM32 microcontrollers. I've just started using a bluepill STM32F103C8T6 microcontroller and I'm trying to set up a simple SPI communication.
At the moment I do not have slaves, only a master (the STM32), and I'm monitoring the MOSI pin with an oscilloscope. That MOSI pin is set low when inactive.
I've been trying for several days now to use the HAL_SPI_TRANSMIT function.
My code is simple:
I've managed to get my daisy chain function to work but for some reason (??) it only works with certain bytes. If I send 0x05, I can see the corresponding trace on my oscilloscope (00000101). If I send 0x88, I see 10001000 going through but then the MOSI pin stays high!
I have no idea why
Any help is welcome :)
#include "main.h"
#include "spi.h"
#include "gpio.h"
void SystemClock_Config(void);
void SPI1_EnableSlave(void);
void SPI1_DisableSlave(void);
void DaisyChain_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_SPI1_Init();
DaisyChain_Init();
while (1)
{
// Turn on/off LED
HAL_GPIO_TogglePin(BP_LED_GPIO_Port, BP_LED_Pin);
// Delay in ms
HAL_Delay(100);
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB busses clocks
*/
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_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
void SPI1_EnableSlave()
{
// Set slave SS pin low
HAL_GPIO_WritePin(SPI1_CS_GPIO_Port, SPI1_CS_Pin, GPIO_PIN_RESET);
}
void SPI1_DisableSlave()
{
// Set slave SS pin high
HAL_GPIO_WritePin(SPI1_CS_GPIO_Port, SPI1_CS_Pin, GPIO_PIN_SET);
}
void DaisyChain_Init()
{
uint8_t SpiOutputBuffer;
SPI1_EnableSlave();
SpiOutputBuffer = 0x88;
HAL_SPI_Transmit(&hspi1, &SpiOutputBuffer, 1, 1);
SPI1_DisableSlave();
}
2020-06-25 06:29 AM
You can connect MOSI to MISO via 1k resistor, to take null modem =) what you write that`s you will read.
2020-06-25 06:31 AM
I can try that, but the thing is I'm not even trying to read anything, I'm just looking to spit out bytes on the MOSI...
2020-06-25 06:40 AM
> MOSI pin is set low when inactive.
Why do you think this? MOSI doesn't have an "inactive state". It's only sampled when SCK goes high/low. There's nothing invalid about MOSI staying high or low when SCK is inactive.
2020-06-25 06:50 AM
My bad, it seems like I didn't read enough on the MOSI MISO "idle state" litterature. SPI MOSI / MISO pins seem to idle indifferently high or low depending on the last bit sent and that shouldn't be an issue...
Thanks!