|
|
@ -76,23 +76,11 @@ void MBIA045_set_current_gain(uint8_t gain); |
|
|
|
void MBIA045_write_config_register(uint16_t regValue); |
|
|
|
void MBIA045_set_color(int index, uint8_t red, uint8_t green, uint8_t blue); |
|
|
|
void MBIA045_set_color_all(uint8_t red, uint8_t green, uint8_t blue); |
|
|
|
void MBIA045_update_pwm_buffers(PWMDriver *pwm); |
|
|
|
void MBIA045_update_pwm_buffers(void); |
|
|
|
void MBIA045_flush(void); |
|
|
|
void MBIA045_disable_rows(void); |
|
|
|
void MBIA045_select_row(int row); |
|
|
|
|
|
|
|
static const PWMConfig g_pwm_cfg = { |
|
|
|
2000000, |
|
|
|
5, |
|
|
|
NULL, |
|
|
|
{ |
|
|
|
{PWM_OUTPUT_ACTIVE_HIGH, NULL}, // Channel 0, pin PA15 |
|
|
|
{PWM_OUTPUT_DISABLED, MBIA045_update_pwm_buffers}, |
|
|
|
{PWM_OUTPUT_DISABLED, NULL}, |
|
|
|
{PWM_OUTPUT_DISABLED, NULL}, |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
void MBIA045_init(void) { |
|
|
|
/* Initialise all PWM arrays to zero. |
|
|
|
* Perform one group transfer to turn LEDs off |
|
|
@ -112,12 +100,59 @@ void MBIA045_init(void) { |
|
|
|
* which is used for row refresh/enable. |
|
|
|
*/ |
|
|
|
|
|
|
|
palSetPadMode(GPIOA, 12, 1); |
|
|
|
// Use the HCLK |
|
|
|
// Note we need to 0 the bits first as the clksel register has a reset value |
|
|
|
// of 0xFFFF_FFFF |
|
|
|
CLK->CLKSEL1 &= ~CLK_CLKSEL1_PWM01_S_Msk; |
|
|
|
CLK->CLKSEL1 |= 2 << CLK_CLKSEL1_PWM01_S_Pos; |
|
|
|
|
|
|
|
CLK->CLKSEL2 &= ~CLK_CLKSEL2_PWM01_S_E_Msk; |
|
|
|
CLK->CLKSEL2 |= 2 << CLK_CLKSEL2_PWM01_S_E_Pos; |
|
|
|
|
|
|
|
// Enable the PWM peripheral clock |
|
|
|
CLK->APBCLK |= 1 << CLK_APBCLK_PWM01_EN_Pos; |
|
|
|
|
|
|
|
// Set prescaler for PWM0 and PWM1 to 1 |
|
|
|
PWMA->PPR |= 1 << PWM_PPR_CP01_Pos; |
|
|
|
|
|
|
|
// Enable PWM interrupt vector |
|
|
|
// Interrupt priority value taken from chibios defaults |
|
|
|
nvicEnableVector(NUC123_PWMA_NUMBER, 3); |
|
|
|
|
|
|
|
// Set clock division to 1 |
|
|
|
PWMA->CSR |= 1 << PWM_CSR_CSR2_Pos; |
|
|
|
|
|
|
|
// Set pin PA12 to PWM0 |
|
|
|
SYS->GPA_MFP |= 1 << 12; |
|
|
|
|
|
|
|
// Enable PWM0 output |
|
|
|
PWMA->POE |= 1 << PWM_POE_PWM0_Pos; |
|
|
|
|
|
|
|
pwmStart(&PWMD1, &g_pwm_cfg); |
|
|
|
// Enable PWM0 and PWM1 auto reload |
|
|
|
PWMA->PCR |= (1 << PWM_PCR_CH0MOD_Pos | 1 << PWM_PCR_CH1MOD_Pos); |
|
|
|
|
|
|
|
pwmEnableChannel(&PWMD1, 0, 10); |
|
|
|
pwmEnableChannel(&PWMD1, 1, 100000); |
|
|
|
// Enable PWM1 reset interrupt |
|
|
|
PWMA->PIER |= 1 << PWM_PIER_PWMIE1_Pos; |
|
|
|
|
|
|
|
// Set PWM0 freq to 9MHz and 50% duty cycle (it's just a nice clock) |
|
|
|
// |
|
|
|
// freq = HCLK/[(prescale+1)*(clock divider)*(CNR+1)] |
|
|
|
// 72MHz/(1 + 1)*(1)*(3+1) |
|
|
|
// |
|
|
|
// duty = (CMR+1)/(CNR+1) |
|
|
|
// (1+1)/(3+1) |
|
|
|
PWMA->CNR0 = 3; |
|
|
|
PWMA->CMR0 = 1; |
|
|
|
|
|
|
|
// Set PWM1 freq to 1.8kHz (duty doesn't matter) |
|
|
|
// |
|
|
|
// freq = HCLK/[(prescale+1)*(clock divider)*(CNR+1)] |
|
|
|
// 72MHz/(1 + 1)*(1)*(3+1) |
|
|
|
PWMA->CNR1 = 19999; |
|
|
|
PWMA->CMR1 = 1; |
|
|
|
|
|
|
|
// Start PWM channel 0 and 1 |
|
|
|
PWMA->PCR |= (1 << PWM_PCR_CH0EN_Pos | 1 << PWM_PCR_CH1EN_Pos); |
|
|
|
|
|
|
|
MBIA045_disable_rows(); |
|
|
|
|
|
|
@ -125,8 +160,6 @@ void MBIA045_init(void) { |
|
|
|
MBIA045_EN = PAL_LOW; |
|
|
|
|
|
|
|
MBIA045_set_current_gain(0b000011u); |
|
|
|
|
|
|
|
pwmEnableChannelNotification(&PWMD1, 1); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@ -229,7 +262,7 @@ void MBIA045_set_color_all(uint8_t red, uint8_t green, uint8_t blue) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void MBIA045_update_pwm_buffers(PWMDriver *pwm) { |
|
|
|
void MBIA045_update_pwm_buffers(void) { |
|
|
|
/** |
|
|
|
* Pass current PWM row to MBIA045 shift registers |
|
|
|
* |
|
|
@ -413,6 +446,19 @@ void MBIA045_select_row(int row) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
OSAL_IRQ_HANDLER(NUC123_PWMA_HANDLER) { |
|
|
|
OSAL_IRQ_PROLOGUE(); |
|
|
|
|
|
|
|
/* Check for PWM1 underflow IRQ */ |
|
|
|
if ((PWMA->PIIR >> PWM_PIIR_PWMIF1_Pos) & 1) { |
|
|
|
/* Clear interrupt flag */ |
|
|
|
PWMA->PIIR |= 1 << PWM_PIIR_PWMIF1_Pos; |
|
|
|
MBIA045_update_pwm_buffers(); |
|
|
|
} |
|
|
|
|
|
|
|
OSAL_IRQ_EPILOGUE(); |
|
|
|
} |
|
|
|
|
|
|
|
const rgb_matrix_driver_t rgb_matrix_driver = { |
|
|
|
.init = MBIA045_init, |
|
|
|
.flush = MBIA045_flush, |
|
|
|