Files
mongoose/tutorials/nordic/xiao-nrf52840/usb/hal.h
T

243 lines
8.9 KiB
C

#pragma once
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "nrf.h"
#define BIT(x) (1UL << (x))
#define PIN(port, num) (((port) << 5) | (num))
#define PINNO(pin) ((pin) & 31)
#define PINPORT(pin) ((pin) >> 5)
#define LED_RED PIN(0, 26) // RGB user LED, each colour active low
#define LED_GREEN PIN(0, 30)
#define LED_BLUE PIN(0, 6)
#define IMU_PWR PIN(1, 8) // LSM6DS3TR-C IMU power, active high (Sense only)
#define IMU_SCL PIN(0, 27) // IMU I2C bus
#define IMU_SDA PIN(0, 7)
#define FLASH_CS PIN(0, 25) // P25Q16H 2 MB flash, used over plain SPI
#define FLASH_SCK PIN(0, 21) // The board wires it for QSPI: IO0..IO3
#define FLASH_MOSI PIN(0, 20) // IO0
#define FLASH_MISO PIN(0, 24) // IO1
#define FLASH_WP PIN(0, 22) // IO2, active low
#define FLASH_HOLD PIN(0, 23) // IO3, active low
uint64_t hal_get_tick(void);
size_t hal_ram_used(void);
size_t hal_ram_free(void);
static inline NRF_GPIO_Type *hal_gpio_bank(uint16_t pin) {
return PINPORT(pin) == 0 ? NRF_P0 : NRF_P1;
}
// Returns the pin's level. Works for outputs too, see hal_gpio_output()
static inline int hal_gpio_read(uint16_t pin) {
return hal_gpio_bank(pin)->IN & BIT(PINNO(pin)) ? 1 : 0;
}
static inline void hal_gpio_write(uint16_t pin, bool val) {
NRF_GPIO_Type *gpio = hal_gpio_bank(pin);
if (val) {
gpio->OUTSET = BIT(PINNO(pin));
} else {
gpio->OUTCLR = BIT(PINNO(pin));
}
}
static inline void hal_gpio_toggle(uint16_t pin) {
hal_gpio_write(pin, (hal_gpio_bank(pin)->OUT & BIT(PINNO(pin))) == 0);
}
// Input buffer stays connected, so hal_gpio_read() returns the output level
static inline void hal_gpio_output(uint16_t pin) {
hal_gpio_bank(pin)->PIN_CNF[PINNO(pin)] =
(GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos) |
(GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos);
}
// SPIM2 to the on-board flash, 8 MHz, mode 0. SPIM0 is not used because it
// shares its registers with TWIM0, the IMU's I2C
static inline void hal_spi_init(void) {
hal_gpio_write(FLASH_CS, true);
hal_gpio_write(FLASH_WP, true);
hal_gpio_write(FLASH_HOLD, true);
hal_gpio_write(FLASH_SCK, false);
hal_gpio_output(FLASH_CS);
hal_gpio_output(FLASH_WP);
hal_gpio_output(FLASH_HOLD);
hal_gpio_output(FLASH_SCK); // Its input buffer must stay connected
hal_gpio_output(FLASH_MOSI);
hal_gpio_bank(FLASH_MISO)->PIN_CNF[PINNO(FLASH_MISO)] =
(GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) |
(GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos);
NRF_SPIM2->PSEL.SCK = FLASH_SCK; // PIN() matches the PSEL port/pin format
NRF_SPIM2->PSEL.MOSI = FLASH_MOSI;
NRF_SPIM2->PSEL.MISO = FLASH_MISO;
NRF_SPIM2->FREQUENCY = SPIM_FREQUENCY_FREQUENCY_M8;
NRF_SPIM2->CONFIG = 0; // MSB first, mode 0
NRF_SPIM2->ORC = 0xff; // Sent when there is nothing to write
NRF_SPIM2->ENABLE = SPIM_ENABLE_ENABLE_Enabled << SPIM_ENABLE_ENABLE_Pos;
}
// struct mg_spi callbacks. spi is unused, there is one bus
static inline void hal_spi_begin(void *spi) {
(void) spi;
hal_gpio_write(FLASH_CS, false);
}
static inline void hal_spi_end(void *spi) {
(void) spi;
hal_gpio_write(FLASH_CS, true);
}
// Sends write and receives into read, len bytes. Either can be NULL. Buffers
// must be in RAM, for EasyDMA
static inline void hal_spi_txn(void *spi, uint8_t *write, uint8_t *read,
size_t len) {
(void) spi;
while (len > 0) {
size_t n = len > 0xffff ? 0xffff : len; // MAXCNT is 16 bits
NRF_SPIM2->TXD.PTR = (uint32_t) write;
NRF_SPIM2->TXD.MAXCNT = write == NULL ? 0 : n;
NRF_SPIM2->RXD.PTR = (uint32_t) read;
NRF_SPIM2->RXD.MAXCNT = read == NULL ? 0 : n;
NRF_SPIM2->EVENTS_END = 0;
NRF_SPIM2->TASKS_START = 1;
while (!NRF_SPIM2->EVENTS_END) (void) 0;
if (write != NULL) write += n;
if (read != NULL) read += n;
len -= n;
}
}
// Vectors above the bootloader, 64 MHz crystal clock, 1 ms SysTick
static inline void hal_system_init(void) {
extern uint32_t __isr_vector[];
SCB->VTOR = (uint32_t) __isr_vector;
__DSB();
__ISB();
__enable_irq();
NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
NRF_CLOCK->TASKS_HFCLKSTART = 1;
while (!NRF_CLOCK->EVENTS_HFCLKSTARTED) (void) 0;
SystemCoreClockUpdate();
SysTick_Config(SystemCoreClock / 1000);
}
static inline void hal_rng_init(void) {
NRF_RNG->CONFIG = 1; // Bias correction
NRF_RNG->TASKS_START = 1;
}
static inline uint32_t hal_rng_read(void) {
uint32_t value = 0;
for (unsigned i = 0; i < 4; i++) {
while (!NRF_RNG->EVENTS_VALRDY) (void) 0;
value |= (uint32_t) NRF_RNG->VALUE << (i * 8);
NRF_RNG->EVENTS_VALRDY = 0;
}
return value;
}
// Blocking I2C transfer on TWIM0: writes tx, then reads rxlen bytes into rx
// if rxlen > 0. Buffers must be in RAM, for EasyDMA. Returns true on success
static inline bool hal_i2c_xfer(uint8_t addr, uint8_t *tx, size_t txlen,
uint8_t *rx, size_t rxlen) {
NRF_TWIM_Type *twim = NRF_TWIM0;
uint64_t deadline = hal_get_tick() + 5;
bool ok = true;
twim->ADDRESS = addr;
twim->TXD.PTR = (uint32_t) tx;
twim->TXD.MAXCNT = txlen;
twim->RXD.PTR = (uint32_t) rx;
twim->RXD.MAXCNT = rxlen;
twim->SHORTS = rxlen ? TWIM_SHORTS_LASTTX_STARTRX_Msk | TWIM_SHORTS_LASTRX_STOP_Msk
: TWIM_SHORTS_LASTTX_STOP_Msk;
twim->ERRORSRC = twim->ERRORSRC; // Write 1s to clear
twim->EVENTS_ERROR = 0;
twim->EVENTS_STOPPED = 0;
twim->TASKS_STARTTX = 1;
while (!twim->EVENTS_STOPPED && hal_get_tick() < deadline) {
if (twim->EVENTS_ERROR) { // E.g. NACK. The bus does not stop by itself
twim->EVENTS_ERROR = 0;
twim->TASKS_STOP = 1;
ok = false;
}
}
if (!twim->EVENTS_STOPPED) twim->TASKS_STOP = 1, ok = false; // Timed out
return ok;
}
// LSM6DS3TR-C IMU registers and values, see Seeed_Arduino_LSM6DS3
#define IMU_WHO_AM_I 0x0f // Reads 0x6a for the TR-C, 0x69 for older parts
#define IMU_CTRL1_XL 0x10 // Accelerometer control
#define IMU_XL_104HZ_2G 0x40 // 104 Hz output rate, +-2 g full scale
#define IMU_OUTX_L_XL 0x28 // X acceleration, low byte, then X high, Y, Z
// The IMU's I2C address lives in TWIM0's ADDRESS register, set by
// hal_imu_init(), and TWIM0 is left disabled if there is no IMU. So this
// header keeps no state, which would be one copy per file including it
static inline bool hal_imu_found(void) {
return NRF_TWIM0->ENABLE == (TWIM_ENABLE_ENABLE_Enabled << TWIM_ENABLE_ENABLE_Pos);
}
// Reads one IMU register into val. Returns true on success
static inline bool hal_imu_read_reg(uint8_t reg, uint8_t *val) {
return hal_i2c_xfer((uint8_t) NRF_TWIM0->ADDRESS, &reg, 1, val, 1);
}
// Powers up the IMU, sets up its I2C bus and the accelerometer. Returns the
// IMU's I2C address, or 0 if it does not answer
static inline int hal_imu_init(void) {
static const uint8_t addrs[] = {0x6a, 0x6b}; // Depends on the SDO pin
uint64_t t;
// The IMU is powered from this pin, so it needs high drive, as in Seeed's
// LSM6DS3 driver. Standard drive may not supply enough current
hal_gpio_bank(IMU_PWR)->PIN_CNF[PINNO(IMU_PWR)] =
(GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos) |
(GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos) |
(GPIO_PIN_CNF_DRIVE_H0H1 << GPIO_PIN_CNF_DRIVE_Pos);
hal_gpio_write(IMU_PWR, true);
t = hal_get_tick() + 10;
while (hal_get_tick() < t) (void) 0; // Let the IMU power up
for (unsigned i = 0; i < 2; i++) {
uint16_t pin = i == 0 ? IMU_SCL : IMU_SDA;
hal_gpio_bank(pin)->PIN_CNF[PINNO(pin)] =
(GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) |
(GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
(GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) |
(GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos);
}
NRF_TWIM0->PSEL.SCL = IMU_SCL; // PIN() matches the PSEL port/pin format
NRF_TWIM0->PSEL.SDA = IMU_SDA;
NRF_TWIM0->FREQUENCY = TWIM_FREQUENCY_FREQUENCY_K400
<< TWIM_FREQUENCY_FREQUENCY_Pos;
NRF_TWIM0->ENABLE = TWIM_ENABLE_ENABLE_Enabled << TWIM_ENABLE_ENABLE_Pos;
for (size_t i = 0; i < sizeof(addrs); i++) {
uint8_t id = 0, cmd[] = {IMU_CTRL1_XL, IMU_XL_104HZ_2G};
NRF_TWIM0->ADDRESS = addrs[i]; // Used by hal_imu_read_reg()
if (hal_imu_read_reg(IMU_WHO_AM_I, &id) && (id == 0x6a || id == 0x69) &&
hal_i2c_xfer(addrs[i], cmd, sizeof(cmd), NULL, 0)) {
return addrs[i];
}
}
NRF_TWIM0->ENABLE = 0; // No IMU, see hal_imu_found()
return 0;
}
// Fills raw with X, Y, Z acceleration as raw counts, -32768..32767, zeros if
// there is no IMU. At +-2 g one count is 0.061 mg, so flat on a table Z reads
// about +16400. Bytes are read one by one, not relying on register address
// auto-increment
static inline void hal_imu_accel(int *raw) {
for (int i = 0; i < 3; i++) {
uint8_t reg = (uint8_t) (IMU_OUTX_L_XL + i * 2), lo = 0, hi = 0;
raw[i] = 0;
if (!hal_imu_found() || !hal_imu_read_reg(reg, &lo) ||
!hal_imu_read_reg((uint8_t) (reg + 1), &hi)) {
continue;
}
raw[i] = (int16_t) ((hi << 8) | lo);
}
}