MCU is STM32F446, 180MHz.
I've set up a PWM timer with 50% duty cycle, in one half I do SCK down and MOSI setup, in the other SCK up.
The timer operates in bursts of 2*64 bit bursts, with chip select switch in between.
Reason to re-do it with timer is that the previous implementation was just a big function that cycled through the vector and banged the bits with random timing, had to do microDelay() for the chip selects and it took too much time for a synchronous operation.
Problem is, this only works until 6µs cycle. If I go faster than that, something breaks and the MCU stalls.
Init code:
Code: Select all
HardwareTimer* FireTimer = new HardwareTimer(TIM10);
FireTimer->setMode(1, TIMER_DISABLED, NC);
FireTimer->setOverflow(6, MICROSEC_FORMAT);
FireTimer->setCaptureCompare(1, 3, MICROSEC_COMPARE_FORMAT); // 50%
FireTimer->attachInterrupt(1, fireCompareCallback);
Code: Select all
void fireCompareCallback() {
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_15, GPIO_PIN_SET); // SCK high
}
Code: Select all
void fireUpdateCallback(HardwareTimer *FireTimer, std::vector<uint8_t> *line, uint8_t *ITi, uint8_t *ITj) {
uint8_t d = (*line)[*ITi]; // size=16
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_15, GPIO_PIN_RESET); // SCK low
d = (d >> (*ITj));
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_14, ((d & 1) == 1) ? GPIO_PIN_SET : GPIO_PIN_RESET); // MOSI
(*ITj)++;
if(*ITj == 8) {
*ITj = 0;
(*ITi)++;
}
if(*ITi == 8) {
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_7, GPIO_PIN_SET); // nSS2 off
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_8, GPIO_PIN_RESET); // nSS1 on
}
if(*ITi == 16) {
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_8, GPIO_PIN_SET); // nSS1 off
FireTimer->pause();
}
}
Code: Select all
void firePatternIT(std::vector<uint8_t> line) {
ITi = 0;
ITj = 0;
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_7, GPIO_PIN_RESET); // nSS2 on
FireTimer->attachInterrupt(std::bind(fireUpdateCallback, FireTimer, &line, &ITi, &ITj));
FireTimer->resume();
}