generated from Embedded_Projects/MPyATE_Template
168 lines
4.3 KiB
C
168 lines
4.3 KiB
C
/* Board */
|
|
#include "pin_mux.h"
|
|
|
|
/* SDK drivers */
|
|
#include "fsl_gpio.h"
|
|
#include "fsl_swm.h"
|
|
|
|
/* App */
|
|
#include "app_gpio.h"
|
|
#include "app_reg_if.h"
|
|
|
|
#define APP_GPIO_REG_OFFSET_CFG (0x00U)
|
|
#define APP_GPIO_REG_OFFSET_INPUT (0x01U)
|
|
#define APP_GPIO_REG_OFFSET_OUTPUT (0x02U)
|
|
|
|
#define APP_GPIO_PIN_COUNT 4
|
|
|
|
static const uint8_t s_app_gpio_id_table[APP_GPIO_PIN_COUNT] = {
|
|
BOARD_INITIOPINS_IO0_PIN,
|
|
BOARD_INITIOPINS_IO1_PIN,
|
|
BOARD_INITIOPINS_IO2_PIN,
|
|
BOARD_INITIOPINS_IO3_PIN,
|
|
};
|
|
|
|
static const uint8_t s_app_gpio_iocon_table[APP_GPIO_PIN_COUNT] = {
|
|
IOCON_INDEX_PIO0_15,
|
|
IOCON_INDEX_PIO0_11,
|
|
IOCON_INDEX_PIO0_10,
|
|
IOCON_INDEX_PIO0_20,
|
|
};
|
|
|
|
static const uint32_t s_app_gpio_mask_table[APP_GPIO_PIN_COUNT] = {
|
|
BOARD_INITIOPINS_IO0_PIN_MASK,
|
|
BOARD_INITIOPINS_IO1_PIN_MASK,
|
|
BOARD_INITIOPINS_IO2_PIN_MASK,
|
|
BOARD_INITIOPINS_IO3_PIN_MASK,
|
|
};
|
|
|
|
static inline uint16_t app_gpio_read_reg_cfg(void);
|
|
static inline uint16_t app_gpio_read_reg_input(void);
|
|
static inline uint16_t app_gpio_read_reg_output(void);
|
|
static inline void app_gpio_write_reg_cfg(uint16_t data);
|
|
static inline void app_gpio_write_reg_output(uint16_t data);
|
|
|
|
void app_gpio_init(void) {
|
|
GPIO->DIRCLR[0] = (BOARD_INITIOPINS_IO0_PIN_MASK | BOARD_INITIOPINS_IO1_PIN_MASK | BOARD_INITIOPINS_IO2_PIN_MASK |
|
|
BOARD_INITIOPINS_IO3_PIN_MASK);
|
|
}
|
|
|
|
uint16_t app_gpio_module_reg_read(uint8_t addr) {
|
|
uint16_t ret = 0x5555U;
|
|
|
|
switch (addr) {
|
|
case APP_GPIO_REG_OFFSET_CFG:
|
|
ret = app_gpio_read_reg_cfg();
|
|
break;
|
|
case APP_GPIO_REG_OFFSET_INPUT:
|
|
ret = app_gpio_read_reg_input();
|
|
break;
|
|
case APP_GPIO_REG_OFFSET_OUTPUT:
|
|
ret = app_gpio_read_reg_output();
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
void app_gpio_module_reg_write(uint8_t addr, uint16_t data) {
|
|
switch (addr) {
|
|
case APP_GPIO_REG_OFFSET_CFG:
|
|
app_gpio_write_reg_cfg(data);
|
|
break;
|
|
case APP_GPIO_REG_OFFSET_OUTPUT:
|
|
app_gpio_write_reg_output(data);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
static inline uint16_t app_gpio_read_reg_cfg(void) {
|
|
/* CFG register fields:
|
|
* GPIO0_CFG: [1:0]
|
|
* GPIO1_CFG: [3:2]
|
|
* GPIO2_CFG: [5:4]
|
|
* GPIO3_CFG: [7:6]
|
|
*
|
|
* Values:
|
|
* 2'b00: OUTPUT_PP
|
|
* 2'b01: OUTPUT_OD
|
|
* 2'b10: INPUT
|
|
* 2'b11: Undefined
|
|
*/
|
|
|
|
uint16_t ret = 0x0000U;
|
|
|
|
for (uint8_t i = 0; i < APP_GPIO_PIN_COUNT; i++) {
|
|
uint32_t dir = GPIO->DIR[0] & (s_app_gpio_mask_table[i]);
|
|
if (dir == 0UL) {
|
|
/* Input mode */
|
|
ret |= (2U << (2 * i));
|
|
} else {
|
|
/* Output mode */
|
|
if ((IOCON->PIO[s_app_gpio_iocon_table[i]] & IOCON_PIO_OD_MASK) == IOCON_PIO_OD(1)) {
|
|
/* OD mode */
|
|
ret |= (1U << (2 * i));
|
|
}
|
|
}
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
static inline void app_gpio_write_reg_cfg(uint16_t data) {
|
|
for (uint8_t i = 0; i < APP_GPIO_PIN_COUNT; i++) {
|
|
if (data & (2U << (2 * i))) {
|
|
/* Input mode */
|
|
GPIO->DIRCLR[0] = (s_app_gpio_mask_table[i]);
|
|
} else {
|
|
/* Output mode */
|
|
GPIO->DIRSET[0] = (s_app_gpio_mask_table[i]);
|
|
|
|
if (data & (1U << (2 * i))) {
|
|
/* OD mode */
|
|
IOCON->PIO[s_app_gpio_iocon_table[i]] |= IOCON_PIO_OD(1);
|
|
} else {
|
|
/* PP mode */
|
|
IOCON->PIO[s_app_gpio_iocon_table[i]] &= ~(IOCON_PIO_OD(1));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
static inline uint16_t app_gpio_read_reg_input(void) {
|
|
uint16_t ret = 0x0000U;
|
|
|
|
for (uint8_t i = 0; i < APP_GPIO_PIN_COUNT; i++) {
|
|
if (GPIO->B[s_app_gpio_id_table[i]]) {
|
|
ret |= (1U << i);
|
|
}
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
static inline uint16_t app_gpio_read_reg_output(void) {
|
|
uint16_t ret = 0x0000U;
|
|
|
|
for (uint8_t i = 0; i < APP_GPIO_PIN_COUNT; i++) {
|
|
if (GPIO->SET[0] & (s_app_gpio_mask_table[i])) {
|
|
ret |= (1U << i);
|
|
}
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
static inline void app_gpio_write_reg_output(uint16_t data) {
|
|
for (uint8_t i = 0; i < APP_GPIO_PIN_COUNT; i++) {
|
|
if (data & (1 << i)) {
|
|
GPIO->SET[0] = (s_app_gpio_mask_table[i]);
|
|
} else {
|
|
GPIO->CLR[0] = (s_app_gpio_mask_table[i]);
|
|
}
|
|
}
|
|
} |