Commit f4362a63 authored by Vidisha Srivastav's avatar Vidisha Srivastav
Browse files

matched init structure from i2c-refactor branch and got it all to build

parent 11130b09
......@@ -44,40 +44,48 @@
#define DATARATE_LOWPOWER_1K6HZ 0x1000
#define DATARATE_LOWPOWER_5KHZ 0x1001
spi_sensor_t sensor_center;
i2c_sensor_t sensor_right;
i2c_sensor_t sensor_left;
extern void common_hal_time_delay_ms(uint32_t);
static uint8_t get_spi_range(spi_sensor_t *device);
static void set_spi_data_rate(spi_sensor_t *device, uint8_t rate);
spi_sensor_t center_sensor;
/*
Initializes the Feather M4 SPI bus and predetermined body accelerometer output pin.
Creates a SPI device object for the accelerometer
*/
void init_spi_accel(spi_sensor_t *device)
void spi_accel_init(spi_sensor_t *device)
{
// Check device ID
uint8_t device_id = _read_register_byte(device, _REG_WHOAMI);
uint8_t device_id = spi_read_register_byte(device, _REG_WHOAMI);
if (device_id != 0x33) {
printf("Failed to find LIS3DH on chip select");
}
// Reboot
_write_register_byte(device, _REG_CTRL5, 0x80);
spi_write_register_byte(device, _REG_CTRL5, 0x80);
// TODO: wait/sleep for 5 ms
// Enable all axes, normal mode.
_write_register_byte(device, _REG_CTRL1, 0x07);
spi_write_register_byte(device, _REG_CTRL1, 0x07);
// Set 400Hz data rate.
set_data_rate(device, DATARATE_400_HZ);
set_spi_data_rate(device, DATARATE_400_HZ);
// High res & BDU enabled.
_write_register_byte(device, _REG_CTRL4, 0x88);
spi_write_register_byte(device, _REG_CTRL4, 0x88);
// Enable ADCs.
_write_register_byte(device, _REG_TEMPCFG, 0x80);
spi_write_register_byte(device, _REG_TEMPCFG, 0x80);
// Latch interrupt for INT1
_write_register_byte(device, _REG_CTRL5, 0x08);
spi_write_register_byte(device, _REG_CTRL5, 0x08);
}
void init_accels(void) {
center_sensor = spi_sensor_create();
// i2c_sensor_t left_i2c_sensor = i2c_sensor_create(LEFT_WING_I2CADDR);
// i2c_sensor_t right_i2c_sensor = i2c_sensor_create(RIGHT_WING_I2CADDR);
spi_accel_init(&center_sensor);
// i2c_accel_init(&left_wing, left_i2c_sensor);
// i2c_accel_init(&right_wing, right_i2c_sensor);
}
/*
......@@ -175,13 +183,13 @@ acceleration read_i2c_accelerometer(i2c_sensor_t *device){
acceleration get_raw_accel(ACCEL_TYPE side){
acceleration accel;
if (side == CENTER){
accel = read_bird_accelerometer(&sensor_center);
accel = read_bird_accelerometer(&center_sensor);
}
else if(side==RIGHTWING){
accel = read_i2c_accelerometer(&sensor_right);
//accel = read_i2c_accelerometer(&sensor_right);
}
else if (side==LEFTWING) {
accel = read_i2c_accelerometer(&sensor_left);
//accel = read_i2c_accelerometer(&sensor_left);
}
else
printf("Invalid accelerometer choice. Please select LEFTWING(0), CENTER(1) or RIGHTWING(2)");
......@@ -195,7 +203,7 @@ void reset_bird_accel(spi_sensor_t *device){
void accel_reset(ACCEL_TYPE side){
if (side == CENTER){
reset_bird_accel(&sensor_center);
reset_bird_accel(&center_sensor);
}
/*else if(side==RIGHTWING){
i2c_sensor_t wing_device = get_wing_sensor(RIGHT);
......@@ -237,6 +245,15 @@ __attribute__((unused)) static void set_spi_range(spi_sensor_t *device, uint8_t
spi_write_register_byte(device, _REG_CTRL4, ctl4);
}
//Tests the SPI bus, in the case of the LIS3DH this should print 0x33
//If this prints 0xFF, check the pins/physical connection
void test(void)
{
uint8_t id = spi_read_register_byte(&center_sensor, 0x0F);
printf("ID: 0x%X\n", id);
}
void test_spi_reset(void){
for(uint8_t i = 0; i<1000; i++){
if(!i/3){
......
......@@ -38,8 +38,10 @@ typedef struct {
typedef uint8_t ACCEL_TYPE;
void init_accels(void);
acceleration get_raw_accel (uint8_t);
void accel_reset(uint8_t);
void test_spi_reading(void);
void test_spi_reset(void);
......
......@@ -38,7 +38,6 @@
float get_bird_angle(void);
float get_wing_angle(bool left);
void set_wing_angle(bool left, int degrees_C);
void test(void);
#endif
......@@ -13,26 +13,22 @@
#define SPI_PHASE 0
#define SPI_XTRA_CLK 0
static bool initted false
static bool initted = false;
void spi_init(spi_sensor_t* device){
if(initted){
return;
}
//init spi bus
common_hal_busio_spi_construct(&device.bus, SCK, MOSI,MISO);
common_hal_busio_spi_construct(&device->bus, SCK, MOSI,MISO);
//init chip select pins
common_hal_digitalio_digitalinout_construct(&device.cs, CS_CENTER);
common_hal_digitalio_digitalinout_switch_to_output(&device.cs, true, DRIVE_MODE_PUSH_PULL);
common_hal_digitalio_digitalinout_construct(&device->cs, CS_CENTER);
common_hal_digitalio_digitalinout_switch_to_output(&device->cs, true, DRIVE_MODE_PUSH_PULL);
//construct spidevices
//These are the default values from shared-bindings/adafruit_bus_device/SPIDevice.c
uint32_t baudrate=100000;
uint8_t polarity = 0;
uint8_t phase = 0;
uint8_t extra_clocks = 0;
common_hal_adafruit_bus_device_spidevice_construct(&device.sensor, &device.bus, &device.cs, SPI_BAUD, SPI_POLARITY, SPI_PHASE, SPI_XTRA_CLK);
common_hal_adafruit_bus_device_spidevice_construct(&device->sensor, &device->bus, &device->cs, SPI_BAUD, SPI_POLARITY, SPI_PHASE, SPI_XTRA_CLK);
initted = true;
}
......@@ -43,15 +39,15 @@ spi_sensor_t spi_sensor_create(){
}
//Writes to SPI bus, make sure the device has been "entered" into
static void write(uint8_t *buffer, size_t length)
static void write(spi_sensor_t *device, uint8_t *buffer, size_t length)
{
common_hal_busio_spi_write(&sensor_center.bus, buffer, length);
common_hal_busio_spi_write(&device->bus, buffer, length);
}
//Reads from SPI bus into buffer
static void readinto(uint8_t *buffer, size_t length)
static void readinto(spi_sensor_t *device, uint8_t *buffer, size_t length)
{
common_hal_busio_spi_read(&sensor_center.bus, buffer, length, 0);
common_hal_busio_spi_read(&device->bus, buffer, length, 0);
}
//See https://github.com/adafruit/Adafruit_CircuitPython_LIS3DH/blob/master/adafruit_lis3dh.py
......@@ -67,20 +63,20 @@ static void *_read_register (spi_sensor_t *device, uint8_t reg, size_t length)
}
//Enter before beginning the operation
common_hal_adafruit_bus_device_spidevice_enter(&device->sensor);
write(device->gbuf, 1);
readinto(device->gbuf, length);
write(device, device->gbuf, 1);
readinto(device, device->gbuf, length);
common_hal_adafruit_bus_device_spidevice_exit(&device->sensor);
//Exit after completing the operation
return device->gbuf;
}
//Uncomment when needed since circuitpython doesn't compile with warnings
static void _write_register_byte(spi_sensor_t *device, uint8_t reg, uint8_t value)
void spi_write_register_byte(spi_sensor_t *device, uint8_t reg, uint8_t value)
{
device->gbuf[0] = reg & 0x7F;
device->gbuf[1] = value & 0xFF;
common_hal_adafruit_bus_device_spidevice_enter(&device->sensor);
write(device->gbuf, 2);
write(device, device->gbuf, 2);
common_hal_adafruit_bus_device_spidevice_exit(&device->sensor);
}
......@@ -89,37 +85,3 @@ uint8_t spi_read_register_byte(spi_sensor_t *device, uint8_t reg)
return *(int *)_read_register(device, reg, 1);
}
void spi_write_register_byte(spi_sensor_t *device, uint8_t reg, uint8_t value){
_write_register_byte(device, reg, value);
}
void spi_sensor_setup(spi_sensor_t *device) {
// Check device ID
uint8_t device_id = _read_register_byte(device, _REG_WHOAMI);
if (device_id != 0x33) {
printf("Failed to find LIS3DH on chip select");
}
// Reboot
_write_register_byte(device, _REG_CTRL5, 0x80);
// TODO: wait/sleep for 5 ms
// Enable all axes, normal mode.
_write_register_byte(device, _REG_CTRL1, 0x07);
// Set 400Hz data rate.
set_data_rate(device, DATARATE_400_HZ);
// High res & BDU enabled.
_write_register_byte(device, _REG_CTRL4, 0x88);
// Enable ADCs.
_write_register_byte(device, _REG_TEMPCFG, 0x80);
// Latch interrupt for INT1
_write_register_byte(device, _REG_CTRL5, 0x08);
}
//Tests the SPI bus, in the case of the LIS3DH this should print 0x33
//If this prints 0xFF, check the pins/physical connection
void test()
{
uint8_t id = _read_register_byte(&sensor_center, 0x0F);
printf("ID: 0x%X\n", id);
}
......@@ -21,8 +21,8 @@ typedef struct {
digitalio_digitalinout_obj_t cs;
} spi_sensor_t;
spi_sensor_t spi_sensor_create(void);
uint8_t spi_read_register_byte(spi_sensor_t*, uint8_t);
void spi_sensor_create(spi_sensor_t*);
void spi_write_register_byte(spi_sensor_t*, uint8_t, uint8_t);
/*uint8_t get_spi_range(spi_sensor_t*);
......
......@@ -62,8 +62,6 @@ MP_DEFINE_CONST_FUN_OBJ_KW(control_motor_tasks_obj, 1, control_motor_obj_tasks);
// This is the function that initializes the spi bus. Called once in python script.
STATIC mp_obj_t control_motor_obj_init_spi(void) {
spi_sensor_t device = get_bird_sensor();
init_spi(&device);
return mp_const_none;
}
......
......@@ -4,6 +4,8 @@
#include "ports/atmel-samd/timer_handler.h"
#include "shared-module/motor_control/__init__.h"
#include "ports/atmel-samd/common-hal/motor_control/Control.h"
#include "ports/atmel-samd/common-hal/motor_control/PID.h"
extern const mp_obj_type_t control_motor_type;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment