Cours:PiPico : Différence entre versions
(→GPIO) |
(→adc) |
||
| (19 révisions intermédiaires par le même utilisateur non affichées) | |||
| Ligne 3 : | Ligne 3 : | ||
https://datasheets.raspberrypi.com/pico/Pico-R3-A4-Pinout.pdf | https://datasheets.raspberrypi.com/pico/Pico-R3-A4-Pinout.pdf | ||
| − | = | + | =utilisation de l'ide arduino= |
| + | |||
| + | https://github.com/earlephilhower/arduino-pico | ||
| + | |||
| + | |||
| + | =non recommandé : QtCreator= | ||
== Création assistée d'un projet == | == Création assistée d'un projet == | ||
| Ligne 14 : | Ligne 19 : | ||
Attention, ne pas oublier de spécifier le chemin vers le SDK : | Attention, ne pas oublier de spécifier le chemin vers le SDK : | ||
* ''export PICO_SDK_PATH=../../pico-sdk'' en remplaçant ''../../pico-sdk'' par le chemin vers le SDK. | * ''export PICO_SDK_PATH=../../pico-sdk'' en remplaçant ''../../pico-sdk'' par le chemin vers le SDK. | ||
| − | |||
| − | |||
| − | |||
| − | |||
== Ouverture avec QTCreator == | == Ouverture avec QTCreator == | ||
| Ligne 24 : | Ligne 25 : | ||
* d'ouvrir (comme projet) le fichier ''CMakeLists.txt'' | * d'ouvrir (comme projet) le fichier ''CMakeLists.txt'' | ||
| − | =GPIO= | + | =VsCode= |
| + | |||
| + | *installer VsCode | ||
| + | **https://doc.ubuntu-fr.org/visual_studio_code | ||
| + | *ajouter le plugin pi pico | ||
| + | **https://github.com/raspberrypi/pico-vscode | ||
| + | **https://datasheets.raspberrypi.com/pico/getting-started-with-pico.pdf | ||
| + | |||
| + | =Registres= | ||
| + | |||
| + | ==gpio== | ||
| + | <source lang=cpp> | ||
| + | #include "pico/stdlib.h" | ||
| + | #include "hardware/regs/addressmap.h" | ||
| + | |||
| + | unsigned int& gpioOUT = *(unsigned int*) (SIO_BASE + 0x10); | ||
| + | unsigned int& gpioOE = *(unsigned int*) (SIO_BASE + 0x20); | ||
| + | |||
| + | unsigned int& gpioCtrl0 = *(unsigned int *) (IO_BANK0_BASE+0x4); | ||
| + | //facultatif | ||
| + | unsigned int& padsBank0_GPIO0 = *(unsigned int *) (PADS_BANK0_BASE+0x4); | ||
| + | |||
| + | |||
| + | int main() { | ||
| + | // facultatif | ||
| + | padsBank0_GPIO0|=(1<<6); // input enable | ||
| + | padsBank0_GPIO0&=~(1<<7); // output disable | ||
| + | // piloter la broche avec fonction SIO | ||
| + | gpioCtrl0=5; | ||
| + | // sortie enable | ||
| + | gpioOE=1<<0; | ||
| + | while (true) | ||
| + | { | ||
| + | gpioOUT&=~(1<<0); | ||
| + | sleep_ms(200); | ||
| + | gpioOUT|=(1<<0); | ||
| + | sleep_ms(10); | ||
| + | } | ||
| + | } | ||
| + | </source> | ||
| + | |||
| + | =usage des périphériques au travers du sdk= | ||
| + | |||
| + | ==GPIO== | ||
https://www.raspberrypi.com/documentation/pico-sdk/hardware.html#hardware_gpio | https://www.raspberrypi.com/documentation/pico-sdk/hardware.html#hardware_gpio | ||
| Ligne 44 : | Ligne 88 : | ||
*gpio_set_pulls (uint gpio, bool up, bool down) | *gpio_set_pulls (uint gpio, bool up, bool down) | ||
| − | =Pwm= | + | ==Pwm== |
| + | |||
| + | https://raspberry-pi.developpez.com/actu/344283/Raspberry-Pi-Pico-moins-Apprendre-a-generer-des-signaux-PWM-un-billet-blog-de-f-leb/ | ||
| + | |||
| + | Fmli = Fcpu / ( wrap * clkdiv ) // sur pi pico, Fcpu=125MHz | ||
| + | |||
| + | *clkdiv: | ||
| + | **Le registre qui stocke le diviseur de fréquence comprend 8 bits pour la partie entière, et 4 bits pour la partie fractionnaire | ||
| + | **valeur max de clkdiv : 255 + 15/16 | ||
| + | **ex : pwm_set_clkdiv_int_frac (slice, 38, 3); // diviseur de fréquence = 38 + 3/16 | ||
*gpio_set_function(PICO_DEFAULT_LED_PIN, GPIO_FUNC_PWM); | *gpio_set_function(PICO_DEFAULT_LED_PIN, GPIO_FUNC_PWM); | ||
| Ligne 53 : | Ligne 106 : | ||
*pwm_set_gpio_level(PICO_DEFAULT_LED_PIN, fade * fade); // rapport cyclique 16 bits | *pwm_set_gpio_level(PICO_DEFAULT_LED_PIN, fade * fade); // rapport cyclique 16 bits | ||
| + | ==I2C== | ||
| + | |||
| + | ===i2c scanner=== | ||
| + | |||
| + | <source lang=cpp> | ||
| + | #include <stdio.h> | ||
| + | #include "pico/stdlib.h" | ||
| + | #include "hardware/i2c.h" | ||
| + | #include "hardware/timer.h" | ||
| + | #include "hardware/clocks.h" | ||
| + | #include "pico/binary_info.h" | ||
| + | |||
| + | |||
| + | int64_t alarm_callback(alarm_id_t id, void *user_data) { | ||
| + | // Put your timeout handler code in here | ||
| + | return 0; | ||
| + | } | ||
| + | |||
| + | |||
| + | |||
| + | bool reserved_addr(uint8_t addr) { | ||
| + | return (addr & 0x78) == 0 || (addr & 0x78) == 0x78; | ||
| + | } | ||
| + | int main() | ||
| + | { | ||
| + | stdio_init_all(); | ||
| + | |||
| + | // I2C Initialisation. Using it at 400Khz. | ||
| + | i2c_init(i2c0, 100*1000); | ||
| + | |||
| + | gpio_set_function(PICO_DEFAULT_I2C_SDA_PIN, GPIO_FUNC_I2C); | ||
| + | gpio_set_function(PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C); | ||
| + | gpio_pull_up(PICO_DEFAULT_I2C_SDA_PIN); | ||
| + | gpio_pull_up(PICO_DEFAULT_I2C_SCL_PIN); | ||
| + | // For more examples of I2C use see https://github.com/raspberrypi/pico-examples/tree/master/i2c | ||
| + | bi_decl(bi_2pins_with_func(PICO_DEFAULT_I2C_SDA_PIN, PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C)); | ||
| + | |||
| + | // Timer example code - This example fires off the callback after 2000ms | ||
| + | add_alarm_in_ms(2000, alarm_callback, NULL, false); | ||
| + | // For more examples of timer use see https://github.com/raspberrypi/pico-examples/tree/master/timer | ||
| + | |||
| + | printf("System Clock Frequency is %d Hz\n", clock_get_hz(clk_sys)); | ||
| + | printf("USB Clock Frequency is %d Hz\n", clock_get_hz(clk_usb)); | ||
| + | // For more examples of clocks use see https://github.com/raspberrypi/pico-examples/tree/master/clocks | ||
| + | |||
| + | while (true) { | ||
| + | printf("\nI2C Bus Scan\n"); | ||
| + | printf(" 0 1 2 3 4 5 6 7 8 9 A B C D E F\n"); | ||
| + | |||
| + | for (int addr = 0; addr < (1 << 7); ++addr) { | ||
| + | if (addr % 16 == 0) { | ||
| + | printf("%02x ", addr); | ||
| + | } | ||
| + | |||
| + | // Perform a 1-byte dummy read from the probe address. If a slave | ||
| + | // acknowledges this address, the function returns the number of bytes | ||
| + | // transferred. If the address byte is ignored, the function returns | ||
| + | // -1. | ||
| + | |||
| + | // Skip over any reserved addresses. | ||
| + | int ret; | ||
| + | uint8_t rxdata; | ||
| + | if (reserved_addr(addr)) | ||
| + | ret = PICO_ERROR_GENERIC; | ||
| + | else | ||
| + | ret = i2c_read_blocking(i2c_default, addr, &rxdata, 1, false); | ||
| + | |||
| + | printf(ret < 0 ? "." : "@"); | ||
| + | printf(addr % 16 == 15 ? "\n" : " "); | ||
| + | } | ||
| + | printf("Done.\n"); | ||
| + | |||
| + | sleep_ms(1000); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | </source> | ||
| + | |||
| + | ===read/write register=== | ||
| + | |||
| + | <source lang=cpp> | ||
| + | void writeI2cReg(uint8_t targetAddress, uint8_t regAddress, uint8_t regValue) | ||
| + | { | ||
| + | uint8_t data[2]={regAddress,regValue}; | ||
| + | i2c_write_blocking(i2c0,targetAddress,data,2,false); | ||
| + | } | ||
| + | |||
| + | uint8_t readI2cReg(uint8_t targetAddress, uint8_t regAddress) | ||
| + | { | ||
| + | i2c_write_blocking(i2c0,targetAddress,®Address,1,true); | ||
| + | uint8_t regValue; | ||
| + | i2c_read_blocking(i2c0,targetAddress,®Value,1,false); | ||
| + | return regValue; | ||
| + | } | ||
| + | </source> | ||
| + | |||
| + | ==adc== | ||
| + | |||
| + | ==uart== | ||
| + | *https://www.raspberrypi.com/documentation/pico-sdk/hardware.html#group_hardware_uart | ||
| + | |||
| + | ==pio== | ||
| + | |||
| + | ===ws2812=== | ||
| + | https://github.com/ForsakenNGS/PicoLED | ||
=liens= | =liens= | ||
Version actuelle datée du 27 avril 2025 à 20:42
Sommaire
Brochage
https://datasheets.raspberrypi.com/pico/Pico-R3-A4-Pinout.pdf
utilisation de l'ide arduino
https://github.com/earlephilhower/arduino-pico
non recommandé : QtCreator
Création assistée d'un projet
À l'aide de pico_project.py :
- git clone https://github.com/raspberrypi/pico-project-generator.git
- ./pico_project.py --gui
- ne pas cocher "create VSCode project"
Attention, ne pas oublier de spécifier le chemin vers le SDK :
- export PICO_SDK_PATH=../../pico-sdk en remplaçant ../../pico-sdk par le chemin vers le SDK.
Ouverture avec QTCreator
QtCreator est capable de lire et interpréter le fichier CMakeLists.txt comme descripteur de projet, il suffit donc
- d'ouvrir (comme projet) le fichier CMakeLists.txt
VsCode
- installer VsCode
- ajouter le plugin pi pico
Registres
gpio
#include "pico/stdlib.h"
#include "hardware/regs/addressmap.h"
unsigned int& gpioOUT = *(unsigned int*) (SIO_BASE + 0x10);
unsigned int& gpioOE = *(unsigned int*) (SIO_BASE + 0x20);
unsigned int& gpioCtrl0 = *(unsigned int *) (IO_BANK0_BASE+0x4);
//facultatif
unsigned int& padsBank0_GPIO0 = *(unsigned int *) (PADS_BANK0_BASE+0x4);
int main() {
// facultatif
padsBank0_GPIO0|=(1<<6); // input enable
padsBank0_GPIO0&=~(1<<7); // output disable
// piloter la broche avec fonction SIO
gpioCtrl0=5;
// sortie enable
gpioOE=1<<0;
while (true)
{
gpioOUT&=~(1<<0);
sleep_ms(200);
gpioOUT|=(1<<0);
sleep_ms(10);
}
}
usage des périphériques au travers du sdk
GPIO
https://www.raspberrypi.com/documentation/pico-sdk/hardware.html#hardware_gpio
| Syntaxe | void gpio_init(uint gpio) | ||
|---|---|---|---|
| Paramètres |
|
- gpio_put(LED_PIN, 1);
- gpio_set_dir(LED_PIN, GPIO_OUT);
- gpio_set_pulls (uint gpio, bool up, bool down)
Pwm
Fmli = Fcpu / ( wrap * clkdiv ) // sur pi pico, Fcpu=125MHz
- clkdiv:
- Le registre qui stocke le diviseur de fréquence comprend 8 bits pour la partie entière, et 4 bits pour la partie fractionnaire
- valeur max de clkdiv : 255 + 15/16
- ex : pwm_set_clkdiv_int_frac (slice, 38, 3); // diviseur de fréquence = 38 + 3/16
- gpio_set_function(PICO_DEFAULT_LED_PIN, GPIO_FUNC_PWM);
- uint slice_num = pwm_gpio_to_slice_num(PICO_DEFAULT_LED_PIN);
- pwm_config config = pwm_get_default_config();
- pwm_config_set_clkdiv(&config, 4.f);
- pwm_init(slice_num, &config, true);
- pwm_set_gpio_level(PICO_DEFAULT_LED_PIN, fade * fade); // rapport cyclique 16 bits
I2C
i2c scanner
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/i2c.h"
#include "hardware/timer.h"
#include "hardware/clocks.h"
#include "pico/binary_info.h"
int64_t alarm_callback(alarm_id_t id, void *user_data) {
// Put your timeout handler code in here
return 0;
}
bool reserved_addr(uint8_t addr) {
return (addr & 0x78) == 0 || (addr & 0x78) == 0x78;
}
int main()
{
stdio_init_all();
// I2C Initialisation. Using it at 400Khz.
i2c_init(i2c0, 100*1000);
gpio_set_function(PICO_DEFAULT_I2C_SDA_PIN, GPIO_FUNC_I2C);
gpio_set_function(PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C);
gpio_pull_up(PICO_DEFAULT_I2C_SDA_PIN);
gpio_pull_up(PICO_DEFAULT_I2C_SCL_PIN);
// For more examples of I2C use see https://github.com/raspberrypi/pico-examples/tree/master/i2c
bi_decl(bi_2pins_with_func(PICO_DEFAULT_I2C_SDA_PIN, PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C));
// Timer example code - This example fires off the callback after 2000ms
add_alarm_in_ms(2000, alarm_callback, NULL, false);
// For more examples of timer use see https://github.com/raspberrypi/pico-examples/tree/master/timer
printf("System Clock Frequency is %d Hz\n", clock_get_hz(clk_sys));
printf("USB Clock Frequency is %d Hz\n", clock_get_hz(clk_usb));
// For more examples of clocks use see https://github.com/raspberrypi/pico-examples/tree/master/clocks
while (true) {
printf("\nI2C Bus Scan\n");
printf(" 0 1 2 3 4 5 6 7 8 9 A B C D E F\n");
for (int addr = 0; addr < (1 << 7); ++addr) {
if (addr % 16 == 0) {
printf("%02x ", addr);
}
// Perform a 1-byte dummy read from the probe address. If a slave
// acknowledges this address, the function returns the number of bytes
// transferred. If the address byte is ignored, the function returns
// -1.
// Skip over any reserved addresses.
int ret;
uint8_t rxdata;
if (reserved_addr(addr))
ret = PICO_ERROR_GENERIC;
else
ret = i2c_read_blocking(i2c_default, addr, &rxdata, 1, false);
printf(ret < 0 ? "." : "@");
printf(addr % 16 == 15 ? "\n" : " ");
}
printf("Done.\n");
sleep_ms(1000);
}
}
read/write register
void writeI2cReg(uint8_t targetAddress, uint8_t regAddress, uint8_t regValue)
{
uint8_t data[2]={regAddress,regValue};
i2c_write_blocking(i2c0,targetAddress,data,2,false);
}
uint8_t readI2cReg(uint8_t targetAddress, uint8_t regAddress)
{
i2c_write_blocking(i2c0,targetAddress,®Address,1,true);
uint8_t regValue;
i2c_read_blocking(i2c0,targetAddress,®Value,1,false);
return regValue;
}
adc
uart
pio
ws2812
https://github.com/ForsakenNGS/PicoLED