Skip to content

Instantly share code, notes, and snippets.

@a-v-s
Created March 10, 2021 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a-v-s/db4bb2deaa8696637e11a60a8b8ebc86 to your computer and use it in GitHub Desktop.
Save a-v-s/db4bb2deaa8696637e11a60a8b8ebc86 to your computer and use it in GitHub Desktop.
SPI HAL for STM32F1xx for bsrfid
/*
* spi_hal.c
*
* Created on: 29 nov. 2020
* Author: andre
*/
#include <stdbool.h>
#include "stm32f1xx.h"
#include "stm32f1xx_hal.h"
#include "stm32f1xx_hal_gpio.h"
#include "stm32f1xx_hal_spi.h"
#include "rc52x_transport.h"
static SPI_HandleTypeDef m_spi_handle;
void set_cs(bool cs) {
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, cs);
}
void set_rs(bool rs) {
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, rs);
}
int spi_transceive(void* buffer, size_t size) {
set_cs(false);
int result = HAL_SPI_TransmitReceive(&m_spi_handle, buffer, buffer, size, 1000);
set_cs(true);
return result;
}
int spi_transmit(void* buffer, size_t size, bool nostop) {
set_cs(false);
int result = HAL_SPI_Transmit(&m_spi_handle, buffer, size, 1000);
if (!nostop) set_cs(true);
return result;
}
int spi_receive(void* data, size_t size, bool nostop) {
set_cs(false);
int result = HAL_SPI_Receive(&m_spi_handle,data,size,1000);
if (!nostop) set_cs(true);
return result;
}
void reset_rfid() {
set_cs(true);
set_rs(false);
HAL_Delay(1);
set_rs(true);
HAL_Delay(1);
}
void spi_init(void* rc52x) {
__HAL_RCC_SPI2_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
m_spi_handle.Init.Mode = SPI_MODE_MASTER;
m_spi_handle.Init.Direction = SPI_DIRECTION_2LINES;
m_spi_handle.Init.DataSize = SPI_DATASIZE_8BIT;
// I assume mode 0
m_spi_handle.Init.CLKPolarity = SPI_POLARITY_LOW;
m_spi_handle.Init.CLKPhase = SPI_PHASE_1EDGE;
// The ucglib controls the line itself
m_spi_handle.Init.NSS = SPI_NSS_SOFT;
// We'll need to do speed calculations later
//m_spi_handle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
m_spi_handle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
// What is the default?
m_spi_handle.Init.FirstBit = SPI_FIRSTBIT_MSB;
m_spi_handle.Init.TIMode = SPI_TIMODE_DISABLE;
m_spi_handle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
m_spi_handle.Init.CRCPolynomial = 0;
m_spi_handle.Instance = SPI2;
int res = HAL_SPI_Init(&m_spi_handle);
if (res)
__BKPT(0);
GPIO_InitTypeDef GPIO_InitStruct;
// Common configuration for all channels
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
// Configure the SPI pins
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pin = GPIO_PIN_13 | GPIO_PIN_15;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_InitStruct.Mode = GPIO_MODE_AF_INPUT;
GPIO_InitStruct.Pin = GPIO_PIN_14;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
// Configure the output pins controlled by the lib
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pin = GPIO_PIN_8;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pin = GPIO_PIN_9;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pin = GPIO_PIN_12;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
rc52x_transport_init(rc52x, mfrc_transport_spi, spi_transmit, spi_receive, spi_transceive);
reset_rfid();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment