123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- #include "ch.h"
- #include "hal.h"
- static const SPIConfig hs_spicfg = {
- false,
- NULL,
- GPIOA,
- GPIOA_SPI1NSS,
- 0,
- 0
- };
- static const SPIConfig ls_spicfg = {
- false,
- NULL,
- GPIOA,
- GPIOA_SPI1NSS,
- SPI_CR1_BR_2 | SPI_CR1_BR_1,
- 0
- };
- static uint8_t txbuf[512];
- static uint8_t rxbuf[512];
- static THD_WORKING_AREA(spi_thread_1_wa, 256);
- static THD_FUNCTION(spi_thread_1, p) {
- (void)p;
- chRegSetThreadName("SPI thread 1");
- while (true) {
- spiAcquireBus(&SPID1);
- palClearPad(IOPORT3, GPIOC_LED);
- spiStart(&SPID1, &hs_spicfg);
- spiSelect(&SPID1);
- spiExchange(&SPID1, 512,
- txbuf, rxbuf);
- spiUnselect(&SPID1);
- spiReleaseBus(&SPID1);
- }
- }
- static THD_WORKING_AREA(spi_thread_2_wa, 256);
- static THD_FUNCTION(spi_thread_2, p) {
- (void)p;
- chRegSetThreadName("SPI thread 2");
- while (true) {
- spiAcquireBus(&SPID1);
- palSetPad(IOPORT3, GPIOC_LED);
- spiStart(&SPID1, &ls_spicfg);
- spiSelect(&SPID1);
- spiExchange(&SPID1, 512,
- txbuf, rxbuf);
- spiUnselect(&SPID1);
- spiReleaseBus(&SPID1);
- }
- }
- int main(void) {
- unsigned i;
-
- halInit();
- chSysInit();
-
- palSetPadMode(IOPORT1, 5, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
- palSetPadMode(IOPORT1, 6, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
- palSetPadMode(IOPORT1, 7, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
- palSetPadMode(IOPORT1, GPIOA_SPI1NSS, PAL_MODE_OUTPUT_PUSHPULL);
- palSetPad(IOPORT1, GPIOA_SPI1NSS);
-
- for (i = 0; i < sizeof(txbuf); i++)
- txbuf[i] = (uint8_t)i;
-
- chThdCreateStatic(spi_thread_1_wa, sizeof(spi_thread_1_wa),
- NORMALPRIO + 1, spi_thread_1, NULL);
- chThdCreateStatic(spi_thread_2_wa, sizeof(spi_thread_2_wa),
- NORMALPRIO + 1, spi_thread_2, NULL);
-
- while (true) {
- chThdSleepMilliseconds(500);
- }
- return 0;
- }
|