commit 8433c1b6c752c4232c13215a6ee68313ab179e82 Author: Samuel Date: Fri Sep 23 19:43:45 2022 +0200 Initialisation de la communication avec le Gyroscope diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..de6d037 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,18 @@ +cmake_minimum_required(VERSION 3.13) +include(pico_sdk_import.cmake) +project(test_project C CXX ASM) +set(CMAKE_C_STANDARD 11) +set(CMAKE_CXX_STANDARD 17) +pico_sdk_init() +add_executable(test +test.c +) +pico_enable_stdio_usb(test 1) +pico_enable_stdio_uart(test 1) +pico_add_extra_outputs(test) +target_link_libraries(test pico_stdlib hardware_spi hardware_i2c) + +add_custom_target(Flash + DEPENDS test + COMMAND sudo picotool load ${PROJECT_BINARY_DIR}/test.uf2 +) \ No newline at end of file diff --git a/test.c b/test.c new file mode 100644 index 0000000..f5029f8 --- /dev/null +++ b/test.c @@ -0,0 +1,121 @@ +#include +#include "pico/stdlib.h" +#include "hardware/gpio.h" +#include "hardware/spi.h" +#include "hardware/i2c.h" +#include "pico/binary_info.h" + + +const uint PIN_CS = 1; +const uint LED_PIN = 25; + +void init_gyro(void); +void read_gyro(void); +void read_gyro_i2c(); +void init_gyro_i2c(); + +static inline void cs_select() { + asm volatile("nop \n nop \n nop"); + gpio_put(PIN_CS, 0); // Active low + asm volatile("nop \n nop \n nop"); +} + +static inline void cs_deselect() { + asm volatile("nop \n nop \n nop"); + gpio_put(PIN_CS, 1); + asm volatile("nop \n nop \n nop"); +} + +int main() { + bi_decl(bi_program_description("This is a test binary.")); + bi_decl(bi_1pin_with_name(LED_PIN, "On-board LED")); + + stdio_init_all(); + + gpio_init(LED_PIN); + gpio_set_dir(LED_PIN, GPIO_OUT); + gpio_put(LED_PIN, 1); + + init_gyro(); + + while (1) { + /*gpio_put(LED_PIN, 0); + sleep_ms(251); + gpio_put(LED_PIN, 1); + puts("Bonjour"); + sleep_ms(1000);*/ + sleep_ms(1000); + read_gyro(); + } +} + +void init_gyro_i2c(){ + gpio_set_function(0, GPIO_FUNC_I2C); // SDA + gpio_set_function(1, GPIO_FUNC_I2C); // SCL + i2c_init(i2c0, 40 * 1000); +} + +void read_gyro_i2c(){ + uint8_t tampon[10]=""; + uint8_t reg = 0x0F; + int rep; + uint8_t adresse_7bits = 0b1101011; + + // Envoi adresse registre à lire + rep = i2c_write_blocking(i2c0, adresse_7bits, ®, 1, true ); + if(rep == PICO_ERROR_GENERIC){ + puts("Erreur I2C"); + }else{ + puts("envoi OK"); + } + + // Lecture identification + rep = i2c_read_blocking(i2c0, adresse_7bits, tampon, 1, false ); + if(rep == PICO_ERROR_GENERIC){ + puts("Erreur I2C"); + }else{ + puts("Reception OK"); + puts(tampon); + } +} + +void init_gyro(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(); + + spi_init(spi0, 100 * 1000); // SPI init @ 100 kHZ + + //Ç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); +} +void read_gyro(void){ + uint8_t tampon[10]=""; + uint8_t reg[2] = {0x0F | 0x80 | 0x40, '\0'}; + int nb_recu; + // Lire l'adresse d'identification + // WHO_AM_I : 0x0F + cs_select(); + puts("Envoi"); + + spi_write_blocking(spi0, reg, 1); + // Doit répondre : 0b1101 0111 + puts(reg); + puts("Lecture"); + sleep_ms(10); + + nb_recu = spi_read_blocking(spi0, 0x55, tampon, 1); + tampon[nb_recu]='\0'; + puts(tampon); + cs_deselect(); + + + + +} \ No newline at end of file