2022-09-23 20:09:40 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include "pico/stdlib.h"
|
|
|
|
#include "hardware/gpio.h"
|
|
|
|
#include "hardware/spi.h"
|
2022-09-24 09:38:55 +00:00
|
|
|
#include "hardware/structs/spi.h"
|
|
|
|
#include "spi_nb.h"
|
2022-09-23 20:09:40 +00:00
|
|
|
|
|
|
|
const uint PIN_CS = 1;
|
|
|
|
|
|
|
|
int gyro_init_check();
|
2022-09-24 16:55:09 +00:00
|
|
|
void gyro_config();
|
2022-09-23 20:09:40 +00:00
|
|
|
int gyro_read_register_blocking(uint8_t registrer, uint8_t *tampon, uint8_t nb_a_lire);
|
|
|
|
|
|
|
|
|
|
|
|
void Gyro_Init(void){
|
|
|
|
//
|
|
|
|
gpio_set_function(16, GPIO_FUNC_SPI); // SDI
|
|
|
|
gpio_set_function(18, GPIO_FUNC_SPI); // SCK
|
|
|
|
gpio_set_function(19, GPIO_FUNC_SPI); // SDO
|
|
|
|
gpio_set_function(PIN_CS, GPIO_OUT); // CSn
|
|
|
|
|
|
|
|
gpio_init(PIN_CS);
|
|
|
|
gpio_set_dir(PIN_CS, GPIO_OUT);
|
|
|
|
cs_deselect();
|
|
|
|
|
2022-09-24 16:55:09 +00:00
|
|
|
spi_init(spi0, 100 * 1000); // SPI init @ 100 kHz
|
|
|
|
//spi_init(spi0, 2 * 1000 * 1000); // SPI init @ 2 MHz
|
2022-09-23 20:09:40 +00:00
|
|
|
|
|
|
|
//Ça doit être les valeurs par défaut, mais ça marche !
|
|
|
|
spi_set_format(spi0, 8, SPI_CPHA_1, SPI_CPOL_1, SPI_MSB_FIRST);
|
|
|
|
|
|
|
|
// Test de la présence du gyroscope :
|
|
|
|
if(gyro_init_check()){
|
|
|
|
puts("Gyroscope non trouve");
|
2022-09-24 18:54:10 +00:00
|
|
|
gyro_config();
|
2022-09-23 20:09:40 +00:00
|
|
|
}else{
|
|
|
|
puts("Gyroscope trouve");
|
2022-09-24 16:55:09 +00:00
|
|
|
gyro_config();
|
2022-09-23 20:09:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int gyro_init_check(){
|
|
|
|
// Renvoi 0 si l'initialisation s'est bien passée
|
|
|
|
// Renvoi 1 si le gyroscope n'a pas répondu
|
|
|
|
uint8_t tampon[2]="";
|
|
|
|
gyro_read_register_blocking(0x0F, tampon, 1);
|
|
|
|
if(tampon[0] == 0xd7){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
2022-09-24 16:55:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void gyro_config(){
|
|
|
|
// Registre CTRL1
|
|
|
|
// DR : 11
|
|
|
|
// BW : 10
|
|
|
|
// PD : 1
|
|
|
|
// Zen : 1
|
|
|
|
// Yen : 1
|
|
|
|
// Xen : 1
|
|
|
|
|
|
|
|
uint16_t tampon[2] = {0x20, 0b11101111};
|
2022-09-24 18:54:10 +00:00
|
|
|
uint8_t tampon2[10]="\0\0\0\0\0\0\0\0\0";
|
|
|
|
int statu, nb_read;
|
2022-09-24 16:55:09 +00:00
|
|
|
while(spi_nb_busy(spi0) == SPI_BUSY);
|
|
|
|
cs_select();
|
|
|
|
int rep = spi_nb_write_data(spi0, tampon, 2);
|
2022-09-24 18:54:10 +00:00
|
|
|
if(rep == SPI_ERR_TRANSMIT_FIFO_FULL){
|
|
|
|
printf("Erreur: spi_read_register: SPI_ERR_TRANSMIT_FIFO_FULL\n");
|
|
|
|
//return statu;
|
|
|
|
}
|
2022-09-24 16:55:09 +00:00
|
|
|
while(spi_nb_busy(spi0) == SPI_BUSY);
|
|
|
|
cs_deselect();
|
|
|
|
|
2022-09-24 18:54:10 +00:00
|
|
|
nb_read = spi_nb_read_data_8bits(spi0, tampon2);
|
|
|
|
printf("%d caracteres lus\n", nb_read);
|
|
|
|
|
|
|
|
printf("tampon2 : %#4x %#4x %#4x\n", tampon2[0], tampon2[1], tampon2[2]);
|
|
|
|
|
2022-09-24 16:55:09 +00:00
|
|
|
if (rep == SPI_ERR_TRANSMIT_FIFO_FULL){
|
|
|
|
while(1){
|
|
|
|
printf("gyro_config: SPI_ERR_TRANSMIT_FIFO_FULL\n");
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
puts("gyro_config ok !");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Registre
|
2022-09-23 20:09:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int gyro_read_register_blocking(uint8_t registrer, uint8_t *tampon, uint8_t nb_a_lire){
|
|
|
|
uint8_t reg = registrer | 0xC0 ;
|
|
|
|
int nb_recu;
|
|
|
|
cs_select();
|
|
|
|
spi_write_blocking(spi0, ®, 1);
|
|
|
|
sleep_ms(10);
|
|
|
|
nb_recu = spi_read_blocking(spi0, 0, tampon, nb_a_lire);
|
|
|
|
cs_deselect();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-09-24 16:55:09 +00:00
|
|
|
void Gyro_Read(uint16_t step_ms){
|
2022-09-24 18:54:10 +00:00
|
|
|
uint8_t tampon[10]="\0\0\0\0\0\0\0\0\0";
|
|
|
|
uint8_t tampon2[10]="ABCDEFGHI";
|
2022-09-24 16:55:09 +00:00
|
|
|
int16_t rot_x, rot_y, rot_z;
|
|
|
|
static double angle_x=0, angle_y=0, angle_z=0;
|
2022-09-23 20:09:40 +00:00
|
|
|
int nb_recu;
|
|
|
|
|
|
|
|
|
2022-09-24 16:55:09 +00:00
|
|
|
|
|
|
|
spi_read_register(spi0, 0x20, tampon, 1);
|
|
|
|
//printf ("Gyro CTRL1 (bis) : %#4x\n", tampon[1] );
|
|
|
|
|
|
|
|
//printf ("RPI SSPCPSR : %#4x\n", spi_get_hw(spi0)->cpsr );
|
|
|
|
//printf ("RPI SSPCR0 : %#4x\n", spi_get_hw(spi0)->cr0 );
|
|
|
|
|
|
|
|
|
|
|
|
//gyro_read_register_blocking(0x28, tampon, 6);
|
|
|
|
spi_read_register(spi0, 0x28, tampon, 6);
|
|
|
|
|
2022-09-24 18:54:10 +00:00
|
|
|
|
2022-09-24 16:55:09 +00:00
|
|
|
for(int i=0; i<10; i++){
|
|
|
|
printf("%#4x ", tampon[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
rot_x = -(tampon[1] + (tampon[2] << 8));
|
|
|
|
rot_y = -(tampon[3] + (tampon[4] << 8));
|
|
|
|
rot_z = -(tampon[5] + (tampon[6] << 8));
|
|
|
|
|
|
|
|
angle_x = angle_x + (double)rot_x * step_ms * 0.001 * 0.00875;
|
|
|
|
angle_y = angle_y + (double)rot_y * step_ms * 0.001 * 0.00875;
|
|
|
|
angle_z = angle_z + (double)rot_z * step_ms * 0.001 * 0.00875;
|
|
|
|
|
|
|
|
printf("rx : %f, ry : %f, rz: %f\n", angle_x, angle_y, angle_z);
|
|
|
|
|
2022-09-24 18:54:10 +00:00
|
|
|
//while(spi_nb_busy(spi0));
|
|
|
|
//spi_nb_read_data_8bits(spi0,tampon);
|
|
|
|
//printf("tampon : %s\n", tampon);
|
|
|
|
|
2022-09-24 16:55:09 +00:00
|
|
|
|
|
|
|
}
|