stm-ledstrip : Driver and test routine for WS2811 RGB-LED
stm-ledstrip : Driver and test routine for WS2811 RGB-LED
#include "ws2812.h"
#include <stm32f4xx.h>
#include <stm32f4xx_rcc.h>
#include <stm32f4xx_gpio.h>
#include <stm32f4xx_tim.h>
#include <stm32f4xx_dma.h> static uint16_t PWM_Buffer[ PWM_BUFFER_SIZE ];
uint32_t frame_pos = ;
int incomplete_return = ; void Update_Buffer( uint16_t* buffer ); static void start_dma( void )
{
static DMA_InitTypeDef dma_init =
{ .DMA_BufferSize = PWM_BUFFER_SIZE, .DMA_Channel = DMA_Channel_5, .DMA_DIR =
DMA_DIR_MemoryToPeripheral, .DMA_FIFOMode = DMA_FIFOMode_Disable,
.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull, .DMA_Memory0BaseAddr =
(uint32_t) PWM_Buffer, .DMA_MemoryBurst = DMA_MemoryBurst_Single,
.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord, .DMA_MemoryInc =
DMA_MemoryInc_Enable, .DMA_Mode = DMA_Mode_Circular,
.DMA_PeripheralBaseAddr = (uint32_t) &TIM3->CCR4, .DMA_PeripheralBurst =
DMA_PeripheralBurst_Single, .DMA_PeripheralDataSize =
DMA_PeripheralDataSize_HalfWord, .DMA_PeripheralInc =
DMA_PeripheralInc_Disable, .DMA_Priority = DMA_Priority_Medium }; DMA_Init( DMA1_Stream2, &dma_init );
DMA_Cmd( DMA1_Stream2, ENABLE );
TIM_DMACmd( TIM3, TIM_DMA_CC4, ENABLE );
} static void init_buffers( void )
{
for ( int i = ; i < PWM_BUFFER_SIZE; i++ )
{
PWM_Buffer[ i ] = ;
}
for ( int i = ; i < FRAMEBUFFER_SIZE; i++ )
{
ws2812_framebuffer[ i ].red = ;
ws2812_framebuffer[ i ].green = ;
ws2812_framebuffer[ i ].blue = ;
}
} void ws2812_init( )
{
init_buffers( ); //InitStructures...
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBase_InitStructure;
TIM_OCInitTypeDef TIM_OC_InitStructure;
NVIC_InitTypeDef nvic_init; //Clock für GPIO setzen
RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOC, ENABLE ); //Clock für TIM4 setzen
RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM3, ENABLE ); //GPIO_PIN konfigurieren GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init( GPIOC, &GPIO_InitStructure ); //GPIO Alternate function verbinden
GPIO_PinAFConfig( GPIOC, GPIO_PinSource9, GPIO_AF_TIM3 ); TIM_TimeBase_InitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBase_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBase_InitStructure.TIM_Period = ;
TIM_TimeBase_InitStructure.TIM_Prescaler = ;
TIM_TimeBaseInit( TIM3, &TIM_TimeBase_InitStructure ); TIM_OC_InitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OC_InitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OC_InitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Set;
TIM_OC_InitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC_InitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OC_InitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OC_InitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
TIM_OC_InitStructure.TIM_Pulse = ;
TIM_OC4Init( TIM3, &TIM_OC_InitStructure ); TIM_CtrlPWMOutputs( TIM3, ENABLE ); TIM_OC4PreloadConfig( TIM3, TIM_OCPreload_Enable );
TIM_ARRPreloadConfig( TIM3, ENABLE ); TIM_CCxCmd( TIM3, TIM_Channel_4, TIM_CCx_Enable );
TIM_Cmd( TIM3, ENABLE ); // DMA
RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_DMA1, ENABLE );
TIM_DMACmd( TIM3, TIM_DMA_CC4, ENABLE );
DMA_ITConfig( DMA1_Stream2, DMA_IT_HT, ENABLE );
DMA_ITConfig( DMA1_Stream2, DMA_IT_TC, ENABLE ); start_dma( ); // NVIC for DMA
nvic_init.NVIC_IRQChannel = DMA1_Stream2_IRQn;
nvic_init.NVIC_IRQChannelPreemptionPriority = ;
nvic_init.NVIC_IRQChannelSubPriority = ;
nvic_init.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init( &nvic_init );
} // writes the pwm values of one byte into the array which will be used by the dma
static inline void color2pwm( uint16_t ** const dest, const uint8_t color )
{
uint8_t mask = 0x80; do
{
if ( color & mask )
{
* *dest = ;
}
else
{
* *dest = ;
}
*dest += ;
mask >>= ;
}while ( mask != );
} void Update_Buffer( uint16_t* buffer )
{
struct led *framebufferp;
uint32_t i, j;
uint16_t * bufp; for ( i = ; i < ( PWM_BUFFER_SIZE / ) / ; i++ )
{
if ( incomplete_return )
{
incomplete_return = ;
for ( j = ; j < ; j++ )
{
buffer[ i * + j ] = ;
} }
else
{
if ( frame_pos == FRAMEBUFFER_SIZE )
{
incomplete_return = ;
frame_pos = ; for ( j = ; j < ; j++ )
{
buffer[ i * + j ] = ;
}
}
else
{
framebufferp = &ws2812_framebuffer[ frame_pos++ ];
bufp = buffer + ( i * ); // edit here to change order of colors in "ws2812_framebuffer" (0x00RRGGBB, 0x00GGBBRR, etc)
// the chip needs G R B
color2pwm( &bufp, framebufferp->green ); // green
color2pwm( &bufp, framebufferp->red ); // red
color2pwm( &bufp, framebufferp->blue ); // blue
}
}
}
} void DMA1_Stream2_IRQHandler( void )
{
// Half-Transfer completed
if ( DMA_GetITStatus( DMA1_Stream2, DMA_IT_HTIF2 ) )
{
DMA_ClearITPendingBit( DMA1_Stream2, DMA_IT_HTIF2 );
Update_Buffer( PWM_Buffer );
} // Transfer completed
if ( DMA_GetITStatus( DMA1_Stream2, DMA_IT_TCIF2 ) )
{
DMA_ClearITPendingBit( DMA1_Stream2, DMA_IT_TCIF2 );
Update_Buffer( PWM_Buffer + ( PWM_BUFFER_SIZE / ) );
} }
stm-ledstrip : Driver and test routine for WS2811 RGB-LED的更多相关文章
- 0xWS2812 STM32 driver for WS2812(B) RGB LEDs
0xWS2812 STM32 driver for WS2812(B) RGB LEDs 0xWS2812 pronounced "hex-WS2812" This code ai ...
- kernel/printk.c
/* * linux/kernel/printk.c * * Copyright (C) 1991, 1992 Linus Torvalds * * Modified to make sys_s ...
- 关于IoCallDriver
通常我们所知IoCallDriver是把irp传递给下一层设备,传递到底是什么意思呢?IoCallDriver中实际调用了IopfCallDriver,其代码如下:NTSTATUSFORCEINLIN ...
- IRP小结 0x01 IRP & IO_STACK_LOCATION(结合WRK理解)
写博客整理记录一下IRP相关的知识点,加深一下印象. 所有的I/O请求都是以IRP的形式提交的.当I/O管理器为了响应某个线程调用的的I/O API的时候,就会构造一个IRP,用于在I/O系统处理这个 ...
- tiny4412 串口驱动分析七 --- log打印的几个阶段之内核启动阶段(earlyprintk)
作者:彭东林 邮箱:pengdonglin137@163.com 开发板:tiny4412ADK+S700 4GB Flash 主机:Wind7 64位 虚拟机:Vmware+Ubuntu12_04 ...
- linux内核中mtd架构分析
一. 引言 MTD(memory technology device内存技术设备)是用于访问memory设备(RAM.ROM.flash)的Linux的子系统.MTD的主要目的是为了使新的memory ...
- Device Drivers
Types of Device Drivers Windows可能会有User-mode的驱动,但是我们只关注Kernel-Mode的驱动. WDM Drivers WDM是一种驱动模型,是比较常用的 ...
- linux内核更新前后配置文件的比较
说明:这里先给出一个比较的结果,作为记录,后续会给出内核配置差异的详细解释. [root@xiaolyu linux-4.7.2]# diff .config .config_bak 3c3< ...
- linux设备驱动归纳总结(九):1.platform总线的设备和驱动【转】
本文转载自:http://blog.chinaunix.net/uid-25014876-id-111745.html linux设备驱动归纳总结(九):1.platform总线的设备和驱动 xxxx ...
随机推荐
- auto
把左和右外边距设置为 auto,规定的是均等地分配可用的外边距.结果就是居中的元素: <style> .centerrr { margin:auto; width:70%; backgro ...
- 蓝桥杯 问题 1110: 2^k进制数 (排列组合+高精度巧妙处理)
题目链接 题目描述 设r是个2^k 进制数,并满足以下条件: (1)r至少是个2位的2^k 进制数. (2)作为2^k 进制数,除最后一位外,r的每一位严格小于它右边相邻的那一位. (3)将r转换为2 ...
- H - Tickets dp
题目链接: https://cn.vjudge.net/contest/68966#problem/H AC代码; #include<iostream> #include<strin ...
- Git常见错误处理
如果输入$ Git remote add origin git@github.com:djqiang(github帐号名)/gitdemo(项目名).git 提示出错信息:fatal: remo ...
- Spring中构造器、init-method、@PostConstruct、afterPropertiesSet孰先孰后,自动注入发生时间以及单例多例的区别、SSH线程安全问题
首先明白,spring的IOC功能需要是利用反射原理,反射获取类的无参构造方法创建对象,如果一个类没有无参的构造方法spring是不会创建对象的.在这里需要提醒一下,如果我们在class中没有显示的声 ...
- linux常用运维命令【转】
自己的小网站跑在阿里云的ECS上面,偶尔也去分析分析自己网站服务器日志,看看网站的访问量.看看有没有黑阔搞破坏!于是收集,整理一些服务器日志分析命令,大家可以试试! 1.查看有多少个IP访问: awk ...
- 走进异步编程的世界--async/await项目使用实战
起因:今天要做一个定时器任务:五分钟查询一次数据库发现超时未支付的订单数据将其状态改为已经关闭(数据量大约100条的情况) 开始未使用异步: public void SelfCloseGpPayOrd ...
- 解决windows10 里vs2017 直接开始执行提示“此任务要求应用程序有提升的权限”1.
1.打开vs的安装路径,我的是 C:\Program Files (x86)\Common Files\Microsoft Shared\MSEnv\ ,找到 VSLauncher.exe 右击该文 ...
- tomcat -> 简介&部署
Tomcat 简介 Tomcat是Apache软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache.Sun和其他一些公司及个人共同开 ...
- HighCharts、EChart图表X轴纵向显示
HighCharts 回调javascript函数来格式化标签,值通过this.value获得,this的其他属性还包括axis, chart, isFirst and isLast. 默认为: fu ...