n SCREENSHOT 8. Like the entries in Screenshot 7, these SPI settings will be turned into SPI portal configuration code
by STMCube32MX.
/* SPI1 init function */
static void MX_SPI1_Init(void)
{
/* SPI1 parameter configuration*/
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_MASTER;
hspi1.Init.Direction = SPI_DIRECTION_1LINE;
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_
BAUDRATEPRESCALER_16;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_
DISABLE;
hspi1.Init.CRCPolynomial = 7;
hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}
{
txBite = command;
clrDC; // command mode
clrCS; // chip selected
HAL_SPI_Transmit(&hspi1,&txBite,1,1000);
setCS; // deselect chip
}
//***********************************************
//* LCD Write Data
//***********************************************
void writeData(uint8_t data)
{
txBite = data;
setDC; // data mode
clrCS; // chip selected
HAL_SPI_Transmit(&hspi1,&txBite,1,1000);
setCS; // deselect chip
Again, the SPI init code is self-commenting. You
can alter the final code by simply following the current
definition backwards to the origin of all the definitions for a
particular structure entry. For instance, if you trace back the
Note that there is no read data command. That’s
because the CFAL9664B-F-B1-CB doesn’t allow any LCD
read operations in SPI mode. You can get a list of all the
available STM32F030R8T6 HAL functions by clicking on
the Functions tab in the Keil Project window. If you’re
curious as to “how did they do that,” you can follow the
trail backwards from the HAL function to the low-level
calls in the Keil uVision IDE (integrated development
environment).
SPI_BAUDRATEPRESCALER_XXX alias, you’ll find this:
If you’re wondering where the SPI initialization
code is, STMCube32MX put it into the project source
automatically. Let’s take a look at it:
/** @defgroup SPI_BaudRate_Prescaler SPI BaudRate
Prescaler
@{
*/
#define SPI_BAUDRATEPRESCALER_2 (0x00000000U)
#define SPI_BAUDRATEPRESCALER_4 (SPI_CR1_BR_0)
#define SPI_BAUDRATEPRESCALER_8 (SPI_CR1_BR_1)
#define SPI_BAUDRATEPRESCALER_16 (SPI_CR1_BR_1 |
SPI_CR1_BR_0)
#define SPI_BAUDRATEPRESCALER_32 (SPI_CR1_BR_2)
#define SPI_BAUDRATEPRESCALER_64 (SPI_CR1_BR_2 |
SPI_CR1_BR_0)
#define SPI_BAUDRATEPRESCALER_128 (SPI_CR1_BR_2 |
SPI_CR1_BR_1)
#define SPI_BAUDRATEPRESCALER_256 (SPI_CR1_BR_2 |
SPI_CR1_BR_1 | SPI_CR1_BR_0)
So, instead of running another spin of the
STMCube32MX configurator to change the SPI_
BAUDRATEPRESCALER_XXX value, you can simply change
the prescaler value manually in the generated source code.
We now have enough code infrastructure to assemble
58 March 2018